+++ /dev/null
-import math
-import mathutils
-
-def make_edge_key(i1, i2):
- return (min(i1, i2), max(i1, i2))
-
-class Edge:
- def __init__(self, me):
- if me.__class__==Edge:
- self._medge = me._medge
- self.vertices = me.vertices[:]
- self.smooth = me.smooth
- else:
- self._medge = me
- self.smooth = False
- self.faces = []
-
- def __getattr__(self, attr):
- return getattr(self._medge, attr)
-
- def check_smooth(self, limit):
- if len(self.faces)!=2:
- return
-
- d = self.faces[0].normal.dot(self.faces[1].normal)
- self.smooth = ((d>limit and self.faces[0].use_smooth and self.faces[1].use_smooth) or d>0.99995)
-
- def other_face(self, f):
- if f.index==self.faces[0].index:
- if len(self.faces)>=2:
- return self.faces[1]
- else:
- return None
- else:
- return self.faces[0]
-
-
-class Vertex:
- def __init__(self, mv):
- if mv.__class__==Vertex:
- self._mvert = mv._mvert
- self.normal = mv.normal
- self.uvs = mv.uvs[:]
- self.tan = mv.tan
- self.bino = mv.bino
- else:
- self._mvert = mv
- self.uvs = []
- self.tan = None
- self.bino = None
- self.flag = False
- self.faces = []
-
- def __getattr__(self, attr):
- return getattr(self._mvert, attr)
-
- def __cmp__(self, other):
- if other is None:
- return 1
- return cmp(self.index, other.index)
-
-
-class Face:
- def __init__(self, mf):
- self._mface = mf
- self.edges = []
- self.vertices = mf.vertices[:]
- self.uvs = []
- self.flag = False
-
- def __getattr__(self, attr):
- return getattr(self._mface, attr)
-
- def __cmp__(self, other):
- if other is None:
- return 1
- return cmp(self.index, other.index)
-
- def pivot_vertices(self, *vt):
- flags = [(v in vt) for v in self.vertices]
- l = len(self.vertices)
- for i in range(l):
- if flags[i] and not flags[(i+l-1)%l]:
- return self.vertices[i:]+self.vertices[:i]
-
- def get_edge(self, v1, v2):
- key = make_edge_key(v1.index, v2.index)
- for e in self.edges:
- if e.key==key:
- return e
- raise KeyError("No edge %s"%(key,))
-
- def get_neighbors(self):
- neighbors = [e.other_face(self) for e in self.edges]
- return list(filter(bool, neighbors))
-
-
-class Line:
- def __init__(self, e):
- self.edge = e
- self.vertices = e.vertices[:]
- self.flag = False
-
-
-class UvLayer:
- def __init__(self, l, t):
- self._layer = l
- self.uvtex = t
- self.name = self.uvtex.name
- self.unit = None
- self.hidden = False
- dot = self.name.find('.')
- if dot>=0:
- ext = self.name[dot:]
- if ext.startswith(".unit") and ext[5:].isdigit():
- self.unit = int(ext[5:])
- elif ext==".hidden":
- self.hidden = True
-
- def __getattr__(self, attr):
- return getattr(self._layer, attr)
-
-class FakeUvLayer:
- def __init__(self, n):
- self.uvtex = None
- self.name = n
- self.unit = None
- self.hidden = False
-
-class Mesh:
- def __init__(self, m):
- self._mesh = m
-
- self.vertices = [Vertex(v) for v in self.vertices]
- self.faces = [Face(f) for f in self.polygons]
-
- self.materials = self.materials[:]
-
- self.uv_layers = [UvLayer(self.uv_layers[i], self.uv_textures[i]) for i in range(len(self.uv_layers))]
- self.assign_texture_units()
-
- for f in self.faces:
- f.vertices = [self.vertices[i] for i in f.vertices]
- for v in f.vertices:
- v.faces.append(f)
- for u in self.uv_layers:
- f.uvs.append([u.data[f.loop_indices[i]].uv for i in range(len(f.vertices))])
-
- self.edges = dict([(e.key, Edge(e)) for e in self.edges])
- for f in self.faces:
- for k in f.edge_keys:
- e = self.edges[k]
- e.faces.append(self.faces[f.index])
- f.edges.append(e)
-
- self.lines = [Line(e) for e in self.edges.values() if not e.faces]
-
- if self.use_auto_smooth:
- smooth_limit = math.cos(self.auto_smooth_angle*math.pi/180)
- else:
- smooth_limit = -1
-
- for e in self.edges.values():
- e.vertices = [self.vertices[i] for i in e.vertices]
- e.check_smooth(smooth_limit)
-
- def __getattr__(self, attr):
- return getattr(self._mesh, attr)
-
- def splice(self, other):
- material_map = []
- for m in other.materials:
- if m in self.materials:
- material_map.append(self.materials.index(m))
- else:
- material_map.append(len(self.materials))
- self.materials.append(m)
-
- offset = len(self.vertices)
- for v in other.vertices:
- v.index += offset
- self.vertices.append(v)
-
- offset = len(self.faces)
- for f in other.faces:
- f.index += offset
- if other.materials:
- f.material_index = material_map[f.material_index]
- self.faces.append(f)
-
- for e in other.edges.values():
- e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
- self.edges[e.key] = e
-
- self.lines += other.lines
-
- def flatten_faces(self):
- for f in self.faces:
- f.use_smooth = False
-
- for e in self.edges.values():
- e.check_smooth(1)
-
- def assign_texture_units(self):
- # Assign texture units for any non-hidden UV layers that lack one
- units = [u.unit for u in self.uv_layers if u.unit is not None]
- if units:
- free_unit = max(units)+1
- else:
- free_unit = 0
- for u in self.uv_layers:
- if u.unit is None:
- if not u.hidden:
- u.unit = free_unit
- free_unit += 1
-
- def generate_material_uv(self):
- self.uv_layers.append(FakeUvLayer("material_tex"))
- self.assign_texture_units()
- for f in self.faces:
- f.uvs.append([((f.material_index+0.5)/len(self.materials), 0.5)]*len(f.vertices))
-
- def split_vertices(self, find_group_func, progress, *args):
- groups = []
- for i in range(len(self.vertices)):
- v = self.vertices[i]
- for f in v.faces:
- f.flag = False
-
- vg = []
- for f in v.faces:
- if not f.flag:
- vg.append(find_group_func(v, f, *args))
-
- groups.append(vg)
-
- if progress:
- progress.set_progress(i*0.5/len(self.vertices))
-
- for i in range(len(self.vertices)):
- if len(groups[i])==1:
- continue
-
- for g in groups[i][1:]:
- v = Vertex(self.vertices[i])
- v.index = len(self.vertices)
- self.vertices.append(v)
-
- for f in g:
- for j in range(len(f.edges)):
- e = f.edges[j]
-
- if self.vertices[i] not in e.vertices:
- continue
-
- if e.other_face(f) not in g and len(e.faces)>=2:
- e.faces.remove(f)
- e = Edge(e)
- f.edges[j] = e
- e.faces.append(f)
- else:
- del self.edges[e.key]
-
- e.vertices[e.vertices.index(self.vertices[i])] = v
-
- e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
- self.edges[e.key] = e
-
- self.vertices[i].faces.remove(f)
- f.vertices[f.vertices.index(self.vertices[i])] = v
- v.faces.append(f)
-
- if progress:
- progress.set_progress(0.5+i*0.5/len(self.vertices))
-
- def split_smooth(self, progress = None):
- self.split_vertices(self.find_smooth_group, progress)
-
- def split_uv(self, index, progress = None):
- self.split_vertices(self.find_uv_group, progress, index)
-
- def find_smooth_group(self, vertex, face):
- face.flag = True
- queue = [face]
-
- for f in queue:
- for e in f.edges:
- other = e.other_face(f)
- if other not in vertex.faces:
- continue
-
- if e.smooth:
- if not other.flag:
- other.flag = True
- queue.append(other)
-
- return queue
-
- def find_uv_group(self, vertex, face, index):
- uv = face.uvs[index][face.vertices.index(vertex)]
- face.flag = True
- group = [face]
- for f in vertex.faces:
- if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
- f.flag = True
- group.append(f)
- return group
-
- def compute_normals(self):
- for v in self.vertices:
- if v.faces:
- v.normal = mathutils.Vector()
- for f in v.faces:
- fv = f.pivot_vertices(v)
- edge1 = fv[1].co-fv[0].co
- edge2 = fv[-1].co-fv[0].co
- weight = 1
- if len(f.get_edge(fv[0], fv[1]).faces)==1:
- weight += 1
- if len(f.get_edge(fv[0], fv[-1]).faces)==1:
- weight += 1
- v.normal += f.normal*edge1.angle(edge2)*weight
- v.normal.normalize()
- else:
- # XXX Should use edges to compute normal
- v.normal = mathutils.Vector(0, 0, 1)
-
- def compute_uv(self):
- for v in self.vertices:
- if v.faces:
- f = v.faces[0]
- i = f.vertices.index(v)
- v.uvs = [u[i] for u in f.uvs]
-
- def compute_tbn(self, index):
- if not self.uv_layers:
- return
-
- for v in self.vertices:
- v.tan = mathutils.Vector()
- v.bino = mathutils.Vector()
- for f in v.faces:
- fv = f.pivot_vertices(v)
- uv0 = fv[0].uvs[index]
- uv1 = fv[1].uvs[index]
- uv2 = fv[-1].uvs[index]
- du1 = uv1[0]-uv0[0]
- du2 = uv2[0]-uv0[0]
- dv1 = uv1[1]-uv0[1]
- dv2 = uv2[1]-uv0[1]
- edge1 = fv[1].co-fv[0].co
- edge2 = fv[-1].co-fv[0].co
- div = (du1*dv2-du2*dv1)
- if div:
- mul = edge1.angle(edge2)/div
- v.tan += (edge1*dv2-edge2*dv1)*mul
- v.bino += (edge2*du1-edge1*du2)*mul
-
- if v.tan.length:
- v.tan.normalize()
- if v.bino.length:
- v.bino.normalize()
-
- def create_strip(self, face, max_len):
- # Find an edge with another unused face next to it
- edge = None
- for e in face.edges:
- other = e.other_face(face)
- if other and not other.flag:
- edge = e
- break
-
- if not edge:
- return None
-
- # Add initial vertices so that we'll complete the edge on the first
- # iteration
- vertices = face.pivot_vertices(*edge.vertices)
- if len(vertices)==3:
- result = [vertices[-1], vertices[0]]
- else:
- result = [vertices[-2], vertices[-1]]
-
- while 1:
- face.flag = True
-
- vertices = face.pivot_vertices(*result[-2:])
- k = len(result)%2
-
- # Quads need special handling because the winding of every other
- # triangle in the strip is reversed
- if len(vertices)==4 and not k:
- result.append(vertices[3])
- result.append(vertices[2])
- if len(vertices)==4 and k:
- result.append(vertices[3])
-
- if len(result)>=max_len:
- break
-
- # Hop over the last edge
- edge = face.get_edge(*result[-2:])
- face = edge.other_face(face)
- if not face or face.flag:
- break
-
- return result