Datasets:
Download code/robohome/src/engine/source/packages/asset_registry/parametric.py from RoboParty/RoboHome: direct link, hf CLI and curl.
- Browser
- Download file 7.82 kB
-
https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/engine/source/packages/asset_registry/parametric.py
- Command line
-
hf download hf://datasets/RoboParty/RoboHome/code/robohome/src/engine/source/packages/asset_registry/parametric.py
-
curl -L -o parametric.py https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/engine/source/packages/asset_registry/parametric.py
7.82 kB
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| import os | |
| import re | |
| from pathlib import Path | |
| from typing import Any | |
| import yaml # type: ignore[import-untyped] | |
| from pydantic import BaseModel, ConfigDict, Field, model_validator | |
| from packages.asset_registry.models import ( | |
| AssetFile, | |
| AssetRecord, | |
| DimensionSource, | |
| GeometryMetadata, | |
| MountType, | |
| PlacementMetadata, | |
| RuntimeCompatibility, | |
| ) | |
| from packages.asset_registry.proxy_sdf import render_proxy_sdf | |
| class ParametricMaterial(BaseModel): | |
| model_config = ConfigDict(extra="forbid") | |
| base_color_texture: str | None = None | |
| roughness_texture: str | None = None | |
| normal_texture: str | None = None | |
| base_color: tuple[float, float, float, float] = (0.5, 0.5, 0.5, 1.0) | |
| roughness: float = Field(default=0.45, ge=0.0, le=1.0) | |
| metallic: float = Field(default=0.0, ge=0.0, le=1.0) | |
| texture_scale: tuple[float, float] = (1.0, 1.0) | |
| class ParametricAssetRecipe(BaseModel): | |
| model_config = ConfigDict(extra="forbid") | |
| asset_id: str | |
| display_name: str | |
| description: str | |
| category: str | |
| tags: list[str] | |
| synonyms: list[str] = Field(default_factory=list) | |
| dimensions_m: tuple[float, float, float] | |
| geometry_style: str = "slab" | |
| module_count: int = Field(default=1, ge=1, le=16) | |
| mount: MountType | |
| support_surfaces: list[str] = Field(default_factory=list) | |
| bevel_m: float = Field(default=0.004, ge=0.0) | |
| material: ParametricMaterial | |
| source_url: str | |
| source_license: str = "CC0-1.0" | |
| def validate_geometry(self) -> ParametricAssetRecipe: | |
| if any(value <= 0.0 for value in self.dimensions_m): | |
| raise ValueError("parametric asset dimensions must be positive") | |
| if self.bevel_m * 2.0 >= min(self.dimensions_m): | |
| raise ValueError("bevel_m must be smaller than half the shortest dimension") | |
| return self | |
| class ParametricAssetLibrary(BaseModel): | |
| model_config = ConfigDict(extra="forbid") | |
| schema_version: str = "1.0" | |
| output_root: str | |
| assets: list[ParametricAssetRecipe] | |
| def _expand_path(value: str, *, base: Path) -> Path: | |
| expanded = os.path.expandvars(value) | |
| unresolved = re.findall(r"\$\{[^}]+\}|\$[A-Za-z_][A-Za-z0-9_]*", expanded) | |
| if unresolved: | |
| raise ValueError(f"unresolved environment variables in path {value!r}: {unresolved}") | |
| path = Path(expanded).expanduser() | |
| return (path if path.is_absolute() else base / path).resolve() | |
| def load_parametric_asset_library(path: Path) -> ParametricAssetLibrary: | |
| payload = yaml.safe_load(path.read_text(encoding="utf-8")) | |
| return ParametricAssetLibrary.model_validate(payload) | |
| def build_jobs( | |
| library: ParametricAssetLibrary, | |
| *, | |
| recipe_path: Path, | |
| ) -> tuple[Path, list[dict[str, Any]]]: | |
| output_root = _expand_path(library.output_root, base=recipe_path.parent) | |
| jobs: list[dict[str, Any]] = [] | |
| for recipe in library.assets: | |
| safe_id = re.sub(r"[^A-Za-z0-9_.-]+", "_", recipe.asset_id) | |
| asset_root = output_root / safe_id | |
| material = recipe.material.model_dump(mode="json") | |
| for key in ("base_color_texture", "roughness_texture", "normal_texture"): | |
| if material.get(key): | |
| material[key] = str(_expand_path(str(material[key]), base=recipe_path.parent)) | |
| jobs.append( | |
| { | |
| "asset_id": recipe.asset_id, | |
| "dimensions_m": list(recipe.dimensions_m), | |
| "geometry_style": recipe.geometry_style, | |
| "module_count": recipe.module_count, | |
| "bevel_m": recipe.bevel_m, | |
| "material": material, | |
| "canonical_usd": str(asset_root / "asset.usdc"), | |
| "proxy_glb": str(asset_root / "asset.glb"), | |
| } | |
| ) | |
| return output_root, jobs | |
| def _md5(path: Path) -> str: | |
| digest = hashlib.md5(usedforsecurity=False) | |
| with path.open("rb") as handle: | |
| for chunk in iter(lambda: handle.read(4 * 1024 * 1024), b""): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def records_from_outputs( | |
| library: ParametricAssetLibrary, | |
| *, | |
| recipe_path: Path, | |
| jobs: list[dict[str, Any]], | |
| ) -> list[AssetRecord]: | |
| jobs_by_id = {str(job["asset_id"]): job for job in jobs} | |
| records: list[AssetRecord] = [] | |
| for recipe in library.assets: | |
| job = jobs_by_id[recipe.asset_id] | |
| canonical = Path(job["canonical_usd"]) | |
| proxy = Path(job["proxy_glb"]) | |
| for required in (canonical, proxy): | |
| if not required.is_file() or required.stat().st_size == 0: | |
| raise RuntimeError(f"parametric asset output is missing: {required}") | |
| dimensions = recipe.dimensions_m | |
| bbox_min = (-dimensions[0] / 2.0, -dimensions[1] / 2.0, 0.0) | |
| bbox_max = (dimensions[0] / 2.0, dimensions[1] / 2.0, dimensions[2]) | |
| sdf = proxy.with_suffix(".sdf") | |
| sdf.write_text( | |
| render_proxy_sdf( | |
| recipe.asset_id, | |
| proxy.name, | |
| bbox_min, | |
| bbox_max, | |
| articulated=False, | |
| ), | |
| encoding="utf-8", | |
| ) | |
| records.append( | |
| AssetRecord( | |
| asset_id=recipe.asset_id, | |
| scene_name=re.sub(r"[^A-Za-z0-9_]", "_", recipe.asset_id), | |
| display_name=recipe.display_name, | |
| description=recipe.description, | |
| source="RoboHouseGen Parametric", | |
| source_license=recipe.source_license, | |
| category=recipe.category, | |
| tags=recipe.tags, | |
| synonyms=recipe.synonyms, | |
| files=[ | |
| AssetFile( | |
| relative_path=str(canonical.relative_to(canonical.parent.parent)), | |
| source_url=recipe.source_url, | |
| md5=_md5(canonical), | |
| size_bytes=canonical.stat().st_size, | |
| ) | |
| ], | |
| usd_path=str(canonical), | |
| dimensions_m=dimensions, | |
| quality_tier="A", | |
| qa_status="CONVERTED", | |
| normalized_category=recipe.category, | |
| dimension_source=DimensionSource.MEASURED, | |
| dimension_confidence=1.0, | |
| geometry_metadata=GeometryMetadata( | |
| bbox_min_m=bbox_min, | |
| bbox_max_m=bbox_max, | |
| footprint_m=(dimensions[0], dimensions[1]), | |
| ground_offset_m=0.0, | |
| meters_per_unit=1.0, | |
| up_axis="Z", | |
| ), | |
| placement_metadata=PlacementMetadata( | |
| mount=recipe.mount, | |
| support_surfaces=recipe.support_surfaces, | |
| ), | |
| runtime_compatibility=RuntimeCompatibility( | |
| canonical_usd_path=str(canonical), | |
| proxy_gltf_path=str(proxy), | |
| proxy_sdf_path=str(sdf), | |
| compatible_backends=["isaac-sim", "scenesmith"], | |
| articulation=False, | |
| has_collision=False, | |
| ), | |
| ) | |
| ) | |
| return records | |
| def merge_records(existing: list[AssetRecord], generated: list[AssetRecord]) -> list[AssetRecord]: | |
| replacements = {record.asset_id: record for record in generated} | |
| merged = [record for record in existing if record.asset_id not in replacements] | |
| merged.extend(generated) | |
| return sorted(merged, key=lambda record: record.asset_id) | |
| def write_job_manifest(path: Path, jobs: list[dict[str, Any]]) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text(json.dumps(jobs, indent=2) + "\n", encoding="utf-8") | |