Skip to content

fix: support multiple worldbody elements in MJCF parser#1496

Merged
adenzler-nvidia merged 1 commit into
newton-physics:mainfrom
adenzler-nvidia:fix/mjcf-multiple-worldbody
Jan 29, 2026
Merged

fix: support multiple worldbody elements in MJCF parser#1496
adenzler-nvidia merged 1 commit into
newton-physics:mainfrom
adenzler-nvidia:fix/mjcf-multiple-worldbody

Conversation

@adenzler-nvidia

@adenzler-nvidia adenzler-nvidia commented Jan 29, 2026

Copy link
Copy Markdown
Member

MuJoCo allows multiple elements in a single model (e.g., when using to bring in separate MJCF files). Previously, Newton's parser only processed the first worldbody element.

Changed root.find('worldbody') to iterate over root.findall('worldbody') so all worldbody elements are processed into the world body, matching MuJoCo's behavior.

Added tests for:

  • Multiple inline worldbody elements with geoms and bodies
  • Multiple worldbody elements with sites
  • Includes that create multiple worldbody elements

Before your PR is "Ready for review"

  • Necessary tests have been added and new examples are tested (see newton/tests/test_examples.py)
  • Documentation is up-to-date
  • Code passes formatting and linting checks with pre-commit run -a

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced MJCF file parsing to correctly handle multiple worldbody elements with per-world configuration and defaults.
    • Improved processing of bodies, geoms, sites, and frames across multiple worldbodies.
  • Tests

    • Added comprehensive test coverage for multiple worldbody scenarios, including cross-worldbody site parsing and external content inclusion.

✏️ Tip: You can customize this high-level summary in your review settings.

MuJoCo allows multiple <worldbody> elements in a single model (e.g.,
when using <include> to bring in separate MJCF files). Previously,
Newton's parser only processed the first worldbody element.

Changed root.find('worldbody') to iterate over root.findall('worldbody')
so all worldbody elements are processed into the world body, matching
MuJoCo's behavior.

Added tests for:
- Multiple inline worldbody elements with geoms and bodies
- Multiple worldbody elements with sites
- Includes that create multiple worldbody elements
@coderabbitai

coderabbitai Bot commented Jan 29, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Modified MJCF parsing to support multiple worldbody elements instead of a single worldbody. Restructured parsing logic to loop through each worldbody and process its child elements (bodies, geoms, sites, frames) within per-world context. Added comprehensive test coverage for multiple worldbody scenarios.

Changes

Cohort / File(s) Summary
MJCF Multi-Worldbody Parsing
newton/_src/utils/import_mjcf.py
Replaced single worldbody lookup with loop over all worldbody elements. Moved body, geom, site, and frame processing blocks into per-world loop, enabling world-specific default merging and context-aware parsing.
Multiple Worldbody Tests
newton/tests/test_import_mjcf.py
Added TestMjcfMultipleWorldbody test suite with three tests covering multiple worldbody parsing, cross-worldbody site handling, and includes that create additional worldbodies.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • eric-heiden
  • vreutskyy
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding support for multiple worldbody elements in the MJCF parser, which is the primary objective of the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Jan 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@newton/tests/test_import_mjcf.py`:
- Around line 4183-4187: The xml_resolver function currently has an unused
parameter base_dir causing ARG001; rename that parameter to _base_dir (or prefix
with an underscore) in the xml_resolver definition so the signature reads
xml_resolver(_base_dir, file_path) (or explicitly ignore it) and update any
references accordingly to silence Ruff while keeping behavior intact for the
xml_resolver function used in the tests.

Comment thread newton/tests/test_import_mjcf.py

@vreutskyy vreutskyy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@adenzler-nvidia adenzler-nvidia added this pull request to the merge queue Jan 29, 2026
Merged via the queue into newton-physics:main with commit ff7a249 Jan 29, 2026
22 checks passed
mmacklin pushed a commit to mmacklin/newton that referenced this pull request Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants