Blender V5.3
node_geo_tool_set_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
5#include "BKE_mesh.hh"
7
8#include "FN_field.hh"
11
12#include "NOD_rna_define.hh"
13
15#include "UI_resources.hh"
16
17#include "RNA_enum_types.hh"
18
20
22
23#include "node_geometry_util.hh"
24
26
28enum class SelectionType {
29 Boolean = 0,
30 Float = 1,
31};
32
34{
35 b.use_custom_socket_order();
36 b.allow_any_socket_order();
37 b.add_default_layout();
38 b.add_input<decl::Geometry>("Geometry"_ustr).description("Geometry to update the selection of");
39 b.add_output<decl::Geometry>("Geometry"_ustr).align_with_previous();
40 if (const bNode *node = b.node_or_null()) {
41 switch (SelectionType(node->custom2)) {
42 case SelectionType::Boolean:
43 b.add_input<decl::Bool>("Selection"_ustr).default_value(true).evaluated_geometry_field();
44 break;
45 case SelectionType::Float:
46 b.add_input<decl::Float>("Selection"_ustr).default_value(1.0f).evaluated_geometry_field();
47 break;
48 }
49 }
50}
51
52static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
53{
54 layout.prop(ptr, "domain", UI_ITEM_NONE, "", ICON_NONE);
55 layout.prop(ptr, "selection_type", UI_ITEM_NONE, "", ICON_NONE);
56}
57
58static void node_init(bNodeTree * /*tree*/, bNode *node)
59{
60 node->custom1 = int16_t(AttrDomain::Point);
61 node->custom2 = int16_t(SelectionType::Boolean);
62}
63
64static GField clamp_selection(const GField &selection)
65{
66 if (selection.cpp_type().is<bool>()) {
67 return selection;
68 }
69 static auto clamp = mf::build::SI1_SO<float, float>(
70 "Clamp", [](const float value) { return std::clamp(value, 0.0f, 1.0f); });
71 return Field<float>(FieldOperation::from(clamp, {selection}));
72}
73
74static GField invert_selection(const GField &selection)
75{
76 if (selection.cpp_type().is<bool>()) {
77 static const mf::MultiFunction &invert = fn::multi_function::registry::lookup("!bool"_ustr);
78 return GField(FieldOperation::from(invert, {selection}));
79 }
80
81 static const mf::MultiFunction &invert = fn::multi_function::registry::lookup(
82 "float - float"_ustr);
83 return GField(FieldOperation::from(invert, {fn::Field<float>(1.0f), selection}));
84}
85
92 const StringRef name,
93 const bke::AttrDomain domain)
94{
95 if (const std::optional<bke::AttributeMetaData> meta_data = attributes.lookup_meta_data(name)) {
96 if (meta_data->domain != domain) {
97 attributes.remove(name);
98 }
99 }
100}
101
103{
104 if (!check_tool_context_and_error(params)) {
105 return;
106 }
107 GeometrySet geometry = params.extract_input<GeometrySet>("Geometry"_ustr);
108 const eObjectMode mode = params.user_data()->call_data->operator_data->mode;
111 "Selection control is not supported in this mode");
112 params.set_output("Geometry"_ustr, std::move(geometry));
113 return;
114 }
115
116 const GField selection = params.extract_input<GField>("Selection"_ustr);
117 const AttrDomain domain = AttrDomain(params.node().custom1);
120 if (Mesh *mesh = geometry.get_mesh_for_write()) {
121 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
122 remove_with_wrong_domain(attributes, ".select_vert", AttrDomain::Point);
123 remove_with_wrong_domain(attributes, ".select_edge", AttrDomain::Edge);
124 remove_with_wrong_domain(attributes, ".select_poly", AttrDomain::Face);
125 switch (mode) {
126 case OB_MODE_EDIT: {
127 const Field<bool> field = *conversions.try_convert<bool>(selection);
128 switch (domain) {
129 case AttrDomain::Point:
131 ".select_vert",
132 AttrDomain::Point,
133 field);
135 break;
136 case AttrDomain::Edge:
138 ".select_edge",
139 AttrDomain::Edge,
140 field);
142 break;
143 case AttrDomain::Face:
145 ".select_poly",
146 AttrDomain::Face,
147 field);
149 break;
150 default: {
151 break;
152 }
153 }
154 break;
155 }
156 case OB_MODE_SCULPT: {
157 GField on_domain = GField::from_input<bke::EvaluateOnDomainInput>(selection, domain);
158 GField clamped_and_inverted = invert_selection(clamp_selection(std::move(on_domain)));
159 const Field<float> field = *conversions.try_convert<float>(
160 std::move(clamped_and_inverted));
162 ".sculpt_mask",
163 AttrDomain::Point,
164 field);
165 break;
166 }
167 default: {
168 break;
169 }
170 }
171 }
172 if (geometry.has_curves()) {
173 const GField field = clamp_selection(selection);
174 if (ELEM(domain, AttrDomain::Point, AttrDomain::Curve)) {
176 geometry.get_component_for_write<CurveComponent>(), ".selection", domain, field);
177 }
178 }
179 if (geometry.has_pointcloud()) {
180 const GField field = clamp_selection(selection);
181 if (domain == AttrDomain::Point) {
183 geometry.get_component_for_write<PointCloudComponent>(), ".selection", domain, field);
184 }
185 }
186 if (geometry.has_grease_pencil()) {
187 /* Grease Pencil only supports boolean selection. */
188 const Field<bool> field = *conversions.try_convert<bool>(selection);
189 if (ELEM(domain, AttrDomain::Point, AttrDomain::Curve)) {
191 geometry.get_component_for_write<GreasePencilComponent>(),
192 ".selection",
193 domain,
194 field);
195 }
196 }
197 });
198 params.set_output("Geometry"_ustr, std::move(geometry));
199}
200
201static void node_rna(StructRNA *srna)
202{
204 "domain",
205 "Domain",
206 "",
209 int(AttrDomain::Point));
210 static EnumPropertyItem mode_items[] = {
211 {int(SelectionType::Boolean),
212 "BOOLEAN",
213 0,
214 "Boolean",
215 "Store true or false selection values in edit mode"},
216 {int(SelectionType::Float),
217 "FLOAT",
218 0,
219 "Float",
220 "Store floating point selection values. For mesh geometry, stored inverted as the sculpt "
221 "mode mask"},
222 {0, nullptr, 0, nullptr, nullptr},
223 };
225 "selection_type",
226 "Selection Type",
227 "",
228 mode_items,
230 int(SelectionType::Boolean));
231}
232
233static void node_register()
234{
235 static bke::bNodeType ntype;
236 geo_node_type_base(&ntype, "GeometryNodeToolSetSelection"_ustr, GEO_NODE_TOOL_SET_SELECTION);
237 ntype.ui_name = "Set Selection";
238 ntype.ui_description = "Set selection of the edited geometry, for tool execution";
239 ntype.enum_name_legacy = "TOOL_SELECTION_SET";
241 ntype.declare = node_declare;
242 ntype.initfunc = node_init;
247
248 node_rna(ntype.rna_ext.srna);
249}
251
252} // namespace blender::nodes::node_geo_tool_set_selection_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_TOOL_SET_SELECTION
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define ELEM(...)
bool is() const
std::optional< AttributeMetaData > lookup_meta_data(StringRef name) const
GVArray try_convert(GVArray varray, const CPPType &to_type) const
const CPPType & cpp_type() const
Definition FN_field.hh:518
static FieldOperationPtr from(std::shared_ptr< const mf::MultiFunction > fn, Vector< GField > inputs)
Definition field.cc:252
static GField from_input(Args &&...args)
Definition FN_field.hh:482
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
GeoNodesUserData * user_data() const
void set_output(const UString identifier, T &&value)
constexpr T clamp(T, U, U) RET
float2x2 invert(float2x2 mat)
void mesh_select_vert_flush(Mesh &mesh)
bool try_capture_field_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, const StringRef name, AttrDomain domain, const fn::Field< bool > &selection, const fn::GField &field)
const DataTypeConversions & get_implicit_type_conversions()
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void mesh_select_edge_flush(Mesh &mesh)
void mesh_select_face_flush(Mesh &mesh)
const MultiFunction & lookup(UString id)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void node_declare(NodeDeclarationBuilder &b)
static void remove_with_wrong_domain(bke::MutableAttributeAccessor attributes, const StringRef name, const bke::AttrDomain domain)
static GField invert_selection(const GField &selection)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static GField clamp_selection(const GField &selection)
void search_link_ops_for_tool_node(GatherLinkSearchOpParams &params)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
bool check_tool_context_and_error(GeoNodeExecParams &params)
@ OB_MODE_PAINT_GREASE_PENCIL
const EnumPropertyItem rna_enum_attribute_domain_point_edge_face_curve_items[]
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed short int16_t
Definition BKE_text.h:77
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
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
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)