59453f0ac7147ae626c0c2f82b21205734074fa7
[blender.git] / source / blender / editors / armature / armature_add.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  * Operators and API's for creating bones
26  */
27
28 /** \file blender/editors/armature/armature_add.c
29  *  \ingroup edarmature
30  */
31
32 #include "DNA_armature_types.h"
33 #include "DNA_constraint_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_scene_types.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_math.h"
41
42 #include "BKE_action.h"
43 #include "BKE_constraint.h"
44 #include "BKE_context.h"
45 #include "BKE_idprop.h"
46 #include "BKE_deform.h"
47
48 #include "RNA_access.h"
49 #include "RNA_define.h"
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53
54 #include "ED_armature.h"
55 #include "ED_screen.h"
56 #include "ED_view3d.h"
57
58 #include "armature_intern.h"
59
60 /* *************** Adding stuff in editmode *************** */
61
62 /* default bone add, returns it selected, but without tail set */
63 /* XXX should be used everywhere, now it mallocs bones still locally in functions */
64 EditBone *ED_armature_edit_bone_add(bArmature *arm, const char *name)
65 {
66         EditBone *bone = MEM_callocN(sizeof(EditBone), "eBone");
67         
68         BLI_strncpy(bone->name, name, sizeof(bone->name));
69         unique_editbone_name(arm->edbo, bone->name, NULL);
70         
71         BLI_addtail(arm->edbo, bone);
72         
73         bone->flag |= BONE_TIPSEL;
74         bone->weight = 1.0f;
75         bone->dist = 0.25f;
76         bone->xwidth = 0.1f;
77         bone->zwidth = 0.1f;
78         bone->ease1 = 1.0f;
79         bone->ease2 = 1.0f;
80         bone->rad_head = 0.10f;
81         bone->rad_tail = 0.05f;
82         bone->segments = 1;
83         bone->layer = arm->layer;
84         
85         return bone;
86 }
87
88 EditBone *ED_armature_edit_bone_add_primitive(Object *obedit_arm, float length, bool view_aligned)
89 {
90         bArmature *arm = obedit_arm->data;
91         EditBone *bone;
92
93         ED_armature_deselect_all(obedit_arm);
94         
95         /* Create a bone */
96         bone = ED_armature_edit_bone_add(arm, "Bone");
97
98         arm->act_edbone = bone;
99
100         zero_v3(bone->head);
101         zero_v3(bone->tail);
102
103         bone->tail[view_aligned ? 1 : 2] = length;
104
105         return bone;
106 }
107
108
109 /* previously addvert_armature */
110 /* the ctrl-click method */
111 static int armature_click_extrude_exec(bContext *C, wmOperator *UNUSED(op))
112 {
113         View3D *v3d;
114         bArmature *arm;
115         EditBone *ebone, *newbone, *flipbone;
116         float mat[3][3], imat[3][3];
117         const float *curs;
118         int a, to_root = 0;
119         Object *obedit;
120         Scene *scene;
121
122         scene = CTX_data_scene(C);
123         v3d = CTX_wm_view3d(C);
124         obedit = CTX_data_edit_object(C);
125         arm = obedit->data;
126         
127         /* find the active or selected bone */
128         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
129                 if (EBONE_VISIBLE(arm, ebone)) {
130                         if (ebone->flag & BONE_TIPSEL || arm->act_edbone == ebone)
131                                 break;
132                 }
133         }
134         
135         if (ebone == NULL) {
136                 for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
137                         if (EBONE_VISIBLE(arm, ebone)) {
138                                 if (ebone->flag & BONE_ROOTSEL || arm->act_edbone == ebone)
139                                         break;
140                         }
141                 }
142                 if (ebone == NULL) 
143                         return OPERATOR_CANCELLED;
144                 
145                 to_root = 1;
146         }
147         
148         ED_armature_deselect_all(obedit);
149         
150         /* we re-use code for mirror editing... */
151         flipbone = NULL;
152         if (arm->flag & ARM_MIRROR_EDIT)
153                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
154
155         for (a = 0; a < 2; a++) {
156                 if (a == 1) {
157                         if (flipbone == NULL)
158                                 break;
159                         else {
160                                 SWAP(EditBone *, flipbone, ebone);
161                         }
162                 }
163                 
164                 newbone = ED_armature_edit_bone_add(arm, ebone->name);
165                 arm->act_edbone = newbone;
166                 
167                 if (to_root) {
168                         copy_v3_v3(newbone->head, ebone->head);
169                         newbone->rad_head = ebone->rad_tail;
170                         newbone->parent = ebone->parent;
171                 }
172                 else {
173                         copy_v3_v3(newbone->head, ebone->tail);
174                         newbone->rad_head = ebone->rad_tail;
175                         newbone->parent = ebone;
176                         newbone->flag |= BONE_CONNECTED;
177                 }
178                 
179                 curs = ED_view3d_cursor3d_get(scene, v3d);
180                 copy_v3_v3(newbone->tail, curs);
181                 sub_v3_v3v3(newbone->tail, newbone->tail, obedit->obmat[3]);
182                 
183                 if (a == 1)
184                         newbone->tail[0] = -newbone->tail[0];
185                 
186                 copy_m3_m4(mat, obedit->obmat);
187                 invert_m3_m3(imat, mat);
188                 mul_m3_v3(imat, newbone->tail);
189                 
190                 newbone->length = len_v3v3(newbone->head, newbone->tail);
191                 newbone->rad_tail = newbone->length * 0.05f;
192                 newbone->dist = newbone->length * 0.25f;
193                 
194         }
195         
196         ED_armature_sync_selection(arm->edbo);
197
198         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
199         
200         return OPERATOR_FINISHED;
201 }
202
203 static int armature_click_extrude_invoke(bContext *C, wmOperator *op, const wmEvent *event)
204 {
205         /* TODO most of this code is copied from set3dcursor_invoke,
206          * it would be better to reuse code in set3dcursor_invoke */
207
208         /* temporarily change 3d cursor position */
209         Scene *scene;
210         ARegion *ar;
211         View3D *v3d;
212         float *fp, tvec[3], oldcurs[3], mval_f[2];
213         int retv;
214
215         scene = CTX_data_scene(C);
216         ar = CTX_wm_region(C);
217         v3d = CTX_wm_view3d(C);
218         
219         fp = ED_view3d_cursor3d_get(scene, v3d);
220         
221         copy_v3_v3(oldcurs, fp);
222
223         VECCOPY2D(mval_f, event->mval);
224         ED_view3d_win_to_3d(ar, fp, mval_f, tvec);
225         copy_v3_v3(fp, tvec);
226
227         /* extrude to the where new cursor is and store the operation result */
228         retv = armature_click_extrude_exec(C, op);
229
230         /* restore previous 3d cursor position */
231         copy_v3_v3(fp, oldcurs);
232
233         return retv;
234 }
235
236 void ARMATURE_OT_click_extrude(wmOperatorType *ot)
237 {
238         /* identifiers */
239         ot->name = "Click-Extrude";
240         ot->idname = "ARMATURE_OT_click_extrude";
241         ot->description = "Create a new bone going from the last selected joint to the mouse position";
242         
243         /* api callbacks */
244         ot->invoke = armature_click_extrude_invoke;
245         ot->exec = armature_click_extrude_exec;
246         ot->poll = ED_operator_editarmature;
247         
248         /* flags */
249         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
250
251         /* props */
252 }
253
254 /* adds an EditBone between the nominated locations (should be in the right space) */
255 EditBone *add_points_bone(Object *obedit, float head[3], float tail[3])
256 {
257         EditBone *ebo;
258         
259         ebo = ED_armature_edit_bone_add(obedit->data, "Bone");
260         
261         copy_v3_v3(ebo->head, head);
262         copy_v3_v3(ebo->tail, tail);
263         
264         return ebo;
265 }
266
267
268 static EditBone *get_named_editbone(ListBase *edbo, const char *name)
269 {
270         EditBone  *eBone;
271
272         if (name) {
273                 for (eBone = edbo->first; eBone; eBone = eBone->next) {
274                         if (STREQ(name, eBone->name))
275                                 return eBone;
276                 }
277         }
278
279         return NULL;
280 }
281
282 /* Call this before doing any duplications
283  * */
284 void preEditBoneDuplicate(ListBase *editbones)
285 {
286         /* clear temp */
287         ED_armature_ebone_listbase_temp_clear(editbones);
288 }
289
290 /*
291  * Note: When duplicating cross objects, editbones here is the list of bones
292  * from the SOURCE object but ob is the DESTINATION object
293  * */
294 void updateDuplicateSubtargetObjects(EditBone *dupBone, ListBase *editbones, Object *src_ob, Object *dst_ob)
295 {
296         /* If an edit bone has been duplicated, lets
297          * update it's constraints if the subtarget
298          * they point to has also been duplicated
299          */
300         EditBone     *oldtarget, *newtarget;
301         bPoseChannel *pchan;
302         bConstraint  *curcon;
303         ListBase     *conlist;
304         
305         if ((pchan = BKE_pose_channel_verify(dst_ob->pose, dupBone->name))) {
306                 if ((conlist = &pchan->constraints)) {
307                         for (curcon = conlist->first; curcon; curcon = curcon->next) {
308                                 /* does this constraint have a subtarget in
309                                  * this armature?
310                                  */
311                                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(curcon);
312                                 ListBase targets = {NULL, NULL};
313                                 bConstraintTarget *ct;
314                                 
315                                 if (cti && cti->get_constraint_targets) {
316                                         cti->get_constraint_targets(curcon, &targets);
317                                         
318                                         for (ct = targets.first; ct; ct = ct->next) {
319                                                 if ((ct->tar == src_ob) && (ct->subtarget[0])) {
320                                                         ct->tar = dst_ob; /* update target */ 
321                                                         oldtarget = get_named_editbone(editbones, ct->subtarget);
322                                                         if (oldtarget) {
323                                                                 /* was the subtarget bone duplicated too? If
324                                                                  * so, update the constraint to point at the 
325                                                                  * duplicate of the old subtarget.
326                                                                  */
327                                                                 if (oldtarget->temp.ebone) {
328                                                                         newtarget = oldtarget->temp.ebone;
329                                                                         BLI_strncpy(ct->subtarget, newtarget->name, sizeof(ct->subtarget));
330                                                                 }
331                                                         }
332                                                 }
333                                         }
334                                         
335                                         if (cti->flush_constraint_targets)
336                                                 cti->flush_constraint_targets(curcon, &targets, 0);
337                                 }
338                         }
339                 }
340         }
341 }
342
343 void updateDuplicateSubtarget(EditBone *dupBone, ListBase *editbones, Object *ob)
344 {
345         updateDuplicateSubtargetObjects(dupBone, editbones, ob, ob);
346 }
347
348
349 EditBone *duplicateEditBoneObjects(EditBone *curBone, const char *name, ListBase *editbones,
350                                    Object *src_ob, Object *dst_ob)
351 {
352         EditBone *eBone = MEM_mallocN(sizeof(EditBone), "addup_editbone");
353         
354         /*      Copy data from old bone to new bone */
355         memcpy(eBone, curBone, sizeof(EditBone));
356         
357         curBone->temp.ebone = eBone;
358         eBone->temp.ebone = curBone;
359         
360         if (name != NULL) {
361                 BLI_strncpy(eBone->name, name, sizeof(eBone->name));
362         }
363
364         unique_editbone_name(editbones, eBone->name, NULL);
365         BLI_addtail(editbones, eBone);
366         
367         /* copy the ID property */
368         if (curBone->prop)
369                 eBone->prop = IDP_CopyProperty(curBone->prop);
370
371         /* Lets duplicate the list of constraints that the
372          * current bone has.
373          */
374         if (src_ob->pose) {
375                 bPoseChannel *chanold, *channew;
376                 
377                 chanold = BKE_pose_channel_verify(src_ob->pose, curBone->name);
378                 if (chanold) {
379                         /* WARNING: this creates a new posechannel, but there will not be an attached bone
380                          *              yet as the new bones created here are still 'EditBones' not 'Bones'.
381                          */
382                         channew = BKE_pose_channel_verify(dst_ob->pose, eBone->name);
383
384                         if (channew) {
385                                 BKE_pose_channel_copy_data(channew, chanold);
386                         }
387                 }
388         }
389         
390         return eBone;
391 }
392
393 EditBone *duplicateEditBone(EditBone *curBone, const char *name, ListBase *editbones, Object *ob)
394 {
395         return duplicateEditBoneObjects(curBone, name, editbones, ob, ob);
396 }
397
398 static int armature_duplicate_selected_exec(bContext *C, wmOperator *UNUSED(op))
399 {
400         bArmature *arm;
401         EditBone *ebone_iter;
402         EditBone *ebone_first_dupe = NULL;  /* The beginning of the duplicated bones in the edbo list */
403
404         Object *obedit = CTX_data_edit_object(C);
405         arm = obedit->data;
406
407         /* cancel if nothing selected */
408         if (CTX_DATA_COUNT(C, selected_bones) == 0)
409                 return OPERATOR_CANCELLED;
410         
411         ED_armature_sync_selection(arm->edbo); // XXX why is this needed?
412
413         preEditBoneDuplicate(arm->edbo);
414
415         /* Select mirrored bones */
416         if (arm->flag & ARM_MIRROR_EDIT) {
417                 for (ebone_iter = arm->edbo->first; ebone_iter; ebone_iter = ebone_iter->next) {
418                         if (EBONE_VISIBLE(arm, ebone_iter) &&
419                             (ebone_iter->flag & BONE_SELECTED))
420                         {
421                                 EditBone *ebone;
422
423                                 ebone = ED_armature_bone_get_mirrored(arm->edbo, ebone_iter);
424                                 if (ebone) {
425                                         ebone->flag |= BONE_SELECTED;
426                                 }
427                         }
428                 }
429         }
430
431         
432         /* Find the selected bones and duplicate them as needed */
433         for (ebone_iter = arm->edbo->first; ebone_iter && ebone_iter != ebone_first_dupe; ebone_iter = ebone_iter->next) {
434                 if (EBONE_VISIBLE(arm, ebone_iter) &&
435                     (ebone_iter->flag & BONE_SELECTED))
436                 {
437                         EditBone *ebone;
438
439                         ebone = duplicateEditBone(ebone_iter, ebone_iter->name, arm->edbo, obedit);
440
441                         if (!ebone_first_dupe) {
442                                 ebone_first_dupe = ebone;
443                         }
444                 }
445         }
446
447         /* Run though the list and fix the pointers */
448         for (ebone_iter = arm->edbo->first; ebone_iter && ebone_iter != ebone_first_dupe; ebone_iter = ebone_iter->next) {
449                 if (EBONE_VISIBLE(arm, ebone_iter) &&
450                     (ebone_iter->flag & BONE_SELECTED))
451                 {
452                         EditBone *ebone = ebone_iter->temp.ebone;
453
454                         if (!ebone_iter->parent) {
455                                 /* If this bone has no parent,
456                                  * Set the duplicate->parent to NULL
457                                  */
458                                 ebone->parent = NULL;
459                         }
460                         else if (ebone_iter->parent->temp.ebone) {
461                                 /* If this bone has a parent that was duplicated,
462                                  * Set the duplicate->parent to the curBone->parent->temp
463                                  */
464                                 ebone->parent = ebone_iter->parent->temp.ebone;
465                         }
466                         else {
467                                 /* If this bone has a parent that IS not selected,
468                                  * Set the duplicate->parent to the curBone->parent
469                                  */
470                                 ebone->parent = (EditBone *) ebone_iter->parent;
471                                 ebone->flag &= ~BONE_CONNECTED;
472                         }
473
474                         /* Lets try to fix any constraint subtargets that might
475                          * have been duplicated
476                          */
477                         updateDuplicateSubtarget(ebone, arm->edbo, obedit);
478                 }
479         }
480         
481         /* correct the active bone */
482         if (arm->act_edbone && arm->act_edbone->temp.ebone) {
483                 arm->act_edbone = arm->act_edbone->temp.ebone;
484         }
485
486         /* Deselect the old bones and select the new ones */
487         for (ebone_iter = arm->edbo->first; ebone_iter && ebone_iter != ebone_first_dupe; ebone_iter = ebone_iter->next) {
488                 if (EBONE_VISIBLE(arm, ebone_iter)) {
489                         ebone_iter->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
490                 }
491         }
492
493         ED_armature_validate_active(arm);
494
495         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
496         
497         return OPERATOR_FINISHED;
498 }
499
500
501 void ARMATURE_OT_duplicate(wmOperatorType *ot)
502 {
503         /* identifiers */
504         ot->name = "Duplicate Selected Bone(s)";
505         ot->idname = "ARMATURE_OT_duplicate";
506         ot->description = "Make copies of the selected bones within the same armature";
507         
508         /* api callbacks */
509         ot->exec = armature_duplicate_selected_exec;
510         ot->poll = ED_operator_editarmature;
511         
512         /* flags */
513         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
514 }
515
516 /**
517  * near duplicate of #armature_duplicate_selected_exec,
518  * except for parenting part (keep in sync)
519  */
520 static int armature_symmetrize_exec(bContext *C, wmOperator *op)
521 {
522         bArmature *arm;
523         EditBone *ebone_iter;
524         EditBone *ebone_first_dupe = NULL;  /* The beginning of the duplicated mirrored bones in the edbo list */
525
526         Object *obedit = CTX_data_edit_object(C);
527         const int direction = RNA_enum_get(op->ptr, "direction");
528         const int axis = 0;
529
530         arm = obedit->data;
531
532         /* cancel if nothing selected */
533         if (CTX_DATA_COUNT(C, selected_bones) == 0)
534                 return OPERATOR_CANCELLED;
535
536         ED_armature_sync_selection(arm->edbo); // XXX why is this needed?
537
538         preEditBoneDuplicate(arm->edbo);
539
540         /* Select mirrored bones */
541         for (ebone_iter = arm->edbo->first; ebone_iter; ebone_iter = ebone_iter->next) {
542                 if (EBONE_VISIBLE(arm, ebone_iter) &&
543                     (ebone_iter->flag & BONE_SELECTED))
544                 {
545                         char name_flip[MAX_VGROUP_NAME];
546
547                         BKE_deform_flip_side_name(name_flip, ebone_iter->name, false);
548
549                         if (STREQ(name_flip, ebone_iter->name)) {
550                                 /* if the name matches, we don't have the potential to be mirrored, just skip */
551                                 ebone_iter->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
552                         }
553                         else {
554                                 EditBone *ebone = ED_armature_bone_find_name(arm->edbo, name_flip);
555
556                                 if (ebone) {
557                                         if ((ebone->flag & BONE_SELECTED) == 0) {
558                                                 /* simple case, we're selected, the other bone isn't! */
559                                                 ebone_iter->temp.ebone = ebone;
560                                         }
561                                         else {
562                                                 /* complicated - choose which direction to copy */
563                                                 float axis_delta;
564
565                                                 axis_delta = ebone->head[axis] - ebone_iter->head[axis];
566                                                 if (axis_delta == 0.0f) {
567                                                         axis_delta = ebone->tail[axis] - ebone_iter->tail[axis];
568                                                 }
569
570                                                 if (axis_delta == 0.0f) {
571                                                         /* both mirrored bones exist and point to eachother and overlap exactly.
572                                                          *
573                                                          * in this case theres no well defined solution, so de-select both and skip.
574                                                          */
575                                                         ebone->flag      &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
576                                                         ebone_iter->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
577                                                 }
578                                                 else {
579                                                         EditBone *ebone_src, *ebone_dst;
580                                                         if (((axis_delta < 0.0f) ? -1 : 1) == direction) {
581                                                                 ebone_src = ebone;
582                                                                 ebone_dst = ebone_iter;
583                                                         }
584                                                         else {
585                                                                 ebone_src = ebone_iter;
586                                                                 ebone_dst = ebone;
587                                                         }
588
589                                                         ebone_src->temp.ebone = ebone_dst;
590                                                         ebone_dst->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
591                                                 }
592                                         }
593                                 }
594                         }
595                 }
596         }
597
598         /*      Find the selected bones and duplicate them as needed, with mirrored name */
599         for (ebone_iter = arm->edbo->first; ebone_iter && ebone_iter != ebone_first_dupe; ebone_iter = ebone_iter->next) {
600                 if (EBONE_VISIBLE(arm, ebone_iter) &&
601                     (ebone_iter->flag & BONE_SELECTED) &&
602                     /* will be set if the mirror bone already exists (no need to make a new one) */
603                     (ebone_iter->temp.ebone == NULL))
604                 {
605                         char name_flip[MAX_VGROUP_NAME];
606
607                         BKE_deform_flip_side_name(name_flip, ebone_iter->name, false);
608
609                         /* bones must have a side-suffix */
610                         if (!STREQ(name_flip, ebone_iter->name)) {
611                                 EditBone *ebone;
612
613                                 ebone = duplicateEditBone(ebone_iter, name_flip, arm->edbo, obedit);
614
615                                 if (!ebone_first_dupe) {
616                                         ebone_first_dupe = ebone;
617                                 }
618                         }
619                 }
620         }
621
622         /*      Run though the list and fix the pointers */
623         for (ebone_iter = arm->edbo->first; ebone_iter && ebone_iter != ebone_first_dupe; ebone_iter = ebone_iter->next) {
624                 if (ebone_iter->temp.ebone) {
625                         /* copy all flags except for ... */
626                         const int flag_copy = ((int)~0) & ~(BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
627
628                         EditBone *ebone = ebone_iter->temp.ebone;
629
630                         /* copy flags incase bone is pre-existing data */
631                         ebone->flag = (ebone->flag & ~flag_copy) | (ebone_iter->flag & flag_copy);
632
633                         if (ebone_iter->parent == NULL) {
634                                 /* If this bone has no parent,
635                                  * Set the duplicate->parent to NULL
636                                  */
637                                 ebone->parent = NULL;
638                                 ebone->flag &= ~BONE_CONNECTED;
639                         }
640                         else {
641                                 /* the parent may have been duplicated, if not lookup the mirror parent */
642                                 EditBone *ebone_parent =
643                                         (ebone_iter->parent->temp.ebone ?
644                                          ebone_iter->parent->temp.ebone : ED_armature_bone_get_mirrored(arm->edbo, ebone_iter->parent));
645
646                                 if (ebone_parent == NULL) {
647                                         /* If the mirror lookup failed, (but the current bone has a parent)
648                                          * then we can assume the parent has no L/R but is a center bone.
649                                          * So just use the same parent for both.
650                                          */
651                                         ebone_parent = ebone_iter->parent;
652                                         ebone->flag &= ~BONE_CONNECTED;
653                                 }
654
655                                 ebone->parent = ebone_parent;
656                         }
657
658                         /* Lets try to fix any constraint subtargets that might
659                          * have been duplicated
660                          */
661                         updateDuplicateSubtarget(ebone, arm->edbo, obedit);
662                 }
663         }
664
665         transform_armature_mirror_update(obedit);
666
667         /* Selected bones now have their 'temp' pointer set,
668          * so we don't need this anymore */
669
670         /* Deselect the old bones and select the new ones */
671         for (ebone_iter = arm->edbo->first; ebone_iter && ebone_iter != ebone_first_dupe; ebone_iter = ebone_iter->next) {
672                 if (EBONE_VISIBLE(arm, ebone_iter)) {
673                         ebone_iter->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
674                 }
675         }
676
677         /* New bones will be selected, but some of the bones may already exist */
678         for (ebone_iter = arm->edbo->first; ebone_iter && ebone_iter != ebone_first_dupe; ebone_iter = ebone_iter->next) {
679                 EditBone *ebone = ebone_iter->temp.ebone;
680                 if (ebone && EBONE_SELECTABLE(arm, ebone)) {
681                         ED_armature_ebone_select_set(ebone, true);
682                 }
683         }
684
685         /* correct the active bone */
686         if (arm->act_edbone && arm->act_edbone->temp.ebone) {
687                 arm->act_edbone = arm->act_edbone->temp.ebone;
688         }
689
690         ED_armature_validate_active(arm);
691
692         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
693
694         return OPERATOR_FINISHED;
695 }
696
697 /* following conventions from #MESH_OT_symmetrize */
698 void ARMATURE_OT_symmetrize(wmOperatorType *ot)
699 {
700         /* subset of 'symmetrize_direction_items' */
701         static EnumPropertyItem arm_symmetrize_direction_items[] = {
702                 {-1, "NEGATIVE_X", 0, "-X to +X", ""},
703                 {+1, "POSITIVE_X", 0, "+X to -X", ""},
704                 {0, NULL, 0, NULL, NULL}
705         };
706
707         /* identifiers */
708         ot->name = "Symmetrize";
709         ot->idname = "ARMATURE_OT_symmetrize";
710         ot->description = "Enforce symmetry, make copies of the selection or use existing";
711
712         /* api callbacks */
713         ot->exec = armature_symmetrize_exec;
714         ot->poll = ED_operator_editarmature;
715
716         /* flags */
717         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
718
719         ot->prop = RNA_def_enum(
720                 ot->srna, "direction", arm_symmetrize_direction_items, -1,
721                 "Direction", "Which sides to copy from and to (when both are selected)");
722 }
723
724 /* ------------------------------------------ */
725
726 /* previously extrude_armature */
727 /* context; editmode armature */
728 /* if forked && mirror-edit: makes two bones with flipped names */
729 static int armature_extrude_exec(bContext *C, wmOperator *op)
730 {
731         Object *obedit;
732         bArmature *arm;
733         EditBone *newbone, *ebone, *flipbone, *first = NULL;
734         int a, totbone = 0, do_extrude;
735         bool forked = RNA_boolean_get(op->ptr, "forked");
736
737         obedit = CTX_data_edit_object(C);
738         arm = obedit->data;
739
740         /* since we allow root extrude too, we have to make sure selection is OK */
741         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
742                 if (EBONE_VISIBLE(arm, ebone)) {
743                         if (ebone->flag & BONE_ROOTSEL) {
744                                 if (ebone->parent && (ebone->flag & BONE_CONNECTED)) {
745                                         if (ebone->parent->flag & BONE_TIPSEL)
746                                                 ebone->flag &= ~BONE_ROOTSEL;
747                                 }
748                         }
749                 }
750         }
751         
752         /* Duplicate the necessary bones */
753         for (ebone = arm->edbo->first; ((ebone) && (ebone != first)); ebone = ebone->next) {
754                 if (EBONE_VISIBLE(arm, ebone)) {
755                         /* we extrude per definition the tip */
756                         do_extrude = false;
757                         if (ebone->flag & (BONE_TIPSEL | BONE_SELECTED)) {
758                                 do_extrude = true;
759                         }
760                         else if (ebone->flag & BONE_ROOTSEL) {
761                                 /* but, a bone with parent deselected we do the root... */
762                                 if (ebone->parent && (ebone->parent->flag & BONE_TIPSEL)) {
763                                         /* pass */
764                                 }
765                                 else {
766                                         do_extrude = 2;
767                                 }
768                         }
769                         
770                         if (do_extrude) {
771                                 /* we re-use code for mirror editing... */
772                                 flipbone = NULL;
773                                 if (arm->flag & ARM_MIRROR_EDIT) {
774                                         flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
775                                         if (flipbone) {
776                                                 forked = 0;  // we extrude 2 different bones
777                                                 if (flipbone->flag & (BONE_TIPSEL | BONE_ROOTSEL | BONE_SELECTED))
778                                                         /* don't want this bone to be selected... */
779                                                         flipbone->flag &= ~(BONE_TIPSEL | BONE_SELECTED | BONE_ROOTSEL);
780                                         }
781                                         if ((flipbone == NULL) && (forked))
782                                                 flipbone = ebone;
783                                 }
784                                 
785                                 for (a = 0; a < 2; a++) {
786                                         if (a == 1) {
787                                                 if (flipbone == NULL)
788                                                         break;
789                                                 else {
790                                                         SWAP(EditBone *, flipbone, ebone);
791                                                 }
792                                         }
793                                         
794                                         totbone++;
795                                         newbone = MEM_callocN(sizeof(EditBone), "extrudebone");
796                                         
797                                         if (do_extrude == true) {
798                                                 copy_v3_v3(newbone->head, ebone->tail);
799                                                 copy_v3_v3(newbone->tail, newbone->head);
800                                                 newbone->parent = ebone;
801                                                 
802                                                 newbone->flag = ebone->flag & (BONE_TIPSEL | BONE_RELATIVE_PARENTING);  // copies it, in case mirrored bone
803                                                 
804                                                 if (newbone->parent) newbone->flag |= BONE_CONNECTED;
805                                         }
806                                         else {
807                                                 copy_v3_v3(newbone->head, ebone->head);
808                                                 copy_v3_v3(newbone->tail, ebone->head);
809                                                 newbone->parent = ebone->parent;
810                                                 
811                                                 newbone->flag = BONE_TIPSEL;
812                                                 
813                                                 if (newbone->parent && (ebone->flag & BONE_CONNECTED)) {
814                                                         newbone->flag |= BONE_CONNECTED;
815                                                 }
816                                         }
817                                         
818                                         newbone->weight = ebone->weight;
819                                         newbone->dist = ebone->dist;
820                                         newbone->xwidth = ebone->xwidth;
821                                         newbone->zwidth = ebone->zwidth;
822                                         newbone->ease1 = ebone->ease1;
823                                         newbone->ease2 = ebone->ease2;
824                                         newbone->rad_head = ebone->rad_tail; // don't copy entire bone...
825                                         newbone->rad_tail = ebone->rad_tail;
826                                         newbone->segments = 1;
827                                         newbone->layer = ebone->layer;
828                                         
829                                         BLI_strncpy(newbone->name, ebone->name, sizeof(newbone->name));
830                                         
831                                         if (flipbone && forked) {   // only set if mirror edit
832                                                 if (strlen(newbone->name) < (MAXBONENAME - 2)) {
833                                                         if (a == 0) strcat(newbone->name, "_L");
834                                                         else strcat(newbone->name, "_R");
835                                                 }
836                                         }
837                                         unique_editbone_name(arm->edbo, newbone->name, NULL);
838                                         
839                                         /* Add the new bone to the list */
840                                         BLI_addtail(arm->edbo, newbone);
841                                         if (!first)
842                                                 first = newbone;
843                                         
844                                         /* restore ebone if we were flipping */
845                                         if (a == 1 && flipbone)
846                                                 SWAP(EditBone *, flipbone, ebone);
847                                 }
848                         }
849                         
850                         /* Deselect the old bone */
851                         ebone->flag &= ~(BONE_TIPSEL | BONE_SELECTED | BONE_ROOTSEL);
852                 }
853         }
854         /* if only one bone, make this one active */
855         if (totbone == 1 && first) arm->act_edbone = first;
856
857         if (totbone == 0) return OPERATOR_CANCELLED;
858
859         /* Transform the endpoints */
860         ED_armature_sync_selection(arm->edbo);
861
862         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
863
864         return OPERATOR_FINISHED;
865 }
866
867 void ARMATURE_OT_extrude(wmOperatorType *ot)
868 {
869         /* identifiers */
870         ot->name = "Extrude";
871         ot->idname = "ARMATURE_OT_extrude";
872         ot->description = "Create new bones from the selected joints";
873         
874         /* api callbacks */
875         ot->exec = armature_extrude_exec;
876         ot->poll = ED_operator_editarmature;
877         
878         /* flags */
879         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
880         
881         /* props */
882         RNA_def_boolean(ot->srna, "forked", 0, "Forked", "");
883 }
884
885 /* ********************** Bone Add *************************************/
886
887 /*op makes a new bone and returns it with its tip selected */
888
889 static int armature_bone_primitive_add_exec(bContext *C, wmOperator *op) 
890 {
891         RegionView3D *rv3d = CTX_wm_region_view3d(C);
892         Object *obedit = CTX_data_edit_object(C);
893         EditBone *bone;
894         float obmat[3][3], curs[3], viewmat[3][3], totmat[3][3], imat[3][3];
895         char name[MAXBONENAME];
896         
897         RNA_string_get(op->ptr, "name", name);
898         
899         copy_v3_v3(curs, ED_view3d_cursor3d_get(CTX_data_scene(C), CTX_wm_view3d(C)));
900
901         /* Get inverse point for head and orientation for tail */
902         invert_m4_m4(obedit->imat, obedit->obmat);
903         mul_m4_v3(obedit->imat, curs);
904
905         if (rv3d && (U.flag & USER_ADD_VIEWALIGNED))
906                 copy_m3_m4(obmat, rv3d->viewmat);
907         else unit_m3(obmat);
908         
909         copy_m3_m4(viewmat, obedit->obmat);
910         mul_m3_m3m3(totmat, obmat, viewmat);
911         invert_m3_m3(imat, totmat);
912         
913         ED_armature_deselect_all(obedit);
914         
915         /*      Create a bone   */
916         bone = ED_armature_edit_bone_add(obedit->data, name);
917
918         copy_v3_v3(bone->head, curs);
919         
920         if (rv3d && (U.flag & USER_ADD_VIEWALIGNED))
921                 add_v3_v3v3(bone->tail, bone->head, imat[1]);   // bone with unit length 1
922         else
923                 add_v3_v3v3(bone->tail, bone->head, imat[2]);   // bone with unit length 1, pointing up Z
924
925         /* note, notifier might evolve */
926         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
927
928         return OPERATOR_FINISHED;
929 }
930
931 void ARMATURE_OT_bone_primitive_add(wmOperatorType *ot)
932 {
933         /* identifiers */
934         ot->name = "Add Bone";
935         ot->idname = "ARMATURE_OT_bone_primitive_add";
936         ot->description = "Add a new bone located at the 3D-Cursor";
937         
938         /* api callbacks */
939         ot->exec = armature_bone_primitive_add_exec;
940         ot->poll = ED_operator_editarmature;
941         
942         /* flags */
943         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
944         
945         RNA_def_string(ot->srna, "name", "Bone", MAXBONENAME, "Name", "Name of the newly created bone");
946         
947 }
948
949 /* ********************** Subdivide *******************************/
950
951 /* Subdivide Operators:
952  * This group of operators all use the same 'exec' callback, but they are called
953  * through several different operators - a combined menu (which just calls the exec in the 
954  * appropriate ways), and two separate ones.
955  */
956
957 static int armature_subdivide_exec(bContext *C, wmOperator *op)
958 {
959         Object *obedit = CTX_data_edit_object(C);
960         bArmature *arm = obedit->data;
961         EditBone *newbone, *tbone;
962         int cuts, i;
963         
964         /* there may not be a number_cuts property defined (for 'simple' subdivide) */
965         cuts = RNA_int_get(op->ptr, "number_cuts");
966         
967         /* loop over all editable bones */
968         // XXX the old code did this in reverse order though!
969         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones)
970         {
971                 for (i = cuts + 1; i > 1; i--) {
972                         /* compute cut ratio first */
973                         float cutratio = 1.0f / (float)i;
974                         float cutratioI = 1.0f - cutratio;
975                         
976                         float val1[3];
977                         float val2[3];
978                         float val3[3];
979                         
980                         newbone = MEM_mallocN(sizeof(EditBone), "ebone subdiv");
981                         *newbone = *ebone;
982                         BLI_addtail(arm->edbo, newbone);
983                         
984                         /* calculate location of newbone->head */
985                         copy_v3_v3(val1, ebone->head);
986                         copy_v3_v3(val2, ebone->tail);
987                         copy_v3_v3(val3, newbone->head);
988                         
989                         val3[0] = val1[0] * cutratio + val2[0] * cutratioI;
990                         val3[1] = val1[1] * cutratio + val2[1] * cutratioI;
991                         val3[2] = val1[2] * cutratio + val2[2] * cutratioI;
992                         
993                         copy_v3_v3(newbone->head, val3);
994                         copy_v3_v3(newbone->tail, ebone->tail);
995                         copy_v3_v3(ebone->tail, newbone->head);
996                         
997                         newbone->rad_head = ((ebone->rad_head * cutratio) + (ebone->rad_tail * cutratioI));
998                         ebone->rad_tail = newbone->rad_head;
999                         
1000                         newbone->flag |= BONE_CONNECTED;
1001
1002                         newbone->prop = NULL;
1003
1004                         unique_editbone_name(arm->edbo, newbone->name, NULL);
1005                         
1006                         /* correct parent bones */
1007                         for (tbone = arm->edbo->first; tbone; tbone = tbone->next) {
1008                                 if (tbone->parent == ebone)
1009                                         tbone->parent = newbone;
1010                         }
1011                         newbone->parent = ebone;
1012                 }
1013         }
1014         CTX_DATA_END;
1015         
1016         /* note, notifier might evolve */
1017         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
1018         
1019         return OPERATOR_FINISHED;
1020 }
1021
1022 void ARMATURE_OT_subdivide(wmOperatorType *ot)
1023 {
1024         PropertyRNA *prop;
1025
1026         /* identifiers */
1027         ot->name = "Subdivide Multi";
1028         ot->idname = "ARMATURE_OT_subdivide";
1029         ot->description = "Break selected bones into chains of smaller bones";
1030         
1031         /* api callbacks */
1032         ot->exec = armature_subdivide_exec;
1033         ot->poll = ED_operator_editarmature;
1034         
1035         /* flags */
1036         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1037         
1038         /* Properties */
1039         prop = RNA_def_int(ot->srna, "number_cuts", 1, 1, 1000, "Number of Cuts", "", 1, 10);
1040         /* avoid re-using last var because it can cause _very_ high poly meshes and annoy users (or worse crash) */
1041         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1042 }