Blender V5.3
NOD_node_declaration.hh
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
9#pragma once
10
11#include <cstdint>
12#include <functional>
13#include <type_traits>
14#include <variant>
15
16#include "BLI_array.hh"
17#include "BLI_map.hh"
18#include "BLI_string_ref.hh"
19#include "BLI_ustring.hh"
20#include "BLI_utildefines.hh"
21#include "BLI_vector.hh"
22
23#include "BLT_translation.hh" /* IWYU pragma: export */
24
25#include "DNA_node_types.h"
26
27#include "RNA_types.hh"
28
30
31namespace blender {
32
33struct bContext;
34struct bNode;
35
36namespace ui {
37struct Layout;
38} // namespace ui
39
40namespace nodes {
41
44
49 None,
50 Transforms,
51 OperationDomain,
52};
53
68
69namespace reference_lifetimes {
70
77
78 friend bool operator==(const DataPropagation &a, const DataPropagation &b) = default;
79};
80
87
88 friend bool operator==(const ReferencePropagation &a, const ReferencePropagation &b) = default;
89};
90
97
98 friend bool operator==(const UseRelation &a, const UseRelation &b) = default;
99};
100
107
108 friend bool operator==(const AvailableRelation &a, const AvailableRelation &b) = default;
109};
110
120
121std::ostream &operator<<(std::ostream &stream, const RelationsInNode &relations);
122
123} // namespace reference_lifetimes
124
125/* Common alias for this namespace. */
126namespace rl = reference_lifetimes;
127
130 public:
131 const PanelDeclaration *parent = nullptr;
132
133 virtual ~ItemDeclaration() = default;
134};
135
136using ItemDeclarationPtr = std::unique_ptr<ItemDeclaration>;
137
140 std::string property_name;
141};
142
157
158using CustomSocketDrawFn = std::function<void(CustomSocketDrawParams &params)>;
159using CustomSocketLabelFn = std::function<StringRefNull(const bNode &node)>;
161 std::function<std::optional<bool>(const socket_usage_inference::SocketUsageParams &params)>;
162
164 struct None {};
165 struct All {};
169 std::variant<None, All, Partial> variant = None();
170};
171
176 public:
180 std::string description;
181 std::optional<std::string> translation_context;
193 bool optional_label = false;
194 bool hide_value = false;
195 bool compact = false;
196 bool is_multi_input = false;
197 bool no_mute_links = false;
198 bool is_available = true;
199 bool is_attribute_name = false;
204 bool is_panel_toggle = false;
205 bool is_layer_name = false;
208
210 int index = -1;
211
213
214 StructureType structure_type = StructureType::Single;
215
216 private:
217 CompositorInputRealizationMode compositor_realization_mode_ =
219
225 int compositor_domain_priority_ = -1;
226
228 std::function<void(bNode &)> make_available_fn_;
229
230 public:
237 std::unique_ptr<SocketNameRNA> socket_name_rna;
241 std::unique_ptr<CustomSocketDrawFn> custom_draw_fn;
245 std::unique_ptr<CustomSocketLabelFn> label_fn;
250 std::unique_ptr<SocketUsageInferenceFn> usage_inference_fn;
251
255 template<typename SocketDecl> friend class SocketDeclarationBuilder;
256
257 ~SocketDeclaration() override = default;
258
259 virtual bNodeSocket &build(bNodeTree &ntree, bNode &node) const = 0;
260 virtual bool matches(const bNodeSocket &socket) const = 0;
261 virtual bNodeSocket &update_or_build(bNodeTree &ntree, bNode &node, bNodeSocket &socket) const;
262
267 virtual bool can_connect(const bNodeSocket &socket) const = 0;
268
274 void make_available(bNode &node) const;
275
277 int compositor_domain_priority() const;
278
279 protected:
280 void set_common_flags(bNodeSocket &socket) const;
281 bool matches_common_data(const bNodeSocket &socket) const;
282};
283
287
289 protected:
292
293 /* Variables which are used to add additional data to the declaration after all sockets have been
294 * added. */
300
304
305 public:
306 virtual ~BaseSocketDeclarationBuilder() = default;
307
309
310 BaseSocketDeclarationBuilder &hide_value(bool value = true);
311
312 BaseSocketDeclarationBuilder &multi_input(bool value = true);
313
314 BaseSocketDeclarationBuilder &compact(bool value = true);
315
317
318 BaseSocketDeclarationBuilder &description(std::string value = "");
319
321 std::optional<std::string> value = std::nullopt);
322
324
329 BaseSocketDeclarationBuilder &available(bool value = true);
330
332
334
336
349
356
360
364
373
375
381
388 BaseSocketDeclarationBuilder &make_available(std::function<void(bNode &)> fn);
389
394
400
405
411
416 BaseSocketDeclarationBuilder &usage_by_menu(const UString menu_input_identifier,
417 const int menu_value);
418
423 BaseSocketDeclarationBuilder &usage_by_menu(const UString menu_input_identifier,
424 const Array<int> menu_values);
425
430 BaseSocketDeclarationBuilder &usage_by_bool(UString bool_input_identifier, bool value);
431
436
442
449 const StructRNA *srna,
450 const void *data,
451 StringRef property_name);
455 BaseSocketDeclarationBuilder &panel_toggle(bool value = true);
456
458
461
463 int index() const;
464
465 bool is_input() const;
466 bool is_output() const;
467
469};
470
476template<typename SocketDecl>
478 protected:
479 using Self = typename SocketDecl::Builder;
480 static_assert(std::is_base_of_v<SocketDeclaration, SocketDecl>);
481 SocketDecl *decl_;
482
486};
487
488using SocketDeclarationPtr = std::unique_ptr<SocketDeclaration>;
489
491
493
495 public:
496 std::function<DrawNodeLayoutFn> draw;
501 bool is_default = false;
502};
503
508 public:
511 std::string description;
512 std::optional<std::string> translation_context;
513 bool default_collapsed = false;
516 int index = -1;
518
519 private:
520 friend NodeDeclarationBuilder;
522
523 public:
524 ~PanelDeclaration() override = default;
525
526 void build(bNodePanelState &panel) const;
527 bool matches(const bNodePanelState &panel) const;
528 void update_or_build(const bNodePanelState &old_panel, bNodePanelState &new_panel) const;
529
530 int depth() const;
531
533 const SocketDeclaration *panel_input_decl() const;
534};
535
541 public:
545
551
552 template<typename DeclType>
553 typename DeclType::Builder &add_socket(UString name,
554 UString identifier,
555 eNodeSocketInOut in_out);
556
557 template<typename DeclType>
558 typename DeclType::Builder &add_input(UString name, UString identifier = ""_ustr);
559 template<typename DeclType>
560 typename DeclType::Builder &add_output(UString name, UString identifier = ""_ustr);
561
563 UString name,
564 UString identifier = ""_ustr);
566 UString name,
567 UString identifier = ""_ustr);
569 UString name,
570 UString identifier = ""_ustr);
572 UString name,
573 UString identifier = ""_ustr);
574
575 PanelDeclarationBuilder &add_panel(UString name, int identifier = -1);
576
577 void add_separator();
578 void add_default_layout();
579 void add_layout(std::function<void(ui::Layout &, bContext *, PointerRNA *)> draw);
580};
581
583 protected:
586
588
589 public:
591 : DeclarationListBuilder(node_builder, decl.items), decl_(&decl)
592 {
593 this->parent_panel_decl = &decl;
594 }
595
596 Self &description(std::string value = "");
597 Self &translation_context(std::optional<std::string> value = std::nullopt);
598 Self &default_closed(bool closed);
599};
600
601using PanelDeclarationPtr = std::unique_ptr<PanelDeclaration>;
602
604 public:
613 std::unique_ptr<rl::RelationsInNode> reference_lifetime_relations_;
614
621
627
633
638
640
642 void assert_valid() const;
643
644 bool matches(const bNode &node) const;
646
647 const rl::RelationsInNode *reference_lifetime_relations() const
648 {
650 }
651
652 MEM_CXX_CLASS_ALLOC_FUNCS("NodeDeclaration")
653};
654
655class NodeDeclarationBuilder : public DeclarationListBuilder {
656 private:
657 const bke::bNodeType &typeinfo_;
658 NodeDeclaration &declaration_;
659 const bNodeTree *ntree_ = nullptr;
660 const bNode *node_ = nullptr;
662 Vector<BaseSocketDeclarationBuilder *> input_socket_builders_;
663 Vector<BaseSocketDeclarationBuilder *> output_socket_builders_;
665 bool is_function_node_ = false;
666
667 friend DeclarationListBuilder;
668
669 public:
672 const bNodeTree *ntree = nullptr,
673 const bNode *node = nullptr);
674
675 const bNode *node_or_null() const
676 {
677 declaration_.is_context_dependent = true;
678 return node_;
679 }
680
681 const bNodeTree *tree_or_null() const
682 {
683 declaration_.is_context_dependent = true;
684 return ntree_;
685 }
686
692 {
693 is_function_node_ = true;
694 }
695
696 void finalize();
697
698 void use_custom_socket_order(bool enable = true);
699 void allow_any_socket_order(bool enable = true);
700
701 rl::RelationsInNode &get_reference_lifetime_relations()
702 {
703 if (!declaration_.reference_lifetime_relations_) {
704 declaration_.reference_lifetime_relations_ = std::make_unique<rl::RelationsInNode>();
705 }
706 return *declaration_.reference_lifetime_relations_;
707 }
708
710 {
711 return declaration_;
712 }
713
714 private:
715 void build_remaining_anonymous_attribute_relations();
716};
717
718using ImplicitInputValueFn = std::function<void(const bNode &node, void *r_value)>;
719std::optional<ImplicitInputValueFn> get_implicit_input_value_fn(NodeDefaultInputType type);
721 NodeDefaultInputType input_type);
723 NodeDefaultInputType input_type);
725
726void build_node_declaration(const bke::bNodeType &typeinfo,
727 NodeDeclaration &r_declaration,
728 const bNodeTree *ntree,
729 const bNode *node);
730
731std::unique_ptr<SocketDeclaration> make_declaration_for_socket_type(
732 eNodeSocketDatatype socket_type);
733
734/* -------------------------------------------------------------------- */
737
738template<typename DeclType>
739inline typename DeclType::Builder &DeclarationListBuilder::add_input(UString name,
740 UString identifier)
741{
742 return this->add_socket<DeclType>(name, identifier, SOCK_IN);
743}
744
745template<typename DeclType>
746inline typename DeclType::Builder &DeclarationListBuilder::add_output(UString name,
747 UString identifier)
748{
749 return this->add_socket<DeclType>(name, identifier, SOCK_OUT);
750}
751
752template<typename DeclType>
753inline typename DeclType::Builder &DeclarationListBuilder::add_socket(UString name,
754 UString identifier,
755 eNodeSocketInOut in_out)
756{
757 static_assert(std::is_base_of_v<SocketDeclaration, DeclType>);
758 using SocketBuilder = typename DeclType::Builder;
759
760 BLI_assert(ELEM(in_out, SOCK_IN, SOCK_OUT));
761
762 std::unique_ptr<SocketBuilder> socket_decl_builder_ptr = std::make_unique<SocketBuilder>();
763 SocketBuilder &socket_decl_builder = *socket_decl_builder_ptr;
764 this->node_decl_builder.socket_builders_.append(std::move(socket_decl_builder_ptr));
765
766 std::unique_ptr<DeclType> socket_decl_ptr = std::make_unique<DeclType>();
767 DeclType &socket_decl = *socket_decl_ptr;
768 this->node_decl_builder.declaration_.all_items.append(std::move(socket_decl_ptr));
769 this->items.append(&socket_decl);
770
771 socket_decl.parent = this->parent_panel_decl;
772 socket_decl_builder.node_decl_builder_ = &this->node_decl_builder;
773
774 socket_decl_builder.decl_ = &socket_decl;
775 socket_decl_builder.decl_base_ = &socket_decl;
776 socket_decl.name = name;
777 socket_decl.identifier = identifier.is_empty() ? name : identifier;
778 socket_decl.in_out = in_out;
779 socket_decl.socket_type = DeclType::static_socket_type;
780
781 if (this->node_decl_builder.is_function_node_) {
782 socket_decl.structure_type = StructureType::Dynamic;
783 if (in_out == SOCK_OUT) {
784 socket_decl_builder.propagate_references();
785 socket_decl_builder.inferred_structure_type();
786 }
787 }
788
789 if (in_out == SOCK_IN) {
790 this->node_decl_builder.input_socket_builders_.append(&socket_decl_builder);
791 socket_decl.index = this->node_decl_builder.declaration_.inputs.append_and_get_index(
792 &socket_decl);
793 }
794 else {
795 this->node_decl_builder.output_socket_builders_.append(&socket_decl_builder);
796 socket_decl.index = this->node_decl_builder.declaration_.outputs.append_and_get_index(
797 &socket_decl);
798 }
799 return socket_decl_builder;
800}
801
803
804/* -------------------------------------------------------------------- */
807
809{
810 return decl_base_->index;
811}
812
814{
815 return decl_base_->in_out == SOCK_IN;
816}
817
819{
820 return decl_base_->in_out == SOCK_OUT;
821}
822
824
825} // namespace nodes
826} // namespace blender
#define ELEM(...)
constexpr StringRefNull()
bool is_empty() const
void append(const T &value)
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)
DeclarationListBuilder(NodeDeclarationBuilder &node_decl_builder, Vector< ItemDeclaration * > &items)
DeclType::Builder & add_socket(UString name, UString identifier, eNodeSocketInOut in_out)
virtual ~ItemDeclaration()=default
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
const rl::RelationsInNode * reference_lifetime_relations() const
Vector< SocketDeclaration * > inputs
bool matches(const bNode &node) const
std::unique_ptr< rl::RelationsInNode > reference_lifetime_relations_
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
~PanelDeclaration() override=default
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
virtual bool matches(const bNodeSocket &socket) const =0
std::unique_ptr< SocketNameRNA > socket_name_rna
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
~SocketDeclaration() override=default
virtual bool can_connect(const bNodeSocket &socket) const =0
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
std::ostream & operator<<(std::ostream &stream, const RelationsInNode &relations)
std::unique_ptr< SocketDeclaration > SocketDeclarationPtr
std::optional< ImplicitInputValueFn > get_implicit_input_value_fn(const NodeDefaultInputType type)
std::function< StringRefNull(const bNode &node)> CustomSocketLabelFn
void build_node_declaration(const bke::bNodeType &typeinfo, NodeDeclaration &r_declaration, const bNodeTree *ntree, const bNode *node)
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
void(ui::Layout &, bContext *, PointerRNA *) DrawNodeLayoutFn
std::unique_ptr< SocketDeclaration > make_declaration_for_socket_type(const eNodeSocketDatatype socket_type)
std::unique_ptr< PanelDeclaration > PanelDeclarationPtr
std::function< void(const bNode &node, void *r_value)> ImplicitInputValueFn
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
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BMesh const char void * data
signed char int8_t
Definition BKE_text.h:76
Defines a socket type.
Definition BKE_node.hh:163
Defines a node type.
Definition BKE_node.hh:265
const Map< const bNode *, const bNode * > * menu_switch_source_by_index_switch
void draw_standard(ui::Layout &layout, std::optional< StringRefNull > label_override=std::nullopt)
friend bool operator==(const OutputDependency &a, const OutputDependency &b)=default
friend bool operator==(const StructureTypeInterface &a, const StructureTypeInterface &b)=default
friend bool operator==(const AvailableRelation &a, const AvailableRelation &b)=default
friend bool operator==(const DataPropagation &a, const DataPropagation &b)=default
friend bool operator==(const ReferencePropagation &a, const ReferencePropagation &b)=default
friend bool operator==(const RelationsInNode &a, const RelationsInNode &b)=default
friend bool operator==(const UseRelation &a, const UseRelation &b)=default