# How to copy rotation from Active to Selected in Python?

I'm new in Python and would like to know how to copy Rotation values from an Active to a Selected object.

I already have stored the active and selected object ids in vars and am able to select them both and set an active one. But I don't know how to "transfer" the rotation values from the active object to the selected object.

I'm using Blender 2.91.2.

Thank you

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

• Thynk you very much! Jan 25, 2021 at 12:34