Need help with rotating different faces of a rubik's cube with python API. When different faces are rotated sequentially some cubes start to act up Pls help
now these first two rotations got animated fine


but in the third rotation one object started moving along a different axis
(even though there are other objects just like this one(been in the same roation before))

and when more rotations are added more blocks start behaving differently.
Here is a sample function:
The wrt_global is a list of dictionaries keeping track of the global axis to the axis of individual objects. (The commented out part is solutions I've already tried).
def rotate_front_anti(f):
obj=list(bpy.data.objects)
for sq in obj:
    sq.select_set(state=(sq.location[0]&gt;=2))
    

#bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='MEDIAN')
ob=bpy.context.selected_objects

for i in range(0,len(ob)):
   ob[i].rotation_mode='XYZ'

for i in range(0,15):
    for j in range(0,len(ob)):
        ob[j].keyframe_insert(data_path = &quot;location&quot;, frame = f, index= -1)
        ob[j].keyframe_insert(&quot;rotation_euler&quot;, frame = f)
    
        #ob[j].rotation_euler = (Matrix.Rotation(math.radians(-6), 3, 'X') * ob[j].rotation_euler.to_matrix()).to_euler()
        
        if wrt_global[int(ob[j].name)]['x'] == 'x':
            ob[j].rotation_euler.x += math.radians(-6)
        elif wrt_global[int(ob[j].name)]['x'] == 'y':
            ob[j].rotation_euler.y += math.radians(-6)
        else:
            ob[j].rotation_euler.z += math.radians(-6)
        #ob[j].rotation_axis_angle=(math.radians(-6),1,0,0)
        ob[j].location[2],ob[j].location[1] = new_coords(ob[j].location[2],ob[j].location[1],1,1,6)
        ob[j].keyframe_insert(data_path = &quot;location&quot;, frame = f, index= -1)
        ob[j].keyframe_insert(&quot;rotation_euler&quot;, frame = f)
    f+=1


  
for  j in range(0,len(ob)):
    temp=wrt_global[int(ob[j].name)]['y']
    wrt_global[int(ob[j].name)]['y']=wrt_global[int(ob[j].name)]['z']
    wrt_global[int(ob[j].name)]['z']=temp




bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='MEDIAN')
return f
Gimbal lock has been occurred for using rotation_euler. Can you try rotating matrix_local or matrix_world. It also has the advantage of simplifying the calculation of the rotation position from the rubik cube origin.