Avoid unnecessarily dirtying scene when using callbacks

At times, it may be useful to use callbacks such as after File > New, Save or Load to automate repetitive tasks or to setup your work environment. Examples of this application include setting up a node watcher or a callback after File > New to load settings from a file, or to create a template environment. However, these operations may cause the scene to become dirty unnecessarily, which may confuse users into thinking their scene needs to be saved when it is not the case.

To workaround this issue, you can reset the dirtiness of the scene by using the ` file -modified “true”|”false” ` command and flag.

As an example, refer to the $MAYA_LOCATION/scripts/others/supportRenderers.mel file in the Maya installation directory.

Upon loading a new scene, Maya checks if the current renderer is available, and if it isn't, Maya sets the current renderer to the preferred renderer instead. This setAttr action results in the scene becoming dirty. If the file was simply opened and no other modifications occurred, then it is unnecessary to dirty the scene.

The following code snippet does the following:

{
    int $wasModified = `file -query -modified`;
    setAttr
        "defaultRenderGlobals.currentRenderer"
        -type "string"
        $preferredRenderer;
    // If the scene wasn't dirtied prior to setting the current renderer,
    // we want to set the scene as not modified to avoid dirtying the scene.
    if (!$wasModified)
    {
        file -modified false;
    }
}