]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_animation.py
Sort things by name when exporting
[libs/gl.git] / blender / io_mspgl / export_animation.py
index b990e054ff0c8b8196207a1c1ef7c65e728eaf1d..45eff8f9d09e80cdbe8aa0d0a18520f8db0559fd 100644 (file)
@@ -1,22 +1,63 @@
 import math
+import os
 
 class AnimationExporter:
+       def __init__(self):
+               self.export_all = False
+               self.collection = True
+               self.looping_threshold = 0.001
+
        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)
 
-               resource = self.export_animation(context, anim_data.action)
+                       path, base = os.path.split(out_fn)
+                       base, ext = os.path.splitext(base)
 
-               with open(out_fn, "w") as out_file:
-                       for s in resource.statements:
-                               s.write_to_file(out_file)
+                       if self.collection:
+                               from .datafile import Statement
+                               with open(os.path.join(path, base+".mdc"), "w") as out_file:
+                                       for n in sorted(resources.keys()):
+                                               r = resources[n]
+                                               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)
+
+                       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
-               anim = create_animation_from_action(context, action)
+               anim = create_animation_from_action(context, action, looping_threshold=self.looping_threshold)
 
                from .datafile import Resource, Statement
                resource = Resource(action.name+".anim")
@@ -61,5 +102,7 @@ class AnimationExporter:
 
                        resource.statements.append(st)
 
+               resource.statements.append(Statement("looping", anim.looping))
+
                return resource