Page MenuHome

Filebrowser: Expose selecting of a file to the Python API
ClosedPublic

Authored by Paul Golter (paulgolter) on Oct 11 2021, 6:00 PM.

Details

Summary

This is a small patch that enables users to select files in the filebrowser via the Python API.

In Python it would look like this: filebrowser_area.spaces.active.select_file(relative_path="path/to/my/file")

It is based on ED_fileselect_activate_by_id. It's more or less a stripped down copy of it.

As I am not very experienced with exposing stuff to the Python API I don't know if I forgot something. In that case let me know!

Diff Detail

Event Timeline

Paul Golter (paulgolter) requested review of this revision.Oct 11 2021, 6:00 PM
Paul Golter (paulgolter) created this revision.
Julian Eisel (Severin) requested changes to this revision.Oct 11 2021, 6:34 PM
Julian Eisel (Severin) added inline comments.
source/blender/editors/space_file/filesel.c
547

A function called select_file() should not deselect other files. At least not without being very explicit about it.
I'd suggest adding a separate deselect_all() function for that which can just call file_select_deselect_all().

550–551

Use WM_main_add_notifier(NC_SPACE | ND_SPACE_FILE_PARAMS, NULL); here, since this is not asset related.

source/blender/makesrna/intern/rna_space_api.c
141

Better remove the empty line so parameter definitions are visually grouped with the function they are for.
Ignore the fact that the code above doesn't follow that.

142

No need to assign to parm, it's not used.

142

Sub-type should be PROP_FILEPATH, otherwise Unicode characters won't be handled properly.

This revision now requires changes to proceed.Oct 11 2021, 6:34 PM

Hey Julian! Thanks a lot for the feedback.
Totally makes sense to have two different functions to select and deselect. For that I added the ED_fileselect_deselect_all function which calls file_select_deselect_all and WM_main_add_notifier. I hope that's what you imagined. Also fixed the other remarks, if there is anything let me know!

Paul Golter (paulgolter) marked 5 inline comments as done.

Another update after I found out I can get Blender to crash if I create a new filebrowser area via a python operator and immediately try to activate a file.
The solution was to add the file_on_reload_callback_register similiar as ED_fileselect_activate_by_id does it.
Also using filelist_file() inside of the for loop to retrieve the file, otherwise it would be a null pointer.

In addition some renaming of the function names.

Looks good :) Just a few minor things to tweak, no need for a re-review after these have been done.

source/blender/editors/space_file/filesel.c
540

"... that the to-be-activated file at relative_path will only..."

552

We avoid these empty lines after ifs :)

source/blender/makesrna/intern/rna_space_api.c
143

Should be: "... to the current File Browser directory"

This revision is now accepted and ready to land.Oct 12 2021, 6:05 PM
Paul Golter (paulgolter) marked 3 inline comments as done.

Sweet, here the final diff, ready to be merged.