Is it possible to find nodes connected to a node?

For example, could you trace your way back through a node tree from the material output node by finding the node connected to the material output node, then finding the node(s) connected to that node, then the nodes connected to those nodes, etc.?

How is this possible?


1 Answer 1


The python path to the links between each node is


So for each material we want to start at the Material Output node then loop through each array of inputs[] and links[] to move through the connections. Going this way (using from_node) there should only be one link for each input, but if you want to reverse the order (by using to_node) you can have multiple links for each output.

import bpy

def followLinks(node_in):
    for n_inputs in node_in.inputs:
        for node_links in n_inputs.links:
            print("going to " + node_links.from_node.name)

for mat in bpy.data.materials:
    print("Traversing " + mat.name)
    for mat_node in mat.node_tree.nodes:
        if mat_node.type == 'OUTPUT_MATERIAL':
            # we start at the material output node
            print("Starting at " + mat_node.name)
  • $\begingroup$ I have thousands of objects for which I need to update their materials' nodes/links using Blender's Python API. I am pretty new to nodes stuff in Cycles and I haven't been able to wrap my head around how I can do that. It seems that some version of your solution here should work for what I want to do. I wonder, could you please take a look at my question here? It would be great if you know how I can do what I want. $\endgroup$
    – Amir
    Commented Mar 8, 2018 at 16:39
  • $\begingroup$ Hi, could you show a code sample how to get the inputs index of connected node to the output of a given node? For example, if I have a normalMap node connected to Principled BSDF, I can get the Principled BSDF by mat.node_tree.nodes['Normal Map'].outputs[0].links[0].to_node, but is there a way to find it's connected to inputs[19] of Principled BSDF? $\endgroup$
    – June Wang
    Commented Jul 10, 2021 at 13:31
  • $\begingroup$ @JuneWang you would be better asking that as a new question $\endgroup$
    – sambler
    Commented Jul 13, 2021 at 6:10
  • 1
    $\begingroup$ Just in case it will help someone, this is also true for geometry nodes. $\endgroup$
    – ofekp
    Commented Feb 15, 2022 at 20:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .