8
$\begingroup$

I have a simple function that I'd like to call from a driver Python expression.

enter image description here

I've run the script to define the function, but it doesn't seem to be accessible from the driver expression.

Error in Driver: The following Python expression failed:
    'myDriverFunction()'

Traceback (most recent call last):
  File "<bpy driver>", line 1, in <module>
NameError: name 'myDriverFunction' is not defined

Is it possible to modify the namespace in which drivers execute?

$\endgroup$
2

1 Answer 1

14
$\begingroup$

Your custom driver must add itself to the list of drivers BPY is aware of.
See the example here: https://docs.blender.org/manual/en/dev/animation/drivers/workflow_examples.html#driver-namespace

import bpy

def driverFunc(val):

    return val * val    # return val squared

bpy.app.driver_namespace['driverFunc'] = driverFunc    # add function to driver_namespace
$\endgroup$
1
  • $\begingroup$ Also be sure to run the script to have it apply. $\endgroup$
    – Jay
    Oct 20, 2023 at 18:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .