Skip to main content
Known Participant
February 5, 2014
Question

"Replace with Precomp" Script

  • February 5, 2014
  • 2 replies
  • 5566 views

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?

This topic has been closed for replies.

2 replies

Participant
April 26, 2023

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!

Legend
February 5, 2014

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

Known Participant
February 5, 2014

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!

Legend
February 5, 2014
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