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