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
52 def __getattr__(self, attr):
53 return getattr(self._mvert, attr)
55 def __cmp__(self, other):
58 return cmp(self.index, other.index)
62 def __init__(self, mf):
65 self.vertices = mf.vertices[:]
70 def __getattr__(self, attr):
71 return getattr(self._mface, attr)
73 def __cmp__(self, other):
76 return cmp(self.index, other.index)
78 def pivot_vertices(self, *vt):
79 flags = [(v in vt) for v in self.vertices]
80 l = len(self.vertices)
82 if flags[i] and not flags[(i+l-1)%l]:
83 return self.vertices[i:]+self.vertices[:i]
85 def get_edge(self, v1, v2):
86 key = make_edge_key(v1.index, v2.index)
90 raise KeyError("No edge %s"%(key,))
94 def __init__(self, e):
96 self.vertices = e.vertices[:]
100 def uvtex_unit_number(uvtex):
101 dot = uvtex.name.find('.')
102 if dot!=-1 and uvtex.name[dot+1:dot+5]=="unit" and uvtex.name[dot+5:].isdigit():
103 return int(uvtex.name[dot+5])
108 def __init__(self, m):
111 self.vertices = [Vertex(v) for v in self.vertices]
112 self.faces = [Face(f) for f in self.faces]
114 self.materials = self.materials[:]
116 self.uv_textures = [u for u in self.uv_textures if not u.name.endswith(".hidden")]
117 self.uv_textures.sort(key=uvtex_unit_number)
120 f.vertices = [self.vertices[i] for i in f.vertices]
123 for u in self.uv_textures:
124 r = u.data[f.index].uv_raw;
125 f.uvs.append([(r[i], r[i+1]) for i in range(0, 8, 2)])
127 self.edges = dict([(e.key, Edge(e)) for e in self.edges])
129 for k in f.edge_keys:
131 e.faces.append(self.faces[f.index])
134 self.lines = [Line(e) for e in self.edges.values() if not e.faces]
136 if self.use_auto_smooth:
137 smooth_limit = math.cos(self.auto_smooth_angle*math.pi/180)
141 for e in self.edges.values():
142 e.vertices = [self.vertices[i] for i in e.vertices]
143 e.check_smooth(smooth_limit)
145 def __getattr__(self, attr):
146 return getattr(self._mesh, attr)
148 def splice(self, other):
150 for m in other.materials:
151 if m in self.materials:
152 material_map.append(self.materials.index(m))
154 material_map.append(len(self.materials))
155 self.materials.append(m)
157 offset = len(self.vertices)
158 for v in other.vertices:
160 self.vertices.append(v)
162 offset = len(self.faces)
163 for f in other.faces:
165 f.material = material_map[f.material_index]
168 for e in other.edges.values():
169 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
170 self.edges[e.key] = e
172 self.lines += other.lines
174 def flatten_faces(self):
178 for e in self.edges.values():
181 def generate_material_uv(self):
183 f.uv = ([(f.material_index+0.5)/len(self.materials), 0.5],)*len(f.vertices)
186 def split_vertices(self, find_group_func, progress, *args):
188 for i in range(len(self.vertices)):
196 vg.append(find_group_func(v, f, *args))
201 progress.set_progress(i*0.5/len(self.vertices))
203 for i in range(len(self.vertices)):
204 if len(groups[i])==1:
207 for g in groups[i][1:]:
208 v = Vertex(self.vertices[i])
209 v.index = len(self.vertices)
210 self.vertices.append(v)
213 for j in range(len(f.edges)):
216 if self.vertices[i] not in e.vertices:
219 if e.other_face(f) not in g and len(e.faces)>=2:
225 del self.edges[e.key]
227 e.vertices[e.vertices.index(self.vertices[i])] = v
229 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
230 self.edges[e.key] = e
232 self.vertices[i].faces.remove(f)
233 f.vertices[f.vertices.index(self.vertices[i])] = v
237 progress.set_progress(0.5+i*0.5/len(self.vertices))
239 def split_smooth(self, progress = None):
240 self.split_vertices(self.find_smooth_group, progress)
242 def split_uv(self, index, progress = None):
243 self.split_vertices(self.find_uv_group, progress, index)
245 def find_smooth_group(self, vertex, face):
251 other = e.other_face(f)
252 if other not in vertex.faces:
262 def find_uv_group(self, vertex, face, index):
263 uv = face.uvs[index][face.vertices.index(vertex)]
266 for f in vertex.faces:
267 if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
272 def compute_normals(self):
273 for v in self.vertices:
275 v.normal = mathutils.Vector()
277 fv = f.pivot_vertices(v)
278 edge1 = fv[1].co-fv[0].co
279 edge2 = fv[-1].co-fv[0].co
281 if len(f.get_edge(fv[0], fv[1]).faces)==1:
283 if len(f.get_edge(fv[0], fv[-1]).faces)==1:
285 v.normal += f.normal*edge1.angle(edge2)*weight
288 # XXX Should use edges to compute normal
289 v.normal = mathutils.Vector(0, 0, 1)
291 def compute_uv(self):
292 for v in self.vertices:
295 i = f.vertices.index(v)
296 v.uvs = [u[i] for u in f.uvs]
298 def compute_tbn(self, uvtex):
299 if not self.uv_textures:
302 uvtex_names = [u.name for u in self.uv_textures]
303 if uvtex in uvtex_names:
304 uvtex_index = uvtex_names.index(uvtex)
308 for v in self.vertices:
309 v.tan = mathutils.Vector()
310 v.bino = mathutils.Vector()
312 fv = f.pivot_vertices(v)
313 uv0 = fv[0].uvs[uvtex_index]
314 uv1 = fv[1].uvs[uvtex_index]
315 uv2 = fv[-1].uvs[uvtex_index]
320 edge1 = fv[1].co-fv[0].co
321 edge2 = fv[-1].co-fv[0].co
322 div = (du1*dv2-du2*dv1)
324 mul = edge1.angle(edge2)/div
325 v.tan += (edge1*dv2-edge2*dv1)*mul
326 v.bino += (edge2*du1-edge1*du2)*mul
333 def create_strip(self, face, max_len):
336 other = e.other_face(face)
337 if other and not other.flag:
344 vertices = face.pivot_vertices(*edge.vertices)
346 result = [vertices[-1], vertices[0]]
348 result = [vertices[-2], vertices[-1]]
351 vertices = face.pivot_vertices(*result[-2:])
355 if len(vertices)==4 and not k:
356 result.append(vertices[3])
357 result.append(vertices[2])
358 if len(vertices)==4 and k:
359 result.append(vertices[3])
361 if len(result)>=max_len:
364 edge = face.get_edge(*result[-2:])
366 next = edge.other_face(face)
367 if not next or next.flag: