]> git.tdb.fi Git - libs/gl.git/commitdiff
Deal with some irregularities in the Blender exporter
authorMikko Rasa <tdb@tdb.fi>
Wed, 11 Sep 2013 09:06:30 +0000 (12:06 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 11 Sep 2013 09:06:30 +0000 (12:06 +0300)
Things like loose vertices, non-manifold edges or zero-length edges should
no longer cause crashes.

blender/io_mspgl/mesh.py

index a41cbc1311c8a9c077ff78980ecfb1e48a37b4e3..88758730881d0c734373ae081b50ea48b60cf760 100644 (file)
@@ -34,6 +34,12 @@ 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):
@@ -248,26 +254,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)
@@ -316,16 +331,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:
@@ -333,6 +352,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: