Esempio n. 1
0
 def testGetAssetFromPlaceholder(self):
   mujoco = parser.from_path(_TEST_MODEL_XML)
   # Add an extra texture asset from a placeholder.
   contents = b'I am a texture bytestring'
   extension = '.png'
   vfs_filename = hashlib.sha1(contents).hexdigest() + extension
   placeholder = mjcf.Asset(contents=contents, extension=extension)
   mujoco.asset.add('texture', name='fake_texture', file=placeholder)
   self.assertDictContainsSubset({vfs_filename: contents}, mujoco.get_assets())
    def testAssetInheritance(self):
        parent = element.RootElement(model='parent')
        child = element.RootElement(model='child')
        grandchild = element.RootElement(model='grandchild')

        ext = '.png'
        parent_str = b'I belong to the parent'
        child_str = b'I belong to the child'
        grandchild_str = b'I belong to the grandchild'
        parent_vfs_name, child_vfs_name, grandchild_vfs_name = (
            hashlib.sha1(s).hexdigest() + ext
            for s in (parent_str, child_str, grandchild_str))

        parent_ph = mjcf.Asset(contents=parent_str, extension=ext)
        child_ph = mjcf.Asset(contents=child_str, extension=ext)
        grandchild_ph = mjcf.Asset(contents=grandchild_str, extension=ext)

        parent.asset.add('texture', name='parent_tex', file=parent_ph)
        child.asset.add('texture', name='child_tex', file=child_ph)
        grandchild.asset.add('texture',
                             name='grandchild_tex',
                             file=grandchild_ph)

        parent.attach(child)
        child.attach(grandchild)

        # The grandchild should only return its own assets.
        self.assertDictEqual({grandchild_vfs_name: grandchild_str},
                             grandchild.get_assets())

        # The child should return its own assets plus those of the grandchild.
        self.assertDictEqual(
            {
                child_vfs_name: child_str,
                grandchild_vfs_name: grandchild_str
            }, child.get_assets())

        # The parent should return everything.
        self.assertDictEqual(
            {
                parent_vfs_name: parent_str,
                child_vfs_name: child_str,
                grandchild_vfs_name: grandchild_str
            }, parent.get_assets())
Esempio n. 3
0
def _get_texture(name):
    contents = resources.GetResource(
        os.path.join(_ASSETS_PATH, '{}.png'.format(name)))
    return mjcf.Asset(contents, '.png')
Esempio n. 4
0
    def _build(self,
               name='walker',
               marker_rgba=None,
               camera_control=False,
               roll_gear=-60,
               steer_gear=55,
               walker_id=None,
               initializer=None):
        """Build a BoxHead.

    Args:
      name: name of the walker.
      marker_rgba: RGBA value set to walker.marker_geoms to distinguish between
        walkers (in multi-agent setting).
      camera_control: If `True`, the walker exposes two additional actuated
        degrees of freedom to control the egocentric camera height and tilt.
      roll_gear: gear determining forward acceleration.
      steer_gear: gear determining steering (spinning) torque.
      walker_id: (Optional) An integer in [0-10], this number will be shown on
        the walker's head. Defaults to `None` which does not show any number.
      initializer: (Optional) A `WalkerInitializer` object.

    Raises:
      ValueError: if received invalid walker_id.
    """
        super(BoxHead, self)._build(initializer=initializer)
        xml_path = os.path.join(_ASSETS_PATH, 'boxhead.xml')
        self._mjcf_root = mjcf.from_xml_string(
            resources.GetResource(xml_path, 'r'))
        if name:
            self._mjcf_root.model = name

        if walker_id is not None and not 0 <= walker_id <= _MAX_WALKER_ID:
            raise ValueError(_INVALID_WALKER_ID.format(walker_id))

        self._walker_id = walker_id
        if walker_id is not None:
            png_bytes = _asset_png_with_background_rgba_bytes(
                'digits/%02d.png' % walker_id, marker_rgba)
            head_texture = self._mjcf_root.asset.add('texture',
                                                     name='head_texture',
                                                     type='2d',
                                                     file=mjcf.Asset(
                                                         png_bytes, '.png'))
            head_material = self._mjcf_root.asset.add('material',
                                                      name='head_material',
                                                      texture=head_texture)
            self._mjcf_root.find('geom', 'head').material = head_material
            self._mjcf_root.find('geom', 'head').rgba = None

            self._mjcf_root.find('geom',
                                 'top_down_cam_box').material = head_material
            self._mjcf_root.find('geom', 'top_down_cam_box').rgba = None

        self._body_texture = self._mjcf_root.asset.add(
            'texture',
            name='ball_body',
            type='cube',
            builtin='checker',
            rgb1=marker_rgba[:-1] if marker_rgba else '.4 .4 .4',
            rgb2='.8 .8 .8',
            width='100',
            height='100')
        self._body_material = self._mjcf_root.asset.add(
            'material', name='ball_body', texture=self._body_texture)
        self._mjcf_root.find('geom', 'shell').material = self._body_material

        # Set corresponding marker color if specified.
        if marker_rgba is not None:
            for geom in self.marker_geoms:
                geom.set_attributes(rgba=marker_rgba)

        self._root_joints = None
        self._camera_control = camera_control
        if not camera_control:
            for name in ('camera_pitch', 'camera_yaw'):
                self._mjcf_root.find('actuator', name).remove()
                self._mjcf_root.find('joint', name).remove()
        self._roll_gear = roll_gear
        self._steer_gear = steer_gear
        self._mjcf_root.find('actuator', 'roll').gear[0] = self._roll_gear
        self._mjcf_root.find('actuator', 'steer').gear[0] = self._steer_gear

        # Initialize previous action.
        self._prev_action = np.zeros(shape=self.action_spec.shape,
                                     dtype=self.action_spec.dtype)
Esempio n. 5
0
def _make_goboard(boardsize,
                  square_halfwidth,
                  height=0.01,
                  sensor_size=0.7,
                  name='goboard'):
    """Builds a Go with touch sensors centered on each intersection."""
    y_offset = -0.08
    rows = boardsize
    columns = boardsize
    root = mjcf.RootElement(model=name)
    if _SHOW_DEBUG_GRID:
        black_mat = root.asset.add('material',
                                   name='black',
                                   rgba=(0.2, 0.2, 0.2, 0.5))
        white_mat = root.asset.add('material',
                                   name='white',
                                   rgba=(0.8, 0.8, 0.8, 0.5))
    else:
        transparent_mat = root.asset.add('material',
                                         name='intersection',
                                         rgba=(0, 1, 0, 0.0))

    sensor_mat = root.asset.add('material', name='sensor', rgba=(0, 1, 0, 0.3))

    contents = resources.GetResource(_TEXTURE_PATH)
    root.asset.add('texture',
                   name='goboard',
                   type='2d',
                   file=mjcf.Asset(contents, '.png'))
    board_mat = root.asset.add('material',
                               name='goboard',
                               texture='goboard',
                               texrepeat=[0.97, 0.97])

    root.default.geom.set_attributes(type='box',
                                     size=(square_halfwidth, square_halfwidth,
                                           height))
    root.default.site.set_attributes(
        type='box',
        size=(sensor_size * square_halfwidth, ) * 2 + (0.5 * height, ),
        material=sensor_mat,
        group=composer.SENSOR_SITES_GROUP)

    board_height = height
    if _SHOW_DEBUG_GRID:
        board_height = 0.5 * height

    root.worldbody.add('geom',
                       pos=(0, 0 + y_offset, height),
                       type='box',
                       size=(square_halfwidth * boardsize, ) * 2 +
                       (board_height, ),
                       name=name,
                       material=board_mat)

    xpos = (np.arange(columns) - 0.5 * (columns - 1)) * 2 * square_halfwidth
    ypos = (np.arange(rows) - 0.5 *
            (rows - 1)) * 2 * square_halfwidth + y_offset
    geoms = []
    touch_sensors = []
    for i in range(rows):
        for j in range(columns):
            name = '{}_{}'.format(i, j)
            if _SHOW_DEBUG_GRID:
                transparent_mat = black_mat if ((i % 2) == (j %
                                                            2)) else white_mat
            geoms.append(
                root.worldbody.add('geom',
                                   pos=(xpos[j], ypos[i], height),
                                   name=name,
                                   material=transparent_mat))
            site = root.worldbody.add('site',
                                      pos=(xpos[j], ypos[i], 2 * height),
                                      name=name)
            touch_sensors.append(root.sensor.add('touch', site=site,
                                                 name=name))

    pass_geom = root.worldbody.add(
        'geom',
        pos=(0, y_offset, 0.0),
        size=(square_halfwidth * boardsize * 2, square_halfwidth * boardsize) +
        (0.5 * height, ),
        name='pass',
        material=transparent_mat)
    site = root.worldbody.add(
        'site',
        pos=(0, y_offset, 0.0),
        size=(square_halfwidth * boardsize * 2, square_halfwidth * boardsize) +
        (0.5 * height, ),
        name='pass')
    pass_sensor = root.sensor.add('touch', site=site, name='pass')

    return root, geoms, touch_sensors, pass_geom, pass_sensor