1 # $Id: mesh_export.py 137 2010-12-05 19:22:35Z tdb $
6 def __init__(self, size):
8 self.slots = [-1]*self.size
11 hit = v.index in self.slots
13 self.slots.remove(v.index)
14 self.slots.append(v.index)
19 def fetch_strip(self, strip):
26 def test_strip(self, strip):
28 for i in range(len(strip)):
31 if strip[i].index in self.slots[i:]:
37 def __init__(self, fn):
39 self.file = sys.stdout
41 self.file = open(fn, "w")
44 def make(self, kwd, *params):
51 return "%s%s"%(kwd, pstr)
53 def write(self, kwd, *params):
54 self.file.write("%s%s;\n"%('\t'*self.indent, self.make(kwd, *params)))
56 def begin(self, kwd, *params):
58 self.file.write("%s%s\n%s{\n"%(i, self.make(kwd, *params), i))
63 self.file.write("%s};\n"%('\t'*self.indent))
68 self.use_strips = True
69 self.use_degen_tris = True
70 self.max_strip_len = 1024
71 self.optimize_cache = False
73 self.export_lines = True
77 self.material_tex = False
79 def stripify(self, mesh, progress = None):
88 if self.optimize_cache:
89 cache = VertexCache(self.cache_size)
111 other = e.other_face(f)
112 if other and not other.flag:
126 other = e.other_face(f)
127 if other and not other.flag:
129 if score>0 and score<best:
138 for i in range(len(island_strips)):
139 hits = cache.test_strip(island_strips[i])
144 s = island_strips[best]
145 del island_strips[best]
151 faces_done += len(island)
153 progress.set_progress(float(faces_done)/len(mesh.faces))
155 loose += [f for f in island if not f.flag]
163 strip = mesh.create_strip(face, self.max_strip_len)
165 island_strips.append(strip)
168 cache = VertexCache(self.cache_size)
171 if self.use_degen_tris and strips:
176 glue = [big_strip[-1], s[0]]
182 total_hits += cache.fetch_strip(glue)
186 total_hits += cache.fetch_strip(s)
190 order = (-1, -2, 0, 1)
192 order = (0, 1, -1, -2)
193 vertices = [f.vertices[i] for i in order[:len(f.vertices)]]
196 glue = [big_strip[-1], vertices[0]]
199 total_hits += cache.fetch_strip(glue)
201 big_strip += vertices
203 total_hits += cache.fetch_strip(vertices)
210 def export(self, context, fn):
212 objs = context.selected_objects
214 objs = [context.active_object]
217 raise Exception("Nothing to export")
220 raise Exception("Can only export Mesh data")
222 from .mesh import Mesh
223 from .util import Progress
225 progress = Progress()
226 progress.set_task("Preparing", 0.0, 0.0)
231 bmesh = o.create_mesh(context.scene, True, "PREVIEW")
232 bmeshes.append(bmesh)
236 mesh.splice(Mesh(bmesh))
238 progress.set_task("Smoothing", 0.05, 0.35)
241 mesh.compute_normals()
243 if self.material_tex:
244 mesh.generate_material_uv()
247 progress.set_task("Splitting UVs", 0.35, 0.65)
257 progress.set_task("Creating strips", 0.65, 0.95)
258 strips, loose = self.stripify(mesh, progress)
260 progress.set_task("Writing file", 0.95, 1.0)
262 out_file = OutFile(fn)
264 out_file.begin("mesh")
270 fmt += "_ATTRIB33_ATTRIB34"
272 out_file.begin("vertices", fmt)
277 for v in mesh.vertices:
279 out_file.write("normal3", *v.normal)
282 out_file.write("texcoord2", *v.uv)
285 out_file.write("attrib3", 3, *v.tan)
288 out_file.write("attrib3", 4, *v.bino)
290 out_file.write("vertex3", *v.co)
293 out_file.begin("batch", "TRIANGLE_STRIP")
297 indices.append(v.index)
299 out_file.write("indices", *indices)
302 out_file.write("indices", *indices)
306 out_file.begin("batch", "TRIANGLES")
308 for i in range(2, len(f.vertices)):
309 out_file.write("indices", f.vertices[0].index, f.vertices[i-1].index, f.vertices[i].index)
312 if self.export_lines and mesh.lines:
313 out_file.write("batch", "LINES")
315 out_file.write("indices", l.vertices[0].index, l.vertices[1].index)
320 out_file.begin("technique")
321 out_file.begin("pass", '""')
322 if self.material_tex:
323 out_file.begin("material")
324 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
326 out_file.begin("texunit", 0)
327 out_file.begin("texture2d")
328 out_file.write("min_filter", "NEAREST")
329 out_file.write("mag_filter", "NEAREST")
330 out_file.write("storage", "RGB", len(mesh.materials), 1)
332 for m in mesh.materials:
333 color = [int(c*255) for c in m.diffuse_color]
334 texdata += "\\x%02X\\x%02X\\x%02X"%tuple(color)
336 out_file.write("raw_data", texdata)
340 m = mesh.materials[0]
341 out_file.begin("material")
342 out_file.write("diffuse", m.R, m.G, m.B, 1.0)
343 out_file.write("ambient", m.R*m.amb, m.G*m.amb, m.B*m.amb, 1.0)
344 out_file.write("specular", m.specR*m.spec, m.specG*m.spec, m.specB*m.spec, 1.0)
345 out_file.write("shininess", m.hard);
350 progress.set_task("Done", 1.0, 1.0)
353 bpy.data.meshes.remove(m)