Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Squash and Stretch – Random Seed?

  • Squash and Stretch – Random Seed?

    Posted by Steven Kutny on March 31, 2014 at 4:01 pm

    Hello expression experts!

    I’m using this wonderful set of expressions for a “Squash and Stretch” animation but I need to know how to modify them to create a random offset. When I duplicate the layers they are applied to, the animation remains the same for each. Many thanks in advance!
    -Steve

    //Bouncing ball with squash and stretch
    //Apply to Position
    freq = 1.0; //oscillations per second
    amplitude = 90;
    decay = .5;
    posCos = Math.abs(Math.cos(freq*time*2*Math.PI));
    y = amplitude*posCos/Math.exp(decay*time);
    position - [0,y]

    //and here's the expression for the scale parameter.
    freq = 1.0;
    squashFreq = 4.0;
    decay = 5.0;
    masterDecay = 0.4;
    amplitude = 25;
    delay = 1/(freq*4);
    if (time > delay){
    bounce = Math.sin(squashFreq*time*2*Math.PI);
    bounceDecay = Math.exp(decay*((time - delay)%(freq/2)));
    overallDecay = Math.exp(masterDecay*(time - delay));
    x = scale[0] + amplitude*bounce/bounceDecay/overallDecay;
    y = scale[0]*scale[1]/x;
    [x,y]
    }else{
    scale
    }

    Steven Kutny
    Designer/Compositor
    http://www.stevenkutny.com

    Steven Kutny replied 12 years, 1 month ago 2 Members · 7 Replies
  • 7 Replies
  • Dan Ebberts

    March 31, 2014 at 6:46 pm

    If you have a bunch of parameters in an expression and you want to vary them by say, plus or minus 10% on each layer, you could do something like this:

    seedRandom(index,true);
    freq = 1.0*random(.9,1.1);
    amplitude = 90*random(.9,1.1);
    decay = .5*random(.9,1.1);
    posCos = Math.abs(Math.cos(freq*time*2*Math.PI));
    y = amplitude*posCos/Math.exp(decay*time);
    position – [0,y]

    You would do a similar thing for the scale expression.

  • Steven Kutny

    March 31, 2014 at 7:06 pm

    Thanks for the quick response Dan! This works for the subtle variations, but what if I wanted to offset the entire animation by 10 frames without having to pre-comp? No variation, just frame/time offset.

  • Dan Ebberts

    March 31, 2014 at 7:12 pm

    Ah, that would be more like this:

    freq = 1.0;
    amplitude = 90;
    decay = .5;
    delay = .1;
    t = (time-inPoint) – delay*(index-1);
    posCos = Math.abs(Math.cos(freq*t*2*Math.PI));
    y = amplitude*posCos/Math.exp(decay*t);
    position – [0,y]

    Dan

  • Steven Kutny

    April 1, 2014 at 2:01 pm

    Perfect! Thanks so much for this Dan, it is a big help 🙂

  • Steven Kutny

    April 1, 2014 at 7:03 pm

    Hi Dan,

    Grateful if I could get your input one more time with this. I notice that the look of the animation changes depending on the layer order. If I create layers about them, or if I reorder the layers in my comp, their animation shifts.

    I removed the (index-1) part from this line: t = (time-inPoint) – delay*(index-1); which fixes that problem, but now I see the bouncing doesn’t always land on the same base line with a 0.1 delay; the hits will be slightly higher than the final resting position, unless I change the delay to 0.5 or something higher. Just making sure my change to that line is correct for this expression.

    Also wondering if there is a way to match up the squash & stretch expression on scale to the bouncing? I’m getting varied results. Not sure if I need to match the delay to the delay on position or the difference between the decay and masterDecay…

    Many thanks!

  • Steven Kutny

    April 1, 2014 at 7:15 pm

    Fixed the scale problem using this code form your site:

    maxDev = 13; // max deviation in pixels
    spd = 30; //speed of oscillation
    decay = 1.0; //how fast it slows down

    t = time – inPoint;
    x = scale[0] + maxDev*Math.sin(spd*t)/Math.exp(decay*t);
    y = scale[0]*scale[1]/x;
    [x,y]

    I repositioned my null to where it should have been, at the bottom of my ball, and now the sin wave feels correct. Thanks again!

  • Steven Kutny

    April 1, 2014 at 8:08 pm

    Going with this for position… Frame rate must dictate how bounce lands on the base line when tweaking the frequency… Sometimes a higher or lower freq throws off the hit. Trial and error but it’s all good.

    freq = 1.0;
    amplitude = 500;
    decay = 1;
    t = time-inPoint;
    posCos = Math.abs(Math.cos(freq*t*2*Math.PI));
    y = amplitude*posCos/Math.exp(decay*t);
    position - [0,y]

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy