2 * $Id: editaction.c 17746 2008-12-08 11:19:44Z aligorith $
4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): Joshua Leung
27 * ***** END GPL LICENSE BLOCK *****
39 #include "MEM_guardedalloc.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_arithb.h"
44 #include "DNA_anim_types.h"
45 #include "DNA_action_types.h"
46 #include "DNA_armature_types.h"
47 #include "DNA_camera_types.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_constraint_types.h"
54 #include "DNA_key_types.h"
55 #include "DNA_lamp_types.h"
56 #include "DNA_material_types.h"
57 #include "DNA_userdef_types.h"
58 #include "DNA_gpencil_types.h"
59 #include "DNA_windowmanager_types.h"
61 #include "RNA_access.h"
62 #include "RNA_define.h"
63 #include "RNA_enum_types.h"
65 #include "BKE_action.h"
66 #include "BKE_depsgraph.h"
67 #include "BKE_fcurve.h"
69 #include "BKE_material.h"
70 #include "BKE_object.h"
71 #include "BKE_context.h"
72 #include "BKE_report.h"
73 #include "BKE_utildefines.h"
75 #include "UI_view2d.h"
77 #include "BIF_transform.h"
79 #include "ED_anim_api.h"
80 #include "ED_keyframing.h"
81 #include "ED_keyframes_draw.h"
82 #include "ED_keyframes_edit.h"
83 #include "ED_screen.h"
84 #include "ED_space_api.h"
89 #include "action_intern.h"
91 /* ************************************************************************** */
92 /* KEYFRAME-RANGE STUFF */
94 /* *************************** Calculate Range ************************** */
96 /* Get the min/max keyframes*/
97 static void get_keyframe_extents (bAnimContext *ac, float *min, float *max)
99 ListBase anim_data = {NULL, NULL};
103 /* get data to filter, from Action or Dopesheet */
104 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
105 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
107 /* set large values to try to override */
111 /* check if any channels to set range with */
112 if (anim_data.first) {
113 /* go through channels, finding max extents */
114 for (ale= anim_data.first; ale; ale= ale->next) {
115 Object *nob= ANIM_nla_mapping_get(ac, ale);
116 FCurve *fcu= (FCurve *)ale->key_data;
119 /* get range and apply necessary scaling before */
120 calc_fcurve_range(fcu, &tmin, &tmax);
123 tmin= get_action_frame_inv(nob, tmin);
124 tmax= get_action_frame_inv(nob, tmax);
127 /* try to set cur using these values, if they're more extreme than previously set values */
128 *min= MIN2(*min, tmin);
129 *max= MAX2(*max, tmax);
133 BLI_freelistN(&anim_data);
136 /* set default range */
138 *min= (float)ac->scene->r.sfra;
139 *max= (float)ac->scene->r.efra;
148 /* ****************** Automatic Preview-Range Operator ****************** */
150 static int actkeys_previewrange_exec(bContext *C, wmOperator *op)
156 /* get editor data */
157 if (ANIM_animdata_get_context(C, &ac) == 0)
158 return OPERATOR_CANCELLED;
159 if (ac.scene == NULL)
160 return OPERATOR_CANCELLED;
164 /* set the range directly */
165 get_keyframe_extents(&ac, &min, &max);
166 scene->r.psfra= (int)floor(min + 0.5f);
167 scene->r.pefra= (int)floor(max + 0.5f);
169 /* set notifier that things have changed */
170 // XXX err... there's nothing for frame ranges yet, but this should do fine too
171 WM_event_add_notifier(C, NC_SCENE|ND_FRAME, ac.scene);
173 return OPERATOR_FINISHED;
176 void ACT_OT_previewrange_set (wmOperatorType *ot)
179 ot->name= "Auto-Set Preview Range";
180 ot->idname= "ACT_OT_previewrange_set";
183 ot->exec= actkeys_previewrange_exec;
184 ot->poll= ED_operator_areaactive;
187 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
190 /* ****************** View-All Operator ****************** */
192 static int actkeys_viewall_exec(bContext *C, wmOperator *op)
198 /* get editor data */
199 if (ANIM_animdata_get_context(C, &ac) == 0)
200 return OPERATOR_CANCELLED;
203 /* set the horizontal range, with an extra offset so that the extreme keys will be in view */
204 get_keyframe_extents(&ac, &v2d->cur.xmin, &v2d->cur.xmax);
206 extra= 0.1f * (v2d->cur.xmax - v2d->cur.xmin);
207 v2d->cur.xmin -= extra;
208 v2d->cur.xmax += extra;
210 /* set vertical range */
212 v2d->cur.ymin= (float)-(v2d->mask.ymax - v2d->mask.ymin);
214 /* do View2D syncing */
215 UI_view2d_sync(CTX_wm_screen(C), CTX_wm_area(C), v2d, V2D_LOCK_COPY);
217 /* set notifier that things have changed */
218 ED_area_tag_redraw(CTX_wm_area(C));
220 return OPERATOR_FINISHED;
223 void ACT_OT_view_all (wmOperatorType *ot)
226 ot->name= "View All";
227 ot->idname= "ACT_OT_view_all";
230 ot->exec= actkeys_viewall_exec;
231 ot->poll= ED_operator_areaactive;
234 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
237 /* ************************************************************************** */
240 /* ******************** Copy/Paste Keyframes Operator ************************* */
241 /* NOTE: the backend code for this is shared with the graph editor */
243 static short copy_action_keys (bAnimContext *ac)
245 ListBase anim_data = {NULL, NULL};
248 /* clear buffer first */
252 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY);
253 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
256 ok= copy_animedit_keys(ac, &anim_data);
259 BLI_freelistN(&anim_data);
265 static short paste_action_keys (bAnimContext *ac)
267 ListBase anim_data = {NULL, NULL};
271 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
272 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
274 /* paste keyframes */
275 ok= paste_animedit_keys(ac, &anim_data);
278 BLI_freelistN(&anim_data);
283 /* ------------------- */
285 static int actkeys_copy_exec(bContext *C, wmOperator *op)
289 /* get editor data */
290 if (ANIM_animdata_get_context(C, &ac) == 0)
291 return OPERATOR_CANCELLED;
294 if (ac.datatype == ANIMCONT_GPENCIL) {
298 if (copy_action_keys(&ac)) {
299 BKE_report(op->reports, RPT_ERROR, "No keyframes copied to keyframes copy/paste buffer");
300 return OPERATOR_CANCELLED;
304 /* set notifier that things have changed */
305 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
307 return OPERATOR_FINISHED;
310 void ACT_OT_keyframes_copy (wmOperatorType *ot)
313 ot->name= "Copy Keyframes";
314 ot->idname= "ACT_OT_keyframes_copy";
317 ot->exec= actkeys_copy_exec;
318 ot->poll= ED_operator_areaactive;
321 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
326 static int actkeys_paste_exec(bContext *C, wmOperator *op)
330 /* get editor data */
331 if (ANIM_animdata_get_context(C, &ac) == 0)
332 return OPERATOR_CANCELLED;
334 /* paste keyframes */
335 if (ac.datatype == ANIMCONT_GPENCIL) {
339 if (paste_action_keys(&ac)) {
340 BKE_report(op->reports, RPT_ERROR, "No keyframes to paste");
341 return OPERATOR_CANCELLED;
345 /* validate keyframes after editing */
346 ANIM_editkeyframes_refresh(&ac);
348 /* set notifier that things have changed */
349 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
351 return OPERATOR_FINISHED;
354 void ACT_OT_keyframes_paste (wmOperatorType *ot)
357 ot->name= "Paste Keyframes";
358 ot->idname= "ACT_OT_keyframes_paste";
361 ot->exec= actkeys_paste_exec;
362 ot->poll= ED_operator_areaactive;
365 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
368 /* ******************** Insert Keyframes Operator ************************* */
370 /* defines for insert keyframes tool */
371 EnumPropertyItem prop_actkeys_insertkey_types[] = {
372 {1, "ALL", "All Channels", ""},
373 {2, "SEL", "Only Selected Channels", ""},
374 {3, "GROUP", "In Active Group", ""}, // xxx not in all cases
375 {0, NULL, NULL, NULL}
378 /* this function is responsible for snapping keyframes to frame-times */
379 static void insert_action_keys(bAnimContext *ac, short mode)
381 ListBase anim_data = {NULL, NULL};
385 Scene *scene= ac->scene;
386 float cfra= (float)CFRA;
390 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
391 if (mode == 2) filter |= ANIMFILTER_SEL;
392 else if (mode == 3) filter |= ANIMFILTER_ACTGROUPED;
394 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
396 /* init keyframing flag */
397 if (IS_AUTOKEY_FLAG(AUTOMATKEY)) flag |= INSERTKEY_MATRIX;
398 if (IS_AUTOKEY_FLAG(INSERTNEEDED)) flag |= INSERTKEY_NEEDED;
399 // if (IS_AUTOKEY_MODE(EDITKEYS)) flag |= INSERTKEY_REPLACE;
401 /* insert keyframes */
402 for (ale= anim_data.first; ale; ale= ale->next) {
403 //Object *nob= ANIM_nla_mapping_get(ac, ale);
404 FCurve *fcu= (FCurve *)ale->key_data;
406 /* adjust current frame for NLA-scaling */
408 // cfra= get_action_frame(nob, CFRA);
410 // cfra= (float)CFRA;
412 /* if there's an id */
414 insert_keyframe(ale->id, NULL, ((fcu->grp)?(fcu->grp->name):(NULL)), fcu->rna_path, fcu->array_index, cfra, flag);
416 insert_vert_fcurve(fcu, cfra, fcu->curval, 0);
419 BLI_freelistN(&anim_data);
422 /* ------------------- */
424 static int actkeys_insertkey_exec(bContext *C, wmOperator *op)
429 /* get editor data */
430 if (ANIM_animdata_get_context(C, &ac) == 0)
431 return OPERATOR_CANCELLED;
432 if (ac.datatype == ANIMCONT_GPENCIL)
433 return OPERATOR_CANCELLED;
435 /* get snapping mode */
436 mode= RNA_enum_get(op->ptr, "type");
439 insert_action_keys(&ac, mode);
441 /* validate keyframes after editing */
442 ANIM_editkeyframes_refresh(&ac);
444 /* set notifier that things have changed */
445 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
447 return OPERATOR_FINISHED;
450 void ACT_OT_keyframes_insert (wmOperatorType *ot)
453 ot->name= "Insert Keyframes";
454 ot->idname= "ACT_OT_keyframes_insert";
457 ot->invoke= WM_menu_invoke;
458 ot->exec= actkeys_insertkey_exec;
459 ot->poll= ED_operator_areaactive;
462 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
465 RNA_def_enum(ot->srna, "type", prop_actkeys_insertkey_types, 0, "Type", "");
468 /* ******************** Duplicate Keyframes Operator ************************* */
470 static void duplicate_action_keys (bAnimContext *ac)
472 ListBase anim_data = {NULL, NULL};
477 if (ac->datatype == ANIMCONT_GPENCIL)
478 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT);
480 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
481 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
483 /* loop through filtered data and delete selected keys */
484 for (ale= anim_data.first; ale; ale= ale->next) {
485 //if (ale->type == ANIMTYPE_GPLAYER)
486 // delete_gplayer_frames((bGPDlayer *)ale->data);
488 duplicate_fcurve_keys((FCurve *)ale->key_data);
491 /* free filtered list */
492 BLI_freelistN(&anim_data);
495 /* ------------------- */
497 static int actkeys_duplicate_exec(bContext *C, wmOperator *op)
501 /* get editor data */
502 if (ANIM_animdata_get_context(C, &ac) == 0)
503 return OPERATOR_CANCELLED;
505 /* duplicate keyframes */
506 duplicate_action_keys(&ac);
508 /* validate keyframes after editing */
509 ANIM_editkeyframes_refresh(&ac);
511 /* set notifier that things have changed */
512 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
514 return OPERATOR_FINISHED; // xxx - start transform
517 static int actkeys_duplicate_invoke(bContext *C, wmOperator *op, wmEvent *event)
519 actkeys_duplicate_exec(C, op);
521 RNA_int_set(op->ptr, "mode", TFM_TIME_TRANSLATE);
522 WM_operator_name_call(C, "TFM_OT_transform", WM_OP_INVOKE_REGION_WIN, op->ptr);
524 return OPERATOR_FINISHED;
527 void ACT_OT_keyframes_duplicate (wmOperatorType *ot)
530 ot->name= "Duplicate Keyframes";
531 ot->idname= "ACT_OT_keyframes_duplicate";
534 ot->invoke= actkeys_duplicate_invoke;
535 ot->exec= actkeys_duplicate_exec;
536 ot->poll= ED_operator_areaactive;
539 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
541 /* to give to transform */
542 RNA_def_int(ot->srna, "mode", TFM_TIME_TRANSLATE, 0, INT_MAX, "Mode", "", 0, INT_MAX);
545 /* ******************** Delete Keyframes Operator ************************* */
547 static void delete_action_keys (bAnimContext *ac)
549 ListBase anim_data = {NULL, NULL};
554 if (ac->datatype == ANIMCONT_GPENCIL)
555 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT);
557 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
558 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
560 /* loop through filtered data and delete selected keys */
561 for (ale= anim_data.first; ale; ale= ale->next) {
562 //if (ale->type == ANIMTYPE_GPLAYER)
563 // delete_gplayer_frames((bGPDlayer *)ale->data);
565 delete_fcurve_keys((FCurve *)ale->key_data); // XXX... this doesn't delete empty curves anymore
568 /* free filtered list */
569 BLI_freelistN(&anim_data);
572 /* ------------------- */
574 static int actkeys_delete_exec(bContext *C, wmOperator *op)
578 /* get editor data */
579 if (ANIM_animdata_get_context(C, &ac) == 0)
580 return OPERATOR_CANCELLED;
582 /* delete keyframes */
583 delete_action_keys(&ac);
585 /* validate keyframes after editing */
586 ANIM_editkeyframes_refresh(&ac);
588 /* set notifier that things have changed */
589 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
591 return OPERATOR_FINISHED;
594 void ACT_OT_keyframes_delete (wmOperatorType *ot)
597 ot->name= "Delete Keyframes";
598 ot->idname= "ACT_OT_keyframes_delete";
601 ot->invoke= WM_operator_confirm;
602 ot->exec= actkeys_delete_exec;
603 ot->poll= ED_operator_areaactive;
606 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
609 /* ******************** Clean Keyframes Operator ************************* */
611 static void clean_action_keys (bAnimContext *ac, float thresh)
613 ListBase anim_data = {NULL, NULL};
618 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_SEL | ANIMFILTER_CURVESONLY);
619 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
621 /* loop through filtered data and clean curves */
622 for (ale= anim_data.first; ale; ale= ale->next)
623 clean_fcurve((FCurve *)ale->key_data, thresh);
626 BLI_freelistN(&anim_data);
629 /* ------------------- */
631 static int actkeys_clean_exec(bContext *C, wmOperator *op)
636 /* get editor data */
637 if (ANIM_animdata_get_context(C, &ac) == 0)
638 return OPERATOR_CANCELLED;
639 if (ac.datatype == ANIMCONT_GPENCIL)
640 return OPERATOR_PASS_THROUGH;
642 /* get cleaning threshold */
643 thresh= RNA_float_get(op->ptr, "threshold");
645 /* clean keyframes */
646 clean_action_keys(&ac, thresh);
648 /* validate keyframes after editing */
649 ANIM_editkeyframes_refresh(&ac);
651 /* set notifier that things have changed */
652 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
654 return OPERATOR_FINISHED;
657 void ACT_OT_keyframes_clean (wmOperatorType *ot)
660 ot->name= "Clean Keyframes";
661 ot->idname= "ACT_OT_keyframes_clean";
664 //ot->invoke= // XXX we need that number popup for this!
665 ot->exec= actkeys_clean_exec;
666 ot->poll= ED_operator_areaactive;
669 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
672 RNA_def_float(ot->srna, "threshold", 0.001f, 0.0f, FLT_MAX, "Threshold", "", 0.0f, 1000.0f);
675 /* ******************** Sample Keyframes Operator *********************** */
677 /* little cache for values... */
678 typedef struct tempFrameValCache {
682 /* Evaluates the curves between each selected keyframe on each frame, and keys the value */
683 static void sample_action_keys (bAnimContext *ac)
685 ListBase anim_data = {NULL, NULL};
690 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
691 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
693 /* loop through filtered data and add keys between selected keyframes on every frame */
694 for (ale= anim_data.first; ale; ale= ale->next) {
695 FCurve *fcu= (FCurve *)ale->key_data;
696 BezTriple *bezt, *start=NULL, *end=NULL;
697 tempFrameValCache *value_cache, *fp;
701 /* find selected keyframes... once pair has been found, add keyframes */
702 for (i=0, bezt=fcu->bezt; i < fcu->totvert; i++, bezt++) {
703 /* check if selected, and which end this is */
704 if (BEZSELECTED(bezt)) {
709 /* cache values then add keyframes using these values, as adding
710 * keyframes while sampling will affect the outcome...
712 range= (int)( ceil(end->vec[1][0] - start->vec[1][0]) );
713 sfra= (int)( floor(start->vec[1][0]) );
716 value_cache= MEM_callocN(sizeof(tempFrameValCache)*range, "IcuFrameValCache");
719 for (n=0, fp=value_cache; n<range && fp; n++, fp++) {
720 fp->frame= (float)(sfra + n);
721 fp->val= evaluate_fcurve(fcu, fp->frame);
724 /* add keyframes with these */
725 for (n=0, fp=value_cache; n<range && fp; n++, fp++) {
726 insert_vert_fcurve(fcu, fp->frame, fp->val, 1);
729 /* free temp cache */
730 MEM_freeN(value_cache);
732 /* as we added keyframes, we need to compensate so that bezt is at the right place */
733 bezt = fcu->bezt + i + range - 1;
737 /* bezt was selected, so it now marks the start of a whole new chain to search */
742 /* just set start keyframe */
749 /* recalculate channel's handles? */
750 calchandles_fcurve(fcu);
753 /* admin and redraws */
754 BLI_freelistN(&anim_data);
757 /* ------------------- */
759 static int actkeys_sample_exec(bContext *C, wmOperator *op)
763 /* get editor data */
764 if (ANIM_animdata_get_context(C, &ac) == 0)
765 return OPERATOR_CANCELLED;
766 if (ac.datatype == ANIMCONT_GPENCIL)
767 return OPERATOR_PASS_THROUGH;
769 /* sample keyframes */
770 sample_action_keys(&ac);
772 /* validate keyframes after editing */
773 ANIM_editkeyframes_refresh(&ac);
775 /* set notifier that things have changed */
776 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
778 return OPERATOR_FINISHED;
781 void ACT_OT_keyframes_sample (wmOperatorType *ot)
784 ot->name= "Sample Keyframes";
785 ot->idname= "ACT_OT_keyframes_sample";
788 ot->exec= actkeys_sample_exec;
789 ot->poll= ED_operator_areaactive;
792 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
795 /* ************************************************************************** */
798 /* ******************** Set Extrapolation-Type Operator *********************** */
800 /* defines for set extrapolation-type for selected keyframes tool */
801 EnumPropertyItem prop_actkeys_expo_types[] = {
802 {FCURVE_EXTRAPOLATE_CONSTANT, "CONSTANT", "Constant Extrapolation", ""},
803 {FCURVE_EXTRAPOLATE_LINEAR, "LINEAR", "Linear Extrapolation", ""},
804 {0, NULL, NULL, NULL}
807 /* this function is responsible for setting extrapolation mode for keyframes */
808 static void setexpo_action_keys(bAnimContext *ac, short mode)
810 ListBase anim_data = {NULL, NULL};
815 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
816 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
818 /* loop through setting mode per F-Curve */
819 for (ale= anim_data.first; ale; ale= ale->next) {
820 FCurve *fcu= (FCurve *)ale->data;
825 BLI_freelistN(&anim_data);
828 /* ------------------- */
830 static int actkeys_expo_exec(bContext *C, wmOperator *op)
835 /* get editor data */
836 if (ANIM_animdata_get_context(C, &ac) == 0)
837 return OPERATOR_CANCELLED;
838 if (ac.datatype == ANIMCONT_GPENCIL)
839 return OPERATOR_PASS_THROUGH;
841 /* get handle setting mode */
842 mode= RNA_enum_get(op->ptr, "type");
844 /* set handle type */
845 setexpo_action_keys(&ac, mode);
847 /* validate keyframes after editing */
848 ANIM_editkeyframes_refresh(&ac);
850 /* set notifier that things have changed */
851 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
853 return OPERATOR_FINISHED;
856 void ACT_OT_keyframes_extrapolation_type_set (wmOperatorType *ot)
859 ot->name= "Set Keyframe Extrapolation";
860 ot->idname= "ACT_OT_keyframes_extrapolation_type_set";
863 ot->invoke= WM_menu_invoke;
864 ot->exec= actkeys_expo_exec;
865 ot->poll= ED_operator_areaactive;
868 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
871 RNA_def_enum(ot->srna, "type", prop_actkeys_expo_types, 0, "Type", "");
874 /* ******************** Set Interpolation-Type Operator *********************** */
876 /* this function is responsible for setting interpolation mode for keyframes */
877 static void setipo_action_keys(bAnimContext *ac, short mode)
879 ListBase anim_data = {NULL, NULL};
882 BeztEditFunc set_cb= ANIM_editkeyframes_ipo(mode);
885 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
886 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
888 /* loop through setting BezTriple interpolation
889 * Note: we do not supply BeztEditData to the looper yet. Currently that's not necessary here...
891 for (ale= anim_data.first; ale; ale= ale->next)
892 ANIM_fcurve_keys_bezier_loop(NULL, ale->key_data, NULL, set_cb, calchandles_fcurve);
895 BLI_freelistN(&anim_data);
898 /* ------------------- */
900 static int actkeys_ipo_exec(bContext *C, wmOperator *op)
905 /* get editor data */
906 if (ANIM_animdata_get_context(C, &ac) == 0)
907 return OPERATOR_CANCELLED;
908 if (ac.datatype == ANIMCONT_GPENCIL)
909 return OPERATOR_PASS_THROUGH;
911 /* get handle setting mode */
912 mode= RNA_enum_get(op->ptr, "type");
914 /* set handle type */
915 setipo_action_keys(&ac, mode);
917 /* validate keyframes after editing */
918 ANIM_editkeyframes_refresh(&ac);
920 /* set notifier that things have changed */
921 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
923 return OPERATOR_FINISHED;
926 void ACT_OT_keyframes_interpolation_type (wmOperatorType *ot)
929 ot->name= "Set Keyframe Interpolation";
930 ot->idname= "ACT_OT_keyframes_interpolation_type";
933 ot->invoke= WM_menu_invoke;
934 ot->exec= actkeys_ipo_exec;
935 ot->poll= ED_operator_areaactive;
938 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
941 RNA_def_enum(ot->srna, "type", beztriple_interpolation_mode_items, 0, "Type", "");
944 /* ******************** Set Handle-Type Operator *********************** */
946 /* this function is responsible for setting handle-type of selected keyframes */
947 static void sethandles_action_keys(bAnimContext *ac, short mode)
949 ListBase anim_data = {NULL, NULL};
952 BeztEditFunc set_cb= ANIM_editkeyframes_handles(mode);
955 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
956 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
958 /* loop through setting flags for handles
959 * Note: we do not supply BeztEditData to the looper yet. Currently that's not necessary here...
961 for (ale= anim_data.first; ale; ale= ale->next) {
963 BeztEditFunc toggle_cb;
965 /* check which type of handle to set (free or aligned)
966 * - check here checks for handles with free alignment already
968 if (ANIM_fcurve_keys_bezier_loop(NULL, ale->key_data, NULL, set_cb, NULL))
969 toggle_cb= ANIM_editkeyframes_handles(HD_FREE);
971 toggle_cb= ANIM_editkeyframes_handles(HD_ALIGN);
973 /* set handle-type */
974 ANIM_fcurve_keys_bezier_loop(NULL, ale->key_data, NULL, toggle_cb, calchandles_fcurve);
977 /* directly set handle-type */
978 ANIM_fcurve_keys_bezier_loop(NULL, ale->key_data, NULL, set_cb, calchandles_fcurve);
983 BLI_freelistN(&anim_data);
986 /* ------------------- */
988 static int actkeys_handletype_exec(bContext *C, wmOperator *op)
993 /* get editor data */
994 if (ANIM_animdata_get_context(C, &ac) == 0)
995 return OPERATOR_CANCELLED;
996 if (ac.datatype == ANIMCONT_GPENCIL)
997 return OPERATOR_PASS_THROUGH;
999 /* get handle setting mode */
1000 mode= RNA_enum_get(op->ptr, "type");
1002 /* set handle type */
1003 sethandles_action_keys(&ac, mode);
1005 /* validate keyframes after editing */
1006 ANIM_editkeyframes_refresh(&ac);
1008 /* set notifier that things have changed */
1009 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
1011 return OPERATOR_FINISHED;
1014 void ACT_OT_keyframes_handle_type_set (wmOperatorType *ot)
1017 ot->name= "Set Keyframe Handle Type";
1018 ot->idname= "ACT_OT_keyframes_handle_type_set";
1021 ot->invoke= WM_menu_invoke;
1022 ot->exec= actkeys_handletype_exec;
1023 ot->poll= ED_operator_areaactive;
1026 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1029 RNA_def_enum(ot->srna, "type", beztriple_handle_type_items, 0, "Type", "");
1032 /* ************************************************************************** */
1033 /* TRANSFORM STUFF */
1035 /* ***************** Snap Current Frame Operator *********************** */
1037 /* snap current-frame indicator to 'average time' of selected keyframe */
1038 static int actkeys_cfrasnap_exec(bContext *C, wmOperator *op)
1041 ListBase anim_data= {NULL, NULL};
1046 /* get editor data */
1047 if (ANIM_animdata_get_context(C, &ac) == 0)
1048 return OPERATOR_CANCELLED;
1050 /* init edit data */
1051 memset(&bed, 0, sizeof(BeztEditData));
1053 /* loop over action data, averaging values */
1054 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY);
1055 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1057 for (ale= anim_data.first; ale; ale= ale->next)
1058 ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, bezt_calc_average, NULL);
1060 BLI_freelistN(&anim_data);
1062 /* set the new current frame value, based on the average time */
1064 Scene *scene= ac.scene;
1065 CFRA= (int)floor((bed.f1 / bed.i1) + 0.5f);
1068 /* set notifier that things have changed */
1069 WM_event_add_notifier(C, NC_SCENE|ND_FRAME, ac.scene);
1071 return OPERATOR_FINISHED;
1074 void ACT_OT_keyframes_cfrasnap (wmOperatorType *ot)
1077 ot->name= "Snap Current Frame to Keys";
1078 ot->idname= "ACT_OT_keyframes_cfrasnap";
1081 ot->exec= actkeys_cfrasnap_exec;
1082 ot->poll= ED_operator_areaactive;
1085 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1088 /* ******************** Snap Keyframes Operator *********************** */
1090 /* defines for snap keyframes tool */
1091 EnumPropertyItem prop_actkeys_snap_types[] = {
1092 {ACTKEYS_SNAP_CFRA, "CFRA", "Current frame", ""},
1093 {ACTKEYS_SNAP_NEAREST_FRAME, "NEAREST_FRAME", "Nearest Frame", ""}, // XXX as single entry?
1094 {ACTKEYS_SNAP_NEAREST_SECOND, "NEAREST_SECOND", "Nearest Second", ""}, // XXX as single entry?
1095 {ACTKEYS_SNAP_NEAREST_MARKER, "NEAREST_MARKER", "Nearest Marker", ""},
1096 {0, NULL, NULL, NULL}
1099 /* this function is responsible for snapping keyframes to frame-times */
1100 static void snap_action_keys(bAnimContext *ac, short mode)
1102 ListBase anim_data = {NULL, NULL};
1107 BeztEditFunc edit_cb;
1110 if (ac->datatype == ANIMCONT_GPENCIL)
1111 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT);
1113 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
1114 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1116 /* get beztriple editing callbacks */
1117 edit_cb= ANIM_editkeyframes_snap(mode);
1119 memset(&bed, 0, sizeof(BeztEditData));
1120 bed.scene= ac->scene;
1122 /* snap keyframes */
1123 for (ale= anim_data.first; ale; ale= ale->next) {
1124 Object *nob= ANIM_nla_mapping_get(ac, ale);
1127 ANIM_nla_mapping_apply_fcurve(nob, ale->key_data, 0, 1);
1128 ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, edit_cb, calchandles_fcurve);
1129 ANIM_nla_mapping_apply_fcurve(nob, ale->key_data, 1, 1);
1131 //else if (ale->type == ACTTYPE_GPLAYER)
1132 // snap_gplayer_frames(ale->data, mode);
1134 ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, edit_cb, calchandles_fcurve);
1136 BLI_freelistN(&anim_data);
1139 /* ------------------- */
1141 static int actkeys_snap_exec(bContext *C, wmOperator *op)
1146 /* get editor data */
1147 if (ANIM_animdata_get_context(C, &ac) == 0)
1148 return OPERATOR_CANCELLED;
1150 /* get snapping mode */
1151 mode= RNA_enum_get(op->ptr, "type");
1153 /* snap keyframes */
1154 snap_action_keys(&ac, mode);
1156 /* validate keyframes after editing */
1157 ANIM_editkeyframes_refresh(&ac);
1159 /* set notifier that things have changed */
1160 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
1162 return OPERATOR_FINISHED;
1165 void ACT_OT_keyframes_snap (wmOperatorType *ot)
1168 ot->name= "Snap Keys";
1169 ot->idname= "ACT_OT_keyframes_snap";
1172 ot->invoke= WM_menu_invoke;
1173 ot->exec= actkeys_snap_exec;
1174 ot->poll= ED_operator_areaactive;
1177 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1180 RNA_def_enum(ot->srna, "type", prop_actkeys_snap_types, 0, "Type", "");
1183 /* ******************** Mirror Keyframes Operator *********************** */
1185 /* defines for mirror keyframes tool */
1186 EnumPropertyItem prop_actkeys_mirror_types[] = {
1187 {ACTKEYS_MIRROR_CFRA, "CFRA", "Current frame", ""},
1188 {ACTKEYS_MIRROR_YAXIS, "YAXIS", "Vertical Axis", ""},
1189 {ACTKEYS_MIRROR_XAXIS, "XAXIS", "Horizontal Axis", ""},
1190 {ACTKEYS_MIRROR_MARKER, "MARKER", "First Selected Marker", ""},
1191 {0, NULL, NULL, NULL}
1194 /* this function is responsible for mirroring keyframes */
1195 static void mirror_action_keys(bAnimContext *ac, short mode)
1197 ListBase anim_data = {NULL, NULL};
1202 BeztEditFunc edit_cb;
1204 /* get beztriple editing callbacks */
1205 edit_cb= ANIM_editkeyframes_mirror(mode);
1207 memset(&bed, 0, sizeof(BeztEditData));
1208 bed.scene= ac->scene;
1210 /* for 'first selected marker' mode, need to find first selected marker first! */
1211 // XXX should this be made into a helper func in the API?
1212 if (mode == ACTKEYS_MIRROR_MARKER) {
1213 Scene *scene= ac->scene;
1214 TimeMarker *marker= NULL;
1216 /* find first selected marker */
1217 for (marker= scene->markers.first; marker; marker=marker->next) {
1218 if (marker->flag & SELECT) {
1223 /* store marker's time (if available) */
1225 bed.f1= (float)marker->frame;
1231 if (ac->datatype == ANIMCONT_GPENCIL)
1232 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT);
1234 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
1235 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1237 /* mirror keyframes */
1238 for (ale= anim_data.first; ale; ale= ale->next) {
1239 Object *nob= ANIM_nla_mapping_get(ac, ale);
1242 ANIM_nla_mapping_apply_fcurve(nob, ale->key_data, 0, 1);
1243 ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, edit_cb, calchandles_fcurve);
1244 ANIM_nla_mapping_apply_fcurve(nob, ale->key_data, 1, 1);
1246 //else if (ale->type == ACTTYPE_GPLAYER)
1247 // snap_gplayer_frames(ale->data, mode);
1249 ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, edit_cb, calchandles_fcurve);
1251 BLI_freelistN(&anim_data);
1254 /* ------------------- */
1256 static int actkeys_mirror_exec(bContext *C, wmOperator *op)
1261 /* get editor data */
1262 if (ANIM_animdata_get_context(C, &ac) == 0)
1263 return OPERATOR_CANCELLED;
1265 /* get mirroring mode */
1266 mode= RNA_enum_get(op->ptr, "type");
1268 /* mirror keyframes */
1269 mirror_action_keys(&ac, mode);
1271 /* validate keyframes after editing */
1272 ANIM_editkeyframes_refresh(&ac);
1274 /* set notifier that things have changed */
1275 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
1277 return OPERATOR_FINISHED;
1280 void ACT_OT_keyframes_mirror (wmOperatorType *ot)
1283 ot->name= "Mirror Keys";
1284 ot->idname= "ACT_OT_keyframes_mirror";
1287 ot->invoke= WM_menu_invoke;
1288 ot->exec= actkeys_mirror_exec;
1289 ot->poll= ED_operator_areaactive;
1292 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1295 RNA_def_enum(ot->srna, "type", prop_actkeys_mirror_types, 0, "Type", "");
1298 /* ************************************************************************** */