5
$\begingroup$

I want to dissolve REROUTE nodes with Python.

Is there any way other than using bpy.ops.node.delete_reconnect()?

enter image description here

$\endgroup$

1 Answer 1

5
$\begingroup$

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)
$\endgroup$
3
  • 1
    $\begingroup$ As core Python dev, Raymond Hettinger says: "if you mutate something you're iterating over, you're living in a state of sin and deserve whatever happens to you". Iterate over a copy of nodes like so: 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
  • 1
    $\begingroup$ @MarkusvonBroady oops oh yeah correct, i didnt realize i was modifying that im iterating over lol thanks for pointing that out XD $\endgroup$
    – Harry McKenzie
    Commented Jul 10, 2023 at 12:10
  • $\begingroup$ thanks markus. yes no need the > 0 check, i was testing stuff and forgot to remove that check. i made the change thank you hehe $\endgroup$
    – Harry McKenzie
    Commented Jul 10, 2023 at 12:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .