A sequence of 90-degree rotations with Python 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!
To do this with matrix multiplication &amp; globally.  As for the "wobbling" do  you mean the interpolation?

import bpy
from math import radians
from mathutils import Matrix

mat_rot_x = Matrix.Rotation(radians(90.0), 4, 'X')
mat_rot_mx = Matrix.Rotation(radians(-90.0), 4, 'X')
mat_rot_y = Matrix.Rotation(radians(90.0), 4, 'Y')

obj = bpy.data.objects["Sphere"]
obj.animation_data_clear()

obj.rotation_euler = [0, 0, 0]
bpy.context.scene.frame_set(1)
obj.keyframe_insert(data_path="rotation_euler", frame=1)

obj.matrix_world *= mat_rot_x
obj.rotation_euler = obj.matrix_world.to_euler()
obj.keyframe_insert(data_path="rotation_euler", frame=20)

obj.matrix_world *= mat_rot_y
obj.keyframe_insert(data_path="rotation_euler", frame=40)

obj.matrix_world *= mat_rot_mx
obj.rotation_euler = obj.matrix_world.to_euler()
obj.keyframe_insert(data_path="rotation_euler", frame=60)