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_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_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 void ED_armature_edit_bone_remove(bArmature *arm, EditBone *exBone)
138 {
139         EditBone *curBone;
140
141         /* Find any bones that refer to this bone */
142         for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
143                 if (curBone->parent == exBone) {
144                         curBone->parent = exBone->parent;
145                         curBone->flag &= ~BONE_CONNECTED;
146                 }
147         }
148
149         bone_free(arm, exBone);
150 }
151
152 bool ED_armature_ebone_is_child_recursive(EditBone *ebone_parent, EditBone *ebone_child)
153 {
154         for (ebone_child = ebone_child->parent; ebone_child; ebone_child = ebone_child->parent) {
155                 if (ebone_child == ebone_parent)
156                         return true;
157         }
158         return false;
159 }
160
161 /**
162  * Finds the first parent shared by \a ebone_child
163  *
164  * \param ebone_child  Children bones to search
165  * \param ebone_child_tot  Size of the ebone_child array
166  * \return The shared parent or NULL.
167  */
168 EditBone *ED_armature_bone_find_shared_parent(EditBone *ebone_child[], const unsigned int ebone_child_tot)
169 {
170         unsigned int i;
171         EditBone *ebone_iter;
172
173 #define EBONE_TEMP_UINT(ebone) (*((unsigned int *)(&((ebone)->temp))))
174
175         /* clear all */
176         for (i = 0; i < ebone_child_tot; i++) {
177                 for (ebone_iter = ebone_child[i]; ebone_iter; ebone_iter = ebone_iter->parent) {
178                         EBONE_TEMP_UINT(ebone_iter) = 0;
179                 }
180         }
181
182         /* accumulate */
183         for (i = 0; i < ebone_child_tot; i++) {
184                 for (ebone_iter = ebone_child[i]->parent; ebone_iter; ebone_iter = ebone_iter->parent) {
185                         EBONE_TEMP_UINT(ebone_iter) += 1;
186                 }
187         }
188
189         /* only need search the first chain */
190         for (ebone_iter = ebone_child[0]->parent; ebone_iter; ebone_iter = ebone_iter->parent) {
191                 if (EBONE_TEMP_UINT(ebone_iter) == ebone_child_tot) {
192                         return ebone_iter;
193                 }
194         }
195
196 #undef EBONE_TEMP_UINT
197
198         return NULL;
199 }
200
201 void ED_armature_ebone_to_mat3(EditBone *ebone, float mat[3][3])
202 {
203         float delta[3];
204
205         /* Find the current bone matrix */
206         sub_v3_v3v3(delta, ebone->tail, ebone->head);
207         vec_roll_to_mat3(delta, ebone->roll, mat);
208 }
209
210 void ED_armature_ebone_to_mat4(EditBone *ebone, float mat[4][4])
211 {
212         float m3[3][3];
213
214         ED_armature_ebone_to_mat3(ebone, m3);
215
216         copy_m4_m3(mat, m3);
217         copy_v3_v3(mat[3], ebone->head);
218 }
219
220 void ED_armature_ebone_from_mat3(EditBone *ebone, float mat[3][3])
221 {
222         float vec[3], roll;
223         const float len = len_v3v3(ebone->head, ebone->tail);
224
225         mat3_to_vec_roll(mat, vec, &roll);
226
227         madd_v3_v3v3fl(ebone->tail, ebone->head, vec, len);
228         ebone->roll = roll;
229 }
230
231 void ED_armature_ebone_from_mat4(EditBone *ebone, float mat[4][4])
232 {
233         float mat3[3][3];
234
235         copy_m3_m4(mat3, mat);
236         /* We want normalized matrix here, to be consistent with ebone_to_mat. */
237         BLI_ASSERT_UNIT_M3(mat3);
238
239         sub_v3_v3(ebone->tail, ebone->head);
240         copy_v3_v3(ebone->head, mat[3]);
241         add_v3_v3(ebone->tail, mat[3]);
242         ED_armature_ebone_from_mat3(ebone, mat3);
243 }
244
245 /**
246  * Return a pointer to the bone of the given name
247  */
248 EditBone *ED_armature_bone_find_name(const ListBase *edbo, const char *name)
249 {
250         return BLI_findstring(edbo, name, offsetof(EditBone, name));
251 }
252
253
254 /* *************************************************************** */
255 /* Mirroring */
256
257 /**
258  * \see #BKE_pose_channel_get_mirrored (pose-mode, matching function)
259  */
260 EditBone *ED_armature_bone_get_mirrored(const ListBase *edbo, EditBone *ebo)
261 {
262         char name_flip[MAXBONENAME];
263
264         if (ebo == NULL)
265                 return NULL;
266         
267         BLI_string_flip_side_name(name_flip, ebo->name, false, sizeof(name_flip));
268         
269         if (!STREQ(name_flip, ebo->name)) {
270                 return ED_armature_bone_find_name(edbo, name_flip);
271         }
272         
273         return NULL;
274 }
275
276 /* ------------------------------------- */
277
278 /* helper function for tools to work on mirrored parts.
279  * it leaves mirrored bones selected then too, which is a good indication of what happened */
280 void armature_select_mirrored_ex(bArmature *arm, const int flag)
281 {
282         BLI_assert((flag & ~(BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL)) == 0);
283         /* Select mirrored bones */
284         if (arm->flag & ARM_MIRROR_EDIT) {
285                 EditBone *curBone, *ebone_mirr;
286                 
287                 for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
288                         if (arm->layer & curBone->layer) {
289                                 if (curBone->flag & flag) {
290                                         ebone_mirr = ED_armature_bone_get_mirrored(arm->edbo, curBone);
291                                         if (ebone_mirr)
292                                                 ebone_mirr->flag |= (curBone->flag & flag);
293                                 }
294                         }
295                 }
296         }
297         
298 }
299
300 void armature_select_mirrored(bArmature *arm)
301 {
302         armature_select_mirrored_ex(arm, BONE_SELECTED);
303 }
304
305 void armature_tag_select_mirrored(bArmature *arm)
306 {
307         EditBone *curBone;
308
309         /* always untag */
310         for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
311                 curBone->flag &= ~BONE_DONE;
312         }
313
314         /* Select mirrored bones */
315         if (arm->flag & ARM_MIRROR_EDIT) {
316                 for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
317                         if (arm->layer & curBone->layer) {
318                                 if (curBone->flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL)) {
319                                         EditBone *ebone_mirr = ED_armature_bone_get_mirrored(arm->edbo, curBone);
320                                         if (ebone_mirr && (ebone_mirr->flag & BONE_SELECTED) == 0) {
321                                                 ebone_mirr->flag |= BONE_DONE;
322                                         }
323                                 }
324                         }
325                 }
326                 
327                 for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
328                         if (curBone->flag & BONE_DONE) {
329                                 EditBone *ebone_mirr = ED_armature_bone_get_mirrored(arm->edbo, curBone);
330                                 curBone->flag |= ebone_mirr->flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
331                         }
332                 }
333         }
334 }
335
336 /* only works when tagged */
337 void armature_tag_unselect(bArmature *arm)
338 {
339         EditBone *curBone;
340
341         for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
342                 if (curBone->flag & BONE_DONE) {
343                         curBone->flag &= ~(BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL | BONE_DONE);
344                 }
345         }
346 }
347
348 /* ------------------------------------- */
349
350 /* if editbone (partial) selected, copy data */
351 /* context; editmode armature, with mirror editing enabled */
352 void transform_armature_mirror_update(Object *obedit)
353 {
354         bArmature *arm = obedit->data;
355         EditBone *ebo, *eboflip;
356         
357         for (ebo = arm->edbo->first; ebo; ebo = ebo->next) {
358                 /* no layer check, correct mirror is more important */
359                 if (ebo->flag & (BONE_TIPSEL | BONE_ROOTSEL)) {
360                         eboflip = ED_armature_bone_get_mirrored(arm->edbo, ebo);
361                         
362                         if (eboflip) {
363                                 /* we assume X-axis flipping for now */
364                                 if (ebo->flag & BONE_TIPSEL) {
365                                         EditBone *children;
366                                         
367                                         eboflip->tail[0] = -ebo->tail[0];
368                                         eboflip->tail[1] = ebo->tail[1];
369                                         eboflip->tail[2] = ebo->tail[2];
370                                         eboflip->rad_tail = ebo->rad_tail;
371                                         eboflip->roll = -ebo->roll;
372                                         eboflip->curveOutX = -ebo->curveOutX;
373                                         eboflip->roll2 = -ebo->roll2;
374                                         
375                                         /* Also move connected children, in case children's name aren't mirrored properly */
376                                         for (children = arm->edbo->first; children; children = children->next) {
377                                                 if (children->parent == eboflip && children->flag & BONE_CONNECTED) {
378                                                         copy_v3_v3(children->head, eboflip->tail);
379                                                         children->rad_head = ebo->rad_tail;
380                                                 }
381                                         }
382                                 }
383                                 if (ebo->flag & BONE_ROOTSEL) {
384                                         eboflip->head[0] = -ebo->head[0];
385                                         eboflip->head[1] = ebo->head[1];
386                                         eboflip->head[2] = ebo->head[2];
387                                         eboflip->rad_head = ebo->rad_head;
388                                         eboflip->roll = -ebo->roll;
389                                         eboflip->curveInX = -ebo->curveInX;
390                                         eboflip->roll1 = -ebo->roll1;
391                                         
392                                         /* Also move connected parent, in case parent's name isn't mirrored properly */
393                                         if (eboflip->parent && eboflip->flag & BONE_CONNECTED) {
394                                                 EditBone *parent = eboflip->parent;
395                                                 copy_v3_v3(parent->tail, eboflip->head);
396                                                 parent->rad_tail = ebo->rad_head;
397                                         }
398                                 }
399                                 if (ebo->flag & BONE_SELECTED) {
400                                         eboflip->dist = ebo->dist;
401                                         eboflip->roll = -ebo->roll;
402                                         eboflip->xwidth = ebo->xwidth;
403                                         eboflip->zwidth = ebo->zwidth;
404                                         
405                                         eboflip->curveInX = -ebo->curveInX;
406                                         eboflip->curveOutX = -ebo->curveOutX;
407                                         eboflip->roll1 = -ebo->roll1;
408                                         eboflip->roll2 = -ebo->roll2;
409                                 }
410                         }
411                 }
412         }
413 }
414
415 /* *************************************************************** */
416 /* Armature EditMode Conversions */
417
418 /* converts Bones to EditBone list, used for tools as well */
419 EditBone *make_boneList(ListBase *edbo, ListBase *bones, EditBone *parent, Bone *actBone)
420 {
421         EditBone    *eBone;
422         EditBone    *eBoneAct = NULL;
423         EditBone    *eBoneTest = NULL;
424         Bone        *curBone;
425                 
426         for (curBone = bones->first; curBone; curBone = curBone->next) {
427                 eBone = MEM_callocN(sizeof(EditBone), "make_editbone");
428                 
429                 /* Copy relevant data from bone to eBone
430                  * Keep selection logic in sync with ED_armature_sync_selection.
431                  */
432                 eBone->parent = parent;
433                 BLI_strncpy(eBone->name, curBone->name, sizeof(eBone->name));
434                 eBone->flag = curBone->flag;
435                 
436                 /* fix selection flags */
437                 if (eBone->flag & BONE_SELECTED) {
438                         /* if the bone is selected the copy its root selection to the parents tip */
439                         eBone->flag |= BONE_TIPSEL;
440                         if (eBone->parent && (eBone->flag & BONE_CONNECTED)) {
441                                 eBone->parent->flag |= BONE_TIPSEL;
442                         }
443
444                         /* For connected bones, take care when changing the selection when we have a connected parent,
445                          * this flag is a copy of '(eBone->parent->flag & BONE_TIPSEL)'. */
446                         eBone->flag |= BONE_ROOTSEL;
447                 }
448                 else {
449                         /* if the bone is not selected, but connected to its parent
450                          * always use the parents tip selection state */
451                         if (eBone->parent && (eBone->flag & BONE_CONNECTED)) {
452                                 eBone->flag &= ~BONE_ROOTSEL;
453                         }
454                 }
455                 
456                 copy_v3_v3(eBone->head, curBone->arm_head);
457                 copy_v3_v3(eBone->tail, curBone->arm_tail);
458                 eBone->roll = curBone->arm_roll;
459                 
460                 /* rest of stuff copy */
461                 eBone->length = curBone->length;
462                 eBone->dist = curBone->dist;
463                 eBone->weight = curBone->weight;
464                 eBone->xwidth = curBone->xwidth;
465                 eBone->zwidth = curBone->zwidth;
466                 eBone->rad_head = curBone->rad_head;
467                 eBone->rad_tail = curBone->rad_tail;
468                 eBone->segments = curBone->segments;
469                 eBone->layer = curBone->layer;
470
471                 /* Bendy-Bone parameters */
472                 eBone->roll1 = curBone->roll1;
473                 eBone->roll2 = curBone->roll2;
474                 eBone->curveInX = curBone->curveInX;
475                 eBone->curveInY = curBone->curveInY;
476                 eBone->curveOutX = curBone->curveOutX;
477                 eBone->curveOutY = curBone->curveOutY;
478                 eBone->ease1 = curBone->ease1;
479                 eBone->ease2 = curBone->ease2;
480                 eBone->scaleIn = curBone->scaleIn;
481                 eBone->scaleOut = curBone->scaleOut;
482
483                 if (curBone->prop)
484                         eBone->prop = IDP_CopyProperty(curBone->prop);
485                 
486                 BLI_addtail(edbo, eBone);
487                 
488                 /*      Add children if necessary */
489                 if (curBone->childbase.first) {
490                         eBoneTest = make_boneList(edbo, &curBone->childbase, eBone, actBone);
491                         if (eBoneTest)
492                                 eBoneAct = eBoneTest;
493                 }
494                 
495                 if (curBone == actBone)
496                         eBoneAct = eBone;
497         }
498         
499         return eBoneAct;
500 }
501
502 /* This function:
503  *     - sets local head/tail rest locations using parent bone's arm_mat.
504  *     - calls BKE_armature_where_is_bone() which uses parent's transform (arm_mat) to define this bone's transform.
505  *     - fixes (converts) EditBone roll into Bone roll.
506  *     - calls again BKE_armature_where_is_bone(), since roll fiddling may have changed things for our bone...
507  * Note that order is crucial here, we can only handle child if all its parents in chain have already been handled
508  * (this is ensured by recursive process). */
509 static void armature_finalize_restpose(ListBase *bonelist, ListBase *editbonelist)
510 {
511         Bone *curBone;
512         EditBone *ebone;
513
514         for (curBone = bonelist->first; curBone; curBone = curBone->next) {
515                 /* Set bone's local head/tail.
516                  * Note that it's important to use final parent's restpose (arm_mat) here, instead of setting those values
517                  * from editbone's matrix (see T46010). */
518                 if (curBone->parent) {
519                         float parmat_inv[4][4];
520
521                         invert_m4_m4(parmat_inv, curBone->parent->arm_mat);
522
523                         /* Get the new head and tail */
524                         sub_v3_v3v3(curBone->head, curBone->arm_head, curBone->parent->arm_tail);
525                         sub_v3_v3v3(curBone->tail, curBone->arm_tail, curBone->parent->arm_tail);
526
527                         mul_mat3_m4_v3(parmat_inv, curBone->head);
528                         mul_mat3_m4_v3(parmat_inv, curBone->tail);
529                 }
530                 else {
531                         copy_v3_v3(curBone->head, curBone->arm_head);
532                         copy_v3_v3(curBone->tail, curBone->arm_tail);
533                 }
534
535                 /* Set local matrix and arm_mat (restpose).
536                  * Do not recurse into children here, armature_finalize_restpose() is already recursive. */
537                 BKE_armature_where_is_bone(curBone, curBone->parent, false);
538
539                 /* Find the associated editbone */
540                 for (ebone = editbonelist->first; ebone; ebone = ebone->next) {
541                         if (ebone->temp.bone == curBone) {
542                                 float premat[3][3];
543                                 float postmat[3][3];
544                                 float difmat[3][3];
545                                 float imat[3][3];
546
547                                 /* Get the ebone premat and its inverse. */
548                                 ED_armature_ebone_to_mat3(ebone, premat);
549                                 invert_m3_m3(imat, premat);
550
551                                 /* Get the bone postmat. */
552                                 copy_m3_m4(postmat, curBone->arm_mat);
553
554                                 mul_m3_m3m3(difmat, imat, postmat);
555
556 #if 0
557                                 printf("Bone %s\n", curBone->name);
558                                 print_m4("premat", premat);
559                                 print_m4("postmat", postmat);
560                                 print_m4("difmat", difmat);
561                                 printf("Roll = %f\n",  RAD2DEGF(-atan2(difmat[2][0], difmat[2][2])));
562 #endif
563
564                                 curBone->roll = -atan2f(difmat[2][0], difmat[2][2]);
565
566                                 /* and set restposition again */
567                                 BKE_armature_where_is_bone(curBone, curBone->parent, false);
568                                 break;
569                         }
570                 }
571
572                 /* Recurse into children... */
573                 armature_finalize_restpose(&curBone->childbase, editbonelist);
574         }
575 }
576
577 /* put EditMode back in Object */
578 void ED_armature_from_edit(bArmature *arm)
579 {
580         EditBone *eBone, *neBone;
581         Bone *newBone;
582         Object *obt;
583         
584         /* armature bones */
585         BKE_armature_bonelist_free(&arm->bonebase);
586         arm->act_bone = NULL;
587         
588         /* remove zero sized bones, this gives unstable restposes */
589         for (eBone = arm->edbo->first; eBone; eBone = neBone) {
590                 float len_sq = len_squared_v3v3(eBone->head, eBone->tail);
591                 neBone = eBone->next;
592                 if (len_sq <= SQUARE(0.000001f)) {  /* FLT_EPSILON is too large? */
593                         EditBone *fBone;
594                         
595                         /*      Find any bones that refer to this bone  */
596                         for (fBone = arm->edbo->first; fBone; fBone = fBone->next) {
597                                 if (fBone->parent == eBone)
598                                         fBone->parent = eBone->parent;
599                         }
600                         if (G.debug & G_DEBUG)
601                                 printf("Warning: removed zero sized bone: %s\n", eBone->name);
602                         bone_free(arm, eBone);
603                 }
604         }
605         
606         /*      Copy the bones from the editData into the armature */
607         for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
608                 newBone = MEM_callocN(sizeof(Bone), "bone");
609                 eBone->temp.bone = newBone;   /* Associate the real Bones with the EditBones */
610                 
611                 BLI_strncpy(newBone->name, eBone->name, sizeof(newBone->name));
612                 copy_v3_v3(newBone->arm_head, eBone->head);
613                 copy_v3_v3(newBone->arm_tail, eBone->tail);
614                 newBone->arm_roll = eBone->roll;
615                 
616                 newBone->flag = eBone->flag;
617                 
618                 if (eBone == arm->act_edbone) {
619                         /* don't change active selection, this messes up separate which uses
620                          * editmode toggle and can separate active bone which is de-selected originally */
621                         /* newBone->flag |= BONE_SELECTED; */ /* important, editbones can be active with only 1 point selected */
622                         arm->act_bone = newBone;
623                 }
624                 newBone->roll = 0.0f;
625                 
626                 newBone->weight = eBone->weight;
627                 newBone->dist = eBone->dist;
628                 
629                 newBone->xwidth = eBone->xwidth;
630                 newBone->zwidth = eBone->zwidth;
631                 newBone->rad_head = eBone->rad_head;
632                 newBone->rad_tail = eBone->rad_tail;
633                 newBone->segments = eBone->segments;
634                 newBone->layer = eBone->layer;
635
636                 /* Bendy-Bone parameters */
637                 newBone->roll1 = eBone->roll1;
638                 newBone->roll2 = eBone->roll2;
639                 newBone->curveInX = eBone->curveInX;
640                 newBone->curveInY = eBone->curveInY;
641                 newBone->curveOutX = eBone->curveOutX;
642                 newBone->curveOutY = eBone->curveOutY;
643                 newBone->ease1 = eBone->ease1;
644                 newBone->ease2 = eBone->ease2;
645                 newBone->scaleIn = eBone->scaleIn;
646                 newBone->scaleOut = eBone->scaleOut;
647
648
649                 if (eBone->prop)
650                         newBone->prop = IDP_CopyProperty(eBone->prop);
651         }
652         
653         /* Fix parenting in a separate pass to ensure ebone->bone connections are valid at this point.
654          * Do not set bone->head/tail here anymore, using EditBone data for that is not OK since our later fiddling
655          * with parent's arm_mat (for roll conversion) may have some small but visible impact on locations (T46010). */
656         for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
657                 newBone = eBone->temp.bone;
658                 if (eBone->parent) {
659                         newBone->parent = eBone->parent->temp.bone;
660                         BLI_addtail(&newBone->parent->childbase, newBone);
661                 }
662                 /*      ...otherwise add this bone to the armature's bonebase */
663                 else {
664                         BLI_addtail(&arm->bonebase, newBone);
665                 }
666         }
667         
668         /* Finalize definition of restpose data (roll, bone_mat, arm_mat, head/tail...). */
669         armature_finalize_restpose(&arm->bonebase, arm->edbo);
670         
671         /* so all users of this armature should get rebuilt */
672         for (obt = G.main->object.first; obt; obt = obt->id.next) {
673                 if (obt->data == arm) {
674                         BKE_pose_rebuild(obt, arm);
675                 }
676         }
677         
678         DEG_id_tag_update(&arm->id, 0);
679 }
680
681 void ED_armature_edit_free(struct bArmature *arm)
682 {
683         EditBone *eBone;
684         
685         /*      Clear the editbones list */
686         if (arm->edbo) {
687                 if (arm->edbo->first) {
688                         for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
689                                 if (eBone->prop) {
690                                         IDP_FreeProperty(eBone->prop);
691                                         MEM_freeN(eBone->prop);
692                                 }
693                         }
694                         
695                         BLI_freelistN(arm->edbo);
696                 }
697                 MEM_freeN(arm->edbo);
698                 arm->edbo = NULL;
699                 arm->act_edbone = NULL;
700         }
701 }
702
703 /* Put armature in EditMode */
704 void ED_armature_to_edit(bArmature *arm)
705 {
706         ED_armature_edit_free(arm);
707         arm->edbo = MEM_callocN(sizeof(ListBase), "edbo armature");
708         arm->act_edbone = make_boneList(arm->edbo, &arm->bonebase, NULL, arm->act_bone);
709
710 //      BIF_freeTemplates(); /* force template update when entering editmode */
711 }
712
713 /* *************************************************************** */
714 /* Used by Undo for Armature EditMode*/
715
716 /* free's bones and their properties */
717
718 void ED_armature_ebone_listbase_free(ListBase *lb)
719 {
720         EditBone *ebone, *ebone_next;
721
722         for (ebone = lb->first; ebone; ebone = ebone_next) {
723                 ebone_next = ebone->next;
724
725                 if (ebone->prop) {
726                         IDP_FreeProperty(ebone->prop);
727                         MEM_freeN(ebone->prop);
728                 }
729
730                 MEM_freeN(ebone);
731         }
732
733         BLI_listbase_clear(lb);
734 }
735
736 void ED_armature_ebone_listbase_copy(ListBase *lb_dst, ListBase *lb_src)
737 {
738         EditBone *ebone_src;
739         EditBone *ebone_dst;
740
741         BLI_assert(BLI_listbase_is_empty(lb_dst));
742
743         for (ebone_src = lb_src->first; ebone_src; ebone_src = ebone_src->next) {
744                 ebone_dst = MEM_dupallocN(ebone_src);
745                 if (ebone_dst->prop) {
746                         ebone_dst->prop = IDP_CopyProperty(ebone_dst->prop);
747                 }
748                 ebone_src->temp.ebone = ebone_dst;
749                 BLI_addtail(lb_dst, ebone_dst);
750         }
751
752         /* set pointers */
753         for (ebone_dst = lb_dst->first; ebone_dst; ebone_dst = ebone_dst->next) {
754                 if (ebone_dst->parent) {
755                         ebone_dst->parent = ebone_dst->parent->temp.ebone;
756                 }
757         }
758 }
759
760 void ED_armature_ebone_listbase_temp_clear(ListBase *lb)
761 {
762         EditBone *ebone;
763         /* be sure they don't hang ever */
764         for (ebone = lb->first; ebone; ebone = ebone->next) {
765                 ebone->temp.p = NULL;
766         }
767 }
768
769 /* *************************************************************** */
770 /* Low level selection functions which hide connected-parent
771  * flag behavior which gets tricky to handle in selection operators.
772  * (no flushing in ED_armature_ebone_select.*, that should be explicit) */
773
774 int ED_armature_ebone_selectflag_get(const EditBone *ebone)
775 {
776         if (ebone->parent && (ebone->flag & BONE_CONNECTED)) {
777                 return ((ebone->flag & (BONE_SELECTED | BONE_TIPSEL)) |
778                         ((ebone->parent->flag & BONE_TIPSEL) ? BONE_ROOTSEL : 0));
779         }
780         else {
781                 return (ebone->flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL));
782         }
783 }
784
785 void ED_armature_ebone_selectflag_set(EditBone *ebone, int flag)
786 {
787         flag = flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
788
789         if (ebone->parent && (ebone->flag & BONE_CONNECTED)) {
790                 ebone->flag &= ~(BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
791                 ebone->parent->flag &= ~BONE_TIPSEL;
792
793                 ebone->flag |= flag;
794                 ebone->parent->flag |= (flag & BONE_ROOTSEL) ? BONE_TIPSEL : 0;
795         }
796         else {
797                 ebone->flag &= ~(BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
798                 ebone->flag |= flag;
799         }
800 }
801
802 void ED_armature_ebone_selectflag_enable(EditBone *ebone, int flag)
803 {
804         BLI_assert((flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL)) != 0);
805         ED_armature_ebone_selectflag_set(ebone, ebone->flag | flag);
806 }
807
808 void ED_armature_ebone_selectflag_disable(EditBone *ebone, int flag)
809 {
810         BLI_assert((flag & (BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL)) != 0);
811         ED_armature_ebone_selectflag_set(ebone, ebone->flag & ~flag);
812 }
813
814 /* could be used in more places */
815 void ED_armature_ebone_select_set(EditBone *ebone, bool select)
816 {
817         int flag;
818         if (select) {
819                 BLI_assert((ebone->flag & BONE_UNSELECTABLE) == 0);
820                 flag = (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
821         }
822         else {
823                 flag = 0;
824         }
825         ED_armature_ebone_selectflag_set(ebone, flag);
826 }