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 r in resources.values():
36 st = Statement("animation", r.name)
38 st.write_to_file(out_file)
40 for r in resources.values():
41 with open(os.path.join(path, r.name), w) as out_file:
42 for s in r.statements:
43 s.write_to_file(out_file)
45 anim_data = context.active_object.animation_data
47 raise Exception("Object has no animation data")
48 if not anim_data.action:
49 raise Exception("No active action")
51 resource = self.export_animation(context, anim_data.action)
53 with open(out_fn, "w") as out_file:
54 for s in resource.statements:
55 s.write_to_file(out_file)
57 def export_animation(self, context, action):
58 from .animation import create_animation_from_action
59 anim = create_animation_from_action(context, action, looping_threshold=self.looping_threshold)
61 from .datafile import Resource, Statement
62 resource = Resource(action.name+".anim")
64 components = [(0, "location", "position"), (1, "rotation_euler", "euler"), (2, "scale", "scale")]
67 for k in anim.keyframes:
69 resource.statements.append(Statement("interval", k.time-prev_time))
72 st = Statement("control_keyframe" if k.control else "keyframe")
77 for j, dp, kw in components:
79 transform[j*3+c.array_index] = c.knots[i][1]
80 mask |= 1<<(j*3+c.array_index)
84 ss = Statement("transform")
86 for i, dp, kw in components:
87 v = transform[i*3:i*3+3]
89 v = [c*180/math.pi for c in v]
93 ss.sub.append(Statement(kw, *v))
98 ss.sub.append(Statement("{}_{}".format(kw, coords[j]), v[j]))
102 resource.statements.append(st)
104 resource.statements.append(Statement("looping", anim.looping))