An object has an action in its animation data, how can I edit its keyframes? When I print the snippet below, I see that there are 8 entries in the dictionary of fcurves (I assume that they are three entries for translation, 3 for rotation (euler) and 3 for scale). How can I edit each specific channel of this action? For example, how do I update the translation for the keyframes for channel Z-axis?

>> for key, value in bpy.data.actions['ActionName'].fcurves.items():
...     print(key, value)
0 <bpy_struct, FCurve at 0x7f2299562788>
1 <bpy_struct, FCurve at 0x7f2299562c88> 
2 <bpy_struct, FCurve at 0x7f2299562b88>
3 <bpy_struct, FCurve at 0x7f2299562208>
4 <bpy_struct, FCurve at 0x7f2299562a88>
5 <bpy_struct, FCurve at 0x7f2299562f08>
6 <bpy_struct, FCurve at 0x7f2299562888>
7 <bpy_struct, FCurve at 0x7f2299562d08>
8 <bpy_struct, FCurve at 0x7f2299690088>

The related answers (provided by @batFINGER) answered my question, but I provide here an example for completeness sake.

# Configuration
action_name = 'MyAction'
data_path = 'location'
index = 2                   # Z axis

# Find the appropriate action
action = bpy.data.actions.get(action_name)
if action:
    # From this action, retrieve the appropriate F-Curve
    fcurve = action.fcurves.find(data_path, index)
    if fcurve:
        # Iterate over all keyframes
        for kfp in fcurve.keyframe_points:
            # Print current keyframe info
            print('Frame = {:04}; Value = {}'.format(kfp.co[0], kfp.co[1]))
            # Change keyframe data
            # Push back in time by 2 frames
            kfp.co[0] += 2
            # Move F-Curve up by 1
            kpf.co[1] += 1
| improve this answer | |

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.