Proper fix for crash loading old files with compositor
authorSergey Sharybin <sergey.vfx@gmail.com>
Wed, 24 May 2017 13:39:15 +0000 (15:39 +0200)
committerSergey Sharybin <sergey.vfx@gmail.com>
Wed, 24 May 2017 13:39:15 +0000 (15:39 +0200)
Now we keep all links around, even for sockets which were
implicitly renamed. And also ensuring new sockets have proper
storage.

source/blender/blenloader/intern/versioning_270.c

index 06eb631f6696086704d994bc3d12c9c4f42e0160..d2f43a2d79e89e3871342416532403fb3a7a7470 100644 (file)
@@ -249,6 +249,41 @@ static void do_version_hue_sat_node(bNodeTree *ntree, bNode *node)
        node->storage = NULL;
 }
 
+static void do_versions_compositor_render_passes_storage(bNode *node)
+{
+       int pass_index = 0;
+       const char *sockname;
+       for (bNodeSocket *sock = node->outputs.first; sock && pass_index < 31; sock = sock->next, pass_index++) {
+               if (sock->storage == NULL) {
+                       NodeImageLayer *sockdata = MEM_callocN(sizeof(NodeImageLayer), "node image layer");
+                       sock->storage = sockdata;
+                       BLI_strncpy(sockdata->pass_name, node_cmp_rlayers_sock_to_pass(pass_index), sizeof(sockdata->pass_name));
+
+                       if (pass_index == 0) sockname = "Image";
+                       else if (pass_index == 1) sockname = "Alpha";
+                       else sockname = node_cmp_rlayers_sock_to_pass(pass_index);
+                       BLI_strncpy(sock->name, sockname, sizeof(sock->name));
+               }
+       }
+}
+
+static void do_versions_compositor_render_passes(bNodeTree *ntree)
+{
+       for (bNode *node = ntree->nodes.first; node; node = node->next) {
+               if (node->type == CMP_NODE_R_LAYERS) {
+                       /* First we make sure existing sockets have proper names.
+                        * This is important because otherwise verification will
+                        * drop links from sockets which were renamed.
+                        */
+                       do_versions_compositor_render_passes_storage(node);
+                       /* Make sure new sockets are properly created. */
+                       node_verify_socket_templates(ntree, node);
+                       /* Make sure all possibly created sockets have proper storage. */
+                       do_versions_compositor_render_passes_storage(node);
+               }
+       }
+}
+
 void blo_do_versions_270(FileData *fd, Library *UNUSED(lib), Main *main)
 {
        if (!MAIN_VERSION_ATLEAST(main, 270, 0)) {
@@ -1612,25 +1647,7 @@ void blo_do_versions_270(FileData *fd, Library *UNUSED(lib), Main *main)
 
                FOREACH_NODETREE(main, ntree, id) {
                        if (ntree->type == NTREE_COMPOSIT) {
-                               bNode *node;
-                               for (node = ntree->nodes.first; node; node = node->next) {
-                                       if (node->type == CMP_NODE_R_LAYERS) {
-                                               int pass_index = 0;
-                                               const char *sockname;
-                                               for (bNodeSocket *sock = node->outputs.first; sock && pass_index < 31; sock = sock->next, pass_index++) {
-                                                       if (sock->storage == NULL) {
-                                                               NodeImageLayer *sockdata = MEM_callocN(sizeof(NodeImageLayer), "node image layer");
-                                                               sock->storage = sockdata;
-                                                               BLI_strncpy(sockdata->pass_name, node_cmp_rlayers_sock_to_pass(pass_index), sizeof(sockdata->pass_name));
-
-                                                               if (pass_index == 0) sockname = "Image";
-                                                               else if (pass_index == 1) sockname = "Alpha";
-                                                               else sockname = node_cmp_rlayers_sock_to_pass(pass_index);
-                                                               BLI_strncpy(sock->name, sockname, sizeof(sock->name));
-                                                       }
-                                               }
-                                       }
-                               }
+                               do_versions_compositor_render_passes(ntree);
                        }
                } FOREACH_NODETREE_END
        }