Search code examples
pythonmayamaya-api

Customize Maya's addCheckCallback pop up message


When the user saves a file I want a check to happen prior to saving. If the check fails then it doesn't save. I got this working with mSceneMessage and kBeforeSaveCheck, but I don't know how to customize the pop-up message when it fails. Is this possible?

import maya.OpenMaya as om
import maya.cmds as cmds

def func(retCode, clientData):
    objExist = cmds.objExists('pSphere1')
    om.MScriptUtil.setBool(retCode, (not objExist) ) # Cancel save if there's pSphere1 in the scene

cb_id = om.MSceneMessage.addCheckCallback(om.MSceneMessage.kBeforeSaveCheck, func)

Right now it displays

File operation cancelled by user supplied callback.


Solution

  • I'm a bit slow to the question, but I needed something similar today so I figured I'd respond. I cannot decide if I would recommend this in the general case, but strictly speaking, it is possible to change a considerable number of static strings in the Maya interface using the displayString command. The easy part is, you know the string you are looking for

    import maya.cmds as cmds
    
    message = u"File operation cancelled by user supplied callback."
    
    keys = cmds.displayString("_", q=True, keys=True)
    for k in keys:
        value = cmds.displayString(k, q=True, value=True)
        if value == message:
            print("Found matching displayString: {}".format(k))
    

    Running this on Maya 2015 finds over 30000 registered display strings and returns a single matching key: s_TfileIOStrings.rFileOpCancelledByUser. Seems promising to me.

    Here's your initial code modified to change the display string:

    import maya.OpenMaya as om
    import maya.cmds as cmds
    
    
    def func(retCode, clientData):
        """Cancel save if there is a pSphere1 in the scene"""
    
        objExist = cmds.objExists('pSphere1')
    
        string_key = "s_TfileIOStrings.rFileOpCancelledByUser"
        string_default = "File operation cancelled by user supplied callback."
        string_error = "There is a pSphere1 node in your scene"
    
        message = string_error if objExist else string_default
        cmds.displayString(string_key, replace=True, value=message)
    
        om.MScriptUtil.setBool(retCode, (not objExist))
    
    
    cb_id = om.MSceneMessage.addCheckCallback(om.MSceneMessage.kBeforeSaveCheck, func)