Blender V5.3
node_fn_string_to_value.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 "BLI_string_utf8.hh"
6
7#include "fast_float.h"
8
10#include "node_shader_util.hh"
11
12#include "NOD_rna_define.hh"
14
16#include "UI_resources.hh"
17
18#include <charconv>
19
21
23{
24 b.is_function_node();
25 b.add_input<decl::String>("String"_ustr).optional_label();
26
27 const bNode *node = b.node_or_null();
28 if (node != nullptr) {
29 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
30 b.add_input<decl::Int>("Base"_ustr)
31 .min(2)
32 .max(36)
33 .default_value(10)
34 .description("Numeric base for the input string (e.g. 2 for binary, 16 for hexadecimal)")
35 .available(data_type == SOCK_INT);
36 b.add_output(data_type, "Value"_ustr);
37 }
38
39 b.add_output<decl::Int>("Length"_ustr);
40}
41
42static const mf::MultiFunction *get_multi_function(const bNode &bnode)
43{
44 static auto str_to_float_fn = mf::build::SI1_SO2<std::string, float, int>(
45 "String to Value", [](const std::string &s, float &value, int &length) -> void {
46 const auto result = fast_float::from_chars(s.data(), s.data() + s.size(), value);
47 if (result.ec != std::errc()) {
48 value = 0.0f;
49 length = 0;
50 return;
51 }
52 length = BLI_strnlen_utf8(s.data(), result.ptr - s.data());
53 });
54
55 static auto str_to_int_fn = mf::build::SI2_SO2<std::string, int, int, int>(
56 "String to Value", [](const std::string &s, int base, int &value, int &length) -> void {
57 if (base < 2 || base > 36) {
58 value = 0;
59 length = 0;
60 return;
61 }
62 const auto result = std::from_chars(s.data(), s.data() + s.size(), value, base);
63 if (result.ec != std::errc()) {
64 value = 0;
65 length = 0;
66 return;
67 }
68 length = BLI_strnlen_utf8(s.data(), result.ptr - s.data());
69 });
70
71 switch (eNodeSocketDatatype(bnode.custom1)) {
72 case SOCK_FLOAT:
73 return &str_to_float_fn;
74 case SOCK_INT:
75 return &str_to_int_fn;
76 default:
78 return nullptr;
79 }
80}
81
83{
84 const mf::MultiFunction *fn = get_multi_function(builder.node());
85 builder.set_matching_fn(fn);
86}
87
88static void node_init(bNodeTree *, bNode *node)
89{
90 node->custom1 = SOCK_FLOAT;
91}
92
94{
95 const eNodeSocketDatatype socket_type = params.other_socket().type;
96 if (params.in_out() == SOCK_IN) {
97 if (socket_type == SOCK_STRING) {
98 params.add_item(IFACE_("String"), [](LinkSearchOpParams &params) {
99 bNode &node = params.add_node("FunctionNodeStringToValue"_ustr);
100 params.update_and_connect_available_socket(node, "String"_ustr);
101 });
102 }
103 }
104 else if (params.in_out() == SOCK_OUT) {
105 if (ELEM(socket_type, SOCK_INT, SOCK_BOOLEAN)) {
106 params.add_item(IFACE_("Value"), [](LinkSearchOpParams &params) {
107 bNode &node = params.add_node("FunctionNodeStringToValue"_ustr);
108 node.custom1 = SOCK_INT;
109 params.update_and_connect_available_socket(node, "Value"_ustr);
110 });
111 }
112 else if (params.node_tree().typeinfo->validate_link(SOCK_FLOAT, socket_type)) {
113 params.add_item(IFACE_("Value"), [](LinkSearchOpParams &params) {
114 bNode &node = params.add_node("FunctionNodeStringToValue"_ustr);
115 node.custom1 = SOCK_FLOAT;
116 params.update_and_connect_available_socket(node, "Value"_ustr);
117 });
118 }
119
120 if (socket_type == SOCK_INT) {
121 params.add_item(IFACE_("Length"), [](LinkSearchOpParams &params) {
122 bNode &node = params.add_node("FunctionNodeStringToValue"_ustr);
123 params.update_and_connect_available_socket(node, "Length"_ustr);
124 });
125 }
126 }
127}
128
129static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
130{
131 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
132}
133
134static void node_rna(StructRNA *srna)
135{
136 static const EnumPropertyItem data_types[] = {
137 {SOCK_FLOAT, "FLOAT", ICON_NODE_SOCKET_FLOAT, N_("Float"), N_("Floating-point value")},
138 {SOCK_INT, "INT", ICON_NODE_SOCKET_INT, N_("Integer"), N_("32-bit integer")},
139 {0, nullptr, 0, nullptr, nullptr}};
140
142 "data_type",
143 "Data Type",
144 "",
145 data_types,
147 SOCK_FLOAT);
148}
149
150static void node_register()
151{
152 static bke::bNodeType ntype;
153
154 common_node_type_base(&ntype, "FunctionNodeStringToValue"_ustr);
155 ntype.ui_name = "String to Value";
156 ntype.ui_description = "Derive a numeric value from a given string representation";
158 ntype.declare = node_declare;
159 ntype.initfunc = node_init;
164
165 node_rna(ntype.rna_ext.srna);
166}
168
169} // namespace blender::nodes::node_fn_string_to_value_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
#define ELEM(...)
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void set_matching_fn(const mf::MultiFunction *fn)
float length(VecOp< float, D >) RET
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_init(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static const mf::MultiFunction * get_multi_function(const bNode &bnode)
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)
size_t size_t BLI_strnlen_utf8(const char *strc, size_t strc_maxlen) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:38
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
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
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
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
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)