After Effects script to create a 3D Cube

Published 1 year ago, has comments

This script is used to create a 3d cube. You can later replace the different sides and spin the null around for easy animation. To make it more flexible, you can enter the size of the sides and the script will adjust.

In order for the sides to hold their positions (I've found that during animations, sometimes the sides will slip slightly out of place), they are all positioned with expressions and should keep their place no matter what you do to them.

There is a null object as well, all the sides have been parented to it and should make moving the cube around relatively simple.
//*********************************************************
//this is a script to make a 3d cube with a null object with which to control the cube
//to run, simply have an empty comp ready, select it in the project window,
//and run the script. the cube's sides are held in place through expressions
//so they will 'always' hold their positions.
//thanks to dan ebberts, nicholas white and everyone at creative cow for their help.
//****************************************************
side_size = prompt("how big will each layer be?", "");
side_size /= 1;
offset = side_size / 2;
myComp = app.project.item(1);
for (i = 1; i <= 6; i++) {
solid_name = i;
mySolid = myComp.layers.addSolid([1, .5, .3], "side " + i, side_size, side_size, 1);
mySolid.threeDLayer = true;
solid_anchor_expression = "anchor = [" + offset + "," + offset + ",0]";
mySolid.property("anchorPoint").expression = solid_anchor_expression;
var rot_axis;
for (r = 1; r <= 3; r++) {
switch (r) {
case 1:
rot_axis = "X";
break;
case 2:
rot_axis = "Y";
break;
case 3:
rot_axis = "Z";
}
rot_axis_expression = "rotation" + rot_axis + " = 0";
var rot_property = rot_axis + " Rotation";
mySolid.property(rot_property).expression = rot_axis_expression;
}
var or_array;
switch (i) {
case 1:
or_array = "[0,0,0]";
break;
case 2:
or_array = "[0,90,0]";
break;
case 3:
or_array = "[0,90,0]";
break;
case 4:
or_array = "[0,0,0]";
break;
case 5:
or_array = "[90,0,0]";
break;
case 6:
or_array = "[90,0,0]";
}
or_array_expression = "orientation = " + or_array;
mySolid.property("Orientation").expression = or_array_expression;
var pos_array;
switch (i) {
case 1:
pos_array = "[" + offset + "," + offset + ",0]";
break;
case 2:
pos_array = "[" + side_size + "," + offset + "," + offset + "]";
break;
case 3:
pos_array = "[0," + offset + "," + offset + "]";
break;
case 4:
pos_array = "[" + offset + "," + offset + "," + side_size + "]";
break;
case 5:
pos_array = "[" + offset + ",0," + offset + "]";
break;
case 6:
pos_array = "[" + offset + "," + side_size + "," + offset + "]";
}
pos_array_expression = "position = " + pos_array;
mySolid.property("Position").expression = pos_array_expression;
}
myNull = myComp.layers.addNull();
myNull.threeDLayer = true;
null_anchor_expression = "anchor = [0,0,0]";
myNull.property("anchorPoint").expression = null_anchor_expression;
myComp.layer(1).property("Position").setValue([offset, offset, offset]);
var null_parent;
null_parent = myComp.layer(1);
for (c = 2; c <= 7; c++) myComp.layer(c).parent = null_parent;
alert("done!");