### Setup MuJoCo Environment and Dependencies Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb This script installs MuJoCo and related libraries, configures the environment for GPU rendering using EGL, and verifies the installation. It also sets up XLA flags for performance optimization and downloads sample XML models. ```python !pip install mujoco !pip install mujoco_mjx !pip install brax import os import subprocess import distutils.util if subprocess.run('nvidia-smi').returncode: raise RuntimeError('Cannot communicate with GPU.') NVIDIA_ICD_CONFIG_PATH = '/usr/share/glvnd/egl_vendor.d/10_nvidia.json' if not os.path.exists(NVIDIA_ICD_CONFIG_PATH): with open(NVIDIA_ICD_CONFIG_PATH, 'w') as f: f.write('{\n "file_format_version" : "1.0.0",\n "ICD" : {\n "library_path" : "libEGL_nvidia.so.0"\n }\n}') %env MUJOCO_GL=egl import mujoco from mujoco import rollout, mjx xla_flags = os.environ.get('XLA_FLAGS', '') xla_flags += ' --xla_gpu_triton_gemm_any=True' os.environ['XLA_FLAGS'] = xla_flags !git clone https://github.com/google-deepmind/mujoco !git clone https://github.com/google-deepmind/dm_control ``` -------------------------------- ### Running MuJoCo Simulator Examples Source: https://github.com/google-deepmind/mujoco/blob/main/doc/programming/index.rst Execute precompiled code samples to verify the MuJoCo simulator installation. These commands demonstrate how to launch the 'simulate' executable with a sample model. ```Text Windows: simulate ..\model\humanoid\humanoid.xml Linux and macOS: ./simulate ../model/humanoid/humanoid.xml ``` -------------------------------- ### Simulating with Open-Loop Control Inputs Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb Demonstrates passing open-loop control sequences to the rollout function. This example simulates 100 humanoids with unique sine-wave control signals and applies cartesian forces. ```python control = np.sin((2 * np.pi * times).reshape(nstep, 1) + ctrl_phase) state, _ = rollout.rollout(humanoid_model, humanoid_datas, initial_states, control) # Applying cartesian forces xfrc_size = mujoco.mj_stateSize(humanoid_model, mujoco.mjtState.mjSTATE_XFRC_APPLIED) xfrc = np.zeros((nbatch, nstep, xfrc_size)) ``` -------------------------------- ### Install MuJoCo and Setup GPU Rendering (Python) Source: https://github.com/google-deepmind/mujoco/blob/main/python/LQR.ipynb Installs the MuJoCo library and configures the environment for GPU-accelerated rendering using EGL. It checks for GPU availability and sets necessary environment variables. Dependencies include `mujoco`, `distutils`, `os`, `subprocess`, and `google.colab`. ```python !pip install mujoco # Set up GPU rendering. from google.colab import files import distutils.util import os import subprocess if subprocess.run('nvidia-smi').returncode: raise RuntimeError( 'Cannot communicate with GPU. ' 'Make sure you are using a GPU Colab runtime. ' 'Go to the Runtime menu and select Choose runtime type.') # Add an ICD config so that glvnd can pick up the Nvidia EGL driver. # This is usually installed as part of an Nvidia driver package, but the Colab # kernel doesn't install its driver via APT, and as a result the ICD is missing. # (https://github.com/NVIDIA/libglvnd/blob/master/src/EGL/icd_enumeration.md) NVIDIA_ICD_CONFIG_PATH = '/usr/share/glvnd/egl_vendor.d/10_nvidia.json' if not os.path.exists(NVIDIA_ICD_CONFIG_PATH): with open(NVIDIA_ICD_CONFIG_PATH, 'w') as f: f.write(""" { "file_format_version" : \"1.0.0\", "ICD" : { "library_path" : \"libEGL_nvidia.so.0\" } } """) # Configure MuJoCo to use the EGL rendering backend (requires GPU) print('Setting environment variable to use GPU rendering:') %env MUJOCO_GL=egl # Check if installation was succesful. try: print('Checking that the installation succeeded:') import mujoco mujoco.MjModel.from_xml_string('') except Exception as e: raise e from RuntimeError( 'Something went wrong during installation. Check the shell output above ' 'for more information.\n' 'If using a hosted Colab runtime, make sure you enable GPU acceleration ' 'by going to the Runtime menu and selecting "Choose runtime type".') print('Installation successful.') # Other imports and helper functions import numpy as np from typing import Callable, Optional, Union, List import scipy.linalg # Graphics and plotting. print('Installing mediapy:') !command -v ffmpeg >/dev/null || (apt update && apt install -y ffmpeg) !pip install -q mediapy import mediapy as media import matplotlib.pyplot as plt # More legible printing from numpy. np.set_printoptions(precision=3, suppress=True, linewidth=100) from IPython.display import clear_output clear_output() ``` -------------------------------- ### Install Simulate Executable and Headers Source: https://github.com/google-deepmind/mujoco/blob/main/simulate/CMakeLists.txt Installs the 'simulate' executable, its associated libraries, and public headers. This makes the simulate component available for use after installation. ```cmake install( TARGETS simulate EXPORT ${PROJECT_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT simulate LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT simulate ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT simulate BUNDLE DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT simulate PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" COMPONENT simulate ) ``` -------------------------------- ### Install Dependencies with uv Source: https://github.com/google-deepmind/mujoco/blob/main/mjx/mujoco/mjx/codegen/README.md Install the latest MuJoCo and local MJX using uv. Ensure you have uv installed and are in the root mjx/ directory. ```bash uv venv .venv --default-index https://pypi.org/simple source .venv/bin/activate uv pip install --upgrade --force-reinstall mujoco --default-index https://pypi.org/simple --extra-index-url https://py.mujoco.org/ uv pip install -e ".[warp,dev]" --default-index https://pypi.org/simple ``` -------------------------------- ### Minimal MJX Example Source: https://github.com/google-deepmind/mujoco/blob/main/doc/mjx.rst A basic example demonstrating how to import mujoco.mjx, place a model on device, create data on device, and step the simulation. ```python import mujoco.mjx # Load a model and place it on device model = mjx.put_model(mujoco.MjModel.from_xml_path("humanoid.xml")) # Create simulation data on device data = mjx.make_data(model) # Step the simulation data = mjx.step(model, data) ``` -------------------------------- ### Optimizing `chunk_size` for MuJoCo Rollouts (Python) Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb Explores the impact of `chunk_size` on MuJoCo rollout performance, particularly for short rollouts. The default chunking strategy may not be optimal in all cases. This example benchmarks different `chunk_size` values to find the most efficient setting for a given workload, plotting steps per second against chunk size. ```python nbatch = 100 nstep = 1 ntiming = 20 # Load model hopper_model = mujoco.MjModel.from_xml_path(hopper_path) hopper_data = mujoco.MjData(hopper_model) hopper_datas = [copy.copy(hopper_data) for _ in range(nthread)] # Get initial states initial_states = get_state(hopper_model, hopper_data, nbatch) def rollout_chunk_size(chunk_size=None): rollout.rollout(hopper_model, hopper_datas, initial_states, nstep=nstep, chunk_size=chunk_size) # Rollout with different chunk sizes default_chunk_size = int(max(1.0, 0.1 * nbatch / nthread)) chunk_sizes = sorted([1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, default_chunk_size]) t_chunk_size = benchmark(lambda x: rollout_chunk_size(x), chunk_sizes, ntiming=ntiming) ``` -------------------------------- ### Install MuJoCo Sample Executables and Libraries Source: https://github.com/google-deepmind/mujoco/blob/main/sample/CMakeLists.txt Installs the compiled sample targets (executables, libraries, archives) to their designated locations within the installation prefix. This ensures samples are available after building. ```cmake install( TARGETS basic compile dependencies record testspeed EXPORT ${PROJECT_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT samples LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT samples ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT samples BUNDLE DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT samples PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT samples ) ``` -------------------------------- ### Configure and Install USD Plugin Info Source: https://github.com/google-deepmind/mujoco/blob/main/src/experimental/usd/CMakeLists.txt Generates and installs the plugInfo.json file for a given USD plugin. Use this for plugins that require specific configuration files. ```cmake configure_and_install_usd_plugin_info( ${MJCF_PLUGIN_TARGET_NAME} plugins/mjcf ${MJ_USD_INSTALL_DIR_LIB} ) configure_and_install_usd_plugin_info( ${MJC_PHYSICS_PLUGIN_TARGET_NAME} mjcPhysics ${MJ_USD_INSTALL_DIR_LIB} ) ``` -------------------------------- ### Minimal Viewer Launch Example Source: https://github.com/google-deepmind/mujoco/blob/main/doc/python.rst A basic example of launching the MuJoCo viewer using a context manager, which automatically closes the viewer upon exiting the block. This example includes a timer to close the viewer after 30 seconds. ```python import time import mujoco import mujoco.viewer m = mujoco.MjModel.from_xml_path('/path/to/mjcf.xml') d = mujoco.MjData(m) with mujoco.viewer.launch_passive(m, d) as viewer: # Close the viewer automatically after 30 wall-seconds. start = time.time() while viewer.is_running() and time.time() - start < 30: step_start = time.time() # mj_step can be replaced with code that also evaluates # a policy and applies a control signal before stepping the physics. mujoco.mj_step(m, d) # Example modification of a viewer option: toggle contact points every two seconds. with viewer.lock(): viewer.opt.flags[mujoco.mjtVisFlag.mjVIS_CONTACTPOINT] = int(d.time % 2) ``` -------------------------------- ### Minimal MuJoCo Example Source: https://github.com/google-deepmind/mujoco/blob/main/doc/python.rst A basic example demonstrating how to load an XML model, create a physics data instance, and run the simulation. This snippet requires a 'gizmo.stl' file to be present at '/path/to/gizmo.stl'. ```python import mujoco XML=r""" """ ASSETS=dict() with open('/path/to/gizmo.stl', 'rb') as f: ASSETS['gizmo.stl'] = f.read() model = mujoco.MjModel.from_xml_string(XML, ASSETS) data = mujoco.MjData(model) while data.time < 1: mujoco.mj_step(model, data) print(data.geom_xpos) ``` -------------------------------- ### Install Plugin File Source: https://github.com/google-deepmind/mujoco/blob/main/src/experimental/usd/CMakeLists.txt Installs the configured plugin file to the specified destination directory. ```cmake install(FILES "${OUTPUT_FILE}" DESTINATION "${install_base_dir}/${plugin_name}" ) ``` -------------------------------- ### Install mujoco-warp from Source Source: https://github.com/google-deepmind/mujoco/blob/main/doc/mjwarp/index.rst Clone the repository and install dependencies to build mujoco-warp from source. ```shell git clone https://github.com/google-deepmind/mujoco_warp.git cd mujoco_warp uv sync --all-extras ``` -------------------------------- ### Install Libraries Source: https://github.com/google-deepmind/mujoco/blob/main/CMakeLists.txt Installs the MuJoCo libraries, including runtime and development components, to the specified installation directories. ```cmake if(NOT EMSCRIPTEN AND NOT (APPLE AND MUJOCO_BUILD_MACOS_FRAMEWORKS)) set(MUJOCO_TARGETS mujoco) # Install the libraries. install( TARGETS ${MUJOCO_TARGETS} EXPORT ${PROJECT_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT runtime LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT runtime ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT dev PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/mujoco" COMPONENT dev ) set(CONFIG_PACKAGE_LOCATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") ``` -------------------------------- ### Install MuJoCo and Set Up GPU Rendering Source: https://github.com/google-deepmind/mujoco/blob/main/python/mjspec.ipynb Installs the MuJoCo library and configures the environment for GPU-accelerated rendering by setting the MUJOCO_GL environment variable to 'egl'. Includes checks for GPU availability and successful installation. ```python !pip install mujoco # Set up GPU rendering. from google.colab import files import distutils.util import os import subprocess if subprocess.run('nvidia-smi').returncode: raise RuntimeError( 'Cannot communicate with GPU. ' 'Make sure you are using a GPU Colab runtime. ' 'Go to the Runtime menu and select Choose runtime type.') # Add an ICD config so that glvnd can pick up the Nvidia EGL driver. # This is usually installed as part of an Nvidia driver package, but the Colab # kernel doesn't install its driver via APT, and as a result the ICD is missing. # (https://github.com/NVIDIA/libglvnd/blob/master/src/EGL/icd_enumeration.md) NVIDIA_ICD_CONFIG_PATH = '/usr/share/glvnd/egl_vendor.d/10_nvidia.json' if not os.path.exists(NVIDIA_ICD_CONFIG_PATH): with open(NVIDIA_ICD_CONFIG_PATH, 'w') as f: f.write("""{ "file_format_version" : \"1.0.0\", "ICD" : { "library_path" : \"libEGL_nvidia.so.0\" } } """) # Configure MuJoCo to use the EGL rendering backend (requires GPU) print('Setting environment variable to use GPU rendering:') %env MUJOCO_GL=egl # Check if installation was successful. try: print('Checking that the installation succeeded:') import mujoco as mj mj.MjModel.from_xml_string('') except Exception as e: raise e from RuntimeError( 'Something went wrong during installation. Check the shell output above ' 'for more information.\n' 'If using a hosted Colab runtime, make sure you enable GPU acceleration ' 'by going to the Runtime menu and selecting "Choose runtime type".') print('Installation successful.') ``` -------------------------------- ### Minimal MJWarp Example Source: https://github.com/google-deepmind/mujoco/blob/main/doc/mjwarp/index.rst This is a minimal example demonstrating the basic usage of MJWarp. It shows how to set up and potentially run a simulation. ```python # No code provided in the source for this example. ``` -------------------------------- ### Install libmujoco_simulate Library and Headers Source: https://github.com/google-deepmind/mujoco/blob/main/simulate/CMakeLists.txt Installs the 'libmujoco_simulate' library and its public headers, specifically placing headers in a 'simulate' subdirectory. This is for the core simulation library. ```cmake install( TARGETS libmujoco_simulate EXPORT mujoco RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT simulate LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT simulate ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT simulate PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/simulate" COMPONENT simulate ) ``` -------------------------------- ### Example: Record Humanoid Animation Source: https://github.com/google-deepmind/mujoco/blob/main/doc/programming/samples.rst An example demonstrating how to record a 5-second animation at 60 frames per second using the default humanoid model and saving the raw RGB data to 'rgb.out'. ```Shell record humanoid.xml 5 60 rgb.out ``` -------------------------------- ### Install mujoco-mjx with Warp support Source: https://github.com/google-deepmind/mujoco/blob/main/doc/mjx.rst Install mujoco-mjx with support for MuJoCo Warp, optimizing performance for NVIDIA GPUs. ```shell pip install mujoco-mjx[warp] ``` -------------------------------- ### Install MuJoCo, MJX, and Brax Source: https://github.com/google-deepmind/mujoco/blob/main/mjx/training_apg.ipynb Installs the core libraries required for MuJoCo, MJX, and Brax simulations using pip. These are fundamental for setting up the environment. ```python # Install MuJoCo, MJX, and Brax !pip install mujoco !pip install mujoco_mjx !pip install brax ``` -------------------------------- ### Install Newton USD Plugin Source: https://github.com/google-deepmind/mujoco/blob/main/src/experimental/usd/CMakeLists.txt Installs the Newton USD plugin. This is a specific function for integrating Newton physics with USD. ```cmake install_newton_usd_plugin( ${MJ_USD_INSTALL_DIR_LIB} ) ``` -------------------------------- ### Install MuJoCo Bindings from Source Source: https://github.com/google-deepmind/mujoco/blob/main/doc/python.rst Install the MuJoCo Python bindings using pip, specifying the paths to the MuJoCo library and plugin directories via environment variables. ```shell cd dist MUJOCO_PATH=/PATH/TO/MUJOCO \ MUJOCO_PLUGIN_PATH=/PATH/TO/MUJOCO/PLUGIN \ pip install mujoco-x.y.z.tar.gz ``` -------------------------------- ### Install MuJoCo, MJX, and Brax dependencies Source: https://github.com/google-deepmind/mujoco/blob/main/mjx/tutorial.ipynb Installs the core MuJoCo physics engine, the MJX JAX-based implementation, and the Brax library via pip. ```python !pip install mujoco !pip install mujoco_mjx !pip install brax ``` -------------------------------- ### Simulate Tippe Tops with Different Models Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb Demonstrates simulating 100 tippe tops with identical initial conditions but different physical properties (size, color) by using different MuJoCo models. This showcases the flexibility of `rollout` in handling variations across models, provided they share compatible dimensions. ```python import mujoco import time import numpy as np from mujoco import rollout, media # Assume top_model, get_state, render_many are defined elsewhere # Assume different model paths (e.g., top_model_colorful, top_model_large) are available nbatch = 100 nthread = 4 # Example number of threads # Create a list of models and corresponding data objects top_models = [] top_datas = [] initial_states_list = [] # Example: Load 100 different tippe top models for i in range(nbatch): # Replace with actual logic to load different models # For demonstration, we'll reuse the same model structure but imagine different parameters model_path = f"path/to/tippetop_{i}.xml" # Placeholder path model = mujoco.MjModel.from_xml_path(model_path) data = mujoco.MjData(model) mujoco.mj_resetDataKeyframe(model, data, 0) state = get_state(model, data) top_models.append(model) top_datas.append(data) initial_states_list.append(state) # Stack initial states for batch processing initial_states = np.stack(initial_states_list) # Prepare data for multithreading thread_datas = [copy.copy(d) for d in top_datas] # This needs careful handling if models differ significantly # Run the rollout start = time.time() # Note: rollout might need adjustments if models have different dimensions # For simplicity, assuming compatible dimensions here. state, sensordata = rollout.rollout(top_models, thread_datas, initial_states, nstep=int(top_nstep*1.5)) end = time.time() # Render the results (requires careful handling of multiple models for rendering) # This part would typically involve rendering each model's state separately or using a combined approach # For simplicity, showing a placeholder for rendering print("Rendering would happen here, potentially combining frames from different models.") print(f'Rollout time {end-start:.1f} seconds') ``` -------------------------------- ### Simulate Multiple Tippe Tops with Different Initial Speeds Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb Simulates 100 tippe tops with varying initial rotational speeds using multithreading. It generates initial states, runs the rollout, and then renders all the tops simultaneously. This demonstrates parallel simulation and visualization. ```python import mujoco import time import numpy as np import copy from mujoco import rollout, media # Assume top_model, get_state, render_many are defined elsewhere nbatch = 100 # Simulate this many tops # Get nbatch initial states and scale the initial speed of the tippe top using the batch index top_data = mujoco.MjData(top_model) mujoco.mj_resetDataKeyframe(top_model, top_data, 0) initial_states = get_state(top_model, top_data, nbatch) initial_states[:, -1] *= np.linspace(0.5, 1.5, num=nbatch) # Run the rollout start = time.time() top_datas = [copy.copy(top_data) for _ in range(nthread)] # 1 MjData per thread state, sensordata = rollout.rollout(top_model, top_datas, initial_states, nstep=int(top_nstep*1.5)) end = time.time() # Use state to render all the tops at once start_render = time.time() framerate = 60 frames = render_many(top_model, top_data, state, framerate, transparent=True) media.show_video(frames, fps=framerate) end_render = time.time() print(f'Rollout time {end-start:.1f} seconds') print(f'Rendering time {end_render-start_render:.1f} seconds') ``` -------------------------------- ### Initializing MuJoCo Models for Benchmarking Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb Provides utility functions to initialize MuJoCo models, including the 'tippe_top' and 'humanoid' models. It sets specific initial states and simulates steps to achieve stable configurations for benchmarking. ```python #@title Benchmarking utilities top_model = mujoco.MjModel.from_xml_string(tippe_top) def init_top(model): data = mujoco.MjData(model) # Set to the state to a spinning top (keyframe 0) mujoco.mj_resetDataKeyframe(model, data, 0) return data # Create and initialize humanoid model # Step for 2 seconds to get a stable set of contacts to benchmark humanoid_model = mujoco.MjModel.from_xml_path(humanoid_path) humanoid_data = mujoco.MjData(humanoid_model) humanoid_data.qvel[2] = 4 # Make the humanoid jump while humanoid_data.time < 2.0: mujoco.mj_step(humanoid_model, humanoid_data) humanoid_initial_state = get_state(humanoid_model, humanoid_data) def init_humanoid(model): data = mujoco.MjData(model) mujoco.mj_setState(model, data, humanoid_initial_state.flatten(), mujoco.mjtState.mjSTATE_FULLPHYSICS) return data # Create and initialize humanoid100 model ``` -------------------------------- ### Demonstrate Warmstarting for Constraint Solver (Python) Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb This Python code demonstrates the effect of warmstarting the constraint solver in MuJoCo simulations. It compares a continuous rollout with chunked rollouts, both with and without warmstarting, using the tippe top model with the CG solver. The goal is to show how warmstarting prevents divergence in chaotic systems. Dependencies include copy, mujoco, time, rollout, and render_many. ```python top_model_cg = copy.copy(top_model) # Change to CG solver, the Newton solver converges too well for # warmstarting to have an appreciable effect top_model_cg.opt.solver = mujoco.mjtSolver.mjSOL_CG chunks = 100 steps_per_chunk = 60 nstep = steps_per_chunk*chunks # Get initial states top_data_cg = mujoco.MjData(top_model_cg) mujoco.mj_resetDataKeyframe(top_model_cg, top_data_cg, 0) initial_state = get_state(top_model_cg, top_data_cg) start = time.time() # Rollout with nstep steps state_all, _ = rollout.rollout(top_model_cg, top_data_cg, initial_state, nstep=nstep) # Rollout in chunks with warmstarting state_chunks = [] state_chunk, _ = rollout.rollout(top_model_cg, top_data_cg, initial_state, nstep=steps_per_chunk) state_chunks.append(state_chunk) for _ in range(chunks-1): state_chunk, _ = rollout.rollout(top_model_cg, top_data_cg, state_chunks[-1][0, -1, :], nstep=steps_per_chunk, initial_warmstart=top_data_cg.qacc_warmstart) state_chunks.append(state_chunk) state_all_chunked_warmstart = np.concatenate(state_chunks, axis=1) # Rollout in chunks without warmstarting state_chunks = [] state_chunk, _ = rollout.rollout(top_model_cg, top_data_cg, initial_state, nstep=steps_per_chunk) state_chunks.append(state_chunk) first_warmstart = None for i in range(chunks-1): state_chunk, _ = rollout.rollout(top_model_cg, top_data_cg, state_chunks[-1][0, -1, :], nstep=steps_per_chunk) state_chunks.append(state_chunk) state_all_chunked = np.concatenate(state_chunks, axis=1) end = time.time() # Render the rollouts start_render = time.time() framerate = 60 state_render = np.concatenate((state_all, state_all_chunked, state_all_chunked_warmstart), axis=0) camera = 'distant' frames1 = render_many(top_model_cg, top_data_cg, state_all, framerate, shape=(240, 320), transparent=False, camera=camera) frames2 = render_many(top_model_cg, top_data_cg, state_all_chunked_warmstart, framerate, shape=(240, 320), transparent=False, camera=camera) frames3 = render_many(top_model_cg, top_data_cg, state_all_chunked, framerate, shape=(240, 320), transparent=False, camera=camera) media.show_video(np.concatenate((frames1, frames2, frames3), axis=2)) end_render = time.time() print(f'Rollout took {end-start:.1f} seconds') print(f'Rendering took {end_render-start_render:.1f} seconds') ``` -------------------------------- ### Setup Emscripten SDK Source: https://github.com/google-deepmind/mujoco/blob/main/wasm/README.md Installs and activates Emscripten SDK version 4.0.10, and sets up the environment for subsequent commands. This is a prerequisite for compiling C++ code to WebAssembly. ```shell git clone https://github.com/emscripten-core/emsdk.git ./emsdk/emsdk install 4.0.10 ./emsdk/emsdk activate 4.0.10 source ./emsdk/emsdk_env.sh ``` -------------------------------- ### Setup Python Environment for Bindings Generation Source: https://github.com/google-deepmind/mujoco/blob/main/wasm/README.md Creates a Python virtual environment, activates it, and installs necessary Python dependencies for generating C++ bindings. This includes `absl` and `pytest` for testing. ```shell python3 -m venv .venv source .venv/bin/activate pip install -r python/build_requirements.txt ``` -------------------------------- ### MuJoCo XML Model Definition Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb An example XML string defining a 'tippe top' physics model, including geometry, assets, and sensors. This can be parsed by MuJoCo to create an MjModel instance. ```xml ``` -------------------------------- ### Batch Simulation and Rendering with MuJoCo Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb This snippet demonstrates how to create multiple variations of a MuJoCo model by randomizing geometry properties, initializing them on a grid, and performing a parallel rollout. It also shows how to configure a camera and render the resulting simulation frames into a video. ```python nbatch = 100 spec = mujoco.MjSpec.from_string(tippe_top) spec.lights[0].pos[2] = 2 models = [] for i in range(nbatch): for geom in spec.geoms: if geom.name in ['ball', 'stem', 'ballast']: geom.rgba[:3] = np.random.rand(3) size_scale = 0.4*np.random.rand(1) + 0.75 # ... (scaling logic) ... models.append(spec.compile()) # Run the rollout state, sensordata = rollout.rollout(models, top_datas, initial_states, nstep=nstep) # Render video framerate = 60 cam = mujoco.MjvCamera() mujoco.mjv_defaultCamera(cam) frames = render_many(models, top_data, state, framerate, camera=cam) media.show_video(frames, fps=framerate) ``` -------------------------------- ### C Code: Rotate Vector by Quaternion Function Source: https://github.com/google-deepmind/mujoco/blob/main/STYLEGUIDE.md Example of a C function demonstrating conditional logic and K&R style braces for rotating a vector by a quaternion. It handles both null quaternions and regular processing cases. ```c // rotate vector by quaternion void mju_rotVecQuat(mjtNum res[3], const mjtNum vec[3], const mjtNum quat[4]) { // null quat: copy vec if (quat[0] == 1 && quat[1] == 0 && quat[2] == 0 && quat[3] == 0) { mju_copy3(res, vec); } // regular processing else { mjtNum mat[9]; mju_quat2Mat(mat, quat); mju_mulMatVec3(res, mat, vec); } } ``` -------------------------------- ### C Code: Transpose Matrix Function Source: https://github.com/google-deepmind/mujoco/blob/main/STYLEGUIDE.md Example of a C function to transpose a matrix, demonstrating K&R style braces and inline comments. This function takes a result matrix, an input matrix, and dimensions as input. ```c // transpose matrix void mju_transpose(mjtNum* res, const mjtNum* mat, int nr, int nc) { for (int i=0; i < nr; i++) { for (int j=0; j < nc; j++) { res[j*nr+i] = mat[i*nc+j]; } } } ``` -------------------------------- ### MuJoCo Actuated Bat Simulation - Python Source: https://github.com/google-deepmind/mujoco/blob/main/python/tutorial.ipynb This Python code snippet continues the simulation of the actuated bat and passive 'piƱata' from the previous example. It resets the MuJoCo data and sets a constant control signal for the actuator. This setup is intended for further simulation steps to observe the dynamic behavior. ```python # Assuming model and data are already loaded from the previous snippet # MJCF = """...""" # model = mujoco.MjModel.from_xml_string(MJCF) # data = mujoco.MjData(model) n_frames = 180 height = 240 width = 320 frames = [] fps = 60.0 times = [] sensordata = [] # constant actuator signal mujoco.mj_resetData(model, data) data.ctrl = 20 # The simulation loop would continue here to generate frames for video ``` -------------------------------- ### Warmstart IK with Previous Solution Source: https://github.com/google-deepmind/mujoco/blob/main/python/least_squares.ipynb Improves the stability of IK solutions by initializing the solver with the solution from the previous time step. This 'warmstart' technique helps mitigate glitches caused by the IK problem having multiple local minima. ```python #@title Warmstart with previous solution {vertical-output: true} frames = [] x = x0 for t in np.linspace(0, 2 * np.pi, n_frame): # Get target pose pos, quat = pose(t) # Define IK problem ik_target = lambda x: ik(x, pos=pos, quat=quat) jac_target = lambda x, r: ik_jac(x, r, pos=pos, quat=quat) x, _ = minimize.least_squares(x, ik_target, bounds, jacobian=jac_target, verbose=0); mujoco.mj_kinematics(model, data) mujoco.mj_camlight(model, data) renderer.update_scene(data, camera, voption) frames.append(renderer.render()) media.show_video(frames, loop=False) ``` -------------------------------- ### Comparing Rollout Class vs. Method for Threadpool Reuse (Python) Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb Compares the performance of the `rollout` class and method for reusing threadpools. The `Rollout` class allows for safe reuse of internally managed thread pools, which can significantly speed up short rollouts. This example benchmarks both approaches to demonstrate the performance difference. ```python nbatch = 100 nsteps = [2**i for i in [2, 3, 4, 5, 6, 7]] ntiming = 5 top_data = mujoco.MjData(top_model) mujoco.mj_resetDataKeyframe(top_model, top_data, 0) top_datas = [copy.copy(top_data) for _ in range(nthread)] initial_states = get_state(top_model, top_data, nbatch) def rollout_method(nstep): for i in range(20): rollout.rollout(top_model, top_datas, initial_states, nstep=nstep) def rollout_class(nstep): with rollout.Rollout(nthread=nthread) as rollout_: for i in range(20): rollout_.rollout(top_model, top_datas, initial_states, nstep=nstep) t_method = benchmark(lambda x: rollout_method(x), nsteps, ntiming) t_class = benchmark(lambda x: rollout_class(x), nsteps, ntiming) plt.loglog(nsteps, nbatch * np.array(nsteps) / t_method, label='recreating threadpools') plt.loglog(nsteps, nbatch * np.array(nsteps) / t_class, label='reusing threadpool') plt.xlabel('nstep') plt.ylabel('steps per second') ticker = matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')) plt.gca().yaxis.set_minor_formatter(ticker) plt.legend() plt.grid(True, which="both", axis="both") ``` -------------------------------- ### Configure and Install USD Plugin Info Source: https://github.com/google-deepmind/mujoco/blob/main/src/experimental/usd/CMakeLists.txt A helper function to determine library paths and set up the plugInfo.json file for a USD plugin. ```cmake function(configure_and_install_usd_plugin_info plugin_name source_subpath install_base_dir) # Determine shared library prefix if(CMAKE_SHARED_LIBRARY_PREFIX) set(LIB_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX}) else() set(LIB_PREFIX "") endif() set(PLUG_INFO_LIBRARY_PATH "../../${LIB_PREFIX}${plugin_name}${CMAKE_SHARED_LIBRARY_SUFFIX}") set(OUTPUT_DIR "${CMAKE_BINARY_DIR}/${install_base_dir}/${plugin_name}") set(OUTPUT_FILE "${OUTPUT_DIR}/plugInfo.json") ``` -------------------------------- ### Install Mujoco Models Source: https://github.com/google-deepmind/mujoco/blob/main/CMakeLists.txt Installs the Mujoco model files into the share directory. Excludes the CMakeLists.txt file from the installation to avoid conflicts. ```cmake install( DIRECTORY model DESTINATION "${CMAKE_INSTALL_DATADIR}/mujoco" PATTERN "CMakeLists.txt" EXCLUDE ) ``` -------------------------------- ### Initialize and Rollout MuJoCo Models Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb Initializes different MuJoCo models (spinning top, humanoid, humanoid100) and performs rollouts for a specified number of steps. It then renders the simulation frames and displays the video. This snippet demonstrates basic model setup and simulation execution. ```python import mujoco import time import numpy as np from mujoco import rollout, media, mjx # Assume humanoid_path, humanoid100_path, get_state, render_many are defined elsewhere # Set to the state to a spinning top (keyframe 0) top_model = mujoco.MjModel.from_xml_path("path/to/top.xml") # Placeholder path top_data = mujoco.MjData(top_model) mujoco.mj_resetDataKeyframe(top_model, top_data, 0) top_state = get_state(top_model, top_data) # Create and initialize humanoid model humanoid_path = "path/to/humanoid.xml" # Placeholder path humanoid_model = mujoco.MjModel.from_xml_path(humanoid_path) humanoid_data = mujoco.MjData(humanoid_model) humanoid_data.qvel[2] = 4 # Make the humanoid jump humanoid_state = get_state(humanoid_model, humanoid_data) # Create and initialize humanoid100 model humanoid100_path = "path/to/humanoid100.xml" # Placeholder path humanoid100_model = mujoco.MjModel.from_xml_path(humanoid100_path) humanoid100_data = mujoco.MjData(humanoid100_model) h100_state = get_state(humanoid100_model, humanoid100_data) start = time.time() top_nstep = int(6 / top_model.opt.timestep) top_state, _ = rollout.rollout(top_model, top_data, top_state, nstep=top_nstep) humanoid_nstep = int(3 / humanoid_model.opt.timestep) humanoid_state, _ = rollout.rollout(humanoid_model, humanoid_data, humanoid_state, nstep=humanoid_nstep) humanoid100_nstep = int(3 / humanoid100_model.opt.timestep) h100_state, _ = rollout.rollout(humanoid100_model, humanoid100_data, h100_state, nstep=humanoid100_nstep) end = time.time() start_render = time.time() top_frames = render_many(top_model, top_data, top_state, framerate=60, shape=(240, 320)) humanoid_frames = render_many(humanoid_model, humanoid_data, humanoid_state, framerate=120, shape=(240, 320)) humanoid100_frames = render_many(humanoid100_model, humanoid100_data, h100_state, framerate=120, shape=(240, 320)) # humanoid and humanoid100 are shown at half speed media.show_video(np.concatenate((top_frames, humanoid_frames, humanoid100_frames), axis=2), fps=60) end_render = time.time() print(f'Rollout took {end-start:.1f} seconds') print(f'Rendering took {end_render-start_render:.1f} seconds') ``` -------------------------------- ### MuJoCo Benchmarking Configuration and Execution Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb Sets up parameters for benchmarking MuJoCo rollouts, including nominal batch size and step count, as well as arrays for varying these parameters. It then calls the `benchmark_rollout` function to execute and collect performance data. ```python nominal_nbatch = 256 # Batch size to use when testing different nstep nominal_nstep = 5 # Step count to use when testing different nbatch nbatch = [1, 256, 2048, 8192] nstep = [1, 10, 100, 1000] top_benchmark_results = benchmark_rollout(top_model, init_top, ``` -------------------------------- ### Install Shared Libraries Source: https://github.com/google-deepmind/mujoco/blob/main/src/experimental/usd/CMakeLists.txt Installs the shared libraries for the specified targets. This command ensures that the compiled plugin libraries are placed in the correct installation directory. ```cmake install(TARGETS ${MJCF_PLUGIN_TARGET_NAME} ${MJC_PHYSICS_PLUGIN_TARGET_NAME} EXPORT ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ) ``` -------------------------------- ### Execute MJX Model Benchmarks Source: https://github.com/google-deepmind/mujoco/blob/main/python/rollout.ipynb Configures and runs benchmarks for specific MJX models like Tippe Top and Humanoid. It utilizes JIT unroll caches to optimize execution time for repetitive physics steps. ```python nominal_nbatch = 16384 nominal_nstep = 5 nbatch = [4096, 16384, 65536, 131072] nstep = [1, 10, 100, 200] mjx_top_results = benchmark_mjx(top_model, init_top, nbatch, nstep, nominal_nbatch, nominal_nstep, jit_unroll_cache=top_jit_unroll_cache) plot_mjx_benchmark(mjx_top_results, nbatch, nstep, nominal_nbatch, nominal_nstep, title='MJX Tippe Top') ``` -------------------------------- ### Basic Usage Example Source: https://github.com/google-deepmind/mujoco/blob/main/doc/python.rst Demonstrates the fundamental steps to initialize and use the USDExporter to export a simulation trajectory. ```APIDOC ## Basic USD Export Usage ### Description This example shows how to initialize the USDExporter, step through a simulation, update the scene with new frames, and finally save the exported USD file. ### Code Example ```python import mujoco from mujoco.usd import exporter m = mujoco.MjModel.from_xml_path('/path/to/mjcf.xml') d = mujoco.MjData(m) # Create the USDExporter exp = exporter.USDExporter(model=m) duration = 5 framerate = 60 while d.time < duration: # Step the physics mujoco.mj_step(m, d) if exp.frame_count < d.time * framerate: # Update the USD with a new frame exp.update_scene(data=d) # Export the USD file exp.save_scene(filetype="usd") ``` ``` -------------------------------- ### Install mujoco-warp from PyPI Source: https://github.com/google-deepmind/mujoco/blob/main/doc/mjwarp/index.rst Install the mujoco-warp package using pip. ```shell pip install mujoco-warp ``` -------------------------------- ### Install mujoco-mjx Source: https://github.com/google-deepmind/mujoco/blob/main/doc/mjx.rst Install the base mujoco-mjx package using pip. ```shell pip install mujoco-mjx ``` -------------------------------- ### Install MuJoCo USD Exporter Source: https://github.com/google-deepmind/mujoco/blob/main/doc/python.rst Install the MuJoCo USD exporter and its dependencies using pip. This command installs the optional dependencies 'usd-core' and 'pillow' required for USD export. ```shell pip install mujoco[usd] ``` -------------------------------- ### Configure and Install Package Configuration File Source: https://github.com/google-deepmind/mujoco/blob/main/CMakeLists.txt Configures the main package configuration file and installs it along with the version file. This allows CMake to find and use the installed package. ```cmake configure_package_config_file( cmake/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION ${CONFIG_PACKAGE_LOCATION} ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" DESTINATION ${CONFIG_PACKAGE_LOCATION} ) ``` -------------------------------- ### Main Simulation Entry Points Source: https://github.com/google-deepmind/mujoco/blob/main/doc/APIreference/functions_override.rst Provides the main entry points for the simulator. :ref:`mj_step` advances the simulation by one time step. Controls and forces can be set in advance or via a control callback. :ref:`mj_step1` and :ref:`mj_step2` break down the simulation pipeline for more granular control, but do not work with the RK4 solver. :ref:`mj_forward` performs computations without integration, useful for initialization and out-of-order computations. ```APIDOC ## Main simulation These are the main entry points to the simulator. Most users will only need to call :ref:`mj_step`, which computes everything and advanced the simulation state by one time step. Controls and applied forces must either be set in advance (in ``mjData.{ctrl, qfrc_applied, xfrc_applied}``), or a control callback :ref:`mjcb_control` must be installed which will be called just before the controls and applied forces are needed. Alternatively, one can use :ref:`mj_step1` and :ref:`mj_step2` which break down the simulation pipeline into computations that are executed before and after the controls are needed; in this way one can set controls that depend on the results from :ref:`mj_step1`. Keep in mind though that the RK4 solver does not work with mj_step1/2. See :ref:`Pipeline` for a more detailed description. mj_forward performs the same computations as :ref:`mj_step` but without the integration. It is useful after loading or resetting a model (to put the entire mjData in a valid state), and also for out-of-order computations that involve sampling or finite-difference approximations. ``` -------------------------------- ### Using MjVfs with MjSpec (Direct Instance) Source: https://github.com/google-deepmind/mujoco/blob/main/doc/python.rst Shows how to use MjVfs by creating an instance directly and manually closing it. Useful for managing VFS resources explicitly. ```python vfs = mujoco.MjVfs() vfs["model.xml"] = some_xml_string.encode("utf-8") spec = mujoco.MjSpec.from_file("model.xml", vfs=vfs) spec.compile(vfs=vfs) vfs.close() ``` -------------------------------- ### Build MuJoCo Documentation Source: https://github.com/google-deepmind/mujoco/blob/main/doc/programming/index.rst Build the HTML documentation locally by navigating to the doc directory, installing requirements, and running make. ```shell cd mujoco/doc ``` ```shell pip install -r requirements.txt ``` ```shell make html ```