0
$\begingroup$

because I have the need to constantly call the render node into my scenes. I would like to automate the process of joining variants with the same name. or somehow celebrate them

Ideally, of course, there would be an option to create up to 8 similar nodes with the necessary names. But my skills are not even enough to automatically connect one

enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

I will walk you through the process of joining one output to one input. You should be able to generalize from that. For my example, I'm going to use a Render Layers node for the input side but instead of your group node I'm going to use a Denoise node for the output side. I'm also going to assume that the nodes are already in the compositor node tree.

  • The first thing you need to know is that the Compositor Node Tree is attached to the scene, so to access it use

    tree = bpy.context.scene.node_tree
    
  • You need to find the links for that tree

    links = tree.links
    
  • You need to find the Render Layer Node's Image output

    render_node = tree.nodes['Render Layers']
    output_socket = render_node.outputs['Image']
    
  • You need to find the Denoise Node's Image input

    denoise_node = tree.nodes['Denoise']
    input_socket = denoise_node.inputs['Image']
    
  • and you need to link them

    links.new(output_socket, input_socket)
    

You'll need to repeat the last three steps for each pair of sockets you want to connect.

Putting the example altogether:

import bpy

bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
links = tree.links
render_node = tree.nodes['Render Layers']
output_socket = render_node.outputs['Image']

denoise_node = tree.nodes['Denoise']
input_socket = denoise_node.inputs['Image']

links.new(output_socket, input_socket)

If the nodes aren't already created, you create them using tree.nodes.new():

render_node = tree.nodes.new('CompositorNodeRLayers')
denoise_node = tree.nodes.new('CompositorNodeDenoise')

To access your group node, you refer to it by its name, ie tree.nodes['Group'].

To add a group takes two steps, creating the Node Group node, and linking it to an existing node tree:

group_node = tree.nodes.new('CompositorNodeGroup')
group_node.node_tree = bpy.data.node_groups['NodeGroup']

where you replace 'NodeGroup' with the name of your node group.

Creating the Node Tree node group is outside of the scope of the question.

Finally, since the names of the Render Layers node's outputs don't seem to directly match the names of your group's inputs; I suggest you use some sort of data structure, such as a list of tuples to store the names for you to iterate over to create all of the connections. Here's one approach:

def attach_sockets(tree, from_node, to_node, socket_list):
    ''' socket_list is alist of tuples of socket names.
        The first is the output socket of from_node to use.
        The second is the input socket of to_node.
        Each pair is linked.
        No error handling is done.
    '''
    for sockets in socket_list:
        src = from_node.outputs[sockets[0]]
        dst = to_node.inputs[sockets[1]]
        tree.links.new(src, dst)

# A list of tuples containing the names of the output socket
# and input socket for a set of attachments.
socket_list = [
    ('Image', 'Image'),
    ('Denoising Normal', 'Normal'),
    ('Denoising Albedo', 'Albedo'),
]

attach_sockets(tree, render_node, group_node, socket_list)
$\endgroup$
1
  • $\begingroup$ KeyError: 'bpy_prop_collection[key]: key "Render Layers" not found. He writes this when trying to refer to the line render_node = tree.nodes['Render Layers'] $\endgroup$
    – Welet
    Commented Jun 22, 2022 at 10:15

You must log in to answer this question.

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