I have a lot of wrongly precomposed layers because of some faulty automation from my part. Now I want to reverse that, but I cannot seem to get this to work properly. Can someone take a look at it?
This script is supposed to walk all selected layers which are precomps, and copy the layers in there back to the parent comp, and move them below the layer currently being processed. But it just removes all selected layers which are precomps.
Code: Select all
clearOutput();
var myComp = app.project.activeItem;
if (myComp == null || !(myComp instanceof CompItem)) {
alert("No composition selected.");
}
else {
// Vars
var myLayers = myComp.layers;
var headroom = "25";
//headroom = parseInt(prompt("How much frames headroom are in your precomps?", headroom));
headroom *= myComp.frameDuration;
app.beginUndoGroup("UNpreComp Layers");
// Walk layers
for (var l = myLayers.length; l> 0; l--) { // BACKwards
// Vars
var layer = myLayers[l];
// UnPreCompose
if (!layer.shy && layer.source instanceof CompItem) {
myPreComp = layer.source;
for (var n = myPreComp.length; n> 0; n--) {
myPreComp[n].copyToComp(myComp);
myComp.layer(1).startTime -= headroom;
myComp.layer(1).moveAfter(layer);
}
layer.remove();
}
else alert('Not a PreComp layer');
}
app.endUndoGroup();
}