Sub topic 15
Drivers: F-curves driven by other values
This chapter assumes you can read curves in the Graph Editor and know that an F-curve has a data path, an array index, keyframe points, extrapolation and optional modifiers. It also assumes basic Python in Blender's Scripting workspace. It builds directly on the earlier chapters on F-curves and F-curve modifiers.
What a driver is
An animation F-curve maps time to a property value. The input is the current frame and the output is, for example, the X location.
A driver is also an F-curve, but its input is not the frame. The input is a number computed from other data in the scene, such as another object's position, a bone's rotation, or a custom property. The curve turns that number into the value of the driven property. A driven property updates whenever its inputs change, with no keyframes on a timeline.
Typical uses:
- A corrective shape key that turns on as an elbow bends.
- A wheel that rotates in proportion to how far the car has moved.
- One slider that controls several properties at once.
- A light whose strength follows the height of an object.
Because a driver is an F-curve, everything you already know applies: interpolation, handles, extrapolation, and modifiers. What changes is the meaning of the horizontal axis.
Where drivers are stored
Keyframe F-curves live in an action. Driver F-curves do not. They are stored on the datablock's animation data, in animation_data.drivers. This has two consequences:
- Changing or unlinking an object's action does not remove its drivers.
- Python code that uses
animation_data.action.fcurvesdoes not see them. Useanimation_data.driversinstead.
Creating a driver in the interface
- Right-click a property field, such as Scale Z, and choose Add Driver. A popover opens to configure the driver. Pressing Ctrl+D while hovering over a property adds a driver with default settings.
- The field turns purple. This means the property is now driven and you cannot set it by hand.
- Open the Drivers Editor. Either change an editor's type to Drivers, or use Window > Drivers Editor. It looks like the Graph Editor and uses the same navigation, channel list and key tools.
- Select the driven channel, then open the Drivers tab in the Sidebar (N) to configure it.
A field that is red-underlined or shown in red has an invalid driver. The usual causes are a variable pointing at a missing object or a path that does not exist, and an expression with a syntax error. The Drivers tab shows an error message that names the problem.
Driver types
The driver type decides how the variables are combined into one number, which is the input to the F-curve.
| Type | Result |
|---|---|
| Scripted Expression | A Python-syntax expression using the variable names |
| Averaged Value | The mean of all variables |
| Sum Values | The sum of all variables |
| Minimum Value | The smallest variable |
| Maximum Value | The largest variable |
For the last four types you do not write an expression. For Scripted Expression, Blender evaluates simple arithmetic itself. Examples are x * 2, var + 1, sin(x) and max(a, b). It does not start a Python interpreter for these, so they work even when Auto Run Python Scripts is disabled. Expressions that need more, such as calling arbitrary Python functions, are treated as full Python and are blocked unless auto-run is enabled for that file. Prefer simple expressions where you can. They are faster and they keep your file working for people who have auto-run turned off.
Driver variables
Each variable has a name, a type, and one or more targets.
| Variable type | What it reads |
|---|---|
| Single Property | Any property, addressed by an ID and an RNA data path |
| Transform Channel | One location, rotation or scale channel of an object or bone, in a chosen space |
| Rotational Difference | The angle between two objects or bones |
| Distance | The distance between two objects or bones |
| Context Property | A property of the scene or view layer in the current context |
Transform Channel is the easiest to set up, because you pick an object and a channel from menus. The Space setting matters. World Space reads the final position after parents and constraints. Transform Space is the object's local channel value, and Local Space is the value relative to its parent. If a driver gives odd results when the source is parented, check this setting first.
Single Property is the most general. For a custom property named open on an object, the path is ["open"]. For a modifier setting, the path might be modifiers["Subdivision"].levels. Right-clicking a property and choosing Copy as New Driver and then Paste Driver on another property builds the variable for you.
The driver F-curve: remapping the value
When Blender creates a driver, its F-curve usually has a Generator modifier and no keyframes. The Generator is set up as a straight line with factor 1 and offset 0, so the output equals the input.
You can reshape that relationship in two ways.
Use the expression. For example, var * 0.5 + 1 sets a scale and an offset in one line.
Use the curve. Delete the Generator modifier in the Modifiers tab, then insert keyframes on the driver curve. In the Drivers Editor the horizontal axis is the driver's input value, not time. A key at X = 0, Y = 1 and another at X = 2, Y = 0 means "when the input is 0, output 1. When the input is 2, output 0", with the interpolation you chose between them. This is how you build a non-linear response, for example an easing curve that opens a door quickly at first.
Outside the first and last key, the curve follows its extrapolation setting. With Constant extrapolation the output stops changing beyond the last key. With Linear it keeps going along the end slope. Choose deliberately, because a driver input can wander beyond the range you tested.
The View menu's Show Only Selected and the normal key tools all work here. Use Frame Selected (numpad period) to find a curve when its input values are far from the frame range.
Creating and editing drivers with Python
driver_add() creates a driver and returns the F-curve. For an array property such as scale, pass an index to get one F-curve. If you omit the index, you get a list of F-curves, one per element.
This script is complete. It creates two objects and drives the sphere's Z scale from the cube's world X location.
import bpy
bpy.ops.mesh.primitive_cube_add(location=(0.0, 0.0, 0.0))
source = bpy.context.object
source.name = "Source"
bpy.ops.mesh.primitive_uv_sphere_add(location=(0.0, 3.0, 0.0))
target = bpy.context.object
target.name = "Target"
# Drive Scale Z (array index 2) of the sphere.
fcurve = target.driver_add("scale", 2)
driver = fcurve.driver
driver.type = 'SCRIPTED'
var = driver.variables.new()
var.name = "x"
var.type = 'TRANSFORMS'
var.targets[0].id = source
var.targets[0].transform_type = 'LOC_X'
var.targets[0].transform_space = 'WORLD_SPACE'
driver.expression = "1 + x * 0.5"Move the cube along X and the sphere stretches or shrinks. Check that the driver is valid with driver.is_valid. If it is False, driver.is_simple_expression and the Drivers tab help locate the cause.
To remap through keyframes instead of the expression, clear the Generator and add points whose frame value is the driver input:
import bpy
target = bpy.data.objects["Target"]
fcurve = next(
fc for fc in target.animation_data.drivers
if fc.data_path == "scale" and fc.array_index == 2
)
fcurve.driver.expression = "x"
for mod in list(fcurve.modifiers):
fcurve.modifiers.remove(mod)
fcurve.keyframe_points.insert(0.0, 1.0)
fcurve.keyframe_points.insert(2.0, 3.0)
fcurve.keyframe_points.insert(4.0, 3.5)
for kp in fcurve.keyframe_points:
kp.interpolation = 'BEZIER'
kp.handle_left_type = 'AUTO_CLAMPED'
kp.handle_right_type = 'AUTO_CLAMPED'
fcurve.extrapolation = 'CONSTANT'
fcurve.update()Here the first argument of insert() is the position on the horizontal axis, which for a driver is the input value rather than a frame. To remove a driver, call target.driver_remove("scale", 2).
Common errors and gotchas
- Dependency cycles. If object A drives B and B drives A, directly or through a chain, Blender cannot order the evaluation. The result is lagging or wrong values, and the console reports a cycle. Make drivers read from a source that does not depend on the target.
- Reading the wrong space. Rotation values are in radians inside expressions, even though the interface shows degrees. A driver that reads a 90 degree rotation sees about 1.571.
- Driving a property you also keyframe. The driver wins, and the keyframes on that property have no visible effect. Remove one of them.
- Constraint or parent updates one frame late. Reading World Space from an object that is itself driven can lag. Prefer reading from the original source.
- Auto Run disabled. Complex scripted expressions show a warning and do nothing until you allow scripts for the file. Simple expressions are not affected.
- Lost links. If you rename or delete an object that a variable targets, the driver becomes invalid. Fix the variable's ID field.
Exercise
A lamp's Power value is driven by the Z location of an empty named "Dial" through a single Transform Channel variable called h. You want the lamp to be at 0 W when the empty is at Z = 0 and at 1000 W when Z = 2, and to stay at 1000 W if the empty goes higher. Describe two ways to set this up.
Show answer
Option 1, with the expression: set the type to Scripted Expression and use min(max(h, 0), 2) * 500. The clamp keeps the input between 0 and 2, and the factor 500 maps 2 to 1000. Option 2, with the curve: use the expression h, delete the Generator modifier, insert keys at (0, 0) and (2, 1000) on the driver curve, set the interpolation you want, and set extrapolation to Constant so the output stays at 1000 above Z = 2 (and at 0 below Z = 0).
Exercise
A Python script loops over obj.animation_data.action.fcurves to list every curve on a driven object, but a driven Scale Z curve is missing. Where should the script look, and how would you find that specific curve?
Show answer
Drivers are stored in obj.animation_data.drivers. Find the curve by matching its data path and index, for example next(fc for fc in obj.animation_data.drivers if fc.data_path == "scale" and fc.array_index == 2).