Expression to randomly change position of layer on grid.

New Here ,
Nov 04, 2015 Nov 04, 2015

Copy link to clipboard

Copied

Hello,


I would like an expression to randomly affect the position of a layer on a grid, like the example on Dan Ebberts website (Mastering Expressions, Pushing The Limits: Random Motion). However, instead of moving along a path from point A to point B, I would like the layer to jump from point A to point B (with no path in between) over 1 frame. The layer would then hold on that grid space for 5-10 frames before jumping to another grid space, and so on.

Having the ability to control the number of layers on the grid by way of a slider would also be very useful.

Please help, very stressed!

TOPICS
Expressions

Views

4.2K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Nov 04, 2015 Nov 04, 2015

Copy link to clipboard

Copied

You are talking about a complex expression. The challenge is to randomly position a layer in the comp space, randomly change the time that each layer was in it's new position, and then make sure that two layers don't end up in the same position at the same time.

I've got a couple of animation presets that will arrange layers in a grid. First you need a layer named DL controller. This should be a null or a guide layer because it does not render. You apply this animation preset to the DL controller layer: Dropbox - Distribute layers-DL controller.ffx

This adds some sliders to the controller layer.

Screen Shot 2015-11-04 at 2.56.08 AM.png

Next you create some slave layers. These can be any size but they should all be the same scale. These layers can be anything from text to shape layers to nested comps. Add the slave layers and apply this animation preset: Dropbox - Distribute layers slaves.ffx

This will give you this expression:

// Find Layer Size and calculate new size if scaled

sf = scale - [100, 100];

xSize = thisLayer.sourceRectAtTime().width + (thisLayer.sourceRectAtTime().width * sf[0]/100);

ySize = thisLayer.sourceRectAtTime().height + (thisLayer.sourceRectAtTime().height * sf[1]/100);

realSize = [xSize, ySize];

// Set number of columns

cn = Math.floor(thisComp.layer("DL controller").effect("numCol")("Slider"));

if (cn <= 0){

  numCol = 1}

else numCol = (cn);

// Set positions

cp = thisComp.layer("DL controller").position;

sp = thisComp.layer("DL controller").effect("sp")("Slider");

if (sp == 0) {

  sp = 0}

else sp = sp;

col = (thisLayer.index-1) % numCol;

row = Math.floor((thisLayer.index-1)/numCol);

pos = [cp[0] + (sp + realSize[0]) * col,  cp[1] + (row * sp) + realSize[1] * row];

Now comes the fun part. Position the controller (must be at the bottom of the stack) and the first slave layer follows. Scale the first slave layer to get the size you want, then start duplicating the layer and you get as many columns as you want starting from the position of the DL controller layer.

Screen Shot 2015-11-04 at 3.02.05 AM.png

So with that as a starting point let's take a look at the expression and see what it does:

The first section just checks to make sure that you have at least one layer. The second section is where things get interesting. The "cp" is the current position of the controller so that starts distributing the layers. The column "col" function uses the index of the layer to adjust the position based on the width of the layer. When the number of columns is reached then a new row is started.

Here's what you could do. You could replace the "col" variable with a value at time function and maybe throw in a random generator. You could add a slider for number of cells in the grid to add a maximum value for the random column value you were trying to achieve. Say you wanted 10 columns and 5 rows, the number of cells would then be 50.

I haven't got time to mess around with it right now but that should get you started. I think it would be something like this:

cells = Math.round(random(1, 20)); // new variable to set the total number of cells in the grid

col = (cells) % numCol;

row = cells/numCol;

pos = [cp[0] + (sp + realSize[0]) * col,  cp[1] + (row * sp) + realSize[1] * row];

This is going to give you a maximum of 20 cells. You could replace the 20 with a slider value. I will have to think about how you would go about using the "time" method to change the time between the random numbers. There is probably a clue in Dan's grid expression.

There is more than one way to do this. Maybe the easiest thing to do would be pre-compose the layers then apply some time remapping. You would need to set Preserve Frame Rate when nested in the Pre-comp's Advanced Settings.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 16, 2022 Feb 16, 2022

Copy link to clipboard

Copied

LATEST

Hi Rick,

 

I've been looking for something like this for a while, thank you for such an elegant solution. 

 

Tom

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines