Blender V5.3
armature_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "DNA_object_types.h"
11
12#include "MEM_guardedalloc.h"
13
14#include "BLI_listbase.hh"
15#include "BLI_math_matrix_c.hh"
16#include "BLI_math_vector_c.hh"
17#include "BLI_string_utf8.hh"
18#include "BLI_string_utils.hh"
19
20#include "BKE_armature.hh"
21#include "BKE_global.hh"
22#include "BKE_idprop.hh"
23#include "BKE_lib_id.hh"
24#include "BKE_main.hh"
25
26#include "DEG_depsgraph.hh"
27
28#include "ED_armature.hh"
29
30#include "ANIM_armature.hh"
32
33#include "armature_intern.hh"
34
35#include <cstring>
36
37namespace blender {
38
39/* -------------------------------------------------------------------- */
42
43void ED_armature_edit_sync_selection(ListBaseT<EditBone> *edbo)
44{
45 for (EditBone &ebo : *edbo) {
46 /* if bone is not selectable, we shouldn't alter this setting... */
47 if ((ebo.flag & BONE_UNSELECTABLE) == 0) {
48 if ((ebo.flag & BONE_CONNECTED) && (ebo.parent)) {
49 if (ebo.parent->flag & BONE_TIPSEL) {
50 ebo.flag |= BONE_ROOTSEL;
51 }
52 else {
53 ebo.flag &= ~BONE_ROOTSEL;
54 }
55 }
56
57 if ((ebo.flag & BONE_TIPSEL) && (ebo.flag & BONE_ROOTSEL)) {
58 ebo.flag |= BONE_SELECTED;
59 }
60 else {
61 ebo.flag &= ~BONE_SELECTED;
62 }
63 }
64 }
65}
66
68
69/* -------------------------------------------------------------------- */
72
73int bone_looper(Object *ob, Bone *bone, void *data, int (*bone_func)(Object *, Bone *, void *))
74{
75 /* We want to apply the function bone_func to every bone
76 * in an armature -- feed bone_looper the first bone and
77 * a pointer to the bone_func and watch it go! The int count
78 * can be useful for counting bones with a certain property
79 * (e.g. skinnable)
80 */
81 int count = 0;
82
83 if (bone) {
84 /* only do bone_func if the bone is non null */
85 count += bone_func(ob, bone, data);
86
87 /* try to execute bone_func for the first child */
88 count += bone_looper(ob, bone->childbase.first(), data, bone_func);
89
90 /* try to execute bone_func for the next bone at this
91 * depth of the recursion.
92 */
93 count += bone_looper(ob, bone->next, data, bone_func);
94 }
95
96 return count;
97}
98
100
101/* -------------------------------------------------------------------- */
104
105void bone_free(bArmature *arm, EditBone *bone)
106{
107 if (arm->act_edbone == bone) {
108 arm->act_edbone = nullptr;
109 }
110
111 if (bone->prop) {
112 IDP_FreeProperty(bone->prop);
113 }
114 if (bone->system_properties) {
116 }
117
118 /* Clear references from other edit bones. */
119 for (EditBone &ebone : *arm->edbo) {
120 if (ebone.bbone_next == bone) {
121 ebone.bbone_next = nullptr;
122 }
123 if (ebone.bbone_prev == bone) {
124 ebone.bbone_prev = nullptr;
125 }
126 }
127
128 bone->bone_collections.free_no_destruct();
129 BLI_freelinkN(arm->edbo, bone);
130}
131
132void ED_armature_ebone_remove_ex(bArmature *arm, EditBone *exBone, bool clear_connected)
133{
134 /* Find any bones that refer to this bone */
135 for (EditBone &curBone : *arm->edbo) {
136 if (curBone.parent == exBone) {
137 curBone.parent = exBone->parent;
138 if (clear_connected) {
139 curBone.flag &= ~BONE_CONNECTED;
140 }
141 }
142 }
143
144 bone_free(arm, exBone);
145}
146
148{
149 ED_armature_ebone_remove_ex(arm, exBone, true);
150}
151
153{
154 for (ebone_child = ebone_child->parent; ebone_child; ebone_child = ebone_child->parent) {
155 if (ebone_child == ebone_parent) {
156 return true;
157 }
158 }
159 return false;
160}
161
162EditBone *ED_armature_ebone_find_shared_parent(EditBone *ebone_child[], const uint ebone_child_tot)
163{
164#define EBONE_TEMP_UINT(ebone) (*((uint *)(&((ebone)->temp))))
165
166 /* clear all */
167 for (uint i = 0; i < ebone_child_tot; i++) {
168 for (EditBone *ebone_iter = ebone_child[i]; ebone_iter; ebone_iter = ebone_iter->parent) {
169 EBONE_TEMP_UINT(ebone_iter) = 0;
170 }
171 }
172
173 /* accumulate */
174 for (uint i = 0; i < ebone_child_tot; i++) {
175 for (EditBone *ebone_iter = ebone_child[i]->parent; ebone_iter;
176 ebone_iter = ebone_iter->parent)
177 {
178 EBONE_TEMP_UINT(ebone_iter) += 1;
179 }
180 }
181
182 /* only need search the first chain */
183 for (EditBone *ebone_iter = ebone_child[0]->parent; ebone_iter; ebone_iter = ebone_iter->parent)
184 {
185 if (EBONE_TEMP_UINT(ebone_iter) == ebone_child_tot) {
186 return ebone_iter;
187 }
188 }
189
190#undef EBONE_TEMP_UINT
191
192 return nullptr;
193}
194
195void ED_armature_ebone_to_mat3(EditBone *ebone, float r_mat[3][3])
196{
197 float delta[3], roll;
198
199 /* Find the current bone matrix */
200 sub_v3_v3v3(delta, ebone->tail, ebone->head);
201 roll = ebone->roll;
202 if (!normalize_v3(delta)) {
203 /* Use the orientation of the parent bone if any. */
204 const EditBone *ebone_parent = ebone->parent;
205 if (ebone_parent) {
206 sub_v3_v3v3(delta, ebone_parent->tail, ebone_parent->head);
207 normalize_v3(delta);
208 roll = ebone_parent->roll;
209 }
210 }
211
212 vec_roll_to_mat3_normalized(delta, roll, r_mat);
213}
214
215void ED_armature_ebone_to_mat4(EditBone *ebone, float r_mat[4][4])
216{
217 float m3[3][3];
218
219 ED_armature_ebone_to_mat3(ebone, m3);
220
221 copy_m4_m3(r_mat, m3);
222 copy_v3_v3(r_mat[3], ebone->head);
223}
224
225void ED_armature_ebone_from_mat3(EditBone *ebone, const float mat[3][3])
226{
227 float vec[3], roll;
228 const float len = len_v3v3(ebone->head, ebone->tail);
229
230 mat3_to_vec_roll(mat, vec, &roll);
231
232 madd_v3_v3v3fl(ebone->tail, ebone->head, vec, len);
233 ebone->roll = roll;
234}
235
236void ED_armature_ebone_from_mat4(EditBone *ebone, const float mat[4][4])
237{
238 float mat3[3][3];
239
240 copy_m3_m4(mat3, mat);
241 /* We want normalized matrix here, to be consistent with ebone_to_mat. */
242 BLI_ASSERT_UNIT_M3(mat3);
243
244 sub_v3_v3(ebone->tail, ebone->head);
245 copy_v3_v3(ebone->head, mat[3]);
246 add_v3_v3(ebone->tail, mat[3]);
247 ED_armature_ebone_from_mat3(ebone, mat3);
248}
249
250EditBone *ED_armature_ebone_find_name(const ListBaseT<EditBone> *edbo, const char *name)
251{
252 return static_cast<EditBone *>(BLI_findstring(edbo, name, offsetof(EditBone, name)));
253}
254
256
257/* -------------------------------------------------------------------- */
260
261EditBone *ED_armature_ebone_get_mirrored(const ListBaseT<EditBone> *edbo, EditBone *ebo)
262{
263 char name_flip[MAXBONENAME];
264
265 if (ebo == nullptr) {
266 return nullptr;
267 }
268
269 BLI_string_flip_side_name(name_flip, ebo->name, false, sizeof(name_flip));
270
271 if (!STREQ(name_flip, ebo->name)) {
272 return ED_armature_ebone_find_name(edbo, name_flip);
273 }
274
275 return nullptr;
276}
277
278/* ------------------------------------- */
279
281{
282 BLI_assert((flag & ~(BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL)) == 0);
283 /* Select mirrored bones */
284 if (arm->flag & ARM_MIRROR_EDIT) {
285 for (EditBone &curBone : *arm->edbo) {
286 if (animrig::bone_is_visible(arm, &curBone)) {
287 if (curBone.flag & flag) {
288 EditBone *ebone_mirr = ED_armature_ebone_get_mirrored(arm->edbo, &curBone);
289 if (ebone_mirr) {
290 ebone_mirr->flag |= (curBone.flag & flag);
291 }
292 }
293 }
294 }
295 }
296}
297
302
304{
305 /* always untag */
306 for (EditBone &curBone : *arm->edbo) {
307 curBone.flag &= ~BONE_DONE;
308 }
309
310 /* Select mirrored bones */
311 if (arm->flag & ARM_MIRROR_EDIT) {
312 for (EditBone &curBone : *arm->edbo) {
313 if (animrig::bone_is_visible(arm, &curBone)) {
314 if (curBone.flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL)) {
315 EditBone *ebone_mirr = ED_armature_ebone_get_mirrored(arm->edbo, &curBone);
316 if (ebone_mirr && (ebone_mirr->flag & BONE_SELECTED) == 0) {
317 ebone_mirr->flag |= BONE_DONE;
318 }
319 }
320 }
321 }
322
323 for (EditBone &curBone : *arm->edbo) {
324 if (curBone.flag & BONE_DONE) {
325 EditBone *ebone_mirr = ED_armature_ebone_get_mirrored(arm->edbo, &curBone);
326 curBone.flag |= ebone_mirr->flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
327 }
328 }
329 }
330}
331
333{
334 for (EditBone &curBone : *arm->edbo) {
335 if (curBone.flag & BONE_DONE) {
337 }
338 }
339}
340
341/* ------------------------------------- */
342
344{
345 /* TODO: When this function is called by property updates,
346 * canceling the value change will not restore mirrored bone correctly. */
347
348 /* Currently check_select==true when this function is called from a transform operator,
349 * eg. from 3d viewport. */
350
351 /* no layer check, correct mirror is more important */
352 if (!check_select ||
353 (animrig::bone_is_visible(arm, ebo) && (ebo->flag & (BONE_TIPSEL | BONE_ROOTSEL))))
354 {
355 EditBone *eboflip = ED_armature_ebone_get_mirrored(arm->edbo, ebo);
356 if (eboflip) {
357 /* We assume X-axis flipping for now. */
358
359 /* Always mirror roll, since it can be changed by moving either head or tail. */
360 eboflip->roll = -ebo->roll;
361
362 if (!check_select || ebo->flag & BONE_TIPSEL) {
363 /* Mirror tail properties. */
364
365 eboflip->tail[0] = -ebo->tail[0];
366 eboflip->tail[1] = ebo->tail[1];
367 eboflip->tail[2] = ebo->tail[2];
368 eboflip->rad_tail = ebo->rad_tail;
369 eboflip->curve_out_x = -ebo->curve_out_x;
370 eboflip->curve_out_z = ebo->curve_out_z;
371 copy_v3_v3(eboflip->scale_out, ebo->scale_out);
372 eboflip->ease2 = ebo->ease2;
373 eboflip->roll2 = -ebo->roll2;
374
375 /* Also move connected children, in case children's name aren't mirrored properly. */
376 for (EditBone &children : *arm->edbo) {
377 if (children.parent == eboflip && children.flag & BONE_CONNECTED) {
378 copy_v3_v3(children.head, eboflip->tail);
379 children.rad_head = ebo->rad_tail;
380 }
381 }
382 }
383
384 if (!check_select || ebo->flag & BONE_ROOTSEL) {
385 /* Mirror head properties. */
386 eboflip->head[0] = -ebo->head[0];
387 eboflip->head[1] = ebo->head[1];
388 eboflip->head[2] = ebo->head[2];
389 eboflip->rad_head = ebo->rad_head;
390
391 eboflip->curve_in_x = -ebo->curve_in_x;
392 eboflip->curve_in_z = ebo->curve_in_z;
393 copy_v3_v3(eboflip->scale_in, ebo->scale_in);
394 eboflip->ease1 = ebo->ease1;
395 eboflip->roll1 = -ebo->roll1;
396
397 /* Also move connected parent, in case parent's name isn't mirrored properly. */
398 if (eboflip->parent && eboflip->flag & BONE_CONNECTED) {
399 EditBone *parent = eboflip->parent;
400 copy_v3_v3(parent->tail, eboflip->head);
401 parent->rad_tail = ebo->rad_head;
402 }
403 }
404
405 if (!check_select || ebo->flag & BONE_SELECTED) {
406 /* Mirror bone body properties (both head and tail are selected). */
407 /* TODO: These values can also be changed from pose mode,
408 * so only mirroring them in edit mode is not ideal. */
409 eboflip->dist = ebo->dist;
410 eboflip->weight = ebo->weight;
411
412 eboflip->segments = ebo->segments;
413 eboflip->xwidth = ebo->xwidth;
414 eboflip->zwidth = ebo->zwidth;
415 }
416 }
417 }
418}
419
421{
422 bArmature *arm = id_cast<bArmature *>(obedit->data);
423 for (EditBone &ebo : *arm->edbo) {
425 }
426}
427
429
430/* -------------------------------------------------------------------- */
433
440static void copy_bonecollection_membership(EditBone *eBone, const Bone *bone)
441{
442 BLI_assert(eBone->bone_collections.is_empty());
444}
445
446/* converts Bones to EditBone list, used for tools as well */
447static EditBone *make_boneList_recursive(ListBaseT<EditBone> *edbo,
448 ListBaseT<Bone> *bones,
449 EditBone *parent,
450 Bone *actBone)
451{
452 EditBone *eBone;
453 EditBone *eBoneAct = nullptr;
454 EditBone *eBoneTest = nullptr;
455
456 for (Bone &curBone : *bones) {
457 eBone = MEM_new<EditBone>("make_editbone");
458 eBone->temp.bone = &curBone;
459
460 /* Copy relevant data from bone to eBone
461 * Keep selection logic in sync with ED_armature_edit_sync_selection.
462 */
463 eBone->parent = parent;
464 STRNCPY_UTF8(eBone->name, curBone.name);
465 eBone->flag = curBone.flag;
466 eBone->inherit_scale_mode = curBone.inherit_scale_mode;
467 eBone->drawtype = curBone.drawtype;
468
469 /* fix selection flags */
470 if (eBone->flag & BONE_SELECTED) {
471 /* if the bone is selected the copy its root selection to the parents tip */
472 eBone->flag |= BONE_TIPSEL;
473 if (eBone->parent && (eBone->flag & BONE_CONNECTED)) {
474 eBone->parent->flag |= BONE_TIPSEL;
475 }
476
477 /* For connected bones, take care when changing the selection when we have a
478 * connected parent, this flag is a copy of '(eBone->parent->flag & BONE_TIPSEL)'. */
479 eBone->flag |= BONE_ROOTSEL;
480 }
481 else {
482 /* if the bone is not selected, but connected to its parent
483 * always use the parents tip selection state */
484 if (eBone->parent && (eBone->flag & BONE_CONNECTED)) {
485 eBone->flag &= ~BONE_ROOTSEL;
486 }
487 }
488
489 copy_v3_v3(eBone->head, curBone.arm_head);
490 copy_v3_v3(eBone->tail, curBone.arm_tail);
491 eBone->roll = curBone.arm_roll;
492
493 /* rest of stuff copy */
494 eBone->length = curBone.length;
495 eBone->dist = curBone.dist;
496 eBone->weight = curBone.weight;
497 eBone->xwidth = curBone.xwidth;
498 eBone->zwidth = curBone.zwidth;
499 eBone->rad_head = curBone.rad_head;
500 eBone->rad_tail = curBone.rad_tail;
501 eBone->segments = curBone.segments;
502 eBone->layer = curBone.layer;
503
504 /* Bendy-Bone parameters */
505 eBone->roll1 = curBone.roll1;
506 eBone->roll2 = curBone.roll2;
507 eBone->curve_in_x = curBone.curve_in_x;
508 eBone->curve_in_z = curBone.curve_in_z;
509 eBone->curve_out_x = curBone.curve_out_x;
510 eBone->curve_out_z = curBone.curve_out_z;
511 eBone->ease1 = curBone.ease1;
512 eBone->ease2 = curBone.ease2;
513
514 copy_v3_v3(eBone->scale_in, curBone.scale_in);
515 copy_v3_v3(eBone->scale_out, curBone.scale_out);
516
517 eBone->bbone_prev_type = curBone.bbone_prev_type;
518 eBone->bbone_next_type = curBone.bbone_next_type;
519
520 eBone->bbone_mapping_mode = eBone_BBoneMappingMode(curBone.bbone_mapping_mode);
521 eBone->bbone_flag = curBone.bbone_flag;
522 eBone->bbone_prev_flag = curBone.bbone_prev_flag;
523 eBone->bbone_next_flag = curBone.bbone_next_flag;
524
525 eBone->color = curBone.color;
526 copy_bonecollection_membership(eBone, &curBone);
527
528 if (curBone.prop) {
529 eBone->prop = IDP_CopyProperty(curBone.prop);
530 }
531 if (curBone.system_properties) {
532 eBone->system_properties = IDP_CopyProperty(curBone.system_properties);
533 }
534
535 BLI_addtail(edbo, eBone);
536
537 /* Add children if necessary. */
538 if (curBone.childbase.first_) {
539 eBoneTest = make_boneList_recursive(edbo, &curBone.childbase, eBone, actBone);
540 if (eBoneTest) {
541 eBoneAct = eBoneTest;
542 }
543 }
544
545 if (&curBone == actBone) {
546 eBoneAct = eBone;
547 }
548 }
549
550 return eBoneAct;
551}
552
553static EditBone *find_ebone_link(ListBaseT<EditBone> *edbo, Bone *link)
554{
555 if (link != nullptr) {
556 for (EditBone &ebone : *edbo) {
557 if (ebone.temp.bone == link) {
558 return &ebone;
559 }
560 }
561 }
562
563 return nullptr;
564}
565
566EditBone *make_boneList(ListBaseT<EditBone> *edbo, ListBaseT<Bone> *bones, Bone *actBone)
567{
568 BLI_assert(!edbo->first() && !edbo->last());
569
570 EditBone *active = make_boneList_recursive(edbo, bones, nullptr, actBone);
571
572 for (EditBone &ebone : *edbo) {
573 Bone *bone = ebone.temp.bone;
574
575 /* Convert custom B-Bone handle links. */
576 ebone.bbone_prev = find_ebone_link(edbo, bone->bbone_prev);
577 ebone.bbone_next = find_ebone_link(edbo, bone->bbone_next);
578 }
579
580 return active;
581}
582
595static void armature_finalize_restpose(ListBaseT<Bone> *bonelist,
596 ListBaseT<EditBone> *editbonelist)
597{
598 for (Bone &curBone : *bonelist) {
599 /* Set bone's local head/tail.
600 * Note that it's important to use final parent's rest-pose (arm_mat) here,
601 * instead of setting those values from edit-bone's matrix (see #46010). */
602 if (curBone.parent) {
603 float parmat_inv[4][4];
604
605 invert_m4_m4(parmat_inv, curBone.parent->arm_mat);
606
607 /* Get the new head and tail */
608 sub_v3_v3v3(curBone.head, curBone.arm_head, curBone.parent->arm_tail);
609 sub_v3_v3v3(curBone.tail, curBone.arm_tail, curBone.parent->arm_tail);
610
611 mul_mat3_m4_v3(parmat_inv, curBone.head);
612 mul_mat3_m4_v3(parmat_inv, curBone.tail);
613 }
614 else {
615 copy_v3_v3(curBone.head, curBone.arm_head);
616 copy_v3_v3(curBone.tail, curBone.arm_tail);
617 }
618
619 /* Set local matrix and arm_mat (rest-pose).
620 * Do not recurse into children here, armature_finalize_restpose() is already recursive. */
621 BKE_armature_where_is_bone(&curBone, curBone.parent, false);
622
623 /* Find the associated editbone */
624 for (EditBone &ebone : *editbonelist) {
625 if (ebone.temp.bone == &curBone) {
626 float premat[3][3];
627 float postmat[3][3];
628 float difmat[3][3];
629 float imat[3][3];
630
631 /* Get the ebone premat and its inverse. */
632 ED_armature_ebone_to_mat3(&ebone, premat);
633 invert_m3_m3(imat, premat);
634
635 /* Get the bone postmat. */
636 copy_m3_m4(postmat, curBone.arm_mat);
637
638 mul_m3_m3m3(difmat, imat, postmat);
639
640#if 0
641 printf("Bone %s\n", curBone->name);
642 print_m4("premat", premat);
643 print_m4("postmat", postmat);
644 print_m4("difmat", difmat);
645 printf("Roll = %f\n", RAD2DEGF(-atan2(difmat[2][0], difmat[2][2])));
646#endif
647
648 curBone.roll = -atan2f(difmat[2][0], difmat[2][2]);
649
650 /* And set rest-position again. */
651 BKE_armature_where_is_bone(&curBone, curBone.parent, false);
652 break;
653 }
654 }
655
656 /* Recurse into children... */
657 armature_finalize_restpose(&curBone.childbase, editbonelist);
658 }
659}
660
662{
663 Bone *newBone;
664 Object *obt;
665
666 /* armature bones */
670
671 arm->act_bone = nullptr;
672
673 /* Avoid (almost) zero sized bones, this gives unstable rest-poses. */
674 {
675 /* If this threshold is adjusted, also update the `bl_animation_armature.py` test. */
676 constexpr float bone_length_threshold = 0.000001f;
677 constexpr float bone_length_threshold_sq = bone_length_threshold * bone_length_threshold;
678 constexpr float adjusted_bone_length = 2 * bone_length_threshold;
679
680 /* Build a map from parent to its children, to speed up the loop below. */
681 Map<EditBone *, VectorSet<EditBone *>> parent_to_children;
682 for (EditBone &eBone : *arm->edbo) {
683 parent_to_children.lookup_or_add_default(eBone.parent).add_new(&eBone);
684 }
685
686 for (EditBone &eBone : *arm->edbo) {
687 const float len_sq = len_squared_v3v3(eBone.head, eBone.tail);
688 if (len_sq > bone_length_threshold_sq) {
689 continue;
690 }
691
692 /* Move the tail away from the head, to ensure the bone has at least some length.
693 * Historical note: until 5.0, Blender used to delete these bones. However, this was an issue
694 * with importers that assume that the bones they import actually will exist on the Armature.
695 * So instead, the bones are elongated a bit for numerical stability. These are very small
696 * adjustments, and so are unlikely to cause issues in practice. */
697
698 float offset[3];
699 if (len_sq == 0.0f) {
700 /* The bone is actually zero-length, which means it has no direction. Just pick one. */
701 offset[0] = 0.0f;
702 offset[1] = 0.0f;
703 offset[2] = adjusted_bone_length;
704 }
705 else {
706 sub_v3_v3v3(offset, eBone.tail, eBone.head);
707 normalize_v3_length(offset, adjusted_bone_length);
708 }
709
710 /* Apply this offset to the bone's tail to make it long enough for numerical stability. And
711 * disconnect it so that the children don't have to be updated, and can remain at their
712 * current location.
713 *
714 * Disconnecting the children is a lot simpler than the alternative: offsetting the children
715 * themselves. That would create subtle issues, for example if there are two bone chains that
716 * would initially exactly align, but one of them has a tiny bone; if all children were
717 * shifted, they would no longer align. */
718 add_v3_v3v3(eBone.tail, eBone.head, offset);
719 if (G.debug & G_DEBUG) {
720 printf("Warning: elongated (almost) zero sized bone: %s\n", eBone.name);
721 }
722
723 VectorSet<EditBone *> *children = parent_to_children.lookup_ptr(&eBone);
724 if (children) {
725 for (EditBone *child : *children) {
726 child->flag &= ~BONE_CONNECTED;
727 }
728 }
729 }
730 }
731
732 /* Copy the bones from the edit-data into the armature. */
733 for (EditBone &eBone : *arm->edbo) {
734 newBone = MEM_new<Bone>("bone");
735 eBone.temp.bone = newBone; /* Associate the real Bones with the EditBones */
736
737 STRNCPY_UTF8(newBone->name, eBone.name);
738 copy_v3_v3(newBone->arm_head, eBone.head);
739 copy_v3_v3(newBone->arm_tail, eBone.tail);
740 newBone->arm_roll = eBone.roll;
741
742 newBone->flag = eBone.flag;
743 newBone->inherit_scale_mode = eBone.inherit_scale_mode;
744 newBone->drawtype = eBone.drawtype;
745
746 if (&eBone == arm->act_edbone) {
747 /* Don't change active selection, this messes up separate which uses
748 * edit-mode toggle and can separate active bone which is de-selected originally. */
749
750 /* important, edit-bones can be active with only 1 point selected */
751 /* `newBone->flag |= BONE_SELECTED;` */
752 arm->act_bone = newBone;
753 }
754 newBone->roll = 0.0f;
755
756 newBone->weight = eBone.weight;
757 newBone->dist = eBone.dist;
758
759 newBone->xwidth = eBone.xwidth;
760 newBone->zwidth = eBone.zwidth;
761 newBone->rad_head = eBone.rad_head;
762 newBone->rad_tail = eBone.rad_tail;
763 newBone->segments = eBone.segments;
764 newBone->layer = eBone.layer;
765
766 /* Bendy-Bone parameters */
767 newBone->roll1 = eBone.roll1;
768 newBone->roll2 = eBone.roll2;
769 newBone->curve_in_x = eBone.curve_in_x;
770 newBone->curve_in_z = eBone.curve_in_z;
771 newBone->curve_out_x = eBone.curve_out_x;
772 newBone->curve_out_z = eBone.curve_out_z;
773 newBone->ease1 = eBone.ease1;
774 newBone->ease2 = eBone.ease2;
775 copy_v3_v3(newBone->scale_in, eBone.scale_in);
776 copy_v3_v3(newBone->scale_out, eBone.scale_out);
777
778 newBone->bbone_prev_type = eBone.bbone_prev_type;
779 newBone->bbone_next_type = eBone.bbone_next_type;
780
781 newBone->bbone_mapping_mode = eBone.bbone_mapping_mode;
782 newBone->bbone_flag = eBone.bbone_flag;
783 newBone->bbone_prev_flag = eBone.bbone_prev_flag;
784 newBone->bbone_next_flag = eBone.bbone_next_flag;
785
786 newBone->color = eBone.color;
787
788 for (BoneCollectionReference &ref : eBone.bone_collections) {
789 BoneCollectionReference *newBoneRef = MEM_new<BoneCollectionReference>(
790 "ED_armature_from_edit", ref);
791 BLI_addtail(&newBone->runtime.collections, newBoneRef);
792 }
793
794 if (eBone.prop) {
795 newBone->prop = IDP_CopyProperty(eBone.prop);
796 }
797 if (eBone.system_properties) {
799 }
800 }
801
802 /* Fix parenting in a separate pass to ensure ebone->bone connections are valid at this point.
803 * Do not set bone->head/tail here anymore,
804 * using EditBone data for that is not OK since our later fiddling with parent's arm_mat
805 * (for roll conversion) may have some small but visible impact on locations (#46010). */
806 for (EditBone &eBone : *arm->edbo) {
807 newBone = eBone.temp.bone;
808 if (eBone.parent) {
809 newBone->parent = eBone.parent->temp.bone;
810 BLI_addtail(&newBone->parent->childbase, newBone);
811 }
812 /* ...otherwise add this bone to the armature's bonebase */
813 else {
814 BLI_addtail(&arm->bonebase, newBone);
815 }
816
817 /* Also transfer B-Bone custom handles. */
818 if (eBone.bbone_prev) {
819 newBone->bbone_prev = eBone.bbone_prev->temp.bone;
820 }
821 if (eBone.bbone_next) {
822 newBone->bbone_next = eBone.bbone_next->temp.bone;
823 }
824 }
825
826 /* Finalize definition of rest-pose data (roll, bone_mat, arm_mat, head/tail...). */
829
831
832 /* so all users of this armature should get rebuilt */
833 for (obt = bmain->objects.first(); obt; obt = static_cast<Object *>(obt->id.next)) {
834 if (obt->data == id_cast<const ID *>(arm)) {
835 BKE_pose_rebuild(bmain, obt, arm, true);
836 }
837 }
838
839 DEG_id_tag_update(&arm->id, 0);
840}
841
843{
844 /* Clear the edit-bones list. */
845 if (arm->edbo) {
846 if (arm->edbo->first()) {
847 for (EditBone &eBone : *arm->edbo) {
848 if (eBone.prop) {
849 IDP_FreeProperty(eBone.prop);
850 }
851 if (eBone.system_properties) {
853 }
854 eBone.bone_collections.free_no_destruct();
855 }
856
857 arm->edbo->free_no_destruct();
858 }
859 MEM_delete(arm->edbo);
860 arm->edbo = nullptr;
861 arm->act_edbone = nullptr;
862 }
863}
864
866{
868 arm->edbo = MEM_new_zeroed<ListBaseT<EditBone>>("edbo armature");
869 arm->act_edbone = make_boneList(arm->edbo, &arm->bonebase, arm->act_bone);
870}
871
873
874/* -------------------------------------------------------------------- */
877
878void ED_armature_ebone_listbase_free(ListBaseT<EditBone> *lb, const bool do_id_user)
879{
880 EditBone *ebone, *ebone_next;
881
882 for (ebone = lb->first(); ebone; ebone = ebone_next) {
883 ebone_next = ebone->next;
884
885 if (ebone->prop) {
886 IDP_FreeProperty_ex(ebone->prop, do_id_user);
887 }
888 if (ebone->system_properties) {
889 IDP_FreeProperty_ex(ebone->system_properties, do_id_user);
890 }
891
892 ebone->bone_collections.free_no_destruct();
893
894 MEM_delete(ebone);
895 }
896
897 lb->clear_no_delete();
898}
899
900void ED_armature_ebone_listbase_copy(ListBaseT<EditBone> *lb_dst,
901 ListBaseT<EditBone> *lb_src,
902 const bool do_id_user)
903{
904 BLI_assert(lb_dst->is_empty());
905
906 for (EditBone &ebone_src : *lb_src) {
907 EditBone *ebone_dst = MEM_dupalloc(&ebone_src);
908 if (ebone_dst->prop) {
909 ebone_dst->prop = IDP_CopyProperty_ex(ebone_dst->prop,
910 do_id_user ? 0 : LIB_ID_CREATE_NO_USER_REFCOUNT);
911 }
912 if (ebone_dst->system_properties) {
914 ebone_dst->system_properties, do_id_user ? 0 : LIB_ID_CREATE_NO_USER_REFCOUNT);
915 }
916 ebone_src.temp.ebone = ebone_dst;
917 BLI_addtail(lb_dst, ebone_dst);
918 }
919
920 /* set pointers */
921 for (EditBone &ebone_dst : *lb_dst) {
922 if (ebone_dst.parent) {
923 ebone_dst.parent = ebone_dst.parent->temp.ebone;
924 }
925 if (ebone_dst.bbone_next) {
926 ebone_dst.bbone_next = ebone_dst.bbone_next->temp.ebone;
927 }
928 if (ebone_dst.bbone_prev) {
929 ebone_dst.bbone_prev = ebone_dst.bbone_prev->temp.ebone;
930 }
931
932 BLI_duplicatelist(&ebone_dst.bone_collections, &ebone_dst.bone_collections);
933 }
934}
935
936void ED_armature_ebone_listbase_temp_clear(ListBaseT<EditBone> *lb)
937{
938 /* be sure they don't hang ever */
939 for (EditBone &ebone : *lb) {
940 ebone.temp.p = nullptr;
941 }
942}
943
945
946/* -------------------------------------------------------------------- */
952
954{
955 if (ebone->parent && (ebone->flag & BONE_CONNECTED)) {
956 return ((ebone->flag & (BONE_SELECTED | BONE_TIPSEL)) |
957 ((ebone->parent->flag & BONE_TIPSEL) ? BONE_ROOTSEL : 0));
958 }
959 return (ebone->flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL));
960}
961
963{
964 flag = flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
965
966 if (ebone->parent && (ebone->flag & BONE_CONNECTED)) {
968 ebone->parent->flag &= ~BONE_TIPSEL;
969
970 ebone->flag |= flag;
971 ebone->parent->flag |= (flag & BONE_ROOTSEL) ? BONE_TIPSEL : eBone_Flag{};
972 }
973 else {
975 ebone->flag |= flag;
976 }
977}
978
984
990
992{
993 /* NOTE: this function could be used in more places. */
994
995 eBone_Flag flag;
996 if (select) {
997 BLI_assert((ebone->flag & BONE_UNSELECTABLE) == 0);
999 }
1000 else {
1001 flag = eBone_Flag{};
1002 }
1004}
1005
1007
1008} // namespace blender
Functions to deal with Armatures.
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
#define BLI_ASSERT_UNIT_M3(m)
#define RAD2DEGF(_rad)
#define STRNCPY_UTF8(dst, src)
#define MAXBONENAME
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define EBONE_TEMP_UINT(ebone)
#define STREQ(a, b)
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
Value & lookup_or_add_default(const Key &key)
Definition BLI_map.hh:639
#define offsetof(t, d)
#define atan2
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
bool bone_is_visible(const bArmature *armature, const Bone *bone)
BMesh Iterators.
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void IDP_FreeProperty_ex(IDProperty *prop, bool do_id_user)
Definition idprop.cc:1398
void BKE_pose_rebuild(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void ED_armature_to_edit(bArmature *arm)
void ED_armature_from_edit(Main *bmain, bArmature *arm)
void copy_m3_m4(float m1[3][3], const float m2[4][4])
void mat3_to_vec_roll(const float mat[3][3], float r_vec[3], float *r_roll)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
void ED_armature_ebone_selectflag_disable(EditBone *ebone, eBone_Flag flag)
void ED_armature_edit_transform_mirror_update(Object *obedit)
void ED_armature_ebone_selectflag_enable(EditBone *ebone, eBone_Flag flag)
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
void copy_m4_m3(float m1[4][4], const float m2[3][3])
bool ED_armature_ebone_is_child_recursive(EditBone *ebone_parent, EditBone *ebone_child)
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
IDProperty * IDP_CopyProperty(const IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:986
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void ED_armature_ebone_listbase_copy(ListBaseT< EditBone > *lb_dst, ListBaseT< EditBone > *lb_src, const bool do_id_user)
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
void armature_tag_unselect(bArmature *arm)
void ED_armature_ebone_remove_ex(bArmature *arm, EditBone *exBone, bool clear_connected)
void BKE_armature_bone_hash_free(bArmature *arm)
EditBone * make_boneList(ListBaseT< EditBone > *edbo, ListBaseT< Bone > *bones, Bone *actBone)
@ LIB_ID_CREATE_NO_USER_REFCOUNT
void ED_armature_ebone_transform_mirror_update(bArmature *arm, EditBone *ebo, bool check_select)
void ED_armature_ebone_listbase_temp_clear(ListBaseT< EditBone > *lb)
Global G
Definition blender.cc:57
MINLINE void sub_v3_v3(float r[3], const float a[3])
EditBone * ED_armature_ebone_find_shared_parent(EditBone *ebone_child[], const uint ebone_child_tot)
EditBone * ED_armature_ebone_get_mirrored(const ListBaseT< EditBone > *edbo, EditBone *ebo)
void ED_armature_ebone_from_mat4(EditBone *ebone, const float mat[4][4])
void armature_tag_select_mirrored(bArmature *arm)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void ED_armature_ebone_to_mat3(EditBone *ebone, float r_mat[3][3])
void ANIM_armature_bonecoll_reconstruct(bArmature *armature)
void ED_armature_ebone_selectflag_set(EditBone *ebone, eBone_Flag flag)
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
void ED_armature_edit_sync_selection(ListBaseT< EditBone > *edbo)
static EditBone * make_boneList_recursive(ListBaseT< EditBone > *edbo, ListBaseT< Bone > *bones, EditBone *parent, Bone *actBone)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
void ED_armature_ebone_to_mat4(EditBone *ebone, float r_mat[4][4])
void ED_armature_edit_free(bArmature *arm)
void BKE_armature_where_is_bone(Bone *bone, const Bone *bone_parent, bool use_recursion)
void ED_armature_ebone_from_mat3(EditBone *ebone, const float mat[3][3])
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
MINLINE float normalize_v3(float n[3])
void ED_armature_ebone_remove(bArmature *arm, EditBone *exBone)
MINLINE void add_v3_v3(float r[3], const float a[3])
int ED_armature_ebone_selectflag_get(const EditBone *ebone)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void armature_select_mirrored(bArmature *arm)
void BKE_armature_bone_hash_make(bArmature *arm)
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
void armature_select_mirrored_ex(bArmature *arm, eBone_Flag flag)
MINLINE float normalize_v3_length(float n[3], float unit_length)
void bone_free(bArmature *arm, EditBone *bone)
static void copy_bonecollection_membership(EditBone *eBone, const Bone *bone)
static void armature_finalize_restpose(ListBaseT< Bone > *bonelist, ListBaseT< EditBone > *editbonelist)
void vec_roll_to_mat3_normalized(const float nor[3], float roll, float r_mat[3][3])
void print_m4(const char *str, const float m[4][4])
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
static EditBone * find_ebone_link(ListBaseT< EditBone > *edbo, Bone *link)
IDProperty * IDP_CopyProperty_ex(const IDProperty *prop, int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:968
void ED_armature_ebone_listbase_free(ListBaseT< EditBone > *lb, const bool do_id_user)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void ED_armature_ebone_select_set(EditBone *ebone, bool select)
int bone_looper(Object *ob, Bone *bone, void *data, int(*bone_func)(Object *, Bone *, void *))
void BKE_armature_bonelist_free(ListBaseT< Bone > *lb, bool do_id_user)
EditBone * ED_armature_ebone_find_name(const ListBaseT< EditBone > *edbo, const char *name)
size_t BLI_string_flip_side_name(char *name_dst, const char *name_src, bool strip_number, size_t name_dst_maxncpy) ATTR_NONNULL(1
#define atan2f
ListBaseT< BoneCollectionReference > collections
struct Bone * bbone_prev
eBone_BBoneFlag bbone_flag
eBone_BBoneHandleFlag bbone_next_flag
struct Bone * bbone_next
struct Bone * parent
eBone_BBoneHandleType bbone_prev_type
eBone_BBoneHandleFlag bbone_prev_flag
eBone_BBoneMappingMode bbone_mapping_mode
Bone_Runtime runtime
eBone_BBoneHandleType bbone_next_type
IDProperty * system_properties
struct Bone * next
ListBaseT< Bone > childbase
eBone_InheritScaleMode inherit_scale_mode
union blender::EditBone::@252003325204232231327014100071366025164067232113 temp
ListBaseT< BoneCollectionReference > bone_collections
eBone_BBoneHandleFlag bbone_next_flag
eBone_BBoneMappingMode bbone_mapping_mode
eBone_BBoneFlag bbone_flag
IDProperty * system_properties
eBone_InheritScaleMode inherit_scale_mode
eBone_BBoneHandleType bbone_next_type
EditBone * bbone_prev
EditBone * bbone_next
eBone_BBoneHandleFlag bbone_prev_flag
eBone_BBoneHandleType bbone_prev_type
IDProperty * prop
void * next
Definition DNA_ID.h:418
ListBaseT< Object > objects
Definition BKE_main.hh:388
ListBaseT< EditBone > * edbo
struct EditBone * act_edbone
bke::bArmature_Runtime * runtime
ListBaseT< Bone > bonebase