diff options
author | Sybren A. Stüvel <sybren@blender.org> | 2020-08-31 19:13:49 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@blender.org> | 2020-09-01 18:28:05 +0300 |
commit | fef1a6c54e74372b863561ad40c1bd5bffef8c38 (patch) | |
tree | 4e5f41667e54aeef16a11809eb5ee3a42219135e /source/blender/io/usd/intern/usd_capi.cc | |
parent | f1b10477c28c91f8b29a0ab4a25a149e7f0147db (diff) |
USD: move library initialisation from `main()` to USD module
Initialize the USD library when used (instead of at startup), so that
this can happen inside the IO/USD module. This makes calls to the USD
library local to Blender's USD code.
Note that failure to find the USD JSON files will now only be reported
when the USD exporter is used, and not on every startup of Blender.
This is the first step in cleaning up the way Blender patches and
initialises the USD library.
Manifest Task: https://developer.blender.org/T80320
Diffstat (limited to 'source/blender/io/usd/intern/usd_capi.cc')
-rw-r--r-- | source/blender/io/usd/intern/usd_capi.cc | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/source/blender/io/usd/intern/usd_capi.cc b/source/blender/io/usd/intern/usd_capi.cc index 83b8c18d436..b5b314136ed 100644 --- a/source/blender/io/usd/intern/usd_capi.cc +++ b/source/blender/io/usd/intern/usd_capi.cc @@ -32,6 +32,7 @@ #include "DNA_scene_types.h" +#include "BKE_appdir.h" #include "BKE_blender_version.h" #include "BKE_context.h" #include "BKE_global.h" @@ -44,6 +45,17 @@ #include "WM_api.h" #include "WM_types.h" +/* Workaround to make it possible to pass a path at runtime to USD. + * + * USD requires some JSON files, and it uses a static constructor to determine the possible + * file-system paths to find those files. This made it impossible for Blender to pass a path to + * the USD library at runtime, as the constructor would run before Blender's main() function. + * We have patched USD (see usd.diff) to avoid that particular static constructor, and have an + * initialization function instead. + * + * This function is implemented in the USD source code, `pxr/base/plug/initConfig.cpp`. */ +extern "C" void usd_initialise_plugin_path(const char *datafiles_usd_path); + namespace blender { namespace io { namespace usd { @@ -59,6 +71,19 @@ struct ExportJobData { bool export_ok; }; +static void ensure_usd_plugin_path_registered(void) +{ + static bool plugin_path_registered = false; + if (plugin_path_registered) { + return; + } + plugin_path_registered = true; + + /* Tell USD which directory to search for its JSON files. If 'datafiles/usd' + * does not exist, the USD library will not be able to read or write any files. */ + usd_initialise_plugin_path(BKE_appdir_folder_id(BLENDER_DATAFILES, "usd")); +} + static void export_startjob(void *customdata, /* Cannot be const, this function implements wm_jobs_start_callback. * NOLINTNEXTLINE: readability-non-const-parameter. */ @@ -180,6 +205,8 @@ 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<blender::io::usd::ExportJobData *>( MEM_mallocN(sizeof(blender::io::usd::ExportJobData), "ExportJobData")); |