Knowledge of “Scripting for After Effects” can become very handy when you have to do lots of repetitive tasks. As an example, you are creating a design and you need to create lots of cubes in this project. We know creating a cube in After Effects is very easy. Just take a few 3d solid and rearrange to make a 3d cube. But imagine you need to create ten, twenty, thirty or hundreds of cubes. Now it will be pretty boring. Here an after effects script comes handy.

After Effects scripts are written in javascript. After Effects even comes with a code editor “Extendscript” which makes scripting very easy as you can run your script from the editor while developing. If you are not comfortable with “Extendscript”, you can also use any editor of your choice.

Knowledge of javascript will be very to write a script. But if you are not familiar with javascript like me, don’t worry, it pretty easy language and there are plenty of tutorials on google.it pretty easy language and there are plenty of tutorials on google.

Now let’s start with the fun stuff. Here we will be writing a script to create a 3D cube in after effects. I will go through a step by step process and try to explain it as per my knowledge. Quick note for the pros, I am not a pro in scripting, so please correct me if I make any mistake.

First, we need to address a composition in after effects. So we will be creating few variables. Variables are containers for storing data values.If you don’t understand now, don’t worry it will make sense in a while.be creating few variables. Variables are containers for storing data values.If you don’t understand now, don’t worry it will make sense in a while.

So first we create a variable to address a composition. It looks like this

var actComp = app.project.activeItem;

Here we are looking for an active comp. It is same as a compositing that is selected. We gave our variable a name, “actComp”. This can be anything.compositing that is selected. We gave our variable a name, “actComp”. This can be anything.

Think of the process while you create a 3d Cube in after effects manually. Our script also will do the same process automatically.

So when we have a Composition, now we need to create few solids which will represent six sides of a cube. We also need to specify the width and height of the solid. And this should be equal to the width and height of the cube. For this example, we will create a simple 400/400 cube. So we need six solid with 400/400 px size.six sides of a cube. We also need to specify the width and height of the solid. And this should be equal to the width and height of the cube. For this example, we will create a simple 400/400 cube. So we need six solid with 400/400 px size.

So this is how we do it. We will be creating 6 solid and store them in individual variables.

var actComp = app.project.activeItem;
var leftSolid= actComp.layers.addSolid(color, name, width, height, pixelAspect, duration);

Above is the snippet to create a solid. Here we need to enter few parameters. the color of the solid, name of the solid, width of the solid, height of the solid, pixel aspect ratio, and duration of the solid in second.need to enter few parameters. the color of the solid, name of the solid, width of the solid, height of the solid, pixel aspect ratio, and duration of the solid in second.

Here is the edited version of the script.

var actComp = app.project.activeItem;
var leftSolid= actComp.layers.addSolid([1,1,1], "LeftSolid", 400, 400, 1, 5);
var rightSolid= actComp.layers.addSolid([1,1,1], "RightSolid", 400, 400, 1, 5);
var frontSolid= actComp.layers.addSolid([1,1,1], "FrontSolid", 400, 400, 1, 5);
var backSolid= actComp.layers.addSolid([1,1,1], "BackSolid", 400, 400, 1, 5);
var topSolid= actComp.layers.addSolid([1,1,1], "TopSolid", 400, 400, 1, 5);
var bottomSolid= actComp.layers.addSolid([1,1,1], "BottomSolid", 400, 400, 1, 5);

Here I have created six solid. Check the structure of the script. I have created solid with white color [1,1,1], Name of the solid “LeftSolid”, solid width (400px), solid height (400px), pixel aspect ratio (1), solid duration (5 sec).
Now we need to make all the layers 3D layers. it is very simple. Here is how we do it.

var actComp = app.project.activeItem;

var leftSolid= actComp.layers.addSolid([1,1,1], "LeftSolid", 400, 400, 1, 5);
var rightSolid= actComp.layers.addSolid([1,1,1], "RightSolid", 400, 400, 1, 5);
var frontSolid= actComp.layers.addSolid([1,1,1], "FrontSolid", 400, 400, 1, 5);
var backSolid= actComp.layers.addSolid([1,1,1], "BackSolid", 400, 400, 1, 5);
var topSolid= actComp.layers.addSolid([1,1,1], "TopSolid", 400, 400, 1, 5);
var bottomSolid= actComp.layers.addSolid([1,1,1], "BottomSolid", 400, 400, 1, 5);

leftSolid.threeDLayer=true;
rightSolid.threeDLayer=true;
frontSolid.threeDLayer=true;
backSolid.threeDLayer=true;
topSolid.threeDLayer=true;
bottomSolid.threeDLayer=true;

Now open after effects and create a composition. Now select the composition and if you run the script, you can see, it has created six solid with respective name and all the layers are 3D.

Now we need to rearrange these layers to make our 3d Cube. First, we will rotate all the layers except front layer.


var actComp = app.project.activeItem;

var leftSolid= actComp.layers.addSolid([1,1,1], "LeftSolid", 400, 400, 1, 5);
var rightSolid= actComp.layers.addSolid([1,1,1], "RightSolid", 400, 400, 1, 5);
var frontSolid= actComp.layers.addSolid([1,1,1], "FrontSolid", 400, 400, 1, 5);
var backSolid= actComp.layers.addSolid([1,1,1], "BackSolid", 400, 400, 1, 5);
var topSolid= actComp.layers.addSolid([1,1,1], "TopSolid", 400, 400, 1, 5);
var bottomSolid= actComp.layers.addSolid([1,1,1], "BottomSolid", 400, 400, 1, 5);

leftSolid.threeDLayer=true;
rightSolid.threeDLayer=true;
frontSolid.threeDLayer=true;
backSolid.threeDLayer=true;
topSolid.threeDLayer=true;
bottomSolid.threeDLayer=true;

leftSolid.property("Transform").property("Y Rotation").setValue(90);
rightSolid.property("Transform").property("Y Rotation").setValue(-90);
topSolid.property("Transform").property("X Rotation").setValue(-90);
bottomSolid.property("Transform").property("X Rotation").setValue(90);
backSolid.property("Transform").property("Y Rotation").setValue(180);

Now we need to offset the position of layers. Here is the snippet to set the position.


leftSolid.property("Transform").property("Position").setValue.setValue([x,y,z]);

We need to enter three value in the array [x,y,z]. We can simply enter 3 position value but we want it to make little dynamic.value in the array [x,y,z]. We can simply enter 3 position value but we want it to make little dynamic.
Here we will take help of width and height of the composition to calculate the position of solids. To get the width and height of the composition we can just enter actComp.width or actComp.height.help of width and height of the composition to calculate the position of solids. To get the width and height of the composition we can just enter actComp.width or actComp.height.


leftSolid.property("Transform").property("Position").setValue([(actComp.width/2)-(400/2),(actComp.height/2),(400/2)*1]);

now check this line, how I have calculated the position of solid.

x= (actComp.width/2)-(400/2) that is same as (1920/2)-(400/2) and that is equal to “760”. Here “400” equal to our cube width.actComp.width/2)-(400/2) that is same as (1920/2)-(400/2) and that is equal to “760”. Here “400” equal to our cube width.
y = (actComp.height/2) that is equal to (1080/2) and that is equal to “540”.
z = (400/2) that is equal to “200”. here 400 = cube depth.

so altogether position value for the leftSolid becomes [760,540.200].

As my composition is a full HD with “1” pixel aspect ratio, it works fine. But if the pixel aspect ratio of your composition is not “1”, then our z value need to be multiplied with pixel aspect ratio. In that case, z value will be,need to be multiplied with pixel aspect ratio. In that case, z value will be,

z= (400/2)*1)*1

So here is the full script that set positions of all the layers.set positions of all the layers.


leftSolid.property("Transform").property("Position").setValue([(actComp.width/2)-(400/2),(actComp.height/2),(400/2)*1]);
backSolid.property("Transform").property("Position").setValue([(actComp.width/2),(actComp.height/2),400*1]);
rightSolid.property("Transform").property("Position").setValue([(actComp.width/2)+(400/2),(actComp.height/2),(400/2)*1]);
topSolid.property("Transform").property("Position").setValue([(actComp.width/2),(actComp.height/2)-(400/2),400/2]);
bottomSolid.property("Transform").property("Position").setValue([(actComp.width/2),(actComp.height/2)+(400/2),400/2]);

Now select your composition and run the script and see the magic. You have created your first script for after effects to create a 3D cube.

Here is the full script


var actComp = app.project.activeItem;

var leftSolid= actComp.layers.addSolid([1,1,1], "LeftSolid", 400, 400, 1, 5);
var rightSolid= actComp.layers.addSolid([1,1,1], "RightSolid", 400, 400, 1, 5);
var frontSolid= actComp.layers.addSolid([1,1,1], "FrontSolid", 400, 400, 1, 5);
var backSolid= actComp.layers.addSolid([1,1,1], "BackSolid", 400, 400, 1, 5);
var topSolid= actComp.layers.addSolid([1,1,1], "TopSolid", 400, 400, 1, 5);
var bottomSolid= actComp.layers.addSolid([1,1,1], "BottomSolid", 400, 400, 1, 5);

leftSolid.threeDLayer=true;
rightSolid.threeDLayer=true;
frontSolid.threeDLayer=true;
backSolid.threeDLayer=true;
topSolid.threeDLayer=true;
bottomSolid.threeDLayer=true;

leftSolid.property("Transform").property("Y Rotation").setValue(90);
rightSolid.property("Transform").property("Y Rotation").setValue(-90);
topSolid.property("Transform").property("X Rotation").setValue(-90);
bottomSolid.property("Transform").property("X Rotation").setValue(90);
backSolid.property("Transform").property("Y Rotation").setValue(180);

leftSolid.property("Transform").property("Position").setValue([(actComp.width/2)-(400/2),(actComp.height/2),(400/2)*1]);
backSolid.property("Transform").property("Position").setValue([(actComp.width/2),(actComp.height/2),400*1]);
rightSolid.property("Transform").property("Position").setValue([(actComp.width/2)+(400/2),(actComp.height/2),(400/2)*1]);
topSolid.property("Transform").property("Position").setValue([(actComp.width/2),(actComp.height/2)-(400/2),400/2]);
bottomSolid.property("Transform").property("Position").setValue([(actComp.width/2),(actComp.height/2)+(400/2),400/2]);

Now just play with it. You can store width, height of the cube in variables and use those variables in your script. You can also download my Free MCube Script. The code is open so you can look at it and can implement on your work. Hopefully, this was helpful.