Maya Python静态类功能范围
问题描述:
我有一些麻烦让我的静态类工作。我缺少关于一个班级内功能范围的内容。如果调用该脚本给我以下错误:Maya Python静态类功能范围
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)
如何在类中定义这些函数?我打电话给那个模块:
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()
也许iam在这边做错了什么?赞赏任何帮助。
答
在这种情况下,您需要为类中的所有方法添加self
引用。常用的Python类看起来是这样的:
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
引用是你如何让在类的成员变量和其他功能 - 这是指的是什么其他语言调用this
的Python的方式。
只能在一个实例上调用实例方法(它是以self
传入的实例)。您可以使用@classmethod
装饰器创建一个在类本身上调用的方法 - 而不是该类的任何特定实例。类方法也有争论,但不是self
它是对类的引用。您可以使用同样的方式来获得在类级别定义的变量,它是由类的所有副本共享:
class HasShared(object):
shared = 99
@classmethod
def a_class_method(cls):
print cls.shared
(你可以混合在同一个班级比赛类和实例方法)。
您还可以使用@staticmethod
装饰器制作静态方法。这些没有得到默认参数都:
class NotPythonic(object):
@staticmethod
def needs_no_args():
print "call me as NotPythonic.needs_no_args()"
在Python中,我们倾向于避免这个公式,因为你可以通过只创建一个模块中的功能未做类来保存他们得到的静态方法。对于您发布的示例代码,我可能只是使用实例方法创建一个常规类,因为您的函数需要gui小部件的名称才能真正向他们提出问题。
谢谢你的帮助。我在我的课程中打电话给我说我的creatWindow函数时遇到了另一个问题。我明白我需要调用如下函数:self.createWindow。但它给了我错误“#NameError:name'self'未定义#”。 –
您需要为所有成员函数添加一个参数。称它为“自我”只是一种惯例,但必须有一些东西 – theodox
好吧我正在接近修复它。我的问题留在我的“disableCostumFrames”函数中。这些使用部分函数,被称为:cmds.radioButton(label ='Full',select = True,onCommand = partial(disableCostumFrames,Blast_startFrame,Blast_endFrame))。我如何去与自己一起使用它们? –