code cleanup: favor braces when blocks have mixed brace use.
[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
47 #include "RNA_access.h"
48 #include "RNA_define.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "ED_armature.h"
54 #include "ED_screen.h"
55 #include "ED_view3d.h"
56
57 #include "armature_intern.h"
58
59 /* *************** Adding stuff in editmode *************** */
60
61 /* default bone add, returns it selected, but without tail set */
62 /* XXX should be used everywhere, now it mallocs bones still locally in functions */
63 EditBone *ED_armature_edit_bone_add(bArmature *arm, const char *name)
64 {
65         EditBone *bone = MEM_callocN(sizeof(EditBone), "eBone");
66         
67         BLI_strncpy(bone->name, name, sizeof(bone->name));
68         unique_editbone_name(arm->edbo, bone->name, NULL);
69         
70         BLI_addtail(arm->edbo, bone);
71         
72         bone->flag |= BONE_TIPSEL;
73         bone->weight = 1.0f;
74         bone->dist = 0.25f;
75         bone->xwidth = 0.1f;
76         bone->zwidth = 0.1f;
77         bone->ease1 = 1.0f;
78         bone->ease2 = 1.0f;
79         bone->rad_head = 0.10f;
80         bone->rad_tail = 0.05f;
81         bone->segments = 1;
82         bone->layer = arm->layer;
83         
84         return bone;
85 }
86
87 /* v3d and rv3d are allowed to be NULL */
88 void add_primitive_bone(Scene *scene, View3D *v3d, RegionView3D *rv3d)
89 {
90         Object *obedit = scene->obedit; // XXX get from context
91         bArmature *arm = obedit->data;
92         float obmat[3][3], curs[3], viewmat[3][3], totmat[3][3], imat[3][3];
93         EditBone    *bone;
94
95         /* Get inverse point for head and orientation for tail */
96         invert_m4_m4(obedit->imat, obedit->obmat);
97         mul_v3_m4v3(curs, obedit->imat, give_cursor(scene, v3d));
98
99         if (rv3d && (U.flag & USER_ADD_VIEWALIGNED))
100                 copy_m3_m4(obmat, rv3d->viewmat);
101         else unit_m3(obmat);
102         
103         copy_m3_m4(viewmat, obedit->obmat);
104         mul_m3_m3m3(totmat, obmat, viewmat);
105         invert_m3_m3(imat, totmat);
106         
107         ED_armature_deselect_all(obedit, 0);
108         
109         /* Create a bone */
110         bone = ED_armature_edit_bone_add(arm, "Bone");
111
112         arm->act_edbone = bone;
113
114         copy_v3_v3(bone->head, curs);
115         
116         if (rv3d && (U.flag & USER_ADD_VIEWALIGNED))
117                 add_v3_v3v3(bone->tail, bone->head, imat[1]);   // bone with unit length 1
118         else
119                 add_v3_v3v3(bone->tail, bone->head, imat[2]);   // bone with unit length 1, pointing up Z
120 }
121
122
123 /* previously addvert_armature */
124 /* the ctrl-click method */
125 static int armature_click_extrude_exec(bContext *C, wmOperator *UNUSED(op))
126 {
127         View3D *v3d;
128         bArmature *arm;
129         EditBone *ebone, *newbone, *flipbone;
130         float mat[3][3], imat[3][3];
131         const float *curs;
132         int a, to_root = 0;
133         Object *obedit;
134         Scene *scene;
135
136         scene = CTX_data_scene(C);
137         v3d = CTX_wm_view3d(C);
138         obedit = CTX_data_edit_object(C);
139         arm = obedit->data;
140         
141         /* find the active or selected bone */
142         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
143                 if (EBONE_VISIBLE(arm, ebone)) {
144                         if (ebone->flag & BONE_TIPSEL || arm->act_edbone == ebone)
145                                 break;
146                 }
147         }
148         
149         if (ebone == NULL) {
150                 for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
151                         if (EBONE_VISIBLE(arm, ebone)) {
152                                 if (ebone->flag & BONE_ROOTSEL || arm->act_edbone == ebone)
153                                         break;
154                         }
155                 }
156                 if (ebone == NULL) 
157                         return OPERATOR_CANCELLED;
158                 
159                 to_root = 1;
160         }
161         
162         ED_armature_deselect_all(obedit, 0);
163         
164         /* we re-use code for mirror editing... */
165         flipbone = NULL;
166         if (arm->flag & ARM_MIRROR_EDIT)
167                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
168
169         for (a = 0; a < 2; a++) {
170                 if (a == 1) {
171                         if (flipbone == NULL)
172                                 break;
173                         else {
174                                 SWAP(EditBone *, flipbone, ebone);
175                         }
176                 }
177                 
178                 newbone = ED_armature_edit_bone_add(arm, ebone->name);
179                 arm->act_edbone = newbone;
180                 
181                 if (to_root) {
182                         copy_v3_v3(newbone->head, ebone->head);
183                         newbone->rad_head = ebone->rad_tail;
184                         newbone->parent = ebone->parent;
185                 }
186                 else {
187                         copy_v3_v3(newbone->head, ebone->tail);
188                         newbone->rad_head = ebone->rad_tail;
189                         newbone->parent = ebone;
190                         newbone->flag |= BONE_CONNECTED;
191                 }
192                 
193                 curs = give_cursor(scene, v3d);
194                 copy_v3_v3(newbone->tail, curs);
195                 sub_v3_v3v3(newbone->tail, newbone->tail, obedit->obmat[3]);
196                 
197                 if (a == 1)
198                         newbone->tail[0] = -newbone->tail[0];
199                 
200                 copy_m3_m4(mat, obedit->obmat);
201                 invert_m3_m3(imat, mat);
202                 mul_m3_v3(imat, newbone->tail);
203                 
204                 newbone->length = len_v3v3(newbone->head, newbone->tail);
205                 newbone->rad_tail = newbone->length * 0.05f;
206                 newbone->dist = newbone->length * 0.25f;
207                 
208         }
209         
210         ED_armature_sync_selection(arm->edbo);
211
212         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
213         
214         return OPERATOR_FINISHED;
215 }
216
217 static int armature_click_extrude_invoke(bContext *C, wmOperator *op, wmEvent *event)
218 {
219         /* TODO most of this code is copied from set3dcursor_invoke,
220          * it would be better to reuse code in set3dcursor_invoke */
221
222         /* temporarily change 3d cursor position */
223         Scene *scene;
224         ARegion *ar;
225         View3D *v3d;
226         float *fp, tvec[3], oldcurs[3], mval_f[2];
227         int retv;
228
229         scene = CTX_data_scene(C);
230         ar = CTX_wm_region(C);
231         v3d = CTX_wm_view3d(C);
232         
233         fp = give_cursor(scene, v3d);
234         
235         copy_v3_v3(oldcurs, fp);
236
237         VECCOPY2D(mval_f, event->mval);
238         ED_view3d_win_to_3d(ar, fp, mval_f, tvec);
239         copy_v3_v3(fp, tvec);
240
241         /* extrude to the where new cursor is and store the operation result */
242         retv = armature_click_extrude_exec(C, op);
243
244         /* restore previous 3d cursor position */
245         copy_v3_v3(fp, oldcurs);
246
247         return retv;
248 }
249
250 void ARMATURE_OT_click_extrude(wmOperatorType *ot)
251 {
252         /* identifiers */
253         ot->name = "Click-Extrude";
254         ot->idname = "ARMATURE_OT_click_extrude";
255         ot->description = "Create a new bone going from the last selected joint to the mouse position";
256         
257         /* api callbacks */
258         ot->invoke = armature_click_extrude_invoke;
259         ot->exec = armature_click_extrude_exec;
260         ot->poll = ED_operator_editarmature;
261         
262         /* flags */
263         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
264
265         /* props */
266 }
267
268 /* adds an EditBone between the nominated locations (should be in the right space) */
269 EditBone *add_points_bone(Object *obedit, float head[3], float tail[3])
270 {
271         EditBone *ebo;
272         
273         ebo = ED_armature_edit_bone_add(obedit->data, "Bone");
274         
275         copy_v3_v3(ebo->head, head);
276         copy_v3_v3(ebo->tail, tail);
277         
278         return ebo;
279 }
280
281
282 static EditBone *get_named_editbone(ListBase *edbo, char *name)
283 {
284         EditBone  *eBone;
285
286         if (name) {
287                 for (eBone = edbo->first; eBone; eBone = eBone->next) {
288                         if (!strcmp(name, eBone->name))
289                                 return eBone;
290                 }
291         }
292
293         return NULL;
294 }
295
296 /* Call this before doing any duplications
297  * */
298 void preEditBoneDuplicate(ListBase *editbones)
299 {
300         EditBone *eBone;
301         
302         /* clear temp */
303         for (eBone = editbones->first; eBone; eBone = eBone->next) {
304                 eBone->temp = NULL;
305         }
306 }
307
308 /*
309  * Note: When duplicating cross objects, editbones here is the list of bones
310  * from the SOURCE object but ob is the DESTINATION object
311  * */
312 void updateDuplicateSubtargetObjects(EditBone *dupBone, ListBase *editbones, Object *src_ob, Object *dst_ob)
313 {
314         /* If an edit bone has been duplicated, lets
315          * update it's constraints if the subtarget
316          * they point to has also been duplicated
317          */
318         EditBone     *oldtarget, *newtarget;
319         bPoseChannel *pchan;
320         bConstraint  *curcon;
321         ListBase     *conlist;
322         
323         if ((pchan = BKE_pose_channel_verify(dst_ob->pose, dupBone->name))) {
324                 if ((conlist = &pchan->constraints)) {
325                         for (curcon = conlist->first; curcon; curcon = curcon->next) {
326                                 /* does this constraint have a subtarget in
327                                  * this armature?
328                                  */
329                                 bConstraintTypeInfo *cti = BKE_constraint_get_typeinfo(curcon);
330                                 ListBase targets = {NULL, NULL};
331                                 bConstraintTarget *ct;
332                                 
333                                 if (cti && cti->get_constraint_targets) {
334                                         cti->get_constraint_targets(curcon, &targets);
335                                         
336                                         for (ct = targets.first; ct; ct = ct->next) {
337                                                 if ((ct->tar == src_ob) && (ct->subtarget[0])) {
338                                                         ct->tar = dst_ob; /* update target */ 
339                                                         oldtarget = get_named_editbone(editbones, ct->subtarget);
340                                                         if (oldtarget) {
341                                                                 /* was the subtarget bone duplicated too? If
342                                                                  * so, update the constraint to point at the 
343                                                                  * duplicate of the old subtarget.
344                                                                  */
345                                                                 if (oldtarget->temp) {
346                                                                         newtarget = (EditBone *) oldtarget->temp;
347                                                                         BLI_strncpy(ct->subtarget, newtarget->name, sizeof(ct->subtarget));
348                                                                 }
349                                                         }
350                                                 }
351                                         }
352                                         
353                                         if (cti->flush_constraint_targets)
354                                                 cti->flush_constraint_targets(curcon, &targets, 0);
355                                 }
356                         }
357                 }
358         }
359 }
360
361 void updateDuplicateSubtarget(EditBone *dupBone, ListBase *editbones, Object *ob)
362 {
363         updateDuplicateSubtargetObjects(dupBone, editbones, ob, ob);
364 }
365
366
367 EditBone *duplicateEditBoneObjects(EditBone *curBone, const char *name, ListBase *editbones,
368                                    Object *src_ob, Object *dst_ob)
369 {
370         EditBone *eBone = MEM_mallocN(sizeof(EditBone), "addup_editbone");
371         
372         /*      Copy data from old bone to new bone */
373         memcpy(eBone, curBone, sizeof(EditBone));
374         
375         curBone->temp = eBone;
376         eBone->temp = curBone;
377         
378         if (name != NULL) {
379                 BLI_strncpy(eBone->name, name, sizeof(eBone->name));
380         }
381
382         unique_editbone_name(editbones, eBone->name, NULL);
383         BLI_addtail(editbones, eBone);
384         
385         /* copy the ID property */
386         if (curBone->prop)
387                 eBone->prop = IDP_CopyProperty(curBone->prop);
388
389         /* Lets duplicate the list of constraints that the
390          * current bone has.
391          */
392         if (src_ob->pose) {
393                 bPoseChannel *chanold, *channew;
394                 
395                 chanold = BKE_pose_channel_verify(src_ob->pose, curBone->name);
396                 if (chanold) {
397                         /* WARNING: this creates a new posechannel, but there will not be an attached bone
398                          *              yet as the new bones created here are still 'EditBones' not 'Bones'.
399                          */
400                         channew = BKE_pose_channel_verify(dst_ob->pose, eBone->name);
401
402                         if (channew) {
403                                 BKE_pose_channel_copy_data(channew, chanold);
404                         }
405                 }
406         }
407         
408         return eBone;
409 }
410
411 EditBone *duplicateEditBone(EditBone *curBone, const char *name, ListBase *editbones, Object *ob)
412 {
413         return duplicateEditBoneObjects(curBone, name, editbones, ob, ob);
414 }
415
416 /* previously adduplicate_armature */
417 static int armature_duplicate_selected_exec(bContext *C, wmOperator *UNUSED(op))
418 {
419         bArmature *arm;
420         EditBone    *eBone = NULL;
421         EditBone    *curBone;
422         EditBone    *firstDup = NULL; /*        The beginning of the duplicated bones in the edbo list */
423
424         Object *obedit = CTX_data_edit_object(C);
425         arm = obedit->data;
426
427         /* cancel if nothing selected */
428         if (CTX_DATA_COUNT(C, selected_bones) == 0)
429                 return OPERATOR_CANCELLED;
430         
431         ED_armature_sync_selection(arm->edbo); // XXX why is this needed?
432
433         preEditBoneDuplicate(arm->edbo);
434
435         /* Select mirrored bones */
436         if (arm->flag & ARM_MIRROR_EDIT) {
437                 for (curBone = arm->edbo->first; curBone; curBone = curBone->next) {
438                         if (EBONE_VISIBLE(arm, curBone)) {
439                                 if (curBone->flag & BONE_SELECTED) {
440                                         eBone = ED_armature_bone_get_mirrored(arm->edbo, curBone);
441                                         if (eBone)
442                                                 eBone->flag |= BONE_SELECTED;
443                                 }
444                         }
445                 }
446         }
447
448         
449         /*      Find the selected bones and duplicate them as needed */
450         for (curBone = arm->edbo->first; curBone && curBone != firstDup; curBone = curBone->next) {
451                 if (EBONE_VISIBLE(arm, curBone)) {
452                         if (curBone->flag & BONE_SELECTED) {
453                                 
454                                 eBone = duplicateEditBone(curBone, curBone->name, arm->edbo, obedit);
455                                 
456                                 if (!firstDup)
457                                         firstDup = eBone;
458
459                         }
460                 }
461         }
462
463         /*      Run though the list and fix the pointers */
464         for (curBone = arm->edbo->first; curBone && curBone != firstDup; curBone = curBone->next) {
465                 if (EBONE_VISIBLE(arm, curBone)) {
466                         if (curBone->flag & BONE_SELECTED) {
467                                 eBone = (EditBone *) curBone->temp;
468                                 
469                                 if (!curBone->parent) {
470                                         /* If this bone has no parent,
471                                          * Set the duplicate->parent to NULL
472                                          */
473                                         eBone->parent = NULL;
474                                 }
475                                 else if (curBone->parent->temp) {
476                                         /* If this bone has a parent that was duplicated,
477                                          * Set the duplicate->parent to the curBone->parent->temp
478                                          */
479                                         eBone->parent = (EditBone *)curBone->parent->temp;
480                                 }
481                                 else {
482                                         /* If this bone has a parent that IS not selected,
483                                          * Set the duplicate->parent to the curBone->parent
484                                          */
485                                         eBone->parent = (EditBone *) curBone->parent;
486                                         eBone->flag &= ~BONE_CONNECTED;
487                                 }
488                                 
489                                 /* Lets try to fix any constraint subtargets that might
490                                  * have been duplicated 
491                                  */
492                                 updateDuplicateSubtarget(eBone, arm->edbo, obedit);
493                         }
494                 }
495         }
496         
497         /* correct the active bone */
498         if (arm->act_edbone) {
499                 eBone = arm->act_edbone;
500                 if (eBone->temp)
501                         arm->act_edbone = eBone->temp;
502         }
503
504         /*      Deselect the old bones and select the new ones */
505         for (curBone = arm->edbo->first; curBone && curBone != firstDup; curBone = curBone->next) {
506                 if (EBONE_VISIBLE(arm, curBone))
507                         curBone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
508         }
509
510         ED_armature_validate_active(arm);
511
512         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
513         
514         return OPERATOR_FINISHED;
515 }
516
517
518 void ARMATURE_OT_duplicate(wmOperatorType *ot)
519 {
520         /* identifiers */
521         ot->name = "Duplicate Selected Bone(s)";
522         ot->idname = "ARMATURE_OT_duplicate";
523         ot->description = "Make copies of the selected bones within the same armature";
524         
525         /* api callbacks */
526         ot->exec = armature_duplicate_selected_exec;
527         ot->poll = ED_operator_editarmature;
528         
529         /* flags */
530         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
531 }
532
533 /* ------------------------------------------ */
534
535 /* previously extrude_armature */
536 /* context; editmode armature */
537 /* if forked && mirror-edit: makes two bones with flipped names */
538 static int armature_extrude_exec(bContext *C, wmOperator *op)
539 {
540         Object *obedit;
541         bArmature *arm;
542         EditBone *newbone, *ebone, *flipbone, *first = NULL;
543         int a, totbone = 0, do_extrude;
544         int forked = RNA_boolean_get(op->ptr, "forked");
545
546         obedit = CTX_data_edit_object(C);
547         arm = obedit->data;
548
549         /* since we allow root extrude too, we have to make sure selection is OK */
550         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
551                 if (EBONE_VISIBLE(arm, ebone)) {
552                         if (ebone->flag & BONE_ROOTSEL) {
553                                 if (ebone->parent && (ebone->flag & BONE_CONNECTED)) {
554                                         if (ebone->parent->flag & BONE_TIPSEL)
555                                                 ebone->flag &= ~BONE_ROOTSEL;
556                                 }
557                         }
558                 }
559         }
560         
561         /* Duplicate the necessary bones */
562         for (ebone = arm->edbo->first; ((ebone) && (ebone != first)); ebone = ebone->next) {
563                 if (EBONE_VISIBLE(arm, ebone)) {
564                         /* we extrude per definition the tip */
565                         do_extrude = FALSE;
566                         if (ebone->flag & (BONE_TIPSEL | BONE_SELECTED)) {
567                                 do_extrude = TRUE;
568                         }
569                         else if (ebone->flag & BONE_ROOTSEL) {
570                                 /* but, a bone with parent deselected we do the root... */
571                                 if (ebone->parent && (ebone->parent->flag & BONE_TIPSEL)) {
572                                         /* pass */
573                                 }
574                                 else {
575                                         do_extrude = 2;
576                                 }
577                         }
578                         
579                         if (do_extrude) {
580                                 /* we re-use code for mirror editing... */
581                                 flipbone = NULL;
582                                 if (arm->flag & ARM_MIRROR_EDIT) {
583                                         flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
584                                         if (flipbone) {
585                                                 forked = 0;  // we extrude 2 different bones
586                                                 if (flipbone->flag & (BONE_TIPSEL | BONE_ROOTSEL | BONE_SELECTED))
587                                                         /* don't want this bone to be selected... */
588                                                         flipbone->flag &= ~(BONE_TIPSEL | BONE_SELECTED | BONE_ROOTSEL);
589                                         }
590                                         if ((flipbone == NULL) && (forked))
591                                                 flipbone = ebone;
592                                 }
593                                 
594                                 for (a = 0; a < 2; a++) {
595                                         if (a == 1) {
596                                                 if (flipbone == NULL)
597                                                         break;
598                                                 else {
599                                                         SWAP(EditBone *, flipbone, ebone);
600                                                 }
601                                         }
602                                         
603                                         totbone++;
604                                         newbone = MEM_callocN(sizeof(EditBone), "extrudebone");
605                                         
606                                         if (do_extrude == TRUE) {
607                                                 copy_v3_v3(newbone->head, ebone->tail);
608                                                 copy_v3_v3(newbone->tail, newbone->head);
609                                                 newbone->parent = ebone;
610                                                 
611                                                 newbone->flag = ebone->flag & (BONE_TIPSEL | BONE_RELATIVE_PARENTING);  // copies it, in case mirrored bone
612                                                 
613                                                 if (newbone->parent) newbone->flag |= BONE_CONNECTED;
614                                         }
615                                         else {
616                                                 copy_v3_v3(newbone->head, ebone->head);
617                                                 copy_v3_v3(newbone->tail, ebone->head);
618                                                 newbone->parent = ebone->parent;
619                                                 
620                                                 newbone->flag = BONE_TIPSEL;
621                                                 
622                                                 if (newbone->parent && (ebone->flag & BONE_CONNECTED)) {
623                                                         newbone->flag |= BONE_CONNECTED;
624                                                 }
625                                         }
626                                         
627                                         newbone->weight = ebone->weight;
628                                         newbone->dist = ebone->dist;
629                                         newbone->xwidth = ebone->xwidth;
630                                         newbone->zwidth = ebone->zwidth;
631                                         newbone->ease1 = ebone->ease1;
632                                         newbone->ease2 = ebone->ease2;
633                                         newbone->rad_head = ebone->rad_tail; // don't copy entire bone...
634                                         newbone->rad_tail = ebone->rad_tail;
635                                         newbone->segments = 1;
636                                         newbone->layer = ebone->layer;
637                                         
638                                         BLI_strncpy(newbone->name, ebone->name, sizeof(newbone->name));
639                                         
640                                         if (flipbone && forked) {   // only set if mirror edit
641                                                 if (strlen(newbone->name) < 30) {
642                                                         if (a == 0) strcat(newbone->name, "_L");
643                                                         else strcat(newbone->name, "_R");
644                                                 }
645                                         }
646                                         unique_editbone_name(arm->edbo, newbone->name, NULL);
647                                         
648                                         /* Add the new bone to the list */
649                                         BLI_addtail(arm->edbo, newbone);
650                                         if (!first)
651                                                 first = newbone;
652                                         
653                                         /* restore ebone if we were flipping */
654                                         if (a == 1 && flipbone)
655                                                 SWAP(EditBone *, flipbone, ebone);
656                                 }
657                         }
658                         
659                         /* Deselect the old bone */
660                         ebone->flag &= ~(BONE_TIPSEL | BONE_SELECTED | BONE_ROOTSEL);
661                 }
662         }
663         /* if only one bone, make this one active */
664         if (totbone == 1 && first) arm->act_edbone = first;
665
666         if (totbone == 0) return OPERATOR_CANCELLED;
667
668         /* Transform the endpoints */
669         ED_armature_sync_selection(arm->edbo);
670
671         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
672
673         return OPERATOR_FINISHED;
674 }
675
676 void ARMATURE_OT_extrude(wmOperatorType *ot)
677 {
678         /* identifiers */
679         ot->name = "Extrude";
680         ot->idname = "ARMATURE_OT_extrude";
681         ot->description = "Create new bones from the selected joints";
682         
683         /* api callbacks */
684         ot->exec = armature_extrude_exec;
685         ot->poll = ED_operator_editarmature;
686         
687         /* flags */
688         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
689         
690         /* props */
691         RNA_def_boolean(ot->srna, "forked", 0, "Forked", "");
692 }
693
694 /* ********************** Bone Add *************************************/
695
696 /*op makes a new bone and returns it with its tip selected */
697
698 static int armature_bone_primitive_add_exec(bContext *C, wmOperator *op) 
699 {
700         RegionView3D *rv3d = CTX_wm_region_view3d(C);
701         Object *obedit = CTX_data_edit_object(C);
702         EditBone *bone;
703         float obmat[3][3], curs[3], viewmat[3][3], totmat[3][3], imat[3][3];
704         char name[MAXBONENAME];
705         
706         RNA_string_get(op->ptr, "name", name);
707         
708         copy_v3_v3(curs, give_cursor(CTX_data_scene(C), CTX_wm_view3d(C)));
709
710         /* Get inverse point for head and orientation for tail */
711         invert_m4_m4(obedit->imat, obedit->obmat);
712         mul_m4_v3(obedit->imat, curs);
713
714         if (rv3d && (U.flag & USER_ADD_VIEWALIGNED))
715                 copy_m3_m4(obmat, rv3d->viewmat);
716         else unit_m3(obmat);
717         
718         copy_m3_m4(viewmat, obedit->obmat);
719         mul_m3_m3m3(totmat, obmat, viewmat);
720         invert_m3_m3(imat, totmat);
721         
722         ED_armature_deselect_all(obedit, 0);
723         
724         /*      Create a bone   */
725         bone = ED_armature_edit_bone_add(obedit->data, name);
726
727         copy_v3_v3(bone->head, curs);
728         
729         if (rv3d && (U.flag & USER_ADD_VIEWALIGNED))
730                 add_v3_v3v3(bone->tail, bone->head, imat[1]);   // bone with unit length 1
731         else
732                 add_v3_v3v3(bone->tail, bone->head, imat[2]);   // bone with unit length 1, pointing up Z
733
734         /* note, notifier might evolve */
735         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
736
737         return OPERATOR_FINISHED;
738 }
739
740 void ARMATURE_OT_bone_primitive_add(wmOperatorType *ot)
741 {
742         /* identifiers */
743         ot->name = "Add Bone";
744         ot->idname = "ARMATURE_OT_bone_primitive_add";
745         ot->description = "Add a new bone located at the 3D-Cursor";
746         
747         /* api callbacks */
748         ot->exec = armature_bone_primitive_add_exec;
749         ot->poll = ED_operator_editarmature;
750         
751         /* flags */
752         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
753         
754         RNA_def_string(ot->srna, "name", "Bone", MAXBONENAME, "Name", "Name of the newly created bone");
755         
756 }
757
758 /* ********************** Subdivide *******************************/
759
760 /* Subdivide Operators:
761  * This group of operators all use the same 'exec' callback, but they are called
762  * through several different operators - a combined menu (which just calls the exec in the 
763  * appropriate ways), and two separate ones.
764  */
765
766 static int armature_subdivide_exec(bContext *C, wmOperator *op)
767 {
768         Object *obedit = CTX_data_edit_object(C);
769         bArmature *arm = obedit->data;
770         EditBone *newbone, *tbone;
771         int cuts, i;
772         
773         /* there may not be a number_cuts property defined (for 'simple' subdivide) */
774         cuts = RNA_int_get(op->ptr, "number_cuts");
775         
776         /* loop over all editable bones */
777         // XXX the old code did this in reverse order though!
778         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones)
779         {
780                 for (i = cuts + 1; i > 1; i--) {
781                         /* compute cut ratio first */
782                         float cutratio = 1.0f / (float)i;
783                         float cutratioI = 1.0f - cutratio;
784                         
785                         float val1[3];
786                         float val2[3];
787                         float val3[3];
788                         
789                         newbone = MEM_mallocN(sizeof(EditBone), "ebone subdiv");
790                         *newbone = *ebone;
791                         BLI_addtail(arm->edbo, newbone);
792                         
793                         /* calculate location of newbone->head */
794                         copy_v3_v3(val1, ebone->head);
795                         copy_v3_v3(val2, ebone->tail);
796                         copy_v3_v3(val3, newbone->head);
797                         
798                         val3[0] = val1[0] * cutratio + val2[0] * cutratioI;
799                         val3[1] = val1[1] * cutratio + val2[1] * cutratioI;
800                         val3[2] = val1[2] * cutratio + val2[2] * cutratioI;
801                         
802                         copy_v3_v3(newbone->head, val3);
803                         copy_v3_v3(newbone->tail, ebone->tail);
804                         copy_v3_v3(ebone->tail, newbone->head);
805                         
806                         newbone->rad_head = 0.5f * (ebone->rad_head + ebone->rad_tail);
807                         ebone->rad_tail = newbone->rad_head;
808                         
809                         newbone->flag |= BONE_CONNECTED;
810                         
811                         unique_editbone_name(arm->edbo, newbone->name, NULL);
812                         
813                         /* correct parent bones */
814                         for (tbone = arm->edbo->first; tbone; tbone = tbone->next) {
815                                 if (tbone->parent == ebone)
816                                         tbone->parent = newbone;
817                         }
818                         newbone->parent = ebone;
819                 }
820         }
821         CTX_DATA_END;
822         
823         /* note, notifier might evolve */
824         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
825         
826         return OPERATOR_FINISHED;
827 }
828
829 void ARMATURE_OT_subdivide(wmOperatorType *ot)
830 {
831         PropertyRNA *prop;
832
833         /* identifiers */
834         ot->name = "Subdivide Multi";
835         ot->idname = "ARMATURE_OT_subdivide";
836         ot->description = "Break selected bones into chains of smaller bones";
837         
838         /* api callbacks */
839         ot->exec = armature_subdivide_exec;
840         ot->poll = ED_operator_editarmature;
841         
842         /* flags */
843         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
844         
845         /* Properties */
846         prop = RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
847         /* avoid re-using last var because it can cause _very_ high poly meshes and annoy users (or worse crash) */
848         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
849 }