Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to create a rectangle with expressions

Explorer ,
Sep 13, 2023 Sep 13, 2023

Hi, I am just starting with after effects and and expressions... Sorry if this is really basic
I am trying to create a rectangle with expressions, but cannot work out how. I would like to be able to set the width, height and position of the rectangle.

 

The idea being to duplicate the layer many times and change values for each layer.

What would be the best way to do it?

Thanks!

TOPICS
Expressions , How to
2.3K
Translate
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

correct answers 1 Correct answer

Community Expert , Sep 13, 2023 Sep 13, 2023

You need to start with a path so you have a place to put the expression. Select the pen tool, make sure no layers are selected, and Click a single point anywhere in the Comp panel to create a new shape layer and a path. Then press the 'u' key twice to open the path property and add the expression.

 

If I were doing something like you are suggesting, I would use a parametric shape (rectangle) on a text layer, add a couple of point controls to the shape layer, and label one "Height and Width,} and

...
Translate
Community Expert ,
Sep 13, 2023 Sep 13, 2023

A path expression like this would let you define the width, height, and position (in comp coordinates). You could tie those to sliders and a point control:

w = 500; // width
h = 250; // height
p = [500,500]; // position

center = fromComp(p);
UL = center + [-w,-h]/2;
UR = center + [w,-h]/2;
LR = center + [w,h]/2;
LL = center + [-w,h]/2;

createPath([UL,UR,LR,LL],[],[],true)
Translate
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
Explorer ,
Sep 13, 2023 Sep 13, 2023

Thank you Dan! I will give it a go.

I don't know how to do sliders / point control, I will look for a tutorial. If you have any suggestions it would be greatly appreciated.

Thanks

Translate
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 Expert ,
Sep 13, 2023 Sep 13, 2023

You'd add two slider controls to the layer: Effect > Expression Controls > Slider Control and rename them Width and Height, then add a point control: Effect > Expression Controls > Point Control and rename it Position. Then modify the path expression to this:

w = effect("Width")("Slider");
h = effect("Height")("Slider");
p = effect("Position")("Point");

center = fromComp(p);
UL = center + [-w,-h]/2;
UR = center + [w,-h]/2;
LR = center + [w,h]/2;
LL = center + [-w,h]/2;

createPath([UL,UR,LR,LL],[],[],true)

 

Translate
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
Explorer ,
Sep 13, 2023 Sep 13, 2023

Thank you!

Translate
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
Explorer ,
Sep 13, 2023 Sep 13, 2023

For some reason I keep getting:

"Error: ReferenceError: createPath is not defined"

I am not sure what I am doind wrong? This happens before and after adding Sliders and Point Controls.

Translate
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 Expert ,
Sep 13, 2023 Sep 13, 2023

Are you adding the expression to a path property? I just click in an empty comp window with the pen tool and then twirl open in the timeline until I see the path property. Add the controls, then drop in the expression.

Screenshot 2023-09-13 102531.png

Translate
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 Expert ,
Sep 13, 2023 Sep 13, 2023

You need to start with a path so you have a place to put the expression. Select the pen tool, make sure no layers are selected, and Click a single point anywhere in the Comp panel to create a new shape layer and a path. Then press the 'u' key twice to open the path property and add the expression.

 

If I were doing something like you are suggesting, I would use a parametric shape (rectangle) on a text layer, add a couple of point controls to the shape layer, and label one "Height and Width,} and the other "Position." Save the rectangle and effects as an animation preset. Double-click the preset to add a new shape layer and set the size and position using the two effects Point Controls.

 

This is the expression and a screenshot.

// Rectangle 1/Size
effect("Rectangle Size")("Point")

// Rectangle 1/Position
p = effect("Position")("Point");
[p[0] - thisComp.width/2, p[1] - thisComp.height/2]

RickGerard_0-1694627516354.png

You could do the same thing with Dan's expression. 

 

The downside to this approach is that you could only automatically create a single rectangle on a single layer with the preset. Duplicating the Rectangle (or path) on a single shape layer and duplicating the controllers would give you the option of having more than one shape on a single layer, but you would have to edit the duplicate rectangles' position and size expressions to link them to the duplicate sliders.

 

 

 

 

 

 

Translate
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
Explorer ,
Sep 13, 2023 Sep 13, 2023
LATEST

Thank you very much Richard!

Translate
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