Blender V4.5
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
12
14{
15 b.use_custom_socket_order();
16 b.allow_any_socket_order();
17 b.add_input<decl::Geometry>("Geometry")
18 .supported_type({GeometryComponent::Type::Mesh, GeometryComponent::Type::GreasePencil});
19 b.add_output<decl::Geometry>("Geometry").propagate_all().align_with_previous();
20 b.add_input<decl::Material>("Old");
21 b.add_input<decl::Material>("New").translation_context(BLT_I18NCONTEXT_ID_MATERIAL);
22}
23
25 Material *src_material,
26 Material *dst_material)
27{
28 for (const int i : materials.index_range()) {
29 if (materials[i] == src_material) {
30 materials[i] = dst_material;
31 }
32 }
33}
34
36{
37 Material *old_material = params.extract_input<Material *>("Old");
38 Material *new_material = params.extract_input<Material *>("New");
39
40 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
41
42 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
43 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
44 replace_materials({mesh->mat, mesh->totcol}, old_material, new_material);
45 }
46 if (GreasePencil *grease_pencil = geometry_set.get_grease_pencil_for_write()) {
47 replace_materials({grease_pencil->material_array, grease_pencil->material_array_num},
48 old_material,
49 new_material);
50 }
51 });
52
53 params.set_output("Geometry", std::move(geometry_set));
54}
55
56static void node_register()
57{
58 static blender::bke::bNodeType ntype;
59
60 geo_node_type_base(&ntype, "GeometryNodeReplaceMaterial", GEO_NODE_REPLACE_MATERIAL);
61 ntype.ui_name = "Replace Material";
62 ntype.ui_description = "Swap one material with another";
63 ntype.enum_name_legacy = "REPLACE_MATERIAL";
65 ntype.declare = node_declare;
68}
70
71} // namespace blender::nodes::node_geo_material_replace_cc
Low-level operations for grease pencil.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#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:2748
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)
void modify_geometry_sets(ForeachSubGeometryCallback callback)
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
i
Definition text_draw.cc:230