Пример #1
0
    def append_material(self, material):
        """
        Adds a new texture / material combination to the assets subtree of this XML
        Input is expected to be a CustomMaterial object

        See http://www.mujoco.org/book/XMLreference.html#asset for specific details on attributes expected for
        Mujoco texture / material tags, respectively

        Note that the "file" attribute for the "texture" tag should be specified relative to the textures directory
        located in robosuite/models/assets/textures/

        Args:
            material (CustomMaterial): Material to add to this object
        """
        # First check if asset attribute exists; if not, define the asset attribute
        if not hasattr(self, "asset"):
            self.asset = ET.Element("asset")
        # If the material name is not in shared materials, add this to our assets
        if material.name not in self.shared_materials:
            self.asset.append(ET.Element("texture",
                                         attrib=material.tex_attrib))
            self.asset.append(
                ET.Element("material", attrib=material.mat_attrib))
        # Add this material name to shared materials if it should be shared
        if material.shared:
            self.shared_materials.add(material.name)
            self.shared_textures.add(material.tex_attrib["name"])
        # Update prefix for assets
        add_prefix(root=self.asset,
                   prefix=self.naming_prefix,
                   exclude=self.exclude_from_prefixing)
Пример #2
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)
Пример #3
0
 def _get_object_properties(self):
     """
     Extends the base class method to also add prefixes to all bodies in this object
     """
     super()._get_object_properties()
     add_prefix(root=self.root,
                prefix=self.naming_prefix,
                exclude=self.exclude_from_prefixing)
Пример #4
0
 def _get_object_properties(self):
     """
     Extends the superclass method to add prefixes to all assets
     """
     super()._get_object_properties()
     # Add prefix to all assets
     add_prefix(root=self.asset,
                prefix=self.naming_prefix,
                exclude=self.exclude_from_prefixing)
Пример #5
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)
Пример #6
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)