Free Function Friday Ep.10 – resizeComp

November 27th 2015 08:00:28am

Free Function Friday is upon us yet again, time for the weekend, and some free ExtendScript code learning. Today we build a function to resize a composition. You would think this is a simple straight forward task, but nope. You could use the native attributes wideth and height for a CompItem object.

var comp = app.project.item(1); //Assumes first project item is a comp.

//Get width
var getWidth = comp.width;

//Get height
var getHeight = comp.width;

//--------------------------------

//Set width
comp.width = 1920;

//Set height
comp.height = 1080;

You can set the size of the comp that way. Super simple and fast, but there is one issue, the layers in your comp may not look how you would expect them to afterwards.

When you resize a composition in After Effects through it’s dialog window, the comp will resize from the center outwards. A fairly logical assumption when resizing. If you change the comp size via ExtendScript with the .width and .height attributes mentioned earlier, you’ll notice that it resizes the comp starting from the top left corner which produces a very different result. This has to do with screen coordinates for the viewer panel.

If you reveal the ruler guides in the comp viewer you’ll see that the top left corner is a [0,0] coordinate and the bottom right corner will be whatever size your composition is, like 1920px by 1080px for example.

Adobe was nice enough to do the math for you and automatically default to resizing from the center of the comp and not the true [0,0] point, but in ExtendScript it is up to us to do that math. For this function we are doing some simple offset math and building it to resize from the center of the composition by default, but you are certainly welcome to figure out the alternate offset math required to use the other corners of a comp as an option.

Source Code:

app.beginUndoGroup("CompResize");
	var myComp = app.project.activeItem;
	resizeComp(myComp, 500, 500);
app.endUndoGroup();
/*	TESTING ABOVE	*/

function resizeComp(compObj, newWidth, newHeight){
	if(compObj instanceof CompItem){
		if(typeof newWidth == "number" && typeof newHeight == "number"){
			var locData, allLayers, allLayersLen, curLayer, clAP, clPos, xShift, yShift, oldCW, oldCH, clH, clW, xOff, yOff;
			pData = new Array();
			cWidth = newWidth;
			cHeight = newHeight;
			allLayers = compObj.layers;
			allLayersLen = allLayers.length;
			oldCW = compObj.width;
			oldCH = compObj.height;
			compObj.width = cWidth;
			compObj.height = cHeight;
			for(var i=1; i<=allLayersLen; i++){
				curLayer = allLayers[i];
				clW = curLayer.width;
				clH = curLayer.height;
				clPos = curLayer.transform.position.value;
				clAP = curLayer.transform.anchorPoint.value;
				xShift = (oldCW - cWidth)/2;
				yShift = (oldCH - cHeight)/2;
				curLayer.transform.position.setValue([clPos[0] - xShift, clPos[1] - yShift]);
			}
		}else{
			alert("Invalid argument(2/3):\nInteger required for Width and Height.");
		}
	}else{
		alert("Invalid argument(1):\nComp Object required.");
	}
}
Checkout more:
Houdini Mardini Rural
Houdini | FFX Collection
Expression Shorts Numerical Readout
Advanced 2D Face Replacement
After Effects ExtendScript Training: Ep. 16 part 2