PyQt - how to check if UI is already running?

4,811 views
Skip to first unread message

Panupat Chongstitwattana

unread,
Jan 9, 2012, 2:54:58 AM1/9/12
to python_in...@googlegroups.com
In Maya 2010, if I run the script to start a UI that is already running, it would crash Maya D: How can I check if the UI is already running and close it?

Kamil Hepner

unread,
Jan 9, 2012, 5:31:38 AM1/9/12
to python_in...@googlegroups.com
It's very simple:


winName = "myWindow"
if pm.windows.window(winName, exists=True):
       pm.windows.deleteUI(winName)


2012/1/9 Panupat Chongstitwattana <panu...@gmail.com>
In Maya 2010, if I run the script to start a UI that is already running, it would crash Maya D: How can I check if the UI is already running and close it?

Panupat Chongstitwattana

unread,
Jan 9, 2012, 6:56:44 AM1/9/12
to python_in...@googlegroups.com
Hi Kamil.

Thanks for suggestion. But the cmds deleteUI/window modules can't seem to detect the PyQt window's titles :(

Ricardo Viana

unread,
Jan 9, 2012, 7:00:37 AM1/9/12
to python_in...@googlegroups.com
Humm. It should. Check you have your naming right on the .ui for window widget.

Best regards
Ricardo Viana

Kurian O.S

unread,
Jan 9, 2012, 7:04:27 AM1/9/12
to python_in...@googlegroups.com
you can use 

class MyApp (QApplication):
     ....

if __name__ == '__main__':
    app = MyApp( sys.argv)
    if app.isRunning():
        ... do whatevr u want

--:: Kurian ::--

Panupat Chongstitwattana

unread,
Jan 9, 2012, 7:31:36 AM1/9/12
to python_in...@googlegroups.com
Kamil

The UI class is Ui_AddPlayblast.py. The set title line looks like this

AddPlayblast.setWindowTitle(QtGui.QApplication.translate("AddPlayblast", "Manual Add Playblast", None, QtGui.QApplication.UnicodeUTF8))

I tried using both "AddPlayblast" and "Manual Add Playblast" to no avail. Is there anything else I should try?


Kurian - the UI script will be run exclusively in Maya. As I understand, the __name__ = __main__ only works if you run the py file directly? Initally I'm launching the UI with these commands

app = QtGui.QApplication(sys.argv)
myapp = AddPlayblast()
myapp.show()
sys.exit(app.exec_())

I tested it out in another function, and it is this line that freezes Maya

myapp = AddPlayblast()


best regard,
Panupat C.

David Moulder

unread,
Jan 9, 2012, 7:57:52 AM1/9/12
to python_in...@googlegroups.com
You are using pumpThread right?

If so you shouldn't do sys.exit(app.exec_())

just myapp.show()

If your not using pumpThread you have to in Maya 2010.

You can find pumpThread in the sdk folder.  From memory you need to import it and initialize it before any Qt Gui is created.

import pumpThread as pt
pt.initializePumpThread()

-Dave

Panupat Chongstitwattana

unread,
Jan 9, 2012, 9:01:24 AM1/9/12
to python_in...@googlegroups.com
David - does pumpThread come with Maya 2010?

I'll try it out once I get to my studio tomorrow, thanks :)

Kurian O.S

unread,
Jan 9, 2012, 9:07:05 AM1/9/12
to python_in...@googlegroups.com
O otherwise you can try using this 

import sip
import maya.OpenMayaUI as mui
from PyQt4.QtCore import *
from PyQt4.QtQtGui import *

def getMayaWindow():
    ptr = mui.MQtUtil.mainWindow()
    return sip.wrapinstance(long(ptr), QObject)

class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.setObjectName('mainUI')
        self.mainLayout = QVBoxLayout(self)
        self.myButton = QPushButton('myButton')
        self.mainLayout.addWidget(self.myButton)

global app
global form
app = qApp
form = Form(getMayaWindow())
form.show()

PS :  PyQt4.QtCore import * is not really a good idea at all.

Panupat Chongstitwattana

unread,
Jan 9, 2012, 9:23:18 AM1/9/12
to python_in...@googlegroups.com
David - My Maya 2010 on mac doesn't seem to have pumpThread. Is it a window module? I'll test it out at my office, they're all on Windows.

Kurian - What happens when you pass the getMayaWindow() into QDialog? It wraps itself inside Maya's UI?

Justin Israel

unread,
Jan 9, 2012, 11:14:52 AM1/9/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
Kurian, he is using maya 2010 before they rewrote it in qt. so I don't believe MQtUtil even exists. The pumpThread utility is what he needs. It creates a fakey event loop that keeps processing events from qt as they stack up and also takes care of the global qapp that will be shared. There is actually no global qapp being used by maya. 


Justin Israel

unread,
Jan 9, 2012, 11:19:06 AM1/9/12
to python_in...@googlegroups.com
It should be in the sdk directory and not in your path just yet. But if you cant find it:

Kurian O.S

unread,
Jan 9, 2012, 11:36:39 AM1/9/12
to python_in...@googlegroups.com
Aha I didnt see that and it wont work previous version and he should use pumbthread in that case ..

Panupat Chongstitwattana

unread,
Jan 10, 2012, 5:03:59 AM1/10/12
to python_in...@googlegroups.com
Justin, thank you for the link.

I'm a little curious, how do I properly use the pumpThread? With pumpThread, my UI would disappear as soon as it appeared. Am I putting the pumpThread at the right place? 


import pumpThread
from PyQt4 import uic

class MySimpleUI(object):
    def __init__(self):
        pumpThread.initializePumpThread()
        self.ui = uic.loadUi('D:\\ui\\hello.ui')
        
def mySimpleUI():
    myUI = MySimpleUI()
    myUI.ui.show()


hello.ui is only a widget with a text.

David Moulder

unread,
Jan 10, 2012, 5:23:54 AM1/10/12
to python_in...@googlegroups.com
This is now down to pythons garbage collection destroying the ui.

If you make a global variable and assign you ui to that then you'll be fine.

ie:

global myUI
myUI = None

def mySimpleUI():
    myUI = MySimpleUI()
    myUI.ui.show()

-Dave

Panupat Chongstitwattana

unread,
Jan 10, 2012, 5:34:31 AM1/10/12
to python_in...@googlegroups.com
Thank you Dave.

It's still acting weird I tried putting "global myUI"  at different places, the UI only shows up if it's inside the function mySimple UI. But when calling the fuction again it would give me this error whether I've closed the first UI or not.

# Error: TypeError: 'MySimpleUI' object is not callable # 

Putting myUI = None around the code doesn't fix it.

Everything works fine tho if I remove the function and just have these 2 lines by themselves. 

myUI = MySimpleUI()
myUI.ui.show()

anyway around this? :O Sorry for asking so many question.

best regard,
Panupat C.

David Moulder

unread,
Jan 10, 2012, 7:04:40 AM1/10/12
to python_in...@googlegroups.com

This should be enough to get you going.

Panupat Chongstitwattana

unread,
Jan 10, 2012, 10:53:25 PM1/10/12
to python_in...@googlegroups.com
Thanks David. I think I got it working for now.

If I want to launch multiple UI, they all will need to initialize pumpthread?

David Moulder

unread,
Jan 11, 2012, 4:40:46 AM1/11/12
to python_in...@googlegroups.com
If you read the pumpthread code you'll see that you can only initialize it once I believe.  So calling it multiple times won't make any difference and just ensures it's running.

Good luck.  QT is great.
Reply all
Reply to author
Forward
0 new messages