Why my python Maya code crashes the second time execute it

First of all thanks for reading this and trying to help me with my code. Im quite new in the python Qt wordld. Im trying to create a simple toolkit for my utilities for speeding up my workflow in maya. I saw that a the standart in the industry is to use QT so i started my journey.

Always the second time I execute my code it always crash and I’m not able to be aware of the error.

From my pov its and error of holding the Qt widgets and creating a crash, but i’m not able to solve it.

try:
    # Qt5
    from PySide2 import QtCore, QtWidgets, QtGui
    from shiboken2 import wrapInstance
    from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
except ImportError:
    # Qt6
    from PySide6 import QtCore, QtWidgets
    from shiboken6 import wrapInstance

import sys
import maya.OpenMayaUI as omui
import maya.cmds as mc
import maya.mel as mel


def mayaMainWindow():
    mainWindowPointer = omui.MQtUtil.mainWindow()
    return wrapInstance(int(mainWindowPointer), QtWidgets.QWidget)

#Start the class for the tab Color Management
class ColorManagement(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.createWidgets()
        self.createLayout()
        self.createConnections()


    #Function for creating the widgets       
    def createWidgets(self):

        #Create Widgets for setting up the Aces
        self.colorButton1 = QtWidgets.QPushButton()
        self.colorButton1.setMaximumWidth(4)
        self.colorButton2 = QtWidgets.QPushButton()
        self.colorButton2.setStyleSheet("background-color: #fc0303")
        self.colorButton2.setMaximumWidth(4)
        self.colorButton3 = QtWidgets.QPushButton()
        self.colorButton3.setStyleSheet("background-color: #fc0303")
        self.colorButton3.setMaximumWidth(4)
        self.colorButton4 = QtWidgets.QPushButton()
        self.colorButton4.setStyleSheet("background-color: #067d0a")
        self.colorButton4.setMaximumWidth(4)
        self.colorButton5 = QtWidgets.QPushButton()
        self.colorButton5.setStyleSheet("background-color: #067d0a")
        self.colorButton5.setMaximumWidth(4)

        self.acesLabel = QtWidgets.QLabel("Setup ACES 1.2")
        self.acesLabel.setStyleSheet("color: white")

        self.filePathLenght = QtWidgets.QLineEdit()
        self.filePathLenght.setText("D:/aces_1.2/config.ocio")

        
        self.selectFilePathButton = QtWidgets.QPushButton()
        self.selectFilePathButton.setIcon(QtGui.QIcon(":fileOpen.png"))

        self.infoText01 = QtWidgets.QLabel("1. Set the path to config.ocio")
        self.infoText02 = QtWidgets.QLabel("2. Load after selecting the config.ocio")
        self.infoText01.setStyleSheet("font-weight: bold")
        self.infoText02.setStyleSheet("font-weight: bold")

        self.loadButton = QtWidgets.QPushButton("LOAD")
        self.loadButton.setStyleSheet("font-weight: bold; color: white")


        #Create widgets for setting up the convert color spaces
        self.fileButtonImage = QtWidgets.QPushButton()
        self.fileButtonImage.setIcon(QtGui.QIcon(":/file.svg"))
        self.fileButtonImage.setStyleSheet("background-color: transparent")
        self.fileButtonImage.setMaximumSize(15, 15)
        self.fileButtonImage2 = QtWidgets.QPushButton()
        self.fileButtonImage2.setIcon(QtGui.QIcon(":/file.svg"))
        self.fileButtonImage2.setStyleSheet("background-color: transparent")
        self.fileButtonImage2.setMaximumSize(15, 15)
        self.allFileLable = QtWidgets.QLabel("Convert ALL files to:")
        self.allFileLable.setStyleSheet("color: white; font-weight: bold")
        self.selectedFilesLable = QtWidgets.QLabel("Convert SELECTED files to:")
        self.selectedFilesLable.setStyleSheet("color: white; font-weight: bold")

        self.spaceSeparator = QtWidgets.QSpacerItem(20, 10)

        self.allToAces = QtWidgets.QPushButton("ALL to ACEScg")
        self.allToRaw = QtWidgets.QPushButton("ALL to Utilty-Raw")

        self.selectedToAces = QtWidgets.QPushButton("SELECTED to ACEScg")
        self.selectedToRaw = QtWidgets.QPushButton("SELECTED to Utilty-Raw")

        
        

        
        
    #Function for creating the layout
    def createLayout(self):
        nameLayout = QtWidgets.QHBoxLayout()
        nameLayout.addWidget(self.colorButton1)
        nameLayout.addWidget(self.acesLabel)
        nameLayout.addStretch()

        filePathLayout = QtWidgets.QHBoxLayout()
        filePathLayout.addWidget(self.filePathLenght)
        filePathLayout.addWidget(self.selectFilePathButton)
                
        formLayout = QtWidgets.QFormLayout()
        formLayout.addRow("File Path:", filePathLayout)
        
        
        infoLayout = QtWidgets.QVBoxLayout()
        infoLayout.addWidget(self.infoText01)
        infoLayout.addWidget(self.infoText02)

        infoAndButtonLayout = QtWidgets.QHBoxLayout()
        infoAndButtonLayout.addLayout(infoLayout)
        infoAndButtonLayout.addWidget(self.loadButton)

        allFilesTo = QtWidgets.QHBoxLayout()
        allFilesTo.addWidget(self.fileButtonImage)
        allFilesTo.addWidget(self.allFileLable)

        buttonAllFiles = QtWidgets.QHBoxLayout()
        buttonAllFiles.addWidget(self.colorButton2)
        buttonAllFiles.addWidget(self.allToAces)
        buttonAllFiles.addWidget(self.colorButton3)
        buttonAllFiles.addWidget(self.allToRaw)

        selectedFilesTo = QtWidgets.QHBoxLayout()
        selectedFilesTo.addWidget(self.fileButtonImage2)
        selectedFilesTo.addWidget(self.selectedFilesLable)

        buttonSelectedFiles = QtWidgets.QHBoxLayout()
        buttonSelectedFiles.addWidget(self.colorButton4)
        buttonSelectedFiles.addWidget(self.selectedToAces)
        buttonSelectedFiles.addWidget(self.colorButton5)
        buttonSelectedFiles.addWidget(self.selectedToRaw)



        #FINAL LAYOUT     
        mainLayout = QtWidgets.QVBoxLayout(self)
        mainLayout.addLayout(nameLayout)
        mainLayout.addLayout(formLayout)
        mainLayout.addLayout(infoAndButtonLayout)
        mainLayout.addItem(self.spaceSeparator)
        mainLayout.addLayout(allFilesTo)
        mainLayout.addLayout(buttonAllFiles)
        mainLayout.addItem(self.spaceSeparator)
        mainLayout.addLayout(selectedFilesTo)
        mainLayout.addLayout(buttonSelectedFiles)
        mainLayout.addStretch()
        
        
        
    #Function for the connections
    def createConnections(self):
        self.selectFilePathButton.clicked.connect(self.showFileSelectDialog)
        self.loadButton.pressed.connect(self.loadAces)
        self.allToAces.pressed.connect(lambda: self.colorSpaceImages("all", "acesCg"))
        self.allToRaw.pressed.connect(lambda: self.colorSpaceImages("all", "uRaw"))
        self.selectedToAces.pressed.connect(lambda: self.colorSpaceImages("selected", "acesCg"))
        self.selectedToRaw.pressed.connect(lambda: self.colorSpaceImages("selected", "uRaw"))

    

    #Function for selecting the file                 
    def showFileSelectDialog(self):
        self.acesFilePath = mc.fileDialog2(caption= "Select the .ocio file", fileMode=1, fileFilter='OpenColorIO Config (*.ocio)')[0] #Open file path for searching the .ocio file
        if self.acesFilePath:
            self.filePathLenght.setText(self.acesFilePath) #In case the selected file exists change the path of the field

    #Load Aces when pressed Load button, in case no aces were seleccted always pick up the standart directory
    def loadAces(self):
        filePath = self.filePathLenght.text()
        mc.colorManagementPrefs(edit=True, cmEnabled=True)
        mc.colorManagementPrefs(edit=True, configFilePath=filePath)
        mc.colorManagementPrefs(edit=True, viewTransformName="P3-D60 (ACES)")
        mc.colorManagementFileRules('Default', edit=True, colorSpace='Utility - Raw')
        mel.eval('print ("ACES SET CORRECTLY")')


    def colorSpaceImages(self, selected, type):
        if selected == "all":
            files = mc.ls(type='file')        
        if selected == 'selected':
            files = mc.ls(selection=True)       
        for file in files:
            if type == 'acesCg':
                mc.setAttr("%s.colorSpace" % file, "ACES - ACEScg", type="string")
            if type == 'uRaw':
                mc.setAttr("%s.colorSpace" % file, "Utility - Raw", type="string")
        if type == 'acesCg':
            mel.eval('print ("COLOR SPACE CHANGED TO ACEScg")')        
        if type == 'uRaw':
            mel.eval('print ("COLOR SPACE CHANGED TO Utility")')              


class FastApproach(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.btn_01 = QtWidgets.QPushButton("Button 01")
        self.btn_02 = QtWidgets.QPushButton("Button 02")
        self.btn_03 = QtWidgets.QPushButton("Button 03")

        mainLayout = QtWidgets.QVBoxLayout(self)
        mainLayout.addWidget(self.btn_01)
        mainLayout.addWidget(self.btn_02)
        mainLayout.addWidget(self.btn_03)
        mainLayout.addStretch()


class RiggingModule(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.btn_01 = QtWidgets.QPushButton("Button 01")

        mainLayout = QtWidgets.QVBoxLayout(self)
        mainLayout.addWidget(self.btn_01)
        mainLayout.addStretch()


class MainWindow(MayaQWidgetDockableMixin, QtWidgets.QDialog):
    def __init__(self, parent=mayaMainWindow()):
        super(MainWindow, self).__init__(parent)

        self.setWindowTitle("ToolKit")
        self.setMinimumSize(400, 200)

        # On macOS make the window a Tool to keep it on top of Maya
        if sys.platform == "darwin":
            self.setWindowFlag(QtCore.Qt.Tool, True)

        self.createWidgets()
        self.createLayout()
        self.createConnections()

    def createWidgets(self):
        self.colorManagementWidget = ColorManagement()
        self.fastApproachWidget = FastApproach()
        self.riggingWidget = RiggingModule()

        self.tab_widget = QtWidgets.QTabWidget()
        self.tab_widget.addTab(self.colorManagementWidget, "Color Management")
        self.tab_widget.addTab(self.fastApproachWidget, "Fast Approach")
        self.tab_widget.addTab(self.riggingWidget, "Rigging")

        self.ok_btn = QtWidgets.QPushButton("OK")
        self.cancel_btn = QtWidgets.QPushButton("Cancel")

    def createLayout(self):
        btn_layout = QtWidgets.QHBoxLayout()
        btn_layout.addStretch()
        btn_layout.addWidget(self.ok_btn)
        btn_layout.addWidget(self.cancel_btn)

        mainLayout = QtWidgets.QVBoxLayout(self)
        mainLayout.addWidget(self.tab_widget)
        mainLayout.addLayout(btn_layout)

    def createConnections(self):
        self.cancel_btn.pressed.connect(self.close)

    def dockControls(self):
        if mc.workspaceControl("ToolKitGuidoWorkspaceControl", q=True, exists=True):
            mc.deleteUI("ToolKitGuidoWorkspaceControl", control=True)

        controlName = mc.workspaceControl("ToolKitGuidoWorkspaceControl", label="ToolKit Guido", tabToControl=["AttributeEditor", -1], retain=True, floating=False)

        controlWidget = omui.MQtUtil.findControl(controlName)
        controlWrap = wrapInstance(int(controlWidget), QtWidgets.QWidget)
        controlWrap.layout().addWidget(self)


if __name__ == "__main__":
    try:
        win.close()  # pylint: disable=E0601
        win.deleteLater()
    except:
        pass

    win = MainWindow()
    win.show(dockable=True)
    win.dockControls()

New contributor

Guido González is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật