Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Limiting a random distance between points…

  • Limiting a random distance between points…

    Posted by Nimrod Zin on December 17, 2020 at 7:13 am

    Can anyone please tell me why this expression doesn’t work?
    I’m changing a random position every keyframe but I want to change to be limited to a minimum from the last point it was.

    nearestKeyIndex = thisProperty.nearestKey(time).index;
    previousKeyTime = thisProperty.key(nearestKeyIndex-1).time;
    seedRandom(nearestKeyIndex,true);
    x=random(200,1650);
    y=random(150,650);
    nearestKeyPoint = [x,y];
    previousKeyPoint = thisProperty.valueAtTime(previousKeyTime);
    delta = length(nearestKeyPoint, previousKeyPoint);
    if (delta <100){
    	newPoint = nearestKeyPoint +[200,200];
    	newPoint;
    }else{
    	nearestKeyPoint;
    }
    Dan Ebberts replied 5 years, 8 months ago 2 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    December 17, 2020 at 7:53 am

    It won’t work the way you want because this line:

    previousKeyPoint = thisProperty.valueAtTime(previousKeyTime);

    will retrieve the keyframed value, not the value that the expression previously calculated at that time (expressions don’t have access to their own previous results). What could work though, would be to have the expression loop through all previous keyframes and calculate the current value that way.

  • Nimrod Zin

    December 17, 2020 at 1:02 pm

    Thank you so much…

    I’m not sure I got you or vice versa…

    I translated the expression values to a text layer and seems it does get the right values.

    The expression is “working” (doesn’t give an error), but it looks like it’s ignoring the if statement.

  • Dan Ebberts

    December 17, 2020 at 5:45 pm

    I’m not sure I’ve got the details exactly right, but I’d approach it like this:

    n = nearestKey(time).index;

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

    seedRandom(index,true);

    if (n > 0){

    curPos = key(1).value;

    for (i = 2; i <= n; i++){

    prevPos = curPos

    curPos = random([200,150],[1650,650]);

    if (length(prevPos,curPos) < 100)

    curPos += [200,200];

    }

    curPos

    }else

    value

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