Sep 2009
1 / 6
Sep 2009
Sep 2012

Hi,

I am currently trying to let a user file to a directory and I then want to store the directory. To do so I use the fileBrowserDialog. I had difficulty with saving the actual path itself in a string until I came across this code on this site here by user called PythonBoy and I also saw the same tip in other places. Everything works perfectly now but I am a bit cofused and I hope someone can explain the following to me...How does browseForFolderFeedback get the variable $result? How does the fileBrowserDialog save the path??


global proc browseForFolder( string $startFolder )
{
string $mayaFolder;

// Get current directory.. we'll restore this when we're done
$mayaFolder = workspace -q -dir;

// Set the current directory to our own. Note that this will set Maya's workspace folder even if the specified folder does not exist.
workspace -dir $startFolder;

// Present the user with a folder-selection dialog. It will default to $startFolder. The $mayaFolder is included as the first argument for
// the callback.
fileBrowserDialog -mode 4
-fileCommand ( "browseForFolderCallback \"" + $mayaFolder + "\"" )
-actionName "Pick Your Folder";
}

global proc browseForFolderCallback( string $mayaFolder, string $result, string $type )
{
// Do whatever you need to with the $result
print ( "Folder selection: " + $result + "\n" );

// Restore the original folder path
workspace -dir $mayaFolder;
}

Example:

browseForFolder( "C:/projects/gameData/" );

I would appreciate any clarification on this...am I just overlooking a simple programming
principle here or something??

Thanks!!!!

  • created

    Sep '09
  • last reply

    Sep '12
  • 5

    replies

  • 6.3k

    views

  • 1

    user

  • 2

    links

Hi Gangie81,

Not sure I am that less of a newbie than you, but I think I know how to adress your issue:

fileBrowserDialog returns the choosen path, so you want to capture it implicitly by using the action to define the variable $result:

$result=fileBrowserDialog -mode 4
-fileCommand ( "browseForFolderCallback \"" + $mayaFolder + "\"" )
-actionName "Pick Your Folder"
;

You can then indeed do whatever you want with $result (aka the choosen path) !

Hi UrbnCTZn,

Thank you for your reply.Actually when doing that I get $return = 1 (as in I think that the command was successful). The odd thing is the code I quoted works perfectly and $return gives me the correct path... I am just wondering how! Cause I am not really catching $result anywhere, as far as I can see.

Thank you again...I am just a little confused on the inner workings.

9 days later

fileBrowserDialog automatically catches the folder/file and the type and sends it to the procedure you declare, which in this case is "browseForFolderCallback". It will append the two variables (folder/file and type) to whatever you put in the "-fileCommand" flag.

Hope that makes sense!

2 years later

Hey I know this post is old but I have an issue with trying to get a maya 2013 python script to work in Maya2010 and I was hoping to replace the FileDialog2 command with a FileBrowserDialog. This is the closest I've gotten to a solution. Somebody told me to use the old FileDialog but that doesn't let you pick folder (I think?).

The script writes presets into xml files and you need to specify a folder.

I tried to convert the above example script to python to then work on incorporating in the bigger script but I failed at this part : -fileCommand ( "browseForFolderCallback \"" + $mayaFolder + "\"" ), at least I think so because I managed to get it to open the dialogue window but then it says syntax error as soon as I click ok. Unless the error lies in the second function ? I'm totally a newbie at scripting...

Here is what I tried using

import maya.cmds as cmds
def browseFolder(startFolder):
    mayaFolder = cmds.workspace( q=True, dir=True )
    ##   Set the current directory to our own.
    ##   Note that this will set Maya's workspace folder even
    ##   if the specified folder does not exist.
    cmds.workspace (dir=startFolder)
    ##   Present the user with a folder-selection dialog.
    ##   It will default to startFolder.
    ##   The mayaFolder is included as the first argument for the callback.
    cmds.fileBrowserDialog(mode=4, fileCommand="browseForFolderCallback(%s)" %mayaFolder, actionName = "Pick your folder")
def browseForFolderCallback(mayaFolder, result, fileType): # Define 3 parameters
    print "Folder selected is: " + str(result)
browseFolder("C:/")

Anyway feedback would be awesome, ideally I don't need it to print my path out but want it to store the path in a global variable called folder_path (which works with the rest of the script)

Thanks in advance!

Hi Jessica,

The fileBrowserDialog is deprecated according to  the documentation, however it still shows an example using the fileCommand callback http://download.autodesk.com/global/docs/maya2012/en\_us/CommandsPython/fileBrowserDialog.html4 
It also shows you the recommended way of doing things.

import maya.cmds as cmds
 
# Old way:
def importImage( fileName, fileType):
   cmds.file( fileName, i=True );
   return 1
 
cmds.fileBrowserDialog( m=0, fc=importImage, ft='image', an='Import_Image', om='Import' )
 
# Recommended way:
filename = cmds.fileDialog2(fileMode=1, caption="Import Image")
cmds.file( filename[0], i=True );

It is not really neccesary to use callbacks since the code will pause while choosing a directory. When it continues you can check the input and call the needed functions.

So to fix your code above:

import maya.cmds as cmds
#
#old way:
#
def browseForFolderCallback(mayaFolder, fileType = None):
	# fileType is None because we won't be needing it anyway and is there for compatibility
    print "Folder selected is: %s" % str(mayaFolder)
 
def browseFolder(startFolder):
    mayaFolder = cmds.workspace( q=True, dir=True )
    cmds.workspace (dir=startFolder)
    cmds.fileBrowserDialog(mode=4, fileCommand=browseForFolderCallback, actionName = "Pick your folder")
 
browseFolder("C:/")
 
#
#new way:
#
def browseFolder2(startFolder):
	mayaFolder = cmds.workspace(q=True, dir=True)
	cmds.workspace (dir=startFolder)
	mayaFolder = cmds.fileDialog2(fm=3, caption = "Pick your folder")
 
	# When you press cancel or close, mayaFolder would be None and not running this code.
	if mayaFolder:
		browseForFolderCallback(mayaFolder[0])
 
browseFolder2("C:/")