[Closed] How to implement an Abort button?
I have a script that creates a lot of geometry after entering some parameters and clicking on an execute button.
If it’s taking too long, is there a way to stop it so the user can lower the parameters and start again? The idea is to stop the part where the objects are created, not the script itself.
I hope my example is not too confusing. Thanks for your help.
Not via a button, as 3ds Max is going to be far too busy to worry about users clicking buttons.
Try this:
<loop start>
<code>
if (keyboard.escPressed) do ( exit ) -- bail from this loop early, further code will still be executed
<loop end>
Thanks, Richard. That looks even better than using a button.
Do I have to set escapeEnable = false first to prevent the script from crashing?
can’t say I’ve ever had to do that, but might be a good safety precaution
I don’t know if it’s still the case, but when I’ve used that kind of escape hatch with maxscript, it doesn’t care if max is active or not. As in if you have a script working in the background and you’ve popped another app up in the front. Then you hit esc to do something in the app you’re working in, it will still stop the script from running in the background. That bit me in the butt a few times with stuff that I was solving with a script in the background while working on other things in other apps.
Just something to keep in mind.
Cg.
yep – sadly there’s no way to check if 3ds Max is the foreground window (maybe with .NET, but got stuck last time I tried) to prevent that. Again, this type of bail-out is mostly when still developing scripts so that if you accidentally created an endless loop, you can exit it without having to end-process max, re-start, potentially having lost script code, etc.
It -can- be used in final scripts, but I would highly recommend you also check for, say, keyboard.shiftPressed. That way users can still end processing but they have to hit the otherwise uncommon keycombo of Shift+Escape. (Ctrl+Escape pops up start menu, Alt+Escape tends to minimize the current app.)
if keyboard.escPressed do DoAbort = queryBox...
works fine.
If I try
if keyboard.escPressed and keyboard.shiftPressed do DoAbort = queryBox...
it doesn’t work.
Is there a special way to deal with keyboard inputs?
a good method for a final script might also be to use a Progress Bar display which will give you both a Cancel Button and the option to ESC cancel, and of course shows you the progress to ease the wait.
For Example:
NotCancelled = true
progressStart " Testing! "
for i = 1 to 6000 while NotCancelled do
(
Percentage = 100. * (i / 6000.)
NotCancelled = progressUpdate Percentage
for x = 1 to 5000 while NotCancelled do
(
y = 2
)
)
progressEnd()
ho-hum… what’s up with that… you’re right – can’t seem to get that to work here either now o_O
I copy/pasted that from an ooooooooold script of mine… must’ve worked at some point.
I’m guessing something intercepting the shift+escape (much like ctrl & alt) somewhere… your guess is as good as mine as to what that might be.
well unless you want to get silly with something like ‘mouse cursor must be in upper-left corner of screen when hitting Esc’ (mouse.screenpos), I suppose just watching your Esc key pressed would have to do
The .NET way seems to involve writing some C#/something to call the Win32 APIs to get e.g. the foreground window.