| |
| """Surgical rename of corrupted Mixamo bone namespaces in binary FBX: |
| 'mixamorig<N>:' -> 'mixamorig:'. |
| |
| Parses the FBX node tree, edits ONLY string properties, re-serializes with |
| recomputed offsets. Mesh, materials, textures, geometry are preserved exactly. |
| |
| Correctness gate: with no edits the output must be BYTE-IDENTICAL to the input |
| (`--selftest`). Some nodes carry a trailing null record despite having no |
| children; that is tracked per-node (`nullrec`) so round-trips are exact. |
| """ |
| import os, re, struct, sys |
|
|
| RENAME = re.compile(rb'mixamorig[0-9]+:') |
| ELEM = {'f':4,'d':8,'l':8,'i':4,'b':1} |
| SCALAR = {"Y":2,"C":1,"I":4,"F":4,"D":8,"L":8} |
|
|
| class Node: |
| __slots__=("name","props","children","nullrec") |
| def __init__(s,name): s.name=name; s.props=[]; s.children=[]; s.nullrec=False |
|
|
| def parse(path): |
| d=open(path,'rb').read() |
| assert d[:21]==b"Kaydara FBX Binary \x00", "not a binary FBX" |
| version=struct.unpack("<I",d[23:27])[0] |
| u64 = version>=7500 |
| NREC = 25 if u64 else 13 |
|
|
| def rd_prop(p): |
| t=chr(d[p]); s=p; p+=1 |
| if t in SCALAR: |
| p+=SCALAR[t]; return p,(t,d[s+1:p]) |
| if t in ELEM: |
| length,enc,clen=struct.unpack("<III",d[p:p+12]); hdr=d[p:p+12]; p+=12 |
| body=clen if enc else length*ELEM[t] |
| data=d[p:p+body]; p+=body |
| return p,(t,hdr+data) |
| if t in "SR": |
| ln=struct.unpack("<I",d[p:p+4])[0]; p+=4 |
| data=d[p:p+ln]; p+=ln |
| return p,(t,data) |
| raise ValueError("bad prop type %r @%d"%(t,s)) |
|
|
| def is_null(p): |
| return p+NREC<=len(d) and d[p:p+NREC]==b"\x00"*NREC |
|
|
| def rd_node(p): |
| if u64: end,nprop,plen=struct.unpack("<QQQ",d[p:p+24]); q=p+24 |
| else: end,nprop,plen=struct.unpack("<III",d[p:p+12]); q=p+12 |
| if end==0 and nprop==0 and plen==0: |
| return None,p+NREC |
| nl=d[q]; q+=1; name=d[q:q+nl]; q+=nl |
| n=Node(name); p=q |
| for _ in range(nprop): |
| p,pr=rd_prop(p); n.props.append(pr) |
| |
| while p < end: |
| if is_null(p): |
| n.nullrec=True; p+=NREC; break |
| c,p2=rd_node(p) |
| if c is None: n.nullrec=True; p=p2; break |
| n.children.append(c); p=p2 |
| return n,end |
|
|
| p=27; top=[]; toplevel_null=False |
| while p < len(d): |
| if is_null(p): |
| toplevel_null=True; p+=NREC; break |
| n,p2=rd_node(p) |
| if n is None: toplevel_null=True; p=p2; break |
| top.append(n); p=p2 |
| return {"header":d[:27],"u64":u64,"top":top,"footer":d[p:], |
| "toplevel_null":toplevel_null,"orig":d,"version":version} |
|
|
| def edit(node,c): |
| for i,(t,pay) in enumerate(node.props): |
| if t in "SR" and RENAME.search(pay): |
| node.props[i]=(t,RENAME.sub(b'mixamorig:',pay)); c[0]+=1 |
| for ch in node.children: edit(ch,c) |
|
|
| def pbytes(t,pay): |
| if t in "SR": return t.encode()+struct.pack("<I",len(pay))+pay |
| return t.encode()+pay |
|
|
| def nsize(n,u64): |
| NREC=25 if u64 else 13 |
| sz=(25 if u64 else 13)+len(n.name) |
| sz+=sum(len(pbytes(t,p)) for t,p in n.props) |
| sz+=sum(nsize(c,u64) for c in n.children) |
| if n.nullrec: sz+=NREC |
| return sz |
|
|
| def wnode(out,n,u64): |
| NREC=25 if u64 else 13 |
| props=b"".join(pbytes(t,p) for t,p in n.props) |
| end=len(out)+nsize(n,u64) |
| out+= struct.pack("<QQQ",end,len(n.props),len(props)) if u64 else \ |
| struct.pack("<III",end,len(n.props),len(props)) |
| out+=bytes([len(n.name)])+n.name+props |
| for c in n.children: wnode(out,c,u64) |
| if n.nullrec: out+=b"\x00"*NREC |
|
|
| def serialize(t): |
| out=bytearray(t["header"]) |
| for n in t["top"]: wnode(out,n,t["u64"]) |
| if t["toplevel_null"]: out+=b"\x00"*(25 if t["u64"] else 13) |
| out+=t["footer"] |
| return bytes(out) |
|
|
| def process(inp,outp,do_edit=True): |
| t=parse(inp); c=[0] |
| if do_edit: |
| for n in t["top"]: edit(n,c) |
| data=serialize(t) |
| if outp: open(outp,"wb").write(data) |
| return c[0],data,t |
|
|
| if __name__=="__main__": |
| a=sys.argv[1:] |
| if a and a[0]=="--selftest": |
| ok=True |
| for f in a[1:]: |
| _,out,t=process(f,None,do_edit=False) |
| same = out==t["orig"] |
| ok &= same |
| print(" %-22s identical=%s (%d bytes)"%(os.path.basename(f),same,len(t["orig"]))) |
| sys.exit(0 if ok else 1) |
| n,data,_=process(a[0],a[1]); print("renamed %d strings -> %s"%(n,a[1])) |
|
|