Blender V5.3
node_fn_set_string_case.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_array.hh"
6#include "BLI_string_utf8.hh"
7#include "BLI_vector.hh"
8
9#include "BKE_node_runtime.hh"
10
12
13#include "node_function_util.hh"
14#include "node_shader_util.hh"
15
17
18enum class Case {
21};
22
23static const EnumPropertyItem case_items[] = {
24 {int(Case::Uppercase),
25 "UPPERCASE",
26 0,
27 N_("Uppercase"),
28 N_("Convert all characters to uppercase")},
29 {int(Case::Lowercase),
30 "LOWERCASE",
31 0,
32 N_("Lowercase"),
33 N_("Convert all characters to lowercase")},
34 {},
35};
36
38{
39 b.is_function_node();
40 b.use_custom_socket_order();
41 b.allow_any_socket_order();
42 b.add_input<decl::String>("String"_ustr).optional_label();
43 b.add_output<decl::String>("String"_ustr).align_with_previous();
44 b.add_input<decl::Menu>("Case"_ustr).static_items(case_items).optional_label();
45}
46
47static std::string apply_string_case(const std::string &s, const Case mode)
48{
49 if (s.empty()) {
50 return s;
51 }
52
53 size_t len_bytes;
54 const size_t len_chars = BLI_strlen_utf8_ex(s.c_str(), &len_bytes);
55
56 Array<char32_t, 64> utf32(len_chars + 1);
57 BLI_str_utf8_as_utf32(utf32.data(), s.c_str(), utf32.size());
58
59 for (size_t i = 0; i < len_chars; i++) {
60 const char32_t c = utf32[i];
61 switch (mode) {
62 case Case::Uppercase:
63 utf32[i] = BLI_str_utf32_char_to_upper(c);
64 break;
65 case Case::Lowercase:
66 utf32[i] = BLI_str_utf32_char_to_lower(c);
67 break;
68 }
69 }
70
71 Array<char, 64> out(len_chars * 4 + 1);
72 BLI_str_utf32_as_utf8(out.data(), utf32.data(), out.size());
73 return std::string(out.data());
74}
75
77{
78 if (!params.node_tree().typeinfo->validate_link(params.other_socket().type, SOCK_STRING)) {
79 return;
80 }
81
82 if (params.in_out() == SOCK_IN) {
83 for (const EnumPropertyItem *item = case_items; item->identifier != nullptr; item++) {
84 if (item->name != nullptr && item->identifier[0] != '\0') {
85 const int value = item->value;
86 params.add_item(IFACE_(item->name), [value](LinkSearchOpParams &params) {
87 bNode &node = params.add_node("FunctionNodeSetStringCase"_ustr);
88 bke::node_find_socket(node, SOCK_IN, "Case"_ustr)
89 ->default_value_typed<bNodeSocketValueMenu>()
90 ->value = value;
91 params.update_and_connect_available_socket(node, "String"_ustr);
92 });
93 }
94 }
95 }
96 else {
97 params.add_item(IFACE_("String"), [](LinkSearchOpParams &params) {
98 bNode &node = params.add_node("FunctionNodeSetStringCase"_ustr);
99 params.update_and_connect_available_socket(node, "String"_ustr);
100 });
101 }
102}
103
105{
106 static auto fn = mf::build::SI2_SO<std::string, MenuValue, std::string>(
107 "String Case", [](const std::string &s, MenuValue mode) -> std::string {
108 return apply_string_case(s, Case(mode.value));
109 });
110 builder.set_matching_fn(&fn);
111}
112
113static void node_register()
114{
115 static bke::bNodeType ntype;
116
117 common_node_type_base(&ntype, "FunctionNodeSetStringCase"_ustr);
118 ntype.ui_name = "Set String Case";
119 ntype.ui_description = "Convert the case of a string";
121 ntype.declare = node_declare;
125}
127
128} // namespace blender::nodes::node_fn_set_string_case_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
int64_t size() const
Definition BLI_array.hh:267
const T * data() const
Definition BLI_array.hh:323
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)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_declare(NodeDeclarationBuilder &b)
static std::string apply_string_case(const std::string &s, const Case mode)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
size_t BLI_str_utf8_as_utf32(char32_t *__restrict dst_w, const char *__restrict src_c, size_t dst_w_maxncpy) ATTR_NONNULL(1
size_t size_t BLI_str_utf32_as_utf8(char *__restrict dst, const char32_t *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
char32_t BLI_str_utf32_char_to_lower(char32_t wc)
size_t BLI_strlen_utf8_ex(const char *strc, size_t *r_len_bytes) ATTR_NONNULL(1
char32_t BLI_str_utf32_char_to_upper(char32_t wc)
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
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
std::string ui_description
Definition BKE_node.hh:271
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)