Expression Experiments - Grid Position
With this expression, you can automate the arranging the object on a rectangular position by duplicating the layer like the image below.

[Quick Set Up]
Create the first object and position it wherever you want. As a leader object, it’s a base of all positions. The name of first object needs to include a space and number 1, such as “rectangle 1”. Then duplicate it and apply the expression below to the position property.


objNum = 5; //number of objects in a line
pos = thisComp.layer("rectangle 1").transform.position; //leader's position
num = parseInt(name.split(" ")[1]) - 1;
repeatX = num % objNum;
repeatY = Math.floor(num / objNum);
gapX = 200; //pixel
gapY = 200; //pixel
x = gapX * repeatX;
y = gapY * repeatY;
pos + [x, y]

[Modified Version]
Here’s a sample with a modified version of this expression.
Sample with modified version of the expression.

After all, a project file is worth a thousand words. So download it from the link below and play around with it!
I would much appreciate it if you could give me a feedback. So please don’t hesitate to get in touch with me if you find some errors or better ways.
I hope you develop the expression and create awesome motion graphics.

Download the AEP file for free
Download the AEP file (CS6) for free

8 thoughts on “ Expression Experiments – Grid Position ”

  1. Thanks for sharing Shinsuke. A really handy expression.

    Rather than relying on layer names perhaps a slightly more flexible version would be to use the power of the layer index command.

    objNum = 6; //number of objects in a line
    pos = thisComp.layer(1).transform.position; //leader’s position
    num = thisLayer.index – 1;
    repeatX = num % objNum;
    repeatY = Math.floor(num / objNum);
    gapX = 200; //pixel
    gapY = 200; //pixel
    x = gapX * repeatX;
    y = gapY * repeatY;
    pos + [x, y]

    1. Thank you for your feedback, Jason.

      Your expression is smart. I also use the index number of layer. But with layer names, it doesn’t depend on the stacking order of layers. So I use them separately in different situations.

      Anyway, your feedback is worth much for this series. So I kindly ask for your continued support.

  2. Hey Shinsuke – just getting into these, they’re great. Here’s a good trick I figured out:

    So I set up a master control layer for this one, with sliders for number per row, X gap, and Y gap. Worked pretty well, except whenever I fidgeted with the slider for the number of rows, it broke the expression because it needed a positive integer (and of course, the sliders jump all over the place, into the negative numbers, and with decimals). Anyway, I eventually figured out this quick/handy snippet that makes the slider round up to a positive whole integer:

    Math.abs(Math.round(thisComp.layer(“grid controls”).effect(“number per row”)(“Slider”)))

    … where “grid controls” is my null control layer, and “number per rows” is the slider control. Probably obvious to you expression guys, but I’m new, and it felt good to figure it out!

    1. Thank you for your feedback, Thom.

      As you pointed out, it causes some expression errors if the value for the number of objects in a row is zero or with decimals. So if you set up a slider for the value, apply this expression for the slider property.

      Math.floor(clamp(value, 1, 100))

      Screenshot of the expression

      “clamp” is a method that can limit the values. It’s so simple and handy! If you want more information about clamp, check out the link below.
      Keep a Clamp on It

      I hope this series help you even just a little.

  3. Thanks for sharing Shinsuke! 🙂

    Inspiring expression and cool site! I have a problem regarding the download: I get a ZIP-File after the download and after extracting the ZIP with several unpackers i only got a *.cpgz-File which again contains a ZIP-File. Tried it on Mac and PC.

    I’d really love to see how you achieved the modified version in the project file. Could you explain that or check your File-Upload?

    All the best,

    1. Thank you for your kind words, Simon.
      And I’m sorry for the trouble. Could you try to download it and extract it again? Because it works for me. And if it doesn’t work for you, please get in touch with me again on my contact page. In that case, I’ll send you the file by attaching it to the email.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s