Copy Global Rotation
This example I am copying the global rotation of the active to all other selected objects.
Get the rotation matrix of the active object from its 3x3 rotation matrix, and convert to 4x4
Then for each selected object, decompose, keep the scale and location and recompose with the new rotation. A 4x4 transform matrix is composed of translation, rotation and scale. M = T @ R @ S
. Decomposing is separating the parts into a vector, quaternion, and vector for each respectively.
import bpy
from mathutils import Matrix
context = bpy.context
ob = context.object
R = ob.matrix_world.to_3x3().to_4x4() # rot matrix
for o in context.selected_objects:
if o is ob:
continue
# decompose
loc, _, scale = o.matrix_world.decompose()
# recompose
o.matrix_world = (
Matrix.Translation(loc) @
R @
Matrix.Diagonal(scale.to_4d())
)
for local use the local matrix. For basis (ie what you see in UI rotation field) use basis matrix. Does a child object inherit the matrix from the parent?
Another method would be to add a copy rotation constraint to each, and apply. Apply a Transform