How can I define the different properties of an Cycles/Eevee material? Such things as glossy, glass, diffuse, RGB-color, emissive via python scripting?
3 Answers
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 mat.use_nodes = True 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 driver = diffuse.inputs[1].driver_add("default_value") 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 = nodes.new(type='ShaderNodeEmission') 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 = nodes.new(type='ShaderNodeOutputMaterial') node_output.location = 400,0
Link nodes:
# link nodes links = mat.node_tree.links link = links.new(node_emission.outputs[0], node_output.inputs[0]) # get specific link 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), None) # remove links links.remove(link)
As before hower over node atributes in UI to see the python paths
-
$\begingroup$ 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? $\endgroup$– AmirCommented Mar 8, 2018 at 16:30
-
1$\begingroup$ @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. $\endgroup$ Commented Mar 8, 2018 at 19:32
-
$\begingroup$ Thank you for your comment. I posted an answer to my question $\endgroup$– AmirCommented Mar 9, 2018 at 0:30
-
$\begingroup$ Instead of
node_emission.inputs[0]
andnode_emission.inputs[1]
, one can also writenode_emission.inputs["Color"]
andnode_emission.inputs["Strength"]
, which could improve clarity. $\endgroup$– jakob.jCommented Dec 26, 2019 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.