这个问题有三个部分。
首先,你希望你的选项是可以调用的。我喜欢为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)
我真的不知道如何实现这一点,但你应该找到一种方式来拉所选'menuItem'中的'label',构建这些标签的词典(例如'funcdict = {“散步”:散步,“跑步”:跑步,“欢呼”:欢呼}'),并做'funcdict [sel_menu_item](* args)'。作为一个附注,我将'chr'函数重命名为'cheer'--它是一个关键字。 –
感谢您的快速回复将尝试它,让你知道它是怎么回事! – Arnvfx