RoboHome / code /robohome /src /engine /source /scripts /capture_pool_surfaces.py
RoboParty Release
Unify robohome source, assets and scenes delivery
3f15e9f
Raw History Blame Contribute Delete
3.13 kB
"""Measure native upward triangles for layout from freshly prepared source bytes."""
import argparse
import json
import sqlite3
import sys
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
CODE=ROOT/'scripts/internal/scene_generation/real_assets'
sys.path.insert(0,str(CODE))
def main():
import bpy
import asset_import as adapter
from stage_checkpoint import atomic_json,HashMemo
from layout_surface_feedback import identity
p=argparse.ArgumentParser()
p.add_argument('--pool',type=Path,action='append',required=True)
p.add_argument('--out',type=Path,required=True)
p.add_argument('--shard',type=int,default=0)
p.add_argument('--shards',type=int,default=1)
args=p.parse_args(sys.argv[sys.argv.index('--')+1:])
comp=adapter.load_module('fresh_surface_compositor',CODE.parent/'base/blender_compose_asset_first_scene.py')
rows=[];memo=HashMemo()
for path in args.pool:
receipt=json.loads(path.with_suffix('.receipt.json').read_text())
if memo.digest(path)!=receipt['sha256']:raise ValueError('Pool changed')
with sqlite3.connect(path.resolve().as_uri()+'?mode=ro',uri=True) as db:
rows.extend(json.loads(r[0]) for r in db.execute('SELECT candidate_json FROM candidates ORDER BY candidate_key'))
records={};failures=[]
for index,e in enumerate(rows):
if index%args.shards!=args.shard:continue
roles=set(e.get('catalog_roles',[]))
if not roles.intersection({'kitchen_counter','sink','side_table','dining_table','desk','vanity','coffee_table','media_console','rug'}):continue
try:
bpy.ops.wm.read_factory_settings(use_empty=True)
e=dict(e,entity_id='surface_'+e['pool_candidate_key'],yaw_deg=0,center_xy_m=[0,0],base_z_m=0,uniform_scale=1.)
m=comp._import_entity(e);meshes=[bpy.data.objects[n] for n in m['mesh_object_names']]
triangles=[]
for obj in meshes:
obj.data.calc_loop_triangles()
for tri in obj.data.loop_triangles:
world=[obj.matrix_world@obj.data.vertices[i].co for i in tri.vertices]
normal=(world[1]-world[0]).cross(world[2]-world[0])
if normal.length<1e-12 or normal.normalized().z<.95:continue
triangles.append([list(v) for v in world])
records[e['pool_candidate_key']]={'identity':identity(e),'triangles_xyz_m':triangles,
'upward_planes':adapter.upward_planes(meshes),'source_entity_id':e['entity_id'],
'source_scene_id':'fresh_source_preparation','source_scale':1.,
'method':'actual_imported_upward_triangles_at_identity_pose','pose_modified':False}
except Exception as error:
failures.append({'asset_id':e['asset_id'],'error':repr(error)})
atomic_json(args.out,{'schema':'rhg.layout_surface_feedback.v1','records':records,
'failures':failures,'shared_attribute_records_modified':False})
print(json.dumps({'records':len(records),'failures':failures}),flush=True)
if __name__=='__main__':main()