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 bpyget 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,0Link 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
- 51,077
- 7
- 129
- 218
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.
- 417
- 3
- 9
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.
node_emission.inputs[0]andnode_emission.inputs[1], one can also writenode_emission.inputs["Color"]andnode_emission.inputs["Strength"], which could improve clarity. – jakob.j Dec 26 '19 at 13:01