"""Parse a GLB's glTF JSON and print the node parent/child tree (to confirm hierarchy survived export)."""
import struct
import json
import sys

with open(sys.argv[1], "rb") as f:
    data = f.read()
clen = struct.unpack("<I", data[12:16])[0]
gltf = json.loads(data[20:20 + clen].decode("utf-8"))
nodes = gltf["nodes"]
kids = {i: n.get("children", []) for i, n in enumerate(nodes)}
has_parent = {c for cs in kids.values() for c in cs}
roots = [i for i in range(len(nodes)) if i not in has_parent]


def show(i, d=0):
    print("  " * d + "- " + nodes[i].get("name", "?"))
    for c in kids[i]:
        show(c, d + 1)


for r in roots:
    show(r)
