1
$\begingroup$

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


class AddButton(bpy.types.Operator):
bl_idname = "add.button"
bl_label = "Add PBR Group"

def execute(self, context):
    mat = bpy.data.materials.new('zheight')
    mat.use_nodes = True
    nodes = mat.node_tree.nodes
   # print("execute") 
   # print(self.filepath) # /home/addajr/Grafika/3D/textury/metal.jpg
    obj = bpy.context.object
    # change on cycles
    bpy.context.scene.render.engine = 'CYCLES'
    #Add a material slot
    bpy.ops.object.material_slot_add()
    #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    


    ##########   add custom nodes ########     
    nodes = mat.node_tree.nodes
    group = bpy.data.node_groups.new(type="ShaderNodeTree", name="PBR")
    print( group.name )

    .
    .
    .
    .
    tree = bpy.context.object.active_material.node_tree
    group_node = tree.nodes.new("ShaderNodeGroup")
    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"}
$\endgroup$
0
$\begingroup$

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
$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$
    – Mc Gavish
    Oct 11 '15 at 12:47
  • 1
    $\begingroup$ 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'] ) $\endgroup$
    – TLousky
    Oct 11 '15 at 12:53
3
$\begingroup$

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 )
$\endgroup$
4
  • $\begingroup$ Thank you! It works very well, but I have the same problem with dynamic name. $\endgroup$
    – Mc Gavish
    Oct 11 '15 at 10:08
  • $\begingroup$ Can you post the operator that creates your node group? It will help answering in a relevant way $\endgroup$
    – TLousky
    Oct 11 '15 at 11:31
  • $\begingroup$ Sure, I posted it below: $\endgroup$
    – Mc Gavish
    Oct 11 '15 at 11:43
  • 1
    $\begingroup$ @McGavish I converted this to an edit since it isn't an answer, please amend your post as deemed necessary. $\endgroup$
    – iKlsR
    Oct 11 '15 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.