Commit: 10733b356d8a0878aa48c80f8182929a5ad06d3e
Author: Bastien Montagne
Date:   Mon Feb 12 12:42:33 2018 +0100
Branches: asset-engine
https://developer.blender.org/rB10733b356d8a0878aa48c80f8182929a5ad06d3e

Add last missing piece of asset engine API, load_post callback.

This allows asset engine to perform whatever they need on data-blocks
(either linked or appended ones), once they have been added to Blender
data (Main).

This is an optional callback intended for advanced/complex asset
management (and possibly also for generative procesing), basic asset
engines should typically not need it.

Amber uses it just to do some dummy testing prints right now.

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

M       release/scripts/modules/amber/engine.py
M       source/blender/blenkernel/BKE_asset_engine.h
M       source/blender/makesdna/DNA_ID.h
M       source/blender/makesrna/intern/rna_asset.c
M       source/blender/windowmanager/intern/wm_files_link.c

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

diff --git a/release/scripts/modules/amber/engine.py 
b/release/scripts/modules/amber/engine.py
index 5130a168b82..58464a0063c 100644
--- a/release/scripts/modules/amber/engine.py
+++ b/release/scripts/modules/amber/engine.py
@@ -495,6 +495,10 @@ class AssetEngineAmber(AssetEngine):
         entries.root_path = ""
         return True
 
+    def load_post(self, context, uuids):
+        print("load_post: active object:", context.object.name if 
context.object else "<NONE>")
+        print("load_post: for", ", ".join(uuid.id.name for uuid in 
uuids.uuids))
+
     def check_dir(self, entries, do_change):
         while do_change and not os.path.exists(entries.root_path):
             entries.root_path = 
os.path.normpath(os.path.join(entries.root_path, ".."))
diff --git a/source/blender/blenkernel/BKE_asset_engine.h 
b/source/blender/blenkernel/BKE_asset_engine.h
index 9f8276b6329..65cafafa03c 100644
--- a/source/blender/blenkernel/BKE_asset_engine.h
+++ b/source/blender/blenkernel/BKE_asset_engine.h
@@ -41,6 +41,7 @@ extern "C" {
 struct AssetEngine;
 struct AssetEngineType;
 struct AssetUUIDList;
+struct bContext;
 struct FileDirEntryArr;
 struct FileDirEntry;
 struct FileDirEntryVariant;
@@ -160,7 +161,7 @@ typedef bool (*ae_load_pre)(struct AssetEngine *engine, 
struct AssetUUIDList *uu
 /* 'post-loading' hook, called after opening/appending/linking/updating given 
entries.
  * E.g. allows an advanced engine to make fancy scripted operations over 
loaded items. */
 /* TODO */
-typedef bool (*ae_load_post)(struct AssetEngine *engine, struct ID *items, 
const int *num_items);
+typedef bool (*ae_load_post)(struct bContext *C, struct AssetEngine *engine,  
struct AssetUUIDList *uuids);
 
 /* Check if given dirpath is valid for current asset engine, it can also 
modify it if do_change is true.
  * r_dir is assumed to be least FILE_MAX.
diff --git a/source/blender/makesdna/DNA_ID.h b/source/blender/makesdna/DNA_ID.h
index a0d9f7aa6f6..2b8d366713c 100644
--- a/source/blender/makesdna/DNA_ID.h
+++ b/source/blender/makesdna/DNA_ID.h
@@ -206,6 +206,9 @@ typedef struct AssetUUID {
        short width;
        short height;
        char *ibuff;  /* RGBA 8bits. */
+
+       /* Used for load_post... */
+       struct ID *id;
 } AssetUUID;
 
 /**
diff --git a/source/blender/makesrna/intern/rna_asset.c 
b/source/blender/makesrna/intern/rna_asset.c
index e8e59062d06..7ab18692df6 100644
--- a/source/blender/makesrna/intern/rna_asset.c
+++ b/source/blender/makesrna/intern/rna_asset.c
@@ -700,6 +700,34 @@ static bool rna_ae_load_pre(AssetEngine *engine, 
AssetUUIDList *uuids, struct Fi
        return ret_success;
 }
 
+static bool rna_ae_load_post(bContext *C, AssetEngine *engine, AssetUUIDList 
*uuids)
+{
+       extern FunctionRNA rna_AssetEngine_load_post_func;
+       PointerRNA ptr;
+       PropertyRNA *parm;
+       ParameterList list;
+       FunctionRNA *func;
+
+       void *ret;
+       bool ret_success;
+
+       RNA_pointer_create(NULL, engine->type->ext.srna, engine, &ptr);
+       func = &rna_AssetEngine_load_post_func;
+
+       RNA_parameter_list_create(&list, &ptr, func);
+       RNA_parameter_set_lookup(&list, "context", &C);
+       RNA_parameter_set_lookup(&list, "uuids", &uuids);
+       engine->type->ext.call(NULL, &ptr, func, &list);
+
+       parm = RNA_function_find_parameter(NULL, func, "success_return");
+       RNA_parameter_get(&list, parm, &ret);
+       ret_success = ((*(int *)ret) != 0);
+
+       RNA_parameter_list_free(&list);
+
+       return ret_success;
+}
+
 static bool rna_ae_check_dir(AssetEngine *engine, char *r_dir, bool do_change)
 {
        extern FunctionRNA rna_AssetEngine_check_dir_func;
@@ -851,7 +879,7 @@ static StructRNA *rna_AssetEngine_register(Main *bmain, 
ReportList *reports, voi
        AssetEngineType *aet, dummyaet = {NULL};
        AssetEngine dummyengine = {NULL};
        PointerRNA dummyptr;
-       int have_function[12];
+       int have_function[13];
 
        /* setup dummy engine & engine type to store static properties in */
        dummyengine.type = &dummyaet;
@@ -898,11 +926,13 @@ static StructRNA *rna_AssetEngine_register(Main *bmain, 
ReportList *reports, voi
 
        aet->load_pre = (have_function[7]) ? rna_ae_load_pre : NULL;
 
-       aet->check_dir = (have_function[8]) ? rna_ae_check_dir : NULL;
+       aet->load_post = (have_function[8]) ? rna_ae_load_post : NULL;
+
+       aet->check_dir = (have_function[9]) ? rna_ae_check_dir : NULL;
 
-       aet->sort_filter = (have_function[9]) ? rna_ae_sort_filter : NULL;
-       aet->entries_block_get = (have_function[10]) ? rna_ae_entries_block_get 
: NULL;
-       aet->entries_uuid_get = (have_function[11]) ? rna_ae_entries_uuid_get : 
NULL;
+       aet->sort_filter = (have_function[10]) ? rna_ae_sort_filter : NULL;
+       aet->entries_block_get = (have_function[11]) ? rna_ae_entries_block_get 
: NULL;
+       aet->entries_uuid_get = (have_function[12]) ? rna_ae_entries_uuid_get : 
NULL;
 
        BLI_addtail(&asset_engines, aet);
 
@@ -1033,6 +1063,11 @@ static void rna_def_asset_uuid(BlenderRNA *brna)
        RNA_def_property_ui_text(prop, "Preview Pixels", "Preview pixels, as 
bytes (always RGBA 32bits)");
        RNA_def_property_dynamic_array_funcs(prop, 
"rna_AssetUUID_preview_pixels_get_length");
        RNA_def_property_int_funcs(prop, "rna_AssetUUID_preview_pixels_get", 
"rna_AssetUUID_preview_pixels_set", NULL);
+
+       prop = RNA_def_property(srna, "id", PROP_POINTER, PROP_NONE);
+       RNA_def_property_struct_type(prop, "ID");
+       RNA_def_property_flag(prop, PROP_PTR_NO_OWNERSHIP);
+       RNA_def_property_ui_text(prop, "Loaded ID", "Pointer to linked/appended 
data-block, for load_post callback only");
 }
 
 static void rna_def_asset_uuid_list(BlenderRNA *brna)
@@ -1544,6 +1579,15 @@ static void rna_def_asset_engine(BlenderRNA *brna)
        parm = RNA_def_boolean(func, "success_return", false, "", "Success");
        RNA_def_function_output(func, parm);
 
+       /* Post-load callback */
+       func = RNA_def_function(srna, "load_post", NULL);
+       RNA_def_function_ui_description(func, "Post-process loaded assets 
data-blocks");
+       RNA_def_function_flag(func, FUNC_REGISTER_OPTIONAL | FUNC_ALLOW_WRITE);
+       RNA_def_pointer(func, "context", "Context", "", "Current Blender 
context");
+       RNA_def_pointer(func, "uuids", "AssetUUIDList", "", "Identifiers and 
data-blocks of loaded assets");
+       parm = RNA_def_boolean(func, "success_return", false, "", "Success");
+       RNA_def_function_output(func, parm);
+
        /* Dir-validating callback */
        func = RNA_def_function(srna, "check_dir", NULL);
        RNA_def_function_ui_description(func, "Check if given path is valid (as 
in, can be listed) for this engine");
diff --git a/source/blender/windowmanager/intern/wm_files_link.c 
b/source/blender/windowmanager/intern/wm_files_link.c
index 7b2e13b6984..b9a301613a0 100644
--- a/source/blender/windowmanager/intern/wm_files_link.c
+++ b/source/blender/windowmanager/intern/wm_files_link.c
@@ -234,6 +234,28 @@ static WMLinkAppendDataItem *wm_link_append_data_item_add(
        return item;
 }
 
+static bool wm_asset_engine_load_post_from_append_data(bContext *C, 
AssetEngine *ae, WMLinkAppendData *lapp_data)
+{
+       if (lapp_data->num_items > 0 && ae->type->load_post != NULL) {
+               LinkNode *itemlink;
+               AssetUUID *uuid;
+               AssetUUIDList uuids = {
+                   .uuids = MEM_callocN(sizeof(*uuids.uuids) * 
lapp_data->num_items, __func__),
+                   .nbr_uuids = lapp_data->num_items,
+                   .asset_engine_version = ae->type->version
+               };
+
+               for (itemlink = lapp_data->items.list, uuid = uuids.uuids; 
itemlink; itemlink = itemlink->next, uuid++) {
+                       WMLinkAppendDataItem *lapp_item = itemlink->link;
+                       *uuid = *lapp_item->uuid;
+                       uuid->id = lapp_item->new_id;
+               }
+
+               return ae->type->load_post(C, ae, &uuids);
+       }
+       return true;
+}
+
 static int path_to_idcode(const char *path)
 {
        const int filetype = ED_path_extension_type(path);
@@ -612,7 +634,7 @@ static int wm_link_append_exec(bContext *C, wmOperator *op)
                return OPERATOR_CANCELLED;
        }
 
-       if ((flag & FILE_LINK) != 0) {
+       if (!do_append) {
                /* XXX Currently this only applies to virtual libs ('linking' 
mere image files...).
                 *     However, we may want to make this a general option at 
link time when importing assets... */
                lapp_data->flag |= BLO_LIBLINK_GENERATE_OVERRIDE;
@@ -676,6 +698,11 @@ static int wm_link_append_exec(bContext *C, wmOperator *op)
                }
        }
 
+       if (aet != NULL && aet->load_post != NULL) {
+               AssetEngine *ae = BKE_asset_engine_create(aet, NULL);
+               wm_asset_engine_load_post_from_append_data(C, ae, lapp_data);
+       }
+
        wm_link_append_data_free(lapp_data);
 
        /* important we unset, otherwise these object wont
@@ -1621,6 +1648,8 @@ static int wm_assets_reload_exec(bContext *C, wmOperator 
*op)
 
                lib_relocate_do(bmain, NULL, lapp_data, op->reports, 
auce->ae->type, do_reload);
 
+               wm_asset_engine_load_post_from_append_data(C, auce->ae, 
lapp_data);
+
                wm_link_append_data_free(lapp_data);
                BLI_ghash_free(libraries, MEM_freeN, NULL);
                BKE_filedir_entryarr_clear(paths);

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to