[Bf-blender-cvs] [eaa577bf131] node-add-asset-menu: Initial progress
Hans Goudey
noreply at git.blender.org
Sat Oct 1 00:33:13 CEST 2022
Commit: eaa577bf131c5d7606aefe0e3847a7c0aeeb06d2
Author: Hans Goudey
Date: Wed Sep 28 22:01:33 2022 -0500
Branches: node-add-asset-menu
https://developer.blender.org/rBeaa577bf131c5d7606aefe0e3847a7c0aeeb06d2
Initial progress
===================================================================
M source/blender/blenkernel/BKE_asset_catalog.hh
M source/blender/blenkernel/BKE_asset_catalog_path.hh
M source/blender/blenkernel/BKE_asset_library.hh
M source/blender/blenkernel/intern/asset_catalog.cc
M source/blender/blenkernel/intern/asset_catalog_path.cc
M source/blender/editors/space_node/CMakeLists.txt
A source/blender/editors/space_node/add_menu_assets.cc
M source/blender/editors/space_node/node_intern.hh
M source/blender/editors/space_node/space_node.cc
M source/blender/makesdna/DNA_asset_types.h
===================================================================
diff --git a/source/blender/blenkernel/BKE_asset_catalog.hh b/source/blender/blenkernel/BKE_asset_catalog.hh
index caed12d4fdf..b73180a3653 100644
--- a/source/blender/blenkernel/BKE_asset_catalog.hh
+++ b/source/blender/blenkernel/BKE_asset_catalog.hh
@@ -147,6 +147,7 @@ class AssetCatalogService {
void update_catalog_path(CatalogID catalog_id, const AssetCatalogPath &new_catalog_path);
AssetCatalogTree *get_catalog_tree();
+ const AssetCatalogTree *get_catalog_tree() const;
/** Return true only if there are no catalogs known. */
bool is_empty() const;
diff --git a/source/blender/blenkernel/BKE_asset_catalog_path.hh b/source/blender/blenkernel/BKE_asset_catalog_path.hh
index 135906dd064..93ab0389daf 100644
--- a/source/blender/blenkernel/BKE_asset_catalog_path.hh
+++ b/source/blender/blenkernel/BKE_asset_catalog_path.hh
@@ -49,7 +49,7 @@ class AssetCatalogPath {
AssetCatalogPath() = default;
AssetCatalogPath(StringRef path);
- AssetCatalogPath(const std::string &path);
+ AssetCatalogPath(std::string path);
AssetCatalogPath(const char *path);
AssetCatalogPath(const AssetCatalogPath &other_path) = default;
AssetCatalogPath(AssetCatalogPath &&other_path) noexcept;
diff --git a/source/blender/blenkernel/BKE_asset_library.hh b/source/blender/blenkernel/BKE_asset_library.hh
index e0a39e3aee8..9b6ff242bab 100644
--- a/source/blender/blenkernel/BKE_asset_library.hh
+++ b/source/blender/blenkernel/BKE_asset_library.hh
@@ -44,12 +44,12 @@ struct AssetLibrary {
* No-op if the catalog cannot be found. This could be the kind of "the
* catalog definition file is corrupt/lost" scenario that the simple name is
* meant to help recover from. */
- void refresh_catalog_simplename(struct AssetMetaData *asset_data);
+ void refresh_catalog_simplename(AssetMetaData *asset_data);
void on_blend_save_handler_register();
void on_blend_save_handler_unregister();
- void on_blend_save_post(struct Main *, struct PointerRNA **pointers, int num_pointers);
+ void on_blend_save_post(Main *bmain, PointerRNA **pointers, int num_pointers);
private:
bCallbackFuncStore on_save_callback_store_{};
diff --git a/source/blender/blenkernel/intern/asset_catalog.cc b/source/blender/blenkernel/intern/asset_catalog.cc
index f7b14cc3479..745a0affb96 100644
--- a/source/blender/blenkernel/intern/asset_catalog.cc
+++ b/source/blender/blenkernel/intern/asset_catalog.cc
@@ -546,6 +546,11 @@ AssetCatalogTree *AssetCatalogService::get_catalog_tree()
return catalog_tree_.get();
}
+const AssetCatalogTree *AssetCatalogService::get_catalog_tree() const
+{
+ return catalog_tree_.get();
+}
+
std::unique_ptr<AssetCatalogTree> AssetCatalogService::read_into_tree()
{
auto tree = std::make_unique<AssetCatalogTree>();
diff --git a/source/blender/blenkernel/intern/asset_catalog_path.cc b/source/blender/blenkernel/intern/asset_catalog_path.cc
index 669adb7adf4..9c653c1a137 100644
--- a/source/blender/blenkernel/intern/asset_catalog_path.cc
+++ b/source/blender/blenkernel/intern/asset_catalog_path.cc
@@ -12,7 +12,7 @@ namespace blender::bke {
const char AssetCatalogPath::SEPARATOR = '/';
-AssetCatalogPath::AssetCatalogPath(const std::string &path) : path_(path)
+AssetCatalogPath::AssetCatalogPath(std::string path) : path_(std::move(path))
{
}
diff --git a/source/blender/editors/space_node/CMakeLists.txt b/source/blender/editors/space_node/CMakeLists.txt
index d7eccbf0b68..ff9e5352d0a 100644
--- a/source/blender/editors/space_node/CMakeLists.txt
+++ b/source/blender/editors/space_node/CMakeLists.txt
@@ -29,6 +29,7 @@ set(INC
set(SRC
add_node_search.cc
+ add_menu_assets.cc
drawnode.cc
link_drag_search.cc
node_add.cc
diff --git a/source/blender/editors/space_node/add_menu_assets.cc b/source/blender/editors/space_node/add_menu_assets.cc
new file mode 100644
index 00000000000..45f6421c064
--- /dev/null
+++ b/source/blender/editors/space_node/add_menu_assets.cc
@@ -0,0 +1,116 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "DNA_space_types.h"
+
+#include "BKE_asset.h"
+#include "BKE_asset_catalog.hh"
+#include "BKE_asset_library.hh"
+#include "BKE_idprop.h"
+#include "BKE_screen.h"
+
+#include "ED_asset.h"
+
+#include "node_intern.hh"
+
+namespace blender::ed::space_node {
+
+bool node_add_catalog_assets_poll(const bContext *C, MenuType *mt)
+{
+ return CTX_wm_space_node(C);
+}
+
+static void gather_items_for_asset_library(const bContext &C,
+ const bNodeTree &node_tree,
+ const bke::CatalogID &catalog,
+ const AssetLibraryReference &library_ref,
+ Vector<AssetHandle> &assets,
+ Vector<bke::AssetCatalogPath> &child_catalogs)
+{
+ AssetFilterSettings filter_settings{};
+ filter_settings.id_types = FILTER_ID_NT;
+
+ const bke::AssetLibrary &library = {};
+ const bke::AssetCatalogService *service = library.catalog_service.get();
+ if (!service) {
+ return;
+ }
+
+ const bke::AssetCatalogFilter filter = service->create_catalog_filter(catalog);
+
+ // bke::AssetCatalogTree *catalog_tree = BKE_asset_library_get_catalog_tree(&library);
+
+ /* Iterate over all the assets in the current catalog. */
+ ED_assetlist_storage_fetch(&library_ref, &C);
+ ED_assetlist_ensure_previews_job(&library_ref, &C);
+ ED_assetlist_iterate(library_ref, [&](AssetHandle asset) {
+ if (!ED_asset_filter_matches_asset(&filter_settings, &asset)) {
+ return true;
+ }
+ const AssetMetaData *meta_data = ED_asset_handle_get_metadata(&asset);
+ if (!filter.contains(meta_data->catalog_id)) {
+ return true;
+ }
+ const AssetMetaData &asset_data = *ED_asset_handle_get_metadata(&asset);
+ const IDProperty *tree_type = BKE_asset_metadata_idprop_find(&asset_data, "type");
+ if (tree_type == nullptr || IDP_Int(tree_type) != node_tree.type) {
+ return true;
+ }
+ assets.append(asset);
+ return true;
+ });
+}
+
+static void gather_items_for_all_assets(const bContext &C,
+ const bNodeTree &node_tree,
+ const bke::CatalogID &catalog,
+ Vector<AssetHandle> &assets,
+ Vector<bke::AssetCatalogPath> &child_catalogs)
+{
+ int i;
+ LISTBASE_FOREACH_INDEX (const bUserAssetLibrary *, asset_library, &U.asset_libraries, i) {
+ AssetLibraryReference library_ref{};
+ library_ref.custom_library_index = i;
+ library_ref.type = ASSET_LIBRARY_CUSTOM;
+ /* Skip local assets to avoid duplicates when the asset is part of the local file library. */
+ gather_items_for_asset_library(C, node_tree, catalog, library_ref, assets, child_catalogs);
+ }
+
+ AssetLibraryReference library_ref{};
+ library_ref.custom_library_index = -1;
+ library_ref.type = ASSET_LIBRARY_LOCAL;
+ gather_items_for_asset_library(C, node_tree, catalog, library_ref, assets, child_catalogs);
+}
+
+void node_add_catalog_assets_draw(const bContext *C, Menu *menu)
+{
+ const SpaceNode &snode = *CTX_wm_space_node(C);
+ const bNodeTree *edit_tree = snode.edittree;
+ if (!edit_tree) {
+ return;
+ }
+
+ const bke::AssetCatalogPath menu_catalog_path("Test Catalog");
+
+ Vector<AssetHandle> assets_in_catalog;
+ Vector<bke::AssetCatalogPath> child_catalogs;
+ gather_items_for_all_assets(
+ *C, *edit_tree, menu_catalog_path, assets_in_catalog, child_catalogs);
+ if (assets_in_catalog.is_empty()) {
+ return;
+ }
+ uiLayout *layout = menu->layout;
+ uiItemS(layout);
+ for (AssetHandle asset : assets_in_catalog) {
+ uiItemO(layout, ED_asset_handle_get_name(&asset), ICON_NONE, "NODE_add_node");
+ }
+}
+
+static MenuType NODE_MT_node_add_catalog_assets = []() {
+ MenuType type{};
+ BLI_strncpy(type.idname, "NODE_MT_node_add_catalog_assets", sizeof(type.idname));
+ type.poll = node_add_catalog_assets_poll;
+ type.draw = node_add_catalog_assets_draw;
+ return type;
+}();
+
+} // namespace blender::ed::space_node
\ No newline at end of file
diff --git a/source/blender/editors/space_node/node_intern.hh b/source/blender/editors/space_node/node_intern.hh
index 50c03489027..dbcc7e5cb3e 100644
--- a/source/blender/editors/space_node/node_intern.hh
+++ b/source/blender/editors/space_node/node_intern.hh
@@ -381,4 +381,8 @@ void invoke_node_link_drag_add_menu(bContext &C,
void invoke_add_node_search_menu(bContext &C, const float2 &cursor, bool use_transform);
+/* add_menu_assets.cc */
+
+extern MenuType NODE_MT_node_add_catalog_assets;
+
} // namespace blender::ed::space_node
diff --git a/source/blender/editors/space_node/space_node.cc b/source/blender/editors/space_node/space_node.cc
index 3e5cbf88e15..e920ddbef79 100644
--- a/source/blender/editors/space_node/space_node.cc
+++ b/source/blender/editors/space_node/space_node.cc
@@ -1172,5 +1172,7 @@ void ED_spacetype_node()
art->draw = node_toolbar_region_draw;
BLI_addhead(&st->regiontypes, art);
+ WM_menutype_add(&NODE_MT_node_add_catalog_assets);
+
BKE_spacetype_register(st);
}
diff --git a/source/blender/makesdna/DNA_asset_types.h b/source/blender/makesdna/DNA_asset_types.h
index 29795519719..c63e74dedc0 100644
--- a/source/blender/makesdna/DNA_asset_types.h
+++ b/source/blender/makesdna/DNA_asset_types.h
@@ -53,7 +53,7 @@ typedef struct AssetMetaData {
* Mapped to a path in the asset catalog hierarchy by an #AssetCatalogService.
* Use #BKE_asset_metadata_catalog_id_set() to ensure a valid ID is set.
*/
- struct bUUID catalog_id;
+ bUUID catalog_id;
/**
* Short name of the asset's catalog. This is for debugging purposes only, to allow (partial)
* reconstruction of asset catalogs in the unfortunate case that the mapping from catalog UUID to
More information about the Bf-blender-cvs
mailing list