synched with trunk at revision 30243
[blender.git] / source / blender / blenloader / intern / readfile.c
index 34baa0d..41be946 100644 (file)
 #include "BKE_sequencer.h"
 #include "BKE_texture.h" // for open_plugin_tex
 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
-
+#include "BKE_ipo.h"
 #include "BKE_sound.h"
 
 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
@@ -507,7 +507,7 @@ static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *name, c
 //     printf("blo_find_main: converted to %s\n", name1);
 
        for (m= mainlist->first; m; m= m->next) {
-               char *libname= (m->curlib)?m->curlib->filename:m->name;
+               char *libname= (m->curlib)?m->curlib->filepath:m->name;
                
                if (BLI_streq(name1, libname)) {
                        if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
@@ -520,7 +520,7 @@ static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *name, c
 
        lib= alloc_libblock(&m->library, ID_LI, "lib");
        strncpy(lib->name, name, sizeof(lib->name)-1);
-       BLI_strncpy(lib->filename, name1, sizeof(lib->filename));
+       BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
        
        m->curlib= lib;
        
@@ -959,11 +959,11 @@ static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
 FileData *blo_openblenderfile(char *name, ReportList *reports)
 {
        gzFile gzfile;
-       
+       errno= 0;
        gzfile= gzopen(name, "rb");
 
-       if (NULL == gzfile) {
-               BKE_report(reports, RPT_ERROR, "Unable to open");
+       if (gzfile == Z_NULL) {
+               BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", name, errno ? strerror(errno) : "Unknown erro reading file");
                return NULL;
        } else {
                FileData *fd = filedata_new();
@@ -996,7 +996,7 @@ FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
 {
        if (!memfile) {
-               BKE_report(reports, RPT_ERROR, "Unable to open");
+               BKE_report(reports, RPT_ERROR, "Unable to open blend <memory>");
                return NULL;
        } else {
                FileData *fd= filedata_new();
@@ -2244,7 +2244,7 @@ static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
        }
        
        if(rebuild) {
-               ob->recalc= OB_RECALC;
+               ob->recalc= OB_RECALC_ALL;
                pose->flag |= POSE_RECALC;
        }
 }
@@ -2839,6 +2839,7 @@ static void direct_link_texture(FileData *fd, Tex *tex)
        tex->vd= newdataadr(fd, tex->vd);
        if(tex->vd) {
                tex->vd->dataset = NULL;
+               tex->vd->ok = 0;
        }
        
        tex->nodetree= newdataadr(fd, tex->nodetree);
@@ -3073,6 +3074,7 @@ static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase
                        for(; pt; pt=pt->next)
                                pt->ob=newlibadr(fd, id->lib, pt->ob);
 
+                       psys->parent= newlibadr_us(fd, id->lib, psys->parent);
                        psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
 
                        if(psys->clmd) {
@@ -3142,7 +3144,7 @@ static void direct_link_particlesystems(FileData *fd, ListBase *particles)
                psys->childcachebufs.first = psys->childcachebufs.last = NULL;
                psys->frand = NULL;
                psys->pdd = NULL;
-
+               
                direct_link_pointcache_list(fd, &psys->ptcaches, &psys->pointcache);
 
                if(psys->clmd) {
@@ -3456,7 +3458,7 @@ static void lib_link_object(FileData *fd, Main *main)
                                        /* this triggers object_update to always use a copy */
                                        ob->proxy->proxy_from= ob;
                                        /* force proxy updates after load/undo, a bit weak */
-                                       ob->recalc= ob->proxy->recalc= OB_RECALC;
+                                       ob->recalc= ob->proxy->recalc= OB_RECALC_ALL;
                                }
                        }
                        ob->proxy_group= newlibadr(fd, ob->id.lib, ob->proxy_group);
@@ -4562,6 +4564,7 @@ static void lib_link_screen(FileData *fd, Main *main)
                if(sc->id.flag & LIB_NEEDLINK) {
                        sc->id.us= 1;
                        sc->scene= newlibadr(fd, sc->id.lib, sc->scene);
+                       sc->animtimer= NULL; /* saved in rare cases */
                        
                        sa= sc->areabase.first;
                        while(sa) {
@@ -5164,6 +5167,10 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                }
                                snode->nodetree= snode->edittree= NULL;
                        }
+                       else if(sl->spacetype==SPACE_TIME) {
+                               SpaceTime *stime= (SpaceTime *)sl;
+                               stime->caches.first= stime->caches.last= NULL;
+                       }
                        else if(sl->spacetype==SPACE_LOGIC) {
                                SpaceLogic *slogic= (SpaceLogic *)sl;
                                        
@@ -5237,8 +5244,8 @@ static void direct_link_library(FileData *fd, Library *lib, Main *main)
        
        for(newmain= fd->mainlist.first; newmain; newmain= newmain->next) {
                if(newmain->curlib) {
-                       if(strcmp(newmain->curlib->filename, lib->filename)==0) {
-                               printf("Fixed error in file; multiple instances of lib:\n %s\n", lib->filename);
+                       if(strcmp(newmain->curlib->filepath, lib->filepath)==0) {
+                               printf("Fixed error in file; multiple instances of lib:\n %s\n", lib->filepath);
                                
                                change_idid_adr(&fd->mainlist, fd, lib, newmain->curlib);
 //                             change_idid_adr_fd(fd, lib, newmain->curlib);
@@ -5253,8 +5260,8 @@ static void direct_link_library(FileData *fd, Library *lib, Main *main)
                }
        }
        /* make sure we have full path in lib->filename */
-       BLI_strncpy(lib->filename, lib->name, sizeof(lib->name));
-       cleanup_path(fd->relabase, lib->filename);
+       BLI_strncpy(lib->filepath, lib->name, sizeof(lib->name));
+       cleanup_path(fd->relabase, lib->filepath);
        
 //     printf("direct_link_library: name %s\n", lib->name);
 //     printf("direct_link_library: filename %s\n", lib->filename);
@@ -5287,7 +5294,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->filename, sizeof(lib->name));
+                       strncpy(lib->name, lib->filepath, sizeof(lib->name));
                        BLI_path_rel(lib->name, basepath);
                }
        }
@@ -7890,7 +7897,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        if(ob->type==OB_ARMATURE) {
                                if(ob->pose)
                                        ob->pose->flag |= POSE_RECALC;
-                               ob->recalc |= OB_RECALC;        // cannot call stuff now (pointers!), done in setup_app_data
+                               ob->recalc |= OB_RECALC_ALL;    // cannot call stuff now (pointers!), done in setup_app_data
 
                                /* new generic xray option */
                                arm= newlibadr(fd, lib, ob->data);
@@ -9737,11 +9744,6 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        do_versions_gpencil_2_50(main, screen);
                }
                
-               /* old Animation System (using IPO's) needs to be converted to the new Animato system 
-                * (NOTE: conversion code in blenkernel/intern/ipo.c for now)
-                */
-               //do_versions_ipos_to_animato(main);
-               
                /* shader, composit and texture node trees have id.name empty, put something in
                 * to have them show in RNA viewer and accessible otherwise.
                 */
@@ -10856,7 +10858,9 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
        /* put 2.50 compatibility code here until next subversion bump */
        {
                Object *ob;
+               Scene *scene;
                bScreen *sc;
+               Tex *tex;
 
                for (sc= main->screen.first; sc; sc= sc->id.next) {
                        ScrArea *sa;
@@ -10864,13 +10868,36 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                SpaceLink *sl;
                                for (sl= sa->spacedata.first; sl; sl= sl->next) {
                                        if (sl->spacetype == SPACE_NODE) {
-                                               SpaceNode *snode;
+                                               SpaceNode *snode= (SpaceNode *)sl;
+                                               ListBase *regionbase;
+                                               ARegion *ar;
+
+                                               if (sl == sa->spacedata.first)
+                                                       regionbase = &sa->regionbase;
+                                               else
+                                                       regionbase = &sl->regionbase;
 
-                                               snode= (SpaceNode *)sl;
                                                if (snode->v2d.minzoom > 0.09f)
                                                        snode->v2d.minzoom= 0.09f;
                                                if (snode->v2d.maxzoom < 2.31f)
                                                        snode->v2d.maxzoom= 2.31f;
+
+                                               for (ar= regionbase->first; ar; ar= ar->next) {
+                                                       if (ar->regiontype == RGN_TYPE_WINDOW) {
+                                                               if (ar->v2d.minzoom > 0.09f)
+                                                                       ar->v2d.minzoom= 0.09f;
+                                                               if (ar->v2d.maxzoom < 2.31f)
+                                                                       ar->v2d.maxzoom= 2.31f;
+                                                       }
+                                               }
+                                       }
+                                       else if (sl->spacetype == SPACE_TIME) {
+                                               SpaceTime *stime= (SpaceTime *)sl;
+                                               
+                                               /* enable all cache display */
+                                               stime->cache_display |= TIME_CACHE_DISPLAY;
+                                               stime->cache_display |= (TIME_CACHE_SOFTBODY|TIME_CACHE_PARTICLES);
+                                               stime->cache_display |= (TIME_CACHE_CLOTH|TIME_CACHE_SMOKE);
                                        }
                                }
                        }
@@ -10912,6 +10939,24 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                }
                        }
                }
+               
+               /* initialise scene active layer */
+               for (scene= main->scene.first; scene; scene=scene->id.next) {
+                       int i;
+                       for(i=0; i<20; i++) {
+                               if(scene->lay & (1<<i)) {
+                                       scene->layact= 1<<i;
+                                       break;
+                               }
+                       }
+               }
+
+               for(tex= main->tex.first; tex; tex= tex->id.next) {
+                       /* if youre picky, this isn't correct until we do a version bump
+                        * since you could set saturation to be 0.0*/
+                       if(tex->saturation==0.0f)
+                               tex->saturation= 1.0f;
+               }
 
                //set defaults for recast data
                {
@@ -10949,13 +10994,21 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        }                       
                }
        }
-
        /* 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! */
 
        /* don't forget to set version number in blender.c! */
 }
 
+#if 0 // XXX: disabled for now... we still don't have this in the right place in the loading code for it to work
+static void do_versions_after_linking(FileData *fd, Library *lib, Main *main)
+{
+       /* old Animation System (using IPO's) needs to be converted to the new Animato system */
+       if(main->versionfile < 250)
+               do_versions_ipos_to_animato(main);
+}
+#endif
+
 static void lib_link_all(FileData *fd, Main *main)
 {
        oldnewmap_sort(fd);
@@ -11102,6 +11155,7 @@ BlendFileData *blo_read_file_internal(FileData *fd, const char *filename)
        blo_join_main(&fd->mainlist);
 
        lib_link_all(fd, bfd->main);
+       //do_versions_after_linking(fd, NULL, bfd->main); // XXX: not here (or even in this function at all)! this causes crashes on many files - Aligorith (July 04, 2010)
        lib_verify_nodetree(bfd->main, 1);
        fix_relpaths_library(fd->relabase, bfd->main); /* make all relative paths, relative to the open blend file */
        
@@ -12067,7 +12121,7 @@ static void give_base_to_groups(Main *mainvar, Scene *scene)
                        base= scene_add_base(scene, ob);
                        base->flag |= SELECT;
                        base->object->flag= base->flag;
-                       ob->recalc |= OB_RECALC;
+                       ob->recalc |= OB_RECALC_ALL;
                        scene->basact= base;
 
                        /* assign the group */
@@ -12126,6 +12180,7 @@ static void append_named_part(const bContext *C, Main *mainl, FileData *fd, char
                                                        ob->lay = scene->lay;
                                                }
                                        }
+                                       ob->mode= 0;
                                        base->lay= ob->lay;
                                        base->object= ob;
                                        ob->id.us++;
@@ -12261,7 +12316,7 @@ static void library_append_end(const bContext *C, Main *mainl, FileData **fd, in
        if(flag & FILE_RELPATH) {
 
                /* use the full path, this could have been read by other library even */
-               BLI_strncpy(mainl->curlib->name, mainl->curlib->filename, sizeof(mainl->curlib->name));
+               BLI_strncpy(mainl->curlib->name, mainl->curlib->filepath, sizeof(mainl->curlib->name));
                
                /* uses current .blend file as reference */
                BLI_path_rel(mainl->curlib->name, G.sce);
@@ -12378,10 +12433,10 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                if(fd==NULL) {
 
                                        /* printf and reports for now... its important users know this */
-                                       BKE_reportf(basefd->reports, RPT_INFO, "read library:  '%s', '%s'\n", mainptr->curlib->filename, mainptr->curlib->name);
-                                       if(!G.background && basefd->reports) printf("read library: '%s', '%s'\n", mainptr->curlib->filename, mainptr->curlib->name);
+                                       BKE_reportf(basefd->reports, RPT_INFO, "read library:  '%s', '%s'\n", mainptr->curlib->filepath, mainptr->curlib->name);
+                                       if(!G.background && basefd->reports) printf("read library: '%s', '%s'\n", mainptr->curlib->filepath, mainptr->curlib->name);
 
-                                       fd= blo_openblenderfile(mainptr->curlib->filename, basefd->reports);
+                                       fd= blo_openblenderfile(mainptr->curlib->filepath, basefd->reports);
                                        
                                        /* allow typing in a new lib path */
                                        if(G.rt==-666) {
@@ -12389,19 +12444,19 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                                        char newlib_path[240] = { 0 };
                                                        printf("Missing library...'\n");
                                                        printf("        current file: %s\n", G.sce);
-                                                       printf("        absolute lib: %s\n", mainptr->curlib->filename);
+                                                       printf("        absolute lib: %s\n", mainptr->curlib->filepath);
                                                        printf("        relative lib: %s\n", mainptr->curlib->name);
                                                        printf("  enter a new path:\n");
 
                                                        if(scanf("%s", newlib_path) > 0) {
                                                                strcpy(mainptr->curlib->name, newlib_path);
-                                                               strcpy(mainptr->curlib->filename, newlib_path);
-                                                               cleanup_path(G.sce, mainptr->curlib->filename);
+                                                               strcpy(mainptr->curlib->filepath, newlib_path);
+                                                               cleanup_path(G.sce, mainptr->curlib->filepath);
                                                                
-                                                               fd= blo_openblenderfile(mainptr->curlib->filename, basefd->reports);
+                                                               fd= blo_openblenderfile(mainptr->curlib->filepath, basefd->reports);
 
                                                                if(fd) {
-                                                                       printf("found: '%s', party on macuno!\n", mainptr->curlib->filename);
+                                                                       printf("found: '%s', party on macuno!\n", mainptr->curlib->filepath);
                                                                }
                                                        }
                                                }
@@ -12424,8 +12479,8 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                        else mainptr->curlib->filedata= NULL;
 
                                        if (fd==NULL) {
-                                               BKE_reportf(basefd->reports, RPT_ERROR, "Can't find lib '%s'\n", mainptr->curlib->filename);
-                                               if(!G.background && basefd->reports) printf("ERROR: can't find lib %s \n", mainptr->curlib->filename);
+                                               BKE_reportf(basefd->reports, RPT_ERROR, "Can't find lib '%s'\n", mainptr->curlib->filepath);
+                                               if(!G.background && basefd->reports) printf("ERROR: can't find lib %s \n", mainptr->curlib->filepath);
                                        }
                                }
                                if(fd) {
@@ -12442,8 +12497,8 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
 
                                                                append_id_part(fd, mainptr, id, &realid);
                                                                if (!realid) {
-                                                                       BKE_reportf(fd->reports, RPT_ERROR, "LIB ERROR: %s:'%s' missing from '%s'\n", BLO_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filename);
-                                                                       if(!G.background && basefd->reports) printf("LIB ERROR: %s:'%s' missing from '%s'\n", BLO_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filename);
+                                                                       BKE_reportf(fd->reports, RPT_ERROR, "LIB ERROR: %s:'%s' missing from '%s'\n", BLO_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
+                                                                       if(!G.background && basefd->reports) printf("LIB ERROR: %s:'%s' missing from '%s'\n", BLO_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
                                                                }
                                                                
                                                                change_idid_adr(mainlist, basefd, id, realid);
@@ -12478,8 +12533,8 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                ID *idn= id->next;
                                if(id->flag & LIB_READ) {
                                        BLI_remlink(lbarray[a], id);
-                                       BKE_reportf(basefd->reports, RPT_ERROR, "LIB ERROR: %s:'%s' unread libblock missing from '%s'\n", BLO_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filename);
-                                       if(!G.background && basefd->reports)printf("LIB ERROR: %s:'%s' unread libblock missing from '%s'\n", BLO_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filename);
+                                       BKE_reportf(basefd->reports, RPT_ERROR, "LIB ERROR: %s:'%s' unread libblock missing from '%s'\n", BLO_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
+                                       if(!G.background && basefd->reports)printf("LIB ERROR: %s:'%s' unread libblock missing from '%s'\n", BLO_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
                                        change_idid_adr(mainlist, basefd, id, NULL);
 
                                        MEM_freeN(id);