2
$\begingroup$

I have some bones with any number of constraints. I want to get the visual transform matrix of that bones that takes also into account when for example a stretch to constraint modifies that bone.

So basically the output of setting keyframes with Visual LocRotScale setting.

This would be really helpfull for exporting bone data.

$\endgroup$
2
$\begingroup$

The page of PoseBone.matrix says

Final 4x4 matrix after constraints and drivers are applied (object space)

You can convert the matrix from object space to local space calling Object.convert_space

Edit

The comment of convert_space explicitly states that this can not be used for visual keyframing. For the simplest options with

  • Inherit Rotation
  • Inherit Scale

enabled. You can get the local transform for the bone like this

bone properties

def get_mat_offset(pose_bone):
    """ calculate the (local) rest matrix of the bone 
        needs to be multiplied with the parent's pose matrix
        to get the rest matrix in armature space """
    bone = pose_bone.bone
    mat_offset = bone.matrix.to_4x4()
    mat_offset.translation = bone.head.copy()
    mat_offset.translation.y += bone.parent.length
    return mat_offset

pb = obj.pose.bones['Bone.001']

if pb.parent:
    mat_offset = get_mat_offset(pb)
    mat_pose_parent = pb.parent.matrix

    mat_rest_inv = mat_pose_parent * mat_offset
    mat_rest_inv.invert()

    matrix_basis = mat_rest_inv * pb.matrix

else:
    matrix_basis = pb.bone.matrix_local.inverted() * pb.matrix

Further you can decompose the matrix calling mathutils.Matrix.decompose

loc, rot, scale = mat_basis.decompose()
| improve this answer | |
$\endgroup$
  • $\begingroup$ This answer was incorrect. An (unvalidated) python port of blender's posing code can be found here. $\endgroup$ – pink vertex Apr 1 '16 at 17:41

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.