I am trying to write an addon, I am making a node group that will be created with a single button however I have run into issues with these final lines:

    custom_node_name = "dust"
    my_group = create_dust_group(self, context, custom_node_name)
    dust_node = context.material.node_tree.nodes.new('ShaderNodeGroup')
    dust_node.node_tree = bpy.data.node_groups[my_group.name]
    dust_node.use_custom_color = True
    dust_node.color = (0.5, 0.3, 0.9)

the problem I'm having is with the line

dust_node = context.material.node_tree.nodes.new('ShaderNodeGroup')

I don't know how to get it to work for creating a material

any help or suggestions would be greatly appreciated

thank you


Different contexts also can have different context members.

Context Access (bpy.context)

The context members available depend on the area of Blender which is currently being accessed.

The properties panel is a classic example. Looking at property panel code may come across context.material or context.modifier but will find them unavailable in say the python console.

>>> C.material
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
AttributeError: 'Context' object has no attribute 'material'

Use another way to get material, from the active object

>>> C.object.active_material

Re creating a material

Add Nodes to Material with Python

Python: Add existing NodeGroup to Material

| improve this answer | |

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.