]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_mesh.py
Add properties to export materials as texture array layers
[libs/gl.git] / blender / io_mspgl / export_mesh.py
index cae0e5894b97fa05f6f9fa21bd3c2f2b6bdc03b9..1652c1a494756eec3c772718b01897297046e444 100644 (file)
@@ -1,5 +1,6 @@
 import itertools
 import bpy
+import mathutils
 
 class VertexCache:
        def __init__(self, size):
@@ -193,24 +194,27 @@ class MeshExporter:
                return strips, loose
 
        def export(self, context, out_file, objs=None, progress=None):
+               if objs:
+                       objs = [(o, mathutils.Matrix()) for i in objs]
+
                if self.compound:
                        if objs is None:
-                               objs = context.selected_objects
+                               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
                elif objs is None:
-                       objs = [context.active_object]
+                       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")
 
@@ -226,13 +230,18 @@ class MeshExporter:
 
                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)
 
                if progress:
                        progress.set_task("Smoothing", 0.05, 0.35)
@@ -247,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]
 
@@ -311,10 +321,11 @@ class MeshExporter:
                fmt = ["NORMAL3"]
                if texunits:
                        for i, u in texunits:
+                               size = str(len(mesh.vertices[0].uvs[i]))
                                if u.unit==0 or force_unit0:
-                                       fmt.append("TEXCOORD2")
+                                       fmt.append("TEXCOORD"+size)
                                else:
-                                       fmt.append("TEXCOORD2_%d"%u.unit)
+                                       fmt.append("TEXCOORD%s_%d"%(size, u.unit))
                        if self.tbn_vecs:
                                fmt += ["TANGENT3", "BINORMAL3"]
                if self.export_groups:
@@ -332,10 +343,11 @@ class MeshExporter:
                                normal = v.normal
                        for i, u in texunits:
                                if v.uvs[i]!=uvs.get(i):
+                                       size = str(len(v.uvs[i]))
                                        if u.unit==0 or force_unit0:
-                                               out_file.write("texcoord2", *v.uvs[i])
+                                               out_file.write("texcoord"+size, *v.uvs[i])
                                        else:
-                                               out_file.write("multitexcoord2", u.unit, *v.uvs[i])
+                                               out_file.write("multitexcoord"+size, u.unit, *v.uvs[i])
                                        uvs[i] = v.uvs[i]
                        if self.tbn_vecs:
                                if v.tan!=tan:
@@ -380,6 +392,9 @@ class MeshExporter:
                                out_file.write("indices", l.vertices[0].index, l.vertices[1].index)
                        out_file.end()
 
+               if winding_test:
+                       out_file.write("winding", "COUNTERCLOCKWISE")
+
                if progress:
                        progress.set_task("Done", 1.0, 1.0)