import curation_paths as paths import sqlite3,json,os,sys,math,collections as C sys.path.insert(0,paths.AUDIT_SCRIPTS); paths.add_usd_site() import numpy as np from multiprocessing import Pool from pxr import Usd,UsdGeom R=paths.R; A=paths.A db=sqlite3.connect("file:"+paths.L+"/full_matrix_reviewed_compatibility_v5.sqlite3?mode=ro",uri=True) todo=[] for aid,cat,src,rj in db.execute("select asset_id,normalized_category,source,record_json from assets where asset_kind='OBJECT'"): r=json.loads(rj); st=(r.get("annotation_status") or {}).get("dimensions"); d=r.get("dimensions_m") if st in("unknown","conflict") or not d or min(d)<=0: p=r.get("canonical_path") or "" if os.path.exists(p): todo.append((aid,cat,src.lower(),p,d,(r.get("units_and_up_axis") or {}).get("up_axis"))) print("to measure",len(todo),flush=True) def usd_ext(p): st=Usd.Stage.Open(p); mpu=UsdGeom.GetStageMetersPerUnit(st) or 1.0; up=UsdGeom.GetStageUpAxis(st) c=UsdGeom.BBoxCache(Usd.TimeCode.Default(),[UsdGeom.Tokens.default_,UsdGeom.Tokens.render,UsdGeom.Tokens.proxy]) r=c.ComputeWorldBound(st.GetDefaultPrim() or st.GetPseudoRoot()).ComputeAlignedRange(); e=[(r.GetMax()[i]-r.GetMin()[i])*mpu for i in range(3)] return e,up,mpu def work(t): aid,cat,src,p,old,up=t; e=os.path.splitext(p)[1].lower() try: if e in(".usd",".usda",".usdc"): ext,upx,mpu=usd_ext(p); how="usd_bbox_x_metersPerUnit" elif e in(".glb",".gltf"): from glbpts import read; P=read(p); ext=list((P.max(0)-P.min(0))); upx="Y(glTF)"; how="glb_vertex_bbox" ext=[ext[0],ext[2],ext[1]] # gltf Y-up -> canonical X,Y,Z extents (x, -z, y) elif e==".urdf": return dict(asset_id=aid,note="urdf_use_geometry_match") else: return dict(asset_id=aid,note="unsupported:"+e) if not all(math.isfinite(x) and x>0 for x in ext): return dict(asset_id=aid,err="nonpositive") return dict(asset_id=aid,category=cat,source=src,ext_xyz_m=[round(x,4) for x in ext],how=how,declared_up=upx,old=old) except Exception as ex: return dict(asset_id=aid,err=str(ex)[:60]) if __name__=="__main__": with Pool(16) as p: out=p.map(work,[t for t in todo],chunksize=8) json.dump(out,open(A+"/dims_remeasured.json","w")) c=C.Counter(("ok" if "ext_xyz_m" in o else o.get("note") or "err:"+str(o.get("err"))) for o in out); print(dict(c)) ok=[o for o in out if "ext_xyz_m" in o] agree=sum(1 for o in ok if o["old"] and len(o["old"])>=3 and all(abs(a-b)<=.02*max(a,b) for a,b in zip(sorted(o["old"]),sorted(o["ext_xyz_m"])))) print("remeasured",len(ok),"| sorted extents agree with registered (conflict cases) within 2%:",agree) print("by source",dict(C.Counter(o["source"] for o in ok)))