I am attempting to make a python script that creates a nodegroup based on some scene properties, however the group_make operator needs to be in a node editor context to run.

It seems awkward to try and make a node view temporarily while the script is running, as the plan is to create the nodegroup with a fake user and have the user add it from the add menu in the node editor.

How should I go about this?

Update: I have found that you can create node groups without a node editor with bpy.data.node_groups.new (thanks to ideasman42's comment here), however I still can't figure out how to add nodes to it.

How can I edit the nodes inside nodegroup without a node editor?

I can't find node_tree for my node group. Here is my problem:

eq_node = bpy.data.node_groups.new("Equal", "ShaderNodeTree")

tree = eq_node.node_tree #This gives "no such attribute 'node_tree'". How can I acces the node tree of the node group?
nodes = tree.nodes
links = tree.links

#Create nodes in node group here with nodes.new
  • $\begingroup$ Have a look at addons_contrib/online_mat_lib. It saves materials out to a text file which can then be loaded in another blend file, it saves groups and I'm pretty sure it re-creates them without the node view visible, and it is a large script. $\endgroup$
    – sambler
    Dec 9, 2013 at 9:08

1 Answer 1


Thanks to iKlsR's response the complete answer here becomes mostly the same -

import bpy

# create a group
test_group = bpy.data.node_groups.new('testGroup', 'ShaderNodeTree')

# 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)

# create three math nodes in a group
node_add = test_group.nodes.new('ShaderNodeMath')
node_add.operation = 'ADD'
node_add.location = (100,0)

node_greater = test_group.nodes.new('ShaderNodeMath')
node_greater.operation = 'GREATER_THAN'
node_greater.label = 'greater'
node_greater.location = (-100,100)

node_less = test_group.nodes.new('ShaderNodeMath')
node_less.operation = 'LESS_THAN'
node_less.label = 'less'
node_less.location = (-100,-100)

# link nodes together
test_group.links.new(node_add.inputs[0], node_greater.outputs[0])
test_group.links.new(node_add.inputs[1], node_less.outputs[0])

# link inputs
test_group.links.new(group_inputs.outputs['in_to_greater'], node_greater.inputs[0])
test_group.links.new(group_inputs.outputs['in_to_less'], node_less.inputs[0])

#link output
test_group.links.new(node_add.outputs[0], group_outputs.inputs['out_result'])

To perform the same in 2.66 or older use -

import bpy

# create a group
test_group = bpy.data.node_groups.new('testGroup', 'SHADER')

# create group inputs and outputs
# add a node
node_add = test_group.nodes.new('MATH')
# link them
test_group.links.new(test_group.inputs[0], node_add.inputs[0])
test_group.links.new(node_add.outputs[0], test_group.outputs[0])
  • $\begingroup$ Thanks. Should this be reported as a bug? $\endgroup$
    – gandalf3
    Dec 10, 2013 at 20:18
  • $\begingroup$ Possibly, I found three closed bug reports (T26171 T36584 and T36808) that make me think we are missing something. If I can't find a solution later today I'll submit a bug report. $\endgroup$
    – sambler
    Dec 11, 2013 at 0:03
  • $\begingroup$ If you want to follow or comment I have submitted T37776 $\endgroup$
    – sambler
    Dec 11, 2013 at 7:10
  • $\begingroup$ So it's solved? You should add an answer how (crosspost from phab) and accept yourself. $\endgroup$
    – CodeManX
    Dec 11, 2013 at 21:08
  • $\begingroup$ I have updated the answer to show only the working code, and linked to the similar answer that gave the missing key to getting it working. $\endgroup$
    – sambler
    Dec 12, 2013 at 1:28

You must log in to answer this question.

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