Added a unified script to run mujoco examples#398
Conversation
Signed-off-by: Kenny Vilella <kvilella@nvidia.com>
📝 Walkthrough""" WalkthroughA new example script for simulating articulated robots using the Newton physics engine with MuJoCo-style models has been added. The script defines an Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Example
participant NewtonEngine
participant Renderer
participant MuJoCoViewer
User->>Example: Initialize (parse args, create Example)
Example->>NewtonEngine: Load robot model, set up simulation
loop Simulation steps
Example->>Example: step()
Example->>NewtonEngine: simulate()
alt Rendering enabled
Example->>Renderer: render()
end
alt MuJoCo viewer enabled
Example->>MuJoCoViewer: sync state and render
end
end
Suggested reviewers
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
Signed-off-by: Kenny Vilella <kvilella@nvidia.com>
There was a problem hiding this comment.
Actionable comments posted: 4
🧹 Nitpick comments (1)
newton/examples/example_mujoco.py (1)
16-23: Update module documentation to reflect all supported robot types.The module documentation mentions only "G1" and "humanoid articulation from xml", but the script actually supports multiple robot types (humanoid, g1, cartpole, quadruped) and both XML and URDF formats.
-# Example Sim G1 +# Example MuJoCo-style Robot Simulations # -# Shows how to set up a simulation of a rigid-body humanoid articulation -# from a xml using the newton.ModelBuilder(). +# Shows how to set up simulations of various rigid-body articulated robots +# (humanoid, g1, cartpole, quadruped) from XML or URDF files using the newton.ModelBuilder(). # Note this example does not include a trained policy.
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
newton/examples/example_mujoco.py(1 hunks)
🧰 Additional context used
🪛 Ruff (0.11.9)
newton/examples/example_mujoco.py
89-89: Unused noqa directive (non-enabled: PLC0415)
Remove unused noqa directive
(RUF100)
⏰ Context from checks skipped due to timeout of 900000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: run-newton-tests / newton-unittests (windows-latest)
- GitHub Check: run-newton-tests / newton-unittests (ubuntu-latest)
- GitHub Check: run-asv-benchmarks
🔇 Additional comments (1)
newton/examples/example_mujoco.py (1)
89-89: Keep thenoqadirective - it's correctly suppressing a linting warning.The
noqa: PLC0415directive is correctly used here to suppress the "import-outside-toplevel" warning for the optionaltqdmimport. The static analysis tool incorrectly flags this as unused.
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (3)
newton/examples/example_mujoco.py (3)
182-183: Raise an exception instead of just printing an error.When an unrecognized robot name is provided, the method should raise an exception to prevent undefined behavior in the rest of the code.
- print("Name of the provided robot not recognized: ", robot) - return + raise ValueError(f"Name of the provided robot not recognized: {robot}")
329-329: Fix AttributeError:use_mujocoattribute doesn't exist.The
Exampleclass doesn't defineself.use_mujoco, so referencingexample.use_mujococauses an AttributeError. The solver is created withuse_mujoco=Falsebut this parameter isn't stored as an accessible attribute.Store the
use_mujocovalue in the Example class or access it from the solver if available:- show_mujoco_viewer = args.show_mujoco_viewer and example.use_mujoco + # Since solver is created with use_mujoco=False, we can check the args directly + show_mujoco_viewer = args.show_mujoco_viewer and FalseOr store the value during initialization:
# In __init__ method, add: + self.use_mujoco = False # or pass as parameter
344-345: Fix AttributeError:solver.use_mujocoattribute access.The code references
example.solver.use_mujocobut based on the MuJoCoSolver initialization on line 199, theuse_mujocoparameter is passed to the constructor but may not be stored as an accessible attribute.Since the solver is created with
use_mujoco=False, you can either:
- Check if the MuJoCoSolver exposes this attribute
- Use the known value directly since it's hardcoded to False
- if not example.solver.use_mujoco: + # Since use_mujoco is hardcoded to False in solver creation + if True: # Always true since use_mujoco=False
🧹 Nitpick comments (1)
newton/examples/example_mujoco.py (1)
17-22: Update the header comment to reflect the generic nature of the script.The comment describes this as "Example Sim G1" but the script actually supports multiple robot types (humanoid, g1, cartpole, quadruped). Update the comment to reflect its generic, unified nature.
-# Example Sim G1 +# Example MuJoCo Unified Script # -# Shows how to set up a simulation of a rigid-body humanoid articulation -# from a xml using the newton.ModelBuilder(). -# Note this example does not include a trained policy. +# Shows how to set up simulations for various articulated robots +# (humanoid, g1, cartpole, quadruped) using newton.ModelBuilder(). +# Supports multiple robots to avoid code duplication and enable scaling.
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
newton/examples/example_mujoco.py(1 hunks)
🧰 Additional context used
🪛 Ruff (0.11.9)
newton/examples/example_mujoco.py
102-102: Unused noqa directive (non-enabled: PLC0415)
Remove unused noqa directive
(RUF100)
⏰ Context from checks skipped due to timeout of 900000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: run-newton-tests / newton-unittests (windows-latest)
- GitHub Check: run-newton-tests / newton-unittests (ubuntu-latest)
- GitHub Check: run-asv-benchmarks
🔇 Additional comments (3)
newton/examples/example_mujoco.py (3)
35-69: Well-structured class initialization.The constructor provides comprehensive configuration options with sensible defaults. The fixed seed ensures reproducible simulations, and the parameter organization is logical.
185-218: Model setup and solver initialization looks correct.The multi-environment setup, solver configuration, and state initialization follow Newton best practices. The conditional renderer setup properly handles headless mode.
219-259: Simulation and rendering methods are well-implemented.The CUDA graph setup properly checks device compatibility, the simulation loop correctly handles substeps with collision detection, and rendering is appropriately optional with contact visualization support.
Signed-off-by: Kenny Vilella <kvilella@nvidia.com>
Signed-off-by: Kenny Vilella <kvilella@nvidia.com>
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (1)
newton/examples/example_mujoco.py (1)
360-361: Fix AttributeError:solver.use_mujocoattribute access.The code references
example.solver.use_mujocobut based on the MuJoCoSolver initialization on lines 199-208, theuse_mujocoparameter is passed to the constructor, not stored as an attribute.Either store the
use_mujocovalue in the Example class during initialization or check if the MuJoCoSolver exposes this attribute.- if not example.solver.use_mujoco: + if not example.use_mujoco:
🧹 Nitpick comments (2)
newton/examples/example_mujoco.py (2)
104-104: Remove unusednoqadirective.The
noqadirective is unnecessary since the import error handling makes thePLC0415rule irrelevant here.- import tqdm # noqa: PLC0415 + import tqdm
36-229: Consider refactoring the constructor for better maintainability.The
__init__method is quite long (~194 lines) and handles multiple robot configurations. Consider extracting robot-specific setup into separate methods (e.g.,_setup_humanoid(),_setup_g1(), etc.) to improve readability and maintainability.This would also make it easier to add new robot types in the future and reduce the complexity of the main constructor.
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
newton/examples/example_mujoco.py(1 hunks)
🧰 Additional context used
🪛 Ruff (0.11.9)
newton/examples/example_mujoco.py
104-104: Unused noqa directive (non-enabled: PLC0415)
Remove unused noqa directive
(RUF100)
⏰ Context from checks skipped due to timeout of 900000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: run-asv-benchmarks
- GitHub Check: run-newton-tests / newton-unittests (ubuntu-latest)
- GitHub Check: run-newton-tests / newton-unittests (windows-latest)
🔇 Additional comments (5)
newton/examples/example_mujoco.py (5)
1-33: LGTM: Clean imports and setup.The license header, imports, and initial configuration are well-structured and appropriate for this physics simulation example.
230-236: LGTM: Clean simulation stepping logic.The simulation method correctly implements the physics stepping sequence with force clearing, collision detection, solver stepping, and state buffer swapping.
237-248: LGTM: Proper step implementation with CUDA graph support.The method correctly handles both CUDA graph capture and direct simulation execution, with appropriate random actuation when specified.
249-259: LGTM: Clean rendering implementation.The render method properly handles null renderer checks and implements the standard rendering sequence with optional contact visualization.
261-369: Well-structured main script with comprehensive CLI options.The main script provides excellent configurability through command-line arguments and handles device setup properly. The simulation loop is clean and includes appropriate MuJoCo viewer integration when requested.
Signed-off-by: Kenny Vilella <kvilella@nvidia.com>
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (1)
newton/examples/example_mujoco.py (1)
367-367: Fix AttributeError:solver.use_mujocoattribute access.Based on the MuJoCoSolver initialization, the
use_mujocoparameter is passed to the constructor but not stored as an attribute. Use the value stored in the Example class instead.- if not example.solver.use_mujoco: + if not example.use_mujoco:
🧹 Nitpick comments (2)
newton/examples/example_mujoco.py (2)
111-111: Remove unusednoqadirective.The
noqa: PLC0415directive is not needed here as PLC0415 is not enabled in your linting configuration.- import tqdm # noqa: PLC0415 + import tqdm
93-188: Consider refactoring repetitive parameter initialization.The parameter initialization code is repeated for each robot type with different default values. This could be simplified using a configuration dictionary to improve maintainability and reduce code duplication.
Define a configuration dictionary at the class level:
ROBOT_CONFIGS = { "humanoid": { "solver": "newton", "integrator": "euler", "solver_iteration": 10, "ls_iteration": 5, "njmax": 100, "nconmax": 50, }, "g1": { "solver": "newton", "integrator": "euler", "solver_iteration": 5, "ls_iteration": 5, "njmax": 300, "nconmax": 150, }, # ... other robots }Then replace the repetitive code with:
- # Setting mujoco_warp parameters - solver = solver if solver is not None else "newton" - integrator = integrator if integrator is not None else "euler" - solver_iteration = solver_iteration if solver_iteration is not None else 10 - ls_iteration = ls_iteration if ls_iteration is not None else 5 - njmax = njmax if njmax is not None else 100 - nconmax = nconmax if nconmax is not None else 50 + # Setting mujoco_warp parameters + config = self.ROBOT_CONFIGS[robot] + solver = solver if solver is not None else config["solver"] + integrator = integrator if integrator is not None else config["integrator"] + solver_iteration = solver_iteration if solver_iteration is not None else config["solver_iteration"] + ls_iteration = ls_iteration if ls_iteration is not None else config["ls_iteration"] + njmax = njmax if njmax is not None else config["njmax"] + nconmax = nconmax if nconmax is not None else config["nconmax"]
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
newton/examples/example_mujoco.py(1 hunks)
🧰 Additional context used
🪛 Ruff (0.11.9)
newton/examples/example_mujoco.py
111-111: Unused noqa directive (non-enabled: PLC0415)
Remove unused noqa directive
(RUF100)
⏰ Context from checks skipped due to timeout of 900000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: run-newton-tests / newton-unittests (ubuntu-latest)
- GitHub Check: run-newton-tests / newton-unittests (windows-latest)
- GitHub Check: run-asv-benchmarks
adenzler-nvidia
left a comment
There was a problem hiding this comment.
Thanks - left some comments. High-level question for me is whether all the robot-specific config should be in some config file or a dict/list at the top of the file? Also to make it easier to configure a new robot?
Kenny-Vilella
left a comment
There was a problem hiding this comment.
For your high level questions, I actually though about it.
My impression is that with the current number of robots the current approach was more readable and that we should switch to your suggestion when increasing the number of robots.
But maybe I can just do it right now in order to not let the situation worsen.
Signed-off-by: Kenny Vilella <kvilella@nvidia.com>
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
newton/examples/example_mujoco.py (1)
97-97: Remove unusednoqadirective.The
noqadirective is not needed sincePLC0415is not enabled in the linting configuration.- import tqdm # noqa: PLC0415 + import tqdm
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
newton/examples/example_mujoco.py(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: Kenny-Vilella
PR: newton-physics/newton#398
File: newton/examples/example_mujoco.py:352-352
Timestamp: 2025-07-14T03:57:29.659Z
Learning: The use_mujoco option in newton/examples/example_mujoco.py is currently unsupported and causes crashes. The code automatically disables this option with a warning message when users attempt to enable it. This is intentionally kept as a placeholder for future implementation.
newton/examples/example_mujoco.py (1)
Learnt from: Kenny-Vilella
PR: newton-physics/newton#398
File: newton/examples/example_mujoco.py:352-352
Timestamp: 2025-07-14T03:57:29.659Z
Learning: The use_mujoco option in newton/examples/example_mujoco.py is currently unsupported and causes crashes. The code automatically disables this option with a warning message when users attempt to enable it. This is intentionally kept as a placeholder for future implementation.
🪛 Ruff (0.11.9)
newton/examples/example_mujoco.py
97-97: Unused noqa directive (non-enabled: PLC0415)
Remove unused noqa directive
(RUF100)
⏰ Context from checks skipped due to timeout of 900000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: run-newton-tests / newton-unittests (ubuntu-latest)
- GitHub Check: run-newton-tests / newton-unittests (windows-latest)
- GitHub Check: run-asv-benchmarks
🔇 Additional comments (5)
newton/examples/example_mujoco.py (5)
42-67: Well-structured robot configuration dictionary.The
ROBOT_CONFIGSdictionary provides a clean, maintainable way to define robot-specific parameters. This aligns well with the PR objective of avoiding code duplication across robot examples.
70-155: Robot setup functions are well-implemented.The individual robot setup functions (
_setup_humanoid,_setup_g1,_setup_cartpole,_setup_quadruped) are well-structured and handle robot-specific initialization properly. The G1 setup includes mesh simplification which is a good performance optimization.
158-286: Example class provides comprehensive simulation functionality.The
Exampleclass is well-designed with:
- Proper initialization of simulation parameters
- CUDA graph support for performance optimization
- Flexible configuration options
- Clean separation of concerns between simulation, stepping, and rendering
The implementation correctly handles the disabled
use_mujocoandrender_contactoptions as noted in the retrieved learnings.
347-352: Appropriate handling of unsupported options.The code correctly disables the
use_mujocoandrender_contactoptions with clear warning messages, which aligns with the retrieved learnings about these features being unsupported and causing crashes.
289-397: Comprehensive command-line interface.The argument parsing and main execution loop provide a flexible interface that supports:
- Multiple robot types
- Configurable simulation parameters
- Optional rendering and MuJoCo viewer integration
- CUDA graph optimization
This successfully achieves the PR objective of creating a unified script with extensive customization options while avoiding code duplication.
Signed-off-by: Kenny Vilella <kvilella@nvidia.com>
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
newton/examples/example_mujoco.py (3)
96-96: Remove unusednoqadirective.The
noqadirective forPLC0415is unused and should be removed to keep the code clean.- import tqdm # noqa: PLC0415 + import tqdm
41-66: Consider increasing default solver iteration counts.The current default solver iteration counts (e.g., 100 for humanoid) are relatively low. Based on the past review discussions, these could be set higher by default since conditional termination is available, and users ideally shouldn't need to worry about these details.
Consider increasing the default iteration counts across all robot configurations to ensure better convergence while maintaining the conditional termination safety net.
185-186: Consider making iteration defaults more explicit.The solver and line search iteration defaults are set inline. Consider moving these to the robot configuration constants or making them named constants for better maintainability.
+DEFAULT_SOLVER_ITERATION = 100 +DEFAULT_LS_ITERATION = 50 + # In __init__ method: - solver_iteration = solver_iteration if solver_iteration is not None else 100 - ls_iteration = ls_iteration if ls_iteration is not None else 50 + solver_iteration = solver_iteration if solver_iteration is not None else DEFAULT_SOLVER_ITERATION + ls_iteration = ls_iteration if ls_iteration is not None else DEFAULT_LS_ITERATION
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
newton/examples/example_mujoco.py(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: Kenny-Vilella
PR: newton-physics/newton#398
File: newton/examples/example_mujoco.py:352-352
Timestamp: 2025-07-14T03:57:29.659Z
Learning: The use_mujoco option in newton/examples/example_mujoco.py is currently unsupported and causes crashes. The code automatically disables this option with a warning message when users attempt to enable it. This is intentionally kept as a placeholder for future implementation.
newton/examples/example_mujoco.py (1)
Learnt from: Kenny-Vilella
PR: newton-physics/newton#398
File: newton/examples/example_mujoco.py:352-352
Timestamp: 2025-07-14T03:57:29.659Z
Learning: The use_mujoco option in newton/examples/example_mujoco.py is currently unsupported and causes crashes. The code automatically disables this option with a warning message when users attempt to enable it. This is intentionally kept as a placeholder for future implementation.
🪛 Ruff (0.11.9)
newton/examples/example_mujoco.py
96-96: Unused noqa directive (non-enabled: PLC0415)
Remove unused noqa directive
(RUF100)
⏰ Context from checks skipped due to timeout of 900000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: run-asv-benchmarks
- GitHub Check: run-newton-tests / newton-unittests (windows-latest)
- GitHub Check: run-newton-tests / newton-unittests (ubuntu-latest)
🔇 Additional comments (4)
newton/examples/example_mujoco.py (4)
334-336: LGTM: Proper handling of unsupported use_mujoco option.The code correctly disables the unsupported
use_mujocooption with a clear warning message. This is a good approach to prevent crashes while keeping the option available for future implementation.
248-250: Good CUDA graph compatibility check.The code properly validates CUDA graph compatibility by checking both CUDA device availability and mempool support, with appropriate fallback messaging.
358-373: Robust MuJoCo viewer integration.The MuJoCo viewer integration properly handles the conditional viewer launch and synchronization, with appropriate checks for the solver state.
205-205: Excellent error handling for unrecognized robots.The code properly raises a
ValueErrorwith a descriptive message for unrecognized robot names, which is much better than the previous print/return pattern.
|
I think it would be better to turn this example into unit tests. These hard coded settings will be difficult to keep updated in the future otherwise. The examples are really just meant to show how one can set up an asset and simulate it in Newton, as a tutorial. Having this boilerplate code around it makes it really complicated. |
|
@eric-heiden The main reason of this PR is for benchmarking. So maybe this script is not at the correct place, or with the correct name, but we still need something equivalent to that. |
Does it makes sense then to turn this into a benchmark script? Maybe those could benefit from more code reuse? Although we have some of these models already in the benchmarks. What might be more useful is if the assets came from USD files so we make sure they run properly and fast. |
Signed-off-by: Kenny Vilella <kvilella@nvidia.com> Co-authored-by: Eric Heiden <eheiden@nvidia.com>
# Description This change enables the Fabric Scene Delegate (FSD), which should help speed up workflows requiring rendering as it optimizes the transformation updates for rendering. Fixes # (issue) <!-- As a practice, it is recommended to open an issue to have discussions on the proposed pull request. This makes it easier for the community to keep track of what is being developed or added, and if a given feature is demanded by more than one party. --> ## Type of change <!-- As you go through the list, delete the ones that are not applicable. --> - New feature (non-breaking change which adds functionality) - Breaking change (fix or feature that would cause existing functionality to not work as expected) ## Checklist - [ ] I have run the [`pre-commit` checks](https://pre-commit.com/) with `./isaaclab.sh --format` - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I have updated the changelog and the corresponding version in the extension's `config/extension.toml` file - [ ] I have added my name to the `CONTRIBUTORS.md` or my name already exists there <!-- As you go through the checklist above, you can mark something as done by putting an x character in it For example, - [x] I have done this task - [ ] I have not done this task --> --------- Co-authored-by: Kelly Guo <kellyg@nvidia.com>
Signed-off-by: Kenny Vilella <kvilella@nvidia.com> Co-authored-by: Eric Heiden <eheiden@nvidia.com>
Description
Created an
example_mujoco.pyscript that takes several predefined robot and run an example.Multiple options are available.
This will allow us to avoid code duplication and scale our example to a large number of robots.
Newton Migration Guide
Please ensure the migration guide for warp.sim users is up-to-date with the changes made in this MR.
docs/migration.rstis up-to dateBefore your PR is "Ready for review"
newton/tests/test_examples.py)pre-commit run -aSummary by CodeRabbit