Commit Graph

759 Commits

Author SHA1 Message Date
Yuval Tassa 67a1ea6dca Track authored flags for global attributes
PiperOrigin-RevId: 931585539
Change-Id: Ifdc8c59de6c5a553daf6e0af09d8192aff6b0610
2026-06-13 02:58:32 -07:00
Yuval Tassa 58f6d52491 Introduce new logging API, fixes #858
PiperOrigin-RevId: 930744288
Change-Id: I6ec1203b55c031390f3eef23192e2337508ce886
2026-06-11 14:36:57 -07:00
Yuval Tassa e349ea5834 Remove MJAPI from templated function declaration.
PiperOrigin-RevId: 928849785
Change-Id: I35ba63f599ba12a5d2bdb0d694240e39ca7d4174
2026-06-08 17:08:17 -07:00
Yuval Tassa 986d73c062 Fix non-unit normals in mesh convex hull compiler.
PiperOrigin-RevId: 928777934
Change-Id: I43d7adcf4c5781b57f0af9805a8ce01ebc2e3872
2026-06-08 14:40:11 -07:00
Alessio Quaglino 7a7dc7ccad Add support for loading 1D flex components from OBJ line segments.
The obj_decoder plugin is updated to parse line segments ('l') from OBJ files and represent them as degenerate triangles. The user_flexcomp compiler now supports flex dimensions of 1, using these parsed line segments to define the 1D flex elements. The compile sample's error reporting is also improved.

PiperOrigin-RevId: 928566870
Change-Id: I6920729d1b1d5aeb391401c3245ebb1d4784c190
2026-06-08 08:03:30 -07:00
Yuval Tassa ea2d785e0a Improve quality of maxhullvert coarse convex hulls using Qhull's Q9 option.
PiperOrigin-RevId: 928417339
Change-Id: I59ecd454f063f2e32e1c3ce08eb4a18506ddf8c8
2026-06-08 01:56:24 -07:00
Kevin Zakka 5fe916045c Replace strncpy with memcpy for gridlayout initialization.
memcpy is more appropriate here as the source and destination buffers have the same size, ensuring the entire buffer is copied.

PiperOrigin-RevId: 927395770
Change-Id: Idbe9dade3160f593060fae5783743b28c0452695
2026-06-05 11:25:49 -07:00
Alessio Quaglino 91c92279d2 Enable interior nodes for interpolated flex shell mode.
Previously shell mode required cellcount=1 along at least one axis. This CL
adds support for cellcount > 1 in all three axes by pinning interior grid nodes
to the parent body and reconstructing their positions from boundary nodes via
Transfinite Interpolation (TFI).

PiperOrigin-RevId: 924314800
Change-Id: I8c2438f4866dd4133feed65f535a1ab69f0c9188
2026-05-31 10:36:01 -07:00
Alessio Quaglino 4c381635e1 Add mjs_makeFlex to the MuJoCo C and Python APIs.
- Add mjs_makeFlex C API that wraps mjCFlexcomp::Make(), creating
  a flex with auto-generated bodies, joints, and equality constraints
  from parameters (type, dim, dof, count, cellcount, spacing, scale,
  radius, mass, inertiabox, equality, rigid, flatskin, elastic2d,
  pos, quat, origin, file, vfs).
- Add Python binding body.make_flex() in specs.cc with keyword args.
- Refactor flex_spec_provision.py to use make_flex() instead of XML
  string templating + separate spec + attach. Eliminates textwrap
  dependency.
- Merge user_flexcomp into user_model BUILD target to resolve
  circular dependency.

PiperOrigin-RevId: 922725304
Change-Id: Ice13787c45f3173407fec659f9590a6453bc65dd
2026-05-28 05:59:20 -07:00
Yuval Tassa 393c1e4217 Migrate mjtByte to mjtBool for boolean-valued fields.
PiperOrigin-RevId: 921408093
Change-Id: Icb00457836359779f0f72fc02d6a7c0dc9f6bd23
2026-05-26 05:35:49 -07:00
Yuval Tassa 2810edd27a Rename mjtnum.h to mjtype.h, move enum types to mjtype.h
PiperOrigin-RevId: 919020039
Change-Id: I441295c0baa0b7456f78239fbd5478d32ffc07c1
2026-05-21 06:43:29 -07:00
Yuval Tassa bdf00966f9 Add compiler timing diagnostics to mjsCompiler, printed by compile.cc
For example, `compile mujoco_menagerie/robotis_op3/scene.xml` now outputs

```
Compile 1 (cold cache):
  total:      317.2 ms
  assets:     284.8 ms (wall clock)
    load:     616.1 ms
    hull:      26.4 ms
    poly:     137.8 ms
    inert:    177.8 ms
    bvh:      568.2 ms
    octr:       1.6 ms
    tex:       25.3 ms
  other:       32.4 ms

Compile 2 (warm cache):
  total:       79.9 ms
  assets:      54.5 ms (wall clock)
    load:     888.5 ms
    hull:       0.0 ms
    poly:       0.0 ms
    inert:      0.0 ms
    bvh:        0.0 ms
    octr:       0.0 ms
    tex:       21.4 ms
  other:       25.3 ms
```

PiperOrigin-RevId: 917850214
Change-Id: Iaec86230bec0faf2e47820e20cbff61de5b2621e
2026-05-19 08:28:47 -07:00
Alessio Quaglino 66d764a116 Scale flex warp constraint by thickness^3.
Warp is not a true stretch mode but a parasitic shear mode so it should be scaled like bending (~thickness^3) rather than stretching (~thickness). The thickness parameter can therefore be used to set the stretch to warp stiffness ratio of the strain constraints.

PiperOrigin-RevId: 916900311
Change-Id: I52e54210fbb2a6d309abc1f084934830adce3423
2026-05-17 14:33:12 -07:00
Alessio Quaglino 8a20ce24d3 Refactor shell stiffness computation to use pure membrane modes plus an explicit warp mode.
PiperOrigin-RevId: 916879634
Change-Id: Ib706909521b4f503b13904a34b0e29aefd4fa433
2026-05-17 13:00:08 -07:00
Alessio Quaglino fa912dffa0 Add flex arrays to CompareModel check.
Due to a bug, all flex mjModel fields were previously ignored.

PiperOrigin-RevId: 912546202
Change-Id: Ica95ecfcbbe366fd81de6e8f7d83ed57a7d23033
2026-05-08 08:47:51 -07:00
Alessio Quaglino 8cef5bb978 Add configurable octree max depth for meshes.
The maximum depth of the octree used for SDF generation can now be specified in the mesh definition via `mjsMesh::octree_maxdepth`. The default value is 6.

PiperOrigin-RevId: 912494999
Change-Id: I6d828d1d3999b99d1210a6829a05b04fac591e1f
2026-05-08 06:35:45 -07:00
Sam Haves ec50260e26 Add mjs_getOriginSpec to retrieve the original spec of an element.
The new function mjs_getOriginSpec returns the mjSpec that was used to define a given mjsElement. Unlike mjs_getSpec, this value remains constant even after the element has been attached to a different model.

PiperOrigin-RevId: 911930951
Change-Id: Ia9cd79d9dfabc513d6121eadcffc5d41075224c9
2026-05-07 06:33:28 -07:00
Alessio Quaglino 0c05215e18 Reduce flex stiffness and bending memory size to the exact amount needed.
PiperOrigin-RevId: 911774147
Change-Id: I3b18eba23e75ec91ece8342945f25e0cd7c63a1a
2026-05-06 23:53:19 -07:00
Yuval Tassa 0f53f55853 Add checks for degenerate meshes in convex hull computation.
Fixes #3260

PiperOrigin-RevId: 911610731
Change-Id: I9efd15eee1bb5cb13639fd733a49535a147c3401
2026-05-06 16:25:13 -07:00
Sam Haves 579a27e9d2 Make mjSpec related getter/find utilities take const parameters.
PiperOrigin-RevId: 910848529
Change-Id: I1c966a6299fe47317cbf0bfb8f8b835cbcd4ed65
2026-05-05 12:35:56 -07:00
Alessio Quaglino d933b195ee Implement bending forces for interpolated flex shells.
This change adds a new passive force computation for flexes with elastic2d="bend" and dof="trilinear". The bending energy is based on the squared difference of normals between adjacent face elements at their shared edge midpoint. The edge data is precomputed during model compilation and stored in flex_bending.

PiperOrigin-RevId: 910772638
Change-Id: I3b12c7b7f1ba6ac1875df495d89e8cfec921ca80
2026-05-05 10:33:18 -07:00
Alessio Quaglino b9c1877ecb Project out rigid body modes from flex strain equality constraints.
Modify EigendecomposeStiffness to project out rigid body translations and rotations from the stiffness matrix eigenvectors. This prevents "ghost damping" from rigid body modes being incorrectly constrained. Update hollow_vs_solid.xml with solimp parameters for edge constraints. Adjust engine_core_constraint_test.cc to reflect the reduced number of equality constraints due to the projection.

PiperOrigin-RevId: 910631207
Change-Id: Ibaaa59bc030cfc6ae657d8f0d1b2a51002cc8d4e
2026-05-05 06:01:15 -07:00
Alessio Quaglino dbd451138c Add flex_bendingadr to mjModel.
PiperOrigin-RevId: 909088123
Change-Id: If062335d0aa3702c1e0b082dd9e29c3be0e76ea6
2026-05-02 00:13:39 -07:00
Alessio Quaglino 7d54123303 Make flex vert0 rotation-invariant for interpolated flexes.
The `vert0` array, used for parametric vertex coordinates in flexes, is now computed in the flex's local, unrotated frame when using interpolation. This ensures that the parametric coordinates are independent of the flex's initial orientation. A new test confirms that `flex_vert0` is identical for an unrotated and a rotated flex grid.

PiperOrigin-RevId: 908196464
Change-Id: I47d6bcc2bc5df581479d485480e0e947ec6d3ffd
2026-04-30 08:41:01 -07:00
Alessio Quaglino 9c6a4f76eb Add 2D membrane elasticity for interpolated flex shell mode
When elastic2d="stretch" is set on an interpolated flexcomp, treat the bounding box boundary as membrane elements rather than volumetric cells. This computes plane-stress stiffness over the boundary faces and updates the runtime force/derivative kernels accordingly.

Interior vertex tracking (moving vertices that follow the deforming shell) is not yet implemented so all mesh vertices need to be on the bounding box surface or the background grid should have no interior nodes (i.e. cellcount should be 1 on at least one axis).

PiperOrigin-RevId: 907654080
Change-Id: I51b90e2f6a1d1b036f9604e42de20e377dc5d3f9
2026-04-29 10:17:21 -07:00
Sam Haves d92fe0810c Add MjSpec.encode method.
PiperOrigin-RevId: 907177387
Change-Id: I65800298798037c4a0c0cd79ec86706be67db51e
2026-04-28 14:22:41 -07:00
Alessio Quaglino a891782553 Clean-up flex assumptions.
Do not allow a mix of `elastic2d != none` with `dof = trilinear` since the latter assumes 3d elasticity.

Also, do not assume that `flex_interp > 0` in the engine. This will enable to use, e.g., `flex_interp = -1` to mean a linear surface finite element instead of a 3d finite element which is currently identified with `flex_interp = 1`.

PiperOrigin-RevId: 903852035
Change-Id: Ia6290b4a05e9e510ffb7f36d141cd525b40d3110
2026-04-22 08:01:34 -07:00
Sam Haves 4cfebcc32b Add mj_containsFileVFS and mj_containsBufferVFS functions.
PiperOrigin-RevId: 903785790
Change-Id: I013b37a177284f8440179c4ae5c6221e0f572b49
2026-04-22 05:27:15 -07:00
Alessio Quaglino ba149aa043 Fix flexcomp strain constraints with rotated grids.
The reference node positions and the positions used for computing stiffness eigenvectors were previously stored in world frame. However, the runtime expects these quantities in the unrotated local frame. This caused non-zero constraint residuals and simulation instability when the grid was rotated — either by the parent body's initial orientation, or by the flexcomp's own frame attributes.

Rather than tracking each rotation source individually, this change extracts the total grid rotation directly from the cell geometry. All node positions are then un-rotated before computing the stiffness matrix.

PiperOrigin-RevId: 903232388
Change-Id: If877af89025ce1e61a76b38c29403d593d892749
2026-04-21 08:02:11 -07:00
Sam Haves da01bd37a2 Remove modelfiledir_ from compiled assets, use owning spec modelfiledir instead.
Previously specs that were attached to some parent spec would resolve its asset paths relative to the modelfiledir of the parent spec. This means path resolution would change depending on the source of the parent spec. Instead, this change makes asset file path resolution relative to the "owning spec" i.e. the spec where the asset was created.

This enables workflows such as loading a parent spec via resource provider, then loading a child spec via `from_zip` or in memory providing `spec.assets` and resolution will work as intended.

PiperOrigin-RevId: 903207910
Change-Id: Ia58020ab372a3ceadf31e804d145e2ae53d8e5f9
2026-04-21 07:00:05 -07:00
Alessio Quaglino 188196603d Fix flexcomp empty cell detection that was causing missing cells.
The previous implementation only checked whether grid cells contained mesh vertices to determine occupancy. For coarse meshes with large faces, most cells were incorrectly marked empty and pruned—even cells fully inside the object volume. This change improves the algorithm by:

- Marking cells that overlap with any mesh element's AABB as non-empty.
- For surface meshes, running a flood-fill from the grid boundary through non-overlapping cells to identify truly exterior cells. This preserves empty interior cells, preventing incorrect pruning of the object's core.
- For volumetric meshes, defaulting to element-AABB overlap detection directly.

Limitations for non-watertight meshes: If the mesh contains holes larger than the grid cell size, the flood-fill will leak into the interior. In this case, all non-element cells (including interior ones) will be marked as empty.

PiperOrigin-RevId: 902675331
Change-Id: I5a84303a33d5ca7436213e7ce9aca3806c9f5a0f
2026-04-20 09:27:40 -07:00
Alessio Quaglino 2d12dee025 Add dof="2d" option to flexcomp for in-plane deformations.
PiperOrigin-RevId: 902620719
Change-Id: Ib06d3f7b9439e1d90a8373a4289ade0c327e72e4
2026-04-20 07:14:40 -07:00
Alessio Quaglino 35f7db8e91 Add StringToVector<std::string> overload for char*.
This overload allows `StringToVector` to accept a `char*` argument when parsing into a `std::vector<std::string>`, by converting the `char*` to `std::string` before processing.

PiperOrigin-RevId: 902611443
Change-Id: I2048918f915c1bac74522f368ea28f23ca9a6885
2026-04-20 06:48:59 -07:00
Alessio Quaglino 508e581ba9 Optimize flex by pinning nodes in empty cells.
This change introduces an optimization for flexcomp objects defined by a mesh. It identifies grid cells that do not contain any mesh vertices and marks them as empty. Nodes that are exclusively part of empty cells are pinned, preventing them from moving. Stiffness computations are skipped for empty cells, reducing computational cost. The total mass is now distributed only among the non-pinned nodes.

PiperOrigin-RevId: 902565735
Change-Id: Id0a9a685536d5e18a3e42124a25ab08ff3a918f2
2026-04-20 04:41:17 -07:00
Alessio Quaglino fa7b36d111 Fix undefined reference errors for StringToVector with GCC and LTO.
This CL addresses link failures when building MuJoCo with GCC and Link Time Optimization (LTO) enabled:
- Declared the `StringToVector<std::string>(const std::string&)` specialization in `user_util.h` to prevent the compiler from incorrectly trying to instantiate the general template.
- Added an explicit instantiation for `StringToVector<unsigned char>(char*)` in `user_util.cc` to provide the definition needed by its `const std::string&` counterpart.

PiperOrigin-RevId: 902557141
Change-Id: I27b585cf1deb276d4150a5cbe6cf7e59c323a20a
2026-04-20 04:17:14 -07:00
Alessio Quaglino 3230cf99f9 Change flex constraints to eigenmodes of the stiffness matrix.
This provides a reduction from 26 to 18 constraints for trilinear and from 162 to 75 for quadratic. The assembly of the constraints becomes trivial. In total the speedup for a trilinear 3x3x3 grid is about 3x.

PiperOrigin-RevId: 902502398
Change-Id: I764772c7adef78da5a644f64701f842d36e4b543
2026-04-20 02:02:44 -07:00
Alessio Quaglino 3d45a33190 Refactor flex strain constraints to be per-cell.
Each mjEQ_FLEXSTRAIN equality now represents a single cell within a flex. This allows for more efficient sparse Jacobian computation by only considering the degrees of freedom of the nodes within each specific cell. This change gives a speedup of about 10x on a 3x3x3 model.

PiperOrigin-RevId: 902164069
Change-Id: I78eedf1d5cf39b8989fe9863c22d164922fc0efb
2026-04-19 07:21:18 -07:00
Alessio Quaglino b16383dfaf Use banded solver for implicit flex integration.
The flex interpolation stiffness matrix within the implicit/implicitfast solvers is now built and factorized in a banded format instead of a dense one. This involves:
-   Calculating the bandwidth based on the sparsity of the mass/damping matrix and the connectivity within flex cells.
-   Allocating and populating a banded matrix `H`.
-   Using `mju_cholFactorBand` and `mju_cholSolveBand` for factorization and solving.
This change improves performance for flexes with many DOFs but local coupling.

PiperOrigin-RevId: 901297952
Change-Id: I3efe06353d1903ea65ab30dc49685cede228bb68
2026-04-17 08:00:07 -07:00
Alessio Quaglino 6c7ed66781 Implement multi-cell finite element method for interpolated flexes.
This change introduces a `flex_cellcount` field to `mjModel` to specify the number of cells in each dimension for interpolated flexes. The stiffness computation, passive force calculation, and Jacobian derivatives are updated to operate on a per-cell basis, significantly improving performance by localizing computations to the nodes within each cell.

PiperOrigin-RevId: 901216393
Change-Id: Ic23132e609de11e71bb7fef8d1f139daad2ec264
2026-04-17 04:13:19 -07:00
Matija Kecman c23b5e8420 Fix WASM build issues, improve test coverage and improve cmake files
* Ensure compiler.usethread=0 is set before compilation in single-threaded WASM builds
* Explicitly enable exceptions for the single-threaded WASM module
* Test the single-threaded module in addition to the multi-threaded module in internal and external CI
* Consolidate wasm/tests/CMakeLists.txt into wasm/CMakeLists.txt, fixing the single-threaded benchmark test build which was missing shared linker flags

PiperOrigin-RevId: 901149728
Change-Id: If4946e29e5116610782d218906cf3f5940c1cdd0
2026-04-17 01:29:40 -07:00
Alessio Quaglino 5724158593 Refactor flex stiffness storage to support variable sizes.
This change introduces `nflexstiffness` and `flex_stiffnessadr` to allow flex stiffness matrices to have sizes other than the fixed 21 per element. Higher-order flexes can now store larger stiffness matrices based on their number of nodes. The `flex_stiffnessadr` array provides the starting index for each flex's stiffness data within the `flex_stiffness` array.

PiperOrigin-RevId: 899632263
Change-Id: Ie49182c46c3777acf0c6b492345edfcf62fb5e44
2026-04-14 09:39:00 -07:00
Yuval Tassa c004d144d1 Correct DC motor derivative calculation and enforce actearly.
PiperOrigin-RevId: 898983657
Change-Id: I008529eacf3e400696d26bd3a52a4cf4c1c12225
2026-04-13 10:56:58 -07:00
Alessio Quaglino 6b724616c0 Prevent deletion from an attached mjSpec.
This change disallows calling `mjs_delete` on an mjSpec that has been attached to another mjSpec via `mjs_attach`. Attempting to delete an element from an attached spec will now result in an error. The Python bindings for `mjSpec.delete` have been updated to raise a ValueError when this occurs.

PiperOrigin-RevId: 897266695
Change-Id: Ic0670125a3028191ec50eca890f02b5910ec8b03
2026-04-09 13:14:00 -07:00
Yuval Tassa 81720071b8 Changes to dcmotor:
- Remove `lugre:viscous`, should now be added directly to actuator `damping`. Trying to do this for the user was incompatible with default inheritance (compounding instead of overriding).
- Move voltage limiting from the `saturation` to the `controller` attribute.
- Fix indexing issues in default inheritance.

PiperOrigin-RevId: 897087642
Change-Id: I5388c2633e15c7e223992e7eb5d6a28db75a6438
2026-04-09 06:52:45 -07:00
Yuval Tassa 382474bb9d Minor improvements to dcmotor
PiperOrigin-RevId: 895907301
Change-Id: Ia50a6d06c1ede9894cc71f375db837d104f0211e
2026-04-07 08:11:13 -07:00
Yuval Tassa 70a7647ad9 Add <dcmotor> actuator and related docs and tests.
PiperOrigin-RevId: 892927987
Change-Id: I38ed6412801341ba03ddf5fe7b93a6081df24d37
2026-04-01 07:50:23 -07:00
Haroon Qureshi 75bbf03cfb Add PBR test model.
PiperOrigin-RevId: 890431107
Change-Id: Ib3b5fa775daa93b4c798483df226585441ea5c61
2026-03-27 07:05:08 -07:00
Haroon Qureshi 4fce53c31f Detect KTX files as image/ktx.
PiperOrigin-RevId: 889260070
Change-Id: I93a505bca3da87d05c71fe302f9e7ac31806ab90
2026-03-25 08:32:28 -07:00
Alessio Quaglino c49bc7c7c0 Warn when a non-rigid flex has no constraints or passive forces.
Add a warning in user_model.cc if a flex is not rigid and has no edge equality constraints or passive forces (edge stiffness, edge damping, or vertex damping). Such flexes would not affect the simulation. A test case is added to verify the warning.

PiperOrigin-RevId: 889215981
Change-Id: I481128e1456921b632719547c676d201fdfbe931
2026-03-25 06:50:30 -07:00
Sam Haves f5d3ce3451 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
2026-03-25 05:35:39 -07:00