Detach node links by node name?

I’m making a script that I want to attach or detach links between a texture node’s output and a shader node. I have it working to make links between them using material.node_tree.links.new(shader.inputs[0], diffuse_tex.outputs[0]). But I cannot figure out how to detach them or otherwise cut the link. Is there some other material.node_tree.links thing I can use?

Sure: https://docs.blender.org/api/current/bpy.types.NodeLinks.html#bpy.types.NodeLinks.remove

link = node_tree.nodes['Material Output'].inputs['Surface'].links[0]
node_tree.links.remove(link)
2 Likes