Blender V5.3
geometry_nodes_closure.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 "BKE_node_runtime.hh"
10
13
14namespace blender::nodes {
15
16std::optional<int> ClosureSignature::find_input_index(const StringRef key) const
17{
18 for (const int i : this->inputs.index_range()) {
19 const Item &item = this->inputs[i];
20 if (item.key == key) {
21 return i;
22 }
23 }
24 return std::nullopt;
25}
26
27std::optional<int> ClosureSignature::find_output_index(const StringRef key) const
28{
29 for (const int i : this->outputs.index_range()) {
30 const Item &item = this->outputs[i];
31 if (item.key == key) {
32 return i;
33 }
34 }
35 return std::nullopt;
36}
37
39{
40 for (const Item &item : this->inputs) {
41 const_cast<Item &>(item).structure_type = NodeSocketInterfaceStructureType::Auto;
42 }
43 for (const Item &item : this->outputs) {
44 const_cast<Item &>(item).structure_type = NodeSocketInterfaceStructureType::Auto;
45 }
46}
47
49{
50 return a.inputs.as_span() == b.inputs.as_span() && a.outputs.as_span() == b.outputs.as_span();
51}
52
54{
55 return !(a == b);
56}
57
59 const bool allow_auto_structure_type)
60{
61 BLI_assert(node.is_type("NodeClosureOutput"_ustr));
62 const bNodeTree &tree = node.owner_tree();
63 const bNode *input_node =
65 const auto &storage = *static_cast<const NodeClosureOutput *>(node.storage);
67 if (input_node) {
68 for (const int i : IndexRange(storage.input_items.items_num)) {
69 const NodeClosureInputItem &item = storage.input_items.items[i];
70 const bNodeSocket &socket = input_node->output_socket(i);
72 {
73 const NodeSocketInterfaceStructureType structure_type =
75 socket, item.structure_type, allow_auto_structure_type);
76 signature.inputs.add({item.name, stype, structure_type});
77 }
78 }
79 }
80 for (const int i : IndexRange(storage.output_items.items_num)) {
81 const NodeClosureOutputItem &item = storage.output_items.items[i];
82 const bNodeSocket &socket = node.input_socket(i);
84 const NodeSocketInterfaceStructureType structure_type =
86 socket, item.structure_type, allow_auto_structure_type);
87 signature.outputs.add({item.name, stype, structure_type});
88 }
89 }
90 return signature;
91}
92
94 const bool allow_auto_structure_type)
95{
96 BLI_assert(node.is_type("NodeEvaluateClosure"_ustr));
97 const auto &storage = *static_cast<const NodeEvaluateClosure *>(node.storage);
99 for (const int i : IndexRange(storage.input_items.items_num)) {
100 const NodeEvaluateClosureInputItem &item = storage.input_items.items[i];
101 const bNodeSocket &socket = node.input_socket(i + 1);
103 const NodeSocketInterfaceStructureType structure_type =
105 socket, item.structure_type, allow_auto_structure_type);
106 signature.inputs.add({item.name, stype, structure_type});
107 }
108 }
109 for (const int i : IndexRange(storage.output_items.items_num)) {
110 const NodeEvaluateClosureOutputItem &item = storage.output_items.items[i];
111 const bNodeSocket &socket = node.output_socket(i);
113 const NodeSocketInterfaceStructureType structure_type =
115 socket, item.structure_type, allow_auto_structure_type);
116 signature.outputs.add({item.name, stype, structure_type});
117 }
118 }
119 return signature;
120}
121
123{
124 BLI_assert(node.is_type("GeometryNodeClosureToList"_ustr));
125 const auto &storage = *static_cast<const GeometryNodeClosureToList *>(node.storage);
126 ClosureSignature signature;
127 signature.inputs.add({.key = "Index",
128 .type = bke::node_socket_type_find("NodeSocketInt"),
130 for (const int i : IndexRange(storage.items_num)) {
131 const GeometryNodeClosureToListItem &item = storage.items[i];
132 const auto type = eNodeSocketDatatype(item.socket_type);
133 signature.outputs.add(
134 {.key = item.name,
136 .structure_type = NodeSocketInterfaceStructureType(item.structure_type)});
137 }
138 return signature;
139}
140
142{
143 for (const Item &item : this->items) {
144 if (item.define_signature) {
145 return true;
146 }
147 }
148 return false;
149}
150
151std::optional<ClosureSignature> LinkedClosureSignatures::get_merged_signature() const
152{
153 ClosureSignature signature;
154 for (const Item &src_signature : this->items) {
155 for (const ClosureSignature::Item &item : src_signature.signature.inputs) {
156 if (!signature.inputs.add(item)) {
157 const ClosureSignature::Item &existing_item = *signature.inputs.lookup_key_ptr_as(
158 item.key);
159 if (existing_item.type->type != item.type->type) {
160 return std::nullopt;
161 }
162 if (existing_item.structure_type != item.structure_type) {
163 const_cast<ClosureSignature::Item &>(existing_item).structure_type =
165 }
166 }
167 }
168 for (const ClosureSignature::Item &item : src_signature.signature.outputs) {
169 if (!signature.outputs.add(item)) {
170 const ClosureSignature::Item &existing_item = *signature.outputs.lookup_key_ptr_as(
171 item.key);
172 if (existing_item.type->type != item.type->type) {
173 return std::nullopt;
174 }
175 if (existing_item.structure_type != item.structure_type) {
176 const_cast<ClosureSignature::Item &>(existing_item).structure_type =
178 }
179 }
180 }
181 }
182 return signature;
183}
184
185} // namespace blender::nodes
constexpr IndexRange()=default
const bNode * get_corresponding_input(const bNodeTree &tree, const bNode &output_bnode) const
friend bool operator==(const ClosureSignature &a, const ClosureSignature &b)
std::optional< int > find_output_index(StringRef key) const
CustomIDVectorSet< Item, ItemKeyGetter > inputs
std::optional< int > find_input_index(StringRef key) const
CustomIDVectorSet< Item, ItemKeyGetter > outputs
friend bool operator!=(const ClosureSignature &a, const ClosureSignature &b)
static ClosureSignature from_evaluate_closure_node(const bNode &node, bool allow_auto_structure_type)
static ClosureSignature from_closure_output_node(const bNode &node, bool allow_auto_structure_type)
static ClosureSignature from_closure_to_list_node(const bNode &node)
#define storage(slot, qualifiers)
const bNodeZoneType * zone_type_by_node_type(const int node_type)
bNodeSocketType * node_socket_type_find(StringRef idname)
Definition node.cc:2966
bNodeSocketType * node_socket_type_find_static(int type, int subtype=0)
Definition node.cc:2975
NodeSocketInterfaceStructureType get_structure_type_for_bundle_signature(const bNodeSocket &socket, const NodeSocketInterfaceStructureType stored_structure_type, const bool allow_auto_structure_type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
NodeSocketInterfaceStructureType structure_type
eNodeSocketDatatype socket_type
NodeSocketInterfaceStructureType structure_type
NodeSocketInterfaceStructureType structure_type
NodeSocketInterfaceStructureType structure_type
NodeSocketInterfaceStructureType structure_type
Defines a socket type.
Definition BKE_node.hh:163
eNodeSocketDatatype type
Definition BKE_node.hh:198
std::optional< ClosureSignature > get_merged_signature() const