Hi! :wave: With this blog post I want to describe the internals of MayaCrasher, a tool for Windows I wrote months ago.

This software is targeted to 3D artists who use Maya on a daily basis.

What does it do? Simple: It saves your project when nothing else can help you. For example, Maya is frozen and you can only terminate it using Task Manager but, if you do that, you’ll lose all unsaved data.

Now you have an alternative! Open this tool, a Maya’s “Fatal Error” will appear and your project will be saved! :crossed_fingers: How can it accomplish such a noble task? Enjoy this post!

What is Maya

Autodesk Maya is a 3D modeling and animation software used both in videogame and film industry. It is available for both Windows, Mac and Linux.

It’s a powerful and customizable software, but sometimes this complexity hides bugs that make it hang or crash when you least expect it.

Maya

Crash or freeze

When Maya crashes, a window informing you about the crash appears and, if you are lucky, it saves your project in a temporary directory. This directory is usually %TEMP%, alias to C:\Users\YourName\AppData\Local\Temp. This window is called in nerd-terms “Crash Handler”, and looks like this:

Maya 2019 Crash Handler

Sometimes you are not that lucky to obtain a crash, Maya gets stuck, becomes completely frozen and the only thing left to do is to close it from the task manager throwing away all your unsaved work.

In this case it would be beautiful to make it crash on purpose, so that the crash handler appears and saves your project.

MayaCrasher

Please welcome MayaCrasher! :smile:

MayaCrasher is a Windows app to help you when Maya is stuck and you want to save the project, it’s extremely simple to use: open MayaCrasher, it appears a black window with this text:

Please CTRL+Click on the Maya window that is stuck...

Go on the frozen Maya, keep CTRL pressed on your keyboard and click on the frozen window.

Ta daah! :tada:

Hopefully, you’ll see the crash handler and, if you go into %TEMP%, you’ll find your project saved.

Be aware that, it may happen that Maya is frozen so badly that even the crash handler is not able to save the project.

Anyway, how does this magical tool work internally?

Exception handling

To understand how this tool works, we need to know something about Windows or other desktop operating systems.

Modern operating systems give to app developers a set of functionalities to interact with a program that just did something wrong.

Examples of “wrong” are:

  • The program divided by zero
  • The program tried to access a forbidden memory area
  • The program asked the CPU to execute an invalid command

If a programmer does not write in their app some code to handle these cases, Windows has its own “Default Exception Handler”, which is executed and kills your program while showing a window like this:

Default Crash Handler

On the other way, a programmer can write some extra code in their program that is executed when an exception (aka “something wrong”) happened. Theoretically the program could also ignore the exception and go on, but if the exception happened, probably there is something really wrong going on and the safest thing to do is to close the program as soon as you can.

A typical crash handler, like the one in Maya, helps closing gracefully the program and maybe send some information about the crash to the developer itself so that they can investigate the bug.

How does MayaCrasher work

Ok so, to save your beautiful artwork when Maya is frozen, we must find a way to open the Maya’s crash handler. To do that, Maya should do an invalid operation. But, not only this is difficult using the Maya itself, the user interface is even frozen.

As a side note: on Linux or Mac OS X is extremely easy to do that and it does not require interacting with Maya. It just takes to open a Terminal and write: pkill -11 maya. Press Enter and you’ll see the crash handler: the command you just wrote emulated a memory error(-11) caused by Maya. Other signal numbers can emulate other stuff, like -8 will emulate a math error, and so on.

On Windows there is no concept of signals (AFAIK), so a way to do the same thing is to literally cause a blatant memory error in Maya. How?

Windows exposes some commands (called APIs) to app developer to interact with the operating system. Some of them can make your program interact with another running on the same computer.

MayaCrasher basically does this:

  1. Prepare some code that tries to access to invalid memory
  2. Ask Windows to reserve some space in Maya’s memory
  3. Copy the code that crashes into Maya’s memory
  4. Ask Windows to execute the code we just copied in the role of Maya
  5. Hello crash handler :smile:

Simple as that.

For the record, here the source code of what is described here.

The user interface

I thought a lot how to write less code as possible for this app and have anyway a good UX (user experience). Forget a beautiful UI, it takes too much time and it’s not useful with a program like this.

Can we make a good UX even with an ugly terminal app?

To crash Maya, the only information needed is the PID (Process ID) of the target Maya. This is different every time you launch maya.exe, also multiple instances of Maya running on your computer have different PIDs. Actually, each one is identified by its own unique PID.

So, in the first iteration of MayaCrasher, these were the steps to crash maya:

  • Open the task manager
  • Go to the full process list
  • Find maya.exe
  • Read the PID of maya in another column
  • Open a command prompt
  • Type MayaCrasher.exe <PID HERE>
  • Maya crashed

Yuk! Too many steps. :unamused:

Another alternative I tried is to search automatically for a process called maya.exe and get its PID. But what happens when there is more than one Maya opened? We can crash all of them, maybe causing more data loss than ever. Or we can provide a list of alternatives that is extremely clear to an artist:

Hello! What Maya you want to crash?
1) maya.exe PID=12345
2) maya.exe PID=98765
3) maya.exe PID=45678

This can defnitely win the prize of worst UI 2023, it’s like playing at the PID lottery.

So, after digging into the Windows API list, I found some interesting API:

Both of them can be combined to create an interesting behaviour: a program that obtains the PID of a window that is pointed by the mouse.

If we keep it just like that, you open MayaCrasher and whatever is under the mouse starts crashing, first of all MayaCrasher itself. :rocket:

Ok, moving the mouse is not enough. Let’s improve it with an API that detects when the mouse is clicked and another one to get the program name of a PID.

In this way we can crash a program if both:

  • User clicked on it
  • The program name is maya.exe

The latter check prevents that the user misclicks on another program and crashes it.

So it’s done!

First time I used this, I tried with two Maya opened: Maya1 and Maya2. The aim was to crash Maya1.

  • Me: “I should crash Maya1”
  • Me: “But Maya2 is in front of it!”
  • Me: “Well let’s minimize Maya2”
  • Click to minimize
  • :boom:
  • Crashed Maya2.

:unamused:

Nope. A click only is not enough.

So, in the final version I decided to use CTRL + Click, which is something that cannot be pressed by mistake (while MayaCrasher is open, at least).

Result

I released MayaCrasher free and open-source, you can find it on GitHub. To download it, go in the Releases page and download MayaCrasher.zip.

Or, if you prefer to build it by yourself, you can download the sourcecode and compile it, it just needs Visual Studio.

Here’s a demo of MayaCrasher in action:

It is tested only with Maya 2019, even if theoretically it should work with all versions of Maya. Let me know if it also works with yours! :pray:

That’s it folks! Thank you for reading! :heart:

~~~

If you want to get in touch, drop me an email at ds4@the.al or ping me on IRC (the_al@freenode|libera|hackint) or Discord (the_al).