Blender V5.3
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_curves.hh"
7#include "BKE_instances.hh"
8
10
11#include "node_geometry_util.hh"
12
13#include <fmt/format.h>
14#include <fmt/ranges.h>
15
17
19{
20 b.use_custom_socket_order();
21 b.allow_any_socket_order();
22 b.add_input<decl::Geometry>("Geometry"_ustr).description("Geometry to remove attributes from");
23 b.add_output<decl::Geometry>("Geometry"_ustr).propagate_all_geometry().align_with_previous();
24 b.add_input<decl::Menu>("Pattern Mode"_ustr)
25 .static_items(string_pattern_mode_items)
27 .description("How the attributes to remove are chosen");
28 b.add_input<decl::String>("Name"_ustr).is_attribute_name().optional_label();
29}
30
37
38static void remove_attributes(const bke::AttributeAccessor &accessor,
39 FunctionRef<MutableAttributeAccessor()> get_write_accessor,
41{
42 Vector<std::string> attributes_to_remove;
43 if (const std::optional<StringRef> exact_pattern = params.pattern.exact_pattern()) {
44 if (bke::attribute_name_is_anonymous(*exact_pattern)) {
45 return;
46 }
47 if (!bke::allow_procedural_attribute_access(*exact_pattern)) {
48 return;
49 }
50 if (accessor.contains(*exact_pattern)) {
51 attributes_to_remove.append(*exact_pattern);
52 }
53 }
54 else {
55 accessor.foreach_attribute([&](const bke::AttributeIter &iter) {
56 const StringRef attribute_name = iter.name;
57 if (bke::attribute_name_is_anonymous(attribute_name)) {
58 return;
59 }
60 if (!bke::allow_procedural_attribute_access(attribute_name)) {
61 return;
62 }
63 if (params.pattern.match(attribute_name)) {
64 attributes_to_remove.append(attribute_name);
65 }
66 });
67 }
68 if (attributes_to_remove.is_empty()) {
69 return;
70 }
71 MutableAttributeAccessor write_accessor = get_write_accessor();
72 for (const StringRef attribute_name : attributes_to_remove) {
73 if (!bke::allow_procedural_attribute_access(attribute_name)) {
74 continue;
75 }
76 if (write_accessor.remove(attribute_name)) {
77 params.removed_attributes.add(attribute_name);
78 }
79 else {
80 params.failed_attributes.add(attribute_name);
81 }
82 }
83}
84
86{
92 {
93 if (!geometry_set.has(type)) {
94 continue;
95 }
96 /* First check if the attribute exists before getting write access,
97 * to avoid potentially expensive unnecessary copies. */
98 const GeometryComponent &read_only_component = *geometry_set.get_component(type);
100 *read_only_component.attributes(),
101 [&]() {
102 GeometryComponent &component = geometry_set.get_component_for_write(type);
103 return *component.attributes_for_write();
104 },
105 params);
106 }
107
108 if (bke::Instances *instances = geometry_set.get_instances_for_write()) {
109 instances->ensure_geometry_instances();
110 for (bke::InstanceReference &reference : instances->references_for_write()) {
111 if (reference.type() == bke::InstanceReference::Type::GeometrySet) {
112 remove_attributes_recursive(reference.geometry_set(), params);
113 }
114 }
115 }
117 using namespace blender::bke::greasepencil;
118 for (const int layer_index : grease_pencil->layers().index_range()) {
119 Drawing *drawing = grease_pencil->get_eval_drawing(grease_pencil->layer(layer_index));
120 if (drawing == nullptr) {
121 continue;
122 }
124 drawing->strokes().attributes(),
125 [&]() {
126 bke::CurvesGeometry &curves = drawing->strokes_for_write();
127 return curves.attributes_for_write();
128 },
129 params);
130 }
131 }
132}
133
135{
136 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry"_ustr);
137 const std::string pattern = params.extract_input<std::string>("Name"_ustr);
138 if (pattern.empty()) {
139 params.set_output("Geometry"_ustr, std::move(geometry_set));
140 return;
141 }
142
143 std::string pattern_error;
144 std::optional<StringPattern> pattern_fn = StringPattern::from_str(
145 params.get_input<StringPatternMode>("Pattern Mode"_ustr), pattern, pattern_error);
146 if (!pattern_fn) {
147 params.error_message_add(NodeWarningType::Error, pattern_error);
148 params.set_output("Geometry"_ustr, std::move(geometry_set));
149 return;
150 }
151
152 RemoveAttributeParams removal_params{*pattern_fn};
153 remove_attributes_recursive(geometry_set, removal_params);
154
155 for (const StringRef attribute_name : removal_params.removed_attributes) {
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, ", "));
168 }
169 else if (removal_params.removed_attributes.is_empty()) {
170 if (const std::optional<StringRef> exact_pattern = removal_params.pattern.exact_pattern()) {
171 const std::string message = fmt::format(
172 fmt::runtime(TIP_("Attribute does not exist: \"{}\"")), *exact_pattern);
174 }
175 }
176
177 params.set_output("Geometry"_ustr, std::move(geometry_set));
178}
179
180static void node_register()
181{
182 static bke::bNodeType ntype;
183
184 geo_node_type_base(&ntype, "GeometryNodeRemoveAttribute"_ustr, GEO_NODE_REMOVE_ATTRIBUTE);
185 ntype.ui_name = "Remove Named Attribute";
186 ntype.ui_description =
187 "Delete an attribute with a specified name from a geometry. Typically used to optimize "
188 "performance";
189 ntype.enum_name_legacy = "REMOVE_ATTRIBUTE";
191 ntype.declare = node_declare;
195}
197
198} // namespace blender::nodes::node_geo_remove_attribute_cc
Low-level operations for curves.
Low-level operations for grease pencil.
#define NODE_CLASS_ATTRIBUTE
Definition BKE_node.hh:493
#define GEO_NODE_REMOVE_ATTRIBUTE
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
bool add(const Key &key)
Definition BLI_set.hh:248
bool is_empty() const
Definition BLI_set.hh:614
void append(const T &value)
bool is_empty() const
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
bool contains(StringRef name) const
AttributeAccessor attributes() const
virtual std::optional< AttributeAccessor > attributes() const
const bke::CurvesGeometry & strokes() const
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
T get_input(const UString identifier) const
void used_named_attribute(StringRef attribute_name, NamedAttributeUsage usage)
void set_output(const UString identifier, T &&value)
MutableAttributeAccessor(void *owner, const AttributeAccessorFunctions &fn)
bool match(StringRef query) const
static std::optional< StringPattern > from_str(StringPatternMode mode, StringRef pattern, std::string &r_error)
std::optional< StringRef > exact_pattern() const
bool attribute_name_is_anonymous(const StringRef name)
bool allow_procedural_attribute_access(StringRef attribute_name)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_declare(NodeDeclarationBuilder &b)
static void remove_attributes_recursive(GeometrySet &geometry_set, RemoveAttributeParams &params)
static void node_geo_exec(GeoNodeExecParams params)
static void remove_attributes(const bke::AttributeAccessor &accessor, FunctionRef< MutableAttributeAccessor()> get_write_accessor, RemoveAttributeParams &params)
const EnumPropertyItem string_pattern_mode_items[]
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Instances * get_instances_for_write()
bool has(const GeometryComponent::Type component_type) const
const GeometryComponent * get_component(GeometryComponent::Type component_type) const
GreasePencil * get_grease_pencil_for_write()
static constexpr int _180
Definition BKE_node.hh:247
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
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393