Skip to content

Instantly share code, notes, and snippets.

@daylanKifky
Created February 14, 2018 16:07
Show Gist options
  • Save daylanKifky/d2e508d9cccb3a7ad7a88076a1914f63 to your computer and use it in GitHub Desktop.
Save daylanKifky/d2e508d9cccb3a7ad7a88076a1914f63 to your computer and use it in GitHub Desktop.
Get the position of a bone in Blender in pose space
import bpy
from mathutils import *
C=bpy.context
D=bpy.data
def get_bone_co_pose_space(bone_name, tip_or_head):
"""Expects an active Armature object, and if run as main an empty object called "Empty" """
name = bone_name
C.object.data.show_axes = True
bone = C.object.data.bones[name]
Mtip = Matrix.Translation(bone.tail)
Mhead = Matrix.Translation(bone.head)
if tip_or_head.lower() == "tip":
dest = Mtip
elif tip_or_head.lower() == "head":
dest = Mhead
if bone.parent:
Mptip = Matrix.Translation(bone.parent.tail - bone.parent.head)
#head and orientation of parent bone
Mw = bone.parent.matrix_local
#grandfather orientation
Mw *= bone.parent.matrix.to_4x4().inverted()
#tip of parent bone
Mw *= Mptip
#back to orientation of parent bone
Mw *= bone.parent.matrix.to_4x4()
#tip of bone
Mw *= dest
#orientation of bone
Mw *= bone.matrix.to_4x4()
else:
Mw = bone.matrix_local
Mw *= bone.matrix.to_4x4().inverted()
Mw *= dest
Mw *= bone.matrix.to_4x4()
return Mw
if __name__ == "__main__":
D.objects["Empty"].matrix_world = \
get_bone_co_pose_space("Bone.001", "tip")
@rohitdavas
Copy link

It will throw an error - AttributeError: bpy_prop "Bone.matrix_local" is read-only.

Fix =

temp =  bone.parent.matrix_local
Mw  =temp.copy()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment