Blender V5.3
node_geo_rename_attribute.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
6
7#include "BKE_attribute.hh"
8
10
11#include <fmt/format.h>
12
14
15enum class RenameMode {
18};
19
22 "SINGLE",
23 0,
24 N_("Single"),
25 N_("Rename a single attribute with the provided attribute name")},
27 "PREFIX",
28 0,
29 N_("Prefix"),
30 N_("Rename all attributes with the provided prefix")},
31 {},
32};
33
35{
36 b.use_custom_socket_order();
37 b.allow_any_socket_order();
38
39 b.add_input<decl::Geometry>("Geometry"_ustr);
40 b.add_output<decl::Geometry>("Geometry"_ustr).align_with_previous().propagate_all_geometry();
41
42 b.add_input<decl::Menu>("Mode"_ustr).static_items(rename_mode_items).optional_label();
43
44 b.add_input<decl::String>("Old"_ustr).optional_label().is_attribute_name();
45 b.add_input<decl::String>("New"_ustr).optional_label();
46 b.add_input<decl::Bool>("Overwrite"_ustr).default_value(false);
47}
48
50{
51 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry"_ustr);
52 const RenameMode mode = params.extract_input<RenameMode>("Mode"_ustr);
53 const std::string old_name = params.extract_input<std::string>("Old"_ustr);
54 const std::string new_name = params.extract_input<std::string>("New"_ustr);
55 const bool overwrite = params.extract_input<bool>("Overwrite"_ustr);
56
57 if (old_name.empty()) {
58 params.set_output("Geometry"_ustr, std::move(geometry_set));
59 return;
60 }
61 if (old_name == new_name) {
62 params.set_output("Geometry"_ustr, std::move(geometry_set));
63 return;
64 }
65
66 std::atomic<bool> not_found = false;
67 Mutex failures_lock;
70 for (const auto type :
71 {
77 })
78 {
79 if (!geometry.has(type)) {
80 continue;
81 }
82 AlignedBuffer<512, 8> strings_buffer;
83 ResourceScope scope(strings_buffer);
85 {
86 const GeometryComponent &component = *geometry.get_component(type);
87 const AttributeAccessor attributes = *component.attributes();
88 switch (mode) {
89 case RenameMode::Single: {
90 if (!attributes.contains(old_name)) {
91 not_found = true;
92 continue;
93 }
94 renames.add_new(old_name, new_name);
95 break;
96 }
97 case RenameMode::Prefix: {
98 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
99 if (iter.name.startswith(old_name)) {
100 renames.add(
101 scope.allocator().copy_string(iter.name),
102 scope.construct<std::string>(new_name + iter.name.substr(old_name.size())));
103 }
104 });
105 break;
106 }
107 }
108 }
109 if (renames.is_empty()) {
110 continue;
111 }
112 GeometryComponent &component = geometry.get_component_for_write(type);
113 MutableAttributeAccessor attributes = *component.attributes_for_write();
114 const Set<StringRef> failed = attributes.rename(renames, overwrite);
115 if (!failed.is_empty()) {
116 std::scoped_lock lock(failures_lock);
117 for (const StringRef failed_name : failed) {
118 failures.add_as(failed_name, renames.lookup(failed_name));
119 }
120 }
121 }
122 });
123
124 if (not_found) {
126 fmt::format("{}: '{}'", TIP_("Attribute not found"), old_name));
127 }
128 for (const auto &[old_name, new_name] : failures.items()) {
129 params.error_message_add(
131 fmt::format(fmt::runtime("Failed to rename attribute: '{}' to '{}'"), old_name, new_name));
132 }
133
134 params.set_output("Geometry"_ustr, std::move(geometry_set));
135}
136
137static void node_register()
138{
139 static bke::bNodeType ntype;
140
141 geo_node_type_base(&ntype, "GeometryNodeRenameAttribute"_ustr);
142 ntype.ui_name = "Rename Attribute";
143 ntype.ui_description = "Change the name of an attribute";
146 ntype.declare = node_declare;
148}
150
151} // namespace blender::nodes::node_geo_rename_attribute_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
volatile int lock
StringRefNull copy_string(StringRef str)
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
bool add_as(ForwardKey &&key, ForwardValue &&...value)
Definition BLI_map.hh:312
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
bool is_empty() const
Definition BLI_map.hh:986
ItemIterator items() const &
Definition BLI_map.hh:902
T & construct(Args &&...args)
LinearAllocator & allocator()
bool is_empty() const
Definition BLI_set.hh:614
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
bool contains(StringRef name) const
virtual std::optional< AttributeAccessor > attributes() const
virtual std::optional< MutableAttributeAccessor > attributes_for_write()
bool rename(StringRef old_name, StringRef new_name, bool overwrite=false)
constexpr bool startswith(StringRef prefix) const
constexpr StringRef substr(int64_t start, int64_t size) const
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
std::mutex Mutex
Definition BLI_mutex.hh:51
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)