Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions How to import an image into the timeline (with script)?

  • How to import an image into the timeline (with script)?

    Posted by Clara Martin on May 14, 2024 at 1:14 pm

    Hello,

    I can import files contained in a directory of my computer in the “footage” part of after effects content for example the file: “my_picture.jpg”

    Is it possible to import this “my_picture.jpg” file directly into the timeline using a script.

    It is possible with script to create text layers for example directly in the timeline, I would like to know if we can directly add an image of my choice.

    Thanks in advance 🙂

    Brie Clayton replied 2 years ago 4 Members · 6 Replies
  • 6 Replies
  • Hector Vera

    May 14, 2024 at 3:04 pm

    Yeah it is possible to directly import an image into the After Effects Timeline using a script. Here is one example using the JavaScript API:

    // Set the path to the image file

    var imageFilePath = “my_picture.jpg”;

    // Create a new After Effects comp

    var comp = new Comp(“my_comp”);

    // Define the size of the comp to match the dimensions of the image

    var width = 1600;

    var height = 1200;

    var compSize = [width, height];

    // Import the image using the File.importFile() method

    var layer = comp.addLayer(“My Image Layer”);

    var importResults = layer.file.importFile(imageFilePath);

    // Add the layer to the topmost layer of the timeline

    layer.move(comp.numLayers – 1);

    // Set the duration of the comp to match the duration of the image

    comp.duration = importResults.duration;

    // Write the comp to disk

    var filePath = “my_comp.aep”;

    comp.saveAs(filePath);

    This script above creates a new composition, imports the image file from a specific path, adds the imported layer to the topmost layer of the timeline and also sets the duration of the comp to match the duration of the image. Then it exports the comp into a new .aep file. Note that you’ll need to have the File.importFile() method installed in order for this script to work properly.

    Hope that this helps and if you have any other questions, feel free to ask! 🙂

  • Dan Ebberts

    May 14, 2024 at 3:42 pm

    As far as I know, for any layer type that has a source file (image, video, etc.), the source file has to be imported into the project bin before you can use it to create a layer in a comp’s timeline. Is that what you’re asking (if you can bypass the file importing and go directly to creating an image layer in the timeline)?

  • Clara Martin

    May 14, 2024 at 5:30 pm

    So the first solution provided by Hector Vera does not work for the moment.

    No, I do not want to directly import an image into the timeline. I have this image in my present panel project.

    I just want to use it via a script to import it into my timeline 🙂

    Here is what I have:

    – 1 composition already created which is called “comp”

    – My file “my_picture.jpg” in the panel project

    When I do this:

    // alert with comp name works

    alert(comp.name);

    // this does not create an image layer for me even though imageFilePath points to the correct photo on my computer

    var imageFilePath = ‘/Users/clara/Movies/AE/Images/my_picture.jpg’;

    var layer = comp.addLayer(“My Image Layer”);

    var importResults = layer.file.importFile(imageFilePath);

    Thank you in advance both of you 🙂

  • Dan Ebberts

    May 14, 2024 at 5:55 pm

    I think it would be more like this simplified example:

    function getComp(theName){
    for (var i = 1; i <= app.project.numItems; i++){
    if (app.project.item(i) instanceof CompItem && app.project.item(i).name == theName) return app.project.item(i);
    }
    return null;
    }
    var comp = getComp("comp");
    var imageFilePath = '/Users/clara/Movies/AE/Images/my_picture.jpg';
    var importOptions = new ImportOptions(File(imageFilePath));
    var importResults = app.project.importFile(importOptions);
    var layer = comp.layers.add(importResults);
  • Clara Martin

    May 14, 2024 at 7:08 pm

    Thank you very much, it works wonderfully 😉

  • Brie Clayton

    May 14, 2024 at 11:07 pm

    Thank you for solving this, Dan!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy