[Bf-blender-cvs] [17a3ce7a1e7] temp-T80320-usd-hack-removal: USD: move library initialisation from `main()` to USD module

Sybren A. Stüvel noreply at git.blender.org
Tue Sep 1 16:09:33 CEST 2020


Commit: 17a3ce7a1e7d21c5a583c3a34fe8b7321b1f927e
Author: Sybren A. Stüvel
Date:   Mon Aug 31 18:13:49 2020 +0200
Branches: temp-T80320-usd-hack-removal
https://developer.blender.org/rB17a3ce7a1e7d21c5a583c3a34fe8b7321b1f927e

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

===================================================================

M	source/blender/io/usd/intern/usd_capi.cc
M	source/creator/creator.c

===================================================================

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"));
 
diff --git a/source/creator/creator.c b/source/creator/creator.c
index 3d76d832d9f..37fbf0cf76a 100644
--- a/source/creator/creator.c
+++ b/source/creator/creator.c
@@ -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);



More information about the Bf-blender-cvs mailing list