context.material.node_tree - context has no attribute to material? 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. 

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


Use another way to get material, from the active object

&gt;&gt;&gt; C.object.active_material
bpy.data.materials['Material']


Re creating a material

Add Nodes to Material with Python

Python: Add existing NodeGroup to Material