From adf990f5e11d5edd3c71c249dac408e5bcbb5b84 Mon Sep 17 00:00:00 2001
From: Bastien Montagne <bastien@blender.org>
Date: Wed, 17 Jul 2024 10:33:12 +0200
Subject: [PATCH] BKE: id_delete: Do not forbid layer collection resync in
 non-Main case.

There is no reason to deal with layer collections resync in non-main
case, deletion code should never trigger it anyway.

This will avoid quite a lot of the non-main-thread calls to
`BKE_layer_collection_resync_forbid` (detected and 'fixed' in previous
commit).
---
 .../blenkernel/intern/lib_id_delete.cc        | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/source/blender/blenkernel/intern/lib_id_delete.cc b/source/blender/blenkernel/intern/lib_id_delete.cc
index 19577d1519b..af524e88847 100644
--- a/source/blender/blenkernel/intern/lib_id_delete.cc
+++ b/source/blender/blenkernel/intern/lib_id_delete.cc
@@ -172,18 +172,25 @@ static int id_free(Main *bmain, void *idv, int flag, const bool use_flag_from_id
   return flag;
 }
 
-void BKE_id_free_ex(Main *bmain, void *idv, int flag, const bool use_flag_from_idtag)
+void BKE_id_free_ex(Main *bmain, void *idv, const int flag_orig, const bool use_flag_from_idtag)
 {
   /* ViewLayer resync needs to be delayed during Scene freeing, since internal relationships
    * between the Scene's master collection and its view_layers become invalid
    * (due to remapping). */
-  BKE_layer_collection_resync_forbid();
+  if (bmain && (flag_orig & LIB_ID_FREE_NO_MAIN) == 0) {
+    BKE_layer_collection_resync_forbid();
+  }
 
-  flag = id_free(bmain, idv, flag, use_flag_from_idtag);
+  int flag_final = id_free(bmain, idv, flag_orig, use_flag_from_idtag);
 
-  BKE_layer_collection_resync_allow();
-  if (bmain && (flag & LIB_ID_FREE_NO_MAIN) == 0) {
-    BKE_main_collection_sync_remap(bmain);
+  if (bmain) {
+    if ((flag_orig & LIB_ID_FREE_NO_MAIN) == 0) {
+      BKE_layer_collection_resync_allow();
+    }
+
+    if ((flag_final & LIB_ID_FREE_NO_MAIN) == 0) {
+      BKE_main_collection_sync_remap(bmain);
+    }
   }
 }
 
-- 
GitLab