Blender V5.3
node_geo_material_selection.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 "DNA_mesh_types.h"
8
9#include "BLI_index_mask.hh"
10
11#include "BKE_curves.hh"
12#include "BKE_grease_pencil.hh"
13
15
17{
18 b.add_input<decl::Material>("Material"_ustr).optional_label(true);
19 b.add_output<decl::Bool>("Selection"_ustr).structure_type(StructureType::Field);
20}
21
23 const Material *material,
24 const AttributeAccessor &attributes,
25 const AttrDomain domain,
26 const IndexMask &domain_mask)
27{
28 const int domain_size = attributes.domain_size(domain);
29 Vector<int> slots;
30 for (const int slot_i : materials.index_range()) {
31 if (materials[slot_i] == material) {
32 slots.append(slot_i);
33 }
34 }
35 if (slots.is_empty()) {
36 return VArray<bool>::from_single(false, domain_size);
37 }
38
39 const VArray<int> material_indices = *attributes.lookup_or_default<int>(
40 "material_index", domain, 0);
41 if (const std::optional<int> single = material_indices.get_if_single()) {
42 return VArray<bool>::from_single(slots.contains(*single), domain_size);
43 }
44
45 const VArraySpan<int> material_indices_span(material_indices);
46 Array<bool> domain_selection(domain_mask.min_array_size());
47 domain_mask.foreach_index_optimized<int>(
48 [&](const int domain_index) {
49 const int slot_i = material_indices_span[domain_index];
50 domain_selection[domain_index] = slots.contains(slot_i);
51 },
53 return VArray<bool>::from_container(std::move(domain_selection));
54}
55
57 Material *material_;
58
59 public:
61 : bke::GeometryFieldInput(CPPType::get<bool>(), "Material Selection node"),
62 material_(material)
63 {
64 }
65
67 const IndexMask &mask) const final
68 {
69 switch (context.type()) {
71 const Mesh *mesh = context.mesh();
72 if (!mesh) {
73 return {};
74 }
75 const AttrDomain domain = context.domain();
76 const IndexMask domain_mask = (domain == AttrDomain::Face) ? mask :
77 IndexMask(mesh->faces_num);
78 const AttributeAccessor attributes = mesh->attributes();
80 {mesh->mat, mesh->totcol}, material_, attributes, AttrDomain::Face, domain_mask);
81 return attributes.adapt_domain<bool>(std::move(selection), AttrDomain::Face, domain);
82 }
84 const bke::CurvesGeometry *curves = context.curves_or_strokes();
85 if (!curves) {
86 return {};
87 }
88 const AttrDomain domain = context.domain();
89 const IndexMask domain_mask = (domain == AttrDomain::Curve) ?
90 mask :
91 IndexMask(curves->curves_num());
92 const AttributeAccessor attributes = curves->attributes();
93 const VArray<int> material_indices = *attributes.lookup_or_default<int>(
94 "material_index", AttrDomain::Curve, 0);
95 const GreasePencil &grease_pencil = *context.grease_pencil();
97 {grease_pencil.material_array, grease_pencil.material_array_num},
98 material_,
99 attributes,
100 AttrDomain::Curve,
101 domain_mask);
102 return attributes.adapt_domain<bool>(std::move(selection), AttrDomain::Curve, domain);
103 }
105 const Curves *curves_id = context.curves_id();
106 if (!curves_id) {
107 return {};
108 }
109 const bke::CurvesGeometry *curves = context.curves_or_strokes();
110 if (!curves) {
111 return {};
112 }
113 const AttrDomain domain = context.domain();
114 const IndexMask domain_mask = (domain == AttrDomain::Curve) ?
115 mask :
116 IndexMask(curves->curves_num());
117 const AttributeAccessor attributes = curves->attributes();
118 const VArray<int> material_indices = *attributes.lookup_or_default<int>(
119 "material_index", AttrDomain::Curve, 0);
120 VArray<bool> selection = select_by_material({curves_id->mat, curves_id->totcol},
121 material_,
122 attributes,
123 AttrDomain::Curve,
124 domain_mask);
125 return attributes.adapt_domain<bool>(std::move(selection), AttrDomain::Curve, domain);
126 }
127 default:
128 return {};
129 }
130 }
131
132 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
133 {
134 static constexpr int8_t id = 0;
135 hash.add(&id);
136 hash.add(material_);
137 }
138
139 std::optional<AttrDomain> preferred_domain(
140 const GeometryComponent & /*component*/) const override
141 {
142 return AttrDomain::Face;
143 }
144 bke::NativeFieldDomain native_domain_info(const GeometryComponent & /*component*/) const override
145 {
146 return bke::NativeFieldDomain::Domain{AttrDomain::Face};
147 }
148};
149
151{
152 Material *material = params.extract_input<Material *>("Material"_ustr);
153 params.set_output("Selection"_ustr,
155}
156
157static void node_register()
158{
159 static bke::bNodeType ntype;
160
161 geo_node_type_base(&ntype, "GeometryNodeMaterialSelection"_ustr, GEO_NODE_MATERIAL_SELECTION);
162 ntype.ui_name = "Material Selection";
163 ntype.ui_description = "Provide a selection of faces that use the specified material";
164 ntype.enum_name_legacy = "MATERIAL_SELECTION";
166 ntype.declare = node_declare;
169}
171
172} // namespace blender::nodes::node_geo_material_selection_cc
Low-level operations for curves.
Low-level operations for grease pencil.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_MATERIAL_SELECTION
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
std::optional< T > get_if_single() const
static VArray from_single(T value, const int64_t size)
static VArray from_container(ContainerT container)
bool contains(const T &value) const
void append(const T &value)
bool is_empty() const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GVArray adapt_domain(const GVArray &varray, const AttrDomain from_domain, const AttrDomain to_domain) const
int domain_size(const AttrDomain domain) const
uint64_t hash() const
Definition field.cc:185
void foreach_index_optimized(Fn &&fn, Mode mode=exec_mode::serial) const
static Field from_input(Args &&...args)
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
bke::NativeFieldDomain native_domain_info(const GeometryComponent &) const override
std::optional< AttrDomain > preferred_domain(const GeometryComponent &) const override
GVArray get_varray_for_context(const bke::GeometryFieldContext &context, const IndexMask &mask) const final
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &) const override
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
constexpr ParallelGrainSize grain_size(int grain_size)
static VArray< bool > select_by_material(const Span< Material * > materials, const Material *material, const AttributeAccessor &attributes, const AttrDomain domain, const IndexMask &domain_mask)
static void node_declare(NodeDeclarationBuilder &b)
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed char int8_t
Definition BKE_text.h:76
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