2017-09-25 43 views
0

我是一個新手,編碼,和我試圖找到一種方法,有一個搜索欄僅過濾按鈕我want.I創建一個窗口的圖標文本按鈕,要根據按鈕標籤名稱搜索和過濾按鈕標籤名稱

對其進行過濾,這裏是展示什麼,我想do.Also一個例子,所有這些圓圈標註名稱circleA,circleB,circleC,盤旋。如果我型圈,我想證明這名字

https://ibb.co/gNnDr5

,我發現這個網頁,不正是我想要什麼樣的按鈕,但怎麼我改變,因此它會查找標籤名稱,只顯示一個我打字for.Also,默認情況下,我想告訴所有的圖標

http://melscriptingfordummies.blogspot.in/2011/02/mel-script-example-keyword-search.html

回答

0

這裏是你想使用該戰略的一個簡單的例子。這使用按鈕,而不是icontextbuttons,但想法是相同的。結構是你想要複製的部分;整個事情被包裝在一個可擴展的columnLayout中,過濾器是帶有文本字段和按鈕的RowLayout,然後所有的按鈕都在第二個ColumnLayout中,當你顯示隱藏按鈕時,這些按鈕可以展開和收縮。

import maya.cmds as cmds 

# some dummy commands. Not the use of "_", which is 
# a lazy way to ignore the argument which Maya will 
# add to all callback functions 

def apple_command(_): 
    cmds.polyCube() 

def orange_command(_): 
    cmds.polySphere() 

def banana_command(_): 
    cmds.polyPlane() 

window = cmds.window(title = "filter") 
root = cmds.columnLayout() 
# filter button and text field 
header = cmds.rowLayout(nc=2) 
filter_field = cmds.textField() 
filter_button = cmds.button(label = 'filter') 
cmds.setParent("..") 

# hideable butttons 
button_list = cmds.columnLayout() 
# create a bunch of buttons, storing the name in a dictionary for easier filtering 
# this is a dictionary with the label of the button and the commands you want to 
# attach to the buttons 
button_names = { 
    'apples': apple_command, 
    'oranges': orange_command, 
    'bananas': banana_comand, 
    'mangoes': apple_command, 
    'coconuts': orange_command, 
    'durians': banana_command 
    } 

button_widgets = {} 
for button_name, button_command in button_names.items(): 
     button_widgets[button_name] = cmds.button(label = button_name, width = 160, c= button_command) 
     # in a real application you'd also make the buttons do something.... 


# this makes the filter button apply the filter 
# defining it down here lets it 'remember' the name of the filter 
# field and the contents of the button dictionary 
def apply_filter(*_): 
    # get the current text in the filter field 
    filter_text = cmds.textField(filter_field, q=True, text=True) 
    # loop over the dictionary, setting the visiblity of the button 
    # based on the key and the filter 
    for key, value in button_widgets.items(): 
     viz = True 
     if len(filter_text): 
      viz = filter_text.lower() in key.lower() 
     cmds.button(value, edit=True, visible = viz) 

# now hook it too the button 
cmds.button(filter_button, e=True, c= apply_filter) 
cmds.showWindow(window) 

基本上你做的是,當你創建一個按鈕成它們映射到實際GUI控件的字典,收集過濾器能夠名稱。然後你可以遍歷整個事物,根據過濾器設置可見性。在columnLayout中,當您更改其可見性時,它們將自動縮小差距。

您可以通過創建佈局和顯示/隱藏他們以同樣的方式,而不是管理個人控制使用上套控制這一招。

+0

通常的命令將被應用於像'cmds.button(標記= '一些標籤',C = some_python_function)'。這裏有更多[這裏](https://theodox.github.io/2014/maya_callbacks_cheat_sheet) – theodox

+0

哦...我強烈建議從頭開始學習python。 MEL很難與任何嚴肅的事情一起工作,而且現在對於你的職業生涯來說它並不像Python那樣有價值 – theodox

+0

謝謝,因爲我還是一個新手mel很容易理解。我的目標是慢慢地從mel變成python 。對不起,如果我添加它創建相同的命令的命令將所有buttons.How我可以說,「蘋果」按鈕創建立方體和「橘子」按鈕創建錐 – skb