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:]:
38 self.show_progress = True
39 self.use_strips = True
40 self.use_degen_tris = False
41 self.max_strip_len = 1024
42 self.optimize_cache = True
44 self.export_lines = False
45 self.export_uv = "UNIT0"
49 self.material_tex = False
50 self.smoothing = "MSPGL"
51 self.export_groups = False
54 def stripify(self, mesh, progress=None):
63 if self.optimize_cache:
64 cache = VertexCache(self.cache_size)
71 # No current island; find any unused face to start from
82 # Find all faces connected to the first one
87 for n in face.get_neighbors():
92 face_neighbors = [f.get_neighbors() for f in island]
94 # Unflag the island for the next phase
98 # Find an unused face with as few unused neighbors as possible, but
99 # at least one. This heuristic gives a preference to faces in corners
100 # or along borders of a non-closed island.
103 for i, f in enumerate(island):
107 score = sum(not n.flag for n in face_neighbors[i])
108 if score>0 and score<best:
113 # Create a strip starting from the face. This will flag the faces.
114 strip = mesh.create_strip(face, self.max_strip_len)
116 island_strips.append(strip)
120 # Couldn't find a candidate face for starting a strip, so we're
121 # done with this island
125 # Find the strip that benefits the most from the current
126 # contents of the vertex cache
128 for i in range(len(island_strips)):
129 hits = cache.test_strip(island_strips[i])
134 strip = island_strips.pop(best)
138 cache.fetch_strip(strip)
140 faces_done += len(island)
142 progress.set_progress(float(faces_done)/len(mesh.faces))
144 # Collect any faces that weren't used in strips
145 loose += [f for f in island if not f.flag]
153 cache = VertexCache(self.cache_size)
156 if self.use_degen_tris and strips:
161 # Generate glue elements, ensuring that the next strip begins at
163 glue = [big_strip[-1], s[0]]
169 total_hits += cache.fetch_strip(glue)
173 total_hits += cache.fetch_strip(s)
176 # Add loose faces to the end. This wastes space, using five
177 # elements per triangle and six elements per quad.
179 order = (-1, -2, 0, 1)
181 order = (0, 1, -1, -2)
182 vertices = [f.vertices[i] for i in order[:len(f.vertices)]]
185 glue = [big_strip[-1], vertices[0]]
188 total_hits += cache.fetch_strip(glue)
190 big_strip += vertices
192 total_hits += cache.fetch_strip(vertices)
199 def export(self, context, out_file, objs=None, progress=None):
201 objs = [(o, mathutils.Matrix()) for o in objs]
205 objs = [(o, mathutils.Matrix()) for o in context.selected_objects]
212 children.append((c, m*c.matrix_local))
216 objs = [(context.active_object, mathutils.Matrix())]
219 raise Exception("Nothing to export")
222 raise Exception("Can only export Mesh data")
224 from .mesh import Mesh
225 from .util import Progress
227 if self.show_progress:
229 progress = Progress(context)
230 progress.set_task("Preparing", 0.0, 0.0)
238 if o.data.winding_test:
240 bmesh = o.to_mesh(context.scene, True, "PREVIEW")
241 bmeshes.append(bmesh)
250 progress.set_task("Smoothing", 0.05, 0.35)
251 if self.smoothing=="NONE":
253 mesh.split_smooth(progress)
255 if self.smoothing!="BLENDER":
256 mesh.compute_normals()
258 if self.export_groups:
259 mesh.sort_vertex_groups(self.max_groups)
261 # Create a mapping from vertex group indices to bone indices
262 first_obj = objs[0][0]
263 group_index_map = dict((i, i) for i in range(len(first_obj.vertex_groups)))
264 if first_obj.parent and first_obj.parent.type=="ARMATURE":
265 armature = first_obj.parent.data
266 bone_indices = dict((armature.bones[i].name, i) for i in range(len(armature.bones)))
267 for g in first_obj.vertex_groups:
268 if g.name in bone_indices:
269 group_index_map[g.index] = bone_indices[g.name]
271 if self.material_tex and mesh.materials:
272 mesh.generate_material_uv()
276 if mesh.uv_layers and (self.export_uv!="NONE" or self.material_tex):
277 # Figure out which UV layers to export
278 if self.export_uv=="ALL":
279 texunits = range(len(mesh.uv_layers))
280 elif self.material_tex:
281 # The material UV layer is always the last one
282 texunits = [len(mesh.uv_layers)-1]
285 for i, u in enumerate(mesh.uv_layers):
289 texunits = [(i, mesh.uv_layers[i]) for i in texunits]
290 texunits = [u for u in texunits if not u[1].hidden]
293 # TBN coordinates must be generated before vertices are split by any other layer
294 uv_names = [u.name for i, u in texunits]
295 if self.tbn_uvtex in uv_names:
296 tbn_index = uv_names.index(self.tbn_uvtex)
297 unit = texunits[tbn_index]
298 del texunits[tbn_index]
299 texunits.insert(0, unit)
301 for i, u in texunits:
303 progress.set_task("Splitting UVs", 0.35+0.3*i/len(texunits), 0.35+0.3*(i+1)/len(texunits))
304 mesh.split_uv(i, progress)
305 if self.tbn_vecs and u.name==self.tbn_uvtex:
315 progress.set_task("Creating strips", 0.65, 0.95)
316 strips, loose = self.stripify(mesh, progress)
319 progress.set_task("Writing file", 0.95, 1.0)
321 from .outfile import open_output
322 out_file = open_output(out_file)
326 for i, u in texunits:
327 size = str(len(mesh.vertices[0].uvs[i]))
328 if u.unit==0 or force_unit0:
329 fmt.append("TEXCOORD"+size)
331 fmt.append("TEXCOORD%s_%d"%(size, u.unit))
333 fmt += ["TANGENT3", "BINORMAL3"]
334 if self.export_groups:
335 fmt.append("ATTRIB%d_5"%(self.max_groups*2))
336 fmt.append("VERTEX3")
337 out_file.begin("vertices", *fmt)
343 for v in mesh.vertices:
345 out_file.write("normal3", *v.normal)
347 for i, u in texunits:
348 if v.uvs[i]!=uvs.get(i):
349 size = str(len(v.uvs[i]))
350 if u.unit==0 or force_unit0:
351 out_file.write("texcoord"+size, *v.uvs[i])
353 out_file.write("multitexcoord"+size, u.unit, *v.uvs[i])
357 out_file.write("tangent3", *v.tan)
360 out_file.write("binormal3", *v.bino)
362 if self.export_groups:
363 group_attr = [(group_index_map[g.group], g.weight*v.group_weight_scale) for g in v.groups[:self.max_groups]]
364 while len(group_attr)<self.max_groups:
365 group_attr.append((0, 0.0))
366 group_attr = list(itertools.chain(*group_attr))
367 if group_attr!=group:
368 out_file.write("attrib%d"%len(group_attr), 5, *group_attr)
370 out_file.write("vertex3", *v.co)
373 out_file.begin("batch", "TRIANGLE_STRIP")
377 indices.append(v.index)
379 out_file.write("indices", *indices)
382 out_file.write("indices", *indices)
386 out_file.begin("batch", "TRIANGLES")
388 for i in range(2, len(f.vertices)):
389 out_file.write("indices", f.vertices[0].index, f.vertices[i-1].index, f.vertices[i].index)
392 if self.export_lines and mesh.lines:
393 out_file.begin("batch", "LINES")
395 out_file.write("indices", l.vertices[0].index, l.vertices[1].index)
399 out_file.write("winding", "COUNTERCLOCKWISE")
402 progress.set_task("Done", 1.0, 1.0)
405 bpy.data.meshes.remove(m)