Take the 2-minute tour ×
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It's 100% free, no registration required.

I'm trying to add a driver to a Cycles Value node using Python. So far my code is:

node = material.node_tree.nodes.new("ShaderNodeValue")
node.driver_add("default_value")

However, this just gives me a 'property "default_value" not found' error. I've also tried:

node.driver_add("value")
node.driver_add("outputs", 0)
node.driver_add("outputs[0].default_value")
node.driver_add("nodes['Value'].outputs[0].default_value")

None of these work either. So my question is: how do I exactly add a driver to a Cycles Value node? It can be done via the UI right-click menu, but I can't find a data path that driver_add() will accept.

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

You need to add the driver on the output, not the node directly:

fcurve = node.outputs[0].driver_new("default_value")

or

fcurve = node.outputs['Value'].driver_new("default_value")

The driver expression is accessed like:

fcurve.driver.expression
share|improve this answer
    
Thanks! Still finding my around Blender's API so I appreciate the guidance. –  Dobz Mar 1 at 11:18
add comment

I would suggest doing it with the GUI first and then examine the data structure.

I.e. added a driver to the Composite Node
GUI driver

The path is relative to node_tree.

So try to add the driver to node_tree with the given data path

import bpy

#right click on the socket value of the node -> Copy Data Path
path = 'nodes["Composite"].inputs[1].default_value'

scene = bpy.context.scene
scene.use_nodes = True
driver = scene.node_tree.driver_add(path)

Copy Data Path

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.