Sub topic 14
Reading and editing F-curves with Python
This chapter shows how to reach the F-curves you have been editing in the Graph Editor from Blender's Python API (bpy). It assumes you can already insert keyframes, know what a data path and an array index are, and can run a script from the Text Editor with an object selected and active. You do not need to know Python well beyond loops and attribute access.
Where the curves live
An object does not hold F-curves directly. The chain is: object, then animation_data, then action, then fcurves, then keyframe_points.
animation_data is None until something is keyed or an action is assigned. Many first errors come from skipping this check.
Reading a curve
Each F-curve is identified by a data path (a string such as "location") and an array index (0, 1, 2 for X, Y, Z). The find method takes both and returns None if no match exists.
import bpy
obj = bpy.context.object
obj.location = (0.0, 0.0, 0.0)
obj.keyframe_insert(data_path="location", frame=1)
obj.location = (4.0, 0.0, 0.0)
obj.keyframe_insert(data_path="location", frame=24)
# The X location curve has data path "location" and array index 0.
fcurve = obj.animation_data.action.fcurves.find("location", index=0)
print(fcurve.data_path, fcurve.array_index, len(fcurve.keyframe_points))Running this prints location 0 2. If you leave out index, it defaults to 0, so the Y and Z curves need index=1 and index=2 explicitly.
To list everything on an object, loop over action.fcurves:
import bpy
obj = bpy.context.object
anim = obj.animation_data
if anim is None or anim.action is None:
print("No animation on", obj.name)
else:
for fc in anim.action.fcurves:
group = fc.group.name if fc.group else "(no group)"
print(f"{fc.data_path}[{fc.array_index}] group={group} keys={len(fc.keyframe_points)}")Properties that mirror Graph Editor controls are plain attributes on the curve: select, hide, lock, and mute. Setting fc.mute = True has the same effect as the checkbox in the channel list.
Keyframe points
Each entry in fcurve.keyframe_points is a point with these attributes:
| Attribute | Meaning |
|---|---|
| `co` | Position as `(frame, value)` |
| `interpolation` | Mode toward the next key, such as `'BEZIER'`, `'LINEAR'`, `'CONSTANT'` |
| `easing` | `'AUTO'`, `'EASE_IN'`, `'EASE_OUT'`, `'EASE_IN_OUT'` |
| `handle_left`, `handle_right` | Handle positions as `(frame, value)` |
| `handle_left_type`, `handle_right_type` | `'AUTO_CLAMPED'`, `'AUTO'`, `'VECTOR'`, `'ALIGNED'`, `'FREE'` |
| `select_control_point` | Whether the key itself is selected |
To read the curve's value at any frame, including between keys, use fcurve.evaluate(frame). This applies interpolation, extrapolation, and modifiers, so it reports what the property will actually be on that frame. The value is read from the curve itself, not from the object.
import bpy
fcurve = bpy.context.object.animation_data.action.fcurves.find("location", index=0)
for frame in (1, 6, 12, 18, 24, 30):
print(frame, round(fcurve.evaluate(frame), 3))Editing interpolation and handles
Setting these attributes is the scripted version of the Interpolation and Handle Type menus. This script applies Bezier interpolation with Auto Clamped handles to every key on one curve:
import bpy
obj = bpy.context.object
fcurve = obj.animation_data.action.fcurves.find("location", index=0)
for kp in fcurve.keyframe_points:
kp.interpolation = 'BEZIER'
kp.handle_left_type = 'AUTO_CLAMPED'
kp.handle_right_type = 'AUTO_CLAMPED'
fcurve.update()fcurve.update() re-sorts the points and recalculates automatic handles. Call it after you change co or handle types, otherwise the curve can look stale until the next UI refresh.
Two details cause confusion:
easingonly has a visible effect on non-Bezier interpolation types such as'SINE'or'QUAD'. Setting it on a Bezier key does nothing, which matches the Graph Editor, where the Easing menu is for those other modes.- Handle types matter when you move handles by script. If a handle type is
'AUTO_CLAMPED', Blender recalculates the handle and discards your value. To place a handle by hand, set the type to'FREE'or'ALIGNED'first, then assignhandle_leftorhandle_right.
Moving and adding keys
You can change a key's position by assigning to co. Moving a key's co does not move its handles for you. With 'FREE' handles, shift them by the same amount:
import bpy
fcurve = bpy.context.object.animation_data.action.fcurves.find("location", index=0)
kp = fcurve.keyframe_points[-1]
kp.handle_left_type = 'FREE'
kp.handle_right_type = 'FREE'
dx, dy = 6.0, 1.0
kp.co.x += dx
kp.co.y += dy
kp.handle_left.x += dx
kp.handle_left.y += dy
kp.handle_right.x += dx
kp.handle_right.y += dy
fcurve.update()To add a key to an existing curve, use keyframe_points.insert(frame, value). It returns the new point, and if a key already exists on that frame, it replaces the value instead of adding a duplicate.
import bpy
fcurve = bpy.context.object.animation_data.action.fcurves.find("location", index=0)
new_kp = fcurve.keyframe_points.insert(12, 2.5)
new_kp.interpolation = 'BEZIER'
print([tuple(k.co) for k in fcurve.keyframe_points])To create a curve that does not exist yet, call action.fcurves.new(data_path="scale", index=0). Then add points with insert. To remove a key, use fcurve.keyframe_points.remove(kp). To remove a whole curve, use action.fcurves.remove(fcurve).
Extrapolation and modifiers
Extrapolation is set per curve. 'CONSTANT' holds the first and last values, and 'LINEAR' continues along the slope at each end. Modifiers live in fcurve.modifiers, and you create them by type name. This example makes the curve continue linearly outside the keyed range and adds Noise:
import bpy
obj = bpy.context.object
fcurve = obj.animation_data.action.fcurves.find("location", index=0)
fcurve.extrapolation = 'LINEAR'
noise = fcurve.modifiers.new(type='NOISE')
noise.scale = 5.0
noise.strength = 0.2For looping motion, add a Cycles modifier with fcurve.modifiers.new(type='CYCLES'). Modifiers run in list order, and each has its own mute flag, so you can switch one off without deleting it. Because evaluate() includes modifiers, a Noise modifier makes the result differ from the raw keys. If you need the keys alone, mute the modifier or read kp.co directly.
Common errors
AttributeError: 'NoneType' object has no attribute 'action': the object has noanimation_data. Key something first, or create it withobj.animation_data_create().fcurveisNone: the data path or index is wrong. Vector properties need the rightindex, and a path for a bone looks likepose.bones["Arm"].location, notlocation.- Your handle edits vanish: the handle type is automatic. Set it to
'FREE'or'ALIGNED'before assigning. - Keys appear out of order after setting
co: callfcurve.update(). - A script that worked fails on a newer Blender: you are probably using
action.fcurvesafter the slotted-actions change.
Exercise
Write the lines that find the Z rotation curve of the active object and set its extrapolation to linear. Then explain why fcurve.evaluate(30) can differ from the value stored in the last keyframe when the last key is on frame 24.
Show answer
Use fcurve = bpy.context.object.animation_data.action.fcurves.find("rotation_euler", index=2) and then fcurve.extrapolation = 'LINEAR'. The Z rotation curve has data path rotation_euler and index 2 (this assumes the object uses Euler rotation mode). Frame 30 is outside the keyed range, so evaluate applies extrapolation. With linear extrapolation the value continues along the end slope instead of staying at the last key's value. A modifier such as Noise would also change the result.
Exercise
You set kp.handle_right.y = 3.0 on a key, but after fcurve.update() the handle is back at its old position. What is the most likely cause and the fix?
Show answer
The key's handle type is automatic, such as 'AUTO_CLAMPED', so Blender recalculates the handle and discards the manual value. Set kp.handle_right_type = 'FREE' (or 'ALIGNED') before assigning handle_right.