if "bpy" in locals():
import imp
- for sub in "export_mesh", "mesh", "outfile", "util":
+ for sub in "armature", "export_armature", "export_mesh", "mesh", "outfile", "util":
if sub in locals():
imp.reload(locals()[sub])
import bpy
from bpy_extras.io_utils import ExportHelper
-class ExportMspGLMeshBase(ExportHelper):
+class ExportMspGLBase(ExportHelper):
+ def execute(self, context):
+ exporter = self.create_exporter()
+ self.prepare_exporter(exporter)
+ exporter.export(context, self.filepath)
+ return {"FINISHED"}
+
+ def create_exporter(self):
+ raise Exception("create_exporter must be overridden")
+
+ def prepare_exporter(self, exporter):
+ for k, v in self.as_keywords().items():
+ setattr(exporter, k, v)
+
+class ExportMspGLMeshBase(ExportMspGLBase):
use_strips = bpy.props.BoolProperty(name="Use strips", description="Combine faces into triangle strips", default=True)
use_degen_tris = bpy.props.BoolProperty(name="Use degen tris", description="Concatenate triangle strips with degenerate triangles", default=False)
max_strip_len = bpy.props.IntProperty(name="Max strip length", description="Maximum length for a triangle strip", default=1024, min=4, max=16384)
items=(("NONE", "None", "No smoothing"),
("BLENDER", "Blender", "Use Blender's vertex normals"),
("MSPGL", "MspGL", "Compute vertex normals internally")))
+ export_groups = bpy.props.BoolProperty(name="Vertex groups", description="Export vertex groups and weights", default=False)
- def execute(self, context):
+ def create_exporter(self):
from .export_mesh import MeshExporter
- exporter = MeshExporter()
- self.prepare_exporter(exporter)
- exporter.export(context, self.filepath)
- return {"FINISHED"}
-
- def prepare_exporter(self, exporter):
- for k, v in self.as_keywords().items():
- setattr(exporter, k, v)
+ return MeshExporter()
def draw(self, context):
col = self.layout.column()
col.prop(self, "export_lines")
col.prop(self, "compound")
col.prop(self, "smoothing")
+ col.prop(self, "export_groups")
self.layout.separator()
col = self.layout.column()
col.label("Triangle strips")
col.prop(self, "textures")
col.prop(self, "material_tex")
+class ExportMspGLArmature(bpy.types.Operator, ExportMspGLBase):
+ bl_idname = "export.mspgl_armature"
+ bl_label = "Export Msp GL armature"
+
+ filename_ext = ".armature"
+
+ def create_exporter(self):
+ from .export_armature import ArmatureExporter
+ return ArmatureExporter()
+
def menu_func_export(self, context):
self.layout.operator(ExportMspGLMesh.bl_idname, text="Msp GL mesh")
self.layout.operator(ExportMspGLObject.bl_idname, text="Msp GL object")
+ self.layout.operator(ExportMspGLArmature.bl_idname, text="Msp GL armature")
def register():
bpy.utils.register_module(__name__)
--- /dev/null
+class Link:
+ def __init__(self, bone):
+ self._bone = bone
+ self.name = self._bone.name
+ self.index = None
+ self.parent_name = None
+ if self._bone.parent:
+ self.parent_name = self._bone.parent.name
+ self.parent = None
+ self.base = self._bone.head_local
+
+class Armature:
+ def __init__(self, arm):
+ self._armature = arm
+
+ self.links = [Link(b) for b in self._armature.bones]
+ for i, l in enumerate(self.links):
+ l.index = i
+
+ links_by_name = dict((l.name, l) for l in self.links)
+ for l in self.links:
+ if l.parent_name:
+ l.parent = links_by_name[l.parent_name]
+
+ def sort_links(self):
+ sorted_links = []
+ for l in self.links:
+ if l in sorted_links:
+ continue
+ if not l.parent:
+ sorted_links.append(l)
+ else:
+ chain = [l]
+ p = l.parent
+ while p and p not in sorted_links:
+ chain.append(p)
+ p = p.parent
+ sorted_links += reversed(chain)
+ self.links = sorted_links
--- /dev/null
+from .outfile import OutFile
+
+class ArmatureExporter:
+ def export(self, context, fn):
+ obj = context.active_object
+ if obj.type!="ARMATURE":
+ raise Exception("Can only export Armature data")
+
+ from .armature import Armature
+
+ armature = Armature(obj.data)
+ armature.sort_links()
+
+ out_file = OutFile(fn)
+ for l in armature.links:
+ out_file.begin("link", '"{}"'.format(l.name))
+ out_file.write("index", l.index)
+ if l.parent:
+ out_file.write("parent", '"{}"'.format(l.parent.name))
+ out_file.write("base", *tuple(l.base))
+ out_file.end()
+import itertools
import bpy
from .outfile import OutFile
self.material_tex = False
self.textures = "REF"
self.smoothing = "MSPGL"
+ self.export_groups = False
+ self.max_groups = 2
def stripify(self, mesh, progress = None):
for f in mesh.faces:
if self.smoothing!="BLENDER":
mesh.compute_normals()
+ if self.export_groups:
+ 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
+ bone_indices = dict((armature.bones[i].name, i) for i in range(len(armature.bones)))
+ for g in objs[0].vertex_groups:
+ if g.name in bone_indices:
+ group_index_map[g.index] = bone_indices[g.name]
+
if self.material_tex and mesh.materials:
mesh.generate_material_uv()
fmt += "_TEXCOORD2%d"%u.unit
if self.tbn_vecs:
fmt += "_ATTRIB33_ATTRIB34"
+ if self.export_groups:
+ fmt += "_ATTRIB%d5"%(self.max_groups*2)
fmt += "_VERTEX3"
out_file.begin("vertices", fmt)
normal = None
uvs = [None]*len(texunits)
tan = None
bino = None
+ group = None
for v in mesh.vertices:
if v.normal!=normal:
out_file.write("normal3", *v.normal)
if v.bino!=bino:
out_file.write("attrib3", 4, *v.bino)
bino = v.bino
+ if self.export_groups:
+ group_attr = [(group_index_map[g.group], g.weight*v.group_weight_scale) for g in v.groups[:self.max_groups]]
+ while len(group_attr)<self.max_groups:
+ group_attr.append((0, 0.0))
+ group_attr = list(itertools.chain(*group_attr))
+ if group_attr!=group:
+ out_file.write("attrib%d"%len(group_attr), 5, *group_attr)
+ group = group_attr
out_file.write("vertex3", *v.co)
out_file.end()
for s in strips: