]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_animation.py
Adjust and re-export desertpillars data with new features
[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 Resource
34                                 dummy = Resource("dummy", "dummy")
35                                 dummy.references = list(sorted(resources.values(), key=lambda r: r.name))
36                                 dummy.write_collection(os.path.join(path, base+".mdc"), exclude_self=True)
37                         else:
38                                 for r in resources.values():
39                                         r.write_to_file(os.path.join(path, r.name))
40                 else:
41                         anim_data = context.active_object.animation_data
42                         if not anim_data:
43                                 raise Exception("Object {} has no animation data".format(context.active_object.name))
44                         if not anim_data.action:
45                                 raise Exception("Object {} has no active action".format(context.active_object.name))
46
47                         resource = self.export_animation(context, anim_data.action)
48
49                         resource.write_to_file(out_fn)
50
51         def export_animation(self, context, action):
52                 from .animation import create_animation_from_action
53                 anim = create_animation_from_action(context, action, looping_threshold=self.looping_threshold)
54
55                 from .datafile import Resource, Statement
56                 resource = Resource(action.name+".anim", "animation")
57
58                 components = [(0, "location", "position"), (1, "rotation_euler", "euler"), (2, "scale", "scale")]
59                 coords = "xyz"
60                 prev_time = 0.0
61                 for k in anim.keyframes:
62                         if k.time>prev_time:
63                                 resource.statements.append(Statement("interval", k.time-prev_time))
64                                 prev_time = k.time
65
66                         st = Statement("control_keyframe" if k.control else "keyframe")
67
68                         transform = [0.0]*9
69                         mask = 0
70                         for c, i in k.curves:
71                                 for j, dp, kw in components:
72                                         if c.data_path==dp:
73                                                 transform[j*3+c.array_index] = c.knots[i][1]
74                                                 mask |= 1<<(j*3+c.array_index)
75                                                 break
76
77                         if mask:
78                                 ss = Statement("transform")
79
80                                 for i, dp, kw in components:
81                                         v = transform[i*3:i*3+3]
82                                         if i==1:
83                                                 v = [c*180/math.pi for c in v]
84
85                                         m = 7<<(i*3)
86                                         if (mask&m)==m:
87                                                 ss.sub.append(Statement(kw, *v))
88                                         else:
89                                                 m &= m>>2
90                                                 for j in range(3):
91                                                         if mask&(m<<j):
92                                                                 ss.sub.append(Statement("{}_{}".format(kw, coords[j]), v[j]))
93
94                                 st.sub.append(ss)
95
96                         resource.statements.append(st)
97
98                 resource.statements.append(Statement("looping", anim.looping))
99
100                 return resource
101