0

I was able to call the file browser from Python by referring to a previous question.

I used the following code.

import bpy

Opens a web browser and prints the filepath to console.

#(To see console.. Window >>> Toggle system console

class OpenBrowser(bpy.types.Operator): bl_idname = "open.browser" bl_label = "Minimum code to open browser & get filepath"

filepath = bpy.props.StringProperty(subtype="FILE_PATH") 
#somewhere to remember the address of the file


def execute(self, context):
    display = "filepath= "+self.filepath  
    print(display) #Prints to console  
    #Window>>>Toggle systen console

    return {'FINISHED'}

def invoke(self, context, event): # See comments at end  [1]

    context.window_manager.fileselect_add(self) 
    #Open browser, take reference to 'self' 
    #read the path to selected file, 
    #put path in declared string type data structure self.filepath

    return {'RUNNING_MODAL'}  
    # Tells Blender to hang on for the slow user input


bpy.utils.register_class(OpenBrowser) #Tell Blender this exists and should be used

[1] In this invoke(self, context, event) is being triggered by the below command

#but in your script you create a button or menu item. When it is clicked

Blender runs invoke() automatically.

#execute(self,context) prints self.filepath as proof it works.. I hope.

bpy.ops.open.browser('INVOKE_DEFAULT')

I want to show only folders in the file browser. But with this code, the user can select both files and folders.

And I don't know how to rename the button in the upper right part of the image.

enter image description here

If I could specify the first folder to open, I would like to do that too.

gncc
  • 153
  • 4
  • 1
    This code is so bad, I really do not understand why the anwer and code you linked to, gets any upvotes at all... and this won't help much in your case. What's your goal? Just selecting a folder? – brockmann Jul 21 '21 at 10:28
  • Yes.My goal is just selecting folder. I want to allow the user to select a folder to save the files. – gncc Jul 21 '21 at 10:45
  • I've tried that code, but it allows me to select "both files and folders". I want to be able to select "only folders". Do you think that's not possible with Blender's Python? – gncc Jul 21 '21 at 11:38
  • 1
    The code only allows to select folders as requested but the files are visible as well because that's a user setting - click the filter icon in the header, enable Folders and the files will disappear, see: https://i.stack.imgur.com/O30PH.png – brockmann Jul 21 '21 at 11:49
  • ... and it is possbile to enable that by default but then you would have to implement a new operator and this will blow up your code a lot for a tiny little feature. IMHO there should be a good reason to do that. – brockmann Jul 21 '21 at 11:57
  • Is it equally difficult to specify in code the directory that will be displayed first when the file browser opens? – gncc Jul 21 '21 at 12:17

0 Answers0