Blender V5.3
eyedropper_bone.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "DNA_ID.h"
12#include "DNA_layer_types.h"
13
14#include "BKE_armature.hh"
15#include "BKE_context.hh"
16#include "BKE_object.hh"
17#include "BKE_report.hh"
18#include "BKE_screen.hh"
19
20#include "BLI_assert.hh"
21#include "BLI_math_vector_c.hh"
22#include "BLI_string_utf8.hh"
23
24#include "RNA_access.hh"
25#include "RNA_prototypes.hh"
26
27#include "ED_armature.hh"
28#include "ED_outliner.hh"
29#include "ED_screen.hh"
30#include "ED_space_api.hh"
31#include "ED_view3d.hh"
32
33#include "WM_api.hh"
34#include "WM_types.hh"
35
36#include "UI_view2d.hh"
37
38#include "eyedropper_intern.hh"
39#include "interface_intern.hh"
40
41namespace blender::ui {
42
51
55 PropertyRNA *prop = nullptr;
56 /* The property we are looking for. */
59
60 bool is_undo = false;
61
62 ScrArea *cursor_area = nullptr; /* Area under the cursor. */
64 void *draw_handle_pixel = nullptr;
65 int name_pos[2] = {};
66 char name[64] = {};
67};
68
71 /* Either EditBone, bPoseChannel or Bone. */
73 char *name = nullptr;
74};
75
76static void datadropper_draw_cb(const bContext * /*C*/, ARegion * /*region*/, void *arg)
77{
78 BoneDropper *ddr = static_cast<BoneDropper *>(arg);
80}
81
82static bool is_bone_dropper_valid(BoneDropper *bone_dropper)
83{
84 if (!bone_dropper->ptr || (bone_dropper->prop == nullptr)) {
85 return false;
86 }
87 if (!RNA_property_editable(&bone_dropper->ptr, bone_dropper->prop)) {
88 return false;
89 }
90
91 ID *search_id = bone_dropper->search_ptr.owner_id;
92
93 if (GS(search_id->name) == ID_OB) {
94 Object *ob = reinterpret_cast<Object *>(search_id);
95 /* Allows for the eyedropper to work on pose bones. */
96 if (ob->type == OB_ARMATURE && ob->data) {
97 return true;
98 }
99 }
100
101 if (GS(search_id->name) != ID_AR) {
102 return false;
103 }
104
105 return true;
106}
107
109{
110 int index_dummy;
111 PointerRNA button_ptr;
112 PropertyRNA *button_prop;
113 Button *button = context_active_but_prop_get(C, &button_ptr, &button_prop, &index_dummy);
114
115 if (!button || button->type != ButtonType::SearchMenu) {
116 return false;
117 }
118
119 BoneDropper *bone_dropper = MEM_new<BoneDropper>(__func__);
120 ButtonSearch *search_button = static_cast<ButtonSearch *>(button);
121 bone_dropper->ptr = button_ptr;
122 bone_dropper->prop = button_prop;
123 bone_dropper->search_ptr = search_button->rnasearchpoin;
124 bone_dropper->search_prop = search_button->rnasearchprop;
125 if (!is_bone_dropper_valid(bone_dropper)) {
126 MEM_delete(bone_dropper);
127 return false;
128 }
129
130 op->customdata = bone_dropper;
131
132 bone_dropper->is_undo = button_flag_is_set(button, BUT_UNDO);
133
135 ARegionType *area_region_type = BKE_regiontype_from_id(space_type, RGN_TYPE_WINDOW);
136 bone_dropper->cursor_area = CTX_wm_area(C);
137 bone_dropper->area_region_type = area_region_type;
139 area_region_type, datadropper_draw_cb, bone_dropper, REGION_DRAW_POST_PIXEL);
140
141 return true;
142}
143
145{
146 wmWindow *win = CTX_wm_window(C);
148
149 if (op->customdata) {
150 BoneDropper *bdr = static_cast<BoneDropper *>(op->customdata);
151 op->customdata = nullptr;
152
153 if (bdr->area_region_type) {
155 }
157
158 MEM_delete(bdr);
159 }
161}
162
164{
165 bonedropper_exit(C, op);
166}
167
168/* To switch the draw callback when region under mouse event changes */
170{
171 if (area.spacetype == bdr.cursor_area->spacetype) {
172 return;
173 }
174
175 /* If the spacetype changed remove the old callback. */
177
179 ED_region_tag_redraw(region);
180
181 /* Set draw callback in new region. */
183
184 bdr.cursor_area = &area;
185 bdr.area_region_type = art;
188}
189
191 const int mval[2],
192 const BoneDropper &bdr)
193{
194 Base *base = nullptr;
195
196 switch (CTX_data_mode_enum(C)) {
197 case CTX_MODE_POSE: {
198 bPoseChannel *bone = ED_armature_pick_pchan(C, mval, true, &base);
199 if (!bone || !base) {
201 }
202 Object *ob = base->object;
203 bArmature *armature = id_cast<bArmature *>(ob->data);
204 if (bdr.search_ptr.type == RNA_Pose && &ob->id != bdr.search_ptr.owner_id) {
206 }
207 if (bdr.search_ptr.type == RNA_Armature &&
208 (!armature || &armature->id != bdr.search_ptr.owner_id))
209 {
211 }
212
213 BoneSampleData sample_data;
214 sample_data.name = bone->name;
215 /* Not using the search pointer owner ID because pose bones are part of the object. */
216 sample_data.bone_rna = RNA_pointer_create_discrete(&base->object->id, RNA_PoseBone, bone);
218 return sample_data;
219 }
220
222 EditBone *ebone = ED_armature_pick_ebone(C, mval, true, &base);
223 if (!ebone || !base) {
225 }
226 Object *ob = base->object;
227 bArmature *armature = id_cast<bArmature *>(ob->data);
228 if (!armature || &armature->id != bdr.search_ptr.owner_id) {
230 }
231
232 BoneSampleData sample_data;
233 sample_data.name = ebone->name;
234 sample_data.bone_rna = RNA_pointer_create_discrete(&armature->id, RNA_EditBone, ebone);
236 return sample_data;
237 }
238
239 default:
241 }
242}
243
245 const int mval[2],
246 const BoneDropper &bdr)
247{
248 BoneSampleData sample_data;
249
250 const bool success = ED_outliner_give_rna_under_cursor(C, mval, &sample_data.bone_rna);
251 if (!success) {
253 return sample_data;
254 }
255 ID *bone_id = sample_data.bone_rna.owner_id;
256 ID *search_id = bdr.search_ptr.owner_id;
257
258 /* By comparing the ID of the RNA returned by the outliner with the ID we are searching in, we
259 * can determine if the Bone is for the correct armature. */
260 if (sample_data.bone_rna.type == RNA_Bone) {
261 if (bone_id != search_id) {
263 return sample_data;
264 }
265 Bone *bone = static_cast<Bone *>(sample_data.bone_rna.data);
266 sample_data.name = bone->name;
268 return sample_data;
269 }
270
271 if (sample_data.bone_rna.type == RNA_EditBone) {
272 if (bone_id != search_id) {
274 return sample_data;
275 }
276 EditBone *bone = static_cast<EditBone *>(sample_data.bone_rna.data);
277 sample_data.name = bone->name;
279 return sample_data;
280 }
281
282 if (sample_data.bone_rna.type == RNA_PoseBone) {
283 bPoseChannel *pose_bone = static_cast<bPoseChannel *>(sample_data.bone_rna.data);
284 /* Special case for pose bones. Because they are not stored in the Armature, the IDs of the
285 * search property and the picked result might not match since the comparison would be between
286 * armature and object. */
287 if (bdr.search_ptr.type == RNA_Object) {
288 if (bone_id != search_id) {
290 return sample_data;
291 }
292 }
293 /* If looking for an armature, get the Armature object and follow the data pointer. */
294 if (bdr.search_ptr.type == RNA_Armature) {
295 /* Expecting Pose Bones to be stored on the object. */
296 BLI_assert(GS(sample_data.bone_rna.owner_id->name) == ID_OB);
297 Object *armature_object = id_cast<Object *>(sample_data.bone_rna.owner_id);
298 if (armature_object->data != bdr.search_ptr.owner_id) {
300 return sample_data;
301 }
302 }
303 sample_data.name = pose_bone->name;
305 return sample_data;
306 }
307
309 return sample_data;
310}
311
313 bContext *C, wmWindow &win, ScrArea &area, BoneDropper &bdr, const int event_xy[2])
314{
316 return {};
317 }
318
319 ARegion *region = BKE_area_find_region_xy(&area, RGN_TYPE_WINDOW, event_xy);
320
321 if (!region) {
322 return {};
323 }
324
325 wmWindow *win_prev = CTX_wm_window(C);
326 ScrArea *area_prev = CTX_wm_area(C);
327 ARegion *region_prev = CTX_wm_region(C);
328
329 const int mval[2] = {event_xy[0] - region->winrct.xmin, event_xy[1] - region->winrct.ymin};
330
331 CTX_wm_window_set(C, &win);
332 CTX_wm_area_set(C, &area);
333 CTX_wm_region_set(C, region);
334
335 /* Unfortunately it's necessary to always draw else we leave stale text. */
336 ED_region_tag_redraw(region);
337
338 BoneSampleData sample_data;
339 switch (area.spacetype) {
340 case SPACE_VIEW3D: {
341 sample_data = sample_data_from_3d_view(C, mval, bdr);
342 break;
343 }
344 case SPACE_OUTLINER: {
345 sample_data = sample_data_from_outliner(C, mval, bdr);
346 break;
347 }
348
349 default:
351 break;
352 }
353
354 if (sample_data.name) {
355 STRNCPY_UTF8(bdr.name, sample_data.name);
356 copy_v2_v2_int(bdr.name_pos, mval);
357 }
358
359 CTX_wm_window_set(C, win_prev);
360 CTX_wm_area_set(C, area_prev);
361 CTX_wm_region_set(C, region_prev);
362
363 return sample_data;
364}
365
366static SampleResult bonedropper_sample(bContext *C, BoneDropper &bdr, const int event_xy[2])
367{
368 int event_xy_win[2];
369 wmWindow *win = nullptr;
370 ScrArea *area = nullptr;
371 eyedropper_win_area_find(C, event_xy, event_xy_win, &win, &area);
372
373 if (!win || !area) {
375 }
378 }
379
380 BoneSampleData sample_data = bonedropper_sample_pt(C, *win, *area, bdr, event_xy_win);
381 if (!sample_data.name) {
382 return sample_data.sample_result;
383 }
384
386 /* In case we are searching for a bone, convert the pointer from bPoseChannel. */
387 if (search_type == RNA_Bone && sample_data.bone_rna.type == RNA_PoseBone &&
388 bdr.search_ptr.type == RNA_Armature)
389 {
390 /* We are searching for something in the armature but got a pose bone on the object, so we
391 * need to do a conversion. */
392 Object *pose_ob = id_cast<Object *>(sample_data.bone_rna.owner_id);
393 bPoseChannel *pose_bone = static_cast<bPoseChannel *>(sample_data.bone_rna.data);
395 bdr.search_ptr.owner_id, RNA_Bone, pose_bone->bone_get(*pose_ob));
396 }
397
399 switch (type) {
400 case PROP_STRING:
401 RNA_property_string_set(&bdr.ptr, bdr.prop, sample_data.name);
402 break;
403 case PROP_POINTER:
404 RNA_property_pointer_set(&bdr.ptr, bdr.prop, sample_data.bone_rna, CTX_wm_reports(C));
405 break;
406
407 default:
409 break;
410 }
411
412 RNA_property_update(C, &bdr.ptr, bdr.prop);
413
415}
416
418{
419 switch (result) {
421 BKE_report(op->reports, RPT_WARNING, "Picking a bone failed");
422 break;
425 op->reports, RPT_WARNING, "Picked bone does not belong to the already chosen armature");
426 break;
427
431 "Selection is not a bone. Armature needs to be in Pose Mode or Edit Mode "
432 "to pick in the 3D Viewport");
433 break;
434
436 BKE_report(op->reports, RPT_WARNING, "Selection is not a bone");
437 break;
438
440 BKE_report(op->reports, RPT_WARNING, "Can only pick from the 3D viewport or the outliner");
441 break;
442
444 break;
445 }
446}
447
449{
450 BoneDropper *bdr = static_cast<BoneDropper *>(op->customdata);
451 if (!bdr) {
452 return OPERATOR_CANCELLED;
453 }
454
455 if (event->type == EVT_MODAL_MAP) {
456 switch (event->val) {
457 case EYE_MODAL_CANCEL:
459 return OPERATOR_CANCELLED;
461 const bool is_undo = bdr->is_undo;
462 const SampleResult result = bonedropper_sample(C, *bdr, event->xy);
463 bonedropper_exit(C, op);
465 /* Could support finished & undo-skip. */
466 return is_undo ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
467 }
469 }
470 }
471 }
472 else if (event->type == MOUSEMOVE) {
473 bdr->name[0] = '\0';
474 int event_xy_win[2];
475 wmWindow *win = nullptr;
476 ScrArea *area = nullptr;
477 eyedropper_win_area_find(C, event->xy, event_xy_win, &win, &area);
478
479 if (win && area) {
480 /* Set the region for eyedropper cursor text drawing */
482 bonedropper_sample_pt(C, *win, *area, *bdr, event->xy);
483 }
484 }
485
487}
489{
490 /* This is needed to ensure viewport picking works. */
492
493 if (bonedropper_init(C, op)) {
494 wmWindow *win = CTX_wm_window(C);
495 /* Workaround for de-activating the button clearing the cursor, see #76794 */
498
501 }
502 return OPERATOR_CANCELLED;
503}
504
506{
507 if (bonedropper_init(C, op)) {
508 bonedropper_exit(C, op);
509
510 return OPERATOR_FINISHED;
511 }
512 return OPERATOR_CANCELLED;
513}
514
516{
517 PointerRNA ptr;
518 PropertyRNA *prop;
519 int index_dummy;
520
521 if (CTX_wm_window(C) == nullptr) {
522 return false;
523 }
524
525 const Object *active_object = CTX_data_active_object(C);
526
527 if (!active_object || active_object->type != OB_ARMATURE) {
528 CTX_wm_operator_poll_msg_set(C, "The active object needs to be an armature");
529 return false;
530 }
531
532 if (!ELEM(active_object->mode, OB_MODE_POSE, OB_MODE_EDIT)) {
533 CTX_wm_operator_poll_msg_set(C, "The armature needs to be in Pose mode or Edit mode");
534 return false;
535 }
536
537 Button *but = context_active_but_prop_get(C, &ptr, &prop, &index_dummy);
538
539 if (!but) {
540 return false;
541 }
542
543 if (but->type != ButtonType::SearchMenu || !(but->flag & BUT_VALUE_CLEAR)) {
544 return false;
545 }
546
547 ButtonSearch *search_but = static_cast<ButtonSearch *>(but);
548
550 return false;
551 }
552
553 const StructRNA *type = RNA_property_pointer_type(&search_but->rnasearchpoin,
554 search_but->rnasearchprop);
555
556 return type == RNA_Bone || type == RNA_EditBone || type == RNA_PoseBone;
557}
558
560{
561 /* Identifiers. */
562 ot->name = "Eyedropper Bone";
563 ot->idname = "UI_OT_eyedropper_bone";
564 ot->description = "Sample a bone from the 3D View or the Outliner to store in a property";
565
566 /* API callbacks. */
572
573 /* Flags. */
575}
576
577} // namespace blender::ui
General operations, lookup, etc. for blender objects.
#define STRNCPY_UTF8(dst, src)
ID and Library types, which are fundamental for SDNA.
#define REGION_DRAW_POST_PIXEL
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
#define ELEM(...)
static void bonedropper_cancel(bContext *C, wmOperator *op)
static BoneSampleData sample_data_from_outliner(bContext *C, const int mval[2], const BoneDropper &bdr)
static wmOperatorStatus bonedropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
void eyedropper_win_area_find(const bContext *C, const int event_xy[2], int r_event_xy[2], wmWindow **r_win, ScrArea **r_area)
static wmOperatorStatus bonedropper_exec(bContext *C, wmOperator *op)
static void bonedropper_set_draw_callback_region(ScrArea &area, BoneDropper &bdr)
static void datadropper_draw_cb(const bContext *, ARegion *, void *arg)
bool button_flag_is_set(Button *but, int64_t flag)
static BoneSampleData bonedropper_sample_pt(bContext *C, wmWindow &win, ScrArea &area, BoneDropper &bdr, const int event_xy[2])
static wmOperatorStatus bonedropper_invoke(bContext *C, wmOperator *op, const wmEvent *)
static SampleResult bonedropper_sample(bContext *C, BoneDropper &bdr, const int event_xy[2])
static bool bonedropper_poll(bContext *C)
static int bonedropper_init(bContext *C, wmOperator *op)
void eyedropper_draw_cursor_text_region(const int xy[2], const char *name)
void UI_OT_eyedropper_bone(wmOperatorType *ot)
static bool is_bone_dropper_valid(BoneDropper *bone_dropper)
Button * context_active_but_prop_get(const bContext *C, PointerRNA *r_ptr, PropertyRNA **r_prop, int *r_index)
static void generate_sample_warning(SampleResult result, wmOperator *op)
static BoneSampleData sample_data_from_3d_view(bContext *C, const int mval[2], const BoneDropper &bdr)
static void bonedropper_exit(bContext *C, wmOperator *op)
void context_active_but_clear(bContext *C, wmWindow *win, ARegion *region)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ScrArea * CTX_wm_area(const bContext *C)
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
@ RPT_WARNING
Definition BKE_report.hh:40
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
void BKE_object_update_select_id(Main *bmain)
Definition object.cc:5676
void CTX_wm_window_set(bContext *C, wmWindow *win)
void WM_cursor_modal_set(wmWindow *win, int val)
bool ED_outliner_give_rna_under_cursor(bContext *C, const int mval[2], PointerRNA *r_ptr)
wmWindow * CTX_wm_window(const bContext *C)
enum eContextObjectMode CTX_data_mode_enum(const bContext *C)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
@ CTX_MODE_EDIT_ARMATURE
PropertyType RNA_property_type(PropertyRNA *prop)
EditBone * ED_armature_pick_ebone(bContext *C, const int xy[2], bool findunsel, Base **r_base)
void * ED_region_draw_cb_activate(ARegionType *art, void(*draw)(const bContext *, ARegion *, void *), void *customdata, int type)
void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
bPoseChannel * ED_armature_pick_pchan(bContext *C, const int xy[2], bool findunsel, Base **r_base)
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
StructRNA * RNA_property_pointer_type(PointerRNA *ptr, PropertyRNA *prop)
@ PROP_POINTER
Definition RNA_types.hh:215
@ PROP_STRING
Definition RNA_types.hh:213
bool ED_region_draw_cb_exit(ARegionType *art, void *handle)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
void WM_cursor_modal_restore(wmWindow *win)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
ARegionType * BKE_regiontype_from_id(const SpaceType *st, int regionid)
Definition screen.cc:364
SpaceType * BKE_spacetype_from_id(int spaceid)
Definition screen.cc:354
ARegion * BKE_area_find_region_type(const ScrArea *area, int region_type)
Definition screen.cc:1024
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
@ WM_CURSOR_EYEDROPPER
Definition wm_cursors.hh:38
Main * CTX_data_main(const bContext *C)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
bool RNA_property_editable(const PointerRNA *ptr, PropertyRNA *prop)
void CTX_wm_region_set(bContext *C, ARegion *region)
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
@ OPTYPE_INTERNAL
Definition WM_types.hh:210
ReportList * CTX_wm_reports(const bContext *C)
void WM_event_add_mousemove(wmWindow *win)
Object * CTX_data_active_object(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
void ED_area_tag_redraw(ScrArea *area)
Definition area.cc:784
void CTX_wm_area_set(bContext *C, ScrArea *area)
ARegion * BKE_area_find_region_xy(const ScrArea *area, int regiontype, const int xy[2]) ATTR_NONNULL(3)
Definition screen.cc:1053
struct Object * object
char name[258]
Definition DNA_ID.h:433
StructRNA * type
Definition RNA_types.hh:63
struct SpaceType * type
wmEventType type
Definition WM_types.hh:817
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports