Blender V5.3
editors/armature/bone_collections.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
9
10#include <cstring>
11
12#include "ANIM_armature.hh"
14
15#include "DNA_ID.h"
16#include "DNA_object_types.h"
17
18#include "BLI_listbase.hh"
19
20#include "BKE_action.hh"
21#include "BKE_armature.hh"
22#include "BKE_context.hh"
23#include "BKE_lib_override.hh"
24#include "BKE_library.hh"
25#include "BKE_report.hh"
26
27#include "BLT_translation.hh"
28
29#include "DEG_depsgraph.hh"
30
31#include "RNA_access.hh"
32#include "RNA_define.hh"
33
34#include "WM_api.hh"
35#include "WM_types.hh"
36
37#include "ED_armature.hh"
38#include "ED_object.hh"
39#include "ED_outliner.hh"
40
41#include "UI_interface.hh"
43#include "UI_resources.hh"
44
45#include "armature_intern.hh"
46
47namespace blender {
48
49struct wmOperator;
50
51/* ********************************************** */
52/* Bone collections */
53
55{
56 bArmature *armature = ED_armature_context(C);
57 if (armature == nullptr) {
58 return false;
59 }
60
61 if (!ID_IS_EDITABLE(&armature->id)) {
63 "Cannot add bone collections to a linked Armature without an "
64 "override on the Armature Data");
65 return false;
66 }
67
68 if (BKE_lib_override_library_is_system_defined(nullptr, &armature->id)) {
70 "Cannot add bone collections to a linked Armature with a system "
71 "override; explicitly create an override on the Armature Data");
72 return false;
73 }
74
75 return true;
76}
77
80{
81 bArmature *armature = ED_armature_context(C);
82 if (armature == nullptr) {
83 return false;
84 }
85
86 if (BKE_lib_override_library_is_system_defined(nullptr, &armature->id)) {
88 "Cannot update a linked Armature with a system override; "
89 "explicitly create an override on the Armature Data");
90 return false;
91 }
92
93 BoneCollection *bcoll = armature->runtime->active_collection;
94 if (bcoll == nullptr) {
95 CTX_wm_operator_poll_msg_set(C, "Armature has no active bone collection, select one first");
96 return false;
97 }
98
99 if (!ANIM_armature_bonecoll_is_editable(armature, bcoll)) {
101 C, "Cannot edit bone collections that are linked from another blend file");
102 return false;
103 }
104 return true;
105}
106
108{
109 using namespace blender::animrig;
110
111 bArmature *armature = ED_armature_context(C);
112
113 /* If there is an active bone collection, create the new one as a sibling. */
114 const int parent_index = armature_bonecoll_find_parent_index(
115 armature, armature->runtime->active_collection_index);
116
117 BoneCollection *bcoll = ANIM_armature_bonecoll_new(armature, nullptr, parent_index);
118
119 if (armature->runtime->active_collection) {
120 const int active_child_index = armature_bonecoll_child_number_find(
121 armature, armature->runtime->active_collection);
122 armature_bonecoll_child_number_set(armature, bcoll, active_child_index + 1);
123 }
124
125 ANIM_armature_bonecoll_active_set(armature, bcoll);
126 /* TODO: ensure the ancestors of the new bone collection are all expanded. */
127
129 return OPERATOR_FINISHED;
130}
131
133{
134 /* identifiers */
135 ot->name = "Add Bone Collection";
136 ot->idname = "ARMATURE_OT_collection_add";
137 ot->description = "Add a new bone collection";
138
139 /* API callbacks. */
142
143 /* flags */
145}
146
148{
149 /* The poll function ensures armature->active_collection is not NULL. */
150 bArmature *armature = ED_armature_context(C);
152
153 /* notifiers for updates */
156
157 return OPERATOR_FINISHED;
158}
159
161{
162 /* identifiers */
163 ot->name = "Remove Bone Collection";
164 ot->idname = "ARMATURE_OT_collection_remove";
165 ot->description = "Remove the active bone collection";
166
167 /* API callbacks. */
170
171 /* flags */
173}
174
176{
177 const int direction = RNA_enum_get(op->ptr, "direction");
178
179 /* Poll function makes sure this is valid. */
180 bArmature *armature = ED_armature_context(C);
181
182 const bool ok = ANIM_armature_bonecoll_move(
183 armature, armature->runtime->active_collection, direction);
184 if (!ok) {
185 return OPERATOR_CANCELLED;
186 }
187
189
191 return OPERATOR_FINISHED;
192}
193
195{
196 static const EnumPropertyItem bcoll_slot_move[] = {
197 {-1, "UP", 0, "Up", ""},
198 {1, "DOWN", 0, "Down", ""},
199 {0, nullptr, 0, nullptr, nullptr},
200 };
201
202 /* identifiers */
203 ot->name = "Move Bone Collection";
204 ot->idname = "ARMATURE_OT_collection_move";
205 ot->description = "Change position of active Bone Collection in list of Bone collections";
206
207 /* API callbacks. */
210
211 /* flags */
213
214 RNA_def_enum(ot->srna,
215 "direction",
216 bcoll_slot_move,
217 0,
218 "Direction",
219 "Direction to move the active Bone Collection towards");
220}
221
223{
224 bArmature *armature = id_cast<bArmature *>(ob->data);
225
226 char bcoll_name[MAX_NAME];
227 RNA_string_get(op->ptr, "name", bcoll_name);
228
229 if (bcoll_name[0] == '\0') {
230 return armature->runtime->active_collection;
231 }
232
233 BoneCollection *bcoll = ANIM_armature_bonecoll_get_by_name(armature, bcoll_name);
234 if (!bcoll) {
235 BKE_reportf(op->reports, RPT_ERROR, "No bone collection named '%s'", bcoll_name);
236 return nullptr;
237 }
238
239 return bcoll;
240}
241
242using assign_bone_func = bool (*)(BoneCollection *bcoll, Bone *bone);
243using assign_ebone_func = bool (*)(BoneCollection *bcoll, EditBone *ebone);
244
245/* The following 3 functions either assign or unassign, depending on the
246 * 'assign_bone_func'/'assign_ebone_func' they get passed. */
247
249 Object *ob,
250 BoneCollection *bcoll,
251 assign_bone_func assign_func,
252 bool *made_any_changes,
253 bool *had_bones_to_assign)
254{
255 /* TODO: support multi-object pose mode. */
257 *made_any_changes |= assign_func(bcoll, pchan->bone_get(*ob));
258 *had_bones_to_assign = true;
259 }
261
263
264 bArmature *arm = id_cast<bArmature *>(ob->data);
265 DEG_id_tag_update(&arm->id, ID_RECALC_SELECT); /* Recreate the draw buffers. */
266}
267
269 Object *ob,
270 BoneCollection *bcoll,
271 assign_ebone_func assign_func,
272 bool *made_any_changes,
273 bool *had_bones_to_assign)
274{
275 bArmature *arm = id_cast<bArmature *>(ob->data);
277
278 for (EditBone &ebone : *arm->edbo) {
279 if (!EBONE_EDITABLE(&ebone) || !animrig::bone_is_visible(arm, &ebone)) {
280 continue;
281 }
282 *made_any_changes |= assign_func(bcoll, &ebone);
283 *had_bones_to_assign = true;
284 }
285
289}
290
297 Object *ob,
298 BoneCollection *bcoll,
301 bool *made_any_changes,
302 bool *had_bones_to_assign)
303{
304 switch (CTX_data_mode_enum(C)) {
305 case CTX_MODE_POSE: {
307 C, ob, bcoll, assign_bone_func, made_any_changes, had_bones_to_assign);
308 return true;
309 }
310
313 C, ob, bcoll, assign_ebone_func, made_any_changes, had_bones_to_assign);
314
316 return true;
317 }
318
319 default:
320 return false;
321 }
322}
323
330 Object *ob,
331 BoneCollection *bcoll,
332 const char *bone_name,
335 bool *made_any_changes,
336 bool *had_bones_to_assign)
337{
338 bArmature *arm = id_cast<bArmature *>(ob->data);
339
340 switch (CTX_data_mode_enum(C)) {
341 case CTX_MODE_POSE: {
342 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, bone_name);
343 if (!pchan) {
344 return true;
345 }
346
347 *had_bones_to_assign = true;
348 *made_any_changes |= assign_bone_func(bcoll, pchan->bone_get(*ob));
349
351 DEG_id_tag_update(&arm->id, ID_RECALC_SELECT); /* Recreate the draw buffers. */
352 return true;
353 }
354
356 EditBone *ebone = ED_armature_ebone_find_name(arm->edbo, bone_name);
357 if (!ebone) {
358 return true;
359 }
360
361 *had_bones_to_assign = true;
362 *made_any_changes |= assign_ebone_func(bcoll, ebone);
363
367 return true;
368 }
369
370 default:
371 return false;
372 }
373}
374
376{
378 if (ob == nullptr) {
379 return false;
380 }
381
382 if (ob->type != OB_ARMATURE) {
383 CTX_wm_operator_poll_msg_set(C, "Bone collections can only be edited on an Armature");
384 return false;
385 }
386
387 bArmature *armature = id_cast<bArmature *>(ob->data);
388 if (armature != ED_armature_context(C)) {
389 CTX_wm_operator_poll_msg_set(C, "Pinned armature is not active in the 3D viewport");
390 return false;
391 }
392
393 if (!ID_IS_EDITABLE(armature) && !ID_IS_OVERRIDE_LIBRARY(armature)) {
395 C, "Cannot edit bone collections on linked Armatures without override");
396 return false;
397 }
398 if (BKE_lib_override_library_is_system_defined(nullptr, &armature->id)) {
400 "Cannot edit bone collections on a linked Armature with a system "
401 "override; explicitly create an override on the Armature Data");
402 return false;
403 }
404
405 CTX_wm_operator_poll_msg_set(C, "Linked bone collections are not editable");
406
407 /* The target bone collection can be specified by name in an operator property, but that's not
408 * available here. So just allow in the poll function, and do the final check in the execute. */
409 return true;
410}
411
412/* Assign selected pchans to the bone collection that the user selects */
414{
416 if (ob == nullptr) {
417 return OPERATOR_CANCELLED;
418 }
419
421 if (bcoll == nullptr) {
422 return OPERATOR_CANCELLED;
423 }
424
425 bArmature *armature = id_cast<bArmature *>(ob->data);
426 if (!ANIM_armature_bonecoll_is_editable(armature, bcoll)) {
427 BKE_reportf(op->reports, RPT_ERROR, "Cannot assign to linked bone collection %s", bcoll->name);
428 return OPERATOR_CANCELLED;
429 }
430
431 bool made_any_changes = false;
432 bool had_bones_to_assign = false;
433 const bool mode_is_supported = bone_collection_assign_mode_specific(
434 C,
435 ob,
436 bcoll,
439 &made_any_changes,
440 &had_bones_to_assign);
441
442 if (!mode_is_supported) {
444 op->reports, RPT_ERROR, "This operator only works in pose mode and armature edit mode");
445 return OPERATOR_CANCELLED;
446 }
447 if (!had_bones_to_assign) {
449 op->reports, RPT_WARNING, "No bones selected, nothing to assign to bone collection");
450 return OPERATOR_CANCELLED;
451 }
452 if (!made_any_changes) {
454 op->reports, RPT_WARNING, "All selected bones were already part of this collection");
455 return OPERATOR_CANCELLED;
456 }
457
459 return OPERATOR_FINISHED;
460}
461
463{
464 /* identifiers */
465 ot->name = "Add Selected Bones to Collection";
466 ot->idname = "ARMATURE_OT_collection_assign";
467 ot->description = "Add selected bones to the chosen bone collection";
468
469 /* API callbacks. */
472
473 /* flags */
474 ot->flag = OPTYPE_UNDO;
475
476 /* properties */
478 "name",
479 nullptr,
480 MAX_NAME,
481 "Bone Collection",
482 "Name of the bone collection to assign this bone to; empty to assign to the "
483 "active bone collection");
484}
485
487{
489 if (ob == nullptr) {
490 return false;
491 }
492
493 if (ob->type != OB_ARMATURE) {
494 CTX_wm_operator_poll_msg_set(C, "Bone collections can only be edited on an Armature");
495 return false;
496 }
497
498 bArmature *armature = id_cast<bArmature *>(ob->data);
499 if (!ID_IS_EDITABLE(armature) && !ID_IS_OVERRIDE_LIBRARY(armature)) {
501 C, "Cannot edit bone collections on linked Armatures without override");
502 return false;
503 }
504 if (BKE_lib_override_library_is_system_defined(nullptr, &armature->id)) {
506 "Cannot edit bone collections on a linked Armature with a system "
507 "override; explicitly create an override on the Armature Data");
508 return false;
509 }
510
511 return true;
512}
513
514/* Assign selected pchans to the bone collection that the user selects */
516{
518 if (ob == nullptr) {
519 return OPERATOR_CANCELLED;
520 }
521
522 bArmature *armature = id_cast<bArmature *>(ob->data);
523
524 char bcoll_name[MAX_NAME];
525 RNA_string_get(op->ptr, "name", bcoll_name);
526
527 /* Note that this bone collection can be removed later on, if the assignment part of this
528 * operation failed. */
529 BoneCollection *bcoll = ANIM_armature_bonecoll_new(armature, bcoll_name);
530
531 bool made_any_changes = false;
532 bool had_bones_to_assign = false;
533 const bool mode_is_supported = bone_collection_assign_mode_specific(
534 C,
535 ob,
536 bcoll,
539 &made_any_changes,
540 &had_bones_to_assign);
541
542 if (!mode_is_supported) {
544 op->reports, RPT_ERROR, "This operator only works in pose mode and armature edit mode");
545 ANIM_armature_bonecoll_remove(armature, bcoll);
546 return OPERATOR_CANCELLED;
547 }
548 if (!had_bones_to_assign) {
550 op->reports, RPT_WARNING, "No bones selected, nothing to assign to bone collection");
551 return OPERATOR_FINISHED;
552 }
553 /* Not checking for `made_any_changes`, as if there were any bones to assign, they never could
554 * have already been assigned to this brand new bone collection. */
555
556 ANIM_armature_bonecoll_active_set(armature, bcoll);
558 return OPERATOR_FINISHED;
559}
560
562{
563 /* identifiers */
564 ot->name = "Add Selected Bones to New Collection";
565 ot->idname = "ARMATURE_OT_collection_create_and_assign";
566 ot->description = "Create a new bone collection and assign all selected bones";
567
568 /* API callbacks. */
571
572 /* flags */
574
575 /* properties */
577 "name",
578 nullptr,
579 MAX_NAME,
580 "Bone Collection",
581 "Name of the bone collection to create");
582}
583
585{
587 if (ob == nullptr) {
588 return OPERATOR_CANCELLED;
589 }
590
592 if (bcoll == nullptr) {
593 return OPERATOR_CANCELLED;
594 }
595
596 bool made_any_changes = false;
597 bool had_bones_to_unassign = false;
598 const bool mode_is_supported = bone_collection_assign_mode_specific(
599 C,
600 ob,
601 bcoll,
604 &made_any_changes,
605 &had_bones_to_unassign);
606
607 if (!mode_is_supported) {
609 op->reports, RPT_ERROR, "This operator only works in pose mode and armature edit mode");
610 return OPERATOR_CANCELLED;
611 }
612 if (!had_bones_to_unassign) {
614 op->reports, RPT_WARNING, "No bones selected, nothing to unassign from bone collection");
615 return OPERATOR_CANCELLED;
616 }
617 if (!made_any_changes) {
619 op->reports, RPT_WARNING, "None of the selected bones were assigned to this collection");
620 return OPERATOR_CANCELLED;
621 }
622 return OPERATOR_FINISHED;
623}
624
626{
627 /* identifiers */
628 ot->name = "Remove Selected from Bone collections";
629 ot->idname = "ARMATURE_OT_collection_unassign";
630 ot->description = "Remove selected bones from the active bone collection";
631
632 /* API callbacks. */
635
636 /* flags */
637 ot->flag = OPTYPE_UNDO;
638
640 "name",
641 nullptr,
642 MAX_NAME,
643 "Bone Collection",
644 "Name of the bone collection to unassign this bone from; empty to unassign from "
645 "the active bone collection");
646}
647
649{
651 if (ob == nullptr) {
652 return OPERATOR_CANCELLED;
653 }
654
656 if (bcoll == nullptr) {
657 return OPERATOR_CANCELLED;
658 }
659
660 char bone_name[MAX_NAME];
661 RNA_string_get(op->ptr, "bone_name", bone_name);
662 if (!bone_name[0]) {
663 BKE_reportf(op->reports, RPT_ERROR, "Missing bone name");
664 return OPERATOR_CANCELLED;
665 }
666
667 bool made_any_changes = false;
668 bool had_bones_to_unassign = false;
669 const bool mode_is_supported = bone_collection_assign_named_mode_specific(
670 C,
671 ob,
672 bcoll,
673 bone_name,
676 &made_any_changes,
677 &had_bones_to_unassign);
678
679 if (!mode_is_supported) {
681 op->reports, RPT_ERROR, "This operator only works in pose mode and armature edit mode");
682 return OPERATOR_CANCELLED;
683 }
684 if (!had_bones_to_unassign) {
685 BKE_reportf(op->reports, RPT_WARNING, "Could not find bone '%s'", bone_name);
686 return OPERATOR_CANCELLED;
687 }
688 if (!made_any_changes) {
691 "Bone '%s' was not assigned to collection '%s'",
692 bone_name,
693 bcoll->name);
694 return OPERATOR_CANCELLED;
695 }
696 return OPERATOR_FINISHED;
697}
698
700{
701 /* identifiers */
702 ot->name = "Remove Bone from Bone Collection";
703 ot->idname = "ARMATURE_OT_collection_unassign_named";
704 ot->description = "Unassign the named bone from this bone collection";
705
706 /* API callbacks. */
709
710 /* flags */
711 ot->flag = OPTYPE_UNDO;
712
714 "name",
715 nullptr,
716 MAX_NAME,
717 "Bone Collection",
718 "Name of the bone collection to unassign this bone from; empty to unassign from "
719 "the active bone collection");
721 "bone_name",
722 nullptr,
723 MAX_NAME,
724 "Bone Name",
725 "Name of the bone to unassign from the collection; empty to use the active bone");
726}
727
728static bool editbone_is_member(const EditBone *ebone, const BoneCollection *bcoll)
729{
730 for (BoneCollectionReference &ref : ebone->bone_collections) {
731 if (ref.bcoll == bcoll) {
732 return true;
733 }
734 }
735 return false;
736}
737
739{
741 if (ob && ob->type == OB_ARMATURE) {
742
743 /* For bone selection, at least the pose should be editable to actually store
744 * the selection state. */
745 if (!ID_IS_EDITABLE(ob) && !ID_IS_OVERRIDE_LIBRARY(ob)) {
747 C, "Cannot (de)select bones on linked object, that would need an override");
748 return false;
749 }
750 }
751
752 const bArmature *armature = ED_armature_context(C);
753 if (armature == nullptr) {
754 return false;
755 }
756
757 const bool is_editmode = armature->edbo != nullptr;
758 if (!is_editmode) {
760 if (!active_object || active_object->type != OB_ARMATURE ||
761 active_object->data != id_cast<const ID *>(armature))
762 {
763 /* There has to be an active object in order to hide a pose bone that points to the correct
764 * armature. With pinning, the active object may not be an armature. */
765 CTX_wm_operator_poll_msg_set(C, "The active object does not match the armature");
766 return false;
767 }
768 }
769
770 if (armature->runtime->active_collection == nullptr) {
771 CTX_wm_operator_poll_msg_set(C, "No active bone collection");
772 return false;
773 }
774 return true;
775}
776
778 bArmature *armature,
779 BoneCollection *bcoll,
780 const bool select)
781{
782 const bool is_editmode = armature->edbo != nullptr;
783
784 if (is_editmode) {
785 for (EditBone &ebone : *armature->edbo) {
786 if (!EBONE_SELECTABLE(armature, &ebone)) {
787 continue;
788 }
789 if (!editbone_is_member(&ebone, bcoll)) {
790 continue;
791 }
793 }
794 }
795 else {
797 if (!active_object || active_object->type != OB_ARMATURE ||
798 active_object->data != id_cast<const ID *>(armature))
799 {
800 /* This is covered by the poll function. */
802 return;
803 }
804 for (BoneCollectionMember &member : bcoll->bones) {
805 Bone *bone = member.bone;
806 bPoseChannel *pose_bone = BKE_pose_channel_find_name(active_object->pose, bone->name);
807 BLI_assert_msg(pose_bone != nullptr, "The pose bones and armature bones are out of sync");
808 if (!animrig::bone_is_visible(armature, {pose_bone, pose_bone->bone_get(*active_object)})) {
809 continue;
810 }
811 if (bone->flag & BONE_UNSELECTABLE) {
812 continue;
813 }
814
815 if (select) {
816 animrig::bone_select(pose_bone);
817 }
818 else {
819 animrig::bone_deselect(pose_bone);
820 }
821 }
822 DEG_id_tag_update(&active_object->id, ID_RECALC_SELECT);
823 }
824
827
828 if (is_editmode) {
830 }
831 else {
833 }
834}
835
837{
838 bArmature *armature = ED_armature_context(C);
839 if (armature == nullptr) {
840 return OPERATOR_CANCELLED;
841 }
842
843 BoneCollection *bcoll = armature->runtime->active_collection;
844 if (bcoll == nullptr) {
845 return OPERATOR_CANCELLED;
846 }
847
848 bone_collection_select(C, armature, bcoll, true);
849 return OPERATOR_FINISHED;
850}
851
853{
854 /* identifiers */
855 ot->name = "Select Bones of Bone Collection";
856 ot->idname = "ARMATURE_OT_collection_select";
857 ot->description = "Select bones in active Bone Collection";
858
859 /* API callbacks. */
862
863 /* flags */
865}
866
868{
869 bArmature *armature = ED_armature_context(C);
870 if (armature == nullptr) {
871 return OPERATOR_CANCELLED;
872 }
873
874 BoneCollection *bcoll = armature->runtime->active_collection;
875 if (bcoll == nullptr) {
876 return OPERATOR_CANCELLED;
877 }
878
879 bone_collection_select(C, armature, bcoll, false);
880 return OPERATOR_FINISHED;
881}
882
884{
885 /* identifiers */
886 ot->name = "Deselect Bone Collection";
887 ot->idname = "ARMATURE_OT_collection_deselect";
888 ot->description = "Deselect bones of active Bone Collection";
889
890 /* API callbacks. */
893
894 /* flags */
896}
897
898/* -------------------------- */
899
901{
902 const int collection_index = RNA_int_get(op->ptr, "collection_index");
903 BoneCollection *target_bcoll;
904 const bool index_exceeds_range = collection_index >= arm->collection_array_num;
905
906 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "new_collection_name");
907 if ((RNA_property_is_set(op->ptr, prop) && !index_exceeds_range) ||
908 /* Neither properties can be used, the operator may have been called with defaults.
909 * In this case add a root collection, the default name will be used. */
910 (collection_index < 0))
911 {
912 /* TODO: check this with linked, non-overridden armatures. */
913 char new_collection_name[MAX_NAME];
914 RNA_string_get(op->ptr, "new_collection_name", new_collection_name);
915 target_bcoll = ANIM_armature_bonecoll_new(arm, new_collection_name, collection_index);
916 BLI_assert_msg(target_bcoll,
917 "It should always be possible to create a new bone collection on an armature");
918 ANIM_armature_bonecoll_active_set(arm, target_bcoll);
919 }
920 else {
921 if (index_exceeds_range) {
923 RPT_ERROR,
924 "Bone collection with index %d not found on Armature %s",
925 collection_index,
926 arm->id.name + 2);
927 return nullptr;
928 }
929 target_bcoll = arm->collection_array[collection_index];
930 }
931
932 if (!ANIM_armature_bonecoll_is_editable(arm, target_bcoll)) {
934 RPT_ERROR,
935 "Bone collection %s is not editable, maybe add an override on the armature Data?",
936 target_bcoll->name);
937 return nullptr;
938 }
939
940 return target_bcoll;
941}
942
944 wmOperator *op,
945 const assign_bone_func assign_func_bone,
946 const assign_ebone_func assign_func_ebone)
947{
949 if (ob->mode == OB_MODE_POSE) {
951 }
952 if (!ob) {
953 BKE_reportf(op->reports, RPT_ERROR, "No object found to operate on");
954 return OPERATOR_CANCELLED;
955 }
956
957 bArmature *arm = id_cast<bArmature *>(ob->data);
958 BoneCollection *target_bcoll = add_or_move_to_collection_bcoll(op, arm);
959 if (!target_bcoll) {
960 /* add_or_move_to_collection_bcoll() already reported the reason. */
961 return OPERATOR_CANCELLED;
962 }
963
964 bool made_any_changes = false;
965 bool had_bones_to_assign = false;
966 const bool mode_is_supported = bone_collection_assign_mode_specific(C,
967 ob,
968 target_bcoll,
969 assign_func_bone,
970 assign_func_ebone,
971 &made_any_changes,
972 &had_bones_to_assign);
973
974 if (!mode_is_supported) {
976 op->reports, RPT_ERROR, "This operator only works in pose mode and armature edit mode");
977 return OPERATOR_CANCELLED;
978 }
979 if (!had_bones_to_assign) {
981 op->reports, RPT_WARNING, "No bones selected, nothing to assign to bone collection");
982 return OPERATOR_CANCELLED;
983 }
984 if (!made_any_changes) {
986 op->reports, RPT_WARNING, "All selected bones were already part of this collection");
987 return OPERATOR_CANCELLED;
988 }
989
990 DEG_id_tag_update(&arm->id, ID_RECALC_SELECT); /* Recreate the draw buffers. */
991
994 return OPERATOR_FINISHED;
995}
996
1004
1010
1012{
1014 if (ob == nullptr) {
1015 return false;
1016 }
1017
1018 if (ob->type != OB_ARMATURE) {
1019 CTX_wm_operator_poll_msg_set(C, "Bone collections can only be edited on an Armature");
1020 return false;
1021 }
1022
1023 const bArmature *armature = id_cast<bArmature *>(ob->data);
1024 if (!ID_IS_EDITABLE(armature) && !ID_IS_OVERRIDE_LIBRARY(armature)) {
1025 CTX_wm_operator_poll_msg_set(C, "This needs a local Armature or an override");
1026 return false;
1027 }
1028
1029 if (BKE_lib_override_library_is_system_defined(nullptr, &armature->id)) {
1031 "Cannot update a linked Armature with a system override; "
1032 "explicitly create an override on the Armature Data");
1033 return false;
1034 }
1035
1036 CTX_wm_operator_poll_msg_set(C, "Linked bone collections are not editable");
1037
1038 /* Ideally this would also check the target bone collection to move/assign to.
1039 * However, that requires access to the operator properties, and those are not
1040 * available in the poll function. */
1041 return true;
1042}
1043
1053static void *menu_custom_data_encode(const int bcoll_index, const bool is_move_operation)
1054{
1055 /* Add 1 to the index, so that it's never negative (it can be -1 to indicate 'all roots'). */
1056 const uintptr_t index_and_move_bit = ((bcoll_index + 1) << 1) | (is_move_operation << 0);
1057 return reinterpret_cast<void *>(index_and_move_bit);
1058}
1059
1065static std::pair<int, bool> menu_custom_data_decode(void *menu_custom_data)
1066{
1067 const uintptr_t index_and_move_bit = reinterpret_cast<intptr_t>(menu_custom_data);
1068 const bool is_move_operation = (index_and_move_bit & 1) == 1;
1069 const int bcoll_index = int(index_and_move_bit >> 1) - 1;
1070 return std::make_pair(bcoll_index, is_move_operation);
1071}
1072
1073static int icon_for_bone_collection(const bool collection_contains_active_bone)
1074{
1075 return collection_contains_active_bone ? ICON_REMOVE : ICON_ADD;
1076}
1077
1079 const bArmature *arm,
1080 const BoneCollection *bcoll,
1081 const int bcoll_index,
1082 const bool is_move_operation)
1083{
1084 if (is_move_operation) {
1085 PointerRNA op_ptr = layout.op("ARMATURE_OT_move_to_collection", bcoll->name, ICON_NONE);
1086 RNA_int_set(&op_ptr, "collection_index", bcoll_index);
1087 return;
1088 }
1089
1090 const bool contains_active_bone = ANIM_armature_bonecoll_contains_active_bone(arm, bcoll);
1091 const int icon = icon_for_bone_collection(contains_active_bone);
1092
1093 if (contains_active_bone) {
1094 PointerRNA op_ptr = layout.op("ARMATURE_OT_collection_unassign", bcoll->name, icon);
1095 RNA_string_set(&op_ptr, "name", bcoll->name);
1096 }
1097 else {
1098 PointerRNA op_ptr = layout.op("ARMATURE_OT_collection_assign", bcoll->name, icon);
1099 RNA_string_set(&op_ptr, "name", bcoll->name);
1100 }
1101}
1102
1114static void move_to_collection_menu_create(bContext *C, ui::Layout *layout, void *menu_custom_data)
1115{
1116 int parent_bcoll_index;
1117 bool is_move_operation;
1118 std::tie(parent_bcoll_index, is_move_operation) = menu_custom_data_decode(menu_custom_data);
1119
1120 const Object *ob = ed::object::context_object(C);
1121 const bArmature *arm = id_cast<bArmature *>(ob->data);
1122
1123 /* The "Create a new collection" mode of this operator has its own menu, and should thus be
1124 * invoked. */
1126 PointerRNA op_ptr = layout->op(
1127 is_move_operation ? "ARMATURE_OT_move_to_collection" : "ARMATURE_OT_assign_to_collection",
1128 CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "New Bone Collection"),
1129 ICON_ADD);
1130 RNA_int_set(&op_ptr, "collection_index", parent_bcoll_index);
1131
1132 layout->separator();
1133
1134 /* The remaining operators in this menu should be executed on click. Invoking
1135 * them would show this same menu again. */
1137
1138 int child_index, child_count;
1139 if (parent_bcoll_index == -1) {
1140 child_index = 0;
1141 child_count = arm->collection_root_count;
1142 }
1143 else {
1144 /* Add a menu item to assign to the parent first, before listing the children.
1145 * The parent is assumed to be editable, because otherwise the menu would
1146 * have been disabled already one recursion level higher. */
1147 const BoneCollection *parent = arm->collection_array[parent_bcoll_index];
1149 *layout, arm, parent, parent_bcoll_index, is_move_operation);
1150 layout->separator();
1151
1152 child_index = parent->child_index;
1153 child_count = parent->child_count;
1154 }
1155
1156 /* Loop over the children. There should be at least one, otherwise this parent
1157 * bone collection wouldn't have been drawn as a menu. */
1158 for (int index = child_index; index < child_index + child_count; index++) {
1159 const BoneCollection *bcoll = arm->collection_array[index];
1160
1161 /* Avoid assigning/moving to a linked bone collection. */
1162 if (!ANIM_armature_bonecoll_is_editable(arm, bcoll)) {
1163 ui::Layout &sub = layout->row(false);
1164 sub.enabled_set(false);
1165
1166 menu_add_item_for_move_assign_unassign(sub, arm, bcoll, index, is_move_operation);
1167 continue;
1168 }
1169
1170 if (animrig::bonecoll_has_children(bcoll)) {
1171 layout->menu_fn(bcoll->name,
1172 ICON_NONE,
1174 menu_custom_data_encode(index, is_move_operation));
1175 }
1176 else {
1177 menu_add_item_for_move_assign_unassign(*layout, arm, bcoll, index, is_move_operation);
1178 }
1179 }
1180}
1181
1183{
1184 const char *title = CTX_IFACE_(op->type->translation_context, op->type->name);
1185 ui::PopupMenu *pup = ui::popup_menu_begin(C, title, ICON_NONE);
1186 ui::Layout *layout = popup_menu_layout(pup);
1187
1188 const bool is_move_operation = STREQ(op->type->idname, "ARMATURE_OT_move_to_collection");
1189 move_to_collection_menu_create(C, layout, menu_custom_data_encode(-1, is_move_operation));
1190
1191 popup_menu_end(C, pup);
1192
1193 return OPERATOR_INTERFACE;
1194}
1195
1197{
1198 RNA_string_set(op->ptr, "new_collection_name", IFACE_("Bones"));
1200 C, op, 200, IFACE_("Move to New Bone Collection"), IFACE_("Create"));
1201}
1202
1204 wmOperator *op,
1205 const wmEvent * /*event*/)
1206{
1207 /* Invoking with `collection_index` set has a special meaning: show the menu to create a new bone
1208 * collection as the child of this one. */
1209 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "collection_index");
1210 if (RNA_property_is_set(op->ptr, prop)) {
1211 return move_to_new_collection_invoke(C, op);
1212 }
1213
1215}
1216
1218{
1219 PropertyRNA *prop;
1220
1221 /* identifiers */
1222 ot->name = "Move to Collection";
1223 ot->description = "Move bones to a collection";
1224 ot->idname = "ARMATURE_OT_move_to_collection";
1225
1226 /* API callbacks. */
1230
1231 /* Flags don't include OPTYPE_REGISTER, as the redo panel doesn't make much sense for this
1232 * operator. The visibility of the RNA properties is determined by the needs of the 'New Catalog'
1233 * popup, so that a name can be entered. This means that the redo panel would also only show the
1234 * 'Name' property, without any choice for another collection. */
1235 ot->flag = OPTYPE_UNDO;
1236
1237 prop = RNA_def_int(
1238 ot->srna,
1239 "collection_index",
1240 -1,
1241 -1,
1242 INT_MAX,
1243 "Collection Index",
1244 "Index of the collection to move selected bones to. When the operator should create a new "
1245 "bone collection, do not include this parameter and pass new_collection_name",
1246 -1,
1247 INT_MAX);
1249
1250 prop = RNA_def_string(
1251 ot->srna,
1252 "new_collection_name",
1253 nullptr,
1254 MAX_NAME,
1255 "Name",
1256 "Name of a to-be-added bone collection. Only pass this if you want to create a new bone "
1257 "collection and move the selected bones to it. To move to an existing collection, do not "
1258 "include this parameter and use collection_index");
1260 ot->prop = prop;
1261}
1262
1264{
1265 PropertyRNA *prop;
1266
1267 /* identifiers */
1268 ot->name = "Assign to Collection";
1269 ot->description =
1270 "Assign all selected bones to a collection, or unassign them, depending on whether the "
1271 "active bone is already assigned or not";
1272 ot->idname = "ARMATURE_OT_assign_to_collection";
1273
1274 /* API callbacks. */
1278
1279 /* Flags don't include OPTYPE_REGISTER, as the redo panel doesn't make much sense for this
1280 * operator. The visibility of the RNA properties is determined by the needs of the 'New Catalog'
1281 * popup, so that a name can be entered. This means that the redo panel would also only show the
1282 * 'Name' property, without any choice for another collection. */
1283 ot->flag = OPTYPE_UNDO;
1284
1285 prop = RNA_def_int(
1286 ot->srna,
1287 "collection_index",
1288 -1,
1289 -1,
1290 INT_MAX,
1291 "Collection Index",
1292 "Index of the collection to assign selected bones to. When the operator should create a new "
1293 "bone collection, use new_collection_name to define the collection name, and set this "
1294 "parameter to the parent index of the new bone collection",
1295 -1,
1296 INT_MAX);
1298
1299 prop = RNA_def_string(
1300 ot->srna,
1301 "new_collection_name",
1302 nullptr,
1303 MAX_NAME,
1304 "Name",
1305 "Name of a to-be-added bone collection. Only pass this if you want to create a new bone "
1306 "collection and assign the selected bones to it. To assign to an existing collection, do "
1307 "not include this parameter and use collection_index");
1309 ot->prop = prop;
1310}
1311
1312/* ********************************************** */
1313
1314} // namespace blender
Functions to deal with Armatures.
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
Blender kernel action and pose functionality.
#define FOREACH_PCHAN_SELECTED_IN_OBJECT_END
#define FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN(_ob, _pchan)
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define IFACE_(msgid)
ID and Library types, which are fundamental for SDNA.
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define MAX_NAME
Definition DNA_defs.h:63
Object is a sort of wrapper for general info.
#define EBONE_SELECTABLE(arm, ebone)
#define EBONE_EDITABLE(ebone)
#define C
Definition RandGen.cpp:29
#define ND_DATA
Definition WM_types.hh:552
#define ND_BONE_COLLECTION
Definition WM_types.hh:515
#define NC_OBJECT
Definition WM_types.hh:392
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define INT_MAX
void bone_deselect(bPoseChannel *pchan)
bool bone_is_visible(const bArmature *armature, const Bone *bone)
bool bonecoll_has_children(const BoneCollection *bcoll)
int armature_bonecoll_child_number_set(bArmature *armature, BoneCollection *bcoll, int new_child_number)
int armature_bonecoll_find_parent_index(const bArmature *armature, int bcoll_index)
int armature_bonecoll_child_number_find(const bArmature *armature, const BoneCollection *bcoll)
void bone_select(bPoseChannel *pchan)
Object * context_object(const bContext *C)
Object * context_active_object(const bContext *C)
PopupMenu * popup_menu_begin(bContext *C, const char *title, int icon) ATTR_NONNULL()
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
static void bone_collection_assign_pchans(bContext *C, Object *ob, BoneCollection *bcoll, assign_bone_func assign_func, bool *made_any_changes, bool *had_bones_to_assign)
static wmOperatorStatus bone_collection_add_exec(bContext *C, wmOperator *)
void ARMATURE_OT_collection_move(wmOperatorType *ot)
static wmOperatorStatus bone_collection_assign_exec(bContext *C, wmOperator *op)
static bool bone_collection_add_poll(bContext *C)
static void move_to_collection_menu_create(bContext *C, ui::Layout *layout, void *menu_custom_data)
bool ANIM_armature_bonecoll_assign(BoneCollection *bcoll, Bone *bone)
static wmOperatorStatus bone_collection_create_and_assign_exec(bContext *C, wmOperator *op)
int RNA_enum_get(PointerRNA *ptr, const char *name)
static bool bone_collection_create_and_assign_poll(bContext *C)
bool ANIM_armature_bonecoll_assign_editbone(BoneCollection *bcoll, EditBone *ebone)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
@ RPT_WARNING
Definition BKE_report.hh:40
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void ED_outliner_select_sync_from_pose_bone_tag(bContext *C)
static bool bone_collection_assign_named_mode_specific(bContext *C, Object *ob, BoneCollection *bcoll, const char *bone_name, assign_bone_func assign_bone_func, assign_ebone_func assign_ebone_func, bool *made_any_changes, bool *had_bones_to_assign)
static bool active_bone_collection_poll(bContext *C)
static wmOperatorStatus bone_collection_select_exec(bContext *C, wmOperator *)
void ARMATURE_OT_collection_select(wmOperatorType *ot)
bool(*)(BoneCollection *bcoll, EditBone *ebone) assign_ebone_func
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
BoneCollection * ANIM_armature_bonecoll_new(bArmature *armature, const char *name, int parent_index=-1)
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
static void * menu_custom_data_encode(const int bcoll_index, const bool is_move_operation)
void ANIM_armature_bonecoll_remove(bArmature *armature, BoneCollection *bcoll)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
static wmOperatorStatus move_to_collection_invoke(bContext *C, wmOperator *op, const wmEvent *)
int RNA_int_get(PointerRNA *ptr, const char *name)
bool ANIM_armature_bonecoll_assign_and_move(BoneCollection *bcoll, Bone *bone)
enum eContextObjectMode CTX_data_mode_enum(const bContext *C)
void ARMATURE_OT_collection_add(wmOperatorType *ot)
void ANIM_armature_bonecoll_active_runtime_refresh(bArmature *armature)
bool BKE_lib_override_library_is_system_defined(const Main *bmain, const ID *id)
@ CTX_MODE_EDIT_ARMATURE
Object * ED_pose_object_from_context(bContext *C)
Definition pose_edit.cc:62
void ARMATURE_OT_assign_to_collection(wmOperatorType *ot)
static int icon_for_bone_collection(const bool collection_contains_active_bone)
void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
void ARMATURE_OT_collection_assign(wmOperatorType *ot)
static bool bone_collection_assign_poll(bContext *C)
static bool armature_bone_select_poll(bContext *C)
static void bone_collection_assign_editbones(bContext *C, Object *ob, BoneCollection *bcoll, assign_ebone_func assign_func, bool *made_any_changes, bool *had_bones_to_assign)
static BoneCollection * add_or_move_to_collection_bcoll(wmOperator *op, bArmature *arm)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
void ED_armature_edit_sync_selection(ListBaseT< EditBone > *edbo)
static void menu_add_item_for_move_assign_unassign(ui::Layout &layout, const bArmature *arm, const BoneCollection *bcoll, const int bcoll_index, const bool is_move_operation)
static wmOperatorStatus assign_to_collection_exec(bContext *C, wmOperator *op)
bool ANIM_armature_bonecoll_unassign_editbone(BoneCollection *bcoll, EditBone *ebone)
static wmOperatorStatus bone_collection_unassign_exec(bContext *C, wmOperator *op)
bArmature * ED_armature_context(const bContext *C)
BoneCollection * ANIM_armature_bonecoll_get_by_name(bArmature *armature, const char *name) ATTR_WARN_UNUSED_RESULT
static bool move_to_collection_poll(bContext *C)
void ARMATURE_OT_move_to_collection(wmOperatorType *ot)
bool ANIM_armature_bonecoll_contains_active_bone(const bArmature *armature, const BoneCollection *bcoll)
wmOperatorStatus WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, std::optional< std::string > title, std::optional< std::string > confirm_text, const bool cancel_default, std::optional< std::string > message, const bool show_icon)
static wmOperatorStatus bone_collection_move_exec(bContext *C, wmOperator *op)
bool(*)(BoneCollection *bcoll, Bone *bone) assign_bone_func
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static wmOperatorStatus add_or_move_to_collection_exec(bContext *C, wmOperator *op, const assign_bone_func assign_func_bone, const assign_ebone_func assign_func_ebone)
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
bool ANIM_armature_bonecoll_assign_and_move_editbone(BoneCollection *bcoll, EditBone *ebone)
static wmOperatorStatus bone_collection_unassign_named_exec(bContext *C, wmOperator *op)
static BoneCollection * get_bonecoll_named_or_active(bContext *, wmOperator *op, Object *ob)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
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)
static wmOperatorStatus move_to_new_collection_invoke(bContext *C, wmOperator *op)
static bool editbone_is_member(const EditBone *ebone, const BoneCollection *bcoll)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void ANIM_armature_bonecoll_active_set(bArmature *armature, BoneCollection *bcoll)
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
void ARMATURE_OT_collection_deselect(wmOperatorType *ot)
static wmOperatorStatus bone_collection_deselect_exec(bContext *C, wmOperator *)
void ED_outliner_select_sync_from_edit_bone_tag(bContext *C)
static wmOperatorStatus bone_collection_remove_exec(bContext *C, wmOperator *)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
void ARMATURE_OT_collection_create_and_assign(wmOperatorType *ot)
std::string RNA_string_get(PointerRNA *ptr, const char *name)
bool ANIM_armature_bonecoll_unassign(BoneCollection *bcoll, Bone *bone)
static bool bone_collection_assign_mode_specific(bContext *C, Object *ob, BoneCollection *bcoll, assign_bone_func assign_bone_func, assign_ebone_func assign_ebone_func, bool *made_any_changes, bool *had_bones_to_assign)
static wmOperatorStatus move_to_collection_regular_invoke(bContext *C, wmOperator *op)
static wmOperatorStatus move_to_collection_exec(bContext *C, wmOperator *op)
void ARMATURE_OT_collection_remove(wmOperatorType *ot)
static std::pair< int, bool > menu_custom_data_decode(void *menu_custom_data)
static void bone_collection_select(bContext *C, bArmature *armature, BoneCollection *bcoll, const bool select)
void ED_armature_ebone_select_set(EditBone *ebone, bool select)
bool ANIM_armature_bonecoll_move(bArmature *armature, BoneCollection *bcoll, int step)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
void ARMATURE_OT_collection_unassign_named(wmOperatorType *ot)
EditBone * ED_armature_ebone_find_name(const ListBaseT< EditBone > *edbo, const char *name)
bool ANIM_armature_bonecoll_is_editable(const bArmature *armature, const BoneCollection *bcoll)
_W64 int intptr_t
Definition BKE_text.h:119
void ARMATURE_OT_collection_unassign(wmOperatorType *ot)
ListBaseT< BoneCollectionMember > bones
ListBaseT< BoneCollectionReference > bone_collections
char name[258]
Definition DNA_ID.h:433
struct bPose * pose
ListBaseT< EditBone > * edbo
bke::bArmature_Runtime * runtime
struct BoneCollection ** collection_array
void enabled_set(bool enabled)
void operator_context_set(wm::OpCallContext opcontext)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
Layout & row(bool align)
void menu_fn(StringRefNull name, int icon, MenuCreateFunc func, void *arg)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
const char * translation_context
Definition WM_types.hh:1113
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
struct ReportList * reports
struct wmOperatorType * type