How to check if window exists in Maya 2023?

Hello! I have had trouble getting my scripts to not give an error the first time they run in any maya version using python3. The second time it creates the window fine, I am not sure what should be different as it doesn’t look like a python 2 vs 3 thing to me. It works fine in python 2 Maya versions.

I’ll init a class with this:

class Window_UI:
    def __init__(self):
        if pm.window('myWindow', exists=True):
            pm.deleteUI('myWindow')
        self.window_id = 'myWindow'

The first time always gives the error:
Error: RuntimeError: file C:\Program Files\Autodesk\Maya2023\Python\lib\site-packages\pymel\internal\pmcmds.py line 217: Object ‘myWindow’ not found.

Then it works from that point on. What’s wrong with my code?
Thanks!

Just don’t use PyMel :slight_smile:
Especially in situations in which PyMel (compared to Python CMDS wrappers) does not provide any execution speed benefits, but instead executes commands more slowly and with additional overhead…

import maya.cmds as cmds

class Window_UI:
    def __init__(self):
        win_name = 'myWindow'
        if cmds.window(win_name, exists=True):
            cmds.deleteUI(win_name, window=True)
        self.window_id = win_name

Window_UI().window_id
1 Like

Point taken! It works now using cmds, thanks very much!