• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

"Replace with Precomp" Script

Community Beginner ,
Feb 05, 2014 Feb 05, 2014

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?

TOPICS
Scripting

Views

4.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 05, 2014 Feb 05, 2014

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 05, 2014 Feb 05, 2014

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 05, 2014 Feb 05, 2014

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 05, 2014 Feb 05, 2014

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 05, 2014 Feb 05, 2014

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()

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 09, 2014 Feb 09, 2014

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.numLayers;

               // Loop through the layers in each used comp

                              for (var y = 1; y <= layersInComp; y++)

                                   {

                                             // Replaces matching instances with the new comp

                                             if (compsUsedIn.layer(y).source == selection) {

                                                       compsUsedIn.layer(y).replaceSource(newComp, true);

                                        }

                              }

                    }

          }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 26, 2023 Apr 26, 2023

Copy link to clipboard

Copied

LATEST

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines