I got emailed this blog by about 5 different work mates.. 😀 It takes a while to load as it’s image heavy, and plenty of them! but there are so many beautiful models, in every theme you could imagine, all hand-picked for quality!
I got emailed this blog by about 5 different work mates.. 😀 It takes a while to load as it’s image heavy, and plenty of them! but there are so many beautiful models, in every theme you could imagine, all hand-picked for quality!
Quite often, especially when you are writing and testing a new MEL script, you’ll inadvertently create a for or while loop that never exits. Usually due to a testing condition that never becomes true, 99% of times it’s probably due to a dumb oversight that is (hopefully) picked up during initial testing/debugging, but infinite loops can still sometimes occur in production scripts. Even if you haven’t got stuck in an infinite loop you may suddenly realise that you’ve started a script with the wrong type of, or too many, components selected and the script will be churning away for a good 2-3 hours if not interrupted!
Once a bit of MEL enters such a loop there is no way of breaking out of it other than killing Maya’s process from the Windows Task Manager. Obviously this is a bit of a tedious job at the best of times, especially if it’s a script that needs thorough testing, but what if your loop-locked script was running on a scene you’d not saved for a couple of hours?
Here’s an excellent and, so far, bulletproof (touch wood) method of protecting yourself and your scripts. Because Maya has locked up at this point we can’t issue any kind of command or process from Maya. We can however issue a command from outside Maya, say from DOS…
If you create a batch file somewhere on your PC with the following content:
@echo off
echo This file breaks infinite loops in MEL scripts > c:\breakMel
pause Maya halted - Press any key to continue
del c:\breakMel
call it HALTMAYA.bat or STOPLOOP.bat or whatever name works best for you so you don’t forget what it’s for. If you have no idea what a batch file is, simply copy and paste the above code into a text file and save it as one of the names suggested above (make sure you don’t have file extensions hidden in windows!).
Now, when you run this batch file it should open a DOS shell window and prompt you to press a key to continue. Pressing a key dismisses the DOS box…
So? what exactly did this do and how does it help us stop an infinite loop??
Well if we take a look at the batch code it should be relatively self explanatory, even if you don’t know any DOS batch commands. The first line simply hides the echoing of commands (kinda like Maya script Editor “Echo All Commands” set to “off”). The second line echos the specified sentence, but the important bit is the > at the end. This > symbol tells the echo commad to redirect the echoed words into a file instead of onto the screen. That’s why you never see these words printed in the DOS window, they are sitting in a file called “breakMel” on the root of your C: drive. Of course you can change these words and the name of the file, as they are entirely irrelevant and no-one will ever see either of them. All that’s important here is that a detectable file has been created. The third line simply pauses DOS and prompts you to press a key. This is vitally important because we’ve no idea how long the rogue MEL script will take to get once through the loop it’s stuck in, and we need to ensure that it detects the breakMel file we’ve just created. The pause allows us to sit and wait for the loop to detect the file before we proceed to the fourth and final line which deletes it. If we don’t delete the break file your MEL script will find it again next time it is run and halt immediately!
So, the final piece of the puzzle.. how does your MEL script detect the creation of this file? It’s quite straightforward; you just put a single line inside the loop in your MEL script:
if (`filetest -f "c:/breakMel"`) break;
Every iteration through your loop will look for the existence of a file called “c:\breakMel” and if it exists it will break out of the loop. If you have more than one nested loop though, you will either need to put the same line in each nested level of the loop or change the break command into an error, which is a lot simpler:
if (`filetest -f "c:/breakMel"`) error "User broke out of infinite loop!";
Believe it or not, adding this command doesn’t appear to have any noticeable effect on the performance speed of a script. I initially thought checking for a file would take MEL ages but surprisingly it doesn’t, at least not in this context. I’ve run timing tests with and without this line and there’s no significant difference, so you don’t need to worry about adding this feature to all your scripts. Of course, once you’re convinced your script is fully robust enough that it can’t get stuck in an infinite loop you can always remove this line, but it won’t hurt if you do decide to leave it in.
The other good thing about this trick is that it has no dependencies, so you can safely distribute the script to anyone without worrying that the line will cause any kind of problems at their end, as it’s unlikely anyone will ever have a file called “breakMel” on their C:\ drive!
You can do a similar thing in Python:
import os
if os.path.exists("c:/break"): break
Here’s a link to my old MEL web site:
Eventually I’ll be updating all these old scripts and putting them on here, but until then you can access the older version here.
[EDIT]
Someone’s pointed out that this old site is now defunct and returns a 404 error (thanks Daniel Lee!). so, until I get my arse in gear and make a new page here, you should be able to grab the scripts from this web directory:
work has sorted out some Life Drawing sessions which I go to once a week. It’s only for 2 hours, but it keeps the rust away.. just about. These drawings range from quick 5 minute sketches to full 1 hour “masterpieces” 😀
Powered by WordPress