Cycles: add location/rotate/scale and XYZ mapping options for all texture nodes,
[blender.git] / source / blender / blenloader / intern / readfile.c
index d6fa12cfaa0e89bfa410f006ca1be0a53f547630..1c96cd2e05af59619dc0e6915c95c2d3306ab0ec 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -518,7 +516,7 @@ static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *filepat
        BLI_addtail(mainlist, m);
 
        lib= alloc_libblock(&m->library, ID_LI, "lib");
-       strncpy(lib->name, filepath, sizeof(lib->name)-1);
+       BLI_strncpy(lib->name, filepath, sizeof(lib->name));
        BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
        
        m->curlib= lib;
@@ -1050,7 +1048,7 @@ void blo_freefiledata(FileData *fd)
 
 /* ************ DIV ****************** */
 
-int BLO_has_bfile_extension(char *str)
+int BLO_has_bfile_extension(const char *str)
 {
        return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend") || BLI_testextensie(str, ".blend.gz"));
 }
@@ -2057,6 +2055,30 @@ 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;
@@ -2082,10 +2104,36 @@ static void ntree_tmp_cycles_version_patch(bNodeTree *ntree)
                        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);
@@ -3071,6 +3119,9 @@ static void direct_link_texture(FileData *fd, Tex *tex)
        if(tex->vd) {
                tex->vd->dataset = NULL;
                tex->vd->ok = 0;
+       } else {
+               if(tex->type == TEX_VOXELDATA)
+                       tex->vd= MEM_callocN(sizeof(VoxelData), "direct_link_texture VoxelData");
        }
        
        tex->nodetree= newdataadr(fd, tex->nodetree);
@@ -3171,7 +3222,7 @@ static void direct_link_pointcache(FileData *fd, PointCache *cache)
                                pm->data[i] = newdataadr(fd, pm->data[i]);
                                
                                /* the cache saves non-struct data without DNA */
-                               if(pm->data[i] && strcmp(ptcache_data_struct[i], "")==0 && (fd->flags & FD_FLAGS_SWITCH_ENDIAN)) {
+                               if(pm->data[i] && ptcache_data_struct[i][0]=='\0' && (fd->flags & FD_FLAGS_SWITCH_ENDIAN)) {
                                        int j, tot= (BKE_ptcache_data_size (i) * pm->totpoint)/4; /* data_size returns bytes */
                                        int *poin= pm->data[i];
                                        
@@ -3487,6 +3538,9 @@ static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
        MTFace *tf= mtface;
        int i;
 
+       /* Add pseudo-references (not fake users!) to images used by texface. A
+          little bogus; it would be better if each mesh consistently added one ref
+          to each image it used. - z0r */
        for (i=0; i<totface; i++, tf++) {
                tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
                if(tf->tpage && tf->tpage->id.us==0)
@@ -4553,7 +4607,9 @@ static void lib_link_scene(FileData *fd, Main *main)
                                seq->scene_sound = NULL;
                                if(seq->scene) {
                                        seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
-                                       seq->scene_sound = sound_scene_add_scene_sound(sce, seq, seq->startdisp, seq->enddisp, seq->startofs + seq->anim_startofs);
+                                       if(seq->scene) {
+                                               seq->scene_sound = sound_scene_add_scene_sound(sce, seq, seq->startdisp, seq->enddisp, seq->startofs + seq->anim_startofs);
+                                       }
                                }
                                if(seq->scene_camera) seq->scene_camera= newlibadr(fd, sce->id.lib, seq->scene_camera);
                                if(seq->sound) {
@@ -4983,15 +5039,6 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                sfile->folders_prev= NULL;
                                                sfile->folders_next= NULL;
                                        }
-                                       else if(sl->spacetype==SPACE_IMASEL) {
-                                               SpaceImaSel *simasel= (SpaceImaSel *)sl;
-
-                                               simasel->files = NULL;                                          
-                                               simasel->returnfunc= NULL;
-                                               simasel->menup= NULL;
-                                               simasel->pupmenu= NULL;
-                                               simasel->img= NULL;
-                                       }
                                        else if(sl->spacetype==SPACE_ACTION) {
                                                SpaceAction *saction= (SpaceAction *)sl;
                                                bDopeSheet *ads= &saction->ads;
@@ -5055,11 +5102,6 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                        }
                                                }
                                        }
-                                       else if(sl->spacetype==SPACE_SOUND) {
-                                               SpaceSound *ssound= (SpaceSound *)sl;
-
-                                               ssound->sound= newlibadr_us(fd, sc->id.lib, ssound->sound);
-                                       }
                                        else if(sl->spacetype==SPACE_NODE) {
                                                SpaceNode *snode= (SpaceNode *)sl;
                                                
@@ -5230,12 +5272,6 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                        SpaceFile *sfile= (SpaceFile *)sl;
                                        sfile->op= NULL;
                                }
-                               else if(sl->spacetype==SPACE_IMASEL) {
-                                       SpaceImaSel *simasel= (SpaceImaSel *)sl;
-                                       if (simasel->files) {
-                                               //XXX BIF_filelist_freelib(simasel->files);
-                                       }
-                               }
                                else if(sl->spacetype==SPACE_ACTION) {
                                        SpaceAction *saction= (SpaceAction *)sl;
                                        
@@ -5302,11 +5338,6 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                                }
                                        }
                                }
-                               else if(sl->spacetype==SPACE_SOUND) {
-                                       SpaceSound *ssound= (SpaceSound *)sl;
-
-                                       ssound->sound= restore_pointer_by_name(newmain, (ID *)ssound->sound, 1);
-                               }
                                else if(sl->spacetype==SPACE_NODE) {
                                        SpaceNode *snode= (SpaceNode *)sl;
                                        
@@ -5390,8 +5421,8 @@ static void view3d_split_250(View3D *v3d, ListBase *regions)
                        rv3d->persp= v3d->persp;
                        rv3d->view= v3d->view;
                        rv3d->dist= v3d->dist;
-                       VECCOPY(rv3d->ofs, v3d->ofs);
-                       QUATCOPY(rv3d->viewquat, v3d->viewquat);
+                       copy_v3_v3(rv3d->ofs, v3d->ofs);
+                       copy_qt_qt(rv3d->viewquat, v3d->viewquat);
                }
        }
 
@@ -5693,7 +5724,7 @@ static void fix_relpaths_library(const char *basepath, Main *main)
                         * link into an unsaved blend file. See [#27405].
                         * The remap relative option will make it relative again on save - campbell */
                        if (strncmp(lib->name, "//", 2)==0) {
-                               strncpy(lib->name, lib->filepath, sizeof(lib->name));
+                               BLI_strncpy(lib->name, lib->filepath, sizeof(lib->name));
                        }
                }
        }
@@ -5702,7 +5733,7 @@ static void fix_relpaths_library(const char *basepath, Main *main)
                        /* Libraries store both relative and abs paths, recreate relative paths,
                         * relative to the blend file since indirectly linked libs will be relative to their direct linked library */
                        if (strncmp(lib->name, "//", 2)==0) { /* if this is relative to begin with? */
-                               strncpy(lib->name, lib->filepath, sizeof(lib->name));
+                               BLI_strncpy(lib->name, lib->filepath, sizeof(lib->name));
                                BLI_path_rel(lib->name, basepath);
                        }
                }
@@ -6700,16 +6731,6 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                //ar->v2d.flag |= V2D_IS_INITIALISED;
                                break;
                        }
-                       case SPACE_SOUND:
-                       {
-                               SpaceSound *ssound= (SpaceSound *)sl;
-                               memcpy(&ar->v2d, &ssound->v2d, sizeof(View2D));
-                               
-                               ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
-                               ar->v2d.scroll |= (V2D_SCROLL_LEFT);
-                               //ar->v2d.flag |= V2D_IS_INITIALISED;
-                               break;
-                       }
                        case SPACE_NLA:
                        {
                                SpaceNla *snla= (SpaceNla *)sl;
@@ -6825,11 +6846,17 @@ static void do_versions_windowmanager_2_50(bScreen *screen)
                
                area_add_window_regions(sa, sa->spacedata.first, &sa->regionbase);
                
-               /* space imageselect is depricated */
+               /* space imageselect is deprecated */
                for(sl= sa->spacedata.first; sl; sl= sl->next) {
                        if(sl->spacetype==SPACE_IMASEL)
-                               sl->spacetype= SPACE_INFO;      /* spacedata then matches */
-               }               
+                               sl->spacetype= SPACE_EMPTY;     /* spacedata then matches */
+               }
+               
+               /* space sound is deprecated */
+               for(sl= sa->spacedata.first; sl; sl= sl->next) {
+                       if(sl->spacetype==SPACE_SOUND)
+                               sl->spacetype= SPACE_EMPTY;     /* spacedata then matches */
+               }
                
                /* it seems to be possible in 2.5 to have this saved, filewindow probably */
                sa->butspacetype= sa->spacetype;
@@ -7164,7 +7191,7 @@ void convert_tface_mt(FileData *fd, Main *main)
                G.main = main;
 
                if(!(do_version_tface(main, 1))) {
-                       BKE_report(fd->reports, RPT_ERROR, "Texface conversion problem. Error in console");
+                       BKE_report(fd->reports, RPT_WARNING, "Texface conversion problem. Error in console");
                }
 
                //XXX hack, material.c uses G.main allover the place, instead of main
@@ -7172,6 +7199,53 @@ void convert_tface_mt(FileData *fd, Main *main)
        }
 }
 
+static void do_versions_nodetree_image_default_alpha_output(bNodeTree *ntree)
+{
+       bNode *node;
+       bNodeSocket *sock;
+       for (node=ntree->nodes.first; node; node=node->next) {
+               if (ELEM(node->type, CMP_NODE_IMAGE, CMP_NODE_R_LAYERS)) {
+                       /* default Image output value should have 0 alpha */
+                       sock = node->outputs.first;
+                       ((bNodeSocketValueRGBA*)sock->default_value)->value[3] = 0.0f;
+               }
+       }
+}
+
+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 */
@@ -8752,7 +8826,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                
                for (wo = main->world.first; wo; wo= wo->id.next) {
                        /* Migrate to Bullet for games, except for the NaN versions */
-                       /* People can still explicitely choose for Sumo (after 2.42 is out) */
+                       /* People can still explicitly choose for Sumo (after 2.42 is out) */
                        if(main->versionfile > 225)
                                wo->physicsEngine = WOPHY_BULLET;
                        if(WO_AODIST == wo->aomode)
@@ -9243,7 +9317,6 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
        }
        if(main->versionfile <= 245) {
                Scene *sce;
-               bScreen *sc;
                Object *ob;
                Image *ima;
                Lamp *la;
@@ -9333,49 +9406,6 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        for(ima= main->image.first; ima; ima= ima->id.next) {
                                ima->preview = NULL;
                        }
-                       
-                       /* repair imasel space - completely reworked */
-                       for(sc= main->screen.first; sc; sc= sc->id.next) {
-                               ScrArea *sa;
-                               sa= sc->areabase.first;
-                               while(sa) {
-                                       SpaceLink *sl;
-                                       
-                                       for (sl= sa->spacedata.first; sl; sl= sl->next) {
-                                               if(sl->spacetype==SPACE_IMASEL) {
-                                                       SpaceImaSel *simasel= (SpaceImaSel*) sl;
-                                                       simasel->blockscale= 0.7f;
-                                                       /* view 2D */
-                                                       simasel->v2d.tot.xmin=  -10.0f;
-                                                       simasel->v2d.tot.ymin=  -10.0f;
-                                                       simasel->v2d.tot.xmax= (float)sa->winx + 10.0f;
-                                                       simasel->v2d.tot.ymax= (float)sa->winy + 10.0f;                                         
-                                                       simasel->v2d.cur.xmin=  0.0f;
-                                                       simasel->v2d.cur.ymin=  0.0f;
-                                                       simasel->v2d.cur.xmax= (float)sa->winx;
-                                                       simasel->v2d.cur.ymax= (float)sa->winy;                                         
-                                                       simasel->v2d.min[0]= 1.0;
-                                                       simasel->v2d.min[1]= 1.0;                                               
-                                                       simasel->v2d.max[0]= 32000.0f;
-                                                       simasel->v2d.max[1]= 32000.0f;                                          
-                                                       simasel->v2d.minzoom= 0.5f;
-                                                       simasel->v2d.maxzoom= 1.21f;                                            
-                                                       simasel->v2d.scroll= 0;
-                                                       simasel->v2d.keepzoom= V2D_LIMITZOOM|V2D_KEEPASPECT;
-                                                       simasel->v2d.keeptot= 0;
-                                                       simasel->prv_h = 96;
-                                                       simasel->prv_w = 96;
-                                                       simasel->flag = 7; /* ??? elubie */
-                                                       strcpy (simasel->dir,  U.textudir);     /* TON */
-                                                       simasel->file[0]= '\0';
-                                                       
-                                                       simasel->returnfunc     =  NULL;
-                                                       simasel->title[0]       =  0;
-                                               }
-                                       }
-                                       sa = sa->next;
-                               }
-                       }
                }
 
                /* add point caches */
@@ -9589,7 +9619,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                                                ct= MEM_callocN(sizeof(bConstraintTarget), "PyConTarget");
                                                                
                                                                ct->tar = data->tar;
-                                                               strcpy(ct->subtarget, data->subtarget);
+                                                               BLI_strncpy(ct->subtarget, data->subtarget, sizeof(ct->subtarget));
                                                                ct->space = con->tarspace;
                                                                
                                                                BLI_addtail(&data->targets, ct);
@@ -9619,7 +9649,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                                ct= MEM_callocN(sizeof(bConstraintTarget), "PyConTarget");
                                                
                                                ct->tar = data->tar;
-                                               strcpy(ct->subtarget, data->subtarget);
+                                               BLI_strncpy(ct->subtarget, data->subtarget, sizeof(ct->subtarget));
                                                ct->space = con->tarspace;
                                                
                                                BLI_addtail(&data->targets, ct);
@@ -10091,7 +10121,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        sce->toolsettings->skgen_resolution = 250;
                        sce->toolsettings->skgen_threshold_internal     = 0.1f;
                        sce->toolsettings->skgen_threshold_external     = 0.1f;
-                       sce->toolsettings->skgen_angle_limit                    = 30.0f;
+                       sce->toolsettings->skgen_angle_limit                    = 30.0f;
                        sce->toolsettings->skgen_length_ratio                   = 1.3f;
                        sce->toolsettings->skgen_length_limit                   = 1.5f;
                        sce->toolsettings->skgen_correlation_limit              = 0.98f;
@@ -10306,8 +10336,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                {
                        if(scene->ed && scene->ed->seqbasep)
                        {
-                               for(seq = scene->ed->seqbasep->first; seq; seq = seq->next)
-                               {
+                               SEQ_BEGIN(scene->ed, seq) {
                                        if(seq->type == SEQ_HD_SOUND)
                                        {
                                                char str[FILE_MAX];
@@ -10327,6 +10356,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                                         seq->strip->dir);
                                        }
                                }
+                               SEQ_END
                        }
                }
 
@@ -10490,7 +10520,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                ma->mode |= MA_TRANSP;
                        }
                        else {
-                               ma->mode |= MA_ZTRANSP;
+                               /* ma->mode |= MA_ZTRANSP; */ /* leave ztransp as is even if its not used [#28113] */
                                ma->mode &= ~MA_TRANSP;
                        }
 
@@ -11861,7 +11891,8 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                        if(!mat->mtex[tex_nr]) continue;
                                        if(mat->mtex[tex_nr]->mapto & MAP_ALPHA) transp_tex= 1;
                                }
-                               
+
+                               /* weak! material alpha could be animated */
                                if(mat->alpha < 1.0f || mat->fresnel_tra > 0.0f || transp_tex){
                                        mat->mode |= MA_TRANSP;
                                        mat->mode &= ~(MA_ZTRANSP|MA_RAYTRANSP);
@@ -12098,8 +12129,8 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                                aa->flag = ia->flag;
                                                aa->sta = ia->sta;
                                                aa->end = ia->end;
-                                               strcpy(aa->name, ia->name);
-                                               strcpy(aa->frameProp, ia->frameProp);
+                                               BLI_strncpy(aa->name, ia->name, sizeof(aa->name));
+                                               BLI_strncpy(aa->frameProp, ia->frameProp, sizeof(aa->frameProp));
                                                if (ob->adt)
                                                        aa->act = ob->adt->action;
 
@@ -12187,49 +12218,175 @@ 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 < 4)){
+               {
+                       /* Adaptive time step for particle systems */
+                       ParticleSettings *part;
+                       for (part = main->particle.first; part; part = part->id.next) {
+                               part->courant_target = 0.2f;
+                               part->time_flag &= ~PART_TIME_AUTOSF;
+                       }
+               }
 
-       {
+               {
+                       /* set defaults for obstacle avoidance, recast data */
+                       Scene *sce;
+                       for(sce = main->scene.first; sce; sce = sce->id.next)
+                       {
+                               if (sce->gm.levelHeight == 0.f)
+                                       sce->gm.levelHeight = 2.f;
+
+                               if(sce->gm.recastData.cellsize == 0.0f)
+                                       sce->gm.recastData.cellsize = 0.3f;
+                               if(sce->gm.recastData.cellheight == 0.0f)
+                                       sce->gm.recastData.cellheight = 0.2f;
+                               if(sce->gm.recastData.agentmaxslope == 0.0f)
+                                       sce->gm.recastData.agentmaxslope = (float)M_PI/4;
+                               if(sce->gm.recastData.agentmaxclimb == 0.0f)
+                                       sce->gm.recastData.agentmaxclimb = 0.9f;
+                               if(sce->gm.recastData.agentheight == 0.0f)
+                                       sce->gm.recastData.agentheight = 2.0f;
+                               if(sce->gm.recastData.agentradius == 0.0f)
+                                       sce->gm.recastData.agentradius = 0.6f;
+                               if(sce->gm.recastData.edgemaxlen == 0.0f)
+                                       sce->gm.recastData.edgemaxlen = 12.0f;
+                               if(sce->gm.recastData.edgemaxerror == 0.0f)
+                                       sce->gm.recastData.edgemaxerror = 1.3f;
+                               if(sce->gm.recastData.regionminsize == 0.0f)
+                                       sce->gm.recastData.regionminsize = 8.f;
+                               if(sce->gm.recastData.regionmergesize == 0.0f)
+                                       sce->gm.recastData.regionmergesize = 20.f;
+                               if(sce->gm.recastData.vertsperpoly<3)
+                                       sce->gm.recastData.vertsperpoly = 6;
+                               if(sce->gm.recastData.detailsampledist == 0.0f)
+                                       sce->gm.recastData.detailsampledist = 6.0f;
+                               if(sce->gm.recastData.detailsamplemaxerror == 0.0f)
+                                       sce->gm.recastData.detailsamplemaxerror = 1.0f;
+                       }
+               }
+
+               {
+                       /* flip normals */
+                       Material *ma= main->mat.first;
+                       while(ma) {
+                               int a;
+                               for(a= 0; a<MAX_MTEX; a++) {
+                                       MTex *mtex= ma->mtex[a];
+
+                                       if(mtex) {
+                                               if((mtex->texflag&MTEX_BUMP_FLIPPED)==0) {
+                                                       if((mtex->mapto&MAP_DISPLACE)==0) {
+                                                               if((mtex->mapto&MAP_NORM) && mtex->texflag&(MTEX_COMPAT_BUMP|MTEX_3TAP_BUMP|MTEX_5TAP_BUMP)) {
+                                                                       Tex *tex= newlibadr(fd, lib, mtex->tex);
+
+                                                                       if(!tex || (tex->imaflag&TEX_NORMALMAP)==0) {
+                                                                               mtex->norfac= -mtex->norfac;
+                                                                               mtex->texflag|= MTEX_BUMP_FLIPPED;
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+
+                               ma= ma->id.next;
+                       }
+               }
 
        }
 
-       //set defaults for obstacle avoidance, recast data
-       {
+       if(main->versionfile < 259 || (main->versionfile == 259 && main->subversionfile < 5)) {
                Scene *sce;
-               for(sce = main->scene.first; sce; sce = sce->id.next)
+               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){
                {
-                       if (sce->gm.levelHeight == 0.f)
-                               sce->gm.levelHeight = 2.f;
-
-                       if(sce->gm.recastData.cellsize == 0.0f)
-                               sce->gm.recastData.cellsize = 0.3f;
-                       if(sce->gm.recastData.cellheight == 0.0f)
-                               sce->gm.recastData.cellheight = 0.2f;
-                       if(sce->gm.recastData.agentmaxslope == 0.0f)
-                               sce->gm.recastData.agentmaxslope = M_PI/4;
-                       if(sce->gm.recastData.agentmaxclimb == 0.0f)
-                               sce->gm.recastData.agentmaxclimb = 0.9f;
-                       if(sce->gm.recastData.agentheight == 0.0f)
-                               sce->gm.recastData.agentheight = 2.0f;
-                       if(sce->gm.recastData.agentradius == 0.0f)
-                               sce->gm.recastData.agentradius = 0.6f;
-                       if(sce->gm.recastData.edgemaxlen == 0.0f)
-                               sce->gm.recastData.edgemaxlen = 12.0f;
-                       if(sce->gm.recastData.edgemaxerror == 0.0f)
-                               sce->gm.recastData.edgemaxerror = 1.3f;
-                       if(sce->gm.recastData.regionminsize == 0.0f)
-                               sce->gm.recastData.regionminsize = 50.f;
-                       if(sce->gm.recastData.regionmergesize == 0.0f)
-                               sce->gm.recastData.regionmergesize = 20.f;
-                       if(sce->gm.recastData.vertsperpoly<3)
-                               sce->gm.recastData.vertsperpoly = 6;
-                       if(sce->gm.recastData.detailsampledist == 0.0f)
-                               sce->gm.recastData.detailsampledist = 6.0f;
-                       if(sce->gm.recastData.detailsamplemaxerror == 0.0f)
-                               sce->gm.recastData.detailsamplemaxerror = 1.0f;
-               }                       
+                       /* set default alpha value of Image outputs in image and render layer nodes to 0 */
+                       Scene *sce;
+                       bNodeTree *ntree;
+                       
+                       for (sce=main->scene.first; sce; sce=sce->id.next) {
+                               /* there are files with invalid audio_channels value, the real cause
+                                  is unknown, but we fix it here anyway to avoid crashes */
+                               if(sce->r.ffcodecdata.audio_channels == 0)
+                                       sce->r.ffcodecdata.audio_channels = 2;
+
+                               if (sce->nodetree)
+                                       do_versions_nodetree_image_default_alpha_output(sce->nodetree);
+                       }
+
+                       for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next)
+                               do_versions_nodetree_image_default_alpha_output(ntree);
+               }
+
+               {
+                       /* support old particle dupliobject rotation settings */
+                       ParticleSettings *part;
+
+                       for (part=main->particle.first; part; part=part->id.next) {
+                               if(ELEM(part->ren_as, PART_DRAW_OB, PART_DRAW_GR)) {
+                                       part->draw |= PART_DRAW_ROTATE_OB;
+
+                                       if(part->rotmode == 0)
+                                               part->rotmode = PART_ROT_VEL;
+                               }
+                       }
+               }
+
        }
-       
+
+       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! */
        /* WATCH IT 2!: Userdef struct init has to be in editors/interface/resources.c! */
 
@@ -12357,7 +12514,7 @@ BlendFileData *blo_read_file_internal(FileData *fd, const char *filepath)
        bfd->main->versionfile= fd->fileversion;
        
        bfd->type= BLENFILETYPE_BLEND;
-       strncpy(bfd->main->name, filepath, sizeof(bfd->main->name)-1);
+       BLI_strncpy(bfd->main->name, filepath, sizeof(bfd->main->name));
 
        while(bhead) {
                switch(bhead->code) {
@@ -13347,7 +13504,7 @@ static void give_base_to_objects(Main *mainvar, Scene *sce, Library *lib, const
                if( ob->id.flag & LIB_INDIRECT ) {
                        
                                /* IF below is quite confusing!
-                               if we are appending, but this object wasnt just added allong with a group,
+                               if we are appending, but this object wasnt just added along with a group,
                                then this is already used indirectly in the scene somewhere else and we didnt just append it.
                                
                                (ob->id.flag & LIB_PRE_EXISTING)==0 means that this is a newly appended object - Campbell */
@@ -13695,8 +13852,8 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                                        printf("  enter a new path:\n");
 
                                                        if(scanf("%s", newlib_path) > 0) {
-                                                               strcpy(mainptr->curlib->name, newlib_path);
-                                                               strcpy(mainptr->curlib->filepath, newlib_path);
+                                                               BLI_strncpy(mainptr->curlib->name, newlib_path, sizeof(mainptr->curlib->name));
+                                                               BLI_strncpy(mainptr->curlib->filepath, newlib_path, sizeof(mainptr->curlib->filepath));
                                                                cleanup_path(G.main->name, mainptr->curlib->filepath);
                                                                
                                                                fd= blo_openblenderfile(mainptr->curlib->filepath, basefd->reports);
@@ -13812,7 +13969,7 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
 
 /* reading runtime */
 
-BlendFileData *blo_read_blendafterruntime(int file, char *name, int actualsize, ReportList *reports)
+BlendFileData *blo_read_blendafterruntime(int file, const char *name, int actualsize, ReportList *reports)
 {
        BlendFileData *bfd = NULL;
        FileData *fd = filedata_new();