Maya Python 静态 class 函数范围
Maya Python static class function scope
我的静态 class 无法正常工作。关于 class 中的函数范围,我缺少一些东西。如果调用脚本给我以下错误:
NameError: global name 'disableCostumFrames' is not defined #
import maya.cmds as cmds
from functools import partial
class Blast:
def createWindow():
# Todo:
# hanldes the gui for the user
windowID = 'window'
if cmds.window(windowID, exists = True):
cmds.deleteUI('window')
window = cmds.window(windowID, title="Blast", iconName='Blast', widthHeight=(400, 200) )
cmds.frameLayout( label='')
cmds.rowColumnLayout( numberOfColumns=4, columnWidth=[(1, 100),(3, 100)] )
cmds.text( label='Start: ' )
global Blast_startFrame
Blast_startFrame = cmds.textField( enable = False)
cmds.text( label=' End: ' )
global Blast_endFrame
Blast_endFrame = cmds.textField( enable = False)
cmds.setParent('..')
cmds.rowColumnLayout( numberOfColumns=2, columnWidth=[(1, 100), (2, 100)] )
cmds.radioCollection()
#cmds.radioButton( label='Full', select = True, onCommand= partial(disableCostumFrames, Blast_startFrame, Blast_endFrame ) )
#cmds.radioButton( label='Costum', onCommand= partial(enableCostumFrames, Blast_startFrame, Blast_endFrame ) )
cmds.setParent('..')
cmds.rowColumnLayout( numberOfColumns=1, columnWidth=[(1, 400), (2, 100)] )
cmds.button( label='Playblast' ,command= 'createPlayblast')
cmds.setParent('..')
cmds.showWindow( window )
return Blast_startFrame, Blast_endFrame
def main():
createWindow()
def enableCostumFrames(Blast_startFrame, Blast_endFrame, *args):
cmds.textField(Blast_startFrame, edit=True, enable=True)
cmds.textField(Blast_endFrame, edit=True, enable=True)
def disableCostumFrames(Blast_startFrame, Blast_endFrame, *args):
cmds.textField(Blast_startFrame, edit=True, text="", enable=False)
cmds.textField(Blast_endFrame, edit=True, text="", enable=False)
如何在 class 中定义这些函数?我这样调用模块:
import sys
Dir = 'c:/Blast'
if Dir not in sys.path:
sys.path.append(Dir)
try: reload(Blast_v011)
except: import Blast_v011
Blast_v011.Blast()
也许我这边做错了什么?感谢任何帮助。
在这种情况下,您需要添加对 class 中所有方法的 self
引用。通常的 python class 看起来像这样:
class MyClass(object):
def __init__(self):
self.variable = 123
def some_method(self):
print "my variable = ", self.variable
def some_other_method(self):
if self.variable > 1:
self.some_method()
成员函数中的 self
引用是您获取 class 成员变量和其他函数的方式——它是 python 引用其他语言调用的内容的方式 this
.
实例方法只能在实例上调用(它是作为 self
传入的实例)。您可以创建一个在 class 本身上调用的方法——而不是 class 的任何特定实例——使用 @classmethod
装饰器。 Classmethods 也有一个参数,但它不是 self
,而是对 class 的引用。您可以使用相同的方式获取在 class 级别定义的变量,这些变量由 class:
的所有副本共享
class HasShared(object):
shared = 99
@classmethod
def a_class_method(cls):
print cls.shared
(您可以在同一个 class 中混合搭配 class 和实例方法)。
您还可以使用 @staticmethod
装饰器创建静态方法。这些根本没有默认参数:
class NotPythonic(object):
@staticmethod
def needs_no_args():
print "call me as NotPythonic.needs_no_args()"
在 Python 中,我们倾向于避免这个公式,因为您可以通过在模块中创建一个函数而不用 class 来保存它们来获得静态方法。对于您发布的示例代码,我可能只是使用实例方法制作一个常规的 class,因为您的函数需要 gui 小部件的名称才能真正向它们提问。
我的静态 class 无法正常工作。关于 class 中的函数范围,我缺少一些东西。如果调用脚本给我以下错误:
NameError: global name 'disableCostumFrames' is not defined #
import maya.cmds as cmds
from functools import partial
class Blast:
def createWindow():
# Todo:
# hanldes the gui for the user
windowID = 'window'
if cmds.window(windowID, exists = True):
cmds.deleteUI('window')
window = cmds.window(windowID, title="Blast", iconName='Blast', widthHeight=(400, 200) )
cmds.frameLayout( label='')
cmds.rowColumnLayout( numberOfColumns=4, columnWidth=[(1, 100),(3, 100)] )
cmds.text( label='Start: ' )
global Blast_startFrame
Blast_startFrame = cmds.textField( enable = False)
cmds.text( label=' End: ' )
global Blast_endFrame
Blast_endFrame = cmds.textField( enable = False)
cmds.setParent('..')
cmds.rowColumnLayout( numberOfColumns=2, columnWidth=[(1, 100), (2, 100)] )
cmds.radioCollection()
#cmds.radioButton( label='Full', select = True, onCommand= partial(disableCostumFrames, Blast_startFrame, Blast_endFrame ) )
#cmds.radioButton( label='Costum', onCommand= partial(enableCostumFrames, Blast_startFrame, Blast_endFrame ) )
cmds.setParent('..')
cmds.rowColumnLayout( numberOfColumns=1, columnWidth=[(1, 400), (2, 100)] )
cmds.button( label='Playblast' ,command= 'createPlayblast')
cmds.setParent('..')
cmds.showWindow( window )
return Blast_startFrame, Blast_endFrame
def main():
createWindow()
def enableCostumFrames(Blast_startFrame, Blast_endFrame, *args):
cmds.textField(Blast_startFrame, edit=True, enable=True)
cmds.textField(Blast_endFrame, edit=True, enable=True)
def disableCostumFrames(Blast_startFrame, Blast_endFrame, *args):
cmds.textField(Blast_startFrame, edit=True, text="", enable=False)
cmds.textField(Blast_endFrame, edit=True, text="", enable=False)
如何在 class 中定义这些函数?我这样调用模块:
import sys
Dir = 'c:/Blast'
if Dir not in sys.path:
sys.path.append(Dir)
try: reload(Blast_v011)
except: import Blast_v011
Blast_v011.Blast()
也许我这边做错了什么?感谢任何帮助。
在这种情况下,您需要添加对 class 中所有方法的 self
引用。通常的 python class 看起来像这样:
class MyClass(object):
def __init__(self):
self.variable = 123
def some_method(self):
print "my variable = ", self.variable
def some_other_method(self):
if self.variable > 1:
self.some_method()
成员函数中的 self
引用是您获取 class 成员变量和其他函数的方式——它是 python 引用其他语言调用的内容的方式 this
.
实例方法只能在实例上调用(它是作为 self
传入的实例)。您可以创建一个在 class 本身上调用的方法——而不是 class 的任何特定实例——使用 @classmethod
装饰器。 Classmethods 也有一个参数,但它不是 self
,而是对 class 的引用。您可以使用相同的方式获取在 class 级别定义的变量,这些变量由 class:
class HasShared(object):
shared = 99
@classmethod
def a_class_method(cls):
print cls.shared
(您可以在同一个 class 中混合搭配 class 和实例方法)。
您还可以使用 @staticmethod
装饰器创建静态方法。这些根本没有默认参数:
class NotPythonic(object):
@staticmethod
def needs_no_args():
print "call me as NotPythonic.needs_no_args()"
在 Python 中,我们倾向于避免这个公式,因为您可以通过在模块中创建一个函数而不用 class 来保存它们来获得静态方法。对于您发布的示例代码,我可能只是使用实例方法制作一个常规的 class,因为您的函数需要 gui 小部件的名称才能真正向它们提问。