I am trying to do what I thought would be a simple task but turned out to be a very frustrating endeavor.
I need to program an object to undergo a long series of random 90-degree rotations around the origin of the object, which coincides with the World's origin. I am using Python to set the angles and create keyframes. Let's say I need a 90-degree rotation around the global X, then 90-degree around Y and then -90 around X again. The following code does not work properly:
obj = bpy.data.objects["Sphere"]
obj.animation_data_clear()
obj.rotation_euler = [0, 0, 0]
obj.keyframe_insert(data_path="rotation_euler", frame=1, index=0)
obj.keyframe_insert(data_path="rotation_euler", frame=1, index=1)
obj.keyframe_insert(data_path="rotation_euler", frame=1, index=2)
# rotate 90 around x
obj.rotation_euler.x += pi / 2
obj.keyframe_insert(data_path="rotation_euler", frame=20, index=0)
obj.keyframe_insert(data_path="rotation_euler", frame=20, index=1)
obj.keyframe_insert(data_path="rotation_euler", frame=20, index=2)
# rotate 90 around y
obj.rotation_euler.y += pi / 2
obj.keyframe_insert(data_path="rotation_euler", frame=40, index=0)
obj.keyframe_insert(data_path="rotation_euler", frame=40, index=1)
obj.keyframe_insert(data_path="rotation_euler", frame=40, index=2)
# rotate -90 around x
obj.rotation_euler.x -= pi / 2
obj.keyframe_insert(data_path="rotation_euler", frame=60, index=0)
obj.keyframe_insert(data_path="rotation_euler", frame=60, index=1)
obj.keyframe_insert(data_path="rotation_euler", frame=60, index=2)
Depending on the rotation mode (XYZ, ZYX, etc.) the results are different and never what I want them to be. For example, under XYZ, the first two rotations are correct, but the 3rd rotation ends up being around the Z-axis instead of Y.
So how do I make those rotations consistent, and without wobbling? Someone please point me in the righ direction. Thanks!
Object.matrix_world
directly or apply rotations on it for rotations in global space (may require some matrix math). I remember a post at BA.org about this, but couldn't find it so far. $\endgroup$ – CodeManX Aug 29 '15 at 15:58