Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBastien Montagne <montagne29@wanadoo.fr>2019-02-07 19:16:15 +0300
committerBastien Montagne <montagne29@wanadoo.fr>2019-02-07 22:46:41 +0300
commit599561de841b6f6f2c0ef6a5a4afa80cf3c51918 (patch)
tree342d46ea6ef2960f1b78a6e446ec799de4d20d8d /source/blender/blenkernel/intern/main.c
parentd2afa51ddc68c8a923c9535ba7678ebe468e2220 (diff)
BKE_main: add utils to loop over whole IDs of a given Main database.
We are currently having the same boiler plate code in tens of places accross our code, we can as well have a utils to do that.
Diffstat (limited to 'source/blender/blenkernel/intern/main.c')
-rw-r--r--source/blender/blenkernel/intern/main.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/source/blender/blenkernel/intern/main.c b/source/blender/blenkernel/intern/main.c
index 327c1cda165..2d3988ed8f5 100644
--- a/source/blender/blenkernel/intern/main.c
+++ b/source/blender/blenkernel/intern/main.c
@@ -212,6 +212,55 @@ void BKE_main_relations_free(Main *bmain)
}
/**
+ * Call given callback over every IDs of given \a lb listbase (assumed to be part of given \a bmain).
+ *
+ * \return false if the iteration was iterrupted by the callback.
+ *
+ * \warning \a callback may affect the ID, but DO NOT change the listbase or Main database (add/remove/reorder its IDs).
+ */
+bool BKE_main_listbase_foreach_id(
+ Main *bmain, ListBase *lb,
+ MainForeachIDCallback callback, void *user_data)
+{
+ bool keep_looping = true;
+ for (ID *id = lb->first; id; id = id->next) {
+ if (!(keep_looping = callback(bmain, id, user_data))) {
+ return keep_looping;
+ }
+ }
+ return keep_looping;
+}
+
+/**
+ * Call given callback over every IDs of given \a bmain Main database.
+ *
+ * \param reverse_type_order Allow to reverse order in which ID *types* are handled
+ * (i.e. does not reverse the order in which IDs themselves are handled
+ * whithin a give listbase).
+ * \return false if the iteration was iterrupted by the callback.
+ *
+ * \warning \a callback may affect the ID, but DO NOT change the Main database (add/remove/reorder its IDs).
+ */
+bool BKE_main_foreach_id(
+ Main *bmain, const bool reverse_type_order,
+ MainForeachIDCallback callback, void *user_data)
+{
+ ListBase *lbarray[MAX_LIBARRAY];
+ const int nbr_types = set_listbasepointers(bmain, lbarray);
+
+ bool keep_looping = true;
+ for (int i = reverse_type_order ? nbr_types - 1 : 0;
+ reverse_type_order ? i >= 0 : i < nbr_types;
+ reverse_type_order ? i-- : i++)
+ {
+ if (!(keep_looping = BKE_main_listbase_foreach_id(bmain, lbarray[i], callback, user_data))) {
+ return keep_looping;
+ }
+ }
+ return keep_looping;
+}
+
+/**
* Generates a raw .blend file thumbnail data from given image.
*
* \param bmain: If not NULL, also store generated data in this Main.