Blender  V2.93
armature_utils.c
Go to the documentation of this file.
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 
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 */
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 
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  */
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 */
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 
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 
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 
179 {
180  ED_armature_ebone_remove_ex(arm, exBone, true);
181 }
182 
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 
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 
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 
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 
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 
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 
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 
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 */
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 
343 {
345 }
346 
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 */
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 
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 */
473 {
474  bArmature *arm = obedit->data;
475  LISTBASE_FOREACH (EditBone *, ebo, arm->edbo) {
477  }
478 }
479 
480 /* *************************************************************** */
481 /* Armature EditMode Conversions */
482 
483 /* converts Bones to EditBone list, used for tools as well */
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 
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 
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 
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 */
692 {
693  EditBone *eBone, *neBone;
694  Bone *newBone;
695  Object *obt;
696 
697  /* armature bones */
700  arm->act_bone = NULL;
701 
702  /* Remove zero sized bones, this gives unstable rest-poses. */
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...). */
804 
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 
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 */
840 {
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 
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 
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 
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 
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 
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 
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 
940 {
941  BLI_assert((flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL)) != 0);
942  ED_armature_ebone_selectflag_set(ebone, ebone->flag | flag);
943 }
944 
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 */
953 {
954  int flag;
955  if (select) {
956  BLI_assert((ebone->flag & BONE_UNSELECTABLE) == 0);
958  }
959  else {
960  flag = 0;
961  }
963 }
void BKE_armature_where_is_bone(struct Bone *bone, const struct Bone *bone_parent, const bool use_recursion)
void BKE_pose_rebuild(struct Main *bmain, struct Object *ob, struct bArmature *arm, const bool do_id_user)
Definition: armature.c:2536
void BKE_armature_bonelist_free(struct ListBase *lb, const bool do_id_user)
Definition: armature.c:371
void BKE_armature_bone_hash_free(struct bArmature *arm)
Definition: armature.c:651
void mat3_to_vec_roll(const float mat[3][3], float r_vec[3], float *r_roll)
Definition: armature.c:2059
void BKE_armature_bone_hash_make(struct bArmature *arm)
Definition: armature.c:644
void vec_roll_to_mat3_normalized(const float nor[3], const float roll, float r_mat[3][3])
Definition: armature.c:2155
support for deformation groups and hooks.
@ G_DEBUG
Definition: BKE_global.h:133
void IDP_FreeProperty_ex(struct IDProperty *prop, const bool do_id_user)
Definition: idprop.c:1034
void IDP_FreeProperty(struct IDProperty *prop)
Definition: idprop.c:1040
struct IDProperty * IDP_CopyProperty_ex(const struct IDProperty *prop, const int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
struct IDProperty * IDP_CopyProperty(const struct IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
@ LIB_ID_CREATE_NO_USER_REFCOUNT
Definition: BKE_lib_id.h:92
#define BLI_assert(a)
Definition: BLI_assert.h:58
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:281
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:128
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
void * BLI_findstring(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
#define BLI_ASSERT_UNIT_M3(m)
MINLINE float square_f(float a)
void copy_m3_m4(float m1[3][3], const float m2[4][4])
Definition: math_matrix.c:105
void copy_m4_m3(float m1[4][4], const float m2[3][3])
Definition: math_matrix.c:120
void mul_mat3_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:794
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1278
bool invert_m3_m3(float R[3][3], const float A[3][3])
Definition: math_matrix.c:1161
void print_m4(const char *str, const float M[4][4])
Definition: math_matrix.c:2753
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
Definition: math_matrix.c:391
#define RAD2DEGF(_rad)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float normalize_v3(float r[3])
MINLINE void sub_v3_v3(float r[3], const float a[3])
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
MINLINE void add_v3_v3(float r[3], const float a[3])
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
void BLI_string_flip_side_name(char *r_name, const char *from_name, const bool strip_number, const size_t name_len)
Definition: string_utils.c:159
unsigned int uint
Definition: BLI_sys_types.h:83
#define STREQ(a, b)
void DEG_id_tag_update(struct ID *id, int flag)
#define MAXBONENAME
@ BONE_ROOTSEL
@ BONE_SELECTED
@ BONE_UNSELECTABLE
@ BONE_DONE
@ BONE_HIDDEN_A
@ BONE_TIPSEL
@ BONE_CONNECTED
@ ARM_MIRROR_EDIT
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
bool ED_armature_ebone_is_child_recursive(EditBone *ebone_parent, EditBone *ebone_child)
void ED_armature_ebone_selectflag_disable(EditBone *ebone, int flag)
void ED_armature_edit_transform_mirror_update(Object *obedit)
void ED_armature_ebone_transform_mirror_update(bArmature *arm, EditBone *ebo, bool check_select)
void armature_tag_select_mirrored(bArmature *arm)
int bone_looper(Object *ob, Bone *bone, void *data, int(*bone_func)(Object *, Bone *, void *))
void ED_armature_ebone_listbase_temp_clear(ListBase *lb)
static EditBone * make_boneList_recursive(ListBase *edbo, ListBase *bones, EditBone *parent, Bone *actBone)
void ED_armature_ebone_from_mat3(EditBone *ebone, const float mat[3][3])
void ED_armature_ebone_to_mat4(EditBone *ebone, float r_mat[4][4])
void ED_armature_edit_refresh_layer_used(bArmature *arm)
void ED_armature_ebone_remove_ex(bArmature *arm, EditBone *exBone, bool clear_connected)
void ED_armature_edit_sync_selection(ListBase *edbo)
EditBone * make_boneList(ListBase *edbo, ListBase *bones, struct Bone *actBone)
int ED_armature_ebone_selectflag_get(const EditBone *ebone)
void ED_armature_ebone_selectflag_set(EditBone *ebone, int flag)
void armature_select_mirrored_ex(bArmature *arm, const int flag)
void ED_armature_ebone_select_set(EditBone *ebone, bool select)
EditBone * ED_armature_ebone_find_shared_parent(EditBone *ebone_child[], const uint ebone_child_tot)
void ED_armature_edit_validate_active(struct bArmature *arm)
void ED_armature_ebone_listbase_free(ListBase *lb, const bool do_id_user)
static EditBone * find_ebone_link(ListBase *edbo, Bone *link)
static void armature_finalize_restpose(ListBase *bonelist, ListBase *editbonelist)
void armature_select_mirrored(bArmature *arm)
void ED_armature_ebone_to_mat3(EditBone *ebone, float r_mat[3][3])
void ED_armature_ebone_listbase_copy(ListBase *lb_dst, ListBase *lb_src, const bool do_id_user)
void ED_armature_edit_free(struct bArmature *arm)
void ED_armature_from_edit(Main *bmain, bArmature *arm)
void ED_armature_ebone_selectflag_enable(EditBone *ebone, int flag)
void armature_tag_unselect(bArmature *arm)
void ED_armature_ebone_remove(bArmature *arm, EditBone *exBone)
EditBone * ED_armature_ebone_find_name(const ListBase *edbo, const char *name)
void ED_armature_ebone_from_mat4(EditBone *ebone, const float mat[4][4])
#define EBONE_TEMP_UINT(ebone)
void bone_free(bArmature *arm, EditBone *bone)
void ED_armature_to_edit(bArmature *arm)
EditBone * ED_armature_ebone_get_mirrored(const ListBase *edbo, EditBone *ebo)
int count
#define atan2f(x, y)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
INLINE Rall1d< T, V, S > atan2(const Rall1d< T, V, S > &y, const Rall1d< T, V, S > &x)
Definition: rall1d.h:429
bool active
all scheduled work for the GPU.
float curve_out_y
float ease1
float roll
float zwidth
float curve_in_y
struct Bone * parent
struct Bone * bbone_prev
float roll1
float arm_head[3]
float roll2
char name[64]
float xwidth
float tail[3]
float arm_tail[3]
struct Bone * bbone_next
char inherit_scale_mode
char bbone_prev_type
float scale_in_x
float scale_in_y
short segments
float curve_in_x
float rad_head
IDProperty * prop
float length
float scale_out_x
float ease2
float arm_mat[4][4]
float rad_tail
float head[3]
float scale_out_y
char bbone_next_type
float dist
struct Bone * next
float curve_out_x
ListBase childbase
float weight
float arm_roll
char name[64]
Definition: BKE_armature.h:57
float ease2
Definition: BKE_armature.h:85
float weight
Definition: BKE_armature.h:75
float roll1
Definition: BKE_armature.h:82
float scale_out_y
Definition: BKE_armature.h:87
struct EditBone * next
Definition: BKE_armature.h:49
short segments
Definition: BKE_armature.h:81
float tail[3]
Definition: BKE_armature.h:66
float roll
Definition: BKE_armature.h:62
char bbone_prev_type
Definition: BKE_armature.h:93
float roll2
Definition: BKE_armature.h:82
struct EditBone * ebone
Definition: BKE_armature.h:112
float curve_in_x
Definition: BKE_armature.h:83
float scale_out_x
Definition: BKE_armature.h:87
struct Bone * bone
Definition: BKE_armature.h:113
float zwidth
Definition: BKE_armature.h:77
struct EditBone * bbone_next
Definition: BKE_armature.h:97
float length
Definition: BKE_armature.h:77
float xwidth
Definition: BKE_armature.h:77
float dist
Definition: BKE_armature.h:75
float curve_in_y
Definition: BKE_armature.h:83
char bbone_next_type
Definition: BKE_armature.h:94
struct EditBone * parent
Definition: BKE_armature.h:55
void * p
Definition: BKE_armature.h:114
float rad_tail
Definition: BKE_armature.h:78
union EditBone::@2 temp
struct IDProperty * prop
Definition: BKE_armature.h:51
float ease1
Definition: BKE_armature.h:85
float rad_head
Definition: BKE_armature.h:78
char inherit_scale_mode
Definition: BKE_armature.h:72
float curve_out_y
Definition: BKE_armature.h:84
float curve_out_x
Definition: BKE_armature.h:84
struct EditBone * bbone_prev
Definition: BKE_armature.h:96
float scale_in_y
Definition: BKE_armature.h:86
float head[3]
Definition: BKE_armature.h:65
float scale_in_x
Definition: BKE_armature.h:86
void * next
Definition: DNA_ID.h:274
void * last
Definition: DNA_listBase.h:47
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
ListBase objects
Definition: BKE_main.h:148
void * data
unsigned int layer_used
ListBase bonebase
struct EditBone * act_edbone
unsigned int layer
ListBase * edbo
__forceinline const avxb select(const avxb &m, const avxb &t, const avxb &f)
Definition: util_avxb.h:167
#define G(x, y, z)
uint len