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