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:

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

        # Set node values.
        bevel_node.inputs[0].default_value = 0.001
        image_node.image =[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[0], vector_math_node.inputs[0])[1], vector_math_node.inputs[1])[0], math_node.inputs[0])[0], invert_node.inputs[1])[0], emission_node.inputs[0])[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).[to_socket], from_node.outputs[from_socket])

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[0], emission_node.outputs[0])[0], invert_node.outputs[0])[1], math_node.outputs[0])

        # This node never links to math node.
        vector_math_node.operation = 'DOT_PRODUCT'
        vector_math_node.socket_value_update(context)[1], vector_math_node.outputs[0])
[1], geometry_node.outputs[1])[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'[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!