File size: 4,165 Bytes
33c14d6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | """Render the chess scene from every camera to sim/renders/*.png.
Run: .venv/bin/python sim/render_scene.py [--pose home|zero]
"""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import mujoco
from PIL import Image, ImageDraw
HERE = Path(__file__).resolve().parent
OUT = HERE / "renders"
def load(pose: str = "home"):
m = mujoco.MjModel.from_xml_path(str(HERE / "chess_scene.xml"))
d = mujoco.MjData(m)
if pose == "home":
mujoco.mj_resetDataKeyframe(m, d, m.key("home").id)
else:
mujoco.mj_resetData(m, d)
d.ctrl[:] = d.qpos[:m.nu]
# settle so pieces rest on the board
for _ in range(400):
mujoco.mj_step(m, d)
return m, d
def render_all(m, d, w=960, h=720, wrist_w=640, wrist_h=480):
OUT.mkdir(exist_ok=True)
r = mujoco.Renderer(m, height=h, width=w)
rw = mujoco.Renderer(m, height=wrist_h, width=wrist_w)
paths = {}
camera_metadata = {}
opt = mujoco.MjvOption()
opt.geomgroup[3] = 0 # Convex contact proxies must not appear in training RGB/depth.
for cam in ("side", "human", "overhead", "wrist", "board_detail", "scene", "rover"):
rr = rw if cam == "wrist" else r
rr.update_scene(d, camera=cam, scene_option=opt)
img = rr.render()
p = OUT / f"{cam}.png"
Image.fromarray(img).save(p)
paths[cam] = p
cid = m.camera(cam).id
camera_metadata[cam] = dict(
width=rr.width, height=rr.height, vertical_fov_deg=float(m.cam_fovy[cid]),
position_world_m=d.cam_xpos[cid].tolist(),
rotation_camera_to_world=d.cam_xmat[cid].reshape(3, 3).tolist(),
camera_axes="+x right, +y up, -z forward",
simulation_time_s=float(d.time),
)
r.close(); rw.close()
montage = Image.new("RGB", (1280, 960))
draw = ImageDraw.Draw(montage)
for i, cam in enumerate(("side", "human", "overhead", "wrist")):
x, y = (i % 2) * 640, (i // 2) * 480
with Image.open(paths[cam]) as im:
montage.paste(im.resize((640, 480)), (x, y))
draw.rectangle((x + 8, y + 8, x + 145, y + 35), fill="black")
draw.text((x + 16, y + 16), f"camera: {cam}", fill="white")
montage.save(OUT / "all_views.png")
(OUT / "cameras.json").write_text(json.dumps(camera_metadata, indent=2) + "\n")
return paths
def render_piece_lineup():
"""Inspection image using the same meshes, materials and scale as the scene."""
from make_scene import ARM_DIR, PIECES, WHITE_PIECE, chess_assets, lookat_xyaxes, piece_body
kinds = list(PIECES)
bodies = [piece_body(f"sample_{kind}", kind, WHITE_PIECE, 0, (i - 2.5) * 0.032, 0,
"0.7071067812 0 0 0.7071067812" if kind == "king" else "1 0 0 0")
for i, kind in enumerate(kinds)]
xml = f'''<mujoco>
<compiler meshdir="{ARM_DIR}"/>
<visual><global offwidth="1280" offheight="600"/><quality shadowsize="4096"/></visual>
<asset>{chess_assets()}<material name="piece_mat" specular="0.25" shininess="0.35"/></asset>
<worldbody>
<light pos="0.1 -0.1 0.4" diffuse="0.8 0.8 0.8"/>
<geom type="plane" size="1 1 0.01" rgba="0.19 0.22 0.26 1"/>
{''.join(bodies)}
<camera name="lineup" pos="0.25 0 0.13"
xyaxes="{lookat_xyaxes((0.25, 0, 0.13), (0, 0, 0.020))}" fovy="23"/>
</worldbody>
</mujoco>'''
model = mujoco.MjModel.from_xml_string(xml)
data = mujoco.MjData(model)
mujoco.mj_forward(model, data)
opt = mujoco.MjvOption()
opt.geomgroup[3] = 0
with mujoco.Renderer(model, height=600, width=1280) as renderer:
renderer.update_scene(data, camera="lineup", scene_option=opt)
im = Image.fromarray(renderer.render())
im.save(OUT / "piece_lineup.png")
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--pose", default="home", choices=["home", "zero"])
a = ap.parse_args()
m, d = load(a.pose)
paths = render_all(m, d)
render_piece_lineup()
for k, p in paths.items():
print(k, p.relative_to(HERE.parent))
if __name__ == "__main__":
main()
|