Delete All nodes from x materials

Hi !
I made lot’s of search but I can’t find how to delete all nodes in a list of materials,
My goal is for exemple to delete all existing node in materials with “Wood” in their name and then create a simple Diffuse BSDF linked with material output.


<b>for </b>mat <b>in </b>bpy.data.materials:
<b>   if "Wood" in </b>mat.name:
        <i><i>Delete all nodes
</i></i>        ...


To get around this problem I had the idea to simply rename the materials with _old and replace them in their slots by a new one but I don’t like it


for mat in bpy.data.materials:
    if "Wood" in mat.name:
        nodes = mat.node_tree.nodes
        for node in nodes:
            if node.type != 'OUTPUT_MATERIAL': # skip the material output node as we'll need it later
                nodes.remove(node) 

Thank you very much !!!
It works perfectly
I could not believe it was so simple thank you

If I wan’t To Place A new Node In Custom Position After this
Then How Could I do That ??

With “mat” as your material:

node =  mat.node_tree.nodes.new(<node_type>)
node.location = (125,125) # x,y
1 Like