I need some help with animating a rotating object.

I am attempting to rotate the object with the help of a generated rotation matrix. I insert a keyframe for for every 5 frames with quaternion coordinates.

Here is some example code:

import bpy
from mathutils import Vector, Matrix

f = lambda angl: Matrix(((cos(angl), -sin(angl), 0, 0),
                     (sin(angl), cos(angl), 0, 0),
                     (0, 0, 1, 0),
                     (0, 0, 0, 1))) 

obj = bpy.data.objects['Cube']


obj.rotation_mode = 'QUATERNION'

frame_start = bpy.context.scene.frame_start
frame_end = bpy.context.scene.frame_end
step = 5

for i in range(frame_start, frame_end, step):
    loc, rot, sca = obj.matrix_world.decompose()
    mloc = Matrix.Translation(loc)
    mrot = rot.to_matrix().to_4x4()
    msca = Matrix.Scale(sca[0], 4, (1, 0, 0)) * Matrix.Scale(sca[1], 4,     (0,1,0)) * Matrix.Scale(sca[2], 4, (0,0,1))

    Rm = f(pi/frame_end*8*i)
    obj.matrix_world = mloc * Rm * msca
    obj.keyframe_insert(data_path='rotation_quaternion', frame=i)

The mathematical model works just fine. The cube is rotated around the z axis like it should.

My problem arises when blender transforms the rotation matrix in obj.matrix_world to quaternion coordinates. The resulting curve becomes:

enter image description here

The cube in this case rotates one turn. Then blenders interpolation causes the cube to quickly spin back 360 degrees before countinuing its rotation. The problem may be tied with how I insert the keyframes with quaternions.

I would like the rotation to appear continous without this back-spin. There are modifiers that can take a rotation and repeat it, but that wont work for me since i want to visualize a mathematical model.

So my question is if anyone has any idea on how to mend this.



1 Answer 1


In your current F-curve the interpolation between frames (75,80) , (125,130) is linear, and because there is 5 frames between the frame sets I mentioned instead of 1, the rotation reverses in those 5 frames, causing the object to spin backwards very quickly. What you can do:

  1. Bring all the keyframes starting from frame 80 back 4 frames, so the gap becomes 1 frame instead of 5, and I recommend changing the interpolation from linear to no interpolation, so if you change the framerate or move the keyframes a little bit, the backward rotation will not be visible a lot.
  2. Do that with the keyframes starting from 130, and for any similar 5 frame gaps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .