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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * editarmature.c: Interface for creating and posing armature objects
40 #include "MEM_guardedalloc.h"
44 #include "DNA_action_types.h"
45 #include "DNA_armature_types.h"
46 #include "DNA_constraint_types.h"
48 #include "DNA_mesh_types.h"
49 #include "DNA_meshdata_types.h"
50 #include "DNA_nla_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_screen_types.h"
54 #include "DNA_space_types.h"
55 #include "DNA_userdef_types.h"
56 #include "DNA_view3d_types.h"
57 #include "DNA_modifier_types.h"
58 #include "DNA_ipo_types.h"
59 #include "DNA_curve_types.h"
61 #include "BLI_blenlib.h"
62 #include "BLI_arithb.h"
63 #include "BLI_editVert.h"
64 #include "BLI_ghash.h"
66 #include "BKE_action.h"
67 #include "BKE_armature.h"
68 #include "BKE_constraint.h"
69 #include "BKE_deform.h"
70 #include "BKE_depsgraph.h"
71 #include "BKE_DerivedMesh.h"
72 #include "BKE_global.h"
74 #include "BKE_object.h"
75 #include "BKE_subsurf.h"
76 #include "BKE_utildefines.h"
77 #include "BKE_modifier.h"
79 #include "BIF_editaction.h"
80 #include "BIF_editmode_undo.h"
81 #include "BIF_editdeform.h"
82 #include "BIF_editarmature.h"
83 #include "BIF_editconstraint.h"
85 #include "BIF_graphics.h"
86 #include "BIF_interface.h"
87 #include "BIF_meshlaplacian.h"
88 #include "BIF_meshtools.h"
89 #include "BIF_poseobject.h"
90 #include "BIF_mywindow.h"
91 #include "BIF_resources.h"
92 #include "BIF_screen.h"
93 #include "BIF_space.h"
94 #include "BIF_toolbox.h"
95 #include "BIF_transform.h"
97 #include "BDR_editobject.h"
98 #include "BDR_drawobject.h"
100 #include "BSE_edit.h"
101 #include "BSE_view.h"
102 #include "BSE_trans_types.h"
104 #include "PIL_time.h"
106 #include "reeb.h" // FIX ME
108 #include "mydevice.h"
112 extern float center[3], centroid[3]; /* Originally defined in editobject.c */
115 #define TEST_EDITARMATURE {if(G.obedit==0) return; if( (G.vd->lay & G.obedit->lay)==0 ) return;}
117 /* prototypes for later */
118 static EditBone *editbone_name_exists (ListBase *ebones, char *name); // proto for below
120 /* **************** tools on Editmode Armature **************** */
122 /* converts Bones to EditBone list, used for tools as well */
123 void make_boneList(ListBase *list, ListBase *bones, EditBone *parent)
133 for (curBone=bones->first; curBone; curBone=curBone->next) {
134 eBone= MEM_callocN(sizeof(EditBone), "make_editbone");
136 /* Copy relevant data from bone to eBone */
137 eBone->parent= parent;
138 BLI_strncpy(eBone->name, curBone->name, 32);
139 eBone->flag = curBone->flag;
141 /* fix selection flags */
142 if (eBone->flag & BONE_SELECTED) {
143 eBone->flag |= BONE_TIPSEL;
144 if (eBone->parent && (eBone->flag & BONE_CONNECTED))
145 eBone->parent->flag |= BONE_TIPSEL;
147 eBone->flag |= BONE_ROOTSEL;
150 eBone->flag &= ~BONE_ROOTSEL;
152 VECCOPY(eBone->head, curBone->arm_head);
153 VECCOPY(eBone->tail, curBone->arm_tail);
158 VecSubf(delta, eBone->tail, eBone->head);
159 vec_roll_to_mat3(delta, 0.0, postmat);
161 Mat3CpyMat4(premat, curBone->arm_mat);
163 Mat3Inv(imat, postmat);
164 Mat3MulMat3(difmat, imat, premat);
166 eBone->roll = atan2(difmat[2][0], difmat[2][2]);
168 /* rest of stuff copy */
169 eBone->length= curBone->length;
170 eBone->dist= curBone->dist;
171 eBone->weight= curBone->weight;
172 eBone->xwidth= curBone->xwidth;
173 eBone->zwidth= curBone->zwidth;
174 eBone->ease1= curBone->ease1;
175 eBone->ease2= curBone->ease2;
176 eBone->rad_head= curBone->rad_head;
177 eBone->rad_tail= curBone->rad_tail;
178 eBone->segments = curBone->segments;
179 eBone->layer = curBone->layer;
181 BLI_addtail(list, eBone);
183 /* Add children if necessary */
184 if (curBone->childbase.first)
185 make_boneList(list, &curBone->childbase, eBone);
189 /* nasty stuff for converting roll in editbones into bones */
190 /* also sets restposition in armature (arm_mat) */
191 static void fix_bonelist_roll (ListBase *bonelist, ListBase *editbonelist)
201 for (curBone=bonelist->first; curBone; curBone=curBone->next) {
202 /* sets local matrix and arm_mat (restpos) */
203 where_is_armature_bone(curBone, curBone->parent);
205 /* Find the associated editbone */
206 for (ebone = editbonelist->first; ebone; ebone=ebone->next)
207 if ((Bone*)ebone->temp == curBone)
211 /* Get the ebone premat */
212 VecSubf(delta, ebone->tail, ebone->head);
213 vec_roll_to_mat3(delta, ebone->roll, premat);
215 /* Get the bone postmat */
216 Mat3CpyMat4(postmat, curBone->arm_mat);
218 Mat3Inv(imat, premat);
219 Mat3MulMat3(difmat, imat, postmat);
221 printf ("Bone %s\n", curBone->name);
222 printmatrix4("premat", premat);
223 printmatrix4("postmat", postmat);
224 printmatrix4("difmat", difmat);
225 printf ("Roll = %f\n", (-atan2(difmat[2][0], difmat[2][2]) * (180.0/M_PI)));
227 curBone->roll = -atan2(difmat[2][0], difmat[2][2]);
229 /* and set restposition again */
230 where_is_armature_bone(curBone, curBone->parent);
232 fix_bonelist_roll(&curBone->childbase, editbonelist);
236 /* converts the editbones back to the armature */
237 void editbones_to_armature (ListBase *list, Object *ob)
240 EditBone *eBone, *neBone;
244 arm = get_armature(ob);
251 /* remove zero sized bones, this gives instable restposes */
252 for (eBone=list->first; eBone; eBone= neBone) {
253 float len= VecLenf(eBone->head, eBone->tail);
255 if (len <= FLT_EPSILON) {
258 /* Find any bones that refer to this bone */
259 for (fBone=list->first; fBone; fBone= fBone->next) {
260 if (fBone->parent==eBone)
261 fBone->parent= eBone->parent;
263 printf("Warning: removed zero sized bone: %s\n", eBone->name);
264 BLI_freelinkN(list, eBone);
268 /* Copy the bones from the editData into the armature */
269 for (eBone=list->first; eBone; eBone=eBone->next) {
270 newBone= MEM_callocN(sizeof(Bone), "bone");
271 eBone->temp= newBone; /* Associate the real Bones with the EditBones */
273 BLI_strncpy(newBone->name, eBone->name, 32);
274 memcpy(newBone->head, eBone->head, sizeof(float)*3);
275 memcpy(newBone->tail, eBone->tail, sizeof(float)*3);
276 newBone->flag= eBone->flag;
277 if (eBone->flag & BONE_ACTIVE)
278 newBone->flag |= BONE_SELECTED; /* important, editbones can be active with only 1 point selected */
279 newBone->roll = 0.0f;
281 newBone->weight = eBone->weight;
282 newBone->dist = eBone->dist;
284 newBone->xwidth = eBone->xwidth;
285 newBone->zwidth = eBone->zwidth;
286 newBone->ease1= eBone->ease1;
287 newBone->ease2= eBone->ease2;
288 newBone->rad_head= eBone->rad_head;
289 newBone->rad_tail= eBone->rad_tail;
290 newBone->segments= eBone->segments;
291 newBone->layer = eBone->layer;
294 /* Fix parenting in a separate pass to ensure ebone->bone connections
295 are valid at this point */
296 for (eBone=list->first;eBone;eBone=eBone->next) {
297 newBone= (Bone *)eBone->temp;
299 newBone->parent= (Bone *)eBone->parent->temp;
300 BLI_addtail(&newBone->parent->childbase, newBone);
303 float M_boneRest[3][3];
304 float M_parentRest[3][3];
305 float iM_parentRest[3][3];
308 /* Get the parent's matrix (rotation only) */
309 VecSubf(delta, eBone->parent->tail, eBone->parent->head);
310 vec_roll_to_mat3(delta, eBone->parent->roll, M_parentRest);
312 /* Get this bone's matrix (rotation only) */
313 VecSubf(delta, eBone->tail, eBone->head);
314 vec_roll_to_mat3(delta, eBone->roll, M_boneRest);
316 /* Invert the parent matrix */
317 Mat3Inv(iM_parentRest, M_parentRest);
319 /* Get the new head and tail */
320 VecSubf(newBone->head, eBone->head, eBone->parent->tail);
321 VecSubf(newBone->tail, eBone->tail, eBone->parent->tail);
323 Mat3MulVecfl(iM_parentRest, newBone->head);
324 Mat3MulVecfl(iM_parentRest, newBone->tail);
327 /* ...otherwise add this bone to the armature's bonebase */
329 BLI_addtail(&arm->bonebase, newBone);
332 /* Make a pass through the new armature to fix rolling */
333 /* also builds restposition again (like where_is_armature) */
334 fix_bonelist_roll(&arm->bonebase, list);
336 /* so all users of this armature should get rebuilt */
337 for (obt= G.main->object.first; obt; obt= obt->id.next) {
339 armature_rebuild_pose(obt, arm);
342 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
347 void apply_rot_armature (Object *ob, float mat[3][3])
352 float scale = Mat3ToScalef(mat); /* store the scale of the matrix here to use on envelopes */
353 arm = get_armature(ob);
358 /* Put the armature into editmode */
359 list.first= list.last = NULL;
360 make_boneList(&list, &arm->bonebase, NULL);
362 /* Do the rotations */
363 for (ebone = list.first; ebone; ebone=ebone->next){
364 Mat3MulVecfl(mat, ebone->head);
365 Mat3MulVecfl(mat, ebone->tail);
367 ebone->rad_head *= scale;
368 ebone->rad_tail *= scale;
369 ebone->dist *= scale;
372 /* Turn the list into an armature */
373 editbones_to_armature(&list, ob);
375 /* Free the editbones */
377 BLI_freelistN (&list);
381 /* 0 == do center, 1 == center new, 2 == center cursor */
382 void docenter_armature (Object *ob, int centermode)
387 float cent[3] = {0.0f, 0.0f, 0.0f};
388 float min[3], max[3];
391 arm = get_armature(ob);
394 /* Put the armature into editmode */
395 list.first= list.last = NULL;
396 make_boneList(&list, &arm->bonebase, NULL);
398 /* Find the centerpoint */
399 if (centermode == 2) {
400 VECCOPY(cent, give_cursor());
401 Mat4Invert(ob->imat, ob->obmat);
402 Mat4MulVecfl(ob->imat, cent);
405 INIT_MINMAX(min, max);
407 for (ebone= list.first; ebone; ebone=ebone->next) {
408 DO_MINMAX(ebone->head, min, max);
409 DO_MINMAX(ebone->tail, min, max);
412 cent[0]= (min[0]+max[0])/2.0f;
413 cent[1]= (min[1]+max[1])/2.0f;
414 cent[2]= (min[2]+max[2])/2.0f;
417 /* Do the adjustments */
418 for (ebone= list.first; ebone; ebone=ebone->next){
419 VecSubf(ebone->head, ebone->head, cent);
420 VecSubf(ebone->tail, ebone->tail, cent);
423 /* Turn the list into an armature */
424 editbones_to_armature(&list, ob);
426 /* Free the editbones */
428 BLI_freelistN(&list);
431 /* Adjust object location for new centerpoint */
432 if(centermode && G.obedit==0) {
433 Mat3CpyMat4(omat, ob->obmat);
435 Mat3MulVecfl(omat, cent);
436 ob->loc[0]+= cent[0];
437 ob->loc[1]+= cent[1];
438 ob->loc[2]+= cent[2];
442 /* helper for apply_armature_pose2bones - fixes parenting of objects that are bone-parented to armature */
443 static void applyarmature_fix_boneparents (Object *armob)
447 /* go through all objects in database */
448 for (ob= G.main->object.first; ob; ob= ob->id.next) {
449 /* if parent is bone in this armature, apply corrections */
450 if ((ob->parent == armob) && (ob->partype == PARBONE)) {
451 /* apply current transform from parent (not yet destroyed),
452 * then calculate new parent inverse matrix
456 what_does_parent(ob);
457 Mat4Invert(ob->parentinv, workob.obmat);
462 /* set the current pose as the restpose */
463 void apply_armature_pose2bones(void)
471 /* don't check if editmode (should be done by caller) */
473 if (ob->type!=OB_ARMATURE) return;
474 if (object_data_is_libdata(ob)) {
478 arm= get_armature(ob);
480 /* helpful warnings... */
481 // TODO: add warnings to be careful about actions, applying deforms first, etc.
483 /* Get editbones of active armature to alter */
484 if (G.edbo.first) BLI_freelistN(&G.edbo);
485 make_boneList(&G.edbo, &arm->bonebase, NULL);
487 /* get pose of active object and move it out of posemode */
490 for (pchan=pose->chanbase.first; pchan; pchan=pchan->next) {
491 curbone= editbone_name_exists(&G.edbo, pchan->name);
493 /* simply copy the head/tail values from pchan over to curbone */
494 VECCOPY(curbone->head, pchan->pose_head);
495 VECCOPY(curbone->tail, pchan->pose_tail);
498 * 1. find auto-calculated roll value for this bone now
499 * 2. remove this from the 'visual' y-rotation
502 float premat[3][3], imat[3][3],pmat[3][3], tmat[3][3];
503 float delta[3], eul[3];
505 /* obtain new auto y-rotation */
506 VecSubf(delta, curbone->tail, curbone->head);
507 vec_roll_to_mat3(delta, 0.0, premat);
508 Mat3Inv(imat, premat);
510 /* get pchan 'visual' matrix */
511 Mat3CpyMat4(pmat, pchan->pose_mat);
513 /* remove auto from visual and get euler rotation */
514 Mat3MulMat3(tmat, imat, pmat);
515 Mat3ToEul(tmat, eul);
517 /* just use this euler-y as new roll value */
518 curbone->roll= eul[1];
521 /* clear transform values for pchan */
522 pchan->loc[0]= pchan->loc[1]= pchan->loc[2]= 0;
523 pchan->quat[1]= pchan->quat[2]= pchan->quat[3]= 0;
524 pchan->quat[0]= pchan->size[0]= pchan->size[1]= pchan->size[2]= 1;
527 curbone->flag |= BONE_UNKEYED;
530 /* convert editbones back to bones */
531 editbones_to_armature(&G.edbo, ob);
532 if (G.edbo.first) BLI_freelistN(&G.edbo);
534 /* flush positions of posebones */
537 /* fix parenting of objects which are bone-parented */
538 applyarmature_fix_boneparents(ob);
540 BIF_undo_push("Apply new restpose");
541 allqueue(REDRAWVIEW3D, 0);
545 /* Helper function for armature joining - link fixing */
546 static void joined_armature_fix_links(Object *tarArm, Object *srcArm, bPoseChannel *pchan, EditBone *curbone)
550 bPoseChannel *pchant;
553 /* let's go through all objects in database */
554 for (ob= G.main->object.first; ob; ob= ob->id.next) {
555 /* do some object-type specific things */
556 if (ob->type == OB_ARMATURE) {
558 for (pchant= pose->chanbase.first; pchant; pchant= pchant->next) {
559 for (con= pchant->constraints.first; con; con= con->next) {
560 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
561 ListBase targets = {NULL, NULL};
562 bConstraintTarget *ct;
564 /* constraint targets */
565 if (cti && cti->get_constraint_targets) {
566 cti->get_constraint_targets(con, &targets);
568 for (ct= targets.first; ct; ct= ct->next) {
569 if (ct->tar == srcArm) {
570 if (strcmp(ct->subtarget, "")==0) {
573 else if (strcmp(ct->subtarget, pchan->name)==0) {
575 strcpy(ct->subtarget, curbone->name);
580 if (cti->flush_constraint_targets)
581 cti->flush_constraint_targets(con, &targets, 0);
584 /* action constraint? */
585 if (con->type == CONSTRAINT_TYPE_ACTION) {
586 bActionConstraint *data= con->data;
588 bActionChannel *achan;
593 for (achan= act->chanbase.first; achan; achan= achan->next) {
594 if (strcmp(achan->name, pchan->name)==0)
595 BLI_strncpy(achan->name, curbone->name, 32);
604 /* fix object-level constraints */
606 for (con= ob->constraints.first; con; con= con->next) {
607 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
608 ListBase targets = {NULL, NULL};
609 bConstraintTarget *ct;
611 /* constraint targets */
612 if (cti && cti->get_constraint_targets) {
613 cti->get_constraint_targets(con, &targets);
615 for (ct= targets.first; ct; ct= ct->next) {
616 if (ct->tar == srcArm) {
617 if (strcmp(ct->subtarget, "")==0) {
620 else if (strcmp(ct->subtarget, pchan->name)==0) {
622 strcpy(ct->subtarget, curbone->name);
627 if (cti->flush_constraint_targets)
628 cti->flush_constraint_targets(con, &targets, 0);
633 /* See if an object is parented to this armature */
634 if (ob->parent && (ob->parent == srcArm)) {
635 /* Is object parented to a bone of this src armature? */
636 if (ob->partype==PARBONE) {
637 /* bone name in object */
638 if (!strcmp(ob->parsubstr, pchan->name))
639 BLI_strncpy(ob->parsubstr, curbone->name, 32);
642 /* make tar armature be new parent */
648 int join_armature(void)
652 Base *base, *nextbase;
654 bPoseChannel *pchan, *pchann;
655 ListBase ebbase, eblist;
657 float mat[4][4], oimat[4][4];
659 /* Ensure we're not in editmode and that the active object is an armature*/
660 /* if(G.obedit) return; */ /* Alredy checked in join_menu() */
663 if (ob->type!=OB_ARMATURE) return 0;
664 if (object_data_is_libdata(ob)) {
668 arm= get_armature(ob);
670 /* Get editbones of active armature to add editbones to */
671 ebbase.first=ebbase.last= NULL;
672 make_boneList(&ebbase, &arm->bonebase, NULL);
674 /* get pose of active object and move it out of posemode */
676 ob->flag &= ~OB_POSEMODE;
677 BASACT->flag &= ~OB_POSEMODE;
679 for (base=FIRSTBASE; base; base=nextbase) {
680 nextbase = base->next;
682 if ((base->object->type==OB_ARMATURE) && (base->object!=ob)) {
683 /* Make a list of editbones in current armature */
684 eblist.first=eblist.last= NULL;
685 make_boneList(&eblist, &((bArmature *)base->object->data)->bonebase, NULL);
687 /* Get Pose of current armature */
688 opose= base->object->pose;
689 base->object->flag &= ~OB_POSEMODE;
690 BASACT->flag &= ~OB_POSEMODE;
692 /* Find the difference matrix */
693 Mat4Invert(oimat, ob->obmat);
694 Mat4MulMat4(mat, base->object->obmat, oimat);
696 /* Copy bones and posechannels from the object to the edit armature */
697 for (pchan=opose->chanbase.first; pchan; pchan=pchann) {
699 curbone= editbone_name_exists(&eblist, pchan->name);
702 unique_editbone_name(&ebbase, curbone->name);
704 /* Transform the bone */
714 VecSubf(delta, curbone->tail, curbone->head);
715 vec_roll_to_mat3(delta, curbone->roll, temp);
717 Mat4MulMat34(premat, temp, mat);
719 Mat4MulVecfl(mat, curbone->head);
720 Mat4MulVecfl(mat, curbone->tail);
722 /* Get the postmat */
723 VecSubf(delta, curbone->tail, curbone->head);
724 vec_roll_to_mat3(delta, curbone->roll, temp);
725 Mat4CpyMat3(postmat, temp);
728 Mat4Invert(imat, premat);
729 Mat4MulMat4(difmat, postmat, imat);
731 curbone->roll -= atan2(difmat[2][0], difmat[2][2]);
734 /* Fix Constraints and Other Links to this Bone and Armature */
735 joined_armature_fix_links(ob, base->object, pchan, curbone);
738 sprintf(pchan->name, curbone->name);
741 BLI_remlink(&eblist, curbone);
742 BLI_addtail(&ebbase, curbone);
744 BLI_remlink(&opose->chanbase, pchan);
745 BLI_addtail(&pose->chanbase, pchan);
748 free_and_unlink_base(base);
753 DAG_scene_sort(G.scene); // because we removed object(s)
755 editbones_to_armature(&ebbase, ob);
756 if (ebbase.first) BLI_freelistN(&ebbase);
758 allqueue(REDRAWVIEW3D, 0);
759 allqueue(REDRAWOOPS, 0);
763 /* Helper function for armature separating - link fixing */
764 static void separated_armature_fix_links(Object *origArm, Object *newArm)
767 bPoseChannel *pchan, *pcha, *pchb;
769 ListBase *opchans, *npchans;
771 /* get reference to list of bones in original and new armatures */
772 opchans= &origArm->pose->chanbase;
773 npchans= &newArm->pose->chanbase;
775 /* let's go through all objects in database */
776 for (ob= G.main->object.first; ob; ob= ob->id.next) {
777 /* do some object-type specific things */
778 if (ob->type == OB_ARMATURE) {
779 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
780 for (con= pchan->constraints.first; con; con= con->next) {
781 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
782 ListBase targets = {NULL, NULL};
783 bConstraintTarget *ct;
785 /* constraint targets */
786 if (cti && cti->get_constraint_targets) {
787 cti->get_constraint_targets(con, &targets);
789 for (ct= targets.first; ct; ct= ct->next) {
790 /* any targets which point to original armature are redirected to the new one only if:
791 * - the target isn't origArm/newArm itself
792 * - the target is one that can be found in newArm/origArm
794 if ((ct->tar == origArm) && (ct->subtarget[0] != 0)) {
795 for (pcha=npchans->first, pchb=npchans->last; pcha && pchb; pcha=pcha->next, pchb=pchb->prev) {
796 /* check if either one matches */
797 if ( (strcmp(pcha->name, ct->subtarget)==0) ||
798 (strcmp(pchb->name, ct->subtarget)==0) )
804 /* check if both ends have met (to stop checking) */
805 if (pcha == pchb) break;
808 else if ((ct->tar == newArm) && (ct->subtarget[0] != 0)) {
809 for (pcha=opchans->first, pchb=opchans->last; pcha && pchb; pcha=pcha->next, pchb=pchb->prev) {
810 /* check if either one matches */
811 if ( (strcmp(pcha->name, ct->subtarget)==0) ||
812 (strcmp(pchb->name, ct->subtarget)==0) )
818 /* check if both ends have met (to stop checking) */
819 if (pcha == pchb) break;
824 if (cti->flush_constraint_targets)
825 cti->flush_constraint_targets(con, &targets, 0);
831 /* fix object-level constraints */
833 for (con= ob->constraints.first; con; con= con->next) {
834 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
835 ListBase targets = {NULL, NULL};
836 bConstraintTarget *ct;
838 /* constraint targets */
839 if (cti && cti->get_constraint_targets) {
840 cti->get_constraint_targets(con, &targets);
842 for (ct= targets.first; ct; ct= ct->next) {
843 /* any targets which point to original armature are redirected to the new one only if:
844 * - the target isn't origArm/newArm itself
845 * - the target is one that can be found in newArm/origArm
847 if ((ct->tar == origArm) && (ct->subtarget[0] != 0)) {
848 for (pcha=npchans->first, pchb=npchans->last; pcha && pchb; pcha=pcha->next, pchb=pchb->prev) {
849 /* check if either one matches */
850 if ( (strcmp(pcha->name, ct->subtarget)==0) ||
851 (strcmp(pchb->name, ct->subtarget)==0) )
857 /* check if both ends have met (to stop checking) */
858 if (pcha == pchb) break;
861 else if ((ct->tar == newArm) && (ct->subtarget[0] != 0)) {
862 for (pcha=opchans->first, pchb=opchans->last; pcha && pchb; pcha=pcha->next, pchb=pchb->prev) {
863 /* check if either one matches */
864 if ( (strcmp(pcha->name, ct->subtarget)==0) ||
865 (strcmp(pchb->name, ct->subtarget)==0) )
871 /* check if both ends have met (to stop checking) */
872 if (pcha == pchb) break;
877 if (cti->flush_constraint_targets)
878 cti->flush_constraint_targets(con, &targets, 0);
883 /* See if an object is parented to this armature */
884 if ((ob->parent) && (ob->parent == origArm)) {
885 /* Is object parented to a bone of this src armature? */
886 if (ob->partype==PARBONE) {
887 /* bone name in object */
888 for (pcha=npchans->first, pchb=npchans->last; pcha && pchb; pcha=pcha->next, pchb=pchb->prev) {
889 /* check if either one matches */
890 if ( (strcmp(pcha->name, ob->parsubstr)==0) ||
891 (strcmp(pchb->name, ob->parsubstr)==0) )
897 /* check if both ends have met (to stop checking) */
898 if (pcha == pchb) break;
905 /* Helper function for armature separating - remove certain bones from the given armature
906 * sel: remove selected bones from the armature, otherwise the unselected bones are removed
908 static void separate_armature_bones (Object *ob, short sel)
910 ListBase edbo = {NULL, NULL};
911 bArmature *arm= (bArmature *)ob->data;
912 bPoseChannel *pchan, *pchann;
915 /* make local set of editbones to manipulate here */
916 make_boneList(&edbo, &arm->bonebase, NULL);
918 /* go through pose-channels, checking if a bone should be removed */
919 for (pchan=ob->pose->chanbase.first; pchan; pchan=pchann) {
921 curbone= editbone_name_exists(&edbo, pchan->name);
923 /* check if bone needs to be removed */
924 if ( (sel && (curbone->flag & BONE_SELECTED)) ||
925 (!sel && !(curbone->flag & BONE_SELECTED)) )
930 /* clear the bone->parent var of any bone that had this as its parent */
931 for (ebo= edbo.first; ebo; ebo= ebo->next) {
932 if (ebo->parent == curbone) {
934 ebo->temp= NULL; /* this is needed to prevent random crashes with in editbones_to_armature */
935 ebo->flag &= ~BONE_CONNECTED;
939 /* clear the pchan->parent var of any pchan that had this as its parent */
940 for (pchn= ob->pose->chanbase.first; pchn; pchn=pchn->next) {
941 if (pchn->parent == pchan)
945 /* free any of the extra-data this pchan might have */
946 if (pchan->path) MEM_freeN(pchan->path);
947 free_constraints(&pchan->constraints);
949 /* get rid of unneeded bone */
950 BLI_freelinkN(&edbo, curbone);
951 BLI_freelinkN(&ob->pose->chanbase, pchan);
955 /* exit editmode (recalculates pchans too) */
956 editbones_to_armature(&edbo, ob);
957 BLI_freelistN(&edbo);
960 /* separate selected bones into their armature */
961 void separate_armature (void)
963 Object *oldob, *newob;
964 Base *base, *oldbase, *newbase;
967 if ( G.vd==0 || (G.vd->lay & G.obedit->lay)==0 ) return;
968 if ( okee("Separate")==0 ) return;
974 /* we are going to do this as follows (unlike every other instance of separate):
975 * 1. exit editmode +posemode for active armature/base. Take note of what this is.
976 * 2. duplicate base - BASACT is the new one now
977 * 3. for each of the two armatures, enter editmode -> remove appropriate bones -> exit editmode + recalc
978 * 4. fix constraint links
979 * 5. make original armature active and enter editmode
982 /* 1) only edit-base selected */
984 for (base= FIRSTBASE; base; base= base->next) {
985 if (base->lay & G.vd->lay) {
986 if (base->object==G.obedit) base->flag |= 1;
987 else base->flag &= ~1;
991 /* 1) store starting settings and exit editmode */
994 oldob->flag &= ~OB_POSEMODE;
995 oldbase->flag &= ~OB_POSEMODE;
1000 /* 2) duplicate base */
1001 adduplicate(1, USER_DUP_ARM); /* no transform and zero so do get a linked dupli */
1003 newbase= BASACT; /* basact is set in adduplicate() */
1004 newob= newbase->object;
1005 newbase->flag &= ~SELECT;
1008 /* 3) remove bones that shouldn't still be around on both armatures */
1009 separate_armature_bones(oldob, 1);
1010 separate_armature_bones(newob, 0);
1013 /* 4) fix links before depsgraph flushes */ // err... or after?
1014 separated_armature_fix_links(oldob, newob);
1016 DAG_object_flush_update(G.scene, oldob, OB_RECALC_DATA); /* this is the original one */
1017 DAG_object_flush_update(G.scene, newob, OB_RECALC_DATA); /* this is the separated one */
1020 /* 5) restore original conditions */
1023 BASACT->flag |= SELECT;
1025 make_editArmature();
1027 /* recalc/redraw + cleanup */
1031 allqueue(REDRAWVIEW3D, 0);
1032 allqueue(REDRAWBUTSEDIT, 0);
1033 allqueue(REDRAWOOPS, 0);
1035 BIF_undo_push("Separate Armature");
1038 /* **************** END tools on Editmode Armature **************** */
1039 /* **************** PoseMode & EditMode *************************** */
1041 /* only for opengl selection indices */
1042 Bone *get_indexed_bone (Object *ob, int index)
1044 bPoseChannel *pchan;
1047 if(ob->pose==NULL) return NULL;
1048 index>>=16; // bone selection codes use left 2 bytes
1050 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next, a++) {
1051 if(a==index) return pchan->bone;
1056 /* See if there are any selected bones in this buffer */
1057 static void *get_bone_from_selectbuffer(Base *base, unsigned int *buffer, short hits, short findunsel)
1059 Object *ob= base->object;
1062 void *firstunSel=NULL, *firstSel=NULL, *data;
1063 unsigned int hitresult;
1064 short i, takeNext=0, sel;
1066 for (i=0; i< hits; i++){
1067 hitresult = buffer[3+(i*4)];
1069 if (!(hitresult & BONESEL_NOSEL)) { // -1
1070 if(hitresult & BONESEL_ANY) { // to avoid including objects in selection
1072 hitresult &= ~(BONESEL_ANY);
1073 /* Determine what the current bone is */
1074 if (G.obedit==NULL || base->object!=G.obedit) {
1075 /* no singular posemode, so check for correct object */
1076 if(base->selcol == (hitresult & 0xFFFF)) {
1077 bone = get_indexed_bone(ob, hitresult);
1080 sel = (bone->flag & BONE_SELECTED);
1082 sel = !(bone->flag & BONE_SELECTED);
1092 ebone = BLI_findlink(&G.edbo, hitresult);
1094 sel = (ebone->flag & BONE_SELECTED);
1096 sel = !(ebone->flag & BONE_SELECTED);
1103 if(!firstSel) firstSel= data;
1123 /* used by posemode as well editmode */
1124 static void *get_nearest_bone (short findunsel)
1126 unsigned int buffer[MAXPICKBUF];
1132 hits= view3d_opengl_select(buffer, MAXPICKBUF, 0, 0, 0, 0);
1135 return get_bone_from_selectbuffer(BASACT, buffer, hits, findunsel);
1140 /* helper for setflag_sel_bone() */
1141 static void bone_setflag (int *bone, int flag, short mode)
1144 /* exception for inverse flags */
1145 if (flag == BONE_NO_DEFORM) {
1165 /* Get the first available child of an editbone */
1166 static EditBone *editbone_get_child(EditBone *pabone, short use_visibility)
1170 EditBone *curbone, *chbone=NULL;
1172 if (!G.obedit) return NULL;
1174 arm= (bArmature *)ob->data;
1176 for (curbone= G.edbo.first; curbone; curbone= curbone->next) {
1177 if (curbone->parent == pabone) {
1178 if (use_visibility) {
1179 if ((arm->layer & curbone->layer) && !(pabone->flag & BONE_HIDDEN_A))
1190 void armature_select_hierarchy(short direction, short add_to_sel)
1194 EditBone *curbone, *pabone, *chbone;
1196 if (!G.obedit) return;
1198 arm= (bArmature *)ob->data;
1200 for (curbone= G.edbo.first; curbone; curbone= curbone->next) {
1201 if (EBONE_VISIBLE(arm, curbone)) {
1202 if (curbone->flag & (BONE_ACTIVE)) {
1203 if (direction == BONE_SELECT_PARENT) {
1204 if (curbone->parent == NULL) continue;
1205 else pabone = curbone->parent;
1207 if (EBONE_VISIBLE(arm, pabone)) {
1208 pabone->flag |= (BONE_ACTIVE|BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1209 if (pabone->parent) pabone->parent->flag |= BONE_TIPSEL;
1211 if (!add_to_sel) curbone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1212 curbone->flag &= ~BONE_ACTIVE;
1217 else { // BONE_SELECT_CHILD
1218 chbone = editbone_get_child(curbone, 1);
1219 if (chbone == NULL) continue;
1221 if (EBONE_VISIBLE(arm, chbone)) {
1222 chbone->flag |= (BONE_ACTIVE|BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1225 curbone->flag &= ~(BONE_SELECTED|BONE_ROOTSEL);
1226 if (curbone->parent) curbone->parent->flag &= ~BONE_TIPSEL;
1228 curbone->flag &= ~BONE_ACTIVE;
1236 countall(); // flushes selection!
1238 allqueue (REDRAWVIEW3D, 0);
1239 allqueue (REDRAWBUTSEDIT, 0);
1240 allqueue (REDRAWBUTSOBJECT, 0);
1241 allqueue (REDRAWOOPS, 0);
1243 if (direction==BONE_SELECT_PARENT)
1244 BIF_undo_push("Select edit bone parent");
1245 if (direction==BONE_SELECT_CHILD)
1246 BIF_undo_push("Select edit bone child");
1249 /* used by posemode and editmode */
1250 void setflag_armature (short mode)
1263 arm= (bArmature *)ob->data;
1265 /* get flag to set (sync these with the ones used in eBone_Flag */
1267 flag= pupmenu("Disable Setting%t|Draw Wire%x1|Deform%x2|Mult VG%x3|Hinge%x4|No Scale%x5|Locked%x6");
1269 flag= pupmenu("Enable Setting%t|Draw Wire%x1|Deform%x2|Mult VG%x3|Hinge%x4|No Scale%x5|Locked%x6");
1271 flag= pupmenu("Toggle Setting%t|Draw Wire%x1|Deform%x2|Mult VG%x3|Hinge%x4|No Scale%x5|Locked%x6");
1273 case 1: flag = BONE_DRAWWIRE; break;
1274 case 2: flag = BONE_NO_DEFORM; break;
1275 case 3: flag = BONE_MULT_VG_ENV; break;
1276 case 4: flag = BONE_HINGE; break;
1277 case 5: flag = BONE_NO_SCALE; break;
1278 case 6: flag = BONE_EDITMODE_LOCKED; break;
1282 /* determine which mode armature is in */
1283 if ((!G.obedit) && (ob->flag & OB_POSEMODE)) {
1284 /* deal with pose channels */
1285 bPoseChannel *pchan;
1288 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1289 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
1290 if (pchan->bone->flag & BONE_SELECTED) {
1291 bone_setflag(&pchan->bone->flag, flag, mode);
1296 else if (G.obedit) {
1297 /* deal with editbones */
1301 for (curbone= G.edbo.first; curbone; curbone= curbone->next) {
1302 if (arm->layer & curbone->layer) {
1303 if (curbone->flag & BONE_SELECTED) {
1304 bone_setflag(&curbone->flag, flag, mode);
1310 allqueue(REDRAWVIEW3D, 0);
1311 allqueue(REDRAWBUTSEDIT, 0);
1312 allqueue(REDRAWBUTSOBJECT, 0);
1313 allqueue(REDRAWOOPS, 0);
1315 BIF_undo_push("Change Bone Setting");
1318 /* **************** END PoseMode & EditMode *************************** */
1319 /* **************** Posemode stuff ********************** */
1322 static void selectconnected_posebonechildren (Object *ob, Bone *bone)
1326 if (!(bone->flag & BONE_CONNECTED))
1329 select_actionchannel_by_name (ob->action, bone->name, !(G.qual & LR_SHIFTKEY));
1331 if (G.qual & LR_SHIFTKEY)
1332 bone->flag &= ~BONE_SELECTED;
1334 bone->flag |= BONE_SELECTED;
1336 for (curBone=bone->childbase.first; curBone; curBone=curBone->next){
1337 selectconnected_posebonechildren (ob, curBone);
1341 /* within active object context */
1342 void selectconnected_posearmature(void)
1344 Bone *bone, *curBone, *next;
1347 if(!ob || !ob->pose) return;
1349 if (G.qual & LR_SHIFTKEY)
1350 bone= get_nearest_bone(0);
1352 bone = get_nearest_bone(1);
1357 /* Select parents */
1358 for (curBone=bone; curBone; curBone=next){
1359 select_actionchannel_by_name (ob->action, curBone->name, !(G.qual & LR_SHIFTKEY));
1360 if (G.qual & LR_SHIFTKEY)
1361 curBone->flag &= ~BONE_SELECTED;
1363 curBone->flag |= BONE_SELECTED;
1365 if (curBone->flag & BONE_CONNECTED)
1366 next=curBone->parent;
1371 /* Select children */
1372 for (curBone=bone->childbase.first; curBone; curBone=next){
1373 selectconnected_posebonechildren (ob, curBone);
1376 countall(); // flushes selection!
1378 allqueue (REDRAWVIEW3D, 0);
1379 allqueue(REDRAWBUTSEDIT, 0);
1380 allqueue(REDRAWBUTSOBJECT, 0);
1381 allqueue (REDRAWACTION, 0);
1382 allqueue(REDRAWOOPS, 0);
1383 BIF_undo_push("Select connected");
1387 /* **************** END Posemode stuff ********************** */
1388 /* **************** EditMode stuff ********************** */
1390 /* called in space.c */
1391 void selectconnected_armature(void)
1393 EditBone *bone, *curBone, *next;
1395 if (G.qual & LR_SHIFTKEY)
1396 bone= get_nearest_bone(0);
1398 bone= get_nearest_bone(1);
1403 /* Select parents */
1404 for (curBone=bone; curBone; curBone=next){
1405 if (G.qual & LR_SHIFTKEY){
1406 curBone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1409 curBone->flag |= (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1412 if (curBone->flag & BONE_CONNECTED)
1413 next=curBone->parent;
1418 /* Select children */
1420 for (curBone=G.edbo.first; curBone; curBone=next){
1421 next = curBone->next;
1422 if (curBone->parent == bone){
1423 if (curBone->flag & BONE_CONNECTED){
1424 if (G.qual & LR_SHIFTKEY)
1425 curBone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1427 curBone->flag |= (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1442 countall(); // flushes selection!
1444 allqueue (REDRAWVIEW3D, 0);
1445 allqueue (REDRAWBUTSEDIT, 0);
1446 allqueue(REDRAWBUTSOBJECT, 0);
1447 allqueue(REDRAWOOPS, 0);
1449 BIF_undo_push("Select connected");
1453 /* does bones and points */
1454 /* note that BONE ROOT only gets drawn for root bones (or without IK) */
1455 static EditBone * get_nearest_editbonepoint (int findunsel, int *selmask)
1458 unsigned int buffer[MAXPICKBUF];
1459 unsigned int hitresult, besthitresult=BONESEL_NOSEL;
1461 short hits, mval[2];
1467 getmouseco_areawin(mval);
1468 hits= view3d_opengl_select(buffer, MAXPICKBUF, mval[0]-5, mval[1]-5, mval[0]+5, mval[1]+5);
1470 hits= view3d_opengl_select(buffer, MAXPICKBUF, mval[0]-12, mval[1]-12, mval[0]+12, mval[1]+12);
1472 /* See if there are any selected bones in this group */
1476 if (!(buffer[3] & BONESEL_NOSEL))
1477 besthitresult= buffer[3];
1480 for (i=0; i< hits; i++) {
1481 hitresult= buffer[3+(i*4)];
1482 if (!(hitresult & BONESEL_NOSEL)) {
1485 ebone = BLI_findlink(&G.edbo, hitresult & ~BONESEL_ANY);
1487 /* clicks on bone points get advantage */
1488 if( hitresult & (BONESEL_ROOT|BONESEL_TIP)) {
1489 /* but also the unselected one */
1491 if( (hitresult & BONESEL_ROOT) && (ebone->flag & BONE_ROOTSEL)==0)
1493 else if( (hitresult & BONESEL_TIP) && (ebone->flag & BONE_TIPSEL)==0)
1503 if((ebone->flag & BONE_SELECTED)==0)
1512 besthitresult= hitresult;
1518 if (!(besthitresult & BONESEL_NOSEL)) {
1520 ebone= BLI_findlink(&G.edbo, besthitresult & ~BONESEL_ANY);
1523 if (besthitresult & BONESEL_ROOT)
1524 *selmask |= BONE_ROOTSEL;
1525 if (besthitresult & BONESEL_TIP)
1526 *selmask |= BONE_TIPSEL;
1527 if (besthitresult & BONESEL_BONE)
1528 *selmask |= BONE_SELECTED;
1536 static void delete_bone(EditBone* exBone)
1540 /* Find any bones that refer to this bone */
1541 for (curBone=G.edbo.first;curBone;curBone=curBone->next) {
1542 if (curBone->parent==exBone) {
1543 curBone->parent=exBone->parent;
1544 curBone->flag &= ~BONE_CONNECTED;
1548 BLI_freelinkN(&G.edbo,exBone);
1551 /* only editmode! */
1552 void delete_armature(void)
1554 bArmature *arm= G.obedit->data;
1555 EditBone *curBone, *next;
1559 if (okee("Erase selected bone(s)")==0) return;
1561 /* Select mirrored bones */
1562 if (arm->flag & ARM_MIRROR_EDIT) {
1563 for (curBone=G.edbo.first; curBone; curBone=curBone->next) {
1564 if (arm->layer & curBone->layer) {
1565 if (curBone->flag & BONE_SELECTED) {
1566 next = armature_bone_get_mirrored(curBone);
1568 next->flag |= BONE_SELECTED;
1574 /* First erase any associated pose channel */
1575 if (G.obedit->pose) {
1576 bPoseChannel *chan, *next;
1577 for (chan=G.obedit->pose->chanbase.first; chan; chan=next) {
1579 curBone = editbone_name_exists(&G.edbo, chan->name);
1581 if (curBone && (curBone->flag & BONE_SELECTED) && (arm->layer & curBone->layer)) {
1582 free_constraints(&chan->constraints);
1583 BLI_freelinkN (&G.obedit->pose->chanbase, chan);
1586 for (con= chan->constraints.first; con; con= con->next) {
1587 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1588 ListBase targets = {NULL, NULL};
1589 bConstraintTarget *ct;
1591 if (cti && cti->get_constraint_targets) {
1592 cti->get_constraint_targets(con, &targets);
1594 for (ct= targets.first; ct; ct= ct->next) {
1595 if (ct->tar == G.obedit) {
1596 if (ct->subtarget[0]) {
1597 curBone = editbone_name_exists(&G.edbo, ct->subtarget);
1598 if (curBone && (curBone->flag & BONE_SELECTED) && (arm->layer & curBone->layer)) {
1599 con->flag |= CONSTRAINT_DISABLE;
1600 ct->subtarget[0]= 0;
1606 if (cti->flush_constraint_targets)
1607 cti->flush_constraint_targets(con, &targets, 0);
1615 for (curBone=G.edbo.first;curBone;curBone=next) {
1617 if (arm->layer & curBone->layer) {
1618 if (curBone->flag & BONE_SELECTED)
1619 delete_bone(curBone);
1624 allqueue(REDRAWVIEW3D, 0);
1625 allqueue(REDRAWBUTSEDIT, 0);
1626 allqueue(REDRAWBUTSOBJECT, 0);
1627 allqueue(REDRAWOOPS, 0);
1628 countall(); // flushes selection!
1630 BIF_undo_push("Delete bone(s)");
1633 /* context: editmode armature */
1634 void mouse_armature(void)
1636 EditBone *nearBone = NULL, *ebone;
1639 nearBone= get_nearest_editbonepoint(1, &selmask);
1642 if (!(G.qual & LR_SHIFTKEY)) {
1643 deselectall_armature(0, 0);
1646 /* by definition the non-root connected bones have no root point drawn,
1647 so a root selection needs to be delivered to the parent tip,
1648 countall() (bad location) flushes these flags */
1650 if(selmask & BONE_SELECTED) {
1651 if(nearBone->parent && (nearBone->flag & BONE_CONNECTED)) {
1652 /* click in a chain */
1653 if(G.qual & LR_SHIFTKEY) {
1654 /* hold shift inverts this bone's selection */
1655 if(nearBone->flag & BONE_SELECTED) {
1656 /* deselect this bone */
1657 nearBone->flag &= ~(BONE_TIPSEL|BONE_SELECTED);
1658 /* only deselect parent tip if it is not selected */
1659 if(!(nearBone->parent->flag & BONE_SELECTED))
1660 nearBone->parent->flag &= ~BONE_TIPSEL;
1663 /* select this bone */
1664 nearBone->flag |= BONE_TIPSEL;
1665 nearBone->parent->flag |= BONE_TIPSEL;
1669 /* select this bone */
1670 nearBone->flag |= BONE_TIPSEL;
1671 nearBone->parent->flag |= BONE_TIPSEL;
1675 if(G.qual & LR_SHIFTKEY) {
1676 /* hold shift inverts this bone's selection */
1677 if(nearBone->flag & BONE_SELECTED)
1678 nearBone->flag &= ~(BONE_TIPSEL|BONE_ROOTSEL);
1680 nearBone->flag |= (BONE_TIPSEL|BONE_ROOTSEL);
1682 else nearBone->flag |= (BONE_TIPSEL|BONE_ROOTSEL);
1686 if ((G.qual & LR_SHIFTKEY) && (nearBone->flag & selmask))
1687 nearBone->flag &= ~selmask;
1689 nearBone->flag |= selmask;
1692 countall(); // flushes selection!
1695 /* then now check for active status */
1696 for (ebone=G.edbo.first;ebone;ebone=ebone->next) ebone->flag &= ~BONE_ACTIVE;
1697 if(nearBone->flag & BONE_SELECTED) nearBone->flag |= BONE_ACTIVE;
1700 allqueue(REDRAWVIEW3D, 0);
1701 allqueue(REDRAWBUTSEDIT, 0);
1702 allqueue(REDRAWBUTSOBJECT, 0);
1703 allqueue(REDRAWOOPS, 0);
1706 rightmouse_transform();
1709 void free_editArmature(void)
1711 /* Clear the editbones list */
1713 BLI_freelistN(&G.edbo);
1716 void remake_editArmature(void)
1718 if(okee("Reload original data")==0) return;
1720 make_editArmature();
1721 allqueue(REDRAWVIEW3D, 0);
1722 allqueue(REDRAWOOPS, 0);
1723 allqueue(REDRAWBUTSHEAD, 0);
1724 allqueue(REDRAWBUTSEDIT, 0);
1725 allqueue(REDRAWBUTSOBJECT, 0);
1727 // BIF_undo_push("Delete bone");
1730 /* Put object in EditMode */
1731 void make_editArmature(void)
1735 if (G.obedit==0) return;
1737 free_editArmature();
1739 arm= get_armature(G.obedit);
1742 make_boneList(&G.edbo, &arm->bonebase,NULL);
1745 /* put EditMode back in Object */
1746 void load_editArmature(void)
1750 arm= get_armature(G.obedit);
1753 editbones_to_armature(&G.edbo, G.obedit);
1756 /* toggle==0: deselect
1757 toggle==1: swap (based on test)
1758 toggle==2: only active tag
1759 toggle==3: swap (no test)
1761 void deselectall_armature(int toggle, int doundo)
1763 bArmature *arm= G.obedit->data;
1768 /* Determine if there are any selected bones
1769 And therefore whether we are selecting or deselecting */
1770 for (eBone=G.edbo.first;eBone;eBone=eBone->next){
1771 // if(arm->layer & eBone->layer) {
1772 if (eBone->flag & (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL)){
1782 for (eBone=G.edbo.first;eBone;eBone=eBone->next) {
1784 /* invert selection of bone */
1785 if ((arm->layer & eBone->layer) && (eBone->flag & BONE_HIDDEN_A)==0) {
1786 eBone->flag ^= (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1787 eBone->flag &= ~BONE_ACTIVE;
1792 if(arm->layer & eBone->layer && (eBone->flag & BONE_HIDDEN_A)==0) {
1793 eBone->flag |= (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1795 eBone->parent->flag |= (BONE_TIPSEL);
1799 /* clear active flag */
1800 eBone->flag &= ~(BONE_ACTIVE);
1804 eBone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL | BONE_ACTIVE);
1808 allqueue(REDRAWVIEW3D, 0);
1809 allqueue(REDRAWBUTSEDIT, 0);
1810 allqueue(REDRAWBUTSOBJECT, 0);
1811 allqueue(REDRAWOOPS, 0);
1813 countall(); // flushes selection!
1815 if (sel==1) BIF_undo_push("Select All");
1816 else BIF_undo_push("Deselect All");
1820 /* adjust bone roll to align Z axis with vector
1821 * vec is in local space and is normalized
1823 float rollBoneToVector(EditBone *bone, float new_up_axis[3])
1825 float mat[3][3], nor[3], up_axis[3], vec[3];
1828 VecSubf(nor, bone->tail, bone->head);
1830 vec_roll_to_mat3(nor, 0, mat);
1831 VECCOPY(up_axis, mat[2]);
1833 roll = NormalizedVecAngle2(new_up_axis, up_axis);
1835 Crossf(vec, up_axis, new_up_axis);
1837 if (Inpf(vec, nor) < 0)
1845 /* Sets the roll value of selected bones, depending on the mode
1846 * mode == 0: their z-axes point upwards
1847 * mode == 1: their z-axes point towards 3d-cursor
1849 void auto_align_armature(short mode)
1851 bArmature *arm= G.obedit->data;
1853 EditBone *flipbone = NULL;
1856 float *cursor= give_cursor();
1858 for (ebone = G.edbo.first; ebone; ebone=ebone->next) {
1859 if (EBONE_VISIBLE(arm, ebone)) {
1860 if (arm->flag & ARM_MIRROR_EDIT)
1861 flipbone = armature_bone_get_mirrored(ebone);
1863 if ((ebone->flag & BONE_SELECTED) ||
1864 (flipbone && (flipbone->flag & BONE_SELECTED)))
1866 /* specific method used to calculate roll depends on mode */
1868 /* Z-Axis point towards cursor */
1869 float mat[4][4], tmat[4][4], imat[4][4];
1870 float rmat[4][4], rot[3];
1873 /* find the current bone matrix as a 4x4 matrix (in Armature Space) */
1874 VecSubf(delta, ebone->tail, ebone->head);
1875 vec_roll_to_mat3(delta, ebone->roll, curmat);
1876 Mat4CpyMat3(mat, curmat);
1877 VECCOPY(mat[3], ebone->head);
1879 /* multiply bone-matrix by object matrix (so that bone-matrix is in WorldSpace) */
1880 Mat4MulMat4(tmat, mat, G.obedit->obmat);
1881 Mat4Invert(imat, tmat);
1883 /* find position of cursor relative to bone */
1884 VecMat4MulVecfl(vec, imat, cursor);
1886 /* check that cursor is in usable position */
1887 if ((IS_EQ(vec[0], 0)==0) && (IS_EQ(vec[2], 0)==0)) {
1888 /* Compute a rotation matrix around y */
1889 rot[1] = atan2(vec[0], vec[2]);
1890 rot[0] = rot[2] = 0.0f;
1891 EulToMat4(rot, rmat);
1893 /* Multiply the bone matrix by rotation matrix. This should be new bone-matrix */
1894 Mat4MulMat4(tmat, rmat, mat);
1895 Mat3CpyMat4(curmat, tmat);
1897 /* Now convert from new bone-matrix, back to a roll value (in radians) */
1898 mat3_to_vec_roll(curmat, delta, &ebone->roll);
1902 /* Z-Axis Point Up */
1903 float xaxis[3]={1.0, 0.0, 0.0}, yaxis[3], zaxis[3]={0.0, 0.0, 1.0};
1904 float targetmat[3][3], imat[3][3], diffmat[3][3];
1906 /* Find the current bone matrix */
1907 VecSubf(delta, ebone->tail, ebone->head);
1908 vec_roll_to_mat3(delta, 0.0, curmat);
1910 /* Make new matrix based on y axis & z-up */
1911 VECCOPY (yaxis, curmat[1]);
1914 VECCOPY (targetmat[0], xaxis);
1915 VECCOPY (targetmat[1], yaxis);
1916 VECCOPY (targetmat[2], zaxis);
1917 Mat3Ortho(targetmat);
1919 /* Find the difference between the two matrices */
1920 Mat3Inv(imat, targetmat);
1921 Mat3MulMat3(diffmat, imat, curmat);
1923 ebone->roll = atan2(diffmat[2][0], diffmat[2][2]);
1930 /* **************** undo for armatures ************** */
1932 static void undoBones_to_editBones(void *lbv)
1935 EditBone *ebo, *newebo;
1937 BLI_freelistN(&G.edbo);
1940 for(ebo= lb->first; ebo; ebo= ebo->next) {
1941 newebo= MEM_dupallocN(ebo);
1943 BLI_addtail(&G.edbo, newebo);
1947 for(newebo= G.edbo.first; newebo; newebo= newebo->next) {
1948 if(newebo->parent) newebo->parent= newebo->parent->temp;
1950 /* be sure they dont hang ever */
1951 for(newebo= G.edbo.first; newebo; newebo= newebo->next) {
1956 static void *editBones_to_undoBones(void)
1959 EditBone *ebo, *newebo;
1961 lb= MEM_callocN(sizeof(ListBase), "listbase undo");
1964 for(ebo= G.edbo.first; ebo; ebo= ebo->next) {
1965 newebo= MEM_dupallocN(ebo);
1967 BLI_addtail(lb, newebo);
1971 for(newebo= lb->first; newebo; newebo= newebo->next) {
1972 if(newebo->parent) newebo->parent= newebo->parent->temp;
1978 static void free_undoBones(void *lbv)
1986 /* and this is all the undo system needs to know */
1987 void undo_push_armature(char *name)
1989 undo_editmode_push(name, free_undoBones, undoBones_to_editBones, editBones_to_undoBones, NULL);
1994 /* **************** END EditMode stuff ********************** */
1995 /* *************** Adding stuff in editmode *************** */
1997 EditBone *addEditBone(char *name, ListBase *ebones, bArmature *arm)
1999 EditBone *bone= MEM_callocN(sizeof(EditBone), "eBone");
2001 BLI_strncpy(bone->name, name, 32);
2002 unique_editbone_name(ebones, bone->name);
2004 BLI_addtail(ebones, bone);
2006 bone->flag |= BONE_TIPSEL;
2013 bone->rad_head= 0.10;
2014 bone->rad_tail= 0.05;
2016 bone->layer= arm->layer;
2021 /* default bone add, returns it selected, but without tail set */
2022 static EditBone *add_editbone(char *name)
2024 bArmature *arm= G.obedit->data;
2026 return addEditBone(name, &G.edbo, arm);
2029 static void add_primitive_bone(Object *ob, short newob)
2031 float obmat[3][3], curs[3], viewmat[3][3], totmat[3][3], imat[3][3];
2034 VECCOPY(curs, give_cursor());
2036 /* Get inverse point for head and orientation for tail */
2037 Mat4Invert(G.obedit->imat, G.obedit->obmat);
2038 Mat4MulVecfl(G.obedit->imat, curs);
2040 if ( !(newob) || (U.flag & USER_ADD_VIEWALIGNED) ) Mat3CpyMat4(obmat, G.vd->viewmat);
2041 else Mat3One(obmat);
2043 Mat3CpyMat4(viewmat, G.obedit->obmat);
2044 Mat3MulMat3(totmat, obmat, viewmat);
2045 Mat3Inv(imat, totmat);
2047 deselectall_armature(0, 0);
2050 bone= add_editbone("Bone");
2052 VECCOPY(bone->head, curs);
2054 if ( !(newob) || (U.flag & USER_ADD_VIEWALIGNED) )
2055 VecAddf(bone->tail, bone->head, imat[1]); // bone with unit length 1
2057 VecAddf(bone->tail, bone->head, imat[2]); // bone with unit length 1, pointing up Z
2061 void add_primitiveArmature(int type)
2065 if(G.scene->id.lib) return;
2067 /* this function also comes from an info window */
2068 if ELEM(curarea->spacetype, SPACE_VIEW3D, SPACE_INFO); else return;
2069 if (G.vd==NULL) return;
2071 G.f &= ~(G_VERTEXPAINT+G_TEXTUREPAINT+G_WEIGHTPAINT+G_SCULPTMODE);
2072 setcursor_space(SPACE_VIEW3D, CURSOR_STD);
2074 check_editmode(OB_ARMATURE);
2076 /* If we're not the "obedit", make a new object and enter editmode */
2077 if (G.obedit==NULL) {
2078 add_object(OB_ARMATURE);
2079 base_init_from_view3d(BASACT, G.vd);
2080 G.obedit= BASACT->object;
2082 where_is_object(G.obedit);
2084 make_editArmature();
2085 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
2089 /* no primitive support yet */
2090 add_primitive_bone(G.obedit, newob);
2092 countall(); // flushes selection!
2094 if ((newob) && !(U.flag & USER_ADD_EDITMODE)) {
2098 allqueue(REDRAWALL, 0);
2099 BIF_undo_push("Add primitive");
2102 /* the ctrl-click method */
2103 void addvert_armature(void)
2105 bArmature *arm= G.obedit->data;
2106 EditBone *ebone, *newbone, *flipbone;
2107 float *curs, mat[3][3],imat[3][3];
2112 /* find the active or selected bone */
2113 for (ebone = G.edbo.first; ebone; ebone=ebone->next) {
2114 if (EBONE_VISIBLE(arm, ebone)) {
2115 if (ebone->flag & (BONE_ACTIVE|BONE_TIPSEL))
2121 for (ebone = G.edbo.first; ebone; ebone=ebone->next) {
2122 if (EBONE_VISIBLE(arm, ebone)) {
2123 if (ebone->flag & (BONE_ACTIVE|BONE_ROOTSEL))
2133 deselectall_armature(0, 0);
2135 /* we re-use code for mirror editing... */
2137 if (arm->flag & ARM_MIRROR_EDIT)
2138 flipbone= armature_bone_get_mirrored(ebone);
2140 for (a=0; a<2; a++) {
2145 SWAP(EditBone *, flipbone, ebone);
2149 newbone= add_editbone(ebone->name);
2150 newbone->flag |= BONE_ACTIVE;
2153 VECCOPY(newbone->head, ebone->head);
2154 newbone->rad_head= ebone->rad_tail;
2155 newbone->parent= ebone->parent;
2158 VECCOPY(newbone->head, ebone->tail);
2159 newbone->rad_head= ebone->rad_tail;
2160 newbone->parent= ebone;
2161 newbone->flag |= BONE_CONNECTED;
2164 curs= give_cursor();
2165 VECCOPY(newbone->tail, curs);
2166 VecSubf(newbone->tail, newbone->tail, G.obedit->obmat[3]);
2169 newbone->tail[0]= -newbone->tail[0];
2171 Mat3CpyMat4(mat, G.obedit->obmat);
2173 Mat3MulVecfl(imat, newbone->tail);
2175 newbone->length= VecLenf(newbone->head, newbone->tail);
2176 newbone->rad_tail= newbone->length*0.05f;
2177 newbone->dist= newbone->length*0.25f;
2183 BIF_undo_push("Add Bone");
2184 allqueue(REDRAWVIEW3D, 0);
2187 /* adds an EditBone between the nominated locations (should be in the right space) */
2188 static EditBone *add_points_bone (float head[], float tail[])
2192 ebo= add_editbone("Bone");
2194 VECCOPY(ebo->head, head);
2195 VECCOPY(ebo->tail, tail);
2201 static EditBone *get_named_editbone(char *name)
2206 for (eBone=G.edbo.first; eBone; eBone=eBone->next) {
2207 if (!strcmp(name, eBone->name))
2215 static void update_dup_subtarget(EditBone *dupBone)
2217 /* If an edit bone has been duplicated, lets
2218 * update it's constraints if the subtarget
2219 * they point to has also been duplicated
2221 EditBone *oldtarget, *newtarget;
2223 bConstraint *curcon;
2226 if ( (chan = verify_pose_channel(OBACT->pose, dupBone->name)) ) {
2227 if ( (conlist = &chan->constraints) ) {
2228 for (curcon = conlist->first; curcon; curcon=curcon->next) {
2229 /* does this constraint have a subtarget in
2232 bConstraintTypeInfo *cti= constraint_get_typeinfo(curcon);
2233 ListBase targets = {NULL, NULL};
2234 bConstraintTarget *ct;
2236 if (cti && cti->get_constraint_targets) {
2237 cti->get_constraint_targets(curcon, &targets);
2239 for (ct= targets.first; ct; ct= ct->next) {
2240 if ((ct->tar == G.obedit) && (ct->subtarget[0])) {
2241 oldtarget = get_named_editbone(ct->subtarget);
2243 /* was the subtarget bone duplicated too? If
2244 * so, update the constraint to point at the
2245 * duplicate of the old subtarget.
2247 if (oldtarget->flag & BONE_SELECTED){
2248 newtarget = (EditBone *) oldtarget->temp;
2249 strcpy(ct->subtarget, newtarget->name);
2255 if (cti->flush_constraint_targets)
2256 cti->flush_constraint_targets(curcon, &targets, 0);
2264 void adduplicate_armature(void)
2266 bArmature *arm= G.obedit->data;
2267 EditBone *eBone = NULL;
2269 EditBone *firstDup=NULL; /* The beginning of the duplicated bones in the edbo list */
2271 countall(); // flushes selection!
2273 /* Select mirrored bones */
2274 if (arm->flag & ARM_MIRROR_EDIT) {
2275 for (curBone=G.edbo.first; curBone; curBone=curBone->next) {
2276 if (EBONE_VISIBLE(arm, curBone)) {
2277 if (curBone->flag & BONE_SELECTED) {
2278 eBone = armature_bone_get_mirrored(curBone);
2280 eBone->flag |= BONE_SELECTED;
2286 /* Find the selected bones and duplicate them as needed */
2287 for (curBone=G.edbo.first; curBone && curBone!=firstDup; curBone=curBone->next) {
2288 if (EBONE_VISIBLE(arm, curBone)) {
2289 if (curBone->flag & BONE_SELECTED) {
2290 eBone=MEM_callocN(sizeof(EditBone), "addup_editbone");
2291 eBone->flag |= BONE_SELECTED;
2293 /* Copy data from old bone to new bone */
2294 memcpy(eBone, curBone, sizeof(EditBone));
2296 curBone->temp = eBone;
2297 eBone->temp = curBone;
2299 unique_editbone_name(&G.edbo, eBone->name);
2300 BLI_addtail(&G.edbo, eBone);
2304 /* Lets duplicate the list of constraints that the
2308 bPoseChannel *chanold, *channew;
2309 ListBase *listold, *listnew;
2311 chanold = verify_pose_channel(OBACT->pose, curBone->name);
2313 listold = &chanold->constraints;
2315 /* WARNING: this creates a new posechannel, but there will not be an attached bone
2316 * yet as the new bones created here are still 'EditBones' not 'Bones'.
2319 verify_pose_channel(OBACT->pose, eBone->name);
2321 /* copy transform locks */
2322 channew->protectflag = chanold->protectflag;
2324 /* copy bone group */
2325 channew->agrp_index= chanold->agrp_index;
2327 /* ik (dof) settings */
2328 channew->ikflag = chanold->ikflag;
2329 VECCOPY(channew->limitmin, chanold->limitmin);
2330 VECCOPY(channew->limitmax, chanold->limitmax);
2331 VECCOPY(channew->stiffness, chanold->stiffness);
2332 channew->ikstretch= chanold->ikstretch;
2335 listnew = &channew->constraints;
2336 copy_constraints(listnew, listold);
2339 channew->custom= chanold->custom;
2348 /* Run though the list and fix the pointers */
2349 for (curBone=G.edbo.first; curBone && curBone!=firstDup; curBone=curBone->next) {
2350 if (EBONE_VISIBLE(arm, curBone)) {
2351 if (curBone->flag & BONE_SELECTED) {
2352 eBone=(EditBone*) curBone->temp;
2354 /* If this bone has no parent,
2355 Set the duplicate->parent to NULL
2357 if (!curBone->parent)
2358 eBone->parent = NULL;
2359 /* If this bone has a parent that IS selected,
2360 Set the duplicate->parent to the curBone->parent->duplicate
2362 else if (curBone->parent->flag & BONE_SELECTED)
2363 eBone->parent= (EditBone *)curBone->parent->temp;
2364 /* If this bone has a parent that IS not selected,
2365 Set the duplicate->parent to the curBone->parent
2368 eBone->parent=(EditBone*) curBone->parent;
2369 eBone->flag &= ~BONE_CONNECTED;
2372 /* Lets try to fix any constraint subtargets that might
2373 have been duplicated */
2374 update_dup_subtarget(eBone);
2379 /* Deselect the old bones and select the new ones */
2381 for (curBone=G.edbo.first; curBone && curBone!=firstDup; curBone=curBone->next) {
2382 if (EBONE_VISIBLE(arm, curBone))
2383 curBone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL | BONE_ACTIVE);
2386 BIF_TransformSetUndo("Add Duplicate");
2387 initTransform(TFM_TRANSLATION, CTX_NO_PET);
2390 allqueue(REDRAWBUTSEDIT, 0);
2391 allqueue(REDRAWBUTSOBJECT, 0);
2392 allqueue(REDRAWOOPS, 0);
2397 /* *************** END Adding stuff in editmode *************** */
2398 /* ************** Add/Remove stuff in editmode **************** */
2400 /* temporary data-structure for merge/fill bones */
2401 typedef struct EditBonePoint {
2402 struct EditBonePoint *next, *prev;
2404 EditBone *head_owner; /* EditBone which uses this point as a 'head' point */
2405 EditBone *tail_owner; /* EditBone which uses this point as a 'tail' point */
2407 float vec[3]; /* the actual location of the point in local/EditMode space */
2410 /* find chain-tips (i.e. bones without children) */
2411 static void chains_find_tips (ListBase *list)
2413 EditBone *curBone, *ebo;
2416 /* note: this is potentially very slow ... there's got to be a better way */
2417 for (curBone= G.edbo.first; curBone; curBone= curBone->next) {
2420 /* is this bone contained within any existing chain? (skip if so) */
2421 for (ld= list->first; ld; ld= ld->next) {
2422 for (ebo= ld->data; ebo; ebo= ebo->parent) {
2423 if (ebo == curBone) {
2431 /* skip current bone if it is part of an existing chain */
2434 /* is any existing chain part of the chain formed by this bone? */
2436 for (ebo= curBone->parent; ebo; ebo= ebo->parent) {
2437 for (ld= list->first; ld; ld= ld->next) {
2438 if (ld->data == ebo) {
2447 /* current bone has already been added to a chain? */
2450 /* add current bone to a new chain */
2451 ld= MEM_callocN(sizeof(LinkData), "BoneChain");
2453 BLI_addtail(list, ld);
2458 static void fill_add_joint (EditBone *ebo, short eb_tail, ListBase *points)
2465 VECCOPY(vec, ebo->tail);
2468 VECCOPY(vec, ebo->head);
2471 for (ebp= points->first; ebp; ebp= ebp->next) {
2472 if (VecEqual(ebp->vec, vec)) {
2474 if ((ebp->head_owner) && (ebp->head_owner->parent == ebo)) {
2475 /* so this bone's tail owner is this bone */
2476 ebp->tail_owner= ebo;
2482 if ((ebp->tail_owner) && (ebo->parent == ebp->tail_owner)) {
2483 /* so this bone's head owner is this bone */
2484 ebp->head_owner= ebo;
2492 /* allocate a new point if no existing point was related */
2494 ebp= MEM_callocN(sizeof(EditBonePoint), "EditBonePoint");
2497 VECCOPY(ebp->vec, ebo->tail);
2498 ebp->tail_owner= ebo;
2501 VECCOPY(ebp->vec, ebo->head);
2502 ebp->head_owner= ebo;
2505 BLI_addtail(points, ebp);
2509 /* bone adding between selected joints */
2510 void fill_bones_armature(void)
2512 bArmature *arm= G.obedit->data;
2513 EditBone *ebo, *newbone=NULL;
2514 ListBase points = {NULL, NULL};
2517 /* loop over all bones, and only consider if visible */
2518 for (ebo= G.edbo.first; ebo; ebo= ebo->next) {
2519 if (EBONE_VISIBLE(arm, ebo)) {
2520 if (!(ebo->flag & BONE_CONNECTED) && (ebo->flag & BONE_ROOTSEL))
2521 fill_add_joint(ebo, 0, &points);
2522 if (ebo->flag & BONE_TIPSEL)
2523 fill_add_joint(ebo, 1, &points);
2527 /* the number of joints determines how we fill:
2528 * 1) between joint and cursor (joint=head, cursor=tail)
2529 * 2) between the two joints (order is dependent on active-bone/hierachy)
2530 * 3+) error (a smarter method involving finding chains needs to be worked out
2532 count= BLI_countlist(&points);
2535 error("No joints selected");
2538 else if (count == 1) {
2542 /* Get Points - selected joint */
2543 ebp= (EditBonePoint *)points.first;
2545 /* Get points - cursor (tail) */
2546 VECCOPY (curs, give_cursor());
2548 Mat4Invert(G.obedit->imat, G.obedit->obmat);
2549 Mat4MulVecfl(G.obedit->imat, curs);
2552 newbone= add_points_bone(ebp->vec, curs);
2554 else if (count == 2) {
2555 EditBonePoint *ebp, *ebp2;
2556 float head[3], tail[3];
2559 /* check that the points don't belong to the same bone */
2560 ebp= (EditBonePoint *)points.first;
2563 if ((ebp->head_owner==ebp2->tail_owner) && (ebp->head_owner!=NULL)) {
2564 error("Same bone selected...");
2565 BLI_freelistN(&points);
2568 if ((ebp->tail_owner==ebp2->head_owner) && (ebp->tail_owner!=NULL)) {
2569 error("Same bone selected...");
2570 BLI_freelistN(&points);
2574 /* find which one should be the 'head' */
2575 if ((ebp->head_owner && ebp2->head_owner) || (ebp->tail_owner && ebp2->tail_owner)) {
2576 /* rule: whichever one is closer to 3d-cursor */
2578 float vecA[3], vecB[3];
2581 /* get cursor location */
2582 VECCOPY(curs, give_cursor());
2584 Mat4Invert(G.obedit->imat, G.obedit->obmat);
2585 Mat4MulVecfl(G.obedit->imat, curs);
2588 VecSubf(vecA, ebp->vec, curs);
2589 VecSubf(vecB, ebp2->vec, curs);
2590 distA= VecLength(vecA);
2591 distB= VecLength(vecB);
2593 /* compare distances - closer one therefore acts as direction for bone to go */
2594 headtail= (distA < distB) ? 2 : 1;
2596 else if (ebp->head_owner) {
2599 else if (ebp2->head_owner) {
2603 /* assign head/tail combinations */
2604 if (headtail == 2) {
2605 VECCOPY(head, ebp->vec);
2606 VECCOPY(tail, ebp2->vec);
2608 else if (headtail == 1) {
2609 VECCOPY(head, ebp2->vec);
2610 VECCOPY(tail, ebp->vec);
2613 /* add new bone and parent it to the appropriate end */
2615 newbone= add_points_bone(head, tail);
2617 /* do parenting (will need to set connected flag too) */
2618 if (headtail == 2) {
2619 /* ebp tail or head - tail gets priority */
2620 if (ebp->tail_owner)
2621 newbone->parent= ebp->tail_owner;
2623 newbone->parent= ebp->head_owner;
2626 /* ebp2 tail or head - tail gets priority */
2627 if (ebp2->tail_owner)
2628 newbone->parent= ebp2->tail_owner;
2630 newbone->parent= ebp2->head_owner;
2633 newbone->flag |= BONE_CONNECTED;
2637 // FIXME.. figure out a method for multiple bones
2638 error("Too many points selected");
2639 printf("Points selected: %d \n", count);
2640 BLI_freelistN(&points);
2645 BLI_freelistN(&points);
2647 /* undo + updates */
2648 allqueue(REDRAWVIEW3D, 0);
2649 allqueue(REDRAWBUTSEDIT, 0);
2650 BIF_undo_push("Fill Bones");
2653 /* this function merges between two bones, removes them and those in-between,
2654 * and adjusts the parent relationships for those in-between
2656 static void bones_merge(EditBone *start, EditBone *end, EditBone *endchild, ListBase *chains)
2658 EditBone *ebo, *ebone, *newbone;
2660 float head[3], tail[3];
2662 /* check if same bone */
2664 printf("Error: same bone! \n");
2665 printf("\tstart = %s, end = %s \n", start->name, end->name);
2668 /* step 1: add a new bone
2669 * - head = head/tail of start (default head)
2670 * - tail = head/tail of end (default tail)
2671 * - parent = parent of start
2673 if ((start->flag & BONE_TIPSEL) && !(start->flag & (BONE_SELECTED|BONE_ACTIVE))) {
2674 VECCOPY(head, start->tail);
2677 VECCOPY(head, start->head);
2679 if ((end->flag & BONE_ROOTSEL) && !(end->flag & (BONE_SELECTED|BONE_ACTIVE))) {
2680 VECCOPY(tail, end->head);
2683 VECCOPY(tail, end->tail);
2685 newbone= add_points_bone(head, tail);
2686 newbone->parent = start->parent;
2688 /* step 2a: parent children of in-between bones to newbone */
2689 for (chain= chains->first; chain; chain= chain->next) {
2690 /* ick: we need to check if parent of each bone in chain is one of the bones in the */
2691 for (ebo= chain->data; ebo; ebo= ebo->parent) {
2694 /* try to find which bone from the list to be removed, is the parent */
2695 for (ebone= end; ebone; ebone= ebone->parent) {
2696 if (ebo->parent == ebone) {
2702 /* adjust this bone's parent to newbone then */
2704 ebo->parent= newbone;
2710 /* step 2b: parent child of end to newbone (child from this chain) */
2712 endchild->parent= newbone;
2714 /* step 3: delete all bones between and including start and end */
2715 for (ebo= end; ebo; ebo= ebone) {
2716 ebone= (ebo == start) ? (NULL) : (ebo->parent);
2717 BLI_freelinkN(&G.edbo, ebo);
2721 /* bone merging - has a menu! */
2722 void merge_armature(void)
2724 bArmature *arm= G.obedit->data;
2727 /* process a menu to determine how to merge */
2728 // TODO: there's room for more modes of merging stuff...
2729 val= pupmenu("Merge Selected Bones%t|Within Chains%x1");
2730 if (val <= 0) return;
2733 /* go down chains, merging bones */
2734 ListBase chains = {NULL, NULL};
2735 LinkData *chain, *nchain;
2738 /* get chains (ends on chains) */
2739 chains_find_tips(&chains);
2740 if (chains.first == NULL) return;
2742 /* each 'chain' is the last bone in the chain (with no children) */
2743 for (chain= chains.first; chain; chain= nchain) {
2744 EditBone *bstart= NULL, *bend= NULL;
2745 EditBone *bchild= NULL, *child=NULL;
2747 /* temporarily remove chain from list of chains */
2748 nchain= chain->next;
2749 BLI_remlink(&chains, chain);
2751 /* only consider bones that are visible and selected */
2752 for (ebo=chain->data; ebo; child=ebo, ebo=ebo->parent) {
2753 /* check if visible + selected */
2754 if ( EBONE_VISIBLE(arm, ebo) &&
2755 ((ebo->flag & BONE_CONNECTED) || (ebo->parent==NULL)) &&
2756 (ebo->flag & (BONE_SELECTED|BONE_ACTIVE)) )
2758 /* set either end or start (end gets priority, unless it is already set) */
2767 /* chain is broken... merge any continous segments then clear */
2769 bones_merge(bstart, bend, bchild, &chains);
2777 /* merge from bstart to bend if something not merged */
2779 bones_merge(bstart, bend, bchild, &chains);
2782 BLI_insertlinkbefore(&chains, nchain, chain);
2785 BLI_freelistN(&chains);
2788 /* undo + updates */
2790 allqueue(REDRAWVIEW3D, 0);
2791 allqueue(REDRAWBUTSEDIT, 0);
2792 BIF_undo_push("Merge Bones");
2795 /* ************** END Add/Remove stuff in editmode ************ */
2796 /* *************** Tools in editmode *********** */
2799 void hide_selected_armature_bones(void)
2801 bArmature *arm= G.obedit->data;
2804 for (ebone = G.edbo.first; ebone; ebone=ebone->next) {
2805 if (EBONE_VISIBLE(arm, ebone)) {
2806 if (ebone->flag & (BONE_SELECTED)) {
2807 ebone->flag &= ~(BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL|BONE_ACTIVE);
2808 ebone->flag |= BONE_HIDDEN_A;
2813 allqueue(REDRAWVIEW3D, 0);
2814 allqueue(REDRAWBUTSEDIT, 0);
2815 BIF_undo_push("Hide Bones");
2818 void hide_unselected_armature_bones(void)
2822 for (ebone = G.edbo.first; ebone; ebone=ebone->next) {
2823 bArmature *arm= G.obedit->data;
2824 if (EBONE_VISIBLE(arm, ebone)) {
2825 if (ebone->flag & (BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL));
2827 ebone->flag &= ~BONE_ACTIVE;
2828 ebone->flag |= BONE_HIDDEN_A;