[Bf-blender-cvs] [240345842dd] master: Filebrowser: Expose file select functions for File Browser to Python API.

Paul Golter noreply at git.blender.org
Thu Oct 14 14:42:57 CEST 2021


Commit: 240345842dd1594709433df1ff3448c55968254d
Author: Paul Golter
Date:   Thu Oct 14 14:40:48 2021 +0200
Branches: master
https://developer.blender.org/rB240345842dd1594709433df1ff3448c55968254d

Filebrowser: Expose file select functions for File Browser to Python API.

This patch adds activate_file_by_relative_path(relative_path="") and
deselect_all() function to the space api of the File Browser. While the
first sets the active file and adds it to the selection based on a
relative path to the current File Browser directory the second one
deselects all files but does not change the active file.

Differential Revision: https://developer.blender.org/D12826
Reviewed by: Julian Eisel

===================================================================

M	source/blender/editors/include/ED_fileselect.h
M	source/blender/editors/space_file/filesel.c
M	source/blender/makesrna/intern/rna_space_api.c

===================================================================

diff --git a/source/blender/editors/include/ED_fileselect.h b/source/blender/editors/include/ED_fileselect.h
index 423d619f41a..3beabaf2d1d 100644
--- a/source/blender/editors/include/ED_fileselect.h
+++ b/source/blender/editors/include/ED_fileselect.h
@@ -151,6 +151,9 @@ void ED_fileselect_activate_by_id(struct SpaceFile *sfile,
                                   struct ID *asset_id,
                                   const bool deferred);
 
+void ED_fileselect_deselect_all(struct SpaceFile *sfile);
+void ED_fileselect_activate_by_relpath(struct SpaceFile *sfile, const char *relative_path);
+
 void ED_fileselect_window_params_get(const struct wmWindow *win,
                                      int win_size[2],
                                      bool *is_maximized);
diff --git a/source/blender/editors/space_file/filesel.c b/source/blender/editors/space_file/filesel.c
index 83b33fe8aa9..fc9fc6799e1 100644
--- a/source/blender/editors/space_file/filesel.c
+++ b/source/blender/editors/space_file/filesel.c
@@ -528,6 +528,43 @@ void ED_fileselect_activate_by_id(SpaceFile *sfile, ID *asset_id, const bool def
   WM_main_add_notifier(NC_ASSET | NA_SELECTED, NULL);
 }
 
+static void on_reload_select_by_relpath(SpaceFile *sfile, onReloadFnData custom_data)
+{
+  char *relative_path = (char *)custom_data;
+  ED_fileselect_activate_by_relpath(sfile, relative_path);
+}
+
+void ED_fileselect_activate_by_relpath(SpaceFile *sfile, const char *relative_path)
+{
+  /* If there are filelist operations running now ("pending" true) or soon ("force reset" true),
+   * there is a fair chance that the to-be-activated file at relative_path will only be present
+   * after these operations have completed. Defer activation until then. */
+  struct FileList *files = sfile->files;
+  if (files == NULL || filelist_pending(files) || filelist_needs_force_reset(files)) {
+    file_on_reload_callback_register(sfile, on_reload_select_by_relpath, relative_path);
+    return;
+  }
+
+  FileSelectParams *params = ED_fileselect_get_active_params(sfile);
+  const int num_files_filtered = filelist_files_ensure(files);
+
+  for (int file_index = 0; file_index < num_files_filtered; ++file_index) {
+    const FileDirEntry *file = filelist_file(files, file_index);
+
+    if (STREQ(file->relpath, relative_path)) {
+      params->active_file = file_index;
+      filelist_entry_select_set(files, file, FILE_SEL_ADD, FILE_SEL_SELECTED, CHECK_ALL);
+    }
+  }
+  WM_main_add_notifier(NC_SPACE | ND_SPACE_FILE_PARAMS, NULL);
+}
+
+void ED_fileselect_deselect_all(SpaceFile *sfile)
+{
+  file_select_deselect_all(sfile, FILE_SEL_SELECTED);
+  WM_main_add_notifier(NC_SPACE | ND_SPACE_FILE_PARAMS, NULL);
+}
+
 /* The subset of FileSelectParams.flag items we store into preferences. Note that FILE_SORT_ALPHA
  * may also be remembered, but only conditionally. */
 #define PARAMS_FLAGS_REMEMBERED (FILE_HIDE_DOT)
diff --git a/source/blender/makesrna/intern/rna_space_api.c b/source/blender/makesrna/intern/rna_space_api.c
index 205a88edf84..295ecf590bb 100644
--- a/source/blender/makesrna/intern/rna_space_api.c
+++ b/source/blender/makesrna/intern/rna_space_api.c
@@ -134,6 +134,18 @@ void RNA_api_space_filebrowser(StructRNA *srna)
       0,
       "",
       "Whether to activate the ID immediately (false) or after the file browser refreshes (true)");
+
+  /* Select file by relative path. */
+  func = RNA_def_function(
+      srna, "activate_file_by_relative_path", "ED_fileselect_activate_by_relpath");
+  RNA_def_function_ui_description(func,
+                                  "Set active file and add to selection based on relative path to "
+                                  "current File Browser directory");
+  RNA_def_property(func, "relative_path", PROP_STRING, PROP_FILEPATH);
+
+  /* Deselect all files. */
+  func = RNA_def_function(srna, "deselect_all", "ED_fileselect_deselect_all");
+  RNA_def_function_ui_description(func, "Deselect all files");
 }
 
 #endif



More information about the Bf-blender-cvs mailing list