I want to dissolve REROUTE
nodes with Python.
Is there any way other than using bpy.ops.node.delete_reconnect()
?
You could search the node tree for nodes of type REROUTE and then check their input and output socket paths and reconnect them while deleting the reroute node, since there is no direct way to dissolve the node. In this example I traverse check the shader node tree of the material named "Material":
import bpy
node_tree = bpy.data.materials['Material'].node_tree
for node in node_tree.nodes[:]:
if node.type != 'REROUTE':
continue
input_socket = node.inputs[0]
output_socket = node.outputs[0]
for link in output_socket.links:
node_tree.links.new(input_socket.links[0].from_socket, link.to_socket)
node_tree.nodes.remove(node)
for node in node_tree.nodes[:]:
. BTW, no need for the check if there are any outward links, because you can iterate over an empty iterable (will do nothing). Also no need to use len(...) > 0
, as for non-bool values Python will apply bool()
which returns False
for an empty collection and True
for a non-empty collection.
$\endgroup$
Commented
Jul 10, 2023 at 11:53