Cycles: add location/rotate/scale and XYZ mapping options for all texture nodes,
[blender.git] / source / blender / blenloader / intern / readfile.c
index 15f5b49dd242b9bd378ba69d84293b04c0c13170..1c96cd2e05af59619dc0e6915c95c2d3306ab0ec 100644 (file)
@@ -2055,9 +2055,103 @@ static void lib_link_nodetree(FileData *fd, Main *main)
        }
 }
 
+static void ntree_tmp_cycles_emission_version_patch(FileData *fd, Library *lib, bNodeTree *ntree)
+{
+       bNode *node;
+       bNodeSocket *sock;
+       bNodeSocketValueFloat *valfloat;
+
+       for(node=ntree->nodes.first; node; node=node->next) {
+               if(node->type == SH_NODE_EMISSION) {
+                       for(sock=node->inputs.first; sock; sock=sock->next) {
+                               if(strcmp(sock->name, "Strength") == 0) {
+                                       valfloat= sock->default_value;
+                                       valfloat->value /= M_PI;
+                               }
+                       }
+               }
+               else if(node->type == NODE_GROUP) {
+                       bNodeTree *ntree= newlibadr(fd, lib, node->id);
+
+                       if(ntree)
+                               ntree_tmp_cycles_emission_version_patch(fd, lib, ntree);
+               }
+       }
+}
+
+static void ntree_tmp_cycles_version_patch(bNodeTree *ntree)
+{
+       bNode *node;
+       bNodeSocket *sock;
+
+       for(node=ntree->nodes.first; node; node=node->next) {
+               if(node->type == SH_NODE_FRESNEL) {
+
+                       for(sock=node->inputs.first; sock; sock=sock->next) {
+                               if(strcmp(sock->name, "Fresnel") == 0) {
+                                       bNodeSocket *osock;
+
+                                       node->type = SH_NODE_BLEND_WEIGHT;
+                                       strcpy(sock->name, "Blend");
+
+                                       for(osock=node->outputs.first; osock; osock=osock->next)
+                                               if(strcmp(osock->name, "Fac") == 0)
+                                                       strcpy(osock->name, "Fresnel");
+                               }
+                       }
+               }
+               else {
+                       for(sock=node->inputs.first; sock; sock=sock->next) {
+                               if(strcmp(sock->name, "Closure1") == 0)
+                                       strcpy(sock->name, "Shader1");
+                               if(strcmp(sock->name, "Closure2") == 0)
+                                       strcpy(sock->name, "Shader2");
+
+                               if(strcmp(sock->name, "Color1") == 0)
+                                       strcpy(sock->name, "Color");
+                               if(strcmp(sock->name, "Color2") == 0)
+                                       strcpy(sock->name, "Color");
+
+                               if(strcmp(sock->name, "Vector1") == 0)
+                                       strcpy(sock->name, "Vector");
+                               if(strcmp(sock->name, "Vector2") == 0)
+                                       strcpy(sock->name, "Vector");
+
+                               if(strcmp(sock->name, "Value1") == 0)
+                                       strcpy(sock->name, "Value");
+                               if(strcmp(sock->name, "Value2") == 0)
+                                       strcpy(sock->name, "Value");
+
+                               if(strcmp(sock->name, "Shader1") == 0)
+                                       strcpy(sock->name, "Shader");
+                               if(strcmp(sock->name, "Shader2") == 0)
+                                       strcpy(sock->name, "Shader");
+
+                               if(node->type == SH_NODE_OUTPUT_MATERIAL ||
+                                  node->type == SH_NODE_OUTPUT_WORLD ||
+                                  node->type == SH_NODE_OUTPUT_LAMP) {
+                                       if(strcmp(sock->name, "Shader") == 0)
+                                               strcpy(sock->name, "Surface");
+                               }
+
+                               if(strcmp(sock->name, "Fresnel") == 0) {
+                                       strcpy(sock->name, "IOR");
+                                       sock->ns.vec[0] = 1.0f/MAX2(1.0f - sock->ns.vec[0], 1e-5f);
+                               }
+                       }
+
+                       for(sock=node->outputs.first; sock; sock=sock->next)
+                               if(strcmp(sock->name, "Closure") == 0)
+                                       strcpy(sock->name, "Shader");
+               }
+       }
+}
+
 static void lib_nodetree_init_types_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
 {
        bNode *node;
+
+       ntree_tmp_cycles_version_patch(ntree);
        
        ntreeInitTypes(ntree);
        
@@ -5519,6 +5613,7 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                        else if(sl->spacetype==SPACE_BUTS) {
                                SpaceButs *sbuts= (SpaceButs *)sl;
                                sbuts->path= NULL;
+                               sbuts->texuser= NULL;
                        }
                        else if(sl->spacetype==SPACE_CONSOLE) {
                                SpaceConsole *sconsole= (SpaceConsole *)sl;
@@ -5849,7 +5944,7 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
        if(id->flag & LIB_FAKEUSER) id->us= 1;
        else id->us= 0;
        id->icon_id = 0;
-       id->flag &= ~LIB_ID_RECALC;
+       id->flag &= ~(LIB_ID_RECALC|LIB_ID_RECALC_DATA);
 
        /* this case cannot be direct_linked: it's just the ID part */
        if(bhead->code==ID_ID) {
@@ -7047,13 +7142,40 @@ static void do_versions_nodetree_default_value(bNodeTree *ntree)
                do_versions_socket_default_value(sock);
 }
 
+static void do_versions_nodetree_clear_dynamic_sockets(bNodeTree *ntree)
+{
+       /* conflict with cycles flag .. */
+       bNodeSocket *sock;
+
+       for (sock=ntree->inputs.first; sock; sock=sock->next) {
+               if (sock->flag & SOCK_DYNAMIC) {
+                       sock->flag &= ~SOCK_DYNAMIC;
+                       sock->flag |= SOCK_COLLAPSED;
+               }
+       }
+       for (sock=ntree->outputs.first; sock; sock=sock->next) {
+               if (sock->flag & SOCK_DYNAMIC) {
+                       sock->flag &= ~SOCK_DYNAMIC;
+                       sock->flag |= SOCK_COLLAPSED;
+               }
+       }
+}
+
 static void do_versions_nodetree_dynamic_sockets(bNodeTree *ntree)
 {
        bNodeSocket *sock;
-       for (sock=ntree->inputs.first; sock; sock=sock->next)
-               sock->flag |= SOCK_DYNAMIC;
-       for (sock=ntree->outputs.first; sock; sock=sock->next)
-               sock->flag |= SOCK_DYNAMIC;
+       for (sock=ntree->inputs.first; sock; sock=sock->next) {
+               if(sock->flag & SOCK_DYNAMIC)
+                       sock->flag |= SOCK_COLLAPSED;
+               else
+                       sock->flag |= SOCK_DYNAMIC;
+       }
+       for (sock=ntree->outputs.first; sock; sock=sock->next) {
+               if(sock->flag & SOCK_DYNAMIC)
+                       sock->flag |= SOCK_COLLAPSED;
+               else
+                       sock->flag |= SOCK_DYNAMIC;
+       }
 }
 
 void convert_tface_mt(FileData *fd, Main *main)
@@ -7090,6 +7212,40 @@ static void do_versions_nodetree_image_default_alpha_output(bNodeTree *ntree)
        }
 }
 
+static void do_version_ntree_tex_mapping_260(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
+{
+       bNode *node;
+
+       for(node=ntree->nodes.first; node; node=node->next) {
+               TexMapping *tex_mapping= NULL;
+               ColorMapping *color_mapping= NULL;
+
+               if(node->type == SH_NODE_MAPPING) {
+                       tex_mapping= node->storage;
+                       tex_mapping->projx= PROJ_X;
+                       tex_mapping->projy= PROJ_Y;
+                       tex_mapping->projz= PROJ_Z;
+               }
+               else if(ELEM7(node->type, SH_NODE_TEX_IMAGE, SH_NODE_TEX_NOISE, SH_NODE_TEX_SKY,
+                       SH_NODE_TEX_BLEND, SH_NODE_TEX_VORONOI, SH_NODE_TEX_MAGIC, SH_NODE_TEX_MARBLE) ||
+                       ELEM6(node->type, SH_NODE_TEX_CLOUDS, SH_NODE_TEX_WOOD, SH_NODE_TEX_MUSGRAVE,
+                       SH_NODE_TEX_STUCCI, SH_NODE_TEX_DISTNOISE, SH_NODE_TEX_ENVIRONMENT)) {
+                       NodeTexBase *base= node->storage;
+
+                       if(node->type == SH_NODE_TEX_NOISE)
+                               node->storage= MEM_callocN(sizeof(NodeTexNoise), "NodeTexNoise");
+
+                       tex_mapping= &base->tex_mapping;
+                       color_mapping= &base->color_mapping;
+
+                       if(is_zero_v3(tex_mapping->size)) {
+                               default_tex_mapping(tex_mapping);
+                               default_color_mapping(color_mapping);
+                       }
+               }
+       }
+}
+
 static void do_versions(FileData *fd, Library *lib, Main *main)
 {
        /* WATCH IT!!!: pointers from libdata have not been converted */
@@ -11999,24 +12155,42 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        /* Convert default socket values from bNodeStack */
                        Scene *sce;
                        Material *mat;
+                       Lamp *la;
                        Tex *tex;
                        bNodeTree *ntree;
+                       World *wo;
+
                        for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next) {
                                do_versions_nodetree_default_value(ntree);
                                ntree->update |= NTREE_UPDATE;
                        }
                        for (sce=main->scene.first; sce; sce=sce->id.next)
                                if (sce->nodetree) {
+                               do_versions_nodetree_clear_dynamic_sockets(sce->nodetree);
                                do_versions_nodetree_default_value(sce->nodetree);
                                sce->nodetree->update |= NTREE_UPDATE;
                        }
                        for (mat=main->mat.first; mat; mat=mat->id.next)
                                if (mat->nodetree) {
+                               do_versions_nodetree_clear_dynamic_sockets(mat->nodetree);
                                do_versions_nodetree_default_value(mat->nodetree);
                                mat->nodetree->update |= NTREE_UPDATE;
                        }
+                       for (la=main->lamp.first; la; la=la->id.next)
+                               if (la->nodetree) {
+                               do_versions_nodetree_clear_dynamic_sockets(la->nodetree);
+                               do_versions_nodetree_default_value(la->nodetree);
+                               la->nodetree->update |= NTREE_UPDATE;
+                       }
+                       for (wo=main->world.first; wo; wo=wo->id.next)
+                               if (wo->nodetree) {
+                               do_versions_nodetree_clear_dynamic_sockets(wo->nodetree);
+                               do_versions_nodetree_default_value(wo->nodetree);
+                               wo->nodetree->update |= NTREE_UPDATE;
+                       }
                        for (tex=main->tex.first; tex; tex=tex->id.next)
                                if (tex->nodetree) {
+                               do_versions_nodetree_clear_dynamic_sockets(tex->nodetree);
                                do_versions_nodetree_default_value(tex->nodetree);
                                tex->nodetree->update |= NTREE_UPDATE;
                        }
@@ -12121,8 +12295,32 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
 
        }
 
-       /* put compatibility code here until next subversion bump */
-       {
+       if(main->versionfile < 259 || (main->versionfile == 259 && main->subversionfile < 5)) {
+               Scene *sce;
+               Base *base;
+               Material *ma;
+
+               /* compatibility tweak */
+               for(sce = main->scene.first; sce; sce = sce->id.next) {
+                       if(strcmp(sce->r.engine, "CYCLES") == 0) {
+                               for(base = sce->base.first; base; base=base->next) {
+                                       Object *ob= newlibadr(fd, lib, base->object);
+
+                                       if(ob && ob->type == OB_LAMP) {
+                                               Lamp *la= newlibadr(fd, lib, ob->data);
+                                               if(la)
+                                                       la->area_size= 0.0f;
+                                       }
+                               }
+                       }
+               }
+
+               for(ma = main->mat.first; ma; ma= ma->id.next)
+                       if(ma->nodetree)
+                               ntree_tmp_cycles_emission_version_patch(fd, lib, ma->nodetree);
+       }
+
+       if (main->versionfile < 260){
                {
                        /* set default alpha value of Image outputs in image and render layer nodes to 0 */
                        Scene *sce;
@@ -12155,6 +12353,38 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                }
                        }
                }
+
+       }
+
+       if (main->versionfile < 260 || (main->versionfile == 260 && main->subversionfile < 1)){
+               Object *ob;
+
+               for (ob= main->object.first; ob; ob= ob->id.next) {
+                       ob->collision_boundtype= ob->boundtype;
+               }
+
+               {
+                       Camera *cam;
+                       for(cam= main->camera.first; cam; cam= cam->id.next) {
+                               if (cam->sensor_x < 0.01)
+                                       cam->sensor_x = DEFAULT_SENSOR_WIDTH;
+
+                               if (cam->sensor_y < 0.01)
+                                       cam->sensor_y = DEFAULT_SENSOR_HEIGHT;
+                       }
+               }
+
+               {
+                       bNodeTreeType *ntreetype= ntreeGetType(NTREE_SHADER);
+
+                       if(ntreetype && ntreetype->foreach_nodetree)
+                               ntreetype->foreach_nodetree(main, NULL, do_version_ntree_tex_mapping_260);
+               }
+       }
+
+       /* put compatibility code here until next subversion bump */
+       {
+               
        }
 
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */