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.


On Mon, 21 Dec 2015 5:20 PM Rudi Hammad <[email protected]> wrote:

> 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
>
> El domingo, 20 de diciembre de 2015, 19:47:09 (UTC+1), Justin Israel
> escribió:
>
>>
>>
>> On Mon, Dec 21, 2015 at 12:45 AM Rudi Hammad <[email protected]> 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 <[email protected]> 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 [email protected].
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/python_inside_maya/6d340e44-527a-4158-af25-7d2585204ad5%40googlegroups.com
>>>>> <https://groups.google.com/d/msgid/python_inside_maya/6d340e44-527a-4158-af25-7d2585204ad5%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>> --
>>> 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 [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/python_inside_maya/4976f50f-ee43-45f3-b5e3-6a882f12cc33%40googlegroups.com
>>> <https://groups.google.com/d/msgid/python_inside_maya/4976f50f-ee43-45f3-b5e3-6a882f12cc33%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/629a2ef1-35d4-4eb5-ab6c-774ff7ea276d%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/629a2ef1-35d4-4eb5-ab6c-774ff7ea276d%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1XoKfgNKLhwXaB%2BTB4yMmCfkiSZq7HZ4sEqyaFQDQZ1Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to