BrowseForFolder alternative for more efficient browsing?


#1

BrowseForFolder creates this window with a limited interface to browse for a location. Is there an alternative in Python to get a window with better options for browsing to a folder location? Like, for example the browse window you get when you use ViewCaptureToFile.


(Dale Fugier) #2

Hi @siemen,

What you see is the Windows-standard folder picker. The dialog displayed by ViewCaptureToFile is the Windows-standard file picker. Now, there may some way to configure a OpenFileDialog class object to allow you to pick a folder. You’d need to do some Googling on this.

That said, the Microsoft.WindowsAPICodePack does include a dialog, similar to the Windows-standard file open dialog, that picks folders. Rhino 5 includes the Microsoft.WindowsAPICodePack, so referencing it isn’t too difficult.

import Rhino
import clr
import sys
sys.path.append(r'C:\Program Files\Rhinoceros 5 (64-bit)\System')
clr.AddReference('Microsoft.WindowsAPICodePack.Shell.dll')
import Microsoft.WindowsAPICodePack.Dialogs as msdlg

dialog = msdlg.CommonOpenFileDialog()
dialog.InitialDirectory = Rhino.ApplicationSettings.FileSettings.WorkingFolder
dialog.IsFolderPicker = True
if (dialog.ShowDialog() == msdlg.CommonFileDialogResult.Ok):
    print dialog.FileName

Note, Rhino 6 does not include the Microsoft.WindowsAPICodePack. Thus, you will need to download it and put someplace that your scripts can reference.

– Dale


(qythium) #3

You could also use System.Windows.Forms.FolderBrowserDialog() which doesn’t require extra dependencies and works on both Windows and Mac:

from System.Windows.Forms import DialogResult, FolderBrowserDialog

fbd = FolderBrowserDialog()
dialog_result = fbd.ShowDialog()
if dialog_result == DialogResult.OK:
    print fbd.SelectedPath

#4

@qythium, but isn’t this what rs.BrowseForFolder uses ?

_
c.


(qythium) #5

Whoops, my mistake - I had tested it on a Mac and it looked so significantly different from the above screenshot I assumed it was another interface altogether


#6

@qythium it is cool though to know this works on Mac which i have not expected :wink:

_
c.


(qythium) #7

Another cross-platform solution using Eto :slight_smile: :

import clr
clr.AddReferenceByName('Eto')
import Eto
folder_diag = Eto.Forms.SelectFolderDialog()
folder_diag.ShowDialog(None)
print folder_diag.Directory

#8

Thanks Dale for your reply.

As a workaround for Rhino6 (without having to download anything), Would it be possible to somehow run the ViewCaptureToFile command, browse for the folder location without completing the command and then retrieve that chosen folder for batch exporting? (or batch viewcapturetofile’ing?)


#9

Awesome, thanks!


(Dale Fugier) #10

Hi @siemen,

The ViewCaptureToFile command obtains a file name using RhinoGet.GetFileName.

– Dale


#11

Thanks again for this qythium, this seems to work quite well. The only thing I’m wondering about is how to cancel the command using the escape key when the browse window is active? Usually, with rs.Get() methods I solve this by checking if the output is equal to none to return, but that doesn’t seem to work?


(qythium) #12

You would check for the return value of the ShowDialog() function, which returns a DialogResult similar to winforms:

if folder_dlg.ShowDialog(None) == Eto.Forms.DialogResult.Cancel:
    return
#do something

#13

Thanks, this works great again.
For these kind of things I just have no idea how/where to look for them so your help is much appreciated.