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 if len(f.vertices)>4:
152 raise ValueError("Ngons are not supported")
153 f.vertices = [self.vertices[i] for i in f.vertices]
156 for u in self.uv_layers:
157 f.uvs.append([u.data[f.loop_indices[i]].uv for i in range(len(f.vertices))])
159 self.edges = dict([(e.key, Edge(e)) for e in self.edges])
161 for k in f.edge_keys:
163 e.faces.append(self.faces[f.index])
166 self.lines = [Line(e) for e in self.edges.values() if not e.faces]
168 l.vertices = [self.vertices[i] for i in l.vertices]
170 if self.use_auto_smooth:
171 smooth_limit = math.cos(self.auto_smooth_angle)
175 for e in self.edges.values():
176 e.vertices = [self.vertices[i] for i in e.vertices]
177 e.check_smooth(smooth_limit)
179 def __getattr__(self, attr):
180 return getattr(self._mesh, attr)
182 def splice(self, other):
184 for m in other.materials:
185 if m in self.materials:
186 material_map.append(self.materials.index(m))
188 material_map.append(len(self.materials))
189 self.materials.append(m)
191 offset = len(self.vertices)
192 for v in other.vertices:
194 self.vertices.append(v)
196 offset = len(self.faces)
197 for f in other.faces:
200 f.material_index = material_map[f.material_index]
203 for e in other.edges.values():
204 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
205 self.edges[e.key] = e
207 self.lines += other.lines
209 def flatten_faces(self):
213 for e in self.edges.values():
216 def assign_texture_units(self):
217 # Assign texture units for any non-hidden UV layers that lack one
218 units = [u.unit for u in self.uv_layers if u.unit is not None]
220 free_unit = max(units)+1
223 for u in self.uv_layers:
229 def generate_material_uv(self):
230 self.uv_layers.append(FakeUvLayer("material_tex"))
231 self.assign_texture_units()
233 f.uvs.append([((f.material_index+0.5)/len(self.materials), 0.5)]*len(f.vertices))
235 def split_vertices(self, find_group_func, progress, *args):
237 for i in range(len(self.vertices)):
245 vg.append(find_group_func(v, f, *args))
250 progress.set_progress(i*0.5/len(self.vertices))
252 for i in range(len(self.vertices)):
253 if len(groups[i])==1:
256 for g in groups[i][1:]:
257 v = Vertex(self.vertices[i])
258 v.index = len(self.vertices)
259 self.vertices.append(v)
265 if e.key in v_edge_keys or self.vertices[i] not in e.vertices:
268 e_faces_in_g = [c for c in e.faces if c in g]
269 boundary = len(e_faces_in_g)<len(e.faces)
270 v_edges.append((e, boundary, e_faces_in_g))
271 v_edge_keys.add(e.key)
273 for e, boundary, e_faces_in_g in v_edges:
276 for c in e_faces_in_g:
278 c.edges[c.edges.index(e)] = ne
282 del self.edges[e.key]
284 e.vertices[e.vertices.index(self.vertices[i])] = v
286 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
287 self.edges[e.key] = e
290 self.vertices[i].faces.remove(f)
291 f.vertices[f.vertices.index(self.vertices[i])] = v
295 progress.set_progress(0.5+i*0.5/len(self.vertices))
297 def split_smooth(self, progress=None):
298 self.split_vertices(self.find_smooth_group, progress)
300 def split_uv(self, index, progress=None):
301 self.split_vertices(self.find_uv_group, progress, index)
303 def find_smooth_group(self, vertex, face):
309 other = e.other_face(f)
310 if other not in vertex.faces:
320 def find_uv_group(self, vertex, face, index):
321 uv = face.uvs[index][face.vertices.index(vertex)]
324 for f in vertex.faces:
325 if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
330 def compute_normals(self):
331 for v in self.vertices:
333 v.normal = mathutils.Vector()
335 fv = f.pivot_vertices(v)
336 edge1 = fv[1].co-fv[0].co
337 edge2 = fv[-1].co-fv[0].co
338 if edge1.length and edge2.length:
340 if len(f.get_edge(fv[0], fv[1]).faces)==1:
342 if len(f.get_edge(fv[0], fv[-1]).faces)==1:
344 v.normal += f.normal*edge1.angle(edge2)*weight
348 v.normal = mathutils.Vector((0, 0, 1))
350 # XXX Should use edges to compute normal
351 v.normal = mathutils.Vector((0, 0, 1))
353 def compute_uv(self):
354 for v in self.vertices:
357 i = f.vertices.index(v)
358 v.uvs = [u[i] for u in f.uvs]
360 v.uvs = [(0.0, 0.0)]*len(self.uv_layers)
362 def compute_tbn(self, index):
363 if not self.uv_layers:
366 for v in self.vertices:
367 v.tan = mathutils.Vector()
368 v.bino = mathutils.Vector()
370 fv = f.pivot_vertices(v)
371 uv0 = fv[0].uvs[index]
372 uv1 = fv[1].uvs[index]
373 uv2 = fv[-1].uvs[index]
378 edge1 = fv[1].co-fv[0].co
379 edge2 = fv[-1].co-fv[0].co
380 div = (du1*dv2-du2*dv1)
382 mul = edge1.angle(edge2)/div
383 v.tan += (edge1*dv2-edge2*dv1)*mul
384 v.bino += (edge2*du1-edge1*du2)*mul
391 def sort_vertex_groups(self, max_groups):
392 for v in self.vertices:
394 v.groups = sorted(v.groups, key=(lambda g: g.weight), reverse=True)
395 v.group_weight_scale = 1.0/sum(g.weight for g in v.groups[:max_groups])
397 def create_strip(self, face, max_len):
398 # Find an edge with another unused face next to it
401 other = e.other_face(face)
402 if other and not other.flag:
409 # Add initial vertices so that we'll complete the edge on the first
411 vertices = face.pivot_vertices(*edge.vertices)
413 result = [vertices[-1], vertices[0]]
415 result = [vertices[-2], vertices[-1]]
420 vertices = face.pivot_vertices(*result[-2:])
423 # Quads need special handling because the winding of every other
424 # triangle in the strip is reversed
425 if len(vertices)==4 and not k:
426 result.append(vertices[3])
427 result.append(vertices[2])
428 if len(vertices)==4 and k:
429 result.append(vertices[3])
431 if len(result)>=max_len:
434 # Hop over the last edge
435 edge = face.get_edge(*result[-2:])
436 face = edge.other_face(face)
437 if not face or face.flag: