From 335b4d7c8cfe978efba2d2d0fc3be20a2c69d93b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= <sybren@stuvel.eu>
Date: Wed, 11 Aug 2021 11:22:43 +0200
Subject: [PATCH] Pose Library: change how cleanup is done after Copy As Asset

Copy As Asset creates an asset datablock, saves it to disk, and then
removes it again. This removal has a check to ensure the temp datablock
isn't accidentally still in use by something. When this check fails, it now
still forces the cleanup. The message is now there just to ask people to
file a bug report, instead of blocking their workflow altogether.
---
 pose_library/operators.py | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/pose_library/operators.py b/pose_library/operators.py
index 008aceda4..c0c8b3324 100644
--- a/pose_library/operators.py
+++ b/pose_library/operators.py
@@ -216,22 +216,25 @@ class POSELIB_OT_copy_as_asset(PoseAssetCreator, Operator):
 
         filepath = self.save_datablock(asset)
 
-        # The asset has been saved to disk, so to clean up it has to loose its asset & fake user status.
-        asset.asset_clear()
-        asset.use_fake_user = False
-        if asset.users > 0:
-            self.report({"ERROR"}, "Unexpected non-null user count for the asset")
-            return {"FINISHED"}
-
-        bpy.data.actions.remove(asset)
-
         context.window_manager.clipboard = "%s%s" % (
             self.CLIPBOARD_ASSET_MARKER,
             filepath,
         )
-
         asset_browser.tag_redraw(context.screen)
         self.report({"INFO"}, "Pose Asset copied, use Paste As New Asset in any Asset Browser to paste")
+
+        # The asset has been saved to disk, so to clean up it has to loose its asset & fake user status.
+        asset.asset_clear()
+        asset.use_fake_user = False
+
+        # The asset can be removed from the main DB, as it was purely created to
+        # be stored to disk, and not to be used in this file.
+        if asset.users > 0:
+            # This should never happen, and indicates a bug in the code. Having a warning about it is nice,
+            # but it shouldn't stand in the way of actually cleaning up the meant-to-be-temporary datablock.
+            self.report({"WARNING"}, "Unexpected non-zero user count for the asset, please report this as a bug")
+
+        bpy.data.actions.remove(asset)
         return {"FINISHED"}
 
     def save_datablock(self, action: Action) -> Path:
-- 
GitLab