4 class AnimationExporter:
5 def export_to_file(self, context, out_fn, *, export_all=False, collection=True, looping_threshold=0.001):
8 for o in context.selected_objects:
9 if not o.animation_data:
12 for t in o.animation_data.nla_tracks:
14 if s.action and s.action not in actions:
15 actions.append(s.action)
18 raise Exception("No actions found")
22 resources[a.name+".anim"] = self.export_animation(context, a, looping_threshold=looping_threshold)
24 path, base = os.path.split(out_fn)
25 base, ext = os.path.splitext(base)
28 from .datafile import Resource
29 dummy = Resource("dummy", "dummy")
30 dummy.references = list(sorted(resources.values(), key=lambda r: r.name))
31 dummy.write_collection(os.path.join(path, base+".mdc"), exclude_self=True)
33 for r in resources.values():
34 r.write_to_file(os.path.join(path, r.name))
36 anim_data = context.active_object.animation_data
38 raise Exception("Object {} has no animation data".format(context.active_object.name))
39 if not anim_data.action:
40 raise Exception("Object {} has no active action".format(context.active_object.name))
42 resource = self.export_animation(context, anim_data.action)
44 resource.write_to_file(out_fn)
46 def export_animation(self, context, action, *, looping_threshold=0.001):
47 from .animation import create_animation_from_action
48 anim = create_animation_from_action(context, action, looping_threshold=looping_threshold)
50 from .datafile import Resource, Statement
51 resource = Resource(action.name+".anim", "animation")
53 components = [(0, "location", "position"), (1, "rotation_euler", "euler"), (2, "scale", "scale")]
56 for k in anim.keyframes:
58 resource.statements.append(Statement("interval", k.time-prev_time))
61 st = Statement("control_keyframe" if k.control else "keyframe")
66 for j, dp, kw in components:
68 transform[j*3+c.array_index] = c.knots[i][1]
69 mask |= 1<<(j*3+c.array_index)
73 ss = Statement("transform")
75 for i, dp, kw in components:
76 v = transform[i*3:i*3+3]
78 v = [c*180/math.pi for c in v]
82 ss.sub.append(Statement(kw, *v))
87 ss.sub.append(Statement("{}_{}".format(kw, coords[j]), v[j]))
91 resource.statements.append(st)
93 resource.statements.append(Statement("looping", anim.looping))