Error when Adding a Python Script to a Driver I seem to have a bit of a problem that I was wondering if someone could help me with.  As the title says, for some reason I keep getting an error when I try to add my script to the driver, and I can’t seem to figure out why at all. Here is the screenshot of how I’m applying it to the driver:


and here is the code:

import bpy
import math

def ShiftMoonMonth(objectName):
    obj = bpy.data.objects[objectName]
    sourceRotation = obj.rotation_euler.z
    finalRotation = (math.floor(sourceRotation/389.10)*29.10564039280042)
    return finalRotation

bpy.app.driver_namespace['ShiftMoonMonth'] = ShiftMoonMonth


I have auto-run python scripts turned on, and when I look through the available namespaces in the blender console, ShiftMoonMonth is one of the options I can use.  So I’m pretty sure that the code is linked to the driver, but for some reason it’s still giving this error.  

The place I got the code template from was this tutorial here: https://youtu.be/rfEFU0Mdq3Q?t=21m50s at 21:50, where he is trying to solve a very similar problem to what I’m solving.  As far as I can tell, it seems to be the same as what I have, although it is quite old, so there may be something else you have to do. I also don’t understand the language, so I may have missed something.

Anyway, any help would be very appreciated!
For a single property type driver variable you need also to set the path value.  For example



import bpy

def x(name):
    print(name)
    return 0

bpy.app.driver_namespace["x"] = x


for example shown prints "Lamp", the name of the lamp object, to the console.  Ok, string properties can be passed via driver variables.  (personally don't recommend it) (note also for this case, the expression could simply be x("Lamp") with no need for a driver variable)  

Not setting path for the single property driver variable will result in "None" being printed to console.

Ok, now that we know a name can be passed, it's best to check for errors with something like,

if name is None:
    print("problem with name variable")
    return 0

obj = bpy.context.scene.objects.get(name)

if obj is None:
    print("no object named:", name)
    return 0


as errors will mark the driver as "bad" which can result it in being turned off.

A more usual way would be, where the x Euler rotation of the Lamp object, is passed as a float variable, "var", to the expression floor(2 * var / pi).



PS also of note is that rotation in blender is in radians.