How to run OpenMaya.MCommandMessage without crashing Maya - python

I'm trying to get Maya's log(What it prints in script editor's output window) and add it to my custom PySide Qwidget.
Why this crashes 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()
And this doesn't
def callback(msg, mg ,*args):
console_widget.append(var)
OM.MCommandMessage.addCommandOutputFilterCallback(callback)
Here is the the working code that works partially. It should append log info into 'QtextEdit()' but after first operation Maya crashes(e.g. create sphere).
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()
And this one works without crashing 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)

You need to remove the callback when you close the window. I was testing your code and it worked the first time, but subsequent times froze 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)
Like #ababak said, the crashes happen because the object was destroyed, which happens when you close the window. So before that happens, you need to remove the callback.

You haven't shown the complete code of the first sample.
Those crashes happen when the callback is fired up but the original object has already been destroyed.
I assume you create the UiClass object but later it is garbage collected.
The second sample does not crash as you define your callback function in a global scope and it stays valid until you quit Maya.

Related

How do I direct console output to a pyqt5 plainTextEdit widget with Python?

I am trying to display console output of a python script in a QplainTextEdit widget in PyQt5.
I am getting this error:
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
I have defined my objects in the pyqt GUI file and I believe that I have all the imports.
Update
I have amended the code in this question:
from PyQt5.QtCore import QRectF, Qt
from PyQt5.QtWidgets import QFileDialog, QPlainTextEdit
from PyQt5 import QtCore, QtGui, QtWidgets
from PIL import Image, ImageQt, ImageEnhance
# from PyQt5.QtGui import Qt
from pyqtgraph.examples.text import text
from covid19gui_V3 import Ui_MainWindow
import os
import sys
input_img = Image.open("/home/ironmantis7x/Documents/Maverick_AI/Python/keras-covid-19/maverickAI30k.png")
text_edit = QPlainTextEdit()
class EmittingStream(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
textWritten = QtCore.pyqtSignal(str)
def __init__(self, parent=None, **kwargs):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.ShowIButton.clicked.connect(self.do_test)
self.chooseStudy.clicked.connect(self.do_choosestudy)
self.RunButton_3.clicked.connect(self.do_runstudy)
self.scene = QtWidgets.QGraphicsScene(self)
self.graphicsView.setScene(self.scene)
w, h = input_img.size
self.pixmap_item = self.scene.addPixmap(QtGui.QPixmap())
# self.graphicsView.fitInView(QRectF(0, 0, w, h), Qt.KeepAspectRatio)
self.graphicsView.update()
self.plainTextEdit.update()
self.level = 1
self.enhancer = None
self.timer = QtCore.QTimer(interval=500, timeout=self.on_timeout)
sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
def write(self, text):
self.textWritten.emit(str(text))
#QtCore.pyqtSlot()
def do_test(self):
# input_img = Image.open("/home/ironmantis7x/Documents/Maverick_AI/Python/keras-covid-19/maverickAI30k.png")
self.enhancer = ImageEnhance.Brightness(input_img)
self.timer.start()
self.ShowIButton.setDisabled(True)
#QtCore.pyqtSlot()
def on_timeout(self):
if self.enhancer is not None:
result_img = self.enhancer.enhance(self.level)
qimage = ImageQt.ImageQt(result_img)
self.pixmap_item.setPixmap(QtGui.QPixmap.fromImage(qimage))
if self.level > 7:
self.timer.stop()
self.enhancer = None
self.level = 0
self.ShowIButton.setDisabled(False)
self.level = 1
self.ShowIButton.setDisabled(False)
#QtCore.pyqtSlot()
def do_choosestudy(self):
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.AnyFile)
if dlg.exec_():
filenames = dlg.selectedFiles()
f = open(filenames[0], 'r')
#QtCore.pyqtSlot()
def do_runstudy(self):
os.system("df -h")
# filetext = open('screenout.txt').read()
# filetext.close()
# textViewValue = self.plainTextEdit.toPlainText()
# QPlainTextEdit.appendPlainText(self, str(textViewValue))
# sys.stdout = self(textWritten=self.textWritten)
self.normalOutputWritten(text_edit)
def __del__(self):
# Restore sys.stdout
sys.stdout = sys.__stdout__
def normalOutputWritten(self, text_edit):
#cursor = self.plainTextEdit.textCursor()
#cursor.movePosition(QtGui.QTextCursor.End)
#cursor.insertText(text_edit)
self.plainTextEdit.appendPlainText(text_edit)
#self.plainTextEdit.ensureCursorVisible()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
How can I make this work correctly?
Update 2
I indeed DID do research into the topic and this is one of the main resources I used to try to solve the issue before I posted my question: How to capture output of Python's interpreter and show in a Text widget?
Update 3
I have revised my code in the post to reflect code suggestions in the link I used to help me with my issue.
I am still unable to get this to run correctly. I now get this error:
self.plainTextEdit.appendPlainText(text_edit) TypeError:
appendPlainText(self, str): argument 1 has unexpected type
'QPlainTextEdit'
I have a user interface, TableManagerWindow, that I've been maintaining and developing in Qt designer. After converting via pyuic to a *.py file, I was able to implement what Ferdinand Beyer had suggested in the link you provided above. Simple button to print text to terminal and it indeed does get appended to the QTextEdit widget via append(). Not sure this fits the bill for you for some reason, but I can vouch that it worked for me as well. I'm not savvy enough to get the nuance that is causing your issue, but figured I'd put this here just in case. Admins feel free to delete this if it's extraneous, but it works.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
# Define a stream, custom class, that reports data written to it, with a Qt signal
class EmittingStream(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
class Ui_TableManagerWindow(object):
def setupUi(self, TableManagerWindow):
#define all of my widgets, layout, etc here
.
.
.
# Install a custom output stream by connecting sys.stdout to instance of EmmittingStream.
sys.stdout = EmittingStream(textWritten=self.output_terminal_written)
# Create my signal/connections for custom method
self.source_dir_button.clicked.connect(self.sourceDirButtonClicked)
self.retranslateUi(TableManagerWindow)
QtCore.QMetaObject.connectSlotsByName(TableManagerWindow)
def retranslateUi(self, TableManagerWindow):
.
.
.
#custom method that prints to output terminal. The point is to have this emmitted out to my QTextEdit widget.
def sourceDirButtonClicked(self):
for i in range(10):
print("The Source DIR button has been clicked " + str(i) + " times")
#custom method to write anything printed out to console/terminal to my QTextEdit widget via append function.
def output_terminal_written(self, text):
self.output_terminal_textEdit.append(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
TableManagerWindow = QtWidgets.QMainWindow()
ui = Ui_TableManagerWindow()
ui.setupUi(TableManagerWindow)
TableManagerWindow.show()
sys.exit(app.exec_())

How to Use the Android 'IP Webcam' App with Python

working on simple GUI project. I've got some code from online,and found out how to connect the IP-webcam app, but the question is how do I use this code in my PyQt4 GUI so that the visual of the camera will be shown in the scroll-area widget.
This is the code i used:
import urllib
import cv2
import numpy as np
url='http://192.168.0.100:8080/shot.jpg'
while True:
imgResp=urllib.urlopen(url)
imgNp=np.array(bytearray(imgResp.read()),dtype=np.uint8)
img=cv2.imdecode(imgNp,-1)
# all the opencv processing is done here
cv2.imshow('test',img)
if ord('q')==cv2.waitKey(10):
exit(0)
As #furas points out, a possible option is to use numpy and cv2 to convert it to QPixmap and display it in a QLabel, and so that it looks like streaming run it in a loop.
But instead of getting complicated with all of the above, the simplest thing is to use QtNetwork to get the bytes and convert it directly to QPixmap and send it through signals:
from PyQt4 import QtCore, QtGui, QtNetwork
class IPWebcam(QtCore.QObject):
pixmapChanged = QtCore.pyqtSignal(QtGui.QPixmap)
def __init__(self, url, parent=None):
super(IPWebcam, self).__init__(parent)
self._url = url
self.m_manager = QtNetwork.QNetworkAccessManager(self)
self.m_manager.finished.connect(self._on_finished)
self.m_stopped = True
def start(self):
self.m_stopped = False
self._launch_request()
def stop(self):
self.m_stopped = True
def _launch_request(self):
request = QtNetwork.QNetworkRequest(QtCore.QUrl(self._url))
self.m_manager.get(request)
#QtCore.pyqtSlot(QtNetwork.QNetworkReply)
def _on_finished(self, reply):
ba = reply.readAll()
pixmap = QtGui.QPixmap()
if pixmap.loadFromData(ba):
self.pixmapChanged.emit(pixmap)
if not self.m_stopped:
self._launch_request()
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.m_label = QtGui.QLabel()
self.m_button = QtGui.QPushButton(
"Start", clicked=self.onClicked, checkable=True
)
lay = QtGui.QVBoxLayout(self)
lay.addWidget(self.m_label)
lay.addWidget(self.m_button)
self.resize(640, 480)
url = "http://192.168.0.100:8080/shot.jpg"
self.m_webcam = IPWebcam(url, self)
self.m_webcam.pixmapChanged.connect(self.m_label.setPixmap)
#QtCore.pyqtSlot(bool)
def onClicked(self, checked):
if checked:
self.m_button.setText("Stop")
self.m_webcam.start()
else:
self.m_button.setText("Start")
self.m_webcam.stop()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

The PyQt button does not show up

So....I am using the PyQt lib for python to make a Graphics class which abstracts away most of the features of the QtGui class.I"ll be using it later for my other projects.This seems to be working fine,except that the button and the other widgets do not show up,although the window gets created.
import sys
from PyQt4 import QtGui
class Graphics:
def __init__(self):
self.app=QtGui.QApplication(sys.argv)
self.widgets={}
self.labels={}
self.buttons={}
def getApp(self):
return self.app
def newWidget(self,name:str):
self.widgets[name]=QtGui.QWidget()
return self.widgets[name]
def addButton(self,name:str,text:str):
self.buttons[name]=QtGui.QPushButton(text)
return self.buttons[name]
def addLabel(self,name:str,text:str):
self.labels[name]=QtGui.QLabel()
self.labels[name].setText(text)
return self.labels[name]
def start(self):
for widget in self.widgets:
self.widgets[widget].show()
sys.exit(self.app.exec_())
^ That's the code.Down below shows how i implement the class
from graphics import Graphics
gui=Graphics()
w1=gui.newWidget("hmm")
bt1=gui.addButton("hey","hello")
print(bt1)
gui.start()
It'd be great if you could provide insight as to why this is happening.Thank You
In Qt there is a basic rule: the QWidget children are drawn with respect to the parent QWidget, and if it does not have a parent this will be a window, which is called top-level.
Another concept is QPushButton, QLabel, QSpinBox, etc. are QWidgets since they inherit from this class.
So, since QPushButton does not have a parent, it should show itself as a window, and for that you should use show():
def start(self):
[w.show() for name, w in self.widgets.items()]
[button.show() for name, button in self.buttons.items()]
[label.show() for name, label in self.labels.items()]
sys.exit(self.app.exec_())
If your intention is that some QLabel or QPushButton be part of some QWidget then we must indicate that widget as parent, for example in my next solution I propose to add the name of the widget, and if the widget does not exist it should be created:
import sys
from PyQt4 import QtGui
class Graphics:
def __init__(self):
self.app=QtGui.QApplication(sys.argv)
self.widgets={}
self.labels={}
self.buttons={}
def getApp(self):
return self.app
def newWidget(self, name:str):
w = QtGui.QWidget()
self.widgets[name] = w
return w
def addButton(self, widget_name:str, name:str, text:str):
if widget_name in self.widgets:
w = self.widgets[widget_name]
else:
w = self.newWidget(widget_name)
button = QtGui.QPushButton(text, parent=w)
self.buttons[name] = button
return button
def addLabel(self, widget_name:str, name:str, text:str):
if widget_name in self.widgets:
w = self.widgets[widget_name]
else:
w = self.newWidget(widget_name)
label = QtGui.QLabel(text, parent=w)
self.labels[name] = label
return label
def start(self):
[w.show() for name, w in self.widgets.items()]
sys.exit(self.app.exec_())
If you want to add a parent after the button is created then you can use setParent():
graphics.py
import sys
from PyQt4 import QtGui
class Graphics:
def __init__(self):
self.app=QtGui.QApplication(sys.argv)
self.widgets={}
self.labels={}
self.buttons={}
def getApp(self):
return self.app
def newWidget(self, name:str):
w = QtGui.QWidget()
self.widgets[name] = w
return w
def addButton(self, name:str, text:str):
button = QtGui.QPushButton(text)
self.buttons[name] = button
return button
def addLabel(self, name:str, text:str):
label = QtGui.QLabel(text)
self.labels[name] = label
return label
def start(self):
for _, w in in self.widgets.items():
w.show()
sys.exit(self.app.exec_())
main.py
gui=Graphics()
w1 = gui.newWidget("hmm")
bt1 = gui.addButton("hey","hello")
bt1.setParent(w1) # <-- set w1 as parent of bt1
gui.start()

window freezes when I try to show an evolving value in a Qt window using threads with python

My main objective is to show a constantly evolving value on a Qt-window textEdit. (this window contains only a checkBox and a textEdit).
Sadly, I cannot click on the checkbox and the window is frozen until I shutdown the terminal.
import sys
from threading import Thread
from random import randint
import time
from PyQt4 import QtGui,uic
class MyThread(Thread):
def __init__(self):
Thread.__init__(self)
#function to continually change the targeted value
def run(self):
for i in range(1, 20):
self.a = randint (1, 10)
secondsToSleep = 1
time.sleep(secondsToSleep)
class MyWindow(QtGui.QMainWindow,Thread):
def __init__(self):
Thread.__init__(self)
super(MyWindow,self).__init__()
uic.loadUi('mywindow.ui',self)
self.checkBox.stateChanged.connect(self.checkeven)
self.show()
#i show the value only if the checkbox is checked
def checkeven(self):
while self.checkBox.isChecked():
self.textEdit.setText(str(myThreadOb1.a))
# Run following code when the program starts
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
# Declare objects of MyThread class
myThreadOb1 = MyThread()
myThreadOb2 = MyWindow()
# Start running the threads!
myThreadOb1.start()
myThreadOb2.start()
sys.exit(app.exec_())
At the moment I'm using a thread to set a random value to a, but at the end it is supposed to be a bit more complex as I will have to take the values from an automation.
Do you have any clue why my code is acting like this?
Thank you very much for your help.
The problem is that the while self.checkBox.isChecked() is blocking, preventing the GUI from handling other events.
Also you should not run a PyQt GUI on another thread than the main one.
If you want to send data from one thread to another, a good option is to use the signals.
Doing all these considerations we have the following:
import sys
from threading import Thread
from random import randint
import time
from PyQt4 import QtGui, uic, QtCore
class MyThread(Thread, QtCore.QObject):
aChanged = QtCore.pyqtSignal(int)
def __init__(self):
Thread.__init__(self)
QtCore.QObject.__init__(self)
#function to continually change the targeted value
def run(self):
for i in range(1, 20):
self.aChanged.emit(randint(1, 10))
secondsToSleep = 1
time.sleep(secondsToSleep)
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow,self).__init__()
uic.loadUi('mywindow.ui',self)
self.thread = MyThread()
self.thread.aChanged.connect(self.on_a_changed, QtCore.Qt.QueuedConnection)
self.thread.start()
self.show()
#i show the value only if the checkbox is checked
#QtCore.pyqtSlot(int)
def on_a_changed(self, a):
if self.checkBox.isChecked():
self.textEdit.setText(str(a))
# Run following code when the program starts
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
# Declare objects of MyThread class
w = MyWindow()
sys.exit(app.exec_())
An option more to the style of Qt would be to use QThread since it is a class that handles a thread and is a QObject so it handles the signals easily.
import sys
from random import randint
from PyQt4 import QtGui, uic, QtCore
class MyThread(QtCore.QThread):
aChanged = QtCore.pyqtSignal(int)
def run(self):
for i in range(1, 20):
self.aChanged.emit(randint(1, 10))
secondsToSleep = 1
QtCore.QThread.sleep(secondsToSleep)
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow,self).__init__()
uic.loadUi('mywindow.ui',self)
self.thread = MyThread()
self.thread.aChanged.connect(self.on_a_changed, QtCore.Qt.QueuedConnection)
self.thread.start()
self.show()
#i show the value only if the checkbox is checked
#QtCore.pyqtSlot(int)
def on_a_changed(self, a):
if self.checkBox.isChecked():
self.textEdit.setText(str(a))
# Run following code when the program starts
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
# Declare objects of MyThread class
w = MyWindow()
sys.exit(app.exec_())

buttonClicked signal of QMessageBox isn't working

I would like use a Qmessagebox in order to display some info about a running computation and as a stop function when I click on the OK button.
However when I use the signal buttonClicked nothing is happenning and hte function connect with it is never called
Here a code to illustrate my issue:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class SenderObject(QObject):
something_happened = pyqtSignal( )
class myfunc():
updateTime = SenderObject()
def __init__(self):
self.i = 0
self.stop = True
def run(self):
while self.stop :
self.i+=1
if self.i%100 == 0:
self.updateTime.something_happened.emit()
print('infinit loop',self.i)
class SurfViewer(QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
self.parent = parent
self.setFixedWidth(200)
self.setFixedHeight(200)
self.wid = QWidget()
self.setCentralWidget(self.wid)
self.groups = QHBoxLayout() ####
self.Run = QPushButton('Run')
self.groups.addWidget(self.Run)
self.wid.setLayout(self.groups)
self.Run.clicked.connect(self.run)
self.myfunc = myfunc()
self.myfunc.updateTime.something_happened.connect(self.updateTime)
def run(self):
self.msg = QMessageBox()
self.msg.setText('Click Ok to stop the loop')
self.msg.setWindowTitle(" ")
self.msg.setModal(False)
self.msg.show()
self.myfunc.run()
self.msg.buttonClicked.connect(self.Okpressed)
def Okpressed(self):
self.myfunc.stop = False
#pyqtSlot( )
def updateTime(self ):
self.msg.setText('Click Ok to stop the loop\ni = '+str(self.myfunc.i))
self.parent.processEvents()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = SurfViewer(app)
ex.setWindowTitle('window')
ex.show()
sys.exit(app.exec_( ))
So theself.msg.buttonClicked.connect(self.Okpressed) line never call the function Okpressed. Therefore, myfunc.run is never stopped.
Somebody could help on this?
write
self.msg.buttonClicked.connect(self.Okpressed)
before
self.myfunc.run()
If you call run function before subscribing click event, curse will stuck into infinite while loop. so your click event never subscribed.
First subscribe click event and then call "run" function of "myfunc"
And yes never do this -
from PyQt4.QtGui import *
from PyQt4.QtCore import *
Its vbad programming practice. You can write like
from PyQt4 import QtGui
And use into code like
QtGui.QMessagebox

Categories