Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_geo_separate_geometry.cc « nodes « geometry « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d4da5dda647e504f208f31aee49c65fcee390aff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include "UI_interface.h"
#include "UI_resources.h"

#include "node_geometry_util.hh"

namespace blender::nodes::node_geo_separate_geometry_cc {

NODE_STORAGE_FUNCS(NodeGeometrySeparateGeometry)

static void node_declare(NodeDeclarationBuilder &b)
{
  b.add_input<decl::Geometry>(N_("Geometry"));
  b.add_input<decl::Bool>(N_("Selection"))
      .default_value(true)
      .hide_value()
      .supports_field()
      .description(N_("The parts of the geometry that go into the first output"));
  b.add_output<decl::Geometry>(N_("Selection"))
      .description(N_("The parts of the geometry in the selection"));
  b.add_output<decl::Geometry>(N_("Inverted"))
      .description(N_("The parts of the geometry not in the selection"));
}

static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
{
  uiItemR(layout, ptr, "domain", 0, "", ICON_NONE);
}

static void node_init(bNodeTree * /*tree*/, bNode *node)
{
  NodeGeometrySeparateGeometry *data = MEM_cnew<NodeGeometrySeparateGeometry>(__func__);
  data->domain = ATTR_DOMAIN_POINT;

  node->storage = data;
}

static void node_geo_exec(GeoNodeExecParams params)
{
  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");

  const Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");

  const NodeGeometrySeparateGeometry &storage = node_storage(params.node());
  const eAttrDomain domain = eAttrDomain(storage.domain);

  auto separate_geometry_maybe_recursively = [&](GeometrySet &geometry_set,
                                                 const Field<bool> &selection) {
    bool is_error;
    if (domain == ATTR_DOMAIN_INSTANCE) {
      /* Only delete top level instances. */
      separate_geometry(
          geometry_set, domain, GEO_NODE_DELETE_GEOMETRY_MODE_ALL, selection, is_error);
    }
    else {
      geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
        separate_geometry(
            geometry_set, domain, GEO_NODE_DELETE_GEOMETRY_MODE_ALL, selection, is_error);
      });
    }
  };

  GeometrySet second_set(geometry_set);
  if (params.output_is_required("Selection")) {
    separate_geometry_maybe_recursively(geometry_set, selection_field);
    params.set_output("Selection", std::move(geometry_set));
  }
  if (params.output_is_required("Inverted")) {
    separate_geometry_maybe_recursively(second_set, fn::invert_boolean_field(selection_field));
    params.set_output("Inverted", std::move(second_set));
  }
}

}  // namespace blender::nodes::node_geo_separate_geometry_cc

void register_node_type_geo_separate_geometry()
{
  namespace file_ns = blender::nodes::node_geo_separate_geometry_cc;

  static bNodeType ntype;

  geo_node_type_base(&ntype, GEO_NODE_SEPARATE_GEOMETRY, "Separate Geometry", NODE_CLASS_GEOMETRY);

  node_type_storage(&ntype,
                    "NodeGeometrySeparateGeometry",
                    node_free_standard_storage,
                    node_copy_standard_storage);

  ntype.initfunc = file_ns::node_init;

  ntype.declare = file_ns::node_declare;
  ntype.geometry_node_execute = file_ns::node_geo_exec;
  ntype.draw_buttons = file_ns::node_layout;
  nodeRegisterType(&ntype);
}