import bpy
def driver_func(val):
activeobj = bpy.context.active_object
if activeobj.name == "A":
output = val * val
else:
output = val + 20
return output
#add function to driver_namespace
bpy.app.driver_namespace['driver_func'] = driver_func
Thanks to RJG for the improvement.
This script indeed runs in the scripting editor without errors, but when I
add driver_func in the driver's editor as scripted expression to an object's transform (e.g. z-rotation) I get ERROR: Invalid Python Expression ...
The terminal message reads:
Traceback (most recent call last):
File "<bpy driver>", line 1, in <module>
File "/Users/.../Test.blend/TestPython", line 4, in driver_func
AttributeError: 'Context' object has no attribute 'active_object'
