ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Distributing layers randomly outside of certain range

FAQ   •   VIEW ALL
 Distributing layers randomly outside of certain range on Apr 11, 2019 at 7:05:54 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];

Skylar

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

 Re: Distributing layers randomly outside of certain rangeon Apr 12, 2019 at 8:19:20 am

Would that be a square area?

Kalleheikki Kannisto
Senior Graphic Designer

 Re: Distributing layers randomly outside of certain rangeon Apr 12, 2019 at 4:15:40 pm

Rectangular, but yes

 Re: Distributing layers randomly outside of certain rangeon Apr 12, 2019 at 5:50:17 pmLast Edited By Kalleheikki Kannisto on Apr 12, 2019 at 5:51:41 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)<protect_x)&&(Math.abs(y)<protect_y)){ escape++ if (escape>100){break} }else{ searching = false} } center+[x,y]```

Kalleheikki Kannisto
Senior Graphic Designer