In Blender 3.6 and earlier, inputs and outputs for node groups could be created by calling the new() command for the list of inputs or outputs of the node tree. In Blender 4.0, the developers made changes, and the creation of input and output sockets for node trees was moved to the “interface” object.
Inputs and outputs are still created not for the node group but for its node tree, a pointer to which can be obtained through the node group object.
Pointer to the node tree for the currently selected node group:
1 2 3 |
node_tree = bpy.context.object.active_material.node_tree.nodes.active.node_tree # bpy.data.node_groups['NodeGroup'] |
A pointer to the node tree interface through which we can create inputs and outputs:
1 2 3 |
interface = node_tree.interface # bpy.data.node_groups['NodeGroup']...NodeTreeInterface |
To create a new input or output socket, we need to call the new_socket() interface method with the following required parameters:
- name – new input/output name
- in_out – input or output socket we create. To create input socket, we need to specify the “INPUT” value, and to make the output socket – “OUTPUT”.
- socket_type – type of the socket. Still:
- NodeSocketShader – for shaders
- NodeSocketVector – for vectors
- NodeSocketFloat – for float numbers
- NodeSocketColor – for color
We can also specify additional parameters: “description” – for description, and “parent” for specifying the parent panel of the socket.
Let’s create several input and output sockets.
Input sockets for shaders and vectors:
1 2 3 4 5 6 7 8 9 10 11 |
interface.new_socket( name='Shader', in_out='INPUT', socket_type='NodeSocketShader' ) interface.new_socket( name='Vector', in_out='INPUT', socket_type='NodeSocketVector' ) |
Output sockets for floating point numbers and colors:
1 2 3 4 5 6 7 8 9 10 11 |
interface.new_socket( name='Float', in_out='OUTPUT', socket_type='NodeSocketFloat' ) interface.new_socket( name='Color', in_out='OUTPUT', socket_type='NodeSocketColor' ) |
The created input and output sockets appear in the node group:
I love you. But how do you make the node that the group is set to?
Hi!
You can create the node group itself with
and add it to the GN node tree:
Thanks so much!
I’m also having issues with linking the input nodes of groups to nodes within the group. It keeps giving me a key error, could you shed any light on this?
In “Group Input” nodes you have outputs. So this should be something like this in your last code line:
Oh sorry that was a typo, I was just trying that but it did give the same error when I used inputNode.ouputs[]. And when I try accessing the list by index, it says it’s empty. Is it possible I’m creating my input and output node incorrectly?
group_input : bpy.types.ShaderNodeGroup = nodeTree.nodes.new(‘NodeGroupInput’)
group_input.location = (-400,0)
group_output : bpy.types.ShaderNodeGroup = nodeTree.nodes.new(‘NodeGroupOutput’)
group_output.location = (400,0)
You mast use : only when defying properties in Python.
I don’t understand what is this:
and don’t use types
you need just
thank you, life saver!
Guess all this breaking of the API is done for Geometry Nodes Tools i guess… Man so much work to do. Also, its very badly documented, how do we even create group inputs and outputs, nowhere in there API changes?!
I think that in any case, it is good that Blender is progressing in developing.
Some documentation, some experiments… everything as usual )