Finish nested collections rna (recursions ftw)
authorDalai Felinto <dfelinto@gmail.com>
Wed, 23 Nov 2016 13:08:12 +0000 (14:08 +0100)
committerDalai Felinto <dfelinto@gmail.com>
Wed, 23 Nov 2016 13:08:12 +0000 (14:08 +0100)
source/blender/blenkernel/BKE_scene.h
source/blender/blenkernel/intern/scene.c
source/blender/makesrna/intern/rna_scene.c

index 6ade94f783ab5dca060ef2ddb57a9a77cba9c8c9..4039ff909e759fd76e53a86710e63179278eacc2 100644 (file)
@@ -129,12 +129,15 @@ void BKE_scene_update_for_newframe_ex(struct EvaluationContext *eval_ctx, struct
 struct SceneLayer *BKE_scene_add_layer(struct Scene *sce, const char *name);
 bool BKE_scene_remove_layer(struct Main *main, struct Scene *scene, struct SceneLayer *sl);
 struct LayerCollection *BKE_scene_add_collection(struct SceneLayer *sl, const char *name);
-
 bool BKE_scene_remove_nested_collection(struct SceneLayer *sl, struct LayerCollection *lc_parent, struct LayerCollection *lc);
 struct SceneLayer *BKE_scene_layer_from_collection(struct Scene *scene, struct LayerCollection *lc);
 bool BKE_scene_remove_collection(struct SceneLayer *sl, struct LayerCollection *lc);
 struct LayerCollection *BKE_scene_add_nested_collection(struct SceneLayer *sl, struct LayerCollection *lc, const char *name);
 
+struct LayerCollection *BKE_scene_layer_collection_active(struct SceneLayer *sl);
+int BKE_scene_layer_collection_count(struct SceneLayer *sl);
+int BKE_scene_layer_collection_findindex(struct SceneLayer *sl, struct LayerCollection *lc);
+
 struct SceneRenderLayer *BKE_scene_add_render_layer(struct Scene *sce, const char *name);
 bool BKE_scene_remove_render_layer(struct Main *main, struct Scene *scene, struct SceneRenderLayer *srl);
 
index 21a72d1d6af4d34b50bb3d1c59e81c2eb82647a9..f8319318e52ab1435dff181b3eb0bb1101e73492 100644 (file)
@@ -2034,6 +2034,72 @@ void BKE_scene_update_for_newframe_ex(EvaluationContext *eval_ctx, Main *bmain,
 #endif
 }
 
+/* Scene Layers */
+
+/* Recursively get the collection for a given index */
+static LayerCollection *collection_from_index(ListBase *lb, const int number, int *i)
+{
+       for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
+               if (*i == number) {
+                       return lc;
+               }
+
+               (*i)++;
+
+               LayerCollection *lc_nested = collection_from_index(&lc->collections, number, i);
+               if (lc_nested) {
+                       return lc_nested;
+               }
+       }
+       return NULL;
+}
+
+LayerCollection *BKE_scene_layer_collection_active(SceneLayer *sl)
+{
+       int i = 0;
+       return collection_from_index(&sl->collections, sl->active_collection, &i);
+}
+
+/* Recursively get the count of collections */
+static int collection_count(ListBase *lb)
+{
+       int i = 0;
+       for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
+               i += collection_count(&lc->collections) + 1;
+       }
+       return i;
+}
+
+int BKE_scene_layer_collection_count(SceneLayer *sl)
+{
+       return collection_count(&sl->collections);
+}
+
+/* Recursively get the index for a given collection */
+static int index_from_collection(ListBase *lb, LayerCollection *lc, int *i)
+{
+       for (LayerCollection *lcol = lb->first; lcol; lcol = lcol->next) {
+               if (lcol == lc) {
+                       return *i;
+               }
+
+               (*i)++;
+
+               int i_nested = index_from_collection(&lcol->collections, lc, i);
+               if (i_nested != -1) {
+                       return i_nested;
+               }
+       }
+       return -1;
+}
+
+/* return -1 */
+int BKE_scene_layer_collection_findindex(SceneLayer *sl, LayerCollection *lc)
+{
+       int i = 0;
+       return index_from_collection(&sl->collections, lc, &i);
+}
+
 /* Recursively check if collection is inside the collection ListBase */
 static bool collection_in_collections(ListBase *lb, LayerCollection *lc)
 {
index 74e8658d2a539c3f5768aad44228988921515ed9..f9dc013a4de9480f6ee09cbc9baf8691068e04ca 100644 (file)
@@ -1368,44 +1368,37 @@ static void rna_LayerNestedCollection_remove(
 
 static int rna_Layer_active_collection_index_get(PointerRNA *ptr)
 {
-       /* TODO account for nested collections */
        SceneLayer *sl = (SceneLayer *)ptr->data;
        return sl->active_collection;
 }
 
 static void rna_Layer_active_collection_index_set(PointerRNA *ptr, int value)
 {
-       /* TODO account for nested collections */
        SceneLayer *sl = (SceneLayer *)ptr->data;
-       int num_collections = BLI_listbase_count(&sl->collections);
+       int num_collections = BKE_scene_layer_collection_count(sl);
        sl->active_collection = min_ff(value, num_collections - 1);
 }
 
 static void rna_Layer_active_collection_index_range(
         PointerRNA *ptr, int *min, int *max, int *UNUSED(softmin), int *UNUSED(softmax))
 {
-       /* TODO account for nested collections */
        SceneLayer *sl = (SceneLayer *)ptr->data;
-
        *min = 0;
-       *max = max_ii(0, BLI_listbase_count(&sl->collections) - 1);
+       *max = max_ii(0, BKE_scene_layer_collection_count(sl) - 1);
 }
 
 static PointerRNA rna_Layer_active_collection_get(PointerRNA *ptr)
 {
-       /* TODO account for nested collections */
        SceneLayer *sl = (SceneLayer *)ptr->data;
-       LayerCollection *lc = BLI_findlink(&sl->collections, sl->active_collection);
-
+       LayerCollection *lc = BKE_scene_layer_collection_active(sl);
        return rna_pointer_inherit_refine(ptr, &RNA_LayerCollection, lc);
 }
 
 static void rna_Layer_active_collection_set(PointerRNA *ptr, PointerRNA value)
 {
-       /* TODO account for nested collections */
        SceneLayer *sl = (SceneLayer *)ptr->data;
        LayerCollection *lc = (LayerCollection *)value.data;
-       const int index = BLI_findindex(&sl->collections, lc);
+       const int index = BKE_scene_layer_collection_findindex(sl, lc);
        if (index != -1) sl->active_collection = index;
 }