Local preservation snapshot, not a new deployment or simulation acceptance.
111 lines
4.5 KiB
Python
111 lines
4.5 KiB
Python
"""Import the canonical PlayCanvas GLB with NVIDIA's bundled asset converter.
|
|
|
|
Runs inside the Worker SimulationApp, under the episode's GPU reservation.
|
|
The generated USD is a visual reference only, never an inferred physics rig.
|
|
"""
|
|
|
|
import asyncio
|
|
import hashlib
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from local_state import read_json, write_json
|
|
|
|
|
|
def sha256(path):
|
|
with Path(path).open("rb") as stream:
|
|
return hashlib.file_digest(stream, "sha256").hexdigest()
|
|
|
|
|
|
def prepare_visual(app, robot, cache):
|
|
from isaacsim.core.experimental.utils.app import enable_extension
|
|
from pxr import Usd, UsdGeom
|
|
|
|
source = Path(__file__).resolve().parents[2] / robot["visual"]["source"]
|
|
if sha256(source) != robot["visual"]["sha256"]:
|
|
raise ValueError("Rover visual asset identity changed")
|
|
identity = hashlib.sha256((robot["sha256"] + sha256(__file__)).encode()).hexdigest()
|
|
target = Path(cache) / identity
|
|
target.mkdir(parents=True, exist_ok=True)
|
|
asset, receipt_path = target / "rover.usdc", target / "visual.json"
|
|
source_usd = target / "rover-source.usdc"
|
|
receipt = read_json(receipt_path)
|
|
if (
|
|
receipt
|
|
and asset.is_file()
|
|
and source_usd.is_file()
|
|
and sha256(asset) == receipt["usd_sha256"]
|
|
and sha256(source_usd) == receipt["source_usd_sha256"]
|
|
):
|
|
return asset, receipt
|
|
|
|
enable_extension("omni.kit.asset_converter")
|
|
import omni.kit.asset_converter as converter
|
|
|
|
context = converter.AssetConverterContext()
|
|
context.ignore_animations = True # Runtime advances explicit CAD nodes from each side's speed.
|
|
context.ignore_camera = context.ignore_light = True
|
|
context.use_meter_as_world_unit = True
|
|
context.convert_stage_up_y = True
|
|
context.export_preview_surface = True
|
|
context.smooth_normals = False # Retain the CAD export's authored normals.
|
|
temporary = target / "rover.part.usdc"
|
|
task = converter.get_instance().create_converter_task(
|
|
str(source), str(source_usd), None, context
|
|
)
|
|
future = asyncio.ensure_future(task.wait_until_finished())
|
|
deadline = time.monotonic() + 120
|
|
while not future.done():
|
|
if time.monotonic() > deadline:
|
|
future.cancel()
|
|
raise TimeoutError("Rover conversion timed out")
|
|
app.update()
|
|
if not future.result():
|
|
raise RuntimeError("Rover conversion failed: " + str(task.get_error_message()))
|
|
imported = Usd.Stage.Open(str(source_usd))
|
|
if (
|
|
UsdGeom.GetStageUpAxis(imported) != "Y"
|
|
or abs(UsdGeom.GetStageMetersPerUnit(imported) - 1) > 1e-6
|
|
):
|
|
raise ValueError("Converted glTF must retain metre-scale Y-up")
|
|
# Asset Converter's glTF axis flag changes metadata without rotating the
|
|
# vertices. Compose the glTF's known Y-up into Isaac Z-up explicitly.
|
|
stage = Usd.Stage.CreateNew(str(temporary))
|
|
UsdGeom.SetStageUpAxis(stage, UsdGeom.Tokens.z)
|
|
UsdGeom.SetStageMetersPerUnit(stage, 1)
|
|
world = UsdGeom.Xform.Define(stage, "/World")
|
|
stage.SetDefaultPrim(world.GetPrim())
|
|
model = UsdGeom.Xform.Define(stage, "/World/Model")
|
|
model.GetPrim().GetReferences().AddReference(source_usd.name)
|
|
model.AddRotateXOp().Set(90)
|
|
stage.GetRootLayer().Save()
|
|
box = (
|
|
UsdGeom.BBoxCache(Usd.TimeCode.Default(), [UsdGeom.Tokens.default_])
|
|
.ComputeWorldBound(stage.GetDefaultPrim())
|
|
.ComputeAlignedBox()
|
|
)
|
|
size, minimum = list(box.GetSize()), list(box.GetMin())
|
|
if any(abs(a - b) > 0.005 for a, b in zip(size, robot["visual"]["dimensions_m"], strict=True)):
|
|
raise ValueError(f"Converted rover dimensions differ from CAD: {size}")
|
|
if abs(minimum[2]) > 0.005 or any(abs(minimum[i] + size[i] / 2) > 0.005 for i in (0, 1)):
|
|
raise ValueError("Converted rover is not centred with its tracks at ground height")
|
|
receipt = {
|
|
"schema_version": "missioncore.rover-visual/v1",
|
|
"robot_id": robot["robot_id"],
|
|
"profile_sha256": robot["sha256"],
|
|
"source_sha256": robot["visual"]["sha256"],
|
|
"generator_sha256": sha256(__file__),
|
|
"dimensions_m": size,
|
|
"minimum_m": minimum,
|
|
"up_axis": "Z",
|
|
"meters_per_unit": 1,
|
|
"materials": "embedded-gltf-pbr-to-usd-preview-surface",
|
|
"mesh_count": sum(prim.IsA(UsdGeom.Mesh) for prim in stage.Traverse()),
|
|
}
|
|
del stage
|
|
temporary.replace(asset)
|
|
receipt["usd_sha256"] = sha256(asset)
|
|
receipt["source_usd_sha256"] = sha256(source_usd)
|
|
write_json(receipt_path, receipt)
|
|
return asset, receipt
|