Skip to main content
Participant
October 19, 2017
Answered

How to change the source of Image layer with ExtendedScript?

  • October 19, 2017
  • 2 replies
  • 6197 views

I would like to change an image in a compostion by altering it's source. (Or by any alternate solution)

I have already tried it, but unfortunately I'm getting an error. The situation is that I have an image dragged into my composition, it's the third element in that comp, and I would like to change this image by adding a different path to a new .jpg file.

# access the composition

var theComposition = app.project.item(1);

# get the existing image layer

var theImageObject = theComposition.layers[3];

# import the new image that I'd like to use

var io = new ImportOptions(File("f:/imagez/new/2.jpg"));

# replace the existing image with the new one

theImageObject.replaceSource(io, true);

# I tried with this also, but in this case I couldn't figure out how to replace the path to this

new ImportOptions().file = new File("f:/imagez/new/2.jpg");

After running the code I get the following error:

Unable to call "replaceSource" because of parameter 1. [object ImportOptions] is not of the correct type.

I understand that I have to use a different typed object, however I can't figure out how should it be done. I checked the AVLayers in the docs but those examples weren't really clear for me since I'm very new to this AE scripting. I would really appreciate if somebody could show me a working solution. I just want to change an image in a composition, with a new image which is not imported into the project. Thanks!

This topic has been closed for replies.
Correct answer Mr Sparkle

I think you're missing a step. You've created an ImportOptions object, the next thing to do is feed that to importFile:

// access the composition

var theComposition = app.project.item(1);

// get the existing image layer

var theImageObject = theComposition.layers[3];

// import the new image that I'd like to use

var io = new ImportOptions(File("f:/imagez/new/2.jpg");

// the missing step

var newImport = app.project.importFile(io);

// replace the existing image with the new one

theImageObject.replaceSource(newImport, true);

Alternatively you could change the file source, rather than importing a new asset. See the docs for FootageItem.replace().

Ian

2 replies

Mr SparkleCorrect answer
Participant
October 21, 2017

I think you're missing a step. You've created an ImportOptions object, the next thing to do is feed that to importFile:

// access the composition

var theComposition = app.project.item(1);

// get the existing image layer

var theImageObject = theComposition.layers[3];

// import the new image that I'd like to use

var io = new ImportOptions(File("f:/imagez/new/2.jpg");

// the missing step

var newImport = app.project.importFile(io);

// replace the existing image with the new one

theImageObject.replaceSource(newImport, true);

Alternatively you could change the file source, rather than importing a new asset. See the docs for FootageItem.replace().

Ian

rokako88Author
Participant
October 22, 2017

This solution worked perfectly. Thanks a lot!

Dave_LaRonde
Inspiring
October 19, 2017

That will be a problem.  The error message refers to the fact that the file is NOT imported into the After Effects project.

As a workaround, you could always import all the images that you would use as replacements; I presume that if you're going to all the trouble to write a script, it would be used more than once.

An alternative -- add them all to the comp(s) in question and turn off the eyeball icon to make them invisible.

rokako88Author
Participant
October 20, 2017

Thank you Dave! Honestly I'm not sure that

"The error message refers to the fact that the file is NOT imported into the After Effects project."

I think the error refers to that exact object is not a proper type. It is a very simple script, I would be really surprised if it couldn't be solved programmatically.