4 class AnimationExporter:
6 self.export_all = False
9 def export_to_file(self, context, out_fn):
12 for o in context.selected_objects:
13 if not o.animation_data:
16 for t in o.animation_data.nla_tracks:
18 if s.action and s.action not in actions:
19 actions.append(s.action)
22 raise Exception("No actions found")
26 resources[a.name+".anim"] = self.export_animation(context, a)
28 path, base = os.path.split(out_fn)
29 base, ext = os.path.splitext(base)
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)
37 st.write_to_file(out_file)
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)
44 anim_data = context.active_object.animation_data
46 raise Exception("Object has no animation data")
47 if not anim_data.action:
48 raise Exception("No active action")
50 resource = self.export_animation(context, anim_data.action)
52 with open(out_fn, "w") as out_file:
53 for s in resource.statements:
54 s.write_to_file(out_file)
56 def export_animation(self, context, action):
57 from .animation import create_animation_from_action
58 anim = create_animation_from_action(context, action)
60 from .datafile import Resource, Statement
61 resource = Resource(action.name+".anim")
63 components = [(0, "location", "position"), (1, "rotation_euler", "euler"), (2, "scale", "scale")]
66 for k in anim.keyframes:
68 resource.statements.append(Statement("interval", k.time-prev_time))
71 st = Statement("control_keyframe" if k.control else "keyframe")
76 for j, dp, kw in components:
78 transform[j*3+c.array_index] = c.knots[i][1]
79 mask |= 1<<(j*3+c.array_index)
83 ss = Statement("transform")
85 for i, dp, kw in components:
86 v = transform[i*3:i*3+3]
88 v = [c*180/math.pi for c in v]
92 ss.sub.append(Statement(kw, *v))
97 ss.sub.append(Statement("{}_{}".format(kw, coords[j]), v[j]))
101 resource.statements.append(st)