4 class AnimationExporter:
6 self.export_all = False
8 self.looping_threshold = 0.001
10 def export_to_file(self, context, out_fn):
13 for o in context.selected_objects:
14 if not o.animation_data:
17 for t in o.animation_data.nla_tracks:
19 if s.action and s.action not in actions:
20 actions.append(s.action)
23 raise Exception("No actions found")
27 resources[a.name+".anim"] = self.export_animation(context, a)
29 path, base = os.path.split(out_fn)
30 base, ext = os.path.splitext(base)
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()):
37 st = Statement("animation", r.name)
39 st.write_to_file(out_file)
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)
46 anim_data = context.active_object.animation_data
48 raise Exception("Object has no animation data")
49 if not anim_data.action:
50 raise Exception("No active action")
52 resource = self.export_animation(context, anim_data.action)
54 with open(out_fn, "w") as out_file:
55 for s in resource.statements:
56 s.write_to_file(out_file)
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)
62 from .datafile import Resource, Statement
63 resource = Resource(action.name+".anim")
65 components = [(0, "location", "position"), (1, "rotation_euler", "euler"), (2, "scale", "scale")]
68 for k in anim.keyframes:
70 resource.statements.append(Statement("interval", k.time-prev_time))
73 st = Statement("control_keyframe" if k.control else "keyframe")
78 for j, dp, kw in components:
80 transform[j*3+c.array_index] = c.knots[i][1]
81 mask |= 1<<(j*3+c.array_index)
85 ss = Statement("transform")
87 for i, dp, kw in components:
88 v = transform[i*3:i*3+3]
90 v = [c*180/math.pi for c in v]
94 ss.sub.append(Statement(kw, *v))
99 ss.sub.append(Statement("{}_{}".format(kw, coords[j]), v[j]))
103 resource.statements.append(st)
105 resource.statements.append(Statement("looping", anim.looping))