Many people have asked if there's a way to stop the execution of a MEL procedure. The same question came up on Ask Autodesk again last week, and I gave a quick hint, but I didn't get around to explaining how to do it exactly, even though this type of thing is particularly useful to people who are new to MEL.
There isn't really a mechanism to stop Maya from executing a MEL procedure, like pressing escape. If you accidentally had a few zeros too zero to many in a loop like this,
for ($i =0; $i < 1000000; $i++) {
print ($i +"\n");
}
there is nothing you can do but terminate Maya, or wait. That's really annoying if you were right in the middle of scripting something in the Script Editor and you forgot to save your work. (Yes, that happens to me too).
But the fact that there's no button we can press doesn't mean we can't find another way of terminating a MEL procedure that has run amok. The break statement would allow us to interrupt a loop if some condition became true.
for example, I could write
for ($i =0; $i < 1000000; $i++) {
if ($condition) break;
print ($i +"\n");
}
If $condition is 0 (false), the script stops, but that doesn't really help us. It should be something that I can do outside of Maya, since Maya doesn't respond to input when it is running a MEL Script. I should run a check to see if something exists, like a file. Checking for the existence of a file is easy enough with the exists command. Exists returns 1 if a file exists, 0 if it doesn't. If I use the negate operator !, I can have a condition that says: if the negation of the result of exists evaluates to true, break from the for loop.
for ($i =0; $i < 1000000; $i++) {
if (!`exists $lock`) break;
print ($i +"\n");
}And that's what the following script does:
proc myProc() {
// create a file called maya.lock in the tmp directory
$lock = ( `internalVar -userTmpDir` + "maya.lock" );
// create a filehandle for writing
$fileId=`fopen $lock "w"`;
// close the filehandle
fclose $fileId;
// do something very often
for ($i =0; $i < 1000; $i++) {
// break if the negation of exists $lock is true
if (!`exists $lock`) break;
// do something
print ($i +"\n");
}
// delete the file if we reached the end of the script
if (`exists $lock`) {
sysFile -delete $lock;
}
}
To get to the lock file in Windows to delete the file quickly , I type %tmp% in the address bar.
Now I can run "dangerous" scripts that may not terminate properly in a easy to abort way.





Subscribe
That's a really clever hack for doing this, thanks for sharing!
Posted by: Chris White | 19/04/2009 at 07:30 PM
why not use the progressBar function?
It looks nice and allows you to interrupt your script, all native to Maya.
for example:
global string $gMainProgressBar;
int $maxPBar = 10000;
progressBar -e -bp -ii 1 -max $maxPBar $gMainProgressBar;
for($i=0; $i < $maxPBar; $i++)
{
if(`progressBar -q -ic $gMainProgressBar`)
{
break;
}
// here goes you code
progressBar -e -s 1 $gMainProgressBar; // increases the progress bar
}
progressBar -e -endProgress $gMainProgressBar; // call this when you stop the script or when the script is done
Posted by: sven | 15/06/2009 at 03:57 PM
Wow Sven!!
That's the best Maya trick ever!!
I've looking for a way to interrupt Maya scripts for a decade!!!
Thanks a lot man!!!
Posted by: Wattana | 08/04/2011 at 06:57 PM