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 if self.use_auto_smooth:
167 smooth_limit = math.cos(self.auto_smooth_angle)
171 for e in self.edges.values():
172 e.vertices = [self.vertices[i] for i in e.vertices]
173 e.check_smooth(smooth_limit)
175 def __getattr__(self, attr):
176 return getattr(self._mesh, attr)
178 def splice(self, other):
180 for m in other.materials:
181 if m in self.materials:
182 material_map.append(self.materials.index(m))
184 material_map.append(len(self.materials))
185 self.materials.append(m)
187 offset = len(self.vertices)
188 for v in other.vertices:
190 self.vertices.append(v)
192 offset = len(self.faces)
193 for f in other.faces:
196 f.material_index = material_map[f.material_index]
199 for e in other.edges.values():
200 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
201 self.edges[e.key] = e
203 self.lines += other.lines
205 def flatten_faces(self):
209 for e in self.edges.values():
212 def assign_texture_units(self):
213 # Assign texture units for any non-hidden UV layers that lack one
214 units = [u.unit for u in self.uv_layers if u.unit is not None]
216 free_unit = max(units)+1
219 for u in self.uv_layers:
225 def generate_material_uv(self):
226 self.uv_layers.append(FakeUvLayer("material_tex"))
227 self.assign_texture_units()
229 f.uvs.append([((f.material_index+0.5)/len(self.materials), 0.5)]*len(f.vertices))
231 def split_vertices(self, find_group_func, progress, *args):
233 for i in range(len(self.vertices)):
241 vg.append(find_group_func(v, f, *args))
246 progress.set_progress(i*0.5/len(self.vertices))
248 for i in range(len(self.vertices)):
249 if len(groups[i])==1:
252 for g in groups[i][1:]:
253 v = Vertex(self.vertices[i])
254 v.index = len(self.vertices)
255 self.vertices.append(v)
261 if e.key in v_edge_keys or self.vertices[i] not in e.vertices:
264 e_faces_in_g = [c for c in e.faces if c in g]
265 boundary = len(e_faces_in_g)<len(e.faces)
266 v_edges.append((e, boundary, e_faces_in_g))
267 v_edge_keys.add(e.key)
269 for e, boundary, e_faces_in_g in v_edges:
272 for c in e_faces_in_g:
274 c.edges[c.edges.index(e)] = ne
278 del self.edges[e.key]
280 e.vertices[e.vertices.index(self.vertices[i])] = v
282 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
283 self.edges[e.key] = e
286 self.vertices[i].faces.remove(f)
287 f.vertices[f.vertices.index(self.vertices[i])] = v
291 progress.set_progress(0.5+i*0.5/len(self.vertices))
293 def split_smooth(self, progress = None):
294 self.split_vertices(self.find_smooth_group, progress)
296 def split_uv(self, index, progress = None):
297 self.split_vertices(self.find_uv_group, progress, index)
299 def find_smooth_group(self, vertex, face):
305 other = e.other_face(f)
306 if other not in vertex.faces:
316 def find_uv_group(self, vertex, face, index):
317 uv = face.uvs[index][face.vertices.index(vertex)]
320 for f in vertex.faces:
321 if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
326 def compute_normals(self):
327 for v in self.vertices:
329 v.normal = mathutils.Vector()
331 fv = f.pivot_vertices(v)
332 edge1 = fv[1].co-fv[0].co
333 edge2 = fv[-1].co-fv[0].co
334 if edge1.length and edge2.length:
336 if len(f.get_edge(fv[0], fv[1]).faces)==1:
338 if len(f.get_edge(fv[0], fv[-1]).faces)==1:
340 v.normal += f.normal*edge1.angle(edge2)*weight
344 v.normal = mathutils.Vector((0, 0, 1))
346 # XXX Should use edges to compute normal
347 v.normal = mathutils.Vector((0, 0, 1))
349 def compute_uv(self):
350 for v in self.vertices:
353 i = f.vertices.index(v)
354 v.uvs = [u[i] for u in f.uvs]
356 v.uvs = [(0.0, 0.0)]*len(self.uv_layers)
358 def compute_tbn(self, index):
359 if not self.uv_layers:
362 for v in self.vertices:
363 v.tan = mathutils.Vector()
364 v.bino = mathutils.Vector()
366 fv = f.pivot_vertices(v)
367 uv0 = fv[0].uvs[index]
368 uv1 = fv[1].uvs[index]
369 uv2 = fv[-1].uvs[index]
374 edge1 = fv[1].co-fv[0].co
375 edge2 = fv[-1].co-fv[0].co
376 div = (du1*dv2-du2*dv1)
378 mul = edge1.angle(edge2)/div
379 v.tan += (edge1*dv2-edge2*dv1)*mul
380 v.bino += (edge2*du1-edge1*du2)*mul
387 def sort_vertex_groups(self, max_groups):
388 for v in self.vertices:
390 v.groups = sorted(v.groups, key=(lambda g: g.weight), reverse=True)
391 v.group_weight_scale = 1.0/sum(g.weight for g in v.groups[:max_groups])
393 def create_strip(self, face, max_len):
394 # Find an edge with another unused face next to it
397 other = e.other_face(face)
398 if other and not other.flag:
405 # Add initial vertices so that we'll complete the edge on the first
407 vertices = face.pivot_vertices(*edge.vertices)
409 result = [vertices[-1], vertices[0]]
411 result = [vertices[-2], vertices[-1]]
416 vertices = face.pivot_vertices(*result[-2:])
419 # Quads need special handling because the winding of every other
420 # triangle in the strip is reversed
421 if len(vertices)==4 and not k:
422 result.append(vertices[3])
423 result.append(vertices[2])
424 if len(vertices)==4 and k:
425 result.append(vertices[3])
427 if len(result)>=max_len:
430 # Hop over the last edge
431 edge = face.get_edge(*result[-2:])
432 face = edge.other_face(face)
433 if not face or face.flag: