Create material - how to connect nodes?

Hi all,

Trying to create a material with pyhon and I can’t seem to connect the nodes as I want. Here’s my code:

new_mat = bpy.data.materials.new(name = "code_mat")
new_mat.use_nodes = True
new_mat.node_tree.nodes.new(type="ShaderNodeMix")
new_mat.node_tree.nodes["Mix"].data_type='RGBA'
new_mat.node_tree.nodes["Mix"].location[0] = -200
new_mat.node_tree.nodes["Mix"].location[1] = 200
new_mat.node_tree.links.new(new_mat.node_tree.nodes["Mix"].outputs['Result'], new_mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'])

Basically trying to connect the result of a mix shader node to the principled BSDF base color input.

I did look this up and found this thread but I can’t see where I am going wrong…

Thanks for any help,
Randy

Well, I’ve been working on this problem, tracking down why my code isn’t working, and this is what I’ve found…

This line of code is what is causing it to fail:

new_mat.node_tree.nodes["Mix"].data_type='RGBA'

If I comment out that line, the code works. It creates a new material, adds in a mix shader, positions the node, and connects it’s output to the input of the Principled BSDF. Also, the last line of code above, that makes the link, has the inputs & outputs reversed.

Here’s my current code that almost works:

new_mat = bpy.data.materials.new(name = "code_mat")
new_mat.use_nodes = True
new_mat.node_tree.nodes.new(type="ShaderNodeMix")

new_mat.node_tree.nodes["Mix"].location[0] = -200
new_mat.node_tree.nodes["Mix"].location[1] = 200

new_mat.node_tree.links.new(new_mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'], new_mat.node_tree.nodes["Mix"].outputs['Result'])

I say this almost works because it creates the material, adds in the mix shader and connects it correctly, but because I omitted the line that changes the shader type, it’s the wrong type of mix shader.

I did try changing to code to using:

bpy.ops.node.add_node(type="ShaderNodeMix", settings=[{"name":"data_type", "value":"'RGBA'"}])

To add in the correct type of shader, but I get a ‘context is incorrect’ error.

So how do I add a color mix shader to my material and connect it up?

Thanks for any insights,
Randy

I think the problem here is that the mix node actually has multiple outputs all called "Result", which are used for the different socket types (float, color, vector etc.). When you select one using the "Result" key, you’re actually just selecting the float output (a.k.a the first one), so when you connect it, with the mode set to color, it doesn’t show up, since that float socket is hidden.

Instead, you probably want to get the socket by index, e.g. mix_node.inputs[2], which will let you select any of the output sockets. I did some testing, and the color output is at index 2 (you can just go through the sockets in the python console, and check output.type to see what type it is)

So to summarise; create the link like this:

node_tree.links.new(mix_node.outputs[2], principled_node.inputs['Base Color'])

Also, you got the order right in the first post, it goes links.new(from_output, to_input).
And finally, subjectively, I’d recommend using variables to store references to the node_tree, individual nodes etc. rather than getting them each line with a long convoluted path, as it will make the code a lot more readable.

Hope that helps!

@Strike_Digital Thank you so much!!!

Indeed, it was the fact I was using the wrong output socket. It would have taken me a long time to figure this out if it wasn’t for your help.

As to my coding style…

I know it’s not the python way. Before this, I spent a lot of time with microcontrollers and C/C++ where you need to be more specific. Guess I have a C hangover…

Again, thank you for your help!
Randy

1 Like