Copy link to clipboard
Copied
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!
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
...Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
Thank you!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
 
Copy link to clipboard
Copied
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]
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.
Copy link to clipboard
Copied
Thank you very much Richard!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now