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))])
161 if f.material_index<len(self.materials):
162 mat = self.materials[f.material_index]
163 if mat and mat.array_atlas:
164 layer = (mat.array_layer,)
166 for i in range(len(f.uvs)):
167 f.uvs[i] = [mathutils.Vector(tuple(u)+layer) for u in f.uvs[i]]
169 self.edges = dict([(e.key, Edge(e)) for e in self.edges])
171 for k in f.edge_keys:
173 e.faces.append(self.faces[f.index])
176 self.lines = [Line(e) for e in self.edges.values() if not e.faces]
178 l.vertices = [self.vertices[i] for i in l.vertices]
180 if self.use_auto_smooth:
181 smooth_limit = math.cos(self.auto_smooth_angle)
185 for e in self.edges.values():
186 e.vertices = [self.vertices[i] for i in e.vertices]
187 e.check_smooth(smooth_limit)
189 def __getattr__(self, attr):
190 return getattr(self._mesh, attr)
192 def transform(self, matrix):
193 for v in self.vertices:
196 def splice(self, other):
198 for m in other.materials:
199 if m in self.materials:
200 material_map.append(self.materials.index(m))
202 material_map.append(len(self.materials))
203 self.materials.append(m)
205 offset = len(self.vertices)
206 for v in other.vertices:
208 self.vertices.append(v)
210 offset = len(self.faces)
211 for f in other.faces:
214 f.material_index = material_map[f.material_index]
217 for e in other.edges.values():
218 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
219 self.edges[e.key] = e
221 self.lines += other.lines
223 def flatten_faces(self):
227 for e in self.edges.values():
230 def assign_texture_units(self):
231 # Assign texture units for any non-hidden UV layers that lack one
232 units = [u.unit for u in self.uv_layers if u.unit is not None]
234 free_unit = max(units)+1
237 for u in self.uv_layers:
243 def generate_material_uv(self):
244 self.uv_layers.append(FakeUvLayer("material_tex"))
245 self.assign_texture_units()
247 f.uvs.append([((f.material_index+0.5)/len(self.materials), 0.5)]*len(f.vertices))
249 def split_vertices(self, find_group_func, progress, *args):
251 for i in range(len(self.vertices)):
259 vg.append(find_group_func(v, f, *args))
264 progress.set_progress(i*0.5/len(self.vertices))
266 for i in range(len(self.vertices)):
267 if len(groups[i])==1:
270 for g in groups[i][1:]:
271 v = Vertex(self.vertices[i])
272 v.index = len(self.vertices)
273 self.vertices.append(v)
279 if e.key in v_edge_keys or self.vertices[i] not in e.vertices:
282 e_faces_in_g = [c for c in e.faces if c in g]
283 boundary = len(e_faces_in_g)<len(e.faces)
284 v_edges.append((e, boundary, e_faces_in_g))
285 v_edge_keys.add(e.key)
287 for e, boundary, e_faces_in_g in v_edges:
290 for c in e_faces_in_g:
292 c.edges[c.edges.index(e)] = ne
296 del self.edges[e.key]
298 e.vertices[e.vertices.index(self.vertices[i])] = v
300 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
301 self.edges[e.key] = e
304 self.vertices[i].faces.remove(f)
305 f.vertices[f.vertices.index(self.vertices[i])] = v
309 progress.set_progress(0.5+i*0.5/len(self.vertices))
311 def split_smooth(self, progress=None):
312 self.split_vertices(self.find_smooth_group, progress)
314 def split_uv(self, index, progress=None):
315 self.split_vertices(self.find_uv_group, progress, index)
317 def find_smooth_group(self, vertex, face):
323 other = e.other_face(f)
324 if other not in vertex.faces:
334 def find_uv_group(self, vertex, face, index):
335 uv = face.uvs[index][face.vertices.index(vertex)]
338 for f in vertex.faces:
339 if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
344 def compute_normals(self):
345 for v in self.vertices:
347 v.normal = mathutils.Vector()
349 fv = f.pivot_vertices(v)
350 edge1 = fv[1].co-fv[0].co
351 edge2 = fv[-1].co-fv[0].co
352 if edge1.length and edge2.length:
354 if len(f.get_edge(fv[0], fv[1]).faces)==1:
356 if len(f.get_edge(fv[0], fv[-1]).faces)==1:
358 v.normal += f.normal*edge1.angle(edge2)*weight
362 v.normal = mathutils.Vector((0, 0, 1))
364 # XXX Should use edges to compute normal
365 v.normal = mathutils.Vector((0, 0, 1))
367 def compute_uv(self):
368 for v in self.vertices:
371 i = f.vertices.index(v)
372 v.uvs = [u[i] for u in f.uvs]
374 v.uvs = [(0.0, 0.0)]*len(self.uv_layers)
376 def compute_tbn(self, index):
377 if not self.uv_layers:
380 for v in self.vertices:
381 v.tan = mathutils.Vector()
382 v.bino = mathutils.Vector()
384 fv = f.pivot_vertices(v)
385 uv0 = fv[0].uvs[index]
386 uv1 = fv[1].uvs[index]
387 uv2 = fv[-1].uvs[index]
392 edge1 = fv[1].co-fv[0].co
393 edge2 = fv[-1].co-fv[0].co
394 div = (du1*dv2-du2*dv1)
396 mul = edge1.angle(edge2)/div
397 v.tan += (edge1*dv2-edge2*dv1)*mul
398 v.bino += (edge2*du1-edge1*du2)*mul
405 def sort_vertex_groups(self, max_groups):
406 for v in self.vertices:
408 v.groups = sorted(v.groups, key=(lambda g: g.weight), reverse=True)
409 v.group_weight_scale = 1.0/sum(g.weight for g in v.groups[:max_groups])
411 def create_strip(self, face, max_len):
412 # Find an edge with another unused face next to it
415 other = e.other_face(face)
416 if other and not other.flag:
423 # Add initial vertices so that we'll complete the edge on the first
425 vertices = face.pivot_vertices(*edge.vertices)
427 result = [vertices[-1], vertices[0]]
429 result = [vertices[-2], vertices[-1]]
434 vertices = face.pivot_vertices(*result[-2:])
437 # Quads need special handling because the winding of every other
438 # triangle in the strip is reversed
439 if len(vertices)==4 and not k:
440 result.append(vertices[3])
441 result.append(vertices[2])
442 if len(vertices)==4 and k:
443 result.append(vertices[3])
445 if len(result)>=max_len:
448 # Hop over the last edge
449 edge = face.get_edge(*result[-2:])
450 face = edge.other_face(face)
451 if not face or face.flag: