[MAYA] Handling Automation blocking Start-up Dialogs

I’m using C# to let the user choose series of Maya(.mb) files to run batch actions on. The tool launches a Maya process with a start-up command that sources a MEL script and waits for Maya to exit before launching the next maya file. The MEL script then handles all the actions and exits.

The problem with this system is that these particular files are triggering an error dialog that requires the user to click “Ok” before it finishes it’s start up initialization and script sourcing. So the user has to baby-sit the process to get past the dialog.

Is there a good way to catch and exit, or just bypass, these incidental, automation blocking, dialogs? In this case, it’s not possible/practical to try and fix the source of the error.

Mayabatch will not throw up dialogs.
If need to use Maya instead of Mayabatch and you are using the MEL/Python “file” command to load the files, there are flags that suppress popups.
-force will force the loading of a file without the unsaved work warning.
-prompt will prevent error dialogs generated by the loading of the file.

[QUOTE=bryceclark;10079]I’m using C# to let the user choose series of Maya(.mb) files to run batch actions on. The tool launches a Maya process with a start-up command that sources a MEL script and waits for Maya to exit before launching the next maya file. The MEL script then handles all the actions and exits.

The problem with this system is that these particular files are triggering an error dialog that requires the user to click “Ok” before it finishes it’s start up initialization and script sourcing. So the user has to baby-sit the process to get past the dialog.

Is there a good way to catch and exit, or just bypass, these incidental, automation blocking, dialogs? In this case, it’s not possible/practical to try and fix the source of the error.[/QUOTE]

If a quick, less-than-elegant solution is acceptable, you can use AutoHotKey (http://www.autohotkey.com). AHK is free and relatively easy to pick up. Also, AHK scripts can be compiled into stand-alone executables (making it possible to run AHK scripts sans an AHK installation).

The code for such a script would look something like the following (the bulk of the following code snippet is comprised of comments, making it look far more complicated at first glance than it actually is):

"
/*
This example script waits for the “Delete Multiple Items” dialog to appear. This dialog is triggered by R-clicking on the recycling bin and selecting
the “Empty Recycling Bin” option from the context menu. Once the menu is detected, the script “hits” the “No” button in the dialog (thus closing it out
and cancelling the operation).
*/

/*
wait for and cancel 2 occurrences of the dialog in question (the script can be terminated at any time by R-clicking the green H in the
system tray and selecting the “Exit” option from the context menu.
/
Loop, 2 ; 2 occurrences will be processed
{
/

If the specified window exists, bring it into focus (The window is identified by window title, which is “Delete Multiple Items” in this example.
Partial titles are also acceptable.). If the window does not exist, wait until it does exist and then bring it into focus.
*/
WinWait, Delete Multiple Items,
IfWinNotActive, Delete Multiple Items, , WinActivate, Delete Multiple Items,
WinWaitActive, Delete Multiple Items,

/*
once the dialog in question is in focus, we can click the "No" button to close out the dialog in a number of ways.  Here's one such way.  With the
initial control focus and the tab count required to move the control focus to the "No" button in mind, we can use a simple loop to maneuver the
dialog control focus to the desired button.  Once the correct control is in focus, we can act on this control by sending input with a "SendInput"
statement.  In your case, this likely means sending an ENTER key stroke to effectively press the dialog's "OK" button.
*/
Loop, 1 ; send 1 TAB key strokes to move the dialog's focus to the desired control
{
	SendInput, {TAB}
	; Send, {TAB} ; alternatively there's the "Send" statement
}
SendInput, {ENTER} ; "click" the in focus button

}

GuiClose:
^!q::ExitApp ; Assign a hotkey (ctrl + alt + q) to terminate this script
"

Max actually has some internal functionality that operates similarly (to help automate Max UIs that aren’t exposed to MAXScript). Search for “DialogMonitorOPS” in the MAXScript docs if you’re curious. Presumably Maya has similar functionality.

If you decide to try the AHK route, let me know if you run into any snags.

Here’s the code snippet sans comments:

"
Loop, 2
{
WinWait, Delete Multiple Items,
IfWinNotActive, Delete Multiple Items, , WinActivate, Delete Multiple Items,
WinWaitActive, Delete Multiple Items,

Loop, 1
{
	SendInput, {TAB}
}
SendInput, {ENTER}

}
"

Also, there’s an error with the code snippet as originally posted. You’ll need to remove the following lines. Otherwise, the AHK script won’t automatically close after the specified number of dialogs have been processed.
"
GuiClose:
^!q::ExitApp ; Assign a hotkey (ctrl + alt + q) to terminate this script
"

[QUOTE=btribble;10081]Mayabatch will not throw up dialogs.
If need to use Maya instead of Mayabatch and you are using the MEL/Python “file” command to load the files, there are flags that suppress popups.
-force will force the loading of a file without the unsaved work warning.
-prompt will prevent error dialogs generated by the loading of the file.[/QUOTE]

My MEL script essentially grabs data from animCurves, edits the data that is there, then saves and exits.

Will Mayabatch run the script and run all the commands, without the GUI?

[EDIT] Mayabatch is working like a charm for this script. Still, do commands that interact with the GUI still work in mayabatch?

[QUOTE=eevans;10082]
This example script waits for the “Delete Multiple Items” dialog to appear. This dialog is triggered by R-clicking on the recycling bin and selecting
the “Empty Recycling Bin” option from the context menu. Once the menu is detected, the script “hits” the “No” button in the dialog (thus closing it out
and cancelling the operation).
[/QUOTE]

Would AHK be able to recognize when Maya itself throws a specific dialog?

[QUOTE=bryceclark;10085]Would AHK be able to recognize when Maya itself throws a specific dialog?[/QUOTE]

The short answer is yes. :D:

Looks like most of the dialogs in Maya are standard Windows menus (which are easily accessible via AHK scripting). So, you should be able to detect the dialog in question and bring it into focus using the same method applied in the example script. As for clicking the “OK” button though, you might need to use a method other than the “tab” approach present in my example. The code snippet below demonstrates a means of L-clicking a button in a dialog.

"
Loop, 2
{
WinWait, Delete Multiple Items,
IfWinNotActive, Delete Multiple Items, , WinActivate, Delete Multiple Items,
WinWaitActive, Delete Multiple Items,

/*
get the width and height of the active (in focus) window
/
WinGetActiveStats, Title, AWinWidth, AWinHeight, X, Y
/

The coordinates are relative to the active window ([0,0] being the top left corner of the window). In the case below, the desired point in the dialog to be clicked is located 50 pixels from the right edge of the dialog and 30 pixels up from dialog’s bottom edge.
*/
MouseClick, left, AWinWidth - 50, AWinHeight - 30
}
"

Thanks a bunch for that bit of knowledge, Eric. I can definitely see myself using that in the future for something!

You’re most welcome. :): AHK isn’t particularly sexy or elegant, but I’ve found it to be tremendously handy for quick and dirty automation jobs (especially in cases where the app or apps to be control do not offer native scripting interfaces).

While mayabatch was a preferred solution for this particular issue for speed and cleanliness, it turns out there is an unfortunate reason we need to have maya open during this process. So, I ended up writing a quick variation of the script Eric suggested to bypass the particular issue we have.

Essentially, it just waits until it sees a window with the name “Maya” and element text of “OK” and then hit hits TAB and ENTER.

Loop, 1
{
WinWait, Maya, OK, 
IfWinNotActive, Maya, OK, , WinActivate, Maya, OK,
WinWaitActive, Maya, OK,
Sleep 8000,
Loop, 1 ; send 1 TAB key strokes to move the dialog's focus to the desired control
{
SendInput, {TAB}
; Send, {TAB} ; alternatively there's the "Send" statement
}
SendInput, {ENTER} ; "click" the in focus button
}

I call this script as a new process in my C# tool, launching it just before each iteration of Maya in the list of files to act on, and it works like a charm.

Awesome. :): AHK FTW!