Skip to content

Commit b919337

Browse files
jeongseok-metameta-codesync[bot]
authored andcommitted
Add io_usd target for OpenUSD I/O support (#818)
Summary: Adds `io_usd` target to CMake builds, enabling OpenUSD character I/O functionality. **Key changes:** - Add `io_usd` library with USD character loading/saving support - Link `io_usd` to `io` target for unified I/O interface - Add `io_usd_test` when testing is enabled - Fix USD target names (use `usd`, `usdGeom`, `usdSkel` without `pxr::` prefix) - Support dual USD initialization for internal/external builds - Add TBB version compatibility (2019, 2020.3, 2021+) - Fix `mt_python_library` to filter out non-file items from install lists ## Checklist: - [x] Adheres to the style guidelines - [x] Codebase formatted by running `pixi run lint` Pull Request resolved: #818 Test Plan: `pixi run test` - All 20/20 C++ tests passed Reviewed By: cstollmeta Differential Revision: D87025592 Pulled By: jeongseok-meta fbshipit-source-id: 1e655b79c916b8842d6830935957b7f57b1f9d56
1 parent 0428652 commit b919337

File tree

5 files changed

+2926
-1162
lines changed

5 files changed

+2926
-1162
lines changed

CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ find_package(fx-gltf CONFIG REQUIRED)
117117
find_package(indicators 2.3 CONFIG REQUIRED)
118118
find_package(nlohmann_json CONFIG REQUIRED)
119119
find_package(openfbx CONFIG REQUIRED)
120+
find_package(pxr CONFIG REQUIRED HINTS ${CMAKE_INSTALL_PREFIX} ${CMAKE_INSTALL_PREFIX}/cmake)
120121
find_package(re2 MODULE REQUIRED)
121122
find_package(spdlog CONFIG REQUIRED)
123+
find_package(TBB CONFIG REQUIRED)
122124
find_package(urdfdom CONFIG REQUIRED)
123125

124126
if(MOMENTUM_BUILD_RENDERER)
@@ -448,6 +450,22 @@ mt_library(
448450
urdfdom::urdf_parser
449451
)
450452

453+
mt_library(
454+
NAME io_usd
455+
HEADERS_VARS io_usd_public_headers
456+
SOURCES_VARS io_usd_sources
457+
PUBLIC_LINK_LIBRARIES
458+
character
459+
io_common
460+
io_skeleton
461+
PRIVATE_LINK_LIBRARIES
462+
usd
463+
usdGeom
464+
usdSkel
465+
TBB::tbb
466+
Python3::Python # Conda USD has Python symbols on both Linux and macOS
467+
)
468+
451469
mt_library(
452470
NAME io_legacy_json
453471
HEADERS_VARS io_legacy_json_public_headers
@@ -493,6 +511,7 @@ mt_library(
493511
io_marker
494512
io_shape
495513
io_skeleton
514+
io_usd
496515
)
497516

498517
mt_library(
@@ -798,6 +817,18 @@ if(MOMENTUM_BUILD_TESTING)
798817
"TEST_RESOURCES_PATH=${CMAKE_SOURCE_DIR}/momentum/test/resources"
799818
)
800819

820+
mt_test(
821+
NAME io_usd_test
822+
SOURCES_VARS io_usd_test_sources
823+
LINK_LIBRARIES
824+
character_test_helpers
825+
io_usd
826+
io_test_helper
827+
Python3::Python
828+
ENV
829+
"TEST_RESOURCES_PATH=${CMAKE_SOURCE_DIR}/momentum/test/resources"
830+
)
831+
801832
mt_test(
802833
NAME rasterizer_test
803834
SOURCES_VARS rasterizer_test_sources

cmake/mt_defs.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,16 @@ function(mt_python_library)
597597
mt_append_pymomentum_filelist("${var}" libs)
598598
endforeach()
599599

600+
# Filter out non-file items (e.g., library names that get incorrectly included)
601+
# Only keep items that are actual files
602+
set(filtered_libs "")
603+
foreach(lib ${libs})
604+
if(EXISTS "${lib}")
605+
list(APPEND filtered_libs "${lib}")
606+
endif()
607+
endforeach()
608+
set(libs "${filtered_libs}")
609+
600610
if(NOT ${_ARG_NO_INSTALL})
601611
if(${_ARG_PRESERVE_DIRECTORY_STRUCTURE})
602612
# Store files with their directory structure preserved

momentum/io/usd/usd_io.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,30 @@
3434
#include <pxr/usd/usdSkel/skinningQuery.h>
3535

3636
#include <tbb/global_control.h>
37-
#include <tbb/task_scheduler_init.h>
3837

38+
// TBB version compatibility:
39+
// - TBB 2019 and older: uses task_scheduler_init.h
40+
// - TBB 2020.3 and newer: uses global_control.h only (task_scheduler_init is deprecated)
41+
// - TBB 2021 and newer: task_scheduler_init.h completely removed
42+
#ifdef TBB_INTERFACE_VERSION
43+
#if TBB_INTERFACE_VERSION < 12000 // TBB 2020.3 has interface version 12000
44+
#include <tbb/task_scheduler_init.h>
45+
#define MOMENTUM_USE_TBB_TASK_SCHEDULER_INIT 1
46+
#endif
47+
#else
48+
// Try to include the header and define if successful
49+
#if __has_include(<tbb/task_scheduler_init.h>)
50+
#include <tbb/task_scheduler_init.h>
51+
#define MOMENTUM_USE_TBB_TASK_SCHEDULER_INIT 1
52+
#endif
53+
#endif
54+
55+
// Conditional include for internal Meta environment vs open source
56+
// In open source builds, this header won't exist and MOMENTUM_WITH_USD_PLUGIN_INIT will be
57+
// undefined
58+
#ifdef MOMENTUM_WITH_USD_PLUGIN_INIT
3959
#include <UsdPluginInit.h>
60+
#endif
4061

4162
#include <cstdio>
4263
#include <cstdlib>
@@ -48,6 +69,9 @@
4869
#include <thread>
4970
#include <vector>
5071

72+
// Import USD namespace
73+
PXR_NAMESPACE_USING_DIRECTIVE
74+
5175
namespace momentum {
5276

5377
namespace {
@@ -83,22 +107,38 @@ std::mutex g_usdInitMutex;
83107
std::mutex g_usdOperationMutex;
84108
bool g_usdInitialized = false;
85109
std::unique_ptr<ResolverWarningsSuppressor> g_suppressor;
110+
#ifdef MOMENTUM_WITH_USD_PLUGIN_INIT
86111
std::unique_ptr<UsdPluginInit> g_usdPluginInit;
112+
#endif
87113
std::unique_ptr<tbb::global_control> g_tbbControl;
88114

115+
// TBB compatibility variables for older versions
116+
#ifdef MOMENTUM_USE_TBB_TASK_SCHEDULER_INIT
117+
std::unique_ptr<tbb::task_scheduler_init> g_tbbTaskScheduler;
118+
#endif
119+
89120
void initializeUsdWithSuppressedWarnings() {
90121
std::lock_guard<std::mutex> lock(g_usdInitMutex);
91122

92123
if (g_usdInitialized) {
93124
return;
94125
}
95126

127+
// Initialize TBB with the appropriate method based on version
128+
#ifdef MOMENTUM_USE_TBB_TASK_SCHEDULER_INIT
129+
// Use legacy task_scheduler_init for older TBB versions
130+
g_tbbTaskScheduler = std::make_unique<tbb::task_scheduler_init>(1);
131+
#else
132+
// Use modern global_control for newer TBB versions
96133
g_tbbControl =
97134
std::make_unique<tbb::global_control>(tbb::global_control::max_allowed_parallelism, 1);
135+
#endif
98136

99137
g_suppressor = std::make_unique<ResolverWarningsSuppressor>();
100138
TfDiagnosticMgr::GetInstance().AddDelegate(g_suppressor.get());
101139

140+
#ifdef MOMENTUM_WITH_USD_PLUGIN_INIT
141+
// Internal Meta environment: Use UsdPluginInit for embedded plugins
102142
auto tempDir = filesystem::temp_directory_path();
103143

104144
// Try a few fixed paths to avoid accumulating many plugin folders
@@ -122,6 +162,11 @@ void initializeUsdWithSuppressedWarnings() {
122162
} else {
123163
g_usdPluginInit = std::make_unique<UsdPluginInit>();
124164
}
165+
#else
166+
// Open source environment: USD is already initialized via standard discovery
167+
// The pxr package config automatically handles plugin paths via PXR_PLUGINPATH_NAME
168+
MT_LOGI("USD I/O initialized with system USD installation");
169+
#endif
125170

126171
g_usdInitialized = true;
127172
}

0 commit comments

Comments
 (0)