Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions I’m trying to achieve looping Y movement on a clip using scripts

  • I’m trying to achieve looping Y movement on a clip using scripts

    Posted by Tom Percival on February 13, 2019 at 9:20 pm

    Hello nice, helpful people!

    I’m trying to get a clip to recognise when it is higher up the screen than a certain point (in this case 60) so that it can then reset it’s position to the bottom of the screen and do it all again. All the animation needs to be run by expressions so I can vary the speed, start position and end positions dynamically.

    I’m a bit stuck. The script works at first and the little black square moves up the screen, then when it gets to the conditional statement (less than 60) it resets back to 900 (so far so good…) BUT it doesn’t start to go up again. I don’t get an error message, so the script’s obviously correct, but it’s not doing what I need it to.

    Any ideas how I can get it to do what I want? All help much appreciated.

    velocity = -300
    x = position[0];
    y = position[1]+ (time - inPoint) *velocity;
    reset = 900

    if (y<60){
    y=reset;
    [x,y]
    }else{
    [x,y]
    }

    Tom Percival replied 7 years, 4 months ago 2 Members · 6 Replies
  • 6 Replies
  • Oleg Pirogov

    February 14, 2019 at 2:05 am

    If I wanted to make a loop like this, I would rather animate that one upward moving and then use loopOut() expression to loop it till the end of the comp.

    Anyway, about the expression:
    1) There’s a layer property called “velocity”, so you can’t just use it as a variable name. I’m not even sure why your AE doesn’t give you an error when you assign “velocity = -300”, as velocity property is a vector and -300 is not. Anyway, better pick some other name.
    2) It says at the bottom, because (time – inPoint) keeps growing, so it’s always (y<60) and thus y being set to 900;

  • Tom Percival

    February 14, 2019 at 11:02 am

    Thanks so much for your response! I can’t just animate the property and use loopOut() as this is basically a test for a piece I’m doing where I need to be able to control the speed of the movement as the piece runs through and I need to be able to do this in time with some music (hopefully!)

    Anyway, back to the expression… I’ve added in ‘velo’ as my expression name now so hopefully that will make things more stable!

    I under stand what you mean when you say:

    “2) It says at the bottom, because (time – inPoint) keeps growing, so it’s always (y<60) and thus y being set to 900;”

    But I don’t know how to solve that. The only scripts I could find for motion seem to use that “y = position[1]+ (time – inPoint) *VARIABLE;” to achieve a constant movement. Can you think of another way to get the clip to keep moving once it has ‘reset’ itself?

    My current code is included below:

    velo = -300
    x = position[0];
    y = position[1]+ (time - inPoint) *velo;
    reset = 900

    if (y&lt;60){
    y=reset;
    [x,y]
    }else{
    [x,y]
    }

  • Oleg Pirogov

    February 14, 2019 at 11:46 am

    Well, this:

    yTop = 60;
    yBase = 900;
    period = 10

    t = ((time - inPoint)%period)/period;
    y = yBase+(yTop-yBase)*t

    x = value[0];
    [x,y]

    Makes the layer to go from yBase to yTop in period seconds and the start all over again from yBase.

  • Tom Percival

    February 14, 2019 at 11:50 am

    That is it exactly!

    It’s working just how I want it to on the test. Now I just need to try it on the main clip with the expression slider involved and see if I can get that to work too.

    Thanks so much for your help, I was really banging my head against a wall with it. I’m going to analyse your expression to see if I can work out what’s making it do what it does!But in the mean time, thanks again!

  • Oleg Pirogov

    February 14, 2019 at 12:16 pm

    You’re welcome, though on the second thought what you really need is this:

    1) A slider for Speed called “Speed”.

    2) A second slider to compute distance (called “Distance”) based on keyframe integration of Speed done by world-famous Dan Ebbert’s script. This goes to second slider’s expression:

    spd = effect("Speed")("Slider")
    n = spd.numKeys;
    if (n > 0 && spd.key(1).time < time){
    accum = spd.key(1).value*(spd.key(1).time - inPoint);
    for (i = 2; i <= n; i++){
    if (spd.key(i).time > time) break;
    k1 = spd.key(i-1);
    k2 = spd.key(i);
    accum += (k1.value + k2.value)*(k2.time - k1.time)/2;
    }
    accum += (spd.value + spd.key(i-1).value)*(time - spd.key(i-1).time)/2;
    }else{
    accum = spd.value*(time - inPoint);
    }
    value + accum

    [Here’s: https://www.motionscript.com/articles/speed-control.html – an explanation why you need it and what problems you would encounter if you tried to change speed directly with a slider in my previous expression]

    3) The final expression for position:

    yTop = 60;
    yBase = 900;

    y = yBase-effect("Distance")("Slider")%(yBase-yTop)

    x = value[0];
    [x,y]

  • Tom Percival

    February 14, 2019 at 1:11 pm

    I literally can’t thank you enough!

    I’d bookmarked the Dan Ebberts script as I thought it would be applicable to what I’m doing, but it would have TAKEN me hours (and hours) to be able to implement it without your help.

    I’m creating a music video and I want to simulate a ‘shutter speed’ glitch, so I’d set 24 duplicates of a comp, one on top of each other in a long comp which I’m then cycling through on the parent comp so that at times the footage will ‘roll’ up or down out of sync. I’ve just tested your script with it and it works like a charm! I can get on with all the straightforward editing now!

    I’ll add a link on to this thread when I’ve got the video finished so you can see what you helped to make. But for now, once again, thank you.

    Tom.

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