]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_animation.py
Add an option to export all animations for all selected objects
[libs/gl.git] / blender / io_mspgl / export_animation.py
1 import math
2 import os
3
4 class AnimationExporter:
5         def __init__(self):
6                 self.export_all = False
7                 self.collection = True
8
9         def export_to_file(self, context, out_fn):
10                 if self.export_all:
11                         actions = []
12                         for o in context.selected_objects:
13                                 if not o.animation_data:
14                                         continue
15
16                                 for t in o.animation_data.nla_tracks:
17                                         for s in t.strips:
18                                                 if s.action and s.action not in actions:
19                                                         actions.append(s.action)
20
21                         if not actions:
22                                 raise Exception("No actions found")
23
24                         resources = {}
25                         for a in actions:
26                                 resources[a.name+".anim"] = self.export_animation(context, a)
27
28                         path, base = os.path.split(out_fn)
29                         base, ext = os.path.splitext(base)
30
31                         if self.collection:
32                                 from .datafile import Statement
33                                 with open(os.path.join(path, base+".mdc"), "w") as out_file:
34                                         for r in resources.values():
35                                                 st = Statement("animation", r.name)
36                                                 st.sub = r.statements
37                                                 st.write_to_file(out_file)
38                         else:
39                                 for r in resources.values():
40                                         with open(os.path.join(path, r.name), w) as out_file:
41                                                 for s in r.statements:
42                                                         s.write_to_file(out_file)
43                 else:
44                         anim_data = context.active_object.animation_data
45                         if not anim_data:
46                                 raise Exception("Object has no animation data")
47                         if not anim_data.action:
48                                 raise Exception("No active action")
49
50                         resource = self.export_animation(context, anim_data.action)
51
52                         with open(out_fn, "w") as out_file:
53                                 for s in resource.statements:
54                                         s.write_to_file(out_file)
55
56         def export_animation(self, context, action):
57                 from .animation import create_animation_from_action
58                 anim = create_animation_from_action(context, action)
59
60                 from .datafile import Resource, Statement
61                 resource = Resource(action.name+".anim")
62
63                 components = [(0, "location", "position"), (1, "rotation_euler", "euler"), (2, "scale", "scale")]
64                 coords = "xyz"
65                 prev_time = 0.0
66                 for k in anim.keyframes:
67                         if k.time>prev_time:
68                                 resource.statements.append(Statement("interval", k.time-prev_time))
69                                 prev_time = k.time
70
71                         st = Statement("control_keyframe" if k.control else "keyframe")
72
73                         transform = [0.0]*9
74                         mask = 0
75                         for c, i in k.curves:
76                                 for j, dp, kw in components:
77                                         if c.data_path==dp:
78                                                 transform[j*3+c.array_index] = c.knots[i][1]
79                                                 mask |= 1<<(j*3+c.array_index)
80                                                 break
81
82                         if mask:
83                                 ss = Statement("transform")
84
85                                 for i, dp, kw in components:
86                                         v = transform[i*3:i*3+3]
87                                         if i==1:
88                                                 v = [c*180/math.pi for c in v]
89
90                                         m = 7<<(i*3)
91                                         if (mask&m)==m:
92                                                 ss.sub.append(Statement(kw, *v))
93                                         else:
94                                                 m &= m>>2
95                                                 for j in range(3):
96                                                         if mask&(m<<j):
97                                                                 ss.sub.append(Statement("{}_{}".format(kw, coords[j]), v[j]))
98
99                                 st.sub.append(ss)
100
101                         resource.statements.append(st)
102
103                 return resource
104