Blender V4.3
link_drag_search.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
5#include "AS_asset_library.hh"
7
8#include "BLI_listbase.h"
9
10#include "DNA_space_types.h"
11
12#include "BKE_asset.hh"
13#include "BKE_context.hh"
14#include "BKE_idprop.hh"
15#include "BKE_lib_id.hh"
16#include "BKE_node_runtime.hh"
18#include "BKE_screen.hh"
19
20#include "UI_string_search.hh"
21
22#include "NOD_socket.hh"
24
25#include "BLT_translation.hh"
26
27#include "WM_api.hh"
28
30
31#include "ED_asset.hh"
32#include "ED_node.hh"
33
34#include "node_intern.hh"
35
37
39
53
55{
56 LinkDragSearchStorage &storage = *static_cast<LinkDragSearchStorage *>(arg);
57 const wmNotifier *wmn = params->notifier;
58
59 switch (wmn->category) {
60 case NC_ASSET:
61 if (wmn->data == ND_ASSET_LIST_READING) {
62 storage.update_items_tag = true;
63 }
64 break;
65 }
66}
67
69{
70 bNode &reroute = params.add_node("NodeReroute");
71 if (params.socket.in_out == SOCK_IN) {
72 bke::node_add_link(&params.node_tree,
73 &reroute,
74 static_cast<bNodeSocket *>(reroute.outputs.first),
75 &params.node,
76 &params.socket);
77 }
78 else {
79 bke::node_add_link(&params.node_tree,
80 &params.node,
81 &params.socket,
82 &reroute,
83 static_cast<bNodeSocket *>(reroute.inputs.first));
84 }
85}
86
88{
89 /* Add a group input based on the connected socket, and add a new group input node. */
90 bNodeTreeInterfaceSocket *socket_iface = params.node_tree.tree_interface.add_socket(
91 params.socket.name,
92 params.socket.description,
93 params.socket.typeinfo->idname,
95 nullptr);
96 socket_iface->init_from_socket_instance(&params.socket);
97 params.node_tree.tree_interface.active_item_set(&socket_iface->item);
98
99 bNode &group_input = params.add_node("NodeGroupInput");
100
101 /* This is necessary to create the new sockets in the other input nodes. */
103
104 /* Hide the new input in all other group input nodes, to avoid making them taller. */
105 for (bNode *node : params.node_tree.all_nodes()) {
106 if (node->type == NODE_GROUP_INPUT) {
107 bNodeSocket *new_group_input_socket = bke::node_find_socket(
108 node, SOCK_OUT, socket_iface->identifier);
109 if (new_group_input_socket) {
110 new_group_input_socket->flag |= SOCK_HIDDEN;
111 }
112 }
113 }
114
115 /* Hide all existing inputs in the new group input node, to only display the new one. */
116 LISTBASE_FOREACH (bNodeSocket *, socket, &group_input.outputs) {
117 socket->flag |= SOCK_HIDDEN;
118 }
119
120 bNodeSocket *socket = bke::node_find_socket(&group_input, SOCK_OUT, socket_iface->identifier);
121 if (socket) {
122 /* Unhide the socket for the new input in the new node and make a connection to it. */
123 socket->flag &= ~SOCK_HIDDEN;
124 bke::node_add_link(&params.node_tree, &group_input, socket, &params.node, &params.socket);
125
127 *CTX_data_main(&params.C), params.node_tree, params.socket, *socket);
128 }
129}
130
132 const bNodeTreeInterfaceSocket &interface_socket)
133{
134 const eNodeSocketInOut in_out = eNodeSocketInOut(params.socket.in_out);
138
139 bNode &group_input = params.add_node("NodeGroupInput");
140
141 LISTBASE_FOREACH (bNodeSocket *, socket, &group_input.outputs) {
142 socket->flag |= SOCK_HIDDEN;
143 }
144
145 bNodeSocket *socket = bke::node_find_socket(&group_input, SOCK_OUT, interface_socket.identifier);
146 if (socket != nullptr) {
147 socket->flag &= ~SOCK_HIDDEN;
148 bke::node_add_link(&params.node_tree, &group_input, socket, &params.node, &params.socket);
149 }
150}
151
158 const bNodeSocket &socket,
160 Vector<SocketLinkOperation> &search_link_ops)
161{
162 const AssetMetaData &asset_data = asset.get_metadata();
163 const IDProperty *tree_type = BKE_asset_metadata_idprop_find(&asset_data, "type");
164 if (tree_type == nullptr || IDP_Int(tree_type) != node_tree.type) {
165 return;
166 }
167
168 const bke::bNodeTreeType &node_tree_type = *node_tree.typeinfo;
169 const eNodeSocketInOut in_out = socket.in_out == SOCK_OUT ? SOCK_IN : SOCK_OUT;
170
172 &asset_data, in_out == SOCK_IN ? "inputs" : "outputs");
173
174 int weight = -1;
175 Set<StringRef> socket_names;
176 LISTBASE_FOREACH (IDProperty *, socket_property, &sockets->data.group) {
177 if (socket_property->type != IDP_STRING) {
178 continue;
179 }
180 const char *socket_idname = IDP_String(socket_property);
181 const bke::bNodeSocketType *socket_type = bke::node_socket_type_find(socket_idname);
182 if (socket_type == nullptr) {
183 continue;
184 }
187 if (socket.in_out == SOCK_OUT) {
188 std::swap(from, to);
189 }
190 if (node_tree_type.validate_link && !node_tree_type.validate_link(from, to)) {
191 continue;
192 }
193 if (!socket_names.add(socket_property->name)) {
194 /* See comment in #search_link_ops_for_declarations. */
195 continue;
196 }
197
198 const StringRef asset_name = asset.get_name();
199 const StringRef socket_name = socket_property->name;
200
201 search_link_ops.append(
202 {asset_name + " " + UI_MENU_ARROW_SEP + socket_name,
203 [&asset, socket_property, in_out](nodes::LinkSearchOpParams &params) {
204 Main &bmain = *CTX_data_main(&params.C);
205
206 bNode &node = params.add_node(params.node_tree.typeinfo->group_idname);
207
208 bNodeTree *group = reinterpret_cast<bNodeTree *>(
210 node.id = &group->id;
211 id_us_plus(node.id);
214
215 node.flag &= ~NODE_OPTIONS;
216 node.width = group->default_group_node_width;
217
218 /* Create the inputs and outputs on the new node. */
220
222 node, in_out, socket_property->name);
223 if (new_node_socket != nullptr) {
224 /* Rely on the way #node_add_link switches in/out if necessary. */
226 &params.node_tree, &params.node, &params.socket, &node, new_node_socket);
227 }
228 },
229 weight});
230
231 weight--;
232 }
233}
234
236 const bNodeTree &node_tree,
237 const bNodeSocket &socket,
238 Vector<SocketLinkOperation> &search_link_ops)
239{
241 asset::AssetFilterSettings filter_settings{};
242 filter_settings.id_types = FILTER_ID_NT;
243
244 asset::list::storage_fetch(&library_ref, &C);
246 if (!asset::filter_matches_asset(&filter_settings, asset)) {
247 return true;
248 }
249 search_link_ops_for_asset_metadata(node_tree, socket, asset, search_link_ops);
250 return true;
251 });
252}
253
261 const bNodeSocket &socket,
262 Vector<SocketLinkOperation> &search_link_ops)
263{
264 const SpaceNode &snode = *CTX_wm_space_node(&C);
265 NODE_TYPES_BEGIN (node_type) {
266 const char *disabled_hint;
267 if (node_type->poll && !node_type->poll(node_type, &node_tree, &disabled_hint)) {
268 continue;
269 }
270 if (node_type->add_ui_poll && !node_type->add_ui_poll(&C)) {
271 continue;
272 }
273 if (StringRefNull(node_type->ui_name).endswith("(Legacy)")) {
274 continue;
275 }
276 if (node_type->gather_link_search_ops) {
278 *node_type, snode, node_tree, socket, search_link_ops};
279 node_type->gather_link_search_ops(params);
280 }
281 }
283
284 search_link_ops.append({IFACE_("Reroute"), add_reroute_node_fn});
285
286 const bool is_node_group = !(node_tree.id.flag & ID_FLAG_EMBEDDED_DATA);
287
288 if (is_node_group && socket.in_out == SOCK_IN) {
289 search_link_ops.append({IFACE_("Group Input"), add_group_input_node_fn});
290
291 int weight = -1;
292 node_tree.tree_interface.foreach_item([&](const bNodeTreeInterfaceItem &item) {
293 if (item.item_type != NODE_INTERFACE_SOCKET) {
294 return true;
295 }
296 const bNodeTreeInterfaceSocket &interface_socket =
297 reinterpret_cast<const bNodeTreeInterfaceSocket &>(item);
298 {
300 interface_socket.socket_type);
301 const eNodeSocketDatatype from = from_typeinfo ? eNodeSocketDatatype(from_typeinfo->type) :
303 const eNodeSocketDatatype to = eNodeSocketDatatype(socket.typeinfo->type);
304 if (node_tree.typeinfo->validate_link && !node_tree.typeinfo->validate_link(from, to)) {
305 return true;
306 }
307 }
308 search_link_ops.append({std::string(IFACE_("Group Input")) + " " + UI_MENU_ARROW_SEP +
309 (interface_socket.name ? interface_socket.name : ""),
310 [interface_socket](nodes::LinkSearchOpParams &params) {
311 add_existing_group_input_fn(params, interface_socket);
312 },
313 weight});
314 weight--;
315 return true;
316 });
317 }
318
319 gather_search_link_ops_for_all_assets(C, node_tree, socket, search_link_ops);
320}
321
323 const bContext *C, void *arg, const char *str, uiSearchItems *items, const bool is_first)
324{
325 LinkDragSearchStorage &storage = *static_cast<LinkDragSearchStorage *>(arg);
326 if (storage.update_items_tag) {
328 storage.search_link_ops.clear();
330 storage.update_items_tag = false;
331 }
332
335
336 for (SocketLinkOperation &op : storage.search_link_ops) {
337 search.add(op.name, &op, op.weight);
338 }
339
340 /* Don't filter when the menu is first opened, but still run the search
341 * so the items are in the same order they will appear in while searching. */
342 const char *string = is_first ? "" : str;
343 const Vector<SocketLinkOperation *> filtered_items = search.query(string);
344
345 for (SocketLinkOperation *item : filtered_items) {
346 if (!UI_search_item_add(items, item->name.c_str(), item, ICON_NONE, 0, 0)) {
347 break;
348 }
349 }
350}
351
352static void link_drag_search_exec_fn(bContext *C, void *arg1, void *arg2)
353{
354 Main &bmain = *CTX_data_main(C);
355 SpaceNode &snode = *CTX_wm_space_node(C);
356 bNodeTree &node_tree = *snode.edittree;
357 LinkDragSearchStorage &storage = *static_cast<LinkDragSearchStorage *>(arg1);
358 SocketLinkOperation *item = static_cast<SocketLinkOperation *>(arg2);
359 if (item == nullptr) {
360 return;
361 }
362
364
365 Vector<bNode *> new_nodes;
367 *C, node_tree, storage.from_node, storage.from_socket, new_nodes};
368 item->fn(params);
369 if (new_nodes.is_empty()) {
370 return;
371 }
372
373 /* For now, assume that only one node is created by the callback. */
374 BLI_assert(new_nodes.size() == 1);
375 bNode *new_node = new_nodes.first();
376
377 new_node->locx = storage.cursor.x / UI_SCALE_FAC;
378 new_node->locy = storage.cursor.y / UI_SCALE_FAC + 20;
379 if (storage.in_out() == SOCK_IN) {
380 new_node->locx -= new_node->width;
381 }
382
383 bke::node_set_selected(new_node, true);
385
386 /* Ideally it would be possible to tag the node tree in some way so it updates only after the
387 * translate operation is finished, but normally moving nodes around doesn't cause updates. */
389
390 /* Start translation operator with the new node. */
391 wmOperatorType *ot = WM_operatortype_find("NODE_OT_translate_attach_remove_on_cancel", true);
392 BLI_assert(ot);
397}
398
399static void link_drag_search_free_fn(void *arg)
400{
401 LinkDragSearchStorage *storage = static_cast<LinkDragSearchStorage *>(arg);
402 delete storage;
403}
404
405static uiBlock *create_search_popup_block(bContext *C, ARegion *region, void *arg_op)
406{
407 LinkDragSearchStorage &storage = *(LinkDragSearchStorage *)arg_op;
408
409 uiBlock *block = UI_block_begin(C, region, "_popup", UI_EMBOSS);
412
413 uiBut *but = uiDefSearchBut(block,
414 storage.search,
415 0,
416 ICON_VIEWZOOM,
417 sizeof(storage.search),
418 storage.in_out() == SOCK_OUT ? 10 : 10 - UI_searchbox_size_x(),
419 10,
421 UI_UNIT_Y,
422 "");
426 nullptr,
428 &storage,
429 false,
432 nullptr);
434
435 /* Fake button to hold space for the search items. */
436 uiDefBut(block,
438 0,
439 "",
440 storage.in_out() == SOCK_OUT ? 10 : 10 - UI_searchbox_size_x(),
441 10 - UI_searchbox_size_y(),
444 nullptr,
445 0,
446 0,
447 nullptr);
448
449 const int2 offset = {0, -UI_UNIT_Y};
450 UI_block_bounds_set_popup(block, 0.3f * U.widget_unit, offset);
451 return block;
452}
453
455 bNode &node,
456 bNodeSocket &socket,
457 const float2 &cursor)
458{
459 LinkDragSearchStorage *storage = new LinkDragSearchStorage{node, socket, cursor};
460 /* Use the "_ex" variant with `can_refresh` false to avoid a double free when closing Blender. */
461 UI_popup_block_invoke_ex(&C, create_search_popup_block, storage, nullptr, false);
462}
463
464} // namespace blender::ed::space_node
Main runtime representation of an asset.
IDProperty * BKE_asset_metadata_idprop_find(const AssetMetaData *asset_data, const char *name) ATTR_WARN_UNUSED_RESULT
Definition asset.cc:186
SpaceNode * CTX_wm_space_node(const bContext *C)
Main * CTX_data_main(const bContext *C)
#define IDP_Int(prop)
#define IDP_String(prop)
void id_us_plus(ID *id)
Definition lib_id.cc:351
#define NODE_TYPES_BEGIN(ntype)
Definition BKE_node.hh:597
#define NODE_GROUP_INPUT
Definition BKE_node.hh:805
#define NODE_TYPES_END
Definition BKE_node.hh:605
void BKE_ntree_update_tag_node_property(bNodeTree *ntree, bNode *node)
#define BLI_assert(a)
Definition BLI_assert.h:50
#define LISTBASE_FOREACH(type, var, list)
#define SET_FLAG_FROM_TEST(value, test, flag)
#define IFACE_(msgid)
void DEG_relations_tag_update(Main *bmain)
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:725
#define FILTER_ID_NT
Definition DNA_ID.h:1180
@ IDP_STRING
eNodeSocketInOut
@ SOCK_OUT
@ SOCK_IN
@ SOCK_HIDDEN
eNodeSocketDatatype
@ SOCK_CUSTOM
#define UI_SCALE_FAC
void ED_node_tree_propagate_change(const bContext *C, Main *bmain, bNodeTree *ntree)
Definition node_edit.cc:492
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
@ UI_EMBOSS
void UI_block_theme_style_set(uiBlock *block, char theme_style)
@ UI_BLOCK_SEARCH_MENU
@ UI_BLOCK_LOOP
@ UI_BLOCK_MOVEMOUSE_QUIT
void UI_block_bounds_set_popup(uiBlock *block, int addval, const int bounds_offset[2])
Definition interface.cc:590
uiBut * uiDefBut(uiBlock *block, int type, int retval, blender::StringRef str, int x, int y, short width, short height, void *poin, float min, float max, const char *tip)
void UI_but_func_search_set(uiBut *but, uiButSearchCreateFn search_create_fn, uiButSearchUpdateFn search_update_fn, void *arg, bool free_arg, uiFreeArgFunc search_arg_free_fn, uiButHandleFunc search_exec_fn, void *active)
bool UI_search_item_add(uiSearchItems *items, const char *name, void *poin, int iconid, int but_flag, uint8_t name_prefix_offset)
void UI_popup_block_invoke_ex(bContext *C, uiBlockCreateFunc func, void *arg, uiFreeArgFunc arg_free, bool can_refresh)
uiBlock * UI_block_begin(const bContext *C, ARegion *region, std::string name, eUIEmbossType emboss)
int UI_searchbox_size_x()
int UI_searchbox_size_y()
uiBut * uiDefSearchBut(uiBlock *block, void *arg, int retval, int icon, int maxncpy, int x, int y, short width, short height, const char *tip)
@ UI_BLOCK_THEME_STYLE_POPUP
void UI_but_func_search_set_sep_string(uiBut *but, const char *search_sep_string)
void UI_but_func_search_set_listen(uiBut *but, uiButSearchListenFn listen_fn)
void UI_block_flag_enable(uiBlock *block, int flag)
@ UI_BTYPE_LABEL
void UI_but_flag_enable(uiBut *but, int flag)
@ UI_BUT_ACTIVATE_ON_INIT
#define ND_ASSET_LIST_READING
Definition WM_types.hh:516
#define NC_ASSET
Definition WM_types.hh:371
@ WM_OP_INVOKE_DEFAULT
Definition WM_types.hh:218
unsigned int U
Definition btGjkEpa3.h:78
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr bool endswith(StringRef suffix) const
int64_t size() const
void append(const T &value)
bool is_empty() const
const T & first() const
void add(const StringRef str, T *user_data, const int weight=0)
OperationNode * node
#define str(s)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
AssetLibraryReference all_library_reference()
void node_set_active(bNodeTree *ntree, bNode *node)
Definition node.cc:3896
bNodeLink * node_add_link(bNodeTree *ntree, bNode *fromnode, bNodeSocket *fromsock, bNode *tonode, bNodeSocket *tosock)
Definition node.cc:2912
bNodeSocketType * node_socket_type_find(const char *idname)
Definition node.cc:1763
bool node_set_selected(bNode *node, bool select)
Definition node.cc:3863
bNodeSocket * node_find_enabled_socket(bNode &node, eNodeSocketInOut in_out, StringRef name)
Definition node.cc:1850
void node_socket_move_default_value(Main &bmain, bNodeTree &tree, bNodeSocket &src, bNodeSocket &dst)
Definition node.cc:2846
bNodeSocket * node_find_socket(bNode *node, eNodeSocketInOut in_out, StringRef identifier)
Definition node.cc:1829
void storage_fetch(const AssetLibraryReference *library_reference, const bContext *C)
void iterate(const AssetLibraryReference &library_reference, AssetListHandleIterFn fn, FunctionRef< bool(asset_system::AssetRepresentation &)> prefilter_fn=nullptr)
ID * asset_local_id_ensure_imported(Main &bmain, const asset_system::AssetRepresentation &asset)
bool filter_matches_asset(const AssetFilterSettings *filter, const blender::asset_system::AssetRepresentation &asset)
void invoke_node_link_drag_add_menu(bContext &C, bNode &node, bNodeSocket &socket, const float2 &cursor)
bool node_deselect_all(bNodeTree &node_tree)
static void add_reroute_node_fn(nodes::LinkSearchOpParams &params)
static void link_drag_search_update_fn(const bContext *C, void *arg, const char *str, uiSearchItems *items, const bool is_first)
static void link_drag_search_free_fn(void *arg)
static void gather_search_link_ops_for_all_assets(const bContext &C, const bNodeTree &node_tree, const bNodeSocket &socket, Vector< SocketLinkOperation > &search_link_ops)
static void add_existing_group_input_fn(nodes::LinkSearchOpParams &params, const bNodeTreeInterfaceSocket &interface_socket)
static void add_group_input_node_fn(nodes::LinkSearchOpParams &params)
static void link_drag_search_listen_fn(const wmRegionListenerParams *params, void *arg)
static void link_drag_search_exec_fn(bContext *C, void *arg1, void *arg2)
static uiBlock * create_search_popup_block(bContext *C, ARegion *region, void *arg_op)
static void gather_socket_link_operations(const bContext &C, bNodeTree &node_tree, const bNodeSocket &socket, Vector< SocketLinkOperation > &search_link_ops)
static void search_link_ops_for_asset_metadata(const bNodeTree &node_tree, const bNodeSocket &socket, const asset_system::AssetRepresentation &asset, Vector< SocketLinkOperation > &search_link_ops)
void update_node_declaration_and_sockets(bNodeTree &ntree, bNode &node)
#define UI_MENU_ARROW_SEP
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
ListBase group
Definition DNA_ID.h:146
IDPropertyData data
Definition DNA_ID.h:168
char type
Definition DNA_ID.h:154
void * first
struct bNodeTree * edittree
bNodeSocketTypeHandle * typeinfo
float locy
float width
ListBase inputs
float locx
ListBase outputs
Defines a socket type.
Definition BKE_node.hh:151
bool(* validate_link)(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:475
unsigned int data
Definition WM_types.hh:325
unsigned int category
Definition WM_types.hh:325
int WM_operator_name_call_ptr(bContext *C, wmOperatorType *ot, wmOperatorCallContext context, PointerRNA *properties, const wmEvent *event)
PointerRNA * ptr
Definition wm_files.cc:4126
wmOperatorType * ot
Definition wm_files.cc:4125
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
void WM_operator_properties_create_ptr(PointerRNA *ptr, wmOperatorType *ot)
void WM_operator_properties_free(PointerRNA *ptr)
uint8_t flag
Definition wm_window.cc:138