Check if window exists

Check if window exists

loc3055
Explorer Explorer
2,952 Views
2 Replies
Message 1 of 3

Check if window exists

loc3055
Explorer
Explorer

 

 

It's a shame that such questions have to be asked, but sometimes no choice is left when the so-called tutorials are a bunch of nonsense.... The code they make is useless, because as soon as you restart the program you will get errors "# Error: name 'window1' is not defined", clearly it can't be defined since we are looking if it exists at the first place, so the so called tutors first run the script without check if there's no duplicate, and then they will check it, but as soon as you restart the program everything has to fail.

 

import maya.cmds as mc

if mc.window(window1,exists=True):
mc.deleteUI(window1)

 

If i add this code, then there are errors at on startup, if i don't add it, then i have copies everywhere of the same window, what to do then? There's nothing useful on this situation, people make tutorials which are brainless, idk what is the point to first run the script manually to create the windows that then we want to delete, but not even a word that the same code if run on startup will not work, since again, the first try has to be run without check....

Maya doesn't help as well, the errors sometimes are misleading, for example i got an error that was complaining that the name isn't defined, if i define the name then i got another error, turned out that i just had edit flag on, and it was causing all that issue, but most of the time was lost on looking up non-existing issues.

 

At this point i was left with no other choice, but to ask how to professional check if a window exists, because wasting 2h on such trivial thing is at least angering.

0 Likes
Accepted solutions (2)
2,953 Views
2 Replies
Replies (2)
Message 2 of 3

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

You are slightly misunderstanding the error message here. window1 in the error message is referring to the variable that defines the name of the window, not the window itself. You need to define the variable window1 as a string to be able to look up if a window with that has the same name as the string exists. Something like this:

import maya.cmds as mc

window1 = "WhatYouWantToCallYourWindow"

if mc.window(window1,exists=True):
    mc.deleteUI(window1)

mc.window(window1)
mc.showWindow(window1)

 

I hope it helps!

0 Likes
Message 3 of 3

loc3055
Explorer
Explorer
Accepted solution

I figured it out alone eventually, the movies i saw just twisted so much a simple thing, i tought that the function demanded a pointer to an object, it turned out that it was a simple string, and a constant could be used rather than a variable...

0 Likes