Introduce mjpEncoder plugin architecture

Add a new mjpEncoder plugin type mirroring the existing mjpDecoder pattern.
Encoders serialize an mjSpec + mjModel to an mjResource for a given format.

New API functions:
- mjp_registerEncoder: globally register an encoder
- mjp_defaultEncoder: zero-initialize an encoder struct
- mjp_findEncoder: look up an encoder by filename extension or content type

The mjfEncode callback takes (mjSpec*, mjModel*, mjVFS*, mjResource*) and
returns 0 on success. Writing to mjResource keeps symmetry with the decoder
reading from mjResource and leaves the door open for writable resource providers.

PiperOrigin-RevId: 889187898
Change-Id: I180771b2255b91dea188ac5e2cdc3a8f0fb85364
This commit is contained in:
Sam Haves
2026-03-25 05:34:48 -07:00
committed by Copybara-Service
parent 2d33b50243
commit f5d3ce3451
15 changed files with 617 additions and 0 deletions
+46
View File
@@ -59,6 +59,19 @@ Parse spec from a file.
*Nullable:* ``vfs``, ``error``
.. _mj_encode:
`mj_encode <#mj_encode>`__
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. mujoco-include:: mj_encode
Encode spec/model to a file using a registered encoder.
Returns the number of bytes written on success, -1 on failure.
*Nullable:* ``m``, ``vfs``, ``error``
.. _mj_compile:
`mj_compile <#mj_compile>`__
@@ -3255,6 +3268,39 @@ Return the resource provider with the prefix that matches against the resource n
If no match, return NULL.
.. _mjp_registerEncoder:
`mjp_registerEncoder <#mjp_registerEncoder>`__
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. mujoco-include:: mjp_registerEncoder
Globally register an encoder. This function is thread-safe.
If an identical mjpEncoder is already registered, this function does nothing.
If a non-identical mjpEncoder with the same name is already registered, an mju_error is raised.
.. _mjp_defaultEncoder:
`mjp_defaultEncoder <#mjp_defaultEncoder>`__
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. mujoco-include:: mjp_defaultEncoder
Set default resource encoder definition.
.. _mjp_findEncoder:
`mjp_findEncoder <#mjp_findEncoder>`__
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. mujoco-include:: mjp_findEncoder
Return the encoder that matches against the content type or filename extension.
If no match, return NULL.
.. _Thread:
Threads