我正在尝试获取Maya的日志(它在脚本编辑器的输出窗口中打印的内容(,并将其添加到我的自定义PySide Qwidget中。为什么会导致Maya 崩溃
QtWidgets, QtCore, QtGui
imoprt QtDesiner_UI
import maya.OpenMayaUI as OmUi
from shiboken2 import wrapInstance
import maya.OpenMaya as Om
def get_maya_window():
# Get Maya window
maya_main_window_ptr = OMUI.MQtUtil.mainWindow()
maya_main_window = wrapInstance(long(maya_main_window_ptr),
QtWidgets.QMainWindow)
return maya_main_window
class UiClass(QtWidgets.QMainWindow, QtDesiner_UI.MainWindow):
def __init__(self):
super(UiClass, self).__init__(get_maya_window())
OM.MCommandMessage.addCommandOutputFilterCallback(self.callback)
# some PySide Widgets here
self.console_widget = QtWidgets.QTextEdit()
self.main_form_layout.addRow(self.console_widget)
def callback(self, msg, mg, *args):
self.console_widget.append(msg)
def logic_stuff(self):
#maya commands here
import UI_Window
w = UI_Window.UiClass()
w.show()
这不是
def callback(msg, mg ,*args):
console_widget.append(var)
OM.MCommandMessage.addCommandOutputFilterCallback(callback)
以下是部分工作的工作代码。它应该将日志信息附加到"QtextEdit(("中,但在第一次操作后Maya会崩溃(例如创建球体(。
import maya.OpenMayaUI as OmUi
from shiboken2 import wrapInstance
from PySide2 import QtWidgets, QtCore, QtGui
import maya.OpenMaya as Om
def get_maya_window():
# Get Maya window
maya_main_window_ptr = OmUi.MQtUtil.mainWindow()
maya_main_window = wrapInstance(long(maya_main_window_ptr), QtWidgets.QMainWindow)
return maya_main_window
class MainWidget(QtWidgets.QMainWindow):
def __init__(self):
super(MainWidget, self).__init__(get_maya_window())
self.cent_ly = MW()
self.text_edit = QtWidgets.QTextEdit()
self.cent_ly.vlayout.addWidget(self.text_edit )
self.setCentralWidget(self.cent_ly )
self.callback_id = Om.MCommandMessage.addCommandOutputFilterCallback(self.callback)
def closeEvent(self, event):
Om.MMessage.removeCallback(self.callback_id)
def callback(self, message):
self.text_edit.append(message.strip())
class MW(QtWidgets.QWidget):
def __init__(self):
super(MW , self).__init__()
self.vlayout = QtWidgets.QVBoxLayout()
self.setLayout(self.vlayout)
w = MainWidget()
w.show()
这一个在不破坏Maya 的情况下工作
import maya.OpenMayaUI as OmUi
from shiboken2 import wrapInstance
from PySide2 import QtWidgets, QtCore, QtGui
import maya.OpenMaya as Om
def get_maya_window():
# Get Maya window
maya_main_window_ptr = OmUi.MQtUtil.mainWindow()
maya_main_window = wrapInstance(long(maya_main_window_ptr), QtWidgets.QMainWindow)
return maya_main_window
class MainWidget(QtWidgets.QMainWindow):
def __init__(self):
super(MainWidget, self).__init__(get_maya_window())
self.cent_ly = MW()
self.text_edit = QtWidgets.QTextEdit()
self.cent_ly.vlayout.addWidget(self.text_edit)
self.setCentralWidget(self.cent_ly )
class MW(QtWidgets.QWidget):
def __init__(self):
super(MW , self).__init__()
self.vlayout = QtWidgets.QVBoxLayout()
self.setLayout(self.vlayout)
w = MainWidget()
w.show()
def callback(message, *args):
w.text_edit.append(message.strip())
callback_id = Om.MCommandMessage.addCommandOutputFilterCallback(callback)
# Om.MMessage.removeCallback(callback_id)
关闭窗口时需要删除回调。我在测试你的代码,第一次它就工作了,但随后的几次冻结了Maya。
class UiClass(QtWidgets.QMainWindow):
def __init__(self):
super(UiClass, self).__init__(get_maya_window())
self.callback_id = Om.MCommandMessage.addCommandOutputFilterCallback(self.callback)
def closeEvent(self, event):
Om.MMessage.removeCallback(self.callback_id)
正如@ababak所说,崩溃的发生是因为物体被摧毁了,当你关上窗户时就会发生这种情况。因此,在此之前,您需要删除回调。
您还没有显示第一个示例的完整代码。
当回调被启动但原始对象已经被销毁时,就会发生这些崩溃。
我假设您创建了UiClass
对象,但后来它被垃圾回收了。第二个示例在全局范围内定义回调函数时不会崩溃,并且在退出Maya之前保持有效。