try: library_path = ffibuilder.compile(verbose=True) except Exception as e: build_fn_cleanup(name) raise e # On Mac the MuJoCo library is linked strangely, so we have to fix it here if sys.platform == 'darwin': fixed_library_path = manually_link_libraries(mujoco_path, library_path) move(fixed_library_path, library_path) # Overwrite with fixed library module = load_dynamic_ext(name, library_path) # Now that the module is loaded into memory, we can actually delete it build_fn_cleanup(name) return module.lib.__fun mujoco_path = discover_mujoco() cymj = load_cython_ext(mujoco_path) # Trick to expose all mj* functions from mujoco in mujoco_py.* class dict2(object): pass functions = dict2() for func_name in dir(cymj): if func_name.startswith("_mj"): setattr(functions, func_name[1:], getattr(cymj, func_name)) # Set user-defined callbacks that raise assertion with message cymj.set_warning_callback(user_warning_raise_exception)
try: library_path = ffibuilder.compile(verbose=True) except Exception as e: build_fn_cleanup(name) raise e # On Mac the MuJoCo library is linked strangely, so we have to fix it here if sys.platform == 'darwin': fixed_library_path = manually_link_libraries(mjpro_path, library_path) move(fixed_library_path, library_path) # Overwrite with fixed library module = load_dynamic_ext(name, library_path) # Now that the module is loaded into memory, we can actually delete it build_fn_cleanup(name) return module.lib.__fun mjpro_path, key_path = discover_mujoco() cymj = load_cython_ext(mjpro_path) # Trick to expose all mj* functions from mujoco in mujoco_py.* class dict2(object): pass functions = dict2() for func_name in dir(cymj): if func_name.startswith("_mj"): setattr(functions, func_name[1:], getattr(cymj, func_name)) functions.mj_activate(key_path)
class ignore_mujoco_warnings: """ Class to turn off mujoco warning exceptions within a scope. Useful for large, vectorized rollouts. """ def __enter__(self): self.prev_user_warning = cymj.get_warning_callback() cymj.set_warning_callback(user_warning_ignore_exception) return self def __exit__(self, type, value, traceback): cymj.set_warning_callback(self.prev_user_warning) mjpro_path, key_path = discover_mujoco() cymj = load_cython_ext(mjpro_path) # Trick to expose all mj* functions from mujoco in mujoco_py.* class dict2(object): pass functions = dict2() for func_name in dir(cymj): if func_name.startswith("_mj"): setattr(functions, func_name[1:], getattr(cymj, func_name)) functions.mj_activate(key_path)