Blender V5.3
animrig/intern/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
8
9#include "BLI_listbase.hh"
10#include "BLI_map.hh"
11#include "BLI_string.hh"
12#include "BLI_string_utf8.hh"
13#include "BLI_string_utils.hh"
14#include "BLI_utildefines.hh"
15
16#include "BLT_translation.hh"
17
18#include "DNA_armature_types.h"
19
20#include "MEM_guardedalloc.h"
21
22#include "BKE_animsys.hh"
23#include "BKE_global.hh"
24#include "BKE_idprop.hh"
25#include "BKE_lib_id.hh"
26#include "BKE_lib_override.hh"
27
28#include "RNA_path.hh"
29
30#include "ANIM_armature_iter.hh"
32#include "WM_api.hh"
33
35
36#include <cstring>
37
38namespace blender {
39
40using std::strcmp;
41
42using namespace blender::animrig;
43
44namespace {
45
47constexpr eBoneCollection_Flag default_flags = BONE_COLLECTION_VISIBLE |
50constexpr auto bonecoll_default_name = "Bones";
51
52} // namespace
53
54static void ancestors_visible_update(bArmature *armature,
55 const BoneCollection *parent_bcoll,
56 BoneCollection *bcoll);
57
59{
60 if (name == nullptr || name[0] == '\0') {
61 /* Use a default name if no name was given. */
62 name = DATA_(bonecoll_default_name);
63 }
64
65 /* NOTE: the collection name may change after the collection is added to an
66 * armature, to ensure it is unique within the armature. */
67 BoneCollection *bcoll = MEM_new<BoneCollection>(__func__);
68
69 STRNCPY_UTF8(bcoll->name, name);
70 bcoll->flags = default_flags;
71
72 return bcoll;
73}
74
75void ANIM_bonecoll_free(BoneCollection *bcoll, const bool do_id_user_count)
76{
77 BLI_assert_msg(bcoll->bones.is_empty(),
78 "bone collection still has bones assigned to it, will cause dangling pointers in "
79 "bone runtime data");
80 if (bcoll->prop) {
81 IDP_FreeProperty_ex(bcoll->prop, do_id_user_count);
82 }
83 if (bcoll->system_properties) {
84 IDP_FreeProperty_ex(bcoll->system_properties, do_id_user_count);
85 }
86 MEM_delete(bcoll);
87}
88
96{
97 for (BoneCollectionMember &member : bcoll->bones) {
98 BoneCollectionReference *ref = MEM_new<BoneCollectionReference>(__func__);
99 ref->bcoll = bcoll;
100 BLI_addtail(&member.bone->runtime.collections, ref);
101 }
102}
103
105{
108
109 /* Make sure the BONE_COLLECTION_ANCESTORS_VISIBLE flags are set correctly. */
110 for (BoneCollection *bcoll : armature->collections_roots()) {
111 ancestors_visible_update(armature, nullptr, bcoll);
112 }
113
114 /* Construct the bone-to-collections mapping. */
115 for (BoneCollection *bcoll : armature->collections_span()) {
117 }
118}
119
121{
122 /* Free the bone-to-its-collections mapping. */
124 [&](Bone *bone) { bone->runtime.collections.free_no_destruct(); });
125}
126
133{
134 /* Cannot capture armature & bcoll by reference in the lambda, as that would change its signature
135 * and no longer be compatible with BLI_uniquename_cb(). */
136 auto bonecoll_name_is_duplicate = [&](const StringRef name) -> bool {
137 for (BoneCollection *bcoll_iter : armature->collections_span()) {
138 if (bcoll_iter != bcoll && bcoll_iter->name == name) {
139 return true;
140 }
141 }
142 return false;
143 };
144
145 BLI_uniquename_cb(bonecoll_name_is_duplicate,
146 DATA_(bonecoll_default_name),
147 '.',
148 bcoll->name,
149 sizeof(bcoll->name));
150}
151
159static void bonecoll_insert_at_index(bArmature *armature, BoneCollection *bcoll, const int index)
160{
161 BLI_assert(index <= armature->collection_array_num);
162
163 armature->collection_array = reinterpret_cast<BoneCollection **>(
165 sizeof(BoneCollection *) * (armature->collection_array_num + 1),
166 __func__));
167
168 /* To keep the memory consistent, insert the new element at the end of the
169 * now-grown array, then rotate it into place. */
170 armature->collection_array[armature->collection_array_num] = bcoll;
171 armature->collection_array_num++;
172
173 const int rotate_count = armature->collection_array_num - index - 1;
174 animrig::internal::bonecolls_rotate_block(armature, index, rotate_count, +1);
175
176 if (armature->runtime->active_collection_index >= index) {
178 armature->runtime->active_collection_index + 1);
179 }
180}
181
182static void bonecoll_insert_as_root(bArmature *armature, BoneCollection *bcoll, int at_index)
183{
184 BLI_assert(at_index >= -1);
185 BLI_assert(at_index <= armature->collection_root_count);
186 if (at_index < 0) {
187 at_index = armature->collection_root_count;
188 }
189
190 bonecoll_insert_at_index(armature, bcoll, at_index);
191 armature->collection_root_count++;
192
193 ancestors_visible_update(armature, nullptr, bcoll);
194}
195
197 BoneCollection *bcoll,
198 const int parent_index)
199{
200 BLI_assert_msg(parent_index >= 0, "Armature bone collection index should be 0 or larger");
201 BLI_assert_msg(parent_index < armature->collection_array_num,
202 "Parent bone collection index should not point beyond the end of the array");
203
204 BoneCollection *parent = armature->collection_array[parent_index];
205 if (parent->child_index == 0) {
206 /* This parent doesn't have any children yet, so place them at the end of the array. */
207 parent->child_index = armature->collection_array_num;
208 }
209 const int insert_at_index = parent->child_index + parent->child_count;
210 bonecoll_insert_at_index(armature, bcoll, insert_at_index);
211 parent->child_count++;
212
213 ancestors_visible_update(armature, parent, bcoll);
214
215 return insert_at_index;
216}
217
219 const char *name,
220 const int parent_index)
221{
222 BoneCollection *bcoll = ANIM_bonecoll_new(name);
223
224 if (!ID_IS_LINKED(&armature->id) && ID_IS_OVERRIDE_LIBRARY(&armature->id)) {
225 /* Mark this bone collection as local override, so that certain operations can be allowed. */
227 }
228
229 bonecoll_ensure_name_unique(armature, bcoll);
230
231 if (parent_index < 0) {
232 bonecoll_insert_as_root(armature, bcoll, -1);
233 }
234 else {
235 bonecoll_insert_as_child(armature, bcoll, parent_index);
236 }
237
238 /* Restore the active bone collection pointer, as its index might have changed. */
240
241 return bcoll;
242}
243
255 const BoneCollection *bcoll_to_copy)
256{
257 BoneCollection *bcoll = MEM_dupalloc(bcoll_to_copy);
258
259 /* Reset the child_index and child_count properties. These are unreliable when
260 * coming from an override, as the original array might have been completely
261 * reshuffled. Children will have to be copied separately. */
262 bcoll->child_index = 0;
263 bcoll->child_count = 0;
264
265 if (bcoll->prop) {
266 bcoll->prop = IDP_CopyProperty_ex(bcoll_to_copy->prop,
267 0 /*do_id_user ? 0 : LIB_ID_CREATE_NO_USER_REFCOUNT*/);
268 }
269 if (bcoll->system_properties) {
271 bcoll_to_copy->system_properties, 0 /*do_id_user ? 0 : LIB_ID_CREATE_NO_USER_REFCOUNT*/);
272 }
273
274 /* Remap the bone pointers to the given armature, as `bcoll_to_copy` is
275 * assumed to be owned by another armature. */
276 BLI_duplicatelist(&bcoll->bones, &bcoll->bones);
277 BLI_assert_msg(armature_dst->bonehash, "Expected armature bone hash to be there");
278 for (BoneCollectionMember &member : bcoll->bones) {
279 member.bone = BKE_armature_find_bone_name(const_cast<bArmature *>(armature_dst),
280 member.bone->name);
281 }
282
283 /* Now that the collection points to the right bones, these bones can be
284 * updated to point to this collection. */
286
287 return bcoll;
288}
289
296 const bArmature *armature_src,
297 const int parent_bcoll_dst_index,
298 const BoneCollection *parent_bcoll_src)
299{
300 BLI_assert_msg(parent_bcoll_dst_index >= 0,
301 "this function can only add children to another collection, it cannot add roots");
302
303 /* Iterate over the children in `armature_src`, and clone them one by one into `armature_dst`.
304 *
305 * This uses two loops. The first one adds all the children, the second loop iterates over those
306 * children for the recursion step. As this performs a "breadth-first insertion", it requires
307 * considerably less shuffling of the array as when the recursion was done immediately after
308 * inserting a child. */
309 BoneCollection *parent_bcoll_dst = armature_dst->collection_array[parent_bcoll_dst_index];
310
311 /* Big Fat Assumption: because this code runs as part of the library override system, it is
312 * assumed that the parent is either a newly added root, or another child that was also added by
313 * the liboverride system. Because this would never add a child to an original "sequence of
314 * siblings", insertions of children always happen at the end of the array. This means that
315 * `parent_bcoll_dst_index` remains constant during this entire function. */
316
317 /* Copy & insert all the children. */
318 for (int bcoll_src_index = parent_bcoll_src->child_index;
319 bcoll_src_index < parent_bcoll_src->child_index + parent_bcoll_src->child_count;
320 bcoll_src_index++)
321 {
322 const BoneCollection *bcoll_src = armature_src->collection_array[bcoll_src_index];
323 BoneCollection *bcoll_dst = copy_and_update_ownership(armature_dst, bcoll_src);
324
325 const int bcoll_index_dst = bonecoll_insert_as_child(
326 armature_dst, bcoll_dst, parent_bcoll_dst_index);
327
328#ifndef NDEBUG
329 /* Check that the above Big Fat Assumption holds. */
330 BLI_assert_msg(bcoll_index_dst > parent_bcoll_dst_index,
331 "expecting children to be added to the array AFTER their parent");
332#else
333 (void)bcoll_index_dst;
334#endif
335
336 bonecoll_ensure_name_unique(armature_dst, bcoll_dst);
337 }
338
339 /* Double-check that the above Big Fat Assumption holds. */
340#ifndef NDEBUG
341 const int new_parent_bcoll_dst_index = armature_bonecoll_find_index(armature_dst,
342 parent_bcoll_dst);
343 BLI_assert_msg(new_parent_bcoll_dst_index == parent_bcoll_dst_index,
344 "did not expect parent_bcoll_dst_index to change");
345#endif
346
347 /* Recurse into the children to copy grandchildren. */
348 BLI_assert_msg(parent_bcoll_dst->child_count == parent_bcoll_src->child_count,
349 "all children should have been copied");
350 for (int child_num = 0; child_num < parent_bcoll_dst->child_count; child_num++) {
351 const int bcoll_src_index = parent_bcoll_src->child_index + child_num;
352 const int bcoll_dst_index = parent_bcoll_dst->child_index + child_num;
353
354 const BoneCollection *bcoll_src = armature_src->collection_array[bcoll_src_index];
355 liboverride_recursively_add_children(armature_dst, armature_src, bcoll_dst_index, bcoll_src);
356 }
357}
358
360 const bArmature *armature_src,
361 const BoneCollection *anchor_in_dst,
362 const BoneCollection *bcoll_to_copy)
363{
364#ifndef NDEBUG
365 /* Check that this bone collection is really a root, as this is assumed by the
366 * rest of this function. This is an O(n) check, though, so that's why it's
367 * only running in debug builds. */
368 const int bcoll_index_src = armature_bonecoll_find_index(armature_src, bcoll_to_copy);
369 if (!armature_bonecoll_is_root(armature_src, bcoll_index_src)) {
370 printf(
371 "Armature \"%s\" has library override operation that adds non-root bone collection "
372 "\"%s\". This is unexpected, please file a bug report.\n",
373 armature_src->id.name + 2,
374 bcoll_to_copy->name);
375 }
376#endif
377
378 BoneCollection *bcoll = copy_and_update_ownership(armature_dst, bcoll_to_copy);
379
380 const int anchor_index = armature_bonecoll_find_index(armature_dst, anchor_in_dst);
381 const int bcoll_index = anchor_index + 1;
383 bcoll_index <= armature_dst->collection_root_count,
384 "did not expect library override to add a child bone collection, only roots are expected");
385 bonecoll_insert_as_root(armature_dst, bcoll, bcoll_index);
386 bonecoll_ensure_name_unique(armature_dst, bcoll);
387
388 /* Library override operations are only constructed for the root bones. This means that handling
389 * this operation should also include copying the children. */
390 liboverride_recursively_add_children(armature_dst, armature_src, bcoll_index, bcoll_to_copy);
391
393 return bcoll;
394}
395
397{
398 armature->runtime->active_collection_index = -1;
399 armature->runtime->active_collection = nullptr;
400 armature->active_collection_name[0] = '\0';
401}
402
404{
405 if (bcoll == nullptr) {
407 return;
408 }
409
410 const int index = armature_bonecoll_find_index(armature, bcoll);
411 if (index == -1) {
412 /* TODO: print warning? Or just ignore this case? */
414 return;
415 }
416
417 STRNCPY(armature->active_collection_name, bcoll->name);
419 armature->runtime->active_collection = bcoll;
420}
421
422void ANIM_armature_bonecoll_active_index_set(bArmature *armature, const int bone_collection_index)
423{
424 if (bone_collection_index < 0 || bone_collection_index >= armature->collection_array_num) {
426 return;
427 }
428
429 BoneCollection *bcoll = armature->collection_array[bone_collection_index];
430
431 STRNCPY(armature->active_collection_name, bcoll->name);
432 armature->runtime->active_collection_index = bone_collection_index;
433 armature->runtime->active_collection = bcoll;
434}
435
436void ANIM_armature_bonecoll_active_name_set(bArmature *armature, const char *name)
437{
438 BoneCollection *bcoll = ANIM_armature_bonecoll_get_by_name(armature, name);
439 ANIM_armature_bonecoll_active_set(armature, bcoll);
440}
441
443{
444 const std::string_view active_name = armature->active_collection_name;
445 if (active_name.empty()) {
447 return;
448 }
449
450 int index = 0;
451 for (BoneCollection *bcoll : armature->collections_span()) {
452 if (bcoll->name == active_name) {
454 armature->runtime->active_collection = bcoll;
455 return;
456 }
457 index++;
458 }
459
460 /* No bone collection with the name was found, so better to clear everything. */
462}
463
465{
466 const bool is_override = ID_IS_OVERRIDE_LIBRARY(armature);
467 if (ID_IS_LINKED(armature) && !is_override) {
468 return false;
469 }
470
471 if (is_override && BKE_lib_override_library_is_system_defined(nullptr, &armature->id)) {
472 /* A system override is still not editable. */
473 return false;
474 }
475
476 if (is_override && (bcoll->flags & BONE_COLLECTION_OVERRIDE_LIBRARY_LOCAL) == 0) {
477 /* This particular collection was not added in the local override, so not editable. */
478 return false;
479 }
480 return true;
481}
482
484 const int from_index,
485 const int to_index)
486{
487 if (from_index >= armature->collection_array_num || to_index >= armature->collection_array_num ||
488 from_index == to_index)
489 {
490 return false;
491 }
492
493 /* Only allow moving within the same parent. This is written a bit awkwardly to avoid two calls
494 * to `armature_bonecoll_find_parent_index()` as that is O(n) in the number of bone collections.
495 */
496 const int parent_index = armature_bonecoll_find_parent_index(armature, from_index);
497 if (!armature_bonecoll_is_child_of(armature, parent_index, to_index)) {
498 return false;
499 }
500
501 if (parent_index < 0) {
502 /* Roots can just be moved around, as there is no `child_index` to update in this case. */
503 animrig::internal::bonecolls_move_to_index(armature, from_index, to_index);
504 return true;
505 }
506
507 /* Store the parent's child_index, as that might move if to_index is the first child
508 * (bonecolls_move_to_index() will keep it pointing at that first child). */
509 BoneCollection *parent_bcoll = armature->collection_array[parent_index];
510 const int old_parent_child_index = parent_bcoll->child_index;
511
512 animrig::internal::bonecolls_move_to_index(armature, from_index, to_index);
513
514 parent_bcoll->child_index = old_parent_child_index;
515
516 return true;
517}
518
519static int bonecoll_child_number(const bArmature *armature,
520 const int parent_bcoll_index,
521 const int bcoll_index)
522{
523 if (parent_bcoll_index < 0) {
524 /* Root bone collections are always at the start of the array, and thus their index is the
525 * 'child number'. */
526 return bcoll_index;
527 }
528
529 const BoneCollection *parent_bcoll = armature->collection_array[parent_bcoll_index];
530 return bcoll_index - parent_bcoll->child_index;
531}
532
534 const int from_index,
535 int to_index,
536 const MoveLocation before_after)
537{
538 const int from_parent_index = armature_bonecoll_find_parent_index(armature, from_index);
539 const int to_parent_index = armature_bonecoll_find_parent_index(armature, to_index);
540
541 if (from_parent_index != to_parent_index) {
542 /* Moving between parents. */
543 int to_child_num = bonecoll_child_number(armature, to_parent_index, to_index);
544 if (before_after == MoveLocation::After) {
545 to_child_num++;
546 }
547
549 armature, from_index, to_child_num, from_parent_index, to_parent_index);
550 }
551
552 /* Moving between siblings. */
553 switch (before_after) {
555 if (to_index > from_index) {
556 /* Moving to the right, but needs to go before that one, so needs a decrement. */
557 to_index--;
558 }
559 break;
560
562 if (to_index < from_index) {
563 /* Moving to the left, but needs to go after that one, so needs a decrement. */
564 to_index++;
565 }
566 break;
567 }
568
569 if (!ANIM_armature_bonecoll_move_to_index(armature, from_index, to_index)) {
570 return -1;
571 }
572 return to_index;
573}
574
576{
577 if (bcoll == nullptr) {
578 return false;
579 }
580
581 const int bcoll_index = armature_bonecoll_find_index(armature, bcoll);
582 const int to_index = bcoll_index + step;
583 if (bcoll_index < 0 || to_index < 0 || to_index >= armature->collection_array_num) {
584 return false;
585 }
586
587 ANIM_armature_bonecoll_move_to_index(armature, bcoll_index, to_index);
588
589 return true;
590}
591
593 bArmature *armature,
594 BoneCollection *bcoll,
595 const char *name)
596{
597 char old_name[sizeof(bcoll->name)];
598
599 STRNCPY(old_name, bcoll->name);
600
601 if (name[0] == '\0') {
602 /* Refuse to have nameless collections. The name of the active collection is stored in DNA, and
603 * an empty string means 'no active collection'. */
604 STRNCPY_UTF8(bcoll->name, DATA_(bonecoll_default_name));
605 }
606 else {
607 STRNCPY_UTF8(bcoll->name, name);
608 }
609
610 bonecoll_ensure_name_unique(armature, bcoll);
611
612 if (armature->runtime->active_collection == bcoll) {
613 STRNCPY(armature->active_collection_name, bcoll->name);
614 }
615
616 /* Bone collections can be reached via .collections (4.0+) and .collections_all (4.1+).
617 * Animation data from 4.0 should have been versioned to only use `.collections_all`. */
618 const DriverMap driver_map = BKE_animdata_build_driver_target_map(bmain);
619 BKE_animdata_fix_paths(armature->id,
620 "collections",
621 RNA_path_name_to_infix(old_name),
623 /*verify_paths=*/true,
624 driver_map);
625 BKE_animdata_fix_paths(armature->id,
626 "collections_all",
627 RNA_path_name_to_infix(old_name),
629 /*verify_paths=*/true,
630 driver_map);
631}
632
634{
635 BLI_assert(0 <= index && index < armature->collection_array_num);
636
637 BoneCollection *bcoll = armature->collection_array[index];
638
639 /* Get the active bone collection index before the armature is manipulated. */
640 const int active_collection_index = armature->runtime->active_collection_index;
641
642 /* The parent needs updating, so better to find it before this bone collection is removed. */
643 int parent_bcoll_index = armature_bonecoll_find_parent_index(armature, index);
644 BoneCollection *parent_bcoll = parent_bcoll_index >= 0 ?
645 armature->collection_array[parent_bcoll_index] :
646 nullptr;
647
648 /* Move all the children of the to-be-removed bone collection to their grandparent. */
649 int move_to_child_num = bonecoll_child_number(armature, parent_bcoll_index, index);
650 while (bcoll->child_count > 0) {
651 /* Move the child to its grandparent, at the same spot as the to-be-removed
652 * bone collection. The latter thus (potentially) shifts by 1 in the array.
653 * After removal, this effectively makes it appear like the removed bone
654 * collection is replaced by all its children. */
656 bcoll->child_index, /* Move from index... */
657 move_to_child_num, /* to this child number. */
658 index, /* From this parent... */
659 parent_bcoll_index /* to that parent. */
660 );
661
662 /* Both 'index' and 'parent_bcoll_index' can change each iteration. */
664 BLI_assert_msg(index >= 0, "could not find bone collection after moving things around");
665
666 if (parent_bcoll_index >= 0) { /* If there is no parent, its index should stay -1. */
668 armature, parent_bcoll, parent_bcoll_index);
669 BLI_assert_msg(parent_bcoll_index >= 0,
670 "could not find bone collection parent after moving things around");
671 }
672
673 move_to_child_num++;
674 }
675
676 /* Adjust the parent for the removal of its child. */
677 if (parent_bcoll_index < 0) {
678 /* Removing a root, so the armature itself needs to be updated. */
679 armature->collection_root_count--;
680 BLI_assert_msg(armature->collection_root_count >= 0, "armature root count cannot be negative");
681 }
682 else {
683 parent_bcoll->child_count--;
684 if (parent_bcoll->child_count == 0) {
685 parent_bcoll->child_index = 0;
686 }
687 }
688
689 /* Rotate the to-be-removed collection to the last array element. */
691
692 /* NOTE: we don't bother to shrink the allocation. It's okay if the
693 * capacity has extra space, because the number of valid items is tracked. */
694 armature->collection_array_num--;
695 armature->collection_array[armature->collection_array_num] = nullptr;
696
697 /* Update the active BoneCollection. */
698 if (active_collection_index >= 0) {
699 /* Default: select the next sibling.
700 * If there is none: select the previous sibling.
701 * If there is none: select the parent.
702 */
703 if (armature_bonecoll_is_child_of(armature, parent_bcoll_index, active_collection_index)) {
704 /* active_collection_index still points to a sibling of the removed collection. */
705 ANIM_armature_bonecoll_active_index_set(armature, active_collection_index);
706 }
707 else if (active_collection_index > 0 &&
709 armature, parent_bcoll_index, active_collection_index - 1))
710 {
711 /* The child preceding active_collection_index is a sibling of the removed collection. */
712 ANIM_armature_bonecoll_active_index_set(armature, active_collection_index - 1);
713 }
714 else {
715 /* Select the parent, or nothing if this was a root collection. In that case, if there are no
716 * siblings either, this just means all bone collections have been removed. */
717 ANIM_armature_bonecoll_active_index_set(armature, parent_bcoll_index);
718 }
719 }
720
721 const bool is_solo = bcoll->is_solo();
723 if (is_solo) {
724 /* This might have been the last solo'ed bone collection, so check whether
725 * solo'ing should still be active on the armature. */
727 }
729}
730
736
737template<typename MaybeConstBoneCollection>
738static MaybeConstBoneCollection *bonecolls_get_by_name(Span<MaybeConstBoneCollection *> bonecolls,
739 const char *name)
740{
741 for (MaybeConstBoneCollection *bcoll : bonecolls) {
742 if (STREQ(bcoll->name, name)) {
743 return bcoll;
744 }
745 }
746 return nullptr;
747}
748
750{
751 return bonecolls_get_by_name(armature->collections_span(), name);
752}
753
755{
756 for (int index = 0; index < armature->collection_array_num; index++) {
757 const BoneCollection *bcoll = armature->collection_array[index];
758 if (STREQ(bcoll->name, name)) {
759 return index;
760 }
761 }
762 return -1;
763}
764
767{
768 for (BoneCollection *bcoll : armature->collection_children(parent_bcoll)) {
771 }
772}
773
776{
777 if (!parent_bcoll->is_visible_with_ancestors()) {
778 /* If this bone collection is not visible itself, or any of its ancestors are
779 * invisible, all descendants have an invisible ancestor. */
780 ancestors_visible_descendants_clear(armature, parent_bcoll);
781 return;
782 }
783
784 /* parent_bcoll is visible, and so are its ancestors. This means that all direct children have
785 * visible ancestors. The grandchildren depend on the children's visibility as well, hence the
786 * recursion. */
787 for (BoneCollection *bcoll : armature->collection_children(parent_bcoll)) {
788 bcoll->flags |= BONE_COLLECTION_ANCESTORS_VISIBLE;
790 }
791}
792
795 const BoneCollection *parent_bcoll,
796 BoneCollection *bcoll)
797{
798 if (parent_bcoll == nullptr || parent_bcoll->is_visible_with_ancestors()) {
800 }
801 else {
803 }
805}
806
808{
811}
812
814{
817}
818
820 BoneCollection *bcoll,
821 const bool is_visible)
822{
823 if (is_visible) {
824 ANIM_bonecoll_show(armature, bcoll);
825 }
826 else {
827 ANIM_bonecoll_hide(armature, bcoll);
828 }
829}
830
832 BoneCollection *bcoll,
833 const bool is_solo)
834{
835 if (is_solo) {
836 /* Enabling solo is simple. */
837 bcoll->flags |= BONE_COLLECTION_SOLO;
838 armature->flag |= ARM_BCOLL_SOLO_ACTIVE;
839 return;
840 }
841
842 /* Disabling is harder, as the armature flag can only be disabled when there
843 * are no more bone collections with the SOLO flag set. */
846}
847
849{
850 bool any_bcoll_solo = false;
851 for (const BoneCollection *bcoll : armature->collections_span()) {
852 if (bcoll->flags & BONE_COLLECTION_SOLO) {
853 any_bcoll_solo = true;
854 break;
855 }
856 }
857
858 if (any_bcoll_solo) {
859 armature->flag |= ARM_BCOLL_SOLO_ACTIVE;
860 }
861 else {
862 armature->flag &= ~ARM_BCOLL_SOLO_ACTIVE;
863 }
864}
865
867 const BoneCollection *bcoll)
868{
869 const bool is_solo_active = armature->flag & ARM_BCOLL_SOLO_ACTIVE;
870
871 if (is_solo_active) {
872 /* If soloing is active, nothing in the hierarchy matters except the solo flag. */
873 return bcoll->is_solo();
874 }
875
876 return bcoll->is_visible_with_ancestors();
877}
878
880{
881 if (is_expanded) {
883 }
884 else {
886 }
887}
888
889/* Store the bone's membership on the collection. */
890static void add_membership(BoneCollection *bcoll, Bone *bone)
891{
892 BoneCollectionMember *member = MEM_new<BoneCollectionMember>(__func__);
893 member->bone = bone;
894 BLI_addtail(&bcoll->bones, member);
895}
896/* Store reverse membership on the bone. */
897static void add_reference(Bone *bone, BoneCollection *bcoll)
898{
899 BoneCollectionReference *ref = MEM_new<BoneCollectionReference>(__func__);
900 ref->bcoll = bcoll;
901 BLI_addtail(&bone->runtime.collections, ref);
902}
903
905{
906 /* Precondition check: bail out if already a member. */
907 for (BoneCollectionMember &member : bcoll->bones) {
908 if (member.bone == bone) {
909 return false;
910 }
911 }
912
913 add_membership(bcoll, bone);
914 add_reference(bone, bcoll);
915
916 return true;
917}
918
920{
921 /* Precondition check: bail out if already a member. */
922 for (BoneCollectionReference &ref : ebone->bone_collections) {
923 if (ref.bcoll == bcoll) {
924 return false;
925 }
926 }
927
928 /* Store membership on the edit bone. Bones will be rebuilt when the armature
929 * goes out of edit mode, and by then the newly created bones will be added to
930 * the actual collection on the Armature. */
931 BoneCollectionReference *ref = MEM_new<BoneCollectionReference>(__func__);
932 ref->bcoll = bcoll;
933 BLI_addtail(&ebone->bone_collections, ref);
934
935 return true;
936}
937
944
950
956
958{
959 bool was_found = false;
960
961 /* Remove membership from collection. */
962 for (BoneCollectionMember &member : bcoll->bones.items_mutable()) {
963 if (member.bone == bone) {
964 BLI_freelinkN(&bcoll->bones, &member);
965 was_found = true;
966 break;
967 }
968 }
969
970 /* Remove reverse membership from the bone.
971 * For data consistency sake, this is always done, regardless of whether the
972 * above loop found the membership. */
973 for (BoneCollectionReference &ref : bone->runtime.collections.items_mutable()) {
974 if (ref.bcoll == bcoll) {
975 BLI_freelinkN(&bone->runtime.collections, &ref);
976 break;
977 }
978 }
979
980 return was_found;
981}
982
984{
985 for (BoneCollectionReference &ref : bone->runtime.collections.items_mutable()) {
986 /* TODO: include Armature as parameter, and check that the bone collection to unassign from is
987 * actually editable. */
988 ANIM_armature_bonecoll_unassign(ref.bcoll, bone);
989 }
990}
991
993{
994 for (BoneCollectionReference &ref : ebone->bone_collections.items_mutable()) {
996 }
997}
998
1000{
1001 bool was_found = false;
1002
1003 /* Edit bone membership is only stored on the edit bone itself. */
1004 for (BoneCollectionReference &ref : ebone->bone_collections.items_mutable()) {
1005 if (ref.bcoll == bcoll) {
1006 BLI_freelinkN(&ebone->bone_collections, &ref);
1007 was_found = true;
1008 break;
1009 }
1010 }
1011 return was_found;
1012}
1013
1015{
1016 /* Remove all the old collection memberships. */
1017 for (BoneCollection *bcoll : armature->collections_span()) {
1018 bcoll->bones.free_no_destruct();
1019 }
1020
1021 /* For all bones, restore their collection memberships. */
1022 ANIM_armature_foreach_bone(&armature->bonebase, [&](Bone *bone) {
1023 for (BoneCollectionReference &ref : bone->runtime.collections) {
1024 add_membership(ref.bcoll, bone);
1025 }
1026 });
1027}
1028
1029static bool any_bone_collection_visible(const bArmature *armature,
1030 const ListBaseT<BoneCollectionReference> *collection_refs)
1031{
1032 /* Special case: Hide bone when solo is active and it doesn't belong to any collection, see:
1033 * #137090. */
1034 if (collection_refs->is_empty() && !(armature->flag & ARM_BCOLL_SOLO_ACTIVE)) {
1035 return true;
1036 }
1037
1038 for (const BoneCollectionReference &bcoll_ref : *collection_refs) {
1039 const BoneCollection *bcoll = bcoll_ref.bcoll;
1040 if (ANIM_armature_bonecoll_is_visible_effectively(armature, bcoll)) {
1041 return true;
1042 }
1043 }
1044 return false;
1045}
1046
1047bool ANIM_bone_in_visible_collection(const bArmature *armature, const Bone *bone)
1048{
1049 return any_bone_collection_visible(armature, &bone->runtime.collections);
1050}
1051bool ANIM_bonecoll_is_visible_editbone(const bArmature *armature, const EditBone *ebone)
1052{
1053 return any_bone_collection_visible(armature, &ebone->bone_collections);
1054}
1055
1057{
1058 for (BoneCollection *bcoll : armature->collections_span()) {
1059 ANIM_bonecoll_show(armature, bcoll);
1060 }
1061}
1062
1064{
1065 for (BoneCollection *bcoll : armature->collections_span()) {
1066 ANIM_bonecoll_hide(armature, bcoll);
1067 }
1068}
1069
1070/* ********************************* */
1071/* Armature Layers transitional API. */
1072
1074{
1075 if (armature->runtime->active_collection == nullptr) {
1076 /* No active collection, do not assign to any. */
1077 return;
1078 }
1079
1081}
1082
1083static bool bcoll_list_contains(const ListBaseT<BoneCollectionReference> *collection_refs,
1084 const BoneCollection *bcoll)
1085{
1086 for (const BoneCollectionReference &bcoll_ref : *collection_refs) {
1087 if (bcoll == bcoll_ref.bcoll) {
1088 return true;
1089 }
1090 }
1091 return false;
1092}
1093
1095 const BoneCollection *bcoll)
1096{
1097 if (armature->edbo) {
1098 if (!armature->act_edbone) {
1099 return false;
1100 }
1101 return bcoll_list_contains(&armature->act_edbone->bone_collections, bcoll);
1102 }
1103
1104 if (!armature->act_bone) {
1105 return false;
1106 }
1107 return bcoll_list_contains(&armature->act_bone->runtime.collections, bcoll);
1108}
1109
1111{
1112 if (ANIM_bone_in_visible_collection(armature, bone)) {
1113 return;
1114 }
1115
1116 /* Making the first collection visible is enough to make the bone visible.
1117 *
1118 * Since bones without collection are considered visible,
1119 * bone->runtime.collections.first is certainly a valid pointer. */
1120 BoneCollectionReference *ref = bone->runtime.collections.first();
1122}
1123
1125{
1126 if (ANIM_bonecoll_is_visible_editbone(armature, ebone)) {
1127 return;
1128 }
1129
1130 /* Making the first collection visible is enough to make the bone visible.
1131 *
1132 * Since bones without collection are considered visible,
1133 * ebone->bone_collections.first is certainly a valid pointer. */
1134 BoneCollectionReference *ref = ebone->bone_collections.first();
1136}
1137
1139{
1140 ANIM_armature_bonecoll_show_from_bone(armature, pchan->bone_get(*armature));
1141}
1142
1143/* ********* */
1144/* C++ only. */
1145namespace animrig {
1146
1148{
1149 int index = 0;
1150 for (const BoneCollection *arm_bcoll : armature->collections_span()) {
1151 if (arm_bcoll == bcoll) {
1152 return index;
1153 }
1154 index++;
1155 }
1156
1157 return -1;
1158}
1159
1160int armature_bonecoll_find_parent_index(const bArmature *armature, const int bcoll_index)
1161{
1162 if (bcoll_index < armature->collection_root_count) {
1163 /* Don't bother iterating all collections when it's known to be a root. */
1164 return -1;
1165 }
1166
1167 int index = 0;
1168 for (const BoneCollection *potential_parent : armature->collections_span()) {
1169 if (potential_parent->child_index <= bcoll_index &&
1170 bcoll_index < potential_parent->child_index + potential_parent->child_count)
1171 {
1172 return index;
1173 }
1174
1175 index++;
1176 }
1177
1178 return -1;
1179}
1180
1182 const blender::BoneCollection *bcoll)
1183{
1184 const int bcoll_index = armature_bonecoll_find_index(armature, bcoll);
1185 const int parent_index = armature_bonecoll_find_parent_index(armature, bcoll_index);
1186 return bonecoll_child_number(armature, parent_index, bcoll_index);
1187}
1188
1191 int new_child_number)
1192{
1193 const int bcoll_index = armature_bonecoll_find_index(armature, bcoll);
1194 const int parent_index = armature_bonecoll_find_parent_index(armature, bcoll_index);
1195
1196 BoneCollection fake_armature_parent = {};
1197 fake_armature_parent.child_count = armature->collection_root_count;
1198
1199 BoneCollection *parent_bcoll;
1200 if (parent_index < 0) {
1201 parent_bcoll = &fake_armature_parent;
1202 }
1203 else {
1204 parent_bcoll = armature->collection_array[parent_index];
1205 }
1206
1207 /* Bounds checks. */
1208 if (new_child_number >= parent_bcoll->child_count) {
1209 return -1;
1210 }
1211 if (new_child_number < 0) {
1212 new_child_number = parent_bcoll->child_count - 1;
1213 }
1214
1215 /* Store the parent's child_index, as that might move if to_index is the first child
1216 * (bonecolls_move_to_index() will keep it pointing at that first child). */
1217 const int old_parent_child_index = parent_bcoll->child_index;
1218 const int to_index = parent_bcoll->child_index + new_child_number;
1219 animrig::internal::bonecolls_move_to_index(armature, bcoll_index, to_index);
1220
1221 parent_bcoll->child_index = old_parent_child_index;
1222
1223 /* Make sure that if this was the active bone collection, its index also changes. */
1224 if (armature->runtime->active_collection_index == bcoll_index) {
1225 ANIM_armature_bonecoll_active_index_set(armature, to_index);
1226 }
1227
1228 return to_index;
1229}
1230
1231bool armature_bonecoll_is_root(const bArmature *armature, const int bcoll_index)
1232{
1233 BLI_assert(bcoll_index >= 0);
1234 return bcoll_index < armature->collection_root_count;
1235}
1236
1238 const int potential_parent_index,
1239 const int potential_child_index)
1240{
1241 /* Check for roots, before we try and access collection_array[-1]. */
1242 if (armature_bonecoll_is_root(armature, potential_child_index)) {
1243 return potential_parent_index == -1;
1244 }
1245 if (potential_parent_index < 0) {
1246 return false;
1247 }
1248
1249 const BoneCollection *potential_parent = armature->collection_array[potential_parent_index];
1250 const int upper_bound = potential_parent->child_index + potential_parent->child_count;
1251
1252 return potential_parent->child_index <= potential_child_index &&
1253 potential_child_index < upper_bound;
1254}
1255
1257 const int potential_parent_index,
1258 const int potential_descendant_index)
1259{
1260 BLI_assert_msg(potential_descendant_index >= 0,
1261 "Potential descendant has to exist for this function call to make sense.");
1262
1263 if (armature_bonecoll_is_child_of(armature, potential_parent_index, potential_descendant_index))
1264 {
1265 /* Found a direct child. */
1266 return true;
1267 }
1268
1269 const BoneCollection *potential_parent = armature->collection_array[potential_parent_index];
1270 const int upper_bound = potential_parent->child_index + potential_parent->child_count;
1271
1272 for (int visit_index = potential_parent->child_index; visit_index < upper_bound; visit_index++) {
1273 if (armature_bonecoll_is_descendant_of(armature, visit_index, potential_descendant_index)) {
1274 return true;
1275 }
1276 }
1277
1278 return false;
1279}
1280
1282{
1283 return bcoll->child_count > 0;
1284}
1285
1287 Span<const BoneCollection *> bcolls_source)
1288{
1289 /* Try to preserve the bone collection expanded/collapsed states. These are UI
1290 * changes that shouldn't impact undo steps. Care has to be taken to match the
1291 * old and the new bone collections, though, as they may have been reordered
1292 * or renamed.
1293 *
1294 * Reordering is handled by looking up collections by name.
1295 * Renames are handled by skipping those that cannot be found by name. */
1296
1297 auto find_old = [bcolls_source](const char *name, const int index) -> const BoneCollection * {
1298 /* Only check index when it's valid in the old armature. */
1299 if (index < bcolls_source.size()) {
1300 const BoneCollection *bcoll = bcolls_source[index];
1301 if (STREQ(bcoll->name, name)) {
1302 /* Index and name matches, let's use */
1303 return bcoll;
1304 }
1305 }
1306
1307 /* Try to find by name as a last resort. This function only works with
1308 * non-const pointers, hence the const_cast. */
1309 const BoneCollection *bcoll = bonecolls_get_by_name(bcolls_source, name);
1310 return bcoll;
1311 };
1312
1313 for (int i = 0; i < bcolls_dest.size(); i++) {
1314 BoneCollection *bcoll_new = bcolls_dest[i];
1315
1316 const BoneCollection *bcoll_old = find_old(bcoll_new->name, i);
1317 if (!bcoll_old) {
1318 continue;
1319 }
1320
1321 ANIM_armature_bonecoll_is_expanded_set(bcoll_new, bcoll_old->is_expanded());
1322 }
1323}
1324
1326 const int from_bcoll_index,
1327 int to_child_num,
1328 const int from_parent_index,
1329 const int to_parent_index)
1330{
1331 BLI_assert(0 <= from_bcoll_index && from_bcoll_index < armature->collection_array_num);
1332 BLI_assert(-1 <= from_parent_index && from_parent_index < armature->collection_array_num);
1333 BLI_assert(-1 <= to_parent_index && to_parent_index < armature->collection_array_num);
1334
1335 if (from_parent_index == to_parent_index) {
1336 /* TODO: use `to_child_num` to still move the child to the desired position. */
1337 return from_bcoll_index;
1338 }
1339
1340 /* The Armature itself acts like some sort of 'parent' for the root collections. By having this
1341 * as a 'fake' BoneCollection, all the code below can just be blissfully unaware of the special
1342 * 'all root collections should be at the start of the array' rule. */
1343 BoneCollection armature_root;
1344 armature_root.child_count = armature->collection_root_count;
1345 armature_root.child_index = 0;
1346 armature_root.flags = default_flags;
1347
1348 BoneCollection *from_parent = from_parent_index >= 0 ?
1349 armature->collection_array[from_parent_index] :
1350 &armature_root;
1351 BoneCollection *to_parent = to_parent_index >= 0 ? armature->collection_array[to_parent_index] :
1352 &armature_root;
1353
1354 BLI_assert_msg(-1 <= to_child_num && to_child_num <= to_parent->child_count,
1355 "to_child_num must point to an index of a child of the new parent, or the index "
1356 "of the last child + 1, or be -1 to indicate 'after last child'");
1357 if (to_child_num < 0) {
1358 to_child_num = to_parent->child_count;
1359 }
1360
1361 /* The new parent might not have children yet. */
1362 int to_bcoll_index;
1363 if (to_parent->child_count == 0) {
1364 /* New parents always get their children at the end of the array. */
1365 to_bcoll_index = armature->collection_array_num - 1;
1366 }
1367 else {
1368 to_bcoll_index = to_parent->child_index + to_child_num;
1369
1370 /* Check whether the new parent's children are to the left or right of bcoll_index.
1371 * This determines which direction the collections have to shift, and thus which index to
1372 * move the bcoll to. */
1373 if (to_bcoll_index > from_bcoll_index) {
1374 to_bcoll_index--;
1375 }
1376 }
1377
1378 /* In certain cases the 'from_parent' gets its first child removed, and needs to have its
1379 * child_index incremented. This needs to be done by comparing these fields before the actual
1380 * move happens (as that could also change the child_index). */
1381 const bool needs_post_move_child_index_bump = from_parent->child_index == from_bcoll_index &&
1382 to_bcoll_index <= from_bcoll_index;
1383 /* bonecolls_move_to_index() will try and keep the hierarchy correct, and thus change
1384 * to_parent->child_index to keep pointing to its current-first child. */
1385 const bool becomes_new_first_child = to_child_num == 0 || to_parent->child_count == 0;
1386 animrig::internal::bonecolls_move_to_index(armature, from_bcoll_index, to_bcoll_index);
1387
1388 /* Update child index & count of the old parent. */
1389 from_parent->child_count--;
1390 if (from_parent->child_count == 0) {
1391 /* Clean up the child index when the parent has no more children. */
1392 from_parent->child_index = 0;
1393 }
1394 else if (needs_post_move_child_index_bump) {
1395 /* The start of the block of children of the old parent has moved, because
1396 * we took out the first child. This only needs to be compensated for when
1397 * moving it to the left (or staying put), as then its old siblings stay in
1398 * place.
1399 *
1400 * This only needs to be done if there are any children left, though. */
1401 from_parent->child_index++;
1402 }
1403
1404 /* Update child index & count of the new parent. */
1405 if (becomes_new_first_child) {
1406 to_parent->child_index = to_bcoll_index;
1407 }
1408 to_parent->child_count++;
1409
1410 /* Copy the information from the 'fake' BoneCollection back to the armature. */
1411 armature->collection_root_count = armature_root.child_count;
1412 BLI_assert(armature_root.child_index == 0);
1413
1414 /* Since the parent changed, the effective visibility might change too. */
1415 ancestors_visible_update(armature, to_parent, armature->collection_array[to_bcoll_index]);
1416
1417 return to_bcoll_index;
1418}
1419
1420/* Utility functions for Armature edit-mode undo. */
1421
1423 BoneCollection ***bcoll_array_dst,
1424 int *bcoll_array_dst_num,
1425 BoneCollection **bcoll_array_src,
1426 const int bcoll_array_src_num,
1427 const bool do_id_user)
1428{
1429 BLI_assert(*bcoll_array_dst == nullptr);
1430 BLI_assert(*bcoll_array_dst_num == 0);
1431
1432 *bcoll_array_dst = MEM_new_array_uninitialized<BoneCollection *>(bcoll_array_src_num, __func__);
1433 *bcoll_array_dst_num = bcoll_array_src_num;
1434
1436 for (int i = 0; i < bcoll_array_src_num; i++) {
1437 BoneCollection *bcoll_src = bcoll_array_src[i];
1438 BoneCollection *bcoll_dst = MEM_dupalloc(bcoll_src);
1439
1440 /* This will be rebuilt from the edit bones, so we don't need to copy it. */
1441 bcoll_dst->bones.clear_no_delete();
1442
1443 if (bcoll_src->prop) {
1444 bcoll_dst->prop = IDP_CopyProperty_ex(bcoll_src->prop,
1445 do_id_user ? 0 : LIB_ID_CREATE_NO_USER_REFCOUNT);
1446 }
1447 if (bcoll_src->system_properties) {
1449 bcoll_src->system_properties, do_id_user ? 0 : LIB_ID_CREATE_NO_USER_REFCOUNT);
1450 }
1451
1452 (*bcoll_array_dst)[i] = bcoll_dst;
1453
1454 bcoll_map.add(bcoll_src, bcoll_dst);
1455 }
1456
1457 return bcoll_map;
1458}
1459
1461 int *bcoll_array_num,
1462 const bool do_id_user)
1463{
1464 for (int i = 0; i < *bcoll_array_num; i++) {
1465 BoneCollection *bcoll = (*bcoll_array)[i];
1466
1467 if (bcoll->prop) {
1468 IDP_FreeProperty_ex(bcoll->prop, do_id_user);
1469 }
1470 if (bcoll->system_properties) {
1471 IDP_FreeProperty_ex(bcoll->system_properties, do_id_user);
1472 }
1473
1474 /* This will usually already be empty, because the passed BoneCollection
1475 * list is usually from ANIM_bonecoll_listbase_copy_no_membership().
1476 * However, during undo this is also used to free the BoneCollection
1477 * list on the Armature itself before copying over the undo BoneCollection
1478 * list, in which case this of Bone pointers may not be empty. */
1479 bcoll->bones.free_no_destruct();
1480
1481 MEM_delete(bcoll);
1482 }
1483 MEM_SAFE_DELETE(*bcoll_array);
1484
1485 *bcoll_array_num = 0;
1486}
1487
1489namespace internal {
1490
1492 const int start_index,
1493 const int count,
1494 const int direction)
1495{
1496 BLI_assert_msg(ELEM(direction, 1, -1), "`direction` must be either -1 or +1");
1497
1498 if (count == 0) {
1499 return;
1500 }
1501
1502 /* When the block [start_index:start_index+count] is moved, it causes a duplication of one
1503 * element and overwrites another element. For example: given an array [0, 1, 2, 3, 4], moving
1504 * indices [1, 2] by +1 would result in one double element (1) and one missing element (3): [0,
1505 * 1, 1, 2, 4].
1506 *
1507 * This is resolved by moving that element to the other side of the block, so the result will be
1508 * [0, 3, 1, 2, 4]. This breaks the hierarchical information, so it's up to the caller to update
1509 * this one moved element.
1510 */
1511
1512 const int move_from_index = (direction > 0 ? start_index + count : start_index - 1);
1513 const int move_to_index = (direction > 0 ? start_index : start_index + count - 1);
1514 BoneCollection *bcoll_to_move = armature->collection_array[move_from_index];
1515
1516 BoneCollection **start = armature->collection_array + start_index;
1517 memmove(static_cast<void *>(start + direction),
1518 static_cast<void *>(start),
1519 count * sizeof(BoneCollection *));
1520
1521 armature->collection_array[move_to_index] = bcoll_to_move;
1522
1523 /* Update all child indices that reference something in the moved block. */
1524 for (BoneCollection *bcoll : armature->collections_span()) {
1525 /* Having both child_index and child_count zeroed out just means "no children"; these shouldn't
1526 * be updated at all, as here child_index is not really referencing the element at index 0. */
1527 if (bcoll->child_index == 0 && bcoll->child_count == 0) {
1528 continue;
1529 }
1530
1531 /* Compare to the original start & end of the block (i.e. pre-move). If a
1532 * child_index is within this range, it'll need updating. */
1533 if (start_index <= bcoll->child_index && bcoll->child_index < start_index + count) {
1534 bcoll->child_index += direction;
1535 }
1536 }
1537
1538 /* Make sure the active bone collection index is moved as well. */
1539 const int active_index = armature->runtime->active_collection_index;
1540 if (active_index == move_from_index) {
1541 armature->runtime->active_collection_index = move_to_index;
1542 }
1543 else if (start_index <= active_index && active_index < start_index + count) {
1544 armature->runtime->active_collection_index += direction;
1545 }
1546}
1547
1548void bonecolls_move_to_index(bArmature *armature, const int from_index, const int to_index)
1549{
1550 if (from_index == to_index) {
1551 return;
1552 }
1553
1554 BLI_assert(0 <= from_index);
1555 BLI_assert(from_index < armature->collection_array_num);
1556 BLI_assert(0 <= to_index);
1557 BLI_assert(to_index < armature->collection_array_num);
1558
1559 if (from_index < to_index) {
1560 const int block_start_index = from_index + 1;
1561 const int block_count = to_index - from_index;
1562 bonecolls_rotate_block(armature, block_start_index, block_count, -1);
1563 }
1564 else {
1565 const int block_start_index = to_index;
1566 const int block_count = from_index - to_index;
1567 bonecolls_rotate_block(armature, block_start_index, block_count, +1);
1568 }
1569}
1570
1572{
1573 BoneCollection **collections = armature->collection_array;
1574
1575 if (collections[index] == bcoll) {
1576 return index;
1577 }
1578 if (index > 0 && collections[index - 1] == bcoll) {
1579 return index - 1;
1580 }
1581 if (index < armature->collection_array_num - 1 && collections[index + 1] == bcoll) {
1582 return index + 1;
1583 }
1584 return -1;
1585}
1586
1587void bonecolls_debug_list(const bArmature *armature)
1588{
1589 printf("\033[38;5;214mBone collections of armature \"%s\":\033[0m\n", armature->id.name + 2);
1590 constexpr int root_ansi_color = 95;
1591 printf(
1592 " - \033[%dmroot\033[0m count: %d\n", root_ansi_color, armature->collection_root_count);
1593 for (int i = 0; i < armature->collection_array_num; ++i) {
1594 const BoneCollection *bcoll = armature->collection_array[i];
1595 printf(" - \033[%dmcolls[%d] = %24s\033[0m ",
1596 i < armature->collection_root_count ? root_ansi_color : 0,
1597 i,
1598 bcoll->name);
1599 if (bcoll->child_index == 0 && bcoll->child_count == 0) {
1600 printf("(leaf)");
1601 }
1602 else {
1603 printf("(child index: %d, count: %d)", bcoll->child_index, bcoll->child_count);
1604 }
1605 printf("\n");
1606 }
1607}
1608
1610{
1611 /* Remove bone membership. */
1612 for (BoneCollectionMember &member : bcoll->bones.items_mutable()) {
1613 ANIM_armature_bonecoll_unassign(bcoll, member.bone);
1614 }
1615 if (armature->edbo) {
1616 for (EditBone &ebone : *armature->edbo) {
1618 }
1619 }
1620
1621 ANIM_bonecoll_free(bcoll);
1622}
1623
1624} // namespace internal
1625
1626} // namespace animrig
1627} // namespace blender
Iterators for armatures.
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
#define STRNCPY_UTF8(dst, src)
#define DATA_(msgid)
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
Read Blender memory allocation and freeing.
#define ND_BONE_COLLECTION
Definition WM_types.hh:515
#define NC_OBJECT
Definition WM_types.hh:392
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
Internal C++ functions to deal with bone collections. These are mostly here for internal use in bone_...
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
constexpr int64_t size() const
Definition BLI_span.hh:256
#define index(i)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
void bonecolls_rotate_block(bArmature *armature, const int start_index, const int count, const int direction)
void bonecolls_debug_list(const bArmature *armature)
void bonecoll_unassign_and_free(bArmature *armature, BoneCollection *bcoll)
int bonecolls_find_index_near(bArmature *armature, BoneCollection *bcoll, const int index)
void bonecolls_move_to_index(bArmature *armature, const int from_index, const int to_index)
static void ANIM_armature_foreach_bone(ListBaseT< Bone > *bones, CB callback)
int armature_bonecoll_find_index(const bArmature *armature, const BoneCollection *bcoll)
bool armature_bonecoll_is_root(const bArmature *armature, int bcoll_index)
void bonecolls_copy_expanded_flag(Span< BoneCollection * > bcolls_dest, Span< const BoneCollection * > bcolls_source)
Map< BoneCollection *, BoneCollection * > ANIM_bonecoll_array_copy_no_membership(BoneCollection ***bcoll_array_dst, int *bcoll_array_dst_num, BoneCollection **bcoll_array_src, int bcoll_array_src_num, bool do_id_user)
bool bonecoll_has_children(const BoneCollection *bcoll)
int armature_bonecoll_child_number_set(bArmature *armature, BoneCollection *bcoll, int new_child_number)
bool armature_bonecoll_is_descendant_of(const bArmature *armature, int potential_parent_index, int potential_descendant_index)
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 ANIM_bonecoll_array_free(BoneCollection ***bcoll_array, int *bcoll_array_num, bool do_id_user)
int armature_bonecoll_move_to_parent(bArmature *armature, int from_bcoll_index, int to_child_num, int from_parent_index, int to_parent_index)
bool armature_bonecoll_is_child_of(const bArmature *armature, int potential_parent_index, int potential_child_index)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
void ANIM_armature_runtime_free(bArmature *armature)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void ANIM_bonecoll_show(bArmature *armature, BoneCollection *bcoll)
void ANIM_armature_bonecoll_is_expanded_set(BoneCollection *bcoll, bool is_expanded)
void IDP_FreeProperty_ex(IDProperty *prop, bool do_id_user)
Definition idprop.cc:1398
bool ANIM_armature_bonecoll_assign(BoneCollection *bcoll, Bone *bone)
char * STRNCPY(char(&dst)[N], const char *src)
bool ANIM_armature_bonecoll_assign_editbone(BoneCollection *bcoll, EditBone *ebone)
static BoneCollection * copy_and_update_ownership(const bArmature *armature_dst, const BoneCollection *bcoll_to_copy)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static void armature_bonecoll_active_clear(bArmature *armature)
void ANIM_armature_bonecoll_active_index_set(bArmature *armature, int bone_collection_index)
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
BoneCollection * ANIM_armature_bonecoll_new(bArmature *armature, const char *name, int parent_index=-1)
std::string RNA_path_name_to_infix(const StringRefNull string)
Definition rna_path.cc:1419
static void bonecoll_ensure_name_unique(bArmature *armature, BoneCollection *bcoll)
void ANIM_armature_bonecoll_remove(bArmature *armature, BoneCollection *bcoll)
bool ANIM_bone_in_visible_collection(const bArmature *armature, const Bone *bone)
bool ANIM_armature_bonecoll_assign_and_move(BoneCollection *bcoll, Bone *bone)
BoneCollection * ANIM_bonecoll_new(const char *name) ATTR_WARN_UNUSED_RESULT
void ANIM_armature_bonecoll_active_runtime_refresh(bArmature *armature)
void ANIM_bonecoll_free(BoneCollection *bcoll, bool do_id_user_count=true)
bool BKE_lib_override_library_is_system_defined(const Main *bmain, const ID *id)
static bool any_bone_collection_visible(const bArmature *armature, const ListBaseT< BoneCollectionReference > *collection_refs)
static void bonecoll_insert_as_root(bArmature *armature, BoneCollection *bcoll, int at_index)
@ LIB_ID_CREATE_NO_USER_REFCOUNT
void ANIM_armature_bonecoll_hide_all(bArmature *armature)
void ANIM_armature_refresh_solo_active(bArmature *armature)
void BKE_animdata_fix_paths(ID &id, StringRef prefix, StringRef old_infix, StringRef new_infix, bool verify_paths, const DriverMap &driver_map)
size_t void BLI_uniquename_cb(FunctionRef< bool(StringRefNull)> unique_check, const char *defname, char delim, char *name, size_t name_maxncpy) ATTR_NONNULL(2
static bool bcoll_list_contains(const ListBaseT< BoneCollectionReference > *collection_refs, const BoneCollection *bcoll)
BoneCollection * ANIM_armature_bonecoll_insert_copy_after(bArmature *armature_dst, const bArmature *armature_src, const BoneCollection *anchor_in_dst, const BoneCollection *bcoll_to_copy)
static void ancestors_visible_update(bArmature *armature, const BoneCollection *parent_bcoll, BoneCollection *bcoll)
void ANIM_armature_bonecoll_unassign_all(Bone *bone)
static void ancestors_visible_descendants_clear(bArmature *armature, BoneCollection *parent_bcoll)
void ANIM_armature_bonecoll_reconstruct(bArmature *armature)
static MaybeConstBoneCollection * bonecolls_get_by_name(Span< MaybeConstBoneCollection * > bonecolls, const char *name)
bool ANIM_armature_bonecoll_unassign_editbone(BoneCollection *bcoll, EditBone *ebone)
bool ANIM_bonecoll_is_visible_editbone(const bArmature *armature, const EditBone *ebone)
BoneCollection * ANIM_armature_bonecoll_get_by_name(bArmature *armature, const char *name) ATTR_WARN_UNUSED_RESULT
static void bonecoll_insert_at_index(bArmature *armature, BoneCollection *bcoll, const int index)
DriverMap BKE_animdata_build_driver_target_map(Main &bmain)
Definition anim_data.cc:980
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
void ANIM_armature_bonecoll_remove_from_index(bArmature *armature, const int index)
bool ANIM_armature_bonecoll_contains_active_bone(const bArmature *armature, const BoneCollection *bcoll)
void ANIM_bonecoll_hide(bArmature *armature, BoneCollection *bcoll)
static void liboverride_recursively_add_children(bArmature *armature_dst, const bArmature *armature_src, const int parent_bcoll_dst_index, const BoneCollection *parent_bcoll_src)
void ANIM_armature_bonecoll_active_name_set(bArmature *armature, const char *name)
static void add_reverse_pointers(BoneCollection *bcoll)
bool ANIM_armature_bonecoll_assign_and_move_editbone(BoneCollection *bcoll, EditBone *ebone)
void ANIM_armature_bonecoll_show_all(bArmature *armature)
void ANIM_armature_bonecoll_show_from_ebone(bArmature *armature, const EditBone *ebone)
int ANIM_armature_bonecoll_get_index_by_name(bArmature *armature, const char *name) ATTR_WARN_UNUSED_RESULT
static int bonecoll_child_number(const bArmature *armature, const int parent_bcoll_index, const int bcoll_index)
void ANIM_armature_bonecoll_show_from_pchan(bArmature *armature, const bPoseChannel *pchan)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static void add_membership(BoneCollection *bcoll, Bone *bone)
void ANIM_armature_bonecoll_assign_from_other_editbone(EditBone *dst, EditBone *src)
void ANIM_armature_bonecoll_active_set(bArmature *armature, BoneCollection *bcoll)
void ANIM_armature_bonecoll_assign_active(const bArmature *armature, EditBone *ebone)
static void ancestors_visible_descendants_update(bArmature *armature, BoneCollection *parent_bcoll)
static int bonecoll_insert_as_child(bArmature *armature, BoneCollection *bcoll, const int parent_index)
static void add_reference(Bone *bone, BoneCollection *bcoll)
void ANIM_armature_bonecoll_name_set(Main &bmain, bArmature *armature, BoneCollection *bcoll, const char *name)
Map< ID *, Vector< DriverTarget * > > DriverMap
@ BONE_COLLECTION_OVERRIDE_LIBRARY_LOCAL
@ BONE_COLLECTION_ANCESTORS_VISIBLE
void ANIM_armature_runtime_refresh(bArmature *armature)
bool ANIM_armature_bonecoll_unassign(BoneCollection *bcoll, Bone *bone)
bool ANIM_armature_bonecoll_move_to_index(bArmature *armature, int from_index, int to_index)
IDProperty * IDP_CopyProperty_ex(const IDProperty *prop, int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:968
void ANIM_armature_bonecoll_is_visible_set(bArmature *armature, BoneCollection *bcoll, bool is_visible)
void *(*) MEM_realloc_uninitialized_id(void *vmemh, size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
int ANIM_armature_bonecoll_move_before_after_index(bArmature *armature, int from_index, int to_index, MoveLocation before_after)
bool ANIM_armature_bonecoll_is_visible_effectively(const bArmature *armature, const BoneCollection *bcoll)
bool ANIM_armature_bonecoll_move(bArmature *armature, BoneCollection *bcoll, int step)
Bone * BKE_armature_find_bone_name(bArmature *arm, const char *name)
void ANIM_armature_bonecoll_solo_set(bArmature *armature, BoneCollection *bcoll, bool is_solo)
bool ANIM_armature_bonecoll_is_editable(const bArmature *armature, const BoneCollection *bcoll)
void ANIM_armature_bonecoll_show_from_bone(bArmature *armature, const Bone *bone)
void ANIM_armature_bonecoll_unassign_all_editbone(EditBone *ebone)
eBoneCollection_Flag flags
struct IDProperty * system_properties
ListBaseT< BoneCollectionMember > bones
ListBaseT< BoneCollectionReference > collections
Bone_Runtime runtime
ListBaseT< BoneCollectionReference > bone_collections
char name[258]
Definition DNA_ID.h:433
ListBaseT< EditBone > * edbo
struct EditBone * act_edbone
bke::bArmature_Runtime * runtime
ListBaseT< Bone > bonebase
struct BoneCollection ** collection_array