Free Function Friday Ep.31 – removeUnusedEffects

April 22nd 2016 08:00:25am

Update 4/22/2020: Code has been streamlined to be more efficiant. See source code below.

Welcome to a new episode of Free Function Friday. This function is large enough that it warrants two nested functions to streamline the process. The combination of these two function will return index data for the comp, layer, and effect to be removed. This data will then be processed by the main function to do the removal.

There will be a few scenarios to code for in this function, all relating to how a plugin is determined to be unused. This can be as simple as the plugin effect icon being turned off, thereby preventing it from rendering on a layer. It can also be something more complicated as a layer that is turned off, but happens to be a Track Matte, therefore making plugins still in possible use.

We will need to keep this all in mind as we code because it all effects the process of how we build this function. The more organized we are with it, the easier it will be to build. this episode is definitely on of the longest we’ve had in this series so far coming in at just over 53 minutes. I tried to get through as fast as I could, but there is just a lot on content for it.

The good news is you only have to build it once. Right? Being hands on like this and actually writing the code helps the learning process. Repetition, while annoying at times, does help it sink into the brain.

Source Code: Updated to streamline the code.

app.beginUndoGroup("Remove unused effects.");
	removeUnusedEffects();
app.endUndoGroup();
writeLn("Process complete.");

function removeUnusedEffects(){
	var unusedFXIndices = gatherUnusedEffectsIndices();
	var unusedFXIndicesLen = unusedFXIndices.length;
	if(unusedFXIndicesLen > 0){
		unusedFXIndices = unusedFXIndices.reverse();
		for(var r = 0; r < unusedFXIndicesLen; r++){
			itemIndex = unusedFXIndices[r][0];
			layerIndex = unusedFXIndices[r][1];
			fxIndex = unusedFXIndices[r][2];
			app.project.item(itemIndex).layer(layerIndex).property("ADBE Effect Parade").property(fxIndex).remove();
		}
	}

	function gatherUnusedEffectsIndices(){
		var proj, itemCount, curItem, curComp, layerAry, layerCount, curLayer, effectGroup, effectCount, curEffect, r;
		proj = app.project;
		itemCount = proj.numItems;
		r = new Array();

		for(var i=1; i<=itemCount; i++){
			curItem = proj.item(i);
			if(curItem instanceof CompItem){ //Check if it's a comp
				curComp = curItem;
				layerAry = curComp.layers;
				layerCount = layerAry.length;
				for(var l=1; l<=layerCount; l++){ //Loop through comp layers
					curLayer = layerAry[l];
					if(!(curLayer instanceof CameraLayer) && !(curLayer instanceof LightLayer)){
						effectGroup = curLayer.property("ADBE Effect Parade");
						effectCount = effectGroup.numProperties; //Count layer plugins

						if(effectCount > 0){/*Has effects*/
							if(curLayer.effectsActive === false){/*Main is off, so Remove ALL FX*/
								for(var e=1; e<=effectCount; e++){
									curEffect = curLayer.property("ADBE Effect Parade").property(e);
									r.push([i, l, e]);
								}
							}else{
								if(curLayer.enabled === false && curLayer.isTrackMatte === false){/*Remove ALL FX*/
									for(var e=1; e<=effectCount; e++){
										curEffect = curLayer.property("ADBE Effect Parade").property(e);
										r.push([i, l, e]);
									}
								}else if(curLayer.enabled === false && curLayer.isTrackMatte === true){/*Remove only off FX*/
									for(var e=1; e<=effectCount; e++){
										curEffect = curLayer.property("ADBE Effect Parade").property(e);
										if(curEffect.enabled === false){
											r.push([i, l, e]);
										}
									}
								}else{
									for(var e=1; e<=effectCount; e++){/*Remove only off FX*/
										curEffect = curLayer.property("ADBE Effect Parade").property(e);
										if(curEffect.enabled === false){
											r.push([i, l, e]);
										}
									}
								}
							}
						}
					}
				}
			}
		}
		return r;
	}
}
Checkout more:
Free Function Friday Ep.1 – Intro
Expression Shorts Sample Images Part 1
Flow Volume 1
After Effects ExtendScript Training: Ep. 2
X-Particles Polygon Disintegration