[Bf-blender-cvs] [59a1340] asset-engine: Add asset dependencies (re)build.

Bastien Montagne noreply at git.blender.org
Wed Feb 24 20:44:08 CET 2016


Commit: 59a13407148271ff4f37f48cedb45181698a3100
Author: Bastien Montagne
Date:   Wed Feb 24 20:42:58 2016 +0100
Branches: asset-engine
https://developer.blender.org/rB59a13407148271ff4f37f48cedb45181698a3100

Add asset dependencies (re)build.

Is rebuilt when we link a new asset, or when we (re)load a .blend file.
Runtime data only, not saved in .blend file.

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

M	source/blender/blenkernel/BKE_library.h
M	source/blender/blenkernel/intern/library.c
M	source/blender/blenkernel/intern/library_query.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/makesdna/DNA_ID.h

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

diff --git a/source/blender/blenkernel/BKE_library.h b/source/blender/blenkernel/BKE_library.h
index 954390e..2039ae7 100644
--- a/source/blender/blenkernel/BKE_library.h
+++ b/source/blender/blenkernel/BKE_library.h
@@ -123,6 +123,7 @@ void BKE_library_make_local(struct Main *bmain, struct Library *lib, bool untagg
 
 void BKE_library_asset_repository_init(struct Library *lib, const struct AssetEngineType *aet, const char *repo_root);
 void BKE_library_asset_repository_clear(struct Library *lib);
+void BKE_library_asset_repository_free(struct Library *lib);
 struct AssetRef *BKE_library_asset_repository_asset_add(struct Library *lib, const void *idv);
 void BKE_library_asset_repository_asset_remove(struct Library *lib, const void *idv);
 struct AssetRef *BKE_library_asset_repository_asset_find(struct Library *lib, const void *idv);
@@ -130,6 +131,8 @@ void BKE_library_asset_repository_subdata_add(struct AssetRef *aref, const void
 void BKE_library_asset_repository_subdata_remove(struct AssetRef *aref, const void *idv);
 
 void BKE_libraries_asset_subdata_remove(struct Main *bmain, const void *idv);
+void BKE_libraries_asset_repositories_clear(struct Main *bmain);
+void BKE_libraries_asset_repositories_rebuild(struct Main *bmain);
 
 typedef void (*BKE_library_free_window_manager_cb)(struct bContext *, struct wmWindowManager *);
 typedef void (*BKE_library_free_notifier_reference_cb)(const void *);
diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c
index c0ec296..ecc7062 100644
--- a/source/blender/blenkernel/intern/library.c
+++ b/source/blender/blenkernel/intern/library.c
@@ -1098,7 +1098,7 @@ static void BKE_library_free(Library *lib)
 	if (lib->packedfile)
 		freePackedFile(lib->packedfile);
 
-	BKE_library_asset_repository_clear(lib);
+	BKE_library_asset_repository_free(lib);
 }
 
 static BKE_library_free_window_manager_cb free_windowmanager_cb = NULL;
@@ -1883,7 +1883,7 @@ void BKE_library_make_local(Main *bmain, Library *lib, bool untagged_only, bool
  *                        when having huge assets (or many of them)... */
 void BKE_library_asset_repository_init(Library *lib, const AssetEngineType *aet, const char *repo_root)
 {
-	BKE_library_asset_repository_clear(lib);
+	BKE_library_asset_repository_free(lib);
 	lib->asset_repository = MEM_mallocN(sizeof(*lib->asset_repository), __func__);
 
 	BLI_strncpy(lib->asset_repository->asset_engine, aet->idname, sizeof(lib->asset_repository->asset_engine));
@@ -1900,6 +1900,13 @@ void BKE_library_asset_repository_clear(Library *lib)
 			BLI_freelistN(&aref->id_list);
 			MEM_freeN(aref);
 		}
+	}
+}
+
+void BKE_library_asset_repository_free(Library *lib)
+{
+	if (lib->asset_repository) {
+		BKE_library_asset_repository_clear(lib);
 		MEM_freeN(lib->asset_repository);
 		lib->asset_repository = NULL;
 	}
@@ -1938,7 +1945,7 @@ AssetRef *BKE_library_asset_repository_asset_find(Library *lib, const void *idv)
 	return NULL;
 }
 
-void BKE_library_asset_repository_asset_remove(struct Library *lib, const void *idv)
+void BKE_library_asset_repository_asset_remove(Library *lib, const void *idv)
 {
 	AssetRef *aref = BKE_library_asset_repository_asset_find(lib, idv);
 	BLI_remlink(&lib->asset_repository->assets, aref);
@@ -1946,14 +1953,14 @@ void BKE_library_asset_repository_asset_remove(struct Library *lib, const void *
 	MEM_freeN(aref);
 }
 
-void BKE_library_asset_repository_subdata_add(struct AssetRef *aref, const void *idv)
+void BKE_library_asset_repository_subdata_add(AssetRef *aref, const void *idv)
 {
 	if (BLI_findptr(&aref->id_list, idv, offsetof(LinkData, data)) == NULL) {
 		BLI_addtail(&aref->id_list, BLI_genericNodeN((void *)idv));
 	}
 }
 
-void BKE_library_asset_repository_subdata_remove(struct AssetRef *aref, const void *idv)
+void BKE_library_asset_repository_subdata_remove(AssetRef *aref, const void *idv)
 {
 	LinkData *link = BLI_findptr(&aref->id_list, idv, offsetof(LinkData, data));
 	if (link) {
@@ -1961,7 +1968,7 @@ void BKE_library_asset_repository_subdata_remove(struct AssetRef *aref, const vo
 	}
 }
 
-void BKE_libraries_asset_subdata_remove(struct Main *bmain, const void *idv)
+void BKE_libraries_asset_subdata_remove(Main *bmain, const void *idv)
 {
 	const ID *id = idv;
 
@@ -1971,8 +1978,69 @@ void BKE_libraries_asset_subdata_remove(struct Main *bmain, const void *idv)
 
 	ListBase *lb = which_libbase(bmain, ID_LI);
 	for (Library *lib = lb->first; lib; lib = lib->id.next) {
-		for (AssetRef *aref = lib->asset_repository->assets.first; aref; aref = aref->next) {
-			BLI_freelinkN(&aref->id_list, BLI_findptr(&aref->id_list, idv, offsetof(LinkData, data)));
+		if (lib->asset_repository) {
+			for (AssetRef *aref = lib->asset_repository->assets.first; aref; aref = aref->next) {
+				BLI_freelinkN(&aref->id_list, BLI_findptr(&aref->id_list, idv, offsetof(LinkData, data)));
+			}
+		}
+	}
+}
+
+void BKE_libraries_asset_repositories_clear(Main *bmain)
+{
+	ListBase *lb = which_libbase(bmain, ID_LI);
+	for (Library *lib = lb->first; lib; lib = lib->id.next) {
+		BKE_library_asset_repository_clear(lib);
+	}
+	BKE_main_id_tag_all(bmain, LIB_TAG_ASSET, false);
+}
+
+static int library_asset_dependencies_rebuild_cb(void *userdata, ID *id_self, ID **idp, int UNUSED(cd_flag))
+{
+	if (!idp || !*idp) {
+		return IDWALK_RET_NOP;
+	}
+
+	AssetRef *aref = userdata;
+	ID *id = *idp;
+
+	if (id->uuid) {
+		return IDWALK_RET_STOP_RECURSION;
+	}
+
+	printf("%s (from %s)\n", id->name, id_self->name);
+
+	BKE_library_asset_repository_subdata_add(aref, (const void *)id);
+	id->tag |= LIB_TAG_ASSET;
+	return IDWALK_RET_NOP;
+}
+
+static void library_asset_dependencies_rebuild(ID *asset)
+{
+	Library *lib = asset->lib;
+	BLI_assert(lib->asset_repository);
+
+	asset->tag |= LIB_TAG_ASSET;
+
+	AssetRef *aref = BKE_library_asset_repository_asset_add(lib, asset);
+
+	BKE_library_foreach_ID_link(asset, library_asset_dependencies_rebuild_cb, aref, IDWALK_RECURSE);
+}
+
+void BKE_libraries_asset_repositories_rebuild(Main *bmain)
+{
+	ListBase *lbarray[MAX_LIBARRAY];
+	ID *id;
+	int a;
+
+	BKE_libraries_asset_repositories_clear(bmain);
+
+	a = set_listbasepointers(bmain, lbarray);
+	while (a--) {
+		for (id = lbarray[a]->first; id; id = id->next) {
+			if (id->uuid) {
+				library_asset_dependencies_rebuild(id);
+			}
 		}
 	}
 }
diff --git a/source/blender/blenkernel/intern/library_query.c b/source/blender/blenkernel/intern/library_query.c
index eeb0790..a11e38b 100644
--- a/source/blender/blenkernel/intern/library_query.c
+++ b/source/blender/blenkernel/intern/library_query.c
@@ -88,7 +88,7 @@
 		if (_flag & IDWALK_READONLY) { \
 			BLI_assert(*(id_pp) == old_id); \
 		} \
-		if (_flag & IDWALK_RECURSE) { \
+		if (old_id && (_flag & IDWALK_RECURSE)) { \
 			if (!BLI_gset_haskey((_data)->ids_handled, old_id)) { \
 				BLI_gset_add((_data)->ids_handled, old_id); \
 				if (!(callback_return & IDWALK_RET_STOP_RECURSION)) { \
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 5a0f5e2..772067a 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -7159,7 +7159,10 @@ static void direct_link_library(FileData *fd, Library *lib, Main *main)
 	
 	lib->packedfile = direct_link_packedfile(fd, lib->packedfile);
 	lib->asset_repository = newdataadr(fd, lib->asset_repository);
-	BLI_listbase_clear(&lib->asset_repository->assets);
+	if (lib->asset_repository) {
+		/* Do not clear lib->asset_repository itself! */
+		BLI_listbase_clear(&lib->asset_repository->assets);
+	}
 
 	/* new main */
 	newmain = BKE_main_new();
@@ -7167,9 +7170,6 @@ static void direct_link_library(FileData *fd, Library *lib, Main *main)
 	newmain->curlib = lib;
 	
 	lib->parent = NULL;
-
-	/* Do not clear lib->asset_repository itself! */
-	BLI_listbase_clear(&lib->asset_repository->assets);
 }
 
 static void lib_link_library(FileData *UNUSED(fd), Main *main)
@@ -8439,6 +8439,8 @@ BlendFileData *blo_read_file_internal(FileData *fd, const char *filepath)
 	lib_verify_nodetree(bfd->main, true);
 	fix_relpaths_library(fd->relabase, bfd->main); /* make all relative paths, relative to the open blend file */
 	
+	BKE_libraries_asset_repositories_rebuild(bfd->main);
+
 	link_global(fd, bfd);	/* as last */
 	
 	fd->mainlist = NULL;  /* Safety, this is local variable, shall not be used afterward. */
@@ -9997,6 +9999,8 @@ static void library_link_end(Main *mainl, FileData **fd, const short flag, Scene
 	/* clear group instantiating tag */
 	BKE_main_id_tag_listbase(&(mainvar->group), LIB_TAG_DOIT, false);
 
+	BKE_libraries_asset_repositories_rebuild(mainvar);
+
 	/* patch to prevent switch_endian happens twice */
 	if ((*fd)->flags & FD_FLAGS_SWITCH_ENDIAN) {
 		blo_freefiledata(*fd);
diff --git a/source/blender/makesdna/DNA_ID.h b/source/blender/makesdna/DNA_ID.h
index f866679..dd35fa1 100644
--- a/source/blender/makesdna/DNA_ID.h
+++ b/source/blender/makesdna/DNA_ID.h
@@ -359,6 +359,8 @@ enum {
 	LIB_TAG_EXTERN          = 1 << 0,
 	/* RESET_NEVER Datablock is from a library, and is only used (linked) inderectly through other libraries. */
 	LIB_TAG_INDIRECT        = 1 << 1,
+	/* RESET_NEVER Datablock is (or is used by) an asset. */
+	LIB_TAG_ASSET           = 1 << 9,
 
 	/* RESET_AFTER_USE Three flags used internally in readfile.c, to mark IDs needing to be read (only done once). */
 	LIB_TAG_NEED_EXPAND     = 1 << 3,




More information about the Bf-blender-cvs mailing list