From a04e0385e45f3ff4c15d4e4c152e2564a8b5e643 Mon Sep 17 00:00:00 2001 From: Sam Haves Date: Mon, 28 Jul 2025 10:52:41 -0700 Subject: [PATCH] Add USD content type support in MJCF. PiperOrigin-RevId: 788075561 Change-Id: If37cc254cab0411454de7663e0b3ff9f2b263281 --- include/mujoco/experimental/usd/usd.h | 14 ++++++++++++-- src/experimental/usd/usd_to_mjspec.cc | 7 +++++++ src/xml/xml_native_reader.cc | 7 +++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/include/mujoco/experimental/usd/usd.h b/include/mujoco/experimental/usd/usd.h index 66023708..cfeac6fb 100644 --- a/include/mujoco/experimental/usd/usd.h +++ b/include/mujoco/experimental/usd/usd.h @@ -18,11 +18,21 @@ #include #include -// Given a USD file parse to a spec, then compile and return the low-level model. +// Given a USD file parse to a spec, then compile and return the low-level +// model. // // Particular care is taken for physics data to be lossless but visual // data such as materials may be lossy. -MJAPI mjModel* mj_loadUSD(const char* filename, const mjVFS* vfs, char* error, int error_sz); +MJAPI mjModel* mj_loadUSD(const char* filename, const mjVFS* vfs, char* error, + int error_sz); + +// Given a USD layer identifier, this function will do a best effort conversion +// from the composed stage to mjSpec. +// +// Particular care is taken for physics data to be lossless but visual +// data such as materials may be lossy. +MJAPI mjSpec* mj_parseUSD(const char* identifier, const mjVFS* vfs, char* error, + int error_sz); // Given a USD stage, this function will do a best effort conversion to // mjSpec. diff --git a/src/experimental/usd/usd_to_mjspec.cc b/src/experimental/usd/usd_to_mjspec.cc index f701f63a..fed6ceda 100644 --- a/src/experimental/usd/usd_to_mjspec.cc +++ b/src/experimental/usd/usd_to_mjspec.cc @@ -1744,3 +1744,10 @@ mjSpec* mj_parseUSDStage(const pxr::UsdStageRefPtr stage) { return spec; } + +MJAPI mjSpec* mj_parseUSD(const char* identifier, const mjVFS* vfs, char* error, + int error_sz) { + auto stage = pxr::UsdStage::Open(identifier); + return mj_parseUSDStage(stage); +} + diff --git a/src/xml/xml_native_reader.cc b/src/xml/xml_native_reader.cc index be465213..e549ae6e 100644 --- a/src/xml/xml_native_reader.cc +++ b/src/xml/xml_native_reader.cc @@ -45,6 +45,9 @@ #include "xml/xml_base.h" #include "xml/xml_util.h" #include "tinyxml2.h" +#ifdef mjUSEUSD +#include +#endif // mjUSEUSD namespace { using std::string; @@ -3332,6 +3335,10 @@ void mjXReader::Asset(XMLElement* section, const mjVFS* vfs) { if (content_type == "text/xml") { child = mj_parseXML(filename.c_str(), vfs, error.data(), error.size()); +#ifdef mjUSEUSD + } else if (content_type == "text/usd") { + child = mj_parseUSD(filename.c_str(), vfs, error.data(), error.size()); +#endif // mjUSEUSD } else { throw mjXError(elem, "unsupported content_type: %s", content_type.c_str()); }