0
$\begingroup$

I want to set up drivers: from Custom Properties of a Camera to Custom Properties of selected object. enter image description here

Based on a script from 6 year old post: How to put together a driver with python, I manage to have this code,

import bpy
def add_driver(
        source, target, prop, dataPath,
        index = -1, negative = False, func = ''
    ):
    ''' Add driver to source prop (at index), driven by target dataPath '''

    if index != -1:
        d = source.driver_add( prop, index ).driver
    else:
        d = source.driver_add( prop ).driver

    v = d.variables.new()
    v.name                 = prop
    v.targets[0].id        = target
    v.targets[0].data_path = dataPath

    d.expression = func + "(" + v.name + ")" if func else v.name
    d.expression = d.expression if not negative else "-1 * " + d.expression

#-------------------------------------------- 
ob = bpy.context.active_object
camera = bpy.context.scene.objects['Camera']

prop_name1 = ob.name + "_Opacity"
prop_name2 = ob.name + "_Highlight"


add_driver( ob, camera, prop_name1, prop_name1, -1)
add_driver( ob, camera, prop_name2, prop_name2, -1)

but executing it always give me error:

"Python: TypeError: bpy_struct.driver_add(): property "FirstCube_Opacity" not found

...even if I'm sure that all the objects have proper named Custom Properties. The function of setting up driver works great with other parameters in blender like 'scale'. What Am I doing wrong?

###UPDATE CODE####

Script1 - Creating Custom Properties

import bpy

ob = bpy.context.active_object

prop_name1 = ob.name + "_Opacity"
ob[prop_name1] = 0.0
ob.id_properties_ensure()  # Make sure the manager is updated
property_manager = ob.id_properties_ui(prop_name1)
property_manager.update(min=0.0, max=1.0, soft_min=0.0, soft_max=1.0)

camera = bpy.context.scene.objects['Camera']

prop_name2 = ob.name + "_Opacity"
camera[prop_name2] = 0.0
camera.id_properties_ensure()  # Make sure the manager is updated
property_manager = camera.id_properties_ui(prop_name2)
property_manager.update(min=0.0, max=1.0, soft_min=0.0, soft_max=1.0)

Script2 - Setting up driver, "Object_Opacity" CAMERA's Custom Property should drive "Object_Opacity" Object's Custom Property

import bpy
def add_driver(
        source, target, prop, dataPath,
        index = -1, negative = False, func = ''
    ):
    ''' Add driver to source prop (at index), driven by target dataPath '''

    if index != -1:
        d = source.driver_add( prop, index ).driver
    else:
        d = source.driver_add( prop ).driver

    v = d.variables.new()
    v.name                 = prop
    v.targets[0].id        = target
    v.targets[0].data_path = dataPath

    d.expression = func + "(" + v.name + ")" if func else v.name
    d.expression = d.expression if not negative else "-1 * " + d.expression
ob = bpy.context.active_object
camera = bpy.context.scene.objects['Camera']

prop_name1 = ob.name + "_Opacity"


add_driver( ob, camera, f'["{prop_name1}"]', f'["{prop_name1}"]', -1)
$\endgroup$
3
  • 1
    $\begingroup$ I think you want to enclose your property name with brackets, since it's a custom property that's how they are accessed : add_driver( ob, camera, "prop", f'["{prop_name1}"]', -1) $\endgroup$
    – Gorgious
    Apr 28, 2022 at 8:49
  • $\begingroup$ Thank You! Small succes! Driver is assigned, but it doesn't work :(. The area of value beeing driven is purple, but it's not beeing changing by the Camera's Custom Property. screen: imgur.com/a/sjQQNVW $\endgroup$
    – evilferber
    Apr 28, 2022 at 9:08
  • $\begingroup$ Yeah take notice of the snippet I posted, I replaced the prop name (3rd parameter) to "prop" because a variable name can't have hyphens or brackets in them $\endgroup$
    – Gorgious
    Apr 28, 2022 at 9:14

1 Answer 1

0
$\begingroup$

Replace add_driver(ob, camera, prop_name1, prop_name1, -1)

With add_driver(ob, camera, "prop", f'["{prop_name1}"]', -1)

Custom property are accessed with square brackets and double quotes, when regular properties are accessed directly.

In python variable names can't contain brackets nor quotes so I suggest you use "prop" string in this case which will create a variable named prop.

$\endgroup$
8
  • $\begingroup$ In what blender version do you have it running? I tried in 3.0.0 and in 3.2.0 Alpha, and I always get: "Python: TypeError: bpy_struct.driver_add(): property "prop" not found" $\endgroup$
    – evilferber
    Apr 28, 2022 at 10:54
  • $\begingroup$ You need to have the custom property on both the source and target (by the way, in my opinion the two variable names should be swapped inside the function) $\endgroup$
    – Gorgious
    Apr 28, 2022 at 12:40
  • $\begingroup$ But I have Custom Properties... I did everything ok imo. It looks like just setting up driver (even manually) with made by script Custom Properties is buggy. I recorded video for you, Can You say if You think so too? Maybe it's supposed to be reported as a bug. youtube.com/watch?v=wVFAE1TyckU $\endgroup$
    – evilferber
    Apr 28, 2022 at 13:34
  • $\begingroup$ I believe it has to do with the driver not being notified that the value was updated. Could you post your updated code too ? $\endgroup$
    – Gorgious
    Apr 28, 2022 at 13:37
  • $\begingroup$ The file: drive.google.com/file/d/1B4tpvHLj4Cms7MRyd7ZjLirCKE0n3S42 First script creates Custom Properties. Second should assign driver, but it never worked (I tried different parameters on the fly). Thank you very much for investigating it. $\endgroup$
    – evilferber
    Apr 28, 2022 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .