Blender V5.3
node_geo_tag_filter.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
7
9
11
13{
14 b.add_input<decl::String>("Tag Filter"_ustr).optional_label();
15 b.add_input<decl::String>("Tags"_ustr).structure_type(StructureType::List);
16 b.add_output<decl::Bool>("Match"_ustr);
17}
18
20{
21 SocketValueVariant tags_variant = params.extract_input<SocketValueVariant>("Tags"_ustr);
22 const std::string tag_filter = params.extract_input<std::string>("Tag Filter"_ustr);
23
25 if (tags_variant.is_list()) {
26 const GListPtr tags_list_ptr = tags_variant.extract<GListPtr>();
27 if (tags_list_ptr) {
28 const GList &list = *tags_list_ptr;
29 if (list.cpp_type().is<std::string>()) {
30 list.typed<std::string>().foreach([&](const std::string &tag) { tags.add(tag); });
31 }
32 }
33 }
34 const bool match = tag_filter_matches(tag_filter, tags);
35 params.set_output("Match"_ustr, match);
36}
37
38static void node_register()
39{
40 static bke::bNodeType ntype;
41 geo_node_type_base(&ntype, "GeometryNodeTagFilter"_ustr);
42 ntype.ui_name = "Tag Filter";
43 ntype.ui_description = "Check if a filter string matches a list of tags";
45 ntype.declare = node_declare;
48}
50
51} // namespace blender::nodes::node_geo_tag_filter_cc
52
53namespace blender::nodes {
54
55bool tag_filter_matches(const StringRef tag_filter, const Set<std::string> &tags)
56{
57 if (tag_filter.is_empty()) {
58 /* An empty filter matches everything. */
59 return true;
60 }
61 StringRef remaining = tag_filter;
62 while (!remaining.is_empty()) {
63 const int sep = remaining.find(',');
64 if (sep == -1) {
65 const StringRef tag = remaining.trim();
66 if (tags.contains_as(tag)) {
67 return true;
68 }
69 return false;
70 }
71 const StringRef tag = remaining.substr(0, sep).trim();
72 if (tags.contains_as(tag)) {
73 return true;
74 }
75 remaining = remaining.substr(sep + 1);
76 }
77 return false;
78}
79
80} // namespace blender::nodes
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NOD_REGISTER_NODE(REGISTER_FUNC)
bool contains_as(const ForwardKey &key) const
Definition BLI_set.hh:314
constexpr StringRef trim() const
constexpr bool is_empty() const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr StringRef substr(int64_t start, int64_t size) 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
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
bool tag_filter_matches(const StringRef tag_filter, const Set< std::string > &tags)
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