From 5d0595fded699d23c73d9155be877494fefb81f5 Mon Sep 17 00:00:00 2001 From: Dalai Felinto Date: Tue, 25 Apr 2023 20:54:58 +0200 Subject: [PATCH] Suport relative path option per-asset library This option is true by default, but it can be changed for any asset library (that may be using Link as import method). This also fix "Reset to Default Value" for the Import Method since this was originally not using the defaults. Pull Request: https://projects.blender.org/blender/blender/pulls/107345 --- scripts/startup/bl_ui/space_userpref.py | 1 + source/blender/CMakeLists.txt | 1 + .../blender/asset_system/AS_asset_library.hh | 2 ++ .../asset_system/AS_asset_representation.hh | 2 ++ .../intern/asset_library_service.cc | 1 + .../intern/asset_representation.cc | 15 +++++++++ .../blender/blenkernel/BKE_blender_version.h | 2 +- .../blender/blenkernel/intern/preferences.c | 3 +- .../blenloader/intern/versioning_300.cc | 22 +++++++------ .../blenloader/intern/versioning_userdef.c | 6 ++++ .../blender/editors/asset/ED_asset_handle.h | 1 + .../editors/asset/intern/asset_handle.cc | 5 +++ source/blender/makesdna/DNA_asset_types.h | 5 +++ .../blender/makesdna/DNA_userdef_defaults.h | 28 ++++++++++++++++ source/blender/makesdna/DNA_userdef_types.h | 3 +- source/blender/makesdna/intern/dna_defaults.c | 5 +++ source/blender/makesrna/intern/rna_userdef.c | 7 ++++ source/blender/windowmanager/WM_types.h | 1 + .../windowmanager/intern/wm_dragdrop.cc | 33 ++++++++++++------- 19 files changed, 118 insertions(+), 25 deletions(-) create mode 100644 source/blender/makesdna/DNA_userdef_defaults.h diff --git a/scripts/startup/bl_ui/space_userpref.py b/scripts/startup/bl_ui/space_userpref.py index e977b84c220..a9e2c1556f3 100644 --- a/scripts/startup/bl_ui/space_userpref.py +++ b/scripts/startup/bl_ui/space_userpref.py @@ -1491,6 +1491,7 @@ class USERPREF_PT_file_paths_asset_libraries(FilePathsPanel, Panel): active_library = paths.asset_libraries[active_library_index] layout.prop(active_library, "path") layout.prop(active_library, "import_method", text="Import Method") + layout.prop(active_library, "use_relative_path") class USERPREF_UL_asset_libraries(bpy.types.UIList): diff --git a/source/blender/CMakeLists.txt b/source/blender/CMakeLists.txt index 679496ee783..ac9fcd9c80a 100644 --- a/source/blender/CMakeLists.txt +++ b/source/blender/CMakeLists.txt @@ -120,6 +120,7 @@ set(SRC_DNA_DEFAULTS_INC ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_space_defaults.h ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_speaker_defaults.h ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_texture_defaults.h + ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_userdef_defaults.h ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_vec_defaults.h ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_view3d_defaults.h ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_volume_defaults.h diff --git a/source/blender/asset_system/AS_asset_library.hh b/source/blender/asset_system/AS_asset_library.hh index 9621693f4e0..feebbdd21ec 100644 --- a/source/blender/asset_system/AS_asset_library.hh +++ b/source/blender/asset_system/AS_asset_library.hh @@ -67,6 +67,8 @@ class AssetLibrary { * #import_method_ above, it's just a default. */ bool may_override_import_method_ = false; + bool use_relative_path_ = true; + bCallbackFuncStore on_save_callback_store_{}; public: diff --git a/source/blender/asset_system/AS_asset_representation.hh b/source/blender/asset_system/AS_asset_representation.hh index cee77554453..d47b11c222d 100644 --- a/source/blender/asset_system/AS_asset_representation.hh +++ b/source/blender/asset_system/AS_asset_representation.hh @@ -88,6 +88,7 @@ class AssetRepresentation { * #get_import_method(). Also returns true if there is no predefined import method * (when #get_import_method() returns no value). */ bool may_override_import_method() const; + bool get_use_relative_path() const; /** If this asset is stored inside this current file (#is_local_id() is true), this returns the * ID's pointer, otherwise null. */ ID *local_id() const; @@ -109,3 +110,4 @@ std::string AS_asset_representation_full_library_path_get(const ::AssetRepresent std::optional AS_asset_representation_import_method_get( const ::AssetRepresentation *asset_handle); bool AS_asset_representation_may_override_import_method(const ::AssetRepresentation *asset_handle); +bool AS_asset_representation_use_relative_path_get(const ::AssetRepresentation *asset_handle); diff --git a/source/blender/asset_system/intern/asset_library_service.cc b/source/blender/asset_system/intern/asset_library_service.cc index ff9b99d350f..4e181bb4d8f 100644 --- a/source/blender/asset_system/intern/asset_library_service.cc +++ b/source/blender/asset_system/intern/asset_library_service.cc @@ -101,6 +101,7 @@ AssetLibrary *AssetLibraryService::get_asset_library( AssetLibrary *library = get_asset_library_on_disk_custom(custom_library->name, root_path); library->import_method_ = eAssetImportMethod(custom_library->import_method); library->may_override_import_method_ = true; + library->use_relative_path_ = (custom_library->flag & ASSET_LIBRARY_RELATIVE_PATH) != 0; return library; } diff --git a/source/blender/asset_system/intern/asset_representation.cc b/source/blender/asset_system/intern/asset_representation.cc index 2476099a3d2..23cbbe58cf2 100644 --- a/source/blender/asset_system/intern/asset_representation.cc +++ b/source/blender/asset_system/intern/asset_representation.cc @@ -106,6 +106,14 @@ bool AssetRepresentation::may_override_import_method() const return owner_asset_library_->may_override_import_method_; } +bool AssetRepresentation::get_use_relative_path() const +{ + if (!owner_asset_library_) { + return false; + } + return owner_asset_library_->use_relative_path_; +} + ID *AssetRepresentation::local_id() const { return is_local_id_ ? local_asset_id_ : nullptr; @@ -155,6 +163,13 @@ bool AS_asset_representation_may_override_import_method(const AssetRepresentatio return asset->may_override_import_method(); } +bool AS_asset_representation_use_relative_path_get(const AssetRepresentation *asset_handle) +{ + const asset_system::AssetRepresentation *asset = + reinterpret_cast(asset_handle); + return asset->get_use_relative_path(); +} + /* ---------------------------------------------------------------------- */ /** \name C-API * \{ */ diff --git a/source/blender/blenkernel/BKE_blender_version.h b/source/blender/blenkernel/BKE_blender_version.h index cfd38d6167b..ba870cd7882 100644 --- a/source/blender/blenkernel/BKE_blender_version.h +++ b/source/blender/blenkernel/BKE_blender_version.h @@ -25,7 +25,7 @@ extern "C" { /* Blender file format version. */ #define BLENDER_FILE_VERSION BLENDER_VERSION -#define BLENDER_FILE_SUBVERSION 5 +#define BLENDER_FILE_SUBVERSION 6 /* Minimum Blender version that supports reading file written with the current * version. Older Blender versions will test this and show a warning if the file diff --git a/source/blender/blenkernel/intern/preferences.c b/source/blender/blenkernel/intern/preferences.c index 02169cd4535..814390be8f9 100644 --- a/source/blender/blenkernel/intern/preferences.c +++ b/source/blender/blenkernel/intern/preferences.c @@ -24,6 +24,7 @@ #include "BLT_translation.h" +#include "DNA_defaults.h" #include "DNA_userdef_types.h" #define U BLI_STATIC_ASSERT(false, "Global 'U' not allowed, only use arguments passed in!") @@ -37,6 +38,7 @@ bUserAssetLibrary *BKE_preferences_asset_library_add(UserDef *userdef, const char *path) { bUserAssetLibrary *library = MEM_callocN(sizeof(*library), "bUserAssetLibrary"); + memcpy(library, DNA_struct_default_get(bUserAssetLibrary), sizeof(*library)); BLI_addtail(&userdef->asset_libraries, library); @@ -46,7 +48,6 @@ bUserAssetLibrary *BKE_preferences_asset_library_add(UserDef *userdef, if (path) { BLI_strncpy(library->path, path, sizeof(library->path)); } - library->import_method = ASSET_IMPORT_APPEND_REUSE; return library; } diff --git a/source/blender/blenloader/intern/versioning_300.cc b/source/blender/blenloader/intern/versioning_300.cc index f2667f66c09..c846f2f871a 100644 --- a/source/blender/blenloader/intern/versioning_300.cc +++ b/source/blender/blenloader/intern/versioning_300.cc @@ -1349,6 +1349,18 @@ void do_versions_after_linking_300(FileData * /*fd*/, Main *bmain) FOREACH_NODETREE_END; } + if (!MAIN_VERSION_ATLEAST(bmain, 306, 6)) { + LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) { + Editing *ed = SEQ_editing_get(scene); + if (ed == nullptr) { + continue; + } + + SEQ_for_each_callback( + &scene->ed->seqbase, do_versions_sequencer_init_retiming_tool_data, scene); + } + } + /** * Versioning code until next subversion bump goes here. * @@ -1361,16 +1373,6 @@ void do_versions_after_linking_300(FileData * /*fd*/, Main *bmain) */ { /* Keep this block, even when empty. */ - - LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) { - Editing *ed = SEQ_editing_get(scene); - if (ed == nullptr) { - continue; - } - - SEQ_for_each_callback( - &scene->ed->seqbase, do_versions_sequencer_init_retiming_tool_data, scene); - } } } diff --git a/source/blender/blenloader/intern/versioning_userdef.c b/source/blender/blenloader/intern/versioning_userdef.c index 9b7468636cf..702ccca363f 100644 --- a/source/blender/blenloader/intern/versioning_userdef.c +++ b/source/blender/blenloader/intern/versioning_userdef.c @@ -813,6 +813,12 @@ void blo_do_versions_userdef(UserDef *userdef) } } + if (!USER_VERSION_ATLEAST(306, 6)) { + LISTBASE_FOREACH (bUserAssetLibrary *, asset_library, &userdef->asset_libraries) { + asset_library->flag |= ASSET_LIBRARY_RELATIVE_PATH; + } + } + /** * Versioning code until next subversion bump goes here. * diff --git a/source/blender/editors/asset/ED_asset_handle.h b/source/blender/editors/asset/ED_asset_handle.h index 8bfd4f7150e..0c0de6c5d8e 100644 --- a/source/blender/editors/asset/ED_asset_handle.h +++ b/source/blender/editors/asset/ED_asset_handle.h @@ -32,6 +32,7 @@ void ED_asset_handle_get_full_library_path( /* `1090` for #FILE_MAX_LIBEXTRA, * rely on warnings to let us know if this gets out of sync. */ char r_full_lib_path[1090]); +bool ED_asset_handle_get_use_relative_path(const struct AssetHandle *asset); #ifdef __cplusplus } diff --git a/source/blender/editors/asset/intern/asset_handle.cc b/source/blender/editors/asset/intern/asset_handle.cc index 824d5d0ed08..f589710c3d8 100644 --- a/source/blender/editors/asset/intern/asset_handle.cc +++ b/source/blender/editors/asset/intern/asset_handle.cc @@ -68,3 +68,8 @@ void ED_asset_handle_get_full_library_path(const AssetHandle *asset_handle, BLI_strncpy(r_full_lib_path, library_path.c_str(), FILE_MAX); } + +bool ED_asset_handle_get_use_relative_path(const AssetHandle *asset) +{ + return AS_asset_representation_use_relative_path_get(asset->file_data->asset); +} diff --git a/source/blender/makesdna/DNA_asset_types.h b/source/blender/makesdna/DNA_asset_types.h index f984dedaefa..9c4e2e6a31f 100644 --- a/source/blender/makesdna/DNA_asset_types.h +++ b/source/blender/makesdna/DNA_asset_types.h @@ -126,6 +126,11 @@ typedef enum eAssetImportMethod { ASSET_IMPORT_APPEND_REUSE = 2, } eAssetImportMethod; + +typedef enum eAssetLibrary_Flag { + ASSET_LIBRARY_RELATIVE_PATH = (1 << 0), +} eAssetLibrary_Flag; + /** * Information to identify an asset library. May be either one of the predefined types (current * 'Main', builtin library, project library), or a custom type as defined in the Preferences. diff --git a/source/blender/makesdna/DNA_userdef_defaults.h b/source/blender/makesdna/DNA_userdef_defaults.h new file mode 100644 index 00000000000..500abd73696 --- /dev/null +++ b/source/blender/makesdna/DNA_userdef_defaults.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/** \file + * \ingroup DNA + */ + +#pragma once + +#include "DNA_asset_types.h" + +/* Struct members on own line. */ +/* clang-format off */ + +/* -------------------------------------------------------------------- */ +/** \name bUserAssetLibrary Struct + * \{ */ + +#define _DNA_DEFAULT_bUserAssetLibrary \ + { \ + .import_method = ASSET_IMPORT_APPEND_REUSE, \ + .flag = ASSET_LIBRARY_RELATIVE_PATH, \ + } + +/** \} */ + +/* clang-format off */ + +/** \} */ diff --git a/source/blender/makesdna/DNA_userdef_types.h b/source/blender/makesdna/DNA_userdef_types.h index 70aed0eab5b..805c5de70aa 100644 --- a/source/blender/makesdna/DNA_userdef_types.h +++ b/source/blender/makesdna/DNA_userdef_types.h @@ -593,7 +593,8 @@ typedef struct bUserAssetLibrary { char path[1024]; /* FILE_MAX */ short import_method; /* eAssetImportMethod */ - char _pad0[6]; + short flag; /* eAssetLibrary_Flag */ + char _pad0[4]; } bUserAssetLibrary; typedef struct SolidLight { diff --git a/source/blender/makesdna/intern/dna_defaults.c b/source/blender/makesdna/intern/dna_defaults.c index 9ad3fd27974..456306d196d 100644 --- a/source/blender/makesdna/intern/dna_defaults.c +++ b/source/blender/makesdna/intern/dna_defaults.c @@ -132,6 +132,7 @@ #include "DNA_space_defaults.h" #include "DNA_speaker_defaults.h" #include "DNA_texture_defaults.h" +#include "DNA_userdef_defaults.h" #include "DNA_volume_defaults.h" #include "DNA_world_defaults.h" @@ -222,6 +223,9 @@ SDNA_DEFAULT_DECL_STRUCT(Speaker); /* DNA_texture_defaults.h */ SDNA_DEFAULT_DECL_STRUCT(Tex); +/* DNA_userdef_types.h */ +SDNA_DEFAULT_DECL_STRUCT(bUserAssetLibrary); + /* DNA_view3d_defaults.h */ SDNA_DEFAULT_DECL_STRUCT(View3D); @@ -461,6 +465,7 @@ const void *DNA_default_table[SDNA_TYPE_MAX] = { SDNA_DEFAULT_DECL_EX(UserDef_SpaceData, UserDef.space_data), SDNA_DEFAULT_DECL_EX(UserDef_FileSpaceData, UserDef.file_space_data), SDNA_DEFAULT_DECL_EX(WalkNavigation, UserDef.walk_navigation), + SDNA_DEFAULT_DECL(bUserAssetLibrary), /* DNA_view3d_defaults.h */ SDNA_DEFAULT_DECL(View3D), diff --git a/source/blender/makesrna/intern/rna_userdef.c b/source/blender/makesrna/intern/rna_userdef.c index af14bfe70a1..50f34d931bb 100644 --- a/source/blender/makesrna/intern/rna_userdef.c +++ b/source/blender/makesrna/intern/rna_userdef.c @@ -6258,6 +6258,13 @@ static void rna_def_userdef_filepaths_asset_library(BlenderRNA *brna) "Default Import Method", "Determine how the asset will be imported, unless overridden by the Asset Browser"); RNA_def_property_update(prop, 0, "rna_userdef_update"); + + prop = RNA_def_property(srna, "use_relative_path", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_sdna(prop, NULL, "flag", ASSET_LIBRARY_RELATIVE_PATH); + RNA_def_property_ui_text( + prop, + "Relative Path", + "Use relative path when linking assets from this asset library"); } static void rna_def_userdef_script_directory(BlenderRNA *brna) diff --git a/source/blender/windowmanager/WM_types.h b/source/blender/windowmanager/WM_types.h index 14ebbf5cef1..e87e510e12a 100644 --- a/source/blender/windowmanager/WM_types.h +++ b/source/blender/windowmanager/WM_types.h @@ -1100,6 +1100,7 @@ typedef struct wmDragAsset { int id_type; struct AssetMetaData *metadata; int import_method; /* eAssetImportType */ + bool use_relative_path; /* FIXME: This is temporary evil solution to get scene/view-layer/etc in the copy callback of the * #wmDropBox. diff --git a/source/blender/windowmanager/intern/wm_dragdrop.cc b/source/blender/windowmanager/intern/wm_dragdrop.cc index 6575a2fc91d..5b74ee2984f 100644 --- a/source/blender/windowmanager/intern/wm_dragdrop.cc +++ b/source/blender/windowmanager/intern/wm_dragdrop.cc @@ -572,6 +572,7 @@ wmDragAsset *WM_drag_create_asset_data(const AssetHandle *asset, const char *pat asset_drag->path = path; asset_drag->id_type = ED_asset_handle_get_id_type(asset); asset_drag->import_method = import_type; + asset_drag->use_relative_path = ED_asset_handle_get_use_relative_path(asset); return asset_drag; } @@ -625,10 +626,18 @@ ID *WM_drag_asset_id_import(wmDragAsset *asset_drag, const int flag_extra) ViewLayer *view_layer = CTX_data_view_layer(asset_drag->evil_C); View3D *view3d = CTX_wm_view3d(asset_drag->evil_C); + const bool use_relative_path = asset_drag->use_relative_path; + switch (eAssetImportMethod(asset_drag->import_method)) { case ASSET_IMPORT_LINK: - return WM_file_link_datablock( - bmain, scene, view_layer, view3d, asset_drag->path, idtype, name, flag); + return WM_file_link_datablock(bmain, + scene, + view_layer, + view3d, + asset_drag->path, + idtype, + name, + flag | (use_relative_path ? FILE_RELPATH : 0)); case ASSET_IMPORT_APPEND: return WM_file_append_datablock(bmain, scene, @@ -640,16 +649,16 @@ ID *WM_drag_asset_id_import(wmDragAsset *asset_drag, const int flag_extra) flag | BLO_LIBLINK_APPEND_RECURSIVE | BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR); case ASSET_IMPORT_APPEND_REUSE: - return WM_file_append_datablock(G_MAIN, - scene, - view_layer, - view3d, - asset_drag->path, - idtype, - name, - flag | BLO_LIBLINK_APPEND_RECURSIVE | - BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR | - BLO_LIBLINK_APPEND_LOCAL_ID_REUSE); + return WM_file_append_datablock( + G_MAIN, + scene, + view_layer, + view3d, + asset_drag->path, + idtype, + name, + flag | BLO_LIBLINK_APPEND_RECURSIVE | BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR | + BLO_LIBLINK_APPEND_LOCAL_ID_REUSE | (use_relative_path ? FILE_RELPATH : 0)); } BLI_assert_unreachable();