Blender V5.3
node_geometry_attribute_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
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
19#include "BKE_context.hh"
22#include "BKE_node_runtime.hh"
25
26#include "RNA_access.hh"
27#include "RNA_enum_types.hh"
28
29#include "ED_node.hh"
30#include "ED_screen.hh"
31#include "ED_undo.hh"
32
33#include "UI_interface.hh"
35#include "UI_resources.hh"
36
37#include "NOD_eval_log.hh"
38#include "NOD_socket.hh"
39
40#include "node_intern.hh"
41
42namespace blender {
43
45
46namespace ed::space_node {
47
52
53/* This class must not have a destructor, since it is used by buttons and freed with
54 * #MEM_delete_void. */
55BLI_STATIC_ASSERT(std::is_trivially_destructible_v<AttributeSearchData>, "");
56
59{
60 using namespace nodes::eval_log;
61
62 SpaceNode *snode = CTX_wm_space_node(&C);
63 if (!snode) {
65 return {};
66 }
67 bNodeTree *node_tree = snode->edittree;
68 if (node_tree == nullptr) {
70 return {};
71 }
72 const bNode *node = node_tree->node_by_id(data.node_id);
73 if (node == nullptr) {
75 return {};
76 }
77 const bke::bNodeTreeZones *tree_zones = node_tree->zones();
78 if (!tree_zones) {
79 return {};
80 }
81 const ContextualNodeTreeLogs tree_logs = NodesEvalLog::get_contextual_tree_logs(*snode);
82
83 Set<StringRef> names;
84
85 /* For the attribute input node, collect attribute information from all nodes in the group. */
86 if (node->type_legacy == GEO_NODE_INPUT_NAMED_ATTRIBUTE) {
88 tree_logs.foreach_tree_log([&](NodeTreeLog &tree_log) {
89 tree_log.ensure_socket_values();
90 tree_log.ensure_existing_attributes();
91 for (const GeometryAttributeInfo *attribute : tree_log.existing_attributes) {
92 if (!names.add(attribute->name)) {
93 continue;
94 }
96 continue;
97 }
98 attributes.append(attribute);
99 }
100 });
101 return attributes;
102 }
103 NodeTreeLog *tree_log = tree_logs.get_main_tree_log(*node);
104 if (!tree_log) {
105 return {};
106 }
107 tree_log->ensure_socket_values();
108 NodeLog *node_log = tree_log->find_node_log(node->identifier);
109 if (node_log == nullptr) {
110 return {};
111 }
112
114 for (const bNodeSocket *input_socket : node->input_sockets()) {
115 if (input_socket->type != SOCK_GEOMETRY) {
116 continue;
117 }
118 const ValueLog *value_log = tree_log->find_socket_value_log(*input_socket);
119 if (value_log == nullptr) {
120 continue;
121 }
122 if (const GeometryInfoLog *geo_log = dynamic_cast<const GeometryInfoLog *>(value_log)) {
123 for (const GeometryAttributeInfo &attribute : geo_log->attributes) {
125 if (names.add(attribute.name)) {
126 attributes.append(&attribute);
127 }
128 }
129 }
130 }
131 }
132 return attributes;
133}
134
136 const bContext *C, void *arg, const char *str, ui::SearchItems *items, const bool is_first)
137{
139 return;
140 }
141
142 AttributeSearchData *data = static_cast<AttributeSearchData *>(arg);
143
145
146 ui::attribute_search_add_items(str, true, infos, items, is_first);
147}
148
154{
155 switch (type) {
156 case CD_PROP_FLOAT:
157 case CD_PROP_INT32:
158 case CD_PROP_FLOAT3:
159 case CD_PROP_COLOR:
160 case CD_PROP_BOOL:
162 case CD_PROP_FLOAT4X4:
163 return type;
165 return CD_PROP_COLOR;
166 case CD_PROP_STRING:
167 /* Unsupported currently. */
168 return CD_PROP_FLOAT;
169 case CD_PROP_FLOAT2:
170 case CD_PROP_FLOAT4:
171 case CD_PROP_INT16_2D:
172 case CD_PROP_INT32_2D:
173 /* No 2D vector sockets currently. */
174 return CD_PROP_FLOAT3;
175 case CD_PROP_INT8:
176 return CD_PROP_INT32;
177 default:
178 return CD_PROP_FLOAT;
179 }
180}
181
182static void attribute_search_exec_fn(bContext *C, void *data_v, void *item_v)
183{
185 return;
186 }
187 GeometryAttributeInfo *item = static_cast<GeometryAttributeInfo *>(item_v);
188 if (item == nullptr) {
189 return;
190 }
191 SpaceNode *snode = CTX_wm_space_node(C);
192 if (!snode) {
194 return;
195 }
196 bNodeTree *node_tree = snode->edittree;
197 if (node_tree == nullptr) {
199 return;
200 }
201 AttributeSearchData *data = static_cast<AttributeSearchData *>(data_v);
202 bNode *node = node_tree->node_by_id(data->node_id);
203 if (node == nullptr) {
205 return;
206 }
207
208 /* For the attribute input node, also adjust the type and links connected to the output. */
209 if (node->type_legacy == GEO_NODE_INPUT_NAMED_ATTRIBUTE && item->data_type.has_value()) {
211 node->storage);
214 if (new_type != storage.data_type) {
215 storage.data_type = new_type;
216 /* Make the output socket with the new type on the attribute input node active. */
220 }
221 }
222
223 bNodeSocket *socket = bke::node_find_enabled_input_socket(*node, data->socket_identifier);
224 if (socket == nullptr) {
226 return;
227 }
228 BLI_assert(socket->type == SOCK_STRING);
229
230 bNodeSocketValueString *value = static_cast<bNodeSocketValueString *>(socket->default_value);
231 BLI_strncpy_utf8(value->value, item->name.c_str(), MAX_NAME);
232
233 ED_undo_push(C, "Assign Attribute Name");
234}
235
237 const bNode &node,
238 PointerRNA &socket_ptr,
239 ui::Layout &layout,
240 const StringRef placeholder)
241{
242 ui::Block *block = layout.block();
243 ui::Button *but = uiDefIconTextButR(block,
245 ICON_NONE,
246 "",
247 0,
248 0,
249 10 * UI_UNIT_X, /* Dummy value, replaced by layout system. */
250 UI_UNIT_Y,
251 &socket_ptr,
252 "default_value",
253 0,
254 "");
255 button_placeholder_set(but, placeholder);
256
257 const bNodeSocket &socket = *static_cast<const bNodeSocket *>(socket_ptr.data);
259 data->node_id = node.identifier;
260 STRNCPY_UTF8(data->socket_identifier, socket.identifier);
261
262 button_func_search_set_results_are_suggestions(but, true);
263 button_func_search_set_sep_string(but, UI_MENU_ARROW_SEP);
264 button_func_search_set(but,
265 nullptr,
267 static_cast<void *>(data),
268 true,
269 nullptr,
271 nullptr);
272}
273
274} // namespace ed::space_node
275
276} // namespace blender
#define GEO_NODE_INPUT_NAMED_ATTRIBUTE
#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)
#define storage(slot, qualifiers)
#define attribute(slot)
std::optional< eCustomDataType > attr_type_to_custom_data_type(AttrType attr_type)
bNodeSocket * node_find_enabled_input_socket(bNode &node, StringRef name)
Definition node.cc:3068
bool allow_procedural_attribute_access(StringRef attribute_name)
BLI_STATIC_ASSERT(std::is_trivially_destructible_v< AttributeSearchData >, "")
void node_geometry_add_attribute_search_button(const bContext &, const bNode &node, PointerRNA &socket_ptr, ui::Layout &layout, const StringRef placeholder)
static Vector< const GeometryAttributeInfo * > get_attribute_info_from_context(const bContext &C, AttributeSearchData &data)
static eCustomDataType data_type_in_attribute_input_node(const eCustomDataType type)
static void attribute_search_update_fn(const bContext *C, void *arg, const char *str, ui::SearchItems *items, const bool is_first)
static void attribute_search_exec_fn(bContext *C, void *data_v, void *item_v)
void update_node_declaration_and_sockets(bNodeTree &ntree, bNode &node, Main *bmain)
void attribute_search_add_items(StringRef str, bool can_create_attribute, Span< const nodes::eval_log::GeometryAttributeInfo * > infos, 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
void BKE_main_ensure_invariants(Main &bmain, std::optional< Span< ID * > > modified_ids=std::nullopt)
SpaceNode * CTX_wm_space_node(const bContext *C)
void BKE_ntree_update_tag_node_property(bNodeTree *ntree, bNode *node)
Main * CTX_data_main(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