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.
26 * ***** END GPL LICENSE BLOCK *****
27 * support for animation modes - Reevan McKay
34 #include "MEM_guardedalloc.h"
36 #include "BLI_arithb.h"
37 #include "BLI_blenlib.h"
38 #include "BLI_dynstr.h"
40 #include "DNA_anim_types.h"
41 #include "DNA_action_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_constraint_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_meshdata_types.h"
47 #include "DNA_modifier_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_view3d_types.h"
52 #include "DNA_userdef_types.h"
54 #include "BKE_animsys.h"
55 #include "BKE_action.h"
56 #include "BKE_armature.h"
57 #include "BKE_blender.h"
58 #include "BKE_context.h"
59 #include "BKE_constraint.h"
60 #include "BKE_deform.h"
61 #include "BKE_depsgraph.h"
62 #include "BKE_displist.h"
63 #include "BKE_fcurve.h"
64 #include "BKE_global.h"
65 #include "BKE_modifier.h"
66 #include "BKE_object.h"
67 #include "BKE_utildefines.h"
68 #include "BKE_report.h"
72 #include "RNA_access.h"
73 #include "RNA_define.h"
78 #include "ED_armature.h"
79 #include "ED_anim_api.h"
80 #include "ED_keyframing.h"
81 #include "ED_object.h"
83 #include "ED_screen.h"
84 #include "ED_transform.h" /* for autokey TFM_TRANSLATION, etc */
85 #include "ED_view3d.h"
87 #include "UI_interface.h"
89 #include "armature_intern.h"
91 /* ************* XXX *************** */
92 static int pupmenu() {return 0;}
93 static void error() {};
94 static void BIF_undo_push() {}
95 static void countall() {}
96 static void autokeyframe_pose_cb_func() {}
97 /* ************* XXX *************** */
99 /* This function is used to indicate that a bone is selected and needs keyframes inserted */
100 void set_pose_keys (Object *ob)
102 bArmature *arm= ob->data;
106 for (chan=ob->pose->chanbase.first; chan; chan=chan->next){
107 Bone *bone= chan->bone;
108 if ((bone) && (bone->flag & BONE_SELECTED) && (arm->layer & bone->layer))
109 chan->flag |= POSE_KEY;
111 chan->flag &= ~POSE_KEY;
116 /* This function is used to process the necessary updates for */
117 void ED_armature_enter_posemode(bContext *C, Base *base)
119 Object *ob= base->object;
122 error ("Can't pose libdata");
129 ob->flag |= OB_POSEMODE;
130 base->flag= ob->flag;
132 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_POSE, NULL);
138 ED_view3d_exit_paint_modes(C);
139 ED_object_toggle_modes(C, ob->mode);
142 void ED_armature_exit_posemode(bContext *C, Base *base)
145 Object *ob= base->object;
147 ob->flag &= ~OB_POSEMODE;
148 base->flag= ob->flag;
150 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, NULL);
154 /* if a selected or active bone is protected, throw error (oonly if warn==1) and return 1 */
155 /* only_selected==1 : the active bone is allowed to be protected */
156 static short pose_has_protected_selected(Object *ob, short only_selected, short warn)
158 /* check protection */
161 bArmature *arm= ob->data;
163 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
164 if (pchan->bone && (pchan->bone->layer & arm->layer)) {
165 if (pchan->bone->layer & arm->layer_protected) {
166 if (only_selected && (pchan->bone->flag & BONE_ACTIVE));
167 else if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED))
173 if (warn) error("Cannot change Proxy protected bones");
180 /* only for real IK, not for auto-IK */
181 int ED_pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
186 /* No need to check if constraint is active (has influence),
187 * since all constraints with CONSTRAINT_IK_AUTO are active */
188 for(con= pchan->constraints.first; con; con= con->next) {
189 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
190 bKinematicConstraint *data= con->data;
191 if((data->flag & CONSTRAINT_IK_AUTO)==0)
195 for(bone= pchan->bone->childbase.first; bone; bone= bone->next) {
196 pchan= get_pose_channel(ob->pose, bone->name);
197 if(pchan && ED_pose_channel_in_IK_chain(ob, pchan))
203 /* ********************************************** */
205 /* For the object with pose/action: update paths for those that have got them
206 * This should selectively update paths that exist...
208 * To be called from various tools that do incremental updates
210 void ED_pose_recalculate_paths(bContext *C, Scene *scene, Object *ob)
220 if ELEM(NULL, ob, ob->pose)
224 /* set frame values */
227 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
228 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
230 /* if the pathsf and pathef aren't initialised, abort! */
231 if (ELEM(0, pchan->pathsf, pchan->pathef))
234 /* try to increase area to do (only as much as needed) */
235 sfra= MIN2(sfra, pchan->pathsf);
236 efra= MAX2(efra, pchan->pathef);
240 if (efra <= sfra) return;
242 /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
243 if ((ob->recalc & OB_RECALC)==0) {
244 ob->recalc |= OB_RECALC;
245 ED_anim_object_flush_update(C, ob);
248 ED_anim_object_flush_update(C, ob);
250 /* calculate path over requested range */
251 for (CFRA=sfra; CFRA<=efra; CFRA++) {
253 for (base= FIRSTBASE; base; base= base->next) {
254 if (base->object->recalc) {
255 int temp= base->object->recalc;
257 if (base->object->adt)
258 BKE_animsys_evaluate_animdata(&base->object->id, base->object->adt, (float)CFRA, ADT_RECALC_ALL);
261 object_handle_update(scene, base->object);
262 base->object->recalc= temp;
266 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
267 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
270 * - in range of this pchan's existing path
271 * - ... insert evil filtering/optimising conditions here...
273 if (IN_RANGE(CFRA, pchan->pathsf, pchan->pathef)) {
274 fp= pchan->path+3*(CFRA-sfra);
276 if (arm->pathflag & ARM_PATH_HEADS) {
277 VECCOPY(fp, pchan->pose_head);
280 VECCOPY(fp, pchan->pose_tail);
283 Mat4MulVecfl(ob->obmat, fp);
292 ob->pose->flag &= ~POSE_RECALCPATHS;
294 /* flush one final time - to restore to the original state */
295 for (base= FIRSTBASE; base; base= base->next) {
296 if (base->object->recalc) {
297 int temp= base->object->recalc;
299 if (base->object->adt)
300 BKE_animsys_evaluate_animdata(&base->object->id, base->object->adt, (float)CFRA, ADT_RECALC_ALL);
302 object_handle_update(scene, base->object);
303 base->object->recalc= temp;
310 /* For the object with pose/action: create path curves for selected bones
311 * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
313 static int pose_calculate_paths_exec (bContext *C, wmOperator *op)
315 wmWindow *win= CTX_wm_window(C);
316 ScrArea *sa= CTX_wm_area(C);
317 Scene *scene= CTX_data_scene(C);
326 /* since this call may also be used from the buttons window, we need to check for where to get the object */
327 if (sa->spacetype == SPACE_BUTS)
328 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
330 ob= CTX_data_active_object(C);
332 /* only continue if there's an object */
333 if ELEM(NULL, ob, ob->pose)
334 return OPERATOR_CANCELLED;
337 /* version patch for older files here (do_versions patch too complicated) */
338 if ((arm->pathsf == 0) || (arm->pathef == 0)) {
342 if (arm->pathsize == 0) {
346 /* get frame values to use */
352 BKE_report(op->reports, RPT_ERROR, "Can't calculate paths when pathlen <= 0");
353 return OPERATOR_CANCELLED;
356 /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
357 if ((ob->recalc & OB_RECALC)==0) {
358 ob->recalc |= OB_RECALC;
359 ED_anim_object_flush_update(C, ob);
362 ED_anim_object_flush_update(C, ob);
364 /* alloc the path cache arrays */
365 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
366 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
367 if (arm->layer & pchan->bone->layer) {
368 pchan->pathlen= efra-sfra+1;
370 pchan->pathef= efra+1;
372 MEM_freeN(pchan->path);
373 pchan->path= MEM_callocN(3*pchan->pathlen*sizeof(float), "pchan path");
378 /* step through frame range sampling the values */
379 for (CFRA=sfra; CFRA<=efra; CFRA++) {
380 /* for each frame we calculate, update time-cursor... (may be too slow) */
381 WM_timecursor(win, CFRA);
384 for (base= FIRSTBASE; base; base= base->next) {
385 if (base->object->recalc) {
386 int temp= base->object->recalc;
388 if (base->object->adt)
389 BKE_animsys_evaluate_animdata(&base->object->id, base->object->adt, (float)CFRA, ADT_RECALC_ALL);
391 object_handle_update(scene, base->object);
392 base->object->recalc= temp;
396 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
397 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
398 if (arm->layer & pchan->bone->layer) {
400 fp= pchan->path+3*(CFRA-sfra);
402 if (arm->pathflag & ARM_PATH_HEADS) {
403 VECCOPY(fp, pchan->pose_head);
406 VECCOPY(fp, pchan->pose_tail);
409 Mat4MulVecfl(ob->obmat, fp);
416 /* restore original cursor */
417 WM_cursor_restore(win);
419 /* reset current frame, and clear flags */
421 ob->pose->flag &= ~POSE_RECALCPATHS;
423 /* flush one final time - to restore to the original state */
424 for (base= FIRSTBASE; base; base= base->next) {
425 if (base->object->recalc) {
426 int temp= base->object->recalc;
428 if (base->object->adt)
429 BKE_animsys_evaluate_animdata(&base->object->id, base->object->adt, (float)CFRA, ADT_RECALC_ALL);
431 object_handle_update(scene, base->object);
432 base->object->recalc= temp;
436 /* notifiers for updates */
437 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
439 return OPERATOR_FINISHED;
442 void POSE_OT_paths_calculate (wmOperatorType *ot)
445 ot->name= "Calculate Bone Paths";
446 ot->idname= "POSE_OT_paths_calculate";
447 ot->description= "Calculate paths for the selected bones.";
450 ot->exec= pose_calculate_paths_exec;
451 ot->poll= ED_operator_posemode;
454 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
459 /* for the object with pose/action: clear path curves for selected bones only */
460 void ED_pose_clear_paths(Object *ob)
464 if ELEM(NULL, ob, ob->pose)
467 /* free the path blocks */
468 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
469 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
471 MEM_freeN(pchan->path);
478 /* operator callback for this */
479 static int pose_clear_paths_exec (bContext *C, wmOperator *op)
481 ScrArea *sa= CTX_wm_area(C);
484 /* since this call may also be used from the buttons window, we need to check for where to get the object */
485 if (sa->spacetype == SPACE_BUTS)
486 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
488 ob= CTX_data_active_object(C);
490 /* only continue if there's an object */
491 if ELEM(NULL, ob, ob->pose)
492 return OPERATOR_CANCELLED;
494 /* for now, just call the API function for this (which is shared with backend functions) */
495 ED_pose_clear_paths(ob);
497 /* notifiers for updates */
498 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
500 return OPERATOR_FINISHED;
503 void POSE_OT_paths_clear (wmOperatorType *ot)
506 ot->name= "Clear Bone Paths";
507 ot->idname= "POSE_OT_paths_clear";
508 ot->description= "Clear path caches for selected bones.";
511 ot->exec= pose_clear_paths_exec;
512 ot->poll= ED_operator_posemode;
515 ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
518 /* ******************* Select Constraint Target Operator ************* */
520 // XXX this function is to be removed when the other stuff is recoded
521 void pose_select_constraint_target(Scene *scene)
523 Object *obedit= scene->obedit; // XXX context
525 bArmature *arm= ob->data;
529 /* paranoia checks */
530 if (!ob && !ob->pose) return;
531 if (ob==obedit || (ob->flag & OB_POSEMODE)==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)) {
536 for (con= pchan->constraints.first; con; con= con->next) {
537 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
538 ListBase targets = {NULL, NULL};
539 bConstraintTarget *ct;
541 if (cti && cti->get_constraint_targets) {
542 cti->get_constraint_targets(con, &targets);
544 for (ct= targets.first; ct; ct= ct->next) {
545 if ((ct->tar == ob) && (ct->subtarget[0])) {
546 bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
548 pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
552 if (cti->flush_constraint_targets)
553 cti->flush_constraint_targets(con, &targets, 1);
560 BIF_undo_push("Select constraint target");
564 static int pose_select_constraint_target_exec(bContext *C, wmOperator *op)
566 Object *ob= CTX_data_active_object(C);
567 bArmature *arm= ob->data;
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 for (con= pchan->constraints.first; con; con= con->next) {
576 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
577 ListBase targets = {NULL, NULL};
578 bConstraintTarget *ct;
580 if (cti && cti->get_constraint_targets) {
581 cti->get_constraint_targets(con, &targets);
583 for (ct= targets.first; ct; ct= ct->next) {
584 if ((ct->tar == ob) && (ct->subtarget[0])) {
585 bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
587 pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
593 if (cti->flush_constraint_targets)
594 cti->flush_constraint_targets(con, &targets, 1);
602 return OPERATOR_CANCELLED;
604 WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
606 return OPERATOR_FINISHED;
609 void POSE_OT_select_constraint_target(wmOperatorType *ot)
612 ot->name= "Select Constraint Target";
613 ot->idname= "POSE_OT_select_constraint_target";
616 ot->exec= pose_select_constraint_target_exec;
617 ot->poll= ED_operator_posemode;
620 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
623 /* ******************* select hierarchy operator ************* */
625 static int pose_select_hierarchy_exec(bContext *C, wmOperator *op)
627 Object *ob= CTX_data_active_object(C);
628 bArmature *arm= ob->data;
630 Bone *curbone, *pabone, *chbone;
631 int direction = RNA_enum_get(op->ptr, "direction");
632 int add_to_sel = RNA_boolean_get(op->ptr, "extend");
635 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
636 curbone= pchan->bone;
638 if (arm->layer & curbone->layer) {
639 if (curbone->flag & (BONE_ACTIVE)) {
640 if (direction == BONE_SELECT_PARENT) {
642 if (pchan->parent == NULL) continue;
643 else pabone= pchan->parent->bone;
645 if ((arm->layer & pabone->layer) && !(pabone->flag & BONE_HIDDEN_P)) {
647 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
648 curbone->flag &= ~BONE_ACTIVE;
649 pabone->flag |= (BONE_ACTIVE|BONE_SELECTED);
654 } else { // BONE_SELECT_CHILD
656 if (pchan->child == NULL) continue;
657 else chbone = pchan->child->bone;
659 if ((arm->layer & chbone->layer) && !(chbone->flag & BONE_HIDDEN_P)) {
661 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
662 curbone->flag &= ~BONE_ACTIVE;
663 chbone->flag |= (BONE_ACTIVE|BONE_SELECTED);
674 return OPERATOR_CANCELLED;
676 WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
678 return OPERATOR_FINISHED;
681 void POSE_OT_select_hierarchy(wmOperatorType *ot)
683 static EnumPropertyItem direction_items[]= {
684 {BONE_SELECT_PARENT, "PARENT", 0, "Select Parent", ""},
685 {BONE_SELECT_CHILD, "CHILD", 0, "Select Child", ""},
686 {0, NULL, 0, NULL, NULL}
690 ot->name= "Select Hierarchy";
691 ot->idname= "POSE_OT_select_hierarchy";
694 ot->exec= pose_select_hierarchy_exec;
695 ot->poll= ED_operator_posemode;
698 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
701 RNA_def_enum(ot->srna, "direction", direction_items,
702 BONE_SELECT_PARENT, "Direction", "");
703 RNA_def_boolean(ot->srna, "extend", 0, "Add to Selection", "");
708 void pose_copy_menu(Scene *scene)
710 Object *obedit= scene->obedit; // XXX context
712 bArmature *arm= ob->data;
713 bPoseChannel *pchan, *pchanact;
717 /* paranoia checks */
718 if (ELEM(NULL, ob, ob->pose)) return;
719 if ((ob==obedit) || (ob->flag & OB_POSEMODE)==0) return;
722 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
723 if (pchan->bone->flag & BONE_ACTIVE)
727 if (pchan==NULL) return;
730 /* if proxy-protected bones selected, some things (such as locks + displays) shouldn't be changable,
731 * but for constraints (just add local constraints)
733 if (pose_has_protected_selected(ob, 1, 0)) {
734 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
736 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");
738 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");
741 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
743 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");
745 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");
752 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
753 if ( (arm->layer & pchan->bone->layer) &&
754 (pchan->bone->flag & BONE_SELECTED) &&
755 (pchan != pchanact) )
758 case 1: /* Local Location */
759 VECCOPY(pchan->loc, pchanact->loc);
761 case 2: /* Local Rotation */
762 QUATCOPY(pchan->quat, pchanact->quat);
764 case 3: /* Local Size */
765 VECCOPY(pchan->size, pchanact->size);
767 case 4: /* All Constraints */
769 ListBase tmp_constraints = {NULL, NULL};
771 /* copy constraints to tmpbase and apply 'local' tags before
772 * appending to list of constraints for this channel
774 copy_constraints(&tmp_constraints, &pchanact->constraints);
775 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
778 /* add proxy-local tags */
779 for (con= tmp_constraints.first; con; con= con->next)
780 con->flag |= CONSTRAINT_PROXY_LOCAL;
782 addlisttolist(&pchan->constraints, &tmp_constraints);
784 /* update flags (need to add here, not just copy) */
785 pchan->constflag |= pchanact->constflag;
788 ob->pose->flag |= POSE_RECALC;
791 case 6: /* Transform Locks */
792 pchan->protectflag = pchanact->protectflag;
794 case 7: /* IK (DOF) settings */
796 pchan->ikflag = pchanact->ikflag;
797 VECCOPY(pchan->limitmin, pchanact->limitmin);
798 VECCOPY(pchan->limitmax, pchanact->limitmax);
799 VECCOPY(pchan->stiffness, pchanact->stiffness);
800 pchan->ikstretch= pchanact->ikstretch;
803 case 8: /* Custom Bone Shape */
804 pchan->custom = pchanact->custom;
806 case 9: /* Visual Location */
807 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
809 case 10: /* Visual Rotation */
811 float delta_mat[4][4], quat[4];
813 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
814 Mat4ToQuat(delta_mat, quat);
815 QUATCOPY(pchan->quat, quat);
818 case 11: /* Visual Size */
820 float delta_mat[4][4], size[4];
822 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
823 Mat4ToSize(delta_mat, size);
824 VECCOPY(pchan->size, size);
830 else { /* constraints, optional (note: max we can have is 24 constraints) */
831 bConstraint *con, *con_back;
832 int const_toggle[24];
833 ListBase const_copy = {NULL, NULL};
835 BLI_duplicatelist(&const_copy, &(pchanact->constraints));
837 /* build the puplist of constraints */
838 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
840 // add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
843 // if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
844 // BLI_freelistN(&const_copy);
848 /* now build a new listbase from the options selected */
849 for (i=0, con=const_copy.first; con; i++) {
850 /* if not selected, free/remove it from the list */
851 if (!const_toggle[i]) {
853 BLI_freelinkN(&const_copy, con);
860 /* Copy the temo listbase to the selected posebones */
861 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
862 if ( (arm->layer & pchan->bone->layer) &&
863 (pchan->bone->flag & BONE_SELECTED) &&
866 ListBase tmp_constraints = {NULL, NULL};
868 /* copy constraints to tmpbase and apply 'local' tags before
869 * appending to list of constraints for this channel
871 copy_constraints(&tmp_constraints, &const_copy);
872 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
875 /* add proxy-local tags */
876 for (con= tmp_constraints.first; con; con= con->next)
877 con->flag |= CONSTRAINT_PROXY_LOCAL;
879 addlisttolist(&pchan->constraints, &tmp_constraints);
881 /* update flags (need to add here, not just copy) */
882 pchan->constflag |= pchanact->constflag;
885 BLI_freelistN(&const_copy);
886 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
889 ob->pose->flag |= POSE_RECALC;
892 DAG_object_flush_update(scene, ob, OB_RECALC_DATA); // and all its relations
894 BIF_undo_push("Copy Pose Attributes");
898 /* ******************** copy/paste pose ********************** */
900 /* Global copy/paste buffer for pose - cleared on start/end session + before every copy operation */
901 static bPose *g_posebuf = NULL;
903 void free_posebuf(void)
906 /* was copied without constraints */
907 BLI_freelistN(&g_posebuf->chanbase);
908 MEM_freeN(g_posebuf);
916 static int pose_copy_exec (bContext *C, wmOperator *op)
918 Object *ob= CTX_data_active_object(C);
920 /* sanity checking */
921 if ELEM(NULL, ob, ob->pose) {
922 BKE_report(op->reports, RPT_ERROR, "No Pose to Copy");
923 return OPERATOR_CANCELLED;
926 /* free existing pose buffer */
929 /* sets chan->flag to POSE_KEY if bone selected, then copy those bones to the buffer */
931 copy_pose(&g_posebuf, ob->pose, 0);
934 return OPERATOR_FINISHED;
937 void POSE_OT_copy (wmOperatorType *ot)
940 ot->name= "Copy Pose";
941 ot->idname= "POSE_OT_copy";
942 ot->description= "Copies the current pose of the selected bones to copy/paste buffer.";
945 ot->exec= pose_copy_exec;
946 ot->poll= ED_operator_posemode;
949 ot->flag= OPTYPE_REGISTER;
954 static int pose_paste_exec (bContext *C, wmOperator *op)
956 Scene *scene= CTX_data_scene(C);
957 Object *ob= CTX_data_active_object(C);
958 bPoseChannel *chan, *pchan;
960 int flip= RNA_boolean_get(op->ptr, "flipped");
963 if ELEM(NULL, ob, ob->pose)
964 return OPERATOR_CANCELLED;
966 if (g_posebuf == NULL) {
967 BKE_report(op->reports, RPT_ERROR, "Copy buffer is empty");
968 return OPERATOR_CANCELLED;
971 /* Safely merge all of the channels in the buffer pose into any existing pose */
972 for (chan= g_posebuf->chanbase.first; chan; chan=chan->next) {
973 if (chan->flag & POSE_KEY) {
974 /* get the name - if flipping, we must flip this first */
975 BLI_strncpy(name, chan->name, sizeof(name));
977 bone_flip_name(name, 0); /* 0 = don't strip off number extensions */
979 /* only copy when channel exists, poses are not meant to add random channels to anymore */
980 pchan= get_pose_channel(ob->pose, name);
984 * - only copies transform info for the pose
986 VECCOPY(pchan->loc, chan->loc);
987 VECCOPY(pchan->size, chan->size);
988 pchan->flag= chan->flag;
990 /* check if rotation modes are compatible (i.e. do they need any conversions) */
991 if (pchan->rotmode == chan->rotmode) {
992 /* copy the type of rotation in use */
993 if (pchan->rotmode) {
994 VECCOPY(pchan->eul, chan->eul);
997 QUATCOPY(pchan->quat, chan->quat);
1000 else if (pchan->rotmode) {
1002 QuatToEul(chan->quat, pchan->eul);
1006 EulToQuat(chan->eul, pchan->quat);
1009 /* paste flipped pose? */
1013 /* has to be done as eulers... */
1014 if (pchan->rotmode) {
1015 pchan->eul[1] *= -1;
1016 pchan->eul[2] *= -1;
1021 QuatToEul(pchan->quat, eul);
1024 EulToQuat(eul, pchan->quat);
1028 #if 0 // XXX old animation system
1029 if (autokeyframe_cfra_can_key(ob)) {
1032 /* Set keys on pose */
1033 // TODO: make these use keyingsets....
1034 if (chan->flag & POSE_ROT) {
1035 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
1036 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
1037 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
1038 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
1040 if (chan->flag & POSE_SIZE) {
1041 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
1042 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
1043 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
1045 if (chan->flag & POSE_LOC) {
1046 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
1047 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
1048 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
1051 /* clear any unkeyed tags */
1053 chan->bone->flag &= ~BONE_UNKEYED;
1056 /* add unkeyed tags */
1058 chan->bone->flag |= BONE_UNKEYED;
1060 #endif // XXX old animation system
1065 /* Update event for pose and deformation children */
1066 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1068 if (IS_AUTOKEY_ON(scene)) {
1069 // XXX remake_action_ipos(ob->action);
1072 /* need to trick depgraph, action is not allowed to execute on pose */
1073 where_is_pose(scene, ob);
1077 /* notifiers for updates */
1078 WM_event_add_notifier(C, NC_OBJECT|ND_POSE|ND_TRANSFORM, ob);
1080 return OPERATOR_FINISHED;
1083 void POSE_OT_paste (wmOperatorType *ot)
1086 ot->name= "Paste Pose";
1087 ot->idname= "POSE_OT_paste";
1088 ot->description= "Pastes the stored pose on to the current pose.";
1091 ot->exec= pose_paste_exec;
1092 ot->poll= ED_operator_posemode;
1095 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1098 RNA_def_boolean(ot->srna, "flipped", 0, "Flipped on X-Axis", "");
1101 /* ********************************************** */
1103 /* context weightpaint and deformer in posemode */
1104 void pose_adds_vgroups(Scene *scene, Object *meshobj, int heatweights)
1106 // XXX extern VPaint Gwp; /* from vpaint */
1107 Object *poseobj= modifiers_isDeformedByArmature(meshobj);
1109 if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) {
1110 error("The active object must have a deforming armature in pose mode");
1114 // XXX add_verts_to_dgroups(meshobj, poseobj, heatweights, (Gwp.flag & VP_MIRROR_X));
1117 BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
1119 BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
1122 // and all its relations
1123 DAG_object_flush_update(scene, meshobj, OB_RECALC_DATA);
1126 /* ********************************************** */
1129 static int pose_group_add_exec (bContext *C, wmOperator *op)
1131 ScrArea *sa= CTX_wm_area(C);
1134 /* since this call may also be used from the buttons window, we need to check for where to get the object */
1135 if (sa->spacetype == SPACE_BUTS)
1136 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1138 ob= CTX_data_active_object(C);
1140 /* only continue if there's an object */
1142 return OPERATOR_CANCELLED;
1144 /* for now, just call the API function for this */
1147 /* notifiers for updates */
1148 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1150 return OPERATOR_FINISHED;
1153 void POSE_OT_group_add (wmOperatorType *ot)
1156 ot->name= "Add Bone Group";
1157 ot->idname= "POSE_OT_group_add";
1158 ot->description= "Add a new bone group.";
1161 ot->exec= pose_group_add_exec;
1162 ot->poll= ED_operator_posemode;
1165 ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1169 static int pose_group_remove_exec (bContext *C, wmOperator *op)
1171 ScrArea *sa= CTX_wm_area(C);
1174 /* since this call may also be used from the buttons window, we need to check for where to get the object */
1175 if (sa->spacetype == SPACE_BUTS)
1176 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1178 ob= CTX_data_active_object(C);
1180 /* only continue if there's an object */
1182 return OPERATOR_CANCELLED;
1184 /* for now, just call the API function for this */
1185 pose_remove_group(ob);
1187 /* notifiers for updates */
1188 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1190 return OPERATOR_FINISHED;
1193 void POSE_OT_group_remove (wmOperatorType *ot)
1196 ot->name= "Remove Bone Group";
1197 ot->idname= "POSE_OT_group_remove";
1198 ot->description= "Removes the active bone group.";
1201 ot->exec= pose_group_remove_exec;
1202 ot->poll= ED_operator_posemode;
1205 ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1210 /* invoke callback which presents a list of bone-groups for the user to choose from */
1211 static int pose_groups_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1213 ScrArea *sa= CTX_wm_area(C);
1222 /* since this call may also be used from the buttons window, we need to check for where to get the object */
1223 if (sa->spacetype == SPACE_BUTS)
1224 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1226 ob= CTX_data_active_object(C);
1228 /* only continue if there's an object, and a pose there too */
1229 if (ELEM(NULL, ob, ob->pose))
1230 return OPERATOR_CANCELLED;
1233 /* if there's no active group (or active is invalid), create a new menu to find it */
1234 if (pose->active_group <= 0) {
1235 /* create a new menu, and start populating it with group names */
1236 pup= uiPupMenuBegin(C, op->type->name, 0);
1237 layout= uiPupMenuLayout(pup);
1239 /* special entry - allow to create new group, then use that
1240 * (not to be used for removing though)
1242 if (strstr(op->idname, "assign")) {
1243 uiItemIntO(layout, "New Group", 0, op->idname, "type", 0);
1247 /* add entries for each group */
1248 for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++)
1249 uiItemIntO(layout, grp->name, 0, op->idname, "type", i);
1251 /* finish building the menu, and process it (should result in calling self again) */
1252 uiPupMenuEnd(C, pup);
1254 return OPERATOR_CANCELLED;
1257 /* just use the active group index, and call the exec callback for the calling operator */
1258 RNA_int_set(op->ptr, "type", pose->active_group);
1259 return op->type->exec(C, op);
1263 /* Assign selected pchans to the bone group that the user selects */
1264 static int pose_group_assign_exec (bContext *C, wmOperator *op)
1266 ScrArea *sa= CTX_wm_area(C);
1270 bPoseChannel *pchan;
1273 /* since this call may also be used from the buttons window, we need to check for where to get the object */
1274 if (sa->spacetype == SPACE_BUTS)
1275 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1277 ob= CTX_data_active_object(C);
1279 /* only continue if there's an object, and a pose there too */
1280 if (ELEM(NULL, ob, ob->pose))
1281 return OPERATOR_CANCELLED;
1285 /* set the active group number to the one from operator props
1286 * - if 0 after this, make a new group...
1288 pose->active_group= RNA_int_get(op->ptr, "type");
1289 if (pose->active_group == 0)
1292 /* add selected bones to group then */
1293 // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
1294 // CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans)
1295 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1296 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
1297 // NOTE: sync this view3d_context() in space_view3d.c
1298 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
1299 if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) {
1300 pchan->agrp_index= pose->active_group;
1306 /* notifiers for updates */
1307 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1309 /* report done status */
1311 return OPERATOR_FINISHED;
1313 return OPERATOR_CANCELLED;
1316 void POSE_OT_group_assign (wmOperatorType *ot)
1319 ot->name= "Add Selected to Bone Group";
1320 ot->idname= "POSE_OT_group_assign";
1321 ot->description= "Add selected bones to the chosen bone group.";
1324 ot->invoke= pose_groups_menu_invoke;
1325 ot->exec= pose_group_assign_exec;
1326 ot->poll= ED_operator_posemode;
1329 ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1332 RNA_def_int(ot->srna, "type", 0, 0, 10, "Bone Group Index", "", 0, INT_MAX);
1336 static int pose_group_unassign_exec (bContext *C, wmOperator *op)
1338 ScrArea *sa= CTX_wm_area(C);
1342 bPoseChannel *pchan;
1345 /* since this call may also be used from the buttons window, we need to check for where to get the object */
1346 if (sa->spacetype == SPACE_BUTS)
1347 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1349 ob= CTX_data_active_object(C);
1351 /* only continue if there's an object, and a pose there too */
1352 if (ELEM(NULL, ob, ob->pose))
1353 return OPERATOR_CANCELLED;
1357 /* add selected bones to ungroup then */
1358 // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
1359 // CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans)
1360 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1361 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
1362 // NOTE: sync this view3d_context() in space_view3d.c
1363 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
1364 if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) {
1365 if (pchan->agrp_index) {
1366 pchan->agrp_index= 0;
1373 /* notifiers for updates */
1374 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1376 /* report done status */
1378 return OPERATOR_FINISHED;
1380 return OPERATOR_CANCELLED;
1383 void POSE_OT_group_unassign (wmOperatorType *ot)
1386 ot->name= "Remove Selected from Bone Groups";
1387 ot->idname= "POSE_OT_group_unassign";
1388 ot->description= "Add selected bones from all bone groups";
1391 ot->exec= pose_group_unassign_exec;
1392 ot->poll= ED_operator_posemode;
1395 ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1398 /* ----------------- */
1400 static int pose_groupOps_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1402 Object *ob= CTX_data_active_object(C);
1403 uiPopupMenu *pup= uiPupMenuBegin(C, op->type->name, 0);
1404 uiLayout *layout= uiPupMenuLayout(pup);
1406 /* sanity check - must have object with pose */
1407 if ELEM(NULL, ob, ob->pose)
1408 return OPERATOR_CANCELLED;
1410 /* get mode of action */
1411 if (CTX_DATA_COUNT(C, selected_pchans)) {
1412 /* if selected bone(s), include options to add/remove to active group */
1413 uiItemO(layout, "Add Selected to Active Group", 0, "POSE_OT_group_assign");
1417 uiItemO(layout, "Remove Selected from All Groups", 0, "POSE_OT_group_unassign");
1418 uiItemO(layout, "Remove Active Group", 0, "POSE_OT_group_remove");
1421 /* no selected bones - so just options for groups management */
1422 uiItemO(layout, "Add New Group", 0, "POSE_OT_group_add");
1423 uiItemO(layout, "Remove Active Group", 0, "POSE_OT_group_remove");
1426 return OPERATOR_CANCELLED;
1429 void POSE_OT_groups_menu (wmOperatorType *ot)
1432 ot->name= "Bone Group Tools";
1433 ot->idname= "POSE_OT_groups_menu";
1434 ot->description= "Menu displaying available tools for Bone Groups.";
1436 /* api callbacks (only invoke needed) */
1437 ot->invoke= pose_groupOps_menu_invoke;
1438 ot->poll= ED_operator_posemode;
1441 ot->flag= OPTYPE_REGISTER;
1444 /* ********************************************** */
1446 static short pose_select_same_group (Object *ob)
1448 bPose *pose= (ob)? ob->pose : NULL;
1449 bArmature *arm= (ob)? ob->data : NULL;
1450 bPoseChannel *pchan, *chan;
1453 if (ELEM3(NULL, ob, pose, arm))
1456 /* loop in loop... bad and slow! */
1457 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1458 if (arm->layer & pchan->bone->layer) {
1459 if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1461 /* only if group matches (and is not selected or current bone) */
1462 for (chan= ob->pose->chanbase.first; chan; chan= chan->next) {
1463 if (arm->layer & chan->bone->layer) {
1464 if (pchan->agrp_index == chan->agrp_index) {
1465 chan->bone->flag |= BONE_SELECTED;
1478 static short pose_select_same_layer (Object *ob)
1480 bPose *pose= (ob)? ob->pose : NULL;
1481 bArmature *arm= (ob)? ob->data : NULL;
1482 bPoseChannel *pchan;
1483 short layers= 0, changed= 0;
1485 if (ELEM3(NULL, ob, pose, arm))
1488 /* figure out what bones are selected */
1489 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1490 if (arm->layer & pchan->bone->layer) {
1491 if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1492 layers |= pchan->bone->layer;
1499 /* select bones that are on same layers as layers flag */
1500 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1501 if (arm->layer & pchan->bone->layer) {
1502 if (layers & pchan->bone->layer) {
1503 pchan->bone->flag |= BONE_SELECTED;
1512 void pose_select_grouped (Scene *scene, short nr)
1516 if (nr == 1) changed= pose_select_same_group(OBACT);
1517 else if (nr == 2) changed= pose_select_same_layer(OBACT);
1521 BIF_undo_push("Select Grouped");
1525 /* Shift-G in 3D-View while in PoseMode */
1526 void pose_select_grouped_menu (Scene *scene)
1531 nr= pupmenu("Select Grouped%t|In Same Group%x1|In Same Layer%x2");
1532 pose_select_grouped(scene, nr);
1535 /* ********************************************** */
1537 static int pose_flip_names_exec (bContext *C, wmOperator *op)
1539 Scene *scene= CTX_data_scene(C);
1540 Object *ob= CTX_data_active_object(C);
1544 /* paranoia checks */
1545 if (ELEM(NULL, ob, ob->pose))
1546 return OPERATOR_CANCELLED;
1549 /* loop through selected bones, auto-naming them */
1550 CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans)
1552 BLI_strncpy(newname, pchan->name, sizeof(newname));
1553 bone_flip_name(newname, 1); // 1 = do strip off number extensions
1554 ED_armature_bone_rename(arm, pchan->name, newname);
1558 /* since we renamed stuff... */
1559 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1561 /* note, notifier might evolve */
1562 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1564 return OPERATOR_FINISHED;
1567 void POSE_OT_flip_names (wmOperatorType *ot)
1570 ot->name= "Flip Names";
1571 ot->idname= "POSE_OT_flip_names";
1572 ot->description= "Flips (and corrects) the names of selected bones.";
1575 ot->exec= pose_flip_names_exec;
1576 ot->poll= ED_operator_posemode;
1579 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1582 /* ------------------ */
1584 static int pose_autoside_names_exec (bContext *C, wmOperator *op)
1586 Scene *scene= CTX_data_scene(C);
1587 Object *ob= CTX_data_active_object(C);
1590 short axis= RNA_enum_get(op->ptr, "axis");
1592 /* paranoia checks */
1593 if (ELEM(NULL, ob, ob->pose))
1594 return OPERATOR_CANCELLED;
1597 /* loop through selected bones, auto-naming them */
1598 CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans)
1600 BLI_strncpy(newname, pchan->name, sizeof(newname));
1601 bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
1602 ED_armature_bone_rename(arm, pchan->name, newname);
1606 /* since we renamed stuff... */
1607 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1609 /* note, notifier might evolve */
1610 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1612 return OPERATOR_FINISHED;
1615 void POSE_OT_autoside_names (wmOperatorType *ot)
1617 static EnumPropertyItem axis_items[]= {
1618 {0, "XAXIS", 0, "X-Axis", "Left/Right"},
1619 {1, "YAXIS", 0, "Y-Axis", "Front/Back"},
1620 {2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
1621 {0, NULL, 0, NULL, NULL}};
1624 ot->name= "AutoName by Axis";
1625 ot->idname= "POSE_OT_autoside_names";
1626 ot->description= "Automatically renames the selected bones according to which side of the target axis they fall on.";
1629 ot->invoke= WM_menu_invoke;
1630 ot->exec= pose_autoside_names_exec;
1631 ot->poll= ED_operator_posemode;
1634 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1637 RNA_def_enum(ot->srna, "axis", axis_items, 0, "Axis", "Axis tag names with.");
1640 /* ********************************************** */
1642 /* context active object, or weightpainted object with armature in posemode */
1643 void pose_activate_flipped_bone(Scene *scene)
1646 bArmature *arm= ob->data;
1648 if(ob==NULL) return;
1650 if(G.f & G_WEIGHTPAINT) {
1651 ob= modifiers_isDeformedByArmature(ob);
1653 if(ob && (ob->flag & OB_POSEMODE)) {
1654 bPoseChannel *pchan, *pchanf;
1656 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1657 if(arm->layer & pchan->bone->layer) {
1658 if(pchan->bone->flag & BONE_ACTIVE)
1665 BLI_strncpy(name, pchan->name, 32);
1666 bone_flip_name(name, 1); // 0 = do not strip off number extensions
1668 pchanf= get_pose_channel(ob->pose, name);
1669 if(pchanf && pchanf!=pchan) {
1670 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1671 pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
1673 /* in weightpaint we select the associated vertex group too */
1674 if(G.f & G_WEIGHTPAINT) {
1675 vertexgroup_select_by_name(OBACT, name);
1676 DAG_object_flush_update(scene, OBACT, OB_RECALC_DATA);
1679 // XXX notifiers need to be sent to other editors to update
1687 /* ********************************************** */
1689 /* Present a popup to get the layers that should be used */
1690 // TODO: move to wm?
1691 static uiBlock *wm_layers_select_create_menu(bContext *C, ARegion *ar, void *arg_op)
1693 wmOperator *op= arg_op;
1696 uiStyle *style= U.uistyles.first;
1698 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1699 uiBlockClearFlag(block, UI_BLOCK_LOOP);
1700 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
1702 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 150, 20, style);
1703 uiItemL(layout, op->type->name, 0);
1704 uiTemplateLayers(layout, op->ptr, "layers"); /* must have a property named layers setup */
1706 uiPopupBoundsBlock(block, 4.0f, 0, 0);
1707 uiEndBlock(C, block);
1712 /* ------------------- */
1714 /* Present a popup to get the layers that should be used */
1715 static int pose_armature_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1717 Object *ob= CTX_data_active_object(C);
1718 bArmature *arm= (ob)? ob->data : NULL;
1720 int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1722 /* sanity checking */
1724 return OPERATOR_CANCELLED;
1726 /* get RNA pointer to armature data to use that to retrieve the layers as ints to init the operator */
1727 RNA_id_pointer_create((ID *)arm, &ptr);
1728 RNA_boolean_get_array(&ptr, "layer", layers);
1729 RNA_boolean_set_array(op->ptr, "layers", layers);
1731 /* part to sync with other similar operators... */
1732 /* pass on operator, so return modal */
1733 uiPupBlockOperator(C, wm_layers_select_create_menu, op, WM_OP_EXEC_DEFAULT);
1734 return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
1737 /* Set the visible layers for the active armature (edit and pose modes) */
1738 static int pose_armature_layers_exec (bContext *C, wmOperator *op)
1740 Object *ob= CTX_data_active_object(C);
1741 bArmature *arm= (ob)? ob->data : NULL;
1743 int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1745 /* get the values set in the operator properties */
1746 RNA_boolean_get_array(op->ptr, "layers", layers);
1748 /* get pointer for armature, and write data there... */
1749 RNA_id_pointer_create((ID *)arm, &ptr);
1750 RNA_boolean_set_array(&ptr, "layer", layers);
1752 /* note, notifier might evolve */
1753 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1755 return OPERATOR_FINISHED;
1759 void POSE_OT_armature_layers (wmOperatorType *ot)
1762 ot->name= "Change Armature Layers";
1763 ot->idname= "POSE_OT_armature_layers";
1764 ot->description= "Change the visible armature layers.";
1767 ot->invoke= pose_armature_layers_invoke;
1768 ot->exec= pose_armature_layers_exec;
1769 ot->poll= ED_operator_posemode;
1772 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1775 RNA_def_boolean_array(ot->srna, "layers", 16, NULL, "Layers", "Armature layers to make visible.");
1778 void ARMATURE_OT_armature_layers (wmOperatorType *ot)
1781 ot->name= "Change Armature Layers";
1782 ot->idname= "ARMATURE_OT_armature_layers";
1783 ot->description= "Change the visible armature layers.";
1786 ot->invoke= pose_armature_layers_invoke;
1787 ot->exec= pose_armature_layers_exec;
1788 ot->poll= ED_operator_editarmature;
1791 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1794 RNA_def_boolean_array(ot->srna, "layers", 16, NULL, "Layers", "Armature layers to make visible.");
1797 /* ------------------- */
1799 /* Present a popup to get the layers that should be used */
1800 static int pose_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1802 int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1804 /* get layers that are active already */
1805 memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
1807 CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pchans)
1811 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1812 for (bit= 0; bit < 16; bit++) {
1813 if (pchan->bone->layer & (1<<bit))
1819 /* copy layers to operator */
1820 RNA_boolean_set_array(op->ptr, "layers", layers);
1822 /* part to sync with other similar operators... */
1823 /* pass on operator, so return modal */
1824 uiPupBlockOperator(C, wm_layers_select_create_menu, op, WM_OP_EXEC_DEFAULT);
1825 return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
1828 /* Set the visible layers for the active armature (edit and pose modes) */
1829 static int pose_bone_layers_exec (bContext *C, wmOperator *op)
1831 Object *ob= CTX_data_active_object(C);
1832 bArmature *arm= (ob)? ob->data : NULL;
1834 int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1836 /* get the values set in the operator properties */
1837 RNA_boolean_get_array(op->ptr, "layers", layers);
1839 /* set layers of pchans based on the values set in the operator props */
1840 CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pchans)
1842 /* get pointer for pchan, and write flags this way */
1843 RNA_pointer_create((ID *)arm, &RNA_Bone, pchan->bone, &ptr);
1844 RNA_boolean_set_array(&ptr, "layer", layers);
1848 /* note, notifier might evolve */
1849 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1851 return OPERATOR_FINISHED;
1854 void POSE_OT_bone_layers (wmOperatorType *ot)
1857 ot->name= "Change Bone Layers";
1858 ot->idname= "POSE_OT_bone_layers";
1859 ot->description= "Change the layers that the selected bones belong to.";
1862 ot->invoke= pose_bone_layers_invoke;
1863 ot->exec= pose_bone_layers_exec;
1864 ot->poll= ED_operator_posemode;
1867 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1870 RNA_def_boolean_array(ot->srna, "layers", 16, NULL, "Layers", "Armature layers that bone belongs to.");
1873 /* ------------------- */
1875 /* Present a popup to get the layers that should be used */
1876 static int armature_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1878 int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1880 /* get layers that are active already */
1881 memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
1883 CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones)
1887 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1888 for (bit= 0; bit < 16; bit++) {
1889 if (ebone->layer & (1<<bit))
1895 /* copy layers to operator */
1896 RNA_boolean_set_array(op->ptr, "layers", layers);
1898 /* part to sync with other similar operators... */
1899 /* pass on operator, so return modal */
1900 uiPupBlockOperator(C, wm_layers_select_create_menu, op, WM_OP_EXEC_DEFAULT);
1901 return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
1904 /* Set the visible layers for the active armature (edit and pose modes) */
1905 static int armature_bone_layers_exec (bContext *C, wmOperator *op)
1907 Object *ob= CTX_data_active_object(C);
1908 bArmature *arm= (ob)? ob->data : NULL;
1910 int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1912 /* get the values set in the operator properties */
1913 RNA_boolean_get_array(op->ptr, "layers", layers);
1915 /* set layers of pchans based on the values set in the operator props */
1916 CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones)
1918 /* get pointer for pchan, and write flags this way */
1919 RNA_pointer_create((ID *)arm, &RNA_EditBone, ebone, &ptr);
1920 RNA_boolean_set_array(&ptr, "layers", layers);
1924 /* note, notifier might evolve */
1925 WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1927 return OPERATOR_FINISHED;
1930 void ARMATURE_OT_bone_layers (wmOperatorType *ot)
1933 ot->name= "Change Bone Layers";
1934 ot->idname= "ARMATURE_OT_bone_layers";
1935 ot->description= "Change the layers that the selected bones belong to.";
1938 ot->invoke= armature_bone_layers_invoke;
1939 ot->exec= armature_bone_layers_exec;
1940 ot->poll= ED_operator_editarmature;
1943 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1946 RNA_def_boolean_array(ot->srna, "layers", 16, NULL, "Layers", "Armature layers that bone belongs to.");
1952 /* for use with pose_relax only */
1953 static int pose_relax_icu(struct IpoCurve *icu, float framef, float *val, float *frame_prev, float *frame_next)
1959 BezTriple *bezt = icu->bezt;
1961 BezTriple *bezt_prev=NULL, *bezt_next=NULL;
1965 for (i=0; i < icu->totvert; i++, bezt++) {
1966 if (bezt->vec[1][0] < framef - 0.5) {
1973 if (bezt_prev==NULL) return 0;
1975 /* advance to the next, dont need to advance i */
1978 for (; i < icu->totvert; i++, bezt++) {
1979 if (bezt->vec[1][0] > framef + 0.5) {
1985 if (bezt_next==NULL) return 0;
1988 w1 = framef - bezt_prev->vec[1][0];
1989 w2 = bezt_next->vec[1][0] - framef;
1994 val = (bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1);
1996 /* apply the value with a hard coded 6th */
1997 *val = (((bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1)) + (*val * 5.0f)) / 6.0f;
2001 if (frame_prev) *frame_prev = bezt_prev->vec[1][0];
2002 if (frame_next) *frame_next = bezt_next->vec[1][0];
2009 void pose_relax(Scene *scene)
2016 // IpoCurve *icu_w, *icu_x, *icu_y, *icu_z;
2018 bPoseChannel *pchan;
2019 // bActionChannel *achan;
2020 // float framef = F_CFRA;
2021 // float frame_prev, frame_next;
2022 // float quat_prev[4], quat_next[4], quat_interp[4], quat_orig[4];
2028 // int do_x, do_y, do_z;
2034 arm = (bArmature *)ob->data;
2036 if (!pose || !act || !arm) return;
2038 for (pchan=pose->chanbase.first; pchan; pchan= pchan->next) {
2040 pchan->bone->flag &= ~BONE_TRANSFORM;
2042 if (pchan->bone->layer & arm->layer) {
2043 if (pchan->bone->flag & BONE_SELECTED) {
2044 /* do we have an ipo curve? */
2045 #if 0 // XXX old animation system
2046 achan= get_action_channel(act, pchan->name);
2048 if (achan && achan->ipo) {
2049 /*calc_ipo(achan->ipo, ctime);*/
2051 do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_X), framef, &pchan->loc[0], NULL, NULL);
2052 do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Y), framef, &pchan->loc[1], NULL, NULL);
2053 do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Z), framef, &pchan->loc[2], NULL, NULL);
2054 do_loc += do_x + do_y + do_z;
2056 do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_X), framef, &pchan->size[0], NULL, NULL);
2057 do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Y), framef, &pchan->size[1], NULL, NULL);
2058 do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Z), framef, &pchan->size[2], NULL, NULL);
2059 do_scale += do_x + do_y + do_z;
2061 if( ((icu_w = find_ipocurve(achan->ipo, AC_QUAT_W))) &&
2062 ((icu_x = find_ipocurve(achan->ipo, AC_QUAT_X))) &&
2063 ((icu_y = find_ipocurve(achan->ipo, AC_QUAT_Y))) &&
2064 ((icu_z = find_ipocurve(achan->ipo, AC_QUAT_Z))) )
2066 /* use the quatw keyframe as a basis for others */
2067 if (pose_relax_icu(icu_w, framef, NULL, &frame_prev, &frame_next)) {
2069 quat_prev[0] = eval_icu(icu_w, frame_prev);
2070 quat_prev[1] = eval_icu(icu_x, frame_prev);
2071 quat_prev[2] = eval_icu(icu_y, frame_prev);
2072 quat_prev[3] = eval_icu(icu_z, frame_prev);
2074 quat_next[0] = eval_icu(icu_w, frame_next);
2075 quat_next[1] = eval_icu(icu_x, frame_next);
2076 quat_next[2] = eval_icu(icu_y, frame_next);
2077 quat_next[3] = eval_icu(icu_z, frame_next);
2080 /* apply the setting, completely smooth */
2081 QuatInterpol(pchan->quat, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
2083 /* tricky interpolation */
2084 QuatInterpol(quat_interp, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
2085 QUATCOPY(quat_orig, pchan->quat);
2086 QuatInterpol(pchan->quat, quat_orig, quat_interp, 1.0f/6.0f);
2093 /* apply BONE_TRANSFORM tag so that autokeying will pick it up */
2094 pchan->bone->flag |= BONE_TRANSFORM;
2097 #endif // XXX old animation system
2102 ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
2104 /* do auto-keying */
2105 if (do_loc) flag |= TFM_TRANSLATION;
2106 if (do_scale) flag |= TFM_RESIZE;
2107 if (do_quat) flag |= TFM_ROTATION;
2108 autokeyframe_pose_cb_func(ob, flag, 0);
2110 /* clear BONE_TRANSFORM flags */
2111 for (pchan=pose->chanbase.first; pchan; pchan= pchan->next)
2112 pchan->bone->flag &= ~ BONE_TRANSFORM;
2114 /* do depsgraph flush */
2115 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
2116 BIF_undo_push("Relax Pose");
2119 /* for use in insertkey, ensure rotation goes other way around */
2120 void pose_flipquats(Scene *scene)
2123 bArmature *arm= ob->data;
2124 bPoseChannel *pchan;
2129 /* find sel bones */
2130 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2131 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
2132 /* quaternions have 720 degree range */
2133 pchan->quat[0]= -pchan->quat[0];
2134 pchan->quat[1]= -pchan->quat[1];
2135 pchan->quat[2]= -pchan->quat[2];
2136 pchan->quat[3]= -pchan->quat[3];
2141 autokeyframe_pose_cb_func(ob, TFM_ROTATION, 0);
2144 /* context: active channel */
2145 void pose_special_editmenu(Scene *scene)
2148 Object *obedit= scene->obedit; // XXX context
2152 /* paranoia checks */
2153 if(!ob && !ob->pose) return;
2154 if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
2156 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");
2158 pose_select_constraint_target(scene);
2164 pose_calculate_path(C, ob);
2167 pose_clear_paths(ob);
2170 pose_clear_user_transforms(scene, ob);
2175 else if(ELEM3(nr, 7, 8, 9)) {
2176 pose_autoside_names(nr-7);
2181 /* Restore selected pose-bones to 'action'-defined pose */
2182 void pose_clear_user_transforms(Scene *scene, Object *ob)
2184 bArmature *arm= ob->data;
2185 bPoseChannel *pchan;
2187 if (ob->pose == NULL)
2190 /* if the object has an action, restore pose to the pose defined by the action by clearing pose on selected bones */
2192 /* find selected bones */
2193 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2194 if (pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
2195 /* just clear the BONE_UNKEYED flag, allowing this bone to get overwritten by actions again */
2196 pchan->bone->flag &= ~BONE_UNKEYED;
2200 /* clear pose locking flag
2201 * - this will only clear the user-defined pose in the selected bones, where BONE_UNKEYED has been cleared
2203 ob->pose->flag |= POSE_DO_UNLOCK;
2206 /* no action, so restore entire pose to rest pose (cannot restore only selected bones) */
2207 rest_pose(ob->pose);
2210 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
2211 BIF_undo_push("Clear User Transform");