From: Mikko Rasa Date: Sat, 9 Oct 2010 06:51:32 +0000 (+0000) Subject: Blender exporter: X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=2663d392c1c60e0ca511bb836010124024da3276;p=libs%2Fgl.git Blender exporter: Support exporting mspgl objects Add ability to create a texture from material colors --- diff --git a/mesh_export.py b/mesh_export.py index 4c18bb48..4265ca3d 100644 --- a/mesh_export.py +++ b/mesh_export.py @@ -129,6 +129,7 @@ class Mesh: self._mesh = m self.verts = [Vertex(v) for v in m.verts] self.faces = [Face(f) for f in m.faces] + self.materials = m.materials[:] for f in self.faces: for i in range(len(f.verts)): @@ -158,6 +159,14 @@ class Mesh: return getattr(self._mesh, attr) def splice(self, other): + material_map = [] + for m in other.materials: + if m in self.materials: + material_map.append(self.materials.index(m)) + else: + material_map.append(len(self.materials)) + self.materials.append(m) + offset = len(self.verts) for v in other.verts: v.index += offset @@ -166,6 +175,7 @@ class Mesh: offset = len(self.faces) for f in other.faces: f.index += offset + f.mat = material_map[f.mat] self.faces.append(f) for e in other.edges.itervalues(): @@ -173,6 +183,11 @@ class Mesh: self.edges[e.key] = e self.lines += other.lines + + def generate_material_uv(self): + for f in self.faces: + f.uv = ([(f.mat+0.5)/len(self.materials), 0.5],)*len(f.verts) + self.faceUV = True def split_vertices(self, find_group_func, debug): groups = [] @@ -389,13 +404,39 @@ class VertexCache: return hits -class Exporter: +class OutFile: def __init__(self, fn): - self.filename = fn if fn==None: - self.out_file = sys.stdout + self.file = sys.stdout else: - self.out_file = file(fn, "w") + self.file = open(fn, "w") + self.indent = 0 + + def make(self, kwd, *params): + pstr = "" + for p in params: + if type(p)==float: + pstr += " %.6g"%p + else: + pstr += " %s"%p + return "%s%s"%(kwd, pstr) + + def write(self, kwd, *params): + self.file.write("%s%s;\n"%('\t'*self.indent, self.make(kwd, *params))) + + def begin(self, kwd, *params): + i = '\t'*self.indent + self.file.write("%s%s\n%s{\n"%(i, self.make(kwd, *params), i)) + self.indent += 1 + + def end(self): + self.indent -= 1 + self.file.write("%s};\n"%('\t'*self.indent)) + + +class Exporter: + def __init__(self, fn): + self.out_file = OutFile(fn) self.use_strips = True self.use_degen_tris = True self.max_strip_len = 1024 @@ -404,6 +445,8 @@ class Exporter: self.export_lines = True self.tbn_vecs = False self.compound = False + self.object = False + self.material_tex = False self.debug = False self.strip_debug = False self.split_debug = False @@ -525,6 +568,7 @@ class Exporter: mesh.getFromObject(objs[0]) mesh = Mesh(mesh) if self.compound: + # Must keep a ref to each Blender mesh bmeshes = [] for o in objs[1:]: bmesh = Blender.Mesh.New("export_tmp") @@ -543,6 +587,9 @@ class Exporter: mesh.compute_normals() + if self.material_tex: + mesh.generate_material_uv() + if mesh.faceUV: mesh.split_uv(self.split_debug) if self.debug: @@ -557,56 +604,90 @@ class Exporter: if self.use_strips: strips, loose = self.stripify(mesh) - self.out_file.write("vertices NORMAL3") + if self.object: + self.out_file.begin("mesh") + + fmt = "NORMAL3" if mesh.faceUV: - self.out_file.write("_TEXCOORD2") + fmt += "_TEXCOORD2" if self.tbn_vecs: - self.out_file.write("_ATTRIB33_ATTRIB34") - self.out_file.write("_VERTEX3\n{\n") + fmt += "_ATTRIB33_ATTRIB34" + fmt += "_VERTEX3" + self.out_file.begin("vertices", fmt) norm = None uv = None tan = None bino = None for v in mesh.verts: if v.no!=norm: - self.out_file.write("\tnormal3 %f %f %f;\n"%tuple(v.no)) + self.out_file.write("normal3", *v.no) norm = v.no if v.uv!=uv: - self.out_file.write("\ttexcoord2 %f %f;\n"%tuple(v.uv)) + self.out_file.write("texcoord2", *v.uv) uv = v.uv if v.tan!=tan: - self.out_file.write("\tattrib3 3 %f %f %f;\n"%tuple(v.tan)) + self.out_file.write("attrib3", 3, *v.tan) tan = v.tan if v.bino!=bino: - self.out_file.write("\tattrib3 4 %f %f %f;\n"%tuple(v.bino)) + self.out_file.write("attrib3", 4, *v.bino) bino = v.bino - self.out_file.write("\tvertex3 %f %f %f;\n"%tuple(v.co)) - self.out_file.write("};\n") + self.out_file.write("vertex3", *v.co) + self.out_file.end() for s in strips: - self.out_file.write("batch TRIANGLE_STRIP\n{\n\tindices") + self.out_file.begin("batch", "TRIANGLE_STRIP") + indices = [] n = 0 for v in s: - self.out_file.write(" %u"%v.index) - n += 1; - if n%32==0: - self.out_file.write(";\n\tindices") - self.out_file.write(";\n};\n") - - first = True - for f in loose: - if first: - self.out_file.write("batch TRIANGLES\n{\n") - first = False - for i in range(2, len(f.verts)): - self.out_file.write("\tindices %u %u %u;\n"%(f.verts[0].index, f.verts[i-1].index, f.verts[i].index)) - if not first: - self.out_file.write("};\n") + indices.append(v.index) + if len(indices)>=32: + self.out_file.write("indices", *indices) + indices = [] + if indices: + self.out_file.write("indices", *indices) + self.out_file.end() + + if loose: + self.out_file.begin("batch", "TRIANGLES") + for f in loose: + for i in range(2, len(f.verts)): + self.out_file.write("indices", f.verts[0].index, f.verts[i-1].index, f.verts[i].index) + self.out_file.end() if self.export_lines and mesh.lines: - self.out_file.write("batch LINES\n{\n") + self.out_file.write("batch", "LINES") for l in mesh.lines: - self.out_file.write("\tindices %u %u;\n"%(l.verts[0].index, l.verts[1].index)) - self.out_file.write("};\n") + self.out_file.write("indices", l.verts[0].index, l.verts[1].index) + self.out_file.end() + + if self.object: + self.out_file.end() + self.out_file.begin("technique") + self.out_file.begin("pass", '""') + if self.material_tex: + self.out_file.begin("material") + self.out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0) + self.out_file.end() + self.out_file.begin("texunit", 0) + self.out_file.begin("texture2d") + self.out_file.write("min_filter", "NEAREST") + self.out_file.write("mag_filter", "NEAREST") + self.out_file.write("storage", "RGB", len(mesh.materials), 1, 0) + texdata = '"' + for m in mesh.materials: + texdata += "\\x%02X\\x%02X\\x%02X"%(int(m.R*255), int(m.G*255), int(m.B*255)) + texdata += '"' + self.out_file.write("raw_data", texdata) + self.out_file.end() + self.out_file.end() + elif mesh.materials: + m = mesh.materials[0] + self.out_file.begin("material") + self.out_file.write("diffuse", m.R, m.G, m.B, 1.0) + self.out_file.write("ambient", m.R*m.amb, m.G*m.amb, m.B*m.amb, 1.0) + self.out_file.write("specular", m.specR, m.specG, m.specB, 1.0) + self.out_file.end() + self.out_file.end() + self.out_file.end() class FrontEnd: @@ -623,6 +704,8 @@ class FrontEnd: self.export_lines = Blender.Draw.Create(self.config.get('export_lines', False)) self.tbn_vecs = Blender.Draw.Create(self.config.get('tbn_vecs', False)) self.compound = Blender.Draw.Create(self.config.get('compound', False)) + self.object = Blender.Draw.Create(self.config.get('object', False)) + self.material_tex = Blender.Draw.Create(self.config.get('material_tex', False)) self.debug = Blender.Draw.Create(self.config.get('debug', False)) self.strip_debug = Blender.Draw.Create(self.config.get('strip_debug', False)) self.split_debug = Blender.Draw.Create(self.config.get('split_debug', False)) @@ -635,6 +718,8 @@ class FrontEnd: ("Export lines", self.export_lines, "Export lone edges as lines"), ("Compute T/B vecs", self.tbn_vecs, "Compute tangent/binormal vectors for bumpmapping"), ("Compound", self.compound, "Create a compound mesh of all selected objects"), + ("Object", self.object, "Export as an object"), + ("Material texture", self.material_tex, "Create a texture from material data"), ("Debugging options"), ("Debug", self.debug), ("Debug strips", self.strip_debug), @@ -642,7 +727,10 @@ class FrontEnd: if ret: dirname = self.temp_config.get("dirname", Blender.sys.dirname(Blender.Get("filename"))) obj = Blender.Object.GetSelected()[0] - Blender.Window.FileSelector(self.export, "Export MSP GL mesh", "%s/%s.mesh"%(dirname, obj.name)) + ext = "mesh" + if self.object.val: + ext = "object" + Blender.Window.FileSelector(self.export, "Export MSP GL mesh", "%s/%s.%s"%(dirname, obj.name, ext)) def draw(self): pass @@ -656,6 +744,8 @@ class FrontEnd: self.config['export_lines'] = self.export_lines.val self.config['tbn_vecs'] = self.tbn_vecs.val self.config['compound'] = self.compound.val + self.config['object'] = self.object.val + self.config['material_tex'] = self.material_tex.val self.config['debug'] = self.debug.val self.config['strip_debug'] = self.strip_debug.val self.config['split_debug'] = self.split_debug.val @@ -674,6 +764,8 @@ class FrontEnd: exp.export_lines = self.export_lines.val exp.tbn_vecs = self.tbn_vecs.val exp.compound = self.compound.val + exp.object = self.object.val + exp.material_tex = self.material_tex.val exp.debug = self.debug.val exp.strip_debug = self.strip_debug.val exp.split_debug = self.split_debug.val