ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Comp And Layer Replacement Script

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Thomas Patrick
Comp And Layer Replacement Script
on Mar 14, 2019 at 4:45:13 pm

Hi There!

I'm trying to replace "_OLD" comps with "_NEW" comps as layers in multiple compositions. I've included an annotated screenshot to explain exactly what I mean. Essentially I'll have a folder full of "_OLD" comps and another folder full of "_NEW" comps. I'll also have a folder full of comps that contain the "_OLD" comps as layers.



What I'm trying to do (with varying degrees of success...) is replace the "_OLD" layers with the "_NEW" layers inside the "REPLACED" comps.

The code below attempts to:
-loop through the all of the comps in the project.
-Loop through all of the layers in each comp.
-If a layer is a comp whose name contains "_OLD" check to see if there is a corresponding "_NEW" comp in the "NEW_COMPS" folder
-If the layer exists, replace the "_OLD" layer in the "REPLACED" comp, else alert("CANT FIND COMP x...")

I thought this logic would replace everything correctly but my JS is apparently atrocious...

Thanks for any help/tips!

Tom

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


function main(){

var newComp;
var newCompName;

for (var i = 1; i &lt;= app.project.numItems; i++) {
if (app.project.item(i) instanceof CompItem) {
for (var l = 1; l &lt;= app.project.item(i).numLayers; l++){
if (app.project.item(i).layer(l) instanceof CompItem) {
newCompName = app.project.item(i).layer(l).name.split("OLD")[0] + "NEW";
newComp = getComp(newCompName);
if (newComp == null){
alert ("Can't find comp '" + newCompName + "'");
return;
}
app.project.item(i).layer(l).replaceSource(newComp,false);
}
}
}
}

}

main();


Return to posts index

Tomas Bumbulevičius
Re: Comp And Layer Replacement Script
on Mar 14, 2019 at 5:10:45 pm

Hey Thomas,

in a real life scenario - would it be known which folder contains OLD comps and which - NEW ones? Can those names be set as default and both of them stay constant? If yes, I think it would make sense to do it this way:

1. Retrieve OLD folder content;
2. Retrieve NEW folder content;
3. Then go through COMPS folders and check for specific layers (comp type), and search are both versions of them exists in folders. If yes - initiate replacement.

If this sounds like a plan, I could look into it further. Cheers!

Find out more:
After Effects Tutorials: motion design, expressions, scripting.


Return to posts index

Thomas Patrick
Re: Comp And Layer Replacement Script
on Mar 14, 2019 at 5:18:53 pm

Hi Tomas!

Yes, for my situation those folder names will remain constant. My "COMPS" folder currently has subfolders (for each day eg MON, TUE, etc.) but if that is the source of my problems I could rearrange that....

I'm still unsure what's broken in my script but I'm definitely open to new and better ways to accomplish the task!

Thanks for the interest and the quick reply!

Tom


Return to posts index


Tomas Bumbulevičius
Re: Comp And Layer Replacement Script
on Mar 14, 2019 at 5:42:30 pm

Thomas,

I might have great news for you - the code you were using was only a single line incorrect ! Try below. Added few extra variables just to make this more debuggable to troubleshoot.

Anyways, the reason why it did not behaved properly at first, is because layer inside the comp is no longer 'CompItem' type. I solved this by checking its source type instead as curLayer.source.typeName == "Composition".

See if it works as expected or any other adjustments might be needed.

Cheers!

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


function main(){

var newComp;
var newCompName;


for (var i = 1; i&lt;= app.project.numItems; i++) {
var curItem = app.project.item(i);
if (curItem instanceof CompItem) {
for (var l = 1; l &lt;= app.project.item(i).numLayers; l++){
var curLayer = app.project.item(i).layer(l);
if (curLayer.source.typeName == "Composition") {
newCompName = app.project.item(i).layer(l).name.split("OLD")[0] + "NEW";
newComp = getComp(newCompName);
if (newComp == null){
alert ("Can't find comp '" + newCompName + "'");
return;
}
curLayer.replaceSource(newComp,false);
}
}
}
}

}

app.beginUndoGroup("Replace")

main();

app.endUndoGroup();


Find out more:
After Effects Tutorials: motion design, expressions, scripting.


Return to posts index

Thomas Patrick
Re: Comp And Layer Replacement Script
on Mar 14, 2019 at 7:32:33 pm

Hi Again Tomas,

Thanks again for taking a crack at this!

Unfortunately the code may still need some modification. When running it from the Scripts menu I get the following error:

"Unable to execute script at line 22. null is not an object"

In the script editor this is line 22:
"if (curLayer.source.typeName == "Composition") {"

Is there good way to troubleshoot this?

Thanks for any and all replies!

Tom


Return to posts index

Tomas Bumbulevičius
Re: Comp And Layer Replacement Script
on Mar 14, 2019 at 8:32:36 pm

Thomas,

thanks for your input. Are you working with the same structured .aep as in attached screenshots? If more complex, what layers are inside 'REPLACED_X' comps? I assume, there are either texts or shapes, and they do not have that 'source' attribute we are accessing now. Is this is correct assumption?

Find out more:
After Effects Tutorials: motion design, expressions, scripting.


Return to posts index


Thomas Patrick
Re: Comp And Layer Replacement Script
on Mar 14, 2019 at 8:48:02 pm

Hi Again,

Yes, my "REPLACED" comps will be more complex than the screenshot with a solid, some .movs, and some still images.

For testing purposes however I'm using exactly what you see in the screenshot and still coming up with the same error:

"Unable to execute script at line 22. null is not an object"

Any ideas?


Return to posts index

Tomas Bumbulevičius
Re: Comp And Layer Replacement Script
on Mar 14, 2019 at 8:52:58 pm

Thomas,

try changing the 22 line to the following. If this does not help, I would love to see the project itself - maybe I am missing something.

if ((curLayer.source != null ) && (curLayer.source.typeName == "Composition")) {

Find out more:
After Effects Tutorials: motion design, expressions, scripting.


Return to posts index

Thomas Patrick
Re: Comp And Layer Replacement Script
on Mar 14, 2019 at 8:58:28 pm

Tomas you're a legend!

That line did the trick! I'm going to sit down and go through this code now to figure out what I still need to learn...

Thanks for everything!

Maybe one day I'll be good enough to help you out of a jam too!

Thanks Again,
Tom


Return to posts index


Tomas Bumbulevičius
Re: Comp And Layer Replacement Script
on Mar 14, 2019 at 9:05:26 pm

Tom,

my pleasure! Tom can't leave the other Tom with a headache, haha. You did the heavy lifting on your own, I only spotted on the last set of repetition ! 😃

To get a better understanding, add some break-points (red dots on the right side of the code line numbers), and look at the 'Data Browser' panel how values changes during the loops.

Best wishes!

Find out more:
After Effects Tutorials: motion design, expressions, scripting.


Return to posts index

Thomas Patrick
Re: Comp And Layer Replacement Script
on Mar 14, 2019 at 9:11:33 pm

Thanks for the tips!

I hope to nosedive into your youtube videos soon.....


Return to posts index

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