Baking animation Maya can take a long time. This can be sped up considerably if the viewport isn’t updated constantly.

paneLayout -e -manage false $gMainPane;

disables the viewport until it is re-enabled using

paneLayout -e -manage true$gMainPane;

But if the bake raises an exception the line to re-enable the viewport is never executed and Maya is stuck with a disabled viewport.
So I’ve wrapped this into a context manager to make sure the viewport is re-enabled before the exception is raised.

from maya import cmds, mel

class ViewportDisabled(object):
    '''
    Disables viewport updates to speed up processes like baking etc.
    Example:
    with ViewportDisabled():
        cmds.bakeResults(simulation=True, t=(1001, 1020))
    '''
    def __enter__(self):
        if not cmds.about(batch=True):
            mel.eval('paneLayout -e -manage false $gMainPane')

    def __exit__(self, exception_type=None, exception=None, tb=None):
        if not cmds.about(batch=True):
            mel.eval('paneLayout -e -manage true $gMainPane')
        if exception:
            raise # we don't want to silence exceptions

Example:

from maya import cmds

with ViewportDisabled():
    cmds.bakeResults(simulation=True, t=(1001, 1500))

This reduced baking 500 frames of animation from 200 seconds to 2 seconds for me. Of course your results will vary with the complexity of your rigs.

If you have any questions or comments please do get in touch on Twitter

You might be interested in
Other Tutorials