Skip to main content
Inspiring
October 25, 2023
Question

Waiting to receive a file object

  • October 25, 2023
  • 2 replies
  • 591 views

Hi all.

I have code that imports a file into a project

var fileDepthSourceName = srtCarentFolder + "/Source_Crop.png"
app.project.activeItem.saveFrameToPng(0, File(fileDepthSourceName))
// $.sleep(5000)
f_ImportAsFootage(fileDepthSourceName, "Import");

The last line of code sometimes throws an error.

If before the last line I put the line 

 

$.sleep(5000)

 

then everything happens normally.

 

It seems like a string

 

f_ImportAsFootage(fileDepthSourceName, "Import");

 

call occurs before the file is created.

 

Is it possible to replace the string-- $.sleep(5000) ---

into an infinite loop, the exit from which is possible after receiving the object of this file?

This topic has been closed for replies.

2 replies

Mylenium
Legend
October 25, 2023

The typical way to do it would be to just add a manual refresh after the function. These only trigger once all previous functions have completed. No fancy loops required. Otherwise you could probably do something with checking the project item explicitly instead of just blindly loading the asset. A few ways I could imagine.

 

Mylenium 

AnyONAuthor
Inspiring
October 25, 2023

This is the function

 

 

// @include "E:/Progs/adobe/Scripts/AE/f_findAVItemByName.jsx";
// @include "E:/Progs/adobe/Scripts/AE/f_findFolderByName.jsx";
function f_ImportAsFootage(element, importFolderName) {
    var k = 1;
    while(File(element).exists == false) {
        k + 1
    }
    var ImpFileOpt = new ImportOptions()
    ImpFileOpt.file = new File(element)
    ImpFileOpt.importAs = ImportAsType.FOOTAGE
    app.project.importFile(ImpFileOpt)
    var stringElement = String(element)
    var arraystringElement = stringElement.split("/")
    var fullName = arraystringElement[arraystringElement.length - 1]
    var carentName = f_findAVItemByName(fullName)
    var imoprtFolder = f_findFolderByName(importFolderName)
    carentName.parentFolder = imoprtFolder;
    carentName.selected = false
}

 

 

There is an error on line 

 

 

ImpFileOpt.file = new File(element)

 

 

------------------------------------------------------------------------------------------------------------------

Exception has occurred: 1
  •  
After Effects error: Unable to set “file”. File can not be opened. Try checking access permissions.
---------------------------------------------------------------------------------------------------------------
 
The file is executed from the Visual Studio Code application with Administrator rights.
The access rights for the folder containing this file are set to: Full Control
???
Mylenium
Legend
October 25, 2023

I don't think you can avoid having to put in some wait cycles. You can't control how fast the script interpreter processes the underlying AEGP commands and the same applies to the file handling, since it merely calls a system function and waits for it to execute and return specific states.

 

Mylenium

AnyONAuthor
Inspiring
October 25, 2023

Yes, I want to write a loop to wait for this file.
What should it look like?