Blender V5.0
node_geo_material_replace.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
10
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 .description("Geometry to replace materials on");
22 b.add_output<decl::Geometry>("Geometry").propagate_all().align_with_previous();
23 b.add_input<decl::Material>("Old");
24 b.add_input<decl::Material>("New").translation_context(BLT_I18NCONTEXT_ID_MATERIAL);
25}
26
28 Material *src_material,
29 Material *dst_material)
30{
31 for (const int i : materials.index_range()) {
32 if (materials[i] == src_material) {
33 materials[i] = dst_material;
34 }
35 }
36}
37
39{
40 Material *old_material = params.extract_input<Material *>("Old");
41 Material *new_material = params.extract_input<Material *>("New");
42
43 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
44
45 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
46 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
47 replace_materials({mesh->mat, mesh->totcol}, old_material, new_material);
48 }
49 if (GreasePencil *grease_pencil = geometry_set.get_grease_pencil_for_write()) {
50 replace_materials({grease_pencil->material_array, grease_pencil->material_array_num},
51 old_material,
52 new_material);
53 }
54 });
55
56 params.set_output("Geometry", std::move(geometry_set));
57}
58
59static void node_register()
60{
61 static blender::bke::bNodeType ntype;
62
63 geo_node_type_base(&ntype, "GeometryNodeReplaceMaterial", GEO_NODE_REPLACE_MATERIAL);
64 ntype.ui_name = "Replace Material";
65 ntype.ui_description = "Swap one material with another";
66 ntype.enum_name_legacy = "REPLACE_MATERIAL";
68 ntype.declare = node_declare;
71}
73
74} // namespace blender::nodes::node_geo_material_replace_cc
Low-level operations for grease pencil.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_REPLACE_MATERIAL
#define BLT_I18NCONTEXT_ID_MATERIAL
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr IndexRange index_range() const
Definition BLI_span.hh:670
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void node_geo_exec(GeoNodeExecParams params)
static void replace_materials(MutableSpan< Material * > materials, Material *src_material, Material *dst_material)
static void node_declare(NodeDeclarationBuilder &b)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
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
i
Definition text_draw.cc:230