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
49 self.normal = mv.normal
53 self.group_weight_scale = mv.group_weight_scale
57 self.normal = mv.normal
61 self.group_weight_scale = 1
65 def __getattr__(self, attr):
66 return getattr(self._mvert, attr)
68 def __cmp__(self, other):
71 return cmp(self.index, other.index)
75 def __init__(self, mf):
78 self.vertices = mf.vertices[:]
82 def __getattr__(self, attr):
83 return getattr(self._mface, attr)
85 def __cmp__(self, other):
88 return cmp(self.index, other.index)
90 def pivot_vertices(self, *vt):
91 flags = [(v in vt) for v in self.vertices]
92 l = len(self.vertices)
94 if flags[i] and not flags[(i+l-1)%l]:
95 return self.vertices[i:]+self.vertices[:i]
97 def get_edge(self, v1, v2):
98 key = make_edge_key(v1.index, v2.index)
102 raise KeyError("No edge %s"%(key,))
104 def get_neighbors(self):
105 neighbors = [e.other_face(self) for e in self.edges]
106 return list(filter(bool, neighbors))
110 def __init__(self, e):
112 self.vertices = e.vertices[:]
117 def __init__(self, l, t):
120 self.name = self.uvtex.name
123 dot = self.name.find('.')
125 ext = self.name[dot:]
126 if ext.startswith(".unit") and ext[5:].isdigit():
127 self.unit = int(ext[5:])
131 def __getattr__(self, attr):
132 return getattr(self._layer, attr)
135 def __init__(self, n):
142 def __init__(self, m):
145 self.vertices = [Vertex(v) for v in self.vertices]
146 self.faces = [Face(f) for f in self.polygons]
148 self.materials = self.materials[:]
150 self.uv_layers = [UvLayer(self.uv_layers[i], self.uv_textures[i]) for i in range(len(self.uv_layers))]
151 self.assign_texture_units()
154 if len(f.vertices)>4:
155 raise ValueError("Ngons are not supported")
156 f.vertices = [self.vertices[i] for i in f.vertices]
159 for u in self.uv_layers:
160 f.uvs.append([u.data[f.loop_indices[i]].uv for i in range(len(f.vertices))])
162 self.edges = dict([(e.key, Edge(e)) for e in self.edges])
164 for k in f.edge_keys:
166 e.faces.append(self.faces[f.index])
169 self.lines = [Line(e) for e in self.edges.values() if not e.faces]
171 l.vertices = [self.vertices[i] for i in l.vertices]
173 if self.use_auto_smooth:
174 smooth_limit = math.cos(self.auto_smooth_angle)
178 for e in self.edges.values():
179 e.vertices = [self.vertices[i] for i in e.vertices]
180 e.check_smooth(smooth_limit)
182 def __getattr__(self, attr):
183 return getattr(self._mesh, attr)
185 def transform(self, matrix):
186 for v in self.vertices:
189 def splice(self, other):
191 for m in other.materials:
192 if m in self.materials:
193 material_map.append(self.materials.index(m))
195 material_map.append(len(self.materials))
196 self.materials.append(m)
198 offset = len(self.vertices)
199 for v in other.vertices:
201 self.vertices.append(v)
203 offset = len(self.faces)
204 for f in other.faces:
207 f.material_index = material_map[f.material_index]
210 for e in other.edges.values():
211 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
212 self.edges[e.key] = e
214 self.lines += other.lines
216 def flatten_faces(self):
220 for e in self.edges.values():
223 def assign_texture_units(self):
224 # Assign texture units for any non-hidden UV layers that lack one
225 units = [u.unit for u in self.uv_layers if u.unit is not None]
227 free_unit = max(units)+1
230 for u in self.uv_layers:
236 def generate_material_uv(self):
237 self.uv_layers.append(FakeUvLayer("material_tex"))
238 self.assign_texture_units()
240 f.uvs.append([((f.material_index+0.5)/len(self.materials), 0.5)]*len(f.vertices))
242 def split_vertices(self, find_group_func, progress, *args):
244 for i in range(len(self.vertices)):
252 vg.append(find_group_func(v, f, *args))
257 progress.set_progress(i*0.5/len(self.vertices))
259 for i in range(len(self.vertices)):
260 if len(groups[i])==1:
263 for g in groups[i][1:]:
264 v = Vertex(self.vertices[i])
265 v.index = len(self.vertices)
266 self.vertices.append(v)
272 if e.key in v_edge_keys or self.vertices[i] not in e.vertices:
275 e_faces_in_g = [c for c in e.faces if c in g]
276 boundary = len(e_faces_in_g)<len(e.faces)
277 v_edges.append((e, boundary, e_faces_in_g))
278 v_edge_keys.add(e.key)
280 for e, boundary, e_faces_in_g in v_edges:
283 for c in e_faces_in_g:
285 c.edges[c.edges.index(e)] = ne
289 del self.edges[e.key]
291 e.vertices[e.vertices.index(self.vertices[i])] = v
293 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
294 self.edges[e.key] = e
297 self.vertices[i].faces.remove(f)
298 f.vertices[f.vertices.index(self.vertices[i])] = v
302 progress.set_progress(0.5+i*0.5/len(self.vertices))
304 def split_smooth(self, progress=None):
305 self.split_vertices(self.find_smooth_group, progress)
307 def split_uv(self, index, progress=None):
308 self.split_vertices(self.find_uv_group, progress, index)
310 def find_smooth_group(self, vertex, face):
316 other = e.other_face(f)
317 if other not in vertex.faces:
327 def find_uv_group(self, vertex, face, index):
328 uv = face.uvs[index][face.vertices.index(vertex)]
331 for f in vertex.faces:
332 if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
337 def compute_normals(self):
338 for v in self.vertices:
340 v.normal = mathutils.Vector()
342 fv = f.pivot_vertices(v)
343 edge1 = fv[1].co-fv[0].co
344 edge2 = fv[-1].co-fv[0].co
345 if edge1.length and edge2.length:
347 if len(f.get_edge(fv[0], fv[1]).faces)==1:
349 if len(f.get_edge(fv[0], fv[-1]).faces)==1:
351 v.normal += f.normal*edge1.angle(edge2)*weight
355 v.normal = mathutils.Vector((0, 0, 1))
357 # XXX Should use edges to compute normal
358 v.normal = mathutils.Vector((0, 0, 1))
360 def compute_uv(self):
361 for v in self.vertices:
364 i = f.vertices.index(v)
365 v.uvs = [u[i] for u in f.uvs]
367 v.uvs = [(0.0, 0.0)]*len(self.uv_layers)
369 def compute_tbn(self, index):
370 if not self.uv_layers:
373 for v in self.vertices:
374 v.tan = mathutils.Vector()
375 v.bino = mathutils.Vector()
377 fv = f.pivot_vertices(v)
378 uv0 = fv[0].uvs[index]
379 uv1 = fv[1].uvs[index]
380 uv2 = fv[-1].uvs[index]
385 edge1 = fv[1].co-fv[0].co
386 edge2 = fv[-1].co-fv[0].co
387 div = (du1*dv2-du2*dv1)
389 mul = edge1.angle(edge2)/div
390 v.tan += (edge1*dv2-edge2*dv1)*mul
391 v.bino += (edge2*du1-edge1*du2)*mul
398 def sort_vertex_groups(self, max_groups):
399 for v in self.vertices:
401 v.groups = sorted(v.groups, key=(lambda g: g.weight), reverse=True)
402 v.group_weight_scale = 1.0/sum(g.weight for g in v.groups[:max_groups])
404 def create_strip(self, face, max_len):
405 # Find an edge with another unused face next to it
408 other = e.other_face(face)
409 if other and not other.flag:
416 # Add initial vertices so that we'll complete the edge on the first
418 vertices = face.pivot_vertices(*edge.vertices)
420 result = [vertices[-1], vertices[0]]
422 result = [vertices[-2], vertices[-1]]
427 vertices = face.pivot_vertices(*result[-2:])
430 # Quads need special handling because the winding of every other
431 # triangle in the strip is reversed
432 if len(vertices)==4 and not k:
433 result.append(vertices[3])
434 result.append(vertices[2])
435 if len(vertices)==4 and k:
436 result.append(vertices[3])
438 if len(result)>=max_len:
441 # Hop over the last edge
442 edge = face.get_edge(*result[-2:])
443 face = edge.other_face(face)
444 if not face or face.flag: