def visionary_export(filename = "start.ve"): global hFile print "-------------------------------------------------------" print " start\n" if filename.find('.ve', -3) <= 0: filename += '.ve' start = time.clock() # Try getting the names of all of the selected items in the scene sceneObjects = Blender.Object.GetSelected() if not sceneObjects: meshtools.print_boxed("No objects are selected.") return dumpBuffer = "" processingObjects = sceneObjects[:] while len(processingObjects): object = processingObjects[0] (outputBuffer, additionalObjects) = ParseObject( object ) print outputBuffer print additionalObjects processingObjects = processingObjects[1:] + additionalObjects end = time.clock() # Print status message seconds = " in %.2f %s" % (end-start, "seconds") message = "Successfully exported " + os.path.basename(filename) + seconds meshtools.print_boxed( message )
def visionary_export(filename="start.ve"): global hFile print "-------------------------------------------------------" print " start\n" if filename.find('.ve', -3) <= 0: filename += '.ve' start = time.clock() # Try getting the names of all of the selected items in the scene sceneObjects = Blender.Object.GetSelected() if not sceneObjects: meshtools.print_boxed("No objects are selected.") return dumpBuffer = "" processingObjects = sceneObjects[:] while len(processingObjects): object = processingObjects[0] (outputBuffer, additionalObjects) = ParseObject(object) print outputBuffer print additionalObjects processingObjects = processingObjects[1:] + additionalObjects end = time.clock() # Print status message seconds = " in %.2f %s" % (end - start, "seconds") message = "Successfully exported " + os.path.basename(filename) + seconds meshtools.print_boxed(message)
def read(filename): '''Read points in x, y, z format from file''' #start = time.clock() file = open(filename, "rb") objname = Blender.sys.splitext(Blender.sys.basename(filename))[0] me = NMesh.GetRaw() # Collect data from RAW format for line in file.readlines(): try: try: f1, f2, f3, f4 = map(float, line.split()) except: # raw triangle so take first three only #f1, f2, f3, f4, f5, f6, f7, f8, f9 = map(float, line.split()) f1, f2, f3 = map(float, line.split()) v = NMesh.Vert(f1, f2, f3) me.verts.append(v) except: continue NMesh.PutRaw(me, objname) Blender.Object.GetSelected()[0].name = objname Blender.Redraw() Blender.Window.DrawProgressBar(1.0, '') # clear progressbar file.close() #end = time.clock() #seconds = " in %.2f %s" % (end-start, "seconds") message = "Successfully imported " + Blender.sys.basename(filename)# + seconds meshtools.print_boxed(message)
def visionary_export(filename = "start.ve"): global hFile print "-------------------------------------------------------" print " start\n" if filename.find('.ve', -3) <= 0: filename += '.ve' start = time.clock() # Try getting the names of all of the selected items in the scene objects = Blender.Object.GetSelected() if not objects: meshtools.print_boxed("No objects are selected.") return objects.sort(lambda a,b: cmp(a.name,b.name)) # Call iterator and Parse Function it = iter(objects) parentInstance = ParseObjects( it.next(), it ) header = [] def addtoheader( name, dmp, size ): if size > 0: header.append( (name,GenerateHashValue(name),dmp,size) ) # Build the shared information (dmp, dsize) = PackSharedObjects() addtoheader("shared", dmp, dsize) # Build the object information (dmp, dsize) = PackObject( parentInstance, 0 ) addtoheader("render", dmp, dsize) if len(header) > 0: # Build the header and offsets (dump, size) = BuildFileHeader( header ) if len(dump) > 0: hFile = open( filename, 'wb' ) if hFile is not None: # Write to file hFile.write( dump ) # Do Clean up hFile.close() # End If # End If end = time.clock() # Print status message seconds = " in %.2f %s" % (end-start, "seconds") message = "Successfully exported " + os.path.basename(filename) + seconds meshtools.print_boxed( message )
def main(): print "-------------------------------------------------------" print " start (Practice.py)\n" start = time.clock() # Try getting the names of all of the selected items in the scene objects = Blender.Object.GetSelected() if not objects: meshtools.print_boxed("No objects are selected.") return objects.sort(lambda a,b: cmp(a.name,b.name)) def NumUsers( o ): try: return o.users except: return 0 global ObjectInstances # walk through list of selected objects and check for o in objects: print "%s, %s:\n\tdata: %s, %s, %s, [Users %s]" % ( o.name, type(o), o.data, o.getData(True), type(o.data), NumUsers(o.data) ) # o.getBoundBox(), o.getEuler(), o.getLocation(), o.getSize() if not o.data.name in ObjectInstances: ObjectInstances[o.data.name] = o.data # this will be the same so just add it? else: numUsers = NumUsers(o.data) if numUsers > 1: print "Found %s" % o.data.name elif numUsers: print "Error: %d, was used only once but found on list possible duplicate name!" % (o.data.name) # Create a list of verts by Material! if type(o.data) == Types.NMeshType: faceByMaterial = [[] for i in range(len(o.data.materials))] # create face listing by material! for face in o.data.faces: # print "material", face.mat faceByMaterial[face.mat].append(face) for (indx, faces) in enumerate(faceByMaterial): # print indx, o.data.materials[indx], len(faces) for face in faces: print face, # print end = time.clock() print # Print status message seconds = " in %.2f %s" % (end-start, "seconds") message = "Completed operation in " + seconds meshtools.print_boxed( message )
def filesel_callback(filename): if read(filename) == 0: print "Invalid file" return objname = Blender.sys.splitext(Blender.sys.basename(filename))[0] if not meshtools.overwrite_mesh_name: objname = meshtools.versioned_name(objname) #Blender.NMesh.PutRaw(mesh, objname, 1) Blender.Object.GetSelected()[0].name = objname Blender.Redraw() Blender.Window.DrawProgressBar(1.0, '') message = "Successfully imported " + filename meshtools.print_boxed(message)
def read(filename): #start = time.clock() file = open(filename, "rb") raw = [] for line in file: #.xreadlines(): data = line.split() if data[0] == "vertex": vert = map(float, data[1:]) raw.append(vert) tri = [] for i in xrange(0, len(raw), 3): tri.append(raw[i] + raw[i + 1] + raw[i + 2]) #$import pprint; pprint.pprint(tri) # Collect data from RAW format faces = [] for line in tri: f1, f2, f3, f4, f5, f6, f7, f8, f9 = line faces.append([(f1, f2, f3), (f4, f5, f6), (f7, f8, f9)]) # Generate verts and faces lists, without duplicates verts = [] coords = {} index = 0 for i in xrange(len(faces)): for j in xrange(len(faces[i])): vertex = faces[i][j] if not coords.has_key(vertex): coords[vertex] = index index += 1 verts.append(vertex) faces[i][j] = coords[vertex] objname = Blender.sys.splitext(Blender.sys.basename(filename))[0] meshtools.create_mesh(verts, faces, objname) Blender.Window.DrawProgressBar(1.0, '') # clear progressbar file.close() #end = time.clock() #seconds = " in %.2f %s" % (end-start, "seconds") message = "Successfully imported " + Blender.sys.basename( filename) # + seconds meshtools.print_boxed(message)
def read(filename): #start = time.clock() file = open(filename, "rb") raw = [] for line in file: #.xreadlines(): data = line.split() if data[0] == "vertex": vert = map(float, data[1:]) raw.append(vert) tri = [] for i in xrange(0, len(raw), 3): tri.append(raw[i] + raw[i+1] + raw[i+2]) #$import pprint; pprint.pprint(tri) # Collect data from RAW format faces = [] for line in tri: f1, f2, f3, f4, f5, f6, f7, f8, f9 = line faces.append([(f1, f2, f3), (f4, f5, f6), (f7, f8, f9)]) # Generate verts and faces lists, without duplicates verts = [] coords = {} index = 0 for i in xrange(len(faces)): for j in xrange(len(faces[i])): vertex = faces[i][j] if not coords.has_key(vertex): coords[vertex] = index index += 1 verts.append(vertex) faces[i][j] = coords[vertex] objname = Blender.sys.splitext(Blender.sys.basename(filename))[0] meshtools.create_mesh(verts, faces, objname) Blender.Window.DrawProgressBar(1.0, '') # clear progressbar file.close() #end = time.clock() #seconds = " in %.2f %s" % (end-start, "seconds") message = "Successfully imported " + Blender.sys.basename(filename)# + seconds meshtools.print_boxed(message)
if not (mesh.faces.index(face) % 4): # finish off the last one, if this isn't the first one if mesh.faces.index(face): data += ";\n" s.send(data) # start a new buffer data = objname + " " if len(face.v) == 3: # triangle v1, v2, v3 = face.v faceverts = tuple(v1.co) + tuple(v2.co) + tuple(v3.co) data += "%f %f %f %f %f %f %f %f %f " % faceverts else: # quadrilateral v1, v2, v3, v4 = face.v faceverts1 = tuple(v1.co) + tuple(v2.co) + tuple(v3.co) faceverts2 = tuple(v3.co) + tuple(v4.co) + tuple(v1.co) data += "%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f " % faceverts1 data += "%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f " % faceverts2 # finish off the very last one data += ";\n" s.send(data) Blender.Window.DrawProgressBar(1.0, '') # clear progressbar s.close() message = "Successfully sent " + objname meshtools.print_boxed(message)
# send four faces at a time until we're done if not (mesh.faces.index(face) % 4): # finish off the last one, if this isn't the first one if mesh.faces.index(face): data += ";\n" s.send(data) # start a new buffer data = objname + " " if len(face.v) == 3: # triangle v1, v2, v3 = face.v faceverts = tuple(v1.co) + tuple(v2.co) + tuple(v3.co) data += "%f %f %f %f %f %f %f %f %f " % faceverts else: # quadrilateral v1, v2, v3, v4 = face.v faceverts1 = tuple(v1.co) + tuple(v2.co) + tuple(v3.co) faceverts2 = tuple(v3.co) + tuple(v4.co) + tuple(v1.co) data += "%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f " % faceverts1 data += "%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f " % faceverts2 # finish off the very last one data += ";\n" s.send(data) Blender.Window.DrawProgressBar(1.0, '') # clear progressbar s.close() message = "Successfully sent " + objname meshtools.print_boxed(message)
def main(): print "-------------------------------------------------------" print " start (Practice.py)\n" start = time.clock() # Try getting the names of all of the selected items in the scene objects = Blender.Object.GetSelected() if not objects: meshtools.print_boxed("No objects are selected.") return objects.sort(lambda a, b: cmp(a.name, b.name)) def NumUsers(o): try: return o.users except: return 0 global ObjectInstances # walk through list of selected objects and check for o in objects: print "%s, %s:\n\tdata: %s, %s, %s, [Users %s]" % (o.name, type( o), o.data, o.getData(True), type(o.data), NumUsers(o.data)) # o.getBoundBox(), o.getEuler(), o.getLocation(), o.getSize() if not o.data.name in ObjectInstances: ObjectInstances[ o.data.name] = o.data # this will be the same so just add it? else: numUsers = NumUsers(o.data) if numUsers > 1: print "Found %s" % o.data.name elif numUsers: print "Error: %d, was used only once but found on list possible duplicate name!" % ( o.data.name) # Create a list of verts by Material! if type(o.data) == Types.NMeshType: faceByMaterial = [[] for i in range(len(o.data.materials))] # create face listing by material! for face in o.data.faces: # print "material", face.mat faceByMaterial[face.mat].append(face) for (indx, faces) in enumerate(faceByMaterial): # print indx, o.data.materials[indx], len(faces) for face in faces: print face, # print end = time.clock() print # Print status message seconds = " in %.2f %s" % (end - start, "seconds") message = "Completed operation in " + seconds meshtools.print_boxed(message)
def write(filename): global showProgress print showProgress start = time.clock() convertToTris = 0 if Blender.sys.exists(filename) and ( Blender.Draw.PupMenu("File exists, replace?%t|YES|NO") == 2): fileSelect() return file = open(filename, "wb") # ms3d header file.write("// MilkShape 3D ASCII\n\nFrames: 30\nFrame: 1\n\n") file.write("Meshes: " + str(len(Blender.Object.GetSelected())) + '\n') objects = Blender.Object.GetSelected() for obj in range(len(objects)): meshname = objects[obj].data.name mesh = Blender.NMesh.GetRaw(meshname) thismesh = meshname if dbg: print "\nDEBUG: Mesh", mesh, "Obj: ", obj, "Name: ", objects[ obj].data.name if mesh: print "Mesh", objects[obj].data.name, meshname = "\"" + objects[obj].data.name + "\" 0 " + str( obj ) + "\n" # Setting flags=0, mat index=object number for now, also note that I'm using object name, not meshname file.write(meshname) file.write( str(3 * len(mesh.faces)) + '\n' ) # should be better way to get # of verts, but this is fast for i in range( len(mesh.faces )): # New in v 0.3 - getting verts from faces directly faceVerts = len( mesh.faces[i].v ) # Reversing the winding to fix flipped normals problem for fv in range(faceVerts): # Scan through each vert in face line = '0 ' + str( -round(mesh.faces[i].v[faceVerts - fv - 1][0], 4) ) + ' ' + str( round(mesh.faces[i].v[faceVerts - fv - 1][2], 4) ) + ' ' + str( round(-mesh.faces[i].v[faceVerts - fv - 1][1], 4) ) + ' ' #+ str(round(mesh.faces[i].uv[fv][0],5)) + ' ' + str(round(mesh.faces[i].uv[fv][1],5)) + ' -1\n' if (len(mesh.faces[i].uv)): line += str( round(mesh.faces[i].uv[faceVerts - fv - 1][0], 5) ) + ' ' + str( round(1 - mesh.faces[i].uv[faceVerts - fv - 1][1], 5)) + ' -1\n' else: line += '0.0 0.0 -1\n' if not i % 50 and showProgress.val: Blender.Window.DrawProgressBar( float(i) / len(mesh.faces), thismesh + "Verts") file.write(line) # Normals #print "Norms: ",len(mesh.faces), #FIX! Temp only using # of faces file.write(str(3 * len(mesh.faces))) for i in range(len(mesh.faces)): if not i % 50 and showProgress.val: Blender.Window.DrawProgressBar( float(i) / len(mesh.faces), thismesh + "Normals") #file.write('\n' + str(round(mesh.faces[i].no[0],4)) + ' ' + str(round(mesh.faces[i].no[2],4)) + ' ' + str(round(mesh.faces[i].no[1],4))) faceVerts = len(mesh.faces[i].v) for fv in range(faceVerts): # Scan through each vert in face # Check if the face is smoothed or not: if (mesh.faces[i].smooth): file.write('\n' + str( round(-mesh.faces[i].v[faceVerts - fv - 1].no[0], 4))) file.write(' ' + str( round(mesh.faces[i].v[faceVerts - fv - 1].no[2], 4))) file.write(' ' + str( round(-mesh.faces[i].v[faceVerts - fv - 1].no[1], 4))) else: file.write('\n' + str(round(-mesh.faces[i].no[0], 4)) + ' ' + str(round(mesh.faces[i].no[2], 4)) + ' ' + str(round(-mesh.faces[i].no[1], 4))) # Faces print "Triangles: ", len(mesh.faces) file.write('\n' + str(len(mesh.faces)) + '\n') idx = 0 line = "" for i in range(len(mesh.faces)): # one face at a time if not i % 50 and showProgress.val: Blender.Window.DrawProgressBar( float(i) / len(mesh.faces), thismesh + "Triangles") if (len(mesh.faces[i].v) != 3): #Ensure each face is a triangle! print "\n\n*********** Error! **********\n" print "Object", objects[obj].data.name, "has", len( mesh.faces[i].v ), "vertices in face", i, "Must have THREE verts per face - convert to triangles with CTRL-T!" Blender.Window.WaitCursor(0) exitmsg = 'MS3D Export Error:|Mesh \"' + objects[ obj].data.name + '\" has ' + str( len(mesh.faces[i].v) ) + ' verts in a face, needs to be converted to triangles with CTRL-T!' Blender.Draw.PupMenu(exitmsg) return ######## Keeping depricated (and broken) tri2quad for future reference #if convertToTris==0: #only ask once # print "Object",objects[obj].data.name, "has", len(mesh.faces[i].v), "vertices in face", i, "Must have THREE verts per face" # convertToTris=Blender.Draw.PupMenu("Model not made entirely out of Triangles-Convert?%t|YES|NO") #if convertToTris==1: # for f in mesh.faces: # f.sel = 1 # Blender.Mesh.Mode(3) # mesh.quadToTriangle(0) #elif convertToTris==2: # exitmsg = 'MS3D Export Error:|Mesh \"' + objects[obj].data.name + '\" has ' + str(len(mesh.faces[i].v)) + ' verts in a face, needs to be converted to triangles with CTRL-T!' # Blender.Draw.PupMenu(exitmsg) # return for v in range( len(mesh.faces[i].v) ): #Go through each vertex in face - only need triangles for MS3D, but this would allow for quads too for portability line += str(idx) + " " idx += 1 #print cnt #debug info #file.write('1 ' + line + str( i) + ' ' + str(i) + ' ' + str(i) + ' 1\n') file.write('1 ' + line + line + ' 1\n') line = "" # fresh start for next face ######### Materials! ########## numMats = 0 for obj in range( len(objects)): #FIXME! Stupid cludge for counting materials meshname = objects[obj].data.name mesh = Blender.NMesh.GetRaw(meshname) if (len(mesh.materials) == 0): #Ensure the mesh has a material print '*** Error! ', meshname, 'must have a material & texture!' message = 'MS3D Export Error:|Mesh \"' + objects[ obj].data.name + '\"needs to have a material!' meshtools.print_boxed(message) Blender.Window.WaitCursor(0) Blender.Draw.PupMenu(message) return if mesh.materials[0]: numMats += 1 file.write("\nMaterials: " + str(numMats) + "\n") for obj in range(len(objects)): meshname = objects[obj].data.name mesh = Blender.NMesh.GetRaw(meshname) for material in mesh.materials: file.write("\"" + material.name + "\"\n") #file.write(str(round(material.ref,5))+ " " + str(round(material.ref,5))+ " " + str(round(material.ref,5)) + " " + str(round(material.alpha,5)) + "\n") #TODO Why are these values the same??? TODO # file.write(str("%5f %5f %5f %5f\n" % (material.ref,material.ref,material.ref,material.alpha))) file.write( str("%5f %5f %5f %5f\n" % (material.amb, material.amb, material.amb, material.alpha))) #file.write(str(round(material.rgbCol[0],5)) + " " + str(round(material.rgbCol[1],5)) + " " + str(round(material.rgbCol[2],5)) + " " + str(round(material.alpha,5)) + "\n") file.write( str("%5f %5f %5f %5f\n" % (material.rgbCol[0], material.rgbCol[1], material.rgbCol[2], material.alpha))) file.write( str("%5f %5f %5f %5f\n" % (material.spec, material.spec, material.spec, material.alpha))) #file.write(str(round(material.spec,5)) + " " + str(round(material.spec,5)) + " " + str(round(material.spec,5)) + " " + str(round(material.alpha,5))) file.write( str("%5f %5f %5f %5f\n" % (material.emit, material.emit, material.emit, material.alpha))) file.write(str(material.ref) + "\n") file.write(str(material.alpha) + "\n") # get the current texture image imageName = "" mytex = material.getTextures() if (dbg): print 'MyTex: ', mytex[0] if (mytex[0] != None): image = material.getTextures()[0].tex.getImage() imageName = Blender.sys.basename(image.getFilename()) file.write("\".\\" + stripPath(imageName) + "\"") else: print 'Warning: Mesh ' + meshname + ' does not have a texture image!' message = 'MS3D Export Warning:|Mesh \"' + meshname + '\" does not have a texture image!' meshtools.print_boxed(message) Blender.Window.WaitCursor(0) Blender.Draw.PupMenu(message) file.write("\"\"") #file.write("\".\\" + imageName + "\"") file.write("\n\"\"\n") file.write( '\nBones: 0\n') # we don't need no stinking bones for Scorched! ;) file.write('GroupComments: 0\n') file.write('MaterialComments: 0\n') file.write('BoneComments: 0\n') file.write('ModelComment: 0\n') Blender.Window.DrawProgressBar(1.0, '') #clear progressbar file.close() end = time.clock() seconds = " in %.2f %s" % (end - start, "seconds") message = "Successfully exported " + os.path.basename(filename) + seconds meshtools.print_boxed(message) Blender.Window.WaitCursor(0) Blender.Draw.PupMenu(message) return