简体   繁体   English

自定义Maya的addCheckCallback弹出消息

[英]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. 我使用了mSceneMessage和kBeforeSaveCheck,但我不知道如何在失败时自定义弹出消息。 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. 用户提供的回调取消了文件操作。

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. 我不能决定我是否会推荐这在一般情况下,但严格来说,它可以改变相当数量的使用玛雅接口的静态字符串displayString命令。 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 . 在Maya 2015上运行此命令会发现超过30000个已注册的显示字符串并返回单个匹配键: 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM