0

I am a newbie to coding, and I am trying to find a way to have a search field to filter only the buttons I want.I created an icon text button in a window and want to filter it based on button label name

Here is an example to show what I am trying to do.Also, all those circle label names are circleA,circleB,circleC,circleD. If I type circle, I want to show the buttons that has that name

https://ibb.co/gNnDr5

I found this page that does exactly what I want but, how to I change so that it looks for label names and only show the one I typed for.Also, by default I want to show all the icons

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

skb
  • 1
  • 1

1 Answers1

0

Here's a simple example of the strategy you'd want to use. This uses buttons, not icontextbuttons, but the idea is the same. The structure is the part you'd want to copy; the whole thing is wrapped in a columnLayout for expandability, the filter is a RowLayout with a text field and a button, then all of the buttons are in a second columnLayout that can expand and contract as you show-hide the buttons.

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)

Basically what you do is to collect the filter-able names while you're creating buttons into a dictionary that maps them to the actual GUI widgets. Then you can loop over the whole thing, setting the visibility according to the filter. In a columnLayout they will automatically close up the gaps when you change their visibility.

You could use this trick on sets of controls by creating layouts and show/hiding them the same way instead of managing individual controls.

theodox
  • 12,028
  • 3
  • 23
  • 36
  • usually the commands will be applied like `cmds.button(label = 'some label', c=some_python_function)`. There's more on this [here](https://theodox.github.io/2014/maya_callbacks_cheat_sheet) – theodox Sep 26 '17 at 22:02
  • oh... and i'd strongly recommend learning python from the beginnning. MEL is much harder to work with for anything serious, and nowadays it's not that valuable for your career compared to Python – theodox Sep 26 '17 at 22:03
  • Thank you,as I am still a newbie mel is little easy to understand.My goal is to slowly move from mel to python. Sorry, if I add the command it's creating the same command to all buttons.How can I say that "apple" button creates cube and "oranges" button creates cone – skb Sep 26 '17 at 22:09
  • Thank you so much. One more question, the mel script I found shows the results of my search by pressing enter.Can we do the same in python so that we can remove the filter button? Thanks – skb Sep 26 '17 at 22:23
  • to do that you'd hook the same `apply_filter` command to the `enterCommand` of the text field: `cmds.textField(filter_field, e=True, enterCommand= apply_filter)` down at where the button command is added – theodox Sep 27 '17 at 05:52