Is there a command for getting world-space matrix of pose bones, same as obj.matrix_world
for objects? I found pose_bone.matrix
, but it gives weird result for me.
Yes:
As the documentation says, a pose bones matrix is in object space, this means - unless the object has no loc/scale/rot applied, you will have to apply the objects matrix.
matrix_final =
obj.matrix_world
*
pose_bone.matrix
This test script adds an empty at the pose-bone.
# Assume we are in pose mode with an active bone
import bpy
from bpy import context
pose_bone = context.active_pose_bone
# we can get the object from the pose bone
obj = pose_bone.id_data
matrix_final = obj.matrix_world * pose_bone.matrix
# now we can view the matrix by applying it to an object
obj_empty = bpy.data.objects.new("Test", None)
context.scene.objects.link(obj_empty)
obj_empty.matrix_world = matrix_final
mathutils
now uses the PEP 465 binary operator for multiplying matrices.
matrix_final = obj.matrix_world * pose_bone.matrix
becomes
matrix_final = obj.matrix_world @ pose_bone.matrix
Since 2.8 object are now stored in Collections, so :
context.scene.objects.link(obj_empty)
becomes
bpy.data.collections['Collection'].objects.link(obj_empty)
Here is the complete script updated:
# Assume we are in pose mode with an active bone,
# and a collection named collecion exist
import bpy
from bpy import context
pose_bone = context.active_pose_bone
# we can get the object from the pose bone
obj = pose_bone.id_data
matrix_final = obj.matrix_world @ pose_bone.matrix
# now we can view the matrix by applying it to an object
obj_empty = bpy.data.objects.new("Test", None)
bpy.data.collections['Collection'].objects.link(obj_empty)
obj_empty.matrix_world = matrix_final