Results 1 to 2 of 2
  1. #1

    Data path from custom property to driver variable?

    I create custom Properties for a bone with:
    Code:
    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:
    Code:
    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!?
    Last edited by animationista; 26-Jun-17 at 02:46. Reason: typo



  2. #2

    DriverVariable and DriverTarget

    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 :
    Code:
    myObj.animation_data.drivers[i].driver
    You can add a single property variable with:
    Code:
    myDriver.variables.new()
    myDriver.variables[0].name = 'myVariable'
    myDriver.variables[0].type= 'SINGLE_PROP'
    then access menus below with:
    Code:
    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:
    Code:
    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:
    Code:
    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:

    Code:
    myDriver.expression = "myFunctionName(someArg)"



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •