Add helper function to node_utils to create links to virtual sockets #105975

Hans Goudey merged 1 commits from pioverfour/blender:dp_node_utils_virtual_sockets into main 2023-04-12 14:18:43 +02:00
1 changed files with 39 additions and 0 deletions

View File

@ -1,10 +1,49 @@
# SPDX-License-Identifier: GPL-2.0-or-later
__all__ = (
def connect_sockets(input, output):
Connect sockets in a node tree.
This is useful because the links created through the normal Python API are
invalid when one of the sockets is a virtual socket (grayed out sockets in
Group Input and Group Output nodes).
It replaces, output)
import bpy
# Swap sockets if they are not passed in the proper order
if input.is_output and not output.is_output:
input, output = output, input
input_node = output.node

Probably it worth to mention that performance of the function is O(len(node_group.nodes)) because you call output.node. Also in some cases output.node can return None.

Probably it worth to mention that performance of the function is `O(len(node_group.nodes))` because you call `output.node`. Also in some cases `output.node` can return None.
output_node = input.node
if input_node.id_data is not output_node.id_data:
print("Sockets do not belong to the same node tree")
if type(input) == type(output) == bpy.types.NodeSocketVirtual:
print("Cannot connect two virtual sockets together")
if output_node.type == 'GROUP_OUTPUT' and type(input) == bpy.types.NodeSocketVirtual:,
input = output_node.inputs[-2]
if input_node.type == 'GROUP_INPUT' and type(output) == bpy.types.NodeSocketVirtual:,
output = input_node.outputs[-2]
return, output)
# XXX Names are not unique. Returns the first match.
def find_node_input(node, name):
for input in node.inputs: