From eeaa1dcf7e383517e8097c9438a286e3810962ae Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 15 Jul 2015 00:00:32 +0300 Subject: [PATCH] Properly handle compound children with non-identity local matrix --- blender/io_mspgl/export_mesh.py | 31 +++++++++++++++++++------------ blender/io_mspgl/mesh.py | 7 +++++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/blender/io_mspgl/export_mesh.py b/blender/io_mspgl/export_mesh.py index cae0e589..07f457a1 100644 --- a/blender/io_mspgl/export_mesh.py +++ b/blender/io_mspgl/export_mesh.py @@ -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,15 @@ class MeshExporter: mesh = None bmeshes = [] - for o in objs: + for o, m in objs: 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 +253,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] diff --git a/blender/io_mspgl/mesh.py b/blender/io_mspgl/mesh.py index 04b2b51f..2cd64236 100644 --- a/blender/io_mspgl/mesh.py +++ b/blender/io_mspgl/mesh.py @@ -45,6 +45,7 @@ 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 @@ -52,6 +53,8 @@ class Vertex: 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 @@ -179,6 +182,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: -- 2.45.2