"""Read bedroom relationships from the saved world boxes; no scene edits.""" from pathlib import Path import argparse,json,hashlib,math,sys sys.path.insert(0,str(Path(__file__).parent/'engine/source/scripts/internal/scene_generation/real_assets')) from bedroom_relations import storage_wall def front(e):return e.get('world_front_vector_xy') or e.get('applied_world_front_vector_xy') def back(e): f=front(e) if not f:return None return ('W' if f[0]>0 else 'E') if abs(f[0])>abs(f[1]) else ('S' if f[1]>0 else 'N') def bounds(e): lo,hi=e['world_bbox_m'];return [lo[0],lo[1],hi[0],hi[1]] def role(e): if 'nightstand' in str(e.get('layout_relation','')) or e.get('furnishing_function')=='bedside_surface':return 'nightstand' return e.get('binding_semantic_key') or e.get('category') or e['role'] def edge_gap(b,i,side):return {'W':b[0]-i[0],'E':i[2]-b[2],'S':b[1]-i[1],'N':i[3]-b[3]}[side] def audit(m): plan=m['floor_plan'];half=plan.get('wall_thickness_m',.2)/2;out=[] for room in plan['rooms']: if 'bedroom' not in room['room_type']:continue rid=room['room_id'];rows=[e for e in m['entities'] if e.get('room_id')==rid and e.get('world_bbox_m')] beds=[e for e in rows if role(e)=='bed'];cabs=[e for e in rows if role(e)=='wardrobe'];nights=[e for e in rows if role(e)=='nightstand'];desks=[e for e in rows if role(e)=='desk'] r=dict(room_id=rid,complete_required_items=len(beds)==1 and bool(cabs),bed_count=len(beds),wardrobe_count=len(cabs),nightstand_count=len(nights)) rb=room['bounds_m'];inner=[rb[0]+half,rb[1]+half,rb[2]-half,rb[3]-half];side=storage_wall(plan,rid,inner);r['wardrobe_target_wall']=side if len(beds)==1: bed=beds[0];bb=bounds(bed);head=back(bed);r.update(bed_head_wall=head,bed_asset_id=bed.get('asset_id'),bed_east_west=head in ('W','E'),bed_world_dimensions_m=[round(bb[2]-bb[0],3),round(bb[3]-bb[1],3)]) side_axis=1 if head in ('W','E') else 0 r['nightstands']=[dict(entity_id=n['entity_id'],asset_id=n.get('asset_id'),gap_m=round(max(bounds(n)[side_axis]-bb[side_axis+2],bb[side_axis]-bounds(n)[side_axis+2]),3)) for n in nights] r['nightstands_match_within_room']=len({n.get('asset_id') for n in nights})<=1 r['desks']=[dict(entity_id=d['entity_id'],back_wall=back(d),opposite_bed_head=back(d)=={'W':'E','E':'W','N':'S','S':'N'}.get(head)) for d in desks] r['wardrobes']=[] for c in cabs: cb=bounds(c);wall=back(c);wallgap=edge_gap(cb,inner,side);headgap=edge_gap(cb,inner,head) if head else None gap=cb[1]-bb[3] if side=='N' else bb[1]-cb[3] r['wardrobes'].append(dict(entity_id=c['entity_id'],asset_id=c.get('asset_id'),back_wall=wall,width_m=round(max(cb[2]-cb[0],cb[3]-cb[1]),3),on_window_opposite_wall=wall==side and abs(wallgap)<=.035,wall_gap_m=round(wallgap,3),head_wall_end_gap_m=round(headgap,3) if headgap is not None else None,bed_side_to_front_m=round(gap,3))) out.append(r) bed_ids=[r.get('bed_asset_id') for r in out];night_sets=[set(n.get('asset_id') for n in r.get('nightstands',[])) for r in out] return dict(schema='rhg.saved-bedroom-contract.v1',scope='saved world bounds and source identities; not mesh/physics qualification',rooms=out,bedroom_count=len(out),distinct_bed_models=len(set(bed_ids)-{None}),nightstand_models_shared_across_rooms=sorted(set.union(*(night_sets[i]&night_sets[j] for i in range(len(night_sets)) for j in range(i))) if len(night_sets)>1 else set()),all_wardrobes_opposite_windows=all(r.get('wardrobes') and all(c['on_window_opposite_wall'] for c in r['wardrobes']) for r in out)) def main(): p=argparse.ArgumentParser();p.add_argument('manifest',type=Path);p.add_argument('--output',type=Path,required=True);a=p.parse_args();raw=a.manifest.read_bytes();d=audit(json.loads(raw));d['manifest_sha256']=hashlib.sha256(raw).hexdigest();a.output.write_text(json.dumps(d,ensure_ascii=False,indent=2)+'\n');print(json.dumps({k:v for k,v in d.items() if k!='rooms'},ensure_ascii=False)) if __name__=='__main__':main()