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