Created
February 14, 2018 16:07
-
-
Save daylanKifky/d2e508d9cccb3a7ad7a88076a1914f63 to your computer and use it in GitHub Desktop.
Get the position of a bone in Blender in pose space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It will throw an error - AttributeError: bpy_prop "Bone.matrix_local" is read-only.
Fix =