Exemplo n.º 1
0
    def _get_object_properties(self):
        """
        Helper function to extract relevant object properties (bodies, joints, contact/visual geoms, etc...) from this
        object's XML tree. Assumes the self._obj attribute has already been filled.
        """
        # Parse element tree to get all relevant bodies, joints, actuators, and geom groups
        _elements = sort_elements(root=self.get_obj())
        assert (
            len(_elements["root_body"]) == 1
        ), "Invalid number of root bodies found for robot model. Expected 1," "got {}".format(
            len(_elements["root_body"])
        )
        _elements["root_body"] = _elements["root_body"][0]
        _elements["bodies"] = (
            [_elements["root_body"]] + _elements["bodies"] if "bodies" in _elements else [_elements["root_body"]]
        )
        self._root_body = _elements["root_body"].get("name")
        self._bodies = [e.get("name") for e in _elements.get("bodies", [])]
        self._joints = [e.get("name") for e in _elements.get("joints", [])]
        self._actuators = [e.get("name") for e in _elements.get("actuators", [])]
        self._sites = [e.get("name") for e in _elements.get("sites", [])]
        self._sensors = [e.get("name") for e in _elements.get("sensors", [])]
        self._contact_geoms = [e.get("name") for e in _elements.get("contact_geoms", [])]
        self._visual_geoms = [e.get("name") for e in _elements.get("visual_geoms", [])]

        # Add default materials if we're using domain randomization
        if macros.USING_INSTANCE_RANDOMIZATION:
            tex_element, mat_element, _, used = add_material(root=self.get_obj(), naming_prefix=self.naming_prefix)
            # Only add the material / texture if they were actually used
            if used:
                self.asset.append(tex_element)
                self.asset.append(mat_element)

        # Add prefix to all elements
        add_prefix(root=self.get_obj(), prefix=self.naming_prefix, exclude=self.exclude_from_prefixing)
Exemplo n.º 2
0
    def __init__(self, fname, idn=0):
        super().__init__(fname)

        # Set id and add prefixes to all body names to prevent naming clashes
        self.idn = idn

        # Define other variables that get filled later
        self.mount = None

        # Parse element tree to get all relevant bodies, joints, actuators, and geom groups
        self._elements = sort_elements(root=self.root)
        assert len(self._elements["root_body"]) == 1, "Invalid number of root bodies found for robot model. Expected 1," \
                                                      "got {}".format(len(self._elements["root_body"]))
        self._elements["root_body"] = self._elements["root_body"][0]
        self._elements["bodies"] = [self._elements["root_body"]] + self._elements["bodies"] if \
            "bodies" in self._elements else [self._elements["root_body"]]
        self._root_body = self._elements["root_body"].get("name")
        self._bodies = [
            e.get("name") for e in self._elements.get("bodies", [])
        ]
        self._joints = [
            e.get("name") for e in self._elements.get("joints", [])
        ]
        self._actuators = [
            e.get("name") for e in self._elements.get("actuators", [])
        ]
        self._sites = [e.get("name") for e in self._elements.get("sites", [])]
        self._sensors = [
            e.get("name") for e in self._elements.get("sensors", [])
        ]
        self._contact_geoms = [
            e.get("name") for e in self._elements.get("contact_geoms", [])
        ]
        self._visual_geoms = [
            e.get("name") for e in self._elements.get("visual_geoms", [])
        ]
        self._base_offset = string_to_array(self._elements["root_body"].get(
            "pos", "0 0 0"))

        # Update all xml element prefixes
        add_prefix(root=self.root,
                   prefix=self.naming_prefix,
                   exclude=self.exclude_from_prefixing)

        # Recolor all collision geoms appropriately
        recolor_collision_geoms(root=self.worldbody,
                                rgba=self.contact_geom_rgba)

        # Add default materials
        if macros.USING_INSTANCE_RANDOMIZATION:
            tex_element, mat_element, _, used = add_material(
                root=self.worldbody, naming_prefix=self.naming_prefix)
            # Only add if material / texture was actually used
            if used:
                self.asset.append(tex_element)
                self.asset.append(mat_element)
Exemplo n.º 3
0
    def __init__(self, fname, idn=0):
        super().__init__(fname)

        # Set id and add prefixes to all body names to prevent naming clashes
        self.idn = idn

        # Define other variables that get filled later
        self.mount = None

        # Define filter method to automatically add a default name to visual / collision geoms if encountered
        group_mapping = {
            None: "col",
            "0": "col",
            "1": "vis",
        }
        ctr_mapping = {
            "col": 0,
            "vis": 0,
        }

        def _add_default_name_filter(element, parent):
            # Run default filter
            filter_key = _element_filter(element=element, parent=parent)
            # Also additionally modify element if it is (a) a geom and (b) has no name
            if element.tag == "geom" and element.get("name") is None:
                group = group_mapping[element.get("group")]
                element.set("name", f"g{ctr_mapping[group]}_{group}")
                ctr_mapping[group] += 1
            # Return default filter key
            return filter_key

        # Parse element tree to get all relevant bodies, joints, actuators, and geom groups
        self._elements = sort_elements(root=self.root,
                                       element_filter=_add_default_name_filter)
        assert len(self._elements["root_body"]) == 1, "Invalid number of root bodies found for robot model. Expected 1," \
                                                      "got {}".format(len(self._elements["root_body"]))
        self._elements["root_body"] = self._elements["root_body"][0]
        self._elements["bodies"] = [self._elements["root_body"]] + self._elements["bodies"] if \
            "bodies" in self._elements else [self._elements["root_body"]]
        self._root_body = self._elements["root_body"].get("name")
        self._bodies = [
            e.get("name") for e in self._elements.get("bodies", [])
        ]
        self._joints = [
            e.get("name") for e in self._elements.get("joints", [])
        ]
        self._actuators = [
            e.get("name") for e in self._elements.get("actuators", [])
        ]
        self._sites = [e.get("name") for e in self._elements.get("sites", [])]
        self._sensors = [
            e.get("name") for e in self._elements.get("sensors", [])
        ]
        self._contact_geoms = [
            e.get("name") for e in self._elements.get("contact_geoms", [])
        ]
        self._visual_geoms = [
            e.get("name") for e in self._elements.get("visual_geoms", [])
        ]
        self._base_offset = string_to_array(self._elements["root_body"].get(
            "pos", "0 0 0"))

        # Update all xml element prefixes
        add_prefix(root=self.root,
                   prefix=self.naming_prefix,
                   exclude=self.exclude_from_prefixing)

        # Recolor all collision geoms appropriately
        recolor_collision_geoms(root=self.worldbody,
                                rgba=self.contact_geom_rgba)

        # Add default materials
        if macros.USING_INSTANCE_RANDOMIZATION:
            tex_element, mat_element, _, used = add_material(
                root=self.worldbody, naming_prefix=self.naming_prefix)
            # Only add if material / texture was actually used
            if used:
                self.asset.append(tex_element)
                self.asset.append(mat_element)