X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=blender%2Fio_mspgl%2Fmesh.py;h=44fbdcb0115b20368ff6499fb6b0c075def650a2;hb=2424c57d12547c4361965ce5ec8be6eaa4ac212f;hp=d05b9de844d91eeccf113fe9e961386041fb3baf;hpb=1d0b0fcb7ad573053a8730a90f7fb33061038db2;p=libs%2Fgl.git diff --git a/blender/io_mspgl/mesh.py b/blender/io_mspgl/mesh.py index d05b9de8..44fbdcb0 100644 --- a/blender/io_mspgl/mesh.py +++ b/blender/io_mspgl/mesh.py @@ -1,3 +1,4 @@ +import bpy import math import mathutils @@ -5,18 +6,19 @@ 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 + def __init__(self, edge): + if edge.__class__==Edge: + self._edge = edge._edge + self.vertices = edge.vertices[:] + self.smooth = edge.smooth else: - self._medge = me + self._edge = edge self.smooth = False + self.key = edge.key self.faces = [] def __getattr__(self, attr): - return getattr(self._medge, attr) + return getattr(self._edge, attr) def check_smooth(self, limit): if len(self.faces)!=2: @@ -42,28 +44,27 @@ class Edge: class Vertex: - def __init__(self, mv): - if mv.__class__==Vertex: - self._mvert = mv._mvert - self.co = mv.co - self.normal = mv.normal - self.uvs = mv.uvs[:] - self.tan = mv.tan - self.bino = mv.bino - self.group_weight_scale = mv.group_weight_scale + def __init__(self, vertex): + if vertex.__class__==Vertex: + self._vertex = vertex._vertex + self.uvs = vertex.uvs[:] + self.tan = vertex.tan + self.bino = vertex.bino else: - self._mvert = mv - self.co = mv.co - self.normal = mv.normal + self._vertex = vertex self.uvs = [] self.tan = None self.bino = None - self.group_weight_scale = 1 + self.index = vertex.index + self.co = vertex.co + self.normal = vertex.normal self.flag = False + self.edges = [] self.faces = [] + self.groups = vertex.groups[:] def __getattr__(self, attr): - return getattr(self._mvert, attr) + return getattr(self._vertex, attr) def __cmp__(self, other): if other is None: @@ -71,22 +72,37 @@ class Vertex: return cmp(self.index, other.index) +class VertexGroup: + def __init__(self, group): + self._group = group + self.group = group.group + self.weight = group.weight + + def __getattr__(self, attr): + return getattr(self._group, attr) + + class Face: - def __init__(self, mf): - self._mface = mf + def __init__(self, face): + self._face = face + self.index = face.index self.edges = [] - self.vertices = mf.vertices[:] + self.vertices = face.vertices[:] self.uvs = [] self.flag = False def __getattr__(self, attr): - return getattr(self._mface, attr) + return getattr(self._face, attr) def __cmp__(self, other): if other is None: return 1 return cmp(self.index, other.index) + def pivot_vertex(self, v): + n = self.vertices.index(v) + return [(n+i)%len(self.vertices) for i in range(len(self.vertices))] + def pivot_vertices(self, *vt): flags = [(v in vt) for v in self.vertices] l = len(self.vertices) @@ -101,6 +117,11 @@ class Face: return e raise KeyError("No edge %s"%(key,)) + def other_edge(self, e, v): + for d in self.edges: + if d!=e and v in d.vertices: + return d + def get_neighbors(self): neighbors = [e.other_face(self) for e in self.edges] return list(filter(bool, neighbors)) @@ -114,12 +135,18 @@ class Line: class UvLayer: - def __init__(self, l, t): - self._layer = l - self.uvtex = t - self.name = self.uvtex.name + def __init__(self, arg): + if type(arg)==str: + self._layer = None + self.name = arg + else: + self._layer = arg + self.name = arg.name + self.uvs = [d.uv for d in self.data] + self.unit = None self.hidden = False + dot = self.name.find('.') if dot>=0: ext = self.name[dot:] @@ -131,60 +158,62 @@ class UvLayer: 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 + def __init__(self, mesh): + self._mesh = mesh + + self.winding_test = mesh.winding_test + self.tbn_vecs = mesh.tbn_vecs + self.vertex_groups = mesh.vertex_groups self.vertices = [Vertex(v) for v in self.vertices] self.faces = [Face(f) for f in self.polygons] + self.edges = [Edge(e) for e in self.edges] + self.loops = self.loops[:] self.materials = self.materials[:] + if self.use_uv=='NONE' or not self.uv_layers: + self.uv_layers = [] + else: + self.uv_layers = [UvLayer(u) for u in self.uv_layers] + self.uv_layers = sorted([u for u in self.uv_layers if not u.hidden], key=(lambda u: (u.unit or 1000, u.name))) + + if self.use_uv=='UNIT0': + self.uv_layers = [self.uv_layers[0]] - self.uv_layers = [UvLayer(self.uv_layers[i], self.uv_textures[i]) for i in range(len(self.uv_layers))] - self.assign_texture_units() + next_unit = max((u.unit+1 for u in self.uv_layers if u.unit is not None), default=0) + for u in self.uv_layers: + if not u.unit: + u.unit = next_unit + next_unit += 1 + for v in self.vertices: + v.groups = [VertexGroup(g) for g in v.groups] + + edge_map = {e.key: e for e in self.edges} for f in self.faces: if len(f.vertices)>4: raise ValueError("Ngons are not supported") + 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))]) - if f.material_index