为什么我的 QThreads 总是使 Maya 崩溃?

Why are my QThreads consistently crashing Maya?

我有一个 UI,我想在 Maya 内部使用线程。这样做的原因是我可以 运行 Maya.cmds 没有 hanging/freezing UI 而更新 UI 进度条等

我已经从 Whosebug 中阅读了一些示例,但我的代码每隔一秒就会崩溃 运行。我遵循的示例是 here and

import maya.cmds as cmds
from PySide2 import QtWidgets, QtCore, QtGui, QtUiTools
import mainWindow #Main window just grabs the Maya main window and returns the object to use as parent.

class Tool(QtWidgets.QMainWindow):
    def __init__(self, parent=mainWindow.getMayaMainWindow()):
        super(Tool, self).__init__(parent)

        UI = "pathToUI/UI.ui"
        loader = QtUiTools.QUiLoader()
        ui_file = QtCore.QFile(UI)
        ui_file.open(QtCore.QFile.ReadOnly)
        self.ui = loader.load(ui_file, self)

        #Scans all window objects and if one is open with the same name as this tool then close it so we don't have two open.
        mainWindow.closeUI("Tool")      

        ###HERE'S WHERE THE THREADING STARTS###
        #Create a thread
        thread = QtCore.QThread()
        #Create worker object
        self.worker = Worker()
        #Move worker object into thread (This creates an automatic queue if multiples of the same worker are called)
        self.worker.moveToThread(thread)

        #Connect buttons in the UI to trigger a method inside the worker which should run in a thread
        self.ui.first_btn.clicked.connect(self.worker.do_something)
        self.ui.second_btn.clicked.connect(self.worker.do_something_else)
        self.ui.third_btn.clicked.connect(self.worker.and_so_fourth)

        #Start the thread
        thread.start()

        #Show UI
        self.ui.show()

class Worker(QtCore.QObject):
    def __init__(self):
        super(Worker, self).__init__() #This will immediately crash Maya on tool launch
        #super(Worker).__init__() #This works to open the window but still gets an error '# TypeError: super() takes at least 1 argument (0 given)'

    def do_something(self):
        #Start long code here and update progress bar as needed in a still active UI.
        myTool.ui.progressBar.setValue(0)
        print "doing something!"
        myTool.ui.progressBar.setValue(100)

    def do_something_else(self):
        #Start long code here and update progress bar as needed in a still active UI.
        myTool.ui.progressBar.setValue(0)
        print "doing something else!"
        myTool.ui.progressBar.setValue(100)

    def and_so_fourth(self):
        #Start long code here and update progress bar as needed in a still active UI.
        myTool.ui.progressBar.setValue(0)
        print "and so fourth, all in the new thread in a queue of which method was called first!"
        myTool.ui.progressBar.setValue(100)

#A Button inside Maya will import this code and run the 'launch' function to setup the tool
def launch():
    global myTool
    myTool = Tool()

我希望 UI 保持活动状态(未锁定)并且线程 运行ning Maya cmd 而不会在更新 UI 进度时完全崩溃 Maya酒吧。

任何对此的见解都将是惊人的!

据我所见,它有以下错误:

  • thread 是一个局部变量,当构造函数执行完毕时被删除,导致执行的内容在主线程中完成,这是不需要的,解决方案是扩展生命周期,为此有几种解决方案:1)制作class属性,2)将父级传递给由父级管理的生命周期。在这种情况下使用第二种解决方案。

  • 你不应该从另一个线程修改 GUI,在你的情况下你已经从另一个线程修改了 progressBar,在 Qt 中你必须使用信号。

  • 您必须在另一个线程中执行的方法中使用@Slot 装饰器。

  • 你表示你想修改myTool但是你还没有声明它,所以global myTool不会通过使myTool成为一个局部变量来工作删除。解决方案是声明 myTool:myTool = None 综合以上,解决方案为:

import maya.cmds as cmds
from PySide2 import QtWidgets, QtCore, QtGui, QtUiTools
import mainWindow  # Main window just grabs the Maya main window and returns the object to use as parent.


class Tool(QtWidgets.QMainWindow):
    def __init__(self, parent=mainWindow.getMayaMainWindow()):
        super(Tool, self).__init__(parent)

        UI = "pathToUI/UI.ui"
        loader = QtUiTools.QUiLoader()
        ui_file = QtCore.QFile(UI)
        ui_file.open(QtCore.QFile.ReadOnly)
        self.ui = loader.load(ui_file, self)

        # Scans all window objects and if one is open with the same name as this tool then close it so we don't have two open.
        mainWindow.closeUI("Tool")

        # Create a thread
        thread = QtCore.QThread(self)
        # Create worker object
        self.worker = Worker()
        # Move worker object into thread (This creates an automatic queue if multiples of the same worker are called)
        self.worker.moveToThread(thread)

        # Connect buttons in the UI to trigger a method inside the worker which should run in a thread
        self.ui.first_btn.clicked.connect(self.worker.do_something)
        self.ui.second_btn.clicked.connect(self.worker.do_something_else)
        self.ui.third_btn.clicked.connect(self.worker.and_so_fourth)

        self.worker.valueChanged.connect(self.ui.progressBar.setValue)

        # Start the thread
        thread.start()

        # Show UI
        self.ui.show()


class Worker(QtCore.QObject):
    valueChanged = QtCore.Signal(int)

    @QtCore.Slot()
    def do_something(self):
        # Start long code here and update progress bar as needed in a still active UI.
        self.valueChanged.emit(0)
        print "doing something!"
        self.valueChanged.emit(100)

    @QtCore.Slot()
    def do_something_else(self):
        # Start long code here and update progress bar as needed in a still active UI.
        self.valueChanged.emit(0)
        print "doing something else!"
        self.valueChanged.emit(100)

    @QtCore.Slot()
    def and_so_fourth(self):
        # Start long code here and update progress bar as needed in a still active UI.
        self.valueChanged.emit(0)
        print "and so fourth, all in the new thread in a queue of which method was called first!"
        self.valueChanged.emit(100)

myTool = None

def launch():
    global myTool
    myTool = Tool()