[Bf-blender-cvs] [769563deeb7] temp-asset-browser-catalogs-ui: Basic support for creating catalogs from the UI

Julian Eisel noreply at git.blender.org
Thu Sep 2 20:05:45 CEST 2021


Commit: 769563deeb70d6ccd2042f0c14d97a5cb048ff97
Author: Julian Eisel
Date:   Thu Sep 2 20:01:22 2021 +0200
Branches: temp-asset-browser-catalogs-ui
https://developer.blender.org/rB769563deeb70d6ccd2042f0c14d97a5cb048ff97

Basic support for creating catalogs from the UI

To the right of each catalog, the UI shows a '+' button now. It creates
a catalog (simply named "Catalog" for now) inside this parent catalog.
This change is written to the asset libraries catalog defintion file
immediately.
Adding multiple catalogs on the same level will cause errors currently,
since then you have multiple catalogs on the same level named "Catalog".
We'll have to ensure uniqueness there.

I think we should immediately enable renaming after creating a new
catalog, like we do it for creating new files in the File Browser.

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

M	source/blender/blenkernel/BKE_asset_catalog.hh
M	source/blender/blenkernel/intern/asset_catalog.cc
M	source/blender/editors/asset/CMakeLists.txt
A	source/blender/editors/asset/ED_asset_catalog.hh
A	source/blender/editors/asset/intern/asset_catalog.cc
M	source/blender/editors/asset/intern/asset_ops.cc
M	source/blender/editors/include/ED_fileselect.h
M	source/blender/editors/space_file/asset_catalog_tree_view.cc
M	source/blender/editors/space_file/filesel.c

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

diff --git a/source/blender/blenkernel/BKE_asset_catalog.hh b/source/blender/blenkernel/BKE_asset_catalog.hh
index 3fc2145996f..cf854b203fd 100644
--- a/source/blender/blenkernel/BKE_asset_catalog.hh
+++ b/source/blender/blenkernel/BKE_asset_catalog.hh
@@ -107,6 +107,7 @@ class AssetCatalogService {
 
 class AssetCatalogTreeItem {
   friend class AssetCatalogService;
+  friend class AssetCatalogTree;
 
  public:
   using ChildSet = std::map<std::string, AssetCatalogTreeItem>;
@@ -143,6 +144,9 @@ class AssetCatalogTree {
   friend class AssetCatalogService;
 
  public:
+  /** Ensure an item representing \a path is in the tree, adding it if necessary. */
+  void insert_item(StringRef catalog_path_str);
+
   void foreach_item(const AssetCatalogTreeItem::ItemIterFn callback) const;
 
  protected:
diff --git a/source/blender/blenkernel/intern/asset_catalog.cc b/source/blender/blenkernel/intern/asset_catalog.cc
index 84481d29a79..425f89bc3ca 100644
--- a/source/blender/blenkernel/intern/asset_catalog.cc
+++ b/source/blender/blenkernel/intern/asset_catalog.cc
@@ -71,6 +71,8 @@ AssetCatalog *AssetCatalogService::create_catalog(const CatalogPath &catalog_pat
   catalog_definition_file_->add_new(catalog_ptr);
   catalog_definition_file_->write_to_disk();
 
+  catalog_tree_->insert_item(catalog_ptr->path);
+
   return catalog_ptr;
 }
 
@@ -238,27 +240,7 @@ std::unique_ptr<AssetCatalogTree> AssetCatalogService::read_into_tree()
 
   /* Go through the catalogs, insert each path component into the tree where needed. */
   for (auto &catalog : catalogs_.values()) {
-    /* #fs::path adds useful behavior to the path. Remember that on Windows it uses "\" as
-     * separator! For catalogs it should always be "/". Use #fs::path::generic_string if needed. */
-    fs::path catalog_path = catalog->path;
-
-    const AssetCatalogTreeItem *parent = nullptr;
-    AssetCatalogTreeItem::ChildSet *insert_to_set = &tree->children_;
-
-    BLI_assert_msg(catalog_path.is_relative() && !catalog_path.has_root_path(),
-                   "Malformed catalog path: Path should be a relative path, with no root-name or "
-                   "root-directory as defined by std::filesystem::path.");
-    for (const fs::path &component : catalog_path) {
-      std::string component_name = component.string();
-
-      /* Insert new tree element - if no matching one is there yet! */
-      auto [item, was_inserted] = insert_to_set->emplace(
-          component_name, AssetCatalogTreeItem(component_name, parent));
-
-      /* Walk further into the path (no matter if a new item was created or not). */
-      parent = &item->second;
-      insert_to_set = &item->second.children_;
-    }
+    tree->insert_item(catalog->path);
   }
 
   return tree;
@@ -297,6 +279,31 @@ bool AssetCatalogTreeItem::has_children() const
   return !children_.empty();
 }
 
+void AssetCatalogTree::insert_item(StringRef catalog_path_str)
+{
+  /* #fs::path adds useful behavior to the path. Remember that on Windows it uses "\" as
+   * separator! For catalogs it should always be "/". Use #fs::path::generic_string if needed. */
+  fs::path catalog_path = std::string_view(catalog_path_str);
+
+  const AssetCatalogTreeItem *parent = nullptr;
+  AssetCatalogTreeItem::ChildSet *insert_to_set = &children_;
+
+  BLI_assert_msg(catalog_path.is_relative() && !catalog_path.has_root_path(),
+                 "Malformed catalog path: Path should be a relative path, with no root-name or "
+                 "root-directory as defined by std::filesystem::path.");
+  for (const fs::path &component : catalog_path) {
+    std::string component_name = component.string();
+
+    /* Insert new tree element - if no matching one is there yet! */
+    auto [item, was_inserted] = insert_to_set->emplace(
+        component_name, AssetCatalogTreeItem(component_name, parent));
+
+    /* Walk further into the path (no matter if a new item was created or not). */
+    parent = &item->second;
+    insert_to_set = &item->second.children_;
+  }
+}
+
 void AssetCatalogTree::foreach_item(const AssetCatalogTreeItem::ItemIterFn callback) const
 {
   AssetCatalogTreeItem::foreach_item_recursive(children_, callback);
diff --git a/source/blender/editors/asset/CMakeLists.txt b/source/blender/editors/asset/CMakeLists.txt
index 31c07580570..821d53a8ff2 100644
--- a/source/blender/editors/asset/CMakeLists.txt
+++ b/source/blender/editors/asset/CMakeLists.txt
@@ -25,12 +25,14 @@ set(INC
   ../../makesrna
   ../../windowmanager
   ../../../../intern/guardedalloc
+  ../../../../extern/ghc_filesystem
 )
 
 set(INC_SYS
 )
 
 set(SRC
+  intern/asset_catalog.cc
   intern/asset_filter.cc
   intern/asset_handle.cc
   intern/asset_library_reference.cc
@@ -40,6 +42,7 @@ set(SRC
   intern/asset_ops.cc
   intern/asset_temp_id_consumer.cc
 
+  ED_asset_catalog.hh
   ED_asset_filter.h
   ED_asset_handle.h
   ED_asset_library.h
diff --git a/source/blender/editors/asset/ED_asset_catalog.hh b/source/blender/editors/asset/ED_asset_catalog.hh
new file mode 100644
index 00000000000..c08fa9ad093
--- /dev/null
+++ b/source/blender/editors/asset/ED_asset_catalog.hh
@@ -0,0 +1,32 @@
+/*
+ * 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.
+ */
+
+/** \file
+ * \ingroup edasset
+ */
+
+#pragma once
+
+#include "BLI_string_ref.hh"
+
+namespace blender::bke {
+class AssetCatalog;
+struct AssetLibrary;
+}  // namespace blender::bke
+
+blender::bke::AssetCatalog *ED_asset_catalog_add(blender::bke::AssetLibrary *library,
+                                                 blender::StringRef name,
+                                                 blender::StringRef parent_path = nullptr);
diff --git a/source/blender/editors/asset/intern/asset_catalog.cc b/source/blender/editors/asset/intern/asset_catalog.cc
new file mode 100644
index 00000000000..bc03d3c5263
--- /dev/null
+++ b/source/blender/editors/asset/intern/asset_catalog.cc
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+
+/** \file
+ * \ingroup edasset
+ */
+
+#include "BKE_asset_catalog.hh"
+#include "BKE_asset_library.hh"
+
+#include "ED_asset_catalog.hh"
+
+using namespace blender;
+using namespace blender::bke;
+
+AssetCatalog *ED_asset_catalog_add(blender::bke::AssetLibrary *library,
+                                   StringRef name,
+                                   StringRef parent_path)
+{
+  std::string fullpath = parent_path.is_empty() ?
+                             std::string(name) :
+                             std::string(parent_path) + AssetCatalogService::PATH_SEPARATOR + name;
+  return library->catalog_service->create_catalog(fullpath);
+}
diff --git a/source/blender/editors/asset/intern/asset_ops.cc b/source/blender/editors/asset/intern/asset_ops.cc
index d69a2cae94d..9e6845797bb 100644
--- a/source/blender/editors/asset/intern/asset_ops.cc
+++ b/source/blender/editors/asset/intern/asset_ops.cc
@@ -18,18 +18,26 @@
  * \ingroup edasset
  */
 
+#include "BKE_asset_catalog.hh"
 #include "BKE_context.h"
 #include "BKE_report.h"
 
+#include "BLI_string_ref.hh"
 #include "BLI_vector.hh"
 
 #include "ED_asset.h"
+#include "ED_asset_catalog.hh"
+/* XXX needs access to the file list, should all be done via the asset system in future. */
+#include "ED_fileselect.h"
 
 #include "RNA_access.h"
+#include "RNA_define.h"
 
 #include "WM_api.h"
 #include "WM_types.h"
 
+using namespace blender;
+
 /* -------------------------------------------------------------------- */
 
 using PointerRNAVec = blender::Vector<PointerRNA>;
@@ -295,10 +303,53 @@ static void ASSET_OT_list_refresh(struct wmOperatorType *ot)
 
 /* -------------------------------------------------------------------- */
 
+static bool asset_catalog_new_poll(bContext *C)
+{
+  const SpaceFile *sfile = CTX_wm_space_file(C);
+  return asset_operation_poll(C) && sfile && ED_fileselect_active_asset_library_get(sfile);
+}
+
+static int asset_catalog_new_exec(bContext *C, wmOperator *op)
+{
+  SpaceFile *sfile = CTX_wm_space_file(C);
+  struct AssetLibrary *asset_library = ED_fileselect_active_asset_library_get(sfile);
+  char *parent_path = RNA_string_get_alloc(op->ptr, "parent_path", nullptr, 0, nullptr);
+
+  ED_asset_catalog_add(
+      reinterpret_cast<blender::bke::AssetLibrary *>(asset_library), "Catalog", parent_path);
+
+  MEM_freeN(parent_path);
+
+  return OPERATOR_FINISHED;
+}
+
+static void ASSET_OT_catalog_new(struct wmOperatorType *ot)
+{
+  /* identifiers */
+  ot->name = "New Asset Catalog";
+  ot->description = "Create a new catalog to put assets in";
+  ot->idname = "ASSET_OT_catalog_new";
+
+  /* api callbacks */
+  ot->exec = asset_catalog_new_exec;
+  ot->poll = asset_catalog_new_poll;
+
+  RNA_def_string(ot->srna,
+                 "parent_path",
+                 nullptr,
+                 0,
+                 "Parent Path",
+                 "Optional path defining the location to put the 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list