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 :
You can add a single property variable with:
myDriver.variables.name = 'myVariable'
then access menus below with:
myDriver.variables.targets.id = bpy.data.objects['myArmature']
myDriver.variables.targets.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:
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)"