我一直在这里阅读,但我无法在网上找到解决问题的任何解决方案。我想我的缩进是正确的,但我仍然得到名称错误。有人可以帮助我。 这个脚本应该在maya中运行一个新的面板,它的工作类型,但错误真的很烦人。
class PanelWindow( object ):
def __init__( self, name, title, namespace=__name__ ):
self.__name__ = name
self._title = title
self.instance = str(namespace) + '.' + self.__name__
if not pm.scriptedPanelType(self.__name__, q = True, ex = True):
pm.scriptedPanelType(self.__name__, u = True)
jobCmd = 'python(\\\"%s._setup()\\\")' % self.instance
job = "scriptJob -replacePrevious -parent \"%s\" -event \"SceneOpened\" \"%s\";" % ( self.__name__, jobCmd )
mel.eval(job)
pm.scriptedPanelType( self.__name__, e = True,
unique=True,
createCallback = 'python("%s._createCallback()")' % self.instance,
initCallback = 'python("%s._initCallback()" )' % self.instance,
addCallback = 'python("%s._addCallback()" )' % self.instance,
removeCallback = 'python("%s._removeCallback()")' % self.instance,
deleteCallback = 'python("%s._deleteCallback()")' % self.instance,
saveStateCallback = 'python("%s._deleteCallback()")' % self.instance
)
def _setup(self):
"""Command to be call for new scene"""
panelName = pm.sceneUIReplacement( getNextScriptedPanel=(self.__name__, self._title) )
if panelName == '':
try:
panelName = pm.scriptedPanel( mbv=1, unParent=True, type=self.__name__, label=self._title )
except:
pass
else:
try:
label = panel( self.__name__, query=True, label=True )
pm.scriptedPanel( self.__name__, edit=True, label=self._title )
except:
pass
def _addCallback(self):
"""Create UI and parent any editors."""
print 'ADD CALLBACK'
def show( self ):
mel.eval('tearOffPanel "%s" %s true;' % (self._title, self.__name__) )
global test
test = PanelWindow('myName', 'Light')
test.show()
# NameError: name '__main__' is not defined #
# Error: line 1: name '__main__' is not defined
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# NameError: name '__main__' is not defined #
答案 0 :(得分:12)
执行Python脚本时,Python解释器将名为__name__
的变量设置为正在执行的模块的字符串值"__main__"
(通常此变量包含模块名称)。
通常检查此变量的值以查看是否正在导入模块以用作库,或者是否直接执行该模块。所以你经常在模块的末尾看到这段代码:
if __name__ == '__main__':
# do stuff
我怀疑你从'__main__'
中删除了字符串引号,这给出了你正在看的NameError
>>> if __name__ == __main__:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__main__' is not defined
答案 1 :(得分:4)
你的问题是一些问题,我只提供了代码的一些基本部分,因为其余部分不需要。
问题一是__name__
,如果引用它,我们就不会有问题,因为它只是一个名字而不是什么特别的,我只是将其重命名为name
问题二是面板/面板类型上的重复名称。 IE:
pm.scriptedPanelType(self.__name__, u = True)
pm.scriptedPanel( self.__name__, edit=True, label=self._title )
Maya不喜欢panelType和面板具有相同的名称。
所以:
import maya.cmds as cmds
import pymel.core as pm
import maya.mel as mel
class PanelWindow( object ):
def __init__(self, name, title):
self._name = name
self._title = title
self.panelTypeName = self._name + "Type"
if not pm.scriptedPanelType(self.panelTypeName, query=True, exists=True):
pm.scriptedPanelType(self.panelTypeName, unique=True)
if not pm.scriptedPanel(self._title, query=True, exists=True):
## Only allows one instance
pm.scriptedPanel(self._title, menuBarVisible=1, unParent=True, type=self.panelTypeName, label=self._title )
def _addCallback(self):
"""Create UI and parent any editors."""
print 'ADD CALLBACK'
def show( self ):
mel.eval('tearOffPanel "%s" "%s" true;' % (self._title, self._name) )
PanelWindow('lightControlType1', 'lightControl').show()