Blender V5.3
node_geo_set_material.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_curves_types.h"
8#include "DNA_mesh_types.h"
10#include "DNA_volume_types.h"
11
12#include "BKE_curves.hh"
13#include "BKE_grease_pencil.hh"
14#include "BKE_material.hh"
15#include "BKE_mesh_types.hh"
16
18
20
22{
23 b.use_custom_socket_order();
24 b.allow_any_socket_order();
25 b.add_input<decl::Geometry>("Geometry"_ustr)
26 .supported_type({GeometryComponent::Type::Mesh,
31 b.add_output<decl::Geometry>("Geometry"_ustr)
32 .propagate_all_geometry()
33 .align_with_previous()
34 .description("Geometry to assign a material to");
35 b.add_input<decl::Bool>("Selection"_ustr)
36 .default_value(true)
37 .hide_value()
38 .evaluated_geometry_field();
39 b.add_input<decl::Material>("Material"_ustr).optional_label();
40}
41
43 const fn::FieldContext &field_context,
44 const Field<bool> &selection_field,
45 MutableAttributeAccessor &attributes,
46 const AttrDomain domain,
47 Material *material,
48 SharedCache<std::optional<int>> &max_index_cache)
49{
50 const int domain_size = attributes.domain_size(domain);
51 fn::FieldEvaluator selection_evaluator{field_context, domain_size};
52 selection_evaluator.set_selection(selection_field);
53 selection_evaluator.evaluate();
54 const IndexMask selection = selection_evaluator.get_evaluated_selection_as_mask();
55
56 if (selection.size() != domain_size) {
57 /* If the entire geometry isn't selected, and there is no material slot yet, add an empty
58 * slot so that the faces that aren't selected can still refer to the default material. */
60 }
61
62 int new_index = -1;
63 const int orig_materials_num = *BKE_id_material_len_p(id);
64 if (Material **materials = *BKE_id_material_array_p(id)) {
65 new_index = Span(materials, orig_materials_num).first_index_try(material);
66 }
67
68 if (new_index == -1) {
69 /* Append a new material index. */
70 new_index = orig_materials_num;
71 BKE_id_material_eval_assign(id, new_index + 1, material);
72 }
73
74 if (selection.size() == domain_size) {
75 attributes.remove("material_index");
76 attributes.add<int>("material_index", domain, bke::AttributeInitValue(new_index));
77 max_index_cache.ensure([&](std::optional<int> &data) { data = new_index; });
78 }
79 else {
81 "material_index", domain);
82 index_mask::masked_fill(indices.span, new_index, selection);
83 indices.finish();
84 if (new_index == orig_materials_num) {
85 max_index_cache.ensure([&](std::optional<int> &data) { data = new_index; });
86 }
87 }
88}
89
91{
92 Material *material = params.extract_input<Material *>("Material"_ustr);
93 const Field<bool> selection_field = params.extract_input<Field<bool>>("Selection"_ustr);
94
95 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry"_ustr);
96
97 /* Only add the warnings once, even if there are many unique instances. */
98 bool no_faces_warning = false;
99 bool point_selection_warning = false;
100 bool volume_selection_warning = false;
101
102 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
103 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
104 if (mesh->faces_num == 0) {
105 if (mesh->verts_num > 0) {
106 no_faces_warning = true;
107 }
108 }
109 else {
110 const bke::MeshFieldContext field_context{*mesh, AttrDomain::Face};
111 MutableAttributeAccessor attributes = mesh->attributes_for_write();
113 field_context,
114 selection_field,
115 attributes,
116 AttrDomain::Face,
117 material,
118 mesh->runtime->max_material_index);
119 }
120 }
121 if (Volume *volume = geometry_set.get_volume_for_write()) {
122 BKE_id_material_eval_assign(&volume->id, 1, material);
123 if (selection_field.depends_on_input()) {
124 volume_selection_warning = true;
125 }
126 }
127 if (PointCloud *pointcloud = geometry_set.get_pointcloud_for_write()) {
128 BKE_id_material_eval_assign(&pointcloud->id, 1, material);
129 if (selection_field.depends_on_input()) {
130 point_selection_warning = true;
131 }
132 }
133 if (Curves *curves_id = geometry_set.get_curves_for_write()) {
134 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
135 const bke::CurvesFieldContext field_context{curves, AttrDomain::Curve};
136 MutableAttributeAccessor attributes = curves.attributes_for_write();
137 assign_material_to_id_geometry(&curves_id->id,
138 field_context,
139 selection_field,
140 attributes,
141 AttrDomain::Curve,
142 material,
143 curves.runtime->max_material_index_cache);
144 }
145 if (GreasePencil *grease_pencil = geometry_set.get_grease_pencil_for_write()) {
146 using namespace blender::bke::greasepencil;
147 for (const int layer_index : grease_pencil->layers().index_range()) {
148 Drawing *drawing = grease_pencil->get_eval_drawing(grease_pencil->layer(layer_index));
149 if (drawing == nullptr) {
150 continue;
151 }
153 if (curves.is_empty()) {
154 continue;
155 }
156
157 const bke::GreasePencilLayerFieldContext field_context{
158 *grease_pencil, AttrDomain::Curve, layer_index};
159 MutableAttributeAccessor attributes = curves.attributes_for_write();
161 field_context,
162 selection_field,
163 attributes,
165 material,
166 curves.runtime->max_material_index_cache);
167 }
168 }
169 });
170
171 if (no_faces_warning) {
173 TIP_("Mesh has no faces for material assignment"));
174 }
175 if (volume_selection_warning) {
176 params.error_message_add(
178 TIP_("Volumes only support a single material; selection input cannot be a field"));
179 }
180 if (point_selection_warning) {
181 params.error_message_add(
183 TIP_("Point clouds only support a single material; selection input cannot be a field"));
184 }
185
186 params.set_output("Geometry"_ustr, std::move(geometry_set));
187}
188
189static void node_register()
190{
191 static bke::bNodeType ntype;
192
193 geo_node_type_base(&ntype, "GeometryNodeSetMaterial"_ustr, GEO_NODE_SET_MATERIAL);
194 ntype.ui_name = "Set Material";
195 ntype.ui_description = "Assign a material to geometry elements";
196 ntype.enum_name_legacy = "SET_MATERIAL";
198 ntype.declare = node_declare;
201}
203
204} // namespace blender::nodes::node_geo_set_material_cc
Low-level operations for curves.
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_SET_MATERIAL
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr Span()=default
int domain_size(const AttrDomain domain) const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
bke::CurvesGeometry & strokes_for_write()
void set_selection(Field< bool > selection)
IndexMask get_evaluated_selection_as_mask() const
void error_message_add(const NodeWarningType type, StringRef message) 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
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
static void node_geo_exec(GeoNodeExecParams params)
static void assign_material_to_id_geometry(ID *id, const fn::FieldContext &field_context, const Field< bool > &selection_field, MutableAttributeAccessor &attributes, const AttrDomain domain, Material *material, SharedCache< std::optional< int > > &max_index_cache)
static void node_declare(NodeDeclarationBuilder &b)
void BKE_id_material_eval_assign(ID *id, int slot, Material *material)
short * BKE_id_material_len_p(ID *id)
Material *** BKE_id_material_array_p(ID *id)
BMesh const char void * data
void BKE_id_material_eval_ensure_default_slot(ID *id)
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
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393