diff options
author | Dalai Felinto <dfelinto@gmail.com> | 2018-03-29 23:28:05 +0300 |
---|---|---|
committer | Dalai Felinto <dfelinto@gmail.com> | 2018-03-30 02:03:47 +0300 |
commit | 156b548208ba21db261c1a8df06f89470f86eaa8 (patch) | |
tree | 6afefdb8357ec78b39e5e1e19d2981e17e6974aa /source/blender/blenkernel | |
parent | 1251043636d37f36bc2e7c845e6542898476707d (diff) |
Collections: API to get collection from index
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_collection.h | 1 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/collection.c | 32 |
2 files changed, 33 insertions, 0 deletions
diff --git a/source/blender/blenkernel/BKE_collection.h b/source/blender/blenkernel/BKE_collection.h index b646d1d5e40..bfdafc3ad23 100644 --- a/source/blender/blenkernel/BKE_collection.h +++ b/source/blender/blenkernel/BKE_collection.h @@ -59,6 +59,7 @@ bool BKE_collection_object_remove(struct Main *bmain, struct ID *owner_id, struc bool BKE_collections_object_remove(struct Main *bmain, struct ID *owner_id, struct Object *object, const bool free_us); void BKE_collection_object_move(struct ID *owner_id, struct SceneCollection *sc_dst, struct SceneCollection *sc_src, struct Object *ob); bool BKE_collection_object_exists(struct SceneCollection *scene_collection, struct Object *ob); +struct SceneCollection *BKE_collection_from_index(struct Scene *scene, const int index); struct Group *BKE_collection_group_create(struct Main *bmain, struct Scene *scene, struct LayerCollection *lc); diff --git a/source/blender/blenkernel/intern/collection.c b/source/blender/blenkernel/intern/collection.c index aed0ffb9fc3..669bbfb00cc 100644 --- a/source/blender/blenkernel/intern/collection.c +++ b/source/blender/blenkernel/intern/collection.c @@ -523,6 +523,38 @@ bool BKE_collection_object_exists(struct SceneCollection *scene_collection, stru return false; } +static SceneCollection *scene_collection_from_index_recursive(SceneCollection *scene_collection, const int index, int *index_current) +{ + if (index == (*index_current)) { + return scene_collection; + } + + (*index_current)++; + + for (SceneCollection *scene_collection_iter = scene_collection->scene_collections.first; + scene_collection_iter != NULL; + scene_collection_iter = scene_collection_iter->next) + { + SceneCollection *nested = scene_collection_from_index_recursive(scene_collection_iter, index, index_current); + if (nested != NULL) { + return nested; + } + } + return NULL; +} + +/** + * Return Scene Collection for a given index. + * + * The index is calculated from top to bottom counting the children before the siblings. + */ +SceneCollection *BKE_collection_from_index(Scene *scene, const int index) +{ + int index_current = 0; + SceneCollection *master_collection = BKE_collection_master(&scene->id); + return scene_collection_from_index_recursive(master_collection, index, &index_current); +} + static void layer_collection_sync(LayerCollection *lc_dst, LayerCollection *lc_src) { lc_dst->flag = lc_src->flag; |