How to remove particular Nodes in Shader by checking is it linked or not

FYI: this is all off the top of my head and not actual tested code.

Basically, you just need to loop through links and build a list of ‘connected nodes’

used_nodes = []
for link in node_tree.links:
    if link.to_node:
       used_nodes.append(link.to_node)
    if link.from_node:
       used_nodes.append(link.from_node)

then loop through all of your nodes and look for any that weren’t linked to something else

for n in node_tree.nodes:
    if n not in used_nodes:
        node_tree.nodes.remove(n)

Also note that this will assume that any node connected to anything is something you want to keep. So if you have two random nodes connected together but separate from the rest of the node tree they will not be removed. If you want that level of accuracy you’ll need to start by finding the material output node and then recursively backtrace all of the nodes that are connected in some way to it. Not hard to do, but more complicated than just removing individual isolated nodes.

2 Likes

Thank you, it helps.