1
$\begingroup$

Say I have a quaternion rotation in the world space, and I need an object to rotate according to that rotation, however, that object has its own axis. What sort of operations would I need to perform to get the rotation in that objects space?

Asking because rotations when performing manual rotations with a path curve, and a bone, the bone rotations shows a different quaternion to the path, however I know the path is correct, so I assume it has to do with the bones axis or roll or local space.

Edit. An image.

enter image description here

Edit 2. Tried bone constraint.

enter image description here

Edit 3. Desired result with bone constraint. enter image description here

I basically need to get this result in script. I've tried decomposing the matrix of the bone and multiplying an additional rotation matrix, however that didn't work. I'm not sure if I'm on the right track, but here's what I did:

rot_mat = Quaternion([0.8223, -0.0175, 0.4313, -0.3707]).to_matrix().to_4x4()
orig_loc, orig_rot, orig_scale = shoulder_bone.matrix.decompose()
orig_loc_mat = Matrix.Translation(orig_loc)
orig_rot_mat = orig_rot.to_matrix().to_4x4()
orig_scale_mat = Matrix.Scale(orig_scale[0],4,(1,0,0)) * Matrix.Scale(orig_scale[1],4,(0,1,0)) * Matrix.Scale(orig_scale[2],4,(0,0,1))
#Is this what it means to apply rotation before original? 
shoulder_bone.matrix_world = orig_loc_mat * rot_mat * orig_rot_mat * orig_scale_mat 
$\endgroup$
3
  • $\begingroup$ hello could you please show a drawing of what you want? $\endgroup$
    – moonboots
    Jan 17, 2021 at 5:36
  • $\begingroup$ @moonboots Yes, I have edited the post and added an photos of what I am referencing. As you can see, the rotations are the same numerically, however the are different if you look at it. I want the rotation to look the same, for the bone that is. $\endgroup$
    – eitroo
    Jan 17, 2021 at 19:00
  • $\begingroup$ Am even more confused after latest edit. Could you please add an image showing expected result. re copy rotation constraint shown, is the bone "Shoulder.L" the parent of "Shoulder to Elbow.L" ? $\endgroup$
    – batFINGER
    Jan 18, 2021 at 11:43

2 Answers 2

2
$\begingroup$

Yes, so, in case anyone is wondering how to stack transformation matrices, the code I posted in the question is mostly correct asides from the last line, instead of * you should put in @ signs, like so:

rot_mat = Quaternion([0.8223, -0.0175, 0.4313, -0.3707]).to_matrix().to_4x4()
orig_loc, orig_rot, orig_scale = shoulder_bone.matrix.decompose()
orig_loc_mat = Matrix.Translation(orig_loc)
orig_rot_mat = orig_rot.to_matrix().to_4x4()
orig_scale_mat = Matrix.Scale(orig_scale[0],4,(1,0,0)) * Matrix.Scale(orig_scale[1],4,(0,1,0)) * Matrix.Scale(orig_scale[2],4,(0,0,1))
shoulder_bone.matrix_world = orig_loc_mat @ rot_mat @ orig_rot_mat @ orig_scale_mat 
$\endgroup$
1
1
$\begingroup$

Transforms, as shown, are local transforms, not world transforms. So yes, you can very easily have two orientations that have the same numbers, but different orientations.

The easiest way to copy a world-space orientation is to use a copy rotation constraint-- either a bone constraint or an object constraint, depending on what you want constrained-- in world->world space, obviously. That way, Blender does the math for you.

Constraints won't show up in the transform channel; they use the transform channels as inputs that they modify. If you want, you can make a constraint "real" by selecting the constrainted object and using the "apply visual transform" operation (from the ctrl a menu for me, in either object or pose mode depending on the type of constraint.) After applying visual transform, you can delete the constraint, or leave it if you'd prefer.

If you're interested in the pure math associated with this, I would recommend reading (and understanding) https://en.wikipedia.org/wiki/Rotation_matrix . https://en.wikipedia.org/wiki/Basis_(linear_algebra) may also be useful.

$\endgroup$
3
  • $\begingroup$ Hi, I've tried adding the bone constraint, and yes it copies rotation, however the same issue arises, where the orientation is all wrong. I've added a picture. Edit: I've changed mix option to 'Before Original' and it apparently worked as intended. $\endgroup$
    – eitroo
    Jan 18, 2021 at 11:26
  • $\begingroup$ Do you know maybe how I could perform this through a script? $\endgroup$
    – eitroo
    Jan 18, 2021 at 11:29
  • $\begingroup$ @eitroo if Copy Rotation in replace mode, world->world, doesn't give you what you want, then you don't want the world space transform of the targeted object as the world space transform of the constrained object. Before even worrying about scripting, it would make sense to figure out exactly what it is that you do want. $\endgroup$
    – Nathan
    Jan 18, 2021 at 16:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .