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 * Contributor(s): Blender Foundation, 2002-2009 full recode.
25 * ***** END GPL LICENSE BLOCK *****
34 #include "MEM_guardedalloc.h"
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_constraint_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_nla_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_space_types.h"
49 #include "DNA_userdef_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_modifier_types.h"
52 #include "DNA_ipo_types.h"
53 #include "DNA_curve_types.h"
55 #include "BLI_blenlib.h"
56 #include "BLI_arithb.h"
57 #include "BLI_editVert.h"
58 #include "BLI_ghash.h"
60 #include "BKE_action.h"
61 #include "BKE_armature.h"
62 #include "BKE_constraint.h"
63 #include "BKE_context.h"
64 #include "BKE_deform.h"
65 #include "BKE_depsgraph.h"
66 #include "BKE_DerivedMesh.h"
67 #include "BKE_global.h"
69 #include "BKE_object.h"
70 #include "BKE_report.h"
71 #include "BKE_subsurf.h"
72 #include "BKE_utildefines.h"
73 #include "BKE_modifier.h"
78 #include "RNA_access.h"
79 #include "RNA_define.h"
84 #include "ED_armature.h"
86 #include "ED_object.h"
87 #include "ED_screen.h"
89 #include "ED_view3d.h"
91 #include "armature_intern.h"
92 #include "meshlaplacian.h"
98 /* ************* XXX *************** */
99 static int okee() {return 0;}
100 static int pupmenu() {return 0;}
101 static void waitcursor() {};
102 static void error() {};
103 static void error_libdata() {}
104 static void BIF_undo_push() {}
105 static void adduplicate() {}
106 /* ************* XXX *************** */
108 /* **************** tools on Editmode Armature **************** */
110 /* Sync selection to parent for connected children */
111 static void armature_sync_selection(ListBase *edbo)
115 for (ebo=edbo->first; ebo; ebo= ebo->next) {
116 if ((ebo->flag & BONE_CONNECTED) && (ebo->parent)) {
117 if (ebo->parent->flag & BONE_TIPSEL)
118 ebo->flag |= BONE_ROOTSEL;
120 ebo->flag &= ~BONE_ROOTSEL;
123 if ((ebo->flag & BONE_TIPSEL) && (ebo->flag & BONE_ROOTSEL))
124 ebo->flag |= BONE_SELECTED;
126 ebo->flag &= ~BONE_SELECTED;
130 /* converts Bones to EditBone list, used for tools as well */
131 void make_boneList(ListBase *edbo, ListBase *bones, EditBone *parent)
141 for (curBone=bones->first; curBone; curBone=curBone->next) {
142 eBone= MEM_callocN(sizeof(EditBone), "make_editbone");
144 /* Copy relevant data from bone to eBone */
145 eBone->parent= parent;
146 BLI_strncpy(eBone->name, curBone->name, 32);
147 eBone->flag = curBone->flag;
149 /* fix selection flags */
150 if (eBone->flag & BONE_SELECTED) {
151 eBone->flag |= BONE_TIPSEL;
152 if (eBone->parent && (eBone->flag & BONE_CONNECTED))
153 eBone->parent->flag |= BONE_TIPSEL;
155 eBone->flag |= BONE_ROOTSEL;
158 eBone->flag &= ~BONE_ROOTSEL;
160 VECCOPY(eBone->head, curBone->arm_head);
161 VECCOPY(eBone->tail, curBone->arm_tail);
166 VecSubf(delta, eBone->tail, eBone->head);
167 vec_roll_to_mat3(delta, 0.0, postmat);
169 Mat3CpyMat4(premat, curBone->arm_mat);
171 Mat3Inv(imat, postmat);
172 Mat3MulMat3(difmat, imat, premat);
174 eBone->roll = atan2(difmat[2][0], difmat[2][2]);
176 /* rest of stuff copy */
177 eBone->length= curBone->length;
178 eBone->dist= curBone->dist;
179 eBone->weight= curBone->weight;
180 eBone->xwidth= curBone->xwidth;
181 eBone->zwidth= curBone->zwidth;
182 eBone->ease1= curBone->ease1;
183 eBone->ease2= curBone->ease2;
184 eBone->rad_head= curBone->rad_head;
185 eBone->rad_tail= curBone->rad_tail;
186 eBone->segments = curBone->segments;
187 eBone->layer = curBone->layer;
189 BLI_addtail(edbo, eBone);
191 /* Add children if necessary */
192 if (curBone->childbase.first)
193 make_boneList(edbo, &curBone->childbase, eBone);
197 /* nasty stuff for converting roll in editbones into bones */
198 /* also sets restposition in armature (arm_mat) */
199 static void fix_bonelist_roll (ListBase *bonelist, ListBase *editbonelist)
209 for (curBone=bonelist->first; curBone; curBone=curBone->next) {
210 /* sets local matrix and arm_mat (restpos) */
211 where_is_armature_bone(curBone, curBone->parent);
213 /* Find the associated editbone */
214 for (ebone = editbonelist->first; ebone; ebone=ebone->next)
215 if ((Bone*)ebone->temp == curBone)
219 /* Get the ebone premat */
220 VecSubf(delta, ebone->tail, ebone->head);
221 vec_roll_to_mat3(delta, ebone->roll, premat);
223 /* Get the bone postmat */
224 Mat3CpyMat4(postmat, curBone->arm_mat);
226 Mat3Inv(imat, premat);
227 Mat3MulMat3(difmat, imat, postmat);
229 printf ("Bone %s\n", curBone->name);
230 printmatrix4("premat", premat);
231 printmatrix4("postmat", postmat);
232 printmatrix4("difmat", difmat);
233 printf ("Roll = %f\n", (-atan2(difmat[2][0], difmat[2][2]) * (180.0/M_PI)));
235 curBone->roll = -atan2(difmat[2][0], difmat[2][2]);
237 /* and set restposition again */
238 where_is_armature_bone(curBone, curBone->parent);
240 fix_bonelist_roll(&curBone->childbase, editbonelist);
244 /* put EditMode back in Object */
245 void ED_armature_from_edit(Scene *scene, Object *obedit)
247 bArmature *arm= obedit->data;
248 EditBone *eBone, *neBone;
255 /* remove zero sized bones, this gives instable restposes */
256 for (eBone=arm->edbo->first; eBone; eBone= neBone) {
257 float len= VecLenf(eBone->head, eBone->tail);
259 if (len <= 0.000001f) { /* FLT_EPSILON is too large? */
262 /* Find any bones that refer to this bone */
263 for (fBone=arm->edbo->first; fBone; fBone= fBone->next) {
264 if (fBone->parent==eBone)
265 fBone->parent= eBone->parent;
267 printf("Warning: removed zero sized bone: %s\n", eBone->name);
268 BLI_freelinkN(arm->edbo, eBone);
272 /* Copy the bones from the editData into the armature */
273 for (eBone=arm->edbo->first; eBone; eBone=eBone->next) {
274 newBone= MEM_callocN(sizeof(Bone), "bone");
275 eBone->temp= newBone; /* Associate the real Bones with the EditBones */
277 BLI_strncpy(newBone->name, eBone->name, 32);
278 memcpy(newBone->head, eBone->head, sizeof(float)*3);
279 memcpy(newBone->tail, eBone->tail, sizeof(float)*3);
280 newBone->flag= eBone->flag;
281 if (eBone->flag & BONE_ACTIVE)
282 newBone->flag |= BONE_SELECTED; /* important, editbones can be active with only 1 point selected */
283 newBone->roll = 0.0f;
285 newBone->weight = eBone->weight;
286 newBone->dist = eBone->dist;
288 newBone->xwidth = eBone->xwidth;
289 newBone->zwidth = eBone->zwidth;
290 newBone->ease1= eBone->ease1;
291 newBone->ease2= eBone->ease2;
292 newBone->rad_head= eBone->rad_head;
293 newBone->rad_tail= eBone->rad_tail;
294 newBone->segments= eBone->segments;
295 newBone->layer = eBone->layer;
298 /* Fix parenting in a separate pass to ensure ebone->bone connections
299 are valid at this point */
300 for (eBone=arm->edbo->first;eBone;eBone=eBone->next) {
301 newBone= (Bone *)eBone->temp;
303 newBone->parent= (Bone *)eBone->parent->temp;
304 BLI_addtail(&newBone->parent->childbase, newBone);
307 float M_boneRest[3][3];
308 float M_parentRest[3][3];
309 float iM_parentRest[3][3];
312 /* Get the parent's matrix (rotation only) */
313 VecSubf(delta, eBone->parent->tail, eBone->parent->head);
314 vec_roll_to_mat3(delta, eBone->parent->roll, M_parentRest);
316 /* Get this bone's matrix (rotation only) */
317 VecSubf(delta, eBone->tail, eBone->head);
318 vec_roll_to_mat3(delta, eBone->roll, M_boneRest);
320 /* Invert the parent matrix */
321 Mat3Inv(iM_parentRest, M_parentRest);
323 /* Get the new head and tail */
324 VecSubf(newBone->head, eBone->head, eBone->parent->tail);
325 VecSubf(newBone->tail, eBone->tail, eBone->parent->tail);
327 Mat3MulVecfl(iM_parentRest, newBone->head);
328 Mat3MulVecfl(iM_parentRest, newBone->tail);
331 /* ...otherwise add this bone to the armature's bonebase */
333 BLI_addtail(&arm->bonebase, newBone);
336 /* Make a pass through the new armature to fix rolling */
337 /* also builds restposition again (like where_is_armature) */
338 fix_bonelist_roll(&arm->bonebase, arm->edbo);
340 /* so all users of this armature should get rebuilt */
341 for (obt= G.main->object.first; obt; obt= obt->id.next) {
343 armature_rebuild_pose(obt, arm);
346 DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
351 void apply_rot_armature (Scene *scene, Object *ob, float mat[3][3])
354 bArmature *arm= ob->data;
355 float scale = Mat3ToScalef(mat); /* store the scale of the matrix here to use on envelopes */
357 /* Put the armature into editmode */
358 ED_armature_to_edit(ob);
360 /* Do the rotations */
361 for (ebone = arm->edbo->first; ebone; ebone=ebone->next){
362 Mat3MulVecfl(mat, ebone->head);
363 Mat3MulVecfl(mat, ebone->tail);
365 ebone->rad_head *= scale;
366 ebone->rad_tail *= scale;
367 ebone->dist *= scale;
370 /* Turn the list into an armature */
371 ED_armature_from_edit(scene, ob);
372 ED_armature_edit_free(ob);
375 /* 0 == do center, 1 == center new, 2 == center cursor */
376 void docenter_armature (Scene *scene, View3D *v3d, Object *ob, int centermode)
378 Object *obedit= scene->obedit; // XXX get from context
380 bArmature *arm= ob->data;
381 float cent[3] = {0.0f, 0.0f, 0.0f};
382 float min[3], max[3];
385 /* Put the armature into editmode */
387 ED_armature_to_edit(ob);
389 /* Find the centerpoint */
390 if (centermode == 2) {
391 float *fp= give_cursor(scene, v3d);
393 Mat4Invert(ob->imat, ob->obmat);
394 Mat4MulVecfl(ob->imat, cent);
397 INIT_MINMAX(min, max);
399 for (ebone= arm->edbo->first; ebone; ebone=ebone->next) {
400 DO_MINMAX(ebone->head, min, max);
401 DO_MINMAX(ebone->tail, min, max);
404 cent[0]= (min[0]+max[0])/2.0f;
405 cent[1]= (min[1]+max[1])/2.0f;
406 cent[2]= (min[2]+max[2])/2.0f;
409 /* Do the adjustments */
410 for (ebone= arm->edbo->first; ebone; ebone=ebone->next){
411 VecSubf(ebone->head, ebone->head, cent);
412 VecSubf(ebone->tail, ebone->tail, cent);
415 /* Turn the list into an armature */
416 ED_armature_from_edit(scene, ob);
418 /* Adjust object location for new centerpoint */
419 if(centermode && obedit==NULL) {
420 Mat3CpyMat4(omat, ob->obmat);
422 Mat3MulVecfl(omat, cent);
423 ob->loc[0]+= cent[0];
424 ob->loc[1]+= cent[1];
425 ob->loc[2]+= cent[2];
428 ED_armature_edit_free(ob);
431 /* helper for apply_armature_pose2bones - fixes parenting of objects that are bone-parented to armature */
432 static void applyarmature_fix_boneparents (Scene *scene, Object *armob)
436 /* go through all objects in database */
437 for (ob= G.main->object.first; ob; ob= ob->id.next) {
438 /* if parent is bone in this armature, apply corrections */
439 if ((ob->parent == armob) && (ob->partype == PARBONE)) {
440 /* apply current transform from parent (not yet destroyed),
441 * then calculate new parent inverse matrix
443 ED_object_apply_obmat(ob);
445 what_does_parent(scene, ob, &workob);
446 Mat4Invert(ob->parentinv, workob.obmat);
451 static EditBone *editbone_name_exists (ListBase *edbo, char *name)
455 for (eBone=edbo->first; eBone; eBone=eBone->next) {
456 if (!strcmp(name, eBone->name))
462 /* note: there's a unique_bone_name() too! */
463 void unique_editbone_name (ListBase *edbo, char *name)
469 if (editbone_name_exists(edbo, name)) {
470 /* Strip off the suffix, if it's a number */
471 number= strlen(name);
472 if (number && isdigit(name[number-1])) {
473 dot= strrchr(name, '.'); // last occurrance
478 for (number = 1; number <=999; number++) {
479 sprintf(tempname, "%s.%03d", name, number);
480 if (!editbone_name_exists(edbo, tempname)) {
481 BLI_strncpy(name, tempname, 32);
488 /* set the current pose as the restpose */
489 void apply_armature_pose2bones(Scene *scene, Object *obedit)
491 bArmature *arm= obedit->data;
496 /* don't check if editmode (should be done by caller) */
497 if (object_data_is_libdata(obedit)) {
502 /* helpful warnings... */
503 // TODO: add warnings to be careful about actions, applying deforms first, etc.
505 /* Get editbones of active armature to alter */
506 ED_armature_to_edit(obedit);
508 /* get pose of active object and move it out of posemode */
511 for (pchan=pose->chanbase.first; pchan; pchan=pchan->next) {
512 curbone= editbone_name_exists(arm->edbo, pchan->name);
514 /* simply copy the head/tail values from pchan over to curbone */
515 VECCOPY(curbone->head, pchan->pose_head);
516 VECCOPY(curbone->tail, pchan->pose_tail);
519 * 1. find auto-calculated roll value for this bone now
520 * 2. remove this from the 'visual' y-rotation
523 float premat[3][3], imat[3][3],pmat[3][3], tmat[3][3];
524 float delta[3], eul[3];
526 /* obtain new auto y-rotation */
527 VecSubf(delta, curbone->tail, curbone->head);
528 vec_roll_to_mat3(delta, 0.0, premat);
529 Mat3Inv(imat, premat);
531 /* get pchan 'visual' matrix */
532 Mat3CpyMat4(pmat, pchan->pose_mat);
534 /* remove auto from visual and get euler rotation */
535 Mat3MulMat3(tmat, imat, pmat);
536 Mat3ToEul(tmat, eul);
538 /* just use this euler-y as new roll value */
539 curbone->roll= eul[1];
542 /* clear transform values for pchan */
543 pchan->loc[0]= pchan->loc[1]= pchan->loc[2]= 0;
544 pchan->quat[1]= pchan->quat[2]= pchan->quat[3]= 0;
545 pchan->quat[0]= pchan->size[0]= pchan->size[1]= pchan->size[2]= 1;
548 curbone->flag |= BONE_UNKEYED;
551 /* convert editbones back to bones */
552 ED_armature_from_edit(scene, obedit);
554 /* flush positions of posebones */
555 where_is_pose(scene, obedit);
557 /* fix parenting of objects which are bone-parented */
558 applyarmature_fix_boneparents(scene, obedit);
560 BIF_undo_push("Apply new restpose");
564 /* Helper function for armature joining - link fixing */
565 static void joined_armature_fix_links(Object *tarArm, Object *srcArm, bPoseChannel *pchan, EditBone *curbone)
569 bPoseChannel *pchant;
572 /* let's go through all objects in database */
573 for (ob= G.main->object.first; ob; ob= ob->id.next) {
574 /* do some object-type specific things */
575 if (ob->type == OB_ARMATURE) {
577 for (pchant= pose->chanbase.first; pchant; pchant= pchant->next) {
578 for (con= pchant->constraints.first; con; con= con->next) {
579 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
580 ListBase targets = {NULL, NULL};
581 bConstraintTarget *ct;
583 /* constraint targets */
584 if (cti && cti->get_constraint_targets) {
585 cti->get_constraint_targets(con, &targets);
587 for (ct= targets.first; ct; ct= ct->next) {
588 if (ct->tar == srcArm) {
589 if (strcmp(ct->subtarget, "")==0) {
592 else if (strcmp(ct->subtarget, pchan->name)==0) {
594 strcpy(ct->subtarget, curbone->name);
599 if (cti->flush_constraint_targets)
600 cti->flush_constraint_targets(con, &targets, 0);
603 /* action constraint? */
604 if (con->type == CONSTRAINT_TYPE_ACTION) {
605 bActionConstraint *data= con->data; // XXX old animation system
607 bActionChannel *achan;
612 for (achan= act->chanbase.first; achan; achan= achan->next) {
613 if (strcmp(achan->name, pchan->name)==0)
614 BLI_strncpy(achan->name, curbone->name, 32);
623 /* fix object-level constraints */
625 for (con= ob->constraints.first; con; con= con->next) {
626 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
627 ListBase targets = {NULL, NULL};
628 bConstraintTarget *ct;
630 /* constraint targets */
631 if (cti && cti->get_constraint_targets) {
632 cti->get_constraint_targets(con, &targets);
634 for (ct= targets.first; ct; ct= ct->next) {
635 if (ct->tar == srcArm) {
636 if (strcmp(ct->subtarget, "")==0) {
639 else if (strcmp(ct->subtarget, pchan->name)==0) {
641 strcpy(ct->subtarget, curbone->name);
646 if (cti->flush_constraint_targets)
647 cti->flush_constraint_targets(con, &targets, 0);
652 /* See if an object is parented to this armature */
653 if (ob->parent && (ob->parent == srcArm)) {
654 /* Is object parented to a bone of this src armature? */
655 if (ob->partype==PARBONE) {
656 /* bone name in object */
657 if (!strcmp(ob->parsubstr, pchan->name))
658 BLI_strncpy(ob->parsubstr, curbone->name, 32);
661 /* make tar armature be new parent */
667 int join_armature(Scene *scene, View3D *v3d)
669 Object *ob= scene->basact->object; // XXX context
670 bArmature *arm= ob->data;
671 Base *base, *nextbase;
673 bPoseChannel *pchan, *pchann;
675 float mat[4][4], oimat[4][4];
677 /* Ensure we're not in editmode and that the active object is an armature*/
678 if (ob->type!=OB_ARMATURE) return 0;
679 if(arm->edbo) return 0;
681 if (object_data_is_libdata(ob)) {
686 /* Get editbones of active armature to add editbones to */
687 ED_armature_to_edit(ob);
689 /* get pose of active object and move it out of posemode */
691 ob->flag &= ~OB_POSEMODE;
693 for (base=FIRSTBASE; base; base=nextbase) {
694 nextbase = base->next;
695 if (TESTBASE(v3d, base)){
696 if ((base->object->type==OB_ARMATURE) && (base->object!=ob)) {
697 bArmature *curarm= base->object->data;
699 /* Make a list of editbones in current armature */
700 ED_armature_to_edit(base->object);
702 /* Get Pose of current armature */
703 opose= base->object->pose;
704 base->object->flag &= ~OB_POSEMODE;
705 BASACT->flag &= ~OB_POSEMODE;
707 /* Find the difference matrix */
708 Mat4Invert(oimat, ob->obmat);
709 Mat4MulMat4(mat, base->object->obmat, oimat);
711 /* Copy bones and posechannels from the object to the edit armature */
712 for (pchan=opose->chanbase.first; pchan; pchan=pchann) {
714 curbone= editbone_name_exists(curarm->edbo, pchan->name);
717 unique_editbone_name(arm->edbo, curbone->name);
719 /* Transform the bone */
729 VecSubf(delta, curbone->tail, curbone->head);
730 vec_roll_to_mat3(delta, curbone->roll, temp);
732 Mat4MulMat34(premat, temp, mat);
734 Mat4MulVecfl(mat, curbone->head);
735 Mat4MulVecfl(mat, curbone->tail);
737 /* Get the postmat */
738 VecSubf(delta, curbone->tail, curbone->head);
739 vec_roll_to_mat3(delta, curbone->roll, temp);
740 Mat4CpyMat3(postmat, temp);
743 Mat4Invert(imat, premat);
744 Mat4MulMat4(difmat, postmat, imat);
746 curbone->roll -= atan2(difmat[2][0], difmat[2][2]);
749 /* Fix Constraints and Other Links to this Bone and Armature */
750 joined_armature_fix_links(ob, base->object, pchan, curbone);
753 BLI_strncpy(pchan->name, curbone->name, sizeof(pchan->name));
756 BLI_remlink(curarm->edbo, curbone);
757 BLI_addtail(arm->edbo, curbone);
759 BLI_remlink(&opose->chanbase, pchan);
760 BLI_addtail(&pose->chanbase, pchan);
763 ED_base_object_free_and_unlink(scene, base);
768 DAG_scene_sort(scene); // because we removed object(s)
770 ED_armature_from_edit(scene, ob);
771 ED_armature_edit_free(ob);
776 /* Helper function for armature separating - link fixing */
777 static void separated_armature_fix_links(Object *origArm, Object *newArm)
780 bPoseChannel *pchan, *pcha, *pchb;
782 ListBase *opchans, *npchans;
784 /* get reference to list of bones in original and new armatures */
785 opchans= &origArm->pose->chanbase;
786 npchans= &newArm->pose->chanbase;
788 /* let's go through all objects in database */
789 for (ob= G.main->object.first; ob; ob= ob->id.next) {
790 /* do some object-type specific things */
791 if (ob->type == OB_ARMATURE) {
792 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
793 for (con= pchan->constraints.first; con; con= con->next) {
794 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
795 ListBase targets = {NULL, NULL};
796 bConstraintTarget *ct;
798 /* constraint targets */
799 if (cti && cti->get_constraint_targets) {
800 cti->get_constraint_targets(con, &targets);
802 for (ct= targets.first; ct; ct= ct->next) {
803 /* any targets which point to original armature are redirected to the new one only if:
804 * - the target isn't origArm/newArm itself
805 * - the target is one that can be found in newArm/origArm
807 if ((ct->tar == origArm) && (ct->subtarget[0] != 0)) {
808 for (pcha=npchans->first, pchb=npchans->last; pcha && pchb; pcha=pcha->next, pchb=pchb->prev) {
809 /* check if either one matches */
810 if ( (strcmp(pcha->name, ct->subtarget)==0) ||
811 (strcmp(pchb->name, ct->subtarget)==0) )
817 /* check if both ends have met (to stop checking) */
818 if (pcha == pchb) break;
821 else if ((ct->tar == newArm) && (ct->subtarget[0] != 0)) {
822 for (pcha=opchans->first, pchb=opchans->last; pcha && pchb; pcha=pcha->next, pchb=pchb->prev) {
823 /* check if either one matches */
824 if ( (strcmp(pcha->name, ct->subtarget)==0) ||
825 (strcmp(pchb->name, ct->subtarget)==0) )
831 /* check if both ends have met (to stop checking) */
832 if (pcha == pchb) break;
837 if (cti->flush_constraint_targets)
838 cti->flush_constraint_targets(con, &targets, 0);
844 /* fix object-level constraints */
846 for (con= ob->constraints.first; con; con= con->next) {
847 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
848 ListBase targets = {NULL, NULL};
849 bConstraintTarget *ct;
851 /* constraint targets */
852 if (cti && cti->get_constraint_targets) {
853 cti->get_constraint_targets(con, &targets);
855 for (ct= targets.first; ct; ct= ct->next) {
856 /* any targets which point to original armature are redirected to the new one only if:
857 * - the target isn't origArm/newArm itself
858 * - the target is one that can be found in newArm/origArm
860 if ((ct->tar == origArm) && (ct->subtarget[0] != 0)) {
861 for (pcha=npchans->first, pchb=npchans->last; pcha && pchb; pcha=pcha->next, pchb=pchb->prev) {
862 /* check if either one matches */
863 if ( (strcmp(pcha->name, ct->subtarget)==0) ||
864 (strcmp(pchb->name, ct->subtarget)==0) )
870 /* check if both ends have met (to stop checking) */
871 if (pcha == pchb) break;
874 else if ((ct->tar == newArm) && (ct->subtarget[0] != 0)) {
875 for (pcha=opchans->first, pchb=opchans->last; pcha && pchb; pcha=pcha->next, pchb=pchb->prev) {
876 /* check if either one matches */
877 if ( (strcmp(pcha->name, ct->subtarget)==0) ||
878 (strcmp(pchb->name, ct->subtarget)==0) )
884 /* check if both ends have met (to stop checking) */
885 if (pcha == pchb) break;
890 if (cti->flush_constraint_targets)
891 cti->flush_constraint_targets(con, &targets, 0);
896 /* See if an object is parented to this armature */
897 if ((ob->parent) && (ob->parent == origArm)) {
898 /* Is object parented to a bone of this src armature? */
899 if (ob->partype==PARBONE) {
900 /* bone name in object */
901 for (pcha=npchans->first, pchb=npchans->last; pcha && pchb; pcha=pcha->next, pchb=pchb->prev) {
902 /* check if either one matches */
903 if ( (strcmp(pcha->name, ob->parsubstr)==0) ||
904 (strcmp(pchb->name, ob->parsubstr)==0) )
910 /* check if both ends have met (to stop checking) */
911 if (pcha == pchb) break;
918 /* Helper function for armature separating - remove certain bones from the given armature
919 * sel: remove selected bones from the armature, otherwise the unselected bones are removed
920 * (ob is not in editmode)
922 static void separate_armature_bones (Scene *scene, Object *ob, short sel)
924 bArmature *arm= (bArmature *)ob->data;
925 bPoseChannel *pchan, *pchann;
928 /* make local set of editbones to manipulate here */
929 ED_armature_to_edit(ob);
931 /* go through pose-channels, checking if a bone should be removed */
932 for (pchan=ob->pose->chanbase.first; pchan; pchan=pchann) {
934 curbone= editbone_name_exists(arm->edbo, pchan->name);
936 /* check if bone needs to be removed */
937 if ( (sel && (curbone->flag & BONE_SELECTED)) ||
938 (!sel && !(curbone->flag & BONE_SELECTED)) )
943 /* clear the bone->parent var of any bone that had this as its parent */
944 for (ebo= arm->edbo->first; ebo; ebo= ebo->next) {
945 if (ebo->parent == curbone) {
947 ebo->temp= NULL; /* this is needed to prevent random crashes with in ED_armature_from_edit */
948 ebo->flag &= ~BONE_CONNECTED;
952 /* clear the pchan->parent var of any pchan that had this as its parent */
953 for (pchn= ob->pose->chanbase.first; pchn; pchn=pchn->next) {
954 if (pchn->parent == pchan)
958 /* free any of the extra-data this pchan might have */
959 if (pchan->path) MEM_freeN(pchan->path);
960 free_constraints(&pchan->constraints);
962 /* get rid of unneeded bone */
963 BLI_freelinkN(arm->edbo, curbone);
964 BLI_freelinkN(&ob->pose->chanbase, pchan);
968 /* exit editmode (recalculates pchans too) */
969 ED_armature_from_edit(scene, ob);
970 ED_armature_edit_free(ob);
973 /* separate selected bones into their armature */
974 void separate_armature (Scene *scene, View3D *v3d)
976 Object *obedit= scene->obedit; // XXX get from context
977 Object *oldob, *newob;
978 Base *base, *oldbase, *newbase;
981 if ( okee("Separate")==0 ) return;
987 /* we are going to do this as follows (unlike every other instance of separate):
988 * 1. exit editmode +posemode for active armature/base. Take note of what this is.
989 * 2. duplicate base - BASACT is the new one now
990 * 3. for each of the two armatures, enter editmode -> remove appropriate bones -> exit editmode + recalc
991 * 4. fix constraint links
992 * 5. make original armature active and enter editmode
995 /* 1) only edit-base selected */
997 for (base= FIRSTBASE; base; base= base->next) {
998 if (base->lay & v3d->lay) {
999 if (base->object==obedit) base->flag |= 1;
1000 else base->flag &= ~1;
1004 /* 1) store starting settings and exit editmode */
1007 oldob->flag &= ~OB_POSEMODE;
1008 oldbase->flag &= ~OB_POSEMODE;
1010 ED_armature_from_edit(scene, obedit);
1011 ED_armature_edit_free(obedit);
1013 /* 2) duplicate base */
1014 adduplicate(1, USER_DUP_ARM); /* no transform and zero so do get a linked dupli */
1016 newbase= BASACT; /* basact is set in adduplicate() */
1017 newob= newbase->object;
1018 newbase->flag &= ~SELECT;
1021 /* 3) remove bones that shouldn't still be around on both armatures */
1022 separate_armature_bones(scene, oldob, 1);
1023 separate_armature_bones(scene, newob, 0);
1026 /* 4) fix links before depsgraph flushes */ // err... or after?
1027 separated_armature_fix_links(oldob, newob);
1029 DAG_object_flush_update(scene, oldob, OB_RECALC_DATA); /* this is the original one */
1030 DAG_object_flush_update(scene, newob, OB_RECALC_DATA); /* this is the separated one */
1033 /* 5) restore original conditions */
1036 BASACT->flag |= SELECT;
1038 ED_armature_to_edit(obedit);
1040 /* recalc/redraw + cleanup */
1043 BIF_undo_push("Separate Armature");
1046 /* **************** END tools on Editmode Armature **************** */
1047 /* **************** PoseMode & EditMode *************************** */
1049 /* only for opengl selection indices */
1050 Bone *get_indexed_bone (Object *ob, int index)
1052 bPoseChannel *pchan;
1055 if(ob->pose==NULL) return NULL;
1056 index>>=16; // bone selection codes use left 2 bytes
1058 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next, a++) {
1059 if(a==index) return pchan->bone;
1064 /* See if there are any selected bones in this buffer */
1065 /* only bones from base are checked on */
1066 static void *get_bone_from_selectbuffer(Scene *scene, Base *base, unsigned int *buffer, short hits, short findunsel)
1068 Object *obedit= scene->obedit; // XXX get from context
1071 void *firstunSel=NULL, *firstSel=NULL, *data;
1072 unsigned int hitresult;
1073 short i, takeNext=0, sel;
1075 for (i=0; i< hits; i++){
1076 hitresult = buffer[3+(i*4)];
1078 if (!(hitresult & BONESEL_NOSEL)) { // -1
1079 if(hitresult & BONESEL_ANY) { // to avoid including objects in selection
1081 hitresult &= ~(BONESEL_ANY);
1082 /* Determine what the current bone is */
1083 if (obedit==NULL || base->object!=obedit) {
1084 /* no singular posemode, so check for correct object */
1085 if(base->selcol == (hitresult & 0xFFFF)) {
1086 bone = get_indexed_bone(base->object, hitresult);
1089 sel = (bone->flag & BONE_SELECTED);
1091 sel = !(bone->flag & BONE_SELECTED);
1101 bArmature *arm= obedit->data;
1103 ebone = BLI_findlink(arm->edbo, hitresult);
1105 sel = (ebone->flag & BONE_SELECTED);
1107 sel = !(ebone->flag & BONE_SELECTED);
1114 if(!firstSel) firstSel= data;
1136 /* used by posemode as well editmode */
1137 /* only checks scene->basact! */
1138 static void *get_nearest_bone (bContext *C, short findunsel)
1142 unsigned int buffer[MAXPICKBUF];
1145 view3d_set_viewcontext(C, &vc);
1147 // rect.xmin= ... mouseco!
1150 hits= view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect);
1153 return get_bone_from_selectbuffer(vc.scene, vc.scene->basact, buffer, hits, findunsel);
1158 /* helper for setflag_sel_bone() */
1159 static void bone_setflag (int *bone, int flag, short mode)
1162 /* exception for inverse flags */
1163 if (flag == BONE_NO_DEFORM) {
1183 /* Get the first available child of an editbone */
1184 static EditBone *editbone_get_child(bArmature *arm, EditBone *pabone, short use_visibility)
1186 EditBone *curbone, *chbone=NULL;
1188 for (curbone= arm->edbo->first; curbone; curbone= curbone->next) {
1189 if (curbone->parent == pabone) {
1190 if (use_visibility) {
1191 if ((arm->layer & curbone->layer) && !(pabone->flag & BONE_HIDDEN_A))
1202 void armature_select_hierarchy(Scene *scene, short direction, short add_to_sel)
1204 Object *obedit= scene->obedit; // XXX get from context
1207 EditBone *curbone, *pabone, *chbone;
1209 if (!obedit) return;
1211 arm= (bArmature *)ob->data;
1213 for (curbone= arm->edbo->first; curbone; curbone= curbone->next) {
1214 if (EBONE_VISIBLE(arm, curbone)) {
1215 if (curbone->flag & (BONE_ACTIVE)) {
1216 if (direction == BONE_SELECT_PARENT) {
1217 if (curbone->parent == NULL) continue;
1218 else pabone = curbone->parent;
1220 if (EBONE_VISIBLE(arm, pabone)) {
1221 pabone->flag |= (BONE_ACTIVE|BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1222 if (pabone->parent) pabone->parent->flag |= BONE_TIPSEL;
1224 if (!add_to_sel) curbone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1225 curbone->flag &= ~BONE_ACTIVE;
1230 else { // BONE_SELECT_CHILD
1231 chbone = editbone_get_child(arm, curbone, 1);
1232 if (chbone == NULL) continue;
1234 if (EBONE_VISIBLE(arm, chbone)) {
1235 chbone->flag |= (BONE_ACTIVE|BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1238 curbone->flag &= ~(BONE_SELECTED|BONE_ROOTSEL);
1239 if (curbone->parent) curbone->parent->flag &= ~BONE_TIPSEL;
1241 curbone->flag &= ~BONE_ACTIVE;
1249 armature_sync_selection(arm->edbo);
1251 if (direction==BONE_SELECT_PARENT)
1252 BIF_undo_push("Select edit bone parent");
1253 if (direction==BONE_SELECT_CHILD)
1254 BIF_undo_push("Select edit bone child");
1257 /* used by posemode and editmode */
1258 void setflag_armature (Scene *scene, short mode)
1260 Object *obedit= scene->obedit; // XXX get from context
1272 arm= (bArmature *)ob->data;
1274 /* get flag to set (sync these with the ones used in eBone_Flag */
1276 flag= pupmenu("Disable Setting%t|Draw Wire%x1|Deform%x2|Mult VG%x3|Hinge%x4|No Scale%x5|Locked%x6");
1278 flag= pupmenu("Enable Setting%t|Draw Wire%x1|Deform%x2|Mult VG%x3|Hinge%x4|No Scale%x5|Locked%x6");
1280 flag= pupmenu("Toggle Setting%t|Draw Wire%x1|Deform%x2|Mult VG%x3|Hinge%x4|No Scale%x5|Locked%x6");
1282 case 1: flag = BONE_DRAWWIRE; break;
1283 case 2: flag = BONE_NO_DEFORM; break;
1284 case 3: flag = BONE_MULT_VG_ENV; break;
1285 case 4: flag = BONE_HINGE; break;
1286 case 5: flag = BONE_NO_SCALE; break;
1287 case 6: flag = BONE_EDITMODE_LOCKED; break;
1291 /* determine which mode armature is in */
1292 if ((!obedit) && (ob->flag & OB_POSEMODE)) {
1293 /* deal with pose channels */
1294 bPoseChannel *pchan;
1297 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1298 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
1299 if (pchan->bone->flag & BONE_SELECTED) {
1300 bone_setflag(&pchan->bone->flag, flag, mode);
1306 /* deal with editbones */
1310 for (curbone= arm->edbo->first; curbone; curbone= curbone->next) {
1311 if (arm->layer & curbone->layer) {
1312 if (curbone->flag & BONE_SELECTED) {
1313 bone_setflag(&curbone->flag, flag, mode);
1319 BIF_undo_push("Change Bone Setting");
1322 /* **************** END PoseMode & EditMode *************************** */
1323 /* **************** Posemode stuff ********************** */
1326 static void selectconnected_posebonechildren (Object *ob, Bone *bone)
1329 int shift= 0; // XXX
1331 if (!(bone->flag & BONE_CONNECTED))
1334 // XXX old cruft! use notifiers instead
1335 //select_actionchannel_by_name (ob->action, bone->name, !(shift));
1338 bone->flag &= ~BONE_SELECTED;
1340 bone->flag |= BONE_SELECTED;
1342 for (curBone=bone->childbase.first; curBone; curBone=curBone->next){
1343 selectconnected_posebonechildren (ob, curBone);
1347 /* within active object context */
1348 void selectconnected_posearmature(bContext *C)
1350 Object *ob= CTX_data_edit_object(C);
1351 Bone *bone, *curBone, *next;
1352 int shift= 0; // XXX
1355 bone= get_nearest_bone(C, 0);
1357 bone = get_nearest_bone(C, 1);
1362 /* Select parents */
1363 for (curBone=bone; curBone; curBone=next){
1364 // XXX old cruft! use notifiers instead
1365 //select_actionchannel_by_name (ob->action, curBone->name, !(shift));
1367 curBone->flag &= ~BONE_SELECTED;
1369 curBone->flag |= BONE_SELECTED;
1371 if (curBone->flag & BONE_CONNECTED)
1372 next=curBone->parent;
1377 /* Select children */
1378 for (curBone=bone->childbase.first; curBone; curBone=next){
1379 selectconnected_posebonechildren (ob, curBone);
1382 // XXX this only counted the number of pose channels selected
1383 //countall(); // flushes selection!
1385 BIF_undo_push("Select connected");
1389 /* **************** END Posemode stuff ********************** */
1390 /* **************** EditMode stuff ********************** */
1392 /* called in space.c */
1393 void selectconnected_armature(bContext *C)
1395 Object *obedit= CTX_data_edit_object(C);
1396 bArmature *arm= obedit->data;
1397 EditBone *bone, *curBone, *next;
1398 int shift= 0; // XXX
1401 bone= get_nearest_bone(C, 0);
1403 bone= get_nearest_bone(C, 1);
1408 /* Select parents */
1409 for (curBone=bone; curBone; curBone=next){
1411 curBone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1414 curBone->flag |= (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1417 if (curBone->flag & BONE_CONNECTED)
1418 next=curBone->parent;
1423 /* Select children */
1425 for (curBone=arm->edbo->first; curBone; curBone=next){
1426 next = curBone->next;
1427 if (curBone->parent == bone){
1428 if (curBone->flag & BONE_CONNECTED){
1430 curBone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1432 curBone->flag |= (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
1447 armature_sync_selection(arm->edbo);
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 (ViewContext *vc, short mval[2], ListBase *edbo, int findunsel, int *selmask)
1459 unsigned int buffer[MAXPICKBUF];
1460 unsigned int hitresult, besthitresult=BONESEL_NOSEL;
1466 rect.xmin= mval[0]-5;
1467 rect.xmax= mval[0]+5;
1468 rect.ymin= mval[1]-5;
1469 rect.ymax= mval[1]+5;
1471 hits= view3d_opengl_select(vc, buffer, MAXPICKBUF, &rect);
1473 rect.xmin= mval[0]-12;
1474 rect.xmax= mval[0]+12;
1475 rect.ymin= mval[1]-12;
1476 rect.ymax= mval[1]+12;
1477 hits= view3d_opengl_select(vc, buffer, MAXPICKBUF, &rect);
1479 /* See if there are any selected bones in this group */
1483 if (!(buffer[3] & BONESEL_NOSEL))
1484 besthitresult= buffer[3];
1487 for (i=0; i< hits; i++) {
1488 hitresult= buffer[3+(i*4)];
1489 if (!(hitresult & BONESEL_NOSEL)) {
1492 ebone = BLI_findlink(edbo, hitresult & ~BONESEL_ANY);
1494 /* clicks on bone points get advantage */
1495 if( hitresult & (BONESEL_ROOT|BONESEL_TIP)) {
1496 /* but also the unselected one */
1498 if( (hitresult & BONESEL_ROOT) && (ebone->flag & BONE_ROOTSEL)==0)
1500 else if( (hitresult & BONESEL_TIP) && (ebone->flag & BONE_TIPSEL)==0)
1510 if((ebone->flag & BONE_SELECTED)==0)
1519 besthitresult= hitresult;
1525 if (!(besthitresult & BONESEL_NOSEL)) {
1527 ebone= BLI_findlink(edbo, besthitresult & ~BONESEL_ANY);
1530 if (besthitresult & BONESEL_ROOT)
1531 *selmask |= BONE_ROOTSEL;
1532 if (besthitresult & BONESEL_TIP)
1533 *selmask |= BONE_TIPSEL;
1534 if (besthitresult & BONESEL_BONE)
1535 *selmask |= BONE_SELECTED;
1543 static void delete_bone(ListBase *edbo, EditBone* exBone)
1547 /* Find any bones that refer to this bone */
1548 for (curBone=edbo->first;curBone;curBone=curBone->next) {
1549 if (curBone->parent==exBone) {
1550 curBone->parent=exBone->parent;
1551 curBone->flag &= ~BONE_CONNECTED;
1555 BLI_freelinkN(edbo, exBone);
1558 /* context: editmode armature */
1559 EditBone *ED_armature_bone_get_mirrored(ListBase *edbo, EditBone *ebo)
1561 EditBone *eboflip= NULL;
1567 BLI_strncpy(name, ebo->name, sizeof(name));
1568 bone_flip_name(name, 0); // 0 = don't strip off number extensions
1570 for (eboflip= edbo->first; eboflip; eboflip=eboflip->next) {
1571 if (ebo != eboflip) {
1572 if (!strcmp (name, eboflip->name))
1580 /* only editmode! */
1581 void delete_armature(Scene *scene)
1583 Object *obedit= scene->obedit; // XXX get from context
1584 bArmature *arm= obedit->data;
1585 EditBone *curBone, *next;
1588 if (okee("Erase selected bone(s)")==0) return;
1590 /* Select mirrored bones */
1591 if (arm->flag & ARM_MIRROR_EDIT) {
1592 for (curBone=arm->edbo->first; curBone; curBone=curBone->next) {
1593 if (arm->layer & curBone->layer) {
1594 if (curBone->flag & BONE_SELECTED) {
1595 next = ED_armature_bone_get_mirrored(arm->edbo, curBone);
1597 next->flag |= BONE_SELECTED;
1603 /* First erase any associated pose channel */
1605 bPoseChannel *chan, *next;
1606 for (chan=obedit->pose->chanbase.first; chan; chan=next) {
1608 curBone = editbone_name_exists(arm->edbo, chan->name);
1610 if (curBone && (curBone->flag & BONE_SELECTED) && (arm->layer & curBone->layer)) {
1611 free_constraints(&chan->constraints);
1612 BLI_freelinkN (&obedit->pose->chanbase, chan);
1615 for (con= chan->constraints.first; con; con= con->next) {
1616 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1617 ListBase targets = {NULL, NULL};
1618 bConstraintTarget *ct;
1620 if (cti && cti->get_constraint_targets) {
1621 cti->get_constraint_targets(con, &targets);
1623 for (ct= targets.first; ct; ct= ct->next) {
1624 if (ct->tar == obedit) {
1625 if (ct->subtarget[0]) {
1626 curBone = editbone_name_exists(arm->edbo, ct->subtarget);
1627 if (curBone && (curBone->flag & BONE_SELECTED) && (arm->layer & curBone->layer)) {
1628 con->flag |= CONSTRAINT_DISABLE;
1629 ct->subtarget[0]= 0;
1635 if (cti->flush_constraint_targets)
1636 cti->flush_constraint_targets(con, &targets, 0);
1644 for (curBone=arm->edbo->first;curBone;curBone=next) {
1646 if (arm->layer & curBone->layer) {
1647 if (curBone->flag & BONE_SELECTED)
1648 delete_bone(arm->edbo, curBone);
1653 armature_sync_selection(arm->edbo);
1655 BIF_undo_push("Delete bone(s)");
1658 /* toggle==0: deselect
1659 * toggle==1: swap (based on test)
1660 * toggle==2: only active tag
1661 * toggle==3: swap (no test)
1663 void deselectall_armature(Object *obedit, int toggle, int doundo)
1665 bArmature *arm= obedit->data;
1670 /* Determine if there are any selected bones
1671 And therefore whether we are selecting or deselecting */
1672 for (eBone=arm->edbo->first;eBone;eBone=eBone->next){
1673 // if(arm->layer & eBone->layer) {
1674 if (eBone->flag & (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL)){
1684 for (eBone=arm->edbo->first;eBone;eBone=eBone->next) {
1686 /* invert selection of bone */
1687 if ((arm->layer & eBone->layer) && (eBone->flag & BONE_HIDDEN_A)==0) {
1688 eBone->flag ^= (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1689 eBone->flag &= ~BONE_ACTIVE;
1694 if(arm->layer & eBone->layer && (eBone->flag & BONE_HIDDEN_A)==0) {
1695 eBone->flag |= (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1697 eBone->parent->flag |= (BONE_TIPSEL);
1701 /* clear active flag */
1702 eBone->flag &= ~(BONE_ACTIVE);
1706 eBone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL | BONE_ACTIVE);
1710 armature_sync_selection(arm->edbo);
1712 if (sel==1) BIF_undo_push("Select All");
1713 else BIF_undo_push("Deselect All");
1718 /* context: editmode armature in view3d */
1719 void mouse_armature(bContext *C, short mval[2], int extend)
1721 Object *obedit= CTX_data_edit_object(C);
1722 bArmature *arm= obedit->data;
1724 EditBone *nearBone = NULL, *ebone;
1727 view3d_set_viewcontext(C, &vc);
1729 nearBone= get_nearest_editbonepoint(&vc, mval, arm->edbo, 1, &selmask);
1733 deselectall_armature(obedit, 0, 0);
1735 /* by definition the non-root connected bones have no root point drawn,
1736 so a root selection needs to be delivered to the parent tip */
1738 if(selmask & BONE_SELECTED) {
1739 if(nearBone->parent && (nearBone->flag & BONE_CONNECTED)) {
1740 /* click in a chain */
1742 /* hold shift inverts this bone's selection */
1743 if(nearBone->flag & BONE_SELECTED) {
1744 /* deselect this bone */
1745 nearBone->flag &= ~(BONE_TIPSEL|BONE_SELECTED);
1746 /* only deselect parent tip if it is not selected */
1747 if(!(nearBone->parent->flag & BONE_SELECTED))
1748 nearBone->parent->flag &= ~BONE_TIPSEL;
1751 /* select this bone */
1752 nearBone->flag |= BONE_TIPSEL;
1753 nearBone->parent->flag |= BONE_TIPSEL;
1757 /* select this bone */
1758 nearBone->flag |= BONE_TIPSEL;
1759 nearBone->parent->flag |= BONE_TIPSEL;
1764 /* hold shift inverts this bone's selection */
1765 if(nearBone->flag & BONE_SELECTED)
1766 nearBone->flag &= ~(BONE_TIPSEL|BONE_ROOTSEL);
1768 nearBone->flag |= (BONE_TIPSEL|BONE_ROOTSEL);
1770 else nearBone->flag |= (BONE_TIPSEL|BONE_ROOTSEL);
1774 if (extend && (nearBone->flag & selmask))
1775 nearBone->flag &= ~selmask;
1777 nearBone->flag |= selmask;
1780 armature_sync_selection(arm->edbo);
1783 /* then now check for active status */
1784 for (ebone=arm->edbo->first;ebone;ebone=ebone->next) ebone->flag &= ~BONE_ACTIVE;
1785 if(nearBone->flag & BONE_SELECTED) nearBone->flag |= BONE_ACTIVE;
1788 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, vc.obedit);
1792 void ED_armature_edit_free(struct Object *ob)
1794 bArmature *arm= ob->data;
1796 /* Clear the editbones list */
1798 if (arm->edbo->first)
1799 BLI_freelistN(arm->edbo);
1800 MEM_freeN(arm->edbo);
1805 void ED_armature_edit_remake(Object *obedit)
1807 if(okee("Reload original data")==0) return;
1809 ED_armature_to_edit(obedit);
1811 // BIF_undo_push("Delete bone");
1814 /* Put armature in EditMode */
1815 void ED_armature_to_edit(Object *ob)
1817 bArmature *arm= ob->data;
1819 ED_armature_edit_free(ob);
1820 arm->edbo= MEM_callocN(sizeof(ListBase), "edbo armature");
1821 make_boneList(arm->edbo, &arm->bonebase,NULL);
1825 /* adjust bone roll to align Z axis with vector
1826 * vec is in local space and is normalized
1828 float ED_rollBoneToVector(EditBone *bone, float new_up_axis[3])
1830 float mat[3][3], nor[3], up_axis[3], vec[3];
1833 VecSubf(nor, bone->tail, bone->head);
1835 vec_roll_to_mat3(nor, 0, mat);
1836 VECCOPY(up_axis, mat[2]);
1838 roll = NormalizedVecAngle2(new_up_axis, up_axis);
1840 Crossf(vec, up_axis, new_up_axis);
1842 if (Inpf(vec, nor) < 0)
1851 /* Set roll value for given bone -> Z-Axis Point up (original method) */
1852 void auto_align_ebone_zaxisup(Scene *scene, View3D *v3d, EditBone *ebone)
1854 float delta[3], curmat[3][3];
1855 float xaxis[3]={1.0f, 0.0f, 0.0f}, yaxis[3], zaxis[3]={0.0f, 0.0f, 1.0f};
1856 float targetmat[3][3], imat[3][3], diffmat[3][3];
1858 /* Find the current bone matrix */
1859 VecSubf(delta, ebone->tail, ebone->head);
1860 vec_roll_to_mat3(delta, 0.0f, curmat);
1862 /* Make new matrix based on y axis & z-up */
1863 VECCOPY(yaxis, curmat[1]);
1866 VECCOPY(targetmat[0], xaxis);
1867 VECCOPY(targetmat[1], yaxis);
1868 VECCOPY(targetmat[2], zaxis);
1869 Mat3Ortho(targetmat);
1871 /* Find the difference between the two matrices */
1872 Mat3Inv(imat, targetmat);
1873 Mat3MulMat3(diffmat, imat, curmat);
1875 // old-method... let's see if using mat3_to_vec_roll is more accurate
1876 //ebone->roll = atan2(diffmat[2][0], diffmat[2][2]);
1877 mat3_to_vec_roll(diffmat, delta, &ebone->roll);
1880 /* Set roll value for given bone -> Z-Axis point towards cursor */
1881 void auto_align_ebone_tocursor(Scene *scene, View3D *v3d, EditBone *ebone)
1883 Object *obedit= scene->obedit; // XXX get from context
1884 float *cursor= give_cursor(scene, v3d);
1885 float delta[3], curmat[3][3];
1886 float mat[4][4], tmat[4][4], imat[4][4];
1887 float rmat[4][4], rot[3];
1890 /* find the current bone matrix as a 4x4 matrix (in Armature Space) */
1891 VecSubf(delta, ebone->tail, ebone->head);
1892 vec_roll_to_mat3(delta, ebone->roll, curmat);
1893 Mat4CpyMat3(mat, curmat);
1894 VECCOPY(mat[3], ebone->head);
1896 /* multiply bone-matrix by object matrix (so that bone-matrix is in WorldSpace) */
1897 Mat4MulMat4(tmat, mat, obedit->obmat);
1898 Mat4Invert(imat, tmat);
1900 /* find position of cursor relative to bone */
1901 VecMat4MulVecfl(vec, imat, cursor);
1903 /* check that cursor is in usable position */
1904 if ((IS_EQ(vec[0], 0)==0) && (IS_EQ(vec[2], 0)==0)) {
1905 /* Compute a rotation matrix around y */
1906 rot[1] = atan2(vec[0], vec[2]);
1907 rot[0] = rot[2] = 0.0f;
1908 EulToMat4(rot, rmat);
1910 /* Multiply the bone matrix by rotation matrix. This should be new bone-matrix */
1911 Mat4MulMat4(tmat, rmat, mat);
1912 Mat3CpyMat4(curmat, tmat);
1914 /* Now convert from new bone-matrix, back to a roll value (in radians) */
1915 mat3_to_vec_roll(curmat, delta, &ebone->roll);
1920 static EnumPropertyItem prop_calc_roll_types[] = {
1921 {0, "GLOBALUP", "Z-Axis Up", ""},
1922 {1, "CURSOR", "Z-Axis to Cursor", ""},
1923 {0, NULL, NULL, NULL}
1926 static int armature_calc_roll_exec(bContext *C, wmOperator *op)
1928 Scene *scene= CTX_data_scene(C);
1929 View3D *v3d= (View3D *)CTX_wm_space_data(C);
1930 Object *ob= CTX_data_edit_object(C);
1931 void (*roll_func)(Scene *, View3D *, EditBone *) = NULL;
1933 /* specific method used to calculate roll depends on mode */
1934 switch (RNA_enum_get(op->ptr, "type")) {
1935 case 1: /* Z-Axis point towards cursor */
1936 roll_func= auto_align_ebone_tocursor;
1938 default: /* Z-Axis Point Up */
1939 roll_func= auto_align_ebone_zaxisup;
1943 /* recalculate roll on selected bones */
1944 CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) {
1945 /* roll func is a callback which assumes that all is well */
1946 roll_func(scene, v3d, ebone);
1951 /* note, notifier might evolve */
1952 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
1954 return OPERATOR_FINISHED;
1957 void ARMATURE_OT_calculate_roll(wmOperatorType *ot)
1960 ot->name= "Recalculate Roll";
1961 ot->idname= "ARMATURE_OT_calculate_roll";
1964 ot->invoke = WM_menu_invoke;
1965 ot->exec = armature_calc_roll_exec;
1966 ot->poll = ED_operator_editarmature;
1969 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1972 RNA_def_enum(ot->srna, "type", prop_calc_roll_types, 0, "Type", "");
1975 /* **************** undo for armatures ************** */
1977 static void undoBones_to_editBones(void *lbuv, void *lbev)
1979 ListBase *lbu= lbuv;
1980 ListBase *edbo= lbev;
1981 EditBone *ebo, *newebo;
1983 BLI_freelistN(edbo);
1986 for(ebo= lbu->first; ebo; ebo= ebo->next) {
1987 newebo= MEM_dupallocN(ebo);
1989 BLI_addtail(edbo, newebo);
1993 for(newebo= edbo->first; newebo; newebo= newebo->next) {
1994 if(newebo->parent) newebo->parent= newebo->parent->temp;
1996 /* be sure they dont hang ever */
1997 for(newebo= edbo->first; newebo; newebo= newebo->next) {
2002 static void *editBones_to_undoBones(void *lbev)
2004 ListBase *edbo= lbev;
2006 EditBone *ebo, *newebo;
2008 lb= MEM_callocN(sizeof(ListBase), "listbase undo");
2011 for(ebo= edbo->first; ebo; ebo= ebo->next) {
2012 newebo= MEM_dupallocN(ebo);
2014 BLI_addtail(lb, newebo);
2018 for(newebo= lb->first; newebo; newebo= newebo->next) {
2019 if(newebo->parent) newebo->parent= newebo->parent->temp;
2025 static void free_undoBones(void *lbv)
2033 static void *get_armature_edit(bContext *C)
2035 Object *obedit= CTX_data_edit_object(C);
2036 if(obedit && obedit->type==OB_ARMATURE) {
2037 bArmature *arm= obedit->data;
2043 /* and this is all the undo system needs to know */
2044 void undo_push_armature(bContext *C, char *name)
2046 // XXX solve getdata()
2047 undo_editmode_push(C, name, get_armature_edit, free_undoBones, undoBones_to_editBones, editBones_to_undoBones, NULL);
2052 /* **************** END EditMode stuff ********************** */
2053 /* *************** Adding stuff in editmode *************** */
2055 /* default bone add, returns it selected, but without tail set */
2056 static EditBone *add_editbone(Object *obedit, char *name)
2058 bArmature *arm= obedit->data;
2060 EditBone *bone= MEM_callocN(sizeof(EditBone), "eBone");
2062 BLI_strncpy(bone->name, name, 32);
2063 unique_editbone_name(arm->edbo, bone->name);
2065 BLI_addtail(arm->edbo, bone);
2067 bone->flag |= BONE_TIPSEL;
2074 bone->rad_head= 0.10f;
2075 bone->rad_tail= 0.05f;
2077 bone->layer= arm->layer;
2082 static void add_primitive_bone(Scene *scene, View3D *v3d, RegionView3D *rv3d, short newob)
2084 Object *obedit= scene->obedit; // XXX get from context
2085 float obmat[3][3], curs[3], viewmat[3][3], totmat[3][3], imat[3][3];
2088 VECCOPY(curs, give_cursor(scene, v3d));
2090 /* Get inverse point for head and orientation for tail */
2091 Mat4Invert(obedit->imat, obedit->obmat);
2092 Mat4MulVecfl(obedit->imat, curs);
2094 if ( !(newob) || (U.flag & USER_ADD_VIEWALIGNED) )
2095 Mat3CpyMat4(obmat, rv3d->viewmat);
2096 else Mat3One(obmat);
2098 Mat3CpyMat4(viewmat, obedit->obmat);
2099 Mat3MulMat3(totmat, obmat, viewmat);
2100 Mat3Inv(imat, totmat);
2102 deselectall_armature(obedit, 0, 0);
2105 bone= add_editbone(obedit, "Bone");
2107 VECCOPY(bone->head, curs);
2109 if ( !(newob) || (U.flag & USER_ADD_VIEWALIGNED) )
2110 VecAddf(bone->tail, bone->head, imat[1]); // bone with unit length 1
2112 VecAddf(bone->tail, bone->head, imat[2]); // bone with unit length 1, pointing up Z
2116 void add_primitiveArmature(Scene *scene, View3D *v3d, int type)
2118 RegionView3D *rv3d= NULL; // XXX get from context
2119 Object *obedit= scene->obedit; // XXX get from context
2122 if(scene->id.lib) return;
2124 G.f &= ~(G_VERTEXPAINT+G_TEXTUREPAINT+G_WEIGHTPAINT+G_SCULPTMODE);
2125 // setcursor_space(SPACE_VIEW3D, CURSOR_STD);
2127 // XXX check_editmode(OB_ARMATURE);
2129 /* If we're not the "obedit", make a new object and enter editmode */
2131 add_object(scene, OB_ARMATURE);
2132 ED_object_base_init_from_view(NULL, BASACT); // XXX NULL is C
2133 obedit= BASACT->object;
2135 where_is_object(scene, obedit);
2137 ED_armature_to_edit(obedit);
2138 // setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
2142 /* no primitive support yet */
2143 add_primitive_bone(scene, v3d, rv3d, newob);
2145 //armature_sync_selection(arm->edbo); // XXX which armature?
2147 if ((newob) && !(U.flag & USER_ADD_EDITMODE)) {
2148 ED_armature_from_edit(scene, obedit);
2149 ED_armature_edit_free(obedit);
2152 BIF_undo_push("Add primitive");
2155 /* the ctrl-click method */
2156 void addvert_armature(Scene *scene, View3D *v3d)
2158 Object *obedit= scene->obedit; // XXX get from context
2159 bArmature *arm= obedit->data;
2160 EditBone *ebone, *newbone, *flipbone;
2161 float *curs, mat[3][3],imat[3][3];
2164 /* find the active or selected bone */
2165 for (ebone = arm->edbo->first; ebone; ebone=ebone->next) {
2166 if (EBONE_VISIBLE(arm, ebone)) {
2167 if (ebone->flag & (BONE_ACTIVE|BONE_TIPSEL))
2173 for (ebone = arm->edbo->first; ebone; ebone=ebone->next) {
2174 if (EBONE_VISIBLE(arm, ebone)) {
2175 if (ebone->flag & (BONE_ACTIVE|BONE_ROOTSEL))
2185 deselectall_armature(obedit, 0, 0);
2187 /* we re-use code for mirror editing... */
2189 if (arm->flag & ARM_MIRROR_EDIT)
2190 flipbone= ED_armature_bone_get_mirrored(arm->edbo, ebone);
2192 for (a=0; a<2; a++) {
2197 SWAP(EditBone *, flipbone, ebone);
2201 newbone= add_editbone(obedit, ebone->name);
2202 newbone->flag |= BONE_ACTIVE;
2205 VECCOPY(newbone->head, ebone->head);
2206 newbone->rad_head= ebone->rad_tail;
2207 newbone->parent= ebone->parent;
2210 VECCOPY(newbone->head, ebone->tail);
2211 newbone->rad_head= ebone->rad_tail;
2212 newbone->parent= ebone;
2213 newbone->flag |= BONE_CONNECTED;
2216 curs= give_cursor(scene, v3d);
2217 VECCOPY(newbone->tail, curs);
2218 VecSubf(newbone->tail, newbone->tail, obedit->obmat[3]);
2221 newbone->tail[0]= -newbone->tail[0];
2223 Mat3CpyMat4(mat, obedit->obmat);
2225 Mat3MulVecfl(imat, newbone->tail);
2227 newbone->length= VecLenf(newbone->head, newbone->tail);
2228 newbone->rad_tail= newbone->length*0.05f;
2229 newbone->dist= newbone->length*0.25f;
2233 armature_sync_selection(arm->edbo);
2235 BIF_undo_push("Add Bone");
2238 /* adds an EditBone between the nominated locations (should be in the right space) */
2239 static EditBone *add_points_bone (Object *obedit, float head[], float tail[])
2243 ebo= add_editbone(obedit, "Bone");
2245 VECCOPY(ebo->head, head);
2246 VECCOPY(ebo->tail, tail);
2252 static EditBone *get_named_editbone(ListBase *edbo, char *name)
2257 for (eBone=edbo->first; eBone; eBone=eBone->next) {
2258 if (!strcmp(name, eBone->name))
2266 static void update_dup_subtarget(Object *obedit, EditBone *dupBone)
2268 /* If an edit bone has been duplicated, lets
2269 * update it's constraints if the subtarget
2270 * they point to has also been duplicated
2272 bArmature *arm = obedit->data;
2273 EditBone *oldtarget, *newtarget;
2275 bConstraint *curcon;
2278 if ( (chan = verify_pose_channel(obedit->pose, dupBone->name)) ) {
2279 if ( (conlist = &chan->constraints) ) {
2280 for (curcon = conlist->first; curcon; curcon=curcon->next) {
2281 /* does this constraint have a subtarget in
2284 bConstraintTypeInfo *cti= constraint_get_typeinfo(curcon);
2285 ListBase targets = {NULL, NULL};
2286 bConstraintTarget *ct;
2288 if (cti && cti->get_constraint_targets) {
2289 cti->get_constraint_targets(curcon, &targets);
2291 for (ct= targets.first; ct; ct= ct->next) {
2292 if ((ct->tar == obedit) && (ct->subtarget[0])) {
2293 oldtarget = get_named_editbone(arm->edbo, ct->subtarget);
2295 /* was the subtarget bone duplicated too? If
2296 * so, update the constraint to point at the
2297 * duplicate of the old subtarget.
2299 if (oldtarget->flag & BONE_SELECTED){
2300 newtarget = (EditBone *) oldtarget->temp;
2301 strcpy(ct->subtarget, newtarget->name);
2307 if (cti->flush_constraint_targets)
2308 cti->flush_constraint_targets(curcon, &targets, 0);
2316 void adduplicate_armature(Scene *scene)
2318 Object *obedit= scene->obedit; // XXX get from context
2319 bArmature *arm= obedit->data;
2320 EditBone *eBone = NULL;
2322 EditBone *firstDup=NULL; /* The beginning of the duplicated bones in the edbo list */
2324 armature_sync_selection(arm->edbo); // XXX why is this needed?
2326 /* Select mirrored bones */
2327 if (arm->flag & ARM_MIRROR_EDIT) {
2328 for (curBone=arm->edbo->first; curBone; curBone=curBone->next) {
2329 if (EBONE_VISIBLE(arm, curBone)) {
2330 if (curBone->flag & BONE_SELECTED) {
2331 eBone = ED_armature_bone_get_mirrored(arm->edbo, curBone);
2333 eBone->flag |= BONE_SELECTED;
2339 /* Find the selected bones and duplicate them as needed */
2340 for (curBone=arm->edbo->first; curBone && curBone!=firstDup; curBone=curBone->next) {
2341 if (EBONE_VISIBLE(arm, curBone)) {
2342 if (curBone->flag & BONE_SELECTED) {
2343 eBone=MEM_callocN(sizeof(EditBone), "addup_editbone");
2344 eBone->flag |= BONE_SELECTED;
2346 /* Copy data from old bone to new bone */
2347 memcpy(eBone, curBone, sizeof(EditBone));
2349 curBone->temp = eBone;
2350 eBone->temp = curBone;
2352 unique_editbone_name(arm->edbo, eBone->name);
2353 BLI_addtail(arm->edbo, eBone);
2357 /* Lets duplicate the list of constraints that the
2361 bPoseChannel *chanold, *channew;
2362 ListBase *listold, *listnew;
2364 chanold = verify_pose_channel(OBACT->pose, curBone->name);
2366 listold = &chanold->constraints;
2368 /* WARNING: this creates a new posechannel, but there will not be an attached bone
2369 * yet as the new bones created here are still 'EditBones' not 'Bones'.
2372 verify_pose_channel(OBACT->pose, eBone->name);
2374 /* copy transform locks */
2375 channew->protectflag = chanold->protectflag;
2377 /* copy bone group */
2378 channew->agrp_index= chanold->agrp_index;
2380 /* ik (dof) settings */
2381 channew->ikflag = chanold->ikflag;
2382 VECCOPY(channew->limitmin, chanold->limitmin);
2383 VECCOPY(channew->limitmax, chanold->limitmax);
2384 VECCOPY(channew->stiffness, chanold->stiffness);
2385 channew->ikstretch= chanold->ikstretch;
2388 listnew = &channew->constraints;
2389 copy_constraints(listnew, listold);
2392 channew->custom= chanold->custom;
2401 /* Run though the list and fix the pointers */
2402 for (curBone=arm->edbo->first; curBone && curBone!=firstDup; curBone=curBone->next) {
2403 if (EBONE_VISIBLE(arm, curBone)) {
2404 if (curBone->flag & BONE_SELECTED) {
2405 eBone=(EditBone*) curBone->temp;
2407 /* If this bone has no parent,
2408 Set the duplicate->parent to NULL
2410 if (!curBone->parent)
2411 eBone->parent = NULL;
2412 /* If this bone has a parent that IS selected,
2413 Set the duplicate->parent to the curBone->parent->duplicate
2415 else if (curBone->parent->flag & BONE_SELECTED)
2416 eBone->parent= (EditBone *)curBone->parent->temp;
2417 /* If this bone has a parent that IS not selected,
2418 Set the duplicate->parent to the curBone->parent
2421 eBone->parent=(EditBone*) curBone->parent;
2422 eBone->flag &= ~BONE_CONNECTED;
2425 /* Lets try to fix any constraint subtargets that might
2426 have been duplicated */
2427 update_dup_subtarget(obedit, eBone);
2432 /* Deselect the old bones and select the new ones */
2434 for (curBone=arm->edbo->first; curBone && curBone!=firstDup; curBone=curBone->next) {
2435 if (EBONE_VISIBLE(arm, curBone))
2436 curBone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL | BONE_ACTIVE);
2439 // XXX BIF_TransformSetUndo("Add Duplicate");
2440 // initTransform(TFM_TRANSLATION, CTX_NO_PET);
2446 /* *************** END Adding stuff in editmode *************** */
2447 /* ************** Add/Remove stuff in editmode **************** */
2449 /* temporary data-structure for merge/fill bones */
2450 typedef struct EditBonePoint {
2451 struct EditBonePoint *next, *prev;
2453 EditBone *head_owner; /* EditBone which uses this point as a 'head' point */
2454 EditBone *tail_owner; /* EditBone which uses this point as a 'tail' point */
2456 float vec[3]; /* the actual location of the point in local/EditMode space */
2459 /* find chain-tips (i.e. bones without children) */
2460 static void chains_find_tips (ListBase *edbo, ListBase *list)
2462 EditBone *curBone, *ebo;
2465 /* note: this is potentially very slow ... there's got to be a better way */
2466 for (curBone= edbo->first; curBone; curBone= curBone->next) {
2469 /* is this bone contained within any existing chain? (skip if so) */
2470 for (ld= list->first; ld; ld= ld->next) {
2471 for (ebo= ld->data; ebo; ebo= ebo->parent) {
2472 if (ebo == curBone) {
2480 /* skip current bone if it is part of an existing chain */
2483 /* is any existing chain part of the chain formed by this bone? */
2485 for (ebo= curBone->parent; ebo; ebo= ebo->parent) {
2486 for (ld= list->first; ld; ld= ld->next) {
2487 if (ld->data == ebo) {
2496 /* current bone has already been added to a chain? */
2499 /* add current bone to a new chain */
2500 ld= MEM_callocN(sizeof(LinkData), "BoneChain");
2502 BLI_addtail(list, ld);
2507 static void fill_add_joint (EditBone *ebo, short eb_tail, ListBase *points)
2514 VECCOPY(vec, ebo->tail);
2517 VECCOPY(vec, ebo->head);
2520 for (ebp= points->first; ebp; ebp= ebp->next) {
2521 if (VecEqual(ebp->vec, vec)) {
2523 if ((ebp->head_owner) && (ebp->head_owner->parent == ebo)) {
2524 /* so this bone's tail owner is this bone */
2525 ebp->tail_owner= ebo;
2531 if ((ebp->tail_owner) && (ebo->parent == ebp->tail_owner)) {
2532 /* so this bone's head owner is this bone */
2533 ebp->head_owner= ebo;
2541 /* allocate a new point if no existing point was related */
2543 ebp= MEM_callocN(sizeof(EditBonePoint), "EditBonePoint");
2546 VECCOPY(ebp->vec, ebo->tail);
2547 ebp->tail_owner= ebo;
2550 VECCOPY(ebp->vec, ebo->head);
2551 ebp->head_owner= ebo;
2554 BLI_addtail(points, ebp);
2558 /* bone adding between selected joints */
2559 void fill_bones_armature(Scene *scene, View3D *v3d)
2561 Object *obedit= scene->obedit; // XXX get from context
2562 bArmature *arm= obedit->data;
2563 EditBone *ebo, *newbone=NULL;
2564 ListBase points = {NULL, NULL};
2567 /* loop over all bones, and only consider if visible */
2568 for (ebo= arm->edbo->first; ebo; ebo= ebo->next) {
2569 if (EBONE_VISIBLE(arm, ebo)) {
2570 if (!(ebo->flag & BONE_CONNECTED) && (ebo->flag & BONE_ROOTSEL))
2571 fill_add_joint(ebo, 0, &points);
2572 if (ebo->flag & BONE_TIPSEL)
2573 fill_add_joint(ebo, 1, &points);
2577 /* the number of joints determines how we fill:
2578 * 1) between joint and cursor (joint=head, cursor=tail)
2579 * 2) between the two joints (order is dependent on active-bone/hierachy)
2580 * 3+) error (a smarter method involving finding chains needs to be worked out
2582 count= BLI_countlist(&points);
2585 error("No joints selected");
2588 else if (count == 1) {
2592 /* Get Points - selected joint */
2593 ebp= (EditBonePoint *)points.first;
2595 /* Get points - cursor (tail) */
2596 fp= give_cursor(scene, v3d);
2599 Mat4Invert(obedit->imat, obedit->obmat);
2600 Mat4MulVecfl(obedit->imat, curs);
2603 newbone= add_points_bone(obedit, ebp->vec, curs);
2605 else if (count == 2) {
2606 EditBonePoint *ebp, *ebp2;
2607 float head[3], tail[3];
2610 /* check that the points don't belong to the same bone */
2611 ebp= (EditBonePoint *)points.first;
2614 if ((ebp->head_owner==ebp2->tail_owner) && (ebp->head_owner!=NULL)) {
2615 error("Same bone selected...");
2616 BLI_freelistN(&points);
2619 if ((ebp->tail_owner==ebp2->head_owner) && (ebp->tail_owner!=NULL)) {
2620 error("Same bone selected...");
2621 BLI_freelistN(&points);
2625 /* find which one should be the 'head' */
2626 if ((ebp->head_owner && ebp2->head_owner) || (ebp->tail_owner && ebp2->tail_owner)) {
2627 /* rule: whichever one is closer to 3d-cursor */
2628 float curs[3], *fp= give_cursor(scene, v3d);
2629 float vecA[3], vecB[3];
2632 /* get cursor location */
2635 Mat4Invert(obedit->imat, obedit->obmat);
2636 Mat4MulVecfl(obedit->imat, curs);
2639 VecSubf(vecA, ebp->vec, curs);
2640 VecSubf(vecB, ebp2->vec, curs);
2641 distA= VecLength(vecA);
2642 distB= VecLength(vecB);
2644 /* compare distances - closer one therefore acts as direction for bone to go */
2645 headtail= (distA < distB) ? 2 : 1;
2647 else if (ebp->head_owner) {
2650 else if (ebp2->head_owner) {
2654 /* assign head/tail combinations */
2655 if (headtail == 2) {
2656 VECCOPY(head, ebp->vec);
2657 VECCOPY(tail, ebp2->vec);
2659 else if (headtail == 1) {
2660 VECCOPY(head, ebp2->vec);
2661 VECCOPY(tail, ebp->vec);
2664 /* add new bone and parent it to the appropriate end */
2666 newbone= add_points_bone(obedit, head, tail);
2668 /* do parenting (will need to set connected flag too) */
2669 if (headtail == 2) {
2670 /* ebp tail or head - tail gets priority */
2671 if (ebp->tail_owner)
2672 newbone->parent= ebp->tail_owner;
2674 newbone->parent= ebp->head_owner;
2677 /* ebp2 tail or head - tail gets priority */
2678 if (ebp2->tail_owner)
2679 newbone->parent= ebp2->tail_owner;
2681 newbone->parent= ebp2->head_owner;
2684 newbone->flag |= BONE_CONNECTED;
2688 // FIXME.. figure out a method for multiple bones
2689 error("Too many points selected");
2690 printf("Points selected: %d \n", count);
2691 BLI_freelistN(&points);
2696 BLI_freelistN(&points);
2698 /* undo + updates */
2699 BIF_undo_push("Fill Bones");
2702 /* this function merges between two bones, removes them and those in-between,
2703 * and adjusts the parent relationships for those in-between
2705 static void bones_merge(Object *obedit, EditBone *start, EditBone *end, EditBone *endchild, ListBase *chains)
2707 bArmature *arm= obedit->data;
2708 EditBone *ebo, *ebone, *newbone;
2710 float head[3], tail[3];
2712 /* check if same bone */
2714 printf("Error: same bone! \n");
2715 printf("\tstart = %s, end = %s \n", start->name, end->name);
2718 /* step 1: add a new bone
2719 * - head = head/tail of start (default head)
2720 * - tail = head/tail of end (default tail)
2721 * - parent = parent of start
2723 if ((start->flag & BONE_TIPSEL) && !(start->flag & (BONE_SELECTED|BONE_ACTIVE))) {
2724 VECCOPY(head, start->tail);
2727 VECCOPY(head, start->head);
2729 if ((end->flag & BONE_ROOTSEL) && !(end->flag & (BONE_SELECTED|BONE_ACTIVE))) {
2730 VECCOPY(tail, end->head);
2733 VECCOPY(tail, end->tail);
2735 newbone= add_points_bone(obedit, head, tail);
2736 newbone->parent = start->parent;
2738 /* step 2a: parent children of in-between bones to newbone */
2739 for (chain= chains->first; chain; chain= chain->next) {
2740 /* ick: we need to check if parent of each bone in chain is one of the bones in the */
2741 for (ebo= chain->data; ebo; ebo= ebo->parent) {
2744 /* try to find which bone from the list to be removed, is the parent */
2745 for (ebone= end; ebone; ebone= ebone->parent) {
2746 if (ebo->parent == ebone) {
2752 /* adjust this bone's parent to newbone then */
2754 ebo->parent= newbone;
2760 /* step 2b: parent child of end to newbone (child from this chain) */
2762 endchild->parent= newbone;
2764 /* step 3: delete all bones between and including start and end */
2765 for (ebo= end; ebo; ebo= ebone) {
2766 ebone= (ebo == start) ? (NULL) : (ebo->parent);
2767 BLI_freelinkN(arm->edbo, ebo);
2771 /* bone merging - has a menu! */
2772 void merge_armature(Scene *scene)
2774 Object *obedit= scene->obedit; // XXX get from context
2775 bArmature *arm= obedit->data;
2778 /* process a menu to determine how to merge */
2779 // TODO: there's room for more modes of merging stuff...
2780 val= pupmenu("Merge Selected Bones%t|Within Chains%x1");
2781 if (val <= 0) return;
2784 /* go down chains, merging bones */
2785 ListBase chains = {NULL, NULL};
2786 LinkData *chain, *nchain;
2789 /* get chains (ends on chains) */
2790 chains_find_tips(arm->edbo, &chains);
2791 if (chains.first == NULL) return;
2793 /* each 'chain' is the last bone in the chain (with no children) */
2794 for (chain= chains.first; chain; chain= nchain) {
2795 EditBone *bstart= NULL, *bend= NULL;
2796 EditBone *bchild= NULL, *child=NULL;
2798 /* temporarily remove chain from list of chains */
2799 nchain= chain->next;
2800 BLI_remlink(&chains, chain);
2802 /* only consider bones that are visible and selected */
2803 for (ebo=chain->data; ebo; child=ebo, ebo=ebo->parent) {
2804 /* check if visible + selected */
2805 if ( EBONE_VISIBLE(arm, ebo) &&
2806 ((ebo->flag & BONE_CONNECTED) || (ebo->parent==NULL)) &&
2807 (ebo->flag & (BONE_SELECTED|BONE_ACTIVE)) )
2809 /* set either end or start (end gets priority, unless it is already set) */