2
$\begingroup$

Let's say I have a mesh with an armature and I've given it an animation action. I know I can get the action with bpy.data.actions and the bones with bpy.context.selected_pose_bones. But how would I get only the bones that have keyframes set for that specific action? For example, let's say I make a kick action and only bones for the right leg have keyframes, no other bones have any for that action. How would I get the bones for the right leg given the kick action?

$\endgroup$
1
$\begingroup$

Following is one way of getting the bones in a given action.

armature = bpy.context.scene.objects['Armature']
action = bpy.data.actions['ArmatureAction.002']

pose_bones = set()

for fcurve in action.fcurves:
    pose_bone_path = fcurve.data_path.rpartition('.')[0]
    pose_bone = armature.path_resolve(pose_bone_path)
    pose_bones.add(pose_bone)

>>> pose_bones
{
    bpy.data.objects['Armature'].pose.bones["R_lower_jnt"],
    bpy.data.objects['Armature'].pose.bones["R_hip_jnt"], 
    bpy.data.objects['Armature'].pose.bones["R_thigh_jnt"]
}

Getting to the actual bone (that belongs to the data of the Armature) is accomplished like below

>>> bone = armature.data.bones[pose_bone.name]
>>> bone
bpy.data.armatures['Armature'].bones["R_hip_jnt"]

References

| improve this answer | |
$\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.