Allow loading USD files into simulate.
PiperOrigin-RevId: 776584803 Change-Id: I66a5277b3752d69ba5d44ed419204951edb18d41
This commit is contained in:
committed by
Copybara-Service
parent
e97e5d31d0
commit
e80a62c6d5
@@ -174,6 +174,14 @@ if(SIMULATE_BUILD_EXECUTABLE)
|
|||||||
lodepng
|
lodepng
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (MUJOCO_WITH_USD)
|
||||||
|
target_link_libraries(
|
||||||
|
simulate
|
||||||
|
mujoco::usd
|
||||||
|
mujoco::usd::mjcf
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
target_link_options(simulate PRIVATE ${MUJOCO_SIMULATE_LINK_OPTIONS})
|
target_link_options(simulate PRIVATE ${MUJOCO_SIMULATE_LINK_OPTIONS})
|
||||||
|
|
||||||
if(APPLE AND MUJOCO_BUILD_MACOS_FRAMEWORKS)
|
if(APPLE AND MUJOCO_BUILD_MACOS_FRAMEWORKS)
|
||||||
@@ -240,6 +248,22 @@ if(SIMULATE_BUILD_EXECUTABLE)
|
|||||||
MUJOCO_ENABLE_RPATH
|
MUJOCO_ENABLE_RPATH
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (MUJOCO_WITH_USD)
|
||||||
|
target_compile_definitions(simulate PUBLIC SIMULATE_WITH_USD)
|
||||||
|
|
||||||
|
# Add support to RPATH for the samples.
|
||||||
|
target_add_rpath(
|
||||||
|
TARGETS
|
||||||
|
simulate
|
||||||
|
INSTALL_DIRECTORY
|
||||||
|
"${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}"
|
||||||
|
LIB_DIRS
|
||||||
|
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/mujocoUsd ${USD_DIR}/lib
|
||||||
|
DEPENDS
|
||||||
|
MUJOCO_ENABLE_RPATH
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
install(
|
install(
|
||||||
TARGETS simulate
|
TARGETS simulate
|
||||||
EXPORT ${PROJECT_NAME}
|
EXPORT ${PROJECT_NAME}
|
||||||
|
|||||||
+50
-3
@@ -25,6 +25,12 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
#if defined(SIMULATE_WITH_USD)
|
||||||
|
#include <mujoco/experimental/usd/usd.h>
|
||||||
|
#include <pxr/pxr.h>
|
||||||
|
#include <pxr/usd/usd/common.h>
|
||||||
|
#include <pxr/usd/usd/stage.h>
|
||||||
|
#endif
|
||||||
#include <mujoco/mujoco.h>
|
#include <mujoco/mujoco.h>
|
||||||
#include "glfw_adapter.h"
|
#include "glfw_adapter.h"
|
||||||
#include "simulate.h"
|
#include "simulate.h"
|
||||||
@@ -224,13 +230,49 @@ mjModel* LoadModel(const char* file, mj::Simulate& sim) {
|
|||||||
char loadError[kErrorLength] = "";
|
char loadError[kErrorLength] = "";
|
||||||
mjModel* mnew = 0;
|
mjModel* mnew = 0;
|
||||||
auto load_start = mj::Simulate::Clock::now();
|
auto load_start = mj::Simulate::Clock::now();
|
||||||
if (mju::strlen_arr(filename)>4 &&
|
|
||||||
!std::strncmp(filename + mju::strlen_arr(filename) - 4, ".mjb",
|
std::string filename_str(filename);
|
||||||
mju::sizeof_arr(filename) - mju::strlen_arr(filename)+4)) {
|
std::string extension;
|
||||||
|
size_t dot_pos = filename_str.rfind('.');
|
||||||
|
|
||||||
|
if (dot_pos != std::string::npos && dot_pos < filename_str.length() - 1) {
|
||||||
|
extension = filename_str.substr(dot_pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension == ".mjb") {
|
||||||
mnew = mj_loadModel(filename, nullptr);
|
mnew = mj_loadModel(filename, nullptr);
|
||||||
if (!mnew) {
|
if (!mnew) {
|
||||||
mju::strcpy_arr(loadError, "could not load binary model");
|
mju::strcpy_arr(loadError, "could not load binary model");
|
||||||
}
|
}
|
||||||
|
#if defined(SIMULATE_WITH_USD)
|
||||||
|
} else if (extension == ".usda" || extension == ".usd" ||
|
||||||
|
extension == ".usdc" || extension == ".usdz" ) {
|
||||||
|
auto stage = pxr::UsdStage::Open(filename);
|
||||||
|
if (!stage) {
|
||||||
|
mju::strcpy_arr(loadError, "could not open USD stage");
|
||||||
|
} else {
|
||||||
|
mjSpec* spec = mj_parseUSDStage(stage);
|
||||||
|
if (!spec) {
|
||||||
|
mju::strcpy_arr(loadError, "could not parse USD stage to mjSpec");
|
||||||
|
} else {
|
||||||
|
mjModel* model = mj_compile(spec, nullptr);
|
||||||
|
if (!model) {
|
||||||
|
mju::strcpy_arr(loadError,
|
||||||
|
"could not compile USD parsed mjSpec to mjModel:\n");
|
||||||
|
mju::strcat_arr(loadError, mjs_getError(spec));
|
||||||
|
} else {
|
||||||
|
// handle compile warning
|
||||||
|
if (mjs_isWarning(spec)) {
|
||||||
|
mju::strcpy_arr(
|
||||||
|
loadError,
|
||||||
|
"warning while compiling USD parsed mjSpec to mjModel:\n");
|
||||||
|
mju::strcat_arr(loadError, mjs_getError(spec));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mnew = model;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
mnew = mj_loadXML(filename, nullptr, loadError, kErrorLength);
|
mnew = mj_loadXML(filename, nullptr, loadError, kErrorLength);
|
||||||
|
|
||||||
@@ -495,6 +537,11 @@ int main(int argc, char** argv) {
|
|||||||
// scan for libraries in the plugin directory to load additional plugins
|
// scan for libraries in the plugin directory to load additional plugins
|
||||||
scanPluginLibraries();
|
scanPluginLibraries();
|
||||||
|
|
||||||
|
#if defined(SIMULATE_WITH_USD)
|
||||||
|
// If USD is used, print the version.
|
||||||
|
std::printf("OpenUSD version v%d.%02d\n", PXR_MINOR_VERSION, PXR_PATCH_VERSION);
|
||||||
|
#endif
|
||||||
|
|
||||||
mjvCamera cam;
|
mjvCamera cam;
|
||||||
mjv_defaultCamera(&cam);
|
mjv_defaultCamera(&cam);
|
||||||
|
|
||||||
|
|||||||
@@ -152,6 +152,7 @@ endif()
|
|||||||
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
|
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
|
||||||
|
|
||||||
add_library(${MJCF_PLUGIN_TARGET_NAME} SHARED)
|
add_library(${MJCF_PLUGIN_TARGET_NAME} SHARED)
|
||||||
|
add_library(mujoco::usd::mjcf ALIAS ${MJCF_PLUGIN_TARGET_NAME})
|
||||||
target_sources(${MJCF_PLUGIN_TARGET_NAME} PRIVATE
|
target_sources(${MJCF_PLUGIN_TARGET_NAME} PRIVATE
|
||||||
plugins/mjcf/mjcf_file_format.cc
|
plugins/mjcf/mjcf_file_format.cc
|
||||||
plugins/mjcf/mjcf_file_format.h
|
plugins/mjcf/mjcf_file_format.h
|
||||||
@@ -190,6 +191,7 @@ target_include_directories(${MJC_PHYSICS_PLUGIN_TARGET_NAME} PUBLIC
|
|||||||
## mj_usd
|
## mj_usd
|
||||||
|
|
||||||
add_library(${MJ_USD_TARGET_NAME} SHARED)
|
add_library(${MJ_USD_TARGET_NAME} SHARED)
|
||||||
|
add_library(mujoco::usd ALIAS ${MJ_USD_TARGET_NAME})
|
||||||
target_sources(${MJ_USD_TARGET_NAME} PRIVATE
|
target_sources(${MJ_USD_TARGET_NAME} PRIVATE
|
||||||
writer.cc
|
writer.cc
|
||||||
layer_sink.cc
|
layer_sink.cc
|
||||||
|
|||||||
Reference in New Issue
Block a user