ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

make new null at selected location script.

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Chris Lupton
make new null at selected location script.
on Aug 18, 2019 at 5:56:37 pm

I'm currently trying to create nulls at a user selected location which works fine but how would i go about checking if these nulls already exists when pressed again and increment the name further.

app.beginUndoGroup("Create Target Nulls");
var myComp = app.project.activeItem;
if (myComp != null && (myComp instanceof CompItem)){
var myLayers = myComp.selectedLayers;
for (i=0; i <= myLayers.length-1; i++){

var currentLayer = myLayers[i];

var newNull = myComp.layers.addNull(myComp.duration);
if (currentLayer.threeDLayer) newNull.threeDLayer = true;
if (currentLayer.threeDLayer) {
} else {
} = "Target Null" +"-"+ (i+1); ="Target Null" +"-"+ (i+1);
currentLayer.parent = newNull;
} else {
alert("Select an active comp");

for example with two layers selected currently two nulls at their positions and rotations are created with the names "Target Null-1" and "Target Null-2"

but when i create a new null afterwards with the same script it calls it "Target Null-1" and not "Target Null-3" how would i edit the code so that it checks my composition so that it can name my null correctly?

Thanks for your help

Return to posts index

Dan Ebberts
Re: make new null at selected location script.
on Aug 18, 2019 at 7:18:42 pm

You need to add a function that finds the highest numbered null name and returns the next higher name, like this:

function getNextNullName(theComp){
var n;
var maxN = 0;
var splitText;
for (var i =1; i <= theComp.numLayers; i++){
if (theComp.layer(i).name.indexOf("Target Null-") == 0){
splitText = theComp.layer(i).name.split("-");
n = parseInt(splitText[splitText.length-1],10);
if (! isNaN(n)) maxN = Math.max(maxN,n);
return "Target Null-" + (maxN + 1);

Then instead of this: = "Target Null" +"-"+ (i+1); ="Target Null" +"-"+ (i+1);

you would do something like this: = getNextNullName(myComp); =;

Not tested, but probably close.


Return to posts index

Chris Lupton
Re: make new null at selected location script.
on Aug 21, 2019 at 12:29:23 pm

Major thanks Dan the Man!

I'll give it a go

Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 All Rights Reserved