Blender V4.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
6
7#include <fmt/format.h>
8
9#include "NOD_rna_define.hh"
10
11#include "UI_interface.hh"
12#include "UI_resources.hh"
13
15
16enum class PatternMode {
17 Exact,
19};
20
22{
23 b.add_input<decl::Geometry>("Geometry");
24 b.add_input<decl::String>("Name").is_attribute_name().hide_label();
25 b.add_output<decl::Geometry>("Geometry").propagate_all();
26}
27
28static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
29{
30 uiItemR(layout, ptr, "pattern_mode", UI_ITEM_NONE, "", ICON_NONE);
31}
32
34{
35 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
36 const std::string pattern = params.extract_input<std::string>("Name");
37 if (pattern.empty()) {
38 params.set_output("Geometry", std::move(geometry_set));
39 return;
40 }
41 const bNode &node = params.node();
42 PatternMode pattern_mode = PatternMode(node.custom1);
43 if (pattern_mode == PatternMode::Wildcard) {
44 const int wildcard_count = Span(pattern.c_str(), pattern.size()).count('*');
45 if (wildcard_count == 0) {
46 pattern_mode = PatternMode::Exact;
47 }
48 else if (wildcard_count >= 2) {
49 params.error_message_add(NodeWarningType::Info,
50 TIP_("Only one * is supported in the pattern"));
51 params.set_output("Geometry", std::move(geometry_set));
52 return;
53 }
54 }
55
56 StringRef wildcard_prefix;
57 StringRef wildcard_suffix;
58 if (pattern_mode == PatternMode::Wildcard) {
59 const int wildcard_index = pattern.find('*');
60 wildcard_prefix = StringRef(pattern).substr(0, wildcard_index);
61 wildcard_suffix = StringRef(pattern).substr(wildcard_index + 1);
62 }
63
64 Set<std::string> removed_attributes;
65 Set<std::string> failed_attributes;
66
67 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
72 {
73 if (!geometry_set.has(type)) {
74 continue;
75 }
76 /* First check if the attribute exists before getting write access,
77 * to avoid potentially expensive unnecessary copies. */
78 const GeometryComponent &read_only_component = *geometry_set.get_component(type);
79 Vector<std::string> attributes_to_remove;
80 switch (pattern_mode) {
81 case PatternMode::Exact: {
82 if (read_only_component.attributes()->contains(pattern)) {
83 attributes_to_remove.append(pattern);
84 }
85 break;
86 }
88 read_only_component.attributes()->foreach_attribute([&](const bke::AttributeIter &iter) {
89 const StringRef attribute_name = iter.name;
90 if (bke::attribute_name_is_anonymous(attribute_name)) {
91 return;
92 }
93 if (attribute_name.startswith(wildcard_prefix) &&
94 attribute_name.endswith(wildcard_suffix))
95 {
96 attributes_to_remove.append(attribute_name);
97 }
98 });
99
100 break;
101 }
102 }
103 if (attributes_to_remove.is_empty()) {
104 break;
105 }
106
107 GeometryComponent &component = geometry_set.get_component_for_write(type);
108 for (const StringRef attribute_name : attributes_to_remove) {
109 if (!bke::allow_procedural_attribute_access(attribute_name)) {
110 continue;
111 }
112 if (component.attributes_for_write()->remove(attribute_name)) {
113 removed_attributes.add(attribute_name);
114 }
115 else {
116 failed_attributes.add(attribute_name);
117 }
118 }
119 }
120 });
121
122 for (const StringRef attribute_name : removed_attributes) {
123 params.used_named_attribute(attribute_name, NamedAttributeUsage::Remove);
124 }
125
126 if (!failed_attributes.is_empty()) {
127 Vector<std::string> quoted_attribute_names;
128 for (const StringRef attribute_name : failed_attributes) {
129 quoted_attribute_names.append(fmt::format("\"{}\"", attribute_name));
130 }
131 const std::string message = fmt::format(TIP_("Cannot remove built-in attributes: {}"),
132 fmt::join(quoted_attribute_names, ", "));
133 params.error_message_add(NodeWarningType::Warning, message);
134 }
135 else if (removed_attributes.is_empty() && pattern_mode == PatternMode::Exact) {
136 const std::string message = fmt::format(TIP_("Attribute does not exist: \"{}\""), pattern);
137 params.error_message_add(NodeWarningType::Warning, message);
138 }
139
140 params.set_output("Geometry", std::move(geometry_set));
141}
142
143static void node_rna(StructRNA *srna)
144{
145 static const EnumPropertyItem pattern_mode_items[] = {
147 "EXACT",
148 0,
149 "Exact",
150 "Remove the one attribute with the given name"},
152 "WILDCARD",
153 0,
154 "Wildcard",
155 "Remove all attributes that match the pattern which is allowed to contain a single "
156 "wildcard (*)"},
157 {0, nullptr, 0, nullptr, nullptr},
158 };
160 "pattern_mode",
161 "Pattern Mode",
162 "How the attributes to remove are chosen",
163 pattern_mode_items,
165}
166
167static void node_register()
168{
169 static blender::bke::bNodeType ntype;
170
172 &ntype, GEO_NODE_REMOVE_ATTRIBUTE, "Remove Named Attribute", NODE_CLASS_ATTRIBUTE);
173 ntype.declare = node_declare;
175 bke::node_type_size(&ntype, 170, 100, 700);
178
179 node_rna(ntype.rna_ext.srna);
180}
182
183} // namespace blender::nodes::node_geo_remove_attribute_cc
#define NODE_CLASS_ATTRIBUTE
Definition BKE_node.hh:419
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
bool add(const Key &key)
Definition BLI_set.hh:248
bool is_empty() const
Definition BLI_set.hh:572
constexpr int64_t count(const T &value) const
Definition BLI_span.hh:301
constexpr int64_t find(char c, int64_t pos=0) const
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()
local_group_size(16, 16) .push_constant(Type b
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
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:4602
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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)
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
StructRNA * srna
Definition RNA_types.hh:780
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
bool has(const GeometryComponent::Type component_type) const
const GeometryComponent * get_component(GeometryComponent::Type component_type) const
void modify_geometry_sets(ForeachSubGeometryCallback callback)
Defines a node type.
Definition BKE_node.hh:218
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeDeclareFunction declare
Definition BKE_node.hh:347
PointerRNA * ptr
Definition wm_files.cc:4126