]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/animation.py
Add looping detection to animation exporter
[libs/gl.git] / blender / io_mspgl / animation.py
index ffce11f596fc8099b2e48e39d8401beca84f45d2..59f34b4d7c532e35e0a852493b81120a1a9fff61 100644 (file)
@@ -1,3 +1,4 @@
+import math
 import mathutils
 
 class Curve:
@@ -34,6 +35,7 @@ class Animation:
                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):
@@ -77,6 +79,24 @@ class Animation:
        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
+
+                       print("Difference for {} {} is {}".format(c.data_path, c.array_index, d))
+                       if d>threshold:
+                               self.looping = False
+                               break
+
        def change_fps(self, fps):
                scale = self.fps/fps
                self.fps = fps
@@ -89,8 +109,9 @@ class Animation:
                        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