Files
Warehouse-AMR-Simulation/export_usd.py
T

52 lines
2.1 KiB
Python

from pxr import Usd, UsdGeom, Gf
from fleet_sim import run, make_grid, W, H, CHARGERS, DROP, PICKUPS
res = run(4, "optimized", steps=1200, seed=1, record=True)
COL = {"TO_PICK": (0.1, 0.4, 1.0), "LOADING": (0.6, 0.2, 0.8), "TO_DROP": (0.1, 0.8, 0.2),
"UNLOADING": (0.9, 0.2, 0.2), "TO_CHARGER": (1.0, 0.5, 0.0),
"CHARGING": (1.0, 0.85, 0.0), "IDLE": (0.5, 0.5, 0.5)}
Y = lambda y: H - 1 - y # flip so the 3D layout matches the 2D picture
stage = Usd.Stage.CreateNew("scenes/warehouse.usda")
UsdGeom.SetStageUpAxis(stage, UsdGeom.Tokens.z)
UsdGeom.SetStageMetersPerUnit(stage, 1.0)
stage.SetStartTimeCode(0)
stage.SetEndTimeCode(len(res["frames"]) - 1)
stage.SetTimeCodesPerSecond(10) # plays 10x real time
def box(path, x, y, sx, sy, sz, color):
c = UsdGeom.Cube.Define(stage, path)
c.GetSizeAttr().Set(1.0)
c.AddTranslateOp().Set(Gf.Vec3d(x, Y(y), sz / 2))
c.AddScaleOp().Set(Gf.Vec3f(sx, sy, sz))
c.GetDisplayColorAttr().Set([Gf.Vec3f(*color)])
return c
UsdGeom.Xform.Define(stage, "/World")
box("/World/Floor", W / 2 - 0.5, H / 2 - 0.5, W, H, 0.05, (0.8, 0.8, 0.8))
for y, row in enumerate(make_grid()):
for x, v in enumerate(row):
if v:
box(f"/World/Shelves/S_{x}_{y}", x, y, 0.9, 0.9, 2.0, (0.2, 0.4, 0.9))
for i, (x, y) in enumerate(PICKUPS):
box(f"/World/Pallets/P_{i}", x, y, 0.5, 0.5, 0.3, (0.55, 0.35, 0.15))
box("/World/Dock", DROP[0], DROP[1], 1, 1, 0.1, (0.1, 0.8, 0.2))
for i, (x, y) in enumerate(CHARGERS):
box(f"/World/Charger_{i}", x, y, 1, 1, 0.1, (1.0, 0.8, 0.0))
robots = []
for i in range(len(res["frames"][0])):
c = UsdGeom.Cube.Define(stage, f"/World/Robots/R{i}")
c.GetSizeAttr().Set(1.0)
t_op = c.AddTranslateOp()
c.AddScaleOp().Set(Gf.Vec3f(0.7, 0.7, 0.4))
robots.append((c, t_op))
for t, frame in enumerate(res["frames"]):
for (c, t_op), (pos, state, batt) in zip(robots, frame):
t_op.Set(Gf.Vec3d(pos[0], Y(pos[1]), 0.3), t)
c.GetDisplayColorAttr().Set([Gf.Vec3f(*COL[state])], t)
stage.GetRootLayer().Save()
print("Saved scenes/warehouse.usda")