Copy link to clipboard
Copied
A new addition to AE CC is "Replace With Precomp" which you can access with the context menu in the project panel. Is there a method that was added to After Effects so that we can access this feature via scripting?
Copy link to clipboard
Copied
Not that I know of natively. It could be coded though.
On pg. 47 of the After-Effects-CS6-Scripting-Guide.pdf you can replace a layer with another AVItem object.
app.project.item(index).layer(index).replaceSource(newSource, fixExpressions);
//START
var myComp = app.project.item(1); //Assuming your active comp was the 1st item in the project panel.
var layerToReplace = myComp.layer(1); //Assuming the layer you are replacing is layer 1.
var newLayer = app.project.item(2); //Assuming your replacement layer is the 2nd item in the project panel.
layerToReplace.replaceSource(newLayer, true);
//END
Copy link to clipboard
Copied
Right, I can see how that works but it dosen't quite do what "Replace with Precomp" does.
Replace with Precomp...
1. creates a composition with the same setting as the footage
2. places the selected footage item within it
3. replaces all references to that footage item with references to the new composition.
I should be able to figure out the first two steps. What do you think would be the best way to script the third step?
Thanks!
Copy link to clipboard
Copied
3. replaces all references to that footage item with references to the new composition.
You would need to loop through all comps that use the footage and then doing the replaceSource method on matching layers. That's a simplified explanation of course, but should be scriptable.
Break it down into stages...
1) app.project.item(index).usedIn //Returns array of CompItems
2) Loop through those comps grabbing numLayers
3) Loop through those layers looking for the matching layer(s) based on source
4) replaceSource
Copy link to clipboard
Copied
Was really hoping for a new method but it looks like I'll have to do it the hard way. Thanks for the breakdown, that definitely helps. I won't be able to start in on it for a couple days but I'll let you know how it goes.
And David, I just wanna say thanks for doing the Extendscript training series... It has really help me and I'm sure countless others wrap their heads around script creation for AE.
Copy link to clipboard
Copied
You are very welcome, glad it has been useful for you.
Was really hoping for a new method
I double checked to make sure that they didn't sneak it in somewhere, but these are the only methods available for AVItems...
openInViewer()
remove()
replace()
replaceWithPlaceholder()
replaceWithSequence()
replaceWithSolid()
setProxy()
setProxyToNone()
setProxyWithPlaceholder()
setProxyWithSequence()
setProxyWithSolid()
Copy link to clipboard
Copied
Got it to work. Here is my solution if anyone is looking for something similar. Thanks again David.
======
var proj = app.project;
var selection = proj.selection;
for (var i = 0; i < selection.length; i++)
{
// Get a list of comps that the footage is being used in
var compsUsedIn = selection.usedIn;
// Creates comp based on selected footage settings
var newComp = proj.items.addComp(selection.name, selection.width, selection.height, selection.pixelAspect, selection.duration, selection.frameRate);
// Add footage to the new comp
newComp.layers.add(selection);
// Loops through the used comps
for (var x = 0; x < compsUsedIn.length; x++)
{
var layersInComp = compsUsedIn
// Loop through the layers in each used comp
for (var y = 1; y <= layersInComp; y++)
{
// Replaces matching instances with the new comp
if (compsUsedIn
compsUsedIn
}
}
}
}
Copy link to clipboard
Copied
Hi Everyone, I found a different solution:
1- assign the layer of a comp you want replaced to a layer to a variable.
2 - assign the comp you want to replace the old comp to a variable.
3 - get both vars to be var.selected = true;
This will select the old comp layer and the new comp that will replace it.
Then you do as @foxbot1979 mentioned,
var1.replaceSource(var2.true)
This emulates the alt drag that you usually have to do manually.
Hope it helps!