PyQt和Maya,threading = False?

时间:2012-02-16 13:56:03

标签: python pyqt boolean maya mel

我一直在调查这个问题3天,没有任何运气。我对这一切都很陌生,所以也许有些东西我不知道。

问题适用于: Maya.cmds,PyMel并使用QThread评估MEL或仅使用线程

此代码旨在运行在Maya之后的“mayapy”python解释器上。我创建了一个简短的例子,它在多个实例中重新创建了相同的错误。

一个按钮有效,另一个没有。但他们使用相同的代码。

from PyQt4 import Qt

class doStuff( Qt.QThread ):
    taskProgress = Qt.pyqtSignal(int)

    # --------------------------------------------------------- #
    # Here things start to crash...
    def run( self ):

        # This works
        persp = mel.general.PyNode('persp')
        print persp.translateX.get()

        # This dont work
        poiLights = mel.general.ls( exactType="pointLight" ) 
        for light in poiLights:
            print light

        # This dont work
        geo = mel.general.PyNode('pPyramidShape1')
        print mel.modeling.polyEvaluate( geo, face=True )

        # Emit progress
        self.taskProgress.emit( 1 )

        return
    # END
    # --------------------------------------------------------- #

class ui( Qt.QWidget ):
    def __init__(self, parent=None):
        super(ui, self).__init__(parent)

        # Init QThread
        self.thread = doStuff()

        # Create Widgets
        buttonNo = Qt.QPushButton("Start - Dont work")
        buttonYes = Qt.QPushButton("Start - Works")

        # Setup Layout
        layout = Qt.QVBoxLayout()
        layout.addWidget( buttonYes )
        layout.addWidget( buttonNo )
        self.setLayout( layout )
        self.show()

        # --------------------------------
        # PROBLEM AREA: Button signals

        # This one dont work, but starts the thread correctly.
        self.connect( buttonNo, Qt.SIGNAL("clicked()"), self.thread.start )

        # This one works, but dont start the thread correctly.
        self.connect( buttonYes, Qt.SIGNAL("clicked()"), self.thread.run ) 

        # --------------------------------

        self.thread.taskProgress.connect( self.updateProgress )

        return

    # Feedback progress status
    def updateProgress( self, value ):
        print 'Current progress is:', value

        return

if __name__ == '__main__':

    import sys
    app = Qt.QApplication(sys.path)
    program = ui()

    # init maya
    import pymel.core as mel
    filePath = '/Users/ecker/Dropbox/Scripts/RibExporter/mayaScene3ani.ma'
    mel.openFile( filePath, f=True, o=True )

    sys.exit(app.exec_())

此代码创建2个按钮,按下时开始执行相同的功能。一个人执行thread.startthread.run

thread.start将使线程正常工作,能够将数据反馈到Qt接口(进度条),但大多数Maya代码将开始返回各种类型的错误,如下所示:

Traceback (most recent call last):
  File "/Users/ecker/Dropbox/Scripts/RibExporter/error_recreation2.py", line 22, in run
    poiLights = mel.general.ls( exactType="pointLight" ) 
  File "/Applications/Autodesk/maya2012/Maya.app/Contents/Frameworks/Python.framework/Versions/Current/lib/python2.6/site-packages/pymel/core/general.py", line 969, in ls
    res = _util.listForNone(cmds.ls(*args, **kwargs))
  File "/Applications/Autodesk/maya2012/Maya.app/Contents/Frameworks/Python.framework/Versions/Current/lib/python2.6/site-packages/pymel/internal/pmcmds.py", line 134, in wrappedCmd
    res = new_cmd(*new_args, **new_kwargs)
TypeError: Flag 'long' must be passed a boolean argument

这是一个布尔参数,无论我试图以什么格式和方式给出它,它总是会给出与此类似的错误。在需要布尔值的同一行res = new_cmd(*new_args, **new_kwargs)

我需要threadstart,而不仅仅是跑步。除非有不同的方法来进行线程化,否则需要解决方法吗?

1 个答案:

答案 0 :(得分:3)

Maya与线程不兼容。这里的关键是使用maya.utils.executeInMainThreadWithResult。

http://download.autodesk.com/us/maya/2010help/index.html?url=Python_Python_and_threading.htm,topicNumber=d0e182779

我希望这会有所帮助。