# Connect 2 nodes for all materials via python script

I want to connect 2 nodes for all materials at once. Specifically the vertex color node with the mix vertex color node

I want to turn this:

Into this:

For every material.

Any help would be appreciated, not very familliar with python at all.

John's answer seems to be absolutely correct for how you presented your question. However, if you'd rephrase your question as:

How do I change each Vertex Color output going to a node labelled "Mix Vertex Color" to use Alpha output instead of Color?

Then the best answer I can come up with would be:

import bpy

for mat in bpy.data.materials:
nodes = mat.node_tree.nodes if mat.node_tree else []
vc_nodes = (n for n in nodes if n.type == 'VERTEX_COLOR')
for vc, link in ((n, l) for n in vc_nodes for l in n.outputs['Color'].links):
if link.to_node.label == 'Mix Vertex Color':
print(f"Changed material {mat.name}")


This turns this:

into this:

That is, it iterates over all materials, and then makes sure to change each link from a node of type VERTEX_COLOR to a node with a label Mix Vertex Color, to go from the Alpha socket.

## Alternative code

import bpy

check = lambda x: x.from_node.type=='VERTEX_COLOR' and x.to_node.label=='Mix Vertex Color'
trees = (m.node_tree for m in bpy.data.materials if m.node_tree)

for tree, l in [(t, l) for t in trees for l in t.links if check(l)]:


you can use this script:

import bpy

for eachMat in bpy.data.materials:

if eachMat.node_tree is not None:
if eachMat.node_tree.nodes.find("Vertex Color"):
vertexColor = eachMat.node_tree.nodes["Vertex Color"]
if eachMat.node_tree.nodes.find("Mix"):
mix = eachMat.node_tree.nodes["Mix"]