[Bf-blender-cvs] [afd8e4bce79] blender-v2.93-release: Fix BLO_library_temp_load_id loading temporary ID's into G.main

Campbell Barton noreply at git.blender.org
Fri Apr 16 14:13:28 CEST 2021


Commit: afd8e4bce7937baf016b973c091da3d954ffaf76
Author: Campbell Barton
Date:   Fri Apr 16 22:09:52 2021 +1000
Branches: blender-v2.93-release
https://developer.blender.org/rBafd8e4bce7937baf016b973c091da3d954ffaf76

Fix BLO_library_temp_load_id loading temporary ID's into G.main

The intention with this API function was to temporarily load
ID's tagged LIB_TAG_TEMP_MAIN,
however the way the `real_main` was used,
these ID's were loaded into the G.main.

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

M	source/blender/blenloader/BLO_readfile.h
M	source/blender/blenloader/intern/readfile_tempload.c

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

diff --git a/source/blender/blenloader/BLO_readfile.h b/source/blender/blenloader/BLO_readfile.h
index d8c613ba900..89db216aada 100644
--- a/source/blender/blenloader/BLO_readfile.h
+++ b/source/blender/blenloader/BLO_readfile.h
@@ -226,7 +226,10 @@ int BLO_library_link_copypaste(struct Main *mainl, BlendHandle *bh, const uint64
  * Struct for temporarily loading datablocks from a blend file.
  */
 typedef struct TempLibraryContext {
-  struct Main *temp_main;
+  /** Temporary main used for library data. */
+  struct Main *bmain_lib;
+  /** Temporary main used to load data into (currently initialized from `real_main`). */
+  struct Main *bmain_base;
   struct BlendHandle *blendhandle;
   struct LibraryLink_Params liblink_params;
   struct Library *lib;
diff --git a/source/blender/blenloader/intern/readfile_tempload.c b/source/blender/blenloader/intern/readfile_tempload.c
index 691d8f542ab..4566e1e9b4d 100644
--- a/source/blender/blenloader/intern/readfile_tempload.c
+++ b/source/blender/blenloader/intern/readfile_tempload.c
@@ -21,6 +21,9 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "BLI_string.h"
+
+#include "BKE_main.h"
 #include "BKE_report.h"
 
 #include "DNA_ID.h"
@@ -32,15 +35,20 @@ TempLibraryContext *BLO_library_temp_load_id(struct Main *real_main,
                                              struct ReportList *reports)
 {
   TempLibraryContext *temp_lib_ctx = MEM_callocN(sizeof(*temp_lib_ctx), __func__);
+  temp_lib_ctx->bmain_base = BKE_main_new();
+
+  /* Copy the file path so any path remapping is performed properly. */
+  STRNCPY(temp_lib_ctx->bmain_base->name, real_main->name);
 
   temp_lib_ctx->blendhandle = BLO_blendhandle_from_file(blend_file_path, reports);
 
-  BLO_library_link_params_init(&temp_lib_ctx->liblink_params, real_main, 0, LIB_TAG_TEMP_MAIN);
+  BLO_library_link_params_init(
+      &temp_lib_ctx->liblink_params, temp_lib_ctx->bmain_base, 0, LIB_TAG_TEMP_MAIN);
 
-  temp_lib_ctx->temp_main = BLO_library_link_begin(
+  temp_lib_ctx->bmain_lib = BLO_library_link_begin(
       &temp_lib_ctx->blendhandle, blend_file_path, &temp_lib_ctx->liblink_params);
 
-  temp_lib_ctx->temp_id = BLO_library_link_named_part(temp_lib_ctx->temp_main,
+  temp_lib_ctx->temp_id = BLO_library_link_named_part(temp_lib_ctx->bmain_lib,
                                                       &temp_lib_ctx->blendhandle,
                                                       idcode,
                                                       idname,
@@ -51,8 +59,13 @@ TempLibraryContext *BLO_library_temp_load_id(struct Main *real_main,
 
 void BLO_library_temp_free(TempLibraryContext *temp_lib_ctx)
 {
+  /* This moves the temporary ID and any indirectly loaded data into `bmain_base`
+   * only to free `bmain_base`, while redundant this is the typical code-path for library linking,
+   * it's more convenient to follow this convention rather than create a new code-path for this
+   * one-off use case. */
   BLO_library_link_end(
-      temp_lib_ctx->temp_main, &temp_lib_ctx->blendhandle, &temp_lib_ctx->liblink_params);
+      temp_lib_ctx->bmain_lib, &temp_lib_ctx->blendhandle, &temp_lib_ctx->liblink_params);
   BLO_blendhandle_close(temp_lib_ctx->blendhandle);
+  BKE_main_free(temp_lib_ctx->bmain_base);
   MEM_freeN(temp_lib_ctx);
 }



More information about the Bf-blender-cvs mailing list