8
$\begingroup$

Having a node, I want to unlink it. My current approach is to clear all links from a desired NodeSocket:

my_node.inputs[0].links.clear()

I get a:

AttributeError: 'tuple' object has no attribute 'clear'

I would think that 'links' is a NodeLinks structure, which would have 'clear()' and 'remove()', but apparently no? I also can not simply del my_node.inputs[0].links[0].

One approach I got working is:

for l in my_node.inputs[0].links:
     shader_node_tree_links.remove(l)

but is there a simpler way? Should one of my previous attempts work?

$\endgroup$
10
$\begingroup$

The my_node.inputs[0] is a node Socket type, and the my_node.inputs[0].links is a read only list of links from this socket, so your first attempt won't work, and you have found the correct way, which is deleting the link from node_tree.links as follows:

 # remove only the desired link
 l = my_node.outputs[0].links[0]
 node_tree.links.remove(l)

To find a link:

output = my_node1.outputs[0]
input  = my_node2.inputs[0]

for l in output.links :
    if l.to_socket == input :
       node_tree.remove(l)

Also you can check for the connected node: if l.to_node == my_node2:

See the DOC page for more details.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.