1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup edarmature
22 */
23
24 #include "DNA_armature_types.h"
25 #include "DNA_object_types.h"
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BLI_blenlib.h"
30 #include "BLI_math.h"
31 #include "BLI_string_utils.h"
32
33 #include "BKE_armature.h"
34 #include "BKE_context.h"
35 #include "BKE_deform.h"
36 #include "BKE_global.h"
37 #include "BKE_idprop.h"
38 #include "BKE_lib_id.h"
39 #include "BKE_main.h"
40
41 #include "DEG_depsgraph.h"
42
43 #include "ED_armature.h"
44 #include "ED_util.h"
45
46 #include "armature_intern.h"
47
48 /* *************************************************************** */
49 /* Validation */
50
51 /* Sync selection to parent for connected children */
ED_armature_edit_sync_selection(ListBase * edbo)52 void ED_armature_edit_sync_selection(ListBase *edbo)
53 {
54 EditBone *ebo;
55
56 for (ebo = edbo->first; ebo; ebo = ebo->next) {
57 /* if bone is not selectable, we shouldn't alter this setting... */
58 if ((ebo->flag & BONE_UNSELECTABLE) == 0) {
59 if ((ebo->flag & BONE_CONNECTED) && (ebo->parent)) {
60 if (ebo->parent->flag & BONE_TIPSEL) {
61 ebo->flag |= BONE_ROOTSEL;
62 }
63 else {
64 ebo->flag &= ~BONE_ROOTSEL;
65 }
66 }
67
68 if ((ebo->flag & BONE_TIPSEL) && (ebo->flag & BONE_ROOTSEL)) {
69 ebo->flag |= BONE_SELECTED;
70 }
71 else {
72 ebo->flag &= ~BONE_SELECTED;
73 }
74 }
75 }
76 }
77
ED_armature_edit_validate_active(struct bArmature * arm)78 void ED_armature_edit_validate_active(struct bArmature *arm)
79 {
80 EditBone *ebone = arm->act_edbone;
81
82 if (ebone) {
83 if (ebone->flag & BONE_HIDDEN_A) {
84 arm->act_edbone = NULL;
85 }
86 }
87 }
88
89 /* Update the layers_used variable after bones are moved between layer
90 * NOTE: Used to be done in drawing code in 2.7, but that won't work with
91 * Copy-on-Write, as drawing uses evaluated copies.
92 */
ED_armature_edit_refresh_layer_used(bArmature * arm)93 void ED_armature_edit_refresh_layer_used(bArmature *arm)
94 {
95 arm->layer_used = 0;
96 LISTBASE_FOREACH (EditBone *, ebo, arm->edbo) {
97 arm->layer_used |= ebo->layer;
98 }
99 }
100
101 /* *************************************************************** */
102 /* Bone Operations */
103
104 /* XXX bone_looper is only to be used when we want to access settings
105 * (i.e. editability/visibility/selected) that context doesn't offer */
bone_looper(Object * ob,Bone * bone,void * data,int (* bone_func)(Object *,Bone *,void *))106 int bone_looper(Object *ob, Bone *bone, void *data, int (*bone_func)(Object *, Bone *, void *))
107 {
108 /* We want to apply the function bone_func to every bone
109 * in an armature -- feed bone_looper the first bone and
110 * a pointer to the bone_func and watch it go!. The int count
111 * can be useful for counting bones with a certain property
112 * (e.g. skinnable)
113 */
114 int count = 0;
115
116 if (bone) {
117 /* only do bone_func if the bone is non null */
118 count += bone_func(ob, bone, data);
119
120 /* try to execute bone_func for the first child */
121 count += bone_looper(ob, bone->childbase.first, data, bone_func);
122
123 /* try to execute bone_func for the next bone at this
124 * depth of the recursion.
125 */
126 count += bone_looper(ob, bone->next, data, bone_func);
127 }
128
129 return count;
130 }
131
132 /* *************************************************************** */
133 /* Bone Removal */
134
bone_free(bArmature * arm,EditBone * bone)135 void bone_free(bArmature *arm, EditBone *bone)
136 {
137 if (arm->act_edbone == bone) {
138 arm->act_edbone = NULL;
139 }
140
141 if (bone->prop) {
142 IDP_FreeProperty(bone->prop);
143 }
144
145 /* Clear references from other edit bones. */
146 LISTBASE_FOREACH (EditBone *, ebone, arm->edbo) {
147 if (ebone->bbone_next == bone) {
148 ebone->bbone_next = NULL;
149 }
150 if (ebone->bbone_prev == bone) {
151 ebone->bbone_prev = NULL;
152 }
153 }
154
155 BLI_freelinkN(arm->edbo, bone);
156 }
157
158 /**
159 * \param clear_connected: When false caller is responsible for keeping the flag in a valid state.
160 */
ED_armature_ebone_remove_ex(bArmature * arm,EditBone * exBone,bool clear_connected)161 void ED_armature_ebone_remove_ex(bArmature *arm, EditBone *exBone, bool clear_connected)
162 {
163 EditBone *curBone;
164
165 /* Find any bones that refer to this bone */
166 for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
167 if (curBone->parent == exBone) {
168 curBone->parent = exBone->parent;
169 if (clear_connected) {
170 curBone->flag &= ~BONE_CONNECTED;
171 }
172 }
173 }
174
175 bone_free(arm, exBone);
176 }
177
ED_armature_ebone_remove(bArmature * arm,EditBone * exBone)178 void ED_armature_ebone_remove(bArmature *arm, EditBone *exBone)
179 {
180 ED_armature_ebone_remove_ex(arm, exBone, true);
181 }
182
ED_armature_ebone_is_child_recursive(EditBone * ebone_parent,EditBone * ebone_child)183 bool ED_armature_ebone_is_child_recursive(EditBone *ebone_parent, EditBone *ebone_child)
184 {
185 for (ebone_child = ebone_child->parent; ebone_child; ebone_child = ebone_child->parent) {
186 if (ebone_child == ebone_parent) {
187 return true;
188 }
189 }
190 return false;
191 }
192
193 /**
194 * Finds the first parent shared by \a ebone_child
195 *
196 * \param ebone_child: Children bones to search
197 * \param ebone_child_tot: Size of the ebone_child array
198 * \return The shared parent or NULL.
199 */
ED_armature_ebone_find_shared_parent(EditBone * ebone_child[],const uint ebone_child_tot)200 EditBone *ED_armature_ebone_find_shared_parent(EditBone *ebone_child[], const uint ebone_child_tot)
201 {
202 #define EBONE_TEMP_UINT(ebone) (*((uint *)(&((ebone)->temp))))
203
204 /* clear all */
205 for (uint i = 0; i < ebone_child_tot; i++) {
206 for (EditBone *ebone_iter = ebone_child[i]; ebone_iter; ebone_iter = ebone_iter->parent) {
207 EBONE_TEMP_UINT(ebone_iter) = 0;
208 }
209 }
210
211 /* accumulate */
212 for (uint i = 0; i < ebone_child_tot; i++) {
213 for (EditBone *ebone_iter = ebone_child[i]->parent; ebone_iter;
214 ebone_iter = ebone_iter->parent) {
215 EBONE_TEMP_UINT(ebone_iter) += 1;
216 }
217 }
218
219 /* only need search the first chain */
220 for (EditBone *ebone_iter = ebone_child[0]->parent; ebone_iter;
221 ebone_iter = ebone_iter->parent) {
222 if (EBONE_TEMP_UINT(ebone_iter) == ebone_child_tot) {
223 return ebone_iter;
224 }
225 }
226
227 #undef EBONE_TEMP_UINT
228
229 return NULL;
230 }
231
ED_armature_ebone_to_mat3(EditBone * ebone,float r_mat[3][3])232 void ED_armature_ebone_to_mat3(EditBone *ebone, float r_mat[3][3])
233 {
234 float delta[3], roll;
235
236 /* Find the current bone matrix */
237 sub_v3_v3v3(delta, ebone->tail, ebone->head);
238 roll = ebone->roll;
239 if (!normalize_v3(delta)) {
240 /* Use the orientation of the parent bone if any. */
241 const EditBone *ebone_parent = ebone->parent;
242 if (ebone_parent) {
243 sub_v3_v3v3(delta, ebone_parent->tail, ebone_parent->head);
244 normalize_v3(delta);
245 roll = ebone_parent->roll;
246 }
247 }
248
249 vec_roll_to_mat3_normalized(delta, roll, r_mat);
250 }
251
ED_armature_ebone_to_mat4(EditBone * ebone,float r_mat[4][4])252 void ED_armature_ebone_to_mat4(EditBone *ebone, float r_mat[4][4])
253 {
254 float m3[3][3];
255
256 ED_armature_ebone_to_mat3(ebone, m3);
257
258 copy_m4_m3(r_mat, m3);
259 copy_v3_v3(r_mat[3], ebone->head);
260 }
261
ED_armature_ebone_from_mat3(EditBone * ebone,const float mat[3][3])262 void ED_armature_ebone_from_mat3(EditBone *ebone, const float mat[3][3])
263 {
264 float vec[3], roll;
265 const float len = len_v3v3(ebone->head, ebone->tail);
266
267 mat3_to_vec_roll(mat, vec, &roll);
268
269 madd_v3_v3v3fl(ebone->tail, ebone->head, vec, len);
270 ebone->roll = roll;
271 }
272
ED_armature_ebone_from_mat4(EditBone * ebone,const float mat[4][4])273 void ED_armature_ebone_from_mat4(EditBone *ebone, const float mat[4][4])
274 {
275 float mat3[3][3];
276
277 copy_m3_m4(mat3, mat);
278 /* We want normalized matrix here, to be consistent with ebone_to_mat. */
279 BLI_ASSERT_UNIT_M3(mat3);
280
281 sub_v3_v3(ebone->tail, ebone->head);
282 copy_v3_v3(ebone->head, mat[3]);
283 add_v3_v3(ebone->tail, mat[3]);
284 ED_armature_ebone_from_mat3(ebone, mat3);
285 }
286
287 /**
288 * Return a pointer to the bone of the given name
289 */
ED_armature_ebone_find_name(const ListBase * edbo,const char * name)290 EditBone *ED_armature_ebone_find_name(const ListBase *edbo, const char *name)
291 {
292 return BLI_findstring(edbo, name, offsetof(EditBone, name));
293 }
294
295 /* *************************************************************** */
296 /* Mirroring */
297
298 /**
299 * \see #BKE_pose_channel_get_mirrored (pose-mode, matching function)
300 */
ED_armature_ebone_get_mirrored(const ListBase * edbo,EditBone * ebo)301 EditBone *ED_armature_ebone_get_mirrored(const ListBase *edbo, EditBone *ebo)
302 {
303 char name_flip[MAXBONENAME];
304
305 if (ebo == NULL) {
306 return NULL;
307 }
308
309 BLI_string_flip_side_name(name_flip, ebo->name, false, sizeof(name_flip));
310
311 if (!STREQ(name_flip, ebo->name)) {
312 return ED_armature_ebone_find_name(edbo, name_flip);
313 }
314
315 return NULL;
316 }
317
318 /* ------------------------------------- */
319
320 /* helper function for tools to work on mirrored parts.
321 * it leaves mirrored bones selected then too, which is a good indication of what happened */
armature_select_mirrored_ex(bArmature * arm,const int flag)322 void armature_select_mirrored_ex(bArmature *arm, const int flag)
323 {
324 BLI_assert((flag & ~(BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL)) == 0);
325 /* Select mirrored bones */
326 if (arm->flag & ARM_MIRROR_EDIT) {
327 EditBone *curBone, *ebone_mirr;
328
329 for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
330 if (arm->layer & curBone->layer) {
331 if (curBone->flag & flag) {
332 ebone_mirr = ED_armature_ebone_get_mirrored(arm->edbo, curBone);
333 if (ebone_mirr) {
334 ebone_mirr->flag |= (curBone->flag & flag);
335 }
336 }
337 }
338 }
339 }
340 }
341
armature_select_mirrored(bArmature * arm)342 void armature_select_mirrored(bArmature *arm)
343 {
344 armature_select_mirrored_ex(arm, BONE_SELECTED);
345 }
346
armature_tag_select_mirrored(bArmature * arm)347 void armature_tag_select_mirrored(bArmature *arm)
348 {
349 EditBone *curBone;
350
351 /* always untag */
352 for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
353 curBone->flag &= ~BONE_DONE;
354 }
355
356 /* Select mirrored bones */
357 if (arm->flag & ARM_MIRROR_EDIT) {
358 for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
359 if (arm->layer & curBone->layer) {
360 if (curBone->flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL)) {
361 EditBone *ebone_mirr = ED_armature_ebone_get_mirrored(arm->edbo, curBone);
362 if (ebone_mirr && (ebone_mirr->flag & BONE_SELECTED) == 0) {
363 ebone_mirr->flag |= BONE_DONE;
364 }
365 }
366 }
367 }
368
369 for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
370 if (curBone->flag & BONE_DONE) {
371 EditBone *ebone_mirr = ED_armature_ebone_get_mirrored(arm->edbo, curBone);
372 curBone->flag |= ebone_mirr->flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
373 }
374 }
375 }
376 }
377
378 /* only works when tagged */
armature_tag_unselect(bArmature * arm)379 void armature_tag_unselect(bArmature *arm)
380 {
381 EditBone *curBone;
382
383 for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
384 if (curBone->flag & BONE_DONE) {
385 curBone->flag &= ~(BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL | BONE_DONE);
386 }
387 }
388 }
389
390 /* ------------------------------------- */
391
ED_armature_ebone_transform_mirror_update(bArmature * arm,EditBone * ebo,bool check_select)392 void ED_armature_ebone_transform_mirror_update(bArmature *arm, EditBone *ebo, bool check_select)
393 {
394 /* TODO When this function is called by property updates,
395 * canceling the value change will not restore mirrored bone correctly. */
396
397 /* Currently check_select==true when this function is called from a transform operator,
398 * eg. from 3d viewport. */
399
400 /* no layer check, correct mirror is more important */
401 if (!check_select || ebo->flag & (BONE_TIPSEL | BONE_ROOTSEL)) {
402 EditBone *eboflip = ED_armature_ebone_get_mirrored(arm->edbo, ebo);
403 if (eboflip) {
404 /* We assume X-axis flipping for now. */
405
406 /* Always mirror roll, since it can be changed by moving either head or tail. */
407 eboflip->roll = -ebo->roll;
408
409 if (!check_select || ebo->flag & BONE_TIPSEL) {
410 /* Mirror tail properties. */
411
412 eboflip->tail[0] = -ebo->tail[0];
413 eboflip->tail[1] = ebo->tail[1];
414 eboflip->tail[2] = ebo->tail[2];
415 eboflip->rad_tail = ebo->rad_tail;
416 eboflip->curve_out_x = -ebo->curve_out_x;
417 eboflip->curve_out_y = ebo->curve_out_y;
418 eboflip->scale_out_x = ebo->scale_out_x;
419 eboflip->scale_out_y = ebo->scale_out_y;
420 eboflip->ease2 = ebo->ease2;
421 eboflip->roll2 = -ebo->roll2;
422
423 /* Also move connected children, in case children's name aren't mirrored properly. */
424 EditBone *children;
425 for (children = arm->edbo->first; children; children = children->next) {
426 if (children->parent == eboflip && children->flag & BONE_CONNECTED) {
427 copy_v3_v3(children->head, eboflip->tail);
428 children->rad_head = ebo->rad_tail;
429 }
430 }
431 }
432
433 if (!check_select || ebo->flag & BONE_ROOTSEL) {
434 /* Mirror head properties. */
435 eboflip->head[0] = -ebo->head[0];
436 eboflip->head[1] = ebo->head[1];
437 eboflip->head[2] = ebo->head[2];
438 eboflip->rad_head = ebo->rad_head;
439
440 eboflip->curve_in_x = -ebo->curve_in_x;
441 eboflip->curve_in_y = ebo->curve_in_y;
442 eboflip->scale_in_x = ebo->scale_in_x;
443 eboflip->scale_in_y = ebo->scale_in_y;
444 eboflip->ease1 = ebo->ease1;
445 eboflip->roll1 = -ebo->roll1;
446
447 /* Also move connected parent, in case parent's name isn't mirrored properly. */
448 if (eboflip->parent && eboflip->flag & BONE_CONNECTED) {
449 EditBone *parent = eboflip->parent;
450 copy_v3_v3(parent->tail, eboflip->head);
451 parent->rad_tail = ebo->rad_head;
452 }
453 }
454
455 if (!check_select || ebo->flag & BONE_SELECTED) {
456 /* Mirror bone body properties (both head and tail are selected). */
457 /* TODO: These values can also be changed from pose mode,
458 * so only mirroring them in edit mode is not ideal. */
459 eboflip->dist = ebo->dist;
460 eboflip->weight = ebo->weight;
461
462 eboflip->segments = ebo->segments;
463 eboflip->xwidth = ebo->xwidth;
464 eboflip->zwidth = ebo->zwidth;
465 }
466 }
467 }
468 }
469
470 /* if editbone (partial) selected, copy data */
471 /* context; editmode armature, with mirror editing enabled */
ED_armature_edit_transform_mirror_update(Object * obedit)472 void ED_armature_edit_transform_mirror_update(Object *obedit)
473 {
474 bArmature *arm = obedit->data;
475 LISTBASE_FOREACH (EditBone *, ebo, arm->edbo) {
476 ED_armature_ebone_transform_mirror_update(arm, ebo, true);
477 }
478 }
479
480 /* *************************************************************** */
481 /* Armature EditMode Conversions */
482
483 /* converts Bones to EditBone list, used for tools as well */
make_boneList_recursive(ListBase * edbo,ListBase * bones,EditBone * parent,Bone * actBone)484 static EditBone *make_boneList_recursive(ListBase *edbo,
485 ListBase *bones,
486 EditBone *parent,
487 Bone *actBone)
488 {
489 EditBone *eBone;
490 EditBone *eBoneAct = NULL;
491 EditBone *eBoneTest = NULL;
492 Bone *curBone;
493
494 for (curBone = bones->first; curBone; curBone = curBone->next) {
495 eBone = MEM_callocN(sizeof(EditBone), "make_editbone");
496 eBone->temp.bone = curBone;
497
498 /* Copy relevant data from bone to eBone
499 * Keep selection logic in sync with ED_armature_edit_sync_selection.
500 */
501 eBone->parent = parent;
502 BLI_strncpy(eBone->name, curBone->name, sizeof(eBone->name));
503 eBone->flag = curBone->flag;
504 eBone->inherit_scale_mode = curBone->inherit_scale_mode;
505
506 /* fix selection flags */
507 if (eBone->flag & BONE_SELECTED) {
508 /* if the bone is selected the copy its root selection to the parents tip */
509 eBone->flag |= BONE_TIPSEL;
510 if (eBone->parent && (eBone->flag & BONE_CONNECTED)) {
511 eBone->parent->flag |= BONE_TIPSEL;
512 }
513
514 /* For connected bones, take care when changing the selection when we have a
515 * connected parent, this flag is a copy of '(eBone->parent->flag & BONE_TIPSEL)'. */
516 eBone->flag |= BONE_ROOTSEL;
517 }
518 else {
519 /* if the bone is not selected, but connected to its parent
520 * always use the parents tip selection state */
521 if (eBone->parent && (eBone->flag & BONE_CONNECTED)) {
522 eBone->flag &= ~BONE_ROOTSEL;
523 }
524 }
525
526 copy_v3_v3(eBone->head, curBone->arm_head);
527 copy_v3_v3(eBone->tail, curBone->arm_tail);
528 eBone->roll = curBone->arm_roll;
529
530 /* rest of stuff copy */
531 eBone->length = curBone->length;
532 eBone->dist = curBone->dist;
533 eBone->weight = curBone->weight;
534 eBone->xwidth = curBone->xwidth;
535 eBone->zwidth = curBone->zwidth;
536 eBone->rad_head = curBone->rad_head;
537 eBone->rad_tail = curBone->rad_tail;
538 eBone->segments = curBone->segments;
539 eBone->layer = curBone->layer;
540
541 /* Bendy-Bone parameters */
542 eBone->roll1 = curBone->roll1;
543 eBone->roll2 = curBone->roll2;
544 eBone->curve_in_x = curBone->curve_in_x;
545 eBone->curve_in_y = curBone->curve_in_y;
546 eBone->curve_out_x = curBone->curve_out_x;
547 eBone->curve_out_y = curBone->curve_out_y;
548 eBone->ease1 = curBone->ease1;
549 eBone->ease2 = curBone->ease2;
550 eBone->scale_in_x = curBone->scale_in_x;
551 eBone->scale_in_y = curBone->scale_in_y;
552 eBone->scale_out_x = curBone->scale_out_x;
553 eBone->scale_out_y = curBone->scale_out_y;
554
555 eBone->bbone_prev_type = curBone->bbone_prev_type;
556 eBone->bbone_next_type = curBone->bbone_next_type;
557
558 if (curBone->prop) {
559 eBone->prop = IDP_CopyProperty(curBone->prop);
560 }
561
562 BLI_addtail(edbo, eBone);
563
564 /* Add children if necessary. */
565 if (curBone->childbase.first) {
566 eBoneTest = make_boneList_recursive(edbo, &curBone->childbase, eBone, actBone);
567 if (eBoneTest) {
568 eBoneAct = eBoneTest;
569 }
570 }
571
572 if (curBone == actBone) {
573 eBoneAct = eBone;
574 }
575 }
576
577 return eBoneAct;
578 }
579
find_ebone_link(ListBase * edbo,Bone * link)580 static EditBone *find_ebone_link(ListBase *edbo, Bone *link)
581 {
582 if (link != NULL) {
583 LISTBASE_FOREACH (EditBone *, ebone, edbo) {
584 if (ebone->temp.bone == link) {
585 return ebone;
586 }
587 }
588 }
589
590 return NULL;
591 }
592
make_boneList(ListBase * edbo,ListBase * bones,struct Bone * actBone)593 EditBone *make_boneList(ListBase *edbo, ListBase *bones, struct Bone *actBone)
594 {
595 BLI_assert(!edbo->first && !edbo->last);
596
597 EditBone *active = make_boneList_recursive(edbo, bones, NULL, actBone);
598
599 LISTBASE_FOREACH (EditBone *, ebone, edbo) {
600 Bone *bone = ebone->temp.bone;
601
602 /* Convert custom B-Bone handle links. */
603 ebone->bbone_prev = find_ebone_link(edbo, bone->bbone_prev);
604 ebone->bbone_next = find_ebone_link(edbo, bone->bbone_next);
605 }
606
607 return active;
608 }
609
610 /**
611 * This function:
612 * - Sets local head/tail rest locations using parent bone's arm_mat.
613 * - Calls #BKE_armature_where_is_bone() which uses parent's transform (arm_mat)
614 * to define this bone's transform.
615 * - Fixes (converts) EditBone roll into Bone roll.
616 * - Calls again #BKE_armature_where_is_bone(),
617 * since roll fiddling may have changed things for our bone.
618 *
619 * \note The order is crucial here, we can only handle child
620 * if all its parents in chain have already been handled (this is ensured by recursive process).
621 */
armature_finalize_restpose(ListBase * bonelist,ListBase * editbonelist)622 static void armature_finalize_restpose(ListBase *bonelist, ListBase *editbonelist)
623 {
624 Bone *curBone;
625 EditBone *ebone;
626
627 for (curBone = bonelist->first; curBone; curBone = curBone->next) {
628 /* Set bone's local head/tail.
629 * Note that it's important to use final parent's restpose (arm_mat) here,
630 * instead of setting those values from editbone's matrix (see T46010). */
631 if (curBone->parent) {
632 float parmat_inv[4][4];
633
634 invert_m4_m4(parmat_inv, curBone->parent->arm_mat);
635
636 /* Get the new head and tail */
637 sub_v3_v3v3(curBone->head, curBone->arm_head, curBone->parent->arm_tail);
638 sub_v3_v3v3(curBone->tail, curBone->arm_tail, curBone->parent->arm_tail);
639
640 mul_mat3_m4_v3(parmat_inv, curBone->head);
641 mul_mat3_m4_v3(parmat_inv, curBone->tail);
642 }
643 else {
644 copy_v3_v3(curBone->head, curBone->arm_head);
645 copy_v3_v3(curBone->tail, curBone->arm_tail);
646 }
647
648 /* Set local matrix and arm_mat (restpose).
649 * Do not recurse into children here, armature_finalize_restpose() is already recursive. */
650 BKE_armature_where_is_bone(curBone, curBone->parent, false);
651
652 /* Find the associated editbone */
653 for (ebone = editbonelist->first; ebone; ebone = ebone->next) {
654 if (ebone->temp.bone == curBone) {
655 float premat[3][3];
656 float postmat[3][3];
657 float difmat[3][3];
658 float imat[3][3];
659
660 /* Get the ebone premat and its inverse. */
661 ED_armature_ebone_to_mat3(ebone, premat);
662 invert_m3_m3(imat, premat);
663
664 /* Get the bone postmat. */
665 copy_m3_m4(postmat, curBone->arm_mat);
666
667 mul_m3_m3m3(difmat, imat, postmat);
668
669 #if 0
670 printf("Bone %s\n", curBone->name);
671 print_m4("premat", premat);
672 print_m4("postmat", postmat);
673 print_m4("difmat", difmat);
674 printf("Roll = %f\n", RAD2DEGF(-atan2(difmat[2][0], difmat[2][2])));
675 #endif
676
677 curBone->roll = -atan2f(difmat[2][0], difmat[2][2]);
678
679 /* And set rest-position again. */
680 BKE_armature_where_is_bone(curBone, curBone->parent, false);
681 break;
682 }
683 }
684
685 /* Recurse into children... */
686 armature_finalize_restpose(&curBone->childbase, editbonelist);
687 }
688 }
689
690 /* put EditMode back in Object */
ED_armature_from_edit(Main * bmain,bArmature * arm)691 void ED_armature_from_edit(Main *bmain, bArmature *arm)
692 {
693 EditBone *eBone, *neBone;
694 Bone *newBone;
695 Object *obt;
696
697 /* armature bones */
698 BKE_armature_bone_hash_free(arm);
699 BKE_armature_bonelist_free(&arm->bonebase, true);
700 arm->act_bone = NULL;
701
702 /* remove zero sized bones, this gives unstable restposes */
703 for (eBone = arm->edbo->first; eBone; eBone = neBone) {
704 float len_sq = len_squared_v3v3(eBone->head, eBone->tail);
705 neBone = eBone->next;
706 /* TODO(sergey): How to ensure this is a constexpr? */
707 if (len_sq <= square_f(0.000001f)) { /* FLT_EPSILON is too large? */
708 EditBone *fBone;
709
710 /* Find any bones that refer to this bone */
711 for (fBone = arm->edbo->first; fBone; fBone = fBone->next) {
712 if (fBone->parent == eBone) {
713 fBone->parent = eBone->parent;
714 }
715 }
716 if (G.debug & G_DEBUG) {
717 printf("Warning: removed zero sized bone: %s\n", eBone->name);
718 }
719 bone_free(arm, eBone);
720 }
721 }
722
723 /* Copy the bones from the edit-data into the armature. */
724 for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
725 newBone = MEM_callocN(sizeof(Bone), "bone");
726 eBone->temp.bone = newBone; /* Associate the real Bones with the EditBones */
727
728 BLI_strncpy(newBone->name, eBone->name, sizeof(newBone->name));
729 copy_v3_v3(newBone->arm_head, eBone->head);
730 copy_v3_v3(newBone->arm_tail, eBone->tail);
731 newBone->arm_roll = eBone->roll;
732
733 newBone->flag = eBone->flag;
734 newBone->inherit_scale_mode = eBone->inherit_scale_mode;
735
736 if (eBone == arm->act_edbone) {
737 /* don't change active selection, this messes up separate which uses
738 * editmode toggle and can separate active bone which is de-selected originally */
739
740 /* important, editbones can be active with only 1 point selected */
741 /* newBone->flag |= BONE_SELECTED; */
742 arm->act_bone = newBone;
743 }
744 newBone->roll = 0.0f;
745
746 newBone->weight = eBone->weight;
747 newBone->dist = eBone->dist;
748
749 newBone->xwidth = eBone->xwidth;
750 newBone->zwidth = eBone->zwidth;
751 newBone->rad_head = eBone->rad_head;
752 newBone->rad_tail = eBone->rad_tail;
753 newBone->segments = eBone->segments;
754 newBone->layer = eBone->layer;
755
756 /* Bendy-Bone parameters */
757 newBone->roll1 = eBone->roll1;
758 newBone->roll2 = eBone->roll2;
759 newBone->curve_in_x = eBone->curve_in_x;
760 newBone->curve_in_y = eBone->curve_in_y;
761 newBone->curve_out_x = eBone->curve_out_x;
762 newBone->curve_out_y = eBone->curve_out_y;
763 newBone->ease1 = eBone->ease1;
764 newBone->ease2 = eBone->ease2;
765 newBone->scale_in_x = eBone->scale_in_x;
766 newBone->scale_in_y = eBone->scale_in_y;
767 newBone->scale_out_x = eBone->scale_out_x;
768 newBone->scale_out_y = eBone->scale_out_y;
769
770 newBone->bbone_prev_type = eBone->bbone_prev_type;
771 newBone->bbone_next_type = eBone->bbone_next_type;
772
773 if (eBone->prop) {
774 newBone->prop = IDP_CopyProperty(eBone->prop);
775 }
776 }
777
778 /* Fix parenting in a separate pass to ensure ebone->bone connections are valid at this point.
779 * Do not set bone->head/tail here anymore,
780 * using EditBone data for that is not OK since our later fiddling with parent's arm_mat
781 * (for roll conversion) may have some small but visible impact on locations (T46010). */
782 for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
783 newBone = eBone->temp.bone;
784 if (eBone->parent) {
785 newBone->parent = eBone->parent->temp.bone;
786 BLI_addtail(&newBone->parent->childbase, newBone);
787 }
788 /* ...otherwise add this bone to the armature's bonebase */
789 else {
790 BLI_addtail(&arm->bonebase, newBone);
791 }
792
793 /* Also transfer B-Bone custom handles. */
794 if (eBone->bbone_prev) {
795 newBone->bbone_prev = eBone->bbone_prev->temp.bone;
796 }
797 if (eBone->bbone_next) {
798 newBone->bbone_next = eBone->bbone_next->temp.bone;
799 }
800 }
801
802 /* Finalize definition of restpose data (roll, bone_mat, arm_mat, head/tail...). */
803 armature_finalize_restpose(&arm->bonebase, arm->edbo);
804
805 BKE_armature_bone_hash_make(arm);
806
807 /* so all users of this armature should get rebuilt */
808 for (obt = bmain->objects.first; obt; obt = obt->id.next) {
809 if (obt->data == arm) {
810 BKE_pose_rebuild(bmain, obt, arm, true);
811 }
812 }
813
814 DEG_id_tag_update(&arm->id, 0);
815 }
816
ED_armature_edit_free(struct bArmature * arm)817 void ED_armature_edit_free(struct bArmature *arm)
818 {
819 EditBone *eBone;
820
821 /* Clear the edit-bones list. */
822 if (arm->edbo) {
823 if (arm->edbo->first) {
824 for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
825 if (eBone->prop) {
826 IDP_FreeProperty(eBone->prop);
827 }
828 }
829
830 BLI_freelistN(arm->edbo);
831 }
832 MEM_freeN(arm->edbo);
833 arm->edbo = NULL;
834 arm->act_edbone = NULL;
835 }
836 }
837
838 /* Put armature in EditMode */
ED_armature_to_edit(bArmature * arm)839 void ED_armature_to_edit(bArmature *arm)
840 {
841 ED_armature_edit_free(arm);
842 arm->edbo = MEM_callocN(sizeof(ListBase), "edbo armature");
843 arm->act_edbone = make_boneList(arm->edbo, &arm->bonebase, arm->act_bone);
844 }
845
846 /* *************************************************************** */
847 /* Used by Undo for Armature EditMode*/
848
849 /* free's bones and their properties */
850
ED_armature_ebone_listbase_free(ListBase * lb,const bool do_id_user)851 void ED_armature_ebone_listbase_free(ListBase *lb, const bool do_id_user)
852 {
853 EditBone *ebone, *ebone_next;
854
855 for (ebone = lb->first; ebone; ebone = ebone_next) {
856 ebone_next = ebone->next;
857
858 if (ebone->prop) {
859 IDP_FreeProperty_ex(ebone->prop, do_id_user);
860 }
861
862 MEM_freeN(ebone);
863 }
864
865 BLI_listbase_clear(lb);
866 }
867
ED_armature_ebone_listbase_copy(ListBase * lb_dst,ListBase * lb_src,const bool do_id_user)868 void ED_armature_ebone_listbase_copy(ListBase *lb_dst, ListBase *lb_src, const bool do_id_user)
869 {
870 EditBone *ebone_src;
871 EditBone *ebone_dst;
872
873 BLI_assert(BLI_listbase_is_empty(lb_dst));
874
875 for (ebone_src = lb_src->first; ebone_src; ebone_src = ebone_src->next) {
876 ebone_dst = MEM_dupallocN(ebone_src);
877 if (ebone_dst->prop) {
878 ebone_dst->prop = IDP_CopyProperty_ex(ebone_dst->prop,
879 do_id_user ? 0 : LIB_ID_CREATE_NO_USER_REFCOUNT);
880 }
881 ebone_src->temp.ebone = ebone_dst;
882 BLI_addtail(lb_dst, ebone_dst);
883 }
884
885 /* set pointers */
886 for (ebone_dst = lb_dst->first; ebone_dst; ebone_dst = ebone_dst->next) {
887 if (ebone_dst->parent) {
888 ebone_dst->parent = ebone_dst->parent->temp.ebone;
889 }
890 if (ebone_dst->bbone_next) {
891 ebone_dst->bbone_next = ebone_dst->bbone_next->temp.ebone;
892 }
893 if (ebone_dst->bbone_prev) {
894 ebone_dst->bbone_prev = ebone_dst->bbone_prev->temp.ebone;
895 }
896 }
897 }
898
ED_armature_ebone_listbase_temp_clear(ListBase * lb)899 void ED_armature_ebone_listbase_temp_clear(ListBase *lb)
900 {
901 EditBone *ebone;
902 /* be sure they don't hang ever */
903 for (ebone = lb->first; ebone; ebone = ebone->next) {
904 ebone->temp.p = NULL;
905 }
906 }
907
908 /* *************************************************************** */
909 /* Low level selection functions which hide connected-parent
910 * flag behavior which gets tricky to handle in selection operators.
911 * (no flushing in ED_armature_ebone_select.*, that should be explicit) */
912
ED_armature_ebone_selectflag_get(const EditBone * ebone)913 int ED_armature_ebone_selectflag_get(const EditBone *ebone)
914 {
915 if (ebone->parent && (ebone->flag & BONE_CONNECTED)) {
916 return ((ebone->flag & (BONE_SELECTED | BONE_TIPSEL)) |
917 ((ebone->parent->flag & BONE_TIPSEL) ? BONE_ROOTSEL : 0));
918 }
919 return (ebone->flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL));
920 }
921
ED_armature_ebone_selectflag_set(EditBone * ebone,int flag)922 void ED_armature_ebone_selectflag_set(EditBone *ebone, int flag)
923 {
924 flag = flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
925
926 if (ebone->parent && (ebone->flag & BONE_CONNECTED)) {
927 ebone->flag &= ~(BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
928 ebone->parent->flag &= ~BONE_TIPSEL;
929
930 ebone->flag |= flag;
931 ebone->parent->flag |= (flag & BONE_ROOTSEL) ? BONE_TIPSEL : 0;
932 }
933 else {
934 ebone->flag &= ~(BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
935 ebone->flag |= flag;
936 }
937 }
938
ED_armature_ebone_selectflag_enable(EditBone * ebone,int flag)939 void ED_armature_ebone_selectflag_enable(EditBone *ebone, int flag)
940 {
941 BLI_assert((flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL)) != 0);
942 ED_armature_ebone_selectflag_set(ebone, ebone->flag | flag);
943 }
944
ED_armature_ebone_selectflag_disable(EditBone * ebone,int flag)945 void ED_armature_ebone_selectflag_disable(EditBone *ebone, int flag)
946 {
947 BLI_assert((flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL)) != 0);
948 ED_armature_ebone_selectflag_set(ebone, ebone->flag & ~flag);
949 }
950
951 /* could be used in more places */
ED_armature_ebone_select_set(EditBone * ebone,bool select)952 void ED_armature_ebone_select_set(EditBone *ebone, bool select)
953 {
954 int flag;
955 if (select) {
956 BLI_assert((ebone->flag & BONE_UNSELECTABLE) == 0);
957 flag = (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
958 }
959 else {
960 flag = 0;
961 }
962 ED_armature_ebone_selectflag_set(ebone, flag);
963 }
964