0
$\begingroup$

I'm trying to add and configure a driver through a script. so far I have the following script:

import bpy
bpy.data.materials.new("Property Dump")
bpy.data.materials["Property Dump"].use_nodes=True
prin=bpy.data.materials["Property Dump"].node_tree.nodes['Principled BSDF']
outputnode=bpy.data.materials["Property Dump"].node_tree.nodes['Material Output']
bpy.data.materials["Property Dump"].node_tree.nodes.remove(prin)
bpy.data.materials["Property Dump"].node_tree.nodes.remove(outputnode)
test=bpy.data.materials["Property Dump"].node_tree.nodes.new(type="ShaderNodeMix")
test.location=(100, 100)


bpy.data.materials["Property Dump"].node_tree.nodes["Mix"].inputs[0].driver_add("default_value")

This makes a new material called Property Dump, deletes the default nodes, adds a mix node, and gives a default value driver to the first input of the mix node. What I want to know is how to give the driver an expression, add an input variable, make it single property, etc. How do I do it?

$\endgroup$

1 Answer 1

2
$\begingroup$

You can find information about Drivers in Blender's Official Documentation and the Driver Panel. You can configure a fully functional driver for your chosen field bpy.data.materials["Property Dump"].node_tree.nodes["Mix"].inputs[0] in the following script which is driven in this example by the scale[0] property (which is Scale X) of the bpy.data.objects["Cube"] object, but you can choose any object or property of your interest:

enter image description here

When you create a driver using the method add_driver, it returns an FCurve with a property that contains the driver. You can set the driver Driver Type with values AVERAGE, SUM, MIN, MAX, & SCRIPTED and give it any Python Expression that uses Driver Variables created with the method variables.new() which returns a DriverVariable object that can be setup with a name.

Then you have to configure the driver itself which is in our example the object bpy.data.objects["Cube"] by setting the values in the variable's DriverTargets list. In this case we only have 1 driver target so we retrieve the first element with v.targets[0] and set the id_type to OBJECT and the id to bpy.data.objects["Cube"]. But you can have a driver of any type of ID-block Id Type. Then specify the data_path to use which can be obtained from any property or field in the Blender interface by right clicking any field of interest and selecting Copy Data Path. Then just paste the copied Data Path in your script.

fcurve = bpy.data.materials["Property Dump"].node_tree.nodes["Mix"].inputs[0].driver_add("default_value")

d = fcurve.driver
d.type = "SCRIPTED"
d.expression = "my_var + 0.1"

v = d.variables.new()
v.name = "my_var"

t = v.targets[0]
t.id_type = 'OBJECT'
t.id = bpy.data.objects["Cube"]
t.data_path = "scale[0]"

Try this entire code:

import bpy

bpy.data.materials.new("Property Dump")
bpy.data.materials["Property Dump"].use_nodes=True
prin=bpy.data.materials["Property Dump"].node_tree.nodes['Principled BSDF']
outputnode=bpy.data.materials["Property Dump"].node_tree.nodes['Material Output']
bpy.data.materials["Property Dump"].node_tree.nodes.remove(prin)
bpy.data.materials["Property Dump"].node_tree.nodes.remove(outputnode)
test=bpy.data.materials["Property Dump"].node_tree.nodes.new(type="ShaderNodeMix")
test.location=(100, 100)

fcurve = bpy.data.materials["Property Dump"].node_tree.nodes["Mix"].inputs[0].driver_add("default_value")

d = fcurve.driver
d.type = "SCRIPTED"
d.expression = "my_var + 0.1"

v = d.variables.new()
v.name = "my_var"

t = v.targets[0]
t.id_type = 'OBJECT'
t.id = bpy.data.objects["Cube"]
t.data_path = "scale[0]"
$\endgroup$
5
  • $\begingroup$ still nothing. the documentation says its still called "SCRIPTED" as of 3.4.1 $\endgroup$
    – desperrrr
    Dec 24, 2022 at 3:55
  • $\begingroup$ removing the line gives me the same error for the next line. i think it might be something with another part of my code $\endgroup$
    – desperrrr
    Dec 24, 2022 at 3:56
  • $\begingroup$ tried it with 3.1.2 same issue $\endgroup$
    – desperrrr
    Dec 24, 2022 at 4:01
  • $\begingroup$ my script has a lot more code than what i originally sent. ill update the post to include the entire thing which is most likely the issue $\endgroup$
    – desperrrr
    Dec 24, 2022 at 4:06
  • $\begingroup$ oh alright. i usually limit myself to one question per day as to prevent spamming $\endgroup$
    – desperrrr
    Dec 24, 2022 at 4:20

You must log in to answer this question.

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