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
54 def __getattr__(self, attr):
55 return getattr(self._mvert, attr)
57 def __cmp__(self, other):
60 return cmp(self.index, other.index)
64 def __init__(self, mf):
67 self.vertices = mf.vertices[:]
71 def __getattr__(self, attr):
72 return getattr(self._mface, attr)
74 def __cmp__(self, other):
77 return cmp(self.index, other.index)
79 def pivot_vertices(self, *vt):
80 flags = [(v in vt) for v in self.vertices]
81 l = len(self.vertices)
83 if flags[i] and not flags[(i+l-1)%l]:
84 return self.vertices[i:]+self.vertices[:i]
86 def get_edge(self, v1, v2):
87 key = make_edge_key(v1.index, v2.index)
91 raise KeyError("No edge %s"%(key,))
93 def get_neighbors(self):
94 neighbors = [e.other_face(self) for e in self.edges]
95 return list(filter(bool, neighbors))
99 def __init__(self, e):
101 self.vertices = e.vertices[:]
106 def __init__(self, l, t):
109 self.name = self.uvtex.name
112 dot = self.name.find('.')
114 ext = self.name[dot:]
115 if ext.startswith(".unit") and ext[5:].isdigit():
116 self.unit = int(ext[5:])
120 def __getattr__(self, attr):
121 return getattr(self._layer, attr)
124 def __init__(self, n):
131 def __init__(self, m):
134 self.vertices = [Vertex(v) for v in self.vertices]
135 self.faces = [Face(f) for f in self.polygons]
137 self.materials = self.materials[:]
139 self.uv_layers = [UvLayer(self.uv_layers[i], self.uv_textures[i]) for i in range(len(self.uv_layers))]
140 self.assign_texture_units()
143 f.vertices = [self.vertices[i] for i in f.vertices]
146 for u in self.uv_layers:
147 f.uvs.append([u.data[f.loop_indices[i]].uv for i in range(len(f.vertices))])
149 self.edges = dict([(e.key, Edge(e)) for e in self.edges])
151 for k in f.edge_keys:
153 e.faces.append(self.faces[f.index])
156 self.lines = [Line(e) for e in self.edges.values() if not e.faces]
158 if self.use_auto_smooth:
159 smooth_limit = math.cos(self.auto_smooth_angle*math.pi/180)
163 for e in self.edges.values():
164 e.vertices = [self.vertices[i] for i in e.vertices]
165 e.check_smooth(smooth_limit)
167 def __getattr__(self, attr):
168 return getattr(self._mesh, attr)
170 def splice(self, other):
172 for m in other.materials:
173 if m in self.materials:
174 material_map.append(self.materials.index(m))
176 material_map.append(len(self.materials))
177 self.materials.append(m)
179 offset = len(self.vertices)
180 for v in other.vertices:
182 self.vertices.append(v)
184 offset = len(self.faces)
185 for f in other.faces:
188 f.material_index = material_map[f.material_index]
191 for e in other.edges.values():
192 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
193 self.edges[e.key] = e
195 self.lines += other.lines
197 def flatten_faces(self):
201 for e in self.edges.values():
204 def assign_texture_units(self):
205 # Assign texture units for any non-hidden UV layers that lack one
206 units = [u.unit for u in self.uv_layers if u.unit is not None]
208 free_unit = max(units)+1
211 for u in self.uv_layers:
217 def generate_material_uv(self):
218 self.uv_layers.append(FakeUvLayer("material_tex"))
219 self.assign_texture_units()
221 f.uvs.append([((f.material_index+0.5)/len(self.materials), 0.5)]*len(f.vertices))
223 def split_vertices(self, find_group_func, progress, *args):
225 for i in range(len(self.vertices)):
233 vg.append(find_group_func(v, f, *args))
238 progress.set_progress(i*0.5/len(self.vertices))
240 for i in range(len(self.vertices)):
241 if len(groups[i])==1:
244 for g in groups[i][1:]:
245 v = Vertex(self.vertices[i])
246 v.index = len(self.vertices)
247 self.vertices.append(v)
250 for j in range(len(f.edges)):
253 if self.vertices[i] not in e.vertices:
256 if e.other_face(f) not in g and len(e.faces)>=2:
262 del self.edges[e.key]
264 e.vertices[e.vertices.index(self.vertices[i])] = v
266 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
267 self.edges[e.key] = e
269 self.vertices[i].faces.remove(f)
270 f.vertices[f.vertices.index(self.vertices[i])] = v
274 progress.set_progress(0.5+i*0.5/len(self.vertices))
276 def split_smooth(self, progress = None):
277 self.split_vertices(self.find_smooth_group, progress)
279 def split_uv(self, index, progress = None):
280 self.split_vertices(self.find_uv_group, progress, index)
282 def find_smooth_group(self, vertex, face):
288 other = e.other_face(f)
289 if other not in vertex.faces:
299 def find_uv_group(self, vertex, face, index):
300 uv = face.uvs[index][face.vertices.index(vertex)]
303 for f in vertex.faces:
304 if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
309 def compute_normals(self):
310 for v in self.vertices:
312 v.normal = mathutils.Vector()
314 fv = f.pivot_vertices(v)
315 edge1 = fv[1].co-fv[0].co
316 edge2 = fv[-1].co-fv[0].co
318 if len(f.get_edge(fv[0], fv[1]).faces)==1:
320 if len(f.get_edge(fv[0], fv[-1]).faces)==1:
322 v.normal += f.normal*edge1.angle(edge2)*weight
325 # XXX Should use edges to compute normal
326 v.normal = mathutils.Vector(0, 0, 1)
328 def compute_uv(self):
329 for v in self.vertices:
332 i = f.vertices.index(v)
333 v.uvs = [u[i] for u in f.uvs]
335 def compute_tbn(self, index):
336 if not self.uv_layers:
339 for v in self.vertices:
340 v.tan = mathutils.Vector()
341 v.bino = mathutils.Vector()
343 fv = f.pivot_vertices(v)
344 uv0 = fv[0].uvs[index]
345 uv1 = fv[1].uvs[index]
346 uv2 = fv[-1].uvs[index]
351 edge1 = fv[1].co-fv[0].co
352 edge2 = fv[-1].co-fv[0].co
353 div = (du1*dv2-du2*dv1)
355 mul = edge1.angle(edge2)/div
356 v.tan += (edge1*dv2-edge2*dv1)*mul
357 v.bino += (edge2*du1-edge1*du2)*mul
364 def create_strip(self, face, max_len):
365 # Find an edge with another unused face next to it
368 other = e.other_face(face)
369 if other and not other.flag:
376 # Add initial vertices so that we'll complete the edge on the first
378 vertices = face.pivot_vertices(*edge.vertices)
380 result = [vertices[-1], vertices[0]]
382 result = [vertices[-2], vertices[-1]]
387 vertices = face.pivot_vertices(*result[-2:])
390 # Quads need special handling because the winding of every other
391 # triangle in the strip is reversed
392 if len(vertices)==4 and not k:
393 result.append(vertices[3])
394 result.append(vertices[2])
395 if len(vertices)==4 and k:
396 result.append(vertices[3])
398 if len(result)>=max_len:
401 # Hop over the last edge
402 edge = face.get_edge(*result[-2:])
403 face = edge.other_face(face)
404 if not face or face.flag: