Fix collection syncing when creating new collections from the outliner
authorDalai Felinto <dfelinto@gmail.com>
Fri, 9 Feb 2018 11:04:00 +0000 (09:04 -0200)
committerDalai Felinto <dfelinto@gmail.com>
Fri, 9 Feb 2018 11:05:27 +0000 (09:05 -0200)
We were not passing a scene collection parent to the BKE_collection_add
function, which in turn made syncing not work.

Right now we:
* Explicitly pass the master collection in this case
* Fallback to the master collection in other cases

With unittest.

source/blender/blenkernel/intern/collection.c
source/blender/blenkernel/intern/layer.c
source/blender/editors/space_outliner/outliner_collections.c
tests/python/view_layer/CMakeLists.txt
tests/python/view_layer/test_collection_new_sync.py [new file with mode: 0644]

index 729be02bfc70b876006d1ba52bc99fa834f37305..69168c4dd3d5bca3a4d151e5882969f6ef52e1d7 100644 (file)
@@ -111,6 +111,10 @@ static SceneCollection *collection_add(ID *owner_id, SceneCollection *sc_parent,
  */
 SceneCollection *BKE_collection_add(ID *owner_id, SceneCollection *sc_parent, const int type, const char *name_custom)
 {
+       if (sc_parent == NULL) {
+               sc_parent = BKE_collection_master(owner_id);
+       }
+
        SceneCollection *scene_collection = collection_add(owner_id, sc_parent, type, name_custom);
        BKE_layer_sync_new_scene_collection(owner_id, sc_parent, scene_collection);
        return scene_collection;
index 9807c6e4f10a093b91e2c19a909cd000386ad78a..cb7b72bdabd659a6c1b34646e5aabc0770dd9701 100644 (file)
@@ -375,6 +375,7 @@ static void layer_collection_sync_flags(
 
 static void layer_collections_sync_flags(ListBase *layer_collections_dst, const ListBase *layer_collections_src)
 {
+       BLI_assert(BLI_listbase_count(layer_collections_dst) == BLI_listbase_count(layer_collections_src));
        LayerCollection *layer_collection_dst = (LayerCollection *)layer_collections_dst->first;
        const LayerCollection *layer_collection_src = (const LayerCollection *)layer_collections_src->first;
        while (layer_collection_dst != NULL) {
index be4c8f6208583d4e746fea33c307698a8cd6a80d..3d5f12a94fdc89d35af1c47df7c8695a56a88325 100644 (file)
@@ -342,7 +342,8 @@ static int collection_new_exec(bContext *C, wmOperator *UNUSED(op))
        Main *bmain = CTX_data_main(C);
        Scene *scene = CTX_data_scene(C);
        ViewLayer *view_layer = CTX_data_view_layer(C);
-       SceneCollection *scene_collection = BKE_collection_add(&scene->id, NULL, COLLECTION_TYPE_NONE, NULL);
+       SceneCollection *scene_collection_parent = BKE_collection_master(&scene->id);
+       SceneCollection *scene_collection = BKE_collection_add(&scene->id, scene_collection_parent, COLLECTION_TYPE_NONE, NULL);
        BKE_collection_link(view_layer, scene_collection);
 
        DEG_relations_tag_update(bmain);
index e308e2e0952117694dc03cced9d79937c38c3798..cc5a3ba54b15c3d67dad1ea20eca9c24fe7ee5b8 100644 (file)
@@ -62,6 +62,7 @@ endmacro()
 
 VIEW_LAYER_TEST(active_collection)
 VIEW_LAYER_TEST(background_set)
+VIEW_LAYER_TEST(collection_new_sync)
 VIEW_LAYER_TEST(collection_rename_a)
 VIEW_LAYER_TEST(collection_rename_b)
 VIEW_LAYER_TEST(evaluation_render_settings_a)
diff --git a/tests/python/view_layer/test_collection_new_sync.py b/tests/python/view_layer/test_collection_new_sync.py
new file mode 100644 (file)
index 0000000..582e6c1
--- /dev/null
@@ -0,0 +1,47 @@
+# ############################################################
+# Importing - Same For All Render Layer Tests
+# ############################################################
+
+import unittest
+import os
+import sys
+
+from view_layer_common import *
+
+
+# ############################################################
+# Testing
+# ############################################################
+
+class UnitTesting(ViewLayerTesting):
+    def test_view_layer_syncing(self):
+        """
+        See if we can copy view layers.
+        """
+        import bpy
+        scene = bpy.context.scene
+        view_layer = scene.view_layers.new("All")
+
+        self.assertEqual(len(view_layer.collections), 1)
+        self.assertEqual(view_layer.collections[0].collection, scene.master_collection)
+
+        self.assertEqual(
+                {collection.name for collection in view_layer.collections[0].collections},
+                {'Collection 1'})
+
+        self.assertEqual(
+                bpy.ops.outliner.collection_new(),
+                {'FINISHED'})
+
+        self.assertEqual(
+                {collection.name for collection in view_layer.collections[0].collections},
+                {'Collection 1', 'Collection 2'})
+
+
+# ############################################################
+# Main - Same For All Render Layer Tests
+# ############################################################
+
+if __name__ == '__main__':
+    UnitTesting._extra_arguments = setup_extra_arguments(__file__)
+    unittest.main()