How to browse files using a EditorPlugin?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Icaro-Lima

My script:

tool
extends EditorPlugin

func _enter_tree():
    # Initialization of the plugin goes here
	add_tool_menu_item('Find Texture Borders', self, '_do_work')

func _do_work(ud):
    var res_path = ?? # res://themes/288dpi/yellow_button00.png
	var texture = load(res_path)

I need something like a FileDialog that works in EditorScript and returns a path inside my project’s file system.

:bust_in_silhouette: Reply From: Zylann

You must use a FileDialog node. When using plugins, dialogs must be child of a specific node called the “base control”, I think it’s what makes them take the editor theme as well. Then signals are used.

var _file_dialog

func _enter_tree():

    add_tool_menu_item('Find Texture Borders', self, '_do_work')

	_file_dialog = FileDialog.new()
	_file_dialog.mode = FileDialog.MODE_OPEN_FILE
	_file_dialog.access = FileDialog.ACCESS_RESOURCES
	# ... more config
	_file_dialog.connect("file_selected", self, "_on_FileDialog_file_selected")

	var editor_interface = get_editor_interface()
	var base_control = editor_interface.get_base_control()
	base_control.add_child(_file_dialog)

	# Other setup code...


func _exit_tree():

	# Cleanup
	_file_dialog.queue_free()


func _do_work(ud):
	_file_dialog.popup_centered_ratio()


func _on_FileDialog_file_selected(path):
    var texture = load(res_path)
	# Continue work using selected path

Worked perfectly! I could never have done it alone!

I was curious about the nomenclature you used in “_file_dialog”, I think I will adopt. Where did you see?

Icaro-Lima | 2019-12-16 14:32

It’s a convention for private class members: GDScript style guide — Godot Engine (3.1) documentation in English

Zylann | 2019-12-16 18:32

I tried to implement this to my program, and Godot told me that get_editor_interface isn’t defined. I did some searching and found that it can only be used if you’re using plugins. Do you need plugins to let the user upload files to the exported product, or can it be done without them? If you need them, how do you implement them, and what exactly do you need? Could you please point me to some tutorials where i could start? I tried to look something up, but it’s all very confusing, i have no idea where to start…

mymokol | 2021-10-29 19:38