Blender V4.5
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{
17 b.use_custom_socket_order();
18 b.allow_any_socket_order();
19 b.add_input<decl::Geometry>("Geometry")
20 .supported_type({GeometryComponent::Type::Mesh, GeometryComponent::Type::GreasePencil});
21 b.add_output<decl::Geometry>("Geometry").propagate_all().align_with_previous();
22 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
23 b.add_input<decl::Int>("Material Index").min(0).field_on_all();
24}
25
27 const Field<bool> &selection,
28 const Field<int> &material_index)
29{
30 using namespace blender::bke::greasepencil;
31 for (const int layer_index : grease_pencil.layers().index_range()) {
32 Drawing *drawing = grease_pencil.get_eval_drawing(grease_pencil.layer(layer_index));
33 if (drawing == nullptr) {
34 continue;
35 }
38 bke::GreasePencilLayerFieldContext(grease_pencil, AttrDomain::Curve, layer_index),
39 "material_index",
41 selection,
42 material_index);
43 }
44}
45
47{
48 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
49 const Field<bool> selection = params.extract_input<Field<bool>>("Selection");
50 const Field<int> material_index = params.extract_input<Field<int>>("Material Index");
51
52 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
53 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
54 bke::try_capture_field_on_geometry(mesh->attributes_for_write(),
56 "material_index",
58 selection,
59 material_index);
60 }
61 if (GreasePencil *grease_pencil = geometry_set.get_grease_pencil_for_write()) {
62 set_material_index_in_grease_pencil(*grease_pencil, selection, material_index);
63 }
64 });
65 params.set_output("Geometry", std::move(geometry_set));
66}
67
68static void node_register()
69{
70 static blender::bke::bNodeType ntype;
71
72 geo_node_type_base(&ntype, "GeometryNodeSetMaterialIndex", GEO_NODE_SET_MATERIAL_INDEX);
73 ntype.ui_name = "Set Material Index";
74 ntype.ui_description = "Set the material index for each selected geometry element";
75 ntype.enum_name_legacy = "SET_MATERIAL_INDEX";
78 ntype.declare = node_declare;
80}
82
83} // 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:447
#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:2748
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)
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
void modify_geometry_sets(ForeachSubGeometryCallback callback)
GreasePencil * get_grease_pencil_for_write()
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355