New to Python, New to Maya, Old to 3D

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Display Modes
  07 July 2013
New to Python, New to Maya, Old to 3D

Hi there! I've been a 3ds Max user for 15+ years and now I'm using Maya and Python. I think I'm doing well adapting but I've got a few questions for things not working as expected.

The Open File Dialog defaults to the last used folder, but I would like for it to start in the current open file's directory. This script below runs fine when executing it from the script editor, but it only runs every other time when using a hotkey.

Script code:

import maya.cmds as cmds

def customOpen():

    filePath = cmds.file(query=True, sceneName=True)
    fileName = cmds.file(query=True, sceneName=True, shortName=True)
    filePath = filePath.replace(fileName, '')

    if filePath == '':
        filePath = cmds.workspace(q=True, rd=True)

    fileFilter = "Maya Binary (*.mb);;Maya ASCII (*.ma);;All Files (*.*)"

    newOpenFile = cmds.fileDialog2(caption='Open', fileFilter=fileFilter, startingDirectory=filePath, okCaption='Open', cancelCaption='Cancel', fileMode=1)

    if newOpenFile is not None:
        mBox = 'Save'

        if cmds.file(query=True, modified=True):
            mBox = cmds.confirmDialog(title='Save?', message='The current file is not saved. Save this file?', button=['Save', "Don't Save", 'Cancel'], defaultButton='Save', cancelButton='Cancel', dismissString='Cancel')

            if mBox == 'Save':
                if fileName != '':
                    newSaveFile = cmds.fileDialog2(caption='Save', fileFilter=fileFilter, startingDirectory=filePath, okCaption='Save', cancelCaption='Cancel', fileMode=1)
                    if newSaveFile is not None:
                        cmds.file(newSaveFile, save=True)

        print mBox

        if mBox is not 'Cancel':
            print 'NOT COOL'
            cmds.file(newOpenFile, open=True, force=True)


Here is the hotkey definition:

import openFileDialog as ofd

I'll post more questions if I can't figure them out.

Thanks in advance!
Juan Martinez | Blog | Animator
  07 July 2013
Before even looking too much at the script itself:

-Remove customOpen() at the end of it if you are calling it from the module's in the button (if you haven't done it).
-Is the module actually registered somewhere? If not Python will not know to import it (presence in the right directory will usually be enough).
-Convert it to a Maya plugin so you can simply call it as whatever command you will register it as.
Come, Join the Cult - Rigging from First Principles
  07 July 2013
I gave your script a quick test in Maya 2014 and didn't experience your problem with it running every other time. It appeared to display the dialog each time I used the hotkey... except for the first time where it launched the dialog twice due to the exposed customOpen() in the main script. If you want to keep it around for development and testing purposes (e.g. running it from the script editor), wrap it in the following:

if __name__ == "__main__":
This will keep it from excuting when you import the module elsewhere.

I disagree with converting it to a plug-in (especially if you are just getting used to Maya and Python). It will add unnecessary development overhead (with little benefit) and anyone who wants to use it will always need to make sure it is loaded in the Plug-in Manager. Keep it simple.

  07 July 2013
Thanks for the reply and help guys!

This python file is in the scripts folder. I still have the problem of it running every other time. I'm using Maya 2014 on Windows 8.

But I've been having problems overall with scripting. I think it's because I added a MAYA_APP_DIR environment variable to use my Dropbox folder instead of the default Documents folder. This was to allow me to sync hotkeys and other settings between my laptop and desktop. It works very well, except for custom stuff like this seems to have problems.

The Dropbox syncing is something I did for 3ds Max for a long time and it's nice to always have your hotkeys ready. Just tried to emulate that with Maya.

I'll try removing that and see if works any better.
Juan Martinez | Blog | Animator
  07 July 2013
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 02:45 AM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.