Page 1 of 1

Wait for saveFrameToPng() to create the file.

Posted: March 18th, 2020, 12:48 am
by Andrei Popa

Hello everyone. I am trying to make a direct composition to png string. I thought about saving the comp to a png, then open png, turn it into ScriptUI usable code(with .toSource()) and then delete the png. The problem is that the script tries to do all these things before the png is created. Can I wait for saveFrameToPng() to create the file? I saw it has a .onCOmplete() part, but I am not sure how to use that. Here is my code.

Code: Select all

/**
* Returns the string that can be used to draw the picture in the ScriptUI
* 
* @param {comp:compItem,time:number} options 
*/
function createImageString(options) {

  //Setting defaults
  options = options || {};
  options.comp = options.comp || app.project.activeItem;
  options.time = options.time || options.comp.time;

  var jsxStringForPng = pngToString(options.comp, options.time);
  return jsxStringForPng;

  function pngToString(comp, time) {
    if (app.project.file) {
      var myTempFile = new File(app.project.file.path + "/" + comp.name + ".png");
      var a = comp.saveFrameToPng(time, myTempFile);
     // wait()
      var myString = pngToJsx(myTempFile);
      myTempFile.remove();
      return myString;
    } else {
      alert("Please save the project before using this function");
      return null;
    }

  }

  function pngToJsx(myFile) {
    if (!myFile) return;
    var rawData = readBinaryFile(myFile);
    return rawData.toSource().slice(13, -3);
  }


  function readBinaryFile(file) {
    file.encoding = "BINARY";
    file.open("r");
    var content = file.read();
    file.close();
    return content;
  }
}

Re: Wait for saveFrameToPng() to create the file.

Posted: March 26th, 2021, 5:08 pm
by SteveLewis

This is a fairly old post but I've come up with a bit of a solution and figured I'd share:

After you call saveFrameToPng you can add this:

Code: Select all

while (!myTempFile.exists) {
	$.sleep(500);
}

Maybe a little hackish, but it basically forces extendscript to wait until that file exists on your drive, and only then continue


Re: Wait for saveFrameToPng() to create the file.

Posted: May 12th, 2021, 5:45 am
by Andrei Popa

Thanks for this solution.
It works great, I just had to lower a bit the waiting time because I have a high amount of pics to process.


Re: Wait for saveFrameToPng() to create the file.

Posted: May 21st, 2021, 2:37 am
by spatswatsa

Thanks for helping me this also worked for me GIMP download Mac


Re: Wait for saveFrameToPng() to create the file.

Posted: May 3rd, 2022, 1:23 am
by axwt
SteveLewis wrote: March 26th, 2021, 5:08 pm

This is a fairly old post but I've come up with a bit of a solution and figured I'd share:

After you call saveFrameToPng you can add this:

Code: Select all

while (!myTempFile.exists) {
	$.sleep(500);
}

Maybe a little hackish, but it basically forces extendscript to wait until that file exists on your drive, and only then continue

Might not work in all cases because After Effects will first create and then write to the file.
The better approach would be to check if the content was written to the file.

Code: Select all

function renderPNG(comp, time, outputPngFile) {

	var pngFile = comp.saveFrameToPng(time, outputPngFile);

	while (!pngFile._isReady) { $.sleep(100); }

	return true;

};