Blender  V3.3
node_geo_set_id.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"));
10  b.add_input<decl::Bool>(N_("Selection")).default_value(true).hide_value().supports_field();
11  b.add_input<decl::Int>(N_("ID")).implicit_field();
12  b.add_output<decl::Geometry>(N_("Geometry"));
13 }
14 
16  const Field<bool> &selection_field,
17  const Field<int> &id_field)
18 {
19  const eAttrDomain domain = (component.type() == GEO_COMPONENT_TYPE_INSTANCES) ?
22  const int domain_size = component.attribute_domain_size(domain);
23  if (domain_size == 0) {
24  return;
25  }
26  MutableAttributeAccessor attributes = *component.attributes_for_write();
27  GeometryComponentFieldContext field_context{component, domain};
28 
29  fn::FieldEvaluator evaluator{field_context, domain_size};
30  evaluator.set_selection(selection_field);
31 
32  /* Since adding the ID attribute can change the result of the field evaluation (the random value
33  * node uses the index if the ID is unavailable), make sure that it isn't added before evaluating
34  * the field. However, as an optimization, use a faster code path when it already exists. */
35  if (attributes.contains("id")) {
36  AttributeWriter<int> id_attribute = attributes.lookup_or_add_for_write<int>("id", domain);
37  evaluator.add_with_destination(id_field, id_attribute.varray);
38  evaluator.evaluate();
39  id_attribute.finish();
40  }
41  else {
42  evaluator.add(id_field);
43  evaluator.evaluate();
44  const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
45  const VArray<int> result_ids = evaluator.get_evaluated<int>(0);
46  SpanAttributeWriter<int> id_attribute = attributes.lookup_or_add_for_write_span<int>("id",
47  domain);
48  result_ids.materialize(selection, id_attribute.span);
49  id_attribute.finish();
50  }
51 }
52 
54 {
55  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
56  Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
57  Field<int> id_field = params.extract_input<Field<int>>("ID");
58 
63  if (geometry_set.has(type)) {
64  set_id_in_component(geometry_set.get_component_for_write(type), selection_field, id_field);
65  }
66  }
67 
68  params.set_output("Geometry", std::move(geometry_set));
69 }
70 
71 } // namespace blender::nodes::node_geo_set_id_cc
72 
74 {
75  namespace file_ns = blender::nodes::node_geo_set_id_cc;
76 
77  static bNodeType ntype;
78 
82  nodeRegisterType(&ntype);
83 }
eAttrDomain
Definition: BKE_attribute.h:25
@ ATTR_DOMAIN_INSTANCE
Definition: BKE_attribute.h:32
@ ATTR_DOMAIN_POINT
Definition: BKE_attribute.h:27
GeometryComponentType
@ GEO_COMPONENT_TYPE_MESH
@ GEO_COMPONENT_TYPE_POINT_CLOUD
@ GEO_COMPONENT_TYPE_INSTANCES
@ GEO_COMPONENT_TYPE_CURVE
#define NODE_CLASS_GEOMETRY
Definition: BKE_node.h:359
#define GEO_NODE_SET_ID
Definition: BKE_node.h:1476
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
static uint8 component(Color32 c, uint i)
Definition: ColorBlock.cpp:108
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
void materialize(MutableSpan< T > r_span) const
bool contains(const AttributeIDRef &attribute_id) const
GAttributeWriter lookup_or_add_for_write(const AttributeIDRef &attribute_id, const eAttrDomain domain, const eCustomDataType data_type, const AttributeInit &initializer=AttributeInitDefault())
GSpanAttributeWriter lookup_or_add_for_write_span(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]
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
static void set_id_in_component(GeometryComponent &component, const Field< bool > &selection_field, const Field< int > &id_field)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_geo_set_id()
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
GeometryComponent & get_component_for_write(GeometryComponentType component_type)
bool has(const GeometryComponentType component_type) 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)