Blender V5.3
geometry_nodes_bundle.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 <fmt/format.h>
10#include <fmt/ranges.h>
11
13
14#include "BKE_node_runtime.hh"
15
18
19namespace blender::nodes {
20
22{
23 return a.items.as_span() == b.items.as_span();
24}
25
27{
28 return !(a == b);
29}
30
32{
33 for (const BundleSignature::Item &item : this->items) {
34 const_cast<BundleSignature::Item &>(item).structure_type =
36 }
37}
38
40{
41 if (key.is_empty()) {
42 return false;
43 }
44 if (key != key.trim()) {
45 /* Keys must not have leading or trailing white-space. This simplifies potentially using these
46 * keys in expressions later on (or even just have a comma separated list of keys). */
47 return false;
48 }
50}
51
53{
54 return split_path(path).has_value();
55}
56
57std::optional<Vector<BundleKey>> Bundle::split_path(const StringRef path)
58{
59 if (path.is_empty()) {
60 return std::nullopt;
61 }
62 Vector<BundleKey> path_elems;
63 StringRef remaining = path;
64 while (!remaining.is_empty()) {
65 const int sep = remaining.find_first_of('/');
66 if (sep == StringRef::not_found) {
67 const StringRef key_str = remaining;
68 if (const std::optional<BundleKey> key = BundleKey::from_str(key_str)) {
69 path_elems.append(*key);
70 }
71 else {
72 return std::nullopt;
73 }
74 break;
75 }
76 const StringRef key_str = remaining.substr(0, sep);
77 if (const std::optional<BundleKey> key = BundleKey::from_str(key_str)) {
78 path_elems.append(*key);
79 remaining = remaining.substr(sep + 1);
80 }
81 else {
82 return std::nullopt;
83 }
84 }
85 return path_elems;
86}
87
89{
90 return BundlePtr(MEM_new<Bundle>(__func__));
91}
92
93void Bundle::add_new(const BundleKey key, const BundleItemValue &value)
94{
95 items_.add_new_as(key, value);
96}
97
99{
100 items_.add_new_as(key, std::move(value));
101}
102
103void Bundle::add_override(const BundleKey key, const BundleItemValue &value)
104{
105 this->remove(key);
106 this->add_new(key, value);
107}
108
109bool Bundle::add(const BundleKey key, const BundleItemValue &value)
110{
111 if (this->contains(key)) {
112 return false;
113 }
114 this->add_new(key, value);
115 return true;
116}
117
118bool Bundle::add(const BundleKey key, BundleItemValue &&value)
119{
120 if (this->contains(key)) {
121 return false;
122 }
123 this->add_new(key, std::move(value));
124 return true;
125}
126
128{
129 static const bke::bNodeSocketType *bundle_socket_type = bke::node_socket_type_find_static(
131 return {
133}
134
136{
137 Bundle *current = this;
138 for (const BundleKey path_elem : path.drop_back(1)) {
139 BundleItemValue &item = current->items_.lookup_or_add_cb_as(
140 path_elem, [&]() { return create_nested_bundle_item(); });
141 BundlePtr *child_bundle_ptr = item.as_pointer<BundlePtr>();
142 if (!child_bundle_ptr || !*child_bundle_ptr) {
143 /* Override the items content with a new bundle. */
145 child_bundle_ptr = item.as_pointer<BundlePtr>();
146 }
147 current = &child_bundle_ptr->ensure_mutable_inplace();
148 }
149 current->items_.add_overwrite_as(path.last(), value);
150}
151
153{
155 const Vector<BundleKey> path_elems = *split_path(path);
156 this->add_path_override(path_elems, value);
157}
158
160{
161 if (this->contains_path(path)) {
162 return false;
163 }
164 this->add_path_new(path, value);
165 return true;
166}
167
169{
170 BLI_assert(!this->contains_path(path));
171 this->add_path_override(path, value);
172}
173
175{
176 BundlePtr *bundle_ptr = this->lookup_path_for_write_ptr<BundlePtr>(path);
177 if (bundle_ptr && *bundle_ptr) {
178 return bundle_ptr->ensure_mutable_inplace();
179 }
180 BundlePtr new_bundle = Bundle::create();
181 Bundle &new_bundle_ref = new_bundle.ensure_mutable_inplace();
182 this->add_path_override(path, std::move(new_bundle));
183 return new_bundle_ref;
184}
185
187{
188 return items_.lookup_ptr_as(key);
189}
190
192{
193 return items_.lookup_ptr_as(key);
194}
195
197{
198 BLI_assert(!path.is_empty());
199 const BundleKey first_elem = path[0];
200 const BundleItemValue *item = this->lookup(first_elem);
201 if (!item) {
202 return nullptr;
203 }
204 if (path.size() == 1) {
205 return item;
206 }
207 const BundlePtr child_bundle = item->as<BundlePtr>().value_or(nullptr);
208 if (!child_bundle) {
209 return nullptr;
210 }
211 return child_bundle->lookup_path(path.drop_front(1));
212}
213
215{
217 const Vector<BundleKey> path_elems = *split_path(path);
218 return this->lookup_path(path_elems);
219}
220
222{
223 BLI_assert(!path.is_empty());
224 const BundleKey first_elem = path[0];
225 BundleItemValue *item = this->lookup(first_elem);
226 if (!item) {
227 return nullptr;
228 }
229 if (path.size() == 1) {
230 return item;
231 }
232 BundlePtr *child_bundle_ptr = item->as_pointer<BundlePtr>();
233 if (!child_bundle_ptr) {
234 return nullptr;
235 }
236 if (!*child_bundle_ptr) {
237 return nullptr;
238 }
239 Bundle &child_bundle = child_bundle_ptr->ensure_mutable_inplace();
240 return child_bundle.lookup_path_for_write(path.drop_front(1));
241}
242
244{
246 const Vector<BundleKey> path_elems = *split_path(path);
247 return this->lookup_path_for_write(path_elems);
248}
249
250void Bundle::merge(const Bundle &other)
251{
252 for (const auto &item : other.items_.items()) {
253 this->add(item.key, item.value);
254 }
255}
256
258{
259 for (const auto &item : other.items_.items()) {
260 this->add_override(item.key, item.value);
261 }
262}
263
265{
266 for (const auto &item : items_.items()) {
267 if (auto *socket_value = std::get_if<BundleItemSocketValue>(&item.value.value)) {
268 socket_value->value.ensure_owns_direct_data();
269 }
270 }
271}
272
274{
275 for (const auto &item : items_.items()) {
276 if (const auto *socket_value = std::get_if<BundleItemSocketValue>(&item.value.value)) {
277 if (!socket_value->value.owns_direct_data()) {
278 return false;
279 }
280 }
281 }
282 return true;
283}
284
286{
287 BundlePtr copy_ptr = Bundle::create();
288 Bundle &copy = const_cast<Bundle &>(*copy_ptr);
289 copy.items_ = items_;
290 return copy_ptr;
291}
292
294{
295 return items_.remove_as(key);
296}
297
299{
301 const Vector<BundleKey> path_elems = *split_path(path);
302 return this->remove_path(path_elems);
303}
304
306{
307 BLI_assert(this->is_mutable());
308 BLI_assert(!path.is_empty());
309 if (!this->contains_path(path)) {
310 return false;
311 }
312 Bundle *current = this;
313 for (const BundleKey path_elem : path.drop_back(1)) {
314 BundleItemValue &item = current->items_.lookup_as(path_elem);
315 BundlePtr *child_bundle_ptr = item.as_pointer<BundlePtr>();
316 current = &child_bundle_ptr->ensure_mutable_inplace();
317 }
318 current->items_.remove_contained_as(path.last());
319 return true;
320}
321
322bool Bundle::contains(const BundleKey key) const
323{
324 return items_.contains_as(key);
325}
326
327bool Bundle::contains_path(const StringRef path) const
328{
329 return this->lookup_path(path) != nullptr;
330}
331
333{
334 return this->lookup_path(path) != nullptr;
335}
336
338{
339 return fmt::format("{}", fmt::join(path, "/"));
340}
341
343{
344 return fmt::format("{}", fmt::join(path, "/"));
345}
346
348{
349 MEM_delete(this);
350}
351
353{
354 items_.clear();
355}
356
357std::optional<StringRef> Bundle::type() const
358{
359 const std::string *type = this->lookup_ptr<std::string>(Bundle::type_item_name);
360 return type ? std::optional<StringRef>(*type) : std::nullopt;
361}
362
364{
365 for (const auto &item : items_.items()) {
366 if (const auto *socket_value = std::get_if<BundleItemSocketValue>(&item.value.value)) {
367 socket_value->value.count_memory(memory);
368 }
369 }
370}
371
373 const bNodeSocket &socket,
374 const NodeSocketInterfaceStructureType stored_structure_type,
375 const bool allow_auto_structure_type)
376{
377 if (stored_structure_type != NodeSocketInterfaceStructureType::Auto) {
378 return stored_structure_type;
379 }
380 if (allow_auto_structure_type) {
382 }
384}
385
386void BundleSignature::add(std::string key, const eNodeSocketDatatype socket_type)
387{
389 BLI_assert(stype);
390 items.add({std::move(key), stype});
391}
392
394 const bool allow_auto_structure_type)
395{
396 BLI_assert(node.is_type("NodeCombineBundle"_ustr));
397 const auto &storage = *static_cast<const NodeCombineBundle *>(node.storage);
398 BundleSignature signature;
399 for (const int i : IndexRange(storage.items_num)) {
400 const NodeCombineBundleItem &item = storage.items[i];
401 const bNodeSocket &socket = node.input_socket(i);
403 const NodeSocketInterfaceStructureType structure_type =
405 socket, item.structure_type, allow_auto_structure_type);
406 signature.items.add({item.name, stype, structure_type});
407 }
408 }
409 return signature;
410}
411
413 const bool allow_auto_structure_type)
414{
415 BLI_assert(node.is_type("NodeSeparateBundle"_ustr));
416 const auto &storage = *static_cast<const NodeSeparateBundle *>(node.storage);
417 BundleSignature signature;
418 for (const int i : IndexRange(storage.items_num)) {
419 const NodeSeparateBundleItem &item = storage.items[i];
420 const bNodeSocket &socket = node.output_socket(i);
422 const NodeSocketInterfaceStructureType structure_type =
424 socket, item.structure_type, allow_auto_structure_type);
425 signature.items.add({item.name, stype, structure_type});
426 }
427 }
428 return signature;
429}
430
432{
433 for (const Item &item : this->items) {
434 if (item.is_signature_definition) {
435 return true;
436 }
437 }
438 return false;
439}
440
441std::optional<BundleSignature> LinkedBundleSignatures::get_merged_signature() const
442{
443 BundleSignature signature;
444 for (const Item &src_signature : this->items) {
445 for (const BundleSignature::Item &item : src_signature.signature.items) {
446 if (!signature.items.add(item)) {
447 const BundleSignature::Item &existing_item = *signature.items.lookup_key_ptr_as(item.key);
448 if (item.type->type != existing_item.type->type) {
449 return std::nullopt;
450 }
451 if (existing_item.structure_type != item.structure_type) {
452 const_cast<BundleSignature::Item &>(existing_item).structure_type =
454 }
455 }
456 }
457 }
458 return signature;
459}
460
462 const Bundle &bundle,
463 const FunctionRef<void(Span<BundleKey>, const BundleItemValue &value)> fn,
464 Vector<BundleKey> &r_path)
465{
466 for (const auto &child_item : bundle.items()) {
467 r_path.append(child_item.key);
468 BLI_SCOPED_DEFER([&]() { r_path.pop_last(); });
469
470 if (const BundlePtr *child_bundle_ptr = child_item.value.as_pointer<BundlePtr>()) {
471 if (*child_bundle_ptr) {
472 const Bundle &child_bundle = **child_bundle_ptr;
473 if (!child_bundle.type().has_value()) {
474 foreach_nested_bundle_item_recursive(child_bundle, fn, r_path);
475 continue;
476 }
477 }
478 }
479 fn(r_path, child_item.value);
480 }
481}
482
484 const Bundle &bundle,
485 const FunctionRef<void(Span<BundleKey>, const BundleItemValue &value)> fn)
486{
489}
490
492 const Bundle &bundle, const FunctionRef<bool(StringRef type)> type_filter_fn)
493{
496 bundle, [&](const Span<BundleKey> path, const BundleItemValue &value) {
497 if (const BundlePtr *child_bundle_ptr = value.as_pointer<BundlePtr>()) {
498 if (*child_bundle_ptr) {
499 if (const std::optional<StringRef> type = (*child_bundle_ptr)->type()) {
500 if (type_filter_fn(*type)) {
501 paths.append(Bundle::combine_path(path));
502 }
503 }
504 }
505 }
506 });
507 return paths;
508}
509
511 const eNodeSocketDatatype data_type)
512{
515 bundle, [&](const Span<BundleKey> path, const BundleItemValue &value) {
516 if (const auto *socket_value = std::get_if<BundleItemSocketValue>(&value.value)) {
517 if (socket_value->type->type == data_type) {
518 paths.append(Bundle::combine_path(path));
519 }
520 }
521 });
522 return paths;
523}
524
525std::optional<bke::SocketValueVariant> BundleItemValue::as_socket_value(
526 const bke::bNodeSocketType &dst_socket_type) const
527{
528 const BundleItemSocketValue *socket_value = std::get_if<BundleItemSocketValue>(&this->value);
529 if (!socket_value) {
530 return std::nullopt;
531 }
532 bke::SocketValueVariant copy = socket_value->value;
533 if (!copy.try_convert(*dst_socket_type.base_cpp_type)) {
534 return std::nullopt;
535 }
536 return copy;
537}
538
539} // namespace blender::nodes
#define BLI_SCOPED_DEFER(function_to_defer)
constexpr IndexRange()=default
void remove_contained_as(const ForwardKey &key)
Definition BLI_map.hh:391
Value & lookup_or_add_cb_as(ForwardKey &&key, const CreateValueF &create_value)
Definition BLI_map.hh:630
const Value & lookup_as(const ForwardKey &key) const
Definition BLI_map.hh:553
bool add_overwrite_as(ForwardKey &&key, ForwardValue &&...value)
Definition BLI_map.hh:342
ItemIterator items() const &
Definition BLI_map.hh:902
constexpr Span drop_front(int64_t n) const
Definition BLI_span.hh:175
constexpr Span drop_back(int64_t n) const
Definition BLI_span.hh:186
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:329
constexpr bool is_empty() const
Definition BLI_span.hh:264
void append(const T &value)
static SocketValueVariant from(T &&value)
nodes::StructureType inferred_structure_type
constexpr int64_t find_first_of(StringRef chars, int64_t pos=0) const
constexpr StringRef trim() const
static constexpr int64_t not_found
constexpr bool is_empty() const
constexpr StringRef substr(int64_t start, int64_t size) const
static bool is_valid_key(StringRef key)
static constexpr StringRefNull forbidden_key_chars
static std::optional< BundleKey > from_str(StringRef key)
void add_path_new(StringRef path, const BundleItemValue &value)
void merge_override(const Bundle &other)
void count_memory(MemoryCounter &memory) const
bool add(BundleKey key, const BundleItemValue &value)
static std::string combine_path(Span< StringRef > path)
static bool is_valid_path(StringRef path)
bool contains(BundleKey key) const
bool contains_path(StringRef path) const
std::optional< StringRef > type() const
BundleItemValue * lookup_path_for_write(Span< BundleKey > path)
void add_override(BundleKey key, const BundleItemValue &value)
void add_path_override(StringRef path, const BundleItemValue &value)
bool remove_path(StringRef path)
void merge(const Bundle &other)
const BundleItemValue * lookup_path(Span< BundleKey > path) const
const BundleItemValue * lookup(BundleKey key) const
static std::optional< Vector< BundleKey > > split_path(StringRef path)
Bundle & ensure_nested_bundle(StringRef path)
bool add_path(StringRef path, const BundleItemValue &value)
void add_new(BundleKey key, const BundleItemValue &value)
T * lookup_path_for_write_ptr(StringRef path)
BundleItemMap::ItemIterator items() const
#define storage(slot, qualifiers)
bNodeSocketType * node_socket_type_find_static(int type, int subtype=0)
Definition node.cc:2975
static BundleItemValue create_nested_bundle_item()
NodeSocketInterfaceStructureType get_structure_type_for_bundle_signature(const bNodeSocket &socket, const NodeSocketInterfaceStructureType stored_structure_type, const bool allow_auto_structure_type)
static void foreach_nested_bundle_item_recursive(const Bundle &bundle, const FunctionRef< void(Span< BundleKey >, const BundleItemValue &value)> fn, Vector< BundleKey > &r_path)
Vector< std::string > gather_bundle_paths_by_bundle_type(const Bundle &bundle, const FunctionRef< bool(StringRef type)> type_filter_fn)
Vector< std::string > gather_bundle_paths_by_data_type(const Bundle &bundle, const eNodeSocketDatatype data_type)
void foreach_nested_bundle_item(const Bundle &bundle, const FunctionRef< void(Span< BundleKey >, const BundleItemValue &value)> fn)
ImplicitSharingPtr< Bundle > BundlePtr
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
NodeSocketInterfaceStructureType structure_type
NodeSocketInterfaceStructureType structure_type
bke::bNodeSocketRuntime * runtime
Defines a socket type.
Definition BKE_node.hh:163
const CPPType * base_cpp_type
Definition BKE_node.hh:210
eNodeSocketDatatype type
Definition BKE_node.hh:198
std::variant< BundleItemSocketValue, BundleItemInternalValue > value
std::optional< bke::SocketValueVariant > as_socket_value(const bke::bNodeSocketType &dst_socket_type) const
static BundleSignature from_separate_bundle_node(const bNode &node, bool allow_auto_structure_type)
CustomIDVectorSet< Item, ItemKeyGetter > items
void add(std::string key, const eNodeSocketDatatype socket_type)
friend bool operator!=(const BundleSignature &a, const BundleSignature &b)
friend bool operator==(const BundleSignature &a, const BundleSignature &b)
static BundleSignature from_combine_bundle_node(const bNode &node, bool allow_auto_structure_type)
std::optional< BundleSignature > get_merged_signature() const