4 def make_edge_key(i1, i2):
5 return (min(i1, i2), max(i1, i2))
8 def __init__(self, me):
10 self._medge = me._medge
11 self.vertices = me.vertices[:]
12 self.smooth = me.smooth
18 def __getattr__(self, attr):
19 return getattr(self._medge, attr)
21 def check_smooth(self, limit):
22 if len(self.faces)!=2:
25 d = self.faces[0].normal.dot(self.faces[1].normal)
26 self.smooth = ((d>limit and self.faces[0].use_smooth and self.faces[1].use_smooth) or d>0.99995)
28 def other_face(self, f):
29 if f.index==self.faces[0].index:
30 if len(self.faces)>=2:
37 def other_vertex(self, v):
38 if v.index==self.vertices[0].index:
39 return self.vertices[1]
41 return self.vertices[0]
45 def __init__(self, mv):
46 if mv.__class__==Vertex:
47 self._mvert = mv._mvert
48 self.normal = mv.normal
52 self.group_weight_scale = mv.group_weight_scale
58 self.group_weight_scale = 1
62 def __getattr__(self, attr):
63 return getattr(self._mvert, attr)
65 def __cmp__(self, other):
68 return cmp(self.index, other.index)
72 def __init__(self, mf):
75 self.vertices = mf.vertices[:]
79 def __getattr__(self, attr):
80 return getattr(self._mface, attr)
82 def __cmp__(self, other):
85 return cmp(self.index, other.index)
87 def pivot_vertices(self, *vt):
88 flags = [(v in vt) for v in self.vertices]
89 l = len(self.vertices)
91 if flags[i] and not flags[(i+l-1)%l]:
92 return self.vertices[i:]+self.vertices[:i]
94 def get_edge(self, v1, v2):
95 key = make_edge_key(v1.index, v2.index)
99 raise KeyError("No edge %s"%(key,))
101 def get_neighbors(self):
102 neighbors = [e.other_face(self) for e in self.edges]
103 return list(filter(bool, neighbors))
107 def __init__(self, e):
109 self.vertices = e.vertices[:]
114 def __init__(self, l, t):
117 self.name = self.uvtex.name
120 dot = self.name.find('.')
122 ext = self.name[dot:]
123 if ext.startswith(".unit") and ext[5:].isdigit():
124 self.unit = int(ext[5:])
128 def __getattr__(self, attr):
129 return getattr(self._layer, attr)
132 def __init__(self, n):
139 def __init__(self, m):
142 self.vertices = [Vertex(v) for v in self.vertices]
143 self.faces = [Face(f) for f in self.polygons]
145 self.materials = self.materials[:]
147 self.uv_layers = [UvLayer(self.uv_layers[i], self.uv_textures[i]) for i in range(len(self.uv_layers))]
148 self.assign_texture_units()
151 f.vertices = [self.vertices[i] for i in f.vertices]
154 for u in self.uv_layers:
155 f.uvs.append([u.data[f.loop_indices[i]].uv for i in range(len(f.vertices))])
157 self.edges = dict([(e.key, Edge(e)) for e in self.edges])
159 for k in f.edge_keys:
161 e.faces.append(self.faces[f.index])
164 self.lines = [Line(e) for e in self.edges.values() if not e.faces]
166 l.vertices = [self.vertices[i] for i in l.vertices]
168 if self.use_auto_smooth:
169 smooth_limit = math.cos(self.auto_smooth_angle)
173 for e in self.edges.values():
174 e.vertices = [self.vertices[i] for i in e.vertices]
175 e.check_smooth(smooth_limit)
177 def __getattr__(self, attr):
178 return getattr(self._mesh, attr)
180 def splice(self, other):
182 for m in other.materials:
183 if m in self.materials:
184 material_map.append(self.materials.index(m))
186 material_map.append(len(self.materials))
187 self.materials.append(m)
189 offset = len(self.vertices)
190 for v in other.vertices:
192 self.vertices.append(v)
194 offset = len(self.faces)
195 for f in other.faces:
198 f.material_index = material_map[f.material_index]
201 for e in other.edges.values():
202 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
203 self.edges[e.key] = e
205 self.lines += other.lines
207 def flatten_faces(self):
211 for e in self.edges.values():
214 def assign_texture_units(self):
215 # Assign texture units for any non-hidden UV layers that lack one
216 units = [u.unit for u in self.uv_layers if u.unit is not None]
218 free_unit = max(units)+1
221 for u in self.uv_layers:
227 def generate_material_uv(self):
228 self.uv_layers.append(FakeUvLayer("material_tex"))
229 self.assign_texture_units()
231 f.uvs.append([((f.material_index+0.5)/len(self.materials), 0.5)]*len(f.vertices))
233 def split_vertices(self, find_group_func, progress, *args):
235 for i in range(len(self.vertices)):
243 vg.append(find_group_func(v, f, *args))
248 progress.set_progress(i*0.5/len(self.vertices))
250 for i in range(len(self.vertices)):
251 if len(groups[i])==1:
254 for g in groups[i][1:]:
255 v = Vertex(self.vertices[i])
256 v.index = len(self.vertices)
257 self.vertices.append(v)
263 if e.key in v_edge_keys or self.vertices[i] not in e.vertices:
266 e_faces_in_g = [c for c in e.faces if c in g]
267 boundary = len(e_faces_in_g)<len(e.faces)
268 v_edges.append((e, boundary, e_faces_in_g))
269 v_edge_keys.add(e.key)
271 for e, boundary, e_faces_in_g in v_edges:
274 for c in e_faces_in_g:
276 c.edges[c.edges.index(e)] = ne
280 del self.edges[e.key]
282 e.vertices[e.vertices.index(self.vertices[i])] = v
284 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
285 self.edges[e.key] = e
288 self.vertices[i].faces.remove(f)
289 f.vertices[f.vertices.index(self.vertices[i])] = v
293 progress.set_progress(0.5+i*0.5/len(self.vertices))
295 def split_smooth(self, progress = None):
296 self.split_vertices(self.find_smooth_group, progress)
298 def split_uv(self, index, progress = None):
299 self.split_vertices(self.find_uv_group, progress, index)
301 def find_smooth_group(self, vertex, face):
307 other = e.other_face(f)
308 if other not in vertex.faces:
318 def find_uv_group(self, vertex, face, index):
319 uv = face.uvs[index][face.vertices.index(vertex)]
322 for f in vertex.faces:
323 if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
328 def compute_normals(self):
329 for v in self.vertices:
331 v.normal = mathutils.Vector()
333 fv = f.pivot_vertices(v)
334 edge1 = fv[1].co-fv[0].co
335 edge2 = fv[-1].co-fv[0].co
336 if edge1.length and edge2.length:
338 if len(f.get_edge(fv[0], fv[1]).faces)==1:
340 if len(f.get_edge(fv[0], fv[-1]).faces)==1:
342 v.normal += f.normal*edge1.angle(edge2)*weight
346 v.normal = mathutils.Vector((0, 0, 1))
348 # XXX Should use edges to compute normal
349 v.normal = mathutils.Vector((0, 0, 1))
351 def compute_uv(self):
352 for v in self.vertices:
355 i = f.vertices.index(v)
356 v.uvs = [u[i] for u in f.uvs]
358 v.uvs = [(0.0, 0.0)]*len(self.uv_layers)
360 def compute_tbn(self, index):
361 if not self.uv_layers:
364 for v in self.vertices:
365 v.tan = mathutils.Vector()
366 v.bino = mathutils.Vector()
368 fv = f.pivot_vertices(v)
369 uv0 = fv[0].uvs[index]
370 uv1 = fv[1].uvs[index]
371 uv2 = fv[-1].uvs[index]
376 edge1 = fv[1].co-fv[0].co
377 edge2 = fv[-1].co-fv[0].co
378 div = (du1*dv2-du2*dv1)
380 mul = edge1.angle(edge2)/div
381 v.tan += (edge1*dv2-edge2*dv1)*mul
382 v.bino += (edge2*du1-edge1*du2)*mul
389 def sort_vertex_groups(self, max_groups):
390 for v in self.vertices:
392 v.groups = sorted(v.groups, key=(lambda g: g.weight), reverse=True)
393 v.group_weight_scale = 1.0/sum(g.weight for g in v.groups[:max_groups])
395 def create_strip(self, face, max_len):
396 # Find an edge with another unused face next to it
399 other = e.other_face(face)
400 if other and not other.flag:
407 # Add initial vertices so that we'll complete the edge on the first
409 vertices = face.pivot_vertices(*edge.vertices)
411 result = [vertices[-1], vertices[0]]
413 result = [vertices[-2], vertices[-1]]
418 vertices = face.pivot_vertices(*result[-2:])
421 # Quads need special handling because the winding of every other
422 # triangle in the strip is reversed
423 if len(vertices)==4 and not k:
424 result.append(vertices[3])
425 result.append(vertices[2])
426 if len(vertices)==4 and k:
427 result.append(vertices[3])
429 if len(result)>=max_len:
432 # Hop over the last edge
433 edge = face.get_edge(*result[-2:])
434 face = edge.other_face(face)
435 if not face or face.flag: