Free Function Friday Ep.30 – addCompMarker

April 15th 2016 08:00:24am

Update 4/22/2020: Added native method for AE CC2017 and newer. Source code below

addCompMarker is a function that will add a marker to your composition timeline. From a scripting perspective this is a something that natively does not exist in After Effects. We have access to layer markers, and can even set their comment, duration, chapter, cuePointName, eventCuePoint, URL, and frameTarget attributes. Composition markers however, we have no access to. So how do we build a function for something that has no native access? Well, we cheat, and by cheat I mean we venture into menu command id territory.

You’ve probably heard me warn about not doing this in the past, but today we are using

app.executeCommand()

I try my best to avoid using this method due to it’s somewhat unstable nature, but sometimes you just have to go with what works if no other options exist. As far as I know there are no other ways to do this either. executeCommand() is a method that basically launches a menu command as if you clicked on it with your mouse cursor. In today’s function we need the “Add Marker” menu item to make this all work. Hopefully the app developers will not change it’s ID value, which is 2157 currently. Better yet, if they do change it, I hope it’s because they are adding a native option to ExtendScript, so we don’t have to find back doors like this.

Another feature that we need to add to this function to make sure it works properly, is layer selection retrieval. To add a comp marker successfully, no layers can be selected. Since we cannot guarantee that a user will always deselect layers at the right time, we need to do it for them. We also don’t want to be rude and will need to re-select the same layers after we add the comp marker.

Doing this will create a fairly seamless experience for the user. One slight side effect of the deselect then re-select process is that the user may see a quick flash of the layers that were selected. It really depends on how large the timeline is and how many layers are selected at the time. You can’t even see it sometimes in small timelines. Regardless, I see that as a trivial blip for having the ability to get a comp marker created via scripting.

Confused by the After Effects version names below? I know, it’s annoying. You can get some clarity here as to what version is called what here.

Source Code: (For AE CC12 – CC13.8 [aka:CC2015.3])

var compObj = app.project.item(1);
addCompMarker(compObj);

function addCompMarker(compObj){
	var layers = compObj.layers;
	var layersLen = layers.length;
	var selLayers = new Array();
	var curLayer;

	for(var i=1; i<=layersLen; i++){
		curLayer = compObj.layer(i);
		if(curLayer.selected === true){
			selLayers.push(curLayer);
			curLayer.selected = false;
		}
	}
	try{
		app.executeCommand(2157);
	}catch(e){return null};

	var selLayersLen = selLayers.length;
	if(selLayersLen > 0){
		for(var ii = 0; ii < selLayersLen; ii++){
			selLayers[ii].selected = true;
		}
	}
	return true;
}

Source Code: (For AE CC14.0 [aka:CC2017]- newer)


var compObj = app.project.item(1);
addCompMarker(compObj, compObj.time, "CompMarker", 1, "URL_PATH");

function addCompMarker(compObj, time, text, duration, url){
	var compMarker;
	if(text){
		compMarker = new MarkerValue(text);
	}else{
		compMarker = new MarkerValue(""); //Blank text
	}

	if(duration){
		compMarker.duration = duration;
	}

	if(url){
		compMarker.url = url;
	}

	/*
		Other attributes include:
		.chapter
		.comment
		.cuePointName
		.duration
		.eventCuePoint
		.frameTarget
		.length
		.name
		.url
	*/

	compObj.markerProperty.setValueAtTime(time, compMarker);
}
Checkout more:
Basic 2D Face Replacement
Free Function Friday Ep.8 – moveItemToFolder
Null Swapper After Effects Script
X-Particles Fireworks
Houdini Lock Camera To View Hotkey