]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_mesh.py
Allow flagging meshes for winding test in Blender
[libs/gl.git] / blender / io_mspgl / export_mesh.py
index 7ce501bced05bb122eccf2ff518b57e791b334a2..8f86888249b86951b443f52a4f8ee27f6b8a0217 100644 (file)
@@ -1,5 +1,6 @@
 import itertools
 import bpy
+import mathutils
 
 class VertexCache:
        def __init__(self, size):
@@ -34,12 +35,13 @@ class VertexCache:
 
 class MeshExporter:
        def __init__(self):
+               self.show_progress = True
                self.use_strips = True
-               self.use_degen_tris = True
+               self.use_degen_tris = False
                self.max_strip_len = 1024
-               self.optimize_cache = False
+               self.optimize_cache = True
                self.cache_size = 64
-               self.export_lines = True
+               self.export_lines = False
                self.export_uv = "UNIT0"
                self.tbn_vecs = False
                self.tbn_uvtex = ""
@@ -49,7 +51,7 @@ class MeshExporter:
                self.export_groups = False
                self.max_groups = 2
 
-       def stripify(self, mesh, progress = None):
+       def stripify(self, mesh, progress=None):
                for f in mesh.faces:
                        f.flag = False
 
@@ -191,44 +193,58 @@ class MeshExporter:
 
                return strips, loose
 
-       def export(self, context, out_file):
+       def export(self, context, out_file, objs=None, progress=None):
+               if objs:
+                       objs = [(o, mathutils.Matrix()) for i in objs]
+
                if self.compound:
-                       objs = context.selected_objects
+                       if objs is None:
+                               objs = [(o, mathutils.Matrix()) for o in context.selected_objects]
                        check = objs
                        while check:
                                children = []
-                               for o in check:
+                               for o, m in check:
                                        for c in o.children:
                                                if c.compound:
-                                                       children.append(c)
+                                                       children.append((c, m*c.matrix_local))
                                objs += children
                                check = children
-               else:
-                       objs = [context.active_object]
+               elif objs is None:
+                       objs = [(context.active_object, mathutils.Matrix())]
 
                if not objs:
                        raise Exception("Nothing to export")
-               for o in objs:
+               for o, m in objs:
                        if o.type!="MESH":
                                raise Exception("Can only export Mesh data")
 
                from .mesh import Mesh
                from .util import Progress
 
-               progress = Progress(context)
-               progress.set_task("Preparing", 0.0, 0.0)
+               if self.show_progress:
+                       if not progress:
+                               progress = Progress(context)
+                       progress.set_task("Preparing", 0.0, 0.0)
+               else:
+                       progress = None
 
                mesh = None
                bmeshes = []
-               for o in objs:
+               winding_test = False
+               for o, m in objs:
+                       if o.data.winding_test:
+                               winding_test = True
                        bmesh = o.to_mesh(context.scene, True, "PREVIEW")
                        bmeshes.append(bmesh)
+                       me = Mesh(bmesh)
+                       me.transform(m)
                        if not mesh:
-                               mesh = Mesh(bmesh)
+                               mesh = me
                        else:
-                               mesh.splice(Mesh(bmesh))
+                               mesh.splice(me)
 
-               progress.set_task("Smoothing", 0.05, 0.35)
+               if progress:
+                       progress.set_task("Smoothing", 0.05, 0.35)
                if self.smoothing=="NONE":
                        mesh.flatten_faces()
                mesh.split_smooth(progress)
@@ -240,11 +256,12 @@ class MeshExporter:
                        mesh.sort_vertex_groups(self.max_groups)
 
                        # Create a mapping from vertex group indices to bone indices
-                       group_index_map = dict((i, i) for i in range(len(objs[0].vertex_groups)))
-                       if objs[0].parent and objs[0].parent.type=="ARMATURE":
-                               armature = objs[0].parent.data
+                       first_obj = objs[0][0]
+                       group_index_map = dict((i, i) for i in range(len(first_obj.vertex_groups)))
+                       if first_obj.parent and first_obj.parent.type=="ARMATURE":
+                               armature = first_obj.parent.data
                                bone_indices = dict((armature.bones[i].name, i) for i in range(len(armature.bones)))
-                               for g in objs[0].vertex_groups:
+                               for g in first_obj.vertex_groups:
                                        if g.name in bone_indices:
                                                group_index_map[g.index] = bone_indices[g.name]
 
@@ -279,7 +296,8 @@ class MeshExporter:
                                        texunits.insert(0, unit)
 
                        for i, u in texunits:
-                               progress.set_task("Splitting UVs", 0.35+0.3*i/len(texunits), 0.35+0.3*(i+1)/len(texunits))
+                               if progress:
+                                       progress.set_task("Splitting UVs", 0.35+0.3*i/len(texunits), 0.35+0.3*(i+1)/len(texunits))
                                mesh.split_uv(i, progress)
                                if self.tbn_vecs and u.name==self.tbn_uvtex:
                                        mesh.compute_uv()
@@ -290,10 +308,12 @@ class MeshExporter:
                strips = []
                loose = mesh.faces
                if self.use_strips:
-                       progress.set_task("Creating strips", 0.65, 0.95)
+                       if progress:
+                               progress.set_task("Creating strips", 0.65, 0.95)
                        strips, loose = self.stripify(mesh, progress)
 
-               progress.set_task("Writing file", 0.95, 1.0)
+               if progress:
+                       progress.set_task("Writing file", 0.95, 1.0)
 
                from .outfile import open_output
                out_file = open_output(out_file)
@@ -370,7 +390,11 @@ class MeshExporter:
                                out_file.write("indices", l.vertices[0].index, l.vertices[1].index)
                        out_file.end()
 
-               progress.set_task("Done", 1.0, 1.0)
+               if winding_test:
+                       out_file.write("winding", "COUNTERCLOCKWISE")
+
+               if progress:
+                       progress.set_task("Done", 1.0, 1.0)
 
                for m in bmeshes:
                        bpy.data.meshes.remove(m)