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 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)
38 for r in resources.values():
39 r.write_to_file(os.path.join(path, r.name))
41 anim_data = context.active_object.animation_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))
47 resource = self.export_animation(context, anim_data.action)
49 resource.write_to_file(out_fn)
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)
55 from .datafile import Resource, Statement
56 resource = Resource(action.name+".anim", "animation")
58 components = [(0, "location", "position"), (1, "rotation_euler", "euler"), (2, "scale", "scale")]
61 for k in anim.keyframes:
63 resource.statements.append(Statement("interval", k.time-prev_time))
66 st = Statement("control_keyframe" if k.control else "keyframe")
71 for j, dp, kw in components:
73 transform[j*3+c.array_index] = c.knots[i][1]
74 mask |= 1<<(j*3+c.array_index)
78 ss = Statement("transform")
80 for i, dp, kw in components:
81 v = transform[i*3:i*3+3]
83 v = [c*180/math.pi for c in v]
87 ss.sub.append(Statement(kw, *v))
92 ss.sub.append(Statement("{}_{}".format(kw, coords[j]), v[j]))
96 resource.statements.append(st)
98 resource.statements.append(Statement("looping", anim.looping))