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 <= 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<= app.project.numItems; i++) {
var curItem = app.project.item(i);
if (curItem instanceof CompItem) {
for (var l = 1; l <= 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.
Social Media Profiles