[Bf-blender-cvs] [31acdbed95a] asset-metadata: Add custom property support for assets

Julian Eisel noreply at git.blender.org
Mon Nov 23 14:19:15 CET 2020


Commit: 31acdbed95a5e51baf377f06604313463b3f2a00
Author: Julian Eisel
Date:   Mon Nov 23 14:14:55 2020 +0100
Branches: asset-metadata
https://developer.blender.org/rB31acdbed95a5e51baf377f06604313463b3f2a00

Add custom property support for assets

I didn't do this earlier because I was afraid it would make reading of asset
data (and asset data only!) difficult. Mainly because custom properties could
store pointers to other data, that would have to be read too then (e.g. an
object). But turns out, we can easily disable custom pointers to other
data-blocks, so reading stays simple :).

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

M	source/blender/blenkernel/intern/asset.c
M	source/blender/makesdna/DNA_asset_types.h
M	source/blender/makesrna/intern/rna_asset.c

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

diff --git a/source/blender/blenkernel/intern/asset.c b/source/blender/blenkernel/intern/asset.c
index 0f62ee6df1d..427ede47272 100644
--- a/source/blender/blenkernel/intern/asset.c
+++ b/source/blender/blenkernel/intern/asset.c
@@ -26,6 +26,7 @@
 
 #include "BKE_asset.h"
 #include "BKE_icons.h"
+#include "BKE_idprop.h"
 
 #include "DNA_ID.h"
 #include "DNA_asset_types.h"
@@ -44,6 +45,9 @@ AssetData *BKE_asset_data_create(void)
 
 void BKE_asset_data_free(AssetData *asset_data)
 {
+  if (asset_data->properties) {
+    IDP_FreeProperty(asset_data->properties);
+  }
   MEM_SAFE_FREE(asset_data->description);
   BLI_freelistN(&asset_data->tags);
 
@@ -86,6 +90,10 @@ void BKE_assetdata_write(BlendWriter *writer, AssetData *asset_data)
 {
   BLO_write_struct(writer, AssetData, asset_data);
 
+  if (asset_data->properties) {
+    IDP_BlendWrite(writer, asset_data->properties);
+  }
+
   if (asset_data->description) {
     BLO_write_string(writer, asset_data->description);
   }
@@ -98,6 +106,11 @@ void BKE_assetdata_read(BlendDataReader *reader, AssetData *asset_data)
 {
   /* asset_data itself has been read already. */
 
+  if (asset_data->properties) {
+    BLO_read_data_address(reader, &asset_data->properties);
+    IDP_BlendDataRead(reader, &asset_data->properties);
+  }
+
   BLO_read_data_address(reader, &asset_data->description);
   BLO_read_list(reader, &asset_data->tags);
 }
diff --git a/source/blender/makesdna/DNA_asset_types.h b/source/blender/makesdna/DNA_asset_types.h
index ab626d73be0..b1ce2a01785 100644
--- a/source/blender/makesdna/DNA_asset_types.h
+++ b/source/blender/makesdna/DNA_asset_types.h
@@ -33,10 +33,21 @@ typedef struct CustomTag {
   char name[64]; /* MAX_NAME */
 } CustomTag;
 
+/**
+ * \brief The meta-data of an asset.
+ * By creating and giving this for a data-block (#ID.asset_data), the data-block becomes an asset.
+ *
+ * \note This struct must be readable without having to read anything but blocks from the ID it is
+ *       attached to! That way, asset information of a file can be read, without reading anything
+ *       more than that from the file. So pointers to other IDs or ID data are strictly forbidden.
+ */
 typedef struct AssetData {
   /** Thumbnail image of the data-block. Duplicate of the referenced ID preview. */
   struct PreviewImage *preview;
 
+  /** Custom asset meta-data. Cannot store pointers to IDs (#STRUCT_NO_DATABLOCK_IDPROPERTIES)! */
+  struct IDProperty *properties;
+
   /** Optional description of this asset for display in the UI. Dynamic length. */
   char *description;
   /** Optional name of the person that created this asset. */
diff --git a/source/blender/makesrna/intern/rna_asset.c b/source/blender/makesrna/intern/rna_asset.c
index 32725739cde..60eb9ffc30d 100644
--- a/source/blender/makesrna/intern/rna_asset.c
+++ b/source/blender/makesrna/intern/rna_asset.c
@@ -31,6 +31,7 @@
 #ifdef RNA_RUNTIME
 
 #  include "BKE_asset.h"
+#  include "BKE_idprop.h"
 
 #  include "RNA_access.h"
 
@@ -63,6 +64,18 @@ static void rna_AssetData_tag_remove(AssetData *asset_data,
   RNA_POINTER_INVALIDATE(tag_ptr);
 }
 
+static IDProperty *rna_AssetData_idprops(PointerRNA *ptr, bool create)
+{
+  AssetData *asset_data = ptr->data;
+
+  if (create && !asset_data->properties) {
+    IDPropertyTemplate val = {0};
+    asset_data->properties = IDP_New(IDP_GROUP, &val, "RNA_AssetData group");
+  }
+
+  return asset_data->properties;
+}
+
 static void rna_AssetData_description_get(PointerRNA *ptr, char *value)
 {
   AssetData *asset_data = ptr->data;
@@ -152,6 +165,9 @@ static void rna_def_asset_data(BlenderRNA *brna)
   srna = RNA_def_struct(brna, "AssetData", NULL);
   RNA_def_struct_ui_text(srna, "Asset Data", "Additional data stored for an asset data-block");
   //  RNA_def_struct_ui_icon(srna, ICON_ASSET); /* TODO: Icon doesn't exist!. */
+  /* The struct has custom properties, but no pointer properties to other IDs! */
+  RNA_def_struct_idprops_func(srna, "rna_AssetData_idprops");
+  RNA_def_struct_flag(srna, STRUCT_NO_DATABLOCK_IDPROPERTIES); /* Mandatory! */
 
   prop = RNA_def_property(srna, "description", PROP_STRING, PROP_NONE);
   RNA_def_property_string_funcs(prop,



More information about the Bf-blender-cvs mailing list