Assets: add function to copy asset data to another ID #108547

Manually merged
Sybren A. Stüvel merged 7 commits from dr.sybren/blender:pr/asset-data-copy-to-id into main 2023-06-08 10:15:03 +02:00
3 changed files with 80 additions and 0 deletions
Showing only changes of commit 2cd1343af0 - Show all commits

View File

@ -38,6 +38,13 @@ typedef struct AssetTypeInfo {
struct AssetMetaData *BKE_asset_metadata_create(void);
void BKE_asset_metadata_free(struct AssetMetaData **asset_data);
/**
* Create a copy of the #AssetMetaData so that it can be assigned to another asset.
*
* The caller becomes the owner of the returned pointer.
*/
struct AssetMetaData *BKE_asset_metadata_copy(const struct AssetMetaData *source);
struct AssetTagEnsureResult {
struct AssetTag *tag;
/* Set to false if a tag of this name was already present. */

View File

@ -39,6 +39,38 @@ void BKE_asset_metadata_free(AssetMetaData **asset_data)
*asset_data = nullptr;
}
AssetMetaData *BKE_asset_metadata_copy(const AssetMetaData *source)
{
AssetMetaData *copy = BKE_asset_metadata_create();
copy->local_type_info = source->local_type_info;
if (source->properties) {
copy->properties = IDP_CopyProperty(source->properties);
}
BKE_asset_metadata_catalog_id_set(copy, source->catalog_id, source->catalog_simple_name);
if (source->author) {
copy->author = BLI_strdup(source->author);
}
if (source->description) {
copy->description = BLI_strdup(source->description);
}
if (source->copyright) {
copy->copyright = BLI_strdup(source->copyright);
}
if (source->license) {
copy->license = BLI_strdup(source->license);
}
BLI_duplicatelist(&copy->tags, &source->tags);
copy->active_tag = source->active_tag;
copy->tot_tags = source->tot_tags;
return copy;
}
AssetMetaData::~AssetMetaData()
{
if (properties) {

View File

@ -33,6 +33,9 @@
# include "RNA_access.h"
# include "WM_api.h"
# include "WM_types.h"
static char *rna_AssetMetaData_path(const PointerRNA *UNUSED(ptr))
{
return BLI_strdup("asset_data");
@ -338,6 +341,32 @@ void rna_AssetMetaData_catalog_id_update(struct bContext *C, struct PointerRNA *
AS_asset_library_refresh_catalog_simplename(asset_library, asset_data);
}
void rna_AssetMetaData_copy_to_id(AssetMetaData *self, ReportList *reports, ID *destination)
{
if (destination == NULL) {

For an RNA callback this has a bit too much knowledge/logic for my taste, I'd prefer having this in some ED_asset_xxx() function. Similar to how we have ED_asset_mark_id()/ED_asset_clear_id().

For an RNA callback this has a bit too much knowledge/logic for my taste, I'd prefer having this in some `ED_asset_xxx()` function. Similar to how we have `ED_asset_mark_id()`/`ED_asset_clear_id()`.
BKE_reportf(reports, RPT_ERROR, "destination '%s' cannot be NULL", destination->name + 2);
return;
}
if (!BKE_id_can_be_asset(destination)) {
BKE_reportf(reports,
RPT_ERROR,
"destination '%s' is of a type that cannot be marked as asset",
destination->name + 2);
return;
}
const bool dest_already_was_asset = destination->asset_data != NULL;
if (destination->asset_data) {
BKE_asset_metadata_free(&destination->asset_data);
}
destination->asset_data = BKE_asset_metadata_copy(self);
const int classification = dest_already_was_asset ? NA_EDITED : NA_ADDED;
WM_main_add_notifier(NC_ASSET | classification, NULL);
WM_main_add_notifier(NC_ID | NA_EDITED, NULL);
}
static PointerRNA rna_AssetHandle_file_data_get(PointerRNA *ptr)
{
AssetHandle *asset_handle = ptr->data;
@ -443,6 +472,8 @@ static void rna_def_asset_data(BlenderRNA *brna)
{
StructRNA *srna;
PropertyRNA *prop;
FunctionRNA *func;
PropertyRNA *parm;
srna = RNA_def_struct(brna, "AssetMetaData", NULL);
RNA_def_struct_path_func(srna, "rna_AssetMetaData_path");
@ -524,6 +555,16 @@ static void rna_def_asset_data(BlenderRNA *brna)
"Catalog Simple Name",
"Simple name of the asset's catalog, for debugging and "
"data recovery purposes");
func = RNA_def_function(srna, "copy_to_id", "rna_AssetMetaData_copy_to_id");
RNA_def_function_ui_description(
func,
"Copy this asset data to another data-block. Note that this does not include the preview "
"image, as that is part of the data-block itself, and not the asset data");
RNA_def_function_flag(func, FUNC_USE_REPORTS);
parm = RNA_def_pointer(
func, "destination", "ID", "destination", "Data-block to copy the asset data to");
RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
}
static void rna_def_asset_handle_api(StructRNA *srna)