Blender V5.3
add_modifier_assets.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "AS_asset_catalog.hh"
11#include "AS_asset_library.hh"
13
14#include "BLI_listbase.hh"
16#include "BLI_string_utf8.hh"
17
18#include "DNA_modifier_types.h"
19#include "DNA_screen_types.h"
20#include "DNA_space_types.h"
21
22#include "BKE_asset.hh"
23#include "BKE_context.hh"
24#include "BKE_idprop.hh"
25#include "BKE_lib_id.hh"
26#include "BKE_main.hh"
27#include "BKE_modifier.hh"
28#include "BKE_report.hh"
29#include "BKE_screen.hh"
30
31#include "BLT_translation.hh"
32
33#include "RNA_access.hh"
34
35#include "ED_asset.hh"
37#include "ED_object.hh"
38#include "ED_screen.hh"
39
40#include "MOD_nodes.hh"
41
42#include "NOD_geometry.hh"
43
44#include "UI_interface.hh"
46
47#include "WM_api.hh"
48
49#include "object_intern.hh"
50
51namespace blender::ed::object {
52
54{
56 return asset::list::is_loaded(&all_library_ref);
57}
58
60{
61 asset::AssetFilterSettings type_filter{};
62 type_filter.id_types = FILTER_ID_NT;
63 auto meta_data_filter = [&](const AssetMetaData &meta_data) {
64 const IDProperty *tree_type = BKE_asset_metadata_idprop_find(&meta_data, "type");
65 if (tree_type == nullptr || IDP_int_get(tree_type) != NTREE_GEOMETRY) {
66 return false;
67 }
68 const IDProperty *traits_flag = BKE_asset_metadata_idprop_find(
69 &meta_data, "geometry_node_asset_traits_flag");
70 if (traits_flag == nullptr || !(IDP_int_get(traits_flag) & GEO_NODE_ASSET_MODIFIER)) {
71 return false;
72 }
73 return true;
74 };
78 library, C, type_filter, meta_data_filter, ntreeType_Geometry->asset_catalog_path_prefix);
79}
80
82{
83 static asset::AssetItemTree tree;
84 return &tree;
85}
86
87static void catalog_assets_draw(const bContext *C, Menu *menu)
88{
90
91 const std::optional<StringRefNull> menu_path = CTX_data_string_get(C, "asset_catalog_path");
92 if (!menu_path) {
93 return;
94 }
95 const int skip_essentials = CTX_data_int_get(C, "skip_essentials").value_or(0);
97 menu_path->data());
98 const asset_system::AssetCatalogTreeItem *catalog_item = tree.catalogs.find_item(
99 menu_path->data());
100 BLI_assert(catalog_item != nullptr);
101
102 if (assets.is_empty() && !catalog_item->has_children()) {
103 return;
104 }
105
106 ui::Layout &layout = *menu->layout;
107
108 bool first = true;
109 const auto ensure_separator = [&]() {
110 if (first) {
111 layout.separator();
112 first = false;
113 }
114 };
115
116 for (const asset_system::AssetRepresentation *asset : assets) {
117 if (skip_essentials) {
118 if (ELEM(asset->owner_asset_library().library_reference()->type,
121 {
122 continue;
123 }
124 }
125 ensure_separator();
126
128 asset, "OBJECT_OT_modifier_add_node_group", wm::OpCallContext::InvokeDefault, layout);
129 }
130
131 catalog_item->foreach_child([&](const asset_system::AssetCatalogTreeItem &item) {
132 ensure_separator();
133 asset::draw_menu_for_catalog(item, "OBJECT_MT_add_modifier_catalog_assets", layout);
134 });
135}
136
137static bool unassigned_local_poll(const Main &bmain)
138{
139 for (const bNodeTree &group : bmain.nodetrees) {
140 /* Assets are displayed in other menus, and non-local data-blocks aren't added to this menu. */
141 if (group.id.library_weak_reference || group.id.asset_data) {
142 continue;
143 }
144 if (!group.geometry_node_asset_traits ||
146 {
147 continue;
148 }
149 return true;
150 }
151 return false;
152}
153
154static void unassigned_assets_draw(const bContext *C, Menu *menu)
155{
156 Main &bmain = *CTX_data_main(C);
158 ui::Layout &layout = *menu->layout;
159 wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_modifier_add_node_group", true);
162 }
163
164 bool first = true;
165 bool add_separator = !tree.unassigned_assets.is_empty();
166 for (const bNodeTree &group : bmain.nodetrees) {
167 /* Assets are displayed in other menus, and non-local data-blocks aren't added to this menu. */
168 if (group.id.library_weak_reference || group.id.asset_data) {
169 continue;
170 }
171 if (!group.geometry_node_asset_traits ||
173 {
174 continue;
175 }
176
177 if (add_separator) {
178 layout.separator();
179 add_separator = false;
180 }
181 if (first) {
182 layout.label(IFACE_("Non-Assets"), ICON_NONE);
183 first = false;
184 }
185
186 PointerRNA props_ptr = layout.op(
187 ot, group.id.name + 2, ICON_NONE, wm::OpCallContext::InvokeDefault, UI_ITEM_NONE);
189 }
190}
191
192static void root_catalogs_draw(const bContext *C, Menu *menu)
193{
194 const Object *object = context_active_object(C);
195 if (!object) {
196 return;
197 }
198 ui::Layout &layout = *menu->layout;
199
200 const bool loading_finished = all_loading_finished();
201
203 tree = build_catalog_tree(*C);
204 if (tree.catalogs.is_empty() && loading_finished) {
205 return;
206 }
207
208 layout.separator();
209
210 if (!loading_finished) {
211 layout.label(IFACE_("Loading Asset Libraries"), ICON_STATUS_INFO);
212 }
213
214 Set<std::string> all_builtin_menus = [&]() {
215 Set<std::string> menus;
217 menus.add_new("Edit");
218 }
219 if (ELEM(object->type,
220 OB_MESH,
222 OB_FONT,
223 OB_SURF,
224 OB_VOLUME,
226 {
227 menus.add_new("Generate");
228 }
230 menus.add_new("Deform");
231 }
232 if (ELEM(object->type, OB_MESH)) {
233 menus.add_new("Normals");
234 }
236 menus.add_new("Physics");
237 }
238 return menus;
239 }();
240
242 if (!all_builtin_menus.contains(item.get_name())) {
243 asset::draw_menu_for_catalog(item, "OBJECT_MT_add_modifier_catalog_assets", layout);
244 }
245 });
246
247 if (!tree.unassigned_assets.is_empty() || unassigned_local_poll(*CTX_data_main(C))) {
248 layout.separator();
249 layout.menu(
250 "OBJECT_MT_add_modifier_unassigned_assets", IFACE_("Unassigned"), ICON_FILE_HIDDEN);
251 }
252}
253
255 PointerRNA &ptr,
256 ReportList *reports)
257{
258 Main &bmain = *CTX_data_main(&C);
259 if (bNodeTree *group = reinterpret_cast<bNodeTree *>(
261 {
262 return group;
263 }
264
267 if (!asset) {
268 return nullptr;
269 }
270 return reinterpret_cast<bNodeTree *>(asset::asset_local_id_ensure_imported(bmain, *asset));
271}
272
274{
275 bNodeTree *node_group = get_asset_or_local_node_group(C, ptr, reports);
276 if (!node_group || ID_MISSING(node_group)) {
277 return nullptr;
278 }
279 if (node_group->type != NTREE_GEOMETRY) {
280 if (reports) {
281 BKE_report(reports, RPT_ERROR, "Asset is not a geometry node group");
282 }
283 return nullptr;
284 }
285 return node_group;
286}
287
289{
290 Main *bmain = CTX_data_main(C);
291 Scene *scene = CTX_data_scene(C);
292
294 if (objects.is_empty()) {
295 return OPERATOR_CANCELLED;
296 }
297
298 bNodeTree *node_group = get_node_group(*C, *op->ptr, op->reports);
299 if (!node_group) {
300 return OPERATOR_CANCELLED;
301 }
302
303 bool changed = false;
304 for (const PointerRNA &ptr : objects) {
305 Object *object = static_cast<Object *>(ptr.data);
306 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(
307 modifier_add(op->reports, bmain, scene, object, nullptr, eModifierType_Nodes));
308 if (!nmd) {
309 continue;
310 }
311 changed = true;
312 nmd->node_group = node_group;
313 id_us_plus(&node_group->id);
314 MOD_nodes_update_interface(*bmain, object, nmd);
315
316 /* Don't show the data-block selector since it's not usually necessary for assets. */
319 node_group->geometry_node_asset_traits &&
320 (node_group->geometry_node_asset_traits->flag &
323
324 STRNCPY_UTF8(nmd->modifier.name, DATA_(node_group->id.name + 2));
325 BKE_modifier_unique_name(&object->modifiers, &nmd->modifier);
326
328 }
329
330 if (!changed) {
331 return OPERATOR_CANCELLED;
332 }
333
334 return OPERATOR_FINISHED;
335}
336
338 wmOperator *op,
339 const wmEvent *event)
340{
341 if (event->modifier & KM_ALT || CTX_wm_view3d(C)) {
342 RNA_boolean_set(op->ptr, "use_selected_objects", true);
343 }
344 return modifier_add_asset_exec(C, op);
345}
346
348 wmOperatorType * /*ot*/,
349 PointerRNA *ptr)
350{
353 if (!asset) {
354 return "";
355 }
356 if (!asset->get_metadata().description) {
357 return "";
358 }
359 return TIP_(asset->get_metadata().description);
360}
361
363{
364 ot->name = "Add Modifier";
365 ot->description = "Add a procedural operation/effect to the active object";
366 ot->idname = "OBJECT_OT_modifier_add_node_group";
367
372
374
378}
379
381{
382 MenuType type{};
383 STRNCPY_UTF8(type.idname, "OBJECT_MT_add_modifier_unassigned_assets");
386 type.description = N_(
387 "Modifier node group assets not assigned to a catalog.\n"
388 "Catalogs can be assigned in the Asset Browser");
389 return type;
390}
391
393{
394 MenuType type{};
395 STRNCPY_UTF8(type.idname, "OBJECT_MT_add_modifier_catalog_assets");
399 return type;
400}
401
403{
404 MenuType type{};
405 STRNCPY_UTF8(type.idname, "OBJECT_MT_modifier_add_root_catalogs");
409 return type;
410}
411
419
421 ui::Layout &layout,
422 const StringRef catalog_path,
423 const bool skip_essentials)
424{
426 tree = build_catalog_tree(C);
427 if (tree.catalogs.is_empty() && all_loading_finished()) {
428 return;
429 }
430
431 const asset_system::AssetCatalogTreeItem *item = tree.catalogs.find_root_item(catalog_path);
432 if (!item) {
433 return;
434 }
437 if (!all_library) {
438 return;
439 }
440 ui::Layout &col = layout.column(false);
441 col.context_string_set("asset_catalog_path", item->catalog_path().str());
442 col.context_int_set("skip_essentials", skip_essentials);
443 col.menu_contents("OBJECT_MT_add_modifier_catalog_assets");
444}
445
446} // namespace blender::ed::object
Main runtime representation of an asset.
#define IDP_int_get(prop)
#define STRNCPY_UTF8(dst, src)
#define TIP_(msgid)
#define IFACE_(msgid)
#define DATA_(msgid)
#define ID_MISSING(_id)
Definition DNA_ID.h:695
#define FILTER_ID_NT
Definition DNA_ID.h:1215
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define ND_MODIFIER
Definition WM_types.hh:503
#define NC_OBJECT
Definition WM_types.hh:392
#define SET_FLAG_FROM_TEST(value, test, flag)
#define ELEM(...)
bool contains(const Key &key) const
Definition BLI_set.hh:310
void add_new(const Key &key)
Definition BLI_set.hh:233
constexpr bool is_empty() const
Definition BLI_span.hh:264
bool is_empty() const
void foreach_child(ItemIterFn callback) const
void foreach_root_item(ItemIterFn callback) const
const AssetCatalogTreeItem * find_item(const AssetCatalogPath &path) const
const AssetCatalogTreeItem * find_root_item(const AssetCatalogPath &path) const
AssetLibraryReference all_library_reference()
void all_library_reload_catalogs_if_dirty()
asset_system::AssetLibrary * library_get_once_available(const AssetLibraryReference &library_reference)
bool is_loaded(const AssetLibraryReference *library_reference)
void asset_reading_region_listen_fn(const wmRegionListenerParams *params)
ID * asset_local_id_ensure_imported(Main &bmain, const asset_system::AssetRepresentation &asset, int flags=0, const std::optional< eAssetImportMethod > import_method=std::nullopt, const std::optional< ImportInstantiateContext > instantiate_context=std::nullopt, ReportList *reports=nullptr)
void operator_asset_reference_props_register(StructRNA &srna)
void draw_menu_for_catalog(const asset_system::AssetCatalogTreeItem &item, const StringRefNull menu_name, ui::Layout &layout)
void draw_asset_menu_item(const asset_system::AssetRepresentation *asset, ui::Layout &layout, FunctionRef< void(ui::Layout &, BIFIconID)> draw_item_fn)
AssetItemTree build_filtered_all_catalog_tree(const AssetLibraryReference &library_ref, const bContext &C, const AssetFilterSettings &filter_settings, FunctionRef< bool(const AssetMetaData &)> meta_data_filter={}, const std::optional< StringRef > skip_prefix=std::nullopt)
const asset_system::AssetRepresentation * operator_asset_reference_props_get_asset_from_all_library(const bContext &C, PointerRNA &ptr, ReportList *reports)
static void root_catalogs_draw(const bContext *C, Menu *menu)
void modifier_register_use_selected_objects_prop(wmOperatorType *ot)
ModifierData * modifier_add(ReportList *reports, Main *bmain, Scene *scene, Object *ob, const char *name, int type)
static void catalog_assets_draw(const bContext *C, Menu *menu)
static wmOperatorStatus modifier_add_asset_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static bNodeTree * get_asset_or_local_node_group(const bContext &C, PointerRNA &ptr, ReportList *reports)
static void OBJECT_OT_modifier_add_node_group(wmOperatorType *ot)
static wmOperatorStatus modifier_add_asset_exec(bContext *C, wmOperator *op)
static void unassigned_assets_draw(const bContext *C, Menu *menu)
static asset::AssetItemTree * get_static_item_tree()
static MenuType modifier_add_root_catalogs_menu_type()
static bNodeTree * get_node_group(const bContext &C, PointerRNA &ptr, ReportList *reports)
static bool unassigned_local_poll(const Main &bmain)
static MenuType modifier_add_unassigned_assets_menu_type()
static MenuType modifier_add_catalog_assets_menu_type()
static bool all_loading_finished()
static std::string modifier_add_asset_get_description(bContext *C, wmOperatorType *, PointerRNA *ptr)
void ui_template_modifier_asset_menu_items(const bContext &C, ui::Layout &layout, StringRef catalog_path, bool skip_essentials)
static asset::AssetItemTree build_catalog_tree(const bContext &C)
Object * context_active_object(const bContext *C)
Vector< PointerRNA > modifier_get_edit_objects(const bContext &C, const wmOperator &op)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
void WM_operator_properties_id_lookup(wmOperatorType *ot, const bool add_name_prop)
void id_us_plus(ID *id)
Definition lib_id.cc:433
ID * WM_operator_properties_id_lookup_from_name_or_session_uid(Main *bmain, PointerRNA *ptr, const ID_Type type)
bke::bNodeTreeType * ntreeType_Geometry
void MOD_nodes_update_interface(Main &bmain, Object *object, NodesModifierData *nmd)
Definition MOD_nodes.cc:405
@ GEO_NODE_ASSET_HIDE_MODIFIER_MANAGE_PANEL
@ GEO_NODE_ASSET_MODIFIER
@ ASSET_LIBRARY_ESSENTIALS
@ ASSET_LIBRARY_ONLINE_ESSENTIALS
std::optional< StringRefNull > CTX_data_string_get(const bContext *C, const char *member)
void BKE_modifier_unique_name(ListBaseT< ModifierData > *modifiers, ModifierData *md)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
void WM_operator_properties_id_lookup_set_from_id(PointerRNA *ptr, const ID *id)
IDProperty * BKE_asset_metadata_idprop_find(const AssetMetaData *asset_data, const char *name) ATTR_WARN_UNUSED_RESULT
Definition asset.cc:183
bool ED_operator_object_active_editable(bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
Main * CTX_data_main(const bContext *C)
@ NODES_MODIFIER_HIDE_DATABLOCK_SELECTOR
@ NODES_MODIFIER_HIDE_MANAGE_PANEL
bool WM_menutype_add(MenuType *mt)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
std::optional< int64_t > CTX_data_int_get(const bContext *C, const char *member)
View3D * CTX_wm_view3d(const bContext *C)
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
char name[258]
Definition DNA_ID.h:433
struct LibraryWeakReference * library_weak_reference
Definition DNA_ID.h:529
struct AssetMetaData * asset_data
Definition DNA_ID.h:424
ListBaseT< bNodeTree > nodetrees
Definition BKE_main.hh:409
char idname[BKE_ST_MAXNAME]
MenuTypeFlag flag
void(*) draw(const bContext *C, Menu *menu)
const char * description
void(*) listener(const wmRegionListenerParams *params)
ui::Layout * layout
struct GeometryNodeAssetTraits * geometry_node_asset_traits
eNodeTree_Type type
std::optional< std::string > asset_catalog_path_prefix
Definition BKE_node.hh:546
MultiValueMap< asset_system::AssetCatalogPath, asset_system::AssetRepresentation * > assets_per_path
Vector< asset_system::AssetRepresentation * > unassigned_assets
asset_system::AssetCatalogTree catalogs
void context_int_set(StringRef name, int64_t value)
void label(StringRef name, int icon)
void menu_contents(StringRef menuname)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
void menu(MenuType *mt, std::optional< StringRef > name, int icon)
Layout & column(bool align)
void context_string_set(StringRef name, StringRef value)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
wmEventModifierFlag modifier
Definition WM_types.hh:834
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
std::string(*) get_description(bContext *C, wmOperatorType *ot, PointerRNA *ptr)
Definition WM_types.hh:1197
struct ReportList * reports
#define N_(msgid)