How to set Node's group color via Python?

Could you tell me how to Node's group color via Python? Something like this:

class AddButton(bpy.types.Operator):

def execute(self, context):
mat = bpy.data.materials.new('zheight')
mat.use_nodes = True
nodes = mat.node_tree.nodes
# print("execute")
obj = bpy.context.object
# change on cycles
bpy.context.scene.render.engine = 'CYCLES'
#Assign a material to the last slot
obj.material_slots[obj.material_slots.__len__() - 1].material = MakeMaterial_1()

############ color ############
C = bpy.context
D = bpy.data

compTree = C.scene.node_tree                   # Compositing node tree
matTree  = D.materials[ mat.name ].node_tree # 'MyMaterial' material node tree

t = matTree # For this example we'll use the compositing node tree

nodes = mat.node_tree.nodes
print( group.name )

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

# Turn on custom color
t.nodes[ group.name ].use_custom_color = True
t.nodes[ group.name ].color = ( 1, 0.711485, 0.137081 )

glossinessFactorInput = bpy.data.node_groups[ group.name ].inputs['Glossiness Factor']

glossinessFactorInput.min_value     = -5.0
glossinessFactorInput.max_value     = 5.0
glossinessFactorInput.default_value = 1.5
return {"FINISHED"}


OK, so you can easily make the script dynamic and refer to the newest group's name since you keep a reference to the group after you create it:

group = bpy.data.node_groups.new(type="ShaderNodeTree", name="PBR")
print( group.name )


So instead of using a hardcoded name like in these lines:

groupName = 'Group'

# Turn on custom color
t.nodes[ groupName ].use_custom_color = True


You can simply use group.name:

t.nodes[ group.name ].use_custom_color = True

• Now min/max values are working, but color change is still broken. I get: in execute KeyError:'Bpy_prop_collection[key]: "PBR.001" not found' I edited a post below Oct 11 '15 at 12:47
• Try using group_node.name instead of group.name in the line that causes you trouble ( glossinessFactorInput = bpy.data.node_groups[ group.name ].inputs['Glossiness Factor'] ) Oct 11 '15 at 12:53

A node group is also a node, so you set the node group's color the same way you do any other node:

import bpy
C = bpy.context
D = bpy.data

compTree = C.scene.node_tree                   # Compositing node tree
matTree  = D.materials['MyMaterial'].node_tree # 'MyMaterial' material node tree

t = compTree # For this example we'll use the compositing node tree

groupName = 'Group'

# Turn on custom color
t.nodes[ groupName ].use_custom_color = True

# Set group's color to pink
t.nodes[ groupName ].color = ( 0.5, 0.2, 0.75 )

• Thank you! It works very well, but I have the same problem with dynamic name. Oct 11 '15 at 10:08
• Can you post the operator that creates your node group? It will help answering in a relevant way Oct 11 '15 at 11:31
• Sure, I posted it below: Oct 11 '15 at 11:43
• @McGavish I converted this to an edit since it isn't an answer, please amend your post as deemed necessary. Oct 11 '15 at 13:02