8

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 # 
arvidurs
  • 2,853
  • 4
  • 25
  • 36

2 Answers2

16

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
Peter Gibson
  • 19,086
  • 7
  • 60
  • 64
4

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()
Shannon Hochkins
  • 11,763
  • 15
  • 62
  • 95
  • While it seems pointless to call `str` on `namespace`, it's extremely unlikely that this has anything to do with the error. – user2357112 Feb 18 '14 at 23:01
  • It removes the __main__ error, now I get: # Error: 'PanelWindow' object has no attribute 'name' # Traceback (most recent call last): # File "", line 50, in # File "", line 9, in __init__ # AttributeError: 'PanelWindow' object has no attribute 'name' # – arvidurs Feb 18 '14 at 23:01
  • ...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. – user2357112 Feb 18 '14 at 23:02
  • 1
    What version of Maya are you running @arvidurs? – Shannon Hochkins Feb 18 '14 at 23:04
  • Hm weird, I closed it now several times..Still get the error. I ll try again. Thanks anyways for your effort! – arvidurs Feb 18 '14 at 23:05
  • also testing on maya 2014 – arvidurs Feb 18 '14 at 23:06
  • Could you contact me on skype please: arvidurs – arvidurs Feb 18 '14 at 23:27
  • Yes 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() – arvidurs Feb 18 '14 at 23:38
  • To 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. – Shannon Hochkins Feb 18 '14 at 23:48