Creating 3D Cube with script in After Effects

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 that 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, its 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 some variables. Variables are containers for storing data values. If you don’t understand now, don’t worry it will make sense in a while we create some 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 the same as the composition that is selected. We gave our variable a name, “actComp”. This can be any composition that is selected.

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 a 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 width and height.

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 a 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);

 

Now 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’s 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. Select the composition and run the script, you will 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 the 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 values in the array [x,y,z]. We can simply enter 3 position value but we want it to make little dynamic.
We will take the help of the 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 the 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 the 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 aspect ratio “1”, it works fine. But if the pixel aspect ratio of your composition is not “1”, then our z value needs to be multiplied with the pixel aspect ratio. In that case, z value will be

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

So here is the full script that sets 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 the width, the 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 it in your work. Hopefully, this was helpful.

Share your thoughts