Maya / Python:如何在用户按下textField后关闭自定义UI窗口?

时间:2012-09-06 22:59:58

标签: python window maya

我需要知道在用户按Enter后如何正确关闭此窗口。

txtA = cmds.textField(w = 345, h = 28, text = item, ec = renFc, aie = True)

按Enter键调用此功能:def ren():

我尝试将cmds.deleteUI(renUI)放入refFc函数,但这会导致崩溃。

以下是完整代码:

import maya.cmds as cmds

'''
Rename popup box for outliner - copy/paste this script into a hotkey field in the hotkey editor
'''


class ReUI():
    def __init__(self):

        renUI = 'renUI'

        if cmds.window(renUI, exists = True):
            cmds.deleteUI(renUI)

        renUI = cmds.window(renUI, t = 'JT Rename UI', sizeable = True, tb = True, mnb = False, mxb = False, menuBar = True, tlb = False, nm = 5)
        form = cmds.formLayout()
        rowA = cmds.rowColumnLayout(w = 350, h = 30)



        item = cmds.ls(os = True)[0]

        def ren():
            def renFc(self):
                print 'yes'
                tval = cmds.textField(txtA, text = True, q = True)
                cmds.rename(item, tval)

            txtA = cmds.textField(w = 345, h = 28, text = item, ec = renFc, aie = True)

        ren()

        cmds.showWindow(renUI)



r = ReUI()

1 个答案:

答案 0 :(得分:2)

你正在遇到我害怕的一个小小的错误..有关详细信息,请查看此处的主题:http://forums.cgsociety.org/archive/index.php/t-1000345.html

只是因为链接死了 - 基本上,在2011/2012年,看起来有一个问题,在Maya / QT完成清理之前点击进入并调用deleteUI删除对象,从而给你一个段错误类型的情况。

但是有一种解决方法,你想要导入maya.utils包并在deleteUI调用周围使用executeDeferred()命令(参见:http://download.autodesk.com/global/docs/maya2013/en_us/index.html?url=files/Python_Python_in_Maya.htm,topicNumber=d30e725143)。请查看2013年,看看这是否已修复?

我刚刚修改了你的代码以插入相关的行来演示它的工作原理,但它在很大程度上依赖于字符串'renUI'

(哦,那个函数ren()并不是真正有帮助的..你可以摆脱它并且取消阻止它。)

import maya.utils # you need this line!
class ReUI():
    def __init__(self):

        renUI = 'renUI'

        if cmds.window(renUI, exists = True):
            cmds.deleteUI(renUI)

        renUI = cmds.window(renUI, t = 'JT Rename UI', sizeable = True, tb = True, mnb = False, mxb = False, menuBar = True, tlb = False, nm = 5)
        form = cmds.formLayout()
        rowA = cmds.rowColumnLayout(w = 350, h = 30)



        item = cmds.ls(os = True)[0]

        def ren():
            def renFc(self):
                print 'yes'
                tval = cmds.textField(txtA, text = True, q = True)
                cmds.rename(item, tval)
                maya.utils.executeDeferred("cmds.deleteUI('renUI')") # and this one!

            txtA = cmds.textField(w = 345, h = 28, text = item, ec = renFc, aie = True)

        ren()

        cmds.showWindow(renUI)



r = ReUI()