#!/usr/bin/env python3 """Give a clip's materials the same settings its siblings use. fbx_align_material.py --diff ... what differs fbx_align_material.py --from ... align, in place Materials are matched by name, and only the values inside `Properties70` move — the property's own type strings, the shading model, the textures and everything outside the material are left alone. A property the reference does not have is removed; one only the reference has is added. This exists for `KingCobra-Walk.fbx`, whose material block is anomalous in the same way the rest of that file was. It is the only clip in the library that sets `ReflectionFactor`, and Blender reads that property as the Principled `Metallic` input: with it absent the importer defaults to 1.0, so the clip came in at 0.0 while its eleven siblings — and every other clip in the library — came in at 1.0. The visible result was a paler, greyer snake. Four more properties differed alongside it (`Shininess`, `ShininessExponent`, `Specular`, `SpecularColor`), with no effect in Blender but no reason to keep them apart either. Aligning to a sibling rather than writing `ReflectionFactor` into the other 1,096 clips keeps the library uniform without baking one renderer's default for an unspecified property into the data. Correctness gate: with no edits the serializer must reproduce its input byte for byte. Writes through a temporary file and renames it into place. """ import os, sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from fbx_graft_mesh import parse, serialize, objects, clean, sval, Node def props_of(node): p70 = next((ch for ch in node.children if ch.name == b"Properties70"), None) return p70, {} if p70 is None else { clean(pn.props[0][1]): pn for pn in p70.children if pn.props} def materials(tree): return {clean(n): node for u, (c, n, s, node) in objects(tree).items() if c == b"Material"} def value(pn): return tuple(round(v, 6) if isinstance(v, float) else v for v in (sval(x) for x in pn.props[4:])) def diff(ref_path, path): ref, cur = materials(parse(ref_path)), materials(parse(path)) out = [] for name, node in sorted(cur.items()): if name not in ref: continue _, a = props_of(node) _, b = props_of(ref[name]) for k in sorted(set(a) | set(b)): va = value(a[k]) if k in a else None vb = value(b[k]) if k in b else None if va != vb: out.append((name, k, va, vb)) return out def align(ref_path, path): t = parse(path) ref = materials(parse(ref_path)) changed = [] for name, node in materials(t).items(): if name not in ref: continue p70, a = props_of(node) _, b = props_of(ref[name]) if p70 is None: continue for k in sorted(set(a) | set(b)): va = value(a[k]) if k in a else None vb = value(b[k]) if k in b else None if va == vb: continue if k not in b: p70.children.remove(a[k]); changed.append((name, k, va, "删除")) elif k not in a: cp = Node(b"P"); cp.props = list(b[k].props) p70.children.append(cp); changed.append((name, k, "新增", vb)) else: # keep this file's own type strings, take the reference's values a[k].props = a[k].props[:4] + list(b[k].props[4:]) changed.append((name, k, va, vb)) data = serialize(t) tmp = path + ".tmp" with open(tmp, "wb") as fh: fh.write(data) os.replace(tmp, path) return changed, len(data) def main(argv): if len(argv) < 3 or argv[0] not in ("--diff", "--from"): print(__doc__); return 2 mode, ref, targets = argv[0], argv[1], argv[2:] for p in targets: if serialize(parse(p)) != open(p, "rb").read(): print("拒绝执行:%s 无法字节级往返" % p); return 1 if mode == "--diff": d = diff(ref, p) print("%s vs %s" % (os.path.basename(p), os.path.basename(ref))) for name, k, va, vb in d: print(" %-34s %-20s %-22s → %s" % (name, k, va, vb)) if not d: print(" 材质属性已一致") else: before = os.path.getsize(p) ch, size = align(ref, p) print("%s ← %s" % (os.path.basename(p), os.path.basename(ref))) for name, k, va, vb in ch: print(" %-20s %-22s → %s" % (k, str(va)[:22], vb)) print(" %d → %d 字节" % (before, size)) return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))