1

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>
Dazotaro
  • 901
  • 1
  • 9
  • 20
  • 2
    Related https://blender.stackexchange.com/questions/74773/how-to-get-xyz-coordinates-from-f-curve https://blender.stackexchange.com/questions/74512/how-to-get-keyframes-and-related-information https://blender.stackexchange.com/questions/64342/finding-a-specific-fcurve – batFINGER Mar 30 '19 at 19:19
  • Thanks @batFINGER, all those related questions helped. – Dazotaro Mar 30 '19 at 23:14

1 Answers1

4

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
Dazotaro
  • 901
  • 1
  • 9
  • 20