]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_animation.py
Add exporter for animations
[libs/gl.git] / blender / io_mspgl / export_animation.py
1 import math
2
3 class AnimationExporter:
4         def export_to_file(self, context, out_fn):
5                 anim_data = context.active_object.animation_data
6                 if not anim_data:
7                         raise Exception("Object has no animation data")
8                 if not anim_data.action:
9                         raise Exception("No active action")
10
11                 resource = self.export_animation(context, anim_data.action)
12
13                 with open(out_fn, "w") as out_file:
14                         for s in resource.statements:
15                                 s.write_to_file(out_file)
16
17         def export_animation(self, context, action):
18                 from .animation import create_animation_from_action
19                 anim = create_animation_from_action(context, action)
20
21                 from .datafile import Resource, Statement
22                 resource = Resource(action.name+".anim")
23
24                 components = [(0, "location", "position"), (1, "rotation_euler", "euler"), (2, "scale", "scale")]
25                 coords = "xyz"
26                 prev_time = 0.0
27                 for k in anim.keyframes:
28                         if k.time>prev_time:
29                                 resource.statements.append(Statement("interval", k.time-prev_time))
30                                 prev_time = k.time
31
32                         st = Statement("control_keyframe" if k.control else "keyframe")
33
34                         transform = [0.0]*9
35                         mask = 0
36                         for c, i in k.curves:
37                                 for j, dp, kw in components:
38                                         if c.data_path==dp:
39                                                 transform[j*3+c.array_index] = c.knots[i][1]
40                                                 mask |= 1<<(j*3+c.array_index)
41                                                 break
42
43                         if mask:
44                                 ss = Statement("transform")
45
46                                 for i, dp, kw in components:
47                                         v = transform[i*3:i*3+3]
48                                         if i==1:
49                                                 v = [c*180/math.pi for c in v]
50
51                                         m = 7<<(i*3)
52                                         if (mask&m)==m:
53                                                 ss.sub.append(Statement(kw, *v))
54                                         else:
55                                                 m &= m>>2
56                                                 for j in range(3):
57                                                         if mask&(m<<j):
58                                                                 ss.sub.append(Statement("{}_{}".format(kw, coords[j]), v[j]))
59
60                                 st.sub.append(ss)
61
62                         resource.statements.append(st)
63
64                 return resource
65