I wan't to create a node group "testgroup1" inside "testgroup0" using python script can anybody explain me how to do that?


1 Answer 1


Here is some Python functions to manipulate groups and group instances.

Principally, there is two aspects:

  • Groups are stored in Blender's data in bpy.data.node_groups
  • Inside a material or another group, a group needs to be instantiated so that it is link to this material or other group

The code is commented below:

import bpy

def create_material( name, use_nodes = True, clear = True ):
    Create a material of the given name. 

    name (string): the name of the material to create 
    use_nodes (bool, default True): if True the material is ready to have nodes  
    clear (bool, default True): if True clear default nodes

    material: The created material 
    material = bpy.data.materials.new( name )
    material.use_nodes = use_nodes
    # Materials are created with two default nodes, so we may want to clear them
    if clear:
        clear_material( material )
    return material

def clear_material( material ):
    Remove all node of the given material

    material (material): the material to clear 
    if material.node_tree:

def create_group( name ):
    Create a group in Blender's data

    name (string): the name of the material

    group (group): the created group
    group = bpy.data.node_groups.new( name, 'ShaderNodeTree' )
    return group

def instanciate_group( nodes, group ):
    Instanciate a group inside a node tree

    nodes: the nodes of the node tree in which the group is to instantiate
    group: the group to instanciate

    instance: the created group instance
    instance = nodes.new( type = 'ShaderNodeGroup' )
    instance.node_tree = group
    return instance

# Example:

# Create a material
material = create_material( "mat" )
# Create group0
group0 = create_group( "group0" )
# Create group1
group1 = create_group( "group1" )

# Instantiate the group0 into the material
group0_instance = instanciate_group( material.node_tree.nodes, group0 )
# Instantiate the group1 into group0
group1_instance = instanciate_group( group0.nodes, group1 )

You must log in to answer this question.

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