26 November 2006

query a directory name

How can I build a file browser dialog, to select a file that doesn't yet exist? Or to select a directory only?
  • fileBrowserDialog
  • Notes: The docs on this suck, so this is what you need to do:
  • Example:
    • First, built the file brower dialog: -mode is telling it what to do:
      • fileBrowserDialog -mode 2 -fileCommand "procName" -actionName "whatYouAreDoing";
        • -mode 0 is for reading files, so if you type in a name that dosn't exist, the browers will give an error
        • -mode 1 & 2 are pretty much the same and are for for writing to file, so you can speficy your own filename inthe browers.
        • -mode 4 is for selecting directories only.
        • -fileCommand is the name of the below proc, that the fileBrowserDialog? passes its info to.
        • -actionName is simply what is printed in the UI, telling the user what is going on
    • Next, you need to build a proc, that can take the return from the FBD, and then do something with it. It needs to be in the below format, but the proc name, and var names can change.
      • global proc procName(string $result, string $type){
        textFieldButtonGrp -e -tx $result controlName;}
    • So, the fileBrowserDialog passes its info, via the $result argument, into the "procName" proc. Then procName will update controlName with $result.

  • Another issue: By default you can't specify WHERE the dialog will open too. It appears that it bases where it opens on where Maya's current workspace is. SO, to tell it where to go, you'd need to do something like this:
    • // query Maya's current workspace:
      string $mayaWorkspace = `workspace -q -dir`;
      // set the workspace to where you want the dialog to open:
      workspace -dir $myCustomPath;
      // run the dialog code:
      fileBrowserDialog ....
      // and set the workspace back again:
      workspace -dir $mayaWorkspace
  • Annoying!
I find this article to be extremely useful, so I'll get it pasted. The original article is in here