2
$\begingroup$

I have been creating node groups with the group_make operator, but there doesn't seem to be any way to set properties such as the label or name of the resulting group node.

This makes it difficult to access this node later, and be sure that one is accessing the correct node.

How can I specify node properties on creation?

$\endgroup$

3 Answers 3

5
$\begingroup$

The problem with using operators in scripts is they return {FINISHED} rather than the object you wish to keep working with. Instead of using the make_group operator you could use

import bpy

test_group = bpy.data.node_groups.new('testGroup', 'ShaderNodeTree')
test_group.name = 'anotherGroupName'

node_groups.new() creates the group with the name set to the first pramater. The result returned into test_group can be used later to change that name.

You can also reference the group by name to change it.

bpy.data.node_groups['testgrp'].name = 'NewGroupName'

but without knowing the auto-generated name from the operator this isn't helpful.

$\endgroup$
5
$\begingroup$

You can use something like this:

node_grp = bpy.data.node_groups[-1] # to get last created group
node_grp.name = "Special Name" # to set a name
$\endgroup$
5
  • 3
    $\begingroup$ Its really bad practice to assume this kind of stuff (the position in a sequence of newly added data), that said, it does work and is unlikely to break any time soon. Ideally you would create the group using bpy.data.node_groups.new(name, type) but realize there may be cases where this isnt a drop-in replacement. $\endgroup$
    – ideasman42
    Dec 9, 2013 at 23:59
  • $\begingroup$ For some reason bpy.data.node_groups[-1] is not returning the last created node group. $\endgroup$
    – gandalf3
    Dec 10, 2013 at 0:46
  • $\begingroup$ @gandalf3: many bpy collections are ordered alphabetically, so indices may change as you rename or add items. $\endgroup$
    – CodeManX
    Dec 10, 2013 at 8:13
  • $\begingroup$ @ideasman42 bpy.data.node_groups.new works for me, thanks. @CoDEmanX I assigned the variable right after creating the nodegroup, but it still wasn't the right nodegroup. $\endgroup$
    – gandalf3
    Dec 10, 2013 at 8:17
  • $\begingroup$ The last created node is made active is it not? $\endgroup$
    – Greg Zaal
    Dec 10, 2013 at 19:00
1
$\begingroup$

To rename a new group you can after creating first get out of the group

# Get out of group
bpy.ops.node.group_edit( get_override('NODE_EDITOR') )

Then name the active node which is the group

groupname = bpy.context.active_object.active_material.node_tree.nodes.active

Then if needed get "back in the group"

bpy.ops.node.group_edit( get_override('NODE_EDITOR') )

The override function:

def get_override(area_type):
    for window in bpy.context.window_manager.windows:
        screen = window.screen
        
        for area in screen.areas:
            if area.type == area_type:
                for region in area.regions:
                    if region.type == 'WINDOW':
                        override = {'window': window,
                                    'screen': screen,
                                    'area': area,
                                    'region': region,
                                    'blend_data': bpy.context.blend_data}
                        
                        return override
$\endgroup$
2
  • 3
    $\begingroup$ Please consider adding your get_override func, otherwise no one can tell whether that's a good or bad approach. $\endgroup$
    – brockmann
    Mar 18, 2021 at 11:34
  • $\begingroup$ Thank you brockmann, done. $\endgroup$
    – Lump
    Mar 18, 2021 at 13:47

You must log in to answer this question.

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