0
$\begingroup$

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.

$\endgroup$

1 Answer 1

2
$\begingroup$

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 principled.inputs["Base Color"].is_linked:
        soc = principled.inputs["Base Color"].links[0].from_socket
        if soc.node.type == 'TEX_IMAGE' and soc.name == "Color":
            return soc.node

    return None


def insert_multiply_vcolor(mat, principled, tex):
    mul = mat.node_tree.nodes.new("ShaderNodeMixRGB")
    vcol = mat.node_tree.nodes.new("ShaderNodeVertexColor")

    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

    mat.node_tree.links.new(principled.inputs["Base Color"], mul.outputs[0])
    mat.node_tree.links.new(mul.inputs[1], tex.outputs[0])
    mat.node_tree.links.new(mul.inputs[2], vcol.outputs[0])


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)
$\endgroup$
1
  • $\begingroup$ Thanks! Worked perfectly. $\endgroup$
    – dvendab
    Oct 17, 2021 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.