Blender V5.3
node_geo_combine_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
5#include "NOD_geo_bundle.hh"
11#include "NOD_sync_sockets.hh"
12
13#include "BKE_idprop.hh"
14
15#include "BLO_read_write.hh"
16
18
19#include "COM_bundle_item.hh"
20#include "COM_node_operation.hh"
21
22#include "node_geometry_util.hh"
24
25namespace blender {
26
28
30
32{
33 b.use_custom_socket_order();
34 b.allow_any_socket_order();
35
36 const bNodeTree *tree = b.tree_or_null();
37 const bNode *node = b.node_or_null();
38
39 b.add_output<decl::Bundle>("Bundle"_ustr).propagate_all().structure_type(StructureType::Single);
40
41 if (tree && node) {
42 FlatBundleTypePtr flat_bundle_type;
43 if (const std::optional<StringRefNull> type = combine_bundle_node_type(*tree, *node)) {
44 flat_bundle_type = BundleTypeRegistry::try_find_single_flat(*type);
45 }
46
47 const NodeCombineBundle &storage = node_storage(*node);
48 for (const int i : IndexRange(storage.items_num)) {
49 const NodeCombineBundleItem &item = storage.items[i];
50 const eNodeSocketDatatype socket_type = item.socket_type;
51 const UString name(item.name);
53 auto &decl = b.add_input(socket_type, name, identifier)
54 .socket_name_ptr(
55 &tree->id, *CombineBundleItemsAccessor::item_srna, &item, "name");
57 decl.structure_type(StructureType(item.structure_type));
58 }
59 else {
60 decl.structure_type(StructureType::Dynamic);
61 }
62
63 decl.compositor_realization_mode(CompositorInputRealizationMode::None);
64
65 if (flat_bundle_type) {
66 if (const SocketDeclaration *src_decl = flat_bundle_type->find_decl(name)) {
67 decl.try_copy_ui_data(*src_decl);
68 }
69 }
70
71 if (i == 0 && socket_type == SOCK_STRING && name == Bundle::type_item_name.ustr()) {
72 decl.optional_label();
73 b.add_separator();
74 }
75 }
76 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
77 .custom_draw(socket_items::ui::draw_extend_socket_fn<CombineBundleItemsAccessor>());
78 }
79}
80
81static void node_init(bNodeTree * /*tree*/, bNode *node)
82{
83 auto *storage = MEM_new<NodeCombineBundle>(__func__);
84 node->storage = storage;
85}
86
87static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
88{
89 const NodeCombineBundle &src_storage = node_storage(*src_node);
90 auto *dst_storage = MEM_new<NodeCombineBundle>(__func__, dna::shallow_copy(src_storage));
91 dst_node->storage = dst_storage;
92
94}
95
97{
99 MEM_delete(static_cast<NodeCombineBundle *>(node->storage));
100}
101
103{
104 if (params.C && params.link.fromnode == &params.node && params.link.tosock->type == SOCK_BUNDLE)
105 {
106 const NodeCombineBundle &storage = node_storage(params.node);
107 if (storage.items_num == 0) {
108 SpaceNode *snode = CTX_wm_space_node(params.C);
109 if (snode && snode->edittree == &params.ntree) {
110 sync_sockets_combine_bundle(*snode, params.node, nullptr, params.link.tosock);
111 }
112 }
113 return true;
114 }
116 params.ntree, params.node, params.node, params.link);
117}
118
119static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *node_ptr)
120{
121 bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(node_ptr->owner_id);
122 bNode &node = *static_cast<bNode *>(node_ptr->data);
123
124 layout.use_property_split_set(true);
125 layout.use_property_decorate_set(false);
126
127 layout.op("node.sockets_sync", IFACE_("Sync"), ICON_FILE_REFRESH);
128 layout.prop(node_ptr, "define_signature", UI_ITEM_NONE, std::nullopt, ICON_NONE);
129 if (ui::Layout *panel = layout.panel(C, "bundle_items", false, IFACE_("Bundle Items"))) {
130 socket_items::ui::draw_items_list_with_operators<CombineBundleItemsAccessor>(
131 C, panel, ntree, node);
132 socket_items::ui::draw_active_item_props<CombineBundleItemsAccessor>(
133 ntree, node, [&](PointerRNA *item_ptr) {
134 panel->use_property_split_set(true);
135 panel->use_property_decorate_set(false);
136 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, IFACE_("Type"), ICON_NONE);
137 panel->prop(item_ptr, "structure_type", UI_ITEM_NONE, IFACE_("Shape"), ICON_NONE);
138 });
139 }
140}
141
146
148{
149 const bNode &node = params.node();
150 const NodeCombineBundle &storage = node_storage(node);
151
152 BundlePtr bundle_ptr = Bundle::create();
153 Bundle &bundle = bundle_ptr.ensure_mutable_inplace();
154
155 for (const int i : IndexRange(storage.items_num)) {
156 const NodeCombineBundleItem &item = storage.items[i];
158 if (!stype || !stype->geometry_nodes_default_value) {
159 continue;
160 }
161 const StringRef name = item.name;
162 const std::optional<BundleKey> key = BundleKey::from_str(name);
163 if (!key) {
164 continue;
165 }
167 node.input_socket(i).identifier_ustr());
168 bundle.add(*key, BundleItemSocketValue{stype, std::move(value)});
169 }
170
171 params.set_output("Bundle"_ustr, std::move(bundle_ptr));
172}
173
174using namespace blender::compositor;
175
177 public:
179
180 void execute() override
181 {
182 BundlePtr bundle_ptr = Bundle::create();
183 Bundle &bundle = bundle_ptr.ensure_mutable_inplace();
184
185 const NodeCombineBundle &storage = node_storage(this->node());
186 for (const int i : IndexRange(storage.items_num)) {
187 const StringRef name = storage.items[i].name;
188 const std::optional<BundleKey> key = BundleKey::from_str(name);
189 if (!key) {
190 continue;
191 }
192 Result &input = this->get_input(this->node().input_socket(i).identifier);
193 bundle.add(*key, BundleItem::new_bundle_item_value(this->context(), input));
194 }
195
196 Result &output = this->get_result("Bundle");
197 output.allocate_single_value();
198 output.set_single_value(std::move(bundle_ptr));
199 }
200};
201
203{
204 return new CombineBundleOperation(context, node);
205}
206
208{
209 const bNodeSocket &other_socket = params.other_socket();
210 if (other_socket.in_out == SOCK_OUT) {
212 params.node_tree().type))
213 {
214 return;
215 }
216 params.add_item(IFACE_("Item"), [](LinkSearchOpParams &params) {
217 bNode &node = params.add_node("NodeCombineBundle"_ustr);
218 const auto *item =
220 params.node_tree, node, params.socket.typeinfo->type, params.socket.name);
222 });
223 }
224 else {
225 if (other_socket.type != SOCK_BUNDLE) {
226 return;
227 }
228 params.add_item(IFACE_("Bundle"), [](LinkSearchOpParams &params) {
229 bNode &node = params.add_node("NodeCombineBundle"_ustr);
230 params.connect_available_socket(node, "Bundle"_ustr);
231
232 SpaceNode &snode = *CTX_wm_space_node(&params.C);
233 sync_sockets_combine_bundle(snode, node, nullptr);
234 });
235 }
236}
237
238static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
239{
241}
242
247
248static void node_register()
249{
250 static bke::bNodeType ntype;
251
252 common_node_type_base(&ntype, "NodeCombineBundle"_ustr, NODE_COMBINE_BUNDLE);
253 ntype.ui_name = "Combine Bundle";
254 ntype.ui_description = "Combine multiple socket values into one.";
256 ntype.declare = node_declare;
257 ntype.initfunc = node_init;
266 bke::node_type_storage(ntype, "NodeCombineBundle", node_free_storage, node_copy_storage);
268}
270
271} // namespace nodes::node_geo_combine_bundle_cc
272
273namespace nodes {
274
275StructRNA **CombineBundleItemsAccessor::item_srna = &RNA_NodeCombineBundleItem;
276
278{
279 writer->write_string(item.name);
280}
281
286
288{
289 std::string result;
290 if (name.is_empty()) {
291 return result;
292 }
293 const Span<char> forbidden_chars = BundleKey::forbidden_key_chars;
294 for (const char c : name) {
295 if (forbidden_chars.contains(c)) {
296 result += '_';
297 }
298 else {
299 result += c;
300 }
301 }
302 if (!result.empty()) {
303 /* Disallow leading spaces. */
304 if (std::isspace(result[0])) {
305 result[0] = '_';
306 }
307 /* Disallow trailing spaces. */
308 const int last_index = result.size() - 1;
309 if (std::isspace(result[last_index])) {
310 result[last_index] = '_';
311 }
312 }
314 return result;
315}
316
317std::optional<StringRefNull> combine_bundle_node_type(const bNodeTree & /*tree*/,
318 const bNode &node)
319{
320 BLI_assert(node.is_type("NodeCombineBundle"_ustr));
321 /* Not using topology cache because this is called while building the node. */
322 for (const bNodeSocket &socket : node.inputs) {
323 if (socket.type != SOCK_STRING) {
324 continue;
325 }
326 if (socket.name != Bundle::type_item_name.ustr()) {
327 continue;
328 }
329 return socket.default_value_typed<bNodeSocketValueString>()->value;
330 }
331 return std::nullopt;
332}
333
334} // namespace nodes
335
336} // namespace blender
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_COMBINE_BUNDLE
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define output
constexpr IndexRange()=default
constexpr bool contains(const T &value) const
Definition BLI_span.hh:281
UString()=default
constexpr bool is_empty() const
static nodes::BundleItemValue new_bundle_item_value(Context &context, Result &result)
NodeOperation(Context &context, const bNode &node)
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
static bool is_valid_key(StringRef key)
static constexpr StringRefNull forbidden_key_chars
static std::optional< BundleKey > from_str(StringRef key)
static FlatBundleTypePtr try_find_single_flat(StringRef name)
bool add(BundleKey key, const BundleItemValue &value)
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
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)
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 bool node_insert_link(bke::NodeInsertLinkParams &params)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static void node_init(bNodeTree *, bNode *node)
static void node_geo_exec(GeoNodeExecParams params)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *node_ptr)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static void node_declare(NodeDeclarationBuilder &b)
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_combine_bundle(SpaceNode &snode, bNode &combine_bundle_node, ReportList *reports, const bNodeSocket *src_bundle_socket)
std::shared_ptr< const FlatBundleType > FlatBundleTypePtr
std::optional< StringRefNull > combine_bundle_node_type(const bNodeTree &tree, const bNode &node)
ImplicitSharingPtr< Bundle > BundlePtr
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
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
static void blend_write_item(BlendWriter *writer, const ItemT &item)
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
static bool supports_socket_type(const eNodeSocketDatatype socket_type, const int ntree_type)
static std::string validate_name(const StringRef name)
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)