One frustrating aspect of Maya's design, from a coding perspective at least, is the single global scene and application state. To ensure state is changed and restored effectively, we can use context managers. There are a few common or interesting areas where we may want to apply them.
You can put all of the context managers we create in the following sections into
mayautils.py. For the most part, their usage follows the same patterns we've already seen, so I've chosen not to clutter the chapter with demonstrations. The code files with this book include tests you can run to see them in action.
One often overlooked flag on the bloated
maya.cmds.file command is