Sub topic 14
Reading and Editing F-Curves with Python
Assume that you know basic Blender navigation, can insert keyframes, and understand object location, rotation, and scale. This chapter uses Blender’s Python API to inspect and change animation data that you would normally edit in the Graph Editor.
The data path from an object to an F-curve
An animated object usually has animation data, an action, and one or more F-curves. An F-curve is a sequence of values for one property component over time.
For example, location contains three components. Blender stores them as separate curves:
| Data path | Array index | Meaning |
|---|---|---|
| `location` | 0 | X location |
| `location` | 1 | Y location |
| `location` | 2 | Z location |
The curve’s data_path identifies the property. Its array_index identifies the component. Each curve contains keyframe_points, which store frame and value coordinates.
Finding and inspecting a curve
The following script creates two location keys if an object is not already active. It then finds the X location curve and prints its keyframes.
import bpy
obj = bpy.context.object
if obj is None:
bpy.ops.mesh.primitive_cube_add()
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)
action = obj.animation_data.action
fcurve = action.fcurves.find("location", index=0)
print("Path:", fcurve.data_path)
print("Component:", fcurve.array_index)
for point in fcurve.keyframe_points:
frame, value = point.co
print(f"Frame {frame}: X = {value}")
print("Value at frame 12:", fcurve.evaluate(12))evaluate(frame) calculates the value produced by the curve at a particular frame. This includes interpolation between keys and any active curve modifiers.
Editing keyframe points and handles
Each keyframe point has coordinates in the form (frame, value). You can change those coordinates directly. After changing points, call fcurve.update() so Blender recalculates the curve.
You can also set interpolation and handle types:
for point in fcurve.keyframe_points:
point.interpolation = "BEZIER"
point.handle_left_type = "AUTO_CLAMPED"
point.handle_right_type = "AUTO_CLAMPED"
# Move the second key to frame 30 and value 6.
second_point = fcurve.keyframe_points[1]
second_point.co = (30.0, 6.0)
fcurve.update()AUTO_CLAMPED handles reduce unwanted overshoot near a key. Other useful handle types include VECTOR, ALIGNED, and FREE.
Adding procedural motion
F-curve modifiers change a curve without requiring more stored keyframes. This example makes the curve continue with a straight slope and adds small random variation.
fcurve.extrapolation = "LINEAR"
noise = fcurve.modifiers.new(type="NOISE")
noise.scale = 5.0
noise.strength = 0.2Use a CYCLES modifier when a motion should repeat:
cycles = fcurve.modifiers.new(type="CYCLES")
cycles.mode_before = "REPEAT"
cycles.mode_after = "REPEAT"Inspect modifiers with fcurve.modifiers. Remove one with fcurve.modifiers.remove(modifier). Be careful when changing shared actions, because several objects may use the same action.
A practical Python workflow
- Find the object and confirm that it has animation data.
- Find the curve by
data_pathandarray_index. - Read or change
keyframe_points. - Call
update()after direct edits. - Use
evaluate()to test the result at important frames. - Add modifiers only when procedural behavior is useful.
- Save a copy before scripts that change many curves.
Exercise
Which F-curve contains an object’s Y location, and how would you read its value at frame 18?
Show answer
Use action.fcurves.find("location", index=1) and call fcurve.evaluate(18).