4 def __init__(self, size):
6 self.slots = [-1]*self.size
9 hit = v.index in self.slots
11 self.slots.remove(v.index)
12 self.slots.append(v.index)
17 def fetch_strip(self, strip):
24 def test_strip(self, strip):
26 for i in range(len(strip)):
29 if strip[i].index in self.slots[i:]:
35 def __init__(self, fn):
37 self.file = sys.stdout
39 self.file = open(fn, "w")
42 def make(self, kwd, *params):
49 return "%s%s"%(kwd, pstr)
51 def write(self, kwd, *params):
52 self.file.write("%s%s;\n"%('\t'*self.indent, self.make(kwd, *params)))
54 def begin(self, kwd, *params):
56 self.file.write("%s%s\n%s{\n"%(i, self.make(kwd, *params), i))
61 self.file.write("%s};\n"%('\t'*self.indent))
66 self.use_strips = True
67 self.use_degen_tris = True
68 self.max_strip_len = 1024
69 self.optimize_cache = False
71 self.export_lines = True
72 self.export_uv = "UNIT0"
77 self.material_tex = False
78 self.smoothing = "MSPGL"
80 def stripify(self, mesh, progress = None):
89 if self.optimize_cache:
90 cache = VertexCache(self.cache_size)
112 other = e.other_face(f)
113 if other and not other.flag:
127 other = e.other_face(f)
128 if other and not other.flag:
130 if score>0 and score<best:
139 for i in range(len(island_strips)):
140 hits = cache.test_strip(island_strips[i])
145 s = island_strips[best]
146 del island_strips[best]
152 faces_done += len(island)
154 progress.set_progress(float(faces_done)/len(mesh.faces))
156 loose += [f for f in island if not f.flag]
164 strip = mesh.create_strip(face, self.max_strip_len)
166 island_strips.append(strip)
169 cache = VertexCache(self.cache_size)
172 if self.use_degen_tris and strips:
177 glue = [big_strip[-1], s[0]]
183 total_hits += cache.fetch_strip(glue)
187 total_hits += cache.fetch_strip(s)
191 order = (-1, -2, 0, 1)
193 order = (0, 1, -1, -2)
194 vertices = [f.vertices[i] for i in order[:len(f.vertices)]]
197 glue = [big_strip[-1], vertices[0]]
200 total_hits += cache.fetch_strip(glue)
202 big_strip += vertices
204 total_hits += cache.fetch_strip(vertices)
211 def export(self, context, fn):
213 objs = context.selected_objects
215 objs = [context.active_object]
218 raise Exception("Nothing to export")
221 raise Exception("Can only export Mesh data")
223 from .mesh import Mesh
224 from .util import Progress
226 progress = Progress()
227 progress.set_task("Preparing", 0.0, 0.0)
232 bmesh = o.to_mesh(context.scene, True, "PREVIEW")
233 bmeshes.append(bmesh)
237 mesh.splice(Mesh(bmesh))
239 progress.set_task("Smoothing", 0.05, 0.35)
240 if self.smoothing=="NONE":
242 mesh.split_smooth(progress)
244 if self.smoothing!="BLENDER":
245 mesh.compute_normals()
247 if self.material_tex and mesh.materials:
248 mesh.generate_material_uv()
251 if mesh.uv_layers and self.export_uv!="NONE":
252 # Figure out which UV layers to export
253 if self.export_uv=="UNIT0":
254 if mesh.uv_layers[0].unit==0:
257 texunits = range(len(mesh.uv_layers))
258 texunits = [(i, mesh.uv_layers[i]) for i in texunits]
259 texunits = [u for u in texunits if not u[1].hidden]
262 # TBN coordinates must be generated before vertices are split by any other layer
263 uv_names = [u.name for i, u in texunits]
264 if self.tbn_uvtex in uv_names:
265 tbn_index = uv_names.index(self.tbn_uvtex)
266 unit = texunits[tbn_index]
267 del texunits[tbn_index]
268 texunits.insert(0, unit)
270 for i, u in texunits:
271 progress.set_task("Splitting UVs", 0.35+0.3*i/len(texunits), 0.35+0.3*(i+1)/len(texunits))
272 mesh.split_uv(i, progress)
273 if self.tbn_vecs and u.name==self.tbn_uvtex:
282 progress.set_task("Creating strips", 0.65, 0.95)
283 strips, loose = self.stripify(mesh, progress)
285 progress.set_task("Writing file", 0.95, 1.0)
287 out_file = OutFile(fn)
289 out_file.begin("mesh")
293 for i, u in texunits:
297 fmt += "_TEXCOORD2%d"%u.unit
299 fmt += "_ATTRIB33_ATTRIB34"
301 out_file.begin("vertices", fmt)
303 uvs = [None]*len(texunits)
306 for v in mesh.vertices:
308 out_file.write("normal3", *v.normal)
310 for i, u in texunits:
313 out_file.write("texcoord2", *v.uvs[i])
315 out_file.write("multitexcoord2", u.unit, *v.uvs[i])
318 out_file.write("attrib3", 3, *v.tan)
321 out_file.write("attrib3", 4, *v.bino)
323 out_file.write("vertex3", *v.co)
326 out_file.begin("batch", "TRIANGLE_STRIP")
330 indices.append(v.index)
332 out_file.write("indices", *indices)
335 out_file.write("indices", *indices)
339 out_file.begin("batch", "TRIANGLES")
341 for i in range(2, len(f.vertices)):
342 out_file.write("indices", f.vertices[0].index, f.vertices[i-1].index, f.vertices[i].index)
345 if self.export_lines and mesh.lines:
346 out_file.write("batch", "LINES")
348 out_file.write("indices", l.vertices[0].index, l.vertices[1].index)
353 out_file.begin("technique")
354 out_file.begin("pass", '""')
356 if self.material_tex:
357 out_file.begin("material")
358 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
361 for u in mesh.uv_layers:
362 if u.name=="material_tex":
364 out_file.begin("texunit", index)
365 out_file.begin("texture2d")
366 out_file.write("min_filter", "NEAREST")
367 out_file.write("mag_filter", "NEAREST")
368 out_file.write("storage", "RGB", len(mesh.materials), 1)
370 for m in mesh.materials:
371 color = [int(c*255) for c in m.diffuse_color]
372 texdata += "\\x%02X\\x%02X\\x%02X"%tuple(color)
374 out_file.write("raw_data", texdata)
378 mat = mesh.materials[0]
379 out_file.begin("material")
380 diff = mat.diffuse_color
381 out_file.write("diffuse", diff.r, diff.g, diff.b, 1.0)
382 amb = diff*mat.ambient
383 out_file.write("ambient", amb.r, amb.g, amb.b, 1.0)
384 spec = mat.specular_color*mat.specular_intensity
385 out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
386 out_file.write("shininess", mat.specular_hardness);
391 progress.set_task("Done", 1.0, 1.0)
394 bpy.data.meshes.remove(m)