How to pass default extension to rs.OpenFileName()?

Hi all,

i am not able to pass the default extension to rs.OpenFileName(). How is this done properly ?

import rhinoscriptsyntax as rs

nfilter = "Text (*.txt)|*.txt|STI files (*.sti)|*.sti||"
filename = rs.OpenFileName("File", nfilter, None, None, extension="sti")

it always uses the txt extension, i´ve tried *.sti, .sti, sti or the description, filter strings and both. :confused:


Hi Clement,

IIRC, it always uses the first in the list as default. So put .sti first instead of .txt.

@dale I suppose this is a bug, I don’t quite know what the default file extension argument should do either.


@Helvetosaur, thanks this works! :wink:


Of course, there is always OpenFileDialog.

@dale, thanks. It seems that OpenFileDialog.DefaultExt serves a different purpose than i expected. So i was able to use FilterIndex instead in the example function below.

import System
import os

def OpenFileNameEx(title=None, filter=None, folder=None, filename=None, filter_index=None):

    ofd = System.Windows.Forms.OpenFileDialog()

    if title != None:
        if isinstance(title, str): ofd.Title = title

    if filter != None:
        if isinstance(title, str): ofd.Filter = filter

    if folder != None: 
        if os.path.isdir(folder): ofd.InitialDirectory = folder

    if filename != None:
        if isinstance(filename, str): ofd.FileName = filename

    if filter_index != None:
        if isinstance(filter_index, int): ofd.FilterIndex = filter_index

    if ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK:
        return ofd.FileName