2
$\begingroup$

Is it possible to add a driver to a property in a shader using Python?

I managed to get driver_add working for other properties, but not shader properties. I can also print the property just fine.

selected_node = bpy.context.active_node
selected_node.driver_add("inputs[1].default_value", 0)

Do I need to format it differently for it to work?

The error message says

property "inputs[1].default_value" not found

The property itself is an input field of a Node Group I made by the way.

$\endgroup$
6
  • $\begingroup$ mat.node_tree.driver_add('nodes["My Node Name"].inputs[1].default_value', 0) $\endgroup$
    – scurest
    Feb 22 at 19:46
  • $\begingroup$ @scurest have you tested it? Doesn't work for me: i.imgur.com/Zs8ROCi.png $\endgroup$ Feb 22 at 19:49
  • 1
    $\begingroup$ @MarkusvonBroady Quoted strings in fcurve data paths are "double quoted". $\endgroup$
    – scurest
    Feb 22 at 20:13
  • $\begingroup$ @scurest I fall for this a 2nd time... Thanks :D But still, have you tried it? I don't get an error after fixing the quotes but the field still doesn't become purple. $\endgroup$ Feb 22 at 20:51
  • $\begingroup$ @MarkusvonBroady Yes, I have tried it. The field isn't purple because it isn't the "canonical" path to that datum, because you used a socket name instead of a number. Starting from a blank file, you can manually add a driver to a socket in the UI, then do D.materials[X].node_tree.animation_data.drivers[0].dath_path to see what data path Blender actually creates. $\endgroup$
    – scurest
    Feb 22 at 21:37

1 Answer 1

4
$\begingroup$

Don't add a driver to the node, add it to the input:

D.materials['Material'].node_tree.nodes['Principled BSDF'].inputs['Roughness'].driver_add('default_value')

D is an alias in console for bpy.data

$\endgroup$

You must log in to answer this question.

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