2014-01-10 64 views
1

我想知道是否有填充带有多个选项的选项菜单,然后每个不同的选择的方式给出了构建按钮不同的功能基于期权执行不同的功能

为如:

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) 

所以如果选择的菜单项是行走按钮命令将执行命令wak ,如果选择的菜单项将运行,那么按钮命令将执行命令运行等等...... 是这样甚至可能在玛雅蟒蛇...... ????

+0

我真的不知道如何实现这一点,但你应该找到一种方式来拉所选'menuItem'中的'label',构建这些标签的词典(例如'funcdict = {“散步”:散步,“跑步”:跑步,“欢呼”:欢呼}'),并做'funcdict [sel_menu_item](* args)'。作为一个附注,我将'chr'函数重命名为'cheer'--它是一个关键字。 –

+0

感谢您的快速回复将尝试它,让你知道它是怎么回事! – Arnvfx

回答

0

当然你可以,因为函数是python中的第一类对象。

比方说,你有一个功能列表。

fns = [walk, run, cheer]

1)你需要从一个字符串键蟒蛇功能的映射。 让我们使用词典理解。

options = dict((fn.__name__, fn) for fn in fns)

或者你可以建立任意键的字典。

options = {"Walking": walk, "Running": run, "Cheering": cheer}

2)通过访问与该功能名称词典项获取对函数的引用。

options['run'](*args)

3)???

4)利润

+0

由于他已经有menuItems,我推荐他只是使用menuItems的标签进行映射,例如, 'options = {“Walking”:walk,...}'没有理由从'fn .__ name__'映射到'fn' –

+0

不够公平。我会尽快修改我的答案。 –

+0

这是伟大的意见,但我将如何将这些功能附加到按钮的命令标志...? – Arnvfx

0

有机会到今天这个对做研究小一点,但我实际上并不使用Maya的Python因此这是不可行的代码 - 至少它应该是接近!

def walk(*args): 
    print ("walking") 

def run(*args) 
    print ("running") 

def cheer(*args) 
    print ("cheer") 

fncdict = {"Walking":walk,"Running":run,"Cheering":cheer} 

def dofunc(funcname): 
    try: fncdict[funcname]() 
    except KeyError: print("adsmith doesn't really know how this crap works, 
          and gave you some really shoddy advice. Go downvote 
          his answer.") 

Type = cmds.optionMenu('type',w = 300 ,label = 'Type of crowd:') 
# Please don't name things this way! naming conventions in PEP 8 
# make this look like a class not an instance of optionMenu 
cmds.menuItem(label='Walking', parent = Type) 
cmds.menuItem(label='Running', parent = Type) 
cmds.menuItem(label='Cheering', parent = Type) 

cmds.button('Build',command = lambda x: dofunc(Type.value)) 
# I'm assuming this is the button you want to use to say "GO", right? 

从我读过的很少,它看起来像optionMenu.value是指在活跃menuItem的文字,但我不能肯定地说 - 它可能只是为optionMenu文本在这种情况下,该按钮将会调用dofunc('Type of crowd:')这将返回我构建的例外以耻辱自己。

这是我知道可以工作的另一种选择,但它很丑陋,没有必要。

# all 
# those 
# functions 
# go 
# here 
# including 
# the dict and dofunc 

activeMenuItem = None 

Type = cmds.optionMenu('type',w = 300 ,label = 'Type of crowd:', 
         changeCommand = lambda x: activeMenuItem = x) 
# this fails because you can't do assignments in a lambda -- OOPS!! 

cmds.menuItem(label='Walking', parent = Type) 
cmds.menuItem(label='Running', parent = Type) 
cmds.menuItem(label='Cheering', parent = Type) 

cmds.button('Build',command = lambda x: dofunc(activeMenuItem)) 

optionMenuchangeCommand选项被称为每次更改项目的时间。我已将它分配到lambda,它将变量activeMenuItem更新为新激活的menuItem中的值,然后让该按钮引用该变量而不是查询optionMenu当前选定的按钮,但让我们诚实 - 这就是菜单是MADE做的。毫无疑问,这是一种不存储每一个选择的方法。

编辑:这最后一个将无法正常工作,因为您不能在LAMBDA表达式中进行分配。我的错!

+0

嘿谢谢你的帮助,但Type = cmds.optionMenu('type',w = 300,label ='人群类型:', changeCommand = lambda x:activeMenuItem = x)给我一个语法错误!有没有其他的方式..?因为我可以通过使用cmds.optionMenu('Type',q = True,v = True)来查询菜单项中的文本是否会帮助我尝试执行此cmds.button('Build',label ='Build',w = 300,h = 50,bgc =(0,1,0),actOnPress = True,command = cmds.optionMenu('Type',q = True,v = True))但它没有工作 – Arnvfx

+0

哦,拍你可以在拉姆达内不做任务,没错。不确定 - 那么你必须咨询你的文档,因为我以前从来没有使用Python的这个实现。抱歉! –

+0

@Arnvfx如果我告诉你写我返回活动'menuItem'的'label'属性的代码 - 你会怎么写? –

1

这个问题有三个部分。

首先,你希望你的选项是可以调用的。我喜欢为functools.partial,这样就可以给相同的命令不同的参数,并把它作为被两个不同的动作处理:

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

第二个问题是,在OptionMenus的菜单项不直接解雇他们的命令。他们触发拥有optionMenu的-cc更改命令。所以我们需要一些将标签变回可调用对象的东西。一个小类将做:

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]]() 

第三部分是将这些与标签匹配。您可以使用** kwargs语法,在那里你可以通过在整个字典或关键字命名的优雅做这样的:

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("..") 

继承人的工作形式整件事检查:

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) 
+0

有没有办法执行通过选择按钮而不是突出显示的命令..? – Arnvfx

+0

这只是按钮(标签=“这里有一些标签”,c = bigCircle)。部分是伟大的按钮。按钮(标签=“小”,c =部分(cmds.button,半径= 1))也适用 – theodox

0

我(标签=你好,亲= =问候) cmds.menuItem(label = Hello,parent) ='问候')

DEF问候(*参数) 的菜单项= cmds.optionMenu( '问候',Q =真,V =真) 如果的菜单项=喜 打印的 “hi” menuItemsnew = cmds.optionMenu( '问候',Q =真,v = TRUE) 如果menuItemsnew =你好 打印“你好”

这应该工作,它为我工作