# Creation of Node Setup with python, missing some commands

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

The target material should look like this:

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 datapath to the values I listed before

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.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


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'

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:


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").

• 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 – Clss Mar 25 '19 at 21:43