Customize Maya popup message addCheckCallback

When the user saves the file, I want validation to happen before saving. If the check fails, it will not be saved. I got this working with mSceneMessage and kBeforeSaveCheck, but I don't know how to set up the popup message when it fails. Is it 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 pSphere1 in the scene

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

      

It now displays

The file operation was canceled by a user-called callback.

+3


source to share


1 answer


I slowed down the question a bit, but I needed something similar today, so I figured I could answer. I can't decide if I recommend this in general, but strictly speaking it is possible to change a significant amount of static lines in the Maya interface using displayString

. The easy part, you know the line 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))

      

Doing this on Maya 2015 finds more than 30,000 lines for the display and returns a corresponding key: s_TfileIOStrings.rFileOpCancelledByUser

. Seems promising to me.



Here's your original 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)

      

+3


source







All Articles