main
John Lauer Add symbol, footprint, Fusion and Altium libraries, viewers, provenance, README ab389b2 6h ago
#!/usr/bin/env python3
"""Physical materials for a chip GLB by part (node name from the STEP assembly). step2glb writes base colours only,
so glTF defaults (metallic 1, roughness 1) render everything as rough metal. Sets:
  body -> "Adom.Body (epoxy)" metallic 0, roughness 0.55 (colour kept)
  pins -> "Adom.Leads (matte tin)" metallic 1, roughness 0.35, tin colour
  Adom.LaserEtch -> "Adom.LaserEtch (virtual MPN)" metallic 0, roughness 0.7 (colour kept)
usage: materials.py in.glb [out.glb]"""
import sys
from pygltflib import GLTF2, PbrMetallicRoughness
TIN = [0.72, 0.72, 0.70, 1.0]
SPEC = {"body": ("Adom.Body (epoxy)", 0.0, 0.55, None), "pins": ("Adom.Leads (matte tin)", 1.0, 0.35, TIN),
        "Adom.LaserEtch": ("Adom.LaserEtch (virtual MPN)", 0.0, 0.7, None)}
src = sys.argv[1]; out = sys.argv[2] if len(sys.argv) > 2 else src
g = GLTF2().load(src); seen = {}
for n in g.nodes:
    if n.mesh is None: continue
    key = next((k for k in SPEC if (n.name or "").startswith(k)), None)
    if key is None: continue
    for p in g.meshes[n.mesh].primitives:
        if p.material is None: continue
        if seen.get(p.material, key) != key: sys.exit(f"ERROR: material {p.material} shared by {seen[p.material]} and {key}")
        seen[p.material] = key
        name, metal, rough, col = SPEC[key]; m = g.materials[p.material]
        pbr = m.pbrMetallicRoughness or PbrMetallicRoughness()
        pbr.metallicFactor, pbr.roughnessFactor = metal, rough
        if col: pbr.baseColorFactor = col
        m.pbrMetallicRoughness = pbr; m.name = name
missing = [k for k in ("body", "pins") if k not in seen.values()]
if missing: sys.exit(f"ERROR: no {missing} part in {src}")
g.save(out); print("OK", out, sorted(set(seen.values())))