Search code examples
mel

How do I use the -exists flag from "window" command?


The maya manual does not describe how -exists flag in window command is used. I tried many ways of using it and it does not budge.

I've been fiddling and googling for 2 days, it wasn't going anywhere. I was only trying to detect if one of my window is opened or not.

Here's the test code I've got so far:

string $something = `window -title "name of the window" -widthHeight 200 150`;

columnLayout -adjustableColumn false;

button -label "detect this window" -command "dothis_1";



showWindow $something;



proc dothis_1()

{

   if (`window -ex $something` == true)

   {

      print "window detected!\n";

   }

   else

   {

     print "window detection failed!\n";

    }

}

//--------

So...I assumed I did something wrong somewhere or I simply misunderstood what -exists does? What did I do wrong and how do I detect whether my window is opened or not?


Solution

  • Your procedure has a variable scope issue, where it doesn't know what $something is, because it's defined outside of it.

    You could make your procedure accept an argument for a window name you want to check against, create your window, then pass its name to the button's command:

    string $something = `window -title "name of the window" -widthHeight 200 150`;
    columnLayout -adjustableColumn false;
    button -label "detect this window" -command ("dothis_1 " + $something); // Pass window name to proc.
    showWindow $something;
    
    
    proc dothis_1(string $win) {
       if (`window -ex $win` == true) {
          print "window detected!\n";
       } else {
         print "window detection failed!\n";
       }
    }
    

    Alternatively you should be able to create a global variable so that it's accessible within the procedure too.

    Although your use case is a bit weird. You can only click the button if the window exists!