Wait for saveFrameToPng() to create the file.

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Andrei Popa
Posts: 4
Joined: October 3rd, 2017, 5:02 am

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;
  }
}
SteveLewis
Posts: 1
Joined: March 26th, 2021, 5:02 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

Andrei Popa
Posts: 4
Joined: October 3rd, 2017, 5:02 am

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.

spatswatsa
Posts: 1
Joined: May 17th, 2021, 12:46 am

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

User avatar
axwt
Posts: 1
Joined: May 3rd, 2022, 1:17 am
Contact:

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;

};
Post Reply