From 70375c96d5006e33673fe8b813d004e42aa50774 Mon Sep 17 00:00:00 2001 From: Ray Molenkamp Date: Mon, 5 Dec 2022 23:10:31 +0100 Subject: [PATCH] USD: prepare for building with Python support and shared libraries Shared libraries and USD plugins will be placed in the same folder, where USD already looks for plugins. This means that specifying the path to the plugins will no longer be needed once the new libraries are available for all platforms. For now the code was refactored to support both cases. Ref T99618 --- .../cmake/Modules/FindPythonLibsUnix.cmake | 6 ++-- build_files/cmake/Modules/FindUSD.cmake | 3 ++ source/blender/io/usd/CMakeLists.txt | 6 ++-- .../blender/io/usd/intern/usd_capi_export.cc | 2 -- .../blender/io/usd/intern/usd_capi_import.cc | 10 +++--- source/blender/io/usd/intern/usd_common.cc | 5 +++ .../blender/io/usd/tests/usd_tests_common.cc | 7 +++-- source/blender/io/usd/usd.h | 2 +- source/creator/CMakeLists.txt | 31 ++++++++++++++++--- source/creator/creator.c | 8 +++++ 10 files changed, 61 insertions(+), 19 deletions(-) diff --git a/build_files/cmake/Modules/FindPythonLibsUnix.cmake b/build_files/cmake/Modules/FindPythonLibsUnix.cmake index 0afe1299330..b222ed85a4f 100644 --- a/build_files/cmake/Modules/FindPythonLibsUnix.cmake +++ b/build_files/cmake/Modules/FindPythonLibsUnix.cmake @@ -13,6 +13,7 @@ # # This module defines # PYTHON_VERSION +# PYTHON_VERSION_NO_DOTS # PYTHON_INCLUDE_DIRS # PYTHON_INCLUDE_CONFIG_DIRS # PYTHON_LIBRARIES @@ -64,11 +65,11 @@ IF(DEFINED PYTHON_LIBPATH) SET(_IS_LIB_PATH_DEF ON) ENDIF() -STRING(REPLACE "." "" _PYTHON_VERSION_NO_DOTS ${PYTHON_VERSION}) +STRING(REPLACE "." "" PYTHON_VERSION_NO_DOTS ${PYTHON_VERSION}) SET(_python_SEARCH_DIRS ${PYTHON_ROOT_DIR} - "$ENV{HOME}/py${_PYTHON_VERSION_NO_DOTS}" + "$ENV{HOME}/py${PYTHON_VERSION_NO_DOTS}" "/opt/lib/python-${PYTHON_VERSION}" ) @@ -211,7 +212,6 @@ IF(PYTHONLIBSUNIX_FOUND) ) ENDIF() -UNSET(_PYTHON_VERSION_NO_DOTS) UNSET(_PYTHON_ABI_FLAGS) UNSET(_python_SEARCH_DIRS) diff --git a/build_files/cmake/Modules/FindUSD.cmake b/build_files/cmake/Modules/FindUSD.cmake index ba5a3d7c843..7b776560866 100644 --- a/build_files/cmake/Modules/FindUSD.cmake +++ b/build_files/cmake/Modules/FindUSD.cmake @@ -59,6 +59,9 @@ ELSE() get_filename_component(USD_LIBRARY_DIR ${USD_LIBRARY} DIRECTORY) SET(USD_INCLUDE_DIRS ${USD_INCLUDE_DIR}) set(USD_LIBRARIES ${USD_LIBRARY}) + IF(EXISTS ${USD_INCLUDE_DIR}/pxr/base/tf/pyModule.h) + SET(USD_PYTHON_SUPPORT ON) + ENDIF() ENDIF() ENDIF() diff --git a/source/blender/io/usd/CMakeLists.txt b/source/blender/io/usd/CMakeLists.txt index 06a0f12c571..745a1c69dce 100644 --- a/source/blender/io/usd/CMakeLists.txt +++ b/source/blender/io/usd/CMakeLists.txt @@ -9,9 +9,9 @@ if(UNIX AND NOT APPLE) add_definitions(-D_GLIBCXX_PERMIT_BACKWARD_HASH) endif() if(WIN32) - add_definitions(-DNOMINMAX -DWIN32_LEAN_AND_MEAN) + add_definitions(-DNOMINMAX -DWIN32_LEAN_AND_MEAN -DBOOST_DEBUG_PYTHON) endif() -add_definitions(-DPXR_STATIC) +add_definitions(-DBOOST_ALL_NO_LIB) # USD headers use deprecated TBB headers, silence warning. add_definitions(-DTBB_SUPPRESS_DEPRECATED_MESSAGES=1) @@ -56,6 +56,7 @@ set(INC_SYS ${USD_INCLUDE_DIRS} ${BOOST_INCLUDE_DIR} ${TBB_INCLUDE_DIR} + ${PYTHON_INCLUDE_DIR} ) set(SRC @@ -122,6 +123,7 @@ set(LIB list(APPEND LIB ${BOOST_LIBRARIES} + ${PYTHON_LIBRARIES} ) if(WITH_OPENVDB) diff --git a/source/blender/io/usd/intern/usd_capi_export.cc b/source/blender/io/usd/intern/usd_capi_export.cc index bf25c03fb7a..28da9e388c5 100644 --- a/source/blender/io/usd/intern/usd_capi_export.cc +++ b/source/blender/io/usd/intern/usd_capi_export.cc @@ -174,8 +174,6 @@ bool USD_export(bContext *C, ViewLayer *view_layer = CTX_data_view_layer(C); Scene *scene = CTX_data_scene(C); - blender::io::usd::ensure_usd_plugin_path_registered(); - blender::io::usd::ExportJobData *job = static_cast( MEM_mallocN(sizeof(blender::io::usd::ExportJobData), "ExportJobData")); diff --git a/source/blender/io/usd/intern/usd_capi_import.cc b/source/blender/io/usd/intern/usd_capi_import.cc index 680e9c758d3..319f15e09af 100644 --- a/source/blender/io/usd/intern/usd_capi_import.cc +++ b/source/blender/io/usd/intern/usd_capi_import.cc @@ -382,13 +382,16 @@ static void import_freejob(void *user_data) using namespace blender::io::usd; +void USD_ensure_plugin_path_registered() +{ + blender::io::usd::ensure_usd_plugin_path_registered(); +} + bool USD_import(struct bContext *C, const char *filepath, const USDImportParams *params, bool as_background_job) { - blender::io::usd::ensure_usd_plugin_path_registered(); - /* Using new here since `MEM_*` functions do not call constructor to properly initialize data. */ ImportJobData *job = new ImportJobData(); job->bmain = CTX_data_main(C); @@ -542,9 +545,6 @@ CacheArchiveHandle *USD_create_handle(struct Main * /*bmain*/, const char *filepath, ListBase *object_paths) { - /* Must call this so that USD file format plugins are loaded. */ - ensure_usd_plugin_path_registered(); - pxr::UsdStageRefPtr stage = pxr::UsdStage::Open(filepath); if (!stage) { diff --git a/source/blender/io/usd/intern/usd_common.cc b/source/blender/io/usd/intern/usd_common.cc index 2b6b6f4ed43..e5a83442140 100644 --- a/source/blender/io/usd/intern/usd_common.cc +++ b/source/blender/io/usd/intern/usd_common.cc @@ -11,6 +11,10 @@ namespace blender::io::usd { void ensure_usd_plugin_path_registered() { + /* if PXR_PYTHON_SUPPORT_ENABLED is defined, we *must* be dynamic and + the plugins are placed relative to the USD shared library hence no + hinting is required. */ +#ifndef PXR_PYTHON_SUPPORT_ENABLED static bool plugin_path_registered = false; if (plugin_path_registered) { return; @@ -22,6 +26,7 @@ void ensure_usd_plugin_path_registered() const std::string blender_usd_datafiles = BKE_appdir_folder_id(BLENDER_DATAFILES, "usd"); /* The trailing slash indicates to the USD library that the path is a directory. */ pxr::PlugRegistry::GetInstance().RegisterPlugins(blender_usd_datafiles + "/"); +#endif } } // namespace blender::io::usd diff --git a/source/blender/io/usd/tests/usd_tests_common.cc b/source/blender/io/usd/tests/usd_tests_common.cc index ea4e704006d..6c73788d866 100644 --- a/source/blender/io/usd/tests/usd_tests_common.cc +++ b/source/blender/io/usd/tests/usd_tests_common.cc @@ -36,9 +36,12 @@ std::string register_usd_plugins_for_tests() BLI_assert(path_len + 1 < FILE_MAX); usd_datafiles_dir[path_len] = '/'; usd_datafiles_dir[path_len + 1] = '\0'; - + /* if PXR_PYTHON_SUPPORT_ENABLED is defined, we *must* be dynamic and + the plugins are placed relative to the USD shared library hence no + hinting is required. */ +#ifndef PXR_PYTHON_SUPPORT_ENABLED pxr::PlugRegistry::GetInstance().RegisterPlugins(usd_datafiles_dir); - +#endif return usd_datafiles_dir; } diff --git a/source/blender/io/usd/usd.h b/source/blender/io/usd/usd.h index 3494d8ffdc3..98d544df251 100644 --- a/source/blender/io/usd/usd.h +++ b/source/blender/io/usd/usd.h @@ -119,7 +119,7 @@ struct CacheReader *CacheReader_open_usd_object(struct CacheArchiveHandle *handl void USD_CacheReader_incref(struct CacheReader *reader); void USD_CacheReader_free(struct CacheReader *reader); - +void USD_ensure_plugin_path_registered(void); #ifdef __cplusplus } #endif diff --git a/source/creator/CMakeLists.txt b/source/creator/CMakeLists.txt index 20d538991df..5c51a934e08 100644 --- a/source/creator/CMakeLists.txt +++ b/source/creator/CMakeLists.txt @@ -11,6 +11,7 @@ set(INC ../blender/editors/include ../blender/gpu ../blender/imbuf + ../blender/io/usd ../blender/makesdna ../blender/makesrna ../blender/render @@ -1312,10 +1313,32 @@ blender_target_include_dirs(blender ${INC}) # These files are required at runtime. if(WITH_USD) add_definitions(-DWITH_USD) - install( - DIRECTORY ${USD_LIBRARY_DIR}/usd - DESTINATION "${TARGETDIR_VER}/datafiles" - ) + absolute_include_dirs(../blender/io/usd) + + # On windows the usd library sits in ./blender.shared copy the files + # relative to the location of the USD dll, if the dll does not exist + # assume we are linking against the static 3.5 lib. + if(WIN32 AND + ( + EXISTS ${LIBDIR}/usd/lib/usd_usd_ms.dll OR # USD 22.03 + EXISTS ${LIBDIR}/usd/lib/usd_ms.dll # USD 22.11 + ) + ) + install(DIRECTORY + ${USD_LIBRARY_DIR}/usd + DESTINATION "./blender.shared" + ) + elseif(USD_PYTHON_SUPPORT) + install(DIRECTORY + ${USD_LIBRARY_DIR}/usd + DESTINATION ${TARGETDIR_LIB} + ) + else() + install(DIRECTORY + ${USD_LIBRARY_DIR}/usd + DESTINATION "${TARGETDIR_VER}/datafiles" + ) + endif() endif() diff --git a/source/creator/creator.c b/source/creator/creator.c index 0a6537865d4..431c0c2e56f 100644 --- a/source/creator/creator.c +++ b/source/creator/creator.c @@ -95,6 +95,10 @@ # include "sdlew.h" #endif +#ifdef WITH_USD +# include "usd.h" +#endif + #include "creator_intern.h" /* Own include. */ /* -------------------------------------------------------------------- */ @@ -471,6 +475,10 @@ int main(int argc, /* Initialize sub-systems that use `BKE_appdir.h`. */ IMB_init(); +#ifdef WITH_USD + USD_ensure_plugin_path_registered(); +#endif + #ifndef WITH_PYTHON_MODULE /* First test for background-mode (#Global.background) */ BLI_args_parse(ba, ARG_PASS_SETTINGS, NULL, NULL);