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:
248 mesh.generate_material_uv()
251 if mesh.uv_textures and self.export_uv!="NONE":
252 if self.export_uv=="UNIT0":
255 texunits = list(range(len(mesh.uv_textures)))
259 uvtex_names = [u.name for u in mesh.uv_textures]
260 if self.tbn_uvtex in uvtex_names:
261 tbn_unit = uvtex_names.index(uvtex)
262 del texunits[tbn_unit]
263 texunits.insert(0, tbn_unit)
266 progress.set_task("Splitting UVs", 0.35+0.3*i/len(texunits), 0.35+0.3*(i+1)/len(texunits))
267 mesh.split_uv(i, progress)
268 if self.tbn_vecs and i==tbn_unit:
277 progress.set_task("Creating strips", 0.65, 0.95)
278 strips, loose = self.stripify(mesh, progress)
280 progress.set_task("Writing file", 0.95, 1.0)
282 out_file = OutFile(fn)
284 out_file.begin("mesh")
289 for i in texunits[1:]:
290 fmt += "_TEXCOORD2%d"%i
292 fmt += "_ATTRIB33_ATTRIB34"
294 out_file.begin("vertices", fmt)
296 uvs = [None]*len(mesh.uv_textures)
299 for v in mesh.vertices:
301 out_file.write("normal3", *v.normal)
306 out_file.write("texcoord2", *v.uvs[i])
308 out_file.write("multitexcoord2", i, *v.uvs[i])
311 out_file.write("attrib3", 3, *v.tan)
314 out_file.write("attrib3", 4, *v.bino)
316 out_file.write("vertex3", *v.co)
319 out_file.begin("batch", "TRIANGLE_STRIP")
323 indices.append(v.index)
325 out_file.write("indices", *indices)
328 out_file.write("indices", *indices)
332 out_file.begin("batch", "TRIANGLES")
334 for i in range(2, len(f.vertices)):
335 out_file.write("indices", f.vertices[0].index, f.vertices[i-1].index, f.vertices[i].index)
338 if self.export_lines and mesh.lines:
339 out_file.write("batch", "LINES")
341 out_file.write("indices", l.vertices[0].index, l.vertices[1].index)
346 out_file.begin("technique")
347 out_file.begin("pass", '""')
348 if self.material_tex:
349 out_file.begin("material")
350 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
352 out_file.begin("texunit", 0)
353 out_file.begin("texture2d")
354 out_file.write("min_filter", "NEAREST")
355 out_file.write("mag_filter", "NEAREST")
356 out_file.write("storage", "RGB", len(mesh.materials), 1)
358 for m in mesh.materials:
359 color = [int(c*255) for c in m.diffuse_color]
360 texdata += "\\x%02X\\x%02X\\x%02X"%tuple(color)
362 out_file.write("raw_data", texdata)
366 mat = mesh.materials[0]
367 out_file.begin("material")
368 diff = mat.diffuse_color
369 out_file.write("diffuse", diff.r, diff.g, diff.b, 1.0)
370 amb = diff*mat.ambient
371 out_file.write("ambient", amb.r, amb.g, amb.b, 1.0)
372 spec = mat.specular_color*mat.specular_intensity
373 out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
374 out_file.write("shininess", mat.specular_hardness);
379 progress.set_task("Done", 1.0, 1.0)
382 bpy.data.meshes.remove(m)