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 "graph_intern.h"
91 /* ************************************************************************** */
92 /* KEYFRAME-RANGE STUFF */
94 /* *************************** Calculate Range ************************** */
96 /* Get the min/max keyframes*/
97 static void get_graph_keyframe_extents (bAnimContext *ac, float *xmin, float *xmax, float *ymin, float *ymax)
99 ListBase anim_data = {NULL, NULL};
103 /* get data to filter, from Dopesheet */
104 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | 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 */
108 if (xmin) *xmin= 999999999.0f;
109 if (xmax) *xmax= -999999999.0f;
110 if (ymin) *ymin= 999999999.0f;
111 if (ymax) *ymax= -999999999.0f;
113 /* check if any channels to set range with */
114 if (anim_data.first) {
115 /* go through channels, finding max extents */
116 for (ale= anim_data.first; ale; ale= ale->next) {
117 Object *nob= NULL; //ANIM_nla_mapping_get(ac, ale);
118 FCurve *fcu= (FCurve *)ale->key_data;
121 /* get range and apply necessary scaling before */
122 calc_fcurve_bounds(fcu, &tmin, &tmax, ymin, ymax);
125 tmin= get_action_frame_inv(nob, tmin);
126 tmax= get_action_frame_inv(nob, tmax);
129 /* try to set cur using these values, if they're more extreme than previously set values */
130 if (xmin) *xmin= MIN2(*xmin, tmin);
131 if (xmax) *xmax= MAX2(*xmax, tmax);
135 BLI_freelistN(&anim_data);
138 /* set default range */
140 if (xmin) *xmin= (float)ac->scene->r.sfra;
141 if (xmax) *xmax= (float)ac->scene->r.efra;
145 if (xmax) *xmax= 100;
153 /* ****************** Automatic Preview-Range Operator ****************** */
155 static int graphkeys_previewrange_exec(bContext *C, wmOperator *op)
161 /* get editor data */
162 if (ANIM_animdata_get_context(C, &ac) == 0)
163 return OPERATOR_CANCELLED;
164 if (ac.scene == NULL)
165 return OPERATOR_CANCELLED;
169 /* set the range directly */
170 get_graph_keyframe_extents(&ac, &min, &max, NULL, NULL);
171 scene->r.psfra= (int)floor(min + 0.5f);
172 scene->r.pefra= (int)floor(max + 0.5f);
174 /* set notifier that things have changed */
175 // XXX err... there's nothing for frame ranges yet, but this should do fine too
176 WM_event_add_notifier(C, NC_SCENE|ND_FRAME, ac.scene);
178 return OPERATOR_FINISHED;
181 void GRAPHEDIT_OT_previewrange_set (wmOperatorType *ot)
184 ot->name= "Auto-Set Preview Range";
185 ot->idname= "GRAPHEDIT_OT_previewrange_set";
188 ot->exec= graphkeys_previewrange_exec;
189 ot->poll= ED_operator_areaactive;
192 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
195 /* ****************** View-All Operator ****************** */
197 static int graphkeys_viewall_exec(bContext *C, wmOperator *op)
203 /* get editor data */
204 if (ANIM_animdata_get_context(C, &ac) == 0)
205 return OPERATOR_CANCELLED;
208 /* set the horizontal range, with an extra offset so that the extreme keys will be in view */
209 get_graph_keyframe_extents(&ac, &v2d->cur.xmin, &v2d->cur.xmax, &v2d->cur.ymin, &v2d->cur.ymax);
211 extra= 0.1f * (v2d->cur.xmax - v2d->cur.xmin);
212 v2d->cur.xmin -= extra;
213 v2d->cur.xmax += extra;
215 extra= 0.1f * (v2d->cur.ymax - v2d->cur.ymin);
216 v2d->cur.ymin -= extra;
217 v2d->cur.ymax += extra;
219 /* do View2D syncing */
220 UI_view2d_sync(CTX_wm_screen(C), CTX_wm_area(C), v2d, V2D_LOCK_COPY);
222 /* set notifier that things have changed */
223 ED_area_tag_redraw(CTX_wm_area(C));
225 return OPERATOR_FINISHED;
228 void GRAPHEDIT_OT_view_all (wmOperatorType *ot)
231 ot->name= "View All";
232 ot->idname= "GRAPHEDIT_OT_view_all";
235 ot->exec= graphkeys_viewall_exec;
236 ot->poll= ED_operator_areaactive;
239 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
242 /* ******************** Create Ghost-Curves Operator *********************** */
243 /* This operator samples the data of the selected F-Curves to F-Points, storing them
244 * as 'ghost curves' in the active Graph Editor
247 /* Bake each F-Curve into a set of samples, and store as a ghost curve */
248 static void create_ghost_curves (bAnimContext *ac, int start, int end)
250 SpaceIpo *sipo= (SpaceIpo *)ac->sa->spacedata.first;
251 ListBase anim_data = {NULL, NULL};
255 /* free existing ghost curves */
256 free_fcurves(&sipo->ghostCurves);
260 printf("Error: Frame range for Ghost F-Curve creation is inappropriate \n");
265 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_SEL | ANIMFILTER_CURVESONLY);
266 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
268 /* loop through filtered data and add keys between selected keyframes on every frame */
269 for (ale= anim_data.first; ale; ale= ale->next) {
270 FCurve *fcu= (FCurve *)ale->key_data;
271 FCurve *gcu= MEM_callocN(sizeof(FCurve), "Ghost FCurve");
272 ChannelDriver *driver= fcu->driver;
276 /* disable driver so that it don't muck up the sampling process */
279 /* create samples, but store them in a new curve
280 * - we cannot use fcurve_store_samples() as that will only overwrite the original curve
282 gcu->fpt= fpt= MEM_callocN(sizeof(FPoint)*(end-start+1), "Ghost FPoint Samples");
283 gcu->totvert= end - start + 1;
285 /* use the sampling callback at 1-frame intervals from start to end frames */
286 for (cfra= start; cfra <= end; cfra++, fpt++) {
287 fpt->vec[0]= (float)cfra;
288 fpt->vec[1]= fcurve_samplingcb_evalcurve(fcu, NULL, (float)cfra);
291 /* set color of ghost curve
292 * - make the color slightly darker
294 gcu->color[0]= fcu->color[0] - 0.07f;
295 gcu->color[1]= fcu->color[1] - 0.07f;
296 gcu->color[2]= fcu->color[2] - 0.07f;
298 /* store new ghost curve */
299 BLI_addtail(&sipo->ghostCurves, gcu);
305 /* admin and redraws */
306 BLI_freelistN(&anim_data);
309 /* ------------------- */
311 static int graphkeys_create_ghostcurves_exec(bContext *C, wmOperator *op)
317 /* get editor data */
318 if (ANIM_animdata_get_context(C, &ac) == 0)
319 return OPERATOR_CANCELLED;
321 /* ghost curves are snapshots of the visible portions of the curves, so set range to be the visible range */
323 start= (int)v2d->cur.xmin;
324 end= (int)v2d->cur.xmax;
326 /* bake selected curves into a ghost curve */
327 create_ghost_curves(&ac, start, end);
329 /* update this editor only */
330 ED_area_tag_redraw(CTX_wm_area(C));
332 return OPERATOR_FINISHED;
335 void GRAPHEDIT_OT_ghost_curves_create (wmOperatorType *ot)
338 ot->name= "Create Ghost Curves";
339 ot->idname= "GRAPHEDIT_OT_ghost_curves_create";
340 ot->description= "Create snapshot (Ghosts) of selected F-Curves as background aid for active Graph Editor.";
343 ot->exec= graphkeys_create_ghostcurves_exec;
344 ot->poll= ED_operator_areaactive;
347 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
349 // todo: add props for start/end frames
352 /* ******************** Clear Ghost-Curves Operator *********************** */
353 /* This operator clears the 'ghost curves' for the active Graph Editor */
355 static int graphkeys_clear_ghostcurves_exec(bContext *C, wmOperator *op)
360 /* get editor data */
361 if (ANIM_animdata_get_context(C, &ac) == 0)
362 return OPERATOR_CANCELLED;
363 sipo= (SpaceIpo *)ac.sa->spacedata.first;
365 /* if no ghost curves, don't do anything */
366 if (sipo->ghostCurves.first == NULL)
367 return OPERATOR_CANCELLED;
369 /* free ghost curves */
370 free_fcurves(&sipo->ghostCurves);
372 /* update this editor only */
373 ED_area_tag_redraw(CTX_wm_area(C));
375 return OPERATOR_FINISHED;
378 void GRAPHEDIT_OT_ghost_curves_clear (wmOperatorType *ot)
381 ot->name= "Create Ghost Curves";
382 ot->idname= "GRAPHEDIT_OT_ghost_curves_clear";
383 ot->description= "Clear F-Curve snapshots (Ghosts) for active Graph Editor.";
386 ot->exec= graphkeys_clear_ghostcurves_exec;
387 ot->poll= ED_operator_areaactive;
390 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
393 /* ************************************************************************** */
398 /* ******************** Click-Insert Keyframes Operator ************************* */
400 static int graphkeys_click_insert_exec (bContext *C, wmOperator *op)
406 /* get animation context */
407 if (ANIM_animdata_get_context(C, &ac) == 0)
408 return OPERATOR_CANCELLED;
410 /* get active F-Curve 'anim-list-element' */
411 ale= get_active_fcurve_channel(&ac);
412 if (ELEM(NULL, ale, ale->data)) {
413 if (ale) MEM_freeN(ale);
414 return OPERATOR_CANCELLED;
417 /* get frame and value from props */
418 frame= RNA_float_get(op->ptr, "frame");
419 val= RNA_float_get(op->ptr, "value");
421 /* insert keyframe on the specified frame + value */
422 insert_vert_fcurve((FCurve *)ale->data, frame, val, 0);
427 /* set notifier that things have changed */
428 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
431 return OPERATOR_FINISHED;
434 static int graphkeys_click_insert_invoke (bContext *C, wmOperator *op, wmEvent *evt)
442 /* get animation context */
443 if (ANIM_animdata_get_context(C, &ac) == 0)
444 return OPERATOR_CANCELLED;
446 /* store mouse coordinates in View2D space, into the operator's properties */
450 mval[0]= (evt->x - ar->winrct.xmin);
451 mval[1]= (evt->y - ar->winrct.ymin);
453 UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, &y);
455 RNA_float_set(op->ptr, "frame", x);
456 RNA_float_set(op->ptr, "value", y);
459 return graphkeys_click_insert_exec(C, op);
462 void GRAPHEDIT_OT_keyframes_click_insert (wmOperatorType *ot)
465 ot->name= "Click-Insert Keyframes";
466 ot->idname= "GRAPHEDIT_OT_keyframes_click_insert";
469 ot->invoke= graphkeys_click_insert_invoke;
470 ot->exec= graphkeys_click_insert_exec;
471 ot->poll= ED_operator_areaactive; // XXX active + editable poll
474 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
477 RNA_def_float(ot->srna, "frame", 1.0f, -FLT_MAX, FLT_MAX, "Frame Number", "Frame to insert keyframe on", 0, 100);
478 RNA_def_float(ot->srna, "value", 1.0f, -FLT_MAX, FLT_MAX, "Value", "Value for keyframe on", 0, 100);
481 /* ******************** Copy/Paste Keyframes Operator ************************* */
482 /* NOTE: the backend code for this is shared with the dopesheet editor */
484 static short copy_graph_keys (bAnimContext *ac)
486 ListBase anim_data = {NULL, NULL};
489 /* clear buffer first */
493 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY);
494 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
497 ok= copy_animedit_keys(ac, &anim_data);
500 BLI_freelistN(&anim_data);
505 static short paste_graph_keys (bAnimContext *ac)
507 ListBase anim_data = {NULL, NULL};
511 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_SEL | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
512 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
514 /* paste keyframes */
515 ok= paste_animedit_keys(ac, &anim_data);
518 BLI_freelistN(&anim_data);
523 /* ------------------- */
525 static int graphkeys_copy_exec(bContext *C, wmOperator *op)
529 /* get editor data */
530 if (ANIM_animdata_get_context(C, &ac) == 0)
531 return OPERATOR_CANCELLED;
534 if (copy_graph_keys(&ac)) {
535 BKE_report(op->reports, RPT_ERROR, "No keyframes copied to keyframes copy/paste buffer");
536 return OPERATOR_CANCELLED;
539 /* set notifier that things have changed */
540 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
542 return OPERATOR_FINISHED;
545 void GRAPHEDIT_OT_keyframes_copy (wmOperatorType *ot)
548 ot->name= "Copy Keyframes";
549 ot->idname= "GRAPHEDIT_OT_keyframes_copy";
552 ot->exec= graphkeys_copy_exec;
553 ot->poll= ED_operator_areaactive;
556 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
561 static int graphkeys_paste_exec(bContext *C, wmOperator *op)
565 /* get editor data */
566 if (ANIM_animdata_get_context(C, &ac) == 0)
567 return OPERATOR_CANCELLED;
569 /* paste keyframes */
570 if (paste_graph_keys(&ac)) {
571 BKE_report(op->reports, RPT_ERROR, "No keyframes to paste");
572 return OPERATOR_CANCELLED;
575 /* validate keyframes after editing */
576 ANIM_editkeyframes_refresh(&ac);
578 /* set notifier that things have changed */
579 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
581 return OPERATOR_FINISHED;
584 void GRAPHEDIT_OT_keyframes_paste (wmOperatorType *ot)
587 ot->name= "Paste Keyframes";
588 ot->idname= "GRAPHEDIT_OT_keyframes_paste";
591 ot->exec= graphkeys_paste_exec;
592 ot->poll= ED_operator_areaactive;
595 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
598 /* ******************** Duplicate Keyframes Operator ************************* */
600 static void duplicate_graph_keys (bAnimContext *ac)
602 ListBase anim_data = {NULL, NULL};
607 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE| ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
608 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
610 /* loop through filtered data and delete selected keys */
611 for (ale= anim_data.first; ale; ale= ale->next) {
612 duplicate_fcurve_keys((FCurve *)ale->key_data);
615 /* free filtered list */
616 BLI_freelistN(&anim_data);
619 /* ------------------- */
621 static int graphkeys_duplicate_exec(bContext *C, wmOperator *op)
625 /* get editor data */
626 if (ANIM_animdata_get_context(C, &ac) == 0)
627 return OPERATOR_CANCELLED;
629 /* duplicate keyframes */
630 duplicate_graph_keys(&ac);
632 /* validate keyframes after editing */
633 ANIM_editkeyframes_refresh(&ac);
635 /* set notifier that things have changed */
636 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
638 return OPERATOR_FINISHED;
641 static int graphkeys_duplicate_invoke(bContext *C, wmOperator *op, wmEvent *event)
643 graphkeys_duplicate_exec(C, op);
645 RNA_int_set(op->ptr, "mode", TFM_TRANSLATION);
646 WM_operator_name_call(C, "TFM_OT_transform", WM_OP_INVOKE_REGION_WIN, op->ptr);
648 return OPERATOR_FINISHED;
651 void GRAPHEDIT_OT_keyframes_duplicate (wmOperatorType *ot)
654 ot->name= "Duplicate Keyframes";
655 ot->idname= "GRAPHEDIT_OT_keyframes_duplicate";
658 ot->invoke= graphkeys_duplicate_invoke;
659 ot->exec= graphkeys_duplicate_exec;
660 ot->poll= ED_operator_areaactive;
663 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
665 /* to give to transform */
666 RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
669 /* ******************** Delete Keyframes Operator ************************* */
671 static void delete_graph_keys (bAnimContext *ac)
673 ListBase anim_data = {NULL, NULL};
678 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE| ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
679 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
681 /* loop through filtered data and delete selected keys */
682 for (ale= anim_data.first; ale; ale= ale->next) {
683 delete_fcurve_keys((FCurve *)ale->key_data); // XXX... this doesn't delete empty curves anymore
686 /* free filtered list */
687 BLI_freelistN(&anim_data);
690 /* ------------------- */
692 static int graphkeys_delete_exec(bContext *C, wmOperator *op)
696 /* get editor data */
697 if (ANIM_animdata_get_context(C, &ac) == 0)
698 return OPERATOR_CANCELLED;
700 /* delete keyframes */
701 delete_graph_keys(&ac);
703 /* validate keyframes after editing */
704 ANIM_editkeyframes_refresh(&ac);
706 /* set notifier that things have changed */
707 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
709 return OPERATOR_FINISHED;
712 void GRAPHEDIT_OT_keyframes_delete (wmOperatorType *ot)
715 ot->name= "Delete Keyframes";
716 ot->idname= "GRAPHEDIT_OT_keyframes_delete";
719 ot->invoke= WM_operator_confirm;
720 ot->exec= graphkeys_delete_exec;
721 ot->poll= ED_operator_areaactive;
724 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
727 /* ******************** Clean Keyframes Operator ************************* */
729 static void clean_graph_keys (bAnimContext *ac, float thresh)
731 ListBase anim_data = {NULL, NULL};
736 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE| ANIMFILTER_FOREDIT | ANIMFILTER_SEL | ANIMFILTER_CURVESONLY);
737 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
739 /* loop through filtered data and clean curves */
740 for (ale= anim_data.first; ale; ale= ale->next)
741 clean_fcurve((FCurve *)ale->key_data, thresh);
744 BLI_freelistN(&anim_data);
747 /* ------------------- */
749 static int graphkeys_clean_exec(bContext *C, wmOperator *op)
754 /* get editor data */
755 if (ANIM_animdata_get_context(C, &ac) == 0)
756 return OPERATOR_CANCELLED;
758 /* get cleaning threshold */
759 thresh= RNA_float_get(op->ptr, "threshold");
761 /* clean keyframes */
762 clean_graph_keys(&ac, thresh);
764 /* validate keyframes after editing */
765 ANIM_editkeyframes_refresh(&ac);
767 /* set notifier that things have changed */
768 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
770 return OPERATOR_FINISHED;
773 void GRAPHEDIT_OT_keyframes_clean (wmOperatorType *ot)
776 ot->name= "Clean Keyframes";
777 ot->idname= "GRAPHEDIT_OT_keyframes_clean";
780 //ot->invoke= // XXX we need that number popup for this!
781 ot->exec= graphkeys_clean_exec;
782 ot->poll= ED_operator_areaactive;
785 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
788 RNA_def_float(ot->srna, "threshold", 0.001f, 0.0f, FLT_MAX, "Threshold", "", 0.0f, 1000.0f);
791 /* ******************** Bake F-Curve Operator *********************** */
792 /* This operator bakes the data of the selected F-Curves to F-Points */
794 /* Bake each F-Curve into a set of samples */
795 static void bake_graph_curves (bAnimContext *ac, int start, int end)
797 ListBase anim_data = {NULL, NULL};
802 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_SEL | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
803 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
805 /* loop through filtered data and add keys between selected keyframes on every frame */
806 for (ale= anim_data.first; ale; ale= ale->next) {
807 FCurve *fcu= (FCurve *)ale->key_data;
808 ChannelDriver *driver= fcu->driver;
810 /* disable driver so that it don't muck up the sampling process */
814 fcurve_store_samples(fcu, NULL, start, end, fcurve_samplingcb_evalcurve);
820 /* admin and redraws */
821 BLI_freelistN(&anim_data);
824 /* ------------------- */
826 static int graphkeys_bake_exec(bContext *C, wmOperator *op)
832 /* get editor data */
833 if (ANIM_animdata_get_context(C, &ac) == 0)
834 return OPERATOR_CANCELLED;
836 /* for now, init start/end from preview-range extents */
837 // TODO: add properties for this
843 bake_graph_curves(&ac, start, end);
845 /* validate keyframes after editing */
846 ANIM_editkeyframes_refresh(&ac);
848 /* set notifier that things have changed */
849 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
851 return OPERATOR_FINISHED;
854 void GRAPHEDIT_OT_keyframes_bake (wmOperatorType *ot)
857 ot->name= "Bake Curve";
858 ot->idname= "GRAPHEDIT_OT_keyframes_bake";
861 ot->invoke= WM_operator_confirm; // FIXME...
862 ot->exec= graphkeys_bake_exec;
863 ot->poll= ED_operator_areaactive;
866 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
868 // todo: add props for start/end frames
871 /* ******************** Sample Keyframes Operator *********************** */
872 /* This operator 'bakes' the values of the curve into new keyframes between pairs
873 * of selected keyframes. It is useful for creating keyframes for tweaking overlap.
876 // XXX some of the common parts (with DopeSheet) should be unified in animation module...
878 /* little cache for values... */
879 typedef struct tempFrameValCache {
883 /* Evaluates the curves between each selected keyframe on each frame, and keys the value */
884 static void sample_graph_keys (bAnimContext *ac)
886 ListBase anim_data = {NULL, NULL};
891 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE| ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
892 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
894 /* loop through filtered data and add keys between selected keyframes on every frame */
895 for (ale= anim_data.first; ale; ale= ale->next) {
896 FCurve *fcu= (FCurve *)ale->key_data;
897 BezTriple *bezt, *start=NULL, *end=NULL;
898 tempFrameValCache *value_cache, *fp;
902 /* find selected keyframes... once pair has been found, add keyframes */
903 for (i=0, bezt=fcu->bezt; i < fcu->totvert; i++, bezt++) {
904 /* check if selected, and which end this is */
905 if (BEZSELECTED(bezt)) {
910 /* cache values then add keyframes using these values, as adding
911 * keyframes while sampling will affect the outcome...
913 range= (int)( ceil(end->vec[1][0] - start->vec[1][0]) );
914 sfra= (int)( floor(start->vec[1][0]) );
917 value_cache= MEM_callocN(sizeof(tempFrameValCache)*range, "IcuFrameValCache");
920 for (n=0, fp=value_cache; n<range && fp; n++, fp++) {
921 fp->frame= (float)(sfra + n);
922 fp->val= evaluate_fcurve(fcu, fp->frame);
925 /* add keyframes with these */
926 for (n=0, fp=value_cache; n<range && fp; n++, fp++) {
927 insert_vert_fcurve(fcu, fp->frame, fp->val, 1);
930 /* free temp cache */
931 MEM_freeN(value_cache);
933 /* as we added keyframes, we need to compensate so that bezt is at the right place */
934 bezt = fcu->bezt + i + range - 1;
938 /* bezt was selected, so it now marks the start of a whole new chain to search */
943 /* just set start keyframe */
950 /* recalculate channel's handles? */
951 calchandles_fcurve(fcu);
954 /* admin and redraws */
955 BLI_freelistN(&anim_data);
958 /* ------------------- */
960 static int graphkeys_sample_exec(bContext *C, wmOperator *op)
964 /* get editor data */
965 if (ANIM_animdata_get_context(C, &ac) == 0)
966 return OPERATOR_CANCELLED;
968 /* sample keyframes */
969 sample_graph_keys(&ac);
971 /* validate keyframes after editing */
972 ANIM_editkeyframes_refresh(&ac);
974 /* set notifier that things have changed */
975 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
977 return OPERATOR_FINISHED;
980 void GRAPHEDIT_OT_keyframes_sample (wmOperatorType *ot)
983 ot->name= "Sample Keyframes";
984 ot->idname= "GRAPHEDIT_OT_keyframes_sample";
987 ot->exec= graphkeys_sample_exec;
988 ot->poll= ED_operator_areaactive;
991 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
995 /* ************************************************************************** */
998 /* ******************** Set Extrapolation-Type Operator *********************** */
1000 /* defines for set extrapolation-type for selected keyframes tool */
1001 EnumPropertyItem prop_graphkeys_expo_types[] = {
1002 {FCURVE_EXTRAPOLATE_CONSTANT, "CONSTANT", "Constant Extrapolation", ""},
1003 {FCURVE_EXTRAPOLATE_LINEAR, "LINEAR", "Linear Extrapolation", ""},
1004 {0, NULL, NULL, NULL}
1007 /* this function is responsible for setting extrapolation mode for keyframes */
1008 static void setexpo_graph_keys(bAnimContext *ac, short mode)
1010 ListBase anim_data = {NULL, NULL};
1015 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE| ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
1016 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1018 /* loop through setting mode per F-Curve */
1019 for (ale= anim_data.first; ale; ale= ale->next) {
1020 FCurve *fcu= (FCurve *)ale->data;
1025 BLI_freelistN(&anim_data);
1028 /* ------------------- */
1030 static int graphkeys_expo_exec(bContext *C, wmOperator *op)
1035 /* get editor data */
1036 if (ANIM_animdata_get_context(C, &ac) == 0)
1037 return OPERATOR_CANCELLED;
1039 /* get handle setting mode */
1040 mode= RNA_enum_get(op->ptr, "type");
1042 /* set handle type */
1043 setexpo_graph_keys(&ac, mode);
1045 /* validate keyframes after editing */
1046 ANIM_editkeyframes_refresh(&ac);
1048 /* set notifier that things have changed */
1049 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
1051 return OPERATOR_FINISHED;
1054 void GRAPHEDIT_OT_keyframes_extrapolation_type (wmOperatorType *ot)
1057 ot->name= "Set Keyframe Extrapolation";
1058 ot->idname= "GRAPHEDIT_OT_keyframes_extrapolation_type";
1061 ot->invoke= WM_menu_invoke;
1062 ot->exec= graphkeys_expo_exec;
1063 ot->poll= ED_operator_areaactive;
1066 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1069 RNA_def_enum(ot->srna, "type", prop_graphkeys_expo_types, 0, "Type", "");
1072 /* ******************** Set Interpolation-Type Operator *********************** */
1074 /* this function is responsible for setting interpolation mode for keyframes */
1075 static void setipo_graph_keys(bAnimContext *ac, short mode)
1077 ListBase anim_data = {NULL, NULL};
1080 BeztEditFunc set_cb= ANIM_editkeyframes_ipo(mode);
1083 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE| ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
1084 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1086 /* loop through setting BezTriple interpolation
1087 * Note: we do not supply BeztEditData to the looper yet. Currently that's not necessary here...
1089 for (ale= anim_data.first; ale; ale= ale->next)
1090 ANIM_fcurve_keys_bezier_loop(NULL, ale->key_data, NULL, set_cb, calchandles_fcurve);
1093 BLI_freelistN(&anim_data);
1096 /* ------------------- */
1098 static int graphkeys_ipo_exec(bContext *C, wmOperator *op)
1103 /* get editor data */
1104 if (ANIM_animdata_get_context(C, &ac) == 0)
1105 return OPERATOR_CANCELLED;
1107 /* get handle setting mode */
1108 mode= RNA_enum_get(op->ptr, "type");
1110 /* set handle type */
1111 setipo_graph_keys(&ac, mode);
1113 /* validate keyframes after editing */
1114 ANIM_editkeyframes_refresh(&ac);
1116 /* set notifier that things have changed */
1117 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
1119 return OPERATOR_FINISHED;
1122 void GRAPHEDIT_OT_keyframes_interpolation_type (wmOperatorType *ot)
1125 ot->name= "Set Keyframe Interpolation";
1126 ot->idname= "GRAPHEDIT_OT_keyframes_interpolation_type";
1129 ot->invoke= WM_menu_invoke;
1130 ot->exec= graphkeys_ipo_exec;
1131 ot->poll= ED_operator_areaactive;
1134 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1137 RNA_def_enum(ot->srna, "type", beztriple_interpolation_mode_items, 0, "Type", "");
1140 /* ******************** Set Handle-Type Operator *********************** */
1142 /* this function is responsible for setting handle-type of selected keyframes */
1143 static void sethandles_graph_keys(bAnimContext *ac, short mode)
1145 ListBase anim_data = {NULL, NULL};
1148 BeztEditFunc set_cb= ANIM_editkeyframes_handles(mode);
1151 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE| ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
1152 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1154 /* loop through setting flags for handles
1155 * Note: we do not supply BeztEditData to the looper yet. Currently that's not necessary here...
1157 // XXX we might need to supply BeztEditData to get it to only affect selected handles
1158 for (ale= anim_data.first; ale; ale= ale->next) {
1160 BeztEditFunc toggle_cb;
1162 /* check which type of handle to set (free or aligned)
1163 * - check here checks for handles with free alignment already
1165 if (ANIM_fcurve_keys_bezier_loop(NULL, ale->key_data, NULL, set_cb, NULL))
1166 toggle_cb= ANIM_editkeyframes_handles(HD_FREE);
1168 toggle_cb= ANIM_editkeyframes_handles(HD_ALIGN);
1170 /* set handle-type */
1171 ANIM_fcurve_keys_bezier_loop(NULL, ale->key_data, NULL, toggle_cb, calchandles_fcurve);
1174 /* directly set handle-type */
1175 ANIM_fcurve_keys_bezier_loop(NULL, ale->key_data, NULL, set_cb, calchandles_fcurve);
1180 BLI_freelistN(&anim_data);
1183 /* ------------------- */
1185 static int graphkeys_handletype_exec(bContext *C, wmOperator *op)
1190 /* get editor data */
1191 if (ANIM_animdata_get_context(C, &ac) == 0)
1192 return OPERATOR_CANCELLED;
1194 /* get handle setting mode */
1195 mode= RNA_enum_get(op->ptr, "type");
1197 /* set handle type */
1198 sethandles_graph_keys(&ac, mode);
1200 /* validate keyframes after editing */
1201 ANIM_editkeyframes_refresh(&ac);
1203 /* set notifier that things have changed */
1204 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
1206 return OPERATOR_FINISHED;
1209 void GRAPHEDIT_OT_keyframes_handletype (wmOperatorType *ot)
1212 ot->name= "Set Keyframe Handle Type";
1213 ot->idname= "GRAPHEDIT_OT_keyframes_handletype";
1216 ot->invoke= WM_menu_invoke;
1217 ot->exec= graphkeys_handletype_exec;
1218 ot->poll= ED_operator_areaactive;
1221 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1224 RNA_def_enum(ot->srna, "type", beztriple_handle_type_items, 0, "Type", "");
1227 /* ************************************************************************** */
1228 /* TRANSFORM STUFF */
1230 /* ***************** 'Euler Filter' Operator **************************** */
1231 /* Euler filter tools (as seen in Maya), are necessary for working with 'baked'
1232 * rotation curves (with Euler rotations). The main purpose of such tools is to
1233 * resolve any discontinuities that may arise in the curves due to the clamping
1234 * of values to -180 degrees to 180 degrees.
1237 #if 0 // XXX this is not ready for the primetime yet
1239 /* set of three euler-rotation F-Curves */
1240 typedef struct tEulerFilter {
1241 ID *id; /* ID-block which owns the channels */
1242 FCurve *fcu1, *fcu2, *fcu3; /* x,y,z rotation curves */
1243 int i1, i2, i3; /* current index for each curve */
1246 static int graphkeys_euler_filter_exec (bContext *C, wmOperator *op)
1250 ListBase anim_data= {NULL, NULL};
1254 ListBase eulers = {NULL, NULL};
1255 tEulerFilter *euf= NULL;
1257 /* get editor data */
1258 if (ANIM_animdata_get_context(C, &ac) == 0)
1259 return OPERATOR_CANCELLED;
1261 /* The process is done in two passes:
1262 * 1) Sets of three related rotation curves are identified from the selected channels,
1263 * and are stored as a single 'operation unit' for the next step
1264 * 2) Each set of three F-Curves is processed for each keyframe, with the values being
1265 * processed according to one of several ways.
1268 /* step 1: extract only the rotation f-curves */
1269 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY);
1270 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1272 for (ale= anim_data.first; ale; ale= ale->next) {
1273 FCurve *fcu = (FCurve *)ale->data;
1275 /* check if this is an appropriate F-Curve
1276 * - only rotation curves
1277 * - for pchan curves, make sure we're only using the euler curves
1279 if (ELEM(0, fcu->rna_path, strstr(fcu->rna_path, "rotation")))
1281 if (strstr(fcu->rna_path, "pose.pose_channels")) {
1282 if (strstr(fcu->rna_path, "euler_rotation") == 0)
1286 /* check if current set of 3-curves is suitable to add this curve to
1287 * - things like whether the current set of curves is 'full' should be checked later only
1288 * - first check if id-blocks are compatible
1290 if ((euf) && (ale->id != euf->id)) {
1296 return OPERATOR_CANCELLED;
1299 void GRAPHEDIT_OT_keyframes_euler_filter (wmOperatorType *ot)
1302 ot->name= "Euler Filter";
1303 ot->idname= "GRAPHEDIT_OT_keyframes_euler_filter";
1306 ot->exec= graphkeys_euler_filter_exec;
1307 ot->poll= ED_operator_areaactive;
1310 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1313 #endif // XXX this is not ready for the primetime yet
1315 /* ***************** Snap Current Frame Operator *********************** */
1317 /* snap current-frame indicator to 'average time' of selected keyframe */
1318 static int graphkeys_cfrasnap_exec(bContext *C, wmOperator *op)
1321 ListBase anim_data= {NULL, NULL};
1326 /* get editor data */
1327 if (ANIM_animdata_get_context(C, &ac) == 0)
1328 return OPERATOR_CANCELLED;
1330 /* init edit data */
1331 memset(&bed, 0, sizeof(BeztEditData));
1333 /* loop over action data, averaging values */
1334 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE| ANIMFILTER_CURVESONLY);
1335 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1337 for (ale= anim_data.first; ale; ale= ale->next)
1338 ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, bezt_calc_average, NULL);
1340 BLI_freelistN(&anim_data);
1342 /* set the new current frame value, based on the average time */
1344 Scene *scene= ac.scene;
1345 CFRA= (int)floor((bed.f1 / bed.i1) + 0.5f);
1348 /* set notifier that things have changed */
1349 WM_event_add_notifier(C, NC_SCENE|ND_FRAME, ac.scene);
1351 return OPERATOR_FINISHED;
1354 void GRAPHEDIT_OT_keyframes_cfrasnap (wmOperatorType *ot)
1357 ot->name= "Snap Current Frame to Keys";
1358 ot->idname= "GRAPHEDIT_OT_keyframes_cfrasnap";
1361 ot->exec= graphkeys_cfrasnap_exec;
1362 ot->poll= ED_operator_areaactive;
1365 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1368 /* ******************** Snap Keyframes Operator *********************** */
1370 /* defines for snap keyframes tool */
1371 EnumPropertyItem prop_graphkeys_snap_types[] = {
1372 {GRAPHKEYS_SNAP_CFRA, "CFRA", "Current frame", ""},
1373 {GRAPHKEYS_SNAP_NEAREST_FRAME, "NEAREST_FRAME", "Nearest Frame", ""}, // XXX as single entry?
1374 {GRAPHKEYS_SNAP_NEAREST_SECOND, "NEAREST_SECOND", "Nearest Second", ""}, // XXX as single entry?
1375 {GRAPHKEYS_SNAP_NEAREST_MARKER, "NEAREST_MARKER", "Nearest Marker", ""},
1376 {GRAPHKEYS_SNAP_HORIZONTAL, "HORIZONTAL", "Flatten Handles", ""},
1377 {0, NULL, NULL, NULL}
1380 /* this function is responsible for snapping keyframes to frame-times */
1381 static void snap_graph_keys(bAnimContext *ac, short mode)
1383 ListBase anim_data = {NULL, NULL};
1388 BeztEditFunc edit_cb;
1391 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE| ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
1392 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1394 /* get beztriple editing callbacks */
1395 edit_cb= ANIM_editkeyframes_snap(mode);
1397 memset(&bed, 0, sizeof(BeztEditData));
1398 bed.scene= ac->scene;
1400 /* snap keyframes */
1401 for (ale= anim_data.first; ale; ale= ale->next) {
1402 Object *nob= ANIM_nla_mapping_get(ac, ale);
1405 ANIM_nla_mapping_apply_fcurve(nob, ale->key_data, 0, 1);
1406 ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, edit_cb, calchandles_fcurve);
1407 ANIM_nla_mapping_apply_fcurve(nob, ale->key_data, 1, 1);
1410 ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, edit_cb, calchandles_fcurve);
1412 BLI_freelistN(&anim_data);
1415 /* ------------------- */
1417 static int graphkeys_snap_exec(bContext *C, wmOperator *op)
1422 /* get editor data */
1423 if (ANIM_animdata_get_context(C, &ac) == 0)
1424 return OPERATOR_CANCELLED;
1426 /* get snapping mode */
1427 mode= RNA_enum_get(op->ptr, "type");
1429 /* snap keyframes */
1430 snap_graph_keys(&ac, mode);
1432 /* validate keyframes after editing */
1433 ANIM_editkeyframes_refresh(&ac);
1435 /* set notifier that things have changed */
1436 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
1438 return OPERATOR_FINISHED;
1441 void GRAPHEDIT_OT_keyframes_snap (wmOperatorType *ot)
1444 ot->name= "Snap Keys";
1445 ot->idname= "GRAPHEDIT_OT_keyframes_snap";
1448 ot->invoke= WM_menu_invoke;
1449 ot->exec= graphkeys_snap_exec;
1450 ot->poll= ED_operator_areaactive;
1453 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1456 RNA_def_enum(ot->srna, "type", prop_graphkeys_snap_types, 0, "Type", "");
1459 /* ******************** Mirror Keyframes Operator *********************** */
1461 /* defines for mirror keyframes tool */
1462 EnumPropertyItem prop_graphkeys_mirror_types[] = {
1463 {GRAPHKEYS_MIRROR_CFRA, "CFRA", "Current frame", ""},
1464 {GRAPHKEYS_MIRROR_YAXIS, "YAXIS", "Vertical Axis", ""},
1465 {GRAPHKEYS_MIRROR_XAXIS, "XAXIS", "Horizontal Axis", ""},
1466 {GRAPHKEYS_MIRROR_MARKER, "MARKER", "First Selected Marker", ""},
1467 {0, NULL, NULL, NULL}
1470 /* this function is responsible for mirroring keyframes */
1471 static void mirror_graph_keys(bAnimContext *ac, short mode)
1473 ListBase anim_data = {NULL, NULL};
1478 BeztEditFunc edit_cb;
1480 /* get beztriple editing callbacks */
1481 edit_cb= ANIM_editkeyframes_mirror(mode);
1483 memset(&bed, 0, sizeof(BeztEditData));
1484 bed.scene= ac->scene;
1486 /* for 'first selected marker' mode, need to find first selected marker first! */
1487 // XXX should this be made into a helper func in the API?
1488 if (mode == GRAPHKEYS_MIRROR_MARKER) {
1489 Scene *scene= ac->scene;
1490 TimeMarker *marker= NULL;
1492 /* find first selected marker */
1493 for (marker= scene->markers.first; marker; marker=marker->next) {
1494 if (marker->flag & SELECT) {
1499 /* store marker's time (if available) */
1501 bed.f1= (float)marker->frame;
1507 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE| ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
1508 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1510 /* mirror keyframes */
1511 for (ale= anim_data.first; ale; ale= ale->next) {
1512 Object *nob= ANIM_nla_mapping_get(ac, ale);
1515 ANIM_nla_mapping_apply_fcurve(nob, ale->key_data, 0, 1);
1516 ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, edit_cb, calchandles_fcurve);
1517 ANIM_nla_mapping_apply_fcurve(nob, ale->key_data, 1, 1);
1520 ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, edit_cb, calchandles_fcurve);
1522 BLI_freelistN(&anim_data);
1525 /* ------------------- */
1527 static int graphkeys_mirror_exec(bContext *C, wmOperator *op)
1532 /* get editor data */
1533 if (ANIM_animdata_get_context(C, &ac) == 0)
1534 return OPERATOR_CANCELLED;
1536 /* get mirroring mode */
1537 mode= RNA_enum_get(op->ptr, "type");
1539 /* mirror keyframes */
1540 mirror_graph_keys(&ac, mode);
1542 /* validate keyframes after editing */
1543 ANIM_editkeyframes_refresh(&ac);
1545 /* set notifier that things have changed */
1546 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
1548 return OPERATOR_FINISHED;
1551 void GRAPHEDIT_OT_keyframes_mirror (wmOperatorType *ot)
1554 ot->name= "Mirror Keys";
1555 ot->idname= "GRAPHEDIT_OT_keyframes_mirror";
1558 ot->invoke= WM_menu_invoke;
1559 ot->exec= graphkeys_mirror_exec;
1560 ot->poll= ED_operator_areaactive;
1563 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1566 RNA_def_enum(ot->srna, "type", prop_graphkeys_mirror_types, 0, "Type", "");
1569 /* ******************** Smooth Keyframes Operator *********************** */
1571 static int graphkeys_smooth_exec(bContext *C, wmOperator *op)
1574 ListBase anim_data = {NULL, NULL};
1578 /* get editor data */
1579 if (ANIM_animdata_get_context(C, &ac) == 0)
1580 return OPERATOR_CANCELLED;
1583 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE| ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY);
1584 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1586 /* smooth keyframes */
1587 for (ale= anim_data.first; ale; ale= ale->next) {
1588 /* For now, we can only smooth by flattening handles AND smoothing curve values.
1589 * Perhaps the mode argument could be removed, as that functionality is offerred through
1590 * Snap->Flatten Handles anyway.
1592 smooth_fcurve(ale->key_data);
1594 BLI_freelistN(&anim_data);
1596 /* validate keyframes after editing */
1597 ANIM_editkeyframes_refresh(&ac);
1599 /* set notifier that things have changed */
1600 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_VALUES);
1602 return OPERATOR_FINISHED;
1605 void GRAPHEDIT_OT_keyframes_smooth (wmOperatorType *ot)
1608 ot->name= "Smooth Keys";
1609 ot->idname= "GRAPHEDIT_OT_keyframes_smooth";
1612 ot->exec= graphkeys_smooth_exec;
1613 ot->poll= ED_operator_areaactive;
1616 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1619 /* ************************************************************************** */
1620 /* F-CURVE MODIFIERS */
1622 /* ******************** Add F-Curve Modifier Operator *********************** */
1624 static int graph_fmodifier_add_exec(bContext *C, wmOperator *op)
1632 /* get editor data */
1633 if (ANIM_animdata_get_context(C, &ac) == 0)
1634 return OPERATOR_CANCELLED;
1636 // xxx call the raw methods here instead?
1637 ale= get_active_fcurve_channel(&ac);
1639 return OPERATOR_CANCELLED;
1641 fcu= (FCurve *)ale->data;
1644 return OPERATOR_CANCELLED;
1646 /* get type of modifier to add */
1647 type= RNA_enum_get(op->ptr, "type");
1649 /* add F-Modifier of specified type to active F-Curve, and make it the active one */
1650 fcm= fcurve_add_modifier(fcu, type);
1652 fcurve_set_active_modifier(fcu, fcm);
1654 BKE_report(op->reports, RPT_ERROR, "Modifier couldn't be added. See console for details.");
1655 return OPERATOR_CANCELLED;
1658 /* validate keyframes after editing */
1659 ANIM_editkeyframes_refresh(&ac);
1661 /* set notifier that things have changed */
1662 ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_BOTH);
1664 return OPERATOR_FINISHED;
1667 void GRAPHEDIT_OT_fmodifier_add (wmOperatorType *ot)
1670 ot->name= "Add F-Curve Modifier";
1671 ot->idname= "GRAPHEDIT_OT_fmodifier_add";
1674 ot->invoke= WM_menu_invoke;
1675 ot->exec= graph_fmodifier_add_exec;
1676 ot->poll= ED_operator_areaactive; // XXX need active F-Curve
1679 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1682 RNA_def_enum(ot->srna, "type", fmodifier_type_items, 0, "Type", "");
1685 /* ************************************************************************** */