| """ |
| Render the N largest assets from a scene layout (default N=2). |
| """ |
|
|
| import os |
| import json |
| import argparse |
| import subprocess |
| import sys |
| import tempfile |
| from pathlib import Path |
| from typing import List, Dict, Any |
|
|
| |
| SCRIPT_DIR = Path(__file__).parent |
| REPO_ROOT = SCRIPT_DIR.parent.parent |
| sys.path.insert(0, str(REPO_ROOT)) |
|
|
|
|
| def get_asset_volume(asset: Dict) -> float: |
| """Calculate the volume of an asset from its size.""" |
| size = asset.get('size', [1, 1, 1]) |
| if len(size) >= 3: |
| return size[0] * size[1] * size[2] |
| return 0.0 |
|
|
|
|
| def get_all_assets(layout: Dict) -> List[Dict]: |
| """Extract all assets from layout (supports both flat and zone-based formats).""" |
| all_assets = [] |
| |
| |
| if 'assets' in layout: |
| all_assets.extend(layout['assets']) |
| |
| |
| if 'functional_zones' in layout: |
| for zone in layout['functional_zones']: |
| if 'assets' in zone: |
| all_assets.extend(zone['assets']) |
| |
| return all_assets |
|
|
|
|
| def find_top_n_largest(layout: Dict, n: int = 2) -> List[Dict]: |
| """Find the N largest assets by volume.""" |
| all_assets = get_all_assets(layout) |
| |
| |
| assets_with_volume = [] |
| for asset in all_assets: |
| volume = get_asset_volume(asset) |
| assets_with_volume.append({ |
| 'asset': asset, |
| 'volume': volume |
| }) |
| |
| |
| assets_with_volume.sort(key=lambda x: x['volume'], reverse=True) |
| |
| |
| return [item['asset'] for item in assets_with_volume[:n]] |
|
|
|
|
| def compose_assets_scene(assets: List[Dict], output_path: str): |
| """Compose a scene with only the specified assets.""" |
| from tools.data_gen.compose_generated import compose_scene |
| |
| |
| layout = { |
| "meta": {"scene_type": "asset_vis"}, |
| "architecture": {"boundary_polygon": []}, |
| "assets": assets |
| } |
| |
| compose_scene( |
| layout=layout, |
| output_path=output_path, |
| add_floor=False, |
| add_walls=False, |
| add_ceiling=False, |
| ) |
| print(f" Composed scene: {output_path}") |
|
|
|
|
| def run_command(cmd, env=None, quiet=False): |
| """Run a shell command.""" |
| print(f" Running: {cmd[:100]}...") |
| try: |
| if quiet: |
| subprocess.check_call(cmd, shell=True, env=env, |
| stdout=subprocess.DEVNULL, |
| stderr=subprocess.DEVNULL) |
| else: |
| subprocess.check_call(cmd, shell=True, env=env) |
| return True |
| except subprocess.CalledProcessError as e: |
| print(f" Command failed: {e}") |
| return False |
|
|
|
|
| def render_scene(glb_path: str, output_path: str, view_mode: str = "diagonal", |
| width: int = 1200, height: int = 900): |
| """Render a GLB scene using Blender.""" |
| current_env = os.environ.copy() |
| |
| cmd = ( |
| f"conda run -n blender python InternScenes/InternScenes_Real2Sim/blender_renderer.py " |
| f"--input '{glb_path}' " |
| f"--output '{output_path}' " |
| f"--width {width} --height {height} " |
| f"--engine BLENDER_EEVEE --samples 64 " |
| f"--view-mode {view_mode} --auto-crop " |
| f"--floor-material solid " |
| f"--diagonal-distance 3.0 " |
| f"--camera-factor 2.0 " |
| ) |
| |
| run_command(cmd, env=current_env, quiet=True) |
|
|
|
|
| def render_top_n_largest( |
| layout_path: str, |
| output_dir: str, |
| n: int = 2, |
| render_views: List[str] = None, |
| ): |
| """Main function to render the N largest assets from a layout.""" |
| if render_views is None: |
| render_views = ["side_front"] |
| |
| print(f"\n{'='*60}") |
| print(f"Render Top {n} Largest Assets") |
| print(f"{'='*60}") |
| print(f"Input: {layout_path}") |
| print(f"Output: {output_dir}") |
| print(f"{'='*60}\n") |
| |
| |
| with open(layout_path, 'r') as f: |
| layout = json.load(f) |
| |
| |
| os.makedirs(output_dir, exist_ok=True) |
| |
| |
| print(f"Finding the {n} largest assets by volume...") |
| top_assets = find_top_n_largest(layout, n=n) |
| |
| if len(top_assets) < n: |
| print(f"Warning: Only found {len(top_assets)} asset(s)!") |
| |
| print(f"\nTop {n} largest assets:") |
| for i, asset in enumerate(top_assets): |
| volume = get_asset_volume(asset) |
| category = asset.get('category', 'unknown') |
| size = asset.get('size', [0, 0, 0]) |
| model_uid = asset.get('model_uid', asset.get('uid', 'unknown'))[:40] |
| print(f" {i+1}. {category}") |
| print(f" Size: {size[0]:.2f} x {size[1]:.2f} x {size[2]:.2f}") |
| print(f" Volume: {volume:.3f}") |
| print(f" Model: {model_uid}...") |
| |
| if not top_assets: |
| print("No assets found in this layout!") |
| return |
| |
| print(f"\nRendering {len(top_assets)} asset(s) separately...") |
| |
| |
| with tempfile.TemporaryDirectory() as tmpdir: |
| |
| for idx, asset in enumerate(top_assets): |
| category = asset.get('category', 'unknown') |
| print(f"\n--- Rendering asset {idx+1}: {category} ---") |
| |
| |
| scene_glb = os.path.join(tmpdir, f"asset_{idx+1}.glb") |
| compose_assets_scene([asset], scene_glb) |
| |
| |
| for view in render_views: |
| print(f" Rendering {view} view...") |
| output_img = os.path.join(output_dir, f"asset{idx+1}_{category}_{view}.png") |
| render_scene(scene_glb, output_img, view_mode=view) |
| |
| if os.path.exists(output_img): |
| print(f" Saved: {output_img}") |
| |
| |
| info_path = os.path.join(output_dir, "asset_info.json") |
| info = { |
| 'top_assets': [ |
| { |
| 'category': a.get('category'), |
| 'model_uid': a.get('model_uid', a.get('uid')), |
| 'size': a.get('size'), |
| 'volume': get_asset_volume(a), |
| 'pos': a.get('pos'), |
| } |
| for a in top_assets |
| ] |
| } |
| with open(info_path, 'w') as f: |
| json.dump(info, f, indent=2) |
| |
| print(f"\n{'='*60}") |
| print(f"Done! Outputs saved to {output_dir}") |
| print(f"{'='*60}\n") |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description='Render the N largest assets from a scene layout') |
| parser.add_argument('--input', default="/home/v-meiszhang/amlt-project/InternScenes/test_vis_output2/scannet_scene0368_01_coarse/layout_retrieved.json", help='Path to layout JSON file') |
| parser.add_argument('--output', default='top_n_output', help='Output directory') |
| parser.add_argument('-n', '--num', type=int, default=48, help='Number of assets to render (default: 2)') |
| parser.add_argument('--all-views', default=True, help='Render from all standard views') |
| |
| args = parser.parse_args() |
| |
| views = ["side_front"] |
| if args.all_views: |
| views = ["side_front", "diagonal", "diagonal2", "topdown"] |
| |
| render_top_n_largest( |
| layout_path=args.input, |
| output_dir=args.output, |
| n=args.num, |
| render_views=views, |
| ) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|