]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/mesh.py
Add properties to export materials as texture array layers
[libs/gl.git] / blender / io_mspgl / mesh.py
index aa8df8d254665d4240b50a34934753515a56e96c..d05b9de844d91eeccf113fe9e961386041fb3baf 100644 (file)
@@ -34,20 +34,31 @@ class Edge:
                else:
                        return self.faces[0]
 
+       def other_vertex(self, v):
+               if v.index==self.vertices[0].index:
+                       return self.vertices[1]
+               else:
+                       return self.vertices[0]
+
 
 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
                else:
                        self._mvert = mv
+                       self.co = mv.co
+                       self.normal = mv.normal
                        self.uvs = []
                        self.tan = None
                        self.bino = None
+                       self.group_weight_scale = 1
                self.flag = False
                self.faces = []
 
@@ -140,11 +151,20 @@ class Mesh:
                self.assign_texture_units()
 
                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<len(self.materials):
+                               mat = self.materials[f.material_index]
+                               if mat and mat.array_atlas:
+                                       layer = (mat.array_layer,)
+                                       print(f.uvs, layer)
+                                       for i in range(len(f.uvs)):
+                                               f.uvs[i] = [mathutils.Vector(tuple(u)+layer) for u in f.uvs[i]]
 
                self.edges = dict([(e.key, Edge(e)) for e in self.edges])
                for f in self.faces:
@@ -154,9 +174,11 @@ class Mesh:
                                f.edges.append(e)
 
                self.lines = [Line(e) for e in self.edges.values() if not e.faces]
+               for l in self.lines:
+                       l.vertices = [self.vertices[i] for i in l.vertices]
 
                if self.use_auto_smooth:
-                       smooth_limit = math.cos(self.auto_smooth_angle*math.pi/180)
+                       smooth_limit = math.cos(self.auto_smooth_angle)
                else:
                        smooth_limit = -1
 
@@ -167,6 +189,10 @@ class Mesh:
        def __getattr__(self, attr):
                return getattr(self._mesh, attr)
 
+       def transform(self, matrix):
+               for v in self.vertices:
+                       v.co = matrix*v.co
+
        def splice(self, other):
                material_map = []
                for m in other.materials:
@@ -246,26 +272,35 @@ class Mesh:
                                v.index = len(self.vertices)
                                self.vertices.append(v)
 
+                               v_edges = []
+                               v_edge_keys = set()
                                for f in g:
-                                       for j in range(len(f.edges)):
-                                               e = f.edges[j]
-
-                                               if self.vertices[i] not in e.vertices:
+                                       for e in f.edges:
+                                               if e.key in v_edge_keys or 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_faces_in_g = [c for c in e.faces if c in g]
+                                               boundary = len(e_faces_in_g)<len(e.faces)
+                                               v_edges.append((e, boundary, e_faces_in_g))
+                                               v_edge_keys.add(e.key)
 
-                                               e.vertices[e.vertices.index(self.vertices[i])] = v
+                               for e, boundary, e_faces_in_g in v_edges:
+                                       if boundary:
+                                               ne = Edge(e)
+                                               for c in e_faces_in_g:
+                                                       e.faces.remove(c)
+                                                       c.edges[c.edges.index(e)] = ne
+                                                       ne.faces.append(c)
+                                               e = ne
+                                       else:
+                                               del self.edges[e.key]
 
-                                               e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
-                                               self.edges[e.key] = e
+                                       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
+
+                               for f in g:
                                        self.vertices[i].faces.remove(f)
                                        f.vertices[f.vertices.index(self.vertices[i])] = v
                                        v.faces.append(f)
@@ -273,10 +308,10 @@ class Mesh:
                        if progress:
                                progress.set_progress(0.5+i*0.5/len(self.vertices))
 
-       def split_smooth(self, progress = None):
+       def split_smooth(self, progress=None):
                self.split_vertices(self.find_smooth_group, progress)
 
-       def split_uv(self, index, progress = None):
+       def split_uv(self, index, progress=None):
                self.split_vertices(self.find_uv_group, progress, index)
 
        def find_smooth_group(self, vertex, face):
@@ -314,16 +349,20 @@ class Mesh:
                                        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()
+                                       if edge1.length and edge2.length:
+                                               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
+                               if v.normal.length:
+                                       v.normal.normalize()
+                               else:
+                                       v.normal = mathutils.Vector((0, 0, 1))
                        else:
                                # XXX Should use edges to compute normal
-                               v.normal = mathutils.Vector(0, 0, 1)
+                               v.normal = mathutils.Vector((0, 0, 1))
 
        def compute_uv(self):
                for v in self.vertices:
@@ -331,6 +370,8 @@ class Mesh:
                                f = v.faces[0]
                                i = f.vertices.index(v)
                                v.uvs = [u[i] for u in f.uvs]
+                       else:
+                               v.uvs = [(0.0, 0.0)]*len(self.uv_layers)
 
        def compute_tbn(self, index):
                if not self.uv_layers:
@@ -361,6 +402,12 @@ class Mesh:
                        if v.bino.length:
                                v.bino.normalize()
 
+       def sort_vertex_groups(self, max_groups):
+               for v in self.vertices:
+                       if v.groups:
+                               v.groups = sorted(v.groups, key=(lambda g: g.weight), reverse=True)
+                               v.group_weight_scale = 1.0/sum(g.weight for g in v.groups[:max_groups])
+
        def create_strip(self, face, max_len):
                # Find an edge with another unused face next to it
                edge = None