Add special case support for MJB and TXT files to mj_encode.

PiperOrigin-RevId: 939875974
Change-Id: I57fc753871b348a7f6342aca0a892e74acb74173
This commit is contained in:
Sam Haves
2026-06-29 09:31:49 -07:00
committed by Copybara-Service
parent d83ef0b6b9
commit 4e1795b9a4
5 changed files with 182 additions and 11 deletions
+12 -3
View File
@@ -66,11 +66,20 @@ Parse spec from a file.
.. mujoco-include:: mj_encode
Encode spec/model to a file using a registered encoder.
Encode :ref:`mjSpec` or :ref:`mjModel` to a file. The output format is determined by the file extension (case insensitive) or
``content_type``. Returns the number of bytes written on success, -1 on failure.
Returns the number of bytes written on success, -1 on failure.
The following formats are supported natively, without a registered encoder:
*Nullable:* ``m``, ``vfs``, ``error``
- **MJCF XML** — extension: ``.xml``, content_type: ``text/xml``. If an :ref:`mjSpec` is provided, saves via
:ref:`mj_saveXML`. Otherwise falls back to :ref:`mj_saveLastXML`, which requires a compiled :ref:`mjModel`.
- **MJB** — extension: ``.mjb``. MuJoCo binary format. Requires a compiled :ref:`mjModel`.
- **TXT** — extension: ``.txt``, content_type: ``text/plain``. Human-readable text dump via :ref:`mj_printModel`.
Requires a compiled :ref:`mjModel`.
For all other formats, a registered encoder is looked up via :ref:`mjp_findEncoder`.
*Nullable:* ``s``, ``m``, ``vfs``, ``error``
.. _mj_compile:
+17
View File
@@ -72,6 +72,23 @@ the required size. XML saving automatically compiles the spec before saving.
Save spec to XML file, return 0 on success, -1 otherwise. XML saving requires that the spec first be compiled.
.. _mj_encode:
Encode :ref:`mjSpec` or :ref:`mjModel` to a file. The output format is determined by the file extension (case insensitive) or
``content_type``. Returns the number of bytes written on success, -1 on failure.
The following formats are supported natively, without a registered encoder:
- **MJCF XML** — extension: ``.xml``, content_type: ``text/xml``. If an :ref:`mjSpec` is provided, saves via
:ref:`mj_saveXML`. Otherwise falls back to :ref:`mj_saveLastXML`, which requires a compiled :ref:`mjModel`.
- **MJB** — extension: ``.mjb``. MuJoCo binary format. Requires a compiled :ref:`mjModel`.
- **TXT** — extension: ``.txt``, content_type: ``text/plain``. Human-readable text dump via :ref:`mj_printModel`.
Requires a compiled :ref:`mjModel`.
For all other formats, a registered encoder is looked up via :ref:`mjp_findEncoder`.
*Nullable:* ``s``, ``m``, ``vfs``, ``error``
.. _Mainsimulation:
These are the main entry points to the simulator. Most users will only need to call :ref:`mj_step`, which computes