[Bf-blender-cvs] [c4aba77211e] asset-browser: Support editing asset tags in the "Current File" repository

Julian Eisel noreply at git.blender.org
Fri Nov 20 13:19:42 CET 2020


Commit: c4aba77211e86d11fd48ea234dc606fb4a95eb2c
Author: Julian Eisel
Date:   Fri Nov 20 13:06:41 2020 +0100
Branches: asset-browser
https://developer.blender.org/rBc4aba77211e86d11fd48ea234dc606fb4a95eb2c

Support editing asset tags in the "Current File" repository

Show buttons for adding and removing asset tags in the Asset Browser sidebar.

Also adds some utility Python files that we will need further I think,
especially once we want to add Python APIs for the Asset Browser for asset
management systems.

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

A	release/scripts/modules/bpy_extras/asset_utils.py
M	release/scripts/startup/bl_operators/__init__.py
A	release/scripts/startup/bl_operators/assets.py
M	release/scripts/startup/bl_ui/space_filebrowser.py
M	source/blender/blenkernel/BKE_asset.h
M	source/blender/blenkernel/intern/asset.c
M	source/blender/makesrna/intern/rna_asset.c

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

diff --git a/release/scripts/modules/bpy_extras/asset_utils.py b/release/scripts/modules/bpy_extras/asset_utils.py
new file mode 100644
index 00000000000..cf269825b59
--- /dev/null
+++ b/release/scripts/modules/bpy_extras/asset_utils.py
@@ -0,0 +1,47 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8 compliant>
+
+"""
+Helpers for asset management tasks.
+"""
+
+import bpy
+from bpy.types import (
+    Context,
+)
+
+__all__ = (
+    "SpaceAssetInfo",
+)
+
+class SpaceAssetInfo:
+    @classmethod
+    def is_asset_browser(cls, space_data: bpy.types.Space):
+        return space_data.type == 'FILE_BROWSER' and space_data.browse_mode == 'ASSETS'
+
+    @classmethod
+    def is_asset_browser_poll(cls, context: Context):
+        return cls.is_asset_browser(context.space_data)
+
+    @classmethod
+    def get_active_asset(cls, context: Context):
+        if hasattr(context, "active_file"):
+            active_file = context.active_file
+            return active_file.asset_data
\ No newline at end of file
diff --git a/release/scripts/startup/bl_operators/__init__.py b/release/scripts/startup/bl_operators/__init__.py
index eff88c835e7..cd930eaa318 100644
--- a/release/scripts/startup/bl_operators/__init__.py
+++ b/release/scripts/startup/bl_operators/__init__.py
@@ -27,6 +27,7 @@ if "bpy" in locals():
 _modules = [
     "add_mesh_torus",
     "anim",
+    "assets",
     "clip",
     "console",
     "constraint",
diff --git a/release/scripts/startup/bl_operators/assets.py b/release/scripts/startup/bl_operators/assets.py
new file mode 100644
index 00000000000..c317df78aa5
--- /dev/null
+++ b/release/scripts/startup/bl_operators/assets.py
@@ -0,0 +1,74 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+import bpy
+
+from bpy_extras.asset_utils import (
+    SpaceAssetInfo,
+)
+
+class ASSET_OT_tag_add(bpy.types.Operator):
+    """Add a new keyword tag to the active asset"""
+
+    bl_idname = "asset.tag_add"
+    bl_label = "Add Asset Tag"
+    bl_options = {'REGISTER', 'UNDO'}
+
+    @classmethod
+    def poll(cls, context):
+        return SpaceAssetInfo.is_asset_browser_poll(context) and SpaceAssetInfo.get_active_asset(context)
+
+    def execute(self, context):
+        active_asset = SpaceAssetInfo.get_active_asset(context)
+        active_asset.tags.new("Unnamed Tag")
+
+        return {'FINISHED'}
+
+
+class ASSET_OT_tag_remove(bpy.types.Operator):
+    """Remove an existing keyword tag from the active asset"""
+
+    bl_idname = "asset.tag_remove"
+    bl_label = "Remove Asset Tag"
+    bl_options = {'REGISTER', 'UNDO'}
+
+    @classmethod
+    def poll(cls, context):
+        if not SpaceAssetInfo.is_asset_browser_poll(context):
+            return False
+
+        active_asset = SpaceAssetInfo.get_active_asset(context)
+        if not active_asset:
+            return False
+
+        return active_asset.active_tag in range(len(active_asset.tags))
+
+    def execute(self, context):
+        active_asset = SpaceAssetInfo.get_active_asset(context)
+        tag = active_asset.tags[active_asset.active_tag]
+
+        active_asset.tags.remove(tag)
+        active_asset.active_tag -= 1
+
+        return {'FINISHED'}
+
+
+classes = (
+    ASSET_OT_tag_add,
+    ASSET_OT_tag_remove,
+)
diff --git a/release/scripts/startup/bl_ui/space_filebrowser.py b/release/scripts/startup/bl_ui/space_filebrowser.py
index f036ee08bfa..a7e2b695533 100644
--- a/release/scripts/startup/bl_ui/space_filebrowser.py
+++ b/release/scripts/startup/bl_ui/space_filebrowser.py
@@ -198,7 +198,8 @@ def panel_poll_is_upper_region(region):
 
 
 def panel_poll_is_asset_browsing(context):
-    return context.space_data.browse_mode == 'ASSETS'
+    from bpy_extras.asset_utils import SpaceAssetInfo
+    return SpaceAssetInfo.is_asset_browser_poll(context)
 
 
 class FILEBROWSER_PT_asset_navigation_bar(Panel):
@@ -588,8 +589,13 @@ class ASSETBROWSER_PT_metadata_tags(Panel):
         active_file = context.active_file
         asset_data = active_file.asset_data
 
-        layout.template_list("ASSETBROWSER_UL_metadata_tags", "asset_tags", asset_data, "tags",
-                             asset_data, "active_tag", rows=4)
+        row = layout.row()
+        row.template_list("ASSETBROWSER_UL_metadata_tags", "asset_tags", asset_data, "tags",
+                          asset_data, "active_tag", rows=4)
+
+        col = row.column(align=True)
+        col.operator("asset.tag_add", icon='ADD', text="")
+        col.operator("asset.tag_remove", icon='REMOVE', text="")
 
 
 class ASSETBROWSER_UL_metadata_tags(UIList):
diff --git a/source/blender/blenkernel/BKE_asset.h b/source/blender/blenkernel/BKE_asset.h
index b969bb42525..b87a23c37f0 100644
--- a/source/blender/blenkernel/BKE_asset.h
+++ b/source/blender/blenkernel/BKE_asset.h
@@ -51,6 +51,7 @@ struct CustomTagEnsureResult {
   bool is_new;
 };
 
+struct CustomTag *BKE_assetdata_tag_add(struct AssetData *asset_data, const char *name);
 struct CustomTagEnsureResult BKE_assetdata_tag_ensure(struct AssetData *asset_data,
                                                       const char *name);
 void BKE_assetdata_tag_remove(struct AssetData *asset_data, struct CustomTag *tag);
diff --git a/source/blender/blenkernel/intern/asset.c b/source/blender/blenkernel/intern/asset.c
index a1f7556c2a2..25dfa599b70 100644
--- a/source/blender/blenkernel/intern/asset.c
+++ b/source/blender/blenkernel/intern/asset.c
@@ -22,6 +22,7 @@
 
 #include "BLI_listbase.h"
 #include "BLI_string.h"
+#include "BLI_string_utils.h"
 #include "BLI_utildefines.h"
 
 #include "BKE_asset.h"
@@ -101,6 +102,26 @@ void BKE_asset_data_free(AssetData *asset_data)
   MEM_SAFE_FREE(asset_data);
 }
 
+static CustomTag *assetdata_tag_create(const char *const name)
+{
+  CustomTag *tag = MEM_callocN(sizeof(*tag), __func__);
+  BLI_strncpy(tag->name, name, sizeof(tag->name));
+  return tag;
+}
+
+CustomTag *BKE_assetdata_tag_add(AssetData *asset_data, const char *name)
+{
+  CustomTag *tag = assetdata_tag_create(name);
+
+  BLI_addtail(&asset_data->tags, tag);
+  BLI_uniquename(&asset_data->tags, tag, name, '.', offsetof(CustomTag, name), sizeof(tag->name));
+
+  return tag;
+}
+
+/**
+ * Make sure there is a tag with name \a name, create one if needed.
+ */
 struct CustomTagEnsureResult BKE_assetdata_tag_ensure(AssetData *asset_data, const char *name)
 {
   struct CustomTagEnsureResult result = {.tag = NULL};
@@ -116,9 +137,7 @@ struct CustomTagEnsureResult BKE_assetdata_tag_ensure(AssetData *asset_data, con
     return result;
   }
 
-  tag = MEM_mallocN(sizeof(*tag), __func__);
-  BLI_strncpy(tag->name, name, sizeof(tag->name));
-
+  tag = assetdata_tag_create(name);
   BLI_addtail(&asset_data->tags, tag);
 
   result.tag = tag;
diff --git a/source/blender/makesrna/intern/rna_asset.c b/source/blender/makesrna/intern/rna_asset.c
index decb0b23613..d0beb524773 100644
--- a/source/blender/makesrna/intern/rna_asset.c
+++ b/source/blender/makesrna/intern/rna_asset.c
@@ -38,17 +38,26 @@
 
 static CustomTag *rna_AssetData_tag_new(AssetData *asset_data,
                                         ReportList *reports,
-                                        const char *name)
+                                        const char *name,
+                                        bool skip_if_exists)
 {
-  struct CustomTagEnsureResult result = BKE_assetdata_tag_ensure(asset_data, name);
+  CustomTag *tag = NULL;
 
-  if (!result.is_new) {
-    BKE_reportf(
-        reports, RPT_WARNING, "Tag '%s' already present for given asset", result.tag->name);
-    /* Report, but still return valid item. */
+  if (skip_if_exists) {
+    struct CustomTagEnsureResult result = BKE_assetdata_tag_ensure(asset_data, name);
+
+    if (!result.is_new) {
+      BKE_reportf(
+          reports, RPT_WARNING, "Tag '%s' already present for given asset", result.tag->name);
+      /* Report, but still return valid item. */
+    }
+    tag = result.tag;
+  }
+  else {
+    tag = BKE_assetdata_tag_add(asset_data, name);
   }
 
-  return result.tag;
+  return tag;
 }
 
 static void rna_AssetData_tag_remove(AssetData *asset_data,
@@ -141,6 +150,11 @@ static void rna_def_asset_custom_tags_api(BlenderRNA *brna, PropertyRNA *cprop)
   RNA_def_function_flag(func, FUNC_USE_REPORTS);
   parm = RNA_def_string(func, "name", NULL, MAX_NAME, "Name", "");
   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
+  parm = RNA_def_boolean(func,
+                         "skip_if_exists",
+                         false,
+                         "Skip if Exists",
+                         "Do not add a new tag if one of the same type already exists");
   /* return type */
   parm = RNA_def_pointer(func, "tag", "CustomTag", "", "New tag");


@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list