Skip to content

Fixed missing body and joint indices remapping for equality constraints in builder.add_builder (follow up for #1023)#1149

Merged
vreutskyy merged 5 commits into
newton-physics:mainfrom
vreutskyy:1023-model-builder-duplicate-equality-constraints-for-every-world
Nov 28, 2025
Merged

Fixed missing body and joint indices remapping for equality constraints in builder.add_builder (follow up for #1023)#1149
vreutskyy merged 5 commits into
newton-physics:mainfrom
vreutskyy:1023-model-builder-duplicate-equality-constraints-for-every-world

Conversation

@vreutskyy

@vreutskyy vreutskyy commented Nov 28, 2025

Copy link
Copy Markdown
Member

Description

Fixed missing body and joint indices remapping for equality constraints in builder.add_builder (follow up for #1023/#1139)

Newton Migration Guide

Please ensure the migration guide for warp.sim users is up-to-date with the changes made in this PR.

  • The migration guide in docs/migration.rst is up-to date

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

  • Bug Fixes
    • Fixed incorrect handling of equality constraints when combining multiple simulation builders. Constraint relationships and references are now properly preserved during model merging, ensuring correct behavior in combined simulations.

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

…hysics#1023

Signed-off-by: Viktor Reutskyy <vreutskyy@nvidia.com>
…ts in builder.add_builder (follow up for newton-physics#1023)

Signed-off-by: Viktor Reutskyy <vreutskyy@nvidia.com>
Signed-off-by: Viktor Reutskyy <vreutskyy@nvidia.com>
@coderabbitai

coderabbitai Bot commented Nov 28, 2025

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The changes implement proper index remapping for equality constraints when merging builders in add_builder. Previously, equality constraint attributes were copied without adjusting body and joint indices; now they are explicitly remapped using start indices to ensure correct indexing in the merged model.

Changes

Cohort / File(s) Summary
Builder constraint remapping
newton/_src/sim/builder.py
Refactored add_builder to explicitly remap and extend equality constraint fields (body1, body2, joint1, joint2, and related attributes) by offsetting indices with start_body_idx and start_joint_idx, replacing the previous wholesale copy behavior.
Test assertions for index validation
newton/tests/test_equality_constraints.py
Added assertions to verify correct remapping of equality constraint body and joint indices across multiple merged worlds.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Requires understanding index offsetting semantics in the context of builder merging
  • Multiple equality constraint fields must be validated for correct remapping
  • Verify that start_body_idx and start_joint_idx are applied consistently across all affected constraint fields

Possibly related PRs

  • PR #1139: Adds per-constraint world tracking for equality constraints; directly extends the same constraint-merging logic being refactored here.
  • PR #772: Refactors indexing and index-offset logic during builder combination; shares similar index remapping patterns.
  • PR #460: Introduced equality constraint fields and lists to ModelBuilder; this PR extends that infrastructure with proper index offsetting.

Suggested reviewers

  • adenzler-nvidia
  • eric-heiden
  • mmacklin
  • gdaviet

Pre-merge checks and finishing touches

✅ 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 and specifically describes the main fix: remapping body and joint indices for equality constraints in builder.add_builder, with context linking to related issue #1023.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@adenzler-nvidia adenzler-nvidia 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.

nice catch, thanks!

@codecov

codecov Bot commented Nov 28, 2025

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!

@vreutskyy vreutskyy enabled auto-merge November 28, 2025 10:02

@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: 0

🧹 Nitpick comments (2)
newton/tests/test_equality_constraints.py (1)

170-207: Equality-constraint remapping test logic is sound

The per-world index expectations (3 bodies & 3 joints per robot, 2 constraints per world, constraints ordered as connect then joint) correctly mirror add_builder’s offset behavior and the way equality constraints are appended. The assertions on equality_constraint_body1/2 and equality_constraint_joint1/2 look accurate and should reliably catch future regressions in remapping.

Optionally, you could also assert on model.equality_constraint_world for the same indices to lock in the world assignment semantics, but the current checks are already strong for this PR’s intent.

newton/_src/sim/builder.py (1)

1523-1543: Equality-constraint remapping in add_builder() is consistent with body/joint offsets

Using start_body_idx and start_joint_idx here correctly maps the sub-builder’s constraint body/joint indices into the destination builder’s index space, and the explicit handling of -1 preserves world-sentinel semantics. Moving equality_constraint_* out of more_builder_attrs avoids duplicate, un-remapped copies, so the final arrays stay coherent with the new per-world tests.

If you ever touch this again, you might consider extracting this block into a small helper (e.g., _merge_equality_constraints(...)) for clarity, but functionally this looks solid.

📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8924f29 and 7b91f69.

📒 Files selected for processing (2)
  • newton/_src/sim/builder.py (1 hunks)
  • newton/tests/test_equality_constraints.py (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2025-11-24T08:05:21.390Z
Learnt from: adenzler-nvidia
Repo: newton-physics/newton PR: 1107
File: newton/_src/solvers/mujoco/kernels.py:973-974
Timestamp: 2025-11-24T08:05:21.390Z
Learning: In Newton's MuJoCo solver integration (newton/_src/solvers/mujoco/), the mapping between Newton joints and MuJoCo joints is not 1-to-1. Instead, each Newton DOF maps to a distinct MuJoCo joint. This means that for multi-DOF joints (like D6 with 6 DOFs), there will be 6 corresponding MuJoCo joints, each with its own properties (margin, solimp, solref, etc.). The mapping is done via dof_to_mjc_joint array, ensuring each DOF's properties are written to its own MuJoCo joint without overwriting.

Applied to files:

  • newton/tests/test_equality_constraints.py
📚 Learning: 2025-09-24T00:26:54.486Z
Learnt from: eric-heiden
Repo: newton-physics/newton PR: 710
File: newton/_src/utils/import_usd.py:597-599
Timestamp: 2025-09-24T00:26:54.486Z
Learning: In the Newton ModelBuilder class, `joint_count`, `body_count`, `articulation_count`, and `shape_count` are implemented as property decorated methods, so they should be accessed as attributes (e.g., `builder.joint_count`) rather than called as methods (e.g., `builder.joint_count()`).

Applied to files:

  • newton/_src/sim/builder.py
⏰ 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). (2)
  • GitHub Check: Run GPU Benchmarks / Run GPU Benchmarks on AWS EC2
  • GitHub Check: Run GPU Tests / Run GPU Unit Tests on AWS EC2

@vreutskyy vreutskyy added this pull request to the merge queue Nov 28, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 28, 2025
@vreutskyy vreutskyy enabled auto-merge November 28, 2025 12:22
@vreutskyy vreutskyy added this pull request to the merge queue Nov 28, 2025
Merged via the queue into newton-physics:main with commit 86ce96c Nov 28, 2025
20 checks passed
JerryJiehanWang pushed a commit to JerryJiehanWang/newton that referenced this pull request Dec 2, 2025
…ts in builder.add_builder (follow up for newton-physics#1023) (newton-physics#1149)

Signed-off-by: Viktor Reutskyy <vreutskyy@nvidia.com>
eric-heiden pushed a commit to eric-heiden/newton that referenced this pull request Jan 28, 2026
…ts in builder.add_builder (follow up for newton-physics#1023) (newton-physics#1149)

Signed-off-by: Viktor Reutskyy <vreutskyy@nvidia.com>
mmacklin pushed a commit to mmacklin/newton that referenced this pull request Apr 7, 2026
…ts in builder.add_builder (follow up for newton-physics#1023) (newton-physics#1149)

Signed-off-by: Viktor Reutskyy <vreutskyy@nvidia.com>
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