4 * ***** BEGIN GPL/BL DUAL 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. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
31 * editarmature.c: Interface for creating and posing armature objects
43 #include "MEM_guardedalloc.h"
47 #include "DNA_action_types.h"
48 #include "DNA_armature_types.h"
49 #include "DNA_constraint_types.h"
51 #include "DNA_mesh_types.h"
52 #include "DNA_meshdata_types.h"
53 #include "DNA_object_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_screen_types.h"
56 #include "DNA_space_types.h"
57 #include "DNA_view3d_types.h"
58 #include "DNA_modifier_types.h"
60 #include "BLI_blenlib.h"
61 #include "BLI_arithb.h"
62 #include "BLI_editVert.h"
64 #include "BKE_action.h"
65 #include "BKE_armature.h"
66 #include "BKE_constraint.h"
67 #include "BKE_deform.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_global.h"
71 #include "BKE_object.h"
72 #include "BKE_subsurf.h"
73 #include "BKE_utildefines.h"
74 #include "BKE_modifier.h"
76 #include "BIF_editaction.h"
77 #include "BIF_editmode_undo.h"
78 #include "BIF_editdeform.h"
79 #include "BIF_editarmature.h"
80 #include "BIF_editconstraint.h"
82 #include "BIF_graphics.h"
83 #include "BIF_interface.h"
84 #include "BIF_poseobject.h"
85 #include "BIF_mywindow.h"
86 #include "BIF_resources.h"
87 #include "BIF_screen.h"
88 #include "BIF_space.h"
89 #include "BIF_toolbox.h"
90 #include "BIF_transform.h"
92 #include "BDR_editobject.h"
93 #include "BDR_drawobject.h"
97 #include "BSE_trans_types.h"
101 #include "mydevice.h"
105 extern float centre[3], centroid[3]; /* Originally defined in editobject.c */
108 #define TEST_EDITARMATURE {if(G.obedit==0) return; if( (G.vd->lay & G.obedit->lay)==0 ) return;}
111 /* **************** tools on Editmode Armature **************** */
113 /* converts Bones to EditBone list, used for tools as well */
114 static void make_boneList(ListBase* list, ListBase *bones, EditBone *parent)
124 for (curBone=bones->first; curBone; curBone=curBone->next){
125 eBone= MEM_callocN(sizeof(EditBone), "make_editbone");
127 /* Copy relevant data from bone to eBone */
128 eBone->parent=parent;
129 strcpy (eBone->name, curBone->name);
130 eBone->flag = curBone->flag;
132 /* fix selection flags */
133 if(eBone->flag & BONE_SELECTED) {
134 eBone->flag |= BONE_TIPSEL;
135 if(eBone->parent && (eBone->flag & BONE_IK_TOPARENT))
136 eBone->parent->flag |= BONE_TIPSEL;
138 eBone->flag |= BONE_ROOTSEL;
141 VECCOPY(eBone->head, curBone->arm_head);
142 VECCOPY(eBone->tail, curBone->arm_tail);
147 VecSubf (delta, eBone->tail, eBone->head);
148 vec_roll_to_mat3(delta, 0.0, postmat);
150 Mat3CpyMat4(premat, curBone->arm_mat);
152 Mat3Inv(imat, postmat);
153 Mat3MulMat3(difmat, imat, premat);
155 eBone->roll = atan(difmat[2][0]/difmat[2][2]);
156 if (difmat[0][0]<0.0) eBone->roll +=M_PI;
158 /* rest of stuff copy */
159 eBone->length= curBone->length;
160 eBone->dist= curBone->dist;
161 eBone->weight= curBone->weight;
162 eBone->xwidth= curBone->xwidth;
163 eBone->zwidth= curBone->zwidth;
164 eBone->ease1= curBone->ease1;
165 eBone->ease2= curBone->ease2;
166 eBone->rad_head= curBone->rad_head;
167 eBone->rad_tail= curBone->rad_tail;
168 eBone->segments = curBone->segments;
169 eBone->boneclass = curBone->boneclass;
171 BLI_addtail (list, eBone);
173 /* Add children if necessary */
174 if (curBone->childbase.first)
175 make_boneList (list, &curBone->childbase, eBone);
179 /* nasty stuff for converting roll in editbones into bones */
180 /* also sets restposition in armature (arm_mat) */
181 static void fix_bonelist_roll (ListBase *bonelist, ListBase *editbonelist)
191 for (curBone=bonelist->first; curBone; curBone=curBone->next) {
192 /* sets local matrix and arm_mat (restpos) */
193 where_is_armature_bone(curBone, curBone->parent);
195 /* Find the associated editbone */
196 for (ebone = editbonelist->first; ebone; ebone=ebone->next)
197 if ((Bone*)ebone->temp == curBone)
201 /* Get the ebone premat */
202 VecSubf (delta, ebone->tail, ebone->head);
203 vec_roll_to_mat3(delta, ebone->roll, premat);
205 /* Get the bone postmat */
206 Mat3CpyMat4(postmat, curBone->arm_mat);
208 Mat3Inv(imat, premat);
209 Mat3MulMat3(difmat, imat, postmat);
211 printf ("Bone %s\n", curBone->name);
212 printmatrix4 ("premat", premat);
213 printmatrix4 ("postmat", postmat);
214 printmatrix4 ("difmat", difmat);
215 printf ("Roll = %f\n", (-atan(difmat[2][0]/difmat[2][2]) * (180.0/M_PI)));
217 curBone->roll = -atan(difmat[2][0]/difmat[2][2]);
219 if (difmat[0][0]<0.0) curBone->roll +=M_PI;
221 /* and set restposition again */
222 where_is_armature_bone(curBone, curBone->parent);
224 fix_bonelist_roll (&curBone->childbase, editbonelist);
228 /* converts the editbones back to the armature */
229 static void editbones_to_armature (ListBase *list, Object *ob)
236 arm = get_armature(ob);
243 /* Copy the bones from the editData into the armature */
244 for (eBone=list->first;eBone;eBone=eBone->next){
245 newBone= MEM_callocN (sizeof(Bone), "bone");
246 eBone->temp= newBone; /* Associate the real Bones with the EditBones */
248 strcpy (newBone->name, eBone->name);
249 memcpy (newBone->head, eBone->head, sizeof(float)*3);
250 memcpy (newBone->tail, eBone->tail, sizeof(float)*3);
251 newBone->flag= eBone->flag;
252 newBone->roll = 0.0f;
254 newBone->weight = eBone->weight;
255 newBone->dist = eBone->dist;
257 newBone->xwidth = eBone->xwidth;
258 newBone->zwidth = eBone->zwidth;
259 newBone->ease1= eBone->ease1;
260 newBone->ease2= eBone->ease2;
261 newBone->rad_head= eBone->rad_head;
262 newBone->rad_tail= eBone->rad_tail;
263 newBone->segments= eBone->segments;
264 newBone->boneclass = eBone->boneclass;
268 /* Fix parenting in a separate pass to ensure ebone->bone connections
269 are valid at this point */
270 for (eBone=list->first;eBone;eBone=eBone->next) {
271 newBone= (Bone*) eBone->temp;
273 newBone->parent=(Bone*) eBone->parent->temp;
274 BLI_addtail (&newBone->parent->childbase,newBone);
277 float M_boneRest[3][3];
278 float M_parentRest[3][3];
279 float iM_parentRest[3][3];
282 /* Get the parent's matrix (rotation only) */
283 VecSubf (delta, eBone->parent->tail, eBone->parent->head);
284 vec_roll_to_mat3(delta, eBone->parent->roll, M_parentRest);
286 /* Get this bone's matrix (rotation only) */
287 VecSubf (delta, eBone->tail, eBone->head);
288 vec_roll_to_mat3(delta, eBone->roll, M_boneRest);
290 /* Invert the parent matrix */
291 Mat3Inv(iM_parentRest, M_parentRest);
293 /* Get the new head and tail */
294 VecSubf (newBone->head, eBone->head, eBone->parent->tail);
295 VecSubf (newBone->tail, eBone->tail, eBone->parent->tail);
297 Mat3MulVecfl(iM_parentRest, newBone->head);
298 Mat3MulVecfl(iM_parentRest, newBone->tail);
301 /* ...otherwise add this bone to the armature's bonebase */
303 BLI_addtail (&arm->bonebase,newBone);
306 /* Make a pass through the new armature to fix rolling */
307 /* also builds restposition again (like where_is_armature) */
308 fix_bonelist_roll (&arm->bonebase, list);
310 /* so all users of this armature should get rebuilt */
311 for(obt= G.main->object.first; obt; obt= obt->id.next) {
313 armature_rebuild_pose(obt, arm);
316 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
321 void apply_rot_armature (Object *ob, float mat[3][3])
327 arm = get_armature(ob);
332 /* Put the armature into editmode */
333 list.first= list.last = NULL;
334 make_boneList(&list, &arm->bonebase, NULL);
336 /* Do the rotations */
337 for (ebone = list.first; ebone; ebone=ebone->next){
338 Mat3MulVecfl(mat, ebone->head);
339 Mat3MulVecfl(mat, ebone->tail);
342 /* Turn the list into an armature */
343 editbones_to_armature(&list, ob);
345 /* Free the editbones */
347 BLI_freelistN (&list);
351 void join_armature(void)
354 Base *base, *nextbase;
356 EditBone *curbone, *next;
357 float mat[4][4], imat[4][4];
359 /* Ensure we're not in editmode and that the active object is an armature*/
363 if(ob->type!=OB_ARMATURE) return;
365 /* Make sure the user wants to continue*/
366 if(okee("Join selected armatures")==0) return;
368 /* Put the active armature into editmode and join the bones from the other one*/
372 for (base=FIRSTBASE; base; base=nextbase) {
373 nextbase = base->next;
375 if ((base->object->type==OB_ARMATURE) && (base->object!=ob)){
376 /* Make a list of editbones */
377 eblist.first=eblist.last= NULL;
378 make_boneList (&eblist, &((bArmature*)base->object->data)->bonebase,NULL);
379 /* Find the difference matrix */
380 Mat4Invert(imat, ob->obmat);
381 Mat4MulMat4(mat, base->object->obmat, imat);
383 /* Copy bones from the object to the edit armature */
384 for (curbone=eblist.first; curbone; curbone=next){
385 next = curbone->next;
387 unique_editbone_name (curbone->name);
389 /* Transform the bone */
399 VecSubf (delta, curbone->tail, curbone->head);
400 vec_roll_to_mat3(delta, curbone->roll, temp);
402 Mat4MulMat34 (premat, temp, mat);
404 Mat4MulVecfl(mat, curbone->head);
405 Mat4MulVecfl(mat, curbone->tail);
407 /* Get the postmat */
408 VecSubf (delta, curbone->tail, curbone->head);
409 vec_roll_to_mat3(delta, curbone->roll, temp);
410 Mat4CpyMat3(postmat, temp);
413 Mat4Invert (imat, premat);
414 Mat4MulMat4 (difmat, postmat, imat);
416 curbone->roll -=atan(difmat[2][0]/difmat[2][2]);
419 curbone->roll +=M_PI;
422 BLI_remlink(&eblist, curbone);
423 BLI_addtail(&G.edbo, curbone);
426 free_and_unlink_base(base);
431 DAG_scene_sort(G.scene); // because we removed object(s)
434 allqueue(REDRAWVIEW3D, 0);
435 allqueue(REDRAWOOPS, 0);
439 /* **************** END tools on Editmode Armature **************** */
440 /* **************** PoseMode & EditMode *************************** */
442 /* only for opengl selection indices */
443 Bone *get_indexed_bone (Object *ob, int index)
448 if(ob->pose==NULL) return NULL;
449 index>>=16; // bone selection codes use left 2 bytes
451 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next, a++) {
452 if(a==index) return pchan->bone;
457 /* See if there are any selected bones in this buffer */
458 static void *get_bone_from_selectbuffer(Base *base, unsigned int *buffer, short hits, short findunsel)
460 Object *ob= base->object;
463 void *firstunSel=NULL, *firstSel=NULL, *data;
464 unsigned int hitresult;
465 short i, takeNext=0, sel;
467 for (i=0; i< hits; i++){
468 hitresult = buffer[3+(i*4)];
470 if (!(hitresult & BONESEL_NOSEL)) { // -1
471 if(hitresult & BONESEL_ANY) { // to avoid including objects in selection
473 hitresult &= ~(BONESEL_ANY);
474 /* Determine what the current bone is */
475 if (G.obedit==NULL) {
476 /* no singular posemode, so check for correct object */
477 if(base->selcol == (hitresult & 0xFFFF)) {
478 bone = get_indexed_bone(ob, hitresult);
481 sel = (bone->flag & BONE_SELECTED);
483 sel = !(bone->flag & BONE_SELECTED);
493 ebone = BLI_findlink(&G.edbo, hitresult);
495 sel = (ebone->flag & BONE_SELECTED);
497 sel = !(ebone->flag & BONE_SELECTED);
504 if(!firstSel) firstSel= data;
524 /* used by posemode as well editmode */
525 static void *get_nearest_bone (short findunsel)
527 unsigned int buffer[MAXPICKBUF];
533 hits= view3d_opengl_select(buffer, MAXPICKBUF, 0, 0, 0, 0);
536 return get_bone_from_selectbuffer(BASACT, buffer, hits, findunsel);
542 /* **************** END PoseMode & EditMode *************************** */
543 /* **************** Posemode stuff ********************** */
545 static int select_bonechildren_by_name (Bone *bone, char *name, int select)
549 if (!strcmp (bone->name, name)){
551 bone->flag |= BONE_SELECTED;
553 bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
557 for (curBone=bone->childbase.first; curBone; curBone=curBone->next){
558 if (select_bonechildren_by_name (curBone, name, select))
565 /* called in editaction.c */
566 void select_bone_by_name (bArmature *arm, char *name, int select)
573 for (bone=arm->bonebase.first; bone; bone=bone->next)
574 if (select_bonechildren_by_name (bone, name, select))
578 static void selectconnected_posebonechildren (Object *ob, Bone *bone)
582 if (!(bone->flag & BONE_IK_TOPARENT))
585 select_actionchannel_by_name (ob->action, bone->name, !(G.qual & LR_SHIFTKEY));
587 if (G.qual & LR_SHIFTKEY)
588 bone->flag &= ~BONE_SELECTED;
590 bone->flag |= BONE_SELECTED;
592 for (curBone=bone->childbase.first; curBone; curBone=curBone->next){
593 selectconnected_posebonechildren (ob, curBone);
597 /* within active object context */
598 void selectconnected_posearmature(void)
600 Bone *bone, *curBone, *next;
603 if(!ob || !ob->pose) return;
605 if (G.qual & LR_SHIFTKEY)
606 bone= get_nearest_bone(0);
608 bone = get_nearest_bone(1);
614 for (curBone=bone; curBone; curBone=next){
615 select_actionchannel_by_name (ob->action, curBone->name, !(G.qual & LR_SHIFTKEY));
616 if (G.qual & LR_SHIFTKEY)
617 curBone->flag &= ~BONE_SELECTED;
619 curBone->flag |= BONE_SELECTED;
621 if (curBone->flag & BONE_IK_TOPARENT)
622 next=curBone->parent;
627 /* Select children */
628 for (curBone=bone->childbase.first; curBone; curBone=next){
629 selectconnected_posebonechildren (ob, curBone);
632 countall(); // flushes selection!
634 allqueue (REDRAWVIEW3D, 0);
635 allqueue(REDRAWBUTSEDIT, 0);
636 allqueue(REDRAWBUTSOBJECT, 0);
637 allqueue (REDRAWACTION, 0);
638 allqueue(REDRAWOOPS, 0);
639 BIF_undo_push("Select connected");
643 static int count_bonechildren (Bone *bone, int incount, int flagmask, int allbones){
650 if (bone->flag & flagmask || flagmask == 0xFFFFFFFF){
656 for (curBone=bone->childbase.first; curBone; curBone=curBone->next){
657 incount=count_bonechildren (curBone, incount, flagmask, allbones);
663 static int count_bones (bArmature *arm, int flagmask, int allbones)
671 for (curBone=arm->bonebase.first; curBone; curBone=curBone->next){
672 count = count_bonechildren (curBone, count, flagmask, allbones);
679 /* **************** END Posemode stuff ********************** */
680 /* **************** EditMode stuff ********************** */
682 /* called in space.c */
683 void selectconnected_armature(void)
685 EditBone *bone, *curBone, *next;
687 if (G.qual & LR_SHIFTKEY)
688 bone= get_nearest_bone(0);
690 bone= get_nearest_bone(1);
696 for (curBone=bone; curBone; curBone=next){
697 if (G.qual & LR_SHIFTKEY){
698 curBone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
701 curBone->flag |= (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
704 if (curBone->flag & BONE_IK_TOPARENT)
705 next=curBone->parent;
710 /* Select children */
712 for (curBone=G.edbo.first; curBone; curBone=next){
713 next = curBone->next;
714 if (curBone->parent == bone){
715 if (curBone->flag & BONE_IK_TOPARENT){
716 if (G.qual & LR_SHIFTKEY)
717 curBone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
719 curBone->flag |= (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
734 countall(); // flushes selection!
736 allqueue (REDRAWVIEW3D, 0);
737 allqueue (REDRAWBUTSEDIT, 0);
738 allqueue(REDRAWBUTSOBJECT, 0);
739 allqueue(REDRAWOOPS, 0);
741 BIF_undo_push("Select connected");
745 /* does bones and points */
746 /* note that BONE ROOT only gets drawn for root bones (or without IK) */
747 static EditBone * get_nearest_editbonepoint (int findunsel, int *selmask)
750 unsigned int buffer[MAXPICKBUF];
751 unsigned int hitresult, besthitresult=BONESEL_NOSEL;
759 getmouseco_areawin(mval);
760 hits= view3d_opengl_select(buffer, MAXPICKBUF, mval[0]-5, mval[1]-5, mval[0]+5, mval[1]+5);
762 hits= view3d_opengl_select(buffer, MAXPICKBUF, mval[0]-12, mval[1]-12, mval[0]+12, mval[1]+12);
764 /* See if there are any selected bones in this group */
768 if (!(buffer[3] & BONESEL_NOSEL))
769 besthitresult= buffer[3];
772 for (i=0; i< hits; i++) {
773 hitresult= buffer[3+(i*4)];
774 if (!(hitresult & BONESEL_NOSEL)) {
777 ebone = BLI_findlink(&G.edbo, hitresult & ~BONESEL_ANY);
779 /* clicks on bone points get advantage */
780 if( hitresult & (BONESEL_ROOT|BONESEL_TIP)) {
781 /* but also the unselected one */
783 if( (hitresult & BONESEL_ROOT) && (ebone->flag & BONE_ROOTSEL)==0)
785 else if( (hitresult & BONESEL_TIP) && (ebone->flag & BONE_TIPSEL)==0)
795 if((ebone->flag & BONE_SELECTED)==0)
804 besthitresult= hitresult;
810 if (!(besthitresult & BONESEL_NOSEL)) {
812 ebone= BLI_findlink(&G.edbo, besthitresult & ~BONESEL_ANY);
815 if (besthitresult & BONESEL_ROOT)
816 *selmask |= BONE_ROOTSEL;
817 if (besthitresult & BONESEL_TIP)
818 *selmask |= BONE_TIPSEL;
819 if (besthitresult & BONESEL_BONE)
820 *selmask |= BONE_SELECTED;
828 static void delete_bone(EditBone* exBone)
832 /* Find any bones that refer to this bone */
833 for (curBone=G.edbo.first;curBone;curBone=curBone->next){
834 if (curBone->parent==exBone){
835 curBone->parent=exBone->parent;
836 curBone->flag &= ~BONE_IK_TOPARENT;
840 BLI_freelinkN (&G.edbo,exBone);
843 static EditBone *editbone_name_exists (char *name); // proto for below
846 void delete_armature(void)
848 EditBone *curBone, *next;
852 if(okee("Erase selected bone(s)")==0) return;
854 /* First rase any associated pose channel */
856 bPoseChannel *chan, *next;
857 for (chan=G.obedit->pose->chanbase.first; chan; chan=next) {
859 curBone = editbone_name_exists (chan->name);
861 if (curBone && (curBone->flag&BONE_SELECTED)) {
862 free_constraints(&chan->constraints);
863 BLI_freelinkN (&G.obedit->pose->chanbase, chan);
866 for(con= chan->constraints.first; con; con= con->next) {
867 char *subtarget = get_con_subtarget_name(con, G.obedit);
869 curBone = editbone_name_exists (subtarget);
870 if (curBone && (curBone->flag&BONE_SELECTED)) {
871 con->flag |= CONSTRAINT_DISABLE;
881 for (curBone=G.edbo.first;curBone;curBone=next){
883 if (curBone->flag&BONE_SELECTED)
884 delete_bone(curBone);
888 allqueue(REDRAWVIEW3D, 0);
889 allqueue(REDRAWBUTSEDIT, 0);
890 allqueue(REDRAWBUTSOBJECT, 0);
891 allqueue(REDRAWOOPS, 0);
892 countall(); // flushes selection!
894 BIF_undo_push("Delete bone(s)");
897 /* context: editmode armature */
898 void mouse_armature(void)
900 EditBone *nearBone = NULL, *ebone;
903 nearBone= get_nearest_editbonepoint(1, &selmask);
906 if (!(G.qual & LR_SHIFTKEY)) {
907 deselectall_armature(0);
910 /* by definition the non-root non-IK bones have no root point drawn,
911 so a root selection needs to be delivered to the parent tip,
912 countall() (bad location) flushes these flags */
914 if(selmask & BONE_SELECTED) {
915 if(nearBone->parent && (nearBone->flag & BONE_IK_TOPARENT)) {
916 /* click in a chain */
917 if(G.qual & LR_SHIFTKEY) {
918 /* hold shift inverts this bone's selection */
919 if(nearBone->flag & BONE_SELECTED) {
920 /* deselect this bone */
921 nearBone->flag &= ~(BONE_TIPSEL|BONE_SELECTED);
922 /* only deselect parent tip if it is not selected */
923 if(!(nearBone->parent->flag & BONE_SELECTED))
924 nearBone->parent->flag &= ~BONE_TIPSEL;
927 /* select this bone */
928 nearBone->flag |= BONE_TIPSEL;
929 nearBone->parent->flag |= BONE_TIPSEL;
933 /* select this bone */
934 nearBone->flag |= BONE_TIPSEL;
935 nearBone->parent->flag |= BONE_TIPSEL;
939 if(G.qual & LR_SHIFTKEY) {
940 /* hold shift inverts this bone's selection */
941 if(nearBone->flag & BONE_SELECTED)
942 nearBone->flag &= ~(BONE_TIPSEL|BONE_ROOTSEL);
944 nearBone->flag |= (BONE_TIPSEL|BONE_ROOTSEL);
946 else nearBone->flag |= (BONE_TIPSEL|BONE_ROOTSEL);
950 if ((G.qual & LR_SHIFTKEY) && (nearBone->flag & selmask))
951 nearBone->flag &= ~selmask;
953 nearBone->flag |= selmask;
956 countall(); // flushes selection!
959 /* then now check for active status */
960 for (ebone=G.edbo.first;ebone;ebone=ebone->next) ebone->flag &= ~BONE_ACTIVE;
961 if(nearBone->flag & BONE_SELECTED) nearBone->flag |= BONE_ACTIVE;
964 allqueue(REDRAWVIEW3D, 0);
965 allqueue(REDRAWBUTSEDIT, 0);
966 allqueue(REDRAWBUTSOBJECT, 0);
967 allqueue(REDRAWOOPS, 0);
970 rightmouse_transform();
973 void free_editArmature(void)
976 /* Clear the editbones list */
978 BLI_freelistN (&G.edbo);
982 void remake_editArmature(void)
984 if(okee("Reload original data")==0) return;
987 allqueue(REDRAWVIEW3D, 0);
988 allqueue(REDRAWOOPS, 0);
989 allqueue(REDRAWBUTSHEAD, 0);
990 allqueue(REDRAWBUTSEDIT, 0);
991 allqueue(REDRAWBUTSOBJECT, 0);
993 // BIF_undo_push("Delete bone");
997 /* Put object in EditMode */
998 void make_editArmature(void)
1002 if (G.obedit==0) return;
1004 free_editArmature();
1006 arm= get_armature(G.obedit);
1010 make_boneList (&G.edbo, &arm->bonebase,NULL);
1013 /* put EditMode back in Object */
1014 void load_editArmature(void)
1018 arm= get_armature(G.obedit);
1021 editbones_to_armature(&G.edbo, G.obedit);
1024 /* toggle==0: deselect
1026 toggle==2: only active tag
1028 void deselectall_armature(int toggle)
1034 /* Determine if there are any selected bones
1035 And therefore whether we are selecting or deselecting */
1036 for (eBone=G.edbo.first;eBone;eBone=eBone->next){
1037 if (eBone->flag & (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL)){
1046 for (eBone=G.edbo.first;eBone;eBone=eBone->next){
1048 eBone->flag |= (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1050 eBone->flag &= ~(BONE_ACTIVE);
1052 eBone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL | BONE_ACTIVE);
1055 allqueue(REDRAWVIEW3D, 0);
1056 allqueue(REDRAWBUTSEDIT, 0);
1057 allqueue(REDRAWBUTSOBJECT, 0);
1058 allqueue(REDRAWOOPS, 0);
1060 countall(); // flushes selection!
1063 void auto_align_armature(void)
1064 /* Sets the roll value of selected bones so that their zaxes point upwards */
1067 float xaxis[3]={1.0, 0.0, 0.0}, yaxis[3], zaxis[3]={0.0, 0.0, 1.0};
1068 float targetmat[3][3], imat[3][3];
1069 float curmat[3][3], diffmat[3][3];
1072 for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1073 if (ebone->flag & BONE_SELECTED){
1074 /* Find the current bone matrix */
1075 VecSubf(delta, ebone->tail, ebone->head);
1076 vec_roll_to_mat3(delta, 0.0, curmat);
1078 /* Make new matrix based on y axis & z-up */
1079 VECCOPY (yaxis, curmat[1]);
1082 VECCOPY (targetmat[0], xaxis);
1083 VECCOPY (targetmat[1], yaxis);
1084 VECCOPY (targetmat[2], zaxis);
1085 Mat3Ortho(targetmat);
1087 /* Find the difference between the two matrices */
1088 Mat3Inv(imat, targetmat);
1089 Mat3MulMat3(diffmat, imat, curmat);
1091 ebone->roll = atan(diffmat[2][0]/diffmat[2][2]);
1097 /* **************** undo for armatures ************** */
1099 static void undoBones_to_editBones(void *lbv)
1102 EditBone *ebo, *newebo;
1104 BLI_freelistN(&G.edbo);
1107 for(ebo= lb->first; ebo; ebo= ebo->next) {
1108 newebo= MEM_dupallocN(ebo);
1110 BLI_addtail(&G.edbo, newebo);
1114 for(newebo= G.edbo.first; newebo; newebo= newebo->next) {
1115 if(newebo->parent) newebo->parent= newebo->parent->temp;
1117 /* be sure they dont hang ever */
1118 for(newebo= G.edbo.first; newebo; newebo= newebo->next) {
1123 static void *editBones_to_undoBones(void)
1126 EditBone *ebo, *newebo;
1128 lb= MEM_callocN(sizeof(ListBase), "listbase undo");
1131 for(ebo= G.edbo.first; ebo; ebo= ebo->next) {
1132 newebo= MEM_dupallocN(ebo);
1134 BLI_addtail(lb, newebo);
1138 for(newebo= lb->first; newebo; newebo= newebo->next) {
1139 if(newebo->parent) newebo->parent= newebo->parent->temp;
1145 static void free_undoBones(void *lbv)
1153 /* and this is all the undo system needs to know */
1154 void undo_push_armature(char *name)
1156 undo_editmode_push(name, free_undoBones, undoBones_to_editBones, editBones_to_undoBones);
1161 /* **************** END EditMode stuff ********************** */
1162 /* *************** Adding stuff in editmode *************** */
1164 /* default bone add, returns it selected, but without tail set */
1165 static EditBone *add_editbone(void)
1167 EditBone *bone= MEM_callocN(sizeof(EditBone), "eBone");
1169 strcpy (bone->name,"Bone");
1170 unique_editbone_name (bone->name);
1172 BLI_addtail(&G.edbo, bone);
1174 bone->flag |= BONE_TIPSEL;
1181 bone->rad_head= 0.25;
1182 bone->rad_tail= 0.1;
1184 bone->boneclass = BONE_SKINNABLE;
1189 static void add_primitive_bone(Object *ob)
1191 float obmat[3][3], curs[3], viewmat[3][3], totmat[3][3], imat[3][3];
1194 VECCOPY (curs, give_cursor());
1196 /* Get inverse point for head and orientation for tail */
1197 Mat4Invert(G.obedit->imat, G.obedit->obmat);
1198 Mat4MulVecfl(G.obedit->imat, curs);
1200 Mat3CpyMat4(obmat, G.vd->viewmat);
1201 Mat3CpyMat4(viewmat, G.obedit->obmat);
1202 Mat3MulMat3(totmat, obmat, viewmat);
1203 Mat3Inv(imat, totmat);
1205 deselectall_armature(0);
1208 bone= add_editbone();
1210 VECCOPY(bone->head, curs);
1211 VecAddf(bone->tail, bone->head, imat[1]); // bone with unit length 1
1215 void add_primitiveArmature(int type)
1217 if(G.scene->id.lib) return;
1219 /* this function also comes from an info window */
1220 if ELEM(curarea->spacetype, SPACE_VIEW3D, SPACE_INFO); else return;
1221 if(G.vd==NULL) return;
1223 G.f &= ~(G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT+G_WEIGHTPAINT);
1224 setcursor_space(SPACE_VIEW3D, CURSOR_STD);
1226 check_editmode(OB_ARMATURE);
1228 /* If we're not the "obedit", make a new object and enter editmode */
1229 if(G.obedit==NULL) {
1230 add_object(OB_ARMATURE);
1231 base_init_from_view3d(BASACT, G.vd);
1232 G.obedit= BASACT->object;
1234 where_is_object(G.obedit);
1236 make_editArmature();
1237 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
1240 /* no primitive support yet */
1241 add_primitive_bone(G.obedit);
1243 countall(); // flushes selection!
1245 allqueue(REDRAWALL, 0);
1246 BIF_undo_push("Add primitive");
1249 /* the ctrl-click method */
1250 void addvert_armature(void)
1252 EditBone *ebone, *newbone, *partest;
1253 float *curs, mat[3][3],imat[3][3];
1258 /* find the active or selected bone */
1259 for (ebone = G.edbo.first; ebone; ebone=ebone->next)
1260 if(ebone->flag & (BONE_ACTIVE|BONE_TIPSEL)) break;
1263 for (ebone = G.edbo.first; ebone; ebone=ebone->next)
1264 if(ebone->flag & (BONE_ACTIVE|BONE_ROOTSEL)) break;
1271 deselectall_armature(0);
1272 newbone= add_editbone();
1273 newbone->flag |= BONE_ACTIVE;
1276 VECCOPY(newbone->head, ebone->head);
1278 newbone->parent= ebone->parent;
1281 VECCOPY(newbone->head, ebone->tail);
1284 /* See if there are any ik children of the parent */
1285 for (partest = G.edbo.first; partest; partest= partest->next){
1286 if ((partest->parent == ebone) && (partest->flag & BONE_IK_TOPARENT))
1290 newbone->flag |= BONE_IK_TOPARENT;
1292 newbone->parent= ebone;
1295 curs= give_cursor();
1296 VECCOPY(newbone->tail, curs);
1297 VecSubf(newbone->tail, newbone->tail, G.obedit->obmat[3]);
1299 Mat3CpyMat4(mat, G.obedit->obmat);
1301 Mat3MulVecfl(imat, newbone->tail);
1303 newbone->length= VecLenf(newbone->head, newbone->tail);
1304 newbone->rad_tail= newbone->length*0.1f;
1305 newbone->dist= newbone->length*0.25f;
1309 BIF_undo_push("Add Bone");
1310 allqueue(REDRAWVIEW3D, 0);
1312 while(get_mbut()&R_MOUSE);
1316 void adduplicate_armature(void)
1318 EditBone *eBone = NULL;
1320 EditBone *firstDup=NULL; /* The beginning of the duplicated bones in the edbo list */
1322 countall(); // flushes selection!
1324 /* Find the selected bones and duplicate them as needed */
1325 for (curBone=G.edbo.first; curBone && curBone!=firstDup; curBone=curBone->next){
1326 if (curBone->flag & BONE_SELECTED){
1328 eBone=MEM_callocN(sizeof(EditBone), "addup_editbone");
1329 eBone->flag |= BONE_SELECTED;
1331 /* Copy data from old bone to new bone */
1332 memcpy (eBone, curBone, sizeof(EditBone));
1334 curBone->temp = eBone;
1335 eBone->temp = curBone;
1337 unique_editbone_name (eBone->name);
1338 BLI_addtail (&G.edbo, eBone);
1342 /* Lets duplicate the list of constraits that the
1345 /* temporal removed (ton) */
1350 /* Fix the head and tail */
1351 if (eBone->parent && !eBone->parent->flag & BONE_SELECTED){
1352 VecSubf (eBone->tail, eBone->tail, eBone->head);
1353 VecSubf (eBone->head, eBone->head, eBone->head);
1357 /* Run though the list and fix the pointers */
1358 for (curBone=G.edbo.first; curBone && curBone!=firstDup; curBone=curBone->next){
1360 if (curBone->flag & BONE_SELECTED){
1361 eBone=(EditBone*) curBone->temp;
1363 /* If this bone has no parent,
1364 Set the duplicate->parent to NULL
1366 if (!curBone->parent){
1367 eBone->parent = NULL;
1369 /* If this bone has a parent that IS selected,
1370 Set the duplicate->parent to the curBone->parent->duplicate
1372 else if (curBone->parent->flag & BONE_SELECTED){
1373 eBone->parent=(EditBone*) curBone->parent->temp;
1375 /* If this bone has a parent that IS not selected,
1376 Set the duplicate->parent to the curBone->parent
1379 eBone->parent=(EditBone*) curBone->parent;
1380 eBone->flag &= ~BONE_IK_TOPARENT;
1383 /* Lets try to fix any constraint subtargets that might
1384 have been duplicated */
1385 /* temporal removed (ton) */
1390 /* Deselect the old bones and select the new ones */
1392 for (curBone=G.edbo.first; curBone && curBone!=firstDup; curBone=curBone->next){
1393 curBone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL | BONE_ACTIVE);
1396 BIF_TransformSetUndo("Add Duplicate");
1397 initTransform(TFM_TRANSLATION, CTX_NO_PET);
1400 allqueue(REDRAWBUTSEDIT, 0);
1401 allqueue(REDRAWBUTSOBJECT, 0);
1402 allqueue(REDRAWOOPS, 0);
1407 /* *************** END Adding stuff in editmode *************** */
1408 /* *************** Tools in editmode *********** */
1411 void hide_selected_armature_bones(void)
1415 for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1416 if(ebone->flag & (BONE_SELECTED)) {
1417 ebone->flag &= ~(BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL|BONE_ACTIVE);
1418 ebone->flag |= BONE_HIDDEN_A;
1422 allqueue(REDRAWVIEW3D, 0);
1423 allqueue(REDRAWBUTSEDIT, 0);
1426 void hide_unselected_armature_bones(void)
1430 for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1431 if(ebone->flag & (BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL));
1433 ebone->flag &= ~BONE_ACTIVE;
1434 ebone->flag |= BONE_HIDDEN_A;
1438 allqueue(REDRAWVIEW3D, 0);
1439 allqueue(REDRAWBUTSEDIT, 0);
1442 void show_all_armature_bones(void)
1446 for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1447 if(ebone->flag & BONE_HIDDEN_A) {
1448 ebone->flag |= (BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL);
1449 ebone->flag &= ~BONE_HIDDEN_A;
1453 allqueue(REDRAWVIEW3D, 0);
1454 allqueue(REDRAWBUTSEDIT, 0);
1459 /* the "IK" button in editbuttons */
1460 void attach_bone_to_parent_cb(void *bonev, void *arg2_unused)
1462 EditBone *ebone= bonev;
1463 attach_bone_to_parent(ebone);
1466 void attach_bone_to_parent(EditBone *bone)
1470 if (bone->flag & BONE_IK_TOPARENT) {
1472 /* See if there are any other bones that refer to the same
1473 * parent and disconnect them
1475 for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1477 if (ebone->parent && (ebone->parent == bone->parent) &&
1478 (ebone->flag & BONE_IK_TOPARENT))
1479 ebone->flag &= ~BONE_IK_TOPARENT;
1483 /* Attach this bone to its parent */
1484 VECCOPY(bone->head, bone->parent->tail);
1488 void make_bone_parent(void)
1494 val= pupmenu("Make Parent%t|Connected%x1|Keep Offset%x2");
1499 for (ebone = G.edbo.first; ebone; ebone=ebone->next)
1500 if(ebone->flag & BONE_ACTIVE) break;
1502 EditBone *actbone= ebone, *selbone= NULL;
1505 for (ebone = G.edbo.first; ebone; ebone=ebone->next) {
1506 if(ebone->flag & BONE_SELECTED) {
1507 if(ebone!=actbone) {
1508 if(selbone==NULL) selbone= ebone;
1510 error("Need one active and one selected bone");
1516 if(selbone==NULL) error("Need one active and one selected bone");
1518 /* if selbone had a parent we clear parent tip */
1519 if(selbone->parent && (selbone->flag & BONE_IK_TOPARENT))
1520 selbone->parent->flag &= ~(BONE_TIPSEL);
1522 selbone->parent= actbone;
1524 /* in actbone tree we cannot have a loop */
1525 for(ebone= actbone->parent; ebone; ebone= ebone->parent) {
1526 if(ebone->parent==selbone) {
1527 ebone->parent= NULL;
1528 ebone->flag &= ~BONE_IK_TOPARENT;
1532 if(val==1) { // connected
1533 selbone->flag |= BONE_IK_TOPARENT;
1534 VecSubf(offset, actbone->tail, selbone->head);
1536 VECCOPY(selbone->head, actbone->tail);
1537 VecAddf(selbone->tail, selbone->tail, offset);
1539 // offset for all its children
1540 for (ebone = G.edbo.first; ebone; ebone=ebone->next) {
1542 for(par= ebone->parent; par; par= par->parent) {
1544 VecAddf(ebone->head, ebone->head, offset);
1545 VecAddf(ebone->tail, ebone->tail, offset);
1552 selbone->flag &= ~BONE_IK_TOPARENT;
1555 countall(); // checks selection
1556 allqueue(REDRAWVIEW3D, 0);
1557 allqueue(REDRAWBUTSEDIT, 0);
1558 allqueue(REDRAWOOPS, 0);
1564 void clear_bone_parent(void)
1569 val= pupmenu("Clear Parent%t|Clear Parent%x1|Disconnect IK%x2");
1572 for (ebone = G.edbo.first; ebone; ebone=ebone->next) {
1573 if(ebone->flag & BONE_SELECTED) {
1575 /* for nice selection */
1576 ebone->parent->flag &= ~(BONE_TIPSEL);
1578 if(val==1) ebone->parent= NULL;
1579 ebone->flag &= ~BONE_IK_TOPARENT;
1584 countall(); // checks selection
1585 allqueue(REDRAWVIEW3D, 0);
1586 allqueue(REDRAWBUTSEDIT, 0);
1587 allqueue(REDRAWOOPS, 0);
1591 static EditBone *editbone_name_exists (char *name)
1595 for (eBone=G.edbo.first; eBone; eBone=eBone->next){
1596 if (!strcmp (name, eBone->name))
1602 /* note: there's a unique_bone_name() too! */
1603 void unique_editbone_name (char *name)
1610 if (editbone_name_exists(name)) {
1612 /* Strip off the suffix, if it's a number */
1613 number= strlen(name);
1614 if(number && isdigit(name[number-1])) {
1615 dot= strrchr(name, '.'); // last occurrance
1620 for (number = 1; number <=999; number++){
1621 sprintf (tempname, "%s.%03d", name, number);
1622 if (!editbone_name_exists(tempname)){
1623 strcpy (name, tempname);
1630 /* context; editmode armature */
1631 /* if forked && mirror-edit: makes two bones with flipped names */
1632 void extrude_armature(int forked)
1634 bArmature *arm= G.obedit->data;
1635 EditBone *newbone, *ebone, *flipbone, *first=NULL, *partest;
1636 int a, totbone= 0, do_extrude;
1640 /* since we allow root extrude too, we have to make sure selection is OK */
1641 for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1642 if(ebone->flag & BONE_ROOTSEL) {
1643 if(ebone->parent && (ebone->flag & BONE_IK_TOPARENT)) {
1644 if(ebone->parent->flag & BONE_TIPSEL)
1645 ebone->flag &= ~BONE_ROOTSEL;
1650 /* Duplicate the necessary bones */
1651 for (ebone = G.edbo.first; ((ebone) && (ebone!=first)); ebone=ebone->next){
1653 /* we extrude per definition the tip */
1655 if (ebone->flag & (BONE_TIPSEL|BONE_SELECTED))
1657 else if(ebone->flag & BONE_ROOTSEL) {
1658 /* but, a bone with parent deselected we do the root... */
1659 if(ebone->parent && (ebone->parent->flag & BONE_TIPSEL));
1665 /* we re-use code for mirror editing... */
1667 if(arm->flag & ARM_MIRROR_EDIT) {
1668 flipbone= armature_bone_get_mirrored(ebone);
1670 forked= 0; // we extrude 2 different bones
1671 if(flipbone->flag & (BONE_TIPSEL|BONE_ROOTSEL|BONE_SELECTED))
1672 /* don't want this bone to be selected... */
1673 flipbone->flag &= ~(BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL|BONE_ACTIVE);
1675 if(flipbone==NULL && forked)
1679 for(a=0; a<2; a++) {
1684 SWAP(EditBone *, flipbone, ebone);
1689 newbone = MEM_callocN(sizeof(EditBone), "extrudebone");
1692 VECCOPY (newbone->head, ebone->tail);
1693 VECCOPY (newbone->tail, newbone->head);
1694 newbone->parent = ebone;
1696 newbone->flag = ebone->flag & BONE_TIPSEL; // copies it, in case mirrored bone
1699 VECCOPY(newbone->head, ebone->head);
1700 VECCOPY(newbone->tail, ebone->head);
1701 newbone->parent= ebone->parent;
1703 newbone->flag= BONE_TIPSEL;
1706 newbone->weight= ebone->weight;
1707 newbone->dist= ebone->dist;
1708 newbone->xwidth= ebone->xwidth;
1709 newbone->zwidth= ebone->zwidth;
1710 newbone->ease1= ebone->ease1;
1711 newbone->ease2= ebone->ease2;
1712 newbone->rad_head= ebone->rad_tail; // dont copy entire bone...
1713 newbone->rad_tail= ebone->rad_tail;
1714 newbone->segments= 1;
1715 newbone->boneclass= ebone->boneclass;
1717 /* See if there are any ik children of the parent */
1719 for (partest = G.edbo.first; partest; partest=partest->next){
1720 if ((partest->parent == ebone) && (partest->flag & BONE_IK_TOPARENT))
1725 newbone->flag |= BONE_IK_TOPARENT;
1728 strcpy (newbone->name, ebone->name);
1730 if(flipbone && forked) { // only set if mirror edit
1731 if(strlen(newbone->name)<30) {
1732 if(a==0) strcat(newbone->name, "_L");
1733 else strcat(newbone->name, "_R");
1736 unique_editbone_name(newbone->name);
1738 /* Add the new bone to the list */
1739 BLI_addtail(&G.edbo, newbone);
1743 /* restore ebone if we were flipping */
1744 if(a==1 && flipbone)
1745 SWAP(EditBone *, flipbone, ebone);
1750 /* Deselect the old bone */
1751 ebone->flag &= ~(BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL|BONE_ACTIVE);
1754 /* if only one bone, make this one active */
1755 if(totbone==1 && first) first->flag |= BONE_ACTIVE;
1757 /* Transform the endpoints */
1758 countall(); // flushes selection!
1759 BIF_TransformSetUndo("Extrude");
1760 initTransform(TFM_TRANSLATION, CTX_NO_PET);
1763 allqueue(REDRAWBUTSEDIT, 0);
1764 allqueue(REDRAWBUTSOBJECT, 0);
1765 allqueue(REDRAWOOPS, 0);
1768 /* ***************** Pose tools ********************* */
1770 void clear_armature(Object *ob, char mode)
1772 bPoseChannel *pchan;
1775 arm=get_armature(ob);
1780 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1781 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED)) {
1784 pchan->quat[1]=pchan->quat[2]=pchan->quat[3]=0.0F; pchan->quat[0]=1.0F;
1787 pchan->loc[0]=pchan->loc[1]=pchan->loc[2]=0.0F;
1790 pchan->size[0]=pchan->size[1]=pchan->size[2]=1.0F;
1797 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1798 /* no update for this object, this will execute the action again */
1799 /* is weak... like for ipo editing which uses ctime now... */
1804 /* helper for function below */
1805 static int clear_active_flag(Object *ob, Bone *bone, void *data)
1807 bone->flag &= ~BONE_ACTIVE;
1812 /* called from editview.c, for mode-less pose selection */
1813 int do_pose_selectbuffer(Base *base, unsigned int *buffer, short hits)
1815 Object *ob= base->object;
1818 if (!ob || !ob->pose) return 0;
1820 nearBone= get_bone_from_selectbuffer(base, buffer, hits, 1);
1823 if (!(G.qual & LR_SHIFTKEY)){
1824 deselectall_posearmature(ob, 0);
1825 nearBone->flag |= (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL|BONE_ACTIVE);
1826 select_actionchannel_by_name(ob->action, nearBone->name, 1);
1829 if (nearBone->flag & BONE_SELECTED) {
1830 /* if not active, we make it active */
1831 if((nearBone->flag & BONE_ACTIVE)==0) {
1832 bArmature *arm= ob->data;
1833 bone_looper(ob, arm->bonebase.first, NULL, clear_active_flag);
1835 nearBone->flag |= BONE_ACTIVE;
1838 nearBone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL|BONE_ACTIVE);
1839 select_actionchannel_by_name(ob->action, nearBone->name, 0);
1843 bArmature *arm= ob->data;
1844 bone_looper(ob, arm->bonebase.first, NULL, clear_active_flag);
1846 nearBone->flag |= (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL|BONE_ACTIVE);
1847 select_actionchannel_by_name(ob->action, nearBone->name, 1);
1851 /* in weightpaint we select the associated vertex group too */
1852 if(G.f & G_WEIGHTPAINT) {
1853 if(nearBone->flag & BONE_ACTIVE) {
1854 vertexgroup_select_by_name(OBACT, nearBone->name);
1858 allqueue(REDRAWVIEW3D, 0);
1859 allqueue(REDRAWACTION, 0);
1860 allqueue(REDRAWIPO, 0); /* To force action ipo update */
1861 allqueue(REDRAWBUTSEDIT, 0);
1862 allqueue(REDRAWBUTSOBJECT, 0);
1863 allqueue(REDRAWOOPS, 0);
1866 return nearBone!=NULL;
1870 /* mode==0: deselect
1872 mode==2: clear active tag
1874 static void deselect_bonechildren (Object *ob, Bone *bone, int mode)
1882 bone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL|BONE_ACTIVE);
1884 if(!(bone->flag & BONE_HIDDEN_P))
1885 bone->flag |= (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1887 else bone->flag &= ~BONE_ACTIVE;
1889 if(mode!=2) select_actionchannel_by_name(ob->action, bone->name, mode);
1891 for (curBone=bone->childbase.first; curBone; curBone=curBone->next){
1892 deselect_bonechildren(ob, curBone, mode);
1896 /* test==0: deselect all
1897 test==1: swap select
1898 test==2: only clear active tag
1900 void deselectall_posearmature (Object *ob, int test)
1906 /* we call this from outliner too, but with OBACT set OK */
1907 if(!ob || !ob->pose) return;
1908 arm= get_armature(ob);
1910 /* Determine if we're selecting or deselecting */
1912 if (!count_bones (arm, BONE_SELECTED, 0))
1918 /* Set the flags accordingly */
1919 for (curBone=arm->bonebase.first; curBone; curBone=curBone->next)
1920 deselect_bonechildren (ob, curBone, selectmode);
1922 allqueue(REDRAWBUTSEDIT, 0);
1923 allqueue(REDRAWBUTSOBJECT, 0);
1924 allqueue(REDRAWVIEW3D, 0);
1925 allqueue(REDRAWOOPS, 0);
1926 allqueue(REDRAWACTION, 0);
1931 int bone_looper(Object *ob, Bone *bone, void *data,
1932 int (*bone_func)(Object *, Bone *, void *))
1935 /* We want to apply the function bone_func to every bone
1936 * in an armature -- feed bone_looper the first bone and
1937 * a pointer to the bone_func and watch it go!. The int count
1938 * can be useful for counting bones with a certain property
1945 /* only do bone_func if the bone is non null
1947 count += bone_func(ob, bone, data);
1949 /* try to execute bone_func for the first child
1951 count += bone_looper(ob, bone->childbase.first, data,
1954 /* try to execute bone_func for the next bone at this
1955 * depth of the recursion.
1957 count += bone_looper(ob, bone->next, data, bone_func);
1963 int ik_chain_looper(Object *ob, Bone *bone, void *data,
1964 int (*bone_func)(Object *, Bone *, void *))
1967 /* We want to apply the function bone_func to every bone
1968 * in an ik chain -- feed ikchain_looper a bone in the chain and
1969 * a pointer to the bone_func and watch it go!. The int count
1970 * can be useful for counting bones with a certain property
1979 count += bone_func(ob, bone, data);
1982 for (curBone = bone; curBone; curBone=curBone->parent) {
1983 if (!curBone->parent)
1985 else if (!(curBone->flag & BONE_IK_TOPARENT))
1987 count += bone_func(ob, curBone->parent, data);
1991 for (curBone = bone->childbase.first; curBone; curBone=curBone->next){
1992 if (curBone->flag & BONE_IK_TOPARENT) {
1993 count += bone_func(ob, curBone, data);
2001 static int bone_skinnable(Object *ob, Bone *bone, void *data)
2003 /* Bones that are not of boneclass BONE_UNSKINNABLE
2004 * are regarded to be "skinnable" and are eligible for
2007 * This function performs 2 functions:
2009 * a) It returns 1 if the bone is skinnable.
2010 * If we loop over all bones with this
2011 * function, we can count the number of
2013 * b) If the pointer data is non null,
2014 * it is treated like a handle to a
2015 * bone pointer -- the bone pointer
2016 * is set to point at this bone, and
2017 * the pointer the handle points to
2018 * is incremented to point to the
2019 * next member of an array of pointers
2020 * to bones. This way we can loop using
2021 * this function to construct an array of
2022 * pointers to bones that point to all
2027 if ( bone->boneclass != BONE_UNSKINNABLE ) {
2029 hbone = (Bone ***) data;
2038 static int add_defgroup_unique_bone(Object *ob, Bone *bone, void *data)
2040 /* This group creates a vertex group to ob that has the
2041 * same name as bone (provided the bone is skinnable).
2042 * If such a vertex group aleady exist the routine exits.
2044 if ( bone_skinnable(ob, bone, NULL) ) {
2045 if (!get_named_vertexgroup(ob,bone->name)) {
2046 add_defgroup_name(ob, bone->name);
2053 static int dgroup_skinnable(Object *ob, Bone *bone, void *data)
2055 /* Bones that are not of boneclass BONE_UNSKINNABLE
2056 * are regarded to be "skinnable" and are eligible for
2059 * This function performs 2 functions:
2061 * a) If the bone is skinnable, it creates
2062 * a vertex group for ob that has
2063 * the name of the skinnable bone
2064 * (if one doesn't exist already).
2065 * b) If the pointer data is non null,
2066 * it is treated like a handle to a
2067 * bDeformGroup pointer -- the
2068 * bDeformGroup pointer is set to point
2069 * to the deform group with the bone's
2070 * name, and the pointer the handle
2071 * points to is incremented to point to the
2072 * next member of an array of pointers
2073 * to bDeformGroups. This way we can loop using
2074 * this function to construct an array of
2075 * pointers to bDeformGroups, all with names
2076 * of skinnable bones.
2078 bDeformGroup ***hgroup, *defgroup;
2080 if ( bone->boneclass != BONE_UNSKINNABLE ) {
2081 if ( !(defgroup = get_named_vertexgroup(ob, bone->name)) ) {
2082 defgroup = add_defgroup_name(ob, bone->name);
2086 hgroup = (bDeformGroup ***) data;
2087 **hgroup = defgroup;
2095 static void add_verts_to_closest_dgroup(Object *ob, Object *par)
2097 /* This function implements a crude form of
2098 * auto-skinning: vertices are assigned to the
2099 * deformation groups associated with bones based
2100 * on thier proximity to a bone. Every vert is
2101 * given a weight of 1.0 to the weight group
2102 * cooresponding to the bone that it is
2103 * closest to. The vertex may also be assigned to
2104 * a deformation group associated to a bone
2105 * that is within 10% of the mninimum distance
2106 * between the bone and the nearest vert -- the
2107 * cooresponding weight will fall-off to zero
2108 * as the distance approaches the 10% tolerance mark.
2109 * If the mesh has subsurf enabled then the verts
2110 * on the subsurf limit surface is used to generate
2111 * the weights rather than the verts on the cage
2114 * ("Limit surface" = same amount of vertices as mesh, but vertices
2115 * moved to the subsurfed position, like for 'optimal').
2119 Bone **bonelist, **bonehandle, *bone;
2120 bDeformGroup **dgrouplist, **dgrouphandle, *defgroup;
2125 float *subverts = NULL;
2132 /* If the parent object is not an armature exit */
2133 arm = get_armature(par);
2137 /* count the number of skinnable bones */
2138 numbones = bone_looper(ob, arm->bonebase.first, NULL,
2141 /* create an array of pointer to bones that are skinnable
2142 * and fill it with all of the skinnable bones
2144 bonelist = MEM_mallocN(numbones*sizeof(Bone *), "bonelist");
2145 bonehandle = bonelist;
2146 bone_looper(ob, arm->bonebase.first, &bonehandle,
2149 /* create an array of pointers to the deform groups that
2150 * coorespond to the skinnable bones (creating them
2153 dgrouplist = MEM_mallocN(numbones*sizeof(bDeformGroup *), "dgrouplist");
2154 dgrouphandle = dgrouplist;
2155 bone_looper(ob, arm->bonebase.first, &dgrouphandle,
2158 /* create an array of floats that will be used for each vert
2159 * to hold the distance-factor to each bone.
2161 distance = MEM_mallocN(numbones*sizeof(float), "distance");
2163 mesh = (Mesh*)ob->data;
2165 /* Is subsurf on? Lets use the verts on the limit surface then */
2166 if (modifiers_findByType(ob, eModifierType_Subsurf)) {
2167 subverts = MEM_mallocN(3*mesh->totvert*sizeof(float), "subverts");
2168 subsurf_calculate_limit_positions(mesh, (void *)subverts); /* (ton) made void*, dunno how to cast */
2171 /* for each vertex in the mesh ...
2173 for ( i=0 ; i < mesh->totvert ; ++i ) {
2174 /* get the vert in global coords
2178 subvert = subverts + i*3;
2179 VECCOPY (real_co, subvert);
2182 vert = mesh->mvert + i;
2183 VECCOPY (real_co, vert->co);
2185 Mat4MulVecfl(ob->obmat, real_co);
2188 /* for each skinnable bone ...
2190 for (j=0; j < numbones; ++j) {
2193 /* get the root of the bone in global coords
2195 VECCOPY(root, bone->arm_head);
2196 Mat4MulVecfl(par->obmat, root);
2198 /* get the tip of the bone in global coords
2200 VECCOPY(tip, bone->arm_tail);
2201 Mat4MulVecfl(par->obmat, tip);
2203 /* store the distance-factor from the vertex to
2206 distance[j]= distfactor_to_bone (real_co, root, tip, bone->rad_head, bone->rad_tail, bone->dist);
2209 /* for each deform group ...
2211 for (j=0; j < numbones; ++j) {
2212 defgroup = dgrouplist[j];
2214 /* add the vert to the deform group if weight!=0.0
2216 if (distance[j]!=0.0)
2217 add_vert_to_defgroup (ob, defgroup, i, distance[j], WEIGHT_REPLACE);
2219 remove_vert_defgroup (ob, defgroup, i);
2223 /* free the memory allocated
2225 MEM_freeN(bonelist);
2226 MEM_freeN(dgrouplist);
2227 MEM_freeN(distance);
2228 if (subverts) MEM_freeN(subverts);
2231 void create_vgroups_from_armature(Object *ob, Object *par)
2233 /* Lets try to create some vertex groups
2234 * based on the bones of the parent armature.
2240 /* If the parent object is not an armature exit */
2241 arm = get_armature(par);
2245 /* Prompt the user on whether/how they want the vertex groups
2246 * added to the child mesh */
2247 mode= pupmenu("Create Vertex Groups? %t|"
2248 "Don't Create Groups %x1|"
2250 "Create From Closest Bones %x3");
2253 /* Traverse the bone list, trying to create empty vertex
2254 * groups cooresponding to the bone.
2256 bone_looper(ob, arm->bonebase.first, NULL,
2257 add_defgroup_unique_bone);
2258 if (ob->type == OB_MESH)
2259 create_dverts((Mesh*)ob->data);
2264 /* Traverse the bone list, trying to create vertex groups
2265 * that are populated with the vertices for which the
2268 add_verts_to_closest_dgroup(ob, par);
2274 static int hide_selected_pose_bone(Object *ob, Bone *bone, void *ptr)
2276 if (bone->flag & BONE_SELECTED) {
2277 bone->flag |= BONE_HIDDEN_P;
2278 bone->flag &= ~BONE_SELECTED;
2283 /* active object is armature */
2284 void hide_selected_pose_bones(void)
2288 arm= get_armature (OBACT);
2293 bone_looper(OBACT, arm->bonebase.first, NULL,
2294 hide_selected_pose_bone);
2296 allqueue(REDRAWVIEW3D, 0);
2297 allqueue(REDRAWBUTSEDIT, 0);
2300 static int hide_unselected_pose_bone(Object *ob, Bone *bone, void *ptr)
2302 if (~bone->flag & BONE_SELECTED) {
2303 bone->flag |= BONE_HIDDEN_P;
2308 /* active object is armature */
2309 void hide_unselected_pose_bones(void)
2313 arm=get_armature (OBACT);
2318 bone_looper(OBACT, arm->bonebase.first, NULL,
2319 hide_unselected_pose_bone);
2321 allqueue(REDRAWVIEW3D, 0);
2322 allqueue(REDRAWBUTSEDIT, 0);
2325 static int show_pose_bone(Object *ob, Bone *bone, void *ptr)
2327 if (bone->flag & BONE_HIDDEN_P) {
2328 bone->flag &= ~BONE_HIDDEN_P;
2329 bone->flag |= BONE_SELECTED;
2335 /* active object is armature in posemode */
2336 void show_all_pose_bones(void)
2340 arm=get_armature (OBACT);
2345 bone_looper(OBACT, arm->bonebase.first, NULL,
2348 allqueue(REDRAWVIEW3D, 0);
2349 allqueue(REDRAWBUTSEDIT, 0);
2353 /* ************* RENAMING DISASTERS ************ */
2355 /* note: there's a unique_editbone_name() too! */
2356 void unique_bone_name (bArmature *arm, char *name)
2362 if (get_named_bone(arm, name)) {
2364 /* Strip off the suffix, if it's a number */
2365 number= strlen(name);
2366 if(number && isdigit(name[number-1])) {
2367 dot= strrchr(name, '.'); // last occurrance
2372 for (number = 1; number <=999; number++){
2373 sprintf (tempname, "%s.%03d", name, number);
2374 if (!get_named_bone(arm, tempname)){
2375 strcpy (name, tempname);
2382 #define MAXBONENAME 32
2383 /* helper call for below */
2384 static void constraint_bone_name_fix(Object *ob, ListBase *conlist, char *oldname, char *newname)
2386 bConstraint *curcon;
2389 for (curcon = conlist->first; curcon; curcon=curcon->next){
2390 subtarget = get_con_subtarget_name(curcon, ob);
2392 if (!strcmp(subtarget, oldname) )
2393 BLI_strncpy(subtarget, newname, MAXBONENAME);
2397 /* called by UI for renaming a bone */
2398 /* warning: make sure the original bone was not renamed yet! */
2399 /* seems messy, but thats what you get with not using pointers but channel names :) */
2400 void armature_bone_rename(bArmature *arm, char *oldnamep, char *newnamep)
2403 char newname[MAXBONENAME];
2404 char oldname[MAXBONENAME];
2406 /* names better differ! */
2407 if(strncmp(oldnamep, newnamep, MAXBONENAME)) {
2409 /* we alter newname string... so make copy */
2410 BLI_strncpy(newname, newnamep, MAXBONENAME);
2411 /* we use oldname for search... so make copy */
2412 BLI_strncpy(oldname, oldnamep, MAXBONENAME);
2414 /* now check if we're in editmode, we need to find the unique name */
2415 if(G.obedit && G.obedit->data==arm) {
2418 eBone= editbone_name_exists(oldname);
2420 unique_editbone_name (newname);
2421 BLI_strncpy(eBone->name, newname, MAXBONENAME);
2426 Bone *bone= get_named_bone (arm, oldname);
2429 unique_bone_name (arm, newname);
2430 BLI_strncpy(bone->name, newname, MAXBONENAME);
2435 /* do entire dbase */
2436 for(ob= G.main->object.first; ob; ob= ob->id.next) {
2437 /* we have the object using the armature */
2441 bActionChannel *chan;
2443 /* Rename action channel if necessary */
2445 if (act && !act->id.lib){
2446 /* Find the appropriate channel */
2447 chan= get_named_actionchannel(act, oldname);
2448 if(chan) BLI_strncpy(chan->name, newname, MAXBONENAME);
2451 /* Rename the pose channel, if it exists */
2453 bPoseChannel *pchan = get_pose_channel(ob->pose, oldname);
2455 BLI_strncpy (pchan->name, newname, MAXBONENAME);
2459 /* and actually do the NLA too */
2462 /* Update any object constraints to use the new bone name */
2463 for(cob= G.main->object.first; cob; cob= cob->id.next) {
2464 if(cob->constraints.first)
2465 constraint_bone_name_fix(ob, &cob->constraints, oldname, newname);
2467 bPoseChannel *pchan;
2468 for (pchan = cob->pose->chanbase.first; pchan; pchan=pchan->next) {
2469 constraint_bone_name_fix(ob, &pchan->constraints, oldname, newname);
2476 /* See if an object is parented to this armature */
2477 if (ob->parent && (ob->parent->data == arm)) {
2478 if(ob->partype==PARBONE) {
2479 /* bone name in object */
2480 if (!strcmp(ob->parsubstr, oldname))
2481 BLI_strncpy(ob->parsubstr, newname, MAXBONENAME);
2483 else if(ob->partype==PARSKEL) {
2485 /* bone name in defgroup */
2486 for (dg=ob->defbase.first; dg; dg=dg->next) {
2487 if(!strcmp(dg->name, oldname))
2488 BLI_strncpy(dg->name, newname, MAXBONENAME);
2496 /* context editmode object */
2497 void armature_flip_names(void)
2502 for (ebone = G.edbo.first; ebone; ebone=ebone->next) {
2503 if(ebone->flag & BONE_SELECTED) {
2504 BLI_strncpy(newname, ebone->name, sizeof(newname));
2505 bone_flip_name(newname, 1); // 1 = do strip off number extensions
2506 armature_bone_rename(G.obedit->data, ebone->name, newname);
2510 allqueue(REDRAWVIEW3D, 0);
2511 allqueue(REDRAWBUTSEDIT, 0);
2512 allqueue(REDRAWBUTSOBJECT, 0);
2513 allqueue (REDRAWACTION, 0);
2514 allqueue(REDRAWOOPS, 0);
2515 BIF_undo_push("Flip names");
2519 /* context; editmode armature */
2520 EditBone *armature_bone_get_mirrored(EditBone *ebo)
2522 EditBone *eboflip= NULL;
2525 BLI_strncpy(name, ebo->name, sizeof(name));
2526 bone_flip_name(name, 0); // 0 = don't strip off number extensions
2528 for (eboflip=G.edbo.first; eboflip; eboflip=eboflip->next)
2530 if (!strcmp (name, eboflip->name)) break;