(Re-)Using custom python scripts for drivers

When writing scripted driver expressions, you may get annoyed by the rather small size of the ‘Expr’ text control in which you’re supposed to write them. The easiest solution to this problem is to write the (possibly lengthy) code/function elsewhere, and then call it from the ‘Expr’ field.

You can do this by writing your code in a script (text.py file) you create within the blend file, but this is still quite limiting in my experience. You can’t use your favorite python editor, and the code you write is not reusable in any other blender projects you may have. You really want to put that code wherever you want, and then be able to import that into any blend file you want.

The code below shows how to do that. You’ll still need to have this code in a script within the blend file, but it will import your external scripts and make their content available to be used in a scripted expression driver. You can probably call this ‘wrapper’ code, the actual driver code (not shown) being in the imported script. Btw, you’ll probably want this internal script to ‘register’ on loading, and make sure your blend file is trusted (so ‘auto-run scripts’ is on).

import os
import sys
import imp
import bpy

# 1/3: make sure python can find your scripts
blendDir = os.path.dirname(bpy.data.filepath)
parDir = os.path.join(blendDir, os.pardir)
scriptsDir = os.path.join(parDir, "scripts")
scriptsDir = os.path.normpath(scriptsDir)   # ! won't work without this (win7 x64)
if not scriptsDir in sys.path:
    sys.path.append(scriptsDir)

# 2/3: (if necessary: re-) import your scripts
import drivers
imp.reload(drivers)

# 3/3: make the content of your scripts available for evaluation in scripted driver
bpy.app.driver_namespace['driverMul'] = drivers.driverMul

It uses the folder where your blend file is located (blendDir) as a starting point. It then goes one level up (parDir), and then to the ‘scripts’ sub-folder (scriptsDir). So this example expects your scripts to be in a folder ‘scripts’ that is a sibling of the folder where your blend file is. The scriptsDir could easily be changed to any location on your hard drive, probably, eg ‘C:\blenderScripts’ (not tested). In this case, the drivers.py script contains a function called driverMul, which is added to the driver namespace, so it can be used in scripted expression drivers.

I’ve marked this thread as solved because it took me quite a while to figure out how to do this. I hope this helps save some time for anyone else looking for this.

PS: Tip: if you (re-) run the internal script after having modified the external script and nothing seems to change, try pressing ‘Update Dependencies’ in the driver properties panel. This may also save some time…

1 Like

It’s possible to set expression to driver from python code like this?
bpy.data.texts['Hatching_1'].as_module().FactorCalc()
eg.

bpy.data.materials["Hatching_1"].node_tree.nodes["Value"].set(bpy.data.texts['Hatching_1'].as_module().FactorCalc()`)

DDos on blender API blocking to check docs… ;/