Blender V5.3
node_geometry_layer_search.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_map.hh"
10#include "BLI_set.hh"
11#include "BLI_string.hh"
12#include "BLI_string_ref.hh"
13#include "BLI_string_utf8.hh"
14
15#include "DNA_node_types.h"
16#include "DNA_space_types.h"
17
18#include "BKE_context.hh"
21#include "BKE_node_runtime.hh"
24
25#include "RNA_access.hh"
26#include "RNA_enum_types.hh"
27
28#include "ED_node.hh"
29#include "ED_screen.hh"
30#include "ED_undo.hh"
31
33#include "UI_resources.hh"
34
35#include "NOD_eval_log.hh"
36#include "NOD_socket.hh"
37
38#include "node_intern.hh"
39
40namespace blender {
41
43
44namespace ed::space_node {
45
50
51/* This class must not have a destructor, since it is used by buttons and freed with
52 * #MEM_delete_void. */
53BLI_STATIC_ASSERT(std::is_trivially_destructible_v<LayerSearchData>, "");
54
57{
58 using namespace nodes::eval_log;
59
60 SpaceNode *snode = CTX_wm_space_node(&C);
61 if (!snode) {
63 return {};
64 }
65 bNodeTree *node_tree = snode->edittree;
66 if (node_tree == nullptr) {
68 return {};
69 }
70 const bNode *node = node_tree->node_by_id(data.node_id);
71 if (node == nullptr) {
73 return {};
74 }
75 const bke::bNodeTreeZones *tree_zones = node_tree->zones();
76 if (!tree_zones) {
77 return {};
78 }
79 const ContextualNodeTreeLogs tree_logs = NodesEvalLog::get_contextual_tree_logs(*snode);
80
81 Set<StringRef> names;
82
83 /* For the named layer selection input node, collect layer names from all nodes in the group. */
84 if (node->type_legacy == GEO_NODE_INPUT_NAMED_LAYER_SELECTION) {
86 tree_logs.foreach_tree_log([&](NodeTreeLog &tree_log) {
87 tree_log.ensure_socket_values();
88 tree_log.ensure_layer_names();
89 for (const std::string &name : tree_log.all_layer_names) {
90 if (!names.add(name)) {
91 continue;
92 }
93 layer_names.append(&name);
94 }
95 });
96 return layer_names;
97 }
98 NodeTreeLog *tree_log = tree_logs.get_main_tree_log(*node);
99 if (!tree_log) {
100 return {};
101 }
102 tree_log->ensure_socket_values();
103 NodeLog *node_log = tree_log->find_node_log(node->identifier);
104 if (node_log == nullptr) {
105 return {};
106 }
107
108 Vector<const std::string *> layer_names;
109 for (const bNodeSocket *input_socket : node->input_sockets()) {
110 if (input_socket->type != SOCK_GEOMETRY) {
111 continue;
112 }
113 const ValueLog *value_log = tree_log->find_socket_value_log(*input_socket);
114 if (value_log == nullptr) {
115 continue;
116 }
117 if (const GeometryInfoLog *geo_log = dynamic_cast<const GeometryInfoLog *>(value_log)) {
118 if (const std::optional<GeometryInfoLog::GreasePencilInfo> &grease_pencil_info =
119 geo_log->grease_pencil_info)
120 {
121 for (const std::string &name : grease_pencil_info->layer_names) {
122 if (names.add(name)) {
123 layer_names.append(&name);
124 }
125 }
126 }
127 }
128 }
129 return layer_names;
130}
131
133 const bContext *C, void *arg, const char *str, ui::SearchItems *items, const bool is_first)
134{
136 return;
137 }
138
139 LayerSearchData *data = static_cast<LayerSearchData *>(arg);
140
142
143 BLI_assert(items);
144 ui::grease_pencil_layer_search_add_items(str, names, *items, is_first);
145}
146
147static void layer_search_exec_fn(bContext *C, void *data_v, void *item_v)
148{
150 return;
151 }
152 std::string *item = static_cast<std::string *>(item_v);
153 if (item == nullptr) {
154 return;
155 }
156 SpaceNode *snode = CTX_wm_space_node(C);
157 if (!snode) {
159 return;
160 }
161 bNodeTree *node_tree = snode->edittree;
162 if (node_tree == nullptr) {
164 return;
165 }
166 LayerSearchData *data = static_cast<LayerSearchData *>(data_v);
167 bNode *node = node_tree->node_by_id(data->node_id);
168 if (node == nullptr) {
170 return;
171 }
172
173 bNodeSocket *socket = bke::node_find_enabled_input_socket(*node, data->socket_identifier);
174 if (socket == nullptr) {
176 return;
177 }
178 BLI_assert(socket->type == SOCK_STRING);
179
180 bNodeSocketValueString *value = static_cast<bNodeSocketValueString *>(socket->default_value);
181 BLI_strncpy_utf8(value->value, item->c_str(), MAX_NAME);
182
183 ED_undo_push(C, "Assign Layer Name");
184}
185
187 const bNode &node,
188 PointerRNA &socket_ptr,
189 ui::Layout &layout,
190 const StringRef placeholder)
191{
192 ui::Block *block = layout.block();
193 ui::Button *but = uiDefIconTextButR(block,
195 ICON_OUTLINER_DATA_GP_LAYER,
196 "",
197 0,
198 0,
199 10 * UI_UNIT_X, /* Dummy value, replaced by layout system. */
200 UI_UNIT_Y,
201 &socket_ptr,
202 "default_value",
203 0,
204 "");
205 button_placeholder_set(but, placeholder);
206
207 const bNodeSocket &socket = *static_cast<const bNodeSocket *>(socket_ptr.data);
209 data->node_id = node.identifier;
210 STRNCPY_UTF8(data->socket_identifier, socket.identifier);
211
212 button_func_search_set_results_are_suggestions(but, true);
213 button_func_search_set_sep_string(but, UI_MENU_ARROW_SEP);
214 button_func_search_set(but,
215 nullptr,
217 static_cast<void *>(data),
218 true,
219 nullptr,
221 nullptr);
222}
223
224} // namespace ed::space_node
225
226} // namespace blender
#define GEO_NODE_INPUT_NAMED_LAYER_SELECTION
#define STRNCPY_UTF8(dst, src)
#define MAX_NAME
Definition DNA_defs.h:63
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define BLI_assert_unreachable()
bool add(const Key &key)
Definition BLI_set.hh:248
void append(const T &value)
#define str(s)
bNodeSocket * node_find_enabled_input_socket(bNode &node, StringRef name)
Definition node.cc:3068
BLI_STATIC_ASSERT(std::is_trivially_destructible_v< AttributeSearchData >, "")
static void layer_search_update_fn(const bContext *C, void *arg, const char *str, ui::SearchItems *items, const bool is_first)
static Vector< const std::string * > get_layer_names_from_context(const bContext &C, LayerSearchData &data)
void node_geometry_add_layer_search_button(const bContext &, const bNode &node, PointerRNA &socket_ptr, ui::Layout &layout, const StringRef placeholder)
static void layer_search_exec_fn(bContext *C, void *data_v, void *item_v)
void grease_pencil_layer_search_add_items(StringRef str, Span< const std::string * > layer_names, SearchItems &items, bool is_first)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BMesh const char void * data
wmWindowManager * CTX_wm_manager(const bContext *C)
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
SpaceNode * CTX_wm_space_node(const bContext *C)
signed int int32_t
Definition BKE_text.h:78
bScreen * ED_screen_animation_playing(const wmWindowManager *wm)
#define UI_MENU_ARROW_SEP
struct bNodeTree * edittree
eNodeSocketDatatype type