Unable to check that custom window exists

668 views
Skip to first unread message

likage

unread,
Jul 22, 2014, 5:42:00 AM7/22/14
to python_in...@googlegroups.com
Hi all, I am still pretty new to scripting and I have run into some problems of this simple ui I have wrote.
It is as follows:

def ui():
    main_win
= cmds.window(title = "TEST", width = 600, height = 600)
    tab_win
= cmds.tabLayout()
   
   
# Tab Layout for Outliner
    out_layout
= cmds.formLayout("Outliner")
    out_panel
= cmds.outlinerPanel()
    cmds
.formLayout(out_layout, edit = True,
                    attachForm
=[
                       
(out_panel, "top", 0),
                       
(out_panel, "left", 0),
                       
(out_panel, "bottom", 0),
                       
(out_panel, "right", 0)
                   
])

def main():
   
if cmds.window("TEST", exists = True):
        cmds
.deleteUI("TEST")
       
print "The application is already opened!"
   
else:
        ui
()

However, the main function does not seems to be working as I am still able to open up more than one of the window in which the first part of the if-else function does not seems to be 'recognizing' it.

Any ideas?

Justin Israel

unread,
Jul 22, 2014, 7:35:14 AM7/22/14
to python_in...@googlegroups.com
Hi,

I think you confused setting the value of the window title with giving it an object name. You can see this if you print the value of main_win.
What you want to do is give it an explicit object name, and then check for that object name when wanting to delete or reshow it:
WINDOW_NAME = "TEST"

def ui():
    main_win = cmds.window(WINDOW_NAME, title = "TEST", width = 600, height = 600)
    ...

def main():
    if cmds.window(WINDOW_NAME, exists = True):
        cmds.deleteUI(WINDOW_NAME)
        
print "The application is already opened!"
    else:
        ui()
If you happen to call ui() multiple times though, in this example, it will end up creating new windows with incremented names. Also, if someone else in another app happened to use that same name and a window already existed, your check would fail to match the right window. What you would have to do is save main_win to a persistent state (either a class or a global) and then check for that.



--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/0a89241a-49ab-4e91-93ba-1a85c7006efd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

likage

unread,
Jul 22, 2014, 10:23:59 PM7/22/14
to python_in...@googlegroups.com
Hi Justin,

Thanks for the info, I am unable to figure out why it is not working as I am seeing some similar examples.

However I am having trouble understanding a part of the code you have written, could you kindly explain it to me?
Here's the part:

main_win = cmds.window(WINDOW_NAME, title = "TEST", width = 600, height = 600)
What I do not understand is why would you place WINDOW_NAME in the cmds.window command, like what is its role in it?
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Jul 22, 2014, 10:46:03 PM7/22/14
to python_in...@googlegroups.com
Have you taken a look at the documentation for the cmds module?

The optional positional argument is the name you want to give to the window object. Otherwise it will pick a random one for you. The return value is the name of the window object that was actually used, where if it had collided with an existing window, you would get an incremented version of the name. 

So when you say:   cmds.window("myWindow")
You are saying you want to name the window object "myWindow". This is different from the title that you want to give to the window, such as "The Title of My Window"

There are usually two ways people approach creating windows for their apps. They either don't care if multiple instances of the window can be launched, in which case it doesn't matter all that much what the name of the window object is. Or to prevent multiple instances of a window, by using an expected window name, and trying to delete a previous instance if it exists. 




To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/332e44de-2974-439e-8887-f2bb539835ee%40googlegroups.com.

likage

unread,
Jul 23, 2014, 12:30:03 AM7/23/14
to python_in...@googlegroups.com
Yes I did take a look but honestly speaking, I focus more on the flags' variables and hence I was a bit swamped when you wrote your code.

It was my bad nevertheless, and I will take a note from now on.
Thank you.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.

--
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 python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages