In the following script I am trying to create a material on the active object, then change the context to the shader node editor and then delete the existing default node setup:
import bpy mat = bpy.data.materials.get("Material") geom = bpy.context.active_object if mat is None: # create material mat = bpy.data.materials.new(name="Material") # Assign it to object if geom.data.materials: # assign to 1st material slot geom.data.materials = mat else: # no slots geom.data.materials.append(mat) bpy.context.area.type = 'NODE_EDITOR' bpy.ops.node.select_all(action='TOGGLE') bpy.ops.node.delete()
When I first run this script (from the text editor) on my scene I get the following:
RuntimeError: Operator bpy.ops.node.select_all.poll() failed, context is incorrect
Although, curiously, when I run it a second time (again from the text editor), the context is successfully changed to the shader node editor but no nodes are deleted.
And finally, when I run it a third time, it successfully deletes the existing nodes in the shader node editor.
Why does this script apparently work better each time it is run?
(I am using Blender 2.8)