4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation, 2002-2009 full recode.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/armature/editarmature.c
42 #include "DNA_anim_types.h"
43 #include "DNA_armature_types.h"
44 #include "DNA_constraint_types.h"
45 #include "DNA_meshdata_types.h"
46 #include "DNA_scene_types.h"
48 #include "MEM_guardedalloc.h"
50 #include "BLI_blenlib.h"
52 #include "BLI_utildefines.h"
53 #include "BLI_editVert.h"
54 #include "BLI_ghash.h"
56 #include "BKE_animsys.h"
57 #include "BKE_action.h"
58 #include "BKE_armature.h"
59 #include "BKE_constraint.h"
60 #include "BKE_context.h"
61 #include "BKE_deform.h"
62 #include "BKE_depsgraph.h"
63 #include "BKE_DerivedMesh.h"
64 #include "BKE_global.h"
65 #include "BKE_idprop.h"
67 #include "BKE_object.h"
68 #include "BKE_report.h"
69 #include "BKE_subsurf.h"
70 #include "BKE_modifier.h"
71 #include "DNA_object_types.h"
75 #include "RNA_access.h"
76 #include "RNA_define.h"
81 #include "ED_armature.h"
82 #include "ED_keyframing.h"
84 #include "ED_object.h"
85 #include "ED_screen.h"
87 #include "ED_view3d.h"
89 #include "UI_interface.h"
90 #include "UI_resources.h"
92 #include "armature_intern.h"
93 #include "meshlaplacian.h"
99 /* **************** tools on Editmode Armature **************** */
101 /* Sync selection to parent for connected children */
102 void ED_armature_sync_selection(ListBase *edbo)
106 for (ebo=edbo->first; ebo; ebo= ebo->next) {
107 /* if bone is not selectable, we shouldn't alter this setting... */
108 if ((ebo->flag & BONE_UNSELECTABLE) == 0) {
109 if ((ebo->flag & BONE_CONNECTED) && (ebo->parent)) {
110 if (ebo->parent->flag & BONE_TIPSEL)
111 ebo->flag |= BONE_ROOTSEL;
113 ebo->flag &= ~BONE_ROOTSEL;
116 if ((ebo->flag & BONE_TIPSEL) && (ebo->flag & BONE_ROOTSEL))
117 ebo->flag |= BONE_SELECTED;
119 ebo->flag &= ~BONE_SELECTED;
124 void ED_armature_validate_active(struct bArmature *arm)
126 EditBone *ebone= arm->act_edbone;
129 if(ebone->flag & BONE_HIDDEN_A)
130 arm->act_edbone= NULL;
134 static void bone_free(bArmature *arm, EditBone *bone)
136 if(arm->act_edbone==bone)
137 arm->act_edbone= NULL;
140 IDP_FreeProperty(bone->prop);
141 MEM_freeN(bone->prop);
144 BLI_freelinkN(arm->edbo, bone);
147 void ED_armature_edit_bone_remove(bArmature *arm, EditBone *exBone)
151 /* Find any bones that refer to this bone */
152 for (curBone=arm->edbo->first; curBone; curBone=curBone->next) {
153 if (curBone->parent==exBone) {
154 curBone->parent=exBone->parent;
155 curBone->flag &= ~BONE_CONNECTED;
159 bone_free(arm, exBone);
162 /* context: editmode armature */
163 EditBone *ED_armature_bone_get_mirrored(ListBase *edbo, EditBone *ebo)
165 EditBone *eboflip= NULL;
171 flip_side_name(name, ebo->name, FALSE);
173 for (eboflip= edbo->first; eboflip; eboflip=eboflip->next) {
174 if (ebo != eboflip) {
175 if (!strcmp (name, eboflip->name))
183 /* helper function for tools to work on mirrored parts.
184 it leaves mirrored bones selected then too, which is a good indication of what happened */
185 static void armature_select_mirrored(bArmature *arm)
187 /* Select mirrored bones */
188 if (arm->flag & ARM_MIRROR_EDIT) {
189 EditBone *curBone, *ebone_mirr;
191 for (curBone=arm->edbo->first; curBone; curBone=curBone->next) {
192 if (arm->layer & curBone->layer) {
193 if (curBone->flag & BONE_SELECTED) {
194 ebone_mirr= ED_armature_bone_get_mirrored(arm->edbo, curBone);
196 ebone_mirr->flag |= BONE_SELECTED;
204 static void armature_tag_select_mirrored(bArmature *arm)
209 for (curBone=arm->edbo->first; curBone; curBone=curBone->next) {
210 curBone->flag &= ~BONE_DONE;
213 /* Select mirrored bones */
214 if (arm->flag & ARM_MIRROR_EDIT) {
215 for (curBone=arm->edbo->first; curBone; curBone=curBone->next) {
216 if (arm->layer & curBone->layer) {
217 if (curBone->flag & (BONE_SELECTED|BONE_ROOTSEL|BONE_TIPSEL)) {
218 EditBone *ebone_mirr= ED_armature_bone_get_mirrored(arm->edbo, curBone);
219 if (ebone_mirr && (ebone_mirr->flag & BONE_SELECTED) == 0) {
220 ebone_mirr->flag |= BONE_DONE;
226 for (curBone=arm->edbo->first; curBone; curBone=curBone->next) {
227 if (curBone->flag & BONE_DONE) {
228 EditBone *ebone_mirr= ED_armature_bone_get_mirrored(arm->edbo, curBone);
229 curBone->flag |= ebone_mirr->flag & (BONE_SELECTED|BONE_ROOTSEL|BONE_TIPSEL);
236 /* only works when tagged */
237 static void armature_tag_unselect(bArmature *arm)
241 for (curBone=arm->edbo->first; curBone; curBone=curBone->next) {
242 if (curBone->flag & BONE_DONE) {
243 curBone->flag &= ~(BONE_SELECTED|BONE_ROOTSEL|BONE_TIPSEL|BONE_DONE);
248 /* converts Bones to EditBone list, used for tools as well */
249 EditBone *make_boneList(ListBase *edbo, ListBase *bones, EditBone *parent, Bone *actBone)
252 EditBone *eBoneAct= NULL;
253 EditBone *eBoneTest= NULL;
256 for (curBone=bones->first; curBone; curBone=curBone->next) {
257 eBone= MEM_callocN(sizeof(EditBone), "make_editbone");
259 /* Copy relevant data from bone to eBone */
260 eBone->parent= parent;
261 BLI_strncpy(eBone->name, curBone->name, sizeof(eBone->name));
262 eBone->flag = curBone->flag;
264 /* fix selection flags */
266 if (eBone->flag & BONE_SELECTED) {
267 /* if the bone is selected the copy its root selection to the parents tip */
268 eBone->flag |= BONE_TIPSEL;
269 if (eBone->parent && (eBone->flag & BONE_CONNECTED)) {
270 eBone->parent->flag |= BONE_TIPSEL;
271 eBone->flag &= ~BONE_ROOTSEL; /* this is ignored when there is a connected parent, so unset it */
274 eBone->flag |= BONE_ROOTSEL;
278 /* if the bone is not selected, but connected to its parent
279 * copy the parents tip selection state */
280 if(eBone->parent && (eBone->flag & BONE_CONNECTED)) {
281 /* selecting with the mouse gives this behavior */
282 if(eBone->parent->flag & BONE_TIPSEL) {
283 eBone->flag |= BONE_ROOTSEL;
286 eBone->flag &= ~BONE_ROOTSEL;
289 /* probably not selected but just incase */
290 eBone->flag &= ~BONE_TIPSEL;
294 copy_v3_v3(eBone->head, curBone->arm_head);
295 copy_v3_v3(eBone->tail, curBone->arm_tail);
296 eBone->roll = curBone->arm_roll;
298 /* rest of stuff copy */
299 eBone->length= curBone->length;
300 eBone->dist= curBone->dist;
301 eBone->weight= curBone->weight;
302 eBone->xwidth= curBone->xwidth;
303 eBone->zwidth= curBone->zwidth;
304 eBone->ease1= curBone->ease1;
305 eBone->ease2= curBone->ease2;
306 eBone->rad_head= curBone->rad_head;
307 eBone->rad_tail= curBone->rad_tail;
308 eBone->segments = curBone->segments;
309 eBone->layer = curBone->layer;
312 eBone->prop= IDP_CopyProperty(curBone->prop);
314 BLI_addtail(edbo, eBone);
316 /* Add children if necessary */
317 if (curBone->childbase.first) {
318 eBoneTest= make_boneList(edbo, &curBone->childbase, eBone, actBone);
330 /* nasty stuff for converting roll in editbones into bones */
331 /* also sets restposition in armature (arm_mat) */
332 static void fix_bonelist_roll (ListBase *bonelist, ListBase *editbonelist)
342 for (curBone=bonelist->first; curBone; curBone=curBone->next) {
343 /* sets local matrix and arm_mat (restpos) */
344 where_is_armature_bone(curBone, curBone->parent);
346 /* Find the associated editbone */
347 for (ebone = editbonelist->first; ebone; ebone=ebone->next)
348 if ((Bone*)ebone->temp == curBone)
352 /* Get the ebone premat */
353 sub_v3_v3v3(delta, ebone->tail, ebone->head);
354 vec_roll_to_mat3(delta, ebone->roll, premat);
356 /* Get the bone postmat */
357 copy_m3_m4(postmat, curBone->arm_mat);
359 invert_m3_m3(imat, premat);
360 mul_m3_m3m3(difmat, imat, postmat);
362 printf ("Bone %s\n", curBone->name);
363 print_m4("premat", premat);
364 print_m4("postmat", postmat);
365 print_m4("difmat", difmat);
366 printf ("Roll = %f\n", (-atan2(difmat[2][0], difmat[2][2]) * (180.0/M_PI)));
368 curBone->roll = (float)-atan2(difmat[2][0], difmat[2][2]);
370 /* and set restposition again */
371 where_is_armature_bone(curBone, curBone->parent);
373 fix_bonelist_roll(&curBone->childbase, editbonelist);
377 /* put EditMode back in Object */
378 void ED_armature_from_edit(Object *obedit)
380 bArmature *arm= obedit->data;
381 EditBone *eBone, *neBone;
386 free_bonelist(&arm->bonebase);
388 /* remove zero sized bones, this gives instable restposes */
389 for (eBone=arm->edbo->first; eBone; eBone= neBone) {
390 float len= len_v3v3(eBone->head, eBone->tail);
392 if (len <= 0.000001f) { /* FLT_EPSILON is too large? */
395 /* Find any bones that refer to this bone */
396 for (fBone=arm->edbo->first; fBone; fBone= fBone->next) {
397 if (fBone->parent==eBone)
398 fBone->parent= eBone->parent;
401 printf("Warning: removed zero sized bone: %s\n", eBone->name);
402 bone_free(arm, eBone);
406 /* Copy the bones from the editData into the armature */
407 for (eBone=arm->edbo->first; eBone; eBone=eBone->next) {
408 newBone= MEM_callocN(sizeof(Bone), "bone");
409 eBone->temp= newBone; /* Associate the real Bones with the EditBones */
411 BLI_strncpy(newBone->name, eBone->name, sizeof(newBone->name));
412 copy_v3_v3(newBone->arm_head, eBone->head);
413 copy_v3_v3(newBone->arm_tail, eBone->tail);
414 newBone->arm_roll = eBone->roll;
416 newBone->flag= eBone->flag;
418 if (eBone == arm->act_edbone) {
419 /* don't change active selection, this messes up separate which uses
420 * editmode toggle and can separate active bone which is de-selected originally */
421 /* newBone->flag |= BONE_SELECTED; */ /* important, editbones can be active with only 1 point selected */
422 arm->act_edbone= NULL;
423 arm->act_bone= newBone;
425 newBone->roll = 0.0f;
427 newBone->weight = eBone->weight;
428 newBone->dist = eBone->dist;
430 newBone->xwidth = eBone->xwidth;
431 newBone->zwidth = eBone->zwidth;
432 newBone->ease1= eBone->ease1;
433 newBone->ease2= eBone->ease2;
434 newBone->rad_head= eBone->rad_head;
435 newBone->rad_tail= eBone->rad_tail;
436 newBone->segments= eBone->segments;
437 newBone->layer = eBone->layer;
440 newBone->prop= IDP_CopyProperty(eBone->prop);
443 /* Fix parenting in a separate pass to ensure ebone->bone connections
444 are valid at this point */
445 for (eBone=arm->edbo->first;eBone;eBone=eBone->next) {
446 newBone= (Bone *)eBone->temp;
448 newBone->parent= (Bone *)eBone->parent->temp;
449 BLI_addtail(&newBone->parent->childbase, newBone);
452 float M_parentRest[3][3];
453 float iM_parentRest[3][3];
456 /* Get the parent's matrix (rotation only) */
457 sub_v3_v3v3(delta, eBone->parent->tail, eBone->parent->head);
458 vec_roll_to_mat3(delta, eBone->parent->roll, M_parentRest);
460 /* Invert the parent matrix */
461 invert_m3_m3(iM_parentRest, M_parentRest);
463 /* Get the new head and tail */
464 sub_v3_v3v3(newBone->head, eBone->head, eBone->parent->tail);
465 sub_v3_v3v3(newBone->tail, eBone->tail, eBone->parent->tail);
467 mul_m3_v3(iM_parentRest, newBone->head);
468 mul_m3_v3(iM_parentRest, newBone->tail);
471 /* ...otherwise add this bone to the armature's bonebase */
473 copy_v3_v3(newBone->head, eBone->head);
474 copy_v3_v3(newBone->tail, eBone->tail);
475 BLI_addtail(&arm->bonebase, newBone);
479 /* Make a pass through the new armature to fix rolling */
480 /* also builds restposition again (like where_is_armature) */
481 fix_bonelist_roll(&arm->bonebase, arm->edbo);
483 /* so all users of this armature should get rebuilt */
484 for (obt= G.main->object.first; obt; obt= obt->id.next) {
486 armature_rebuild_pose(obt, arm);
489 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
492 void ED_armature_apply_transform(Object *ob, float mat[4][4])
495 bArmature *arm= ob->data;
496 float scale = mat4_to_scale(mat); /* store the scale of the matrix here to use on envelopes */
498 /* Put the armature into editmode */
499 ED_armature_to_edit(ob);
501 /* Do the rotations */
502 for (ebone = arm->edbo->first; ebone; ebone=ebone->next){
503 mul_m4_v3(mat, ebone->head);
504 mul_m4_v3(mat, ebone->tail);
506 ebone->rad_head *= scale;
507 ebone->rad_tail *= scale;
508 ebone->dist *= scale;
510 /* we could be smarter and scale by the matrix along the x & z axis */
511 ebone->xwidth *= scale;
512 ebone->zwidth *= scale;
515 /* Turn the list into an armature */
516 ED_armature_from_edit(ob);
517 ED_armature_edit_free(ob);
520 /* exported for use in editors/object/ */
521 /* 0 == do center, 1 == center new, 2 == center cursor */
522 void docenter_armature (Scene *scene, Object *ob, float cursor[3], int centermode, int around)
524 Object *obedit= scene->obedit; // XXX get from context
526 bArmature *arm= ob->data;
529 /* Put the armature into editmode */
531 ED_armature_to_edit(ob);
532 obedit= NULL; /* we cant use this so behave as if there is no obedit */
535 /* Find the centerpoint */
536 if (centermode == 2) {
537 copy_v3_v3(cent, cursor);
538 invert_m4_m4(ob->imat, ob->obmat);
539 mul_m4_v3(ob->imat, cent);
542 if(around==V3D_CENTROID) {
545 for (ebone= arm->edbo->first; ebone; ebone=ebone->next) {
547 add_v3_v3(cent, ebone->head);
548 add_v3_v3(cent, ebone->tail);
550 mul_v3_fl(cent, 1.0f/(float)total);
553 float min[3], max[3];
554 INIT_MINMAX(min, max);
555 for (ebone= arm->edbo->first; ebone; ebone=ebone->next) {
556 DO_MINMAX(ebone->head, min, max);
557 DO_MINMAX(ebone->tail, min, max);
559 mid_v3_v3v3(cent, min, max);
563 /* Do the adjustments */
564 for (ebone= arm->edbo->first; ebone; ebone=ebone->next) {
565 sub_v3_v3(ebone->head, cent);
566 sub_v3_v3(ebone->tail, cent);
569 /* Turn the list into an armature */
571 ED_armature_from_edit(ob);
572 ED_armature_edit_free(ob);
575 /* Adjust object location for new centerpoint */
576 if(centermode && obedit==NULL) {
577 mul_mat3_m4_v3(ob->obmat, cent); /* ommit translation part */
578 add_v3_v3(ob->loc, cent);
582 /* ---------------------- */
584 /* checks if an EditBone with a matching name already, returning the matching bone if it exists */
585 static EditBone *editbone_name_exists (ListBase *edbo, const char *name)
587 return BLI_findstring(edbo, name, offsetof(EditBone, name));
590 /* note: there's a unique_bone_name() too! */
591 static int editbone_unique_check(void *arg, const char *name)
593 struct {ListBase *lb;void *bone;} *data= arg;
594 EditBone *dupli= editbone_name_exists(data->lb, name);
595 return dupli && dupli != data->bone;
598 void unique_editbone_name (ListBase *edbo, char *name, EditBone *bone)
600 struct {ListBase *lb; void *bone;} data;
604 BLI_uniquename_cb(editbone_unique_check, &data, "Bone", '.', name, sizeof(bone->name));
607 /* helper for apply_armature_pose2bones - fixes parenting of objects that are bone-parented to armature */
608 static void applyarmature_fix_boneparents (Scene *scene, Object *armob)
612 /* go through all objects in database */
613 for (ob= G.main->object.first; ob; ob= ob->id.next) {
614 /* if parent is bone in this armature, apply corrections */
615 if ((ob->parent == armob) && (ob->partype == PARBONE)) {
616 /* apply current transform from parent (not yet destroyed),
617 * then calculate new parent inverse matrix
619 object_apply_mat4(ob, ob->obmat, FALSE, FALSE);
621 what_does_parent(scene, ob, &workob);
622 invert_m4_m4(ob->parentinv, workob.obmat);
627 /* set the current pose as the restpose */
628 static int apply_armature_pose2bones_exec (bContext *C, wmOperator *op)
630 Scene *scene= CTX_data_scene(C);
631 Object *ob= ED_object_pose_armature(CTX_data_active_object(C)); // must be active object, not edit-object
632 bArmature *arm= get_armature(ob);
637 /* don't check if editmode (should be done by caller) */
638 if (ob->type!=OB_ARMATURE)
639 return OPERATOR_CANCELLED;
640 if (object_data_is_libdata(ob)) {
641 BKE_report(op->reports, RPT_ERROR, "Cannot apply pose to lib-linked armature."); //error_libdata();
642 return OPERATOR_CANCELLED;
645 /* helpful warnings... */
646 // TODO: add warnings to be careful about actions, applying deforms first, etc.
647 if (ob->adt && ob->adt->action)
648 BKE_report(op->reports, RPT_WARNING, "Actions on this armature will be destroyed by this new rest pose as the transforms stored are relative to the old rest pose");
650 /* Get editbones of active armature to alter */
651 ED_armature_to_edit(ob);
653 /* get pose of active object and move it out of posemode */
656 for (pchan=pose->chanbase.first; pchan; pchan=pchan->next) {
657 curbone= editbone_name_exists(arm->edbo, pchan->name);
659 /* simply copy the head/tail values from pchan over to curbone */
660 copy_v3_v3(curbone->head, pchan->pose_head);
661 copy_v3_v3(curbone->tail, pchan->pose_tail);
664 * 1. find auto-calculated roll value for this bone now
665 * 2. remove this from the 'visual' y-rotation
668 float premat[3][3], imat[3][3],pmat[3][3], tmat[3][3];
669 float delta[3], eul[3];
671 /* obtain new auto y-rotation */
672 sub_v3_v3v3(delta, curbone->tail, curbone->head);
673 vec_roll_to_mat3(delta, 0.0f, premat);
674 invert_m3_m3(imat, premat);
676 /* get pchan 'visual' matrix */
677 copy_m3_m4(pmat, pchan->pose_mat);
679 /* remove auto from visual and get euler rotation */
680 mul_m3_m3m3(tmat, imat, pmat);
681 mat3_to_eul( eul,tmat);
683 /* just use this euler-y as new roll value */
684 curbone->roll= eul[1];
687 /* clear transform values for pchan */
690 unit_qt(pchan->quat);
691 unit_axis_angle(pchan->rotAxis, &pchan->rotAngle);
692 pchan->size[0]= pchan->size[1]= pchan->size[2]= 1.0f;
695 curbone->flag |= BONE_UNKEYED;
698 /* convert editbones back to bones, and then free the edit-data */
699 ED_armature_from_edit(ob);
700 ED_armature_edit_free(ob);
702 /* flush positions of posebones */
703 where_is_pose(scene, ob);
705 /* fix parenting of objects which are bone-parented */
706 applyarmature_fix_boneparents(scene, ob);
708 /* note, notifier might evolve */
709 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
711 return OPERATOR_FINISHED;
714 void POSE_OT_armature_apply (wmOperatorType *ot)
717 ot->name= "Apply Pose as Rest Pose";
718 ot->idname= "POSE_OT_armature_apply";
719 ot->description= "Apply the current pose as the new rest pose";
722 ot->exec= apply_armature_pose2bones_exec;
723 ot->poll= ED_operator_posemode;
726 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
730 /* set the current pose as the restpose */
731 static int pose_visual_transform_apply_exec (bContext *C, wmOperator *UNUSED(op))
733 Object *ob= ED_object_pose_armature(CTX_data_active_object(C)); // must be active object, not edit-object
735 /* don't check if editmode (should be done by caller) */
736 if (ob->type!=OB_ARMATURE)
737 return OPERATOR_CANCELLED;
739 /* loop over all selected pchans
741 * TODO, loop over children before parents if multiple bones
742 * at once are to be predictable*/
743 CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones)
745 float delta_mat[4][4];
747 /* chan_mat already contains the delta transform from rest pose to pose-mode pose
748 * as that is baked into there so that B-Bones will work. Once we've set this as the
749 * new raw-transform components, don't recalc the poses yet, otherwise IK result will
750 * change, thus changing the result we may be trying to record.
752 copy_m4_m4(delta_mat, pchan->chan_mat);
753 pchan_apply_mat4(pchan, delta_mat, TRUE);
757 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
759 /* note, notifier might evolve */
760 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
762 return OPERATOR_FINISHED;
765 void POSE_OT_visual_transform_apply (wmOperatorType *ot)
768 ot->name= "Apply Visual Transform to Pose";
769 ot->idname= "POSE_OT_visual_transform_apply";
770 ot->description= "Apply final constrained position of pose bones to their transform.";
773 ot->exec= pose_visual_transform_apply_exec;
774 ot->poll= ED_operator_posemode;
777 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
780 /* ---------------------- */
782 /* Helper function for armature joining - link fixing */
783 static void joined_armature_fix_links(Object *tarArm, Object *srcArm, bPoseChannel *pchan, EditBone *curbone)
787 bPoseChannel *pchant;
790 /* let's go through all objects in database */
791 for (ob= G.main->object.first; ob; ob= ob->id.next) {
792 /* do some object-type specific things */
793 if (ob->type == OB_ARMATURE) {
795 for (pchant= pose->chanbase.first; pchant; pchant= pchant->next) {
796 for (con= pchant->constraints.first; con; con= con->next) {
797 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
798 ListBase targets = {NULL, NULL};
799 bConstraintTarget *ct;
801 /* constraint targets */
802 if (cti && cti->get_constraint_targets) {
803 cti->get_constraint_targets(con, &targets);
805 for (ct= targets.first; ct; ct= ct->next) {
806 if (ct->tar == srcArm) {
807 if (ct->subtarget[0] == '\0') {
810 else if (strcmp(ct->subtarget, pchan->name)==0) {
812 strcpy(ct->subtarget, curbone->name);
817 if (cti->flush_constraint_targets)
818 cti->flush_constraint_targets(con, &targets, 0);
821 /* action constraint? */
822 if (con->type == CONSTRAINT_TYPE_ACTION) {
823 bActionConstraint *data= con->data; // XXX old animation system
825 bActionChannel *achan;
830 for (achan= act->chanbase.first; achan; achan= achan->next) {
831 if (strcmp(achan->name, pchan->name)==0)
832 BLI_strncpy(achan->name, curbone->name, sizeof(achan->name));
841 /* fix object-level constraints */
843 for (con= ob->constraints.first; con; con= con->next) {
844 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
845 ListBase targets = {NULL, NULL};
846 bConstraintTarget *ct;
848 /* constraint targets */
849 if (cti && cti->get_constraint_targets) {
850 cti->get_constraint_targets(con, &targets);
852 for (ct= targets.first; ct; ct= ct->next) {
853 if (ct->tar == srcArm) {
854 if (ct->subtarget[0] == '\0') {
857 else if (strcmp(ct->subtarget, pchan->name)==0) {
859 strcpy(ct->subtarget, curbone->name);
864 if (cti->flush_constraint_targets)
865 cti->flush_constraint_targets(con, &targets, 0);
870 /* See if an object is parented to this armature */
871 if (ob->parent && (ob->parent == srcArm)) {
872 /* Is object parented to a bone of this src armature? */
873 if (ob->partype==PARBONE) {
874 /* bone name in object */
875 if (!strcmp(ob->parsubstr, pchan->name))
876 BLI_strncpy(ob->parsubstr, curbone->name, sizeof(ob->parsubstr));
879 /* make tar armature be new parent */
885 /* join armature exec is exported for use in object->join objects operator... */
886 int join_armature_exec(bContext *C, wmOperator *UNUSED(op))
888 Main *bmain= CTX_data_main(C);
889 Scene *scene= CTX_data_scene(C);
890 Object *ob= CTX_data_active_object(C);
891 bArmature *arm= (ob)? ob->data: NULL;
893 bPoseChannel *pchan, *pchann;
895 float mat[4][4], oimat[4][4];
897 /* Ensure we're not in editmode and that the active object is an armature*/
898 if (!ob || ob->type!=OB_ARMATURE)
899 return OPERATOR_CANCELLED;
900 if (!arm || arm->edbo)
901 return OPERATOR_CANCELLED;
903 /* Get editbones of active armature to add editbones to */
904 ED_armature_to_edit(ob);
906 /* get pose of active object and move it out of posemode */
908 ob->mode &= ~OB_MODE_POSE;
910 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
911 if ((base->object->type==OB_ARMATURE) && (base->object!=ob)) {
912 bArmature *curarm= base->object->data;
914 /* Make a list of editbones in current armature */
915 ED_armature_to_edit(base->object);
917 /* Get Pose of current armature */
918 opose= base->object->pose;
919 base->object->mode &= ~OB_MODE_POSE;
920 //BASACT->flag &= ~OB_MODE_POSE;
922 /* Find the difference matrix */
923 invert_m4_m4(oimat, ob->obmat);
924 mul_m4_m4m4(mat, base->object->obmat, oimat);
926 /* Copy bones and posechannels from the object to the edit armature */
927 for (pchan=opose->chanbase.first; pchan; pchan=pchann) {
929 curbone= editbone_name_exists(curarm->edbo, pchan->name);
932 unique_editbone_name(arm->edbo, curbone->name, NULL);
934 /* Transform the bone */
944 sub_v3_v3v3(delta, curbone->tail, curbone->head);
945 vec_roll_to_mat3(delta, curbone->roll, temp);
947 unit_m4(premat); /* Mat4MulMat34 only sets 3x3 part */
948 mul_m4_m3m4(premat, temp, mat);
950 mul_m4_v3(mat, curbone->head);
951 mul_m4_v3(mat, curbone->tail);
953 /* Get the postmat */
954 sub_v3_v3v3(delta, curbone->tail, curbone->head);
955 vec_roll_to_mat3(delta, curbone->roll, temp);
956 copy_m4_m3(postmat, temp);
959 invert_m4_m4(imat, premat);
960 mul_m4_m4m4(difmat, postmat, imat);
962 curbone->roll -= (float)atan2(difmat[2][0], difmat[2][2]);
965 /* Fix Constraints and Other Links to this Bone and Armature */
966 joined_armature_fix_links(ob, base->object, pchan, curbone);
969 BLI_strncpy(pchan->name, curbone->name, sizeof(pchan->name));
972 BLI_remlink(curarm->edbo, curbone);
973 BLI_addtail(arm->edbo, curbone);
975 BLI_remlink(&opose->chanbase, pchan);
976 BLI_addtail(&pose->chanbase, pchan);
977 free_pose_channels_hash(opose);
978 free_pose_channels_hash(pose);
981 ED_base_object_free_and_unlink(bmain, scene, base);
986 DAG_scene_sort(bmain, scene); // because we removed object(s)
988 ED_armature_from_edit(ob);
989 ED_armature_edit_free(ob);
991 WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
993 return OPERATOR_FINISHED;
996 /* ---------------------- */
998 /* Helper function for armature separating - link fixing */
999 static void separated_armature_fix_links(Object *origArm, Object *newArm)
1002 bPoseChannel *pchan;
1004 ListBase *opchans, *npchans;
1006 /* get reference to list of bones in original and new armatures */
1007 opchans= &origArm->pose->chanbase;
1008 npchans= &newArm->pose->chanbase;
1010 /* let's go through all objects in database */
1011 for (ob= G.main->object.first; ob; ob= ob->id.next) {
1012 /* do some object-type specific things */
1013 if (ob->type == OB_ARMATURE) {
1014 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1015 for (con= pchan->constraints.first; con; con= con->next) {
1016 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1017 ListBase targets = {NULL, NULL};
1018 bConstraintTarget *ct;
1020 /* constraint targets */
1021 if (cti && cti->get_constraint_targets) {
1022 cti->get_constraint_targets(con, &targets);
1024 for (ct= targets.first; ct; ct= ct->next) {
1025 /* any targets which point to original armature are redirected to the new one only if:
1026 * - the target isn't origArm/newArm itself
1027 * - the target is one that can be found in newArm/origArm
1029 if (ct->subtarget[0] != 0) {
1030 if (ct->tar == origArm) {
1031 if(BLI_findstring(npchans, ct->subtarget, offsetof(bPoseChannel, name))) {
1035 else if (ct->tar == newArm) {
1036 if(BLI_findstring(opchans, ct->subtarget, offsetof(bPoseChannel, name))) {
1043 if (cti->flush_constraint_targets) {
1044 cti->flush_constraint_targets(con, &targets, 0);
1051 /* fix object-level constraints */
1052 if (ob != origArm) {
1053 for (con= ob->constraints.first; con; con= con->next) {
1054 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1055 ListBase targets = {NULL, NULL};
1056 bConstraintTarget *ct;
1058 /* constraint targets */
1059 if (cti && cti->get_constraint_targets) {
1060 cti->get_constraint_targets(con, &targets);
1062 for (ct= targets.first; ct; ct= ct->next) {
1063 /* any targets which point to original armature are redirected to the new one only if:
1064 * - the target isn't origArm/newArm itself
1065 * - the target is one that can be found in newArm/origArm
1067 if(ct->subtarget[0] != '\0') {
1068 if (ct->tar == origArm) {
1069 if(BLI_findstring(npchans, ct->subtarget, offsetof(bPoseChannel, name))) {
1073 else if (ct->tar == newArm) {
1074 if(BLI_findstring(opchans, ct->subtarget, offsetof(bPoseChannel, name))) {
1081 if (cti->flush_constraint_targets) {
1082 cti->flush_constraint_targets(con, &targets, 0);
1088 /* See if an object is parented to this armature */
1089 if (ob->parent && (ob->parent == origArm)) {
1090 /* Is object parented to a bone of this src armature? */
1091 if ((ob->partype == PARBONE) && (ob->parsubstr[0] != '\0')) {
1092 if(BLI_findstring(npchans, ob->parsubstr, offsetof(bPoseChannel, name))) {
1100 /* Helper function for armature separating - remove certain bones from the given armature
1101 * sel: remove selected bones from the armature, otherwise the unselected bones are removed
1102 * (ob is not in editmode)
1104 static void separate_armature_bones(Object *ob, short sel)
1106 bArmature *arm= (bArmature *)ob->data;
1107 bPoseChannel *pchan, *pchann;
1110 /* make local set of editbones to manipulate here */
1111 ED_armature_to_edit(ob);
1113 /* go through pose-channels, checking if a bone should be removed */
1114 for (pchan=ob->pose->chanbase.first; pchan; pchan=pchann) {
1115 pchann= pchan->next;
1116 curbone= editbone_name_exists(arm->edbo, pchan->name);
1118 /* check if bone needs to be removed */
1119 if ( (sel && (curbone->flag & BONE_SELECTED)) ||
1120 (!sel && !(curbone->flag & BONE_SELECTED)) )
1125 /* clear the bone->parent var of any bone that had this as its parent */
1126 for (ebo= arm->edbo->first; ebo; ebo= ebo->next) {
1127 if (ebo->parent == curbone) {
1129 ebo->temp= NULL; /* this is needed to prevent random crashes with in ED_armature_from_edit */
1130 ebo->flag &= ~BONE_CONNECTED;
1134 /* clear the pchan->parent var of any pchan that had this as its parent */
1135 for (pchn= ob->pose->chanbase.first; pchn; pchn=pchn->next) {
1136 if (pchn->parent == pchan)
1140 /* free any of the extra-data this pchan might have */
1141 free_pose_channel(pchan);
1142 free_pose_channels_hash(ob->pose);
1144 /* get rid of unneeded bone */
1145 bone_free(arm, curbone);
1146 BLI_freelinkN(&ob->pose->chanbase, pchan);
1150 /* exit editmode (recalculates pchans too) */
1151 ED_armature_from_edit(ob);
1152 ED_armature_edit_free(ob);
1155 /* separate selected bones into their armature */
1156 static int separate_armature_exec (bContext *C, wmOperator *UNUSED(op))
1158 Main *bmain= CTX_data_main(C);
1159 Scene *scene= CTX_data_scene(C);
1160 Object *obedit= CTX_data_edit_object(C);
1161 Object *oldob, *newob;
1162 Base *oldbase, *newbase;
1166 return OPERATOR_CANCELLED;
1168 /* set wait cursor in case this takes a while */
1171 /* we are going to do this as follows (unlike every other instance of separate):
1172 * 1. exit editmode +posemode for active armature/base. Take note of what this is.
1173 * 2. duplicate base - BASACT is the new one now
1174 * 3. for each of the two armatures, enter editmode -> remove appropriate bones -> exit editmode + recalc
1175 * 4. fix constraint links
1176 * 5. make original armature active and enter editmode
1179 /* 1) only edit-base selected */
1180 // TODO: use context iterators for this?
1181 CTX_DATA_BEGIN(C, Base *, base, visible_bases) {
1182 if (base->object==obedit) base->flag |= 1;
1183 else base->flag &= ~1;
1187 /* 1) store starting settings and exit editmode */
1190 oldob->mode &= ~OB_MODE_POSE;
1191 //oldbase->flag &= ~OB_POSEMODE;
1193 ED_armature_from_edit(obedit);
1194 ED_armature_edit_free(obedit);
1196 /* 2) duplicate base */
1197 newbase= ED_object_add_duplicate(bmain, scene, oldbase, USER_DUP_ARM); /* only duplicate linked armature */
1198 newob= newbase->object;
1199 newbase->flag &= ~SELECT;
1202 /* 3) remove bones that shouldn't still be around on both armatures */
1203 separate_armature_bones(oldob, 1);
1204 separate_armature_bones(newob, 0);
1207 /* 4) fix links before depsgraph flushes */ // err... or after?
1208 separated_armature_fix_links(oldob, newob);
1210 DAG_id_tag_update(&oldob->id, OB_RECALC_DATA); /* this is the original one */
1211 DAG_id_tag_update(&newob->id, OB_RECALC_DATA); /* this is the separated one */
1214 /* 5) restore original conditions */
1217 ED_armature_to_edit(obedit);
1219 /* note, notifier might evolve */
1220 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, obedit);
1222 /* recalc/redraw + cleanup */
1225 return OPERATOR_FINISHED;
1228 void ARMATURE_OT_separate (wmOperatorType *ot)
1231 ot->name= "Separate Bones";
1232 ot->idname= "ARMATURE_OT_separate";
1233 ot->description= "Isolate selected bones into a separate armature";
1236 ot->invoke= WM_operator_confirm;
1237 ot->exec= separate_armature_exec;
1238 ot->poll= ED_operator_editarmature;
1241 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1244 /* **************** END tools on Editmode Armature **************** */
1245 /* **************** PoseMode & EditMode *************************** */
1247 /* only for opengl selection indices */
1248 Bone *get_indexed_bone (Object *ob, int index)
1250 bPoseChannel *pchan;
1251 if(ob->pose==NULL) return NULL;
1252 index>>=16; // bone selection codes use left 2 bytes
1254 pchan= BLI_findlink(&ob->pose->chanbase, index);
1255 return pchan ? pchan->bone : NULL;
1258 /* See if there are any selected bones in this buffer */
1259 /* only bones from base are checked on */
1260 static void *get_bone_from_selectbuffer(Scene *scene, Base *base, unsigned int *buffer, short hits, short findunsel)
1262 Object *obedit= scene->obedit; // XXX get from context
1265 void *firstunSel=NULL, *firstSel=NULL, *data;
1266 unsigned int hitresult;
1267 short i, takeNext=0, sel;
1269 for (i=0; i< hits; i++){
1270 hitresult = buffer[3+(i*4)];
1272 if (!(hitresult & BONESEL_NOSEL)) { // -1
1273 if(hitresult & BONESEL_ANY) { // to avoid including objects in selection
1275 hitresult &= ~(BONESEL_ANY);
1276 /* Determine what the current bone is */
1277 if (obedit==NULL || base->object!=obedit) {
1278 /* no singular posemode, so check for correct object */
1279 if(base->selcol == (hitresult & 0xFFFF)) {
1280 bone = get_indexed_bone(base->object, hitresult);
1283 sel = (bone->flag & BONE_SELECTED);
1285 sel = !(bone->flag & BONE_SELECTED);
1295 bArmature *arm= obedit->data;
1297 ebone = BLI_findlink(arm->edbo, hitresult);
1299 sel = (ebone->flag & BONE_SELECTED);
1301 sel = !(ebone->flag & BONE_SELECTED);
1308 if(!firstSel) firstSel= data;
1330 /* used by posemode as well editmode */
1331 /* only checks scene->basact! */
1332 /* x and y are mouse coords (area space) */
1333 static void *get_nearest_bone (bContext *C, short findunsel, int x, int y)
1337 unsigned int buffer[MAXPICKBUF];
1340 view3d_set_viewcontext(C, &vc);
1342 // rect.xmin= ... mouseco!
1343 rect.xmin= rect.xmax= x;
1344 rect.ymin= rect.ymax= y;
1347 hits= view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect);
1350 return get_bone_from_selectbuffer(vc.scene, vc.scene->basact, buffer, hits, findunsel);
1355 /* Get the first available child of an editbone */
1356 static EditBone *editbone_get_child(bArmature *arm, EditBone *pabone, short use_visibility)
1358 EditBone *curbone, *chbone=NULL;
1360 for (curbone= arm->edbo->first; curbone; curbone= curbone->next) {
1361 if (curbone->parent == pabone) {
1362 if (use_visibility) {
1363 if ((arm->layer & curbone->layer) && !(pabone->flag & BONE_HIDDEN_A)) {
1375 /* **************** END PoseMode & EditMode *************************** */
1376 /* **************** Posemode stuff ********************** */
1379 static void selectconnected_posebonechildren (Object *ob, Bone *bone, int extend)
1383 /* stop when unconnected child is encontered, or when unselectable bone is encountered */
1384 if (!(bone->flag & BONE_CONNECTED) || (bone->flag & BONE_UNSELECTABLE))
1387 // XXX old cruft! use notifiers instead
1388 //select_actionchannel_by_name (ob->action, bone->name, !(shift));
1391 bone->flag &= ~BONE_SELECTED;
1393 bone->flag |= BONE_SELECTED;
1395 for (curBone=bone->childbase.first; curBone; curBone=curBone->next)
1396 selectconnected_posebonechildren(ob, curBone, extend);
1399 /* within active object context */
1400 /* previously known as "selectconnected_posearmature" */
1401 static int pose_select_connected_invoke(bContext *C, wmOperator *op, wmEvent *event)
1403 Object *ob= CTX_data_edit_object(C);
1404 Bone *bone, *curBone, *next= NULL;
1405 int extend= RNA_boolean_get(op->ptr, "extend");
1407 view3d_operator_needs_opengl(C);
1410 bone= get_nearest_bone(C, 0, event->mval[0], event->mval[1]);
1412 bone= get_nearest_bone(C, 1, event->mval[0], event->mval[1]);
1415 return OPERATOR_CANCELLED;
1417 /* Select parents */
1418 for (curBone=bone; curBone; curBone=next){
1419 /* ignore bone if cannot be selected */
1420 if ((curBone->flag & BONE_UNSELECTABLE) == 0) {
1421 // XXX old cruft! use notifiers instead
1422 //select_actionchannel_by_name (ob->action, curBone->name, !(shift));
1425 curBone->flag &= ~BONE_SELECTED;
1427 curBone->flag |= BONE_SELECTED;
1429 if (curBone->flag & BONE_CONNECTED)
1430 next=curBone->parent;
1438 /* Select children */
1439 for (curBone=bone->childbase.first; curBone; curBone=next)
1440 selectconnected_posebonechildren(ob, curBone, extend);
1442 WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
1444 return OPERATOR_FINISHED;
1447 static int pose_select_linked_poll(bContext *C)
1449 return ( ED_operator_view3d_active(C) && ED_operator_posemode(C) );
1452 void POSE_OT_select_linked(wmOperatorType *ot)
1455 ot->name= "Select Connected";
1456 ot->idname= "POSE_OT_select_linked";
1457 ot->description= "Select bones related to selected ones by parent/child relationships";
1461 ot->invoke= pose_select_connected_invoke;
1462 ot->poll= pose_select_linked_poll;
1465 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1468 RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first.");
1471 /* **************** END Posemode stuff ********************** */
1472 /* **************** EditMode stuff ********************** */
1474 /* called in space.c */
1475 /* previously "selectconnected_armature" */
1476 static int armature_select_linked_invoke(bContext *C, wmOperator *op, wmEvent *event)
1479 EditBone *bone, *curBone, *next;
1480 int extend= RNA_boolean_get(op->ptr, "extend");
1481 Object *obedit= CTX_data_edit_object(C);
1484 view3d_operator_needs_opengl(C);
1487 bone= get_nearest_bone(C, 0, event->mval[0], event->mval[1]);
1489 bone= get_nearest_bone(C, 1, event->mval[0], event->mval[1]);
1492 return OPERATOR_CANCELLED;
1494 /* Select parents */
1495 for (curBone=bone; curBone; curBone=next) {
1496 if ((curBone->flag & BONE_UNSELECTABLE) == 0) {
1498 curBone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1501 curBone->flag |= (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1505 if (curBone->flag & BONE_CONNECTED)
1506 next=curBone->parent;
1511 /* Select children */
1513 for (curBone=arm->edbo->first; curBone; curBone=next) {
1514 next = curBone->next;
1515 if ((curBone->parent == bone) && (curBone->flag & BONE_UNSELECTABLE)==0) {
1516 if (curBone->flag & BONE_CONNECTED) {
1518 curBone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1520 curBone->flag |= (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1534 ED_armature_sync_selection(arm->edbo);
1536 WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, obedit);
1538 return OPERATOR_FINISHED;
1541 static int armature_select_linked_poll(bContext *C)
1543 return ( ED_operator_view3d_active(C) && ED_operator_editarmature(C) );
1546 void ARMATURE_OT_select_linked(wmOperatorType *ot)
1549 ot->name= "Select Connected";
1550 ot->idname= "ARMATURE_OT_select_linked";
1551 ot->description= "Select bones related to selected ones by parent/child relationships";
1555 ot->invoke= armature_select_linked_invoke;
1556 ot->poll= armature_select_linked_poll;
1559 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1562 RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first.");
1565 /* does bones and points */
1566 /* note that BONE ROOT only gets drawn for root bones (or without IK) */
1567 static EditBone *get_nearest_editbonepoint (ViewContext *vc, const int mval[2], ListBase *edbo, int findunsel, int *selmask)
1571 unsigned int buffer[MAXPICKBUF];
1572 unsigned int hitresult, besthitresult=BONESEL_NOSEL;
1578 rect.xmin= mval[0]-5;
1579 rect.xmax= mval[0]+5;
1580 rect.ymin= mval[1]-5;
1581 rect.ymax= mval[1]+5;
1583 hits= view3d_opengl_select(vc, buffer, MAXPICKBUF, &rect);
1585 rect.xmin= mval[0]-12;
1586 rect.xmax= mval[0]+12;
1587 rect.ymin= mval[1]-12;
1588 rect.ymax= mval[1]+12;
1589 hits= view3d_opengl_select(vc, buffer, MAXPICKBUF, &rect);
1591 /* See if there are any selected bones in this group */
1595 if (!(buffer[3] & BONESEL_NOSEL))
1596 besthitresult= buffer[3];
1599 for (i=0; i< hits; i++) {
1600 hitresult= buffer[3+(i*4)];
1601 if (!(hitresult & BONESEL_NOSEL)) {
1604 ebone = BLI_findlink(edbo, hitresult & ~BONESEL_ANY);
1606 /* clicks on bone points get advantage */
1607 if( hitresult & (BONESEL_ROOT|BONESEL_TIP)) {
1608 /* but also the unselected one */
1610 if( (hitresult & BONESEL_ROOT) && (ebone->flag & BONE_ROOTSEL)==0)
1612 else if( (hitresult & BONESEL_TIP) && (ebone->flag & BONE_TIPSEL)==0)
1622 if((ebone->flag & BONE_SELECTED)==0)
1631 besthitresult= hitresult;
1637 if (!(besthitresult & BONESEL_NOSEL)) {
1639 ebone= BLI_findlink(edbo, besthitresult & ~BONESEL_ANY);
1642 if (besthitresult & BONESEL_ROOT)
1643 *selmask |= BONE_ROOTSEL;
1644 if (besthitresult & BONESEL_TIP)
1645 *selmask |= BONE_TIPSEL;
1646 if (besthitresult & BONESEL_BONE)
1647 *selmask |= BONE_SELECTED;
1655 /* previously delete_armature */
1656 /* only editmode! */
1657 static int armature_delete_selected_exec(bContext *C, wmOperator *UNUSED(op))
1660 EditBone *curBone, *ebone_next;
1662 Object *obedit= CTX_data_edit_object(C); // XXX get from context
1665 /* cancel if nothing selected */
1666 if (CTX_DATA_COUNT(C, selected_bones) == 0)
1667 return OPERATOR_CANCELLED;
1669 armature_select_mirrored(arm);
1671 /* First erase any associated pose channel */
1673 bPoseChannel *pchan, *pchan_next;
1674 for (pchan=obedit->pose->chanbase.first; pchan; pchan= pchan_next) {
1675 pchan_next= pchan->next;
1676 curBone = editbone_name_exists(arm->edbo, pchan->name);
1678 if (curBone && (curBone->flag & BONE_SELECTED) && (arm->layer & curBone->layer)) {
1679 free_pose_channel(pchan);
1680 free_pose_channels_hash(obedit->pose);
1681 BLI_freelinkN (&obedit->pose->chanbase, pchan);
1684 for (con= pchan->constraints.first; con; con= con->next) {
1685 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1686 ListBase targets = {NULL, NULL};
1687 bConstraintTarget *ct;
1689 if (cti && cti->get_constraint_targets) {
1690 cti->get_constraint_targets(con, &targets);
1692 for (ct= targets.first; ct; ct= ct->next) {
1693 if (ct->tar == obedit) {
1694 if (ct->subtarget[0]) {
1695 curBone = editbone_name_exists(arm->edbo, ct->subtarget);
1696 if (curBone && (curBone->flag & BONE_SELECTED) && (arm->layer & curBone->layer)) {
1697 con->flag |= CONSTRAINT_DISABLE;
1698 ct->subtarget[0]= 0;
1704 if (cti->flush_constraint_targets)
1705 cti->flush_constraint_targets(con, &targets, 0);
1713 for (curBone=arm->edbo->first; curBone; curBone= ebone_next) {
1714 ebone_next= curBone->next;
1715 if (arm->layer & curBone->layer) {
1716 if (curBone->flag & BONE_SELECTED) {
1717 if(curBone==arm->act_edbone) arm->act_edbone= NULL;
1718 ED_armature_edit_bone_remove(arm, curBone);
1724 ED_armature_sync_selection(arm->edbo);
1726 WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, obedit);
1728 return OPERATOR_FINISHED;
1731 void ARMATURE_OT_delete(wmOperatorType *ot)
1734 ot->name= "Delete Selected Bone(s)";
1735 ot->idname= "ARMATURE_OT_delete";
1736 ot->description= "Remove selected bones from the armature";
1739 ot->invoke = WM_operator_confirm;
1740 ot->exec = armature_delete_selected_exec;
1741 ot->poll = ED_operator_editarmature;
1744 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1747 /* toggle==0: deselect
1748 * toggle==1: swap (based on test)
1749 * toggle==2: swap (no test), CURRENTLY UNUSED
1751 void ED_armature_deselect_all(Object *obedit, int toggle)
1753 bArmature *arm= obedit->data;
1758 /* Determine if there are any selected bones
1759 And therefore whether we are selecting or deselecting */
1760 for (eBone=arm->edbo->first;eBone;eBone=eBone->next){
1761 // if(arm->layer & eBone->layer) {
1762 if (eBone->flag & (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL)){
1772 for (eBone=arm->edbo->first;eBone;eBone=eBone->next) {
1774 /* invert selection of bone */
1775 if(EBONE_VISIBLE(arm, eBone)) {
1776 eBone->flag ^= (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1777 if(arm->act_edbone==eBone)
1778 arm->act_edbone= NULL;
1783 if(EBONE_VISIBLE(arm, eBone)) {
1784 eBone->flag |= (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1786 eBone->parent->flag |= (BONE_TIPSEL);
1791 eBone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1792 if(arm->act_edbone==eBone)
1793 arm->act_edbone= NULL;
1797 ED_armature_sync_selection(arm->edbo);
1800 void ED_armature_deselect_all_visible(Object *obedit)
1802 bArmature *arm= obedit->data;
1805 for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
1806 /* first and foremost, bone must be visible and selected */
1807 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_UNSELECTABLE)==0) {
1808 ebone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1812 ED_armature_sync_selection(arm->edbo);
1815 /* accounts for connected parents */
1816 static int ebone_select_flag(EditBone *ebone)
1818 if(ebone->parent && (ebone->flag & BONE_CONNECTED)) {
1819 return ((ebone->parent->flag & BONE_TIPSEL) ? BONE_ROOTSEL : 0) | (ebone->flag & (BONE_SELECTED|BONE_TIPSEL));
1822 return ebone->flag & (BONE_SELECTED|BONE_ROOTSEL|BONE_TIPSEL);
1826 /* context: editmode armature in view3d */
1827 int mouse_armature(bContext *C, const int mval[2], int extend)
1829 Object *obedit= CTX_data_edit_object(C);
1830 bArmature *arm= obedit->data;
1832 EditBone *nearBone = NULL;
1835 view3d_set_viewcontext(C, &vc);
1837 BIF_sk_selectStroke(C, mval, extend);
1839 nearBone= get_nearest_editbonepoint(&vc, mval, arm->edbo, 1, &selmask);
1843 ED_armature_deselect_all(obedit, 0);
1845 /* by definition the non-root connected bones have no root point drawn,
1846 so a root selection needs to be delivered to the parent tip */
1848 if(selmask & BONE_SELECTED) {
1849 if(nearBone->parent && (nearBone->flag & BONE_CONNECTED)) {
1850 /* click in a chain */
1852 /* hold shift inverts this bone's selection */
1853 if(nearBone->flag & BONE_SELECTED) {
1854 /* deselect this bone */
1855 nearBone->flag &= ~(BONE_TIPSEL|BONE_SELECTED);
1856 /* only deselect parent tip if it is not selected */
1857 if(!(nearBone->parent->flag & BONE_SELECTED))
1858 nearBone->parent->flag &= ~BONE_TIPSEL;
1861 /* select this bone */
1862 nearBone->flag |= BONE_TIPSEL;
1863 nearBone->parent->flag |= BONE_TIPSEL;
1867 /* select this bone */
1868 nearBone->flag |= BONE_TIPSEL;
1869 nearBone->parent->flag |= BONE_TIPSEL;
1874 /* hold shift inverts this bone's selection */
1875 if(nearBone->flag & BONE_SELECTED)
1876 nearBone->flag &= ~(BONE_TIPSEL|BONE_ROOTSEL);
1878 nearBone->flag |= (BONE_TIPSEL|BONE_ROOTSEL);
1880 else nearBone->flag |= (BONE_TIPSEL|BONE_ROOTSEL);
1884 if (extend && (nearBone->flag & selmask))
1885 nearBone->flag &= ~selmask;
1887 nearBone->flag |= selmask;
1890 ED_armature_sync_selection(arm->edbo);
1893 /* then now check for active status */
1894 if(ebone_select_flag(nearBone)) {
1895 arm->act_edbone= nearBone;
1899 WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, vc.obedit);
1906 void ED_armature_edit_free(struct Object *ob)
1908 bArmature *arm= ob->data;
1911 /* Clear the editbones list */
1913 if (arm->edbo->first) {
1914 for (eBone=arm->edbo->first; eBone; eBone=eBone->next) {
1916 IDP_FreeProperty(eBone->prop);
1917 MEM_freeN(eBone->prop);
1921 BLI_freelistN(arm->edbo);
1923 MEM_freeN(arm->edbo);
1928 /* Put armature in EditMode */
1929 void ED_armature_to_edit(Object *ob)
1931 bArmature *arm= ob->data;
1933 ED_armature_edit_free(ob);
1934 arm->edbo= MEM_callocN(sizeof(ListBase), "edbo armature");
1935 arm->act_edbone= make_boneList(arm->edbo, &arm->bonebase, NULL, arm->act_bone);
1936 arm->act_bone= NULL;
1938 // BIF_freeTemplates(); /* force template update when entering editmode */
1942 /* adjust bone roll to align Z axis with vector
1943 * vec is in local space and is normalized
1946 float ED_rollBoneToVector(EditBone *bone, const float align_axis[3], const short axis_only)
1948 float mat[3][3], nor[3];
1950 sub_v3_v3v3(nor, bone->tail, bone->head);
1951 vec_roll_to_mat3(nor, 0.0f, mat);
1953 /* check the bone isnt aligned with the axis */
1954 if(!is_zero_v3(align_axis) && angle_v3v3(align_axis, mat[2]) > FLT_EPSILON) {
1955 float vec[3], align_axis_proj[3], roll;
1957 /* project the new_up_axis along the normal */
1958 project_v3_v3v3(vec, align_axis, nor);
1959 sub_v3_v3v3(align_axis_proj, align_axis, vec);
1962 if(angle_v3v3(align_axis_proj, mat[2]) > (float)(M_PI/2.0)) {
1963 negate_v3(align_axis_proj);
1967 roll = angle_v3v3(align_axis_proj, mat[2]);
1969 cross_v3_v3v3(vec, mat[2], align_axis_proj);
1971 if (dot_v3v3(vec, nor) < 0) {
1982 static EnumPropertyItem prop_calc_roll_types[] = {
1983 {0, "X", 0, "X Axis", ""},
1984 {1, "Y", 0, "Y Axis", ""},
1985 {2, "Z", 0, "Z Axis", ""},
1986 {5, "ACTIVE", 0, "Active Bone", ""},
1987 {6, "VIEW", 0, "View Axis", ""},
1988 {7, "CURSOR", 0, "Cursor", ""},
1989 {0, NULL, 0, NULL, NULL}
1993 static int armature_calc_roll_exec(bContext *C, wmOperator *op)
1995 Object *ob= CTX_data_edit_object(C);
1996 const short type= RNA_enum_get(op->ptr, "type");
1997 const short axis_only= RNA_boolean_get(op->ptr, "axis_only");
1998 const short axis_flip= RNA_boolean_get(op->ptr, "axis_flip");
2002 bArmature *arm= ob->data;
2005 copy_m3_m4(imat, ob->obmat);
2008 if(type==7) { /* Cursor */
2009 Scene *scene= CTX_data_scene(C);
2010 View3D *v3d= CTX_wm_view3d(C); /* can be NULL */
2011 float cursor_local[3];
2012 float *cursor= give_cursor(scene, v3d);
2015 copy_v3_v3(cursor_local, cursor);
2016 mul_m3_v3(imat, cursor_local);
2019 for(ebone= arm->edbo->first; ebone; ebone= ebone->next) {
2020 if(EBONE_VISIBLE(arm, ebone) && EBONE_EDITABLE(ebone)) {
2021 float cursor_rel[3];
2022 sub_v3_v3v3(cursor_rel, cursor_local, ebone->head);
2023 if(axis_flip) negate_v3(cursor_rel);
2024 ebone->roll= ED_rollBoneToVector(ebone, cursor_rel, axis_only);
2029 float vec[3]= {0.0f, 0.0f, 0.0f};
2030 if(type==6) { /* View */
2031 RegionView3D *rv3d= CTX_wm_region_view3d(C);
2033 BKE_report(op->reports, RPT_ERROR, "No region view3d available");
2034 return OPERATOR_CANCELLED;
2037 copy_v3_v3(vec, rv3d->viewinv[2]);
2038 mul_m3_v3(imat, vec);
2041 float mat[3][3], nor[3];
2042 ebone= (EditBone *)arm->act_edbone;
2044 BKE_report(op->reports, RPT_ERROR, "No active bone set");
2045 return OPERATOR_CANCELLED;
2048 sub_v3_v3v3(nor, ebone->tail, ebone->head);
2049 vec_roll_to_mat3(nor, ebone->roll, mat);
2050 copy_v3_v3(vec, mat[2]);
2053 assert(type >= 0 && type <= 5);
2054 if(type<3) vec[type]= 1.0f;
2055 else vec[type-2]= -1.0f;
2056 mul_m3_v3(imat, vec);
2059 if(axis_flip) negate_v3(vec);
2061 for(ebone= arm->edbo->first; ebone; ebone= ebone->next) {
2062 if(EBONE_VISIBLE(arm, ebone) && EBONE_EDITABLE(ebone)) {
2063 /* roll func is a callback which assumes that all is well */
2064 ebone->roll= ED_rollBoneToVector(ebone, vec, axis_only);
2069 if (arm->flag & ARM_MIRROR_EDIT) {
2070 for(ebone= arm->edbo->first; ebone; ebone= ebone->next) {
2071 if((EBONE_VISIBLE(arm, ebone) && EBONE_EDITABLE(ebone)) == 0) {
2072 EditBone *ebone_mirr= ED_armature_bone_get_mirrored(arm->edbo, ebone);
2073 if (ebone_mirr && (EBONE_VISIBLE(arm, ebone_mirr) && EBONE_EDITABLE(ebone_mirr))) {
2074 ebone->roll= -ebone_mirr->roll;
2080 /* note, notifier might evolve */
2081 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
2083 return OPERATOR_FINISHED;
2086 void ARMATURE_OT_calculate_roll(wmOperatorType *ot)
2089 ot->name= "Recalculate Roll";
2090 ot->idname= "ARMATURE_OT_calculate_roll";
2091 ot->description= "Automatically fix alignment of select bones' axes";
2094 ot->invoke = WM_menu_invoke;
2095 ot->exec = armature_calc_roll_exec;
2096 ot->poll = ED_operator_editarmature;
2099 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2102 ot->prop= RNA_def_enum(ot->srna, "type", prop_calc_roll_types, 0, "Type", "");
2103 RNA_def_boolean(ot->srna, "axis_flip", 0, "Flip Axis", "Negate the alignment axis.");
2104 RNA_def_boolean(ot->srna, "axis_only", 0, "Shortest Rotation", "Ignore the axis direction, use the shortest rotation to align.");
2107 /* **************** undo for armatures ************** */
2109 typedef struct UndoArmature {
2110 EditBone *act_edbone;
2114 static void undoBones_to_editBones(void *uarmv, void *armv)
2116 UndoArmature *uarm= uarmv;
2117 bArmature *arm= armv;
2118 EditBone *ebo, *newebo;
2120 BLI_freelistN(arm->edbo);
2123 for(ebo= uarm->lb.first; ebo; ebo= ebo->next) {
2124 newebo= MEM_dupallocN(ebo);
2126 BLI_addtail(arm->edbo, newebo);
2130 if(uarm->act_edbone) {
2131 ebo= uarm->act_edbone;
2132 arm->act_edbone= ebo->temp;
2135 arm->act_edbone= NULL;
2138 for(newebo= arm->edbo->first; newebo; newebo= newebo->next) {
2139 if(newebo->parent) newebo->parent= newebo->parent->temp;
2141 /* be sure they dont hang ever */
2142 for(newebo= arm->edbo->first; newebo; newebo= newebo->next) {
2147 static void *editBones_to_undoBones(void *armv)
2149 bArmature *arm= armv;
2151 EditBone *ebo, *newebo;
2153 uarm= MEM_callocN(sizeof(UndoArmature), "listbase undo");
2156 for(ebo= arm->edbo->first; ebo; ebo= ebo->next) {
2157 newebo= MEM_dupallocN(ebo);
2159 BLI_addtail(&uarm->lb, newebo);
2163 if(arm->act_edbone) {
2164 ebo= arm->act_edbone;
2165 uarm->act_edbone= ebo->temp;
2169 for(newebo= uarm->lb.first; newebo; newebo= newebo->next) {
2170 if(newebo->parent) newebo->parent= newebo->parent->temp;
2176 static void free_undoBones(void *uarmv)
2178 UndoArmature *uarm= uarmv;
2180 BLI_freelistN(&uarm->lb);
2184 static void *get_armature_edit(bContext *C)
2186 Object *obedit= CTX_data_edit_object(C);
2187 if(obedit && obedit->type==OB_ARMATURE) {
2188 return obedit->data;
2193 /* and this is all the undo system needs to know */
2194 void undo_push_armature(bContext *C, const char *name)
2196 // XXX solve getdata()
2197 undo_editmode_push(C, name, get_armature_edit, free_undoBones, undoBones_to_editBones, editBones_to_undoBones, NULL);
2202 /* **************** END EditMode stuff ********************** */
2203 /* *************** Adding stuff in editmode *************** */
2205 /* default bone add, returns it selected, but without tail set */
2206 EditBone *ED_armature_edit_bone_add(bArmature *arm, const char *name)
2208 EditBone *bone= MEM_callocN(sizeof(EditBone), "eBone");
2210 BLI_strncpy(bone->name, name, sizeof(bone->name));
2211 unique_editbone_name(arm->edbo, bone->name, NULL);
2213 BLI_addtail(arm->edbo, bone);
2215 bone->flag |= BONE_TIPSEL;
2222 bone->rad_head= 0.10f;
2223 bone->rad_tail= 0.05f;
2225 bone->layer= arm->layer;
2230 /* v3d and rv3d are allowed to be NULL */
2231 void add_primitive_bone(Scene *scene, View3D *v3d, RegionView3D *rv3d)
2233 Object *obedit= scene->obedit; // XXX get from context
2234 bArmature *arm= obedit->data;
2235 float obmat[3][3], curs[3], viewmat[3][3], totmat[3][3], imat[3][3];
2238 /* Get inverse point for head and orientation for tail */
2239 invert_m4_m4(obedit->imat, obedit->obmat);
2240 mul_v3_m4v3(curs, obedit->imat, give_cursor(scene, v3d));
2242 if (rv3d && (U.flag & USER_ADD_VIEWALIGNED))
2243 copy_m3_m4(obmat, rv3d->viewmat);
2244 else unit_m3(obmat);
2246 copy_m3_m4(viewmat, obedit->obmat);
2247 mul_m3_m3m3(totmat, obmat, viewmat);
2248 invert_m3_m3(imat, totmat);
2250 ED_armature_deselect_all(obedit, 0);
2253 bone= ED_armature_edit_bone_add(arm, "Bone");
2255 arm->act_edbone= bone;
2257 copy_v3_v3(bone->head, curs);
2259 if (rv3d && (U.flag & USER_ADD_VIEWALIGNED))
2260 add_v3_v3v3(bone->tail, bone->head, imat[1]); // bone with unit length 1
2262 add_v3_v3v3(bone->tail, bone->head, imat[2]); // bone with unit length 1, pointing up Z
2267 /* previously addvert_armature */
2268 /* the ctrl-click method */
2269 static int armature_click_extrude_exec(bContext *C, wmOperator *UNUSED(op))
2273 EditBone *ebone, *newbone, *flipbone;
2274 float *curs, mat[3][3],imat[3][3];
2279 scene = CTX_data_scene(C);
2280 v3d= CTX_wm_view3d(C);
2281 obedit= CTX_data_edit_object(C);
2284 /* find the active or selected bone */
2285 for (ebone = arm->edbo->first; ebone; ebone=ebone->next) {
2286 if (EBONE_VISIBLE(arm, ebone)) {
2287 if (ebone->flag & BONE_TIPSEL || arm->act_edbone == ebone)
2293 for (ebone = arm->edbo->first; ebone; ebone=ebone->next) {
2294 if (EBONE_VISIBLE(arm, ebone)) {
2295 if (ebone->flag & BONE_ROOTSEL || arm->act_edbone == ebone)
2300 return OPERATOR_CANCELLED;
2305 ED_armature_deselect_all(obedit, 0);
2307 /* we re-use code for mirror editing... */
2309 if (arm->flag & ARM_MIRROR_EDIT)
2310 flipbone= ED_armature_bone_get_mirrored(arm->edbo, ebone);
2312 for (a=0; a<2; a++) {
2317 SWAP(EditBone *, flipbone, ebone);
2321 newbone= ED_armature_edit_bone_add(arm, ebone->name);
2322 arm->act_edbone= newbone;
2325 copy_v3_v3(newbone->head, ebone->head);
2326 newbone->rad_head= ebone->rad_tail;
2327 newbone->parent= ebone->parent;
2330 copy_v3_v3(newbone->head, ebone->tail);
2331 newbone->rad_head= ebone->rad_tail;
2332 newbone->parent= ebone;
2333 newbone->flag |= BONE_CONNECTED;
2336 curs= give_cursor(scene, v3d);
2337 copy_v3_v3(newbone->tail, curs);
2338 sub_v3_v3v3(newbone->tail, newbone->tail, obedit->obmat[3]);
2341 newbone->tail[0]= -newbone->tail[0];
2343 copy_m3_m4(mat, obedit->obmat);
2344 invert_m3_m3(imat, mat);
2345 mul_m3_v3(imat, newbone->tail);
2347 newbone->length= len_v3v3(newbone->head, newbone->tail);
2348 newbone->rad_tail= newbone->length*0.05f;
2349 newbone->dist= newbone->length*0.25f;
2353 ED_armature_sync_selection(arm->edbo);
2355 WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, obedit);
2357 return OPERATOR_FINISHED;
2360 static int armature_click_extrude_invoke(bContext *C, wmOperator *op, wmEvent *event)
2362 /* TODO most of this code is copied from set3dcursor_invoke,
2363 it would be better to reuse code in set3dcursor_invoke */
2365 /* temporarily change 3d cursor position */
2369 float *fp = NULL, tvec[3], oldcurs[3], mval_f[2];
2372 scene= CTX_data_scene(C);
2373 ar= CTX_wm_region(C);
2374 v3d = CTX_wm_view3d(C);
2376 fp= give_cursor(scene, v3d);
2378 copy_v3_v3(oldcurs, fp);
2380 VECCOPY2D(mval_f, event->mval);
2381 ED_view3d_win_to_3d(ar, fp, mval_f, tvec);
2382 copy_v3_v3(fp, tvec);
2384 /* extrude to the where new cursor is and store the operation result */
2385 retv= armature_click_extrude_exec(C, op);
2387 /* restore previous 3d cursor position */
2388 copy_v3_v3(fp, oldcurs);
2393 void ARMATURE_OT_click_extrude(wmOperatorType *ot)
2396 ot->name= "Click-Extrude";
2397 ot->idname= "ARMATURE_OT_click_extrude";
2398 ot->description= "Create a new bone going from the last selected joint to the mouse position";
2401 ot->invoke = armature_click_extrude_invoke;
2402 ot->exec = armature_click_extrude_exec;
2403 ot->poll = ED_operator_editarmature;
2406 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2411 /* adds an EditBone between the nominated locations (should be in the right space) */
2412 static EditBone *add_points_bone (Object *obedit, float head[], float tail[])
2416 ebo= ED_armature_edit_bone_add(obedit->data, "Bone");
2418 copy_v3_v3(ebo->head, head);
2419 copy_v3_v3(ebo->tail, tail);
2425 static EditBone *get_named_editbone(ListBase *edbo, char *name)
2430 for (eBone=edbo->first; eBone; eBone=eBone->next) {
2431 if (!strcmp(name, eBone->name))
2439 /* Call this before doing any duplications
2441 void preEditBoneDuplicate(ListBase *editbones)
2446 for (eBone = editbones->first; eBone; eBone = eBone->next)
2453 * Note: When duplicating cross objects, editbones here is the list of bones
2454 * from the SOURCE object but ob is the DESTINATION object
2456 void updateDuplicateSubtargetObjects(EditBone *dupBone, ListBase *editbones, Object *src_ob, Object *dst_ob)
2458 /* If an edit bone has been duplicated, lets
2459 * update it's constraints if the subtarget
2460 * they point to has also been duplicated
2462 EditBone *oldtarget, *newtarget;
2463 bPoseChannel *pchan;
2464 bConstraint *curcon;
2467 if ( (pchan = verify_pose_channel(dst_ob->pose, dupBone->name)) ) {
2468 if ( (conlist = &pchan->constraints) ) {
2469 for (curcon = conlist->first; curcon; curcon=curcon->next) {
2470 /* does this constraint have a subtarget in
2473 bConstraintTypeInfo *cti= constraint_get_typeinfo(curcon);
2474 ListBase targets = {NULL, NULL};
2475 bConstraintTarget *ct;
2477 if (cti && cti->get_constraint_targets) {
2478 cti->get_constraint_targets(curcon, &targets);
2480 for (ct= targets.first; ct; ct= ct->next) {
2481 if ((ct->tar == src_ob) && (ct->subtarget[0])) {
2482 ct->tar = dst_ob; /* update target */
2483 oldtarget = get_named_editbone(editbones, ct->subtarget);
2485 /* was the subtarget bone duplicated too? If
2486 * so, update the constraint to point at the
2487 * duplicate of the old subtarget.
2489 if (oldtarget->temp) {
2490 newtarget = (EditBone *) oldtarget->temp;
2491 strcpy(ct->subtarget, newtarget->name);
2497 if (cti->flush_constraint_targets)
2498 cti->flush_constraint_targets(curcon, &targets, 0);
2505 void updateDuplicateSubtarget(EditBone *dupBone, ListBase *editbones, Object *ob)
2507 updateDuplicateSubtargetObjects(dupBone, editbones, ob, ob);
2511 EditBone *duplicateEditBoneObjects(EditBone *curBone, char *name, ListBase *editbones, Object *src_ob, Object *dst_ob)
2513 EditBone *eBone = MEM_mallocN(sizeof(EditBone), "addup_editbone");
2515 /* Copy data from old bone to new bone */
2516 memcpy(eBone, curBone, sizeof(EditBone));
2518 curBone->temp = eBone;
2519 eBone->temp = curBone;
2523 BLI_strncpy(eBone->name, name, sizeof(eBone->name));
2526 unique_editbone_name(editbones, eBone->name, NULL);
2527 BLI_addtail(editbones, eBone);
2529 /* copy the ID property */
2531 eBone->prop= IDP_CopyProperty(curBone->prop);
2533 /* Lets duplicate the list of constraints that the
2537 bPoseChannel *chanold, *channew;
2539 chanold = verify_pose_channel(src_ob->pose, curBone->name);
2541 /* WARNING: this creates a new posechannel, but there will not be an attached bone
2542 * yet as the new bones created here are still 'EditBones' not 'Bones'.
2544 channew= verify_pose_channel(dst_ob->pose, eBone->name);
2547 duplicate_pose_channel_data(channew, chanold);
2555 EditBone *duplicateEditBone(EditBone *curBone, char *name, ListBase *editbones, Object *ob)
2557 return duplicateEditBoneObjects(curBone, name, editbones, ob, ob);
2560 /* previously adduplicate_armature */
2561 static int armature_duplicate_selected_exec(bContext *C, wmOperator *UNUSED(op))
2564 EditBone *eBone = NULL;
2566 EditBone *firstDup=NULL; /* The beginning of the duplicated bones in the edbo list */
2568 Object *obedit= CTX_data_edit_object(C);
2571 /* cancel if nothing selected */
2572 if (CTX_DATA_COUNT(C, selected_bones) == 0)
2573 return OPERATOR_CANCELLED;
2575 ED_armature_sync_selection(arm->edbo); // XXX why is this needed?
2577 preEditBoneDuplicate(arm->edbo);
2579 /* Select mirrored bones */
2580 if (arm->flag & ARM_MIRROR_EDIT) {
2581 for (curBone=arm->edbo->first; curBone; curBone=curBone->next) {
2582 if (EBONE_VISIBLE(arm, curBone)) {
2583 if (curBone->flag & BONE_SELECTED) {
2584 eBone = ED_armature_bone_get_mirrored(arm->edbo, curBone);
2586 eBone->flag |= BONE_SELECTED;
2593 /* Find the selected bones and duplicate them as needed */
2594 for (curBone=arm->edbo->first; curBone && curBone!=firstDup; curBone=curBone->next) {
2595 if (EBONE_VISIBLE(arm, curBone)) {
2596 if (curBone->flag & BONE_SELECTED) {
2598 eBone= duplicateEditBone(curBone, curBone->name, arm->edbo, obedit);
2607 /* Run though the list and fix the pointers */
2608 for (curBone=arm->edbo->first; curBone && curBone!=firstDup; curBone=curBone->next) {
2609 if (EBONE_VISIBLE(arm, curBone)) {
2610 if (curBone->flag & BONE_SELECTED) {
2611 eBone=(EditBone*) curBone->temp;
2613 if (!curBone->parent) {
2614 /* If this bone has no parent,
2615 * Set the duplicate->parent to NULL
2617 eBone->parent = NULL;
2619 else if (curBone->parent->temp) {
2620 /* If this bone has a parent that was duplicated,
2621 * Set the duplicate->parent to the curBone->parent->temp
2623 eBone->parent= (EditBone *)curBone->parent->temp;
2626 /* If this bone has a parent that IS not selected,
2627 * Set the duplicate->parent to the curBone->parent
2629 eBone->parent=(EditBone*) curBone->parent;
2630 eBone->flag &= ~BONE_CONNECTED;
2633 /* Lets try to fix any constraint subtargets that might
2634 * have been duplicated
2636 updateDuplicateSubtarget(eBone, arm->edbo, obedit);
2641 /* correct the active bone */
2642 if(arm->act_edbone) {
2643 eBone= arm->act_edbone;
2645 arm->act_edbone= eBone->temp;
2648 /* Deselect the old bones and select the new ones */
2649 for (curBone=arm->edbo->first; curBone && curBone!=firstDup; curBone=curBone->next) {
2650 if (EBONE_VISIBLE(arm, curBone))
2651 curBone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
2654 ED_armature_validate_active(arm);
2656 WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, obedit);
2658 return OPERATOR_FINISHED;
2662 void ARMATURE_OT_duplicate(wmOperatorType *ot)
2665 ot->name= "Duplicate Selected Bone(s)";
2666 ot->idname= "ARMATURE_OT_duplicate";
2667 ot->description= "Make copies of the selected bones within the same armature";
2670 ot->exec = armature_duplicate_selected_exec;
2671 ot->poll = ED_operator_editarmature;
2674 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2678 /* *************** END Adding stuff in editmode *************** */
2679 /* ************** Add/Remove stuff in editmode **************** */
2681 /* temporary data-structure for merge/fill bones */
2682 typedef struct EditBonePoint {
2683 struct EditBonePoint *next, *prev;
2685 EditBone *head_owner; /* EditBone which uses this point as a 'head' point */
2686 EditBone *tail_owner; /* EditBone which uses this point as a 'tail' point */
2688 float vec[3]; /* the actual location of the point in local/EditMode space */
2691 /* find chain-tips (i.e. bones without children) */
2692 static void chains_find_tips (ListBase *edbo, ListBase *list)
2694 EditBone *curBone, *ebo;
2697 /* note: this is potentially very slow ... there's got to be a better way */
2698 for (curBone= edbo->first; curBone; curBone= curBone->next) {
2701 /* is this bone contained within any existing chain? (skip if so) */
2702 for (ld= list->first; ld; ld= ld->next) {
2703 for (ebo= ld->data; ebo; ebo= ebo->parent) {
2704 if (ebo == curBone) {
2712 /* skip current bone if it is part of an existing chain */
2715 /* is any existing chain part of the chain formed by this bone? */
2717 for (ebo= curBone->parent; ebo; ebo= ebo->parent) {
2718 for (ld= list->first; ld; ld= ld->next) {
2719 if (ld->data == ebo) {
2728 /* current bone has already been added to a chain? */
2731 /* add current bone to a new chain */
2732 ld= MEM_callocN(sizeof(LinkData), "BoneChain");
2734 BLI_addtail(list, ld);
2738 /* --------------------- */
2740 static void fill_add_joint (EditBone *ebo, short eb_tail, ListBase *points)
2747 copy_v3_v3(vec, ebo->tail);
2750 copy_v3_v3(vec, ebo->head);
2753 for (ebp= points->first; ebp; ebp= ebp->next) {
2754 if (equals_v3v3(ebp->vec, vec)) {
2756 if ((ebp->head_owner) && (ebp->head_owner->parent == ebo)) {
2757 /* so this bone's tail owner is this bone */
2758 ebp->tail_owner= ebo;
2764 if ((ebp->tail_owner) && (ebo->parent == ebp->tail_owner)) {
2765 /* so this bone's head owner is this bone */
2766 ebp->head_owner= ebo;
2774 /* allocate a new point if no existing point was related */
2776 ebp= MEM_callocN(sizeof(EditBonePoint), "EditBonePoint");
2779 copy_v3_v3(ebp->vec, ebo->tail);
2780 ebp->tail_owner= ebo;
2783 copy_v3_v3(ebp->vec, ebo->head);
2784 ebp->head_owner= ebo;
2787 BLI_addtail(points, ebp);
2791 /* bone adding between selected joints */
2792 static int armature_fill_bones_exec (bContext *C, wmOperator *op)
2794 Object *obedit= CTX_data_edit_object(C);
2795 bArmature *arm= (obedit) ? obedit->data : NULL;
2796 Scene *scene= CTX_data_scene(C);
2797 View3D *v3d= CTX_wm_view3d(C);
2798 ListBase points = {NULL, NULL};
2802 if (ELEM(NULL, obedit, arm))
2803 return OPERATOR_CANCELLED;
2805 /* loop over all bones, and only consider if visible */
2806 CTX_DATA_BEGIN(C, EditBone *, ebone, visible_bones)
2808 if (!(ebone->flag & BONE_CONNECTED) && (ebone->flag & BONE_ROOTSEL))
2809 fill_add_joint(ebone, 0, &points);
2810 if (ebone->flag & BONE_TIPSEL)
2811 fill_add_joint(ebone, 1, &points);
2815 /* the number of joints determines how we fill:
2816 * 1) between joint and cursor (joint=head, cursor=tail)
2817 * 2) between the two joints (order is dependent on active-bone/hierachy)
2818 * 3+) error (a smarter method involving finding chains needs to be worked out
2820 count= BLI_countlist(&points);
2823 BKE_report(op->reports, RPT_ERROR, "No joints selected");
2824 return OPERATOR_CANCELLED;
2826 else if (count == 1) {
2830 /* Get Points - selected joint */
2831 ebp= (EditBonePoint *)points.first;
2833 /* Get points - cursor (tail) */
2834 invert_m4_m4(obedit->imat, obedit->obmat);
2835 mul_v3_m4v3(curs, obedit->imat, give_cursor(scene, v3d));
2838 /* newbone= */ add_points_bone(obedit, ebp->vec, curs);
2840 else if (count == 2) {
2841 EditBonePoint *ebp, *ebp2;
2842 float head[3], tail[3];
2845 /* check that the points don't belong to the same bone */
2846 ebp= (EditBonePoint *)points.first;
2849 if ((ebp->head_owner==ebp2->tail_owner) && (ebp->head_owner!=NULL)) {
2850 BKE_report(op->reports, RPT_ERROR, "Same bone selected...");
2851 BLI_freelistN(&points);
2852 return OPERATOR_CANCELLED;
2854 if ((ebp->tail_owner==ebp2->head_owner) && (ebp->tail_owner!=NULL)) {
2855 BKE_report(op->reports, RPT_ERROR, "Same bone selected...");
2856 BLI_freelistN(&points);
2857 return OPERATOR_CANCELLED;
2860 /* find which one should be the 'head' */
2861 if ((ebp->head_owner && ebp2->head_owner) || (ebp->tail_owner && ebp2->tail_owner)) {
2862 /* rule: whichever one is closer to 3d-cursor */
2864 float vecA[3], vecB[3];
2867 /* get cursor location */
2868 invert_m4_m4(obedit->imat, obedit->obmat);
2869 mul_v3_m4v3(curs, obedit->imat, give_cursor(scene, v3d));
2872 sub_v3_v3v3(vecA, ebp->vec, curs);
2873 sub_v3_v3v3(vecB, ebp2->vec, curs);