Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Distributing layers randomly outside of certain range

  • Distributing layers randomly outside of certain range

    Posted by Skylar Moran on April 11, 2019 at 7:05 pm

    Hello!

    I have a bunch of layers that I want to distribute randomly, but only outside a certain area of the comp. I have this expression that I’m using that gets me almost there, but I want to be able to define an area of the comp (in the middle of the comp) that the layer’s position can not be in.

    seedRandom(index,true);
    range = thisComp.layer(“Position”).effect(“Slider Control”)(“Slider”);
    x = random(-range,range);
    y = random(-range,range);
    [x,y];

    Thanks in advance!

    Skylar

    seedRandom(index,true);
    range = thisComp.layer("Position").effect("Slider Control")("Slider");
    x = random(-range,range);
    y = random(-range,range);
    [x,y];

    Kalleheikki Kannisto replied 7 years, 4 months ago 2 Members · 3 Replies
  • 3 Replies
  • Kalleheikki Kannisto

    April 12, 2019 at 8:19 am

    Would that be a square area?

    Kalleheikki Kannisto
    Senior Graphic Designer

  • Skylar Moran

    April 12, 2019 at 4:15 pm

    Rectangular, but yes

  • Kalleheikki Kannisto

    April 12, 2019 at 5:50 pm

    This is fairly complete expression. You need 4 sliders on a separate layer named “controls”: “range x”, “range y”, “protect x” and “protect y”.

    The expression starts the positions around the center, so range x and range y are in both positive and negative directions around the center. Protected area is the size of the area, in both directions around the center. In essence, the actual areas are twice the size of the ranges.

    Expression takes into account the size of the layers themselves so that they don’t overlap the protected area.

    Basically, if the resulting position is inside the protected area, a new set of position values are tested. The “escape” variable is a foolproof in case the protected area becomes larger than the range, the expressions will stop trying after 100 attempts so as to avoid an infinite loop.

    seedRandom(index,true);
    my_size = thisLayer.sourceRectAtTime(time,true);
    my_width = my_size.width;
    my_height = my_size.height;
    range_x = thisComp.layer("controls").effect("range x")("Slider");
    range_y = thisComp.layer("controls").effect("range y")("Slider");
    protect_x = thisComp.layer("controls").effect("protect x")("Slider")+my_width/2;
    protect_y = thisComp.layer("controls").effect("protect y")("Slider")+my_height/2;
    center = [thisComp.width/2,thisComp.height/2];
    searching = true;
    escape = 0;
    while (searching){
    x = random(-range_x,range_x);
    y = random(-range_y,range_y);
    if((Math.abs(x)100){break}
    }else{
    searching = false}
    }
    center+[x,y]

    Kalleheikki Kannisto
    Senior Graphic Designer

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