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 if (d>limit and self.faces[0].use_smooth and self.faces[1].use_smooth) or d>0.999:
29 def other_face(self, f):
30 if f.index==self.faces[0].index:
31 if len(self.faces)>=2:
40 def __init__(self, mv):
41 if mv.__class__==Vertex:
42 self._mvert = mv._mvert
43 self.normal = mv.normal
53 def __getattr__(self, attr):
54 return getattr(self._mvert, attr)
56 def __cmp__(self, other):
59 return cmp(self.index, other.index)
63 def __init__(self, mf):
66 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,))
95 def __init__(self, e):
97 self.vertices = e.vertices[:]
102 def __init__(self, m):
104 self.vertices = [Vertex(v) for v in m.vertices]
105 self.faces = [Face(f) for f in m.faces]
106 self.materials = m.materials[:]
111 uvtex = self.uv_textures[0]
115 f.vertices = [self.vertices[i] for i in f.vertices]
117 f.uv = uvtex.data[f.index].uv
121 self.edges = dict([(e.key, Edge(e)) for e in m.edges])
123 for k in f.edge_keys:
125 e.faces.append(self.faces[f.index])
128 self.lines = [Line(e) for e in self.edges.values() if not e.faces]
130 if m.use_auto_smooth:
131 smooth_limit = math.cos(m.auto_smooth_angle*math.pi/180)
135 for e in self.edges.values():
136 e.vertices = [self.vertices[i] for i in e.vertices]
137 e.check_smooth(smooth_limit)
139 def __getattr__(self, attr):
140 return getattr(self._mesh, attr)
142 def splice(self, other):
144 for m in other.materials:
145 if m in self.materials:
146 material_map.append(self.materials.index(m))
148 material_map.append(len(self.materials))
149 self.materials.append(m)
151 offset = len(self.vertices)
152 for v in other.vertices:
154 self.vertices.append(v)
156 offset = len(self.faces)
157 for f in other.faces:
159 f.material = material_map[f.material_index]
162 for e in other.edges.values():
163 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
164 self.edges[e.key] = e
166 self.lines += other.lines
168 def generate_material_uv(self):
170 f.uv = ([(f.material_index+0.5)/len(self.materials), 0.5],)*len(f.vertices)
173 def split_vertices(self, find_group_func, progress = None):
175 for i in range(len(self.vertices)):
183 vg.append(find_group_func(v, f))
188 progress.set_progress(i*0.5/len(self.vertices))
190 for i in range(len(self.vertices)):
191 if len(groups[i])==1:
194 for g in groups[i][1:]:
195 v = Vertex(self.vertices[i])
196 v.index = len(self.vertices)
197 self.vertices.append(v)
200 for j in range(len(f.edges)):
203 if self.vertices[i] not in e.vertices:
206 if e.other_face(f) not in g and len(e.faces)>=2:
213 del self.edges[e.key]
215 e.vertices[e.vertices.index(self.vertices[i])] = v
217 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
218 self.edges[e.key] = e
220 self.vertices[i].faces.remove(f)
221 f.vertices[f.vertices.index(self.vertices[i])] = v
225 progress.set_progress(0.5+i*0.5/len(self.vertices))
227 def split_smooth(self, progress = None):
228 self.split_vertices(self.find_smooth_group, progress)
230 def split_uv(self, progress = None):
231 self.split_vertices(self.find_uv_group, progress)
233 def find_smooth_group(self, vertex, face):
239 other = e.other_face(f)
240 #if not other or other.index not in face_indices:
241 if other not in vertex.faces:
251 def find_uv_group(self, vertex, face):
252 uv = face.uv[face.vertices.index(vertex)]
255 for f in vertex.faces:
256 if not f.flag and f.uv[f.vertices.index(vertex)]==uv:
261 def compute_normals(self):
262 for v in self.vertices:
264 v.normal = mathutils.Vector()
269 # XXX Should use edges to compute normal
270 v.normal = mathutils.Vector(0, 0, 1)
272 def compute_uv(self):
273 for v in self.vertices:
275 v.uv = v.faces[0].uv[v.faces[0].vertices.index(v)]
277 def compute_tbn(self):
278 for v in self.vertices:
279 v.tan = mathutils.Vector()
280 v.bino = mathutils.Vector()
282 fv = f.pivot_vertices(False, v)
285 du1 = v1.uv[0]-v.uv[0]
286 du2 = v2.uv[0]-v.uv[0]
287 dv1 = v1.uv[1]-v.uv[1]
288 dv2 = v2.uv[1]-v.uv[1]
289 div = du1*dv2-du2*dv1
290 edge1 = fv[1].co-fv[0].co
291 edge2 = fv[-1].co-fv[0].co
293 v.tan += (edge1*dv2-edge2*dv1)/div
294 v.bino += (edge2*du1-edge1*du2)/div
300 def create_strip(self, face, max_len):
303 other = e.other_face(face)
304 if other and not other.flag:
311 vertices = face.pivot_vertices(*edge.vertices)
313 result = [vertices[-1], vertices[0]]
315 result = [vertices[-2], vertices[-1]]
318 vertices = face.pivot_vertices(*result[-2:])
322 if len(vertices)==4 and not k:
323 result.append(vertices[3])
324 result.append(vertices[2])
325 if len(vertices)==4 and k:
326 result.append(vertices[3])
328 if len(result)>=max_len:
331 edge = face.get_edge(*result[-2:])
333 next = edge.other_face(face)
334 if not next or next.flag: