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:
39 def __init__(self, mv):
40 if mv.__class__==Vertex:
41 self._mvert = mv._mvert
42 self.normal = mv.normal
46 self.group_weight_scale = mv.group_weight_scale
52 self.group_weight_scale = 1
56 def __getattr__(self, attr):
57 return getattr(self._mvert, attr)
59 def __cmp__(self, other):
62 return cmp(self.index, other.index)
66 def __init__(self, mf):
69 self.vertices = mf.vertices[:]
73 def __getattr__(self, attr):
74 return getattr(self._mface, attr)
76 def __cmp__(self, other):
79 return cmp(self.index, other.index)
81 def pivot_vertices(self, *vt):
82 flags = [(v in vt) for v in self.vertices]
83 l = len(self.vertices)
85 if flags[i] and not flags[(i+l-1)%l]:
86 return self.vertices[i:]+self.vertices[:i]
88 def get_edge(self, v1, v2):
89 key = make_edge_key(v1.index, v2.index)
93 raise KeyError("No edge %s"%(key,))
95 def get_neighbors(self):
96 neighbors = [e.other_face(self) for e in self.edges]
97 return list(filter(bool, neighbors))
101 def __init__(self, e):
103 self.vertices = e.vertices[:]
108 def __init__(self, l, t):
111 self.name = self.uvtex.name
114 dot = self.name.find('.')
116 ext = self.name[dot:]
117 if ext.startswith(".unit") and ext[5:].isdigit():
118 self.unit = int(ext[5:])
122 def __getattr__(self, attr):
123 return getattr(self._layer, attr)
126 def __init__(self, n):
133 def __init__(self, m):
136 self.vertices = [Vertex(v) for v in self.vertices]
137 self.faces = [Face(f) for f in self.polygons]
139 self.materials = self.materials[:]
141 self.uv_layers = [UvLayer(self.uv_layers[i], self.uv_textures[i]) for i in range(len(self.uv_layers))]
142 self.assign_texture_units()
145 f.vertices = [self.vertices[i] for i in f.vertices]
148 for u in self.uv_layers:
149 f.uvs.append([u.data[f.loop_indices[i]].uv for i in range(len(f.vertices))])
151 self.edges = dict([(e.key, Edge(e)) for e in self.edges])
153 for k in f.edge_keys:
155 e.faces.append(self.faces[f.index])
158 self.lines = [Line(e) for e in self.edges.values() if not e.faces]
160 if self.use_auto_smooth:
161 smooth_limit = math.cos(self.auto_smooth_angle*math.pi/180)
165 for e in self.edges.values():
166 e.vertices = [self.vertices[i] for i in e.vertices]
167 e.check_smooth(smooth_limit)
169 def __getattr__(self, attr):
170 return getattr(self._mesh, attr)
172 def splice(self, other):
174 for m in other.materials:
175 if m in self.materials:
176 material_map.append(self.materials.index(m))
178 material_map.append(len(self.materials))
179 self.materials.append(m)
181 offset = len(self.vertices)
182 for v in other.vertices:
184 self.vertices.append(v)
186 offset = len(self.faces)
187 for f in other.faces:
190 f.material_index = material_map[f.material_index]
193 for e in other.edges.values():
194 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
195 self.edges[e.key] = e
197 self.lines += other.lines
199 def flatten_faces(self):
203 for e in self.edges.values():
206 def assign_texture_units(self):
207 # Assign texture units for any non-hidden UV layers that lack one
208 units = [u.unit for u in self.uv_layers if u.unit is not None]
210 free_unit = max(units)+1
213 for u in self.uv_layers:
219 def generate_material_uv(self):
220 self.uv_layers.append(FakeUvLayer("material_tex"))
221 self.assign_texture_units()
223 f.uvs.append([((f.material_index+0.5)/len(self.materials), 0.5)]*len(f.vertices))
225 def split_vertices(self, find_group_func, progress, *args):
227 for i in range(len(self.vertices)):
235 vg.append(find_group_func(v, f, *args))
240 progress.set_progress(i*0.5/len(self.vertices))
242 for i in range(len(self.vertices)):
243 if len(groups[i])==1:
246 for g in groups[i][1:]:
247 v = Vertex(self.vertices[i])
248 v.index = len(self.vertices)
249 self.vertices.append(v)
252 for j in range(len(f.edges)):
255 if self.vertices[i] not in e.vertices:
258 if e.other_face(f) not in g and len(e.faces)>=2:
264 del self.edges[e.key]
266 e.vertices[e.vertices.index(self.vertices[i])] = v
268 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
269 self.edges[e.key] = e
271 self.vertices[i].faces.remove(f)
272 f.vertices[f.vertices.index(self.vertices[i])] = v
276 progress.set_progress(0.5+i*0.5/len(self.vertices))
278 def split_smooth(self, progress = None):
279 self.split_vertices(self.find_smooth_group, progress)
281 def split_uv(self, index, progress = None):
282 self.split_vertices(self.find_uv_group, progress, index)
284 def find_smooth_group(self, vertex, face):
290 other = e.other_face(f)
291 if other not in vertex.faces:
301 def find_uv_group(self, vertex, face, index):
302 uv = face.uvs[index][face.vertices.index(vertex)]
305 for f in vertex.faces:
306 if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
311 def compute_normals(self):
312 for v in self.vertices:
314 v.normal = mathutils.Vector()
316 fv = f.pivot_vertices(v)
317 edge1 = fv[1].co-fv[0].co
318 edge2 = fv[-1].co-fv[0].co
320 if len(f.get_edge(fv[0], fv[1]).faces)==1:
322 if len(f.get_edge(fv[0], fv[-1]).faces)==1:
324 v.normal += f.normal*edge1.angle(edge2)*weight
327 # XXX Should use edges to compute normal
328 v.normal = mathutils.Vector(0, 0, 1)
330 def compute_uv(self):
331 for v in self.vertices:
334 i = f.vertices.index(v)
335 v.uvs = [u[i] for u in f.uvs]
337 def compute_tbn(self, index):
338 if not self.uv_layers:
341 for v in self.vertices:
342 v.tan = mathutils.Vector()
343 v.bino = mathutils.Vector()
345 fv = f.pivot_vertices(v)
346 uv0 = fv[0].uvs[index]
347 uv1 = fv[1].uvs[index]
348 uv2 = fv[-1].uvs[index]
353 edge1 = fv[1].co-fv[0].co
354 edge2 = fv[-1].co-fv[0].co
355 div = (du1*dv2-du2*dv1)
357 mul = edge1.angle(edge2)/div
358 v.tan += (edge1*dv2-edge2*dv1)*mul
359 v.bino += (edge2*du1-edge1*du2)*mul
366 def sort_vertex_groups(self, max_groups):
367 for v in self.vertices:
369 v.groups = sorted(v.groups, key=(lambda g: g.weight), reverse=True)
370 v.group_weight_scale = 1.0/sum(g.weight for g in v.groups[:max_groups])
372 def create_strip(self, face, max_len):
373 # Find an edge with another unused face next to it
376 other = e.other_face(face)
377 if other and not other.flag:
384 # Add initial vertices so that we'll complete the edge on the first
386 vertices = face.pivot_vertices(*edge.vertices)
388 result = [vertices[-1], vertices[0]]
390 result = [vertices[-2], vertices[-1]]
395 vertices = face.pivot_vertices(*result[-2:])
398 # Quads need special handling because the winding of every other
399 # triangle in the strip is reversed
400 if len(vertices)==4 and not k:
401 result.append(vertices[3])
402 result.append(vertices[2])
403 if len(vertices)==4 and k:
404 result.append(vertices[3])
406 if len(result)>=max_len:
409 # Hop over the last edge
410 edge = face.get_edge(*result[-2:])
411 face = edge.other_face(face)
412 if not face or face.flag: