5 def make_edge_key(i1, i2):
6 return (min(i1, i2), max(i1, i2))
9 def __init__(self, me):
10 if me.__class__==Edge:
11 self._medge = me._medge
12 self.vertices = me.vertices[:]
13 self.smooth = me.smooth
20 def __getattr__(self, attr):
21 return getattr(self._medge, attr)
23 def check_smooth(self, limit):
24 if len(self.faces)!=2:
27 d = self.faces[0].normal.dot(self.faces[1].normal)
28 self.smooth = ((d>limit and self.faces[0].use_smooth and self.faces[1].use_smooth) or d>0.99995)
30 def other_face(self, f):
31 if f.index==self.faces[0].index:
32 if len(self.faces)>=2:
39 def other_vertex(self, v):
40 if v.index==self.vertices[0].index:
41 return self.vertices[1]
43 return self.vertices[0]
47 def __init__(self, mv):
48 if mv.__class__==Vertex:
49 self._mvert = mv._mvert
60 self.normal = mv.normal
64 self.groups = mv.groups[:]
66 def __getattr__(self, attr):
67 return getattr(self._mvert, attr)
69 def __cmp__(self, other):
72 return cmp(self.index, other.index)
76 def __init__(self, base):
78 self.group = base.group
79 self.weight = base.weight
81 def __getattr__(self, attr):
82 return getattr(self._mvert, attr)
86 def __init__(self, mf):
90 self.vertices = mf.vertices[:]
94 def __getattr__(self, attr):
95 return getattr(self._mface, attr)
97 def __cmp__(self, other):
100 return cmp(self.index, other.index)
102 def pivot_vertex(self, v):
103 n = self.vertices.index(v)
104 return [(n+i)%len(self.vertices) for i in range(len(self.vertices))]
106 def pivot_vertices(self, *vt):
107 flags = [(v in vt) for v in self.vertices]
108 l = len(self.vertices)
110 if flags[i] and not flags[(i+l-1)%l]:
111 return self.vertices[i:]+self.vertices[:i]
113 def get_edge(self, v1, v2):
114 key = make_edge_key(v1.index, v2.index)
118 raise KeyError("No edge %s"%(key,))
120 def other_edge(self, e, v):
122 if d!=e and v in d.vertices:
125 def get_neighbors(self):
126 neighbors = [e.other_face(self) for e in self.edges]
127 return list(filter(bool, neighbors))
131 def __init__(self, e):
133 self.vertices = e.vertices[:]
138 def __init__(self, arg):
145 self.uvs = [d.uv for d in self.data]
150 dot = self.name.find('.')
152 ext = self.name[dot:]
153 if ext.startswith(".unit") and ext[5:].isdigit():
154 self.unit = int(ext[5:])
158 def __getattr__(self, attr):
159 return getattr(self._layer, attr)
163 def __init__(self, m):
166 self.winding_test = m.winding_test
167 self.tbn_vecs = m.tbn_vecs
168 self.vertex_groups = m.vertex_groups
170 self.vertices = [Vertex(v) for v in self.vertices]
171 self.faces = [Face(f) for f in self.polygons]
172 self.edges = [Edge(e) for e in self.edges]
173 self.edge_map = {e.key: e for e in self.edges}
174 self.loops = self.loops[:]
176 self.materials = self.materials[:]
177 if self.use_uv=='NONE' or not self.uv_layers:
180 self.uv_layers = [UvLayer(u) for u in self.uv_layers]
181 self.uv_layers = sorted([u for u in self.uv_layers if not u.hidden], key=(lambda u: (u.unit or 1000, u.name)))
183 if self.use_uv=='UNIT0':
184 self.uv_layers = [self.uv_layers[0]]
186 next_unit = max((u.unit+1 for u in self.uv_layers if u.unit is not None), default=0)
187 for u in self.uv_layers:
192 for v in self.vertices:
193 v.groups = [VertexGroup(g) for g in v.groups]
196 if len(f.vertices)>4:
197 raise ValueError("Ngons are not supported")
199 f.vertices = [self.vertices[i] for i in f.vertices]
203 for k in f.edge_keys:
209 e.vertices = [self.vertices[i] for i in e.vertices]
214 self.lines = [Line(e) for e in self.edges if not e.faces]
218 def __getattr__(self, attr):
219 return getattr(self._mesh, attr)
221 def transform(self, matrix):
222 for v in self.vertices:
225 def splice(self, other):
226 if len(self.uv_layers)!=len(other.uv_layers):
227 raise ValueError("Meshes have incompatible UV layers")
228 for i, u in enumerate(self.uv_layers):
229 if u.name!=other.uv_layers[i].name:
230 raise ValueError("Meshes have incompatible UV layers")
233 for m in other.materials:
234 if m in self.materials:
235 material_map.append(self.materials.index(m))
237 material_map.append(len(self.materials))
238 self.materials.append(m)
240 for i, u in enumerate(self.uv_layers):
241 u.uvs += other.uv_layers[i].uvs
243 offset = len(self.vertices)
244 self.vertices += other.vertices
245 for v in self.vertices[offset:]:
248 loop_offset = len(self.loops)
249 self.loops += other.loops
251 offset = len(self.faces)
252 self.faces += other.faces
253 for f in self.faces[offset:]:
255 f.loop_start += loop_offset
256 f.loop_indices = range(f.loop_start, f.loop_start+f.loop_total)
258 f.material_index = material_map[f.material_index]
260 offset = len(self.edges)
261 self.edges += other.edges
262 for e in self.edges[offset:]:
264 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
265 self.edge_map[e.key] = e
267 self.lines += other.lines
269 def prepare_smoothing(self, progress=None):
271 if self.smoothing=='NONE':
276 elif self.use_auto_smooth:
277 smooth_limit = math.cos(self.auto_smooth_angle)
280 e.check_smooth(smooth_limit)
282 self.split_vertices(self.find_smooth_group, progress)
284 if self.smoothing!='BLENDER':
285 self.compute_normals()
287 def prepare_vertex_groups(self, obj):
288 for v in self.vertices:
290 weight_sum = sum(g.weight for g in v.groups)
291 v.groups = sorted(v.groups, key=(lambda g: g.weight), reverse=True)[:self.max_groups_per_vertex]
292 weight_scale = weight_sum/sum(g.weight for g in v.groups)
294 g.weight *= weight_scale
296 if obj.parent and obj.parent.type=="ARMATURE":
297 armature = obj.parent.data
298 bone_indices = {b.name: i for i, b in enumerate(armature.bones)}
299 group_index_map = {i: i for i in range(len(obj.vertex_groups))}
300 for g in first_obj.vertex_groups:
301 if g.name in bone_indices:
302 group_index_map[g.index] = bone_indices[g.name]
304 for v in self.vertices:
306 g.group = group_index_map[g.group]
308 def prepare_uv(self, obj, progress=None):
309 if obj.material_tex and self.use_uv!='NONE':
310 layer = UvLayer("material_tex")
312 if self.use_uv=='UNIT0':
313 self.uv_layers = [layer]
316 self.uv_layers.append(layer)
317 layer.unit = max((u.unit+1 for u in self.uv_layers if u.unit is not None), default=0)
319 layer.uvs = [None]*len(self.loops)
321 uv = mathutils.Vector(((f.material_index+0.5)/len(self.materials), 0.5))
322 for i in f.loop_indices:
325 array_uv_layers = [t.uv_layer for m in self.materials if m.array_atlas for t in m.texture_slots if t and t.texture_coords=='UV']
326 array_uv_layers = [u for u in self.uv_layers if u.name in array_uv_layers]
331 if f.material_index<len(self.materials):
332 mat = self.materials[f.material_index]
333 if mat and mat.array_atlas:
334 layer = mat.array_layer
336 for l in array_uv_layers:
337 for i in f.loop_indices:
338 l.uvs[i] = mathutils.Vector((*l.uvs[i], layer))
341 for u in self.uv_layers:
342 f.uvs.append([u.uvs[i] for i in f.loop_indices])
346 uv_names = [u.name for u in self.uv_layers]
347 if self.tbn_uvtex in uv_names:
348 tbn_layer_index = uv_names.index(self.tbn_uvtex)
349 self.compute_tbn(tbn_layer_index)
350 self.split_vertices(self.find_uv_group, progress, tbn_layer_index)
352 for i in range(len(self.uv_layers)):
353 self.split_vertices(self.find_uv_group, progress, i)
355 for v in self.vertices:
358 i = f.vertices.index(v)
359 v.uvs = [u[i] for u in f.uvs]
361 v.uvs = [(0.0, 0.0)]*len(self.uv_layers)
363 def split_vertices(self, find_group_func, progress, *args):
365 for i in range(len(self.vertices)):
373 vg.append(find_group_func(v, f, *args))
378 progress.set_progress(i*0.5/len(self.vertices))
380 for i in range(len(self.vertices)):
381 for g in groups[i][1:]:
382 v = Vertex(self.vertices[i])
383 v.index = len(self.vertices)
384 self.vertices.append(v)
387 for e in self.vertices[i].edges:
388 e_faces_in_g = [f for f in e.faces if f in g]
390 boundary = len(e_faces_in_g)<len(e.faces)
391 v_edges.append((e, boundary, e_faces_in_g))
393 for e, boundary, e_faces_in_g in v_edges:
396 ne.index = len(self.edges)
397 self.edges.append(ne)
399 for f in e_faces_in_g:
401 f.edges[f.edges.index(e)] = ne
405 del self.edge_map[e.key]
406 self.vertices[i].edges.remove(e)
409 e.vertices[e.vertices.index(self.vertices[i])] = v
411 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
412 self.edge_map[e.key] = e
415 self.vertices[i].faces.remove(f)
416 f.vertices[f.vertices.index(self.vertices[i])] = v
420 progress.set_progress(0.5+i*0.5/len(self.vertices))
422 def find_smooth_group(self, vertex, face):
425 edges = [e for e in face.edges if vertex in e.vertices]
437 e = f.other_edge(e, vertex)
441 def find_uv_group(self, vertex, face, index):
442 uv = face.uvs[index][face.vertices.index(vertex)]
446 for f in vertex.faces:
447 if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
453 def compute_normals(self):
454 for v in self.vertices:
455 v.normal = mathutils.Vector()
457 fv = f.pivot_vertices(v)
458 edge1 = fv[1].co-fv[0].co
459 edge2 = fv[-1].co-fv[0].co
460 if edge1.length and edge2.length:
461 v.normal += f.normal*edge1.angle(edge2)
466 v.normal = mathutils.Vector((0, 0, 1))
468 def compute_tbn(self, index):
469 layer_uvs = self.uv_layers[index].uvs
471 for v in self.vertices:
472 v.tan = mathutils.Vector()
473 v.bino = mathutils.Vector()
475 vi = f.pivot_vertex(v)
476 uv0 = layer_uvs[f.loop_indices[vi[0]]]
477 uv1 = layer_uvs[f.loop_indices[vi[1]]]
478 uv2 = layer_uvs[f.loop_indices[vi[-1]]]
483 edge1 = f.vertices[vi[1]].co-f.vertices[vi[0]].co
484 edge2 = f.vertices[vi[-1]].co-f.vertices[vi[0]].co
485 div = (du1*dv2-du2*dv1)
487 mul = edge1.angle(edge2)/div
488 v.tan += (edge1*dv2-edge2*dv1)*mul
489 v.bino += (edge2*du1-edge1*du2)*mul
496 def drop_references(self):
497 for v in self.vertices:
505 for u in self.uv_layers:
509 def create_strip(self, face, max_len):
510 # Find an edge with another unused face next to it
513 other = e.other_face(face)
514 if other and not other.flag:
521 # Add initial vertices so that we'll complete the edge on the first
523 vertices = face.pivot_vertices(*edge.vertices)
525 result = [vertices[-1], vertices[0]]
527 result = [vertices[-2], vertices[-1]]
532 vertices = face.pivot_vertices(*result[-2:])
535 # Quads need special handling because the winding of every other
536 # triangle in the strip is reversed
537 if len(vertices)==4 and not k:
538 result.append(vertices[3])
539 result.append(vertices[2])
540 if len(vertices)==4 and k:
541 result.append(vertices[3])
543 if len(result)>=max_len:
546 # Hop over the last edge
547 edge = face.get_edge(*result[-2:])
548 face = edge.other_face(face)
549 if not face or face.flag:
554 def create_mesh_from_object(context, obj, progress=None):
556 raise Exception("Object is not a mesh")
558 objs = [(obj, mathutils.Matrix())]
564 if c.type=="MESH" and c.compound:
565 objs.append((c, m*c.matrix_local))
570 bmesh = o.to_mesh(context.scene, True, "PREVIEW")
571 bmeshes.append(bmesh)
573 # Object.to_mesh does not copy custom properties
574 bmesh.winding_test = o.data.winding_test
575 bmesh.smoothing = o.data.smoothing
576 bmesh.use_lines = o.data.use_lines
577 bmesh.vertex_groups = o.data.vertex_groups
578 bmesh.max_groups_per_vertex = o.data.max_groups_per_vertex
579 bmesh.use_uv = o.data.use_uv
580 bmesh.tbn_vecs = o.data.tbn_vecs
581 bmesh.tbn_uvtex = o.data.tbn_uvtex
591 mesh.prepare_smoothing(progress)
592 mesh.prepare_vertex_groups(obj)
593 mesh.prepare_uv(obj, progress)
595 mesh.drop_references()
597 bpy.data.meshes.remove(m)