Edit: Wie DrWeeny sagte, wäre es am besten zu den anderen ähnlichen Themen zu beziehen.
Ich habe ein wenig Erinnerung auf meinem Blog vor einiger Zeit, all die verschiedenen nativen Maya UI Art und Weise Befehle der Verwendung mit einer schnellen Art und Weise zu testen, um der Lage sein, sie zu testen:
- classic Maya String
- Funktion als Argument
- lambda
- functools.partial
- pymel.core.Callback
Jeder Fall wird auch mit Beispielen mit dem Übertragen von Variablen als Argumente für diese Funktionen gegeben. Denn manchmal muss man es können. Insgesamt empfehle ich die Verwendung functools.partial, es gibt nur Vorteile gegenüber den anderen (wenn Sie PySide vergessen).
Maya UI types
def function(*args):
print args
cmds.textFieldGrp(text, edit=True, text=str(args))
variable = 'Variable'
width = [1, 250]
align = [1, 'left']
window = cmds.window(title='UI and commands arguments.')
cmds.columnLayout()
cmds.textFieldGrp(label="\"function()\"", changeCommand="function()", columnWidth=width, columnAlign=align)
cmds.textFieldGrp(label="function", changeCommand=function, columnWidth=width, columnAlign=align)
cmds.textFieldGrp(label="\"function(variable)\"", changeCommand="function(variable)", columnWidth=width, columnAlign=align)
cmds.textFieldGrp(label="lambda x: function(variable)", changeCommand=lambda x: function(variable), columnWidth=width, columnAlign=align)
cmds.separator(style="double", height=20)
import functools
cmds.textFieldGrp(changeCommand=functools.partial(function), label='functools.partial(function)', columnWidth=width, columnAlign=align)
cmds.textFieldGrp(changeCommand=functools.partial(function, variable), label='functools.partial(function, variable)', columnWidth=width, columnAlign=align)
cmds.separator(style="single", height=20)
import pymel.core
cmds.textFieldGrp(changeCommand=pymel.core.Callback(function), label='pymel.core.Callback(function)', columnWidth=width, columnAlign=align)
cmds.textFieldGrp(changeCommand=pymel.core.CallbackWithArgs(function), label='pymel.core.CallbackWithArgs(function)', columnWidth=width, columnAlign=align)
cmds.textFieldGrp(changeCommand=pymel.core.CallbackWithArgs(function, variable), label='pymel.core.CallbackWithArgs(function, variable)', columnWidth=width, columnAlign=align)
cmds.separator(style="single", height=20)
text = cmds.textFieldGrp(label='RESULT: ', text='', width=500)
cmds.showWindow()
Wenn eine Klasse mit
Weil es bei der Verwendung von Klasse im Auge nicht gemacht wurde, hat einige Möglichkeiten nicht funktionieren, wenn Sie in einer Klasse sind.
class MayaUI():
def __init__(self):
self.variable = 'Variable'
self.width = [1, 250]
self.align = [1, 'left']
self.window = cmds.window(title='UI and commands arguments.')
cmds.columnLayout()
cmds.textFieldGrp(label="\"self.function()\"", changeCommand="self.function()", columnWidth=self.width, columnAlign=self.align)
cmds.textFieldGrp(label="self.function", changeCommand=self.function, columnWidth=self.width, columnAlign=self.align)
cmds.textFieldGrp(label="\"self.function(self.variable)\"", changeCommand="self.function(self.variable)", columnWidth=self.width, columnAlign=self.align)
cmds.textFieldGrp(label="lambda x: self.function(self.variable)", changeCommand=lambda x: self.function(self.variable), columnWidth=self.width, columnAlign=self.align)
cmds.separator(style="double", height=20)
import functools
cmds.textFieldGrp(changeCommand=functools.partial(self.function), label='functools.partial(self.function)', columnWidth=self.width, columnAlign=self.align)
cmds.textFieldGrp(changeCommand=functools.partial(self.function, self.variable), label='functools.partial(self.function, self.variable)', columnWidth=self.width, columnAlign=self.align)
cmds.separator(style="single", height=20)
import pymel.core
cmds.textFieldGrp(changeCommand=pymel.core.Callback(self.function), label='pymel.core.Callback(self.function)', columnWidth=self.width, columnAlign=self.align)
cmds.textFieldGrp(changeCommand=pymel.core.CallbackWithArgs(self.function), label='pymel.core.CallbackWithArgs(self.function)', columnWidth=self.width, columnAlign=self.align)
cmds.textFieldGrp(changeCommand=pymel.core.CallbackWithArgs(self.function, self.variable), label='pymel.core.CallbackWithArgs(self.function, self.variable)', columnWidth=self.width, columnAlign=self.align)
# A bit more complicated
_map = {'textFieldGrp': lambda arg:cmds.textFieldGrp(arg, query=True, text=True)}
_com = lambda *args:args[0](self.variable, _map[args[1]](args[2]))
cmds.textFieldGrp('textfieldName', changeCommand=pymel.core.Callback(_com, self.function, 'textFieldGrp', 'textfieldName'), label="pymel.core.Callback(_com, self.function, 'textFieldGrp', 'textfieldName') + lambdas", columnWidth=self.width, columnAlign=self.align)
cmds.separator(style="single", height=20)
self.text = cmds.textFieldGrp(label='RESULT: ', text='', width=500)
cmds.showWindow()
def function(self, *args):
print args
cmds.textFieldGrp(self.text, edit=True, text=str(args))
MayaUI()