Executing different functions based on options selected
Question: I wanted to know if there is a way of populating a option menu with choices
and then each of the different choice gives the build button a different
function

for eg:

    
    
    Type = cmds.optionMenu('type',w = 300 ,label = 'Type of crowd:')
    cmds.menuItem( label='Walking' )
    cmds.menuItem( label='Running' )
    cmds.menuItem( label='Cheering' )
    cmds.button('Build',command = bld)
    
    def walk(*args):
        print (walking)
    
    def run(*args)
        print (running)
    
    def cheer(*args)
        print (cheer)
    

so if the menu item selected would be walking the button command would execute
the command wak and if the menu item selected would be running then the button
command would execute the command run and so on.... is this even possible in
maya python...????


Answer: There's three parts to the problem.

First, you want your options to be callables. I like functools.partial for
that, so that you could give the same command different parameters and have it
be treated as two different actions:

    
    
    from functools import partial
    bigcircle = functools.partial ( cmds.circle, radius = 10)
    littleCircle = functools.partial (cmds.circle, radius = 1)
    

the second problem is that menuItems in OptionMenus don't fire their commands
directly. They trigger the -cc change command on the owning optionMenu. So we
need something that will turn the label back into a callable object. A little
class will do:

    
    
    class menuMgr(object):
        '''call the function associated with a key in the **callables dictionary''' 
        def __init__(self, **callables):
            self.Callables = callables
    
        def __call__(self, *args):
            self.Callables[args[-1]]()
    

The third part is to match these with a label. You can do this elegantly with
the **kwargs syntax, where you can either pass in a whole dictionary or named
keywords:

    
    
    def menu_of_functions(**callables):
        mmgr = menuMgr(**callables)
        Main = cmds.optionMenu('type3',w = 300 ,label = 'Type of crowd:', cc = mmgr)
        for key, partial in callables.items():
            cmds.menuItem(label = key)
        cmds.setParent("..") 
    

Heres the whole thing in working form to inspect:

    
    
    import maya.cmds as cmds
    import functools
    bigCircle = functools.partial ( cmds.circle, radius = 10)
    littleCircle = functools.partial (cmds.circle, radius = 1)
    
    class menuMgr(object):
        def __init__(self, **callables):
            self.Callables = callables
    
        def __call__(self, *args):
            self.Callables[args[-1]]()
    
    
    def menu_of_functions(**callables):
        mmgr = menuMgr(**callables)
        Main = cmds.optionMenu('type3',w = 300 ,label = 'Type of crowd:', cc = mmgr)
        for key, partial in callables.items():
            cmds.menuItem(label = key)
        cmds.setParent("..") 
    
    q = cmds.window()
    cmds.columnLayout()
    menu_of_functions(big = bigCircle, small = littleCircle)
    cmds.showWindow(q)
    

