Get inputs index of the node connected to the output of a given node, etc

Hello, have a few questions:

  1. Is there a way to get the inputs index of the node connected to the output of a given node? For example, if I have a normalMap node connected to Principled BSDF, I can get Principled BSDF by

    for mat in bpy.data.materials:
    for to_node_link in mat.node_tree.nodes[‘Normal Map’].outputs[0].links:
    to_node = to_node_link.to_node

But is there a way to find where’s the link connected to in Principled BSDF inputs[] ?

  1. What’s the equivalent python code for Shift+C, if it can be done without opening Blender?

Something like this perhaps? Or just .to_socket if you don’t need the index specifically.

link = bpy.data.materials['Material'].node_tree.nodes['Normal Map'].outputs[0].links[0]
i = int(link.to_socket.path_from_id()[:-1].split("[")[-1])
2 Likes