Hello!
I’m trying to set the global rotation of a child object in a parented hierarchy to a specific quaternion. Consider this setup, where the parent has some arbitrary rotation:
Here I’d like the global rotation of the selected cube to match the rotation of the cube on the right : Quaternion((0.8660253882408142, 0.0, 0.5, 0.0))
When the child only has a single parent as in the case above, the following code works:
q_target = Quaternion((0.8660253882408142, 0.0, 0.5, 0.0))
qp_inv = C.object.parent.matrix_world.to_quaternion()
qp_inv.invert()
C.object.rotation_quaternion = qp_inv @ q_target
The idea is that the local rotation is calculated by applying an inversion of the parent’s global rotation to the target rotation. This assumes that the rotations to the left are applied last, and works:
However, when the hierarchy is deeper (such as the setup below), the approach breaks down. I’ve tried premultiplying by the parent’s matrix_local.to_quaternion() up the chain, but it doesn’t work. Can someone please help me understand what’s wrong in my understanding and how to make this work for hierarchies with depth greater than 1?
Thanks!


