Not created again already exists material
A material never is True.
Use if mat: which is boolean true if not None  Using if mat is True:  for a material will never be true.
Since you are setting use nodes later can also do this
mat = materials.get(&quot;Foo&quot;) or materials.new(&quot;Foo&quot;)

To drill this in here is a simple example
import bpy

context = bpy.context

def main(context):
    mat = bpy.data.materials.get(&quot;Foo&quot;)
    if mat:
        print(&quot;Material Foo exists, skipping&quot;)
        return 
    else:
        mat = bpy.data.materials.new(&quot;Foo&quot;)
        print(&quot;Created Material Foo&quot;)
        # do something with new material
        
    return

# test calls

main(context)
main(context)
main(context)

Output from default file no material &quot;Foo&quot;
Created Material Foo
Material Foo exists, skipping
Material Foo exists, skipping