2
$\begingroup$

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

$\endgroup$

1 Answer 1

4
$\begingroup$

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
bpy.data.materials['Material']

Re creating a material

Add Nodes to Material with Python

Python: Add existing NodeGroup to Material

$\endgroup$
1
  • $\begingroup$ Thanks! Really helpful! $\endgroup$
    – Derekcbr
    Oct 18, 2022 at 2:14

You must log in to answer this question.

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