#!BPY """ Name: 'Textport save' Blender: 234 Group: 'Export' Tooltip: 'Textport exporter for libgender' """ # Blender Text Export Module # Version 0.0 # Colin Peart # Released under GPL (insert license text here) import Blender from Blender import Types, Object, NMesh, Camera, Lamp, Mathutils from Blender.Mathutils import * import sys # dump -- the object dumper f: File, o: object def dump(f, o, od): if o.block_type == 'NMesh' : mesh = Blender.NMesh.GetRaw(o.name) f.write("M " + o.name + "\n") loc = od.getLocation () f.write("L L\n" + `loc[0]` + " " + `loc[2]` + " " + `loc[1]` + "\n") f.write("T Material\n"); if len (mesh.materials) > 0: material = mesh.materials[0] c0 = material.rgbCol[0] c1 = material.rgbCol[1] c2 = material.rgbCol[2] f.write(`c0` + " " + `c1` + " " + `c2` + " " + `material.alpha` + "\n"); s0 = material.specCol[0] s1 = material.specCol[1] s2 = material.specCol[2] f.write(`s0` + " " + `s1` + " " + `s2` + " " + `material.specTransp` + "\n"); s0 *= material.emit s1 *= material.emit s2 *= material.emit f.write(`s0` + " " + `s1` + " " + `s2` + " 1\n"); f.write(`material.spec` + "\n"); else: f.write("1 1 1 1\n"); f.write("0 0 0 1\n"); f.write("0 0 0 1\n"); f.write("0\n"); # Vertex Coordinate header f.write("V " + `len(mesh.verts)` + "\n") # Verticies for vert in mesh.verts: f.write(`vert.co[0]` + " " + `vert.co[2]` + " " + `vert.co[1]` + "\n") # Vertex Normal Header f.write("N " + `len(mesh.verts)` + "\n") # Vertex Normals for vert in mesh.verts: f.write(`vert.no[0]` + " " + `vert.no[2]` + " " + `vert.no[1]` + "\n") # uv coordinates if mesh.hasVertexUV: # UV header f.write("U " + `len(mesh.verts)` + "\n") for vert in mesh.verts: f.write(`vert.uvco[0]` + " " + `vert.uvco[1]` + "\n") # Faces: Take two passes: First, count the total number of faces, # second: output them. Tessalate quads to triangles. Discard edges (which # have only two vertices) # Modified to use only one pass, by building a variable with the text faces = len(mesh.faces) data = "" colordata = "" for face in mesh.faces: if len (face.v) < 3: faces = faces - 1 elif len (face.v) == 3: # triangle data = data + `face.v[0].index` + " " + `face.v[2].index` + " " + `face.v[1].index` + "\n" data = data + `face.smooth` + "\n" else: # this one is a quad # Break it up into two triangles # Hence one additional face faces = faces + 1 data = data + `face.v[0].index` + " " + `face.v[3].index` + " " + `face.v[1].index` + "\n" data = data + `face.smooth` + "\n" data = data + `face.v[1].index` + " " + `face.v[3].index` + " " + `face.v[2].index` + "\n" data = data + `face.smooth` + "\n" # Now I can write the header with the correct face count, and then the data f.write("A " + `faces` + "\n") f.write(data) #Stage 1: # Gather information -- which objects, which frames, what type of export # Objects to export: Use the set of selected objects objectsToExport = Blender.Object.GetSelected() # Which frames: startFrame = Blender.Get('staframe') endFrame = Blender.Get('endframe') frameset = range(startFrame, endFrame + 1) # Create a file for output filename = Blender.Get('filename') + '.blasc' print('Exporting ' + filename + ' frames:') print(frameset) fout = open(filename, 'w') # Go through the objects and export them # output file header #for ob in objectsToExport: ob = objectsToExport[0] objs = len (objectsToExport) # output object header print('Exporting Object ' + ob.name) fout.write("O " + `objs` + "\n") for i in objectsToExport: dump (fout, i.data, i) #for fr in frameset: # Blender.Set('curframe', fr) #print('Exporting Frame ' + `fr`) # output frame header #fout.write("F " + `fr` + "\n") # Write out common items #fout.write("Y " + `ob.data.block_type` + "\n") #fout.write("L " + `ob.LocX + ob.dLocX` + " " + `ob.LocY + ob.dLocY` + " " + `ob.LocZ + ob.dLocZ` + "\n") #fout.write("R " + `ob.RotX + ob.dRotX` + " " + `ob.RotY + ob.dRotY` + " " + `ob.RotZ + ob.dRotZ` + "\n") #fout.write("S " + `ob.SizeX + ob.dSizeX` + " " + `ob.SizeY + ob.dSizeY` + " " + `ob.SizeZ + ob.dSizeZ` + "\n") # Write out the data # output Frame footer #fout.write("/F\n") #Output object footer # Close the file fout.close(); print('Done!')