X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=blender%2Fio_mspgl%2Fanimation.py;h=b7576fb516d761e0806d29cc8c7279e7e2956c5e;hp=5ad937df196060b934bd1269718beb6833ac76d4;hb=4c142176bad0ad17128a8176bedac3952c069449;hpb=f2cab83d5704f3f4a91f551f402187637e5063d2 diff --git a/blender/io_mspgl/animation.py b/blender/io_mspgl/animation.py index 5ad937df..b7576fb5 100644 --- a/blender/io_mspgl/animation.py +++ b/blender/io_mspgl/animation.py @@ -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): @@ -65,10 +67,35 @@ class Animation: 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 @@ -81,8 +108,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