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) 2008 Blender Foundation
21 * All rights reserved.
23 * Contributor(s): Joshua Leung
25 * ***** END GPL LICENSE BLOCK *****
33 #include "MEM_guardedalloc.h"
35 #include "BLI_blenlib.h"
36 #include "BLI_arithb.h"
38 #include "DNA_anim_types.h"
39 #include "DNA_action_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_space_types.h"
44 #include "DNA_scene_types.h"
46 #include "BKE_action.h"
47 #include "BKE_fcurve.h"
49 #include "BKE_utildefines.h"
51 #include "ED_anim_api.h"
52 #include "ED_keyframing.h"
53 #include "ED_keyframes_edit.h"
55 #include "RNA_access.h"
57 /* This file contains code for various keyframe-editing tools which are 'destructive'
58 * (i.e. they will modify the order of the keyframes, and change the size of the array).
59 * While some of these tools may eventually be moved out into blenkernel, for now, it is
60 * fine to have these calls here.
62 * There are also a few tools here which cannot be easily coded for in the other system (yet).
63 * These may also be moved around at some point, but for now, they
65 * - Joshua Leung, Dec 2008
68 /* **************************************************** */
70 /* Only delete the nominated keyframe from provided ipo-curve.
71 * Not recommended to be used many times successively. For that
72 * there is delete_ipo_keys().
74 void delete_fcurve_key(FCurve *fcu, int index, short do_recalc)
76 /* firstly check that index is valid */
81 if (index >= fcu->totvert)
85 memmove(&fcu->bezt[index], &fcu->bezt[index+1], sizeof(BezTriple)*(fcu->totvert-index-1));
88 /* recalc handles - only if it won't cause problems */
90 calchandles_fcurve(fcu);
93 /* Delete selected keyframes in given F-Curve */
94 void delete_fcurve_keys(FCurve *fcu)
98 /* Delete selected BezTriples */
99 for (i=0; i < fcu->totvert; i++) {
100 if (fcu->bezt[i].f2 & SELECT) {
101 memmove(&fcu->bezt[i], &fcu->bezt[i+1], sizeof(BezTriple)*(fcu->totvert-i-1));
107 /* Free the array of BezTriples if there are not keyframes */
108 if (fcu->totvert == 0) {
110 MEM_freeN(fcu->bezt);
114 #if 0 // XXX for now, we don't get rid of empty curves...
115 /* Only delete if there isn't an ipo-driver still hanging around on an empty curve */
116 if ((icu->totvert==0) && (icu->driver==NULL)) {
117 BLI_remlink(&ipo->curve, icu);
123 /* ---------------- */
125 /* duplicate selected keyframes for the given F-Curve */
126 void duplicate_fcurve_keys(FCurve *fcu)
134 // XXX this does not take into account sample data...
135 for (i=0; i < fcu->totvert; i++) {
136 /* If a key is selected */
137 if (fcu->bezt[i].f2 & SELECT) {
138 /* Expand the list */
139 newbezt = MEM_callocN(sizeof(BezTriple) * (fcu->totvert+1), "beztriple");
141 memcpy(newbezt, fcu->bezt, sizeof(BezTriple) * (i+1));
142 memcpy(newbezt+i+1, fcu->bezt+i, sizeof(BezTriple));
143 memcpy(newbezt+i+2, fcu->bezt+i+1, sizeof (BezTriple) *(fcu->totvert-(i+1)));
146 /* reassign pointers... (free old, and add new) */
147 MEM_freeN(fcu->bezt);
150 /* Unselect the current key */
151 BEZ_DESEL(&fcu->bezt[i]);
154 /* Select the copied key */
155 BEZ_SEL(&fcu->bezt[i]);
160 /* **************************************************** */
163 /* Basic IPO-Curve 'cleanup' function that removes 'double points' and unnecessary keyframes on linear-segments only */
164 void clean_fcurve(FCurve *fcu, float thresh)
166 BezTriple *old_bezts, *bezt, *beztn;
170 /* check if any points */
171 if ((fcu == NULL) || (fcu->totvert <= 1))
174 /* make a copy of the old BezTriples, and clear IPO curve */
175 old_bezts = fcu->bezt;
176 totCount = fcu->totvert;
180 /* now insert first keyframe, as it should be ok */
182 insert_vert_fcurve(fcu, bezt->vec[1][0], bezt->vec[1][1], 0);
184 /* Loop through BezTriples, comparing them. Skip any that do
185 * not fit the criteria for "ok" points.
187 for (i=1; i<totCount; i++) {
188 float prev[2], cur[2], next[2];
190 /* get BezTriples and their values */
191 if (i < (totCount - 1)) {
192 beztn = (old_bezts + (i+1));
193 next[0]= beztn->vec[1][0]; next[1]= beztn->vec[1][1];
197 next[0] = next[1] = 0.0f;
199 lastb= (fcu->bezt + (fcu->totvert - 1));
200 bezt= (old_bezts + i);
202 /* get references for quicker access */
203 prev[0] = lastb->vec[1][0]; prev[1] = lastb->vec[1][1];
204 cur[0] = bezt->vec[1][0]; cur[1] = bezt->vec[1][1];
206 /* check if current bezt occurs at same time as last ok */
207 if (IS_EQT(cur[0], prev[0], thresh)) {
208 /* If there is a next beztriple, and if occurs at the same time, only insert
209 * if there is a considerable distance between the points, and also if the
210 * current is further away than the next one is to the previous.
212 if (beztn && (IS_EQT(cur[0], next[0], thresh)) &&
213 (IS_EQT(next[1], prev[1], thresh)==0))
215 /* only add if current is further away from previous */
216 if (cur[1] > next[1]) {
217 if (IS_EQT(cur[1], prev[1], thresh) == 0) {
218 /* add new keyframe */
219 insert_vert_fcurve(fcu, cur[0], cur[1], 0);
224 /* only add if values are a considerable distance apart */
225 if (IS_EQT(cur[1], prev[1], thresh) == 0) {
226 /* add new keyframe */
227 insert_vert_fcurve(fcu, cur[0], cur[1], 0);
232 /* checks required are dependent on whether this is last keyframe or not */
234 /* does current have same value as previous and next? */
235 if (IS_EQT(cur[1], prev[1], thresh) == 0) {
236 /* add new keyframe*/
237 insert_vert_fcurve(fcu, cur[0], cur[1], 0);
239 else if (IS_EQT(cur[1], next[1], thresh) == 0) {
240 /* add new keyframe */
241 insert_vert_fcurve(fcu, cur[0], cur[1], 0);
245 /* add if value doesn't equal that of previous */
246 if (IS_EQT(cur[1], prev[1], thresh) == 0) {
247 /* add new keyframe */
248 insert_vert_fcurve(fcu, cur[0], cur[1], 0);
254 /* now free the memory used by the old BezTriples */
256 MEM_freeN(old_bezts);
259 /* ---------------- */
261 /* temp struct used for smooth_ipo */
262 typedef struct tSmooth_Bezt {
263 float *h1, *h2, *h3; /* bezt->vec[0,1,2][1] */
266 /* Use a weighted moving-means method to reduce intensity of fluctuations */
267 void smooth_fcurve (FCurve *fcu)
270 int i, x, totSel = 0;
272 /* first loop through - count how many verts are selected, and fix up handles
273 * this is done for both modes
276 for (i=0; i < fcu->totvert; i++, bezt++) {
277 if (BEZSELECTED(bezt)) {
278 /* line point's handles up with point's vertical position */
279 bezt->vec[0][1]= bezt->vec[2][1]= bezt->vec[1][1];
280 if ((bezt->h1==HD_AUTO) || (bezt->h1==HD_VECT)) bezt->h1= HD_ALIGN;
281 if ((bezt->h2==HD_AUTO) || (bezt->h2==HD_VECT)) bezt->h2= HD_ALIGN;
283 /* add value to total */
288 /* if any points were selected, allocate tSmooth_Bezt points to work on */
290 tSmooth_Bezt *tarray, *tsb;
292 /* allocate memory in one go */
293 tsb= tarray= MEM_callocN(totSel*sizeof(tSmooth_Bezt), "tSmooth_Bezt Array");
295 /* populate tarray with data of selected points */
297 for (i=0, x=0; (i < fcu->totvert) && (x < totSel); i++, bezt++) {
298 if (BEZSELECTED(bezt)) {
299 /* tsb simply needs pointer to vec, and index */
300 tsb->h1 = &bezt->vec[0][1];
301 tsb->h2 = &bezt->vec[1][1];
302 tsb->h3 = &bezt->vec[2][1];
304 /* advance to the next tsb to populate */
312 /* calculate the new smoothed F-Curve's with weighted averages:
313 * - this is done with two passes
314 * - uses 5 points for each operation (which stores in the relevant handles)
315 * - previous: w/a ratio = 3:5:2:1:1
316 * - next: w/a ratio = 1:1:2:5:3
319 /* round 1: calculate previous and next */
321 for (i=0; i < totSel; i++, tsb++) {
322 /* don't touch end points (otherwise, curves slowly explode) */
323 if (ELEM(i, 0, (totSel-1)) == 0) {
324 const tSmooth_Bezt *tP1 = tsb - 1;
325 const tSmooth_Bezt *tP2 = (i-2 > 0) ? (tsb - 2) : (NULL);
326 const tSmooth_Bezt *tN1 = tsb + 1;
327 const tSmooth_Bezt *tN2 = (i+2 < totSel) ? (tsb + 2) : (NULL);
329 const float p1 = *tP1->h2;
330 const float p2 = (tP2) ? (*tP2->h2) : (*tP1->h2);
331 const float c1 = *tsb->h2;
332 const float n1 = *tN1->h2;
333 const float n2 = (tN2) ? (*tN2->h2) : (*tN1->h2);
335 /* calculate previous and next */
336 *tsb->h1= (3*p2 + 5*p1 + 2*c1 + n1 + n2) / 12;
337 *tsb->h3= (p2 + p1 + 2*c1 + 5*n1 + 3*n2) / 12;
341 /* round 2: calculate new values and reset handles */
343 for (i=0; i < totSel; i++, tsb++) {
344 /* calculate new position by averaging handles */
345 *tsb->h2 = (*tsb->h1 + *tsb->h3) / 2;
347 /* reset handles now */
352 /* free memory required for tarray */
356 /* recalculate handles */
357 calchandles_fcurve(fcu);
360 /* **************************************************** */
361 /* Copy/Paste Tools */
362 /* - The copy/paste buffer currently stores a set of temporary F-Curves containing only the keyframes
363 * that were selected in each of the original F-Curves
364 * - All pasted frames are offset by the same amount. This is calculated as the difference in the times of
365 * the current frame and the 'first keyframe' (i.e. the earliest one in all channels).
366 * - The earliest frame is calculated per copy operation.
369 /* globals for copy/paste data (like for other copy/paste buffers) */
370 ListBase animcopybuf = {NULL, NULL};
371 static float animcopy_firstframe= 999999999.0f;
373 /* datatype for use in copy/paste buffer */
374 // XXX F-Curve editor should use this too
375 typedef struct tAnimCopybufItem {
376 struct tAnimCopybufItem *next, *prev;
378 ID *id; /* ID which owns the curve */
379 bActionGroup *grp; /* Action Group */
380 char *rna_path; /* RNA-Path */
381 int array_index; /* array index */
383 int totvert; /* number of keyframes stored for this channel */
384 BezTriple *bezt; /* keyframes in buffer */
388 /* This function frees any MEM_calloc'ed copy/paste buffer data */
389 // XXX find some header to put this in!
390 void free_anim_copybuf (void)
392 tAnimCopybufItem *aci, *acn;
394 /* free each buffer element */
395 for (aci= animcopybuf.first; aci; aci= acn) {
400 MEM_freeN(aci->bezt);
404 MEM_freeN(aci->rna_path);
407 BLI_freelinkN(&animcopybuf, aci);
410 /* restore initial state */
411 animcopybuf.first= animcopybuf.last= NULL;
412 animcopy_firstframe= 999999999.0f;
415 /* ------------------- */
417 /* This function adds data to the keyframes copy/paste buffer, freeing existing data first */
418 short copy_animedit_keys (bAnimContext *ac, ListBase *anim_data)
422 /* clear buffer first */
425 /* assume that each of these is an F-Curve */
426 for (ale= anim_data->first; ale; ale= ale->next) {
427 FCurve *fcu= (FCurve *)ale->key_data;
428 tAnimCopybufItem *aci;
429 BezTriple *bezt, *newbuf;
432 /* firstly, check if F-Curve has any selected keyframes
433 * - skip if no selected keyframes found (so no need to create unnecessary copy-buffer data)
434 * - this check should also eliminate any problems associated with using sample-data
436 if (ANIM_fcurve_keys_bezier_loop(NULL, fcu, NULL, ANIM_editkeyframes_ok(BEZT_OK_SELECTED), NULL) == 0)
439 /* init copybuf item info */
440 aci= MEM_callocN(sizeof(tAnimCopybufItem), "AnimCopybufItem");
443 aci->rna_path= MEM_dupallocN(fcu->rna_path);
444 aci->array_index= fcu->array_index;
445 BLI_addtail(&animcopybuf, aci);
447 /* add selected keyframes to buffer */
448 // TODO: currently, we resize array everytime we add a new vert - this works ok as long as it is assumed only a few keys are copied
449 for (i=0, bezt=fcu->bezt; i < fcu->totvert; i++, bezt++) {
450 if (BEZSELECTED(bezt)) {
452 newbuf= MEM_callocN(sizeof(BezTriple)*(aci->totvert+1), "copybuf beztriple");
454 /* assume that since we are just resizing the array, just copy all existing data across */
456 memcpy(newbuf, aci->bezt, sizeof(BezTriple)*(aci->totvert));
458 /* copy current beztriple across too */
459 *(newbuf + aci->totvert)= *bezt;
461 /* free old array and set the new */
462 if (aci->bezt) MEM_freeN(aci->bezt);
466 /* check if this is the earliest frame encountered so far */
467 if (bezt->vec[1][0] < animcopy_firstframe)
468 animcopy_firstframe= bezt->vec[1][0];
474 /* check if anything ended up in the buffer */
475 if (ELEM(NULL, animcopybuf.first, animcopybuf.last))
478 /* everything went fine */
482 /* This function pastes data from the keyframes copy/paste buffer */
483 short paste_animedit_keys (bAnimContext *ac, ListBase *anim_data)
486 const Scene *scene= (ac->scene);
487 const float offset = (float)(CFRA - animcopy_firstframe);
490 /* check if buffer is empty */
491 if (ELEM(NULL, animcopybuf.first, animcopybuf.last)) {
492 //error("No data in buffer to paste");
495 /* check if single channel in buffer (disregard names if so) */
496 if (animcopybuf.first == animcopybuf.last)
499 /* from selected channels */
500 for (ale= anim_data->first; ale; ale= ale->next) {
501 FCurve *fcu = (FCurve *)ale->data; /* destination F-Curve */
502 tAnimCopybufItem *aci= NULL;
506 /* find buffer item to paste from
507 * - if names don't matter (i.e. only 1 channel in buffer), don't check id/group
508 * - if names do matter, only check if id-type is ok for now (group check is not that important)
509 * - most importantly, rna-paths should match (array indices are unimportant for now)
511 // TODO: the matching algorithm here is pathetic!
512 for (aci= animcopybuf.first; aci; aci= aci->next) {
513 /* check that paths exist */
514 if (aci->rna_path && fcu->rna_path) {
515 if (strcmp(aci->rna_path, fcu->rna_path) == 0) {
516 /* should be a match unless there's more than one of these */
517 if ((no_name) || (aci->array_index == fcu->array_index))
524 /* copy the relevant data from the matching buffer curve */
526 /* just start pasting, with the the first keyframe on the current frame, and so on */
527 for (i=0, bezt=aci->bezt; i < aci->totvert; i++, bezt++) {
528 /* temporarily apply offset to src beztriple while copying */
529 bezt->vec[0][0] += offset;
530 bezt->vec[1][0] += offset;
531 bezt->vec[2][0] += offset;
533 /* insert the keyframe */
534 insert_bezt_fcurve(fcu, bezt);
536 /* un-apply offset from src beztriple after copying */
537 bezt->vec[0][0] -= offset;
538 bezt->vec[1][0] -= offset;
539 bezt->vec[2][0] -= offset;
542 /* recalculate F-Curve's handles? */
543 calchandles_fcurve(fcu);
550 /* **************************************************** */