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, t):
105 self.name = self.uvtex.name
108 dot = self.name.find('.')
110 ext = self.name[dot:]
111 if ext.startswith(".unit") and ext[5:].isdigit():
112 self.unit = int(ext[5:])
116 def __getattr__(self, attr):
117 return getattr(self._layer, attr)
120 def __init__(self, n):
127 def __init__(self, m):
130 self.vertices = [Vertex(v) for v in self.vertices]
131 self.faces = [Face(f) for f in self.polygons]
133 self.materials = self.materials[:]
135 self.uv_layers = [UvLayer(self.uv_layers[i], self.uv_textures[i]) for i in range(len(self.uv_layers))]
136 self.assign_texture_units()
139 f.vertices = [self.vertices[i] for i in f.vertices]
142 for u in self.uv_layers:
143 f.uvs.append([u.data[f.loop_indices[i]].uv for i in range(len(f.vertices))])
145 self.edges = dict([(e.key, Edge(e)) for e in self.edges])
147 for k in f.edge_keys:
149 e.faces.append(self.faces[f.index])
152 self.lines = [Line(e) for e in self.edges.values() if not e.faces]
154 if self.use_auto_smooth:
155 smooth_limit = math.cos(self.auto_smooth_angle*math.pi/180)
159 for e in self.edges.values():
160 e.vertices = [self.vertices[i] for i in e.vertices]
161 e.check_smooth(smooth_limit)
163 def __getattr__(self, attr):
164 return getattr(self._mesh, attr)
166 def splice(self, other):
168 for m in other.materials:
169 if m in self.materials:
170 material_map.append(self.materials.index(m))
172 material_map.append(len(self.materials))
173 self.materials.append(m)
175 offset = len(self.vertices)
176 for v in other.vertices:
178 self.vertices.append(v)
180 offset = len(self.faces)
181 for f in other.faces:
184 f.material_index = material_map[f.material_index]
187 for e in other.edges.values():
188 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
189 self.edges[e.key] = e
191 self.lines += other.lines
193 def flatten_faces(self):
197 for e in self.edges.values():
200 def assign_texture_units(self):
201 # Assign texture units for any non-hidden UV layers that lack one
202 units = [u.unit for u in self.uv_layers if u.unit is not None]
204 free_unit = max(units)+1
207 for u in self.uv_layers:
213 def generate_material_uv(self):
214 self.uv_layers.append(FakeUvLayer("material_tex"))
215 self.assign_texture_units()
217 f.uvs.append([((f.material_index+0.5)/len(self.materials), 0.5)]*len(f.vertices))
219 def split_vertices(self, find_group_func, progress, *args):
221 for i in range(len(self.vertices)):
229 vg.append(find_group_func(v, f, *args))
234 progress.set_progress(i*0.5/len(self.vertices))
236 for i in range(len(self.vertices)):
237 if len(groups[i])==1:
240 for g in groups[i][1:]:
241 v = Vertex(self.vertices[i])
242 v.index = len(self.vertices)
243 self.vertices.append(v)
246 for j in range(len(f.edges)):
249 if self.vertices[i] not in e.vertices:
252 if e.other_face(f) not in g and len(e.faces)>=2:
258 del self.edges[e.key]
260 e.vertices[e.vertices.index(self.vertices[i])] = v
262 e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
263 self.edges[e.key] = e
265 self.vertices[i].faces.remove(f)
266 f.vertices[f.vertices.index(self.vertices[i])] = v
270 progress.set_progress(0.5+i*0.5/len(self.vertices))
272 def split_smooth(self, progress = None):
273 self.split_vertices(self.find_smooth_group, progress)
275 def split_uv(self, index, progress = None):
276 self.split_vertices(self.find_uv_group, progress, index)
278 def find_smooth_group(self, vertex, face):
284 other = e.other_face(f)
285 if other not in vertex.faces:
295 def find_uv_group(self, vertex, face, index):
296 uv = face.uvs[index][face.vertices.index(vertex)]
299 for f in vertex.faces:
300 if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
305 def compute_normals(self):
306 for v in self.vertices:
308 v.normal = mathutils.Vector()
310 fv = f.pivot_vertices(v)
311 edge1 = fv[1].co-fv[0].co
312 edge2 = fv[-1].co-fv[0].co
314 if len(f.get_edge(fv[0], fv[1]).faces)==1:
316 if len(f.get_edge(fv[0], fv[-1]).faces)==1:
318 v.normal += f.normal*edge1.angle(edge2)*weight
321 # XXX Should use edges to compute normal
322 v.normal = mathutils.Vector(0, 0, 1)
324 def compute_uv(self):
325 for v in self.vertices:
328 i = f.vertices.index(v)
329 v.uvs = [u[i] for u in f.uvs]
331 def compute_tbn(self, index):
332 if not self.uv_layers:
335 for v in self.vertices:
336 v.tan = mathutils.Vector()
337 v.bino = mathutils.Vector()
339 fv = f.pivot_vertices(v)
340 uv0 = fv[0].uvs[index]
341 uv1 = fv[1].uvs[index]
342 uv2 = fv[-1].uvs[index]
347 edge1 = fv[1].co-fv[0].co
348 edge2 = fv[-1].co-fv[0].co
349 div = (du1*dv2-du2*dv1)
351 mul = edge1.angle(edge2)/div
352 v.tan += (edge1*dv2-edge2*dv1)*mul
353 v.bino += (edge2*du1-edge1*du2)*mul
360 def create_strip(self, face, max_len):
363 other = e.other_face(face)
364 if other and not other.flag:
371 vertices = face.pivot_vertices(*edge.vertices)
373 result = [vertices[-1], vertices[0]]
375 result = [vertices[-2], vertices[-1]]
378 vertices = face.pivot_vertices(*result[-2:])
382 if len(vertices)==4 and not k:
383 result.append(vertices[3])
384 result.append(vertices[2])
385 if len(vertices)==4 and k:
386 result.append(vertices[3])
388 if len(result)>=max_len:
391 edge = face.get_edge(*result[-2:])
393 next = edge.other_face(face)
394 if not next or next.flag: