예제 #1
0
    def get_sim(self):
        self.placements = OrderedDict()
        self.placements["top"] = {"origin": np.zeros(3),
                                  "size": self.world_params.size}
        name_indexes = OrderedDict()
        self.to_names(name_indexes)
        res = self.compile(self.random_state, world_params=self.world_params)
        if not res:
            raise FullVirtualWorldException('Failed to compile world')
        self.set_absolute_position((0, 0, 0))  # Recursively set all positions
        xml_dict = self.to_xml_dict()
        xinit_dict = self.to_xinit()
        udd_callbacks = self.to_udd_callback()
        xml = unparse_dict(xml_dict)

        model = load_model_from_xml(xml)
        sim = MjSim(model, nsubsteps=self.world_params.num_substeps)
        for name, value in xinit_dict.items():
            sim.data.set_joint_qpos(name, value)
        # Places mocap where related bodies are.
        if sim.model.nmocap > 0 and sim.model.eq_data is not None:
            for i in range(sim.model.eq_data.shape[0]):
                if sim.model.eq_type[i] == const.EQ_WELD:
                    sim.model.eq_data[i, :] = np.array(
                        [0., 0., 0., 1., 0., 0., 0.])
        udd_callbacks = (udd_callbacks or [])
        if udd_callbacks is not None and len(udd_callbacks) > 0:
            def merged_udd_callback(sim):
                ret = {}
                for udd_callback in udd_callbacks:
                    ret.update(udd_callback(sim))
                return ret
            sim.udd_callback = merged_udd_callback
        return sim
예제 #2
0
def load_model_from_path_fix_paths(xml_path, zero_gravity=True):
    """
    Loads model from XML path. Ensures that
    all assets are locally available. If needed might rename
    paths.

    :param xml_path: path to xml file
    :param zero_gravity: if true, zero gravity in model
    """
    xml_dict = parse_file(xml_path, enforce_validation=False)

    if zero_gravity:
        # zero gravity so that the object doesn't fall down
        option = xml_dict.setdefault('option', OrderedDict())
        option['@gravity'] = np.zeros(3)
    xml = unparse_dict(xml_dict)
    model = load_model_from_xml(xml)
    return model