Blender V5.3
node_geo_separate_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
6
7#include "ED_screen.hh"
8
9#include "NOD_geo_bundle.hh"
15#include "NOD_sync_sockets.hh"
16
17#include "BKE_idprop.hh"
19
20#include "BLO_read_write.hh"
21
22#include "COM_bundle_item.hh"
24#include "COM_node_operation.hh"
25
28
29#include <fmt/format.h>
30
31namespace blender {
32
34
36
38{
39 b.add_input<decl::Bundle>("Bundle"_ustr).structure_type(StructureType::Single);
40 const bNodeTree *tree = b.tree_or_null();
41 const bNode *node = b.node_or_null();
42 if (tree && node) {
43 const NodeSeparateBundle &storage = node_storage(*node);
44 for (const int i : IndexRange(storage.items_num)) {
45 const NodeSeparateBundleItem &item = storage.items[i];
46 const eNodeSocketDatatype socket_type = item.socket_type;
47 const UString name = item.name ? UString(item.name) : ""_ustr;
49 auto &decl = b.add_output(socket_type, name, identifier)
50 .socket_name_ptr(
51 &tree->id, *SeparateBundleItemsAccessor::item_srna, &item, "name")
52 .propagate_all();
54 decl.structure_type(StructureType(item.structure_type));
55 }
56 else {
57 decl.structure_type(StructureType::Dynamic);
58 }
59 }
60 }
61 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
62 .custom_draw(socket_items::ui::draw_extend_socket_fn<SeparateBundleItemsAccessor>());
63}
64
65static void node_init(bNodeTree * /*tree*/, bNode *node)
66{
67 auto *storage = MEM_new<NodeSeparateBundle>(__func__);
68 node->storage = storage;
69}
70
71static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
72{
73 const NodeSeparateBundle &src_storage = node_storage(*src_node);
74 auto *dst_storage = MEM_new<NodeSeparateBundle>(__func__, dna::shallow_copy(src_storage));
75 dst_node->storage = dst_storage;
76
78}
79
81{
83 MEM_delete(static_cast<NodeSeparateBundle *>(node->storage));
84}
85
87{
88 if (params.C && params.link.tonode == &params.node && params.link.fromsock->type == SOCK_BUNDLE)
89 {
90 const NodeSeparateBundle &storage = node_storage(params.node);
91 if (storage.items_num == 0) {
92 SpaceNode *snode = CTX_wm_space_node(params.C);
93 if (snode && snode->edittree == &params.ntree) {
94 sync_sockets_separate_bundle(*snode, params.node, nullptr, params.link.fromsock);
95 }
96 }
97 return true;
98 }
100 params.ntree, params.node, params.node, params.link);
101}
102
103static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *node_ptr)
104{
105 bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(node_ptr->owner_id);
106 bNode &node = *static_cast<bNode *>(node_ptr->data);
107
108 layout.use_property_split_set(true);
109 layout.use_property_decorate_set(false);
110
111 layout.op("node.sockets_sync", IFACE_("Sync"), ICON_FILE_REFRESH);
112 layout.prop(node_ptr, "define_signature", UI_ITEM_NONE, std::nullopt, ICON_NONE);
113 if (ui::Layout *panel = layout.panel(C, "bundle_items", false, IFACE_("Bundle Items"))) {
114 socket_items::ui::draw_items_list_with_operators<SeparateBundleItemsAccessor>(
115 C, panel, ntree, node);
116 socket_items::ui::draw_active_item_props<SeparateBundleItemsAccessor>(
117 ntree, node, [&](PointerRNA *item_ptr) {
118 panel->use_property_split_set(true);
119 panel->use_property_decorate_set(false);
120 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, IFACE_("Type"), ICON_NONE);
121 panel->prop(item_ptr, "structure_type", UI_ITEM_NONE, IFACE_("Shape"), ICON_NONE);
122 });
123 }
124}
125
130
132{
133 nodes::BundlePtr bundle = params.extract_input<nodes::BundlePtr>("Bundle"_ustr);
134 if (!bundle) {
136 return;
137 }
138
139 const bNode &node = params.node();
140 const NodeSeparateBundle &storage = node_storage(node);
141
142 lf::Params &lf_params = params.low_level_lazy_function_params();
143
144 for (const int i : IndexRange(storage.items_num)) {
145 const NodeSeparateBundleItem &item = storage.items[i];
146 const StringRef name = item.name;
147 std::optional<BundleKey> key = BundleKey::from_str(name);
148 if (!key) {
149 continue;
150 }
152 if (!stype || !stype->geometry_nodes_default_value) {
153 continue;
154 }
155 const BundleItemValue *value = bundle->lookup(*key);
156 if (!value) {
157 params.error_message_add(
159 fmt::format(fmt::runtime(TIP_("Value not found in bundle: \"{}\"")), name));
160 continue;
161 }
162 const auto *socket_value = std::get_if<BundleItemSocketValue>(&value->value);
163 if (!socket_value) {
164 params.error_message_add(
166 fmt::format("{}: \"{}\"", TIP_("Cannot get internal value from bundle"), name));
167 continue;
168 }
169
170 SocketValueVariant output_value = std::move(socket_value->value);
171 if (socket_value->type->type != stype->type) {
172 if (std::optional<SocketValueVariant> converted_value = implicitly_convert_socket_value(
173 *socket_value->type, output_value, *stype))
174 {
175 output_value = std::move(*converted_value);
176 params.error_message_add(
178 fmt::format("{}: \"{}\" ({} " BLI_STR_UTF8_BLACK_RIGHT_POINTING_SMALL_TRIANGLE " {})",
179 TIP_("Implicit type conversion when separating bundle"),
180 name,
181 TIP_(socket_value->type->label),
182 TIP_(stype->label)));
183 }
184 else {
185 params.error_message_add(
187 fmt::format("{}: \"{}\" ({} " BLI_STR_UTF8_BLACK_RIGHT_POINTING_SMALL_TRIANGLE " {})",
188 TIP_("Conversion not supported when separating bundle"),
189 name,
190 TIP_(socket_value->type->label),
191 TIP_(stype->label)));
192 output_value = *stype->geometry_nodes_default_value;
193 }
194 }
195 lf_params.set_output(i, std::move(output_value));
196 }
197
199}
200
201using namespace blender::compositor;
202
204 public:
206
207 void execute() override
208 {
210
211 const NodeSeparateBundle &storage = node_storage(this->node());
212 for (const int i : IndexRange(storage.items_num)) {
213 Result &result = this->get_result(this->node().output_socket(i).identifier);
214 if (!result.should_compute()) {
215 continue;
216 }
217
218 const NodeSeparateBundleItem &item = storage.items[i];
219 const StringRef name = item.name;
220 std::optional<BundleKey> key = BundleKey::from_str(name);
221 if (!key) {
222 continue;
223 }
224
225 const BundleItemValue *item_value = bundle->lookup(key.value());
226 if (!item_value) {
227 this->add_warning(
229 fmt::format(fmt::runtime(TIP_("Value not found in bundle: \"{}\"")), name));
230 continue;
231 }
232
233 Result bundle_result = BundleItem::get_result(this->context(), *item_value);
234 BLI_SCOPED_DEFER([&]() { bundle_result.release(); });
235 if (result.type() == bundle_result.type()) {
236 result.share_data(bundle_result);
237 continue;
238 }
239
241 if (!conversions.is_convertible(bundle_result.get_cpp_type(), result.get_cpp_type())) {
242 this->add_warning(
244 fmt::format("{}: \"{}\" ({} " BLI_STR_UTF8_BLACK_RIGHT_POINTING_SMALL_TRIANGLE " {})",
245 TIP_("Conversion not supported when separating bundle"),
246 name,
247 TIP_(Result::type_name(bundle_result.type())),
248 TIP_(Result::type_name(result.type()))));
249 continue;
250 }
251
252 ConversionOperation conversion_operation(
253 this->context(), bundle_result.type(), result.type());
254 Result conversion_input = this->context().create_result(bundle_result.type(),
255 bundle_result.precision());
256 conversion_input.share_data(bundle_result);
257 conversion_operation.map_input_to_result(&conversion_input);
258 conversion_operation.evaluate();
259 result.share_data(conversion_operation.get_result());
260 conversion_operation.get_result().release();
261
262 this->add_warning(
264 fmt::format("{}: \"{}\" ({} " BLI_STR_UTF8_BLACK_RIGHT_POINTING_SMALL_TRIANGLE " {})",
265 TIP_("Implicit type conversion when separating bundle"),
266 name,
267 TIP_(Result::type_name(bundle_result.type())),
268 TIP_(Result::type_name(result.type()))));
269 }
270
272 }
273};
274
276{
277 return new SeparateBundleOperation(context, node);
278}
279
281{
282 const bNodeSocket &other_socket = params.other_socket();
283 if (other_socket.in_out == SOCK_IN) {
285 params.node_tree().type))
286 {
287 return;
288 }
289 params.add_item(IFACE_("Item"), [](LinkSearchOpParams &params) {
290 bNode &node = params.add_node("NodeSeparateBundle"_ustr);
291 const auto *item =
293 params.node_tree, node, params.socket.typeinfo->type, params.socket.name);
295 });
296 }
297 else {
298 if (other_socket.type != SOCK_BUNDLE) {
299 return;
300 }
301 params.add_item(IFACE_("Bundle"), [](LinkSearchOpParams &params) {
302 bNode &node = params.add_node("NodeSeparateBundle"_ustr);
303 params.connect_available_socket(node, "Bundle"_ustr);
304
305 SpaceNode &snode = *CTX_wm_space_node(&params.C);
306 sync_sockets_separate_bundle(snode, node, nullptr);
307 });
308 }
309}
310
311static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
312{
314}
315
320
321static void node_register()
322{
323 static bke::bNodeType ntype;
324
325 common_node_type_base(&ntype, "NodeSeparateBundle"_ustr, NODE_SEPARATE_BUNDLE);
326 ntype.ui_name = "Separate Bundle";
327 ntype.ui_description = "Split a bundle into multiple sockets.";
329 ntype.declare = node_declare;
330 ntype.initfunc = node_init;
339 bke::node_type_storage(ntype, "NodeSeparateBundle", node_free_storage, node_copy_storage);
341}
343
344} // namespace nodes::node_geo_separate_bundle_cc
345
346namespace nodes {
347
348StructRNA **SeparateBundleItemsAccessor::item_srna = &RNA_NodeSeparateBundleItem;
349
351{
352 writer->write_string(item.name);
353}
354
359
360} // namespace nodes
361} // namespace blender
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_SEPARATE_BUNDLE
#define BLI_SCOPED_DEFER(function_to_defer)
#define BLI_STR_UTF8_BLACK_RIGHT_POINTING_SMALL_TRIANGLE
#define TIP_(msgid)
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
constexpr IndexRange()=default
UString()=default
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
static const char * type_name(const ResultType type)
Definition result.cc:424
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 std::optional< BundleKey > from_str(StringRef key)
const BundleItemValue * lookup(BundleKey key) const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void connect_available_socket(bNode &new_node, UString socket_name)
#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
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *node_ptr)
static void node_init(bNodeTree *, bNode *node)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
void blend_write(BlendWriter *writer, const bNode &node)
void blend_read_data(BlendDataReader *reader, bNode &node)
void copy_array(const bNode &src_node, bNode &dst_node)
Accessor::ItemT * add_item_with_socket_type_and_name(bNodeTree &ntree, bNode &node, const eNodeSocketDatatype socket_type, const char *name, std::optional< int > dimensions=std::nullopt)
bool try_add_item_via_any_extend_socket(bNodeTree &ntree, bNode &extend_node, bNode &storage_node, bNodeLink &link, const std::optional< StringRef > socket_identifier=std::nullopt, typename Accessor::ItemT **r_new_item=nullptr)
void sync_sockets_separate_bundle(SpaceNode &snode, bNode &separate_bundle_node, ReportList *reports, const bNodeSocket *src_bundle_socket)
std::optional< SocketValueVariant > implicitly_convert_socket_value(const bke::bNodeSocketType &from_type, const SocketValueVariant &from_value, const bke::bNodeSocketType &to_type)
ImplicitSharingPtr< Bundle > BundlePtr
BMesh Iterators.
SpaceNode * CTX_wm_space_node(const bContext *C)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void write_string(const char *data)
NodeSocketInterfaceStructureType structure_type
struct bNodeTree * edittree
bke::bNodeSocketType * typeinfo
eNodeSocketDatatype type
eNodeSocketInOut in_out
eNodeTree_Type type
Defines a socket type.
Definition BKE_node.hh:163
eNodeSocketDatatype type
Definition BKE_node.hh:198
const SocketValueVariant * geometry_nodes_default_value
Definition BKE_node.hh:216
Defines a node type.
Definition BKE_node.hh:265
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:421
void(*) register_operators()
Definition BKE_node.hh:448
std::string ui_description
Definition BKE_node.hh:271
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:422
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
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
bool(*) insert_link(NodeInsertLinkParams &params)
Definition BKE_node.hh:364
std::variant< BundleItemSocketValue, BundleItemInternalValue > value
static bool supports_socket_type(const eNodeSocketDatatype socket_type, const int ntree_type)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
static std::string socket_identifier_for_item(const ItemT &item)
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
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)