Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name '__main__' is not defined [closed]

Tags:

python

main

maya

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 # 
like image 386
arvidurs Avatar asked Feb 18 '14 22:02

arvidurs


People also ask

How do you fix NameError name is not defined?

The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.

Why is Python saying not defined?

The Python "NameError: function is not defined" occurs when we try to call a function that is not declared or before it is declared. To solve the error, make sure you haven't misspelled the function's name and call it after it has been declared.

What is if name == Main in Python?

Python files can act as either reusable modules, or as standalone programs. if __name__ == “main”: is used to execute some code only if the file was run directly, and not imported.


2 Answers

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
like image 176
Peter Gibson Avatar answered Nov 04 '22 16:11

Peter Gibson


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()
like image 29
Shannon Hochkins Avatar answered Nov 04 '22 14:11

Shannon Hochkins