sys.exit() closes maya in pyside UI?

835 views
Skip to first unread message

Rudi Hammad

unread,
Dec 20, 2015, 5:16:46 AM12/20/15
to Python Programming for Autodesk Maya
okey, I´ll try to explain. Usually, in my scripts I uses sys.exit()
So, if have a script that requires something to be selected, and nothing is selected, what I do is cmds.warning ("please select something") and then sys.exit()
this works perfectly fine if I execute " manually" the script, but in a Pyside UI ( I followed this tutorial http://www.brechtos.com/using-qt-designer-pyside-create-maya-2014-editor-windows/ )
when I connect that script to a button click, after the warning, maya closes. I don´t think it is a crash, because I don´t get any window error.
isn´t that weird?

Justin Israel

unread,
Dec 20, 2015, 5:29:36 AM12/20/15
to Python Programming for Autodesk Maya

I'm not sure if you are joking or not, so I will just reply assuming you are serious..

It isn't weird that Maya would close if you call sys.exit within a script. What behavior were you expecting?

https://docs.python.org/2/library/sys.html#sys.exit

Calling that from a main thread will kill your process. It might make sense to do that from a standalone main application. But Maya is a persistent process that can host many scripts within its python interpreter. You don't want to be using sys.exit there.

Python has the common "if __name__ == 'main'" idiom just for this reason. To check if your script is the main process or not. You should only use exit if you are the main process, otherwise anyone importing and running your script in their app (Maya) will get adverse effects.


--
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/6d340e44-527a-4158-af25-7d2585204ad5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rudi Hammad

unread,
Dec 20, 2015, 6:45:30 AM12/20/15
to Python Programming for Autodesk Maya
I wasn´t joking, so I am assuming I am just stoopid
I was expecting the script to stop, not maya to close.
thx Justin



El domingo, 20 de diciembre de 2015, 11:29:36 (UTC+1), Justin Israel escribió:

I'm not sure if you are joking or not, so I will just reply assuming you are serious..

It isn't weird that Maya would close if you call sys.exit within a script. What behavior were you expecting?

https://docs.python.org/2/library/sys.html#sys.exit

Calling that from a main thread will kill your process. It might make sense to do that from a standalone main application. But Maya is a persistent process that can host many scripts within its python interpreter. You don't want to be using sys.exit there.

Python has the common "if __name__ == 'main'" idiom just for this reason. To check if your script is the main process or not. You should only use exit if you are the main process, otherwise anyone importing and running your script in their app (Maya) will get adverse effects.


On Sun, 20 Dec 2015 11:16 PM Rudi Hammad <rudih...@gmail.com> wrote:
okey, I´ll try to explain. Usually, in my scripts I uses sys.exit()
So, if have a script that requires something to be selected, and nothing is selected, what I do is cmds.warning ("please select something") and then sys.exit()
this works perfectly fine if I execute " manually" the script, but in a Pyside UI ( I followed this tutorial http://www.brechtos.com/using-qt-designer-pyside-create-maya-2014-editor-windows/ )
when I connect that script to a button click, after the warning, maya closes. I don´t think it is a crash, because I don´t get any window error.
isn´t that weird?

--
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.

Justin Israel

unread,
Dec 20, 2015, 1:47:09 PM12/20/15
to python_in...@googlegroups.com
On Mon, Dec 21, 2015 at 12:45 AM Rudi Hammad <rudih...@gmail.com> wrote:
I wasn´t joking, so I am assuming I am just stoopid
I was expecting the script to stop, not maya to close.
thx Justin

I didn't mean offense. Its just that the docs describe it as quitting the process, so I wasn't sure if you were joking, or just hadn't read what it does before. 

The way to "stop" execution at a certain point is just to return from your function. Either that or my previous suggestion of handling the difference between being run as a standalone script vs within Maya:
def main():
    # 0 means success
    exitcode = 0

    print "Doing common logic here, regardless of Maya or standalone tool"
    # foo()
    # bar()
    #...

    print "oops. an error!"
    # non-zero means error
    exitcode = 1
    return exitcode

if __name__ == '__main__':
    import sys

    print "Running as standalone application"
    # exit the main application with a status code
    sys.exit(main())




El domingo, 20 de diciembre de 2015, 11:29:36 (UTC+1), Justin Israel escribió:

I'm not sure if you are joking or not, so I will just reply assuming you are serious..

It isn't weird that Maya would close if you call sys.exit within a script. What behavior were you expecting?

https://docs.python.org/2/library/sys.html#sys.exit

Calling that from a main thread will kill your process. It might make sense to do that from a standalone main application. But Maya is a persistent process that can host many scripts within its python interpreter. You don't want to be using sys.exit there.

Python has the common "if __name__ == 'main'" idiom just for this reason. To check if your script is the main process or not. You should only use exit if you are the main process, otherwise anyone importing and running your script in their app (Maya) will get adverse effects.


On Sun, 20 Dec 2015 11:16 PM Rudi Hammad <rudih...@gmail.com> wrote:
okey, I´ll try to explain. Usually, in my scripts I uses sys.exit()
So, if have a script that requires something to be selected, and nothing is selected, what I do is cmds.warning ("please select something") and then sys.exit()
this works perfectly fine if I execute " manually" the script, but in a Pyside UI ( I followed this tutorial http://www.brechtos.com/using-qt-designer-pyside-create-maya-2014-editor-windows/ )
when I connect that script to a button click, after the warning, maya closes. I don´t think it is a crash, because I don´t get any window error.
isn´t that weird?

--
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.

--
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/4976f50f-ee43-45f3-b5e3-6a882f12cc33%40googlegroups.com.

Rudi Hammad

unread,
Dec 21, 2015, 8:20:16 PM12/21/15
to Python Programming for Autodesk Maya
no offense taken. Every day I learn something new, so I have plenty of doubts.
anyway, after some research I am using
raise Exception("oops. an error")
Is this okey? I was recently working at Ilion, and this was used a lot by programmers
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.

Justin Israel

unread,
Dec 22, 2015, 10:11:02 PM12/22/15
to Python Programming for Autodesk Maya
Exceptions are one way of doing it. It would be good for you to use a more specific builtin exception type or a custom one if you need to communicate more information with the exception. You can have a different entry point for when you are in Maya, which catches the exception and instead just called the Maya error() command or raises some error dialog. And when someone runs it in standalone, you can catch it, print it, and exit. 


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.

--
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/629a2ef1-35d4-4eb5-ab6c-774ff7ea276d%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages