Context managers for changing scene state

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.

Building the set_file_prompt context manager

One often overlooked flag on the bloated maya.cmds.file command is

Get Practical Maya Programming with Python now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.