"""Create/refresh MAT_wgt01_* materials in wgt01.blend (run headless)."""
import bpy, os
SLOTS = ["cobble", "worn", "plaza", "sidewlk", "wall", "bridge", "gutter", "water",
         "fbrick", "iron", "timber", "planks", "brass", "copper", "plastr", "slate", "glass", "dirt", "grass", "gravel", "road"]
BASE = os.path.dirname(bpy.data.filepath)

for slot in SLOTS:
    name = f"MAT_wgt01_{slot}"
    mat = bpy.data.materials.get(name) or bpy.data.materials.new(name)
    mat.use_fake_user = True
    mat.use_nodes = True
    nt = mat.node_tree; nt.nodes.clear()
    out = nt.nodes.new("ShaderNodeOutputMaterial")
    bsdf = nt.nodes.new("ShaderNodeBsdfPrincipled")
    nt.links.new(bsdf.outputs["BSDF"], out.inputs["Surface"])
    d = os.path.join(BASE, "textures", f"wgt01_{slot}_d.tga")
    img = bpy.data.images.load(d, check_existing=True)
    img.filepath = f"//textures/wgt01_{slot}_d.tga"
    tex = nt.nodes.new("ShaderNodeTexImage"); tex.image = img
    nt.links.new(tex.outputs["Color"], bsdf.inputs["Base Color"])
    n = os.path.join(BASE, "textures", f"wgt01_{slot}_n.tga")
    if os.path.exists(n):
        nimg = bpy.data.images.load(n, check_existing=True)
        nimg.filepath = f"//textures/wgt01_{slot}_n.tga"; nimg.colorspace_settings.name = "Non-Color"
        ntex = nt.nodes.new("ShaderNodeTexImage"); ntex.image = nimg
        nmap = nt.nodes.new("ShaderNodeNormalMap")
        nt.links.new(ntex.outputs["Color"], nmap.inputs["Color"])
        nt.links.new(nmap.outputs["Normal"], bsdf.inputs["Normal"])
    # wet look: low roughness on ground slots
    bsdf.inputs["Roughness"].default_value = 0.35 if slot in ("cobble", "gutter", "water", "iron", "brass", "copper", "glass") else 0.6

bpy.ops.wm.save_mainfile()
print("WIRED:", ", ".join(f"MAT_wgt01_{s}" for s in SLOTS))
