Blender V5.0
node_geo_set_material_index.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
8#include "DNA_mesh_types.h"
9
10#include "BKE_curves.hh"
11#include "BKE_grease_pencil.hh"
12
14
16
18{
19 b.use_custom_socket_order();
20 b.allow_any_socket_order();
21 b.add_input<decl::Geometry>("Geometry")
22 .supported_type({GeometryComponent::Type::Mesh, GeometryComponent::Type::GreasePencil})
23 .description("Geometry to update the material indices on");
24 b.add_output<decl::Geometry>("Geometry").propagate_all().align_with_previous();
25 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
26 b.add_input<decl::Int>("Material Index").min(0).field_on_all();
27}
28
30 const Field<bool> &selection,
31 const Field<int> &material_index)
32{
33 using namespace blender::bke::greasepencil;
34 for (const int layer_index : grease_pencil.layers().index_range()) {
35 Drawing *drawing = grease_pencil.get_eval_drawing(grease_pencil.layer(layer_index));
36 if (drawing == nullptr) {
37 continue;
38 }
41 bke::GreasePencilLayerFieldContext(grease_pencil, AttrDomain::Curve, layer_index),
42 "material_index",
44 selection,
45 material_index);
46 }
47}
48
50{
51 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
52 const Field<bool> selection = params.extract_input<Field<bool>>("Selection");
53 const Field<int> material_index = params.extract_input<Field<int>>("Material Index");
54
55 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
56 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
57 bke::try_capture_field_on_geometry(mesh->attributes_for_write(),
59 "material_index",
61 selection,
62 material_index);
63 }
64 if (GreasePencil *grease_pencil = geometry_set.get_grease_pencil_for_write()) {
65 set_material_index_in_grease_pencil(*grease_pencil, selection, material_index);
66 }
67 });
68 params.set_output("Geometry", std::move(geometry_set));
69}
70
71static void node_register()
72{
73 static blender::bke::bNodeType ntype;
74
75 geo_node_type_base(&ntype, "GeometryNodeSetMaterialIndex", GEO_NODE_SET_MATERIAL_INDEX);
76 ntype.ui_name = "Set Material Index";
77 ntype.ui_description = "Set the material index for each selected geometry element";
78 ntype.enum_name_legacy = "SET_MATERIAL_INDEX";
81 ntype.declare = node_declare;
83}
85
86} // namespace blender::nodes::node_geo_set_material_index_cc
Low-level operations for curves.
Low-level operations for grease pencil.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_SET_MATERIAL_INDEX
#define NOD_REGISTER_NODE(REGISTER_FUNC)
MutableAttributeAccessor attributes_for_write()
bke::CurvesGeometry & strokes_for_write()
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
bool try_capture_field_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, const StringRef attribute_id, AttrDomain domain, const fn::Field< bool > &selection, const fn::GField &field)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void node_declare(NodeDeclarationBuilder &b)
static void set_material_index_in_grease_pencil(GreasePencil &grease_pencil, const Field< bool > &selection, const Field< int > &material_index)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:36
GreasePencil * get_grease_pencil_for_write()
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362