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