in robogym/envs/rearrange/common/utils.py [0:0]
def make_openai_block(name: str, object_size: np.ndarray) -> MujocoXML:
""" Creates a block with OPENAI letters on it faces.
:param name: The name of the block
:param object_size: The size of the block (3-dimensional). This is half-size as per Mujoco
convention
"""
default_object_size = 0.0285
default_letter_offset = 0.0009
# scale face meshes properly
scale = object_size / default_object_size
letter_offset = default_letter_offset * scale
def to_str(x: np.ndarray):
return " ".join(map(str, x.tolist()))
face_pos = {
"top": {
"body": to_str(np.array([0, 0, object_size[2]])),
"geom": to_str(np.array([0, 0, -letter_offset[2]])),
},
"bottom": {
"body": to_str(np.array([0, 0, -object_size[2]])),
"geom": to_str(np.array([0, 0, letter_offset[2]])),
},
"back": {
"body": to_str(np.array([0, object_size[1], 0])),
"geom": to_str(np.array([0, -letter_offset[1], 0])),
},
"right": {
"body": to_str(np.array([object_size[0], 0, 0])),
"geom": to_str(np.array([-letter_offset[0], 0, 0])),
},
"front": {
"body": to_str(np.array([0, -object_size[1], 0])),
"geom": to_str(np.array([0, letter_offset[1], 0])),
},
"left": {
"body": to_str(np.array([-object_size[0], 0, 0])),
"geom": to_str(np.array([letter_offset[0], 0, 0])),
},
}
face_euler = {
"top": to_str(np.array([np.pi / 2, 0, np.pi / 2])),
"bottom": to_str(np.array([np.pi / 2, 0, np.pi / 2])),
"back": to_str(np.array([0, 0, np.pi / 2])),
"right": to_str(np.array([0, 0, 0])),
"front": to_str(np.array([0, 0, -np.pi / 2])),
"left": to_str(np.array([0, 0, np.pi])),
}
def face_xml(_name: str, _face: str, _c: str):
xml = f"""
<body name="{_face}:{_name}" pos="{face_pos[_face]['body']}">
<geom name="letter_{_c}:{_name}" mesh="{_name}{_c}" euler="{face_euler[_face]}"
pos="{face_pos[_face]['geom']}" type="mesh" material="{_name}letter"
conaffinity="0" contype="0" />
</body>
"""
return xml
size_string = " ".join(map(str, list(object_size)))
scale_string = " ".join(map(str, list(scale)))
xml_source = f"""
<mujoco>
<asset>
<material name="{name}letter" specular="1" shininess="0.3" rgba="1 1 1 1"/>
<mesh name="{name}O" file="{ASSETS_DIR}/stls/openai_cube/O.stl"
scale="{scale_string}" />
<mesh name="{name}P" file="{ASSETS_DIR}/stls/openai_cube/P.stl"
scale="{scale_string}" />
<mesh name="{name}E" file="{ASSETS_DIR}/stls/openai_cube/E.stl"
scale="{scale_string}" />
<mesh name="{name}N" file="{ASSETS_DIR}/stls/openai_cube/N.stl"
scale="{scale_string}" />
<mesh name="{name}A" file="{ASSETS_DIR}/stls/openai_cube/A.stl"
scale="{scale_string}" />
<mesh name="{name}I" file="{ASSETS_DIR}/stls/openai_cube/I.stl"
scale="{scale_string}" />
</asset>
<worldbody>
<body name="{name}">
<geom name="{name}" size="{size_string}" type="box"
rgba="0.0 0.0 0.0 0.0" material="block_mat"/>
<joint name="{name}:joint" type="free"/>
{face_xml(name, "top", "O")}
{face_xml(name, "bottom", "P")}
{face_xml(name, "back", "E")}
{face_xml(name, "right", "N")}
{face_xml(name, "front", "A")}
{face_xml(name, "left", "I")}
</body>
</worldbody>
</mujoco>
"""
return MujocoXML.from_string(xml_source)