# How to connect nodes to node group inputs and outputs in python?

How can I connect nodes in a node group to the input or output sockets?

This connection here:

I can't figure out how to access the group node_tree, and the Group node as well as the Group input/output nodes seem to be different than normal nodes.

My script so far:

import bpy

for area in bpy.context.screen.areas:
if area.type == "NODE_EDITOR":
override = {'screen': bpy.context.screen, 'area': area}
bpy.ops.node.select_all(override, action='DESELECT')

tree = bpy.context.object.active_material.node_tree
nodes = tree.nodes

node.location = (100,0)

node.operation = "GREATER_THAN"
node.name = "greater"
node.location = (-100,100)

node.operation = "LESS_THAN"
node.name = "less"
node.location = (-100,-100)

bpy.ops.node.group_make(override)


As a rule of thumb, you should try to avoid operator calls in scripts and use api functions directly as much as possible (if you do this, no need to override). So the snippet below is not needed.

for area in bpy.context.screen.areas:
if area.type == "NODE_EDITOR":
override = {'screen': bpy.context.screen, 'area': area}
bpy.ops.node.select_all(override, action='DESELECT')


Through some experimenting, I found it's easier to not use group_make as the default GroupInput and GroupOutput nodes it creates can be a bit unwieldy and adding a Socket to a tree creates a template rather than an actual socket (the lists displayed in Interface panel).

Rather, it's easier to create a new node group with bpy.data.node_groups.new and work with that.

import bpy

group.inputs.new("NodeSocketFloat", "Input One")
group.inputs.new("NodeSocketFloat", "Input Two")
input_node = group.nodes.new("NodeGroupInput")
input_node.location = (0, 0)

group.outputs.new("NodeSocketFloat", "Out")
output_node = group.nodes.new("NodeGroupOutput")
output_node.location = (600, 0)

gtr_math_node.operation = "GREATER_THAN"
gtr_math_node.name = "greater"
gtr_math_node.location = (200, 100)

lss_math_node.operation = "LESS_THAN"
lss_math_node.name = "less"
lss_math_node.location = (200,-100)

tree = bpy.context.object.active_material.node_tree
group_node.node_tree = group


The above gives you

• Thanks NodeSocketFloat is the key that I was missing. Dec 11 '13 at 10:40

I figured a way to get this to work. It still uses the override as we add the group to the material and put the group into edit mode to use the tree_socket_add operator. Ideally we should be able to do this without using operators to visually edit the group. I'm using the operator usage to separate this from your other group question. As mentioned in that question the api change in 2.67 means this won't work in blender <=2.66

It should be improved by testing that we have an active material or even an object to add a material to.

Note that tree_socket_add adds the sockets to test_group.inputs where we can name them but to make connections we need to use group_inputs.outputs

import bpy

for area in bpy.context.screen.areas:
if area.type == "NODE_EDITOR":
override = {'screen': bpy.context.screen, 'area': area}

# create a group

# create group inputs
group_inputs = test_group.nodes.new('NodeGroupInput')
group_inputs.location = (-350,0)

# create group outputs
group_outputs = test_group.nodes.new('NodeGroupOutput')
group_outputs.location = (300,0)

# add group to active material
# put group into edit mode
bpy.ops.node.group_edit(override)

# create group input/output sockets
test_group.inputs[0].name = 'in_to_greater'
test_group.inputs[1].name = 'in_to_less'
test_group.outputs[0].name = 'out_result'

# create three math nodes in a group

node_greater.operation = 'GREATER_THAN'
node_greater.label = 'greater'
node_greater.location = (-100,100)