我在3Dsmax 2015上做了一些简单的PySide。
这是我的错误:
python.ExecuteFile "C:\Program Files\Autodesk\3ds Max 2015\scripts\Python\demoUniTest.py"
-- Runtime error: Line 32 <module>()
<type 'exceptions.RuntimeError'> A QApplication instance already exists.
这是我的代码:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from math import *
class Form(QDialog):
def __init__(self,parent=None):
super(Form,self).__init__(parent)
self.browser = QTextBrowser()
self.lineedit = QLineEdit("Type an expression and press Enter")
self.lineedit.selectAll()
layout = QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.lineedit)
self.setLayout(layout)
self.lineedit.setFocus()
self.connect(self.lineedit, SIGNAL("returnPressed()"),self.updateUi)
self.setWindowTitle("Calculate")
def updateUi(self):
try:
text = self.lineedit.text()
self.browser.append("%s = <b>%s</b>" % (text,eval(text)))
except:
self.browser.append("<font color=red>%s is invalid</font>" %text)
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
当我在Pycharm上使用此代码时,没有任何错误。它仅在我在3ds Max 2015 Listener上使用时出现
直接引用helpfile(Using PySide):
通常,使用QtGui.QApplication()在脚本中创建PySide应用程序对象。但是,在3ds Max中,已经有一个PySide应用程序正在运行,因此您可以获得该对象的句柄,如下所示:
QtGui.QApplication.instance()
作为注释,这在3DS Max 2018和PySide2中有所改变。我现在正在自己玩它,我可以在经过一些修补之后让它工作。这里是文档的链接,但要注意代码中有一个小错字(至少在撰写本文时):http://help.autodesk.com/view/3DSMAX/2018/ENU/?guid=__developer_what_s_new_in_3ds_max_python_api_what_s_new_in_the_3ds_max_2018_p_html
如其他答案所述,您需要使您的UI成为主3DS Max应用程序的子项。好消息是他们使用GetQMaxMainWindow()
函数为您简化了一点。像这样使用它:
from PySide2 import QtWidgets, QtCore, QtGui
import MaxPlus
import os
class SampleUI(QtWidgets.QDialog):
def __init__(self, parent=MaxPlus.GetQMaxMainWindow()):
super(SampleUI, self).__init__(parent)
self.initUI()
def initUI(self):
mainLayout = QtWidgets.QHBoxLayout()
testBtn = QtWidgets.QPushButton("Test!")
mainLayout.addWidget(testBtn)
self.setLayout(mainLayout)
if __name__ == "__main__":
try:
ui.close()
except:
pass
ui = SampleUI()
ui.show()
您正在该行中创建QApplication的实例:
app = QApplication(sys.argv)
并且因为在此之前的某个地方创建了另一个QApplication实例(大概是在“3Dsmax 2015 Listener”中的某个地方)并且你只允许一个。
看到: