Blender  V3.3
node_geo_set_material.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include "node_geometry_util.hh"
4 
5 #include "UI_interface.h"
6 #include "UI_resources.h"
7 
8 #include "DNA_curves_types.h"
9 #include "DNA_mesh_types.h"
10 #include "DNA_meshdata_types.h"
11 #include "DNA_pointcloud_types.h"
12 #include "DNA_volume_types.h"
13 
14 #include "BKE_material.h"
15 
17 
19 {
20  b.add_input<decl::Geometry>(N_("Geometry"))
21  .supported_type({GEO_COMPONENT_TYPE_MESH,
25  b.add_input<decl::Bool>(N_("Selection")).default_value(true).hide_value().supports_field();
26  b.add_input<decl::Material>(N_("Material")).hide_label();
27  b.add_output<decl::Geometry>(N_("Geometry"));
28 }
29 
30 static void assign_material_to_faces(Mesh &mesh, const IndexMask selection, Material *material)
31 {
32  if (selection.size() != mesh.totpoly) {
33  /* If the entire mesh isn't selected, and there is no material slot yet, add an empty
34  * slot so that the faces that aren't selected can still refer to the default material. */
36  }
37 
38  int new_material_index = -1;
39  for (const int i : IndexRange(mesh.totcol)) {
40  Material *other_material = mesh.mat[i];
41  if (other_material == material) {
42  new_material_index = i;
43  break;
44  }
45  }
46  if (new_material_index == -1) {
47  /* Append a new material index. */
48  new_material_index = mesh.totcol;
49  BKE_id_material_eval_assign(&mesh.id, new_material_index + 1, material);
50  }
51 
53  for (const int i : selection) {
54  MPoly &poly = mesh.mpoly[i];
55  poly.mat_nr = new_material_index;
56  }
57 }
58 
60 {
61  Material *material = params.extract_input<Material *>("Material");
62  const Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
63 
64  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
65 
66  /* Only add the warnings once, even if there are many unique instances. */
67  bool point_selection_warning = false;
68  bool volume_selection_warning = false;
69  bool curves_selection_warning = false;
70 
71  geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
72  if (geometry_set.has_mesh()) {
73  MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
74  Mesh &mesh = *mesh_component.get_for_write();
75  GeometryComponentFieldContext field_context{mesh_component, ATTR_DOMAIN_FACE};
76 
77  fn::FieldEvaluator selection_evaluator{field_context, mesh.totpoly};
78  selection_evaluator.add(selection_field);
79  selection_evaluator.evaluate();
80  const IndexMask selection = selection_evaluator.get_evaluated_as_mask(0);
81 
83  }
84  if (Volume *volume = geometry_set.get_volume_for_write()) {
85  BKE_id_material_eval_assign(&volume->id, 1, material);
86  if (selection_field.node().depends_on_input()) {
87  volume_selection_warning = true;
88  }
89  }
90  if (PointCloud *pointcloud = geometry_set.get_pointcloud_for_write()) {
91  BKE_id_material_eval_assign(&pointcloud->id, 1, material);
92  if (selection_field.node().depends_on_input()) {
93  point_selection_warning = true;
94  }
95  }
96  if (Curves *curves = geometry_set.get_curves_for_write()) {
98  if (selection_field.node().depends_on_input()) {
99  curves_selection_warning = true;
100  }
101  }
102  });
103 
104  if (volume_selection_warning) {
105  params.error_message_add(
107  TIP_("Volumes only support a single material; selection input can not be a field"));
108  }
109  if (point_selection_warning) {
110  params.error_message_add(
112  TIP_("Point clouds only support a single material; selection input can not be a field"));
113  }
114  if (curves_selection_warning) {
115  params.error_message_add(
117  TIP_("Curves only support a single material; selection input can not be a field"));
118  }
119 
120  params.set_output("Geometry", std::move(geometry_set));
121 }
122 
123 } // namespace blender::nodes::node_geo_set_material_cc
124 
126 {
127  namespace file_ns = blender::nodes::node_geo_set_material_cc;
128 
129  static bNodeType ntype;
130 
134  nodeRegisterType(&ntype);
135 }
@ ATTR_DOMAIN_FACE
Definition: BKE_attribute.h:29
void * CustomData_duplicate_referenced_layer(struct CustomData *data, int type, int totelem)
Definition: customdata.cc:2976
@ GEO_COMPONENT_TYPE_MESH
@ GEO_COMPONENT_TYPE_POINT_CLOUD
@ GEO_COMPONENT_TYPE_CURVE
@ GEO_COMPONENT_TYPE_VOLUME
General operations, lookup, etc. for materials.
void BKE_id_material_eval_ensure_default_slot(struct ID *id)
Definition: material.c:784
void BKE_id_material_eval_assign(struct ID *id, int slot, struct Material *material)
Definition: material.c:758
#define NODE_CLASS_GEOMETRY
Definition: BKE_node.h:359
#define GEO_NODE_SET_MATERIAL
Definition: BKE_node.h:1424
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
#define TIP_(msgid)
in reality light always falls off quadratically Particle Info
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to curves
int64_t size() const
Material material
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static void assign_material_to_faces(Mesh &mesh, const IndexMask selection, Material *material)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_geo_set_material()
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
short mat_nr
struct Material ** mat
CustomData pdata
int totpoly
short totcol
struct MPoly * mpoly
Defines a node type.
Definition: BKE_node.h:226
NodeGeometryExecFunction geometry_node_execute
Definition: BKE_node.h:316
NodeDeclareFunction declare
Definition: BKE_node.h:324
#define N_(msgid)