PDA

View Full Version : How to display mel error in Python ?


Ourouk
10-30-2009, 12:03 AM
Hi, I have a little problem. Can't find the answer anywhere...

I have to execute mel scripts in python like this : mel.eval(source"script.mel";)

If errors occur in the mel scripts, I would like to display it in Python. Maya just display the mel error message then the python global error like this :

// Error: Please select at least one node to duplicate <-- MEL ERROR
//
# Error: Error occurred during execution of MEL script
# Traceback (most recent call last):
# File "<maya console>", line 2, in <module>
# RuntimeError: Error occurred during execution of MEL script #

How can I track the MEL error and display it in Python ? In fact, I just would like to display the error like old times in the script editor line at the bottom of Maya UI. It's hard to explain, hope you'll understand ;)

thanks !

Chadrik
10-30-2009, 01:39 AM
pymel (http://code.google.com/p/pymel/) supports this. it will return different types of exceptions depending on what type of mel error occurs, along with the full error message and line numbers. docs on the subject are here (http://pymel.googlecode.com/svn/docs/generated/classes/pymel.core.language/pymel.core.language.Mel.html#pymel.Mel).

sourcing your script in pymel looks like this:
mel.source("script.mel")

Ourouk
10-31-2009, 11:58 AM
Thanks...

But I would like to avoid using another "extra package" like PyMel. I already use PyQt and installing it on various OS in various company is complicated (write permissions, as many install as os, os version etc etc). In fact, with PyQt i can't always use my tools... :/
But I can do UI in mel no more ;)

If I had another package, it'll be twice as complicated ;)

So ? Is there any other solution in pure Python ?

thanks

CaptainSam
10-31-2009, 01:30 PM
PyMel really is THE big Maya Python project. If your serious about Maya scripting, there is basically no way around it, unless you love reinventing the wheel all the time.

Ourouk
10-31-2009, 04:23 PM
Yeah I know that pyMel rocks ;)
Perhaps I'll give it a try...In fact it is already unpacking ;)

But by the way, is there any solution for now for my problem ? :)

Chadrik
10-31-2009, 04:39 PM
Check out the code in the Mel class in pymel.core.language. it uses MCommandResult to handle the calling of mel scripts so that it can retrieve error messages. if you don't want to use pymel you can use this code on your own, but there's no standard way to do this without making a utility like the Mel class. like CaptainSam said, if you're not using pymel you're either not using python in maya to its fullest or you're reinventing the wheel.

-chad

Ourouk
10-31-2009, 05:49 PM
Reinventing the wheel as always been my biggest dream ;)

thanks for the info...Will try asap ;)
bye

Ourouk
10-31-2009, 11:17 PM
Ok...I installed pymel, with some problems...

Auto install error message :
Unexpected token 'setup.py' in expression or statement.
At line:1 char:53
+ "e:\softs\maya2009\bin\mayapy.exe" setup.py <<< install

So I switch to manual install which worked...but what is the problem with auto install ?

thanks
bye

Chadrik
11-01-2009, 01:43 AM
you got that from a windows shell? that doesn't look like a windows shell error....

Ourouk
11-01-2009, 11:03 AM
Yep it was a window shell...but not the default shell, it's windows powershell.
will try with the default one.

underearth
11-02-2009, 06:54 AM
i use following to display error (maya command error)

import maya.mel as mm
try:
mm.eval('error "there is error";')
except RuntimeError:
pass

Chadrik
11-02-2009, 03:04 PM
in your example, you cannot programmatically retrieve the error message in python:

import maya.mel as mm
try:
mm.eval('error "there is error";')
except RuntimeError, err:
print str(err)

from python this prints 'Error occurred during execution of MEL script' and not the desired 'there is error'.

the same problem exists for errors that happen inside mel that are unintended, like syntax errors, conversion errors, etc.

Ourouk
11-04-2009, 08:10 PM
Hi I think I have a pretty crappy solution but...while I am trying pymel, it'll be this way ;)
(thanx to kelsolaar and gaviez)

so all I had to do was this :

import maya.mel as mel
try:
mel.eval("source \"myscript.mel\";myscript();")
except:
pass

it just display the good error message given by the mel script :

// Error: select at least 1 mesh to optimize

For now it is a temporary solution...but it works...don't need more...

thanks !

underearth
11-05-2009, 01:55 PM
at workplace where i am using maya Python i'm also using same way for error..
but at my personal projects I JUST LOVE PYMEL... ( i wish it could have been maya's default)

thanks once again chad

CGTalk Moderation
11-05-2009, 01:55 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.