Blender  V3.3
node_geo_set_material_index.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 
6 
8 {
9  b.add_input<decl::Geometry>(N_("Geometry")).supported_type(GEO_COMPONENT_TYPE_MESH);
10  b.add_input<decl::Bool>(N_("Selection")).default_value(true).hide_value().supports_field();
11  b.add_input<decl::Int>(N_("Material Index")).supports_field().min(0);
12  b.add_output<decl::Geometry>(N_("Geometry"));
13 }
14 
16  const Field<bool> &selection_field,
17  const Field<int> &index_field)
18 {
19  const int domain_size = component.attribute_domain_size(ATTR_DOMAIN_FACE);
20  if (domain_size == 0) {
21  return;
22  }
23  MutableAttributeAccessor attributes = *component.attributes_for_write();
25 
26  AttributeWriter<int> indices = attributes.lookup_or_add_for_write<int>("material_index",
28 
29  fn::FieldEvaluator evaluator{field_context, domain_size};
30  evaluator.set_selection(selection_field);
31  evaluator.add_with_destination(index_field, indices.varray);
32  evaluator.evaluate();
33  indices.finish();
34 }
35 
37 {
38  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
39  Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
40  Field<int> index_field = params.extract_input<Field<int>>("Material Index");
41 
42  geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
43  if (geometry_set.has_mesh()) {
45  geometry_set.get_component_for_write<MeshComponent>(), selection_field, index_field);
46  }
47  });
48  params.set_output("Geometry", std::move(geometry_set));
49 }
50 
51 } // namespace blender::nodes::node_geo_set_material_index_cc
52 
54 {
56 
57  static bNodeType ntype;
58 
60  &ntype, GEO_NODE_SET_MATERIAL_INDEX, "Set Material Index", NODE_CLASS_GEOMETRY);
63  nodeRegisterType(&ntype);
64 }
@ ATTR_DOMAIN_FACE
Definition: BKE_attribute.h:29
@ GEO_COMPONENT_TYPE_MESH
#define NODE_CLASS_GEOMETRY
Definition: BKE_node.h:359
#define GEO_NODE_SET_MATERIAL_INDEX
Definition: BKE_node.h:1461
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
static uint8 component(Color32 c, uint i)
Definition: ColorBlock.cpp:108
GAttributeWriter lookup_or_add_for_write(const AttributeIDRef &attribute_id, const eAttrDomain domain, const eCustomDataType data_type, const AttributeInit &initializer=AttributeInitDefault())
void set_selection(Field< bool > selection)
Definition: FN_field.hh:366
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_gpu_kernel_postfix int ccl_global int * indices
static void node_declare(NodeDeclarationBuilder &b)
static void set_material_index_in_component(GeometryComponent &component, const Field< bool > &selection_field, const Field< int > &index_field)
static void node_geo_exec(GeoNodeExecParams params)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_geo_set_material_index()
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
GeometryComponent & get_component_for_write(GeometryComponentType component_type)
void modify_geometry_sets(ForeachSubGeometryCallback callback)
bool has_mesh() const
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)