import bpy, mathutils, os, json, hashlib

base = r"C:\q2\pkg"
res = {"blender": bpy.app.version_string}

def bbox(objs):
    pts = [o.matrix_world @ mathutils.Vector(c) for o in objs for c in o.bound_box]
    if not pts:
        return None
    lo = [min(p[i] for p in pts) for i in range(3)]
    hi = [max(p[i] for p in pts) for i in range(3)]
    return {"min": [round(x, 3) for x in lo], "max": [round(x, 3) for x in hi], "size": [round(h - l, 3) for l, h in zip(lo, hi)]}

def run(sub, stem):
    out = {}
    for role in ("BLOCKOUT", "ANNOTATIONS"):
        f = os.path.join(base, sub, f"{stem}_{role}.glb")
        bpy.ops.wm.read_factory_settings(use_empty=True)
        bpy.ops.import_scene.gltf(filepath=f)
        ms = [o for o in bpy.data.objects if o.type == 'MESH']
        lsc = {}
        for o in bpy.data.objects:
            ks = [k for k in o.keys() if k.startswith("lsc_")]
            if ks:
                lsc[o.name] = {k: str(o[k])[:50] for k in ks}
        out[role] = {
            "sha256": hashlib.sha256(open(f, "rb").read()).hexdigest()[:16],
            "mesh_objects": len(ms),
            "bbox_blender_ZUp_m": bbox(ms),
            "objects_with_lsc_props": len(lsc),
            "lsc_sample": dict(list(lsc.items())[:2]),
            "names": sorted(o.name for o in ms)[:45],
        }
    return out

res["fixture"] = run("01_orientation_fixture", "TESTONLY_TEST-FIXTURE-ORIENT-01_F01")
res["r02_sample"] = run("02_r02_sample", "TESTONLY_TEST-SAMPLE-CK-CONNECTED-S01_S02-R02-MAP")
json.dump(res, open(r"C:\q2\out\blender_import.json", "w"), indent=1)
print("DONE")
