Skip to content

Randomizing mass

65 · Domain randomization

Five copies of a drive-held arm in five worlds. At 1 s the script scales each world's body_mass and body_inertia by a sampled factor (and the inverse arrays by its reciprocal), then calls notify_model_changed(BODY_INERTIAL_PROPERTIES). A heavier arm sags further against the same drive stiffness, roughly in proportion to its mass at these small angles. With the default update_mass_matrix_interval=1 Steel rebuilds the mass matrix from body_mass every step, but the notification also refreshes the solver's cached spatial inertias and forces a rebuild, so call it whenever you write these arrays.

0.00 s

What to look at

  • All arms are identical for the first second.
  • After 1 s the sag angle grows with the sampled mass factor.

Key parameter

solver.notify_model_changed(ModelFlags.BODY_INERTIAL_PROPERTIES)

Covers body_mass, body_inv_mass, body_inertia, body_inv_inertia and body_com. Keep each inverse array consistent with its forward array.

Try changing

  • Scale only body_mass and leave the inertia alone, as many mass randomizers do.
  • Widen the range to x0.2 to x5 and lower KE until the heaviest arm touches its own hinge limit.
  • Write the arrays from a Warp kernel instead of NumPy so the randomization stays on the GPU.

Concept

When Steel sees a randomized value

What it is

Notify with the matching flags after changing these model arrays so Steel refreshes all derived data consistently. Some inputs are cached until notification; mass, inertia and drive gains can also be picked up by periodic mass-matrix rebuilds, so their behavior without notification depends on update_mass_matrix_interval.

In this scene

After 1 s the sag angle grows with the sampled mass factor.

Considerations

Cost
Call only, default solver, six-link chain and four boxes per world, 1024 worlds (1.12 ms per collide + step): BODY_INERTIAL_PROPERTIES 31 µs (17 µs captured). Flags measured one at a time; combined calls and feature validation can cost more. Body-inertial and joint-DOF notifications also request a mass-matrix refresh.
Graph capture
These flags can be captured in a reset graph. Kinematic-flag changes, sleeping, and shape flags with sparse factors plus restitution run eagerly; captured calls skip some value checks.
Interactions
world_mask limits which worlds lose their warm start; cached data is refreshed for every world.
Use it when
Payload and link-mass variation; scale inertia with mass unless you want a different shape.

The script

cd tutorials && python dr_mass.py prints each variant's final metric; --record out.json also writes the replay. Every tutorial imports replay.py from the same folder.

tutorials/dr_mass.py
# SPDX-FileCopyrightText: Copyright (c) 2026 Skild AI and the Steel contributors
# SPDX-License-Identifier: Apache-2.0
"""Mass randomization: per-world body mass and inertia + BODY_INERTIAL_PROPERTIES."""
import newton
import numpy as np
import warp as wp
from newton.solvers import SolverSteel
from replay import Recorder
DT = 1.0 / 240.0
SUBSTEPS = 8
WORLDS = 5
KE, KD = 15.0, 1.0 # hinge drive holding the arm level
def tf(x, y, z):
return wp.transform((x, y, z), wp.quat_identity())
def build():
arm = newton.ModelBuilder()
link = arm.add_link(xform=tf(0.0, 0.0, 0.8))
arm.add_shape_box(link, xform=tf(0.2, 0.0, 0.0), hx=0.2, hy=0.02, hz=0.02)
arm.add_shape_sphere(link, xform=tf(0.4, 0.0, 0.0), radius=0.05)
j = arm.add_joint_revolute(
-1, link, axis=newton.Axis.Y, parent_xform=tf(0.0, 0.0, 0.8), target_ke=KE, target_kd=KD
)
arm.add_articulation([j])
builder = newton.ModelBuilder()
builder.replicate(arm, WORLDS, spacing=(0.0, 0.35, 0.0))
return builder.finalize()
def randomize_mass(model, scale):
"""Scale every body of world w by scale[w]; keep the inverse arrays consistent."""
s = scale[model.body_world.numpy()]
model.body_mass.assign(model.body_mass.numpy() * s)
model.body_inv_mass.assign(model.body_inv_mass.numpy() / s)
model.body_inertia.assign(model.body_inertia.numpy() * s[:, None, None])
model.body_inv_inertia.assign(model.body_inv_inertia.numpy() / s[:, None, None])
def simulate(rec, label, scale):
model = build()
solver = SolverSteel(model)
state_0, state_1, control = model.state(), model.state(), model.control()
newton.eval_fk(model, model.joint_q, model.joint_qd, state_0)
run = rec.variant(label, model)
for frame in range(rec.frames(4.0)):
if frame == rec.fps: # randomize at 1 s, as an episode reset would
randomize_mass(model, scale)
solver.notify_model_changed(newton.ModelFlags.BODY_INERTIAL_PROPERTIES)
sag = np.degrees(state_0.joint_q.numpy())
run.frame(state_0, **{f"mass x{s:.2f}": a for s, a in zip(scale, sag)})
for _ in range(SUBSTEPS):
solver.step(state_0, state_1, control, None, DT)
state_0, state_1 = state_1, state_0
run.check(solver)
if __name__ == "__main__":
rng = np.random.default_rng(0)
scale = np.sort(rng.uniform(0.5, 2.0, WORLDS))
rec = Recorder()
simulate(rec, "5 worlds, mass x0.5 to x2", scale)
rec.save()

Recorded with SolverSteel on Newton 1.9.0.dev0, Warp 1.19.0, NVIDIA RTX PRO 6000 Blackwell Server Edition. Replays read the state back every frame, so they are not a speed benchmark. Script · page text