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): Ton Roosendaal, Blender Foundation '05, full recode.
25 * ***** END GPL LICENSE BLOCK *****
26 * support for animation modes - Reevan McKay
33 #include "MEM_guardedalloc.h"
35 #include "BLI_arithb.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_dynstr.h"
39 #include "DNA_anim_types.h"
40 #include "DNA_action_types.h"
41 #include "DNA_armature_types.h"
42 #include "DNA_constraint_types.h"
43 #include "DNA_curve_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_meshdata_types.h"
46 #include "DNA_modifier_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_userdef_types.h"
53 #include "BKE_action.h"
54 #include "BKE_armature.h"
55 #include "BKE_blender.h"
56 #include "BKE_context.h"
57 #include "BKE_constraint.h"
58 #include "BKE_deform.h"
59 #include "BKE_depsgraph.h"
60 #include "BKE_displist.h"
61 #include "BKE_fcurve.h"
62 #include "BKE_global.h"
63 #include "BKE_modifier.h"
64 #include "BKE_object.h"
65 #include "BKE_utildefines.h"
67 #include "BIF_transform.h" /* for autokey TFM_TRANSLATION, etc */
70 #include "RNA_access.h"
71 #include "RNA_define.h"
76 #include "ED_armature.h"
77 #include "ED_anim_api.h"
78 #include "ED_keyframing.h"
79 #include "ED_object.h"
81 #include "ED_view3d.h"
83 #include "armature_intern.h"
85 /* ************* XXX *************** */
86 static int movetolayer_short_buts() {return 1;}
87 static int okee() {return 0;}
88 static int pupmenu() {return 0;}
89 static void waitcursor() {};
90 static void error() {};
91 static void BIF_undo_push() {}
92 static void countall() {}
93 static void add_constraint() {}
94 static void autokeyframe_pose_cb_func() {}
95 /* ************* XXX *************** */
97 /* This function is used to indicate that a bone is selected and needs keyframes inserted */
98 void set_pose_keys (Object *ob)
100 bArmature *arm= ob->data;
104 for (chan=ob->pose->chanbase.first; chan; chan=chan->next){
105 Bone *bone= chan->bone;
106 if ((bone) && (bone->flag & BONE_SELECTED) && (arm->layer & bone->layer))
107 chan->flag |= POSE_KEY;
109 chan->flag &= ~POSE_KEY;
114 /* This function is used to process the necessary updates for */
115 void ED_armature_enter_posemode(bContext *C, Base *base)
117 Object *ob= base->object;
120 error ("Can't pose libdata");
127 ob->flag |= OB_POSEMODE;
128 base->flag= ob->flag;
130 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_POSEMODE, NULL);
136 ED_view3d_exit_paint_modes(C);
139 void ED_armature_exit_posemode(bContext *C, Base *base)
142 Object *ob= base->object;
144 ob->flag &= ~OB_POSEMODE;
145 base->flag= ob->flag;
147 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, NULL);
151 /* if a selected or active bone is protected, throw error (oonly if warn==1) and return 1 */
152 /* only_selected==1 : the active bone is allowed to be protected */
153 static short pose_has_protected_selected(Object *ob, short only_selected, short warn)
155 /* check protection */
158 bArmature *arm= ob->data;
160 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
161 if (pchan->bone && (pchan->bone->layer & arm->layer)) {
162 if (pchan->bone->layer & arm->layer_protected) {
163 if (only_selected && (pchan->bone->flag & BONE_ACTIVE));
164 else if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED))
170 if (warn) error("Cannot change Proxy protected bones");
177 /* only for real IK, not for auto-IK */
178 int ED_pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
183 /* No need to check if constraint is active (has influence),
184 * since all constraints with CONSTRAINT_IK_AUTO are active */
185 for(con= pchan->constraints.first; con; con= con->next) {
186 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
187 bKinematicConstraint *data= con->data;
188 if((data->flag & CONSTRAINT_IK_AUTO)==0)
192 for(bone= pchan->bone->childbase.first; bone; bone= bone->next) {
193 pchan= get_pose_channel(ob->pose, bone->name);
194 if(pchan && ED_pose_channel_in_IK_chain(ob, pchan))
200 /* ********************************************** */
202 /* For the object with pose/action: create path curves for selected bones
203 * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
205 void pose_calculate_path(bContext *C, Scene *scene, Object *ob)
214 if (ob==NULL || ob->pose==NULL)
218 /* version patch for older files here (do_versions patch too complicated) */
219 if ((arm->pathsf == 0) || (arm->pathef == 0)) {
223 if (arm->pathsize == 0) {
227 /* set frame values */
232 error("Can't calculate paths when pathlen <= 0");
238 /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
239 if ((ob->recalc & OB_RECALC)==0) {
240 ob->recalc |= OB_RECALC;
241 ED_anim_object_flush_update(C, ob);
244 ED_anim_object_flush_update(C, ob);
247 /* malloc the path blocks */
248 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
249 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
250 if (arm->layer & pchan->bone->layer) {
251 pchan->pathlen= efra-sfra+1;
253 pchan->pathef= efra+1;
255 MEM_freeN(pchan->path);
256 pchan->path= MEM_callocN(3*pchan->pathlen*sizeof(float), "pchan path");
261 for (CFRA=sfra; CFRA<=efra; CFRA++) {
263 for (base= FIRSTBASE; base; base= base->next) {
264 if (base->object->recalc) {
265 int temp= base->object->recalc;
266 object_handle_update(scene, base->object);
267 base->object->recalc= temp;
271 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
272 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
273 if (arm->layer & pchan->bone->layer) {
275 fp= pchan->path+3*(CFRA-sfra);
277 if (arm->pathflag & ARM_PATH_HEADS) {
278 VECCOPY(fp, pchan->pose_head);
281 VECCOPY(fp, pchan->pose_tail);
284 Mat4MulVecfl(ob->obmat, fp);
296 /* For the object with pose/action: update paths for those that have got them
297 * This should selectively update paths that exist...
299 void pose_recalculate_paths(bContext *C, Scene *scene, Object *ob)
308 if (ob==NULL || ob->pose==NULL)
312 /* set frame values */
315 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
316 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
318 /* if the pathsf and pathef aren't initialised, abort! */
319 if (ELEM(0, pchan->pathsf, pchan->pathef))
322 /* try to increase area to do (only as much as needed) */
323 sfra= MIN2(sfra, pchan->pathsf);
324 efra= MAX2(efra, pchan->pathef);
328 if (efra <= sfra) return;
332 /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
333 if ((ob->recalc & OB_RECALC)==0) {
334 ob->recalc |= OB_RECALC;
335 ED_anim_object_flush_update(C, ob);
338 ED_anim_object_flush_update(C, ob);
340 for (CFRA=sfra; CFRA<=efra; CFRA++) {
342 for (base= FIRSTBASE; base; base= base->next) {
343 if (base->object->recalc) {
344 int temp= base->object->recalc;
345 object_handle_update(scene, base->object);
346 base->object->recalc= temp;
350 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
351 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
354 * - in range of this pchan's existing path
355 * - ... insert evil filtering/optimising conditions here...
357 if (IN_RANGE(CFRA, pchan->pathsf, pchan->pathef)) {
358 fp= pchan->path+3*(CFRA-sfra);
360 if (arm->pathflag & ARM_PATH_HEADS) {
361 VECCOPY(fp, pchan->pose_head);
364 VECCOPY(fp, pchan->pose_tail);
367 Mat4MulVecfl(ob->obmat, fp);
377 ob->pose->flag &= ~POSE_RECALCPATHS;
380 /* for the object with pose/action: clear path curves for selected bones only */
381 void pose_clear_paths(Object *ob)
385 if (ob==NULL || ob->pose==NULL)
388 /* free the path blocks */
389 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
390 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
392 MEM_freeN(pchan->path);
402 void pose_select_constraint_target(Scene *scene)
404 Object *obedit= scene->obedit; // XXX context
406 bArmature *arm= ob->data;
410 /* paranoia checks */
411 if (!ob && !ob->pose) return;
412 if (ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
414 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
415 if (arm->layer & pchan->bone->layer) {
416 if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
417 for (con= pchan->constraints.first; con; con= con->next) {
418 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
419 ListBase targets = {NULL, NULL};
420 bConstraintTarget *ct;
422 if (cti && cti->get_constraint_targets) {
423 cti->get_constraint_targets(con, &targets);
425 for (ct= targets.first; ct; ct= ct->next) {
426 if ((ct->tar == ob) && (ct->subtarget[0])) {
427 bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
429 pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
433 if (cti->flush_constraint_targets)
434 cti->flush_constraint_targets(con, &targets, 1);
441 BIF_undo_push("Select constraint target");
445 void pose_select_hierarchy(Scene *scene, short direction, short add_to_sel)
447 Object *obedit= scene->obedit; // XXX context
449 bArmature *arm= ob->data;
451 Bone *curbone, *pabone, *chbone;
453 /* paranoia checks */
454 if (!ob && !ob->pose) return;
455 if (ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
457 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
458 curbone= pchan->bone;
460 if (arm->layer & curbone->layer) {
461 if (curbone->flag & (BONE_ACTIVE)) {
462 if (direction == BONE_SELECT_PARENT) {
464 if (pchan->parent == NULL) continue;
465 else pabone= pchan->parent->bone;
467 if ((arm->layer & pabone->layer) && !(pabone->flag & BONE_HIDDEN_P)) {
469 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
470 curbone->flag &= ~BONE_ACTIVE;
471 pabone->flag |= (BONE_ACTIVE|BONE_SELECTED);
473 // XXX notifiers need to be sent to other editors to update
476 } else { // BONE_SELECT_CHILD
478 if (pchan->child == NULL) continue;
479 else chbone = pchan->child->bone;
481 if ((arm->layer & chbone->layer) && !(chbone->flag & BONE_HIDDEN_P)) {
483 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
484 curbone->flag &= ~BONE_ACTIVE;
485 chbone->flag |= (BONE_ACTIVE|BONE_SELECTED);
487 // XXX notifiers need to be sent to other editors to update
495 if (direction==BONE_SELECT_PARENT)
496 BIF_undo_push("Select pose bone parent");
497 if (direction==BONE_SELECT_CHILD)
498 BIF_undo_push("Select pose bone child");
502 void pose_add_IK(Scene *scene)
504 Object *obedit= scene->obedit; // XXX context
507 /* paranoia checks */
508 if(!ob && !ob->pose) return;
509 if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
511 add_constraint(1); /* 1 means only IK */
514 /* context: all selected channels */
515 void pose_clear_IK(Scene *scene)
517 Object *obedit= scene->obedit; // XXX context
519 bArmature *arm= ob->data;
524 /* paranoia checks */
525 if(!ob && !ob->pose) return;
526 if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
528 if(pose_has_protected_selected(ob, 0, 1))
531 if(okee("Remove IK constraint(s)")==0) return;
533 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
534 if(arm->layer & pchan->bone->layer) {
535 if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
537 for(con= pchan->constraints.first; con; con= next) {
539 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
540 BLI_remlink(&pchan->constraints, con);
541 free_constraint_data(con);
545 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
550 DAG_object_flush_update(scene, ob, OB_RECALC_DATA); // and all its relations
552 BIF_undo_push("Remove IK constraint(s)");
555 void pose_clear_constraints(Scene *scene)
557 Object *obedit= scene->obedit; // XXX context
559 bArmature *arm= ob->data;
562 /* paranoia checks */
563 if(!ob && !ob->pose) return;
564 if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
566 if(pose_has_protected_selected(ob, 0, 1))
569 if(okee("Remove Constraints")==0) return;
572 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
573 if(arm->layer & pchan->bone->layer) {
574 if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
575 free_constraints(&pchan->constraints);
581 DAG_object_flush_update(scene, ob, OB_RECALC_DATA); // and all its relations
583 BIF_undo_push("Remove Constraint(s)");
588 void pose_copy_menu(Scene *scene)
590 Object *obedit= scene->obedit; // XXX context
592 bArmature *arm= ob->data;
593 bPoseChannel *pchan, *pchanact;
597 /* paranoia checks */
598 if (ELEM(NULL, ob, ob->pose)) return;
599 if ((ob==obedit) || (ob->flag & OB_POSEMODE)==0) return;
602 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
603 if (pchan->bone->flag & BONE_ACTIVE)
607 if (pchan==NULL) return;
610 /* if proxy-protected bones selected, some things (such as locks + displays) shouldn't be changable,
611 * but for constraints (just add local constraints)
613 if (pose_has_protected_selected(ob, 1, 0)) {
614 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
616 nr= pupmenu("Copy Pose Attributes %t|Local Location%x1|Local Rotation%x2|Local Size%x3|%l|Visual Location %x9|Visual Rotation%x10|Visual Size%x11|%l|Constraints (All)%x4|Constraints...%x5");
618 nr= pupmenu("Copy Pose Attributes %t|Local Location%x1|Local Rotation%x2|Local Size%x3|%l|Visual Location %x9|Visual Rotation%x10|Visual Size%x11|%l|Constraints (All)%x4");
621 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
623 nr= pupmenu("Copy Pose Attributes %t|Local Location%x1|Local Rotation%x2|Local Size%x3|%l|Visual Location %x9|Visual Rotation%x10|Visual Size%x11|%l|Constraints (All)%x4|Constraints...%x5|%l|Transform Locks%x6|IK Limits%x7|Bone Shape%x8");
625 nr= pupmenu("Copy Pose Attributes %t|Local Location%x1|Local Rotation%x2|Local Size%x3|%l|Visual Location %x9|Visual Rotation%x10|Visual Size%x11|%l|Constraints (All)%x4|%l|Transform Locks%x6|IK Limits%x7|Bone Shape%x8");
632 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
633 if ( (arm->layer & pchan->bone->layer) &&
634 (pchan->bone->flag & BONE_SELECTED) &&
635 (pchan != pchanact) )
638 case 1: /* Local Location */
639 VECCOPY(pchan->loc, pchanact->loc);
641 case 2: /* Local Rotation */
642 QUATCOPY(pchan->quat, pchanact->quat);
644 case 3: /* Local Size */
645 VECCOPY(pchan->size, pchanact->size);
647 case 4: /* All Constraints */
649 ListBase tmp_constraints = {NULL, NULL};
651 /* copy constraints to tmpbase and apply 'local' tags before
652 * appending to list of constraints for this channel
654 copy_constraints(&tmp_constraints, &pchanact->constraints);
655 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
658 /* add proxy-local tags */
659 for (con= tmp_constraints.first; con; con= con->next)
660 con->flag |= CONSTRAINT_PROXY_LOCAL;
662 addlisttolist(&pchan->constraints, &tmp_constraints);
664 /* update flags (need to add here, not just copy) */
665 pchan->constflag |= pchanact->constflag;
668 ob->pose->flag |= POSE_RECALC;
671 case 6: /* Transform Locks */
672 pchan->protectflag = pchanact->protectflag;
674 case 7: /* IK (DOF) settings */
676 pchan->ikflag = pchanact->ikflag;
677 VECCOPY(pchan->limitmin, pchanact->limitmin);
678 VECCOPY(pchan->limitmax, pchanact->limitmax);
679 VECCOPY(pchan->stiffness, pchanact->stiffness);
680 pchan->ikstretch= pchanact->ikstretch;
683 case 8: /* Custom Bone Shape */
684 pchan->custom = pchanact->custom;
686 case 9: /* Visual Location */
687 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
689 case 10: /* Visual Rotation */
691 float delta_mat[4][4], quat[4];
693 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
694 Mat4ToQuat(delta_mat, quat);
695 QUATCOPY(pchan->quat, quat);
698 case 11: /* Visual Size */
700 float delta_mat[4][4], size[4];
702 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
703 Mat4ToSize(delta_mat, size);
704 VECCOPY(pchan->size, size);
710 else { /* constraints, optional (note: max we can have is 24 constraints) */
711 bConstraint *con, *con_back;
712 int const_toggle[24];
713 ListBase const_copy = {NULL, NULL};
715 BLI_duplicatelist(&const_copy, &(pchanact->constraints));
717 /* build the puplist of constraints */
718 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
720 // add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
723 // if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
724 // BLI_freelistN(&const_copy);
728 /* now build a new listbase from the options selected */
729 for (i=0, con=const_copy.first; con; i++) {
730 /* if not selected, free/remove it from the list */
731 if (!const_toggle[i]) {
733 BLI_freelinkN(&const_copy, con);
740 /* Copy the temo listbase to the selected posebones */
741 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
742 if ( (arm->layer & pchan->bone->layer) &&
743 (pchan->bone->flag & BONE_SELECTED) &&
746 ListBase tmp_constraints = {NULL, NULL};
748 /* copy constraints to tmpbase and apply 'local' tags before
749 * appending to list of constraints for this channel
751 copy_constraints(&tmp_constraints, &const_copy);
752 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
755 /* add proxy-local tags */
756 for (con= tmp_constraints.first; con; con= con->next)
757 con->flag |= CONSTRAINT_PROXY_LOCAL;
759 addlisttolist(&pchan->constraints, &tmp_constraints);
761 /* update flags (need to add here, not just copy) */
762 pchan->constflag |= pchanact->constflag;
765 BLI_freelistN(&const_copy);
766 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
769 ob->pose->flag |= POSE_RECALC;
772 DAG_object_flush_update(scene, ob, OB_RECALC_DATA); // and all its relations
774 BIF_undo_push("Copy Pose Attributes");
778 /* ******************** copy/paste pose ********************** */
780 static bPose *g_posebuf=NULL;
782 void free_posebuf(void)
785 // was copied without constraints
786 BLI_freelistN (&g_posebuf->chanbase);
787 MEM_freeN (g_posebuf);
792 void copy_posebuf (Scene *scene)
796 if (!ob || !ob->pose){
803 set_pose_keys(ob); // sets chan->flag to POSE_KEY if bone selected
804 copy_pose(&g_posebuf, ob->pose, 0);
808 void paste_posebuf (Scene *scene, int flip)
811 bPoseChannel *chan, *pchan;
815 if (!ob || !ob->pose)
819 error ("Copy buffer is empty");
824 // disabled until protected bones in proxies follow the rules everywhere else!
825 if(pose_has_protected_selected(ob, 1, 1))
829 /* Safely merge all of the channels in this pose into
831 for (chan=g_posebuf->chanbase.first; chan; chan=chan->next) {
832 if (chan->flag & POSE_KEY) {
833 BLI_strncpy(name, chan->name, sizeof(name));
835 bone_flip_name (name, 0); // 0 = don't strip off number extensions
837 /* only copy when channel exists, poses are not meant to add random channels to anymore */
838 pchan= get_pose_channel(ob->pose, name);
841 /* only loc rot size */
842 /* only copies transform info for the pose */
843 VECCOPY(pchan->loc, chan->loc);
844 VECCOPY(pchan->size, chan->size);
845 QUATCOPY(pchan->quat, chan->quat);
846 pchan->flag= chan->flag;
851 QuatToEul(pchan->quat, eul);
854 EulToQuat(eul, pchan->quat);
857 #if 0 // XXX old animation system
858 if (autokeyframe_cfra_can_key(ob)) {
861 /* Set keys on pose */
862 if (chan->flag & POSE_ROT) {
863 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
864 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
865 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
866 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
868 if (chan->flag & POSE_SIZE) {
869 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
870 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
871 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
873 if (chan->flag & POSE_LOC) {
874 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
875 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
876 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
879 /* clear any unkeyed tags */
881 chan->bone->flag &= ~BONE_UNKEYED;
884 /* add unkeyed tags */
886 chan->bone->flag |= BONE_UNKEYED;
888 #endif // XXX old animation system
893 /* Update event for pose and deformation children */
894 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
897 // XXX remake_action_ipos(ob->action);
900 /* need to trick depgraph, action is not allowed to execute on pose */
901 where_is_pose(scene, ob);
905 BIF_undo_push("Paste Action Pose");
908 /* ********************************************** */
910 /* context weightpaint and deformer in posemode */
911 void pose_adds_vgroups(Scene *scene, Object *meshobj, int heatweights)
913 // XXX extern VPaint Gwp; /* from vpaint */
914 Object *poseobj= modifiers_isDeformedByArmature(meshobj);
916 if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) {
917 error("The active object must have a deforming armature in pose mode");
921 // XXX add_verts_to_dgroups(meshobj, poseobj, heatweights, (Gwp.flag & VP_MIRROR_X));
924 BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
926 BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
929 // and all its relations
930 DAG_object_flush_update(scene, meshobj, OB_RECALC_DATA);
933 /* ********************************************** */
935 /* adds a new pose-group */
936 void pose_add_posegroup (Scene *scene)
939 bPose *pose= (ob) ? ob->pose : NULL;
942 if (ELEM(NULL, ob, ob->pose))
945 grp= MEM_callocN(sizeof(bActionGroup), "PoseGroup");
946 strcpy(grp->name, "Group");
947 BLI_addtail(&pose->agroups, grp);
948 BLI_uniquename(&pose->agroups, grp, "Group", offsetof(bActionGroup, name), 32);
950 pose->active_group= BLI_countlist(&pose->agroups);
952 BIF_undo_push("Add Bone Group");
956 /* Remove the active bone-group */
957 void pose_remove_posegroup (Scene *scene)
960 bPose *pose= (ob) ? ob->pose : NULL;
961 bActionGroup *grp = NULL;
965 if (ELEM(NULL, ob, pose))
967 if (pose->active_group <= 0)
970 /* get group to remove */
971 grp= BLI_findlink(&pose->agroups, pose->active_group-1);
973 /* adjust group references (the trouble of using indices!):
974 * - firstly, make sure nothing references it
975 * - also, make sure that those after this item get corrected
977 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
978 if (pchan->agrp_index == pose->active_group)
979 pchan->agrp_index= 0;
980 else if (pchan->agrp_index > pose->active_group)
984 /* now, remove it from the pose */
985 BLI_freelinkN(&pose->agroups, grp);
986 pose->active_group= 0;
988 BIF_undo_push("Remove Bone Group");
993 char *build_posegroups_menustr (bPose *pose, short for_pupmenu)
995 DynStr *pupds= BLI_dynstr_new();
1001 /* add title first (and the "none" entry) */
1002 BLI_dynstr_append(pupds, "Bone Group%t|");
1004 BLI_dynstr_append(pupds, "Add New%x0|");
1006 BLI_dynstr_append(pupds, "BG: [None]%x0|");
1008 /* loop through groups, adding them */
1009 for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++) {
1010 if (for_pupmenu == 0)
1011 BLI_dynstr_append(pupds, "BG: ");
1012 BLI_dynstr_append(pupds, grp->name);
1014 sprintf(buf, "%%x%d", i);
1015 BLI_dynstr_append(pupds, buf);
1018 BLI_dynstr_append(pupds, "|");
1021 /* convert to normal MEM_malloc'd string */
1022 str= BLI_dynstr_get_cstring(pupds);
1023 BLI_dynstr_free(pupds);
1028 /* Assign selected pchans to the bone group that the user selects */
1029 void pose_assign_to_posegroup (Scene *scene, short active)
1032 bArmature *arm= (ob) ? ob->data : NULL;
1033 bPose *pose= (ob) ? ob->pose : NULL;
1034 bPoseChannel *pchan;
1040 if (ELEM3(NULL, ob, pose, arm))
1043 /* get group to affect */
1044 if ((active==0) || (pose->active_group <= 0)) {
1045 menustr= build_posegroups_menustr(pose, 1);
1046 nr= 0; // XXX pupmenu_col(menustr, 20);
1052 /* add new - note: this does an undo push and sets active group */
1053 pose_add_posegroup(scene);
1056 pose->active_group= nr;
1059 /* add selected bones to group then */
1060 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1061 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1062 pchan->agrp_index= pose->active_group;
1068 BIF_undo_push("Add Bones To Group");
1072 /* Remove selected pchans from their bone groups */
1073 void pose_remove_from_posegroups (Scene *scene)
1076 bArmature *arm= (ob) ? ob->data : NULL;
1077 bPose *pose= (ob) ? ob->pose : NULL;
1078 bPoseChannel *pchan;
1082 if (ELEM3(NULL, ob, pose, arm))
1085 /* remove selected bones from their groups */
1086 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1087 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1088 if (pchan->agrp_index) {
1089 pchan->agrp_index= 0;
1096 BIF_undo_push("Remove Bones From Groups");
1100 /* Ctrl-G in 3D-View while in PoseMode */
1101 void pgroup_operation_with_menu (Scene *scene)
1104 bArmature *arm= (ob) ? ob->data : NULL;
1105 bPose *pose= (ob) ? ob->pose : NULL;
1106 bPoseChannel *pchan= NULL;
1110 if (ELEM3(NULL, ob, pose, arm))
1113 /* check that something is selected */
1114 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1115 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer))
1121 /* get mode of action */
1123 mode= pupmenu("Bone Groups%t|Add Selected to Active Group%x1|Add Selected to Group%x2|%|Remove Selected From Groups%x3|Remove Active Group%x4");
1125 mode= pupmenu("Bone Groups%t|Add New Group%x5|Remove Active Group%x4");
1130 pose_assign_to_posegroup(scene, 1);
1133 pose_assign_to_posegroup(scene, 0);
1136 pose_add_posegroup(scene);
1139 pose_remove_from_posegroups(scene);
1142 pose_remove_posegroup(scene);
1147 /* ********************************************** */
1149 static short pose_select_same_group (Object *ob)
1151 bPose *pose= (ob)? ob->pose : NULL;
1152 bArmature *arm= (ob)? ob->data : NULL;
1153 bPoseChannel *pchan, *chan;
1156 if (ELEM3(NULL, ob, pose, arm))
1159 /* loop in loop... bad and slow! */
1160 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1161 if (arm->layer & pchan->bone->layer) {
1162 if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1164 /* only if group matches (and is not selected or current bone) */
1165 for (chan= ob->pose->chanbase.first; chan; chan= chan->next) {
1166 if (arm->layer & chan->bone->layer) {
1167 if (pchan->agrp_index == chan->agrp_index) {
1168 chan->bone->flag |= BONE_SELECTED;
1181 static short pose_select_same_layer (Object *ob)
1183 bPose *pose= (ob)? ob->pose : NULL;
1184 bArmature *arm= (ob)? ob->data : NULL;
1185 bPoseChannel *pchan;
1186 short layers= 0, changed= 0;
1188 if (ELEM3(NULL, ob, pose, arm))
1191 /* figure out what bones are selected */
1192 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1193 if (arm->layer & pchan->bone->layer) {
1194 if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1195 layers |= pchan->bone->layer;
1202 /* select bones that are on same layers as layers flag */
1203 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1204 if (arm->layer & pchan->bone->layer) {
1205 if (layers & pchan->bone->layer) {
1206 pchan->bone->flag |= BONE_SELECTED;
1216 void pose_select_grouped (Scene *scene, short nr)
1220 if (nr == 1) changed= pose_select_same_group(OBACT);
1221 else if (nr == 2) changed= pose_select_same_layer(OBACT);
1225 BIF_undo_push("Select Grouped");
1229 /* Shift-G in 3D-View while in PoseMode */
1230 void pose_select_grouped_menu (Scene *scene)
1235 nr= pupmenu("Select Grouped%t|In Same Group%x1|In Same Layer%x2");
1236 pose_select_grouped(scene, nr);
1239 /* ********************************************** */
1241 /* context active object */
1242 void pose_flip_names(Scene *scene)
1244 Object *obedit= scene->obedit; // XXX context
1246 bArmature *arm= ob->data;
1247 bPoseChannel *pchan;
1250 /* paranoia checks */
1251 if(!ob && !ob->pose) return;
1252 if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
1254 if(pose_has_protected_selected(ob, 0, 1))
1257 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1258 if(arm->layer & pchan->bone->layer) {
1259 if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1260 BLI_strncpy(newname, pchan->name, sizeof(newname));
1261 bone_flip_name(newname, 1); // 1 = do strip off number extensions
1262 armature_bone_rename(ob, pchan->name, newname);
1267 BIF_undo_push("Flip names");
1270 /* context active object */
1271 void pose_autoside_names(Scene *scene, short axis)
1273 Object *obedit= scene->obedit; // XXX context
1275 bArmature *arm= ob->data;
1276 bPoseChannel *pchan;
1279 /* paranoia checks */
1280 if (ELEM(NULL, ob, ob->pose)) return;
1281 if (ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
1283 if (pose_has_protected_selected(ob, 0, 1))
1286 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1287 if(arm->layer & pchan->bone->layer) {
1288 if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1289 BLI_strncpy(newname, pchan->name, sizeof(newname));
1290 bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
1291 armature_bone_rename(ob, pchan->name, newname);
1296 BIF_undo_push("Flip names");
1299 /* context active object, or weightpainted object with armature in posemode */
1300 void pose_activate_flipped_bone(Scene *scene)
1303 bArmature *arm= ob->data;
1305 if(ob==NULL) return;
1307 if(G.f & G_WEIGHTPAINT) {
1308 ob= modifiers_isDeformedByArmature(ob);
1310 if(ob && (ob->flag & OB_POSEMODE)) {
1311 bPoseChannel *pchan, *pchanf;
1313 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1314 if(arm->layer & pchan->bone->layer) {
1315 if(pchan->bone->flag & BONE_ACTIVE)
1322 BLI_strncpy(name, pchan->name, 32);
1323 bone_flip_name(name, 1); // 0 = do not strip off number extensions
1325 pchanf= get_pose_channel(ob->pose, name);
1326 if(pchanf && pchanf!=pchan) {
1327 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1328 pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
1330 /* in weightpaint we select the associated vertex group too */
1331 if(G.f & G_WEIGHTPAINT) {
1332 vertexgroup_select_by_name(OBACT, name);
1333 DAG_object_flush_update(scene, OBACT, OB_RECALC_DATA);
1336 // XXX notifiers need to be sent to other editors to update
1343 /* This function pops up the move-to-layer popup widgets when the user
1344 * presses either SHIFT-MKEY or MKEY in PoseMode OR EditMode (for Armatures)
1346 void pose_movetolayer(Scene *scene)
1348 Object *obedit= scene->obedit; // XXX context
1352 short shift= 0; // XXX
1354 if (ob==NULL) return;
1358 /* armature layers */
1360 if ( movetolayer_short_buts(&lay, "Armature Layers")==0 ) return;
1364 ob->pose->proxy_layer= lay;
1368 /* the check for editbone layer moving needs to occur before posemode one to work */
1372 for (ebo= arm->edbo->first; ebo; ebo= ebo->next) {
1373 if (arm->layer & ebo->layer) {
1374 if (ebo->flag & BONE_SELECTED)
1380 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1383 for (ebo= arm->edbo->first; ebo; ebo= ebo->next) {
1384 if (arm->layer & ebo->layer) {
1385 if (ebo->flag & BONE_SELECTED) {
1387 if (arm->flag & ARM_MIRROR_EDIT) {
1388 flipBone = armature_bone_get_mirrored(arm->edbo, ebo);
1390 flipBone->layer = lay;
1396 BIF_undo_push("Move Bone Layer");
1398 else if (ob->flag & OB_POSEMODE) {
1399 /* pose-channel layers */
1400 bPoseChannel *pchan;
1402 if (pose_has_protected_selected(ob, 0, 1))
1405 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1406 if (arm->layer & pchan->bone->layer) {
1407 if (pchan->bone->flag & BONE_SELECTED)
1408 lay |= pchan->bone->layer;
1413 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1416 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1417 if (arm->layer & pchan->bone->layer) {
1418 if (pchan->bone->flag & BONE_SELECTED)
1419 pchan->bone->layer= lay;
1423 BIF_undo_push("Move Bone Layer");
1429 /* for use with pose_relax only */
1430 static int pose_relax_icu(struct IpoCurve *icu, float framef, float *val, float *frame_prev, float *frame_next)
1436 BezTriple *bezt = icu->bezt;
1438 BezTriple *bezt_prev=NULL, *bezt_next=NULL;
1442 for (i=0; i < icu->totvert; i++, bezt++) {
1443 if (bezt->vec[1][0] < framef - 0.5) {
1450 if (bezt_prev==NULL) return 0;
1452 /* advance to the next, dont need to advance i */
1455 for (; i < icu->totvert; i++, bezt++) {
1456 if (bezt->vec[1][0] > framef + 0.5) {
1462 if (bezt_next==NULL) return 0;
1465 w1 = framef - bezt_prev->vec[1][0];
1466 w2 = bezt_next->vec[1][0] - framef;
1471 val = (bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1);
1473 /* apply the value with a hard coded 6th */
1474 *val = (((bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1)) + (*val * 5.0f)) / 6.0f;
1478 if (frame_prev) *frame_prev = bezt_prev->vec[1][0];
1479 if (frame_next) *frame_next = bezt_next->vec[1][0];
1486 void pose_relax(Scene *scene)
1493 // IpoCurve *icu_w, *icu_x, *icu_y, *icu_z;
1495 bPoseChannel *pchan;
1496 // bActionChannel *achan;
1497 // float framef = F_CFRA;
1498 // float frame_prev, frame_next;
1499 // float quat_prev[4], quat_next[4], quat_interp[4], quat_orig[4];
1505 // int do_x, do_y, do_z;
1511 arm = (bArmature *)ob->data;
1513 if (!pose || !act || !arm) return;
1515 for (pchan=pose->chanbase.first; pchan; pchan= pchan->next) {
1517 pchan->bone->flag &= ~BONE_TRANSFORM;
1519 if (pchan->bone->layer & arm->layer) {
1520 if (pchan->bone->flag & BONE_SELECTED) {
1521 /* do we have an ipo curve? */
1522 #if 0 // XXX old animation system
1523 achan= get_action_channel(act, pchan->name);
1525 if (achan && achan->ipo) {
1526 /*calc_ipo(achan->ipo, ctime);*/
1528 do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_X), framef, &pchan->loc[0], NULL, NULL);
1529 do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Y), framef, &pchan->loc[1], NULL, NULL);
1530 do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Z), framef, &pchan->loc[2], NULL, NULL);
1531 do_loc += do_x + do_y + do_z;
1533 do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_X), framef, &pchan->size[0], NULL, NULL);
1534 do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Y), framef, &pchan->size[1], NULL, NULL);
1535 do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Z), framef, &pchan->size[2], NULL, NULL);
1536 do_scale += do_x + do_y + do_z;
1538 if( ((icu_w = find_ipocurve(achan->ipo, AC_QUAT_W))) &&
1539 ((icu_x = find_ipocurve(achan->ipo, AC_QUAT_X))) &&
1540 ((icu_y = find_ipocurve(achan->ipo, AC_QUAT_Y))) &&
1541 ((icu_z = find_ipocurve(achan->ipo, AC_QUAT_Z))) )
1543 /* use the quatw keyframe as a basis for others */
1544 if (pose_relax_icu(icu_w, framef, NULL, &frame_prev, &frame_next)) {
1546 quat_prev[0] = eval_icu(icu_w, frame_prev);
1547 quat_prev[1] = eval_icu(icu_x, frame_prev);
1548 quat_prev[2] = eval_icu(icu_y, frame_prev);
1549 quat_prev[3] = eval_icu(icu_z, frame_prev);
1551 quat_next[0] = eval_icu(icu_w, frame_next);
1552 quat_next[1] = eval_icu(icu_x, frame_next);
1553 quat_next[2] = eval_icu(icu_y, frame_next);
1554 quat_next[3] = eval_icu(icu_z, frame_next);
1557 /* apply the setting, completely smooth */
1558 QuatInterpol(pchan->quat, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1560 /* tricky interpolation */
1561 QuatInterpol(quat_interp, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1562 QUATCOPY(quat_orig, pchan->quat);
1563 QuatInterpol(pchan->quat, quat_orig, quat_interp, 1.0f/6.0f);
1570 /* apply BONE_TRANSFORM tag so that autokeying will pick it up */
1571 pchan->bone->flag |= BONE_TRANSFORM;
1574 #endif // XXX old animation system
1579 ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1581 /* do auto-keying */
1582 if (do_loc) flag |= TFM_TRANSLATION;
1583 if (do_scale) flag |= TFM_RESIZE;
1584 if (do_quat) flag |= TFM_ROTATION;
1585 autokeyframe_pose_cb_func(ob, flag, 0);
1587 /* clear BONE_TRANSFORM flags */
1588 for (pchan=pose->chanbase.first; pchan; pchan= pchan->next)
1589 pchan->bone->flag &= ~ BONE_TRANSFORM;
1591 /* do depsgraph flush */
1592 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1593 BIF_undo_push("Relax Pose");
1596 /* for use in insertkey, ensure rotation goes other way around */
1597 void pose_flipquats(Scene *scene)
1600 bArmature *arm= ob->data;
1601 bPoseChannel *pchan;
1606 /* find sel bones */
1607 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1608 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1609 /* quaternions have 720 degree range */
1610 pchan->quat[0]= -pchan->quat[0];
1611 pchan->quat[1]= -pchan->quat[1];
1612 pchan->quat[2]= -pchan->quat[2];
1613 pchan->quat[3]= -pchan->quat[3];
1618 autokeyframe_pose_cb_func(ob, TFM_ROTATION, 0);
1621 /* context: active channel */
1622 void pose_special_editmenu(Scene *scene)
1625 Object *obedit= scene->obedit; // XXX context
1629 /* paranoia checks */
1630 if(!ob && !ob->pose) return;
1631 if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
1633 nr= pupmenu("Specials%t|Select Constraint Target%x1|Flip Left-Right Names%x2|Calculate Paths%x3|Clear Paths%x4|Clear User Transform %x5|Relax Pose %x6|%l|AutoName Left-Right%x7|AutoName Front-Back%x8|AutoName Top-Bottom%x9");
1635 pose_select_constraint_target(scene);
1641 pose_calculate_path(C, ob);
1644 pose_clear_paths(ob);
1647 rest_pose(ob->pose);
1648 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1649 BIF_undo_push("Clear User Transform Pose");
1654 else if(ELEM3(nr, 7, 8, 9)) {
1655 pose_autoside_names(nr-7);