0
$\begingroup$

I'm looking to get the local rotations of bones using Python, in other words given a parent and a child bone, the child bone's rotation off of the parent bone.

I saw this post:Get bone rotation in pose mode using Python

But I'm under the impression that this gives the world rotation? Any explanations are appreciated, thank you!

skeleton

$\endgroup$

1 Answer 1

3
$\begingroup$

pose_bone.matrix is the transform from bone-local-space to armature-space (not world-space). It is the real transform, after constraints, drivers, etc. are applied.

So you can get the transform relative to the bone's parent by first transforming into armature space with pbone.matrix, then transforming from armature space in pbone.parent's space with pbone.parent.matrix.inverted().

for pbone in ob.pose.bones:
    m = pbone.matrix
    if pbone.parent:
        m = pbone.parent.matrix.inverted() @ m
    print(pbone.name, m)

Note that the matrix m might not be a pure rotation matrix in general (although for your screenshot it looks like it will be).

Also note that this won't work if you have non-invertible matrices. Avoid scaling bones to 0.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.