1
$\begingroup$

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>
$\endgroup$
2

1 Answer 1

4
$\begingroup$

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
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .