0

我需要知道用户按下回车后如何正确关闭此窗口。

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

按下回车后调用此函数: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()
4

1 回答 1

2

You're running into a teensy little bug I'm afraid.. for more details check out the thread here: http://forums.cgsociety.org/archive/index.php/t-1000345.html .

Just incase the link dies - basically, in 2011/2012 it looks like there's an issue where hitting enter and invoking deleteUI deletes the object before Maya/QT have finished cleanup, and thus giving you a segfault type situation.

There's a sort of a workaround for it though, you want to import the maya.utils package and use the executeDeferred() command (see: http://download.autodesk.com/global/docs/maya2013/en_us/index.html?url=files/Python_Python_in_Maya.htm,topicNumber=d30e725143 ) around your deleteUI call. Check in 2013 to see if this is fixed perhaps?

I've just hacked your code a little to insert the relevant line to demo it works, but it's heavily dependent on that string 'renUI'

(oh, and that function ren() isn't reaaaly helping.. you can get rid of it and unindent that block.)

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()
于 2012-10-06T14:54:59.477 回答