Blender V5.3
node_geo_tool_selection.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
8
10
12{
13 b.add_output<decl::Bool>("Boolean"_ustr, "Selection"_ustr)
14 .structure_type(StructureType::Field)
15 .description("The selection of each element as a true or false value");
16 b.add_output<decl::Float>("Float"_ustr)
17 .structure_type(StructureType::Field)
18 .description("The selection of each element as a floating point value");
19}
20
21static const void *true_value(const bke::AttrType data_type)
22{
23 switch (data_type) {
25 static const bool value = true;
26 return &value;
27 }
29 static const float value = 1.0f;
30 return &value;
31 }
32 default: {
34 return nullptr;
35 }
36 }
37}
38
39static const void *false_value(const bke::AttrType data_type)
40{
41 switch (data_type) {
43 static const bool value = false;
44 return &value;
45 }
47 static const float value = 0.0f;
48 return &value;
49 }
50 default: {
52 return nullptr;
53 }
54 }
55}
56
58{
59 switch (domain) {
60 case AttrDomain::Point:
61 return ".select_vert";
62 case AttrDomain::Edge:
63 return ".select_edge";
64 case AttrDomain::Face:
65 case AttrDomain::Corner:
66 return ".select_poly";
67 default:
69 return "";
70 }
71}
72
74 public:
76 : bke::GeometryFieldInput(bke::attribute_type_to_cpp_type(data_type), "Edit Selection")
77 {
78 }
79
81 const IndexMask & /*mask*/) const override
82 {
83 const AttrDomain domain = context.domain();
85 const AttributeAccessor attributes = *context.attributes();
86 switch (context.type()) {
90 return *attributes.lookup_or_default(
91 ".selection", domain, data_type, true_value(data_type));
93 return *attributes.lookup_or_default(
94 mesh_selection_name(domain), domain, data_type, false_value(data_type));
95 default:
96 return {};
97 }
98 }
99};
100
102 public:
104 : bke::GeometryFieldInput(bke::attribute_type_to_cpp_type(data_type), "Sculpt Selection")
105 {
106 }
107
109 const IndexMask &mask) const final
110 {
111 const AttrDomain domain = context.domain();
113 const AttributeAccessor attributes = *context.attributes();
114 switch (context.type()) {
118 return *attributes.lookup_or_default(
119 ".selection", domain, data_type, true_value(data_type));
121 const VArraySpan<float> attribute = *attributes.lookup<float>(".sculpt_mask", domain);
122 if (attribute.is_empty()) {
123 return GVArray::from_single(*type_, mask.min_array_size(), true_value(data_type));
124 }
125 switch (data_type) {
126 case bke::AttrType::Bool: {
127 Array<bool> selection(mask.min_array_size());
128 mask.foreach_index_optimized<int>(
129 [&](const int i) { selection[i] = attribute[i] < 1.0f; },
131 return VArray<bool>::from_container(std::move(selection));
132 }
134 Array<float> selection(mask.min_array_size());
135 mask.foreach_index_optimized<int>(
136 [&](const int i) { selection[i] = 1.0f - attribute[i]; },
138 return VArray<float>::from_container(std::move(selection));
139 }
140 default: {
142 return {};
143 }
144 }
145 }
146 default:
147 return {};
148 }
149 }
150};
151
152static GField get_selection_field(const eObjectMode object_mode, const bke::AttrType data_type)
153{
154 switch (object_mode) {
155 case OB_MODE_OBJECT:
157 true_value(data_type));
158 case OB_MODE_EDIT:
160 case OB_MODE_SCULPT:
166 true_value(data_type));
167 default:
169 false_value(data_type));
170 }
171}
172
174{
175 if (!check_tool_context_and_error(params)) {
176 return;
177 }
178 const eObjectMode mode = params.user_data()->call_data->operator_data->mode;
179 params.set_output("Selection"_ustr, get_selection_field(mode, bke::AttrType::Bool));
180 params.set_output("Float"_ustr, get_selection_field(mode, bke::AttrType::Float));
181}
182
183static void node_register()
184{
185 static bke::bNodeType ntype;
186 geo_node_type_base(&ntype, "GeometryNodeToolSelection"_ustr, GEO_NODE_TOOL_SELECTION);
187 ntype.ui_name = "Selection";
188 ntype.ui_description = "User selection of the edited geometry, for tool execution";
189 ntype.enum_name_legacy = "TOOL_SELECTION";
190 ntype.nclass = NODE_CLASS_INPUT;
191 ntype.declare = node_declare;
195}
197
198} // namespace blender::nodes::node_geo_tool_selection_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_TOOL_SELECTION
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define BLI_assert_unreachable()
static GVArray from_single(const CPPType &type, int64_t size, const void *value)
static VArray from_container(ContainerT container)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GAttributeReader lookup(const StringRef name) const
const CPPType * type_
Definition FN_field.hh:341
static GField from_non_owning_constant(const CPPType &type, const void *value)
Definition FN_field.hh:472
static GField from_input(Args &&...args)
Definition FN_field.hh:482
GeoNodesUserData * user_data() const
void set_output(const UString identifier, T &&value)
GVArray get_varray_for_context(const bke::GeometryFieldContext &context, const IndexMask &) const override
GVArray get_varray_for_context(const bke::GeometryFieldContext &context, const IndexMask &mask) const final
#define attribute(slot)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
AttrType cpp_type_to_attribute_type(const CPPType &type)
const CPPType & attribute_type_to_cpp_type(AttrType type)
constexpr ParallelGrainSize grain_size(int grain_size)
static const void * true_value(const bke::AttrType data_type)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static GField get_selection_field(const eObjectMode object_mode, const bke::AttrType data_type)
static StringRef mesh_selection_name(const AttrDomain domain)
static const void * false_value(const bke::AttrType data_type)
void search_link_ops_for_tool_node(GatherLinkSearchOpParams &params)
bool check_tool_context_and_error(GeoNodeExecParams &params)
@ OB_MODE_SCULPT_GREASE_PENCIL
@ OB_MODE_SCULPT_CURVES
@ OB_MODE_PAINT_GREASE_PENCIL
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393