# Connect 2 nodes to Mix RGB node for all materials via python script

I want to connect an image texture node and a vertex color node to a Mix RGB node (with Multiply as blending mode and Fac set to 1), which then connects to the Base Color of my Principled BSDF node - for all materials at once.

Basically I want to turn this:

Into this:

For every material at once.

My main goal here is to mix every image texture with my vertex color basically. Connecting them manually would be way too much work as my object has a ton of materials.

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

Try this

import bpy

# Name of your vertex color layer, change it if you need to
VERTEX_COLOR_NAME = "bakedVertexColor"

def get_texture_node(principled):
if soc.node.type == 'TEX_IMAGE' and soc.name == "Color":
return soc.node

return None

def insert_multiply_vcolor(mat, principled, tex):

mul.location = (
# Cenetered on the horizontal
(tex.location[0] + 240 + principled.location[0])/2 - 70,
tex.location[1] - 100
)
vcol.location = (tex.location[0] + 100, tex.location[1] - 260)

mul.blend_type = 'MULTIPLY'
mul.inputs[0].default_value = 1.0  # Fac
vcol.layer_name = VERTEX_COLOR_NAME

for mat in bpy.data.materials:
if not mat.use_nodes: continue

principled = mat.node_tree.nodes.get("Principled BSDF")
if not principled: continue

tex = get_texture_node(principled)
if not tex: continue

insert_multiply_vcolor(mat, principled, tex)

• Thanks! Worked perfectly. Oct 17 '21 at 13:35