Blender V5.3
node_geometry_volume_grid_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_string_ref.hh"
10#include "BLI_string_utf8.hh"
11
12#include "BLT_translation.hh"
13#include "DNA_node_types.h"
14#include "DNA_space_types.h"
15
16#include "BKE_context.hh"
17#include "BKE_node_runtime.hh"
19
20#include "RNA_access.hh"
21#include "RNA_enum_types.hh"
22
23#include "ED_node.hh"
24#include "ED_screen.hh"
25#include "ED_undo.hh"
26
28#include "UI_resources.hh"
29
30#include "NOD_eval_log.hh"
31
32#include "UI_string_search.hh"
33
34#include "node_intern.hh"
35
36#include <fmt/format.h>
37
38namespace blender {
39
42
43namespace ed::space_node {
44
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<GridSearchData>, "");
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 NodeTreeLog *tree_log = tree_logs.get_main_tree_log(*node);
84 if (!tree_log) {
85 return {};
86 }
87 tree_log->ensure_socket_values();
88 NodeLog *node_log = tree_log->find_node_log(node->identifier);
89 if (node_log == nullptr) {
90 return {};
91 }
92
94 for (const bNodeSocket *input_socket : node->input_sockets()) {
95 if (input_socket->type != SOCK_GEOMETRY) {
96 continue;
97 }
98 const ValueLog *value_log = tree_log->find_socket_value_log(*input_socket);
99 if (value_log == nullptr) {
100 continue;
101 }
102 if (const GeometryInfoLog *geo_log = dynamic_cast<const GeometryInfoLog *>(value_log)) {
103 if (const std::optional<GeometryInfoLog::VolumeInfo> &volume_info = geo_log->volume_info) {
104 for (const VolumeGridInfo &info : volume_info->grids) {
105 if (names.add(info.name)) {
106 grids.append(&info);
107 }
108 }
109 }
110 }
111 }
112 return grids;
113}
114
116{
117 const char *name = nullptr;
119 return IFACE_(StringRef(name));
120}
121
123{
124 std::string text = fmt::format(
125 "{}" UI_SEP_CHAR_S "{}", item.name, grid_data_type_string(item.grid_type));
126 return search_item_add(&items, text, (void *)&item, ICON_NONE, ui::BUT_HAS_SEP_CHAR, 0);
127}
128
130 const bool can_create_grid,
132 ui::SearchItems &search_items,
133 const bool is_first)
134{
135 static std::string dummy_str;
136
137 /* Any string may be valid, so add the current search string along with the hints. */
138 if (!str.is_empty()) {
139 bool contained = false;
140 for (const VolumeGridInfo *info : grids) {
141 if (info->name == str) {
142 contained = true;
143 }
144 }
145 if (!contained) {
146 dummy_str = str;
147 search_item_add(
148 &search_items, str, &dummy_str, can_create_grid ? ICON_ADD : ICON_NONE, 0, 0);
149 }
150 }
151
152 if (str.is_empty() && !is_first) {
153 /* Allow clearing the text field when the string is empty, but not on the first pass,
154 * or opening a name field for the first time would show this search item. */
155 dummy_str = str;
156 search_item_add(&search_items, str, &dummy_str, ICON_X, 0, 0);
157 }
158
159 /* Don't filter when the menu is first opened, but still run the search
160 * so the items are in the same order they will appear in while searching. */
161 const StringRef string = is_first ? "" : str;
162
164 for (const VolumeGridInfo *info : grids) {
165 search.add(info->name, info);
166 }
167
168 const Vector<const VolumeGridInfo *> filtered_names = search.query(string);
169 for (const VolumeGridInfo *info : filtered_names) {
170 if (!grid_search_item_add(search_items, *info)) {
171 break;
172 }
173 }
174}
175
177 const bContext *C, void *arg, const char *str, ui::SearchItems *items, const bool is_first)
178{
180 return;
181 }
182
183 GridSearchData *data = static_cast<GridSearchData *>(arg);
184
186
187 BLI_assert(items);
188 volume_grid_search_add_items(str, data->can_create_grid, grids, *items, is_first);
189}
190
191static void grid_search_exec_fn(bContext *C, void *data_v, void *item_v)
192{
194 return;
195 }
196 std::string *item = static_cast<std::string *>(item_v);
197 if (item == nullptr) {
198 return;
199 }
200 SpaceNode *snode = CTX_wm_space_node(C);
201 if (!snode) {
203 return;
204 }
205 bNodeTree *node_tree = snode->edittree;
206 if (node_tree == nullptr) {
208 return;
209 }
210 GridSearchData *data = static_cast<GridSearchData *>(data_v);
211 bNode *node = node_tree->node_by_id(data->node_id);
212 if (node == nullptr) {
214 return;
215 }
216
217 bNodeSocket *socket = bke::node_find_enabled_input_socket(*node, data->socket_identifier);
218 if (socket == nullptr) {
220 return;
221 }
222 BLI_assert(socket->type == SOCK_STRING);
223
224 bNodeSocketValueString *value = static_cast<bNodeSocketValueString *>(socket->default_value);
225 BLI_strncpy_utf8(value->value, item->c_str(), MAX_NAME);
226
227 ED_undo_push(C, "Assign Grid Name");
228}
229
231 const bNode &node,
232 PointerRNA &socket_ptr,
233 ui::Layout &layout,
234 const StringRef placeholder)
235{
236 ui::Block *block = layout.block();
237 ui::Button *but = uiDefIconTextButR(block,
239 ICON_NONE,
240 "",
241 0,
242 0,
243 10 * UI_UNIT_X, /* Dummy value, replaced by layout system. */
244 UI_UNIT_Y,
245 &socket_ptr,
246 "default_value",
247 0,
248 "");
249 button_placeholder_set(but, placeholder);
250
251 const bNodeSocket &socket = *static_cast<const bNodeSocket *>(socket_ptr.data);
253 data->node_id = node.identifier;
254 data->can_create_grid = node.is_type("GeometryNodeStoreNamedGrid"_ustr);
255 STRNCPY_UTF8(data->socket_identifier, socket.identifier);
256
257 button_func_search_set_results_are_suggestions(but, true);
258 button_func_search_set_sep_string(but, UI_MENU_ARROW_SEP);
259 button_func_search_set(but,
260 nullptr,
262 static_cast<void *>(data),
263 true,
264 nullptr,
266 nullptr);
267}
268
269} // namespace ed::space_node
270
271} // namespace blender
#define STRNCPY_UTF8(dst, src)
#define IFACE_(msgid)
#define MAX_NAME
Definition DNA_defs.h:63
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_SEP_CHAR_S
#define UI_UNIT_X
#define BLI_assert_unreachable()
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr StringRef()
void append(const T &value)
void add(const StringRef str, T *user_data, const int weight=0)
Vector< T * > query(const StringRef query) const
#define str(s)
bNodeSocket * node_find_enabled_input_socket(bNode &node, StringRef name)
Definition node.cc:3068
static void grid_search_update_fn(const bContext *C, void *arg, const char *str, ui::SearchItems *items, const bool is_first)
static void grid_search_exec_fn(bContext *C, void *data_v, void *item_v)
BLI_STATIC_ASSERT(std::is_trivially_destructible_v< AttributeSearchData >, "")
static StringRef grid_data_type_string(const VolumeGridType type)
void node_geometry_add_volume_grid_search_button(const bContext &, const bNode &node, PointerRNA &socket_ptr, ui::Layout &layout, const StringRef placeholder)
static bool grid_search_item_add(ui::SearchItems &items, const VolumeGridInfo &item)
static Vector< const VolumeGridInfo * > get_grid_names_from_context(const bContext &C, GridSearchData &data)
static void volume_grid_search_add_items(const StringRef str, const bool can_create_grid, const Span< const VolumeGridInfo * > grids, ui::SearchItems &search_items, const 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
const EnumPropertyItem rna_enum_volume_grid_data_type_items[]
Definition rna_volume.cc:27
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bool RNA_enum_name_from_value(const EnumPropertyItem *item, int value, const char **r_name)
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