# Control Cycles material nodes and material properties in Python

How can I define the different properties of an Cycles material? Such things as glossy, glass, diffuse, RGB-color, emissive via Python scripting?

Here is how to change material properties, you can see all of them here or see the python path when you hover over in the UI on some material attribute:

    import bpy

# get the material
mat = bpy.data.materials['Material_Name']
# change its parameters - example viewport diffuse color:
mat.diffuse_color = (1,0,0)


Here is how to manipulate material nodes and thus the material look itself:

• Get the nodes:

import bpy

# get the material
mat = bpy.data.materials['Material_Name']
# get the nodes
nodes = mat.node_tree.nodes

# get some specific node:
# returns None if the node does not exist
diffuse = nodes.get("Diffuse BSDF")

• Create driver:

# insert driver to roughness
var = driver.driver.variables.new()
var.name = "variable"
var.targets[0].data_path = "PATH"
var.targets[0].id = "Target_Object_Name"
driver.driver.expression = "variable"

# remove driver
diffuse.inputs[1].driver_remove("default_value")

• Keyframes:

# insert key frame to roughness at frame 10
diffuse.inputs[1].keyframe_insert("default_value", frame=10)

• Create nodes, here are all the node types you can create (look for subclasses):

# remove specific node
nodes.remove(diffuse)

# clear all nodes to start clean
nodes.clear()

# create emission node
node_emission.inputs[0].default_value = (0,1,0,1)  # green RGBA
node_emission.inputs[1].default_value = 5.0 # strength
node_emission.location = 0,0

# create output node
node_output.location = 400,0


# link nodes

from_s = node_emission.outputs[0]
to_s = node_output.inputs[0]
link = next(l for l in links if l.from_socket == from_s and l.to_socket == to_s)



As before hower over node atributes in UI to see the python paths

• I have thousands of objects for which I need to update their materials' nodes/links using Blender's Python API. I am pretty new to nodes stuff in Cycles and I haven't been able to wrap my head around how I can do that. I wonder, could you please take a look at my question here and see if you can post a solution? – Amir Mar 8 '18 at 16:30
• @Amir You need to know the names of the nodes you need to update. I added info how to get specific links to edit them. Now you should be able to put it together. You can try this on some easy example with just 2 nodes, then on your example. If you encounter something that doesn't work or you get stuck, let me know, I will help you further with what that is. – Jaroslav Jerryno Novotny Mar 8 '18 at 19:32
• Thank you for your comment. I posted an answer to my question – Amir Mar 9 '18 at 0:30
• Instead of node_emission.inputs[0] and node_emission.inputs[1], one can also write node_emission.inputs["Color"] and node_emission.inputs["Strength"], which could improve clarity. – jakob.j Dec 26 '19 at 13:01

If you right-click on any value in Blender, there's an option called 'Copy Data Path'. If you select this and paste the result you will get the Python command to change the value.

Here is a repo that does exactly what you want. It has a ready-to-run python script that can be run from the command line in the background without opening the blender GUI. It creates a water material on a surface mesh for a convecting bubble - this is done with 2 different shaders and mixing them using a mixer shader. The script loops through the different files and renders the images as jpeg. The images are also there in the repo. Just clone the repo and run the command given in the README file in the terminal. This scripting was done with Blender v2.78.

https://github.com/nataraj2/BlenderScripting