# How to check if Node connected to another node input in Python

I am doing a process to some switch links in materials. I need to know if something is ever linked or not before to process. I can't find out how to :/ The script works well if something is linked only.

import bpy
for m in bpy.context.object.material_slots:
mat = m.material
nodes = mat.node_tree.nodes

# get node input (socket)
alphasocket = mat.node_tree.nodes.get("Principled BSDF").inputs[18]
# get node link that connects into the socket

emi.name= "emibak"
outepute= mat.node_tree.nodes.get("Material Output").inputs[0]

#here 's where I cant find out how to check
if lnk != None:
print(mat.name + " alphanod found")
else :
print(mat.name + " alphanod missing")



here is another way I am searching...

import bpy

for m in bpy.context.object.material_slots:

mat = m.material
nodes = mat.node_tree.nodes
print("**** Material " + mat.name+ " ****")

for mat_node in mat.node_tree.nodes:
if mat_node.type == 'BSDF_PRINCIPLED':
print("From " + mat_node.name )
alphasocket = mat_node.inputs[18]
#print(alphasocket)
node_in = mat_node.inputs
#print(node_in)

print("Alpha connected to " + n.from_node.name)


This show me what is connected to the alpha channel but if nothing is connected I want to connect a new node.

• Hi. Firstly, I suggest removing anything from your script that isn't relevant to the question you are asking (I'm not sure if the code related to adding an emission node is relevant here). Secondly, when you say "I need to know if something is ever linked", what do you mean by "something"? – Ray Mairlot May 23 '19 at 16:02
• I post the entire (small) code so you can have a better view. What I want is to test if any node is connected to the alpha channel of the Principled Shader. If not, I would create a White RGB node and connect it. – Loranozor Graphitz May 23 '19 at 21:27

What I want is to test if any node is connected to the alpha channel of the Principled Shader. If not, I would create a White RGB node and connect it.

import bpy
context = bpy.context
ob = context.object
# all node based materials on object
mats = set(s.material for s in ob.material_slots
if s.material and s.material.use_nodes)
for m in mats:
nodes = m.node_tree.nodes
bsdfnodes = [n for n in nodes