Maya Python调用modul类函数
问题描述:
嘿,我想了解在Maya中使用Python导入和重新加载脚本的过程。Maya Python调用modul类函数
我有下面的代码抛出以下错误:
# NameError: name 'MyClass' is not defined #
它创建的窗口,但是当我按下按钮它给我上述错误。如果有人能帮助我在这里失去的东西,那会很棒。
import maya.cmds as cmds
from functools import partial
class MyClass():
@classmethod
def createWindow(cls):
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.button(label='Playblast' ,command= 'MyClass.createPlayblast()')
cmds.showWindow(window)
@classmethod
def createPlayblast(cls):
cmds.playblast(f= "playblast", fmt= "image")
print "hallo"
MyClass.createWindow()
我打电话我MODUL这样的:
# call loadTest
import sys
Dir = 'S:/people/Jan-Philipp/python_scripts'
if Dir not in sys.path:
sys.path.append(Dir)
try: reload(loadTest)
except: from loadTest import MyClass
loadTest.MyClass()
干杯,希望大家有一个愉快的一天!
答
您可能想要从窗口中删除MyClass.createWindow()
,并将其留给调用代码。正如所写,它会创建一个窗口,每当你导入该模块。最好只将初始化代码放入模块范围中。
在这种情况下的问题是,您正试图调用该类,就好像它是一个函数。如果只想类方法,你会做这样的
import loadTest
loadTest.MyClass.createWindow()
在Python中,我们一般不需要做让类只具有类方法:一个模块通常是你想要的使用。在这种情况下:
import maya.cmds as cmds
from functools import partial
def createWindow():
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.button(label='Playblast' ,command= createPlayblast)
cmds.showWindow(window)
def createPlayblast():
cmds.playblast(f= "playblast", fmt= "image")
print "hallo"
和
import loadTest
loadTest.createWindow()
模块是用于分组相关功能不是类更好的工具。如果类包含一些持久数据,则类只在Python中有意义。
在这种情况下,在你的文章中发现这行错了? cmds.button(label ='Playblast',command ='MyClass.createPlayblast()') 会是这样的:或? cmds.button(label ='Playblast',command ='createPlayblast()') –
是的,这是正确的。 – theodox
另外,不要使用回调的字符串引用版本 - 直接传递python函数。字符串版本在侦听器和生产代码中的工作方式不同,因此它们会产生大量的错误。请参阅https://theodox.github.io/2014/maya_callbacks_cheat_sheet – theodox