"""Generate material_library.blend with all 35 Schaeffler standard materials.

Run with:  blender --background --python generate_blend.py
"""
import bpy
import os

# Placeholder colors per material — tuned to approximate real-world appearance
# Format: (R, G, B, A) linear color, metallic, roughness
MATERIALS = [
    # --- 01 Metals ---
    ("SCHAEFFLER_010101_Steel-Bare",              (0.55, 0.56, 0.58, 1.0), 1.0, 0.35),
    ("SCHAEFFLER_010102_Steel-Burnished",         (0.15, 0.12, 0.10, 1.0), 1.0, 0.25),
    ("SCHAEFFLER_010103_Steel-Galvanized",        (0.65, 0.67, 0.70, 1.0), 1.0, 0.40),
    ("SCHAEFFLER_010104_Steel-Casted",            (0.35, 0.33, 0.31, 1.0), 1.0, 0.60),
    ("SCHAEFFLER_010105_Steel-Plate",             (0.50, 0.51, 0.53, 1.0), 1.0, 0.30),
    ("SCHAEFFLER_010201_Niro",                    (0.70, 0.72, 0.74, 1.0), 1.0, 0.20),
    ("SCHAEFFLER_010301_Tin",                     (0.75, 0.75, 0.73, 1.0), 1.0, 0.30),
    ("SCHAEFFLER_010401_Aluminium",               (0.80, 0.80, 0.82, 1.0), 1.0, 0.25),
    ("SCHAEFFLER_010501_Brass",                   (0.70, 0.55, 0.20, 1.0), 1.0, 0.25),
    ("SCHAEFFLER_010601_Bronze",                  (0.55, 0.35, 0.15, 1.0), 1.0, 0.30),
    # --- 02 Coatings ---
    ("SCHAEFFLER_020101_Durotect-Blue",           (0.15, 0.25, 0.50, 1.0), 0.8, 0.20),
    ("SCHAEFFLER_020102_Durotect-Black",          (0.05, 0.05, 0.06, 1.0), 0.8, 0.15),
    ("SCHAEFFLER_020201_Coat-Black",              (0.03, 0.03, 0.03, 1.0), 0.6, 0.10),
    # --- 03 Non-metals ---
    ("SCHAEFFLER_030101_Elastomer-Brown",         (0.30, 0.18, 0.08, 1.0), 0.0, 0.55),
    ("SCHAEFFLER_030102_Elastomer-Green",         (0.10, 0.30, 0.10, 1.0), 0.0, 0.55),
    ("SCHAEFFLER_030103_Elastomer-Black",         (0.04, 0.04, 0.04, 1.0), 0.0, 0.55),
    ("SCHAEFFLER_030201_Plastic-Brown",           (0.35, 0.22, 0.10, 1.0), 0.0, 0.40),
    ("SCHAEFFLER_030202_Plastic-Green",           (0.08, 0.35, 0.12, 1.0), 0.0, 0.40),
    ("SCHAEFFLER_030203_Plastic-Black",           (0.02, 0.02, 0.02, 1.0), 0.0, 0.40),
    ("SCHAEFFLER_030204_Plastic-Blue",            (0.10, 0.20, 0.50, 1.0), 0.0, 0.40),
    ("SCHAEFFLER_030205_Plastic-White",           (0.85, 0.85, 0.85, 1.0), 0.0, 0.40),
    ("SCHAEFFLER_030301_Plastic-Clear",           (0.90, 0.90, 0.92, 1.0), 0.0, 0.10),  # + transmission
    ("SCHAEFFLER_030302_Plastic-Translucent-White", (0.80, 0.80, 0.82, 1.0), 0.0, 0.20),  # + transmission
    ("SCHAEFFLER_030401_TPU-Blue",                (0.12, 0.25, 0.55, 1.0), 0.0, 0.45),
    ("SCHAEFFLER_030501_Ceramic-Black",           (0.03, 0.03, 0.04, 1.0), 0.0, 0.15),
    # --- 04 Compounds ---
    ("SCHAEFFLER_040101_E40",                     (0.25, 0.22, 0.18, 1.0), 0.0, 0.50),
    ("SCHAEFFLER_040102_E50",                     (0.28, 0.25, 0.20, 1.0), 0.0, 0.50),
    ("SCHAEFFLER_040201_Elgoglide",               (0.20, 0.22, 0.25, 1.0), 0.0, 0.35),
    ("SCHAEFFLER_040202_Elgotex",                 (0.05, 0.05, 0.06, 1.0), 0.0, 0.35),
    ("SCHAEFFLER_040301_PTFE-Niro-Compound",      (0.60, 0.62, 0.65, 1.0), 0.3, 0.25),
    ("SCHAEFFLER_040302_PTFE-Foil",               (0.85, 0.85, 0.82, 1.0), 0.0, 0.15),
    ("SCHAEFFLER_040303_PTFE-Compound-Black",     (0.04, 0.04, 0.05, 1.0), 0.0, 0.30),
    ("SCHAEFFLER_040304_PTFE-Compound-Orange",    (0.70, 0.35, 0.08, 1.0), 0.0, 0.30),
    ("SCHAEFFLER_040305_GFK-PTFE-Compound",       (0.08, 0.10, 0.08, 1.0), 0.0, 0.45),
    # --- 05 Misc ---
    ("SCHAEFFLER_059999_FailedMaterial",           (1.00, 0.00, 0.50, 1.0), 0.0, 0.50),
]

# Translucent materials that need transmission
TRANSLUCENT = {
    "SCHAEFFLER_030301_Plastic-Clear": 0.9,
    "SCHAEFFLER_030302_Plastic-Translucent-White": 0.5,
}


def main():
    # Start from factory defaults
    bpy.ops.wm.read_factory_settings(use_empty=True)

    for name, color, metallic, roughness in MATERIALS:
        mat = bpy.data.materials.new(name=name)
        mat.use_nodes = True
        nodes = mat.node_tree.nodes
        links = mat.node_tree.links

        # Clear default nodes
        for n in nodes:
            nodes.remove(n)

        # Create Principled BSDF + Material Output
        bsdf = nodes.new("ShaderNodeBsdfPrincipled")
        bsdf.location = (0, 0)
        output = nodes.new("ShaderNodeOutputMaterial")
        output.location = (300, 0)
        links.new(bsdf.outputs["BSDF"], output.inputs["Surface"])

        # Set properties
        bsdf.inputs["Base Color"].default_value = color
        bsdf.inputs["Metallic"].default_value = metallic
        bsdf.inputs["Roughness"].default_value = roughness

        # Transmission for translucent materials
        if name in TRANSLUCENT:
            bsdf.inputs["Transmission Weight"].default_value = TRANSLUCENT[name]
            bsdf.inputs["IOR"].default_value = 1.45

        # Also set the viewport display color for solid-view preview
        mat.diffuse_color = color

        # Fake user so Blender keeps the material on save
        mat.use_fake_user = True

    # Save
    out_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "material_library.blend")
    bpy.ops.wm.save_mainfile(filepath=out_path)
    print(f"\nSaved {len(MATERIALS)} materials to: {out_path}")


if __name__ == "__main__":
    main()
