The easiest way to create new inputs and outputs for node groups is to press the plus button in the node group editing mode (tab) on the N-panel on the “Group” section. However, when creating node groups using the Blender Python API, this method is not available.
To create an input or output for a node group, we must remember that inputs and outputs are not created for a current node, but for the entire node tree, which is enclosed in a node group.
We can get a pointer to a node group node tree through its “node_tree” property
1 2 3 |
bpy.context.object.active_material.node_tree.nodes['NodeGroup'].node_tree # bpy.data.node_groups['NodeGroup'] |
For this node tree we need to create inputs and outputs.
To create an input, we can use the “new()” method of the “inputs” node tree property:
1 |
bpy.context.object.active_material.node_tree.nodes['NodeGroup'].node_tree.inputs.new(type='NodeSocketShader', name='New Intput') |
In the method parameters, we pass the type of input we are creating (NodeSocketShader) and the name we want to assign to it.
In total, the API provides 4 possible types of inputs and outputs:
- NodeSocketShader – for shaders
- NodeSocketVector – for vectors
- NodeSocketFloat – for floating point numbers
- NodeSocketColor – for colors
and another one, not available for common selection:
- NodeSocketInt – for integers
A new output is created in the same way as an input, through the “new()” method, but for the “outputs” property:
1 |
bpy.context.object.active_material.node_tree.nodes['NodeGroup'].node_tree.outputs.new(type='NodeSocketShader', name='New Intput') |
The inputs and outputs created in this way are immediately displayed on the Node Group node, and also appear on the Group Input and Group Output nodes.