You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the long way, the scenic route. Using a python expression from a file. Sometimes you'll have expressions that are a bit more complicated than what you can fit onto 1 line comfortably.
Make a script in the Text Editor with content similar to this, then run it.
importmathimportbpydefdriver_func(current_frame):
frames=120# <-- period.theta=math.pi*2/framesfinal=math.sin(current_frame%frames*theta)
returnfinal# add function to driver_namespacebpy.app.driver_namespace['driver_func'] =driver_func
Right-click the property you want to control (for instance the X rotation of a cube), choose Add Single Driver
enable Auto run python scripts in User Preferences -> File.
Open the Graph Editor, and switch to Drivers view
select the driven property, and toggle the rightside panel
If your driver has a python error, you can correct it and run the code again to overwrite
the namespace key 'driver_func`. You can name this function whatever you wish, and can have as many of them as you need in the .blend.
example 2
This might be obvious, but let's be explicit about the possibilities. We can use Empties as dummy objects, and give them custom properties, and assign a Driver to the custom property. Drivers can execute pretty much anything, that means also updating multiple other objects.
The following code randomizes the location of an object named 'Text' between frame 30 and 60. The return value of the driver function is never used, it's the function body we care about.
importmathimportbpyimportrandomdefdriver_func2(current_frame):
obj=bpy.data.objects.get('Text')
ifnotobj:
return0.0rnd=random.randomif30<current_frame<60:
obj.location=rnd(), rnd(), rnd()
else:
ifobj.location.length>0.0001:
obj.location=0, 0, 0return0.0# add function to driver_namespacebpy.app.driver_namespace['driver_func2'] =driver_func2
example 3
Change material depending on delta location of keyframed object
importbpyfrommathutilsimportVectordefdriver_delta_to_RED(frame):
# triggered by a frame change, any code inside here gets run.p=bpy.data.objects['Plane']
current_xyz=p.locationfcurve=p.animation_data.action.fcurvesx=fcurve[0].evaluate(frame-1)
y=fcurve[1].evaluate(frame-1)
z=fcurve[2].evaluate(frame-1)
x1=fcurve[0].evaluate(frame)
y1=fcurve[1].evaluate(frame)
z1=fcurve[2].evaluate(frame)
# may want to find the top speed first, and normalize# this value using that informationdelta= (Vector((x1, y1, z1)) -Vector((x, y, z))).lengthprint("delta is", delta)
nodes=bpy.data.materials[0].node_tree.nodesnodes[0].inputs[0].default_value= (delta, 0, 0, 1.0)
# the return value is of no relevance and can be static.return0.0bpy.app.driver_namespace['driver_delta_to_RED'] =driver_delta_to_RED