Traversing Selected Layers in After Effects Scripts

I always forget how to do this so I am posting it here.

app.beginUndoGroup("Layer Loop")

function Main() {
    var mySelectedItems = [];
    var comp = app.project.activeItem;
    if (comp instanceof CompItem && comp.selectedLayers.length > 0) {
        for (n = 0; n < comp.selectedLayers.length; n++) {
            myLayer = comp.selectedLayers[n]

Duration of the next active layer.

nextActiveLayer = thisLayer;
for (i = thisLayer.index + 1; i <= thisComp.numLayers; i++) {
	if (thisComp.layer(i).active) {
		nextActiveLayer = thisComp.layer(i);
duration = nextActiveLayer.outPoint - nextActiveLayer.inPoint;
timeToFrames(duration, fps = 1.0 / thisComp.frameDuration, isDuration = true)

Apply this expression to the Source Text property of a text layer at the top of a stack. The for loop in here iterates down from the top to find the first layer that is active and gets its duration.

Parallax offset from a tracked Null

trackLayer = thisComp.layer("MotionTracked")
trackPosition = trackLayer.transform.position
trackDiff = trackLayer.transform.position.valueAtTime(trackLayer.inPoint)
trackAdd = trackPosition - trackDiff
transform.position +trackAdd*.5

I use this expression to add a parallax offset to a motion track. Apply your track to a Null called MotionTracked and crop the layer inPoint to your reference point.

Duration of layer expression

duration = outPoint-inPoint
timeToFrames(duration, fps = 1.0 / thisComp.frameDuration, isDuration = true)

Here’s a quick expression I like to use with the Numbers effect in AE to print the frame duration on the image when I need to add graphics to an edited string out. It helps with shot planing.

A fast way to convert to plain text on Mac OS X

Copy your text and run this code in the terminal.

What is it doing? This command pastes your text from the “pasteboard” (hence the pb in pbpaste and pbcopy) and pipes (|) it back to the pasteboard. Since these commands don’t support richtext by default it gets stripped.

I use this sometimes when I have a lot of little pieces to cut and paste. It’s easy to command-tab to the terminal. Press up to run the previous command and press enter. Some applications have a special shortcut command (Ctrl-Shift-V) to do this but many don’t.

How to create a candystripe material in C4D


Click to download the C4D material.

The trick is to create the simplest tiling texture you can using a gradient. Using a gradient instead of an image based texture makes the material procedural which means you can scale it to any size without seeing artifacts.

The number of stripes you want on your object can then be adjusted using the texture tag’s tiling settings.

My After Effects Folder Setup

One of the first things that I do when starting a project is set up my folder structure using the Folder Setup.jsx after effects script.
Here is my setup
0 Render
1 Main
2 Source
3 PreComp
4 PreRenders
5 Tests

Each Folder on the base layer is proceeded by a number. This keeps everything in order consistently. Any new folders you create in the base layer will automatically jump to the bottom. If you want to jump a folder temporarily to the top of the list you can add an underscore. The problem with using something like * is that it is a special character in unix and will mess with the backup system I use at work. This isn’t a problem in After Effects until you collect the project and end up with a system folder that begins with * –> BAD.

’0 Render’

’0 Render’ is for comps that directly get rendered to output and ’1 Main’ is for the comps that contain most of your work. Often times ’0 Render’ will contain nothing more than one layer. That layer would be a comp from ’1 Main’. The point of this is that the render work area for the comps in ’0 Render’ never change whereas the work areas in the ’1 Main’ folder change constantly as you are adjusting animations. Other things that this setup is good for is if you want to view your main comp in another aspect ratio like center-cut or letterboxed or if you want to apply a global effect to your comp before you render like a grade or a broadcast safe filter.

Ideally all final renders should come from this ’0 Render’ folder and their name should be similar to the comp name so it is easy to tell where exactly the renders came from.

’1 Main’

’1 Main’ is where most of your comps reside. You can create folders in here to further organize and sort your project. Comps that you access frequently go in here even if they are precomps.

’3 PreComp’

’3 PreComp’ are for deep PreComps that don’t get accessed much or for precomps that only contain one or two layers which you have to create due to the way After Effects processes filters.

’2 Source’

All your sources go here. Why is it labeled ’2 Source’? because most of the time you will be jumping from your ’1 Main’ folder to your ’2 Source’ folder pulling assets into your comps. ’3 Precomp’ is accessed much less frequently.Video goes in Video, audio in Audio, Image contains stills, Layers contains photoshop or illustrator files that are brought in with separate layers, the comp that is created when these files are brought in also stays here.Reference contains references, either stills that you want to look refer to, guide layers or a edited cut to time out animation to. References don’t get directly used to create a final render. If a cut is mixed with animation to create a final output should go in Video.Sequence contains image sequences, this is usually only applicable when bringing in 3d renders. I sometimes also create a Vector folder for vector art work.

’4 PreRenders’

I use prerenders for things such as animated backgrounds or really intensive effects a lot. It really speeds up scrubbing in the time line and will speed up your final rendertime as well, allowing you to go through more interactions in a shorter period of time. Once you’ve nailed something down there is no need to keep rendering it over and over again.

’5 Tests’

A lot of time I’ll test an idea or effect away from my main comps so I don’t mess anything up. Those experiments go in here. These aren’t fully fledged comps, more like ‘What if I try this’. Even if I abandon them I keep them around here in case I come back to an idea later in the project or much much later after a project has finished.

I know everyone has their own style of working. This is just mine that I’ve settled on for a couple of years and I think it works pretty well. That said, I’m always looking to improve it.

Epic Wave in Teahupoo

This wave looks like it would easily kill you.

I remember seeing this break in the Riding Giants documentry so while I was sailing across the pacific I was really excited to visit it. It is at the very end of the only highway in Tahiti. It seemed like the end of the earth.

There were no big waves when we were there but we talked to 2 Americans who had rented a house on the point and complained that the whole place was getting too crowded.

I'm the one with the beard