Blender V5.3
node_declaration.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
14
15#include "BLI_assert.hh"
16#include "BLI_listbase.hh"
17#include "BLI_utildefines.hh"
18
20#include "BKE_node.hh"
21#include "BKE_node_runtime.hh"
24
25#include "RNA_access.hh"
26
27namespace blender::nodes {
28
29static void reset_declaration(NodeDeclaration &declaration)
30{
31 std::destroy_at(&declaration);
32 new (&declaration) NodeDeclaration();
33}
34
36 NodeDeclaration &r_declaration,
37 const bNodeTree *ntree,
38 const bNode *node)
39{
40 reset_declaration(r_declaration);
41 NodeDeclarationBuilder node_decl_builder{typeinfo, r_declaration, ntree, node};
42 typeinfo.declare(node_decl_builder);
43 node_decl_builder.finalize();
44}
45
46void NodeDeclarationBuilder::build_remaining_anonymous_attribute_relations()
47{
49
50 Vector<int> data_inputs;
51 for (const int i : declaration_.inputs.index_range()) {
52 if (can_contain_referenced_data(declaration_.inputs[i]->socket_type)) {
53 data_inputs.append(i);
54 }
55 }
56 Vector<int> data_outputs;
57 for (const int i : declaration_.outputs.index_range()) {
58 if (can_contain_referenced_data(declaration_.outputs[i]->socket_type)) {
59 data_outputs.append(i);
60 }
61 }
62
63 for (BaseSocketDeclarationBuilder *socket_builder : input_socket_builders_) {
64 if (socket_builder->input_reference_used_on_all_data_) {
65 rl::RelationsInNode &relations = this->get_reference_lifetime_relations();
66 const int reference_input = socket_builder->decl_base_->index;
67 for (const int data_input : data_inputs) {
68 relations.use_relations.append({reference_input, data_input});
69 }
70 }
71 }
72 for (BaseSocketDeclarationBuilder *socket_builder : output_socket_builders_) {
73 if (socket_builder->output_reference_available_on_all_data_) {
74 rl::RelationsInNode &relations = this->get_reference_lifetime_relations();
75 const int reference_output = socket_builder->decl_base_->index;
76 for (const int data_output : data_outputs) {
77 relations.available_relations.append({reference_output, data_output});
78 }
79 }
80 if (socket_builder->propagate_all_input_references_) {
81 rl::RelationsInNode &relations = this->get_reference_lifetime_relations();
82 const int reference_output = socket_builder->decl_base_->index;
83 for (const int input_i : declaration_.inputs.index_range()) {
84 SocketDeclaration &input_socket_decl = *declaration_.inputs[input_i];
85 if (ELEM(input_socket_decl.structure_type, StructureType::Field, StructureType::Dynamic) ||
86 ELEM(input_socket_decl.socket_type, SOCK_GEOMETRY, SOCK_BUNDLE, SOCK_CLOSURE))
87 {
88 relations.reference_propagations.append({input_i, reference_output});
89 }
90 }
91 }
92 if (socket_builder->propagate_all_input_data_) {
93 rl::RelationsInNode &relations = this->get_reference_lifetime_relations();
94 const int data_output = socket_builder->decl_base_->index;
95 for (const int data_input : data_inputs) {
96 relations.data_propagations.append({data_input, data_output});
97 relations.reference_propagations.append({data_input, data_output});
98 }
99 }
100 if (socket_builder->propagate_all_input_data_from_geometry_) {
101 rl::RelationsInNode &relations = this->get_reference_lifetime_relations();
102 const int data_output = socket_builder->decl_base_->index;
103 for (const int i : declaration_.inputs.index_range()) {
104 if (declaration_.inputs[i]->socket_type == SOCK_GEOMETRY) {
105 relations.data_propagations.append({i, data_output});
106 relations.reference_propagations.append({i, data_output});
107 }
108 }
109 }
110 }
111}
112
114{
115 this->build_remaining_anonymous_attribute_relations();
116#ifndef NDEBUG
117 declaration_.assert_valid();
118#endif
119}
120
123 const bNodeTree *ntree,
124 const bNode *node)
125 : DeclarationListBuilder(*this, declaration.root_items),
126 typeinfo_(typeinfo),
127 declaration_(declaration),
128 ntree_(ntree),
129 node_(node)
130{
131 /* Unused in release builds, but used for BLI_assert() in debug builds. */
132 UNUSED_VARS(typeinfo_);
133}
134
136{
137 declaration_.use_custom_socket_order = enable;
138}
139
141{
142 BLI_assert(declaration_.use_custom_socket_order);
143 declaration_.allow_any_socket_order = enable;
144}
145
147{
148 if (in_out == SOCK_IN) {
149 return inputs;
150 }
151 return outputs;
152}
153
154namespace reference_lifetimes {
155
156std::ostream &operator<<(std::ostream &stream, const RelationsInNode &relations)
157{
158 stream << "Propagate Relations: " << relations.data_propagations.size() << "\n";
159 for (const DataPropagation &relation : relations.data_propagations) {
160 stream << " " << relation.from_input << " -> " << relation.to_output << "\n";
161 }
162 stream << "Reference Relations: " << relations.reference_propagations.size() << "\n";
163 for (const ReferencePropagation &relation : relations.reference_propagations) {
164 stream << " " << relation.from_input << " -> " << relation.to_output << "\n";
165 }
166 stream << "Eval Relations: " << relations.use_relations.size() << "\n";
167 for (const UseRelation &relation : relations.use_relations) {
168 stream << " eval " << relation.reference_input << " on " << relation.data_input << "\n";
169 }
170 stream << "Available Relations: " << relations.available_relations.size() << "\n";
171 for (const AvailableRelation &relation : relations.available_relations) {
172 stream << " " << relation.reference_output << " available on " << relation.data_output
173 << "\n";
174 }
175 stream << "Available on None: " << relations.available_on_none.size() << "\n";
176 for (const int i : relations.available_on_none) {
177 stream << " output " << i << " available on none\n";
178 }
179 return stream;
180}
181
182} // namespace reference_lifetimes
183
187 Vector<const SocketDeclaration *> &r_flat_outputs)
188{
189 /* Expected item order unless any order is allowed: outputs, inputs, panels. */
190 bool found_input = false;
191 bool found_panel = false;
192
193 for (const ItemDeclaration *item_decl : items) {
194 if (const auto *socket_decl = dynamic_cast<const SocketDeclaration *>(item_decl)) {
195 if (socket_decl->in_out == SOCK_IN) {
196 BLI_assert(node_decl.allow_any_socket_order || !found_panel);
197 /* Panel toggles are always the first socket, breaking expected outputs-inputs ordering. */
198 if (!socket_decl->is_panel_toggle) {
199 found_input = true;
200 }
201 r_flat_inputs.append(socket_decl);
202 }
203 else {
204 BLI_assert(node_decl.allow_any_socket_order || (!found_input && !found_panel));
205 r_flat_outputs.append(socket_decl);
206 }
207 }
208 else if (const auto *panel_decl = dynamic_cast<const PanelDeclaration *>(item_decl)) {
209 found_panel = true;
210 assert_valid_panels_recursive(node_decl, panel_decl->items, r_flat_inputs, r_flat_outputs);
211 }
212 }
213 UNUSED_VARS(found_input, found_panel);
214}
215
217{
218 if (!this->use_custom_socket_order) {
219 /* Skip validation for conventional socket layouts. Those are reordered in drawing code. */
220 return;
221 }
222
225 assert_valid_panels_recursive(*this, this->root_items, flat_inputs, flat_outputs);
226
227 BLI_assert(this->inputs.as_span() == flat_inputs);
228 BLI_assert(this->outputs.as_span() == flat_outputs);
229}
230
232{
233 const bNodeSocket *current_input = node.inputs.first();
234 const bNodeSocket *current_output = node.outputs.first();
235 const bNodePanelState *current_panel = node.panel_states_array;
236 for (const ItemDeclarationPtr &item_decl : this->all_items) {
237 if (const SocketDeclaration *socket_decl = dynamic_cast<const SocketDeclaration *>(
238 item_decl.get()))
239 {
240 switch (socket_decl->in_out) {
241 case SOCK_IN:
242 if (current_input == nullptr || !socket_decl->matches(*current_input)) {
243 return false;
244 }
245 current_input = current_input->next;
246 break;
247 case SOCK_OUT:
248 if (current_output == nullptr || !socket_decl->matches(*current_output)) {
249 return false;
250 }
251 current_output = current_output->next;
252 break;
253 }
254 }
255 else if (const PanelDeclaration *panel_decl = dynamic_cast<const PanelDeclaration *>(
256 item_decl.get()))
257 {
258 if (!node.panel_states().contains_ptr(current_panel) || !panel_decl->matches(*current_panel))
259 {
260 return false;
261 }
262 ++current_panel;
263 }
264 else if (dynamic_cast<const SeparatorDeclaration *>(item_decl.get()) ||
265 dynamic_cast<const LayoutDeclaration *>(item_decl.get()))
266 {
267 /* Ignored because they don't have corresponding data in DNA. */
268 }
269 else {
270 /* Unknown item type. */
272 }
273 }
274 /* If items are left over, some were removed from the declaration. */
275 if (current_input != nullptr || current_output != nullptr ||
276 node.panel_states().contains_ptr(current_panel))
277 {
278 return false;
279 }
280 return true;
281}
282
284 bNode &node,
285 bNodeSocket &socket) const
286{
287 /* By default just rebuild. */
288 BLI_assert(socket.in_out == this->in_out);
289 UNUSED_VARS_NDEBUG(socket);
290 return this->build(ntree, node);
291}
292
299
301{
302 if (socket.name != this->name) {
303 return false;
304 }
305 if (socket.identifier != this->identifier) {
306 return false;
307 }
308 if (((socket.flag & SOCK_HIDE_VALUE) != 0) != this->hide_value) {
309 return false;
310 }
311 if (((socket.flag & SOCK_MULTI_INPUT) != 0) != this->is_multi_input) {
312 return false;
313 }
314 if (((socket.flag & SOCK_UNAVAIL) != 0) != !this->is_available) {
315 return false;
316 }
317 return true;
318}
319
320template<typename Fn>
321static bool socket_type_to_static_decl_type(const eNodeSocketDatatype socket_type, Fn &&fn)
322{
323 switch (socket_type) {
324 case SOCK_FLOAT:
325 fn.template operator()<decl::Float>();
326 return true;
327 case SOCK_VECTOR:
328 fn.template operator()<decl::Vector>();
329 return true;
330 case SOCK_RGBA:
331 fn.template operator()<decl::Color>();
332 return true;
333 case SOCK_SHADER:
334 fn.template operator()<decl::Shader>();
335 return true;
336 case SOCK_BOOLEAN:
337 fn.template operator()<decl::Bool>();
338 return true;
339 case SOCK_ROTATION:
340 fn.template operator()<decl::Rotation>();
341 return true;
342 case SOCK_MATRIX:
343 fn.template operator()<decl::Matrix>();
344 return true;
345 case SOCK_INT:
346 fn.template operator()<decl::Int>();
347 return true;
348 case SOCK_STRING:
349 fn.template operator()<decl::String>();
350 return true;
351 case SOCK_GEOMETRY:
352 fn.template operator()<decl::Geometry>();
353 return true;
354 case SOCK_OBJECT:
355 fn.template operator()<decl::Object>();
356 return true;
357 case SOCK_IMAGE:
358 fn.template operator()<decl::Image>();
359 return true;
360 case SOCK_COLLECTION:
361 fn.template operator()<decl::Collection>();
362 return true;
363 case SOCK_MATERIAL:
364 fn.template operator()<decl::Material>();
365 return true;
366 case SOCK_FONT:
367 fn.template operator()<decl::Font>();
368 return true;
369 case SOCK_SCENE:
370 fn.template operator()<decl::Scene>();
371 return true;
372 case SOCK_TEXT_ID:
373 fn.template operator()<decl::Text>();
374 return true;
375 case SOCK_MASK:
376 fn.template operator()<decl::Mask>();
377 return true;
378 case SOCK_SOUND:
379 fn.template operator()<decl::Sound>();
380 return true;
381 case SOCK_MENU:
382 fn.template operator()<decl::Menu>();
383 return true;
384 case SOCK_BUNDLE:
385 fn.template operator()<decl::Bundle>();
386 return true;
387 case SOCK_CLOSURE:
388 fn.template operator()<decl::Closure>();
389 return true;
390 case SOCK_INT_VECTOR:
391 fn.template operator()<decl::IntVector>();
392 return true;
393 default:
394 return false;
395 }
396}
397
398std::unique_ptr<SocketDeclaration> make_declaration_for_socket_type(
399 const eNodeSocketDatatype socket_type)
400{
401 std::unique_ptr<SocketDeclaration> decl;
402 socket_type_to_static_decl_type(socket_type, [&]<std::derived_from<SocketDeclaration> DeclT>() {
403 decl = std::make_unique<DeclT>();
404 });
405 return decl;
406}
407
409 const eNodeSocketDatatype socket_type, const UString name, const UString identifier)
410{
412 socket_type_to_static_decl_type(socket_type, [&]<std::derived_from<SocketDeclaration> DeclT>() {
413 decl = &this->add_input<DeclT>(name, identifier);
414 });
415 if (!decl) {
417 decl = &this->add_input<decl::Float>(""_ustr, ""_ustr);
418 }
419 return *decl;
420}
421
423 const UString name,
424 const UString identifier)
425{
426 return this->add_input(*bke::custom_data_type_to_socket_type(data_type), name, identifier);
427}
428
430 const eNodeSocketDatatype socket_type, const UString name, const UString identifier)
431{
433 socket_type_to_static_decl_type(socket_type, [&]<std::derived_from<SocketDeclaration> DeclT>() {
434 decl = &this->add_output<DeclT>(name, identifier);
435 });
436 if (!decl) {
438 decl = &this->add_output<decl::Float>(""_ustr, ""_ustr);
439 }
440 return *decl;
441}
442
444 const UString name,
445 const UString identifier)
446{
447 return this->add_output(*bke::custom_data_type_to_socket_type(data_type), name, identifier);
448}
449
451{
452 auto decl_ptr = std::make_unique<SeparatorDeclaration>();
453 SeparatorDeclaration &decl = *decl_ptr;
454 this->node_decl_builder.declaration_.all_items.append(std::move(decl_ptr));
455 this->items.append(&decl);
456}
457
459{
461 this->add_layout([](ui::Layout &layout, bContext *C, PointerRNA *ptr) {
462 const bNode &node = *static_cast<bNode *>(ptr->data);
463 node.typeinfo->draw_buttons(layout, C, ptr);
464 });
465 static_cast<LayoutDeclaration &>(*this->items.last()).is_default = true;
466}
467
469 std::function<void(ui::Layout &, bContext *, PointerRNA *)> draw)
470{
471 auto decl_ptr = std::make_unique<LayoutDeclaration>();
472 LayoutDeclaration &decl = *decl_ptr;
473 decl.draw = std::move(draw);
474 this->node_decl_builder.declaration_.all_items.append(std::move(decl_ptr));
475 this->items.append(&decl);
476}
477
479{
480 auto panel_decl_ptr = std::make_unique<PanelDeclaration>();
481 PanelDeclaration &panel_decl = *panel_decl_ptr;
482 auto panel_decl_builder_ptr = std::make_unique<PanelDeclarationBuilder>(this->node_decl_builder,
483 panel_decl);
484 PanelDeclarationBuilder &panel_decl_builder = *panel_decl_builder_ptr;
485
486 if (identifier >= 0) {
487 panel_decl.identifier = identifier;
488 }
489 else {
490 /* Use index as identifier. */
491 panel_decl.identifier = this->node_decl_builder.declaration_.all_items.size();
492 }
493 panel_decl.name = name;
494 panel_decl.parent_panel = this->parent_panel_decl;
495 panel_decl.index = this->node_decl_builder.declaration_.panels.append_and_get_index(&panel_decl);
496 this->node_decl_builder.declaration_.all_items.append(std::move(panel_decl_ptr));
497 this->node_decl_builder.panel_builders_.append_and_get_index(std::move(panel_decl_builder_ptr));
498 this->items.append(&panel_decl);
499 return panel_decl_builder;
500}
501
503{
504 panel = {0};
505 panel.identifier = this->identifier;
506 SET_FLAG_FROM_TEST(panel.flag, this->default_collapsed, NODE_PANEL_COLLAPSED);
507}
508
510{
511 return panel.identifier == this->identifier;
512}
513
515 bNodePanelState &new_panel) const
516{
517 build(new_panel);
518 /* Copy existing state to the new panel */
519 SET_FLAG_FROM_TEST(new_panel.flag, old_panel.is_collapsed(), NODE_PANEL_COLLAPSED);
520}
521
523{
524 int count = 0;
526 {
527 count++;
528 }
529 return count;
530}
531
533{
534 if (this->items.is_empty()) {
535 return nullptr;
536 }
537 const nodes::ItemDeclaration *item_decl = this->items.first();
538 if (const auto *socket_decl = dynamic_cast<const nodes::SocketDeclaration *>(item_decl)) {
539 if (socket_decl->is_panel_toggle && (socket_decl->in_out & SOCK_IN) &&
540 (socket_decl->socket_type & SOCK_BOOLEAN))
541 {
542 return socket_decl;
543 }
544 }
545 return nullptr;
546}
547
555
565
567{
568 BLI_assert(this->is_output());
569 /* The #ReferencePropagation relations are build after all sockets are known. */
571 return *this;
572}
573
575 const Span<int> input_indices)
576{
577 BLI_assert(this->is_output());
578 rl::RelationsInNode &relations = node_decl_builder_->get_reference_lifetime_relations();
579 for (const int from_input : input_indices) {
580 rl::ReferencePropagation relation;
581 relation.from_input = from_input;
582 relation.to_output = decl_base_->index;
583 relations.reference_propagations.append(relation);
584 }
585 return *this;
586}
587
589{
590 BLI_assert(this->is_output());
591 /* The #ReferencePropagation and #DataPropagation relations are build after all sockets are
592 * known. */
595 return *this;
596}
597
599{
600 /* The relations are build after all sockets are known. */
602 return *this;
603}
604
606 const Span<int> input_indices)
607{
608 rl::RelationsInNode &relations = node_decl_builder_->get_reference_lifetime_relations();
609 for (const int from_input : input_indices) {
610 {
611 rl::DataPropagation relation;
612 relation.from_input = from_input;
613 relation.to_output = decl_base_->index;
614 relations.data_propagations.append(relation);
615 }
616 {
617 rl::ReferencePropagation relation;
618 relation.from_input = from_input;
619 relation.to_output = decl_base_->index;
620 relations.reference_propagations.append(relation);
621 }
622 }
623 return *this;
624}
625
631
637
644
646{
647 decl_base_->compact = value;
648 return *this;
649}
650
652{
653 decl_base_->label_fn = std::make_unique<CustomSocketLabelFn>(std::move(fn));
654 return *this;
655}
656
662
664{
665 decl_base_->description = std::move(value);
666 return *this;
667}
668
670 std::optional<std::string> value)
671{
672 decl_base_->translation_context = std::move(value);
673 return *this;
674}
675
681
687
693
699
709
711{
712 BLI_assert(this->is_input());
713 /* The corresponding relations are build after all socket declarations are known. */
715 decl_base_->structure_type = StructureType::Field;
716 return *this;
717}
718
720 const Span<int> geometry_input_indices)
721{
722 BLI_assert(this->is_input());
723 rl::RelationsInNode &relations = node_decl_builder_->get_reference_lifetime_relations();
724 for (const int index : geometry_input_indices) {
725 rl::UseRelation relation;
726 relation.data_input = index;
727 relation.reference_input = decl_base_->index;
728 relations.use_relations.append(relation);
729 }
730 decl_base_->structure_type = StructureType::Field;
731 return *this;
732}
733
742
744 const Span<int> geometry_output_indices)
745{
746 BLI_assert(this->is_output());
748 this->references_other_outputs(geometry_output_indices);
749 decl_base_->structure_type = StructureType::Field;
750 return *this;
751}
752
754{
755 BLI_assert(this->is_output());
756 /* The corresponding relations are build after all socket declarations are known. */
758 return *this;
759}
760
762 const Span<int> output_indices)
763{
764 BLI_assert(this->is_output());
765 rl::RelationsInNode &relations = node_decl_builder_->get_reference_lifetime_relations();
766 for (const int index : output_indices) {
767 rl::AvailableRelation relation;
768 relation.data_output = index;
769 relation.reference_output = decl_base_->index;
770 relations.available_relations.append(relation);
771 }
772 return *this;
773}
774
777{
778 decl_base_->compositor_realization_mode_ = value;
779 return *this;
780}
781
783 int priority)
784{
785 BLI_assert(priority >= 0);
786 decl_base_->compositor_domain_priority_ = priority;
787 return *this;
788}
789
791 std::function<void(bNode &)> fn)
792{
793 decl_base_->make_available_fn_ = std::move(fn);
794 return *this;
795}
796
798{
799 decl_base_->custom_draw_fn = std::make_unique<CustomSocketDrawFn>(std::move(fn));
800 return *this;
801}
802
805{
806 decl_base_->usage_inference_fn = std::make_unique<SocketUsageInferenceFn>(std::move(fn));
807 return *this;
808}
809
811{
812#ifndef NDEBUG
813 int menu_input_count = 0;
814 /* Topology cache may not be available here and this function may be called while doing tree
815 * modifications. */
816 for (bNodeSocket &socket : node.inputs) {
817 if (socket.type == SOCK_MENU) {
818 menu_input_count++;
819 }
820 }
821 BLI_assert(menu_input_count == 1);
822#endif
823
824 for (bNodeSocket &socket : node.inputs) {
825 if (!socket.is_available()) {
826 continue;
827 }
828 if (socket.type != SOCK_MENU) {
829 continue;
830 }
831 return socket;
832 }
834 return node.input_socket(0);
835}
836
838 const int menu_value)
839{
840 this->make_available([menu_value](bNode &node) {
841 bNodeSocket &socket = const_cast<bNodeSocket &>(find_single_menu_input(node));
842 bNodeSocketValueMenu *value = socket.default_value_typed<bNodeSocketValueMenu>();
843 value->value = menu_value;
844 });
845 this->usage_inference([menu_value](const socket_usage_inference::SocketUsageParams &params)
846 -> std::optional<bool> {
847 const bNodeSocket &socket = find_single_menu_input(params.node);
848 if (params.socket.is_input()) {
849 if (const std::optional<bool> any_output_used = params.any_output_is_used()) {
850 if (!*any_output_used) {
851 /* If no output is used, none of the inputs is used either. */
852 return false;
853 }
854 }
855 else {
856 /* It's not known if any output is used yet. This function will be called again once new
857 * information about output usages is available. */
858 return std::nullopt;
859 }
860 }
861 return params.menu_input_may_be(socket.identifier_ustr(), menu_value);
862 });
863 return *this;
864}
865
867 const UString menu_input_identifier, const int menu_value)
868{
869 Array<int> menu_values = {menu_value};
870 this->usage_by_menu(menu_input_identifier, menu_values);
871 return *this;
872}
873
875 const UString menu_input_identifier, const Array<int> menu_values)
876{
877 this->make_available([menu_input_identifier, menu_values](bNode &node) {
878 bNodeSocket &menu_socket = *bke::node_find_socket(node, SOCK_IN, menu_input_identifier);
879 const SocketDeclaration &socket_declaration = *menu_socket.runtime->declaration;
880 socket_declaration.make_available(node);
881 bNodeSocketValueMenu *value = menu_socket.default_value_typed<bNodeSocketValueMenu>();
882 value->value = menu_values[0];
883 });
884 this->usage_inference(
885 [menu_input_identifier, menu_values](
886 const socket_usage_inference::SocketUsageParams &params) -> std::optional<bool> {
887 if (params.socket.is_input()) {
888 if (const std::optional<bool> any_output_used = params.any_output_is_used()) {
889 if (!*any_output_used) {
890 /* If no output is used, none of the inputs is used either. */
891 return false;
892 }
893 }
894 else {
895 /* It's not known if any output is used yet. This function will be called again once
896 * new information about output usages is available. */
897 return std::nullopt;
898 }
899 }
900
901 /* Check if the menu might be any of the given values. */
902 bool menu_might_be_any_value = false;
903 for (const int menu_value : menu_values) {
904 menu_might_be_any_value = params.menu_input_may_be(menu_input_identifier, menu_value);
905 if (menu_might_be_any_value) {
906 break;
907 }
908 }
909
910 const bNodeSocket &menu_socket = *bke::node_find_socket(
911 params.node, SOCK_IN, UString(menu_input_identifier.ref()));
912 const SocketDeclaration &menu_socket_declaration = *menu_socket.runtime->declaration;
913 if (!menu_socket_declaration.usage_inference_fn) {
914 return menu_might_be_any_value;
915 }
916
917 /* If the menu socket has a usage inference function, check if it might be used. */
918 const std::optional<bool> menu_might_be_used =
919 (*menu_socket_declaration.usage_inference_fn)(params);
920 if (!menu_might_be_used.has_value()) {
921 return menu_might_be_any_value;
922 }
923
924 /* The input is only used if the menu might be any of the values and the menu itself is
925 * used. */
926 return *menu_might_be_used && menu_might_be_any_value;
927 });
928 return *this;
929}
930
932 const UString bool_input_identifier, const bool value)
933{
934 this->make_available([bool_input_identifier, value](bNode &node) {
935 bNodeSocket &bool_socket = *bke::node_find_socket(node, SOCK_IN, bool_input_identifier);
936 const SocketDeclaration &socket_declaration = *bool_socket.runtime->declaration;
937 socket_declaration.make_available(node);
938 bool_socket.default_value_typed<bNodeSocketValueBoolean>()->value = value;
939 });
940 this->usage_inference(
941 [bool_input_identifier,
942 value](const socket_usage_inference::SocketUsageParams &params) -> std::optional<bool> {
943 if (params.socket.is_input()) {
944 if (std::optional<bool> any_output_used = params.any_output_is_used()) {
945 if (!*any_output_used) {
946 /* If no output is used, none of the inputs is used either. */
947 return false;
948 }
949 }
950 else {
951 /* It's not known if any output is used yet. This function will be called again once
952 * new information about output usages is available. */
953 return std::nullopt;
954 }
955 }
956 const bool might_be_value = params.bool_input_may_be(bool_input_identifier, value);
957 const bNodeSocket &bool_socket = *bke::node_find_socket(
958 params.node, SOCK_IN, bool_input_identifier);
959 const SocketDeclaration &bool_socket_declaration = *bool_socket.runtime->declaration;
960 if (!bool_socket_declaration.usage_inference_fn) {
961 return might_be_value;
962 }
963 const std::optional<bool> bool_might_be_used =
964 (*bool_socket_declaration.usage_inference_fn)(params);
965 if (!bool_might_be_used.has_value()) {
966 return might_be_value;
967 }
968 return *bool_might_be_used && might_be_value;
969 });
970 return *this;
971}
972
974{
975 if (const SocketDeclaration *panel_toggle_decl = decl_base_->parent->panel_input_decl()) {
976 this->usage_by_bool(panel_toggle_decl->identifier, true);
977 }
978 return *this;
979}
980
986
995
996BaseSocketDeclarationBuilder &BaseSocketDeclarationBuilder ::socket_name_ptr(
997 const PointerRNA ptr, const StringRef property_name)
998{
999 decl_base_->socket_name_rna = std::make_unique<SocketNameRNA>();
1000 decl_base_->socket_name_rna->owner = ptr;
1001 decl_base_->socket_name_rna->property_name = property_name;
1002 return *this;
1003}
1004
1006 const ID *id, const StructRNA *srna, const void *data, StringRef property_name)
1007{
1008 /* Doing const-casts here because this data is generally only available as const when creating
1009 * the declaration, but it's still valid to modify later. */
1010 return this->socket_name_ptr(RNA_pointer_create_discrete(const_cast<ID *>(id),
1011 const_cast<StructRNA *>(srna),
1012 const_cast<void *>(data)),
1013 property_name);
1014}
1015
1017{
1018 decl_base_->is_panel_toggle = value;
1019 return *this;
1020}
1021
1023{
1024 decl_base_->is_layer_name = value;
1025 return *this;
1026}
1027
1033
1035 const SocketDeclaration & /*other_decl*/)
1036{
1037 /* Can't copy any value by default. */
1038 return *this;
1039}
1040
1042{
1043 return compositor_realization_mode_;
1044}
1045
1047{
1048 return compositor_domain_priority_;
1049}
1050
1052{
1053 if (make_available_fn_) {
1054 make_available_fn_(node);
1055 }
1056}
1057
1059{
1060 decl_->description = std::move(value);
1061 return *this;
1062}
1063
1065 std::optional<std::string> value)
1066{
1067 decl_->translation_context = value;
1068 return *this;
1069}
1070
1072{
1073 decl_->default_collapsed = closed;
1074 return *this;
1075}
1076
1078
1079static void position(const bNode & /*node*/, void *r_value)
1080{
1083}
1084
1085static void normal(const bNode & /*node*/, void *r_value)
1086{
1088}
1089
1090static void index(const bNode & /*node*/, void *r_value)
1091{
1093}
1094
1095static void id_or_index(const bNode & /*node*/, void *r_value)
1096{
1098}
1099
1105
1106static void handle_left(const bNode & /*node*/, void *r_value)
1107{
1110}
1111
1112static void handle_right(const bNode & /*node*/, void *r_value)
1113{
1116}
1117
1118} // namespace implicit_field_inputs
1119
1120std::optional<ImplicitInputValueFn> get_implicit_input_value_fn(const NodeDefaultInputType type)
1121{
1122 switch (type) {
1124 return std::nullopt;
1126 return std::make_optional(implicit_field_inputs::index);
1128 return std::make_optional(implicit_field_inputs::id_or_index);
1130 return std::make_optional(implicit_field_inputs::normal);
1132 return std::make_optional(implicit_field_inputs::position);
1134 return std::make_optional(implicit_field_inputs::instance_transform);
1136 return std::make_optional(implicit_field_inputs::handle_left);
1138 return std::make_optional(implicit_field_inputs::handle_right);
1140 return std::nullopt;
1142 return std::nullopt;
1144 return std::nullopt;
1145 }
1146 return std::nullopt;
1147}
1148
1150 const NodeDefaultInputType input_type)
1151{
1152 const eNodeSocketDatatype stype = socket_type.type;
1153 switch (input_type) {
1155 return true;
1157 return ELEM(stype, SOCK_FLOAT, SOCK_INT);
1160 return stype == SOCK_INT;
1165 return stype == SOCK_VECTOR;
1167 return stype == SOCK_MATRIX;
1169 return stype == SOCK_VECTOR;
1171 return stype == SOCK_OBJECT;
1172 }
1173 return false;
1174}
1175
1177 NodeDefaultInputType input_type)
1178{
1179 switch (input_type) {
1181 return true;
1190 return node_tree_type == NTREE_GEOMETRY;
1192 return ELEM(node_tree_type, NTREE_COMPOSIT, NTREE_GEOMETRY);
1194 return node_tree_type == NTREE_COMPOSIT;
1195 }
1196 return false;
1197}
1198
1210
1212 const std::optional<StringRefNull> label_override)
1213{
1214 this->socket.typeinfo->draw(const_cast<bContext *>(&this->C),
1215 &layout,
1216 &this->socket_ptr,
1217 &this->node_ptr,
1218 label_override.has_value() ? *label_override : this->label);
1219}
1220
1221} // namespace blender::nodes
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define UNUSED_VARS_NDEBUG(...)
#define SET_FLAG_FROM_TEST(value, test, flag)
#define ELEM(...)
StringRefNull ref() const
int64_t size() const
void append(const T &value)
static const fn::Field< T > & get_field()
static const fn::Field< int > & get_field()
static const fn::Field< float3 > & get_field()
static SocketValueVariant & construct_in(void *ptr, T &&value)
const nodes::SocketDeclaration * declaration
static const Field< int > & get_field()
Definition field.cc:557
BaseSocketDeclarationBuilder & is_layer_name(bool value=true)
BaseSocketDeclarationBuilder & align_with_previous(bool value=true)
BaseSocketDeclarationBuilder & make_available(std::function< void(bNode &)> fn)
BaseSocketDeclarationBuilder & evaluated_geometry_field()
BaseSocketDeclarationBuilder & label_fn(CustomSocketLabelFn fn)
BaseSocketDeclarationBuilder & references_other_outputs()
BaseSocketDeclarationBuilder & optional_label(bool value=true)
BaseSocketDeclarationBuilder & usage_by_single_menu(const int menu_value)
BaseSocketDeclarationBuilder & compositor_realization_mode(CompositorInputRealizationMode value)
BaseSocketDeclarationBuilder & propagate_all()
BaseSocketDeclarationBuilder & available(bool value=true)
BaseSocketDeclarationBuilder & short_label(UString value)
BaseSocketDeclarationBuilder & inferred_structure_type()
BaseSocketDeclarationBuilder & is_attribute_name(bool value=true)
BaseSocketDeclarationBuilder & propagate_all_geometry()
BaseSocketDeclarationBuilder & usage_by_menu(const UString menu_input_identifier, const int menu_value)
BaseSocketDeclarationBuilder & compact(bool value=true)
BaseSocketDeclarationBuilder & structure_type(StructureType structure_type)
BaseSocketDeclarationBuilder & anonymous_attribute_output()
BaseSocketDeclarationBuilder & usage_inference(SocketUsageInferenceFn fn)
BaseSocketDeclarationBuilder & description(std::string value="")
BaseSocketDeclarationBuilder & socket_name_ptr(PointerRNA ptr, StringRef property_name)
BaseSocketDeclarationBuilder & translation_context(std::optional< std::string > value=std::nullopt)
BaseSocketDeclarationBuilder & multi_input(bool value=true)
BaseSocketDeclarationBuilder & usage_by_bool(UString bool_input_identifier, bool value)
BaseSocketDeclarationBuilder & usage_by_panel_toggle()
BaseSocketDeclarationBuilder & custom_draw(CustomSocketDrawFn fn)
BaseSocketDeclarationBuilder & panel_toggle(bool value=true)
BaseSocketDeclarationBuilder & is_default_link_socket(bool value=true)
BaseSocketDeclarationBuilder & compositor_domain_priority(int priority)
BaseSocketDeclarationBuilder & is_volume_grid_name(bool value=true)
BaseSocketDeclarationBuilder & propagate_references()
virtual BaseSocketDeclarationBuilder & try_copy_ui_data(const SocketDeclaration &other_decl)
BaseSocketDeclarationBuilder & hide_value(bool value=true)
BaseSocketDeclarationBuilder & no_muted_links(bool value=true)
BaseSocketDeclarationBuilder & default_input_type(NodeDefaultInputType value)
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
DeclType::Builder & add_output(UString name, UString identifier=""_ustr)
PanelDeclarationBuilder & add_panel(UString name, int identifier=-1)
void add_layout(std::function< void(ui::Layout &, bContext *, PointerRNA *)> draw)
NodeDeclarationBuilder(const bke::bNodeType &typeinfo, NodeDeclaration &declaration, const bNodeTree *ntree=nullptr, const bNode *node=nullptr)
rl::RelationsInNode & get_reference_lifetime_relations()
Vector< ItemDeclaration * > root_items
Vector< SocketDeclaration * > inputs
bool matches(const bNode &node) const
Vector< SocketDeclaration * > outputs
Vector< PanelDeclaration * > panels
Vector< ItemDeclarationPtr > all_items
Span< SocketDeclaration * > sockets(eNodeSocketInOut in_out) const
Self & description(std::string value="")
Self & translation_context(std::optional< std::string > value=std::nullopt)
PanelDeclarationBuilder(NodeDeclarationBuilder &node_builder, PanelDeclaration &decl)
std::optional< std::string > translation_context
Vector< ItemDeclaration * > items
void update_or_build(const bNodePanelState &old_panel, bNodePanelState &new_panel) const
bool matches(const bNodePanelState &panel) const
const SocketDeclaration * panel_input_decl() const
void build(bNodePanelState &panel) const
std::unique_ptr< CustomSocketDrawFn > custom_draw_fn
OutputStructureTypeDependency structure_type_output_dependency
bool matches_common_data(const bNodeSocket &socket) const
std::unique_ptr< SocketUsageInferenceFn > usage_inference_fn
const CompositorInputRealizationMode & compositor_realization_mode() const
void make_available(bNode &node) const
std::unique_ptr< CustomSocketLabelFn > label_fn
std::optional< std::string > translation_context
virtual bNodeSocket & update_or_build(bNodeTree &ntree, bNode &node, bNodeSocket &socket) const
void set_common_flags(bNodeSocket &socket) const
virtual bNodeSocket & build(bNodeTree &ntree, bNode &node) const =0
bool bool_input_may_be(UString identifier, bool bool_value) const
bool menu_input_may_be(UString identifier, int enum_value) const
bool can_contain_referenced_data(eNodeSocketDatatype socket_type)
std::optional< eNodeSocketDatatype > custom_data_type_to_socket_type(eCustomDataType type)
Definition node.cc:5935
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
static void position(const bNode &, void *r_value)
static void id_or_index(const bNode &, void *r_value)
static void handle_left(const bNode &, void *r_value)
static void index(const bNode &, void *r_value)
static void instance_transform(const bNode &, void *r_value)
static void handle_right(const bNode &, void *r_value)
static void normal(const bNode &, void *r_value)
std::ostream & operator<<(std::ostream &stream, const RelationsInNode &relations)
std::optional< ImplicitInputValueFn > get_implicit_input_value_fn(const NodeDefaultInputType type)
static void assert_valid_panels_recursive(const NodeDeclaration &node_decl, const Span< const ItemDeclaration * > items, Vector< const SocketDeclaration * > &r_flat_inputs, Vector< const SocketDeclaration * > &r_flat_outputs)
std::function< StringRefNull(const bNode &node)> CustomSocketLabelFn
static const bNodeSocket & find_single_menu_input(const bNode &node)
void build_node_declaration(const bke::bNodeType &typeinfo, NodeDeclaration &r_declaration, const bNodeTree *ntree, const bNode *node)
static bool socket_type_to_static_decl_type(const eNodeSocketDatatype socket_type, Fn &&fn)
std::unique_ptr< ItemDeclaration > ItemDeclarationPtr
bool socket_type_supports_default_input_type(const bke::bNodeSocketType &socket_type, const NodeDefaultInputType input_type)
std::function< void(CustomSocketDrawParams &params)> CustomSocketDrawFn
static void reset_declaration(NodeDeclaration &declaration)
std::unique_ptr< SocketDeclaration > make_declaration_for_socket_type(const eNodeSocketDatatype socket_type)
bool default_input_type_is_field(const NodeDefaultInputType input_type)
bool node_tree_type_supports_default_input_type(eNodeTree_Type node_tree_type, NodeDefaultInputType input_type)
std::function< std::optional< bool >(const socket_usage_inference::SocketUsageParams &params)> SocketUsageInferenceFn
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BMesh const char void * data
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
@ NODE_PANEL_COLLAPSED
bke::bNodeSocketType * typeinfo
eNodeSocketDatatype type
bke::bNodeSocketRuntime * runtime
eNodeSocketInOut in_out
struct bNodeSocket * next
Defines a socket type.
Definition BKE_node.hh:163
eNodeSocketDatatype type
Definition BKE_node.hh:198
void(*) draw(bContext *C, ui::Layout *layout, PointerRNA *ptr, PointerRNA *node_ptr, StringRef text)
Definition BKE_node.hh:171
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
NodeDeclareFunction declare
Definition BKE_node.hh:393
void draw_standard(ui::Layout &layout, std::optional< StringRefNull > label_override=std::nullopt)