自定义Maya的addCheckCallback弹出消息

时间:2015-07-27 08:04:56

标签: python maya maya-api

当用户保存文件时,我希望在保存之前进行检查。如果检查失败,那么它就不会保存。我使用了mSceneMessage和kBeforeSaveCheck,但我不知道如何在失败时自定义弹出消息。这可能吗?

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)

现在显示

  

用户提供的回调取消了文件操作。

1 个答案:

答案 0 :(得分:4)

我对这个问题有点迟钝,但今天我需要类似的东西,所以我想我会做出回应。我无法决定是否在一般情况下推荐这个,但严格来说, 可以使用displayString命令在Maya界面中更改相当数量的静态字符串。简单的部分是,你知道你正在寻找的字符串

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))

在Maya 2015上运行此功能可查找超过30000个已注册的显示字符串,并返回单个匹配键:s_TfileIOStrings.rFileOpCancelledByUser。似乎对我很有希望。

这是您修改的初始代码,用于更改显示字符串:

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)