The code in your example actually throws a NameError. # Error: NameError: file <maya console> line 6: global name 'window1' is not defined #
Scope That’s because of something known as *scope*. https://docs.python.org/2/tutorial/classes.html#python-scopes-and-namespaces Have a quick skim through this when you find the time to understand a little more about it. Being a bit dry, it might also be helpful to learn by example. http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules In a nutshell, it’s due to the variable window1 being defined in one function, whilst accessed in another. That variable is actually deleted as soon as the function completes. The reason it might have worked for you when posting this question might have been due to you first declaring it outside of any function, thus making it accessible everywhere. You can think of it as a hierarchy, where a inner function have access to variables declared outside of it, including outside of any function at all. AttributeError Now, why does window1.close() throw a “AttributeError”? type() and dir() might be able to make things a little more clear about what’s happening here. >>> window1 = cmds.window( title="Long Name")>>> window1.close() # Error: AttributeError: file <maya console> line 2: 'unicode' object has no attribute 'close' # >>> type(window1)# Result: <type 'unicode'> # What Python tells you here is that window1 is an instance of a class called “unicode”. As it happens, this class doesn’t have the close member you asked for. >>> dir(window1)# Result: ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', ... # Discarded for brievety Hint: Use type() when you need to know more about what a particular variable actually represents, and dir() to look inside of it and find out what it can do for you. In addition, help() will output some help for you. >>> help(unicode) help(unicode) Help on class unicode in module __builtin__: class unicode(basestring) | unicode(string [, encoding[, errors]]) -> object | | Create a new Unicode object from the given encoded string. | encoding defaults to the current default string encoding. | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'. | | Method resolution order: | unicode | basestring | object | | Methods defined here: ... # Discarded for brievety cmds.window is not object-oriented You might have expected your window1 variable to work similar to this? >>> import PySide.QtGui>>> window = PySide.QtGui.QWidget()>>> window.show()>>> >>> window.close() As it turns out, even though Maya’s GUI may be all PyQt/PySide, the cmds.window command won’t actually return you a Qt instance. It instead returns a unicode object. Just a string. >>> print window1'window1' To make matters a little more confusing, the name you’ve chosen for your variable just so happens to be the exact same name as Maya decided to name the physical window internally, thus returning what looks like the name of your variable. To visualise what’s actually going on, we can create a number of windows, and look at their names. >>> window1 = cmds.window(title="Long Name")>>> print window1'window2' .>>> window1 = cmds.window(title="Long Name")>>> print window1'window3' To make things a little easier to understand, let’s give it a name ourselves; myWindow. Now we can give cmds.showWindow this string, instead of a variable. >>> my_window = cmds.window("myWindow", title="Long Name")>>> >>> cmds.showWindow("myWindow") # This is the same thing Grand finale So, to answer your question, to detect if an existing window is already open before opening a new one, you can decide upon a name and check for it. if not cmds.window("myWindow", query=True, exists=True): my_window = cmds.window("myWindow", title="Long Name") cmds.showWindow(my_window)else: print "Window was already open." There are of course many ways of achieving this, but the general idea remains the same even in PySide-land and even standalone apps. Hope it helps. Best, Marcus -- You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAfY-V%2BLsfNUFai91Mu8ktaFuf8H-Xsa1FaAahowjrnfw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
