Free Function Friday Ep.6 – collectAllPrecomps

October 30th 2015 08:00:32am

Welcome to this weeks episode of Free Function Friday. Today we will build a function to collect all of the precomp objects in a project. The difference between a precomp and a composition, if you are unaware, is that a precomp is a composition that resides within another composition as a layer.

Precomps are a handy way of organizing your layers as you create, and in some cases it is required to perform a certain effect or process. When scripting there may be the need to know which compositions are being used within another composition. One such case is if you are organizing your project into folders via script, as I did in this post for Organizing Project Assets.

I used a slight variant of the collectAllPrecomps function in that script to first gather them and then looped through those results and set their parent folder to the same location. This function was also altered and used in different variations to also gather other types of project items to sort them as well.

Since you will have a collection of CompItems after running the function, you could then send those comps through a loop and read and/or write any of the attributes related to a CompItem object. Those attributes as of After Effects CC 2015 would be…

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

//CompItem Property options...
comp.frameDuration; //The duration of a single frame.
comp.dropFrame; //When true, indicates that the composition uses drop-frame timecode.
comp.workAreaStart; //The work area start time.
comp.workAreaDuration; //The work area duration.
comp.numLayers; //The number of layers in the composition.
comp.hideShyLayers; //When true, shy layers are visible in the Timeline panel.
comp.motionBlur; //When true, motion blur is enabled for this composition.
comp.draft3d; //When true, Draft 3D mode is enabled for the Composition panel.
comp.frameBlending; //When true, time filtering is enabled for this composition.
comp.preserveNestedFrameRate; //When true, the frame rate of nested compositions is preserved.
comp.preserveNestedResolution; //When true, the resolution of nested compositions is preserved.
comp.bgColor; //The background color of the composition.
comp.activeCamera; //The current active camera layer.
comp.displayStartTime; //Changes the display of the start time in the Timeline panel.
comp.resolutionFactor; //The factor by which the x and y resolution of the Composition panel is downsampled.
comp.shutterAngle; //The camera shutter angle.
comp.shutterPhase; //The camera shutter phase.
comp.motionBlurSamplesPerFrame; //The minimum number of motion blur samples per frame for Classic 3D layers, shape layers, and certain effects.
comp.motionBlurAdaptiveSampleLimit; //The maximum number of motion blur samples of 2D layer motion.
comp.layers; //The layers of the composition.
comp.selectedLayers; //The selected layers of the composition.
comp.selectedProperties; //The selected properties of the composition.
comp.renderer; //The rendering plug-in module to be used to render this composition.
comp.renderers; //The set of available rendering plug-in modules.
comp.duplicate(); //Creates and returns a duplicate of this composition.
comp.layer(index); //Gets a layer from this composition by index.
comp.layer(name); //Gets a layer from this composition by name.
comp.layer(otherLayer, relIndex); //Gets a layer relative to another layer.
comp.openInViewer(); //Opens the composition in a Composition panel.

So you can quickly start to see how you could easily set all precomp frame rates, or change precomp sizes, even set the precomp workarea start and end. There are lots of possibilities.

Source Code:

alert(collectAllPrecomps());
/*	TESTING ABOVE	*/

function collectAllPrecomps(){
	var proj, itemTotal, curItem, itemAry, code;
	itemAry = new Array();
	proj = app.project;
	itemTotal = proj.numItems;
	for(var i = 1; i <= itemTotal; i++){
		curItem = proj.item(i);
		if(curItem instanceof CompItem){
			if(curItem.usedIn.length > 0){
				itemAry[itemAry.length] = curItem;
			}
		}
	}
	return itemAry;
}
Checkout more:
X-Particles Polygon Disintegration
Expression Shorts Read External Text Document
Basic 2D Face Replacement
Houdini Mardini Crash
Free Function Friday Ep.2 – getLayerEffects