Data path from custom property to driver variable?

I create custom Properties for a bone with:

pbone = bpy.context.active_pose_bone
pbone['Switcher'] = 1 

And I create drivers in other objects which should be controlled by this custom property. I’ve tried for hours now to get the Data path from this value to the Variable of the Driver…

The driver is created like this:

driver = f.driver_add("hide").driver
        driver.expression = myvariable

I think I need to set the Prop and Path with driver.variables = xx but I can’t figure out how to get that data from the property to the driver. In UI I can simply Copy Data Path but not in python…

How do I do it!?

Hi,
First thank you so much for showing me that you could create a new custom property just by setting : myObj[‘myProp’] = someValue. I was looking for a way to change the property name without success.

So, you access the driver with :

myObj.animation_data.drivers[i].driver

You can add a single property variable with:

myDriver.variables.new()
myDriver.variables[0].name = 'myVariable'
myDriver.variables[0].type= 'SINGLE_PROP'

then access menus below with:

myDriver.variables[0].targets[0].id = bpy.data.objects['myArmature']
myDriver.variables[0].targets[0].data_path = pose.bones["myBone"]["myProp"]

You can finally set the expression as you said:

myDriver.expression = "myVariable"

If you want the expression to be more complex, like if statements, you can use one of the functions available in bpy.app.driver_namespace, like pi or cos.
Or you can define your own function in the text editor:

import bpy

def myfunction(arg1):
    #something 

bpy.app.driver_namespace['myFunctionName'] = myFunction

Make sure your script has a .py extension and check the “Register” button, so it loads each time you open your scene.
You access the function by using its driver_namespace:

myDriver.expression = "myFunctionName(someArg)"