# Script working on an object but not on another

I downloaded a blend file from another user here with a script (for drivers) and a mesh + armature. On the driver of said mesh it says Error:invalid Python expression. I couldn't get it to work for so long although it worked for the user that sent it to me. However after a while I figured out a really odd fix.The fix is to move the armature then ctrl+z after I do that, the driver no longer shows an error instead it shows slow python expression which is fine as long as it works I guess.

Now I tried applying the script to a personal mesh+armature I copied the same exact drivers+variable. I get the Error:invalid Python expression. I tried doing the same odd fix that worked for some reason on the other mesh+armature but it didn't work in this case. Here's the script

import bpy
from mathutils import Vector

def left(self, z_rotation, speed_low, speed_high):
prev = self["prev_z"]
self["prev_z"] = z_rotation
delta = z_rotation - prev
if delta > 0:
self["z"] -= delta * speed_low
elif delta < 0:
self["z"] -= delta * speed_high

return self["z"]

def right(self, z_rotation, speed_low, speed_high):
prev = self["prev_z"]
self["prev_z"] = z_rotation
delta = z_rotation - prev
if delta < 0:
self["z"] += delta * speed_low
elif delta > 0:
self["z"] += delta * speed_high

return self["z"]

bpy.app.driver_namespace["left"] = left
bpy.app.driver_namespace["right"] = right


Here's the driver of the mesh I downloaded from the other user.

And this is the driver I applied to my own mesh(the exact same thing)

I have no clue why this is happening the issue is just really weird and very frustrating. Help would be immensely appreciated.

• See blender.stackexchange.com/questions/165093/use-self-doesnt-work Make sure auto run is set, you have run the script that registers to the driver namespace. Look for errors in the console. It appears the driver code requires the object with the driver to have the "prev_z" and "z" custom property. If not the first line prev = self["prev_z"] will throw an error. blender.stackexchange.com/questions/6173/… Oct 6 '20 at 8:17
• yep I didn't add custom properties adding it fixed it. Post it as an answer so I can accept it Oct 6 '20 at 18:14

Custom properties not set

The driver script as is will throw an error if the custom properties "z" and "prev_z" are not set on the object.

Look for errors in the console.

Where does console output go

Add the appropriate custom properties to the object being driven for it to function as expected.

Note.

The object referred to as self in a driver is an evaluated copy of the object.

Silly test script to ensure the original object has a custom property "foo" set from within the driver.

Returns the evaluated value of the custom property, if it exists, else 0.0.

from bpy.app import driver_namespace as dns

def test(ob):
ob.original["foo"] = ob.original.get("foo", 0.0)
return ob.get("foo", 0.0)

dns["test"] = test


copy and paste this driver into any object to create a "foo" custom property if one does not already exist.