# After Effects Expressions

Currently Being Moderated

## Is it possible to confine expressions to specific keyframes?

### May 4, 2012 11:22 AM

Tags:

I am definitely a beginner when it comes to expressions.

I am currently using a "bounce" expression on the position parameter of some objects as they come on screen. However, the next time they change positions, I don't want them to bounce. Every time a motion tween ends in a static position, the position expression gets applied and the stuff is moving around.

I'd like to be able to keyframe the expression so that it worked only when on the keyframes that it makes sense to affect.

amp = .05;

freq = 1;

decay = 7;

n = 0;

if (numKeys > 0)

{

n = nearestKey(time).index;

if (key(n).time > time)

{

n--;

}

}

if (n == 0)

{

t = 0;

}

else

{

t = time - key(n).time;

}

if (n > 0)

{

v = velocityAtTime(key(n).time - thisComp.frameDuration/10);

value + v*amp*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t);

}

else

{

value

}

I got this script from somewhere online. The only thing I know how to manipulate are the initial values of amp, freq, and decay.

Replies
• Currently Being Moderated
May 4, 2012 11:54 AM   in reply to bjgough

This version will only bounce at the 2nd keyframe:

amp = .05;

freq = 1;

decay = 7;

kf = 2; // only on 2nd keyframe

n = 0;

if (numKeys > 0){

n = nearestKey(time).index;

if (key(n).time > time) n--;

}

if (n == kf){

t = time - key(n).time;

v = velocityAtTime(key(n).time - thisComp.frameDuration/10);

value + v*amp*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t);

}else

value

Dan

|
Mark as:
• Currently Being Moderated
May 4, 2012 12:16 PM   in reply to bjgough

The expression could be modified to let you specify a list (an array actually) of keyframes at which to trigger, or you could set it up to trigger only at even-numbered keyframes. It just depends on what you need it to do.

Dan

|
Mark as:
• Currently Being Moderated
May 9, 2012 11:31 AM   in reply to bjgough

If you are looking for a general solution, use ExpressionTimeline:  With a simple interface, you can make an expression only last for a specific range of time and even transition from one expression to another.

Here are some examples

From the beginning until frame 50 use the keyframed values and after frame 50 use a wiggle expression.

Smoothly transition from one expression to another. The first expression is active from frame 0 to 50, the second expression from frame 50 until the end. The smooth transition between the two expressions is 10 frames long and hence lasts from frame 45 to frame 54. With the expressions in this example, the layer first follows another layer (expression 1) until frame 45. Then until frame 54 it smoothly travels back to its own position and simultaneously starts to wiggle (expression 2).

|
Mark as:
• Currently Being Moderated
Mar 18, 2013 12:29 AM   in reply to bjgough

This expression does'nt works in AE CS6. When applied it gives the error Message saying " invalid numeric result (divided by zero?) expression disabled. error occured at line 29".  I usually use this expression a lot in AE CS5, but in CS6 showing error message. Pls can anyone help me....

|
Mark as:
Actions

#### More Like This

• Retrieving data ...

#### Answers + Points = Status

• 10 points awarded for Correct Answers