16

How do I add an already created node group into a node tree?

I have found that this works:

group = node_tree.nodes.new("ShaderNodeGroup")
group.node_tree = other_group.node_tree

But it creates a copy of other_group instead of just referencing it. So how would I go about just straight adding it to the node tree?

Peter Mortensen
  • 681
  • 4
  • 11
BlendingJake
  • 2,557
  • 1
  • 23
  • 38

2 Answers2

14

After adding the new node group node you can assign the node_group data type to the node_tree:

group = node_tree.nodes.new("ShaderNodeGroup")
group.node_tree = bpy.data.node_groups['OldNodeGroupName']
Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125
2

The only way I know is with operator with overwritten context. That means you will have to have a Node Editor window somewhere open:

context_copy = bpy.context.copy()
context_copy['area'] = next(area for area in bpy.context.screen.areas if area.type=='NODE_EDITOR')
bpy.ops.node.add_node(context_copy,
                      type="ShaderNodeGroup",
                      settings=[{"name":"node_tree", "value":"bpy.data.node_groups['GROUP']"}])
Jaroslav Jerryno Novotny
  • 51,077
  • 7
  • 129
  • 218
  • 3
    I can't believe there wouldn't be a way to do this otherwise, if no one else answers with something different then I will accept this. Thanks – BlendingJake Jan 29 '15 at 17:19