diff --git a/source/blender/blenkernel/BKE_context.h b/source/blender/blenkernel/BKE_context.h index 416947e0b62..8917580689d 100644 --- a/source/blender/blenkernel/BKE_context.h +++ b/source/blender/blenkernel/BKE_context.h @@ -23,6 +23,9 @@ * \ingroup bke */ +/* XXX temporary, until AssetHandle is designed properly and queries can return a pointer to it. */ +#include "DNA_asset_types.h" + #include "DNA_listBase.h" #include "DNA_object_enums.h" #include "RNA_types.h" @@ -358,6 +361,7 @@ int CTX_data_editable_gpencil_layers(const bContext *C, ListBase *list); int CTX_data_editable_gpencil_strokes(const bContext *C, ListBase *list); const struct AssetLibraryReference *CTX_wm_asset_library(const bContext *C); +struct AssetHandle CTX_wm_asset_handle(const bContext *C, bool *r_is_valid); bool CTX_wm_interface_locked(const bContext *C); diff --git a/source/blender/blenkernel/intern/context.c b/source/blender/blenkernel/intern/context.c index 4c91a3f3387..dced945bea0 100644 --- a/source/blender/blenkernel/intern/context.c +++ b/source/blender/blenkernel/intern/context.c @@ -1453,6 +1453,31 @@ const AssetLibraryReference *CTX_wm_asset_library(const bContext *C) return ctx_data_pointer_get(C, "asset_library"); } +AssetHandle CTX_wm_asset_handle(const bContext *C, bool *r_is_valid) +{ + AssetHandle *asset_handle_p = + (AssetHandle *)CTX_data_pointer_get_type(C, "asset_handle", &RNA_AssetHandle).data; + if (asset_handle_p) { + *r_is_valid = true; + return *asset_handle_p; + } + + /* If the asset handle was not found in context directly, try if there's an active file with + * asset data there instead. Not nice to have this here, would be better to have this in + * `ED_asset.h`, but we can't include that in BKE. Even better would be not needing this at all + * and being able to have editors return this in the usual `context` callback. But that would + * require returning a non-owning pointer, which we don't have in the Asset Browser (yet). */ + FileDirEntry *file = + (FileDirEntry *)CTX_data_pointer_get_type(C, "active_file", &RNA_FileSelectEntry).data; + if (file && file->asset_data) { + *r_is_valid = true; + return (AssetHandle){.file_data = file}; + } + + *r_is_valid = false; + return (AssetHandle){0}; +} + Depsgraph *CTX_data_depsgraph_pointer(const bContext *C) { Main *bmain = CTX_data_main(C); diff --git a/source/blender/editors/asset/asset_edit.cc b/source/blender/editors/asset/asset_edit.cc index 5ae07a54b11..949dc1890ad 100644 --- a/source/blender/editors/asset/asset_edit.cc +++ b/source/blender/editors/asset/asset_edit.cc @@ -23,6 +23,8 @@ #include "BKE_lib_id.h" #include "DNA_ID.h" +#include "DNA_asset_types.h" +#include "DNA_space_types.h" #include "UI_interface_icons.h" @@ -118,3 +120,8 @@ AssetLibraryReference ED_asset_library_reference_from_enum_value(int value) } return library; } + +const char *ED_asset_handle_get_name(const AssetHandle *asset) +{ + return asset->file_data->name; +} diff --git a/source/blender/editors/include/ED_asset.h b/source/blender/editors/include/ED_asset.h index a2ef0755fbd..da0809f2e33 100644 --- a/source/blender/editors/include/ED_asset.h +++ b/source/blender/editors/include/ED_asset.h @@ -35,6 +35,8 @@ bool ED_asset_can_make_single_from_context(const struct bContext *C); int ED_asset_library_reference_to_enum_value(const struct AssetLibraryReference *library); struct AssetLibraryReference ED_asset_library_reference_from_enum_value(int value); +const char *ED_asset_handle_get_name(const AssetHandle *asset); + void ED_operatortypes_asset(void); #ifdef __cplusplus diff --git a/source/blender/makesdna/DNA_asset_types.h b/source/blender/makesdna/DNA_asset_types.h index 5acdc4ee862..9b17bca2ab6 100644 --- a/source/blender/makesdna/DNA_asset_types.h +++ b/source/blender/makesdna/DNA_asset_types.h @@ -97,6 +97,17 @@ typedef struct AssetLibraryReference { int custom_library_index; } AssetLibraryReference; +/** + * Not part of the core design, we should try to get rid of it. Only needed to wrap FileDirEntry + * into a type with PropertyGroup as base, so we can have an RNA collection of #AssetHandle's to + * pass to the UI. + */ +# +# +typedef struct AssetHandle { + struct FileDirEntry *file_data; +} AssetHandle; + #ifdef __cplusplus } #endif diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index 52bf80ee45b..97615016016 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -71,6 +71,7 @@ extern StructRNA RNA_ArrayGpencilModifier; extern StructRNA RNA_ArrayModifier; extern StructRNA RNA_Attribute; extern StructRNA RNA_AttributeGroup; +extern StructRNA RNA_AssetHandle; extern StructRNA RNA_AssetLibraryReference; extern StructRNA RNA_AssetMetaData; extern StructRNA RNA_AssetTag; diff --git a/source/blender/makesrna/intern/rna_asset.c b/source/blender/makesrna/intern/rna_asset.c index 54cc87b4eea..82430d3e174 100644 --- a/source/blender/makesrna/intern/rna_asset.c +++ b/source/blender/makesrna/intern/rna_asset.c @@ -125,6 +125,20 @@ static void rna_AssetMetaData_active_tag_range( *max = *softmax = MAX2(asset_data->tot_tags - 1, 0); } +static PointerRNA rna_AssetHandle_file_data_get(PointerRNA *ptr) +{ + AssetHandle *asset_handle = ptr->data; + return rna_pointer_inherit_refine(ptr, &RNA_FileSelectEntry, asset_handle->file_data); +} + +static void rna_AssetHandle_file_data_set(PointerRNA *ptr, + PointerRNA value, + struct ReportList *UNUSED(reports)) +{ + AssetHandle *asset_handle = ptr->data; + asset_handle->file_data = value.data; +} + int rna_asset_library_reference_get(const AssetLibraryReference *library) { return ED_asset_library_reference_to_enum_value(library); @@ -278,6 +292,23 @@ static void rna_def_asset_data(BlenderRNA *brna) RNA_def_property_ui_text(prop, "Active Tag", "Index of the tag set for editing"); } +static void rna_def_asset_handle(BlenderRNA *brna) +{ + StructRNA *srna; + PropertyRNA *prop; + + srna = RNA_def_struct(brna, "AssetHandle", "PropertyGroup"); + RNA_def_struct_ui_text(srna, "Asset Handle", "Reference to some asset"); + + /* TODO why is this editable? There probably shouldn't be a setter. */ + prop = RNA_def_property(srna, "file_data", PROP_POINTER, PROP_NONE); + RNA_def_property_flag(prop, PROP_EDITABLE); + RNA_def_property_struct_type(prop, "FileSelectEntry"); + RNA_def_property_pointer_funcs( + prop, "rna_AssetHandle_file_data_get", "rna_AssetHandle_file_data_set", NULL, NULL); + RNA_def_property_ui_text(prop, "File Entry", "File data used to refer to the asset"); +} + static void rna_def_asset_library_reference(BlenderRNA *brna) { StructRNA *srna = RNA_def_struct(brna, "AssetLibraryReference", NULL); @@ -306,6 +337,7 @@ void RNA_def_asset(BlenderRNA *brna) rna_def_asset_tag(brna); rna_def_asset_data(brna); rna_def_asset_library_reference(brna); + rna_def_asset_handle(brna); RNA_define_animate_sdna(true); } diff --git a/source/blender/makesrna/intern/rna_context.c b/source/blender/makesrna/intern/rna_context.c index 4079406e64b..9da08de2168 100644 --- a/source/blender/makesrna/intern/rna_context.c +++ b/source/blender/makesrna/intern/rna_context.c @@ -58,6 +58,8 @@ const EnumPropertyItem rna_enum_context_mode_items[] = { #ifdef RNA_RUNTIME +# include "DNA_asset_types.h" + # ifdef WITH_PYTHON # include "BPY_extern.h" # endif @@ -134,6 +136,20 @@ static PointerRNA rna_Context_gizmo_group_get(PointerRNA *ptr) return newptr; } +static PointerRNA rna_Context_asset_file_handle_get(PointerRNA *ptr) +{ + bContext *C = (bContext *)ptr->data; + bool is_handle_valid; + AssetHandle asset_handle = CTX_wm_asset_handle(C, &is_handle_valid); + if (!is_handle_valid) { + return PointerRNA_NULL; + } + + PointerRNA newptr; + RNA_pointer_create(NULL, &RNA_FileSelectEntry, asset_handle.file_data, &newptr); + return newptr; +} + static PointerRNA rna_Context_main_get(PointerRNA *ptr) { bContext *C = (bContext *)ptr->data; @@ -281,6 +297,17 @@ void RNA_def_context(BlenderRNA *brna) RNA_def_property_struct_type(prop, "GizmoGroup"); RNA_def_property_pointer_funcs(prop, "rna_Context_gizmo_group_get", NULL, NULL, NULL); + /* TODO can't expose AssetHandle, since there is no permanent storage to it (so we can't + * return a pointer). Instead provide the FileDirEntry pointer it wraps. */ + prop = RNA_def_property(srna, "asset_file_handle", PROP_POINTER, PROP_NONE); + RNA_def_property_clear_flag(prop, PROP_EDITABLE); + RNA_def_property_struct_type(prop, "FileSelectEntry"); + RNA_def_property_pointer_funcs(prop, "rna_Context_asset_file_handle_get", NULL, NULL, NULL); + RNA_def_property_ui_text(prop, + "", + "The file of an active asset. Avoid using this, it will be replaced by " + "a proper AssetHandle design"); + /* Data */ prop = RNA_def_property(srna, "blend_data", PROP_POINTER, PROP_NONE); RNA_def_property_clear_flag(prop, PROP_EDITABLE);