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")
# insert driver to roughness driver = diffuse.inputs.driver_add("default_value") var = driver.driver.variables.new() var.name = "variable" var.targets.data_path = "PATH" var.targets.id = "Target_Object_Name" driver.driver.expression = "variable" # remove driver diffuse.inputs.driver_remove("default_value")
# insert key frame to roughness at frame 10 diffuse.inputs.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.default_value = (0,1,0,1) # green RGBA node_emission.inputs.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 links = mat.node_tree.links link = links.new(node_emission.outputs, node_output.inputs) # get specific link from_s = node_emission.outputs to_s = node_output.inputs link = next(l for l in links if l.from_socket == from_s and l.to_socket == to_s) # remove links links.remove(link)
As before hower over node atributes in UI to see the python paths
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.