Blender V5.3
node_geo_warning.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
7
9#include "UI_resources.hh"
10
11#include "BLI_string_utf8.hh"
12
13#include "NOD_rna_define.hh"
14#include "RNA_access.hh"
15#include "RNA_enum_types.hh"
16
17#include "COM_node_operation.hh"
18
19namespace blender {
20
22
24{
25 b.use_custom_socket_order();
26 b.allow_any_socket_order();
27
28 b.add_default_layout();
29
30 b.add_input<decl::Bool>("Show"_ustr).default_value(true).hide_value();
31 b.add_output<decl::Bool>("Show"_ustr).align_with_previous();
32 b.add_input<decl::String>("Message"_ustr).optional_label();
33}
34
35class LazyFunctionForWarningNode : public LazyFunction {
36 const bNode &node_;
37
38 public:
40 {
41 debug_name_ = "Warning";
43 inputs_.append_as("Show", type, lf::ValueUsage::Used);
44 inputs_.append_as("Message", type);
45 outputs_.append_as("Show", type);
46 }
47
48 void execute_impl(lf::Params &params, const lf::Context &context) const override
49 {
50 const SocketValueVariant show_variant = params.get_input<SocketValueVariant>(0);
51 const bool show = show_variant.copy_as<bool>();
52 if (!show) {
53 params.set_output(0, show_variant);
54 return;
55 }
56 SocketValueVariant *message_variant =
57 params.try_get_input_data_ptr_or_request<SocketValueVariant>(1);
58 if (!message_variant) {
59 /* Wait for the message to be computed. */
60 return;
61 }
62 std::string message = message_variant->extract<std::string>();
63 GeoNodesUserData &user_data = *static_cast<GeoNodesUserData *>(context.user_data);
64 GeoNodesLocalUserData &local_user_data = *static_cast<GeoNodesLocalUserData *>(
65 context.local_user_data);
66 if (eval_log::NodeTreeLogger *tree_logger = local_user_data.try_get_tree_logger(user_data)) {
67 tree_logger->node_warnings.append(
68 *tree_logger->allocator,
69 {node_.identifier, {NodeWarningType(node_.custom1), std::move(message)}});
70 }
71 /* Only set output in the end so that this node is not finished before the warning is set. */
72 params.set_output(0, show_variant);
73 }
74};
75
76using namespace blender::compositor;
77
79 public:
81
82 void execute() override
83 {
84 const Result &input_show = this->get_input("Show");
85 Result &output_show = this->get_result("Show");
86 output_show.share_data(input_show);
87 if (!input_show.get_single_value_default<bool>()) {
88 return;
89 }
90
91 const std::string message = this->get_input("Message").get_single_value_default<std::string>();
92 this->add_warning(NodeWarningType(this->node().custom1), message);
93 }
94};
95
97{
98 return new WarningOperation(context, node);
99}
100
101static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
102{
103 layout.use_property_split_set(true);
104 layout.use_property_decorate_set(false);
105 layout.prop(ptr, "warning_type", UI_ITEM_NONE, "", ICON_NONE);
106}
107
108static void node_rna(StructRNA *srna)
109{
111 "warning_type",
112 "Warning Type",
113 "",
116}
117
118static void node_label(const bNodeTree * /*ntree*/,
119 const bNode *node,
120 char *label,
121 int label_maxncpy)
122{
123 const char *name;
124 bool enum_label = RNA_enum_name(rna_enum_node_warning_type_items, node->custom1, &name);
125 if (!enum_label) {
126 name = N_("Unknown");
127 }
128 BLI_strncpy_utf8(label, IFACE_(name), label_maxncpy);
129}
130
131static void node_register()
132{
133 static bke::bNodeType ntype;
134
135 common_node_type_base(&ntype, "GeometryNodeWarning"_ustr, GEO_NODE_WARNING);
136 ntype.ui_name = "Warning";
137 ntype.ui_description = "Create custom warnings in node groups";
138 ntype.enum_name_legacy = "WARNING";
140 ntype.declare = node_declare;
141 ntype.labelfunc = node_label;
145
146 node_rna(ntype.rna_ext.srna);
147}
148NOD_REGISTER_NODE(node_register)
149
150} // namespace nodes::node_geo_warning_cc
151
152namespace nodes {
153
154std::unique_ptr<LazyFunction> get_warning_node_lazy_function(const bNode &node)
155{
156 using namespace node_geo_warning_cc;
157 BLI_assert(node.type_legacy == GEO_NODE_WARNING);
158 return std::make_unique<LazyFunctionForWarningNode>(node);
159}
160
161} // namespace nodes
162} // namespace blender
#define NODE_CLASS_INTERFACE
Definition BKE_node.hh:490
#define GEO_NODE_WARNING
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
static const CPPType & get()
NodeOperation(Context &context, const bNode &node)
void add_warning(nodes::NodeWarningType type, std::string message)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Result & get_input(StringRef identifier) const
Definition operation.cc:35
void share_data(const Result &source)
Definition result.cc:996
void execute_impl(lf::Params &params, const lf::Context &context) const override
NodeOperation(Context &context, const bNode &node)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_rna(StructRNA *srna)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
std::unique_ptr< LazyFunction > get_warning_node_lazy_function(const 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)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const EnumPropertyItem rna_enum_node_warning_type_items[]
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
bool RNA_enum_name(const EnumPropertyItem *item, const int value, const char **r_name)
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
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
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
eval_log::NodeTreeLogger * try_get_tree_logger(const GeoNodesUserData &user_data) const
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)
#define N_(msgid)