Blender V5.3
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"_ustr)
22 .supported_type({GeometryComponent::Type::Mesh,
25 .description("Geometry to update the material indices on");
26 b.add_output<decl::Geometry>("Geometry"_ustr).propagate_all_geometry().align_with_previous();
27 b.add_input<decl::Bool>("Selection"_ustr)
28 .default_value(true)
29 .hide_value()
30 .evaluated_geometry_field();
31 b.add_input<decl::Int>("Material Index"_ustr).min(0).evaluated_geometry_field();
32}
33
35 const Field<bool> &selection,
36 const Field<int> &material_index)
37{
38 using namespace blender::bke::greasepencil;
39 for (const int layer_index : grease_pencil.layers().index_range()) {
40 Drawing *drawing = grease_pencil.get_eval_drawing(grease_pencil.layer(layer_index));
41 if (drawing == nullptr) {
42 continue;
43 }
47 "material_index",
49 selection,
50 material_index);
51 }
52}
53
55{
56 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry"_ustr);
57 const Field<bool> selection = params.extract_input<Field<bool>>("Selection"_ustr);
58 const Field<int> material_index = params.extract_input<Field<int>>("Material Index"_ustr);
59
60 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
61 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
62 bke::try_capture_field_on_geometry(mesh->attributes_for_write(),
64 "material_index",
66 selection,
67 material_index);
68 }
69 if (Curves *curves_id = geometry_set.get_curves_for_write()) {
70 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
71 bke::try_capture_field_on_geometry(curves.attributes_for_write(),
73 "material_index",
75 selection,
76 material_index);
77 }
79 set_material_index_in_grease_pencil(*grease_pencil, selection, material_index);
80 }
81 });
82 params.set_output("Geometry"_ustr, std::move(geometry_set));
83}
84
85static void node_register()
86{
87 static bke::bNodeType ntype;
88
89 geo_node_type_base(&ntype, "GeometryNodeSetMaterialIndex"_ustr, GEO_NODE_SET_MATERIAL_INDEX);
90 ntype.ui_name = "Set Material Index";
91 ntype.ui_description = "Set the material index for each selected geometry element";
92 ntype.enum_name_legacy = "SET_MATERIAL_INDEX";
95 ntype.declare = node_declare;
97}
99
100} // 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:492
#define GEO_NODE_SET_MATERIAL_INDEX
#define NOD_REGISTER_NODE(REGISTER_FUNC)
MutableAttributeAccessor attributes_for_write()
bke::CurvesGeometry & strokes_for_write()
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
bool try_capture_field_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, const StringRef name, AttrDomain domain, const fn::Field< bool > &selection, const fn::GField &field)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
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(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:38
GreasePencil * get_grease_pencil_for_write()
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