converting mel to python

66 views
Skip to first unread message

yann19

unread,
Dec 23, 2014, 5:34:01 AM12/23/14
to python_in...@googlegroups.com
This is my first attempt at converting mel code into python - http://pastebin.com/raw.php?i=JwEtGJV6

This is the code that I have done:

import maya.cmds as cmds

def reloadScene():
    recentFiles
= []
   
    curFile
= cmds.file(query = True, location = True)
   
   
if (curFile == "unknown") :
   
       
if cmds.optionVar( exists = 'RecentFilesList' ):
            recentFiles
= cmds.optionVar( query = 'RecentFilesList' )
           
           
if cmds.confirmDialog(title = 'Reload Scene', message = ('Reload Last Opened Scene?\n\n' + recentFiles[len(recentFiles)-1]), button = ['Cancel','OK'], defaultButton = 'OK', cancelButton = 'Cancel', dismissString = 'Cancel' ):
                cmds
.file( str(recentFiles[len(recentFiles)-1]), force = True, open = True)
               
print "Opening Last Recent File - ", recentFiles[len(recentFiles)-1]
               
           
else:
                cmds
.warning("No recent files found!")
       
else:
           
if cmds.confirmDialog(title = 'Reload Scene', message = ('Reload Current Scene?\n\n'), button = ['Cancel','OK'], defaultButton = 'OK', cancelButton = 'Cancel', dismissString = 'Cancel' ):
                curFileLoc
= cmds.file(query = True, location = True)
                cmds
.file( curFileLoc , force = True, open = True)
               
print "Re-Opening current file - ", curFileLoc


But for some reasons, if I added in the curFile variables, my dialog is not popping up and I have a very strong inkling that I have gotten the mel part -
if(`file -q -loc` == "unknown") wrong
However, should I omit it, I am unable to get the second dialog working - Reload Current Scene working.

Any pointers are greatly appreciated!


Joe Weidenbach

unread,
Dec 23, 2014, 11:32:24 AM12/23/14
to python_in...@googlegroups.com
Hi again,

So, given this code, The only mistake I see is indeed the parentheses.  Here's the code that works: http://pastebin.com/raw.php?i=i2N9JPAW

A note, I added a call to reloadScene() at the end just to make it functional as a button.

Joe
--
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/687fc019-66ef-4e67-bce2-e95dbeaab015%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.




This email is free from viruses and malware because avast! Antivirus protection is active.


Gerard Gmail

unread,
Dec 23, 2014, 3:40:47 PM12/23/14
to python_in...@googlegroups.com
I'm no power programmer but I would try to economise on repeating similar queries such as checking for the existence of optionVar recentList before querying the recentLst. I *think* that you could use a try: / except: approach to just query the recentFile (don't check for its existence - the try statement will not cause an error if the optVar doesn't actually exist, but execute the except section if the try statement fails. 

I'm not on the box at the moment but the idea would be:

'''
Disclaimer: haven't written this on a machine so there could be bugs/bad formatting etc
'''

try:
    recentFiles = cmds.optionVar( query = 'RecentFilesList' )
except:
    # do something else here.. or..
    cmds.error('no recent list')
    


Gerard. 

yann19

unread,
Dec 23, 2014, 9:31:55 PM12/23/14
to python_in...@googlegroups.com
Hi Joe,

I have tried out your code, pardon me, but I am not seeing much of a difference, other than the removal of brackets in the first if statement...
Even so, I am getting no dialog popup should for the second part of the confirmDialog...
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

yann19

unread,
Dec 23, 2014, 9:39:39 PM12/23/14
to python_in...@googlegroups.com
Hi Gerard,

Thanks for the info, I am also not a power programmer as well. Just started on basic scripting..
Correct me if I am wrong, but even without the use of try/except statement that you mentioned, it should works as well right?
Probably from a good programmer POV, try/except is a better code style, I guess?
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.




This email is free from viruses and malware because avast! Antivirus protection is active.


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

yann19

unread,
Dec 23, 2014, 9:54:54 PM12/23/14
to python_in...@googlegroups.com
Hi all,

i have sort of found the solution and it is working - http://pastebin.com/raw.php?i=PA61f9kb

However, if someone could kindly give me some more pointers for refinement, I am at all ears :)

Even so, I have a question in my code. In the try/except statement as suggested by Joe, I implemented it but as soon as I input in return recentFiles, both the dialogs will not run. Why is it so?

Justin Israel

unread,
Dec 23, 2014, 11:02:11 PM12/23/14
to python_in...@googlegroups.com

Hi

I can't check out too much in depth since I'm on my phone. But you definitely should not be using the return statement in your try except since that will immediately return from your function and not execute any more code. Also, is a try except necessary here? Does an optionvar call raise an exception? I actually can't confirm atm.

A few nitpicky things...  remove the spacing in your params of function calls:

foo( var = "hello" )

should really be

foo(var= "hello")

Also, I find it easier to read code when it tries to return early, instead of having nested indented if statements. So if you have a check that needs to pass before continuing then do something like

if x != 1:
    return

Instead of

if x == 1:
    if y == 2:
        if z == 3:
            print True


--
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/fa42c21b-7f51-4483-b70a-bda207796a76%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages