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
This commit is contained in:
Sybren A. Stüvel 2020-08-31 18:13:49 +02:00
parent f1b10477c2
commit fef1a6c54e
2 changed files with 27 additions and 17 deletions

@ -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"));

@ -442,23 +442,6 @@ int main(int argc,
BKE_materials_init();
#ifdef WITH_USD
/* 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/lib/plug/initConfig.cpp`. */
extern void usd_initialise_plugin_path(const char *datafiles_usd_path);
/* 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"));
#endif /* WITH_USD */
if (G.background == 0) {
#ifndef WITH_PYTHON_MODULE
BLI_argsParse(ba, 2, NULL, NULL);