我正在尝试使用maya python中的单独按钮从optionMenu中删除菜单项

时间:2014-01-22 19:42:32

标签: python maya

我正在编写此代码以导入纹理。我把它们拉到一个下拉菜单中,我正在尝试制作另一个按钮,这个按钮可以让我从该选项菜单中删除menuItems。底部的clearList函数是我遇到很多麻烦的部分。这个功能现在可能没有任何意义,因为我不知道我将如何实现这一目标。这是我的代码:

import maya.cmds as cmds
import os.path
import os

class TextureImport():
    def __init__(self):
        if cmds.window(TextureImport, q=True, ex=True):
            cmds.deleteUI(TextureImport)
        GUI=cmds.window(title="Texture Import Tool", widthHeight=(250,200), s=True, tlb=True)
        cmds.columnLayout(adj=True)
        cmds.button(label="Select Directory", command=self.selectDir)
        cmds.optionMenu('optionMenu', label="File List")
        cmds.button(label="Clear List", command=self.clearList)
        cmds.flowLayout()
        cmds.text('Select your object!', h=25)
        cmds.button(label="Apply Texture")
        cmds.setParent('..')
        cmds.showWindow()

    def selectDir(self, *args):
        basicFilter = "Image Files (*.jpg *.jpeg *.tga *.png *.tiff *.bmp *.psd)"
        myDir = cmds.fileDialog2 (fileFilter=basicFilter, dialogStyle=2, fm=3)
        myFiles = os.listdir(myDir[0])
        print myFiles

        for items in myFiles:
            fileEndings = ('.psd','.PSD','.jpg','JPG','.jpeg','.JPEG','.tga','.TGA','.png','.PNG','.tiff','.TIFF','.bmp','.BMP')
            if items.endswith(fileEndings):
                cmds.menuItem(items)
            else:
                cmds.warning(items + 'This is not a valid image type, you fool.')

    def clearList(self, *args):
        cmds.optionMenu('optionMenu', q=True)
        if optionMenu != 0:
            deleteUI (optionsMenu, menuItem=True)
TextureImport()

2 个答案:

答案 0 :(得分:1)

要删除菜单项目,您必须在项目上调用deleteUI

def clearList(self, *args):
    menuItems = cmds.optionMenu('optionMenu', q=True, itemListLong=True) # itemListLong returns the children
    if menuItems:
        cmds.deleteUI(menuItems)

答案 1 :(得分:0)

您没有将选项菜单存储到变量中:

def clearList(self, *args):
    cmds.optionMenu('optionMenu', q=True)
    if optionMenu != 0:
        deleteUI (optionsMenu, menuItem=True)

应该是

def clearList(self, *args):
    menu = cmds.optionMenu('optionMenu', q=True)  # stored result
    if menu:                                      # took out unnecessary comparison
        cmds.deleteUI(menu, menuItem=True)        # added "cmds." for consistency of namespace