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;
newNull.transform.position.setValue(currentLayer.transform.position.value);
if (currentLayer.threeDLayer) {
newNull.transform.orientation.setValue(currentLayer.transform.orientation.value);
newNull.transform.xRotation.setValue(currentLayer.transform.xRotation.value);
newNull.transform.yRotation.setValue(currentLayer.transform.yRotation.value);
newNull.transform.zRotation.setValue(currentLayer.transform.zRotation.value);
} else {
newNull.rotation.setValue(currentLayer.transform.rotation.value);
}
newNull.source.name = "Target Null" +"-"+ (i+1);
newNull.name ="Target Null" +"-"+ (i+1);
newNull.anchorPoint.setValue([50,50]);
newNull.label=9;
currentLayer.parent = newNull;
}
app.endUndoGroup();
} 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:

newNull.source.name = "Target Null" +"-"+ (i+1);
newNull.name ="Target Null" +"-"+ (i+1);

you would do something like this:

newNull.name = getNextNullName(myComp);
newNull.source.name = newNull.name;

Not tested, but probably close.


Dan



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 CreativeCOW.net All Rights Reserved
[TOP]