Retain control in loop through the ScriptUI
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I used app.sheduleTask to achieve the effect. It's working now.
Copy link to clipboard
Copied
Can you explain ?
Copy link to clipboard
Copied
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).
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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

