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

Retain control in loop through the ScriptUI

Community Beginner ,
Apr 16, 2017 Apr 16, 2017

My script is launched from the ScriptUI Panels (so it's a Window). In the script user interface, there are two buttons: "Start" and "Stop". When you click the "Start" button, the script executes a sequence of commands in a loop. The "Stop" button is supposed to stop this loop (it does this by setting a loop condition to false). But when the "Start" button is pressed, the script UI becomes unresponsive.

Any idea how to make it (the UI) responsive so the "Stop" button can be clicked?

.update() doesn't work with scripts in the ScriptUI Panels.

TOPICS
Scripting
1.4K
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
Advocate ,
Apr 19, 2017 Apr 19, 2017

After Effects does not return focus to palette unless it finishes (or crashes) doing it's job. At least this is a case with ScriptUI panels. Not sure about CEP panels.

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 ,
Apr 19, 2017 Apr 19, 2017

I used app.sheduleTask to achieve the effect. It's working now.

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
Advocate ,
Apr 19, 2017 Apr 19, 2017

Can you explain ?

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 ,
Apr 19, 2017 Apr 19, 2017

When the user clicks the "Start" button, create a task like so:

var taskId =  app.scheduleTask("functionToExecute()", 300, true);

When the user clicks the "Stop" button, cancel the task like so:

app.cancelTask(taskId);

The app doesn't yeild control while a task like rendering is going on, but if it idle, it will yield control (Stop button will be active).

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
Advocate ,
Apr 20, 2017 Apr 20, 2017

Yes, i know how app.scheduleTask works, but i dont see how it could give control back to the palette/panel and enable the 'STOP' button while the script is still running. What your functionToExecute look like, if i may ask ?

Xavier

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 ,
Apr 20, 2017 Apr 20, 2017

Just because you said so, I checked again. And yes, it does work. The function is a pretty basic function like:

function functionToExecute()

{

     read-some-files-and-check-for-a-job

     if-job-found-do-the-job

     render-job

}

By the way, there is no loop within "functionToExecute()", if that's your concern. The loop is "app.scheduleTask". Which version of After Effects did you try this on? I don't know whether this is a glitch in After Effects that makes it work, but it works fine (at least for me). Maybe you can post the code you tried and then I can check it?

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
Advocate ,
Apr 20, 2017 Apr 20, 2017

I haven't actually tried, and haven't much time for it right now.

I just can't figure out how the use of app.schedule task can unfreeze After Effects while a script is being executed.

Xavier

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
Enthusiast ,
Apr 21, 2017 Apr 21, 2017

I think the main thing here is to replace for\while loops with app.scheduleTask that AE won't freeze.

For example:

try{ if(typeof loopTask != 'undefined'){  app.cancelTask(loopTask); } }catch(er){ }

var counter = 1;

function loopFunction() {

    var comp = app.project.items.addComp("loop"+counter, 10, 10,1,1,25);

    counter++;

};

function showUI (thisObj){

w = ( thisObj instanceof Panel) ? thisObj : new Window("palette", "Test");      

      

   start = w.add ("button", [0,0,100,25], "Start");

   stop = w.add ("button", [0,0,100,25], "Stop");

   start.onClick = function(){ loopTask = app.scheduleTask("loopFunction()",500,true);  }

   stop.onClick = function(){ try{ if(typeof loopTask != 'undefined'){  app.cancelTask(loopTask); alert("Stopped at "+counter); } }catch(er){} };  

   w.show ();

  

};

showUI(this);

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
Advocate ,
Apr 21, 2017 Apr 21, 2017
LATEST

Thank you Alex, now i understand. It works well, quite nice.

I was very interesting in this, i think it'll take some more effort to adapt it to what i want, but the idea is good.

Xavier

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