@Ourouk (Ourouk) posted a new topic, subject "How to display mel error in Python ?". It read:

   Hi, I have a little problem. Can't find the answer anywhere…
    
    I have to execute mel scripts in python like this :
    [code] 
        mel.eval(source"script.mel";) 
        
    [/code]
    
    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 :
    [code] 
        // 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 # 
        
    [/code]
    
    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 !

Then @Chadrik (Chadrik) said

    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.
    
    sourcing your script in pymel looks like this:  
    mel.source(“script.mel”)

So @Ourouk (Ourouk) replied

    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

After which, @CaptainSam (CaptainSam) added

    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.

After which, @Ourouk (Ourouk) added

    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 ?

To which @Chadrik (Chadrik) replied

    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

So @Ourouk (Ourouk) replied

    Reinventing the wheel as always been my biggest dream 
    
    thanks for the info…Will try asap   
    bye

@Ourouk (Ourouk) replied

    Ok…I installed pymel, with some problems…
    
    Auto install error message :
    [code] 
        Unexpected token 'setup.py' in expression or statement.
          At line:1 char:53
          + "e:\softs\maya2009\bin\mayapy.exe" setup.py <<< install
        
    [/code]
    
    So I switch to manual install which worked…but what is the problem with auto install ?
    
    thanks  
    bye

To which @Chadrik (Chadrik) replied

    you got that from a windows shell? that doesn't look like a windows shell error…

So @Ourouk (Ourouk) replied

    Yep it was a window shell…but not the default shell, it's windows powershell.  
    will try with the default one.

After which, @underearth (underearth) added

    i use following to display error (maya command error)
    
    import maya.mel as mm  
    try:  
    mm.eval(‘error “there is error”;')  
    except RuntimeError:  
    pass

Then @Chadrik (Chadrik) said

    in your example, you cannot programmatically retrieve the error message in python:
    [code] 
        import maya.mel as mm
         try:
        	mm.eval('error "there is error";')
         except RuntimeError, err:
        	print str(err)
        
    [/code]
    
    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 (Ourouk) replied

    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 :
    [code] 
          import maya.mel as mel
          try:
          	mel.eval("source \"myscript.mel\";myscript();")
          except:
          	pass
        
    [/code]
    
    it just display the good error message given by the mel script :
    [code] 
        // Error: select at least 1 mesh to optimize
        
    [/code]
    
    For now it is a temporary solution…but it works…don't need more…
    
    thanks !

After which, @underearth (underearth) added

    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 (CGTalk_Moderation) replied

    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.


{'source': 'discourse', 'url': 'https://forums.cgsociety.org/t/how-to-display-mel-error-in-python/1373558', 'title': 'How to display mel error in Python ?', 'views': 1189, 'has_accepted_answer': False, 'word_count': 786, 'like_count': 0, 'reply_count': 12, 'participant_count': 5, 'posts': 15, 'tags': '', 'author': '@Ourouk (Ourouk)', 'participants': '@Ourouk (Ourouk), @Chadrik (Chadrik), @CaptainSam (CaptainSam), @underearth (underearth), @CGTalk_Moderation (CGTalk_Moderation)', 'image_url': '', 'created_at': '2009-10-29T17:03:22.000Z', 'last_posted_at': '2009-11-05T06:55:02.000Z'}