]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_animation.py
Add exporter for animations
[libs/gl.git] / blender / io_mspgl / export_animation.py
diff --git a/blender/io_mspgl/export_animation.py b/blender/io_mspgl/export_animation.py
new file mode 100644 (file)
index 0000000..b990e05
--- /dev/null
@@ -0,0 +1,65 @@
+import math
+
+class AnimationExporter:
+       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")
+
+               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)
+
+               from .datafile import Resource, Statement
+               resource = Resource(action.name+".anim")
+
+               components = [(0, "location", "position"), (1, "rotation_euler", "euler"), (2, "scale", "scale")]
+               coords = "xyz"
+               prev_time = 0.0
+               for k in anim.keyframes:
+                       if k.time>prev_time:
+                               resource.statements.append(Statement("interval", k.time-prev_time))
+                               prev_time = k.time
+
+                       st = Statement("control_keyframe" if k.control else "keyframe")
+
+                       transform = [0.0]*9
+                       mask = 0
+                       for c, i in k.curves:
+                               for j, dp, kw in components:
+                                       if c.data_path==dp:
+                                               transform[j*3+c.array_index] = c.knots[i][1]
+                                               mask |= 1<<(j*3+c.array_index)
+                                               break
+
+                       if mask:
+                               ss = Statement("transform")
+
+                               for i, dp, kw in components:
+                                       v = transform[i*3:i*3+3]
+                                       if i==1:
+                                               v = [c*180/math.pi for c in v]
+
+                                       m = 7<<(i*3)
+                                       if (mask&m)==m:
+                                               ss.sub.append(Statement(kw, *v))
+                                       else:
+                                               m &= m>>2
+                                               for j in range(3):
+                                                       if mask&(m<<j):
+                                                               ss.sub.append(Statement("{}_{}".format(kw, coords[j]), v[j]))
+
+                               st.sub.append(ss)
+
+                       resource.statements.append(st)
+
+               return resource
+