Blender V5.0
node_geo_remove_attribute.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
5#include "BKE_instances.hh"
7
8#include <fmt/format.h>
9
11
12enum class PatternMode {
15};
16
19 "EXACT",
20 0,
21 N_("Exact"),
22 N_("Remove the one attribute with the given name")},
24 "WILDCARD",
25 0,
26 N_("Wildcard"),
27 N_("Remove all attributes that match the pattern which is allowed to contain a single "
28 "wildcard (*)")},
29 {0, nullptr, 0, nullptr, nullptr},
30};
31
33{
34 b.use_custom_socket_order();
35 b.allow_any_socket_order();
36 b.add_input<decl::Geometry>("Geometry").description("Geometry to remove attributes from");
37 b.add_output<decl::Geometry>("Geometry").propagate_all().align_with_previous();
38 b.add_input<decl::Menu>("Pattern Mode")
39 .static_items(pattern_mode_items)
41 .description("How the attributes to remove are chosen");
42 b.add_input<decl::String>("Name").is_attribute_name().optional_label();
43}
44
54
56{
57 for (const GeometryComponent::Type type : {GeometryComponent::Type::Mesh,
58 GeometryComponent::Type::PointCloud,
59 GeometryComponent::Type::Curve,
60 GeometryComponent::Type::Instance,
61 GeometryComponent::Type::GreasePencil})
62 {
63 if (!geometry_set.has(type)) {
64 continue;
65 }
66 /* First check if the attribute exists before getting write access,
67 * to avoid potentially expensive unnecessary copies. */
68 const GeometryComponent &read_only_component = *geometry_set.get_component(type);
69 Vector<std::string> attributes_to_remove;
70 switch (params.pattern_mode) {
71 case PatternMode::Exact: {
72 if (read_only_component.attributes()->contains(params.pattern)) {
73 attributes_to_remove.append(params.pattern);
74 }
75 break;
76 }
78 read_only_component.attributes()->foreach_attribute([&](const bke::AttributeIter &iter) {
79 const StringRef attribute_name = iter.name;
80 if (bke::attribute_name_is_anonymous(attribute_name)) {
81 return;
82 }
83 if (attribute_name.startswith(params.wildcard_prefix) &&
84 attribute_name.endswith(params.wildcard_suffix))
85 {
86 attributes_to_remove.append(attribute_name);
87 }
88 });
89
90 break;
91 }
92 }
93 if (attributes_to_remove.is_empty()) {
94 break;
95 }
96
97 GeometryComponent &component = geometry_set.get_component_for_write(type);
98 for (const StringRef attribute_name : attributes_to_remove) {
99 if (!bke::allow_procedural_attribute_access(attribute_name)) {
100 continue;
101 }
102 if (component.attributes_for_write()->remove(attribute_name)) {
103 params.removed_attributes.add(attribute_name);
104 }
105 else {
106 params.failed_attributes.add(attribute_name);
107 }
108 }
109 }
110
111 if (bke::Instances *instances = geometry_set.get_instances_for_write()) {
112 instances->ensure_geometry_instances();
113 for (bke::InstanceReference &reference : instances->references_for_write()) {
114 if (reference.type() == bke::InstanceReference::Type::GeometrySet) {
115 remove_attributes_recursive(reference.geometry_set(), params);
116 }
117 }
118 }
119}
120
122{
123 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
124 const std::string pattern = params.extract_input<std::string>("Name");
125 if (pattern.empty()) {
126 params.set_output("Geometry", std::move(geometry_set));
127 return;
128 }
129
130 PatternMode pattern_mode = params.get_input<PatternMode>("Pattern Mode");
131 if (pattern_mode == PatternMode::Wildcard) {
132 const int wildcard_count = Span(pattern.c_str(), pattern.size()).count('*');
133 if (wildcard_count == 0) {
134 pattern_mode = PatternMode::Exact;
135 }
136 else if (wildcard_count >= 2) {
137 params.error_message_add(NodeWarningType::Info,
138 TIP_("Only one * is supported in the pattern"));
139 params.set_output("Geometry", std::move(geometry_set));
140 return;
141 }
142 }
143
144 RemoveAttributeParams removal_params;
145 removal_params.pattern_mode = pattern_mode;
146 removal_params.pattern = pattern;
147 if (pattern_mode == PatternMode::Wildcard) {
148 const int wildcard_index = pattern.find('*');
149 removal_params.wildcard_prefix = StringRef(pattern).substr(0, wildcard_index);
150 removal_params.wildcard_suffix = StringRef(pattern).substr(wildcard_index + 1);
151 }
152
153 remove_attributes_recursive(geometry_set, removal_params);
154
155 for (const StringRef attribute_name : removal_params.removed_attributes) {
156 params.used_named_attribute(attribute_name, NamedAttributeUsage::Remove);
157 }
158
159 if (!removal_params.failed_attributes.is_empty()) {
160 Vector<std::string> quoted_attribute_names;
161 for (const StringRef attribute_name : removal_params.failed_attributes) {
162 quoted_attribute_names.append(fmt::format("\"{}\"", attribute_name));
163 }
164 const std::string message = fmt::format(
165 fmt::runtime(TIP_("Cannot remove built-in attributes: {}")),
166 fmt::join(quoted_attribute_names, ", "));
167 params.error_message_add(NodeWarningType::Warning, message);
168 }
169 else if (removal_params.removed_attributes.is_empty() && pattern_mode == PatternMode::Exact) {
170 const std::string message = fmt::format(fmt::runtime(TIP_("Attribute does not exist: \"{}\"")),
171 pattern);
172 params.error_message_add(NodeWarningType::Warning, message);
173 }
174
175 params.set_output("Geometry", std::move(geometry_set));
176}
177
178static void node_register()
179{
180 static blender::bke::bNodeType ntype;
181
182 geo_node_type_base(&ntype, "GeometryNodeRemoveAttribute", GEO_NODE_REMOVE_ATTRIBUTE);
183 ntype.ui_name = "Remove Named Attribute";
184 ntype.ui_description =
185 "Delete an attribute with a specified name from a geometry. Typically used to optimize "
186 "performance";
187 ntype.enum_name_legacy = "REMOVE_ATTRIBUTE";
189 ntype.declare = node_declare;
190 bke::node_type_size(ntype, 170, 100, 700);
193}
195
196} // namespace blender::nodes::node_geo_remove_attribute_cc
#define NODE_CLASS_ATTRIBUTE
Definition BKE_node.hh:462
#define GEO_NODE_REMOVE_ATTRIBUTE
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
bool is_empty() const
Definition BLI_set.hh:595
constexpr int64_t count(const T &value) const
Definition BLI_span.hh:300
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr bool startswith(StringRef prefix) const
constexpr bool endswith(StringRef suffix) const
void append(const T &value)
bool is_empty() const
virtual std::optional< AttributeAccessor > attributes() const
virtual std::optional< MutableAttributeAccessor > attributes_for_write()
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
bool attribute_name_is_anonymous(const StringRef name)
bool allow_procedural_attribute_access(StringRef attribute_name)
void node_type_size(bNodeType &ntype, int width, int minwidth, int maxwidth)
Definition node.cc:5384
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
static void node_declare(NodeDeclarationBuilder &b)
static void remove_attributes_recursive(GeometrySet &geometry_set, RemoveAttributeParams &params)
static void node_geo_exec(GeoNodeExecParams params)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
Instances * get_instances_for_write()
bool has(const GeometryComponent::Type component_type) const
const GeometryComponent * get_component(GeometryComponent::Type component_type) const
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
#define N_(msgid)