+import math
import mathutils
class Curve:
self._action = action
self.curves = [Curve(c) for c in action.fcurves]
self.fps = 1
+ self.looping = False
for c in self.curves:
for i in range(0, len(c.knots)-1, 3):
kf.curves.append((c, i))
self.keyframes.sort(key=lambda k: k.time)
+ self.start_time = self.keyframes[0].time
+
+ for c in self.curves:
+ for k in c.knots:
+ k[0] -= self.start_time
+
+ for k in self.keyframes:
+ k.time -= self.start_time
def __getattr__(self, attr):
return getattr(self._curve, attr)
+ def check_looping(self, threshold):
+ self.looping = True
+ for c in self.curves:
+ first_y = c.knots[0][1]
+ last_y = c.knots[-1][1]
+ d = abs(last_y-first_y)
+
+ if c.data_path=="rotation_euler":
+ while d>math.pi/2:
+ d -= math.pi
+ while d<-math.pi/2:
+ d += math.pi
+
+ if d>threshold:
+ self.looping = False
+ break
+
def change_fps(self, fps):
scale = self.fps/fps
self.fps = fps
k.time *= scale
-def create_animation_from_action(context, action):
+def create_animation_from_action(context, action, *, looping_threshold=0.001):
anim = Animation(action)
render = context.scene.render
+ anim.check_looping(looping_threshold)
anim.change_fps(render.fps/render.fps_base)
return anim