Blender V4.5
operators.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include "BKE_attribute.hh"
11#include "BKE_context.hh"
12#include "BKE_lib_id.hh"
13
14#include "ED_pointcloud.hh"
15#include "ED_screen.hh"
16#include "ED_select_utils.hh"
17
20
21#include "DEG_depsgraph.hh"
22
23#include "RNA_access.hh"
24#include "RNA_define.hh"
25
26#include "UI_interface.hh"
27
28#include "WM_api.hh"
29
31
32static bool object_has_editable_pointcloud(const Main &bmain, const Object &object)
33{
34 if (object.type != OB_POINTCLOUD) {
35 return false;
36 }
37 if (object.mode != OB_MODE_EDIT) {
38 return false;
39 }
40 if (!BKE_id_is_editable(&bmain, static_cast<const ID *>(object.data))) {
41 return false;
42 }
43 return true;
44}
45
47 const bool check_editable,
48 const bool check_edit_mode)
49{
51 if (object == nullptr || object->type != OB_POINTCLOUD) {
52 return false;
53 }
54 if (check_editable) {
56 return false;
57 }
58 }
59 if (check_edit_mode) {
60 if ((object->mode & OB_MODE_EDIT) == 0) {
61 return false;
62 }
63 }
64 return true;
65}
66
68{
69 return pointcloud_poll_impl(C, false, false);
70}
71
76
78{
79 VectorSet<PointCloud *> unique_points;
80
81 const Main &bmain = *CTX_data_main(&C);
82
84 if (object && object_has_editable_pointcloud(bmain, *object)) {
85 unique_points.add_new(static_cast<PointCloud *>(object->data));
86 }
87
88 CTX_DATA_BEGIN (&C, Object *, object, selected_objects) {
89 if (object_has_editable_pointcloud(bmain, *object)) {
90 unique_points.add(static_cast<PointCloud *>(object->data));
91 }
92 }
94
95 return unique_points;
96}
97
98static bool has_anything_selected(const Span<PointCloud *> pointclouds)
99{
100 return std::any_of(pointclouds.begin(), pointclouds.end(), [](const PointCloud *pointcloud) {
101 return has_anything_selected(*pointcloud);
102 });
103}
104
106{
107 int action = RNA_enum_get(op->ptr, "action");
108
110
111 if (action == SEL_TOGGLE) {
112 action = has_anything_selected(unique_pointcloud) ? SEL_DESELECT : SEL_SELECT;
113 }
114
115 for (PointCloud *pointcloud : unique_pointcloud) {
116 /* (De)select all the curves. */
117 select_all(*pointcloud, action);
118
119 /* Use #ID_RECALC_GEOMETRY instead of #ID_RECALC_SELECT because it is handled as a generic
120 * attribute for now. */
123 }
124
125 return OPERATOR_FINISHED;
126}
127
129{
130 ot->name = "(De)select All";
131 ot->idname = "POINTCLOUD_OT_select_all";
132 ot->description = "(De)select all point cloud";
133
134 ot->exec = select_all_exec;
136
138
140}
141
143{
144 const int seed = RNA_int_get(op->ptr, "seed");
145 const float probability = RNA_float_get(op->ptr, "probability");
146
148 IndexMaskMemory memory;
149 const IndexMask inv_random_elements = random_mask(
150 pointcloud->totpoint, seed, probability, memory)
151 .complement(IndexRange(pointcloud->totpoint),
152 memory);
153 const bool was_anything_selected = has_anything_selected(*pointcloud);
155 if (!was_anything_selected) {
157 }
158
159 pointcloud::fill_selection_false(selection.span, inv_random_elements);
160 selection.finish();
161
162 /* Use #ID_RECALC_GEOMETRY instead of #ID_RECALC_SELECT because it is handled as a generic
163 * attribute for now. */
166 }
167 return OPERATOR_FINISHED;
168}
169
170static void select_random_ui(bContext * /*C*/, wmOperator *op)
171{
172 uiLayout *layout = op->layout;
173
174 layout->prop(op->ptr, "seed", UI_ITEM_NONE, std::nullopt, ICON_NONE);
175 layout->prop(op->ptr, "probability", UI_ITEM_R_SLIDER, std::nullopt, ICON_NONE);
176}
177
179{
180 ot->name = "Select Random";
181 ot->idname = __func__;
182 ot->description = "Randomizes existing selection or create new random selection";
183
184 ot->exec = select_random_exec;
186 ot->ui = select_random_ui;
187
189
190 RNA_def_int(ot->srna,
191 "seed",
192 0,
193 INT32_MIN,
194 INT32_MAX,
195 "Seed",
196 "Source of randomness",
197 INT32_MIN,
198 INT32_MAX);
199 RNA_def_float(ot->srna,
200 "probability",
201 0.5f,
202 0.0f,
203 1.0f,
204 "Probability",
205 "Chance of every point being included in the selection",
206 0.0f,
207 1.0f);
208}
209
211
223
224} // namespace pointcloud_delete
225
227{
228 ot->name = "Delete";
229 ot->idname = __func__;
230 ot->description = "Remove selected points";
231
234
236}
237
247
249{
251 wmOperatorTypeMacro *otmacro;
252
253 ot = WM_operatortype_append_macro("POINTCLOUD_OT_duplicate_move",
254 "Duplicate",
255 "Make copies of selected elements and move them",
257 WM_operatortype_macro_define(ot, "POINTCLOUD_OT_duplicate");
258 otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
259 RNA_boolean_set(otmacro->ptr, "use_proportional_edit", false);
260 RNA_boolean_set(otmacro->ptr, "mirror", false);
261}
262
264{
265 /* Only set in editmode point cloud, by space_view3d listener. */
266 wmKeyMap *keymap = WM_keymap_ensure(keyconf, "Point Cloud", SPACE_EMPTY, RGN_TYPE_WINDOW);
268}
269
270} // namespace blender::ed::pointcloud
#define CTX_DATA_BEGIN(C, Type, instance, member)
Object * CTX_data_active_object(const bContext *C)
Main * CTX_data_main(const bContext *C)
#define CTX_DATA_END
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2503
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:982
@ OB_MODE_EDIT
@ OB_POINTCLOUD
@ RGN_TYPE_WINDOW
@ SPACE_EMPTY
@ OPERATOR_FINISHED
bool ED_operator_object_active_editable_ex(bContext *C, const Object *ob)
@ SEL_SELECT
@ SEL_DESELECT
@ SEL_TOGGLE
#define C
Definition RandGen.cpp:29
@ UI_ITEM_R_SLIDER
#define UI_ITEM_NONE
#define NC_GEOM
Definition WM_types.hh:390
#define ND_DATA
Definition WM_types.hh:506
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
BMesh const char void * data
static unsigned long seed
Definition btSoftBody.h:39
constexpr const T * end() const
Definition BLI_span.hh:224
constexpr const T * begin() const
Definition BLI_span.hh:220
bool add(const Key &key)
void add_new(const Key &key)
IndexMask complement(const IndexMask &universe, IndexMaskMemory &memory) const
#define INT32_MAX
#define INT32_MIN
static wmOperatorStatus delete_exec(bContext *C, wmOperator *)
Definition operators.cc:212
static wmOperatorStatus select_all_exec(bContext *C, wmOperator *op)
Definition operators.cc:105
bool editable_pointcloud_in_edit_mode_poll(bContext *C)
Definition operators.cc:72
static wmOperatorStatus select_random_exec(bContext *C, wmOperator *op)
Definition operators.cc:142
static void POINTCLOUD_OT_delete(wmOperatorType *ot)
Definition operators.cc:226
static void POINTCLOUD_OT_select_random(wmOperatorType *ot)
Definition operators.cc:178
void fill_selection_true(GMutableSpan span)
Definition selection.cc:136
static bool editable_pointcloud_poll(bContext *C)
Definition operators.cc:67
bool remove_selection(PointCloud &pointcloud)
Definition edit.cc:32
static void select_random_ui(bContext *, wmOperator *op)
Definition operators.cc:170
void POINTCLOUD_OT_duplicate(wmOperatorType *ot)
Definition duplicate.cc:60
static bool pointcloud_poll_impl(bContext *C, const bool check_editable, const bool check_edit_mode)
Definition operators.cc:46
void POINTCLOUD_OT_attribute_set(wmOperatorType *ot)
void keymap_pointcloud(wmKeyConfig *keyconf)
Definition operators.cc:263
bke::GSpanAttributeWriter ensure_selection_attribute(PointCloud &pointcloud, eCustomDataType create_type)
Definition selection.cc:96
void fill_selection_false(GMutableSpan selection, const IndexMask &mask)
Definition selection.cc:126
static bool object_has_editable_pointcloud(const Main &bmain, const Object &object)
Definition operators.cc:32
bool has_anything_selected(const PointCloud &pointcloud)
Definition selection.cc:90
void select_all(PointCloud &pointcloud, int action)
Definition selection.cc:192
VectorSet< PointCloud * > get_unique_editable_pointclouds(const bContext &C)
Definition operators.cc:77
static void POINTCLOUD_OT_select_all(wmOperatorType *ot)
Definition operators.cc:128
void POINTCLOUD_OT_separate(wmOperatorType *ot)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
int RNA_int_get(PointerRNA *ptr, const char *name)
float RNA_float_get(PointerRNA *ptr, const char *name)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
Definition DNA_ID.h:404
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
bool(* poll)(struct bContext *)
struct uiLayout * layout
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition wm_files.cc:4226
wmKeyMap * WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
Definition wm_keymap.cc:893
void WM_operator_properties_select_all(wmOperatorType *ot)
wmOperatorTypeMacro * WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
wmOperatorType * WM_operatortype_append_macro(const char *idname, const char *name, const char *description, int flag)