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