1

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

Christoph Werner
  • 1,965
  • 1
  • 14
  • 31

1 Answers1

4

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

batFINGER
  • 84,216
  • 10
  • 108
  • 233