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

Đang mở Phù hợp với người mới
#3,932 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 6 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
cpp, python
Lĩnh vực
backend, robotics

Hướng nghiên cứu

Đọc plugin/usd_decoder/usd_decoder.cc, đặc biệt là ParseUsdPhysicsMassAPIForBody quanh dòng 922–953, và phần xác thực khối lượng trong src/user/user_objects.cc quanh dòng 3373–3398. Tái hiện sự cố bằng các USD stage được cung cấp và so sánh mô men quán tính đã giải mã với giá trị tham chiếu MJCF. Hoàn tất khi newton:inertia được khai báo ghi đè các thuộc tính dự phòng và mô men quán tính đã giải mã khớp với MJCF nguồn.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
Ngôn ngữ chính
C++
Star
18.3k
Fork
2.2k
Merge trung bình
9 ngày 11 giờ
Pull request đã merge (30 ngày)
5

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của google-deepmind/mujoco

Tất cả issue của google-deepmind/mujoco

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.