index.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ===========
  2. Programming
  3. ===========
  4. .. _inIntro:
  5. Introduction
  6. ~~~~~~~~~~~~
  7. This chapter is the MuJoCo programming guide. A separate chapter contains the :doc:`../APIreference/index`
  8. documentation. MuJoCo is a dynamic library compatible with Windows, Linux and macOS, which requires a processor with AVX
  9. instructions. The library exposes the full functionality of the simulator through a compiler-independent shared-memory C
  10. API. It can also be used in C++ programs.
  11. The MuJoCo codebase is organized into subdirectories corresponding to different major areas of functionality:
  12. Engine
  13. The simulator (or physics engine) is written in C. It is responsible for all runtime computations.
  14. Parser
  15. The XML parser is written in C++. It can parse MJCF models and URDF models, converting them into an internal mjCModel
  16. C++ object which is exposed to the user via mjSpec.
  17. Compiler
  18. The compiler is written in C++. It takes an mjCModel C++ object constructed by the parser, and converts it into an
  19. mjModel C structure used at runtime.
  20. Abstract visualizer
  21. The abstract visualizer is written in C. It generates a list of abstract geometric entities representing the
  22. simulation state, with all information needed for actual rendering. It also provides abstract mouse hooks for camera
  23. and perturbation control.
  24. OpenGL renderer
  25. The renderer is written in C and is based on fixed-function OpenGL. It does not have all the features of
  26. state-of-the-art rendering engines (and can be replaced with such an engine if desired) but nevertheless it provides
  27. efficient and informative 3D rendering.
  28. Thread
  29. The Threading framework (new in MuJoCo 3.0) is written in C++ and exposed in C. It provides a ThreadPool interface
  30. to process Tasks asynchronously. To enable use in MuJoCo, create a ThreadPool and assign it to the thread_pool field
  31. in mjData.
  32. UI framework
  33. The UI framework is written in C. UI elements are rendered in OpenGL. It has its own event
  34. mechanism and abstract hooks for keyboard and mouse input. The code samples use it with GLFW, but it can also be used
  35. with other window libraries.
  36. .. _inStart:
  37. Getting started
  38. ~~~~~~~~~~~~~~~
  39. MuJoCo is an open-source project. Pre-built dynamic libraries are available for x86_64 and arm64 machines running
  40. Windows, Linux, and macOS. These can be downloaded from the `GitHub Releases page
  41. <https://github.com/google-deepmind/mujoco/releases>`_. Users who do not intend to develop or modify core MuJoCo code
  42. are encouraged to use our pre-built libraries, as these come bundled with the same versions of dependencies that we
  43. regularly test against, and benefit from build flags that have been tuned for performance. Our pre-built libraries are
  44. almost entirely self-contained and do not require any other library to be present, outside the standard C runtime. We
  45. also hide all symbols apart from those that form MuJoCo's public API, thus ensuring that it can coexist with any other
  46. libraries that may be loaded into the process (including other versions of libraries that MuJoCo depends on).
  47. The pre-built distribution is a single .zip on Windows, .dmg on macOS, and .tar.gz on Linux. There is no installer.
  48. On Windows and Linux, simply extract the archive in a directory of your choice. From the ``bin`` subdirectory, you can
  49. now run the precompiled code samples, for example:
  50. .. code-block:: Text
  51. Windows: simulate ..\model\humanoid\humanoid.xml
  52. Linux and macOS: ./simulate ../model/humanoid/humanoid.xml
  53. The directory structure is shown below. Users can re-organize it if needed, as well as install the dynamic libraries in
  54. other directories and set the path accordingly. The only file created automatically is MUJOCO_LOG.TXT in the executable
  55. directory; it contains error and warning messages, and can be deleted at any time.
  56. .. code-block:: Text
  57. bin - dynamic libraries, executables, MUJOCO_LOG.TXT
  58. doc - README.txt and REFERENCE.txt
  59. include - header files needed to develop with MuJoCo
  60. model - model collection
  61. sample - code samples and CMakeLists.txt needed to build them
  62. After verifying that the simulator works, you may also want to re-compile the code samples to ensure that you have a
  63. working development environment. We provide a cross-platform `CMake
  64. <https://github.com/google-deepmind/mujoco/blob/main/sample/CMakeLists.txt>`__ setup that can be used to build sample
  65. applications independently of the MuJoCo library itself.
  66. On macOS, the DMG disk image contains ``MuJoCo.app``, which you can double-click to launch the ``simulate`` GUI. You can
  67. also drag ``MuJoCo.app`` into the ``/Application`` on your system, as you would to install any other app. As well as the
  68. ``MuJoCo.app`` `Application Bundle <https://developer.apple.com/go/?id=bundle-
  69. structure>`__, the DMG includes the ``mujoco.framework`` subdirectory containing the MuJoCo dynamic library and all of
  70. its public headers. If you are using Xcode, you can import it as a framework dependency on your project. (This also
  71. works for Swift projects without any modification). If you are building manually, you can use ``-F`` and
  72. ``-framework mujoco`` to specify the header search path and the library search path respectively.
  73. .. _inBuild:
  74. Building from source
  75. ~~~~~~~~~~~~~~~~~~~~
  76. To build MuJoCo from source, you will need CMake and a working C++17 compiler installed. The steps are:
  77. #. Clone the ``mujoco`` repository from GitHub.
  78. #. Create a new build directory somewhere, and ``cd`` into it.
  79. #. Run :shell:`cmake $PATH_TO_CLONED_REPO` to configure the build.
  80. #. Run ``cmake --build .`` to build.
  81. MuJoCo's build system automatically fetches dependencies from upstream repositories over the Internet using CMake's
  82. `FetchContent <https://cmake.org/cmake/help/latest/module/FetchContent.html>`_ module.
  83. The main CMake setup will build the MuJoCo library itself along with all sample applications, but the Python
  84. bindings are not built. Those come with their own build instructions, which can be found in the :doc:`../python`
  85. section of the documentation.
  86. Additionally, the CMake setup also implements an installation phase which will copy and organize the output files to a
  87. target directory.
  88. 5. Select the directory: :shell:`cmake $PATH_TO_CLONED_REPO -DCMAKE_INSTALL_PREFIX=<my_install_dir>`
  89. #. After building, install with ``cmake --install .``
  90. When building on Windows, use Visual Studio 2019 or later and make sure Windows SDK version 10.0.22000 or later is
  91. installed (see :github:issue:`862` for more details).
  92. .. tip::
  93. As a reference, a working build configuration can be found in MuJoCo's
  94. `continuous integration setup <https://github.com/google-deepmind/mujoco/blob/main/.github/workflows/build.yml>`_ on
  95. GitHub.
  96. .. _inHeader:
  97. Header files
  98. ~~~~~~~~~~~~
  99. The distribution contains several header files which are identical on all platforms. They are also available from the
  100. links below, to make this documentation self-contained.
  101. `mujoco.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mujoco.h>`__
  102. This is the main header file and must be included in all programs using MuJoCo. It defines all API functions and
  103. global variables, and includes the all other header files except mjxmacro.h.
  104. `mjmodel.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjmodel.h>`__
  105. Defines the C structure :ref:`mjModel` which is the runtime representation of the
  106. model being simulated. It also defines a number of primitive types and other structures needed to define mjModel.
  107. `mjdata.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjdata.h>`__
  108. Defines the C structure :ref:`mjData` which is the workspace where all computations
  109. read their inputs and write their outputs. It also defines primitive types and other structures needed to define
  110. mjData.
  111. `mjvisualize.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjvisualize.h>`__
  112. Defines the primitive types and structures needed by the abstract visualizer.
  113. `mjrender.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjrender.h>`__
  114. Defines the primitive types and structures needed by the OpenGL renderer.
  115. `mjui.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjui.h>`__
  116. Defines the primitive types and structures needed by the UI framework.
  117. `mjtnum.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjtnum.h>`__
  118. Defines MuJoCo's ``mjtNum`` floating-point type to be either ``double`` or ``float``. See :ref:`mjtNum`.
  119. `mjspec.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjspec.h>`__
  120. Defines enums and structs used for :doc:`procedural model editing <modeledit>`.
  121. `mjplugin.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjplugin.h>`__
  122. Defines data structures required by :ref:`engine plugins<exPlugin>`.
  123. `mjthread.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjthread.h>`__
  124. Defines data structures and functions required by :ref:`thread<Thread>`.
  125. `mjmacro.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjmacro.h>`__
  126. Defines C macros that are useful in user code.
  127. `mjxmacro.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjxmacro.h>`__
  128. This file is optional and is not included by mujoco.h. It defines :ref:`X Macros <tyXMacro>` that can
  129. automate the mapping of mjModel and mjData into scripting languages, as well as other operations that require
  130. accessing all fields of mjModel and mjData.
  131. `mjexport.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjexport.h>`__
  132. Macros used for exporting public symbols from the MuJoCo library. This header should not be used directly by client
  133. code.
  134. `mjsan.h <https://github.com/google-deepmind/mujoco/blob/main/include/mujoco/mjsan.h>`__
  135. Definitions required when building with sanitizer instrumentation.
  136. .. _inVersion:
  137. Versions and compatibility
  138. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  139. MuJoCo has been used extensively since 2010 and is quite mature (even though our version numbering scheme is quite
  140. conservative). Nevertheless it remains under active development, and we have many exciting ideas for new features and
  141. are also making changes based on user feedback. This leads to unavoidable changes in both the modeling language in the
  142. API. While we encourage users to upgrade to the latest version, we recognize that this is not always feasible,
  143. especially when other developers release software that relies on MuJoCo. Therefore we have introduced simple
  144. mechanisms to help avoid version conflicts, as follows.
  145. The situation is more subtle if existing code was developed with a certain version of MuJoCo, and is now being
  146. compiled and linked with a different version. If the definitions of the API functions used in that code have changed,
  147. either the compiler or the linker will generate errors. But even if the function definitions have not changed, it may
  148. still be a good idea to assert that the software version is the same. To this end, the main header (mujoco.h) defines
  149. the symbol :ref:`mjVERSION_HEADER <glNumeric>` and the library provides the function
  150. :ref:`mj_version`. Thus the header and library versions can be compared with:
  151. .. code-block:: C
  152. // recommended version check
  153. if (mjVERSION_HEADER!=mj_version())
  154. complain();
  155. Note that only the main header defines this symbol. We assume that the collection of headers released with each software
  156. version will stay together and will not be mixed between versions. To avoid complications with floating-point
  157. comparisons, the above symbol and function use integers that are 100x the version number, so for example in software
  158. version 2.1 the symbol mjVERSION_HEADER is defined as 210.
  159. .. _inNaming:
  160. Naming convention
  161. ~~~~~~~~~~~~~~~~~
  162. All symbols defined in the API start with the prefix "mj". The character after "mj" in the prefix determines the family
  163. to which the symbol belongs. First we list the prefixes corresponding to type definitions.
  164. ``mj``
  165. Core simulation data structure (C struct), for example :ref:`mjModel`. If all characters
  166. after the prefix are capital, for example :ref:`mjMIN`, this is a macro or a symbol (#define).
  167. ``mjt``
  168. Primitive type, for example :ref:`mjtGeom`. Except for mjtByte and mjtNum, all other
  169. definitions in this family are enums.
  170. ``mjf``
  171. Callback function type, for example :ref:`mjfGeneric`.
  172. ``mjv``
  173. Data structure related to abstract visualization, for example :ref:`mjvCamera`.
  174. ``mjr``
  175. Data structure related to OpenGL rendering, for example :ref:`mjrContext`.
  176. ``mjui``
  177. Data structure related to UI framework, for example :ref:`mjuiSection`.
  178. ``mjs``
  179. Data structure related :doc:`procedural model editing <modeledit>`, for example :ref:`mjsJoint`.
  180. Next we list the prefixes corresponding to function definitions. Note that function prefixes always end with underscore.
  181. ``mj_``
  182. Core simulation function, for example :ref:`mj_step`. Almost all such functions have
  183. pointers to mjModel and mjData as their first two arguments, possibly followed by other arguments. They usually write
  184. their outputs to mjData.
  185. ``mju_``
  186. Utility function, for example :ref:`mju_mulMatVec`. These functions are self-contained
  187. in the sense that they do not have mjModel and mjData pointers as their arguments.
  188. ``mjv_``
  189. Function related to abstract visualization, for example :ref:`mjv_updateScene`.
  190. ``mjr_``
  191. Function related to OpenGL rendering, for example :ref:`mjr_render`.
  192. ``mjui_``
  193. Function related to UI framework, for example :ref:`mjui_update`.
  194. ``mjcb_``
  195. Global callback function pointer, for example :ref:`mjcb_control`. The user can install
  196. custom callbacks by setting these global pointers to user-defined functions.
  197. ``mjd_``
  198. Functions for computing derivatives, for example :ref:`mjd_transitionFD`.
  199. ``mjs_``
  200. Functions for :doc:`procedural model editing <modeledit>`, for example :ref:`mjs_addJoint`.
  201. .. _inOpenGL:
  202. Using OpenGL
  203. ~~~~~~~~~~~~
  204. The use of MuJoCo's native OpenGL renderer will be explained in :ref:`Rendering`. For rendering, MuJoCo uses OpenGL 1.5
  205. in the compatibility profile with the ``ARB_framebuffer_object`` and ``ARB_vertex_buffer_object`` extensions. OpenGL
  206. symbols are loaded via `GLAD <https://github.com/Dav1dde/glad>`_ the first time the :ref:`mjr_makeContext` function
  207. is called. This means that the MuJoCo library itself does not have an explicit dependency on OpenGL and can be used
  208. on systems without OpenGL support, as long as ``mjr_`` functions are not called.
  209. Applications that use MuJoCo's built-in rendering functionalities are responsible for linking against an appropriate
  210. OpenGL context creation library and for ensuring that there is an OpenGL context that is made current on the running
  211. thread. On Windows and macOS, there is a canonical OpenGL library provided by the operating system. On Linux, MuJoCo
  212. currently supports GLX for rendering to an X11 window, OSMesa for headless software rendering, and EGL for hardware
  213. accelerated headless rendering.
  214. Before version 2.1.4, MuJoCo used GLEW rather than GLAD to manage OpenGL symbols, which required linking against
  215. different GLEW libraries at build time depending on the GL implementation used. In order to avoid having manage OpenGL
  216. dependency when no rendering was required, "nogl" builds of the library was made available. Since OpenGL symbols are
  217. now lazily resolved at runtime after the switch to GLAD, the "nogl" libraries are no longer provided.
  218. .. toctree::
  219. :hidden:
  220. simulation
  221. visualization
  222. ui
  223. modeledit
  224. samples
  225. extension