Skip to main content
2 of 4
added 724 characters in body

How do you access and set a specific bone animation fcurve in python ? e.g. set pinky02_L z rotation = 8 degrees

I want to access the keyframes for a specific bone in animation_data.action, just so I can reset them to a default value. My main problem is I dont know how to just access a specific bone.

The general purpose of this is to automatically reset the hand positions of bvh animation loaded onto a manuelbastionilab model. The bvh animation loads it in fine, but because there isnt really any hand data, it sets the animation to a default splayed out position. And because it assigns it to the wrong bones, it comes in with a crooked little finger. So I just want a simple python script to reset any animations I load in to a good default.

I think I should be able to get to the animation data for the keyframes with

myaction = bpy.context.active_object.animation_data.action

But I dont know what comes next, I dont know how to access the fcurves for a specific bone like a joint / bone in my little finger called pinky02_L

This seems incredibly basic but Ive spent ages googling it and failed miserably.

I suspect I might have problems with setting the data too ! I am using quaternions and so I want to access say the z rotation quaternion. So how do I access the z rot channel, I think its some array offset for the fcurve or something ? So once I have found the fcurve for pinky02l (pinky02lfcurve = ?), is it something like curve = pinky02lfcurve[6] to access the z rotation channel ? Or have I got it all horribly wrong ?

keyframePoints = curve.keyframe_points
    for keyframe in keyframePoints:

And Once I have a keyframe how do I set it to a value like :

keyframe.value = math.radians(8.0)

. Is it that simple ? I bet its not!

Any answers to any parts of this would be great ! p.s. thank you to batFinger and Scott Milner for answering my question about it setting it to the wrong degrees the other day.

So from the post lemon refers too I if I am understanding it correctly if I use:

for curve in pinky02lfcurve 
       if curve.data_path.endswith('rotation_quaternion') 
        if curve.array_index == 2 # or is it 3 for z with quaternions ?
            keyframePoints = curve.keyframe_points
            for keyframe in keyframePoints:
                keyframe.co[1] = math.radians(8.0)

that should allow me to set a value ? though Im still hazy on if Im searching curves in fcurves or if a curve and an fcurve is one and the same thing! And I still need to know how to find the fcurve for pinky02_L to start with ! If anyone can confirm if any of this looks ok that would be good