Download chess-sim/code/sim/scene_environment.py from Machanize/playful: direct link, hf CLI and curl.
- Browser
- Download file 18.1 kB
-
https://huggingface.co/datasets/Machanize/playful/resolve/main/chess-sim/code/sim/scene_environment.py
- Command line
-
hf download hf://datasets/Machanize/playful/chess-sim/code/sim/scene_environment.py
-
curl -L -o scene_environment.py https://huggingface.co/datasets/Machanize/playful/resolve/main/chess-sim/code/sim/scene_environment.py
18.1 kB
| """A domestic worktable and camera rig, authored in metres for the chess scene. | |
| The tabletop stays at z=0. Small cosmetic details have no contacts; the table, | |
| stand, board and tray have physical support geometry. No training postprocessing. | |
| """ | |
| from __future__ import annotations | |
| import itertools | |
| import math | |
| TABLE_CENTER = (0.18, 0, -0.02) | |
| TABLE_HALF = (0.60, 0.40, 0.02) | |
| FLOOR_Z = -0.74 | |
| OVERHEAD_POS = (0.42, 0, 0.56) | |
| ROVER_CAMERA_POS = (0.105, -0.14, 0.18) | |
| ROVER_CAMERA_FOV = 90 | |
| MAST_XY = (-0.16, 0.23) | |
| # Deck placeholder footprint (x from the arm axis). The front stops at the board's near | |
| # edge: at 0.11 the board border ran 12 mm under the deck, and diagonal grasps on rank 8 | |
| # put a finger over the deck. The real rover body must not reach further forward than | |
| # this below finger height. | |
| DECK_BACK = -0.110 | |
| DECK_FRONT = 0.098 | |
| DECK_HALF_Y = 0.090 | |
| DECK_CX = (DECK_FRONT + DECK_BACK) / 2 | |
| DECK_HX = (DECK_FRONT - DECK_BACK) / 2 | |
| def numbers(values): | |
| return " ".join(f"{x:.8g}" for x in values) | |
| def bevel_asset(name, half, bevel, uv=False): | |
| vertices = [] | |
| for signs in itertools.product((-1, 1), repeat=3): | |
| for axis in range(3): | |
| vertices.extend(signs[i] * (half[i] if i == axis else half[i] - bevel) for i in range(3)) | |
| coords = [] | |
| for i in range(0, len(vertices), 3): | |
| coords.extend((vertices[i] / (2 * half[0]) + 0.5, vertices[i+1] / (2 * half[1]) + 0.5)) | |
| texture = f' texcoord="{numbers(coords)}"' if uv else '' | |
| return f'<mesh name="{name}" vertex="{numbers(vertices)}"{texture}/>' | |
| def box(name, pos, half, material, cosmetic=True, extra=""): | |
| contact = 'contype="0" conaffinity="0" group="1"' if cosmetic else '' | |
| return (f'<geom name="{name}" type="box" pos="{numbers(pos)}" size="{numbers(half)}" ' | |
| f'material="{material}" {contact} {extra}/>') | |
| def rod(name, start, end, radius, material="rubber", cosmetic=True): | |
| contact = 'contype="0" conaffinity="0" group="1"' if cosmetic else '' | |
| return (f'<geom name="{name}" type="capsule" fromto="{numbers((*start, *end))}" ' | |
| f'size="{radius}" material="{material}" {contact}/>') | |
| def mesh_geom(name, mesh, pos, material): | |
| return (f'<geom name="{name}" type="mesh" mesh="{mesh}" pos="{numbers(pos)}" ' | |
| f'material="{material}" group="1" contype="0" conaffinity="0"/>') | |
| def screw(name, x, y, z, radius=0.0022): | |
| return (f'<geom name="{name}" type="cylinder" pos="{x} {y} {z}" size="{radius} 0.0006" ' | |
| 'material="steel" contype="0" conaffinity="0" group="1"/>' | |
| + box(name + "_slot", (x, y, z + 0.00061), (radius * 0.65, radius * 0.13, 0.000025), "rubber")) | |
| def assets(): | |
| return "\n".join([ | |
| bevel_asset("tabletop_mesh", TABLE_HALF, 0.003, uv=True), | |
| bevel_asset("stand_case_mesh", (DECK_HX - 0.002, DECK_HALF_Y - 0.002, 0.024), 0.002), | |
| bevel_asset("stand_plate_mesh", (DECK_HX, DECK_HALF_Y, 0.003), 0.001), | |
| bevel_asset("camera_case_mesh", (0.024, 0.018, 0.011), 0.002), | |
| bevel_asset("power_supply_mesh", (0.038, 0.022, 0.014), 0.002), | |
| '<texture name="oak_tex" type="2d" file="assets/environment/oak_color.png"/>', | |
| '<texture name="pale_wood_tex" type="2d" file="assets/environment/pale_wood_color.png"/>', | |
| '<texture name="dark_wood_tex" type="2d" file="assets/environment/dark_wood_color.png"/>', | |
| '<material name="table_mat" texture="pale_wood_tex" texrepeat="1.5 1" specular="0.12" shininess="0.2" reflectance="0"/>', | |
| '<material name="light_square_mat" texture="pale_wood_tex" texrepeat="0.18 0.18" specular="0.16" shininess="0.3"/>', | |
| '<material name="dark_square_mat" texture="dark_wood_tex" texrepeat="0.18 0.18" specular="0.18" shininess="0.3"/>', | |
| '<material name="border_mat" texture="oak_tex" texrepeat="0.28 0.28" rgba="0.55 0.42 0.31 1" specular="0.16" shininess="0.3"/>', | |
| '<material name="case_mat" rgba="0.095 0.11 0.125 1" specular="0.12" shininess="0.15"/>', | |
| '<material name="anodized" rgba="0.32 0.35 0.38 1" specular="0.35" shininess="0.4"/>', | |
| '<material name="steel" rgba="0.55 0.57 0.59 1" specular="0.65" shininess="0.65"/>', | |
| '<material name="rubber" rgba="0.025 0.030 0.035 1" specular="0.05" shininess="0.05"/>', | |
| '<material name="lens" rgba="0.012 0.023 0.035 1" specular="0.7" shininess="0.8"/>', | |
| '<material name="felt" rgba="0.14 0.20 0.18 1" specular="0.01" shininess="0.05"/>', | |
| '<material name="bin_mat" rgba="0.22 0.24 0.25 1" specular="0.12" shininess="0.2"/>', | |
| '<material name="wall_mat" rgba="0.90 0.89 0.85 1" specular="0"/>', | |
| '<material name="floor_mat" rgba="0.34 0.35 0.34 1" specular="0.03" texuniform="true" texrepeat="3 3"/>', | |
| # Floors the phase 2 randomiser can put under the table (besides the wood textures). | |
| '<texture name="tile_tex" type="2d" builtin="checker" rgb1="0.86 0.86 0.84" rgb2="0.74 0.74 0.72" ' | |
| 'width="512" height="512" mark="edge" markrgb="0.55 0.55 0.53"/>', | |
| '<texture name="carpet_tex" type="2d" builtin="flat" rgb1="0.55 0.55 0.55" width="512" height="512" ' | |
| 'mark="random" random="0.4" markrgb="0.38 0.38 0.38"/>', | |
| '<texture name="concrete_tex" type="2d" builtin="flat" rgb1="0.64 0.64 0.62" width="512" height="512" ' | |
| 'mark="random" random="0.1" markrgb="0.48 0.48 0.47"/>', | |
| # Everyday clutter (colours set per episode). | |
| '<material name="device_mat" rgba="0.55 0.56 0.58 1" specular="0.4" shininess="0.5"/>', | |
| '<material name="device2_mat" rgba="0.08 0.08 0.09 1" specular="0.4" shininess="0.6"/>', | |
| '<material name="mug_mat" rgba="0.85 0.85 0.82 1" specular="0.3" shininess="0.5"/>', | |
| '<material name="notebook_mat" rgba="0.20 0.30 0.55 1" specular="0.05" shininess="0.1"/>', | |
| '<material name="pen_mat" rgba="0.10 0.20 0.70 1" specular="0.3" shininess="0.4"/>', | |
| '<material name="cable_mat_0" rgba="0.03 0.03 0.035 1" specular="0.1" shininess="0.2"/>', | |
| '<material name="cable_mat_1" rgba="0.03 0.03 0.035 1" specular="0.1" shininess="0.2"/>', | |
| '<material name="trim_mat" rgba="0.84 0.84 0.80 1" specular="0.06"/>', | |
| '<material name="inlay_mat" rgba="0.79 0.70 0.51 1" specular="0.1"/>', | |
| ]) | |
| def room(): | |
| # The table is one static body so phase 2 can slide it under the robot and board | |
| # (the board then sometimes sits near an edge or corner, with the floor in view). | |
| table = [ | |
| # Same table contact height as the previous infinite plane, finite footprint. | |
| '<geom name="table" type="box" pos="0.18 0 -0.02" size="0.60 0.40 0.02" ' | |
| 'group="3" rgba="0.2 0.7 0.3 0.35" friction="1 0.005 0.0001"/>', | |
| mesh_geom("table_surface", "tabletop_mesh", TABLE_CENTER, "table_mat"), | |
| box("table_apron_front", (0.18, -0.33, -0.065), (0.54, 0.012, 0.025), "case_mat"), | |
| box("table_apron_back", (0.18, 0.33, -0.065), (0.54, 0.012, 0.025), "case_mat"), | |
| ] | |
| for i, (x, y) in enumerate(itertools.product((-0.34, 0.70), (-0.33, 0.33))): | |
| table.append(box(f"table_leg_{i}", (x, y, -0.385), (0.018, 0.018, 0.345), "case_mat", False)) | |
| table.append(box(f"table_foot_{i}", (x, y, -0.733), (0.019, 0.019, 0.007), "rubber", False)) | |
| geoms = [ | |
| '<body name="table">' + "\n".join(table) + '</body>', | |
| f'<geom name="floor" type="plane" pos="0 0 {FLOOR_Z}" size="2.5 2.5 0.05" material="floor_mat"/>', | |
| box("back_wall", (-0.78, 0.0, 0.5), (0.04, 1.8, 1.24), "wall_mat", False), | |
| box("side_wall", (0.65, 1.2, 0.5), (1.43, 0.04, 1.24), "wall_mat", False), | |
| box("back_skirting", (-0.735, 0, FLOOR_Z + 0.045), (0.006, 1.16, 0.045), "trim_mat"), | |
| box("side_skirting", (0.68, 1.153, FLOOR_Z + 0.045), (1.42, 0.006, 0.045), "trim_mat"), | |
| ] | |
| return "\n".join(geoms) | |
| def stand(deck_height, rover_camera_axes): | |
| if not math.isclose(deck_height, 0.06): | |
| raise ValueError("Stand enclosure is designed for a 60 mm deck; update its geometry when changing height.") | |
| parts = [ | |
| f'<geom name="deck" type="box" size="{DECK_HX} {DECK_HALF_Y} 0.03" pos="{DECK_CX} 0 0.03" group="3" rgba="0.2 0.7 0.3 0.35"/>', | |
| mesh_geom("stand_enclosure", "stand_case_mesh", (DECK_CX, 0, 0.030), "case_mat"), | |
| mesh_geom("stand_top", "stand_plate_mesh", (DECK_CX, 0, 0.057), "anodized"), | |
| box("stand_bottom_seam", (DECK_CX, 0, 0.007), (DECK_HX - 0.003, DECK_HALF_Y - 0.003, 0.001), "rubber"), | |
| ] | |
| feet_x = (DECK_CX - DECK_HX + 0.017, DECK_CX + DECK_HX - 0.017) | |
| for i, (x, y) in enumerate(itertools.product(feet_x, (-0.073, 0.073))): | |
| parts.append(f'<geom name="stand_foot_{i}" type="cylinder" pos="{x} {y} 0.003" size="0.008 0.003" material="rubber" group="1" contype="0" conaffinity="0"/>') | |
| parts.append(screw(f"stand_screw_{i}", x, y, 0.0603)) | |
| for i in range(9): | |
| parts.append(box(f"case_vent_{i}", (-0.060 + 0.014 * i, -0.0881, 0.030), (0.003, 0.00008, 0.009), "rubber")) | |
| parts.append(box("power_socket", (-0.1082, -0.023, 0.027), (0.0002, 0.008, 0.005), "rubber")) | |
| parts.append(rover_camera(rover_camera_axes)) | |
| return '<body name="rover_deck">' + "\n".join(parts) + '</body>' | |
| def rover_camera(xyaxes): | |
| """Self-contained camera stalk, parented to the rover deck rather than the room.""" | |
| rx, ry, rz, ux, uy, uz = map(float, xyaxes.split()) | |
| back = (ry*uz-rz*uy, rz*ux-rx*uz, rx*uy-ry*ux) | |
| housing = tuple(p + 0.026*b for p, b in zip(ROVER_CAMERA_POS, back)) | |
| stalk_tip = (housing[0], housing[1], housing[2] - 0.016) | |
| parts = [ | |
| '<geom name="rover_camera_foot" type="cylinder" pos="0.084 -0.073 0.063" ' | |
| 'size="0.009 0.003" material="anodized"/>', | |
| rod("rover_camera_stalk", (0.084, -0.073, 0.066), stalk_tip, | |
| 0.0045, "anodized", cosmetic=False), | |
| rod("rover_camera_bracket", stalk_tip, housing, | |
| 0.004, "anodized", cosmetic=False), | |
| rod("rover_camera_cable", (0.078, -0.078, 0.061), | |
| (stalk_tip[0] - 0.005, stalk_tip[1] - 0.005, stalk_tip[2]), 0.0015), | |
| rod("rover_camera_cable_top", (stalk_tip[0] - 0.005, stalk_tip[1] - 0.005, stalk_tip[2]), housing, 0.0015), | |
| ] | |
| parts.append(f'''<body name="rover_camera_mount" pos="{numbers(ROVER_CAMERA_POS)}" xyaxes="{xyaxes}"> | |
| {mesh_geom("rover_camera_housing", "camera_case_mesh", (0, 0, 0.026), "case_mat")} | |
| <geom name="rover_camera_housing_collision" type="box" pos="0 0 0.026" | |
| size="0.024 0.018 0.011" group="3" rgba="0.2 0.7 0.3 0.35"/> | |
| <geom name="rover_camera_lens_ring" type="cylinder" pos="0 0 0.011" | |
| size="0.010 0.007" material="rubber"/> | |
| <geom name="rover_camera_lens_glass" type="cylinder" pos="0 0 0.0035" | |
| size="0.007 0.0005" material="lens" contype="0" conaffinity="0" group="1"/> | |
| <camera name="rover" pos="0 0 0" fovy="{ROVER_CAMERA_FOV}"/> | |
| </body>''') | |
| return "\n".join(parts) | |
| def camera_rig(camera_pos, xyaxes): | |
| mx, my = MAST_XY | |
| x, y, z = camera_pos | |
| parts = [ | |
| f'<geom name="mast_base" type="cylinder" pos="{mx} {my} 0.007" size="0.045 0.007" material="case_mat"/>', | |
| box("mast_column", (mx, my, 0.311), (0.010, 0.010, 0.297), "anodized", False), | |
| rod("camera_boom", (mx, my, 0.603), (x, y, 0.603), 0.008, "anodized", False), | |
| rod("camera_drop_bracket", (x, y, 0.603), (x, y, z + 0.025), 0.005, "steel"), | |
| mesh_geom("overhead_housing", "camera_case_mesh", (x, y, z + 0.022), "case_mat"), | |
| f'<geom name="overhead_lens_ring" type="cylinder" pos="{x} {y} {z+0.007}" size="0.010 0.004" material="rubber" contype="0" conaffinity="0" group="1"/>', | |
| f'<geom name="overhead_lens_glass" type="cylinder" pos="{x} {y} {z+0.0025}" size="0.007 0.0004" material="lens" contype="0" conaffinity="0" group="1"/>', | |
| f'<camera name="overhead" pos="{numbers(camera_pos)}" xyaxes="{xyaxes}" fovy="40"/>', | |
| ] | |
| points = [(x + 0.020, y, z + 0.026), (x + 0.03, y, 0.618), (mx, my + 0.013, 0.618), | |
| (mx, my + 0.013, 0.018), (mx - 0.035, my + 0.04, 0.003), (-0.30, 0.29, 0.003)] | |
| parts.extend(rod(f"camera_usb_{i}", a, b, 0.0016) for i, (a, b) in enumerate(zip(points, points[1:]))) | |
| return '<body name="camera_mast">' + "\n".join(parts) + '</body>' | |
| def cables(): | |
| parts = [mesh_geom("power_brick", "power_supply_mesh", (-0.265, -0.12, 0.014), "rubber")] | |
| points = [(-0.108, -0.023, 0.027), (-0.13, -0.025, 0.025), (-0.155, -0.05, 0.003), | |
| (-0.178, -0.1, 0.003), (-0.205, -0.12, 0.007), (-0.227, -0.12, 0.012)] | |
| parts.extend(rod(f"arm_power_{i}", a, b, 0.002) for i, (a, b) in enumerate(zip(points, points[1:]))) | |
| points = [(-0.303, -0.12, 0.012), (-0.34, -0.14, 0.003), (-0.39, -0.15, 0.003), (-0.422, -0.15, -0.01), (-0.44, -0.15, -0.40)] | |
| parts.extend(rod(f"mains_cable_{i}", a, b, 0.0025) for i, (a, b) in enumerate(zip(points, points[1:]))) | |
| return "\n".join(parts) | |
| def board_details(half, border, top): | |
| # Thin contrasting marquetry surrounds the 64 squares, clear of their contacts. | |
| parts = [] | |
| for sign in (-1, 1): | |
| parts.append(box(f"board_frame_x_{sign}", (sign * (half + border/2), 0, top/2), | |
| (border/2, half + border, top/2), "border_mat", False)) | |
| parts.append(box(f"board_frame_y_{sign}", (0, sign * (half + border/2), top/2), | |
| (half, border/2, top/2), "border_mat", False)) | |
| parts.append(box(f"inlay_x_{sign}", (sign * (half + 0.0015), 0, top + 0.000015), | |
| (0.00035, half + 0.00185, 0.000015), "inlay_mat")) | |
| parts.append(box(f"inlay_y_{sign}", (0, sign * (half + 0.0015), top + 0.000015), | |
| (half + 0.00185, 0.00035, 0.000015), "inlay_mat")) | |
| return "\n".join(parts) | |
| def tray(hx=0.045, hy=0.035, rim=0.020): | |
| """Capture tray: 2 mm floor and walls, felt liner, rounded rim at height `rim`. | |
| ChessWorld reads the interior and rim height back from `tray_floor` and `tray_wall_y_1`.""" | |
| parts = [ | |
| f'<geom name="tray_floor" type="box" size="{hx} {hy} 0.002" pos="0 0 0.002" material="bin_mat"/>', | |
| box("tray_felt", (0, 0, 0.00405), (hx - 0.0025, hy - 0.0025, 0.00005), "felt"), | |
| ] | |
| for sign in (-1, 1): | |
| parts.append(f'<geom name="tray_wall_y_{sign}" type="box" size="{hx} 0.002 {rim / 2}" ' | |
| f'pos="0 {sign * hy} {rim / 2}" material="bin_mat"/>') | |
| parts.append(f'<geom name="tray_wall_x_{sign}" type="box" size="0.002 {hy} {rim / 2}" ' | |
| f'pos="{sign * hx} 0 {rim / 2}" material="bin_mat"/>') | |
| parts.append(rod(f"tray_rim_x_{sign}", (-hx, sign * hy, rim), (hx, sign * hy, rim), 0.0012, "bin_mat")) | |
| parts.append(rod(f"tray_rim_y_{sign}", (sign * hx, -hy, rim), (sign * hx, hy, rim), 0.0012, "bin_mat")) | |
| return "\n".join(parts) | |
| CABLES = 2 # randomised cables lying on the table | |
| CABLE_SEGMENTS = 14 | |
| def _hidden(name, kind, material, pos=(0, 0, 0), size=None, extra=""): | |
| size_attr = f' size="{numbers(size)}"' if size is not None else "" | |
| return (f'<geom name="{name}" type="{kind}" pos="{numbers(pos)}"{size_attr} material="{material}" ' | |
| f'contype="0" conaffinity="0" group="5" {extra}/>') | |
| def clutter(): | |
| """Everyday things around the board: a laptop, a phone, a mug, a notebook, a pen and | |
| cables. Visual only, parked out of sight in geom group 5 (not rendered) until the | |
| phase 2 randomiser places a random subset and moves their geoms to group 1.""" | |
| import math as _m | |
| t = _m.radians(20) # laptop screen leans back 20 deg (open 110 deg) | |
| screen_pos = (0, 0.108 + 0.105 * _m.sin(t), 0.016 + 0.105 * _m.cos(t)) | |
| screen_quat = f"{_m.cos(-t / 2):.6f} {_m.sin(-t / 2):.6f} 0 0" | |
| parts = [ | |
| '<body name="clutter_laptop" pos="0 0 -3">', | |
| _hidden("laptop_base", "box", "device_mat", (0, 0, 0.008), (0.155, 0.108, 0.008)), | |
| _hidden("laptop_keys", "box", "rubber", (0, -0.015, 0.0162), (0.135, 0.055, 0.0002)), | |
| _hidden("laptop_lid", "box", "device_mat", screen_pos, (0.155, 0.004, 0.105), f'quat="{screen_quat}"'), | |
| _hidden("laptop_display", "box", "lens", (0, screen_pos[1] - 0.0042 * _m.cos(t), screen_pos[2] + 0.0042 * _m.sin(t)), | |
| (0.145, 0.0002, 0.095), f'quat="{screen_quat}"'), | |
| '</body>', | |
| '<body name="clutter_phone" pos="0 0 -3">', | |
| _hidden("phone_body", "box", "device2_mat", (0, 0, 0.004), (0.074, 0.036, 0.004)), | |
| _hidden("phone_screen", "box", "lens", (0, 0, 0.0081), (0.070, 0.033, 0.0001)), | |
| '</body>', | |
| '<body name="clutter_mug" pos="0 0 -3">', | |
| _hidden("mug_body", "cylinder", "mug_mat", (0, 0, 0.048), (0.040, 0.048)), | |
| _hidden("mug_coffee", "cylinder", "rubber", (0, 0, 0.086), (0.036, 0.0005)), | |
| f'<geom name="mug_handle_0" type="capsule" fromto="0.038 0 0.028 0.062 0 0.028" size="0.006" material="mug_mat" contype="0" conaffinity="0" group="5"/>', | |
| f'<geom name="mug_handle_1" type="capsule" fromto="0.062 0 0.028 0.062 0 0.070" size="0.006" material="mug_mat" contype="0" conaffinity="0" group="5"/>', | |
| f'<geom name="mug_handle_2" type="capsule" fromto="0.062 0 0.070 0.038 0 0.070" size="0.006" material="mug_mat" contype="0" conaffinity="0" group="5"/>', | |
| '</body>', | |
| '<body name="clutter_notebook" pos="0 0 -3">', | |
| _hidden("notebook_cover", "box", "notebook_mat", (0, 0, 0.007), (0.105, 0.075, 0.007)), | |
| _hidden("notebook_pages", "box", "trim_mat", (0.002, 0, 0.007), (0.104, 0.073, 0.0065)), | |
| '</body>', | |
| '<body name="clutter_pen" pos="0 0 -3">', | |
| f'<geom name="pen_body" type="capsule" fromto="-0.07 0 0.005 0.07 0 0.005" size="0.0045" material="pen_mat" contype="0" conaffinity="0" group="5"/>', | |
| '</body>', | |
| ] | |
| for k in range(CABLES): | |
| for i in range(CABLE_SEGMENTS): | |
| parts.append(f'<geom name="cable_{k}_{i}" type="capsule" fromto="0 0 -3 0.01 0 -3" size="0.0025" ' | |
| f'material="cable_mat_{k}" contype="0" conaffinity="0" group="5"/>') | |
| return "\n".join(parts) | |