Blender V5.3
node_geo_get_bundle_item.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "NOD_geo_bundle.hh"
9#include "NOD_rna_define.hh"
10
11#include "RNA_enum_types.hh"
12
14#include "UI_resources.hh"
15
17
18#include "COM_bundle_item.hh"
20#include "COM_node_operation.hh"
21
22#include <fmt/format.h>
23
25
27
29{
30 b.use_custom_socket_order();
31 b.allow_any_socket_order();
32 b.add_default_layout();
33 const bNode *node = b.node_or_null();
34
35 b.add_input<decl::Bundle>("Bundle"_ustr);
36 b.add_output<decl::Bundle>("Bundle"_ustr).align_with_previous().propagate_all();
37 if (node != nullptr) {
38 const NodeGetBundleItem &storage = node_storage(*node);
39 const eNodeSocketDatatype socket_type = storage.socket_type;
40 auto &decl = b.add_output(socket_type, "Item"_ustr).propagate_all();
41 if (storage.structure_type == NodeSocketInterfaceStructureType::Auto) {
42 decl.structure_type(StructureType::Dynamic);
43 }
44 else {
45 decl.structure_type(StructureType(storage.structure_type));
46 }
47 }
48 b.add_output<decl::Bool>("Exists"_ustr);
49 b.add_input<decl::String>("Path"_ustr).optional_label();
50 b.add_input<decl::Bool>("Remove"_ustr);
51}
52
53static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
54{
55 layout.use_property_split_set(true);
56 layout.use_property_decorate_set(false);
57 layout.prop(ptr, "socket_type", UI_ITEM_NONE, "", ICON_NONE);
58}
59
60static void node_layout_ex(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
61{
62 layout.prop(ptr, "socket_type", UI_ITEM_NONE, "", ICON_NONE);
63 layout.use_property_split_set(true);
64 layout.use_property_decorate_set(false);
65 layout.prop(ptr, "structure_type", UI_ITEM_NONE, IFACE_("Shape"), ICON_NONE);
66}
67
68static void node_init(bNodeTree * /*tree*/, bNode *node)
69{
70 auto *storage = MEM_new<NodeGetBundleItem>(__func__);
71 storage->socket_type = SOCK_FLOAT;
72 node->storage = storage;
73}
74
76{
77 const bNode &node = params.node();
78 const NodeGetBundleItem &storage = node_storage(node);
79
80 nodes::BundlePtr bundle = params.extract_input<nodes::BundlePtr>("Bundle"_ustr);
81 if (!bundle) {
83 return;
84 }
85
86 const std::string path = params.extract_input<std::string>("Path"_ustr);
87 const bool remove = params.extract_input<bool>("Remove"_ustr);
88
89 if (!Bundle::is_valid_path(path)) {
90 if (!path.empty()) {
91 params.error_message_add(NodeWarningType::Warning, "Invalid bundle path");
92 }
93 params.set_output("Bundle"_ustr, std::move(bundle));
95 return;
96 }
97
98 const BundleItemValue *value = bundle->lookup_path(path);
99 if (!value) {
100 if (!params.output_is_required("Exists"_ustr)) {
101 params.error_message_add(NodeWarningType::Warning, "Bundle path not found");
102 }
103 params.set_output("Bundle"_ustr, std::move(bundle));
105 return;
106 }
107 const auto *socket_value = std::get_if<BundleItemSocketValue>(&value->value);
108 if (!socket_value) {
109 params.error_message_add(
111 fmt::format("{}: \"{}\"", TIP_("Cannot get internal value from bundle"), path));
112 params.set_output("Bundle"_ustr, std::move(bundle));
114 return;
115 }
116
118 SocketValueVariant output_value = socket_value->value;
119 if (socket_value->type->type != stype->type) {
120 if (std::optional<SocketValueVariant> converted_value = implicitly_convert_socket_value(
121 *socket_value->type, output_value, *stype))
122 {
123 output_value = std::move(*converted_value);
124 }
125 else {
127 "Cannot implicitly convert item to the selected type");
128 params.set_output("Bundle"_ustr, std::move(bundle));
130 return;
131 }
132 }
133
134 if (remove) {
135 bundle.ensure_mutable_inplace().remove_path(path);
136 }
137
138 params.set_output("Bundle"_ustr, std::move(bundle));
139 params.set_output("Item"_ustr, std::move(output_value));
140 params.set_output("Exists"_ustr, true);
141}
142
143using namespace blender::compositor;
144
146 public:
148
149 void execute() override
150 {
151 Result &bundle_output = this->get_result("Bundle");
152 const Result &bundle_input = this->get_input("Bundle");
153
154 const std::string path = this->get_input("Path").get_single_value<std::string>();
155 const std::optional<Vector<BundleKey>> split_path = Bundle::split_path(path);
156 if (!split_path.has_value()) {
157 if (!path.empty()) {
158 this->add_warning(NodeWarningType::Warning, "Invalid bundle path");
159 }
160 if (bundle_output.should_compute()) {
161 bundle_output.share_data(bundle_input);
162 }
164 return;
165 }
166
167 Result &item_output = this->get_result("Item");
168 Result &exists_output = this->get_result("Exists");
169
170 nodes::BundlePtr bundle = bundle_input.get_single_value<nodes::BundlePtr>();
171
172 const BundleItemValue *item_value = bundle->lookup_path(split_path.value());
173 if (!item_value) {
174 if (bundle_output.should_compute()) {
175 bundle_output.share_data(bundle_input);
176 }
177 if (exists_output.should_compute()) {
178 exists_output.allocate_single_value();
179 exists_output.set_single_value(false);
180 }
181 else {
182 this->add_warning(NodeWarningType::Warning, "Bundle path not found");
183 }
185 return;
186 }
187
188 Result bundle_result = BundleItem::get_result(this->context(), *item_value);
189 BLI_SCOPED_DEFER([&]() { bundle_result.release(); });
190
191 if (item_output.should_compute()) {
192 if (item_output.type() == bundle_result.type()) {
193 item_output.share_data(bundle_result);
194 }
195 else {
197 if (conversions.is_convertible(bundle_result.get_cpp_type(), item_output.get_cpp_type())) {
198 ConversionOperation conversion_operation(
199 this->context(), bundle_result.type(), item_output.type());
200 Result conversion_input = this->context().create_result(bundle_result.type(),
201 bundle_result.precision());
202 conversion_input.share_data(bundle_result);
203 conversion_operation.map_input_to_result(&conversion_input);
204 conversion_operation.evaluate();
205 item_output.share_data(conversion_operation.get_result());
206 conversion_operation.get_result().release();
207 }
208 else {
209 item_output.allocate_invalid();
211 "Cannot implicitly convert item to the selected type");
212 }
213 }
214 }
215
216 if (bundle_output.should_compute()) {
217 if (this->get_input("Remove").get_single_value_default<bool>()) {
218 bundle.ensure_mutable_inplace().remove_path(split_path.value());
219 }
220 bundle_output.allocate_single_value();
221 bundle_output.set_single_value(std::move(bundle));
222 }
223
224 if (exists_output.should_compute()) {
225 exists_output.allocate_single_value();
226 exists_output.set_single_value(true);
227 }
228 }
229};
230
232{
233 return new GetBundleItemOperation(context, node);
234}
235
236static void node_rna(StructRNA *srna)
237{
239 "socket_type",
240 "Socket Type",
241 "Value may be implicitly converted if the type does not match",
243 NOD_storage_enum_accessors(socket_type),
245 [](bContext * /*C*/, PointerRNA *ptr, PropertyRNA * /*prop*/, bool *r_free) {
246 *r_free = true;
247 const bNodeTree &ntree = *id_cast<const bNodeTree *>(ptr->owner_id);
249 [&](const EnumPropertyItem &item) -> bool {
251 eNodeSocketDatatype(item.value), ntree.type);
252 });
253 });
255 "structure_type",
256 "Structure Type",
257 "What kind of higher order types are expected to flow through this socket",
259 NOD_storage_enum_accessors(structure_type));
260}
261
262static void node_register()
263{
264 static bke::bNodeType ntype;
265
266 geo_cmp_node_type_base(&ntype, "NodeGetBundleItem"_ustr);
267 ntype.ui_name = "Get Bundle Item";
268 ntype.ui_description = "Retrieve a bundle item by path.";
270 ntype.initfunc = node_init;
271 ntype.declare = node_declare;
277 ntype, "NodeGetBundleItem", node_free_standard_storage, node_copy_standard_storage);
279
280 node_rna(ntype.rna_ext.srna);
281}
283
284} // namespace blender::nodes::node_geo_get_bundle_item_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define BLI_SCOPED_DEFER(function_to_defer)
#define TIP_(msgid)
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
#define UI_ITEM_NONE
bool is_convertible(const CPPType &from_type, const CPPType &to_type) const
static Result get_result(Context &context, const nodes::BundleItemValue &item_value)
Result create_result(ResultType type, ResultPrecision precision)
NodeOperation(Context &context, const bNode &node)
void add_warning(nodes::NodeWarningType type, std::string message)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
void share_data(const Result &source)
Definition result.cc:996
const CPPType & get_cpp_type() const
Definition result.cc:482
bool should_compute() const
Definition result.cc:1124
void set_single_value(const T &value)
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
static ResultPrecision precision(gpu::TextureFormat format)
Definition result.cc:307
const T & get_single_value() const
static bool is_valid_path(StringRef path)
bool remove_path(StringRef path)
const BundleItemValue * lookup_path(Span< BundleKey > path) const
static std::optional< Vector< BundleKey > > split_path(StringRef path)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
#define storage(slot, qualifiers)
const DataTypeConversions & get_implicit_type_conversions()
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
bNodeSocketType * node_socket_type_find_static(int type, int subtype=0)
Definition node.cc:2975
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
static void node_geo_exec(GeoNodeExecParams params)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_layout_ex(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_init(bNodeTree *, bNode *node)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
std::optional< SocketValueVariant > implicitly_convert_socket_value(const bke::bNodeSocketType &from_type, const SocketValueVariant &from_value, const bke::bNodeSocketType &to_type)
bool socket_type_supported_in_bundle(const eNodeSocketDatatype socket_type, const int ntree_type)
const EnumPropertyItem * enum_items_filter(const EnumPropertyItem *original_item_array, FunctionRef< bool(const EnumPropertyItem &item)> fn)
ImplicitSharingPtr< Bundle > BundlePtr
const EnumPropertyItem rna_enum_node_socket_structure_type_items[]
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
void geo_cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
eNodeTree_Type type
Defines a socket type.
Definition BKE_node.hh:163
eNodeSocketDatatype type
Definition BKE_node.hh:198
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:292
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
NodeDeclareFunction declare
Definition BKE_node.hh:393
std::variant< BundleItemSocketValue, BundleItemInternalValue > value
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)