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,))
95 def __init__(self, e):
97 self.vertices = e.vertices[:]
102 def __init__(self, l):
106 def __getattr__(self, attr):
107 return getattr(self._layer, attr)
110 def __init__(self, n):
113 def uvtex_unit_number(uvtex):
114 dot = uvtex.name.find('.')
115 if dot!=-1 and uvtex.name[dot+1:dot+5]=="unit" and uvtex.name[dot+5:].isdigit():
116 return int(uvtex.name[dot+5])
121 def __init__(self, m):
124 self.vertices = [Vertex(v) for v in self.vertices]
125 self.faces = [Face(f) for f in self.polygons]
127 self.materials = self.materials[:]
129 self.uv_layers = [UvLayer(u) for u in self.uv_layers]
130 for i in range(len(self.uv_layers)):
131 self.uv_layers[i].name = self.uv_textures[i].name
132 self.uv_layers = [u for u in self.uv_layers if not u.name.endswith(".hidden")]
133 self.uv_layers.sort(key=uvtex_unit_number)
136 f.vertices = [self.vertices[i] for i in f.vertices]
139 for u in self.uv_layers:
140 f.uvs.append([u.data[f.loop_indices[i]].uv for i in range(len(f.vertices))])
142 self.edges = dict([(e.key, Edge(e)) for e in self.edges])
144 for k in f.edge_keys:
146 e.faces.append(self.faces[f.index])
149 self.lines = [Line(e) for e in self.edges.values() if not e.faces]
151 if self.use_auto_smooth:
152 smooth_limit = math.cos(self.auto_smooth_angle*math.pi/180)
156 for e in self.edges.values():
157 e.vertices = [self.vertices[i] for i in e.vertices]
158 e.check_smooth(smooth_limit)
160 def __getattr__(self, attr):
161 return getattr(self._mesh, attr)
163 def splice(self, other):
165 for m in other.materials:
166 if m in self.materials:
167 material_map.append(self.materials.index(m))
169 material_map.append(len(self.materials))
170 self.materials.append(m)
172 offset = len(self.vertices)
173 for v in other.vertices:
175 self.vertices.append(v)
177 offset = len(self.faces)
178 for f in other.faces:
181 f.material_index = material_map[f.material_index]
184 for e in other.edges.values():
185 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
186 self.edges[e.key] = e
188 self.lines += other.lines
190 def flatten_faces(self):
194 for e in self.edges.values():
197 def generate_material_uv(self):
198 self.uv_layers.append(FakeUvLayer("material_tex"))
200 f.uvs.append([((f.material_index+0.5)/len(self.materials), 0.5)]*len(f.vertices))
202 def split_vertices(self, find_group_func, progress, *args):
204 for i in range(len(self.vertices)):
212 vg.append(find_group_func(v, f, *args))
217 progress.set_progress(i*0.5/len(self.vertices))
219 for i in range(len(self.vertices)):
220 if len(groups[i])==1:
223 for g in groups[i][1:]:
224 v = Vertex(self.vertices[i])
225 v.index = len(self.vertices)
226 self.vertices.append(v)
229 for j in range(len(f.edges)):
232 if self.vertices[i] not in e.vertices:
235 if e.other_face(f) not in g and len(e.faces)>=2:
241 del self.edges[e.key]
243 e.vertices[e.vertices.index(self.vertices[i])] = v
245 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
246 self.edges[e.key] = e
248 self.vertices[i].faces.remove(f)
249 f.vertices[f.vertices.index(self.vertices[i])] = v
253 progress.set_progress(0.5+i*0.5/len(self.vertices))
255 def split_smooth(self, progress = None):
256 self.split_vertices(self.find_smooth_group, progress)
258 def split_uv(self, index, progress = None):
259 self.split_vertices(self.find_uv_group, progress, index)
261 def find_smooth_group(self, vertex, face):
267 other = e.other_face(f)
268 if other not in vertex.faces:
278 def find_uv_group(self, vertex, face, index):
279 uv = face.uvs[index][face.vertices.index(vertex)]
282 for f in vertex.faces:
283 if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
288 def compute_normals(self):
289 for v in self.vertices:
291 v.normal = mathutils.Vector()
293 fv = f.pivot_vertices(v)
294 edge1 = fv[1].co-fv[0].co
295 edge2 = fv[-1].co-fv[0].co
297 if len(f.get_edge(fv[0], fv[1]).faces)==1:
299 if len(f.get_edge(fv[0], fv[-1]).faces)==1:
301 v.normal += f.normal*edge1.angle(edge2)*weight
304 # XXX Should use edges to compute normal
305 v.normal = mathutils.Vector(0, 0, 1)
307 def compute_uv(self):
308 for v in self.vertices:
311 i = f.vertices.index(v)
312 v.uvs = [u[i] for u in f.uvs]
314 def compute_tbn(self, index):
315 if not self.uv_layers:
318 for v in self.vertices:
319 v.tan = mathutils.Vector()
320 v.bino = mathutils.Vector()
322 fv = f.pivot_vertices(v)
323 uv0 = fv[0].uvs[index]
324 uv1 = fv[1].uvs[index]
325 uv2 = fv[-1].uvs[index]
330 edge1 = fv[1].co-fv[0].co
331 edge2 = fv[-1].co-fv[0].co
332 div = (du1*dv2-du2*dv1)
334 mul = edge1.angle(edge2)/div
335 v.tan += (edge1*dv2-edge2*dv1)*mul
336 v.bino += (edge2*du1-edge1*du2)*mul
343 def create_strip(self, face, max_len):
346 other = e.other_face(face)
347 if other and not other.flag:
354 vertices = face.pivot_vertices(*edge.vertices)
356 result = [vertices[-1], vertices[0]]
358 result = [vertices[-2], vertices[-1]]
361 vertices = face.pivot_vertices(*result[-2:])
365 if len(vertices)==4 and not k:
366 result.append(vertices[3])
367 result.append(vertices[2])
368 if len(vertices)==4 and k:
369 result.append(vertices[3])
371 if len(result)>=max_len:
374 edge = face.get_edge(*result[-2:])
376 next = edge.other_face(face)
377 if not next or next.flag: