ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Script - Use name instead of indexed number

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Sergio Cabrera
Script - Use name instead of indexed number
on May 14, 2019 at 12:46:03 am

When we refer to an item in our project we use app.project.item(1), how can I do it like app.project.item("ItemName")?

What I need to do from the script is:

1) There's a comp named "Comp to duplicate" I need to duplicate it in the project (not the timeline) without using the item number.
2) Change the name of the new duplicated comp to "Duplicated Layer".
3) Apply some color changes that I already know how to do on app.project.item("Duplicated Layer").

The thing is if I duplicate a comp it change the index # of the other comps, that's why I want to start using comp names instead of index numbers. Is there any way to do this?


Return to posts index

Dan Ebberts
Re: Script - Use name instead of indexed number
on May 14, 2019 at 1:05:59 am

I just use a little function like this:


function getComp(theName){
for (var i =1; i <= app.project.numItems; i++){
if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name == theName)){
return app.project.item(i);
}
}
return null;
}

// test
var compName = "Comp 1";
var myComp = getComp(compName);
alert(compName + (myComp == null ? " not " : " ") + "found.");



Dan



Return to posts index

Sergio Cabrera
Re: Script - Use name instead of indexed number
on May 14, 2019 at 2:30:51 am
Last Edited By Sergio Cabrera on May 14, 2019 at 2:33:03 am

There is kinda a loop or I'm not understanding the function, I start by knowing the comp name and at the end of the function I get the same name I knew at the start.

alert (myComp); would retrieve: [object CompItem] instead of app.project.item(1) which is "Comp 1"

How do I add some actions to the "Comp 1" composition? Can you show me an example of renaming "Comp 1" composition name or some other action to get a clear idea?

I was hoping to do something like this:

var newname = "Comp 2"

function renameComposition(newName) {
var curComp = null;
curComp = app.project.item(1);
curComp.name = newName;
}

renameComposition(Comp 2)


But replacing app.project.item(1); with myComp:

var compositionName = "Comp 2"

function renameComposition(newName) {
var curComp = null;
curComp = myComp
curComp.name = newName;
}

renameComposition(compositionName)


Return to posts index


Sergio Cabrera
Re: Script - Use name instead of indexed number
on May 14, 2019 at 2:38:42 am
Last Edited By Sergio Cabrera on May 14, 2019 at 2:40:00 am

Working great, sorry, I was having problems understanding the function, my ego would like to blame the lack of coffee. Thank you so much Dan, you're a god at this! You make it look so easy.

function getComp(theName){
for (var i =1; i <= app.project.numItems; i++){
if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name == theName)){
return app.project.item(i);
}
}
return null;
}

// test
var compName = "Comp1";
var myComp = getComp(compName);


var NewCompositionName = "Comp 2"

function renameComposition(newName) {
var curComp = null;
curComp = getComp(compName);
curComp.name = newName;
}

renameComposition(NewCompositionName)


Return to posts index

Dan Ebberts
Re: Script - Use name instead of indexed number
on May 14, 2019 at 4:16:59 am

If all you want to do is rename the comp, this should be all you need:


function renameComp(oldName,newName){
for (var i =1; i <= app.project.numItems; i++){
if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name == oldName)){
app.project.item(i).name = newName;
break;
}
}
}



Dan



Return to posts index

Sergio Cabrera
Re: Script - Use name instead of indexed number
on May 14, 2019 at 3:35:23 pm

Thank you. One more question, can copyToComp() be used to copy a comp to another comp or it only works with layers?


Return to posts index


Dan Ebberts
Re: Script - Use name instead of indexed number
on May 14, 2019 at 3:57:02 pm

.copyToComp() is only for layers. You could use .add() to add a comp as a layer to another comp.

Dan



Return to posts index

Sergio Cabrera
Re: Script - Use name instead of indexed number
on May 16, 2019 at 2:10:19 am

Thank you Dan!


Return to posts index

Tomas Bumbulevičius
Re: Script - Use name instead of indexed number
on May 18, 2019 at 3:02:46 pm

Hey Dan, what would you recommend for cases, when there are two, identically named comps? (Apart from not making two identical names, hehe!) By default it picks up the first one in the project items array.

Find out more:
After Effects Tutorials: motion design, expressions, scripting.
Boxer - Dynamic Text Boxes Template with a Live Preview


Return to posts index


Dan Ebberts
Re: Script - Use name instead of indexed number
on May 18, 2019 at 6:06:33 pm

I guess you could modify the function to return an array of all comps with that name (or an empty array if there aren't any).

Dan



Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]