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