def _worker_init(mjb_bytes, worker_id, device_ids, shared_rgbs, shared_depths, modder): """ Initializes the global state for the workers. """ s = RenderPoolStorage() with worker_id.get_lock(): proc_worker_id = worker_id.value worker_id.value += 1 s.device_id = device_ids[proc_worker_id % len(device_ids)] s.shared_rgbs_array = np.frombuffer( shared_rgbs.get_obj(), dtype=ctypes.c_uint8) s.shared_depths_array = np.frombuffer( shared_depths.get_obj(), dtype=ctypes.c_float) # avoid a circular import from mujoco_py import load_model_from_mjb, MjRenderContext, MjSim s.sim = MjSim(load_model_from_mjb(mjb_bytes)) # attach a render context to the sim (needs to happen before # modder is called, since it might need to upload textures # to the GPU). MjRenderContext(s.sim, device_id=s.device_id) if modder is not None: s.modder = modder(s.sim, random_state=proc_worker_id) s.modder.whiten_materials() else: s.modder = None global _render_pool_storage _render_pool_storage = s
def _worker_init(mjb_bytes, worker_id, device_ids, shared_rgbs, shared_depths, modder): """ Initializes the global state for the workers. """ s = RenderPoolStorage() with worker_id.get_lock(): proc_worker_id = worker_id.value worker_id.value += 1 s.device_id = device_ids[proc_worker_id % len(device_ids)] s.shared_rgbs_array = np.frombuffer(shared_rgbs.get_obj(), dtype=ctypes.c_uint8) s.shared_depths_array = np.frombuffer(shared_depths.get_obj(), dtype=ctypes.c_float) # avoid a circular import from mujoco_py import load_model_from_mjb, MjRenderContext, MjSim s.sim = MjSim(load_model_from_mjb(mjb_bytes)) # attach a render context to the sim (needs to happen before # modder is called, since it might need to upload textures # to the GPU). MjRenderContext(s.sim, device_id=s.device_id) if modder is not None: s.modder = modder(s.sim, random_state=proc_worker_id) s.modder.whiten_materials() else: s.modder = None global _render_pool_storage _render_pool_storage = s
def test_model_save_load(): model = load_model_from_xml(BASIC_MODEL_XML) xml_from_model = model.get_xml() model_from_xml = load_model_from_xml(xml_from_model) assert(xml_from_model == model_from_xml.get_xml()) mjb_from_model = model.get_mjb() model_from_mjb = load_model_from_mjb(mjb_from_model) assert(mjb_from_model == model_from_mjb.get_mjb())
def test_sim_save(): model = load_model_from_xml(BASIC_MODEL_XML) assert model.nkey == 0 sim = MjSim(model) with StringIO() as f: sim.save(f) f.seek(0) loaded_model = load_model_from_xml(f.read()) assert loaded_model.nkey == 1 with BytesIO() as f: sim.save(f, format='mjb') f.seek(0) loaded_model = load_model_from_mjb(f.read()) assert loaded_model.nkey == 1
def __setstate__(self, state): self.__dict__ = state['state'] self.model = mujoco_py.load_model_from_mjb(state['mjb']) self.sim = mujoco_py.MjSim(self.model) self.data = self.sim.data self.set_env_state(state['env_state'])
""" # Serialization/Deserialization of Models Sometimes its useful to send a mujoco model over the network, or save it to a file with all assets embedded. """ import mujoco_py # The binary MJB format is preferable, since it includes assets like # textures and meshes. model = mujoco_py.load_model_from_path("xmls/claw.xml") mjb_bytestring = model.get_mjb() model_from_binary = mujoco_py.load_model_from_mjb(mjb_bytestring) assert model.nbody == model_from_binary.nbody # XML is preferable to MJB when readability and backward compatibility are # important. xml_string = model.get_xml() model_from_xml = mujoco_py.load_model_from_xml(xml_string) assert model.nbody == model_from_xml.nbody
def get_sim(seed): model = load_model_from_mjb(pattern) return MjSim(model)
def __init__(self, xml_path): self.xml_path = xml_path mjb_bytestring = mujoco_py.load_model_from_path(xml_path).get_mjb() self.model = mujoco_py.load_model_from_mjb(mjb_bytestring) self.sim = mujoco_py.MjSim(self.model) self.viewer = mujoco_py.MjViewer(self.sim)