NameError: name '__main__' is not defined
Solution 1
Your problem was a few things, I've only included a few basic sections of the code as the rest wasn't needed.
Problem one was __name__
, if this were quoted, we wouldn't have a problem, seeing as it's just a name and not anything special, I'm just going to rename this to name
.
Problem two was duplicate names on panels/panelTypes. IE:
pm.scriptedPanelType(self.__name__, u = True)
pm.scriptedPanel( self.__name__, edit=True, label=self._title )
Maya didn't like that both the panelType and the panel had the same names.
So:
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()
Solution 2
When executing Python scripts, the Python interpreter sets a variable called __name__
to be the string value "__main__"
for the module being executed (normally this variable contains the module name).
It is common to check the value of this variable to see if your module is being imported for use as a library, or if it is being executed directly. So you often see this block of code at the end of modules:
if __name__ == '__main__':
# do stuff
I suspect you have left the string quotes off of '__main__'
which gives the NameError you're seeing
>>> if __name__ == __main__:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__main__' is not defined
arvidurs
Updated on February 20, 2020Comments
-
arvidurs over 4 years
I have been reading here, but I couldnt find any solution online to solve my problem..I think I have the indentation right, but I still get the Name Error..Can someone help me out please. This script should run a new panel in maya, which works kind of, but the error is really annoying.
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 #
-
user2357112 over 10 yearsWhile it seems pointless to call
str
onnamespace
, it's extremely unlikely that this has anything to do with the error. -
arvidurs over 10 yearsIt removes the main error, now I get: # Error: 'PanelWindow' object has no attribute 'name' # Traceback (most recent call last): # File "<maya console>", line 50, in <module> # File "<maya console>", line 9, in init # AttributeError: 'PanelWindow' object has no attribute 'name' #
-
user2357112 over 10 years...or perhaps I'm missing something. I'm inclined to think the difference is due to something unrelated to this change, such as turning the interpreter off and on again, but downvote tentatively removed.
-
Shannon Hochkins over 10 yearsWhat version of Maya are you running @arvidurs?
-
arvidurs over 10 yearsHm weird, I closed it now several times..Still get the error. I ll try again. Thanks anyways for your effort!
-
arvidurs over 10 yearsalso testing on maya 2014
-
arvidurs over 10 yearsCould you contact me on skype please: arvidurs
-
arvidurs over 10 yearsYes if I call it like this PanelWindow('myName', 'Light') its fine. But when I want to use the tearoffpanel function, to create the window, I get the error again. Like this: PanelWindow('myName', 'Light').show()
-
Shannon Hochkins over 10 yearsTo start with, it might help if you provide us with what you're actually attempting to do, I'm going to clean up this conversation as it's getting too extensive. Please edit your original question, with what you're actually trying to do.