0
$\begingroup$

I would like to creat a material with python but I'm missing some commands.

The target material should look like this:enter image description here

The script I'm writing should be able to access the two values in front of the math node, and the RGB values in the RGB node.

The command(s) I'm missing are the following:

the command to add a node in the material / node editor

the command to remove a node in the material / node editor

the command to link link an imput and an output

the datapath to the values I listed before

Thanks in advance

AliSot2000


Edit

I've just found another problem: I understand now how to add, remove and link nodes but I don't now how to 'call' them.

In this setup I have two 'Value-Nodes'. How can I refere to those? I'mean ShaderNodeValue won't do because I have two of them.

My current script looks like this:

#Test script for material

bpy.context.scene.render.engine = 'CYCLES'

import bpy
import math

bpy.ops.mesh.primitive_cube_add()

bpy.data.materials.new(name='Testmaterial')
bpy.context.active_object.data.materials.append(bpy.data.materials['Testmaterial']) 
#So I can access it via .active_material

bpy.context.object.active_material.use_nodes = True

Now I would like to remove the Diffuse BSDF, add the MixShader and then link the shader output with the shader imput from Material Output.

the process of adding should be:

bpy.context.object.active_material.node_tree.nodes.new('ShaderNodeMixShader')

I think if I get how to do this the other commands should just be 'copy and paste'

$\endgroup$

1 Answer 1

1
$\begingroup$

Cycles material nodes are accessible through the node_tree attribute of the material.

Example: get the node tree of the active material on the active object:

node_tree = bpy.context.object.active_material.node_tree

Add a node in the material / node editor

You have to know the bl_idname (a string) of the node you want to add. Then you can do:

node = node_tree.nodes.new(bl_idname)
# Example, add a "Diffuse BSDF" node:
node = node_tree.nodes.new("ShaderNodeBsdfDiffuse")

I think you can find all bl_idnames of Cycles nodes here: https://docs.blender.org/api/2.79/bpy.types.ShaderNode.html#bpy.types.ShaderNode

Remove a node in the material / node editor

# node is of type bpy.types.Node
node_tree.nodes.remove(node)

Link an input and an output

Assuming you have two nodes left_node and right_node. output and input can be indices or strings. For example, on a Diffuse BSDF node you can access the output by node.outputs["BSDF"] or by node.outputs[0].

node_tree.links.new(left_node.outputs[output], right_node.inputs[input])

Since you edited your question: You should read the Blender API docs I linked above. Cycles shader nodes inherit from bpy.types.Node. You can get a specific node if you know its name: https://docs.blender.org/api/2.79/bpy.types.Node.html#bpy.types.Node.name

If you add the nodes via script, you already have a reference to the new node returned by the add() method (see above point "add a node").

$\endgroup$
1
  • $\begingroup$ node_tree.links.new(left_node.outputs[output], right_node.inputs[input]) Would it be any different in 2.80?.. It doesn't work. Thanks $\endgroup$
    – Clss
    Commented Mar 25, 2019 at 21:43

You must log in to answer this question.

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