Creating inputs and outputs for node groups in Blender 4.0 using the Python API

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:

A pointer to the node tree interface through which we can create inputs and outputs:

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:

Output sockets for floating point numbers and colors:

The created input and output sockets appear in the node group:

0 0 votes
Article Rating
Subscribe
Notify of
guest

9 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
robocat
robocat
3 months ago

I love you. But how do you make the node that the group is set to?

robocat
robocat
3 months ago
Reply to  Nikita

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?

robocat
robocat
3 months ago
Reply to  Nikita

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)

Last edited 3 months ago by robocat
wonk
wonk
3 months ago

thank you, life saver!

Rombout Versluijs
Rombout
9 months ago

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?!