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

How to change the source of Image layer with ExtendedScript?

Community Beginner ,
Oct 19, 2017 Oct 19, 2017

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!

5.8K
Translate
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

correct answers 1 Correct answer

Community Beginner , Oct 20, 2017 Oct 20, 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.replaceSo

...
Translate
LEGEND ,
Oct 19, 2017 Oct 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.

Translate
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 ,
Oct 20, 2017 Oct 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.

Translate
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 ,
Oct 20, 2017 Oct 20, 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

Translate
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 ,
Oct 22, 2017 Oct 22, 2017
LATEST

This solution worked perfectly. Thanks a lot!

Translate
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