QPushButton of pyside2 and cmds.error() won't work as I expected.

QPushButton of pyside2 and cmds.error() won't work as I expected.

HitomuIto
Explorer Explorer
466 Views
2 Replies
Message 1 of 3

QPushButton of pyside2 and cmds.error() won't work as I expected.

HitomuIto
Explorer
Explorer

Hello.

 

I'm making a tool with GUI by using pyside2 in maya. It has some functions that can be called by clicking buttons a gui has and some of them throw error if needed.

 

However, "cmds.error()" command won't work as I expected. Whenever I use "cmds.error()" command it shows error in red color in the command line at right bottom corner of maya but this time...

 

The following code can reproduce the problem I'm facing. The error is not shown in the command line if you click the button.

from PySide2 import QtWidgets
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin

class TestWindow(MayaQWidgetDockableMixin, QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(TestWindow, self).__init__(parent=parent)

        self.main_widget = QtWidgets.QWidget()
        self.main_layout = QtWidgets.QHBoxLayout()
        
        self.btn = QtWidgets.QPushButton('Throw Error')
        self.btn.clicked.connect(self.throw_error)
        self.main_layout.addWidget(self.btn)
        self.main_widget.setLayout(self.main_layout)
        self.setCentralWidget(self.main_widget)
        
    def throw_error(self):
        cmds.error('Error')

window = TestWindow()
window.show()

 

If someone knows the solution of the problem, please help me!

Thank you.

 

maya_folum.jpg

0 Likes
Accepted solutions (1)
467 Views
2 Replies
Replies (2)
Message 2 of 3

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

This has to do with the way QT handles exceptions. Basically, the error command raises an exception and QT then just stores that information for later and stops, but it doesn't really know about maya so it doesn't print out the exception in the scripteditor.

 

There isn't really a way to get QT UI's working with cmds.error as far as I'm aware of. But you can always use another logger to diplay your error message. I like to use the one from the logging library, since it also is quite helpful for debugging.

from PySide2 import QtWidgets
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
import maya.cmds as cmds
import logging

_logger = logging.getLogger(__name__)
_logger.setLevel(logging.ERROR)


class TestWindow(MayaQWidgetDockableMixin, QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(TestWindow, self).__init__(parent=parent)

        self.main_widget = QtWidgets.QWidget()
        self.main_layout = QtWidgets.QHBoxLayout()
        
        self.btn = QtWidgets.QPushButton('Throw Error')
        self.btn.clicked.connect(self.throw_error)
        self.main_layout.addWidget(self.btn)
        self.main_widget.setLayout(self.main_layout)
        self.setCentralWidget(self.main_widget)
        
    def throw_error(self):
        _logger.error("Error")
        return 
        


window = TestWindow()
window.show()

Now as you notice, I also added a return after the error message. This is technically not necessary here. But if your function did something more than just throwing an error, you would need it, since the logger only prints out an error type message, it doesn't acutally throw an exception, so every code in the function would still be run after the error.

 

I hope it helps!

Message 3 of 3

HitomuIto
Explorer
Explorer

Thank you so much for your support!

 

I've just tried to execute your code and it completely woked as I wanted.

I finally understood how QT handles exception as well as why "return" statement is necessary.


It seems that i can go to next step thanks to you.

Thank you again!

0 Likes