为什么我的Python Maya代码在第二次执行时崩溃了

问题描述 投票:0回答:1

我正在尝试为我的实用程序创建一个简单的工具包,以加快我在 Maya 中的工作流程。

第二次执行代码时,它总是崩溃,并且我无法找到错误的原因。

从我的角度来看,这是持有 Qt 小部件并造成崩溃的错误,但我无法解决它。

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()
python qt maya
1个回答
0
投票

对于任何搜索类似错误的人,我发现在 qt connect 中使用 lambda 发送“变量”数据似乎会影响 Maya 并导致错误,经过部分测试但有相同的错误。

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"))
© www.soinside.com 2019 - 2024. All rights reserved.