Blender  V3.3
node_geo_flip_faces.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include "BKE_mesh.h"
4 #include "BKE_mesh_runtime.h"
5 
6 #include "DNA_mesh_types.h"
7 #include "DNA_meshdata_types.h"
8 
9 #include "BKE_attribute_math.hh"
10 
11 #include "node_geometry_util.hh"
12 
14 
16 {
17  b.add_input<decl::Geometry>(N_("Mesh")).supported_type(GEO_COMPONENT_TYPE_MESH);
18  b.add_input<decl::Bool>(N_("Selection")).default_value(true).hide_value().supports_field();
19  b.add_output<decl::Geometry>(N_("Mesh"));
20 }
21 
22 static void mesh_flip_faces(MeshComponent &component, const Field<bool> &selection_field)
23 {
25  const int domain_size = component.attribute_domain_size(ATTR_DOMAIN_FACE);
26  if (domain_size == 0) {
27  return;
28  }
29  fn::FieldEvaluator evaluator{field_context, domain_size};
30  evaluator.add(selection_field);
31  evaluator.evaluate();
32  const IndexMask selection = evaluator.get_evaluated_as_mask(0);
33 
34  Mesh *mesh = component.get_for_write();
35 
38  Span<MPoly> polys{mesh->mpoly, mesh->totpoly};
40 
41  for (const int i : selection.index_range()) {
42  const MPoly &poly = polys[selection[i]];
43  int start = poly.loopstart;
44  for (const int j : IndexRange(poly.totloop / 2)) {
45  const int index1 = start + j + 1;
46  const int index2 = start + poly.totloop - j - 1;
47  std::swap(loops[index1].v, loops[index2].v);
48  std::swap(loops[index1 - 1].e, loops[index2].e);
49  }
50  }
51 
52  MutableAttributeAccessor attributes = *component.attributes_for_write();
53  attributes.for_all(
54  [&](const bke::AttributeIDRef &attribute_id, const AttributeMetaData &meta_data) {
55  if (meta_data.domain == ATTR_DOMAIN_CORNER) {
56  GSpanAttributeWriter attribute = attributes.lookup_or_add_for_write_span(
57  attribute_id, ATTR_DOMAIN_CORNER, meta_data.data_type);
58  attribute_math::convert_to_static_type(meta_data.data_type, [&](auto dummy) {
59  using T = decltype(dummy);
60  MutableSpan<T> dst_span = attribute.span.typed<T>();
61  for (const int j : selection.index_range()) {
62  const MPoly &poly = polys[selection[j]];
63  dst_span.slice(poly.loopstart + 1, poly.totloop - 1).reverse();
64  }
65  });
66  attribute.finish();
67  }
68  return true;
69  });
70 }
71 
73 {
74  GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");
75 
76  const Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
77 
78  geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
79  if (!geometry_set.has_mesh()) {
80  return;
81  }
82  MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
83  mesh_flip_faces(mesh_component, selection_field);
84  });
85 
86  params.set_output("Mesh", std::move(geometry_set));
87 }
88 
89 } // namespace blender::nodes::node_geo_flip_faces_cc
90 
92 {
93  namespace file_ns = blender::nodes::node_geo_flip_faces_cc;
94 
95  static bNodeType ntype;
96 
100  nodeRegisterType(&ntype);
101 }
@ ATTR_DOMAIN_FACE
Definition: BKE_attribute.h:29
@ ATTR_DOMAIN_CORNER
Definition: BKE_attribute.h:30
void * CustomData_duplicate_referenced_layer(struct CustomData *data, int type, int totelem)
Definition: customdata.cc:2976
@ GEO_COMPONENT_TYPE_MESH
#define NODE_CLASS_GEOMETRY
Definition: BKE_node.h:359
#define GEO_NODE_FLIP_FACES
Definition: BKE_node.h:1491
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
static uint8 component(Color32 c, uint i)
Definition: ColorBlock.cpp:108
void swap(T &a, T &b)
Definition: Common.h:19
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a or normal between and object coordinate space Combine Create a color from its and value channels Color Retrieve a color attribute
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
IndexRange index_range() const
bool for_all(const AttributeForeachCallback fn) const
int add(GField field, GVArray *varray_ptr)
Definition: field.cc:731
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void mesh_flip_faces(MeshComponent &component, const Field< bool > &selection_field)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_geo_flip_faces()
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
struct MLoop * mloop
int totpoly
int totloop
struct MPoly * mpoly
CustomData ldata
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)