Blender V5.3
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"
9#include "DNA_volume_types.h"
10
12
13#include "BKE_grease_pencil.hh"
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,
27 .description("Geometry to replace materials on");
28 b.add_output<decl::Geometry>("Geometry"_ustr).propagate_all_geometry().align_with_previous();
29 b.add_input<decl::Material>("Old"_ustr);
30 b.add_input<decl::Material>("New"_ustr).translation_context(BLT_I18NCONTEXT_ID_MATERIAL);
31}
32
34 Material *src_material,
35 Material *dst_material)
36{
37 for (const int i : materials.index_range()) {
38 if (materials[i] == src_material) {
39 materials[i] = dst_material;
40 }
41 }
42}
43
45{
46 Material *old_material = params.extract_input<Material *>("Old"_ustr);
47 Material *new_material = params.extract_input<Material *>("New"_ustr);
48
49 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry"_ustr);
50
51 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
52 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
53 replace_materials({mesh->mat, mesh->totcol}, old_material, new_material);
54 }
55 if (Volume *volume = geometry_set.get_volume_for_write()) {
56 replace_materials({volume->mat, volume->totcol}, old_material, new_material);
57 }
58 if (PointCloud *pointcloud = geometry_set.get_pointcloud_for_write()) {
59 replace_materials({pointcloud->mat, pointcloud->totcol}, old_material, new_material);
60 }
61 if (Curves *curves = geometry_set.get_curves_for_write()) {
62 replace_materials({curves->mat, curves->totcol}, old_material, new_material);
63 }
64 if (GreasePencil *grease_pencil = geometry_set.get_grease_pencil_for_write()) {
65 replace_materials({grease_pencil->material_array, grease_pencil->material_array_num},
66 old_material,
67 new_material);
68 }
69 });
70
71 params.set_output("Geometry"_ustr, std::move(geometry_set));
72}
73
74static void node_register()
75{
76 static bke::bNodeType ntype;
77
78 geo_node_type_base(&ntype, "GeometryNodeReplaceMaterial"_ustr, GEO_NODE_REPLACE_MATERIAL);
79 ntype.ui_name = "Replace Material";
80 ntype.ui_description = "Swap one material with another";
81 ntype.enum_name_legacy = "REPLACE_MATERIAL";
83 ntype.declare = node_declare;
86}
88
89} // namespace blender::nodes::node_geo_material_replace_cc
Low-level operations for grease pencil.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#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:676
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)
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(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