import bpy, sys, math
from collections import defaultdict
a=sys.argv[sys.argv.index("--")+1:]
bpy.ops.wm.read_factory_settings(use_empty=True); bpy.ops.import_scene.gltf(filepath=a[0])
o=[x for x in bpy.context.scene.objects if x.type=="MESH"][0]; mw=o.matrix_world
vs=[mw@v.co for v in o.data.vertices]
z0=min(p.z for p in vs); print("ZMIN %.3f ZMAX %.3f"%(z0,max(p.z for p in vs)))
# leg tips: vertices near the ground, clustered on a coarse grid (union of touching cells)
low=[p for p in vs if p.z<z0+0.08]
cell=0.06; grid=defaultdict(list)
for p in low: grid[(int(math.floor(p.x/cell)),int(math.floor(p.y/cell)))].append(p)
seen=set(); clusters=[]
for k in grid:
    if k in seen: continue
    st=[k]; seen.add(k); pts=[]
    while st:
        c=st.pop(); pts+=grid[c]
        for dx in (-1,0,1):
            for dy in (-1,0,1):
                n=(c[0]+dx,c[1]+dy)
                if n in grid and n not in seen: seen.add(n); st.append(n)
    clusters.append(pts)
clusters.sort(key=lambda c:-len(c))
for c in clusters[:12]:
    n=len(c); print("TIP n=%4d ctr(%.2f %.2f %.2f)"%(n,sum(p.x for p in c)/n,sum(p.y for p in c)/n,sum(p.z for p in c)/n))
# body (carapace) extents in slices
for z in (-0.5,-0.3,-0.1,0.1,0.3):
    s=[p for p in vs if abs(p.z-z)<0.03 and abs(p.x)<0.5]
    if s: print("BODY z=%.1f x %.2f..%.2f y %.2f..%.2f n=%d"%(z,min(p.x for p in s),max(p.x for p in s),min(p.y for p in s),max(p.y for p in s),len(s)))
for sg in (1,-1):
    s=[p for p in vs if sg*p.x>0.55 and p.z>-0.3]
    n=len(s); print("CLAW %s n=%d ctr(%.2f %.2f %.2f) z %.2f..%.2f y %.2f..%.2f"%("L" if sg>0 else "R",n,sum(p.x for p in s)/n,sum(p.y for p in s)/n,sum(p.z for p in s)/n,min(p.z for p in s),max(p.z for p in s),min(p.y for p in s),max(p.y for p in s)))
