Material Nodes Not Connecting Properly

When linking a vector math node and a math node together, using the code below, the nodes are never linked.

        # Add nodes.
        nodes = bake_material.node_tree.nodes

        bsdf_node = nodes.get("Principled BSDF")
        if bsdf_node != None:
            nodes.remove(bsdf_node)

        material_output_node = nodes.get("Material Output")
        image_node = nodes.new(type='ShaderNodeTexImage')
        emission_node = nodes.new(type='ShaderNodeEmission')
        vector_math_node = nodes.new(type='ShaderNodeVectorMath')
        math_node = nodes.new(type='ShaderNodeMath')
        geometry_node = nodes.new(type='ShaderNodeNewGeometry')
        invert_node = nodes.new(type='ShaderNodeInvert')
        bevel_node = nodes.new(type='ShaderNodeBevel')

        # Set node values.
        bevel_node.inputs[0].default_value = 0.001
        image_node.image = bpy.data.images[bake_image_name]
        math_node.operation = 'MULTIPLY'
        math_node.inputs[1].default_value = 1.0
        vector_math_node.operation = 'DOT_PRODUCT'

        # Link Nodes
        links = bake_material.node_tree.links
        links.new(bevel_node.outputs[0], vector_math_node.inputs[0])
        links.new(geometry_node.outputs[1], vector_math_node.inputs[1])
        links.new(vector_math_node.outputs[0], math_node.inputs[0])
        links.new(math_node.outputs[0], invert_node.inputs[1])
        links.new(invert_node.outputs[0], emission_node.inputs[0])
        links.new(emission_node.outputs[0], material_output_node.inputs[0])

your inputs/outputs are reversed (unless you’re trying to connect the vector_math to the math node? maybe you can describe what the desired node tree should look like).

links.new(to_node.inputs[to_socket], from_node.outputs[from_socket])

https://docs.blender.org/api/current/bpy.types.NodeLinks.html?highlight=links%20new#bpy.types.NodeLinks.new

I reversed the inputs and outputs to match the API, this didn’t make any differences unfortunately.

Only the vectormath node doesn’t link to the math node.

I believe this is because the output socket is changing from vector to a float socket when the operation is changed.

        links = bake_material.node_tree.links

        links.new(material_output_node.inputs[0], emission_node.outputs[0])
        links.new(emission_node.inputs[0], invert_node.outputs[0])
        links.new(invert_node.inputs[1], math_node.outputs[0])

        # This node never links to math node.
        vector_math_node.operation = 'DOT_PRODUCT'
        vector_math_node.update()
        vector_math_node.socket_value_update(context)
        links.new(math_node.inputs[1], vector_math_node.outputs[0])


        links.new(vector_math_node.inputs[1], geometry_node.outputs[1])
        links.new(vector_math_node.inputs[0], bevel_node.outputs[0])

I thought the socket value or node wasn’t updating so I’ve been trying to force the node to update using update() and socket_value_update().

Neither of these seem to have any effect, as I believe this are actually getting called automatically anyways.

SOLUTION: I found that connecting the second (outputs[1]) vector math node output works, which is very strange because the vector math node doesn’t visually have a second output node.

        vector_math_node.operation = 'DOT_PRODUCT'
        links.new(math_node.inputs[1], vector_math_node.outputs[1])

FYI: you can actually access socket names by their dictionary name to avoid this problem in the future.

ie) inputs['Color'], outputs['Value'] etc

1 Like

Ahh, that’s very helpful. Thank you!