Maya not displaying errors when triggered by a qt signal

493 views
Skip to first unread message

fruity

unread,
Jan 12, 2017, 2:26:53 PM1/12/17
to Python Programming for Autodesk Maya
Hi there,

I'm facing a problem that I never noticed before in python / qt : when I try to raise an error from a function triggered by a signal, I can't get the red message in maya. The error is raised in the script editor, but if the SE is closed, no way to know that an error occured.
To see what I mean, you can run the piece of code at the end of my post.
From what I understood, it comes from the mechanism of signals / slots itself, like if the function triggered by a signal was 'local' only. If I call the same method manually (i.e. NOT with the signal), it works.
I tried with both pyqt4 and pyside, same result.
I have a couple of workarounds (I can use the api to raise my error instead, or I can use logging), but the truth is that I really like the built-in errors in python, and I'd love to be able to stick to this workflow if possible.
If anyone has a solution for that, I'd be really happy to know it !
Many thanks

from PySide.QtGui import *
from PySide.QtCore import *
class Xxx(QWidget):
    def __init__(self, parent=None):
        super(Xxx, self).__init__(parent)
        self.ui()
    def ui(self):
        btn = QPushButton('Raise error')
        btn.clicked.connect(self.raiseError)
        mainLayout = QHBoxLayout()
        mainLayout.addWidget(btn)
        self.setLayout(mainLayout)
    def raiseError(self):
        raise NameError
a = Xxx()
a.show()

Justin Israel

unread,
Jan 12, 2017, 4:00:10 PM1/12/17
to Python Programming for Autodesk Maya
I believe it is caused by the fact that Qt slot exceptions can't be caught by the user, since they can happen async in the event loop, so there is a top level sys exception hook that will catch them and print them. But being Maya, this has no integration with the Script Editor, since it doesn't really know an exception happened. This is all a guess.

But, here is a possible solution you could use:

It is a decorator, which you can add to your slots, in order to report exceptions to the Script Editor

Justin

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/76b853ec-2ba8-4d79-85e4-fcf78be74cf2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

fruity

unread,
Jan 12, 2017, 10:12:29 PM1/12/17
to Python Programming for Autodesk Maya
Thanks for the answer Justin !
I didn't really understand the event loop thing. 
The decorator thing is handy, however it'll raise a warning, not an error. If I change it to make it raise an error, same problem.
I'm actually really surprised that no one (including me) never has to raise errors in methods^^
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Jan 12, 2017, 10:35:24 PM1/12/17
to python_in...@googlegroups.com
On Fri, Jan 13, 2017 at 4:12 PM fruity <fruit...@gmail.com> wrote:
Thanks for the answer Justin !
I didn't really understand the event loop thing. 

Its just basically meaning that the function you have asked to run as a slot, when a signal fires, is a request for Qt to run it for you at a later date in time. So when Qt wants to fire the signal and then goes to all the registered slots to run them, it does not know about Maya. It just catches the exception itself and prints it. Thats why you would have to put your own wrapper around it and handle it the way you want, for Maya. 
 
The decorator thing is handy, however it'll raise a warning, not an error. If I change it to make it raise an error, same problem.

Yea I saw that difference as well, but I didn't really track down the reason. I just switched to using a warning to get the example working.
 
I'm actually really surprised that no one (including me) never has to raise errors in methods^^

Well more specifically you are talking about errors from Qt slots, and not just any method that runs and raises an error :-)
 
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/2bb80ce0-41b9-4c3b-ac88-169dcb43795c%40googlegroups.com.

fruity

unread,
Jan 17, 2017, 12:02:33 AM1/17/17
to Python Programming for Autodesk Maya
hehe indead, erros triggered by qt slots, but still, I'm surprised that I never faced that before ! Well, I guess there is a beginning to everything =]
So no solution proper solution, then =[. I'll go for the logger one, at least for now, and will try to update this topic when I'll have time to investigate a bit further and if I find something !
Thanks for the help
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Marcus Ottosson

unread,
Jan 17, 2017, 4:06:23 AM1/17/17
to python_in...@googlegroups.com

Hey Vincent,

Give this a go, it’ll be visible in the command bar, but as a warning (yellow), not an error.

from PySide.QtGui import *
from PySide.QtCore import *

class Xxx(QWidget):
    def __init__(self, parent=None):
        super(Xxx, self).__init__(parent)
        self.ui()
    def ui(self):
        btn = QPushButton('Raise error')
        btn.clicked.connect(self.raiseError)
        mainLayout = QHBoxLayout()
        mainLayout.addWidget(btn)
        self.setLayout(mainLayout)
    def raiseError(self):

        cmds.warning("Don't go to the light!")

a = Xxx()
a.show()

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Reply all
Reply to author
Forward
0 new messages