USD decoder applies newton:inertia without clearing physics:diagonalInertia and physics:principalAxes

オープン 初心者向け
#3,932 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 6 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
cpp, python
領域
backend, robotics

調査の方向性

plugin/usd_decoder/usd_decoder.ccを読み、特に922~953行付近のParseUsdPhysicsMassAPIForBodyと、3373~3398行付近のsrc/user/user_objects.ccにある質量検証を確認してください。提供されたUSDステージで再現し、デコードされた慣性をMJCFの参照値と比較してください。設定されたnewton:inertiaがフォールバック属性を上書きし、デコードされた慣性がソースMJCFと一致すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

My setup

MuJoCo 4.0.1 with mujoco-usd 4.0.1 from PyPI (usd-core 25.11, newton-usd-schemas 0.7.0), Python 3.12, Linux x86_64. The same code is on main.

What's happening? What did you expect?

NewtonMassAPI's newton:inertia is a full inertia tensor in the body's local frame that, per the schema, overrides physics:diagonalInertia and physics:principalAxes when authored. Writers can author both so that readers without the Newton schemas still get the principal-axis form, and mujoco-usd-converter does this for every MJCF body with fullinertia.

The decoder does not apply the override. It copies physics:principalAxes to iquat and physics:diagonalInertia to inertia, then copies newton:inertia to fullinertia and leaves the other two set. Compilation fails:

Error: fullinertia and diagonal inertia cannot both be specified

This blocks every converted MJCF that uses fullinertia. In MuJoCo Menagerie that is 10 of 54 scenes, including franka_emika_panda, unitree_a1, agility_cassie, and robotstudio_so101.

Removing only physics:diagonalInertia is not a workaround. The model then compiles, but the compiler rotates fullinertia by iquat, which still holds physics:principalAxes, so a tensor that is already in the body frame is rotated a second time and the inertia is wrong.

I expected the decoder, when newton:inertia is authored, to set fullinertia and reset inertia to zero and iquat to identity, so the tensor is used as authored in the body frame and the result matches the source MJCF.

The relevant code at 4.0.1:

Steps for reproduction
  1. pip install mujoco-usd==4.0.1
  2. Run the code below. It builds a body with mass 1 and the tensor 0.3 0.25 0.2 0.01 0 0 three ways and compares each decoded inertia with the same body in MJCF.
  3. Authoring all three attributes fails. newton:inertia alone matches the MJCF exactly. physics:principalAxes with newton:inertia compiles with the wrong inertia.
diagonalInertia + principalAxes + newton:inertia: Error: fullinertia and diagonal inertia cannot both be specified
newton:inertia only: compiles, max |inertia - MJCF| = 0
principalAxes + newton:inertia: compiles, max |inertia - MJCF| = 0.00857
Minimal model for reproduction

The stages are built in memory by the code below. The equivalent MJCF:

minimal XML
<mujoco>
  <worldbody>
    <body name="b">
      <freejoint/>
      <inertial pos="0 0 0" mass="1" fullinertia="0.3 0.25 0.2 0.01 0 0"/>
      <geom type="sphere" size="0.1"/>
    </body>
  </worldbody>
</mujoco>
Code required for reproduction
import mujoco
import numpy as np
from pxr import Gf, Sdf, Usd, UsdGeom, UsdPhysics, Vt

FULL = [0.3, 0.25, 0.2, 0.01, 0.0, 0.0]
MJCF = f"""<mujoco><worldbody><body name="b"><freejoint/>
<inertial pos="0 0 0" mass="1" fullinertia="{' '.join(map(str, FULL))}"/>
<geom type="sphere" size="0.1"/></body></worldbody></mujoco>"""


def body_frame_tensor(inertia, iquat):
    mat = np.zeros(9)
    mujoco.mju_quat2Mat(mat, np.asarray(iquat, dtype=float))
    rot = mat.reshape(3, 3)
    return rot @ np.diag(inertia) @ rot.T


def make_stage(diag=None, axes=None, newton=None) -> Usd.Stage:
    stage = Usd.Stage.CreateInMemory()
    UsdGeom.SetStageMetersPerUnit(stage, 1.0)
    UsdGeom.SetStageUpAxis(stage, UsdGeom.Tokens.z)
    body = UsdGeom.Xform.Define(stage, "/World/b").GetPrim()
    UsdPhysics.RigidBodyAPI.Apply(body)
    mass_api = UsdPhysics.MassAPI.Apply(body)
    mass_api.CreateMassAttr().Set(1.0)
    mass_api.CreateCenterOfMassAttr().Set(Gf.Vec3f(0, 0, 0))
    if diag is not None:
        mass_api.CreateDiagonalInertiaAttr().Set(Gf.Vec3f(*diag))
    if axes is not None:
        mass_api.CreatePrincipalAxesAttr().Set(Gf.Quatf(*axes))
    if newton is not None:
        body.ApplyAPI("NewtonMassAPI")
        body.GetAttribute("newton:inertia").Set(Vt.DoubleArray(newton))
    sphere = UsdGeom.Sphere.Define(stage, "/World/b/geom")
    sphere.CreateRadiusAttr().Set(0.1)
    UsdPhysics.CollisionAPI.Apply(sphere.GetPrim())
    return stage


ref = mujoco.MjModel.from_xml_string(MJCF)
ref_tensor = body_frame_tensor(ref.body_inertia[1], ref.body_iquat[1])

# Principal-axis decomposition of FULL, authored as the PhysicsMassAPI fallback for newton:inertia.
eigval, eigvec, quat = np.zeros(3), np.zeros(9), np.zeros(4)
tensor = np.array([[FULL[0], FULL[3], FULL[4]], [FULL[3], FULL[1], FULL[5]], [FULL[4], FULL[5], FULL[2]]])
mujoco.mju_eig3(eigval, eigvec, quat, tensor.flatten())

cases = {
    "diagonalInertia + principalAxes + newton:inertia": dict(diag=eigval, axes=quat, newton=FULL),
    "newton:inertia only": dict(newton=FULL),
    "principalAxes + newton:inertia": dict(axes=quat, newton=FULL),
}
for name, kwargs in cases.items():
    stage = make_stage(**kwargs)
    try:
        model = mujoco.MjSpec.from_file(stage.GetRootLayer().identifier).compile()
        error = np.abs(body_frame_tensor(model.body_inertia[1], model.body_iquat[1]) - ref_tensor).max()
        print(f"{name}: compiles, max |inertia - MJCF| = {error:.3g}")
    except ValueError as e:
        print(f"{name}: {str(e).splitlines()[0]}")
Confirmations
主要言語
C++
スター
18.3k
フォーク
2.2k
平均マージ
9日 11時間
マージ済み PR(30日)
5

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

google-deepmind/mujoco のほかの issue

google-deepmind/mujoco の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。