#!BPY # 19 November, 2009 # Bindesh Kumar Singh # bindeshkumarsingh@gmail.com # www.bindesh.cjb.net # http://www.ourinnovativemind.blogspot.com """ Name: 'Mera m3d exporter' Blender: 249 Group: 'Export' Tooltip: 'Mera m3d exporter' """ import Blender import bpy def write(filename): out = file(filename,"w") sce = bpy.data.scenes.active obj = sce.objects.active mesh = obj.getData(mesh=1) mesh.quadToTriangle(0) # please be out of edit mode, sirf trikon hi stemaal honge numfaces = len( mesh.faces ) # total faces ( triangles ) numvertices = 0 # total vertices numtexcords = 0 # total texture cordinates texture_hai = 0 # Vertices list, dohrate hue maan surakchit nahi honge vertlist = [] for v in mesh.verts : if v not in vertlist: # dohrate hue maan ko delete karne hetu vertlist.append(v) # Texture cordinates list texcordlist = [] texture_hai = mesh.faceUV if texture_hai : # agar texture hai to dump kar do for face in mesh.faces : for uv in face.uv : if uv not in texcordlist: texcordlist.append(uv) # create materials list nummats = 0 matlist = [] for mat in mesh.materials : if mat not in matlist : matlist.append( mat.name ) # sare list ke maan ko gyat karo numvertices = len(vertlist) numtexcords = len(texcordlist) nummats = len( matlist ) if nummats == 0 : # disable texture if no material numtexcords = 0 texture_hai = 0 # dump total faces, total vertices and total texture cordinates out.write('nf %i\n' % (numfaces) ) out.write('nv %i\n' % (numvertices) ) out.write('nm %i\n' % (nummats) ) out.write('nt %i\n' % (numtexcords) ) # dump verts for vert in vertlist : out.write('v %f %f %f\n' % tuple(vert.co) ) # dump materials, in index order from 0 to nummats for mat in mesh.materials : #m [material_color R G B] [ambient] [specular] [diffuse] [emmision] material_name] matcol = mat.rgbCol out.write('m %f %f %f ' % (matcol[0], matcol[1], matcol[2]) ) out.write('%f %f %f %f %s\n' % (mat.amb, mat.spec, mat.ref, mat.emit, mat.name) ) # rgb maan hetu mat col se guna karo # e.g. amb_red = mat.amb x matcol[0] , amb_green = mat.amb x matcol[1], amb_blue = mat.amb x matcol[2] # dump UV if texture_hai : for uv in texcordlist : out.write('vt %f %f\n' % tuple(uv) ) i = 0 # vertices iterator t = 0 # UV iterator # dump vertices index for all triangles for face in mesh.faces : out.write('fv ') for v in face.verts : i = 0 while v != vertlist[i] and i < numvertices : i += 1 out.write('%i '%(i)) out.write('\n') # texture dump prarambh # dump uv index for all triangles if texture_hai : for face in mesh.faces : # format hai, ft uv_index material_index out.write('ft ') for uv in face.uv : t = 0 while uv != texcordlist[t] and t < numtexcords : t += 1 out.write('%i '%(t)) # uv index out.write('%i\n'%(face.mat)) # material index # dump texture files used teximagelist = [] for f in mesh.faces : if f.image and f.image.name not in teximagelist : teximagelist.append( f.image.name ) numtextures = len( teximagelist ) if numtextures > 0 : out.write('nt %i \n'%(numtextures)) # kitne textures stemaal hue hain # dump list of texture files for t in teximagelist : out.write('%s \n'%(t)) # dump list of texture files and corresponding face tid = 0 fid = 0 if numtextures == 1 : out.write('[ sirf_ek_texture_lagu_hai ]\n') else : for t in teximagelist : # ti texture_index face_indices ( used by this texture id ) out.write('ti %i ' % ( tid) ) fid = 0 # face count to zero for f in mesh.faces : if f.image.name != t : fid += 1 out.write( '%i ' % (fid) ) out.write('\n') tid += 1 # texture dump samapt out.write('$EOF') out.close() Blender.Window.FileSelector(write, "Export")