Add an option to export all animations for all selected objects
authorMikko Rasa <tdb@tdb.fi>
Tue, 11 Jun 2019 16:50:14 +0000 (19:50 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 11 Jun 2019 16:50:14 +0000 (19:50 +0300)
blender/io_mspgl/__init__.py
blender/io_mspgl/export_animation.py

index d555ab4b30d54e59f48f5453c2612dfef73ec2ba..f0cb6579266c6ecdf1850676c36689934137eabe 100644 (file)
@@ -96,10 +96,33 @@ class ExportMspGLAnimation(bpy.types.Operator, ExportMspGLBase):
 
        filename_ext = ".anim"
 
+       export_all = bpy.props.BoolProperty(name="Export all animations", description="Export all animations present on the selected objects' NLA tracks")
+       collection = bpy.props.BoolProperty(name="As a collection", description="Export the animations as a single collection file", default=True)
+
+       def check(self, context):
+               result = False
+
+               ext = ".mdc" if self.export_all and self.collection else ".anim"
+               ext_changed = (ext!=self.filename_ext)
+               if ext_changed:
+                       if self.filepath.endswith(self.filename_ext):
+                               self.filepath = self.filepath[:-len(self.filename_ext)]
+                       self.filename_ext = ext
+
+               super_result = super().check(context)
+
+               return ext_changed or super_result
+
        def create_exporter(self):
                from .export_animation import AnimationExporter
                return AnimationExporter()
 
+       def draw(self, context):
+               col = self.layout.column()
+               col.prop(self, "export_all")
+               if self.export_all:
+                       col.prop(self, "collection")
+
 class ExportMspGLScene(bpy.types.Operator, ExportMspGLBase):
        bl_idname = "export_scene.mspgl_scene"
        bl_label = "Export Msp GL scene"
index b990e054ff0c8b8196207a1c1ef7c65e728eaf1d..e2c7733df69be358cd5e54029675a2caebc5361d 100644 (file)
@@ -1,18 +1,57 @@
 import math
+import os
 
 class AnimationExporter:
+       def __init__(self):
+               self.export_all = False
+               self.collection = True
+
        def export_to_file(self, context, out_fn):
-               anim_data = context.active_object.animation_data
-               if not anim_data:
-                       raise Exception("Object has no animation data")
-               if not anim_data.action:
-                       raise Exception("No active action")
+               if self.export_all:
+                       actions = []
+                       for o in context.selected_objects:
+                               if not o.animation_data:
+                                       continue
+
+                               for t in o.animation_data.nla_tracks:
+                                       for s in t.strips:
+                                               if s.action and s.action not in actions:
+                                                       actions.append(s.action)
+
+                       if not actions:
+                               raise Exception("No actions found")
+
+                       resources = {}
+                       for a in actions:
+                               resources[a.name+".anim"] = self.export_animation(context, a)
+
+                       path, base = os.path.split(out_fn)
+                       base, ext = os.path.splitext(base)
+
+                       if self.collection:
+                               from .datafile import Statement
+                               with open(os.path.join(path, base+".mdc"), "w") as out_file:
+                                       for r in resources.values():
+                                               st = Statement("animation", r.name)
+                                               st.sub = r.statements
+                                               st.write_to_file(out_file)
+                       else:
+                               for r in resources.values():
+                                       with open(os.path.join(path, r.name), w) as out_file:
+                                               for s in r.statements:
+                                                       s.write_to_file(out_file)
+               else:
+                       anim_data = context.active_object.animation_data
+                       if not anim_data:
+                               raise Exception("Object has no animation data")
+                       if not anim_data.action:
+                               raise Exception("No active action")
 
-               resource = self.export_animation(context, anim_data.action)
+                       resource = self.export_animation(context, anim_data.action)
 
-               with open(out_fn, "w") as out_file:
-                       for s in resource.statements:
-                               s.write_to_file(out_file)
+                       with open(out_fn, "w") as out_file:
+                               for s in resource.statements:
+                                       s.write_to_file(out_file)
 
        def export_animation(self, context, action):
                from .animation import create_animation_from_action