Merging r46725 through r46963 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenloader / intern / readfile.c
index 3b6b888..edea4cc 100644 (file)
 
 #include "NOD_socket.h"
 
-//XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
-//XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
-//XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
 #include "BLO_readfile.h"
 #include "BLO_undofile.h"
 
@@ -264,18 +261,18 @@ static void convert_tface_mt(FileData *fd, Main *main);
 static void BKE_reportf_wrap(ReportList *reports, ReportType type, const char *format, ...)
 {
        char fixed_buf[1024]; /* should be long enough */
-
+       
        va_list args;
-
+       
        va_start(args, format);
        vsnprintf(fixed_buf, sizeof(fixed_buf), format, args);
        va_end(args);
-
+       
        fixed_buf[sizeof(fixed_buf) - 1] = '\0';
-
+       
        BKE_report(reports, type, fixed_buf);
-
-       if (G.background==0) {
+       
+       if (G.background == 0) {
                printf("%s\n", fixed_buf);
        }
 }
@@ -284,8 +281,8 @@ static OldNewMap *oldnewmap_new(void)
 {
        OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
        
-       onm->entriessize= 1024;
-       onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
+       onm->entriessize = 1024;
+       onm->entries = MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
        
        return onm;
 }
@@ -294,8 +291,8 @@ static int verg_oldnewmap(const void *v1, const void *v2)
 {
        const struct OldNew *x1=v1, *x2=v2;
        
-       if ( x1->old > x2->old) return 1;
-       else if ( x1->old < x2->old) return -1;
+       if (x1->old > x2->old) return 1;
+       else if (x1->old < x2->old) return -1;
        return 0;
 }
 
@@ -303,31 +300,31 @@ static int verg_oldnewmap(const void *v1, const void *v2)
 static void oldnewmap_sort(FileData *fd) 
 {
        qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
-       fd->libmap->sorted= 1;
+       fd->libmap->sorted = 1;
 }
 
 /* nr is zero for data, and ID code for libdata */
 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr) 
 {
        OldNew *entry;
-
+       
        if (oldaddr==NULL || newaddr==NULL) return;
        
-       if (onm->nentries==onm->entriessize) {
-               int osize= onm->entriessize;
-               OldNew *oentries= onm->entries;
-
-               onm->entriessize*= 2;
-               onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
-
+       if (onm->nentries == onm->entriessize) {
+               int osize = onm->entriessize;
+               OldNew *oentries = onm->entries;
+               
+               onm->entriessize *= 2;
+               onm->entries = MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
+               
                memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
                MEM_freeN(oentries);
        }
 
-       entry= &onm->entries[onm->nentries++];
-       entry->old= oldaddr;
-       entry->newp= newaddr;
-       entry->nr= nr;
+       entry = &onm->entries[onm->nentries++];
+       entry->old = oldaddr;
+       entry->newp = newaddr;
+       entry->nr = nr;
 }
 
 void blo_do_versions_oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
@@ -338,29 +335,29 @@ void blo_do_versions_oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newad
 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr) 
 {
        int i;
-
-       if (addr==NULL) return NULL;
-
-       if (onm->lasthit<onm->nentries-1) {
-               OldNew *entry= &onm->entries[++onm->lasthit];
-
-               if (entry->old==addr) {
+       
+       if (addr == NULL) return NULL;
+       
+       if (onm->lasthit < onm->nentries-1) {
+               OldNew *entry = &onm->entries[++onm->lasthit];
+               
+               if (entry->old == addr) {
                        entry->nr++;
                        return entry->newp;
                }
        }
-
-       for (i=0; i<onm->nentries; i++) {
-               OldNew *entry= &onm->entries[i];
-
-               if (entry->old==addr) {
-                       onm->lasthit= i;
-
+       
+       for (i = 0; i < onm->nentries; i++) {
+               OldNew *entry = &onm->entries[i];
+               
+               if (entry->old == addr) {
+                       onm->lasthit = i;
+                       
                        entry->nr++;
                        return entry->newp;
                }
        }
-
+       
        return NULL;
 }
 
@@ -369,17 +366,17 @@ static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
 {
        int i;
        
-       if (addr==NULL) return NULL;
+       if (addr == NULL) return NULL;
        
        /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
        if (onm->sorted) {
                OldNew entry_s, *entry;
                
-               entry_s.old= addr;
+               entry_s.old = addr;
                
-               entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
+               entry = bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
                if (entry) {
-                       ID *id= entry->newp;
+                       ID *id = entry->newp;
                        
                        if (id && (!lib || id->lib)) {
                                return entry->newp;
@@ -387,12 +384,12 @@ static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
                }
        }
        
-       for (i=0; i<onm->nentries; i++) {
-               OldNew *entry= &onm->entries[i];
-
-               if (entry->old==addr) {
-                       ID *id= entry->newp;
-
+       for (i = 0; i < onm->nentries; i++) {
+               OldNew *entry = &onm->entries[i];
+               
+               if (entry->old == addr) {
+                       ID *id = entry->newp;
+                       
                        if (id && (!lib || id->lib)) {
                                return entry->newp;
                        }
@@ -406,19 +403,19 @@ static void oldnewmap_free_unused(OldNewMap *onm)
 {
        int i;
 
-       for (i=0; i<onm->nentries; i++) {
-               OldNew *entry= &onm->entries[i];
-               if (entry->nr==0) {
+       for (i = 0; i < onm->nentries; i++) {
+               OldNew *entry = &onm->entries[i];
+               if (entry->nr == 0) {
                        MEM_freeN(entry->newp);
-                       entry->newp= NULL;
+                       entry->newp = NULL;
                }
        }
 }
 
 static void oldnewmap_clear(OldNewMap *onm) 
 {
-       onm->nentries= 0;
-       onm->lasthit= 0;
+       onm->nentries = 0;
+       onm->lasthit = 0;
 }
 
 static void oldnewmap_free(OldNewMap *onm) 
@@ -437,9 +434,9 @@ static void add_main_to_main(Main *mainvar, Main *from)
 {
        ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
        int a;
-
+       
        set_listbasepointers(mainvar, lbarray);
-       a= set_listbasepointers(from, fromarray);
+       a = set_listbasepointers(from, fromarray);
        while (a--) {
                BLI_movelisttolist(lbarray[a], fromarray[a]);
        }
@@ -449,9 +446,8 @@ void blo_join_main(ListBase *mainlist)
 {
        Main *tojoin, *mainl;
        
-       
-       mainl= mainlist->first;
-       while ((tojoin= mainl->next)) {
+       mainl = mainlist->first;
+       while ((tojoin = mainl->next)) {
                add_main_to_main(mainl, tojoin);
                BLI_remlink(mainlist, tojoin);
                MEM_freeN(tojoin);
@@ -463,24 +459,24 @@ static void split_libdata(ListBase *lb, Main *first)
        ListBase *lbn;
        ID *id, *idnext;
        Main *mainvar;
-
-       id= lb->first;
+       
+       id = lb->first;
        while (id) {
-               idnext= id->next;
+               idnext = id->next;
                if (id->lib) {
-                       mainvar= first;
+                       mainvar = first;
                        while (mainvar) {
-                               if (mainvar->curlib==id->lib) {
+                               if (mainvar->curlib == id->lib) {
                                        lbn= which_libbase(mainvar, GS(id->name));
                                        BLI_remlink(lb, id);
                                        BLI_addtail(lbn, id);
                                        break;
                                }
-                               mainvar= mainvar->next;
+                               mainvar = mainvar->next;
                        }
-                       if (mainvar==NULL) printf("error split_libdata\n");
+                       if (mainvar == NULL) printf("error split_libdata\n");
                }
-               id= idnext;
+               id = idnext;
        }
 }
 
@@ -489,20 +485,20 @@ void blo_split_main(ListBase *mainlist, Main *main)
        ListBase *lbarray[MAX_LIBARRAY];
        Library *lib;
        int i;
-
-       mainlist->first= mainlist->last= main;
-       main->next= NULL;
-
-       if (main->library.first==NULL)
+       
+       mainlist->first = mainlist->last = main;
+       main->next = NULL;
+       
+       if (main->library.first == NULL)
                return;
        
-       for (lib= main->library.first; lib; lib= lib->id.next) {
-               Main *libmain= MEM_callocN(sizeof(Main), "libmain");
-               libmain->curlib= lib;
+       for (lib = main->library.first; lib; lib = lib->id.next) {
+               Main *libmain = MEM_callocN(sizeof(Main), "libmain");
+               libmain->curlib = lib;
                BLI_addtail(mainlist, libmain);
        }
-
-       i= set_listbasepointers(main, lbarray);
+       
+       i = set_listbasepointers(main, lbarray);
        while (i--)
                split_libdata(lbarray[i], main->next);
 }
@@ -522,7 +518,7 @@ static void read_file_version(FileData *fd, Main *main)
        BHead *bhead;
        
        for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
-               if (bhead->code==GLOB) {
+               if (bhead->code == GLOB) {
                        FileGlobal *fg= read_struct(fd, bhead, "Global");
                        if (fg) {
                                main->subversionfile= fg->subversion;
@@ -530,7 +526,7 @@ static void read_file_version(FileData *fd, Main *main)
                                main->minsubversionfile= fg->minsubversion;
                                MEM_freeN(fg);
                        }
-                       else if (bhead->code==ENDB)
+                       else if (bhead->code == ENDB)
                                break;
                }
        }
@@ -547,24 +543,24 @@ static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *filepat
        cleanup_path(relabase, name1);
 //     printf("blo_find_main: original in  %s\n", name);
 //     printf("blo_find_main: converted to %s\n", name1);
-
-       for (m= mainlist->first; m; m= m->next) {
-               char *libname= (m->curlib)?m->curlib->filepath:m->name;
+       
+       for (m = mainlist->first; m; m = m->next) {
+               char *libname = (m->curlib) ? m->curlib->filepath : m->name;
                
                if (BLI_path_cmp(name1, libname) == 0) {
                        if (G.debug & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
                        return m;
                }
        }
-
-       m= MEM_callocN(sizeof(Main), "find_main");
+       
+       m = MEM_callocN(sizeof(Main), "find_main");
        BLI_addtail(mainlist, m);
-
-       lib= BKE_libblock_alloc(&m->library, ID_LI, "lib");
+       
+       lib = BKE_libblock_alloc(&m->library, ID_LI, "lib");
        BLI_strncpy(lib->name, filepath, sizeof(lib->name));
        BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
        
-       m->curlib= lib;
+       m->curlib = lib;
        
        read_file_version(fd, m);
        
@@ -578,8 +574,8 @@ static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *filepat
 static void switch_endian_bh4(BHead4 *bhead)
 {
        /* the ID_.. codes */
-       if ((bhead->code & 0xFFFF)==0) bhead->code >>=16;
-
+       if ((bhead->code & 0xFFFF)==0) bhead->code >>= 16;
+       
        if (bhead->code != ENDB) {
                SWITCH_INT(bhead->len);
                SWITCH_INT(bhead->SDNAnr);
@@ -590,8 +586,8 @@ static void switch_endian_bh4(BHead4 *bhead)
 static void switch_endian_bh8(BHead8 *bhead)
 {
        /* the ID_.. codes */
-       if ((bhead->code & 0xFFFF)==0) bhead->code >>=16;
-
+       if ((bhead->code & 0xFFFF)==0) bhead->code >>= 16;
+       
        if (bhead->code != ENDB) {
                SWITCH_INT(bhead->len);
                SWITCH_INT(bhead->SDNAnr);
@@ -608,37 +604,37 @@ static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
        long long old;
 #endif
 
-       bhead4->code= bhead8->code;
-       bhead4->len= bhead8->len;
+       bhead4->code = bhead8->code;
+       bhead4->len = bhead8->len;
 
        if (bhead4->code != ENDB) {
-
-               //perform a endian swap on 64bit pointers, otherwise the pointer might map to zero
-               //0x0000000000000000000012345678 would become 0x12345678000000000000000000000000
+               /* perform a endian swap on 64bit pointers, otherwise the pointer might map to zero
+                * 0x0000000000000000000012345678 would become 0x12345678000000000000000000000000
+                */
                if (do_endian_swap) {
                        SWITCH_LONGINT(bhead8->old);
                }
-
+               
                /* this patch is to avoid a long long being read from not-eight aligned positions
                 * is necessary on any modern 64bit architecture) */
                memcpy(&old, &bhead8->old, 8);
                bhead4->old = (int) (old >> 3);
-
-               bhead4->SDNAnr= bhead8->SDNAnr;
-               bhead4->nr= bhead8->nr;
+               
+               bhead4->SDNAnr = bhead8->SDNAnr;
+               bhead4->nr = bhead8->nr;
        }
 }
 
 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
 {
        BHead8 *bhead8 = (BHead8 *) bhead;
-
-       bhead8->code= bhead4->code;
-       bhead8->len= bhead4->len;
-
+       
+       bhead8->code = bhead4->code;
+       bhead8->len = bhead4->len;
+       
        if (bhead8->code != ENDB) {
-               bhead8->old= bhead4->old;
-               bhead8->SDNAnr= bhead4->SDNAnr;
+               bhead8->old = bhead4->old;
+               bhead8->SDNAnr = bhead4->SDNAnr;
                bhead8->nr= bhead4->nr;
        }
 }
@@ -649,29 +645,29 @@ static BHeadN *get_bhead(FileData *fd)
        int readsize;
        
        if (fd) {
-               if ( ! fd->eof) {
+               if (!fd->eof) {
                        /* initializing to zero isn't strictly needed but shuts valgrind up
                         * since uninitialized memory gets compared */
-                       BHead8 bhead8= {0};
-                       BHead4 bhead4= {0};
-                       BHead  bhead= {0};
+                       BHead8 bhead8 = {0};
+                       BHead4 bhead4 = {0};
+                       BHead  bhead = {0};
                        
-                       // First read the bhead structure.
-                       // Depending on the platform the file was written on this can
-                       // be a big or little endian BHead4 or BHead8 structure.
-
-                       // As usual 'ENDB' (the last *partial* bhead of the file)
-                       // needs some special handling. We don't want to EOF just yet.
-
+                       /* First read the bhead structure.
+                        * Depending on the platform the file was written on this can
+                        * be a big or little endian BHead4 or BHead8 structure.
+                        *
+                        * As usual 'ENDB' (the last *partial* bhead of the file)
+                        * needs some special handling. We don't want to EOF just yet.
+                        */
                        if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
                                bhead4.code = DATA;
                                readsize = fd->read(fd, &bhead4, sizeof(bhead4));
-
+                               
                                if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
                                        if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
                                                switch_endian_bh4(&bhead4);
                                        }
-
+                                       
                                        if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
                                                bh8_from_bh4(&bhead, &bhead4);
                                        }
@@ -687,12 +683,12 @@ static BHeadN *get_bhead(FileData *fd)
                        else {
                                bhead8.code = DATA;
                                readsize = fd->read(fd, &bhead8, sizeof(bhead8));
-
+                               
                                if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
                                        if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
                                                switch_endian_bh8(&bhead8);
                                        }
-
+                                       
                                        if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
                                                bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
                                        }
@@ -705,21 +701,21 @@ static BHeadN *get_bhead(FileData *fd)
                                        bhead.len= 0;
                                }
                        }
-
+                       
                        /* make sure people are not trying to pass bad blend files */
                        if (bhead.len < 0) fd->eof = 1;
-
-                       // bhead now contains the (converted) bhead structure. Now read
-                       // the associated data and put everything in a BHeadN (creative naming !)
-
-                       if ( ! fd->eof) {
+                       
+                       /* bhead now contains the (converted) bhead structure. Now read
+                        * the associated data and put everything in a BHeadN (creative naming !)
+                        */
+                       if (!fd->eof) {
                                new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
                                if (new_bhead) {
                                        new_bhead->next = new_bhead->prev = NULL;
                                        new_bhead->bhead = bhead;
-
+                                       
                                        readsize = fd->read(fd, new_bhead + 1, bhead.len);
-
+                                       
                                        if (readsize != bhead.len) {
                                                fd->eof = 1;
                                                MEM_freeN(new_bhead);
@@ -733,13 +729,13 @@ static BHeadN *get_bhead(FileData *fd)
                }
        }
 
-       // We've read a new block. Now add it to the list
-       // of blocks.
-
+       /* We've read a new block. Now add it to the list
+        * of blocks.
+        */
        if (new_bhead) {
                BLI_addtail(&fd->listbase, new_bhead);
        }
-
+       
        return(new_bhead);
 }
 
@@ -747,53 +743,53 @@ BHead *blo_firstbhead(FileData *fd)
 {
        BHeadN *new_bhead;
        BHead *bhead = NULL;
-
-       // Rewind the file
-       // Read in a new block if necessary
-
+       
+       /* Rewind the file
+        * Read in a new block if necessary
+        */
        new_bhead = fd->listbase.first;
        if (new_bhead == NULL) {
                new_bhead = get_bhead(fd);
        }
-
+       
        if (new_bhead) {
                bhead = &new_bhead->bhead;
        }
-
+       
        return(bhead);
 }
 
 BHead *blo_prevbhead(FileData *UNUSED(fd), BHead *thisblock)
 {
-       BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
-       BHeadN *prev= bheadn->prev;
-
-       return prev?&prev->bhead:NULL;
+       BHeadN *bheadn = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
+       BHeadN *prev = bheadn->prev;
+       
+       return (prev) ? &prev->bhead : NULL;
 }
 
 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
 {
        BHeadN *new_bhead = NULL;
        BHead *bhead = NULL;
-
+       
        if (thisblock) {
                // bhead is actually a sub part of BHeadN
                // We calculate the BHeadN pointer from the BHead pointer below
                new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
-
+               
                // get the next BHeadN. If it doesn't exist we read in the next one
                new_bhead = new_bhead->next;
                if (new_bhead == NULL) {
                        new_bhead = get_bhead(fd);
                }
        }
-
+       
        if (new_bhead) {
                // here we do the reverse:
                // go from the BHeadN pointer to the BHead pointer
                bhead = &new_bhead->bhead;
        }
-
+       
        return(bhead);
 }
 
@@ -801,17 +797,17 @@ static void decode_blender_header(FileData *fd)
 {
        char header[SIZEOFBLENDERHEADER], num[4];
        int readsize;
-
-       // read in the header data
+       
+       /* read in the header data */
        readsize = fd->read(fd, header, sizeof(header));
-
+       
        if (readsize == sizeof(header)) {
                if (strncmp(header, "BLENDER", 7) == 0) {
-                       int remove_this_endian_test= 1;
-
+                       int remove_this_endian_test = 1;
+                       
                        fd->flags |= FD_FLAGS_FILE_OK;
-
-                       // what size are pointers in the file ?
+                       
+                       /* what size are pointers in the file ? */
                        if (header[7]=='_') {
                                fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
                                if (sizeof(void *) != 4) {
@@ -823,16 +819,16 @@ static void decode_blender_header(FileData *fd)
                                        fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
                                }
                        }
-
-                       // is the file saved in a different endian
-                       // than we need ?
+                       
+                       /* is the file saved in a different endian
+                        * than we need ?
+                        */
                        if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
                                fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
                        }
-
-                       // get the version number
-
-                       memcpy(num, header+9, 3);
+                       
+                       /* get the version number */
+                       memcpy(num, header + 9, 3);
                        num[3] = 0;
                        fd->fileversion = atoi(num);
                }
@@ -842,115 +838,114 @@ static void decode_blender_header(FileData *fd)
 static int read_file_dna(FileData *fd)
 {
        BHead *bhead;
-
-       for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
-               if (bhead->code==DNA1) {
-                       int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
-
-                       fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
+       
+       for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
+               if (bhead->code == DNA1) {
+                       int do_endian_swap = (fd->flags & FD_FLAGS_SWITCH_ENDIAN) ? 1 : 0;
+                       
+                       fd->filesdna = DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
                        if (fd->filesdna) {
-                               
-                               fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
+                               fd->compflags = DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
                                /* used to retrieve ID names from (bhead+1) */
-                               fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
+                               fd->id_name_offs = DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
                        }
-
+                       
                        return 1;
                }
-               else if (bhead->code==ENDB)
+               else if (bhead->code == ENDB)
                        break;
        }
-
+       
        return 0;
 }
 
 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
 {
        int readsize = read(filedata->filedes, buffer, size);
-
+       
        if (readsize < 0) {
                readsize = EOF;
        }
        else {
                filedata->seek += readsize;
        }
-
-       return (readsize);
+       
+       return readsize;
 }
 
 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
 {
        int readsize = gzread(filedata->gzfiledes, buffer, size);
-
+       
        if (readsize < 0) {
                readsize = EOF;
        }
        else {
                filedata->seek += readsize;
        }
-
+       
        return (readsize);
 }
 
 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
 {
-       // don't read more bytes then there are available in the buffer
+       /* don't read more bytes then there are available in the buffer */
        int readsize = (int)MIN2(size, (unsigned int)(filedata->buffersize - filedata->seek));
-
+       
        memcpy(buffer, filedata->buffer + filedata->seek, readsize);
        filedata->seek += readsize;
-
+       
        return (readsize);
 }
 
 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
 {
-       static unsigned int seek= 1<<30;        /* the current position */
-       static unsigned int offset= 0;          /* size of previous chunks */
-       static MemFileChunk *chunk=NULL;
+       static unsigned int seek = (1<<30);     /* the current position */
+       static unsigned int offset = 0;         /* size of previous chunks */
+       static MemFileChunk *chunk = NULL;
        unsigned int chunkoffset, readsize, totread;
        
-       if (size==0) return 0;
+       if (size == 0) return 0;
        
        if (seek != (unsigned int)filedata->seek) {
-               chunk= filedata->memfile->chunks.first;
-               seek= 0;
+               chunk = filedata->memfile->chunks.first;
+               seek = 0;
                
                while (chunk) {
                        if (seek + chunk->size > (unsigned) filedata->seek) break;
-                       seek+= chunk->size;
-                       chunk= chunk->next;
+                       seek += chunk->size;
+                       chunk = chunk->next;
                }
-               offset= seek;
-               seek= filedata->seek;
+               offset = seek;
+               seek = filedata->seek;
        }
        
        if (chunk) {
-               totread= 0;
-
+               totread = 0;
+               
                do {
                        /* first check if it's on the end if current chunk */
                        if (seek-offset == chunk->size) {
-                               offset+= chunk->size;
-                               chunk= chunk->next;
+                               offset += chunk->size;
+                               chunk = chunk->next;
                        }
-
+                       
                        /* debug, should never happen */
-                       if (chunk==NULL) {
+                       if (chunk == NULL) {
                                printf("illegal read, chunk zero\n");
                                return 0;
                        }
-
-                       chunkoffset= seek-offset;
-                       readsize= size-totread;
-
+                       
+                       chunkoffset = seek-offset;
+                       readsize = size-totread;
+                       
                        /* data can be spread over multiple chunks, so clamp size
                         * to within this chunk, and then it will read further in
                         * the next chunk */
                        if (chunkoffset+readsize > chunk->size)
                                readsize= chunk->size-chunkoffset;
-
-                       memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
+                       
+                       memcpy((char*)buffer + totread, chunk->buf + chunkoffset, readsize);
                        totread += readsize;
                        filedata->seek += readsize;
                        seek += readsize;
@@ -958,47 +953,47 @@ static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int s
                
                return totread;
        }
-
+       
        return 0;
 }
 
 static FileData *filedata_new(void)
 {
        FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
-
+       
        fd->filedes = -1;
        fd->gzfiledes = NULL;
-
+       
                /* XXX, this doesn't need to be done all the time,
                 * but it keeps us re-entrant,  remove once we have
                 * a lib that provides a nice lock. - zr
                 */
        fd->memsdna = DNA_sdna_from_data(DNAstr,  DNAlen,  0);
-
+       
        fd->datamap = oldnewmap_new();
        fd->globmap = oldnewmap_new();
        fd->libmap = oldnewmap_new();
-
+       
        return fd;
 }
 
 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
 {
        decode_blender_header(fd);
-
+       
        if (fd->flags & FD_FLAGS_FILE_OK) {
                if (!read_file_dna(fd)) {
                        BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", incomplete", fd->relabase);
                        blo_freefiledata(fd);
-                       fd= NULL;
+                       fd = NULL;
                }
        } 
        else {
                BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", not a blend file", fd->relabase);
                blo_freefiledata(fd);
-               fd= NULL;
+               fd = NULL;
        }
-
+       
        return fd;
 }
 
@@ -1007,9 +1002,9 @@ static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
 FileData *blo_openblenderfile(const char *filepath, ReportList *reports)
 {
        gzFile gzfile;
-       errno= 0;
-       gzfile= BLI_gzopen(filepath, "rb");
-
+       errno = 0;
+       gzfile = BLI_gzopen(filepath, "rb");
+       
        if (gzfile == (gzFile)Z_NULL) {
                BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", filepath, errno ? strerror(errno) : "Unknown error reading file");
                return NULL;
@@ -1018,10 +1013,10 @@ FileData *blo_openblenderfile(const char *filepath, ReportList *reports)
                FileData *fd = filedata_new();
                fd->gzfiledes = gzfile;
                fd->read = fd_read_gzip_from_file;
-
+               
                /* needed for library_append and read_libraries */
                BLI_strncpy(fd->relabase, filepath, sizeof(fd->relabase));
-
+               
                return blo_decode_and_check(fd, reports);
        }
 }
@@ -1033,12 +1028,12 @@ FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
                return NULL;
        }
        else {
-               FileData *fd= filedata_new();
-               fd->buffer= mem;
-               fd->buffersize= memsize;
-               fd->read= fd_read_from_memory;
-               fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
-
+               FileData *fd = filedata_new();
+               fd->buffer = mem;
+               fd->buffersize = memsize;
+               fd->read = fd_read_from_memory;
+               fd->flags |= FD_FLAGS_NOT_MY_BUFFER;
+               
                return blo_decode_and_check(fd, reports);
        }
 }
@@ -1050,12 +1045,12 @@ FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
                return NULL;
        }
        else {
-               FileData *fd= filedata_new();
-               fd->memfile= memfile;
-
-               fd->read= fd_read_from_memfile;
-               fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
-
+               FileData *fd = filedata_new();
+               fd->memfile = memfile;
+               
+               fd->read = fd_read_from_memfile;
+               fd->flags |= FD_FLAGS_NOT_MY_BUFFER;
+               
                return blo_decode_and_check(fd, reports);
        }
 }
@@ -1064,30 +1059,29 @@ FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
 void blo_freefiledata(FileData *fd)
 {
        if (fd) {
-               
                if (fd->filedes != -1) {
                        close(fd->filedes);
                }
-
+               
                if (fd->gzfiledes != NULL) {
                        gzclose(fd->gzfiledes);
                }
-
+               
                if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
                        MEM_freeN(fd->buffer);
                        fd->buffer = NULL;
                }
-
+               
                // Free all BHeadN data blocks
                BLI_freelistN(&fd->listbase);
-
+               
                if (fd->memsdna)
                        DNA_sdna_free(fd->memsdna);
                if (fd->filesdna)
                        DNA_sdna_free(fd->filesdna);
                if (fd->compflags)
                        MEM_freeN(fd->compflags);
-
+               
                if (fd->datamap)
                        oldnewmap_free(fd->datamap);
                if (fd->globmap)
@@ -1109,7 +1103,9 @@ void blo_freefiledata(FileData *fd)
 
 int BLO_has_bfile_extension(const char *str)
 {
-       return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend") || BLI_testextensie(str, ".blend.gz"));
+       return (BLI_testextensie(str, ".ble") || 
+               BLI_testextensie(str, ".blend") || 
+               BLI_testextensie(str, ".blend.gz"));
 }
 
 int BLO_is_a_library(const char *path, char *dir, char *group)
@@ -1121,32 +1117,32 @@ int BLO_is_a_library(const char *path, char *dir, char *group)
        char *fd;
        
        strcpy(dir, path);
-       len= strlen(dir);
-       if (len<7) return 0;
-       if ( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
+       len = strlen(dir);
+       if (len < 7) return 0;
+       if ((dir[len - 1] != '/') && (dir[len - 1] != '\\')) return 0;
        
-       group[0]= 0;
-       dir[len-1]= 0;
+       group[0] = '\0';
+       dir[len - 1] = '\0';
 
        /* Find the last slash */
-       fd= BLI_last_slash(dir);
+       fd = BLI_last_slash(dir);
 
-       if (fd==NULL) return 0;
-       *fd= 0;
+       if (fd == NULL) return 0;
+       *fd = 0;
        if (BLO_has_bfile_extension(fd+1)) {
                /* the last part of the dir is a .blend file, no group follows */
-               *fd= '/'; /* put back the removed slash separating the dir and the .blend file name */
+               *fd = '/'; /* put back the removed slash separating the dir and the .blend file name */
        }
        else {          
-               char *gp = fd+1; // in case we have a .blend file, gp points to the group
-
+               char *gp = fd + 1; // in case we have a .blend file, gp points to the group
+               
                /* Find the last slash */
-               fd= BLI_last_slash(dir);
+               fd = BLI_last_slash(dir);
                if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
-
+               
                /* now we know that we are in a blend file and it is safe to 
                 * assume that gp actually points to a group */
-               if (strcmp("Screen", gp)!=0)
+               if (strcmp("Screen", gp) != 0)
                        BLI_strncpy(group, gp, GROUP_MAX);
        }
        return 1;
@@ -1191,11 +1187,11 @@ void *blo_do_versions_newlibadr(FileData *fd, void *lib, void *adr)             /* only lib
 
 static void *newlibadr_us(FileData *fd, void *lib, void *adr)  /* increases user number */
 {
-       ID *id= newlibadr(fd, lib, adr);
-
+       ID *id = newlibadr(fd, lib, adr);
+       
        if (id)
                id->us++;
-
+       
        return id;
 }
 
@@ -1208,12 +1204,12 @@ static void change_idid_adr_fd(FileData *fd, void *old, void *new)
 {
        int i;
        
-       for (i=0; i<fd->libmap->nentries; i++) {
-               OldNew *entry= &fd->libmap->entries[i];
+       for (i = 0; i < fd->libmap->nentries; i++) {
+               OldNew *entry = &fd->libmap->entries[i];
                
                if (old==entry->newp && entry->nr==ID_ID) {
-                       entry->newp= new;
-                       if (new) entry->nr= GS( ((ID *)new)->name );
+                       entry->newp = new;
+                       if (new) entry->nr = GS( ((ID *)new)->name );
                        break;
                }
        }
@@ -1223,11 +1219,13 @@ static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, voi
 {
        Main *mainptr;
        
-       for (mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
+       for (mainptr = mainlist->first; mainptr; mainptr = mainptr->next) {
                FileData *fd;
                
-               if (mainptr->curlib) fd= mainptr->curlib->filedata;
-               else fd= basefd;
+               if (mainptr->curlib)
+                       fd = mainptr->curlib->filedata;
+               else
+                       fd = basefd;
                
                if (fd) {
                        change_idid_adr_fd(fd, old, new);
@@ -1241,35 +1239,36 @@ static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, voi
  * if the local object still exists */
 void blo_clear_proxy_pointers_from_lib(Main *oldmain)
 {
-       Object *ob= oldmain->object.first;
+       Object *ob = oldmain->object.first;
        
-       for (;ob; ob= ob->id.next)
+       for (; ob; ob= ob->id.next) {
                if (ob->id.lib)
-                       ob->proxy_from= NULL;
+                       ob->proxy_from = NULL;
+       }
 }
 
 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
 {
-       Image *ima= oldmain->image.first;
-       Scene *sce= oldmain->scene.first;
+       Image *ima = oldmain->image.first;
+       Scene *sce = oldmain->scene.first;
        int a;
        
-       fd->imamap= oldnewmap_new();
+       fd->imamap = oldnewmap_new();
        
-       for (;ima; ima= ima->id.next) {
-               Link *ibuf= ima->ibufs.first;
-               for (; ibuf; ibuf= ibuf->next)
+       for (; ima; ima = ima->id.next) {
+               Link *ibuf = ima->ibufs.first;
+               for (; ibuf; ibuf = ibuf->next)
                        oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
                if (ima->gputexture)
                        oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
-               for (a=0; a<IMA_MAX_RENDER_SLOT; a++)
+               for (a=0; a < IMA_MAX_RENDER_SLOT; a++)
                        if (ima->renders[a])
                                oldnewmap_insert(fd->imamap, ima->renders[a], ima->renders[a], 0);
        }
-       for (; sce; sce= sce->id.next) {
+       for (; sce; sce = sce->id.next) {
                if (sce->nodetree) {
                        bNode *node;
-                       for (node= sce->nodetree->nodes.first; node; node= node->next)
+                       for (node = sce->nodetree->nodes.first; node; node = node->next)
                                oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
                }
        }
@@ -1279,63 +1278,63 @@ void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
 /* this works because freeing old main only happens after this call */
 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
 {
-       OldNew *entry= fd->imamap->entries;
-       Image *ima= oldmain->image.first;
-       Scene *sce= oldmain->scene.first;
+       OldNew *entry = fd->imamap->entries;
+       Image *ima = oldmain->image.first;
+       Scene *sce = oldmain->scene.first;
        int i;
        
        /* used entries were restored, so we put them to zero */
-       for (i=0; i<fd->imamap->nentries; i++, entry++) {
-               if (entry->nr>0)
-                       entry->newp= NULL;
+       for (i = 0; i < fd->imamap->nentries; i++, entry++) {
+               if (entry->nr > 0)
+                       entry->newp = NULL;
        }
        
-       for (;ima; ima= ima->id.next) {
+       for (; ima; ima = ima->id.next) {
                Link *ibuf, *next;
                
                /* this mirrors direct_link_image */
-               for (ibuf= ima->ibufs.first; ibuf; ibuf= next) {
-                       next= ibuf->next;
-                       if (NULL==newimaadr(fd, ibuf)) {        /* so was restored */
+               for (ibuf = ima->ibufs.first; ibuf; ibuf = next) {
+                       next = ibuf->next;
+                       if (NULL == newimaadr(fd, ibuf)) {      /* so was restored */
                                BLI_remlink(&ima->ibufs, ibuf);
-                               ima->bindcode= 0;
-                               ima->gputexture= NULL;
+                               ima->bindcode = 0;
+                               ima->gputexture = NULL;
                        }
                }
-               for (i=0; i<IMA_MAX_RENDER_SLOT; i++)
-                       ima->renders[i]= newimaadr(fd, ima->renders[i]);
-
-               ima->gputexture= newimaadr(fd, ima->gputexture);
+               for (i = 0; i < IMA_MAX_RENDER_SLOT; i++)
+                       ima->renders[i] = newimaadr(fd, ima->renders[i]);
+               
+               ima->gputexture = newimaadr(fd, ima->gputexture);
        }
-       for (; sce; sce= sce->id.next) {
+       for (; sce; sce = sce->id.next) {
                if (sce->nodetree) {
                        bNode *node;
-                       for (node= sce->nodetree->nodes.first; node; node= node->next)
-                               node->preview= newimaadr(fd, node->preview);
+                       for (node = sce->nodetree->nodes.first; node; node = node->next)
+                               node->preview = newimaadr(fd, node->preview);
                }
        }
 }
 
 void blo_make_movieclip_pointer_map(FileData *fd, Main *oldmain)
 {
-       MovieClip *clip= oldmain->movieclip.first;
-       Scene *sce= oldmain->scene.first;
-
-       fd->movieclipmap= oldnewmap_new();
-
-       for (;clip; clip= clip->id.next) {
+       MovieClip *clip = oldmain->movieclip.first;
+       Scene *sce = oldmain->scene.first;
+       
+       fd->movieclipmap = oldnewmap_new();
+       
+       for (; clip; clip = clip->id.next) {
                if (clip->cache)
                        oldnewmap_insert(fd->movieclipmap, clip->cache, clip->cache, 0);
-
+               
                if (clip->tracking.camera.intrinsics)
                        oldnewmap_insert(fd->movieclipmap, clip->tracking.camera.intrinsics, clip->tracking.camera.intrinsics, 0);
        }
-
-       for (; sce; sce= sce->id.next) {
+       
+       for (; sce; sce = sce->id.next) {
                if (sce->nodetree) {
                        bNode *node;
-                       for (node= sce->nodetree->nodes.first; node; node= node->next)
-                               if (node->type==CMP_NODE_MOVIEDISTORTION)
+                       for (node = sce->nodetree->nodes.first; node; node= node->next)
+                               if (node->type == CMP_NODE_MOVIEDISTORTION)
                                        oldnewmap_insert(fd->movieclipmap, node->storage, node->storage, 0);
                }
        }
@@ -1345,28 +1344,28 @@ void blo_make_movieclip_pointer_map(FileData *fd, Main *oldmain)
 /* this works because freeing old main only happens after this call */
 void blo_end_movieclip_pointer_map(FileData *fd, Main *oldmain)
 {
-       OldNew *entry= fd->movieclipmap->entries;
-       MovieClip *clip= oldmain->movieclip.first;
-       Scene *sce= oldmain->scene.first;
+       OldNew *entry = fd->movieclipmap->entries;
+       MovieClip *clip = oldmain->movieclip.first;
+       Scene *sce = oldmain->scene.first;
        int i;
-
+       
        /* used entries were restored, so we put them to zero */
-       for (i=0; i<fd->movieclipmap->nentries; i++, entry++) {
-               if (entry->nr>0)
-                               entry->newp= NULL;
+       for (i=0; i < fd->movieclipmap->nentries; i++, entry++) {
+               if (entry->nr > 0)
+                       entry->newp = NULL;
        }
-
-       for (;clip; clip= clip->id.next) {
-               clip->cache= newmclipadr(fd, clip->cache);
-               clip->tracking.camera.intrinsics= newmclipadr(fd, clip->tracking.camera.intrinsics);
+       
+       for (; clip; clip = clip->id.next) {
+               clip->cache = newmclipadr(fd, clip->cache);
+               clip->tracking.camera.intrinsics = newmclipadr(fd, clip->tracking.camera.intrinsics);
        }
-
-       for (; sce; sce= sce->id.next) {
+       
+       for (; sce; sce = sce->id.next) {
                if (sce->nodetree) {
                        bNode *node;
-                       for (node= sce->nodetree->nodes.first; node; node= node->next)
-                               if (node->type==CMP_NODE_MOVIEDISTORTION)
-                                       node->storage= newmclipadr(fd, node->storage);
+                       for (node = sce->nodetree->nodes.first; node; node = node->next)
+                               if (node->type == CMP_NODE_MOVIEDISTORTION)
+                                       node->storage = newmclipadr(fd, node->storage);
                }
        }
 }
@@ -1375,19 +1374,19 @@ void blo_end_movieclip_pointer_map(FileData *fd, Main *oldmain)
 /* undo file support: add all library pointers in lookup */
 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
 {
-       Main *ptr= mainlist->first;
+       Main *ptr = mainlist->first;
        ListBase *lbarray[MAX_LIBARRAY];
        
-       for (ptr= ptr->next; ptr; ptr= ptr->next) {
-               int i= set_listbasepointers(ptr, lbarray);
+       for (ptr = ptr->next; ptr; ptr = ptr->next) {
+               int i = set_listbasepointers(ptr, lbarray);
                while (i--) {
                        ID *id;
-                       for (id= lbarray[i]->first; id; id= id->next)
+                       for (id = lbarray[i]->first; id; id = id->next)
                                oldnewmap_insert(fd->libmap, id, id, GS(id->name));
                }
        }
 }
-               
+
 
 /* ********** END OLD POINTERS ****************** */
 /* ********** READ FILE ****************** */
@@ -1396,33 +1395,33 @@ static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
 {
        int blocksize, nblocks;
        char *data;
-
-       data= (char *)(bhead+1);
-       blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
-
-       nblocks= bhead->nr;
+       
+       data = (char *)(bhead+1);
+       blocksize = filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
+       
+       nblocks = bhead->nr;
        while (nblocks--) {
                DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
-
-               data+= blocksize;
+               
+               data += blocksize;
        }
 }
 
 static void *read_struct(FileData *fd, BHead *bh, const char *blockname)
 {
-       void *temp= NULL;
-
+       void *temp = NULL;
+       
        if (bh->len) {
                /* switch is based on file dna */
                if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
                        switch_endian_structs(fd->filesdna, bh);
-
+               
                if (fd->compflags[bh->SDNAnr]) {        /* flag==0: doesn't exist anymore */
-                       if (fd->compflags[bh->SDNAnr]==2) {
-                               temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
+                       if (fd->compflags[bh->SDNAnr] == 2) {
+                               temp = DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
                        }
                        else {
-                               temp= MEM_mallocN(bh->len, blockname);
+                               temp = MEM_mallocN(bh->len, blockname);
                                memcpy(temp, (bh+1), bh->len);
                        }
                }
@@ -1434,19 +1433,19 @@ static void *read_struct(FileData *fd, BHead *bh, const char *blockname)
 static void link_list(FileData *fd, ListBase *lb)              /* only direct data */
 {
        Link *ln, *prev;
-
-       if (lb->first==NULL) return;
-
-       lb->first= newdataadr(fd, lb->first);
-       ln= lb->first;
-       prev= NULL;
+       
+       if (lb->first == NULL) return;
+       
+       lb->first = newdataadr(fd, lb->first);
+       ln = lb->first;
+       prev = NULL;
        while (ln) {
-               ln->next= newdataadr(fd, ln->next);
-               ln->prev= prev;
-               prev= ln;
-               ln= ln->next;
+               ln->next = newdataadr(fd, ln->next);
+               ln->prev = prev;
+               prev = ln;
+               ln = ln->next;
        }
-       lb->last= prev;
+       lb->last = prev;
 }
 
 static void link_glob_list(FileData *fd, ListBase *lb)         /* for glob data */
@@ -1454,26 +1453,26 @@ static void link_glob_list(FileData *fd, ListBase *lb)          /* for glob data */
        Link *ln, *prev;
        void *poin;
 
-       if (lb->first==NULL) return;
-       poin= newdataadr(fd, lb->first);
+       if (lb->first == NULL) return;
+       poin = newdataadr(fd, lb->first);
        if (lb->first) {
                oldnewmap_insert(fd->globmap, lb->first, poin, 0);
        }
-       lb->first= poin;
-
-       ln= lb->first;
-       prev= NULL;
+       lb->first = poin;
+       
+       ln = lb->first;
+       prev = NULL;
        while (ln) {
-               poin= newdataadr(fd, ln->next);
+               poin = newdataadr(fd, ln->next);
                if (ln->next) {
                        oldnewmap_insert(fd->globmap, ln->next, poin, 0);
                }
-               ln->next= poin;
-               ln->prev= prev;
-               prev= ln;
-               ln= ln->next;
+               ln->next = poin;
+               ln->prev = prev;
+               prev = ln;
+               ln = ln->next;
        }
-       lb->last= prev;
+       lb->last = prev;
 }
 
 static void test_pointer_array(FileData *fd, void **mat)
@@ -1491,29 +1490,29 @@ static void test_pointer_array(FileData *fd, void **mat)
                 * the new dna format.
                 */
        if (*mat) {
-               len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
-
+               len = MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
+                       
                if (fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
-                       ipoin=imat= MEM_mallocN(len*4, "newmatar");
+                       ipoin=imat= MEM_mallocN(len * 4, "newmatar");
                        lpoin= *mat;
-
+                       
                        while (len-- > 0) {
                                if ((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
                                        SWITCH_LONGINT(*lpoin);
-                               *ipoin= (int) ((*lpoin) >> 3);
+                               *ipoin = (int)((*lpoin) >> 3);
                                ipoin++;
                                lpoin++;
                        }
                        MEM_freeN(*mat);
-                       *mat= imat;
+                       *mat = imat;
                }
-
+               
                if (fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
-                       lpoin=lmat= MEM_mallocN(len*8, "newmatar");
-                       ipoin= *mat;
-
+                       lpoin = lmat = MEM_mallocN(len * 8, "newmatar");
+                       ipoin = *mat;
+                       
                        while (len-- > 0) {
-                               *lpoin= *ipoin;
+                               *lpoin = *ipoin;
                                ipoin++;
                                lpoin++;
                        }
@@ -1532,22 +1531,22 @@ static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData
 {
        IDProperty *array;
        int i;
-
-       /*since we didn't save the extra buffer, set totallen to len.*/
+       
+       /* since we didn't save the extra buffer, set totallen to len */
        prop->totallen = prop->len;
        prop->data.pointer = newdataadr(fd, prop->data.pointer);
 
-       array= (IDProperty*) prop->data.pointer;
+       array = (IDProperty *)prop->data.pointer;
        
        /* note!, idp-arrays didn't exist in 2.4x, so the pointer will be cleared
         * theres not really anything we can do to correct this, at least don't crash */
-       if (array==NULL) {
-               prop->len= 0;
-               prop->totallen= 0;
+       if (array == NULL) {
+               prop->len = 0;
+               prop->totallen = 0;
        }
        
-
-       for (i=0; i<prop->len; i++)
+       
+       for (i = 0; i < prop->len; i++)
                IDP_DirectLinkProperty(&array[i], switch_endian, fd);
 }
 
@@ -1555,29 +1554,29 @@ static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *f
 {
        IDProperty **array;
        int i;
-
-       /*since we didn't save the extra buffer, set totallen to len.*/
+       
+       /* since we didn't save the extra buffer, set totallen to len */
        prop->totallen = prop->len;
        prop->data.pointer = newdataadr(fd, prop->data.pointer);
-
+       
        if (prop->subtype == IDP_GROUP) {
                test_pointer_array(fd, prop->data.pointer);
-               array= prop->data.pointer;
-
-               for (i=0; i<prop->len; i++)
+               array = prop->data.pointer;
+               
+               for (i = 0; i < prop->len; i++)
                        IDP_DirectLinkProperty(array[i], switch_endian, fd);
        }
        else if (prop->subtype == IDP_DOUBLE) {
                if (switch_endian) {
-                       for (i=0; i<prop->len; i++) {
-                               SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
+                       for (i = 0; i < prop->len; i++) {
+                               SWITCH_LONGINT(((double *)prop->data.pointer)[i]);
                        }
                }
        }
        else {
                if (switch_endian) {
-                       for (i=0; i<prop->len; i++) {
-                               SWITCH_INT(((int*)prop->data.pointer)[i]);
+                       for (i = 0; i < prop->len; i++) {
+                               SWITCH_INT(((int *)prop->data.pointer)[i]);
                        }
                }
        }
@@ -1594,9 +1593,9 @@ static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *f
 {
        ListBase *lb = &prop->data.group;
        IDProperty *loop;
-
+       
        link_list(fd, lb);
-
+       
        /*Link child id properties now*/
        for (loop=prop->data.group.first; loop; loop=loop->next) {
                IDP_DirectLinkProperty(loop, switch_endian, fd);
@@ -1639,7 +1638,7 @@ static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData
        }
 }
 
-/*stub function*/
+/* stub function */
 static void IDP_LibLinkProperty(IDProperty *UNUSED(prop), int UNUSED(switch_endian), FileData *UNUSED(fd))
 {
 }
@@ -1654,10 +1653,10 @@ static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
        /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
        cumap->flag &= ~CUMA_PREMULLED;
        
-       for (a=0; a<CM_TOT; a++) {
-               cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
-               cumap->cm[a].table= NULL;
-               cumap->cm[a].premultable= NULL;
+       for (a = 0; a < CM_TOT; a++) {
+               cumap->cm[a].curve = newdataadr(fd, cumap->cm[a].curve);
+               cumap->cm[a].table = NULL;
+               cumap->cm[a].premultable = NULL;
        }
 }
 
@@ -1668,12 +1667,12 @@ static void lib_link_brush(FileData *fd, Main *main)
        Brush *brush;
        
        /* only link ID pointers */
-       for (brush= main->brush.first; brush; brush= brush->id.next) {
+       for (brush = main->brush.first; brush; brush = brush->id.next) {
                if (brush->id.flag & LIB_NEEDLINK) {
                        brush->id.flag -= LIB_NEEDLINK;
-
-                       brush->mtex.tex= newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
-                       brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
+                       
+                       brush->mtex.tex = newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
+                       brush->clone.image = newlibadr_us(fd, brush->id.lib, brush->clone.image);
                }
        }
 }
@@ -1683,14 +1682,14 @@ static void direct_link_brush(FileData *fd, Brush *brush)
        /* brush itself has been read */
 
        /* fallof curve */
-       brush->curve= newdataadr(fd, brush->curve);
+       brush->curve = newdataadr(fd, brush->curve);
        if (brush->curve)
                direct_link_curvemapping(fd, brush->curve);
        else
                BKE_brush_curve_preset(brush, CURVE_PRESET_SHARP);
 
-       brush->preview= NULL;
-       brush->icon_imbuf= NULL;
+       brush->preview = NULL;
+       brush->icon_imbuf = NULL;
 }
 
 static void direct_link_script(FileData *UNUSED(fd), Script *script)
@@ -1704,12 +1703,12 @@ static void direct_link_script(FileData *UNUSED(fd), Script *script)
 
 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
 {
-       PackedFile *pf= newdataadr(fd, oldpf);
-
+       PackedFile *pf = newdataadr(fd, oldpf);
+       
        if (pf) {
-               pf->data= newdataadr(fd, pf->data);
+               pf->data = newdataadr(fd, pf->data);
        }
-
+       
        return pf;
 }
 
@@ -1717,17 +1716,17 @@ static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
 
 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
 {
-       PreviewImage *prv= newdataadr(fd, old_prv);
-
+       PreviewImage *prv = newdataadr(fd, old_prv);
+       
        if (prv) {
                int i;
-               for (i=0; i < NUM_ICON_SIZES; ++i) {
+               for (i = 0; i < NUM_ICON_SIZES; ++i) {
                        if (prv->rect[i]) {
                                prv->rect[i] = newdataadr(fd, prv->rect[i]);
                        }
                }
        }
-
+       
        return prv;
 }
 
@@ -1739,18 +1738,16 @@ static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_p
 static void lib_link_ipo(FileData *fd, Main *main)
 {
        Ipo *ipo;
-
-       ipo= main->ipo.first;
-       while (ipo) {
+       
+       for (ipo = main->ipo.first; ipo; ipo = ipo->id.next) {
                if (ipo->id.flag & LIB_NEEDLINK) {
                        IpoCurve *icu;
-                       for (icu= ipo->curve.first; icu; icu= icu->next) {
+                       for (icu = ipo->curve.first; icu; icu = icu->next) {
                                if (icu->driver)
-                                       icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
+                                       icu->driver->ob = newlibadr(fd, ipo->id.lib, icu->driver->ob);
                        }
                        ipo->id.flag -= LIB_NEEDLINK;
                }
-               ipo= ipo->id.next;
        }
 }
 
@@ -1760,12 +1757,11 @@ static void direct_link_ipo(FileData *fd, Ipo *ipo)
        IpoCurve *icu;
 
        link_list(fd, &(ipo->curve));
-       icu= ipo->curve.first;
-       while (icu) {
-               icu->bezt= newdataadr(fd, icu->bezt);
-               icu->bp= newdataadr(fd, icu->bp);
-               icu->driver= newdataadr(fd, icu->driver);
-               icu= icu->next;
+       
+       for (icu = ipo->curve.first; icu; icu = icu->next) {
+               icu->bezt = newdataadr(fd, icu->bezt);
+               icu->bp = newdataadr(fd, icu->bp);
+               icu->driver = newdataadr(fd, icu->driver);
        }
 }
 
@@ -1779,8 +1775,8 @@ static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
                strip->object = newlibadr(fd, id->lib, strip->object);
                strip->act = newlibadr_us(fd, id->lib, strip->act);
                strip->ipo = newlibadr(fd, id->lib, strip->ipo);
-               for (amod= strip->modifiers.first; amod; amod= amod->next)
-                       amod->ob= newlibadr(fd, id->lib, amod->ob);
+               for (amod = strip->modifiers.first; amod; amod = amod->next)
+                       amod->ob = newlibadr(fd, id->lib, amod->ob);
        }
 }
 
@@ -1791,7 +1787,7 @@ static void direct_link_nlastrips(FileData *fd, ListBase *strips)
        
        link_list(fd, strips);
        
-       for (strip= strips->first; strip; strip= strip->next)
+       for (strip = strips->first; strip; strip = strip->next)
                link_list(fd, &strip->modifiers);
 }
 
@@ -1811,12 +1807,12 @@ static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
 {
        FModifier *fcm;
        
-       for (fcm= list->first; fcm; fcm= fcm->next) {
+       for (fcm = list->first; fcm; fcm = fcm->next) {
                /* data for specific modifiers */
                switch (fcm->type) {
                        case FMODIFIER_TYPE_PYTHON:
                        {
-                               FMod_Python *data= (FMod_Python *)fcm->data;
+                               FMod_Python *data = (FMod_Python *)fcm->data;
                                data->script = newlibadr(fd, id->lib, data->script);
                        }
                                break;
@@ -1832,10 +1828,10 @@ static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
                return;
        
        /* relink ID-block references... */
-       for (fcu= list->first; fcu; fcu= fcu->next) {
+       for (fcu = list->first; fcu; fcu = fcu->next) {
                /* driver data */
                if (fcu->driver) {
-                       ChannelDriver *driver= fcu->driver;
+                       ChannelDriver *driver = fcu->driver;
                        DriverVar *dvar;
                        
                        for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
@@ -1843,9 +1839,9 @@ static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
                                {       
                                        /* only relink if still used */
                                        if (tarIndex < dvar->num_targets)
-                                               dtar->id= newlibadr(fd, id->lib, dtar->id); 
+                                               dtar->id = newlibadr(fd, id->lib, dtar->id); 
                                        else
-                                               dtar->id= NULL;
+                                               dtar->id = NULL;
                                }
                                DRIVER_TARGETS_LOOPER_END
                        }
@@ -1862,19 +1858,19 @@ static void direct_link_fmodifiers(FileData *fd, ListBase *list)
 {
        FModifier *fcm;
        
-       for (fcm= list->first; fcm; fcm= fcm->next) {
+       for (fcm = list->first; fcm; fcm = fcm->next) {
                /* relink general data */
-               fcm->data = newdataadr(fd, fcm->data);
-               fcm->edata= NULL;
+               fcm->data  = newdataadr(fd, fcm->data);
+               fcm->edata = NULL;
                
                /* do relinking of data for specific types */
                switch (fcm->type) {
                        case FMODIFIER_TYPE_GENERATOR:
                        {
-                               FMod_Generator *data= (FMod_Generator *)fcm->data;
+                               FMod_Generator *data = (FMod_Generator *)fcm->data;
+                               
+                               data->coefficients = newdataadr(fd, data->coefficients);
                                
-                               data->coefficients= newdataadr(fd, data->coefficients);
-
                                if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
                                        unsigned int a;
                                        for (a = 0; a < data->arraysize; a++)
@@ -1884,14 +1880,14 @@ static void direct_link_fmodifiers(FileData *fd, ListBase *list)
                                break;
                        case FMODIFIER_TYPE_ENVELOPE:
                        {
-                               FMod_Envelope *data= (FMod_Envelope *)fcm->data;
+                               FMod_Envelope *data=  (FMod_Envelope *)fcm->data;
                                
                                data->data= newdataadr(fd, data->data);
                        }
                                break;
                        case FMODIFIER_TYPE_PYTHON:
                        {
-                               FMod_Python *data= (FMod_Python *)fcm->data;
+                               FMod_Python *data = (FMod_Python *)fcm->data;
                                
                                data->prop = newdataadr(fd, data->prop);
                                IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
@@ -1907,24 +1903,24 @@ static void direct_link_fcurves(FileData *fd, ListBase *list)
        FCurve *fcu;
        
        /* link F-Curve data to F-Curve again (non ID-libs) */
-       for (fcu= list->first; fcu; fcu= fcu->next) {
+       for (fcu = list->first; fcu; fcu = fcu->next) {
                /* curve data */
-               fcu->bezt= newdataadr(fd, fcu->bezt);
-               fcu->fpt= newdataadr(fd, fcu->fpt);
+               fcu->bezt = newdataadr(fd, fcu->bezt);
+               fcu->fpt = newdataadr(fd, fcu->fpt);
                
                /* rna path */
-               fcu->rna_path= newdataadr(fd, fcu->rna_path);
+               fcu->rna_path = newdataadr(fd, fcu->rna_path);
                
                /* group */
-               fcu->grp= newdataadr(fd, fcu->grp);
+               fcu->grp = newdataadr(fd, fcu->grp);
                
                /* driver */
                fcu->driver= newdataadr(fd, fcu->driver);
                if (fcu->driver) {
                        ChannelDriver *driver= fcu->driver;
                        DriverVar *dvar;
-
-                       driver->expr_comp= NULL;
+                       
+                       driver->expr_comp = NULL;
                        
                        /* relink variables, targets and their paths */
                        link_list(fd, &driver->variables);
@@ -1933,9 +1929,9 @@ static void direct_link_fcurves(FileData *fd, ListBase *list)
                                {
                                        /* only relink the targets being used */
                                        if (tarIndex < dvar->num_targets)
-                                               dtar->rna_path= newdataadr(fd, dtar->rna_path);
+                                               dtar->rna_path = newdataadr(fd, dtar->rna_path);
                                        else
-                                               dtar->rna_path= NULL;
+                                               dtar->rna_path = NULL;
                                }
                                DRIVER_TARGETS_LOOPER_END
                        }
@@ -1953,13 +1949,13 @@ static void lib_link_action(FileData *fd, Main *main)
        bAction *act;
        bActionChannel *chan;
 
-       for (act= main->action.first; act; act= act->id.next) {
+       for (act = main->action.first; act; act = act->id.next) {
                if (act->id.flag & LIB_NEEDLINK) {
                        act->id.flag -= LIB_NEEDLINK;
                        
 // XXX depreceated - old animation system <<<
                        for (chan=act->chanbase.first; chan; chan=chan->next) {
-                               chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
+                               chan->ipo = newlibadr_us(fd, act->id.lib, chan->ipo);
                                lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
                        }
 // >>> XXX depreceated - old animation system
@@ -1981,7 +1977,7 @@ static void direct_link_action(FileData *fd, bAction *act)
 
 // XXX depreceated - old animation system <<<
        for (achan = act->chanbase.first; achan; achan=achan->next) {
-               achan->grp= newdataadr(fd, achan->grp);
+               achan->grp = newdataadr(fd, achan->grp);
                
                link_list(fd, &achan->constraintChannels);
        }
@@ -1999,7 +1995,7 @@ static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
 {
        NlaStrip *strip;
        
-       for (strip= list->first; strip; strip= strip->next) {
+       for (strip = list->first; strip; strip = strip->next) {
                /* check strip's children */
                lib_link_nladata_strips(fd, id, &strip->strips);
                
@@ -2020,7 +2016,7 @@ static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
        NlaTrack *nlt;
        
        /* we only care about the NLA strips inside the tracks */
-       for (nlt= list->first; nlt; nlt= nlt->next) {
+       for (nlt = list->first; nlt; nlt = nlt->next) {
                lib_link_nladata_strips(fd, id, &nlt->strips);
        }
 }
@@ -2032,7 +2028,7 @@ static void direct_link_nladata_strips(FileData *fd, ListBase *list)
 {
        NlaStrip *strip;
        
-       for (strip= list->first; strip; strip= strip->next) {
+       for (strip = list->first; strip; strip = strip->next) {
                /* strip's child strips */
                link_list(fd, &strip->strips);
                direct_link_nladata_strips(fd, &strip->strips);
@@ -2052,7 +2048,7 @@ static void direct_link_nladata(FileData *fd, ListBase *list)
 {
        NlaTrack *nlt;
        
-       for (nlt= list->first; nlt; nlt= nlt->next) {
+       for (nlt = list->first; nlt; nlt = nlt->next) {
                /* relink list of strips */
                link_list(fd, &nlt->strips);
                
@@ -2069,8 +2065,8 @@ static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
        KS_Path *ksp;
        
        /* here, we're only interested in the ID pointer stored in some of the paths */
-       for (ks= list->first; ks; ks= ks->next) {
-               for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
+       for (ks = list->first; ks; ks = ks->next) {
+               for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
                        ksp->id= newlibadr(fd, id->lib, ksp->id); 
                }
        }
@@ -2083,11 +2079,11 @@ static void direct_link_keyingsets(FileData *fd, ListBase *list)
        KS_Path *ksp;
        
        /* link KeyingSet data to KeyingSet again (non ID-libs) */
-       for (ks= list->first; ks; ks= ks->next) {
+       for (ks = list->first; ks; ks = ks->next) {
                /* paths */
                link_list(fd, &ks->paths);
                
-               for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
+               for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
                        /* rna path */
                        ksp->rna_path= newdataadr(fd, ksp->rna_path);
                }
@@ -2143,7 +2139,7 @@ static void direct_link_animdata(FileData *fd, AnimData *adt)
         */
        // TODO: it's not really nice that anyone should be able to save the file in this
        //              state, but it's going to be too hard to enforce this single case...
-       adt->actstrip= newdataadr(fd, adt->actstrip);
+       adt->actstrip = newdataadr(fd, adt->actstrip);
 }      
 
 /* ************ READ MOTION PATHS *************** */
@@ -2156,7 +2152,7 @@ static void direct_link_motionpath(FileData *fd, bMotionPath *mpath)
                return;
        
        /* relink points cache */
-       mpath->points= newdataadr(fd, mpath->points);
+       mpath->points = newdataadr(fd, mpath->points);
 }
 
 /* ************ READ NODE TREE *************** */
@@ -2168,10 +2164,10 @@ static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
        
        if (ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
        
-       ntree->gpd= newlibadr_us(fd, id->lib, ntree->gpd);
+       ntree->gpd = newlibadr_us(fd, id->lib, ntree->gpd);
        
-       for (node= ntree->nodes.first; node; node= node->next)
-               node->id= newlibadr_us(fd, id->lib, node->id);
+       for (node = ntree->nodes.first; node; node = node->next)
+               node->id = newlibadr_us(fd, id->lib, node->id);
 }
 
 /* library ntree linking after fileread */
@@ -2180,7 +2176,7 @@ static void lib_link_nodetree(FileData *fd, Main *main)
        bNodeTree *ntree;
        
        /* only link ID pointers */
-       for (ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
+       for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next) {
                if (ntree->id.flag & LIB_NEEDLINK) {
                        ntree->id.flag -= LIB_NEEDLINK;
                        lib_link_ntree(fd, &ntree->id, ntree);
@@ -2198,24 +2194,24 @@ static void do_versions_socket_default_value(bNodeSocket *sock)
                return;
        
        switch (sock->type) {
-       case SOCK_FLOAT:
-               valfloat = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueFloat), "default socket value");
-               valfloat->value = sock->ns.vec[0];
-               valfloat->min = sock->ns.min;
-               valfloat->max = sock->ns.max;
-               valfloat->subtype = PROP_NONE;
-               break;
-       case SOCK_VECTOR:
-               valvector = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueVector), "default socket value");
-               copy_v3_v3(valvector->value, sock->ns.vec);
-               valvector->min = sock->ns.min;
-               valvector->max = sock->ns.max;
-               valvector->subtype = PROP_NONE;
-               break;
-       case SOCK_RGBA:
-               valrgba = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueRGBA), "default socket value");
-               copy_v4_v4(valrgba->value, sock->ns.vec);
-               break;
+               case SOCK_FLOAT:
+                       valfloat = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueFloat), "default socket value");
+                       valfloat->value = sock->ns.vec[0];
+                       valfloat->min = sock->ns.min;
+                       valfloat->max = sock->ns.max;
+                       valfloat->subtype = PROP_NONE;
+                       break;
+               case SOCK_VECTOR:
+                       valvector = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueVector), "default socket value");
+                       copy_v3_v3(valvector->value, sock->ns.vec);
+                       valvector->min = sock->ns.min;
+                       valvector->max = sock->ns.max;
+                       valvector->subtype = PROP_NONE;
+                       break;
+               case SOCK_RGBA:
+                       valrgba = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueRGBA), "default socket value");
+                       copy_v4_v4(valrgba->value, sock->ns.vec);
+                       break;
        }
 }
 
@@ -2240,7 +2236,7 @@ static void lib_nodetree_init_types_cb(void *UNUSED(data), ID *UNUSED(id), bNode
        bNode *node;
        
        ntreeInitTypes(ntree);
-
+       
        /* need to do this here instead of in do_versions, otherwise next function can crash */
        blo_do_versions_nodetree_default_value(ntree);
        
@@ -2254,7 +2250,7 @@ static void lib_nodetree_init_types_cb(void *UNUSED(data), ID *UNUSED(id), bNode
  */
 static void lib_node_do_versions_group_indices(bNode *gnode)
 {
-       bNodeTree *ngroup= (bNodeTree*)gnode->id;
+       bNodeTree *ngroup = (bNodeTree*)gnode->id;
        bNode *intnode;
        bNodeSocket *sock, *gsock, *intsock;
        int found;
@@ -2289,9 +2285,9 @@ static void lib_nodetree_do_versions_group_indices_cb(void *UNUSED(data), ID *UN
 {
        bNode *node;
        
-       for (node=ntree->nodes.first; node; node=node->next) {
-               if (node->type==NODE_GROUP) {
-                       bNodeTree *ngroup= (bNodeTree*)node->id;
+       for (node = ntree->nodes.first; node; node = node->next) {
+               if (node->type == NODE_GROUP) {
+                       bNodeTree *ngroup = (bNodeTree*)node->id;
                        if (ngroup && (ngroup->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE))
                                lib_node_do_versions_group_indices(node);
                }
@@ -2313,31 +2309,31 @@ static void lib_verify_nodetree(Main *main, int UNUSED(open))
        bNodeTree *ntree;
        int i;
        bNodeTreeType *ntreetype;
-
+       
        /* this crashes blender on undo/redo */
 #if 0
-               if (open==1) {
+               if (open == 1) {
                        reinit_nodesystem();
                }
 #endif
        
        /* set node->typeinfo pointers */
-       for (i=0; i < NUM_NTREE_TYPES; ++i) {
-               ntreetype= ntreeGetType(i);
+       for (i = 0; i < NUM_NTREE_TYPES; ++i) {
+               ntreetype = ntreeGetType(i);
                if (ntreetype && ntreetype->foreach_nodetree)
                        ntreetype->foreach_nodetree(main, NULL, lib_nodetree_init_types_cb);
        }
-       for (ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
+       for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next)
                lib_nodetree_init_types_cb(NULL, NULL, ntree);
        
        {
-               int has_old_groups=0;
+               int has_old_groups = 0;
                /* XXX this should actually be part of do_versions, but since we need
                 * finished library linking, it is not possible there. Instead in do_versions
                 * we have set the NTREE_DO_VERSIONS flag, so at this point we can do the
                 * actual group node updates.
                 */
-               for (ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
+               for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next) {
                        if (ntree->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE) {
                                /* this adds copies and links from all unlinked internal sockets to group inputs/outputs. */
                                node_group_expose_all_sockets(ntree);
@@ -2346,30 +2342,30 @@ static void lib_verify_nodetree(Main *main, int UNUSED(open))
                }
                
                if (has_old_groups) {
-                       for (i=0; i < NUM_NTREE_TYPES; ++i) {
-                               ntreetype= ntreeGetType(i);
+                       for (i = 0; i < NUM_NTREE_TYPES; ++i) {
+                               ntreetype = ntreeGetType(i);
                                if (ntreetype && ntreetype->foreach_nodetree)
                                        ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_group_indices_cb);
                        }
                }
                
-               for (ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
+               for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next)
                        ntree->flag &= ~NTREE_DO_VERSIONS_GROUP_EXPOSE;
        }
-       
+               
        /* verify all group user nodes */
-       for (ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
+       for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next) {
                ntreeVerifyNodes(main, &ntree->id);
        }
        
        /* make update calls where necessary */
        {
-               for (ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
+               for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next)
                        if (ntree->update)
                                ntreeUpdateTree(ntree);
-
-               for (i=0; i < NUM_NTREE_TYPES; ++i) {
-                       ntreetype= ntreeGetType(i);
+               
+               for (i = 0; i < NUM_NTREE_TYPES; i++) {
+                       ntreetype = ntreeGetType(i);
                        if (ntreetype && ntreetype->foreach_nodetree)
                                ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_update_cb);
                }
@@ -2378,10 +2374,10 @@ static void lib_verify_nodetree(Main *main, int UNUSED(open))
 
 static void direct_link_node_socket(FileData *fd, bNodeSocket *sock)
 {
-       sock->link= newdataadr(fd, sock->link);
-       sock->storage= newdataadr(fd, sock->storage);
-       sock->default_value= newdataadr(fd, sock->default_value);
-       sock->cache= NULL;
+       sock->link = newdataadr(fd, sock->link);
+       sock->storage = newdataadr(fd, sock->storage);
+       sock->default_value = newdataadr(fd, sock->default_value);
+       sock->cache = NULL;
 }
 
 /* ntree itself has been read! */
@@ -2392,46 +2388,46 @@ static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
        bNodeSocket *sock;
        bNodeLink *link;
        
-       ntree->init= 0;         /* to set callbacks and force setting types */
-       ntree->progress= NULL;
-       ntree->execdata= NULL;
+       ntree->init = 0;                /* to set callbacks and force setting types */
+       ntree->progress = NULL;
+       ntree->execdata = NULL;
        
-       ntree->adt= newdataadr(fd, ntree->adt);
+       ntree->adt = newdataadr(fd, ntree->adt);
        direct_link_animdata(fd, ntree->adt);
        
        link_list(fd, &ntree->nodes);
-       for (node= ntree->nodes.first; node; node= node->next) {
+       for (node = ntree->nodes.first; node; node = node->next) {
                if (node->type == NODE_DYNAMIC) {
-                       node->custom1= 0;
-                       node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
+                       node->custom1 = 0;
+                       node->custom1 = BSET(node->custom1, NODE_DYNAMIC_LOADED);
                }
-
-               node->typeinfo= NULL;
+               
+               node->typeinfo = NULL;
                
                link_list(fd, &node->inputs);
                link_list(fd, &node->outputs);
                
                if (node->type == CMP_NODE_MOVIEDISTORTION) {
-                       node->storage= newmclipadr(fd, node->storage);
+                       node->storage = newmclipadr(fd, node->storage);
                }
                else
-                       node->storage= newdataadr(fd, node->storage);
-
+                       node->storage = newdataadr(fd, node->storage);
+               
                if (node->storage) {
                        /* could be handlerized at some point */
                        if (ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
                                direct_link_curvemapping(fd, node->storage);
                        else if (ntree->type==NTREE_COMPOSIT) {
-                               if ( ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
+                               if (ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
                                        direct_link_curvemapping(fd, node->storage);
                                else if (ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
-                                       ((ImageUser *)node->storage)->ok= 1;
+                                       ((ImageUser *)node->storage)->ok = 1;
                        }
                        else if ( ntree->type==NTREE_TEXTURE) {
                                if (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME)
                                        direct_link_curvemapping(fd, node->storage);
                                else if (node->type==TEX_NODE_IMAGE)
-                                       ((ImageUser *)node->storage)->ok= 1;
+                                       ((ImageUser *)node->storage)->ok = 1;
                        }
                }
        }
@@ -2442,26 +2438,26 @@ static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
        link_list(fd, &ntree->outputs);
        
        /* and we connect the rest */
-       for (node= ntree->nodes.first; node; node= node->next) {
+       for (node = ntree->nodes.first; node; node = node->next) {
                node->parent = newdataadr(fd, node->parent);
-               node->preview= newimaadr(fd, node->preview);
-               node->lasty= 0;
+               node->preview = newimaadr(fd, node->preview);
+               node->lasty = 0;
                
-               for (sock= node->inputs.first; sock; sock= sock->next)
+               for (sock = node->inputs.first; sock; sock = sock->next)
                        direct_link_node_socket(fd, sock);
-               for (sock= node->outputs.first; sock; sock= sock->next)
+               for (sock = node->outputs.first; sock; sock = sock->next)
                        direct_link_node_socket(fd, sock);
        }
-       for (sock= ntree->inputs.first; sock; sock= sock->next)
+       for (sock = ntree->inputs.first; sock; sock = sock->next)
                direct_link_node_socket(fd, sock);
-       for (sock= ntree->outputs.first; sock; sock= sock->next)
+       for (sock = ntree->outputs.first; sock; sock = sock->next)
                direct_link_node_socket(fd, sock);
        
-       for (link= ntree->links.first; link; link= link->next) {
-               link->fromnode= newdataadr(fd, link->fromnode);
-               link->tonode= newdataadr(fd, link->tonode);
-               link->fromsock= newdataadr(fd, link->fromsock);
-               link->tosock= newdataadr(fd, link->tosock);
+       for (link = ntree->links.first; link; link= link->next) {
+               link->fromnode = newdataadr(fd, link->fromnode);
+               link->tonode = newdataadr(fd, link->tonode);
+               link->fromsock = newdataadr(fd, link->fromsock);
+               link->tosock = newdataadr(fd, link->tosock);
        }
        
        /* type verification is in lib-link */
@@ -2499,16 +2495,16 @@ static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
        for (con = conlist->first; con; con=con->next) {
                /* patch for error introduced by changing constraints (dunno how) */
                /* if con->data type changes, dna cannot resolve the pointer! (ton) */
-               if (con->data==NULL) {
-                       con->type= CONSTRAINT_TYPE_NULL;
+               if (con->data == NULL) {
+                       con->type = CONSTRAINT_TYPE_NULL;
                }
                /* own ipo, all constraints have it */
-               con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
+               con->ipo = newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
        }
        
        /* relink all ID-blocks used by the constraints */
-       cld.fd= fd;
-       cld.id= id;
+       cld.fd = fd;
+       cld.id = id;
        
        id_loop_constraints(conlist, lib_link_constraint_cb, &cld);
 }
@@ -2516,7 +2512,7 @@ static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
 static void direct_link_constraints(FileData *fd, ListBase *lb)
 {
        bConstraint *con;
-
+       
        link_list(fd, lb);
        for (con=lb->first; con; con=con->next) {
                con->data = newdataadr(fd, con->data);
@@ -2559,16 +2555,16 @@ static void direct_link_constraints(FileData *fd, ListBase *lb)
 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
 {
        bPoseChannel *pchan;
-       bArmature *arm= ob->data;
+       bArmature *arm = ob->data;
        int rebuild;
        
        if (!pose || !arm)
                return;
        
-
+       
        /* always rebuild to match proxy or lib changes */
-       rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
-
+       rebuild = ob->proxy || (ob->id.lib==NULL && arm->id.lib);
+       
        if (ob->proxy) {
                /* sync proxy layer */
                if (pose->proxy_layer)
@@ -2581,15 +2577,15 @@ static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
                                arm->act_bone = bone;
                }
        }
-
+       
        for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
                lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
                
                /* hurms... loop in a loop, but yah... later... (ton) */
-               pchan->bone= BKE_armature_find_bone_name(arm, pchan->name);
+               pchan->bone = BKE_armature_find_bone_name(arm, pchan->name);
                
-               pchan->custom= newlibadr_us(fd, arm->id.lib, pchan->custom);
-               if (pchan->bone==NULL)
+               pchan->custom = newlibadr_us(fd, arm->id.lib, pchan->custom);
+               if (pchan->bone == NULL)
                        rebuild= 1;
                else if (ob->id.lib==NULL && arm->id.lib) {
                        /* local pose selection copied to armature, bit hackish */
@@ -2599,7 +2595,7 @@ static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
        }
        
        if (rebuild) {
-               ob->recalc= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
+               ob->recalc = (OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
                pose->flag |= POSE_RECALC;
        }
 }
@@ -2607,54 +2603,49 @@ static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
 static void lib_link_armature(FileData *fd, Main *main)
 {
        bArmature *arm;
-
-       arm= main->armature.first;
-
-       while (arm) {
+       
+       for (arm = main->armature.first; arm; arm = arm->id.next) {
                if (arm->id.flag & LIB_NEEDLINK) {
                        if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
                        arm->id.flag -= LIB_NEEDLINK;
                }
-               arm= arm->id.next;
        }
 }
 
 static void direct_link_bones(FileData *fd, Bone* bone)
 {
-       Bone    *child;
-
-       bone->parent= newdataadr(fd, bone->parent);
-       bone->prop= newdataadr(fd, bone->prop);
+       Bone *child;
+       
+       bone->parent = newdataadr(fd, bone->parent);
+       bone->prop = newdataadr(fd, bone->prop);
        if (bone->prop)
                IDP_DirectLinkProperty(bone->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
                
        bone->flag &= ~BONE_DRAW_ACTIVE;
-
+       
        link_list(fd, &bone->childbase);
-
+       
        for (child=bone->childbase.first; child; child=child->next)
                direct_link_bones(fd, child);
 }
 
 static void direct_link_armature(FileData *fd, bArmature *arm)
 {
-       Bone    *bone;
-
+       Bone *bone;
+       
        link_list(fd, &arm->bonebase);
-       arm->edbo= NULL;
+       arm->edbo = NULL;
        arm->sketch = NULL;
        
-       arm->adt= newdataadr(fd, arm->adt);
+       arm->adt = newdataadr(fd, arm->adt);
        direct_link_animdata(fd, arm->adt);
        
-       bone=arm->bonebase.first;
-       while (bone) {
+       for (bone = arm->bonebase.first; bone; bone = bone->next) {
                direct_link_bones(fd, bone);
-               bone=bone->next;
        }
-
-       arm->act_bone= newdataadr(fd, arm->act_bone);
-       arm->act_edbone= NULL;
+       
+       arm->act_bone = newdataadr(fd, arm->act_bone);
+       arm->act_edbone = NULL;
 }
 
 /* ************ READ CAMERA ***************** */
@@ -2662,25 +2653,23 @@ static void direct_link_armature(FileData *fd, bArmature *arm)
 static void lib_link_camera(FileData *fd, Main *main)
 {
        Camera *ca;
-
-       ca= main->camera.first;
-       while (ca) {
+       
+       for (ca = main->camera.first; ca; ca = ca->id.next) {
                if (ca->id.flag & LIB_NEEDLINK) {
                        if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
                        
-                       ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
+                       ca->ipo = newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
                        
-                       ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
+                       ca->dof_ob = newlibadr_us(fd, ca->id.lib, ca->dof_ob);
                        
                        ca->id.flag -= LIB_NEEDLINK;
                }
-               ca= ca->id.next;
        }
 }
 
 static void direct_link_camera(FileData *fd, Camera *ca)
 {
-       ca->adt= newdataadr(fd, ca->adt);
+       ca->adt = newdataadr(fd, ca->adt);
        direct_link_animdata(fd, ca->adt);
 }
 
@@ -2692,28 +2681,26 @@ static void lib_link_lamp(FileData *fd, Main *main)
        Lamp *la;
        MTex *mtex;
        int a;
-
-       la= main->lamp.first;
-       while (la) {
+       
+       for (la = main->lamp.first; la; la = la->id.next) {
                if (la->id.flag & LIB_NEEDLINK) {
                        if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
                        
-                       for (a=0; a<MAX_MTEX; a++) {
-                               mtex= la->mtex[a];
+                       for (a = 0; a < MAX_MTEX; a++) {
+                               mtex = la->mtex[a];
                                if (mtex) {
-                                       mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
-                                       mtex->object= newlibadr(fd, la->id.lib, mtex->object);
+                                       mtex->tex = newlibadr_us(fd, la->id.lib, mtex->tex);
+                                       mtex->object = newlibadr(fd, la->id.lib, mtex->object);
                                }
                        }
                        
-                       la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
-
+                       la->ipo = newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
+                       
                        if (la->nodetree)
                                lib_link_ntree(fd, &la->id, la->nodetree);
                        
                        la->id.flag -= LIB_NEEDLINK;
                }
-               la= la->id.next;
        }
 }
 
@@ -2721,14 +2708,14 @@ static void direct_link_lamp(FileData *fd, Lamp *la)
 {
        int a;
        
-       la->adt= newdataadr(fd, la->adt);
+       la->adt = newdataadr(fd, la->adt);
        direct_link_animdata(fd, la->adt);
-
+       
        for (a=0; a<MAX_MTEX; a++) {
-               la->mtex[a]= newdataadr(fd, la->mtex[a]);
+               la->mtex[a] = newdataadr(fd, la->mtex[a]);
        }
        
-       la->curfalloff= newdataadr(fd, la->curfalloff);
+       la->curfalloff = newdataadr(fd, la->curfalloff);
        if (la->curfalloff)
                direct_link_curvemapping(fd, la->curfalloff);
 
@@ -2744,28 +2731,26 @@ static void direct_link_lamp(FileData *fd, Lamp *la)
 static void lib_link_key(FileData *fd, Main *main)
 {
        Key *key;
-
-       key= main->key.first;
-       while (key) {
+       
+       for (key = main->key.first; key; key = key->id.next) {
                /*check if we need to generate unique ids for the shapekeys*/
                if (!key->uidgen) {
                        KeyBlock *block;
-
+                       
                        key->uidgen = 1;
                        for (block=key->block.first; block; block=block->next) {
                                block->uid = key->uidgen++;
                        }
                }
-
+               
                if (key->id.flag & LIB_NEEDLINK) {
                        if (key->adt) lib_link_animdata(fd, &key->id, key->adt);
                        
-                       key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
-                       key->from= newlibadr(fd, key->id.lib, key->from);
-
+                       key->ipo = newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
+                       key->from = newlibadr(fd, key->id.lib, key->from);
+                       
                        key->id.flag -= LIB_NEEDLINK;
                }
-               key= key->id.next;
        }
 }
 
@@ -2773,31 +2758,29 @@ static void switch_endian_keyblock(Key *key, KeyBlock *kb)
 {
        int elemsize, a, b;
        char *data, *poin, *cp;
-
-       elemsize= key->elemsize;
-       data= kb->data;
-
-       for (a=0; a<kb->totelem; a++) {
-
-               cp= key->elemstr;
-               poin= data;
-
-               while ( cp[0] ) {  /* cp[0] == amount */
-
+       
+       elemsize = key->elemsize;
+       data = kb->data;
+       
+       for (a = 0; a < kb->totelem; a++) {
+               cp = key->elemstr;
+               poin = data;
+               
+               while (cp[0]) {  /* cp[0] == amount */
                        switch (cp[1]) {  /* cp[1] = type */
-                       case IPO_FLOAT:
-                       case IPO_BPOINT:
-                       case IPO_BEZTRIPLE:
-                               b= cp[0];
-                               while (b--) {
-                                       SWITCH_INT((*poin));
-                                       poin+= 4;
-                               }
-                               break;
+                               case IPO_FLOAT:
+                               case IPO_BPOINT:
+                               case IPO_BEZTRIPLE:
+                                       b = cp[0];
+                                       
+                                       while (b--) {
+                                               SWITCH_INT((*poin));
+                                               poin += 4;
+                                       }
+                                       break;
                        }
-
-                       cp+= 2;
-
+                       
+                       cp += 2;
                }
                data+= elemsize;
        }
@@ -2806,23 +2789,19 @@ static void switch_endian_keyblock(Key *key, KeyBlock *kb)
 static void direct_link_key(FileData *fd, Key *key)
 {
        KeyBlock *kb;
-
+       
        link_list(fd, &(key->block));
-
-       key->adt= newdataadr(fd, key->adt);
-       direct_link_animdata(fd, key->adt);
        
+       key->adt = newdataadr(fd, key->adt);
+       direct_link_animdata(fd, key->adt);
+               
        key->refkey= newdataadr(fd, key->refkey);
-
-       kb= key->block.first;
-       while (kb) {
-
-               kb->data= newdataadr(fd, kb->data);
+       
+       for (kb = key->block.first; kb; kb = kb->next) {
+               kb->data = newdataadr(fd, kb->data);
                
                if (fd->flags & FD_FLAGS_SWITCH_ENDIAN)
                        switch_endian_keyblock(key, kb);
-
-               kb= kb->next;
        }
 }
 
@@ -2832,37 +2811,36 @@ static void lib_link_mball(FileData *fd, Main *main)
 {
        MetaBall *mb;
        int a;
-
-       mb= main->mball.first;
-       while (mb) {
+       
+       for (mb = main->mball.first; mb; mb = mb->id.next) {
                if (mb->id.flag & LIB_NEEDLINK) {
                        if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
                        
-                       for (a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
-
-                       mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
-
+                       for (a = 0; a < mb->totcol; a++) 
+                               mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
+                       
+                       mb->ipo = newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
+                       
                        mb->id.flag -= LIB_NEEDLINK;
                }
-               mb= mb->id.next;
        }
 }
 
 static void direct_link_mball(FileData *fd, MetaBall *mb)
 {
-       mb->adt= newdataadr(fd, mb->adt);
+       mb->adt = newdataadr(fd, mb->adt);
        direct_link_animdata(fd, mb->adt);
        
-       mb->mat= newdataadr(fd, mb->mat);
+       mb->mat = newdataadr(fd, mb->mat);
        test_pointer_array(fd, (void **)&mb->mat);
-
+       
        link_list(fd, &(mb->elems));
-
-       mb->disp.first= mb->disp.last= NULL;
-       mb->editelems= NULL;
-       mb->bb= NULL;
+       
+       mb->disp.first = mb->disp.last = NULL;
+       mb->editelems = NULL;
+       mb->bb = NULL;
 /*     mb->edit_elems.first= mb->edit_elems.last= NULL;*/
-       mb->lastelem= NULL;
+       mb->lastelem = NULL;
 }
 
 /* ************ READ WORLD ***************** */
@@ -2872,46 +2850,44 @@ static void lib_link_world(FileData *fd, Main *main)
        World *wrld;
        MTex *mtex;
        int a;
-
-       wrld= main->world.first;
-       while (wrld) {
+       
+       for (wrld = main->world.first; wrld; wrld = wrld->id.next) {
                if (wrld->id.flag & LIB_NEEDLINK) {
                        if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
                        
-                       wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
+                       wrld->ipo = newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
                        
-                       for (a=0; a<MAX_MTEX; a++) {
-                               mtex= wrld->mtex[a];
+                       for (a=0; a < MAX_MTEX; a++) {
+                               mtex = wrld->mtex[a];
                                if (mtex) {
-                                       mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
-                                       mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
+                                       mtex->tex = newlibadr_us(fd, wrld->id.lib, mtex->tex);
+                                       mtex->object = newlibadr(fd, wrld->id.lib, mtex->object);
                                }
                        }
-
+                       
                        if (wrld->nodetree)
                                lib_link_ntree(fd, &wrld->id, wrld->nodetree);
                        
                        wrld->id.flag -= LIB_NEEDLINK;
                }
-               wrld= wrld->id.next;
        }
 }
 
 static void direct_link_world(FileData *fd, World *wrld)
 {
        int a;
-
-       wrld->adt= newdataadr(fd, wrld->adt);
+       
+       wrld->adt = newdataadr(fd, wrld->adt);
        direct_link_animdata(fd, wrld->adt);
-
-       for (a=0; a<MAX_MTEX; a++) {
-               wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
+       
+       for (a = 0; a < MAX_MTEX; a++) {
+               wrld->mtex[a] = newdataadr(fd, wrld->mtex[a]);
        }
-
-       wrld->nodetree= newdataadr(fd, wrld->nodetree);
+       
+       wrld->nodetree = newdataadr(fd, wrld->nodetree);
        if (wrld->nodetree)
                direct_link_nodetree(fd, wrld->nodetree);
-
+       
        wrld->preview = direct_link_preview_image(fd, wrld->preview);
 }
 
@@ -2921,20 +2897,18 @@ static void direct_link_world(FileData *fd, World *wrld)
 static void lib_link_vfont(FileData *UNUSED(fd), Main *main)
 {
        VFont *vf;
-
-       vf= main->vfont.first;
-       while (vf) {
+       
+       for (vf = main->vfont.first; vf; vf = vf->id.next) {
                if (vf->id.flag & LIB_NEEDLINK) {
                        vf->id.flag -= LIB_NEEDLINK;
                }
-               vf= vf->id.next;
        }
 }
 
 static void direct_link_vfont(FileData *fd, VFont *vf)
 {
-       vf->data= NULL;
-       vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
+       vf->data = NULL;
+       vf->packedfile = direct_link_packedfile(fd, vf->packedfile);
 }
 
 /* ************ READ TEXT ****************** */
@@ -2942,57 +2916,52 @@ static void direct_link_vfont(FileData *fd, VFont *vf)
 static void lib_link_text(FileData *UNUSED(fd), Main *main)
 {
        Text *text;
-
-       text= main->text.first;
-       while (text) {
+       
+       for (text = main->text.first; text; text = text->id.next) {
                if (text->id.flag & LIB_NEEDLINK) {
                        text->id.flag -= LIB_NEEDLINK;
                }
-               text= text->id.next;
        }
 }
 
 static void direct_link_text(FileData *fd, Text *text)
 {
        TextLine *ln;
-
-       text->name= newdataadr(fd, text->name);
-
-       text->undo_pos= -1;
-       text->undo_len= TXT_INIT_UNDO;
-       text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
-
-       text->compiled= NULL;
-
+       
+       text->name = newdataadr(fd, text->name);
+       
+       text->undo_pos = -1;
+       text->undo_len = TXT_INIT_UNDO;
+       text->undo_buf = MEM_mallocN(text->undo_len, "undo buf");
+       
+       text->compiled = NULL;
+       
 #if 0
        if (text->flags & TXT_ISEXT) {
                BKE_text_reload(text);
                }
                else {
 #endif
-
+       
        link_list(fd, &text->lines);
        link_list(fd, &text->markers);
-
-       text->curl= newdataadr(fd, text->curl);
-       text->sell= newdataadr(fd, text->sell);
-
-       ln= text->lines.first;
-       while (ln) {
-               ln->line= newdataadr(fd, ln->line);
-               ln->format= NULL;
+       
+       text->curl = newdataadr(fd, text->curl);
+       text->sell = newdataadr(fd, text->sell);
+       
+       for (ln = text->lines.first; ln; ln = ln->next) {
+               ln->line = newdataadr(fd, ln->line);
+               ln->format = NULL;
                
                if (ln->len != (int) strlen(ln->line)) {
                        printf("Error loading text, line lengths differ\n");
                        ln->len = strlen(ln->line);
                }
-
-               ln= ln->next;
        }
-
+       
        text->flags = (text->flags) & ~TXT_ISEXT;
-
-       text->id.us= 1;
+       
+       text->id.us = 1;
 }
 
 /* ************ READ IMAGE ***************** */
@@ -3000,15 +2969,13 @@ static void direct_link_text(FileData *fd, Text *text)
 static void lib_link_image(FileData *fd, Main *main)
 {
        Image *ima;
-
-       ima= main->image.first;
-       while (ima) {
+       
+       for (ima = main->image.first; ima; ima = ima->id.next) {
                if (ima->id.flag & LIB_NEEDLINK) {
                        if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
-
+                       
                        ima->id.flag -= LIB_NEEDLINK;
                }
-               ima= ima->id.next;
        }
 }
 
@@ -3016,18 +2983,18 @@ static void link_ibuf_list(FileData *fd, ListBase *lb)
 {
        Link *ln, *prev;
        
-       if (lb->first==NULL) return;
+       if (lb->first == NULL) return;
        
-       lb->first= newimaadr(fd, lb->first);
-       ln= lb->first;
-       prev= NULL;
+       lb->first = newimaadr(fd, lb->first);
+       ln = lb->first;
+       prev = NULL;
        while (ln) {
-               ln->next= newimaadr(fd, ln->next);
-               ln->prev= prev;
-               prev= ln;
-               ln= ln->next;
+               ln->next = newimaadr(fd, ln->next);
+               ln->prev = prev;
+               prev = ln;
+               ln = ln->next;
        }
-       lb->last= prev;
+       lb->last = prev;
 }
 
 static void direct_link_image(FileData *fd, Image *ima)
@@ -3036,33 +3003,33 @@ static void direct_link_image(FileData *fd, Image *ima)
        if (fd->imamap)
                link_ibuf_list(fd, &ima->ibufs);
        else
-               ima->ibufs.first= ima->ibufs.last= NULL;
+               ima->ibufs.first = ima->ibufs.last = NULL;
        
        /* if not restored, we keep the binded opengl index */
-       if (ima->ibufs.first==NULL) {
-               ima->bindcode= 0;
-               ima->gputexture= NULL;
+       if (ima->ibufs.first == NULL) {
+               ima->bindcode = 0;
+               ima->gputexture = NULL;
        }
        
-       ima->anim= NULL;
-       ima->rr= NULL;
-       ima->repbind= NULL;
+       ima->anim = NULL;
+       ima->rr = NULL;
+       ima->repbind = NULL;
        
        /* undo system, try to restore render buffers */
        if (fd->imamap) {
                int a;
                
-               for (a=0; a<IMA_MAX_RENDER_SLOT; a++)
-                       ima->renders[a]= newimaadr(fd, ima->renders[a]);
+               for (a = 0; a < IMA_MAX_RENDER_SLOT; a++)
+                       ima->renders[a] = newimaadr(fd, ima->renders[a]);
        }
        else {
                memset(ima->renders, 0, sizeof(ima->renders));
-               ima->last_render_slot= ima->render_slot;
+               ima->last_render_slot = ima->render_slot;
        }
        
        ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
        ima->preview = direct_link_preview_image(fd, ima->preview);
-       ima->ok= 1;
+       ima->ok = 1;
 }
 
 
@@ -3072,28 +3039,27 @@ static void lib_link_curve(FileData *fd, Main *main)
 {
        Curve *cu;
        int a;
-
-       cu= main->curve.first;
-       while (cu) {
+       
+       for (cu = main->curve.first; cu; cu = cu->id.next) {
                if (cu->id.flag & LIB_NEEDLINK) {
                        if (cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
-
-                       for (a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
-
-                       cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
-                       cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
-                       cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
-                       cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
-                       cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);                   
-                       cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
-                       cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
-
-                       cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
-                       cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
-
+                       
+                       for (a = 0; a < cu->totcol; a++) 
+                               cu->mat[a] = newlibadr_us(fd, cu->id.lib, cu->mat[a]);
+                       
+                       cu->bevobj = newlibadr(fd, cu->id.lib, cu->bevobj);
+                       cu->taperobj = newlibadr(fd, cu->id.lib, cu->taperobj);
+                       cu->textoncurve = newlibadr(fd, cu->id.lib, cu->textoncurve);
+                       cu->vfont = newlibadr_us(fd, cu->id.lib, cu->vfont);
+                       cu->vfontb = newlibadr_us(fd, cu->id.lib, cu->vfontb);                  
+                       cu->vfonti = newlibadr_us(fd, cu->id.lib, cu->vfonti);
+                       cu->vfontbi = newlibadr_us(fd, cu->id.lib, cu->vfontbi);
+                       
+                       cu->ipo = newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
+                       cu->key = newlibadr_us(fd, cu->id.lib, cu->key);
+                       
                        cu->id.flag -= LIB_NEEDLINK;
                }
-               cu= cu->id.next;
        }
 }
 
@@ -3101,15 +3067,15 @@ static void lib_link_curve(FileData *fd, Main *main)
 static void switch_endian_knots(Nurb *nu)
 {
        int len;
-
+       
        if (nu->knotsu) {
-               len= KNOTSU(nu);
+               len = KNOTSU(nu);
                while (len--) {
                        SWITCH_INT(nu->knotsu[len]);
                }
        }
        if (nu->knotsv) {
-               len= KNOTSV(nu);
+               len = KNOTSV(nu);
                while (len--) {
                        SWITCH_INT(nu->knotsv[len]);
                }
@@ -3124,21 +3090,21 @@ static void direct_link_curve(FileData *fd, Curve *cu)
        cu->adt= newdataadr(fd, cu->adt);
        direct_link_animdata(fd, cu->adt);
        
-       cu->mat= newdataadr(fd, cu->mat);
+       cu->mat = newdataadr(fd, cu->mat);
        test_pointer_array(fd, (void **)&cu->mat);
-       cu->str= newdataadr(fd, cu->str);
+       cu->str = newdataadr(fd, cu->str);
        cu->strinfo= newdataadr(fd, cu->strinfo);       
-       cu->tb= newdataadr(fd, cu->tb);
+       cu->tb = newdataadr(fd, cu->tb);
 
        if (cu->vfont == NULL) link_list(fd, &(cu->nurb));
        else {
                cu->nurb.first=cu->nurb.last= NULL;
-
-               tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
+               
+               tb = MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
                if (cu->tb) {
                        memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
                        MEM_freeN(cu->tb);
-                       cu->tb= tb;                     
+                       cu->tb = tb;                    
                }
                else {
                        cu->totbox = 1;
@@ -3149,28 +3115,25 @@ static void direct_link_curve(FileData *fd, Curve *cu)
                if (cu->wordspace == 0.0f) cu->wordspace = 1.0f;
        }
 
-       cu->bev.first=cu->bev.last= NULL;
-       cu->disp.first=cu->disp.last= NULL;
-       cu->editnurb= NULL;
-       cu->lastsel= NULL;
-       cu->path= NULL;
-       cu->editfont= NULL;
+       cu->bev.first = cu->bev.last = NULL;
+       cu->disp.first = cu->disp.last = NULL;
+       cu->editnurb = NULL;
+       cu->lastsel = NULL;
+       cu->path = NULL;
+       cu->editfont = NULL;
        
-       nu= cu->nurb.first;
-       while (nu) {
-               nu->bezt= newdataadr(fd, nu->bezt);
-               nu->bp= newdataadr(fd, nu->bp);
-               nu->knotsu= newdataadr(fd, nu->knotsu);
-               nu->knotsv= newdataadr(fd, nu->knotsv);
+       for (nu = cu->nurb.first; nu; nu = nu->next) {
+               nu->bezt = newdataadr(fd, nu->bezt);
+               nu->bp = newdataadr(fd, nu->bp);
+               nu->knotsu = newdataadr(fd, nu->knotsu);
+               nu->knotsv = newdataadr(fd, nu->knotsv);
                if (cu->vfont == NULL) nu->charidx= nu->mat_nr;
-
+               
                if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
                        switch_endian_knots(nu);
                }
-
-               nu= nu->next;
        }
-       cu->bb= NULL;
+       cu->bb = NULL;
 }
 
 /* ************ READ TEX ***************** */
@@ -3178,81 +3141,81 @@ static void direct_link_curve(FileData *fd, Curve *cu)
 static void lib_link_texture(FileData *fd, Main *main)
 {
        Tex *tex;
-
-       tex= main->tex.first;
-       while (tex) {
+       
+       for (tex = main->tex.first; tex; tex = tex->id.next) {
                if (tex->id.flag & LIB_NEEDLINK) {
                        if (tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
-
-                       tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
-                       tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
-                       if (tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
+                       
+                       tex->ima = newlibadr_us(fd, tex->id.lib, tex->ima);
+                       tex->ipo = newlibadr_us(fd, tex->id.lib, tex->ipo);
+                       if (tex->env)
+                               tex->env->object = newlibadr(fd, tex->id.lib, tex->env->object);
                        if (tex->pd)
-                               tex->pd->object= newlibadr(fd, tex->id.lib, tex->pd->object);
-                       if (tex->vd) tex->vd->object= newlibadr(fd, tex->id.lib, tex->vd->object);
-                       if (tex->ot) tex->ot->object= newlibadr(fd, tex->id.lib, tex->ot->object);
-                               
-
+                               tex->pd->object = newlibadr(fd, tex->id.lib, tex->pd->object);
+                       if (tex->vd)
+                               tex->vd->object = newlibadr(fd, tex->id.lib, tex->vd->object);
+                       if (tex->ot)
+                               tex->ot->object = newlibadr(fd, tex->id.lib, tex->ot->object);
+                       
                        if (tex->nodetree)
                                lib_link_ntree(fd, &tex->id, tex->nodetree);
                        
                        tex->id.flag -= LIB_NEEDLINK;
                }
-               tex= tex->id.next;
        }
 }
 
 static void direct_link_texture(FileData *fd, Tex *tex)
 {
-       tex->adt= newdataadr(fd, tex->adt);
+       tex->adt = newdataadr(fd, tex->adt);
        direct_link_animdata(fd, tex->adt);
        
-       tex->plugin= newdataadr(fd, tex->plugin);
+       tex->plugin = newdataadr(fd, tex->plugin);
        if (tex->plugin) {
-               tex->plugin->handle= NULL;
+               tex->plugin->handle = NULL;
                open_plugin_tex(tex->plugin);
                /* initialize data for this instance, if an initialization
                 * function exists.
                 */
                if (tex->plugin->instance_init)
-                       tex->plugin->instance_init((void *) tex->plugin->data);
+                       tex->plugin->instance_init((void *)tex->plugin->data);
        }
-       tex->coba= newdataadr(fd, tex->coba);
-       tex->env= newdataadr(fd, tex->env);
+       tex->coba = newdataadr(fd, tex->coba);
+       tex->env = newdataadr(fd, tex->env);
        if (tex->env) {
-               tex->env->ima= NULL;
+               tex->env->ima = NULL;
                memset(tex->env->cube, 0, 6*sizeof(void *));
                tex->env->ok= 0;
        }
-       tex->pd= newdataadr(fd, tex->pd);
+       tex->pd = newdataadr(fd, tex->pd);
        if (tex->pd) {
                tex->pd->point_tree = NULL;
-               tex->pd->coba= newdataadr(fd, tex->pd->coba);
-               tex->pd->falloff_curve= newdataadr(fd, tex->pd->falloff_curve);
+               tex->pd->coba = newdataadr(fd, tex->pd->coba);
+               tex->pd->falloff_curve = newdataadr(fd, tex->pd->falloff_curve);
                if (tex->pd->falloff_curve) {
                        direct_link_curvemapping(fd, tex->pd->falloff_curve);
                }
        }
        
-       tex->vd= newdataadr(fd, tex->vd);
+       tex->vd = newdataadr(fd, tex->vd);
        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->vd = MEM_callocN(sizeof(VoxelData), "direct_link_texture VoxelData");
        }
        
-       tex->ot= newdataadr(fd, tex->ot);
+       tex->ot = newdataadr(fd, tex->ot);
        
-       tex->nodetree= newdataadr(fd, tex->nodetree);
+       tex->nodetree = newdataadr(fd, tex->nodetree);
        if (tex->nodetree)
                direct_link_nodetree(fd, tex->nodetree);
        
        tex->preview = direct_link_preview_image(fd, tex->preview);
-
-       tex->iuser.ok= 1;
+       
+       tex->iuser.ok = 1;
 }
 
 
@@ -3264,24 +3227,23 @@ static void lib_link_material(FileData *fd, Main *main)
        Material *ma;
        MTex *mtex;
        int a;
-
-       ma= main->mat.first;
-       while (ma) {
+       
+       for (ma = main->mat.first; ma; ma = ma->id.next) {
                if (ma->id.flag & LIB_NEEDLINK) {
                        if (ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
-
+                       
                        /* Link ID Properties -- and copy this comment EXACTLY for easy finding
                         * of library blocks that implement this.*/
                        if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
-
-                       ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
-                       ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
                        
-                       for (a=0; a<MAX_MTEX; a++) {
-                               mtex= ma->mtex[a];
+                       ma->ipo = newlibadr_us(fd, ma->id.lib, ma->ipo);
+                       ma->group = newlibadr_us(fd, ma->id.lib, ma->group);
+                       
+                       for (a = 0; a < MAX_MTEX; a++) {
+                               mtex = ma->mtex[a];
                                if (mtex) {
-                                       mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
-                                       mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
+                                       mtex->tex = newlibadr_us(fd, ma->id.lib, mtex->tex);
+                                       mtex->object = newlibadr(fd, ma->id.lib, mtex->object);
                                }
                        }
                        
@@ -3290,28 +3252,27 @@ static void lib_link_material(FileData *fd, Main *main)
                        
                        ma->id.flag -= LIB_NEEDLINK;
                }
-               ma= ma->id.next;
        }
 }
 
 static void direct_link_material(FileData *fd, Material *ma)
 {
        int a;
-
-       ma->adt= newdataadr(fd, ma->adt);
+       
+       ma->adt = newdataadr(fd, ma->adt);
        direct_link_animdata(fd, ma->adt);
        
-       for (a=0; a<MAX_MTEX; a++) {
-               ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
+       for (a = 0; a < MAX_MTEX; a++) {
+               ma->mtex[a] = newdataadr(fd, ma->mtex[a]);
        }
-
-       ma->ramp_col= newdataadr(fd, ma->ramp_col);
-       ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
        
-       ma->nodetree= newdataadr(fd, ma->nodetree);
+       ma->ramp_col = newdataadr(fd, ma->ramp_col);
+       ma->ramp_spec = newdataadr(fd, ma->ramp_spec);
+       
+       ma->nodetree = newdataadr(fd, ma->nodetree);
        if (ma->nodetree)
                direct_link_nodetree(fd, ma->nodetree);
-
+       
        ma->preview = direct_link_preview_image(fd, ma->preview);
        ma->gpumaterial.first = ma->gpumaterial.last = NULL;
 }
@@ -3334,39 +3295,39 @@ static void direct_link_pointcache(FileData *fd, PointCache *cache)
                PTCacheMem *pm;
                PTCacheExtra *extra;
                int i;
-
+               
                link_list(fd, &cache->mem_cache);
-
+               
                pm = cache->mem_cache.first;
-
+               
                for (; pm; pm=pm->next) {
                        for (i=0; i<BPHYS_TOT_DATA; i++) {
                                pm->data[i] = newdataadr(fd, pm->data[i]);
                                
                                /* the cache saves non-struct data without DNA */
                                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];
+                                       int j, tot = (BKE_ptcache_data_size (i) * pm->totpoint)/4; /* data_size returns bytes */
+                                       int *poin = pm->data[i];
                                        
-                                       for (j= 0; j<tot; j++)
+                                       for (j = 0; j < tot; j++)
                                                SWITCH_INT(poin[j]);
                                }
                        }
                        
                        link_list(fd, &pm->extradata);
-
+                       
                        for (extra=pm->extradata.first; extra; extra=extra->next)
                                extra->data = newdataadr(fd, extra->data);
                }
        }
        else
                cache->mem_cache.first = cache->mem_cache.last = NULL;
-
+       
        cache->flag &= ~PTCACHE_SIMULATION_VALID;
-       cache->simframe= 0;
-       cache->edit= NULL;
-       cache->free_edit= NULL;
-       cache->cached_frames= NULL;
+       cache->simframe = 0;
+       cache->edit = NULL;
+       cache->free_edit = NULL;
+       cache->cached_frames = NULL;
 }
 
 static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointCache **ocache, int force_disk)
@@ -3381,7 +3342,7 @@ static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointC
                                cache->step = 1;
                        }
                }
-
+               
                *ocache = newdataadr(fd, *ocache);
        }
        else if (*ocache) {
@@ -3392,7 +3353,7 @@ static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointC
                        (*ocache)->flag |= PTCACHE_DISK_CACHE;
                        (*ocache)->step = 1;
                }
-
+               
                ptcaches->first = ptcaches->last = *ocache;
        }
 }
@@ -3400,7 +3361,7 @@ static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointC
 static void lib_link_partdeflect(FileData *fd, ID *id, PartDeflect *pd)
 {
        if (pd && pd->tex)
-               pd->tex=newlibadr_us(fd, id->lib, pd->tex);
+               pd->tex = newlibadr_us(fd, id->lib, pd->tex);
 }
 
 static void lib_link_particlesettings(FileData *fd, Main *main)
@@ -3409,24 +3370,23 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
        ParticleDupliWeight *dw;
        MTex *mtex;
        int a;
-
-       part= main->particle.first;
-       while (part) {
+       
+       for (part = main->particle.first; part; part = part->id.next) {
                if (part->id.flag & LIB_NEEDLINK) {
                        if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
-                       part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
+                       part->ipo = newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
                        
                        part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
                        part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
                        part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
                        part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
-
+                       
                        lib_link_partdeflect(fd, &part->id, part->pd);
                        lib_link_partdeflect(fd, &part->id, part->pd2);
-
+                       
                        if (part->effector_weights)
                                part->effector_weights->group = newlibadr(fd, part->id.lib, part->effector_weights->group);
-
+                       
                        if (part->dupliweights.first && part->dup_group) {
                                int index_ok = 0;
                                /* check for old files without indices (all indexes 0) */
@@ -3443,7 +3403,7 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
                                                }
                                        }
                                }
-
+                               
                                if (index_ok) {
                                        /* if we have indexes, let's use them */
                                        dw = part->dupliweights.first;
@@ -3461,7 +3421,7 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
                        else {
                                part->dupliweights.first = part->dupliweights.last = NULL;
                        }
-
+                       
                        if (part->boids) {
                                BoidState *state = part->boids->states.first;
                                BoidRule *rule;
@@ -3486,31 +3446,31 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
                                }
                        }
                        
-                       for (a=0; a<MAX_MTEX; a++) {
+                       for (a = 0; a < MAX_MTEX; a++) {
                                mtex= part->mtex[a];
                                if (mtex) {
                                        mtex->tex = newlibadr_us(fd, part->id.lib, mtex->tex);
                                        mtex->object = newlibadr(fd, part->id.lib, mtex->object);
                                }
                        }
-
+                       
                        part->id.flag -= LIB_NEEDLINK;
                }
-               part= part->id.next;
        }
 }
 
 static void direct_link_partdeflect(PartDeflect *pd)
 {
-       if (pd) pd->rng=NULL;
+       if (pd) pd->rng = NULL;
 }
 
 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
 {
        int a;
-       part->adt= newdataadr(fd, part->adt);
-       part->pd= newdataadr(fd, part->pd);
-       part->pd2= newdataadr(fd, part->pd2);
+       
+       part->adt = newdataadr(fd, part->adt);
+       part->pd = newdataadr(fd, part->pd);
+       part->pd2 = newdataadr(fd, part->pd2);
 
        direct_link_animdata(fd, part->adt);
        direct_link_partdeflect(part->pd);
@@ -3522,8 +3482,8 @@ static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
 
        link_list(fd, &part->dupliweights);
 
-       part->boids= newdataadr(fd, part->boids);
-       part->fluid= newdataadr(fd, part->fluid);
+       part->boids = newdataadr(fd, part->boids);
+       part->fluid = newdataadr(fd, part->fluid);
 
        if (part->boids) {
                BoidState *state;
@@ -3535,8 +3495,8 @@ static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
                        link_list(fd, &state->actions);
                }
        }
-       for (a=0; a<MAX_MTEX; a++) {
-               part->mtex[a]= newdataadr(fd, part->mtex[a]);
+       for (a = 0; a < MAX_MTEX; a++) {
+               part->mtex[a] = newdataadr(fd, part->mtex[a]);
        }
 }
 
@@ -3545,32 +3505,32 @@ static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase
        ParticleSystem *psys, *psysnext;
 
        for (psys=particles->first; psys; psys=psysnext) {
-               psysnext= psys->next;
+               psysnext = psys->next;
                
                psys->part = newlibadr_us(fd, id->lib, psys->part);
                if (psys->part) {
                        ParticleTarget *pt = psys->targets.first;
-
+                       
                        for (; pt; pt=pt->next)
                                pt->ob=newlibadr(fd, id->lib, pt->ob);
-
-                       psys->parent= newlibadr_us(fd, id->lib, psys->parent);
+                       
+                       psys->parent = newlibadr_us(fd, id->lib, psys->parent);
                        psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
-
+                       
                        if (psys->clmd) {
                                /* XXX - from reading existing code this seems correct but intended usage of
                                 * pointcache should /w cloth should be added in 'ParticleSystem' - campbell */
-                               psys->clmd->point_cache= psys->pointcache;
-                               psys->clmd->ptcaches.first= psys->clmd->ptcaches.last= NULL;
-                               psys->clmd->coll_parms->group= newlibadr(fd, id->lib, psys->clmd->coll_parms->group);
+                               psys->clmd->point_cache = psys->pointcache;
+                               psys->clmd->ptcaches.first = psys->clmd->ptcaches.last= NULL;
+                               psys->clmd->coll_parms->group = newlibadr(fd, id->lib, psys->clmd->coll_parms->group);
                        }
                }
                else {
                        /* particle modifier must be removed before particle system */
-                       ParticleSystemModifierData *psmd= psys_get_modifier(ob, psys);
+                       ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys);
                        BLI_remlink(&ob->modifiers, psmd);
                        modifier_free((ModifierData *)psmd);
-
+                       
                        BLI_remlink(particles, psys);
                        MEM_freeN(psys);
                }
@@ -3581,7 +3541,7 @@ static void direct_link_particlesystems(FileData *fd, ListBase *particles)
        ParticleSystem *psys;
        ParticleData *pa;
        int a;
-
+       
        for (psys=particles->first; psys; psys=psys->next) {
                psys->particles=newdataadr(fd, psys->particles);
                
@@ -3595,10 +3555,10 @@ static void direct_link_particlesystems(FileData *fd, ListBase *particles)
                                pa->keys= NULL;
                                pa->totkey= 0;
                        }
-
+                       
                        psys->flag &= ~PSYS_KEYED;
                }
-
+               
                if (psys->particles && psys->particles->boid) {
                        pa = psys->particles;
                        pa->boid = newdataadr(fd, pa->boid);
@@ -3609,14 +3569,14 @@ static void direct_link_particlesystems(FileData *fd, ListBase *particles)
                        for (a=0, pa=psys->particles; a<psys->totpart; a++, pa++)
                                pa->boid = NULL;
                }
-
+               
                psys->fluid_springs = newdataadr(fd, psys->fluid_springs);
-
+               
                psys->child = newdataadr(fd, psys->child);
                psys->effectors = NULL;
-
+               
                link_list(fd, &psys->targets);
-
+               
                psys->edit = NULL;
                psys->free_edit = NULL;
                psys->pathcache = NULL;
@@ -3628,7 +3588,7 @@ static void direct_link_particlesystems(FileData *fd, ListBase *particles)
                psys->renderdata = NULL;
                
                direct_link_pointcache_list(fd, &psys->ptcaches, &psys->pointcache, 0);
-
+               
                if (psys->clmd) {
                        psys->clmd = newdataadr(fd, psys->clmd);
                        psys->clmd->clothObject = NULL;
@@ -3641,12 +3601,12 @@ static void direct_link_particlesystems(FileData *fd, ListBase *particles)
                                if (psys->clmd->sim_parms->presets > 10)
                                        psys->clmd->sim_parms->presets = 0;
                        }
-
+                       
                        psys->hair_in_dm = psys->hair_out_dm = NULL;
-
+                       
                        psys->clmd->point_cache = psys->pointcache;
                }
-
+               
                psys->tree = NULL;
                psys->bvhtree = NULL;
        }
@@ -3659,11 +3619,11 @@ 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++) {
+       for (i = 0; i < totface; i++, tf++) {
                tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
                if (tf->tpage && tf->tpage->id.us==0)
                        tf->tpage->id.us= 1;
@@ -3673,7 +3633,7 @@ static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
 {
        int i;  
-       for (i=0; i<fdata->totlayer; i++) {
+       for (i = 0; i < fdata->totlayer; i++) {
                CustomDataLayer *layer = &fdata->layers[i];
                
                if (layer->type == CD_MTFACE)
@@ -3686,17 +3646,17 @@ static void lib_link_customdata_mtpoly(FileData *fd, Mesh *me, CustomData *pdata
 {
        int i;
 
-       for (i=0; i<pdata->totlayer; i++) {
+       for (i=0; i < pdata->totlayer; i++) {
                CustomDataLayer *layer = &pdata->layers[i];
                
                if (layer->type == CD_MTEXPOLY) {
                        MTexPoly *tf= layer->data;
                        int i;
-
-                       for (i=0; i<totface; i++, tf++) {
-                               tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
+                       
+                       for (i = 0; i < totface; i++, tf++) {
+                               tf->tpage = newlibadr(fd, me->id.lib, tf->tpage);
                                if (tf->tpage && tf->tpage->id.us==0)
-                                       tf->tpage->id.us= 1;
+                                       tf->tpage->id.us = 1;
                        }
                }
        }
@@ -3705,35 +3665,34 @@ static void lib_link_customdata_mtpoly(FileData *fd, Mesh *me, CustomData *pdata
 static void lib_link_mesh(FileData *fd, Main *main)
 {
        Mesh *me;
-
-       me= main->mesh.first;
-       while (me) {
+       
+       for (me = main->mesh.first; me; me = me->id.next) {
                if (me->id.flag & LIB_NEEDLINK) {
                        int i;
-
+                       
                        /* Link ID Properties -- and copy this comment EXACTLY for easy finding
                         * of library blocks that implement this.*/
                        if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
                        if (me->adt) lib_link_animdata(fd, &me->id, me->adt);
-
+                       
                        /* this check added for python created meshes */
                        if (me->mat) {
-                               for (i=0; i<me->totcol; i++) {
-                                       me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
+                               for (i = 0; i < me->totcol; i++) {
+                                       me->mat[i] = newlibadr_us(fd, me->id.lib, me->mat[i]);
                                }
                        }
-                       else me->totcol= 0;
-
-                       me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
-                       me->key= newlibadr_us(fd, me->id.lib, me->key);
-                       me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
-
+                       else me->totcol = 0;
+                       
+                       me->ipo = newlibadr_us(fd, me->id.lib, me->ipo); // XXX: deprecated: old anim sys
+                       me->key = newlibadr_us(fd, me->id.lib, me->key);
+                       me->texcomesh = newlibadr_us(fd, me->id.lib, me->texcomesh);
+                       
                        lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
                        lib_link_customdata_mtpoly(fd, me, &me->pdata, me->totpoly);
                        if (me->mr && me->mr->levels.first)
                                lib_link_customdata_mtface(fd, me, &me->mr->fdata,
                                                           ((MultiresLevel*)me->mr->levels.first)->totface);
-
+                       
                        /*check if we need to convert mfaces to mpolys*/
                        if (me->totface && !me->totpoly) {
                                /* temporarily switch main so that reading from
@@ -3742,7 +3701,7 @@ static void lib_link_mesh(FileData *fd, Main *main)
                                G.main = main;
                                
                                BKE_mesh_convert_mfaces_to_mpolys(me);
-
+                               
                                G.main = gmain;
                        }
                        
@@ -3761,12 +3720,11 @@ static void lib_link_mesh(FileData *fd, Main *main)
 #else
                        BKE_mesh_tessface_clear(me);
 #endif
-
+                       
                        me->id.flag -= LIB_NEEDLINK;
                }
-               me= me->id.next;
        }
-
+       
        /* convert texface options to material */
        convert_tface_mt(fd, main);
 }
@@ -3774,24 +3732,24 @@ static void lib_link_mesh(FileData *fd, Main *main)
 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
 {
        int i;
-
+       
        if (mdverts == NULL) {
                return;
        }
-
-       for (i= count; i > 0; i--, mdverts++) {
+       
+       for (i = count; i > 0; i--, mdverts++) {
                /*convert to vgroup allocation system*/
                MDeformWeight *dw;
-               if (mdverts->dw && (dw= newdataadr(fd, mdverts->dw))) {
-                       const ssize_t dw_len= mdverts->totweight * sizeof(MDeformWeight);
-                       void *dw_tmp= MEM_mallocN(dw_len, "direct_link_dverts");
+               if (mdverts->dw && (dw = newdataadr(fd, mdverts->dw))) {
+                       const ssize_t dw_len = mdverts->totweight * sizeof(MDeformWeight);
+                       void *dw_tmp = MEM_mallocN(dw_len, "direct_link_dverts");
                        memcpy(dw_tmp, dw, dw_len);
-                       mdverts->dw= dw_tmp;
+                       mdverts->dw = dw_tmp;
                        MEM_freeN(dw);
                }
                else {
-                       mdverts->dw= NULL;
-                       mdverts->totweight= 0;
+                       mdverts->dw = NULL;
+                       mdverts->totweight = 0;
                }
        }
 }
@@ -3800,11 +3758,11 @@ static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps, int exte
 {
        if (mdisps) {
                int i;
-
+               
                for (i = 0; i < count; ++i) {
                        mdisps[i].disps = newdataadr(fd, mdisps[i].disps);
                        mdisps[i].hidden = newdataadr(fd, mdisps[i].hidden);
-
+                       
                        if (mdisps[i].totdisp && !mdisps[i].level) {
                                /* this calculation is only correct for loop mdisps;
                                 * if loading pre-BMesh face mdisps this will be
@@ -3813,13 +3771,13 @@ static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps, int exte
                                float gridsize = sqrtf(mdisps[i].totdisp);
                                mdisps[i].level = (int)(logf(gridsize - 1.0f) / (float)M_LN2) + 1;
                        }
-
-                       if ( (fd->flags & FD_FLAGS_SWITCH_ENDIAN) && (mdisps[i].disps) ) {
+                       
+                       if ((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && (mdisps[i].disps)) {
                                /* DNA_struct_switch_endian doesn't do endian swap for (*disps)[] */
                                /* this does swap for data written at write_mdisps() - readfile.c */
                                int x;
-                               float *tmpdisps= *mdisps[i].disps;
-                               for (x=0;x<mdisps[i].totdisp*3;x++) {
+                               float *tmpdisps = *mdisps[i].disps;
+                               for (x = 0; x < mdisps[i].totdisp * 3; x++) {
                                        SWITCH_INT(*tmpdisps);
                                        tmpdisps++;
                                }
@@ -3832,12 +3790,12 @@ static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps, int exte
 
 static void direct_link_grid_paint_mask(FileData *fd, int count, GridPaintMask *grid_paint_mask)
 {
-       if(grid_paint_mask) {
+       if (grid_paint_mask) {
                int i;
-
-               for(i = 0; i < count; ++i) {
+               
+               for (i = 0; i < count; ++i) {
                        GridPaintMask *gpm = &grid_paint_mask[i];
-                       if(gpm->data)
+                       if (gpm->data)
                                gpm->data = newdataadr(fd, gpm->data);
                }
        }
@@ -3847,34 +3805,34 @@ static void direct_link_grid_paint_mask(FileData *fd, int count, GridPaintMask *
 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
 {
        int i = 0;
-
-       data->layers= newdataadr(fd, data->layers);
-
+       
+       data->layers = newdataadr(fd, data->layers);
+       
        /* annoying workaround for bug [#31079] loading legacy files with
         * no polygons _but_ have stale customdata */
        if (UNLIKELY(count == 0 && data->layers == NULL && data->totlayer != 0)) {
                memset(data, 0, sizeof(*data));
                return;
        }
-
-       data->external= newdataadr(fd, data->external);
-
+       
+       data->external = newdataadr(fd, data->external);
+       
        while (i < data->totlayer) {
                CustomDataLayer *layer = &data->layers[i];
-
+               
                if (layer->flag & CD_FLAG_EXTERNAL)
                        layer->flag &= ~CD_FLAG_IN_MEMORY;
-
+               
                if (CustomData_verify_versions(data, i)) {
                        layer->data = newdataadr(fd, layer->data);
                        if (layer->type == CD_MDISPS)
                                direct_link_mdisps(fd, count, layer->data, layer->flag & CD_FLAG_EXTERNAL);
-                       else if(layer->type == CD_GRID_PAINT_MASK)
+                       else if (layer->type == CD_GRID_PAINT_MASK)
                                direct_link_grid_paint_mask(fd, count, layer->data);
                        i++;
                }
        }
-
+       
        CustomData_update_typemap(data);
 }
 
@@ -3882,68 +3840,67 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
 {
        mesh->mat= newdataadr(fd, mesh->mat);
        test_pointer_array(fd, (void **)&mesh->mat);
-
-       mesh->mvert= newdataadr(fd, mesh->mvert);
-       mesh->medge= newdataadr(fd, mesh->medge);
-       mesh->mface= newdataadr(fd, mesh->mface);
-       mesh->mloop= newdataadr(fd, mesh->mloop);
-       mesh->mpoly= newdataadr(fd, mesh->mpoly);
-       mesh->tface= newdataadr(fd, mesh->tface);
-       mesh->mtface= newdataadr(fd, mesh->mtface);
-       mesh->mcol= newdataadr(fd, mesh->mcol);
-       mesh->msticky= newdataadr(fd, mesh->msticky);
-       mesh->dvert= newdataadr(fd, mesh->dvert);
-       mesh->mloopcol= newdataadr(fd, mesh->mloopcol);
-       mesh->mloopuv= newdataadr(fd, mesh->mloopuv);
-       mesh->mtpoly= newdataadr(fd, mesh->mtpoly);
+       
+       mesh->mvert = newdataadr(fd, mesh->mvert);
+       mesh->medge = newdataadr(fd, mesh->medge);
+       mesh->mface = newdataadr(fd, mesh->mface);
+       mesh->mloop = newdataadr(fd, mesh->mloop);
+       mesh->mpoly = newdataadr(fd, mesh->mpoly);
+       mesh->tface = newdataadr(fd, mesh->tface);
+       mesh->mtface = newdataadr(fd, mesh->mtface);
+       mesh->mcol = newdataadr(fd, mesh->mcol);
+       mesh->msticky = newdataadr(fd, mesh->msticky);
+       mesh->dvert = newdataadr(fd, mesh->dvert);
+       mesh->mloopcol = newdataadr(fd, mesh->mloopcol);
+       mesh->mloopuv = newdataadr(fd, mesh->mloopuv);
+       mesh->mtpoly = newdataadr(fd, mesh->mtpoly);
        mesh->mselect = newdataadr(fd, mesh->mselect);
-
+       
        /* animdata */
-       mesh->adt= newdataadr(fd, mesh->adt);
+       mesh->adt = newdataadr(fd, mesh->adt);
        direct_link_animdata(fd, mesh->adt);
-
+       
        /* normally direct_link_dverts should be called in direct_link_customdata,
         * but for backwards compat in do_versions to work we do it here */
        direct_link_dverts(fd, mesh->totvert, mesh->dvert);
-
+       
        direct_link_customdata(fd, &mesh->vdata, mesh->totvert);
        direct_link_customdata(fd, &mesh->edata, mesh->totedge);
        direct_link_customdata(fd, &mesh->fdata, mesh->totface);
        direct_link_customdata(fd, &mesh->ldata, mesh->totloop);
        direct_link_customdata(fd, &mesh->pdata, mesh->totpoly);
        
-
+       
 #ifdef USE_BMESH_FORWARD_COMPAT
        /* NEVER ENABLE THIS CODE INTO BMESH!
         * THIS IS FOR LOADING BMESH INTO OLDER FILES ONLY */
-       mesh->mpoly= newdataadr(fd, mesh->mpoly);
-       mesh->mloop= newdataadr(fd, mesh->mloop);
+       mesh->mpoly = newdataadr(fd, mesh->mpoly);
+       mesh->mloop = newdataadr(fd, mesh->mloop);
 
        direct_link_customdata(fd, &mesh->pdata, mesh->totpoly);
        direct_link_customdata(fd, &mesh->ldata, mesh->totloop);
 
        if (mesh->mpoly) {
                /* be clever and load polygons as mfaces */
-
                mesh->totface= BKE_mesh_mpoly_to_mface(&mesh->fdata, &mesh->ldata, &mesh->pdata,
                                                   mesh->totface, mesh->totloop, mesh->totpoly);
-
+               
                CustomData_free(&mesh->pdata, mesh->totpoly);
                memset(&mesh->pdata, 0, sizeof(CustomData));
                mesh->totpoly = 0;
-
+               
                CustomData_free(&mesh->ldata, mesh->totloop);
                memset(&mesh->ldata, 0, sizeof(CustomData));
                mesh->totloop = 0;
-
+               
                mesh_update_customdata_pointers(mesh);
        }
 
 #endif
-
-
-       mesh->bb= NULL;
-       mesh->edit_btmesh= NULL;
+       
+       
+       mesh->bb = NULL;
+       mesh->edit_btmesh = NULL;
        
        /* Multires data */
        mesh->mr= newdataadr(fd, mesh->mr);
@@ -3951,15 +3908,15 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
                MultiresLevel *lvl;
                
                link_list(fd, &mesh->mr->levels);
-               lvl= mesh->mr->levels.first;
+               lvl = mesh->mr->levels.first;
                
                direct_link_customdata(fd, &mesh->mr->vdata, lvl->totvert);
                direct_link_dverts(fd, lvl->totvert, CustomData_get(&mesh->mr->vdata, 0, CD_MDEFORMVERT));
                direct_link_customdata(fd, &mesh->mr->fdata, lvl->totface);
                
-               mesh->mr->edge_flags= newdataadr(fd, mesh->mr->edge_flags);
-               mesh->mr->edge_creases= newdataadr(fd, mesh->mr->edge_creases);
-
+               mesh->mr->edge_flags = newdataadr(fd, mesh->mr->edge_flags);
+               mesh->mr->edge_creases = newdataadr(fd, mesh->mr->edge_creases);
+               
                mesh->mr->verts = newdataadr(fd, mesh->mr->verts);
                
                /* If mesh has the same number of vertices as the
@@ -3974,11 +3931,11 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
                        mesh->mr->verts = MEM_dupallocN(mesh->mvert);
                }
                        
-               for (; lvl; lvl= lvl->next) {
-                       lvl->verts= newdataadr(fd, lvl->verts);
-                       lvl->faces= newdataadr(fd, lvl->faces);
-                       lvl->edges= newdataadr(fd, lvl->edges);
-                       lvl->colfaces= newdataadr(fd, lvl->colfaces);
+               for (; lvl; lvl = lvl->next) {
+                       lvl->verts = newdataadr(fd, lvl->verts);
+                       lvl->faces = newdataadr(fd, lvl->faces);
+                       lvl->edges = newdataadr(fd, lvl->edges);
+                       lvl->colfaces = newdataadr(fd, lvl->colfaces);
                }
        }
 
@@ -3990,10 +3947,10 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
        }
        
        if ((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
-               TFace *tf= mesh->tface;
+               TFace *tf = mesh->tface;
                int i;
-
-               for (i=0; i< (mesh->totface); i++, tf++) {
+               
+               for (i = 0; i < (mesh->totface); i++, tf++) {
                        SWITCH_INT(tf->col[0]);
                        SWITCH_INT(tf->col[1]);
                        SWITCH_INT(tf->col[2]);
@@ -4008,28 +3965,26 @@ static void lib_link_latt(FileData *fd, Main *main)
 {
        Lattice *lt;
        
-       lt= main->latt.first;
-       while (lt) {
+       for (lt = main->latt.first; lt; lt = lt->id.next) {
                if (lt->id.flag & LIB_NEEDLINK) {
                        if (lt->adt) lib_link_animdata(fd, &lt->id, lt->adt);
                        
-                       lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo); // XXX depreceated - old animation system
-                       lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
+                       lt->ipo = newlibadr_us(fd, lt->id.lib, lt->ipo); // XXX depreceated - old animation system
+                       lt->key = newlibadr_us(fd, lt->id.lib, lt->key);
                        
                        lt->id.flag -= LIB_NEEDLINK;
                }
-               lt= lt->id.next;
        }
 }
 
 static void direct_link_latt(FileData *fd, Lattice *lt)
 {
-       lt->def= newdataadr(fd, lt->def);
+       lt->def = newdataadr(fd, lt->def);
        
-       lt->dvert= newdataadr(fd, lt->dvert);
+       lt->dvert = newdataadr(fd, lt->dvert);
        direct_link_dverts(fd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
        
-       lt->editlatt= NULL;
+       lt->editlatt = NULL;
        
        lt->adt = newdataadr(fd, lt->adt);
        direct_link_animdata(fd, lt->adt);
@@ -4062,29 +4017,28 @@ static void lib_link_object(FileData *fd, Main *main)
        bActuator *act;
        void *poin;
        int warn=0, a;
-
-       ob= main->object.first;
-       while (ob) {
+       
+       for (ob = main->object.first; ob; ob = ob->id.next) {
                if (ob->id.flag & LIB_NEEDLINK) {
                        if (ob->id.properties) IDP_LibLinkProperty(ob->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
                        if (ob->adt) lib_link_animdata(fd, &ob->id, ob->adt);
                        
 // XXX depreceated - old animation system <<<                  
-                       ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
+                       ob->ipo = newlibadr_us(fd, ob->id.lib, ob->ipo);
                        ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
 // >>> XXX depreceated - old animation system
 
-                       ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
-                       ob->track= newlibadr(fd, ob->id.lib, ob->track);
-                       ob->poselib= newlibadr_us(fd, ob->id.lib, ob->poselib);
-                       ob->dup_group= newlibadr_us(fd, ob->id.lib, ob->dup_group);
+                       ob->parent = newlibadr(fd, ob->id.lib, ob->parent);
+                       ob->track = newlibadr(fd, ob->id.lib, ob->track);
+                       ob->poselib = newlibadr_us(fd, ob->id.lib, ob->poselib);
+                       ob->dup_group = newlibadr_us(fd, ob->id.lib, ob->dup_group);
                        
-                       ob->proxy= newlibadr_us(fd, ob->id.lib, ob->proxy);
+                       ob->proxy = newlibadr_us(fd, ob->id.lib, ob->proxy);
                        if (ob->proxy) {
                                /* paranoia check, actually a proxy_from pointer should never be written... */
-                               if (ob->proxy->id.lib==NULL) {
-                                       ob->proxy->proxy_from= NULL;
-                                       ob->proxy= NULL;
+                               if (ob->proxy->id.lib == NULL) {
+                                       ob->proxy->proxy_from = NULL;
+                                       ob->proxy = NULL;
                                        
                                        if (ob->id.lib)
                                                printf("Proxy lost from  object %s lib %s\n", ob->id.name+2, ob->id.lib->name);
@@ -4093,47 +4047,48 @@ static void lib_link_object(FileData *fd, Main *main)
                                }
                                else {
                                        /* this triggers object_update to always use a copy */
-                                       ob->proxy->proxy_from= ob;
+                                       ob->proxy->proxy_from = ob;
                                        /* force proxy updates after load/undo, a bit weak */
-                                       ob->recalc= ob->proxy->recalc= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
+                                       ob->recalc = ob->proxy->recalc = (OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
                                }
                        }
-                       ob->proxy_group= newlibadr(fd, ob->id.lib, ob->proxy_group);
+                       ob->proxy_group = newlibadr(fd, ob->id.lib, ob->proxy_group);
+                       
+                       poin = ob->data;
+                       ob->data = newlibadr_us(fd, ob->id.lib, ob->data);
                        
-                       poin= ob->data;
-                       ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
-
                        if (ob->data==NULL && poin!=NULL) {
                                if (ob->id.lib)
                                        printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
                                else
                                        printf("Object %s lost data.\n", ob->id.name+2);
-
-                               ob->type= OB_EMPTY;
-                               warn= 1;
-
+                               
+                               ob->type = OB_EMPTY;
+                               warn = 1;
+                               
                                if (ob->pose) {
                                        BKE_pose_free(ob->pose);
                                        ob->pose= NULL;
                                        ob->mode &= ~OB_MODE_POSE;
                                }
                        }
-                       for (a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
+                       for (a=0; a < ob->totcol; a++) 
+                               ob->mat[a] = newlibadr_us(fd, ob->id.lib, ob->mat[a]);
                        
                        /* When the object is local and the data is library its possible
                         * the material list size gets out of sync. [#22663] */
                        if (ob->data && ob->id.lib != ((ID *)ob->data)->lib) {
-                               short *totcol_data= give_totcolp(ob);
+                               short *totcol_data = give_totcolp(ob);
                                /* Only expand so as not to loose any object materials that might be set. */
-                               if (totcol_data && *totcol_data > ob->totcol) {
+                               if (totcol_data && (*totcol_data > ob->totcol)) {
                                        /* printf("'%s' %d -> %d\n", ob->id.name, ob->totcol, *totcol_data); */
                                        resize_object_material(ob, *totcol_data);
                                }
                        }
-
-                       ob->gpd= newlibadr_us(fd, ob->id.lib, ob->gpd);
-                       ob->duplilist= NULL;
-
+                       
+                       ob->gpd = newlibadr_us(fd, ob->id.lib, ob->gpd);
+                       ob->duplilist = NULL;
+                       
                        ob->id.flag -= LIB_NEEDLINK;
                        /* if id.us==0 a new base will be created later on */
                        
@@ -4145,75 +4100,69 @@ static void lib_link_object(FileData *fd, Main *main)
                        lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
                        lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
 // >>> XXX depreceated - old animation system
-
-                       for (paf= ob->effect.first; paf; paf= paf->next) {
-                               if (paf->type==EFF_PARTICLE) {
-                                       paf->group= newlibadr_us(fd, ob->id.lib, paf->group);
+                       
+                       for (paf = ob->effect.first; paf; paf = paf->next) {
+                               if (paf->type == EFF_PARTICLE) {
+                                       paf->group = newlibadr_us(fd, ob->id.lib, paf->group);
                                }
                        }                               
-
-                       sens= ob->sensors.first;
-                       while (sens) {
-                               for (a=0; a<sens->totlinks; a++)
-                                       sens->links[a]= newglobadr(fd, sens->links[a]);
-
-                               if (sens->type==SENS_TOUCH) {
-                                       bTouchSensor *ts= sens->data;
-                                       ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
+                       
+                       for (sens = ob->sensors.first; sens; sens = sens->next) {
+                               for (a = 0; a < sens->totlinks; a++)
+                                       sens->links[a] = newglobadr(fd, sens->links[a]);
+                               
+                               if (sens->type == SENS_TOUCH) {
+                                       bTouchSensor *ts = sens->data;
+                                       ts->ma = newlibadr(fd, ob->id.lib, ts->ma);
                                }
-                               else if (sens->type==SENS_MESSAGE) {
-                                       bMessageSensor *ms= sens->data;
-                                       ms->fromObject=
+                               else if (sens->type == SENS_MESSAGE) {
+                                       bMessageSensor *ms = sens->data;
+                                       ms->fromObject =
                                                newlibadr(fd, ob->id.lib, ms->fromObject);
                                }
-                               sens= sens->next;
                        }
-
-                       cont= ob->controllers.first;
-                       while (cont) {
-                               for (a=0; a<cont->totlinks; a++)
-                                       cont->links[a]= newglobadr(fd, cont->links[a]);
-
-                               if (cont->type==CONT_PYTHON) {
-                                       bPythonCont *pc= cont->data;
-                                       pc->text= newlibadr(fd, ob->id.lib, pc->text);
+                       
+                       for (cont = ob->controllers.first; cont; cont = cont->next) {
+                               for (a=0; a < cont->totlinks; a++)
+                                       cont->links[a] = newglobadr(fd, cont->links[a]);
+                               
+                               if (cont->type == CONT_PYTHON) {
+                                       bPythonCont *pc = cont->data;
+                                       pc->text = newlibadr(fd, ob->id.lib, pc->text);
                                }
-                               cont->slinks= NULL;
-                               cont->totslinks= 0;
-
-                               cont= cont->next;
+                               cont->slinks = NULL;
+                               cont->totslinks = 0;
                        }
-
-                       act= ob->actuators.first;
-                       while (act) {
-                               if (act->type==ACT_SOUND) {
-                                       bSoundActuator *sa= act->data;
+                       
+                       for (act = ob->actuators.first; act; act = act->next) {
+                               if (act->type == ACT_SOUND) {
+                                       bSoundActuator *sa = act->data;
                                        sa->sound= newlibadr_us(fd, ob->id.lib, sa->sound);
                                }
-                               else if (act->type==ACT_GAME) {
+                               else if (act->type == ACT_GAME) {
                                        /* bGameActuator *ga= act->data; */
                                }
-                               else if (act->type==ACT_CAMERA) {
-                                       bCameraActuator *ca= act->data;
+                               else if (act->type == ACT_CAMERA) {
+                                       bCameraActuator *ca = act->data;
                                        ca->ob= newlibadr(fd, ob->id.lib, ca->ob);
                                }
                                        /* leave this one, it's obsolete but necessary to read for conversion */
-                               else if (act->type==ACT_ADD_OBJECT) {
-                                       bAddObjectActuator *eoa= act->data;
+                               else if (act->type == ACT_ADD_OBJECT) {
+                                       bAddObjectActuator *eoa = act->data;
                                        if (eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
                                }
-                               else if (act->type==ACT_OBJECT) {
-                                       bObjectActuator *oa= act->data;
-                                       if (oa==NULL) {
+                               else if (act->type == ACT_OBJECT) {
+                                       bObjectActuator *oa = act->data;
+                                       if (oa == NULL) {
                                                init_actuator(act);
                                        }
                                        else {
-                                               oa->reference= newlibadr(fd, ob->id.lib, oa->reference);
+                                               oa->reference = newlibadr(fd, ob->id.lib, oa->reference);
                                        }
                                }
-                               else if (act->type==ACT_EDIT_OBJECT) {
-                                       bEditObjectActuator *eoa= act->data;
-                                       if (eoa==NULL) {
+                               else if (act->type == ACT_EDIT_OBJECT) {
+                                       bEditObjectActuator *eoa = act->data;
+                                       if (eoa == NULL) {
                                                init_actuator(act);
                                        }
                                        else {
@@ -4221,49 +4170,48 @@ static void lib_link_object(FileData *fd, Main *main)
                                                eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
                                        }
                                }
-                               else if (act->type==ACT_SCENE) {
-                                       bSceneActuator *sa= act->data;
+                               else if (act->type == ACT_SCENE) {
+                                       bSceneActuator *sa = act->data;
                                        sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
                                        sa->scene= newlibadr(fd, ob->id.lib, sa->scene);
                                }
-                               else if (act->type==ACT_ACTION) {
-                                       bActionActuator *aa= act->data;
+                               else if (act->type == ACT_ACTION) {
+                                       bActionActuator *aa = act->data;
                                        aa->act= newlibadr(fd, ob->id.lib, aa->act);
                                }
-                               else if (act->type==ACT_SHAPEACTION) {
-                                       bActionActuator *aa= act->data;
+                               else if (act->type == ACT_SHAPEACTION) {
+                                       bActionActuator *aa = act->data;
                                        aa->act= newlibadr(fd, ob->id.lib, aa->act);
                                }
-                               else if (act->type==ACT_PROPERTY) {
-                                       bPropertyActuator *pa= act->data;
+                               else if (act->type == ACT_PROPERTY) {
+                                       bPropertyActuator *pa = act->data;
                                        pa->ob= newlibadr(fd, ob->id.lib, pa->ob);
                                }
-                               else if (act->type==ACT_MESSAGE) {
-                                       bMessageActuator *ma= act->data;
+                               else if (act->type == ACT_MESSAGE) {
+                                       bMessageActuator *ma = act->data;
                                        ma->toObject= newlibadr(fd, ob->id.lib, ma->toObject);
                                }
-                               else if (act->type==ACT_2DFILTER) {
+                               else if (act->type == ACT_2DFILTER) {
                                        bTwoDFilterActuator *_2dfa = act->data; 
                                        _2dfa->text= newlibadr(fd, ob->id.lib, _2dfa->text);
                                }
-                               else if (act->type==ACT_PARENT) {
+                               else if (act->type == ACT_PARENT) {
                                        bParentActuator *parenta = act->data; 
                                        parenta->ob = newlibadr(fd, ob->id.lib, parenta->ob);
                                }
-                               else if (act->type==ACT_STATE) {
+                               else if (act->type == ACT_STATE) {
                                        /* bStateActuator *statea = act->data; */
                                }
-                               else if (act->type==ACT_ARMATURE) {
+                               else if (act->type == ACT_ARMATURE) {
                                        bArmatureActuator *arma= act->data;
                                        arma->target= newlibadr(fd, ob->id.lib, arma->target);
                                        arma->subtarget= newlibadr(fd, ob->id.lib, arma->subtarget);
                                }
-                               else if (act->type==ACT_STEERING) {
+                               else if (act->type == ACT_STEERING) {
                                        bSteeringActuator *steeringa = act->data; 
                                        steeringa->target = newlibadr(fd, ob->id.lib, steeringa->target);
                                        steeringa->navmesh = newlibadr(fd, ob->id.lib, steeringa->navmesh);
                                }
-                               act= act->next;
                        }
                        
                        {
@@ -4272,28 +4220,27 @@ static void lib_link_object(FileData *fd, Main *main)
                                if (fluidmd && fluidmd->fss)
                                        fluidmd->fss->ipo = newlibadr_us(fd, ob->id.lib, fluidmd->fss->ipo);
                        }
-
+                       
                        {
                                SmokeModifierData *smd = (SmokeModifierData *)modifiers_findByType(ob, eModifierType_Smoke);
                                
-                               if (smd && smd->type == MOD_SMOKE_TYPE_DOMAIN && smd->domain) {
+                               if (smd && (smd->type == MOD_SMOKE_TYPE_DOMAIN) && smd->domain) {
                                        smd->domain->flags |= MOD_SMOKE_FILE_LOAD; /* flag for refreshing the simulation after loading */
                                }
                        }
-
+                       
                        /* texture field */
                        if (ob->pd)
                                lib_link_partdeflect(fd, &ob->id, ob->pd);
-
+                       
                        if (ob->soft)
                                ob->soft->effector_weights->group = newlibadr(fd, ob->id.lib, ob->soft->effector_weights->group);
-
+                       
                        lib_link_particlesystems(fd, ob, &ob->id, &ob->particlesystem);
                        lib_link_modifiers(fd, ob);
                }
-               ob= ob->id.next;
        }
-
+       
        if (warn) {
                BKE_report(fd->reports, RPT_WARNING, "Warning in console");
        }
@@ -4310,13 +4257,13 @@ static void direct_link_pose(FileData *fd, bPose *pose)
        link_list(fd, &pose->chanbase);
        link_list(fd, &pose->agroups);
 
-       pose->chanhash= NULL;
+       pose->chanhash = NULL;
 
        for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
-               pchan->bone= NULL;
-               pchan->parent= newdataadr(fd, pchan->parent);
-               pchan->child= newdataadr(fd, pchan->child);
-               pchan->custom_tx= newdataadr(fd, pchan->custom_tx);
+               pchan->bone = NULL;
+               pchan->parent = newdataadr(fd, pchan->parent);
+               pchan->child = newdataadr(fd, pchan->child);
+               pchan->custom_tx = newdataadr(fd, pchan->custom_tx);
                
                direct_link_constraints(fd, &pchan->constraints);
                
@@ -4324,100 +4271,100 @@ static void direct_link_pose(FileData *fd, bPose *pose)
                if (pchan->prop)
                        IDP_DirectLinkProperty(pchan->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
                
-               pchan->mpath= newdataadr(fd, pchan->mpath);
+               pchan->mpath = newdataadr(fd, pchan->mpath);
                if (pchan->mpath)
                        direct_link_motionpath(fd, pchan->mpath);
                
-               pchan->iktree.first= pchan->iktree.last= NULL;
-               pchan->siktree.first= pchan->siktree.last= NULL;
+               pchan->iktree.first = pchan->iktree.last = NULL;
+               pchan->siktree.first = pchan->siktree.last = NULL;
                
                /* in case this value changes in future, clamp else we get undefined behavior */
                CLAMP(pchan->rotmode, ROT_MODE_MIN, ROT_MODE_MAX);
        }
        pose->ikdata = NULL;
        if (pose->ikparam != NULL) {
-               pose->ikparam= newdataadr(fd, pose->ikparam);
+               pose->ikparam = newdataadr(fd, pose->ikparam);
        }
 }
 
 static void direct_link_modifiers(FileData *fd, ListBase *lb)
 {
        ModifierData *md;
-
+       
        link_list(fd, lb);
-
+       
        for (md=lb->first; md; md=md->next) {
                md->error = NULL;
                md->scene = NULL;
                
                /* if modifiers disappear, or for upward compatibility */
-               if (NULL==modifierType_getInfo(md->type))
-                       md->type= eModifierType_None;
+               if (NULL == modifierType_getInfo(md->type))
+                       md->type = eModifierType_None;
+                       
+               if (md->type == eModifierType_Subsurf) {
+                       SubsurfModifierData *smd = (SubsurfModifierData *)md;
                        
-               if (md->type==eModifierType_Subsurf) {
-                       SubsurfModifierData *smd = (SubsurfModifierData*) md;
-
                        smd->emCache = smd->mCache = NULL;
                }
-               else if (md->type==eModifierType_Armature) {
-                       ArmatureModifierData *amd = (ArmatureModifierData*) md;
+               else if (md->type == eModifierType_Armature) {
+                       ArmatureModifierData *amd = (ArmatureModifierData *)md;
                        
-                       amd->prevCos= NULL;
+                       amd->prevCos = NULL;
                }
-               else if (md->type==eModifierType_Cloth) {
-                       ClothModifierData *clmd = (ClothModifierData*) md;
+               else if (md->type == eModifierType_Cloth) {
+                       ClothModifierData *clmd = (ClothModifierData *)md;
                        
                        clmd->clothObject = NULL;
                        
                        clmd->sim_parms= newdataadr(fd, clmd->sim_parms);
                        clmd->coll_parms= newdataadr(fd, clmd->coll_parms);
-
+                       
                        direct_link_pointcache_list(fd, &clmd->ptcaches, &clmd->point_cache, 0);
                        
                        if (clmd->sim_parms) {
                                if (clmd->sim_parms->presets > 10)
                                        clmd->sim_parms->presets = 0;
-
+                               
                                clmd->sim_parms->reset = 0;
-
+                               
                                clmd->sim_parms->effector_weights = newdataadr(fd, clmd->sim_parms->effector_weights);
-
+                               
                                if (!clmd->sim_parms->effector_weights) {
                                        clmd->sim_parms->effector_weights = BKE_add_effector_weights(NULL);
                                }
                        }
                }
-               else if (md->type==eModifierType_Fluidsim) {
-                       FluidsimModifierData *fluidmd = (FluidsimModifierData*) md;
+               else if (md->type == eModifierType_Fluidsim) {
+                       FluidsimModifierData *fluidmd = (FluidsimModifierData *)md;
                        
-                       fluidmd->fss= newdataadr(fd, fluidmd->fss);
+                       fluidmd->fss = newdataadr(fd, fluidmd->fss);
                        if (fluidmd->fss) {
-                               fluidmd->fss->fmd= fluidmd;
+                               fluidmd->fss->fmd = fluidmd;
                                fluidmd->fss->meshVelocities = NULL;
                        }
                }
-               else if (md->type==eModifierType_Smoke) {
-                       SmokeModifierData *smd = (SmokeModifierData*) md;
-
-                       if (smd->type==MOD_SMOKE_TYPE_DOMAIN) {
+               else if (md->type == eModifierType_Smoke) {
+                       SmokeModifierData *smd = (SmokeModifierData *)md;
+                       
+                       if (smd->type == MOD_SMOKE_TYPE_DOMAIN) {
                                smd->flow = NULL;
                                smd->coll = NULL;
                                smd->domain = newdataadr(fd, smd->domain);
                                smd->domain->smd = smd;
-
+                               
                                smd->domain->fluid = NULL;
                                smd->domain->wt = NULL;
                                smd->domain->shadow = NULL;
                                smd->domain->tex = NULL;
                                smd->domain->tex_shadow = NULL;
                                smd->domain->tex_wt = NULL;
-
+                               
                                smd->domain->effector_weights = newdataadr(fd, smd->domain->effector_weights);
                                if (!smd->domain->effector_weights)
                                        smd->domain->effector_weights = BKE_add_effector_weights(NULL);
-
+                               
                                direct_link_pointcache_list(fd, &(smd->domain->ptcaches[0]), &(smd->domain->point_cache[0]), 1);
-
+                               
                                /* Smoke uses only one cache from now on, so store pointer convert */
                                if (smd->domain->ptcaches[1].first || smd->domain->point_cache[1]) {
                                        if (smd->domain->point_cache[1]) {
@@ -4433,14 +4380,14 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                                        smd->domain->point_cache[1] = NULL;
                                }
                        }
-                       else if (smd->type==MOD_SMOKE_TYPE_FLOW) {
+                       else if (smd->type == MOD_SMOKE_TYPE_FLOW) {
                                smd->domain = NULL;
                                smd->coll = NULL;
                                smd->flow = newdataadr(fd, smd->flow);
                                smd->flow->smd = smd;
                                smd->flow->psys = newdataadr(fd, smd->flow->psys);
                        }
-                       else if (smd->type==MOD_SMOKE_TYPE_COLL) {
+                       else if (smd->type == MOD_SMOKE_TYPE_COLL) {
                                smd->flow = NULL;
                                smd->domain = NULL;
                                smd->coll = newdataadr(fd, smd->coll);
@@ -4450,27 +4397,26 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                                }
                                else
                                        smd->type = 0;
-
                        }
                }
-               else if (md->type==eModifierType_DynamicPaint) {
-                       DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
-
+               else if (md->type == eModifierType_DynamicPaint) {
+                       DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
+                       
                        if (pmd->canvas) {
                                pmd->canvas = newdataadr(fd, pmd->canvas);
                                pmd->canvas->pmd = pmd;
                                pmd->canvas->dm = NULL;
                                pmd->canvas->flags &= ~MOD_DPAINT_BAKING; /* just in case */
-
+                               
                                if (pmd->canvas->surfaces.first) {
                                        DynamicPaintSurface *surface;
                                        link_list(fd, &pmd->canvas->surfaces);
-
+                                       
                                        for (surface=pmd->canvas->surfaces.first; surface; surface=surface->next) {
                                                surface->canvas = pmd->canvas;
                                                surface->data = NULL;
                                                direct_link_pointcache_list(fd, &(surface->ptcaches), &(surface->pointcache), 1);
-
+                                               
                                                if (!(surface->effector_weights = newdataadr(fd, surface->effector_weights)))
                                                        surface->effector_weights = BKE_add_effector_weights(NULL);
                                        }
@@ -4485,9 +4431,8 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                                pmd->brush->dm = NULL;
                        }
                }
-               else if (md->type==eModifierType_Collision) {
-                       
-                       CollisionModifierData *collmd = (CollisionModifierData*) md;
+               else if (md->type == eModifierType_Collision) {
+                       CollisionModifierData *collmd = (CollisionModifierData *)md;
                        /*
                        // TODO: CollisionModifier should use pointcache 
                        // + have proper reset events before enabling this
@@ -4511,55 +4456,55 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                        collmd->mfaces = NULL;
                        
                }
-               else if (md->type==eModifierType_Surface) {
-                       SurfaceModifierData *surmd = (SurfaceModifierData*) md;
-
+               else if (md->type == eModifierType_Surface) {
+                       SurfaceModifierData *surmd = (SurfaceModifierData *)md;
+                       
                        surmd->dm = NULL;
                        surmd->bvhtree = NULL;
                        surmd->x = NULL;
                        surmd->v = NULL;
                        surmd->numverts = 0;
                }
-               else if (md->type==eModifierType_Hook) {
-                       HookModifierData *hmd = (HookModifierData*) md;
-
-                       hmd->indexar= newdataadr(fd, hmd->indexar);
+               else if (md->type == eModifierType_Hook) {
+                       HookModifierData *hmd = (HookModifierData *)md;
+                       
+                       hmd->indexar = newdataadr(fd, hmd->indexar);
                        if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
                                int a;
-                               for (a=0; a<hmd->totindex; a++) {
+                               for (a = 0; a < hmd->totindex; a++) {
                                        SWITCH_INT(hmd->indexar[a]);
                                }
                        }
                }
-               else if (md->type==eModifierType_ParticleSystem) {
-                       ParticleSystemModifierData *psmd = (ParticleSystemModifierData*) md;
-
+               else if (md->type == eModifierType_ParticleSystem) {
+                       ParticleSystemModifierData *psmd = (ParticleSystemModifierData *)md;
+                       
                        psmd->dm= NULL;
                        psmd->psys= newdataadr(fd, psmd->psys);
                        psmd->flag &= ~eParticleSystemFlag_psys_updated;
                        psmd->flag |= eParticleSystemFlag_file_loaded;
                }
-               else if (md->type==eModifierType_Explode) {
-                       ExplodeModifierData *psmd = (ExplodeModifierData*) md;
-
-                       psmd->facepa=NULL;
+               else if (md->type == eModifierType_Explode) {
+                       ExplodeModifierData *psmd = (ExplodeModifierData *)md;
+                       
+                       psmd->facepa = NULL;
                }
-               else if (md->type==eModifierType_MeshDeform) {
-                       MeshDeformModifierData *mmd = (MeshDeformModifierData*) md;
-
-                       mmd->bindinfluences= newdataadr(fd, mmd->bindinfluences);
-                       mmd->bindoffsets= newdataadr(fd, mmd->bindoffsets);
-                       mmd->bindcagecos= newdataadr(fd, mmd->bindcagecos);
-                       mmd->dyngrid= newdataadr(fd, mmd->dyngrid);
-                       mmd->dyninfluences= newdataadr(fd, mmd->dyninfluences);
-                       mmd->dynverts= newdataadr(fd, mmd->dynverts);
-
-                       mmd->bindweights= newdataadr(fd, mmd->bindweights);
-                       mmd->bindcos= newdataadr(fd, mmd->bindcos);
-
+               else if (md->type == eModifierType_MeshDeform) {
+                       MeshDeformModifierData *mmd = (MeshDeformModifierData *)md;
+                       
+                       mmd->bindinfluences = newdataadr(fd, mmd->bindinfluences);
+                       mmd->bindoffsets = newdataadr(fd, mmd->bindoffsets);
+                       mmd->bindcagecos = newdataadr(fd, mmd->bindcagecos);
+                       mmd->dyngrid = newdataadr(fd, mmd->dyngrid);
+                       mmd->dyninfluences = newdataadr(fd, mmd->dyninfluences);
+                       mmd->dynverts = newdataadr(fd, mmd->dynverts);
+                       
+                       mmd->bindweights = newdataadr(fd, mmd->bindweights);
+                       mmd->bindcos = newdataadr(fd, mmd->bindcos);
+                       
                        if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
                                int a;
-
+                               
                                if (mmd->bindoffsets)
                                        for (a=0; a<mmd->totvert+1; a++)
                                                SWITCH_INT(mmd->bindoffsets[a]);
@@ -4569,7 +4514,7 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                                if (mmd->dynverts)
                                        for (a=0; a<mmd->totvert; a++)
                                                SWITCH_INT(mmd->dynverts[a]);
-
+                               
                                if (mmd->bindweights)
                                        for (a=0; a<mmd->totcagevert*mmd->totvert; a++)
                                                SWITCH_INT(mmd->bindweights[a]);
@@ -4578,22 +4523,22 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                                                SWITCH_INT(mmd->bindcos[a]);
                        }
                }
-               else if (md->type==eModifierType_Ocean) {
-                       OceanModifierData *omd = (OceanModifierData*) md;
+               else if (md->type == eModifierType_Ocean) {
+                       OceanModifierData *omd = (OceanModifierData *)md;
                        omd->oceancache = NULL;
                        omd->ocean = NULL;
                        omd->refresh = (MOD_OCEAN_REFRESH_ADD|MOD_OCEAN_REFRESH_RESET|MOD_OCEAN_REFRESH_SIM);
                }
-               else if (md->type==eModifierType_Warp) {
-                       WarpModifierData *tmd = (WarpModifierData *) md;
-
+               else if (md->type == eModifierType_Warp) {
+                       WarpModifierData *tmd = (WarpModifierData *)md;
+                       
                        tmd->curfalloff= newdataadr(fd, tmd->curfalloff);
                        if (tmd->curfalloff)
                                direct_link_curvemapping(fd, tmd->curfalloff);
                }
-               else if (md->type==eModifierType_WeightVGEdit) {
-                       WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
-
+               else if (md->type == eModifierType_WeightVGEdit) {
+                       WeightVGEditModifierData *wmd = (WeightVGEditModifierData *)md;
+                       
                        wmd->cmap_curve = newdataadr(fd, wmd->cmap_curve);
                        if (wmd->cmap_curve)
                                direct_link_curvemapping(fd, wmd->cmap_curve);
@@ -4611,30 +4556,30 @@ static void direct_link_object(FileData *fd, Object *ob)
        
        /* weak weak... this was only meant as draw flag, now is used in give_base_to_objects too */
        ob->flag &= ~OB_FROMGROUP;
-
+       
        /* loading saved files with editmode enabled works, but for undo we like
         * to stay in object mode during undo presses so keep editmode disabled */
        if (fd->memfile)
-               ob->mode &= ~(OB_MODE_EDIT|OB_MODE_PARTICLE_EDIT);
+               ob->mode &= ~(OB_MODE_EDIT | OB_MODE_PARTICLE_EDIT);
        
-       ob->disp.first=ob->disp.last= NULL;
+       ob->disp.first = ob->disp.last = NULL;
        
-       ob->adt= newdataadr(fd, ob->adt);
+       ob->adt = newdataadr(fd, ob->adt);
        direct_link_animdata(fd, ob->adt);
        
-       ob->pose= newdataadr(fd, ob->pose);
+       ob->pose = newdataadr(fd, ob->pose);
        direct_link_pose(fd, ob->pose);
        
-       ob->mpath= newdataadr(fd, ob->mpath);
+       ob->mpath = newdataadr(fd, ob->mpath);
        if (ob->mpath)
                direct_link_motionpath(fd, ob->mpath);
-
+       
        link_list(fd, &ob->defbase);
 // XXX depreceated - old animation system <<<
        direct_link_nlastrips(fd, &ob->nlastrips);
        link_list(fd, &ob->constraintChannels);
 // >>> XXX depreceated - old animation system 
-
+       
        ob->mat= newdataadr(fd, ob->mat);
        test_pointer_array(fd, (void **)&ob->mat);
        ob->matbits= newdataadr(fd, ob->matbits);
@@ -4645,14 +4590,14 @@ static void direct_link_object(FileData *fd, Object *ob)
        link_list(fd, &ob->effect);
        paf= ob->effect.first;
        while (paf) {
-               if (paf->type==EFF_PARTICLE) {
-                       paf->keys= NULL;
+               if (paf->type == EFF_PARTICLE) {
+                       paf->keys = NULL;
                }
-               if (paf->type==EFF_WAVE) {
+               if (paf->type == EFF_WAVE) {
                        WaveEff *wav = (WaveEff*) paf;
                        PartEff *next = paf->next;
                        WaveModifierData *wmd = (WaveModifierData*) modifier_new(eModifierType_Wave);
-
+                       
                        wmd->damp = wav->damp;
                        wmd->flag = wav->flag;
                        wmd->height = wav->height;
@@ -4663,83 +4608,80 @@ static void direct_link_object(FileData *fd, Object *ob)
                        wmd->starty = wav->startx;
                        wmd->timeoffs = wav->timeoffs;
                        wmd->width = wav->width;
-
+                       
                        BLI_addtail(&ob->modifiers, wmd);
-
+                       
                        BLI_remlink(&ob->effect, paf);
                        MEM_freeN(paf);
-
+                       
                        paf = next;
                        continue;
                }
-               if (paf->type==EFF_BUILD) {
+               if (paf->type == EFF_BUILD) {
                        BuildEff *baf = (BuildEff*) paf;
                        PartEff *next = paf->next;
                        BuildModifierData *bmd = (BuildModifierData*) modifier_new(eModifierType_Build);
-
+                       
                        bmd->start = baf->sfra;
                        bmd->length = baf->len;
                        bmd->randomize = 0;
                        bmd->seed = 1;
-
+                       
                        BLI_addtail(&ob->modifiers, bmd);
-
+                       
                        BLI_remlink(&ob->effect, paf);
                        MEM_freeN(paf);
-
+                       
                        paf = next;
                        continue;
                }
-               paf= paf->next;
+               paf = paf->next;
        }
-
+       
        ob->pd= newdataadr(fd, ob->pd);
        direct_link_partdeflect(ob->pd);
        ob->soft= newdataadr(fd, ob->soft);
        if (ob->soft) {
-               SoftBody *sb= ob->soft;         
+               SoftBody *sb = ob->soft;                
                
-               sb->bpoint= NULL;       // init pointers so it gets rebuilt nicely
-               sb->bspring= NULL;
-               sb->scratch= NULL;
+               sb->bpoint = NULL;      // init pointers so it gets rebuilt nicely
+               sb->bspring = NULL;
+               sb->scratch = NULL;
                /* although not used anymore */
                /* still have to be loaded to be compatible with old files */
-               sb->keys= newdataadr(fd, sb->keys);
+               sb->keys = newdataadr(fd, sb->keys);
                test_pointer_array(fd, (void **)&sb->keys);
                if (sb->keys) {
                        int a;
-                       for (a=0; a<sb->totkey; a++) {
-                               sb->keys[a]= newdataadr(fd, sb->keys[a]);
+                       for (a = 0; a < sb->totkey; a++) {
+                               sb->keys[a] = newdataadr(fd, sb->keys[a]);
                        }
                }
-
+               
                sb->effector_weights = newdataadr(fd, sb->effector_weights);
                if (!sb->effector_weights)
                        sb->effector_weights = BKE_add_effector_weights(NULL);
-
+               
                direct_link_pointcache_list(fd, &sb->ptcaches, &sb->pointcache, 0);
        }
-       ob->bsoft= newdataadr(fd, ob->bsoft);
+       ob->bsoft = newdataadr(fd, ob->bsoft);
        ob->fluidsimSettings= newdataadr(fd, ob->fluidsimSettings); /* NT */
 
        link_list(fd, &ob->particlesystem);
        direct_link_particlesystems(fd, &ob->particlesystem);
        
        link_list(fd, &ob->prop);
-       prop= ob->prop.first;
-       while (prop) {
-               prop->poin= newdataadr(fd, prop->poin);
-               if (prop->poin==NULL) prop->poin= &prop->data;
-               prop= prop->next;
+       for (prop = ob->prop.first; prop; prop = prop->next) {
+               prop->poin = newdataadr(fd, prop->poin);
+               if (prop->poin == NULL) 
+                       prop->poin = &prop->data;
        }
 
        link_list(fd, &ob->sensors);
-       sens= ob->sensors.first;
-       while (sens) {
-               sens->data= newdataadr(fd, sens->data);
-               sens->links= newdataadr(fd, sens->links);
+       for (sens = ob->sensors.first; sens; sens = sens->next) {
+               sens->data = newdataadr(fd, sens->data);
+               sens->links = newdataadr(fd, sens->links);
                test_pointer_array(fd, (void **)&sens->links);
-               sens= sens->next;
        }
 
        direct_link_constraints(fd, &ob->constraints);
@@ -4752,40 +4694,36 @@ static void direct_link_object(FileData *fd, Object *ob)
        else if (!ob->state) {
                ob->state = 1;
        }
-       cont= ob->controllers.first;
-       while (cont) {
-               cont->data= newdataadr(fd, cont->data);
-               cont->links= newdataadr(fd, cont->links);
+       for (cont = ob->controllers.first; cont; cont = cont->next) {
+               cont->data = newdataadr(fd, cont->data);
+               cont->links = newdataadr(fd, cont->links);
                test_pointer_array(fd, (void **)&cont->links);
                if (cont->state_mask == 0)
                        cont->state_mask = 1;
-               cont= cont->next;
        }
 
        link_glob_list(fd, &ob->actuators);
-       act= ob->actuators.first;
-       while (act) {
-               act->data= newdataadr(fd, act->data);
-               act= act->next;
+       for (act = ob->actuators.first; act; act = act->next) {
+               act->data = newdataadr(fd, act->data);
        }
 
        link_list(fd, &ob->hooks);
        while (ob->hooks.first) {
                ObHook *hook = ob->hooks.first;
-               HookModifierData *hmd = (HookModifierData*) modifier_new(eModifierType_Hook);
-
+               HookModifierData *hmd = (HookModifierData *)modifier_new(eModifierType_Hook);
+               
                hook->indexar= newdataadr(fd, hook->indexar);
                if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
                        int a;
-                       for (a=0; a<hook->totindex; a++) {
+                       for (a = 0; a < hook->totindex; a++) {
                                SWITCH_INT(hook->indexar[a]);
                        }
                }
-
-                       /* Do conversion here because if we have loaded
-                        * a hook we need to make sure it gets converted
-                        * and freed, regardless of version.
-                        */
+               
+               /* Do conversion here because if we have loaded
+                * a hook we need to make sure it gets converted
+                * and freed, regardless of version.
+                */
                copy_v3_v3(hmd->cent, hook->cent);
                hmd->falloff = hook->falloff;
                hmd->force = hook->force;
@@ -4793,27 +4731,27 @@ static void direct_link_object(FileData *fd, Object *ob)
                hmd->object = hook->parent;
                memcpy(hmd->parentinv, hook->parentinv, sizeof(hmd->parentinv));
                hmd->totindex = hook->totindex;
-
+               
                BLI_addhead(&ob->modifiers, hmd);
                BLI_remlink(&ob->hooks, hook);
                
                modifier_unique_name(&ob->modifiers, (ModifierData*)hmd);
-
+               
                MEM_freeN(hook);
        }
        
-       ob->customdata_mask= 0;
-       ob->bb= NULL;
-       ob->derivedDeform= NULL;
-       ob->derivedFinal= NULL;
-       ob->gpulamp.first= ob->gpulamp.last= NULL;
+       ob->customdata_mask = 0;
+       ob->bb = NULL;
+       ob->derivedDeform = NULL;
+       ob->derivedFinal = NULL;
+       ob->gpulamp.first= ob->gpulamp.last = NULL;
        link_list(fd, &ob->pc_ids);
 
        /* in case this value changes in future, clamp else we get undefined behavior */
        CLAMP(ob->rotmode, ROT_MODE_MIN, ROT_MODE_MAX);
 
        if (ob->sculpt) {
-               ob->sculpt= MEM_callocN(sizeof(SculptSession), "reload sculpt session");
+               ob->sculpt = MEM_callocN(sizeof(SculptSession), "reload sculpt session");
        }
 }
 
@@ -4824,16 +4762,17 @@ static void composite_patch(bNodeTree *ntree, Scene *scene)
 {
        bNode *node;
        
-       for (node= ntree->nodes.first; node; node= node->next)
+       for (node= ntree->nodes.first; node; node= node->next) {
                if (node->id==NULL && ELEM4(node->type, CMP_NODE_R_LAYERS, CMP_NODE_COMPOSITE, CMP_NODE_DEFOCUS, CMP_NODE_OUTPUT_FILE))
-                       node->id= &scene->id;
+                       node->id = &scene->id;
+       }
 }
 
 static void link_paint(FileData *fd, Scene *sce, Paint *p)
 {
        if (p) {
-               p->brush= newlibadr_us(fd, sce->id.lib, p->brush);
-               p->paint_cursor= NULL;
+               p->brush = newlibadr_us(fd, sce->id.lib, p->brush);
+               p->paint_cursor = NULL;
        }
 }
 
@@ -4845,8 +4784,7 @@ static void lib_link_scene(FileData *fd, Main *main)
        SceneRenderLayer *srl;
        TimeMarker *marker;
        
-       sce= main->scene.first;
-       while (sce) {
+       for (sce = main->scene.first; sce; sce = sce->id.next) {
                if (sce->id.flag & LIB_NEEDLINK) {
                        /* Link ID Properties -- and copy this comment EXACTLY for easy finding
                         * of library blocks that implement this.*/
@@ -4855,10 +4793,10 @@ static void lib_link_scene(FileData *fd, Main *main)
                        
                        lib_link_keyingsets(fd, &sce->id, &sce->keyingsets);
                        
-                       sce->camera= newlibadr(fd, sce->id.lib, sce->camera);
-                       sce->world= newlibadr_us(fd, sce->id.lib, sce->world);
-                       sce->set= newlibadr(fd, sce->id.lib, sce->set);
-                       sce->gpd= newlibadr_us(fd, sce->id.lib, sce->gpd);
+                       sce->camera = newlibadr(fd, sce->id.lib, sce->camera);
+                       sce->world = newlibadr_us(fd, sce->id.lib, sce->world);
+                       sce->set = newlibadr(fd, sce->id.lib, sce->set);
+                       sce->gpd = newlibadr_us(fd, sce->id.lib, sce->gpd);
                        
                        link_paint(fd, sce, &sce->toolsettings->sculpt->paint);
                        link_paint(fd, sce, &sce->toolsettings->vpaint->paint);
@@ -4866,29 +4804,29 @@ static void lib_link_scene(FileData *fd, Main *main)
                        link_paint(fd, sce, &sce->toolsettings->imapaint.paint);
                        link_paint(fd, sce, &sce->toolsettings->uvsculpt->paint);
                        sce->toolsettings->skgen_template = newlibadr(fd, sce->id.lib, sce->toolsettings->skgen_template);
-
-                       for (base= sce->base.first; base; base= next) {
-                               next= base->next;
-
+                       
+                       for (base = sce->base.first; base; base = next) {
+                               next = base->next;
+                               
                                /* base->object= newlibadr_us(fd, sce->id.lib, base->object); */
-                               base->object= newlibadr_us(fd, sce->id.lib, base->object);
+                               base->object = newlibadr_us(fd, sce->id.lib, base->object);
                                
-                               if (base->object==NULL) {
+                               if (base->object == NULL) {
                                        BKE_reportf_wrap(fd->reports, RPT_ERROR,
                                                         "LIB ERROR: Object lost from scene:'%s\'",
                                                         sce->id.name + 2);
                                        BLI_remlink(&sce->base, base);
-                                       if (base==sce->basact) sce->basact= NULL;
+                                       if (base == sce->basact) sce->basact = NULL;
                                        MEM_freeN(base);
                                }
                        }
-
+                       
                        SEQ_BEGIN (sce->ed, seq)
                        {
-                               if (seq->ipo) seq->ipo= newlibadr_us(fd, sce->id.lib, seq->ipo);
+                               if (seq->ipo) seq->ipo = newlibadr_us(fd, sce->id.lib, seq->ipo);
                                seq->scene_sound = NULL;
                                if (seq->scene) {
-                                       seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
+                                       seq->scene = newlibadr(fd, sce->id.lib, seq->scene);
                                        if (seq->scene) {
                                                seq->scene_sound = sound_scene_add_scene_sound_defaults(sce, seq);
                                        }
@@ -4897,32 +4835,32 @@ static void lib_link_scene(FileData *fd, Main *main)
                                        seq->clip = newlibadr(fd, sce->id.lib, seq->clip);
                                        seq->clip->id.us++;
                                }
-                               if (seq->scene_camera) seq->scene_camera= newlibadr(fd, sce->id.lib, seq->scene_camera);
+                               if (seq->scene_camera) seq->scene_camera = newlibadr(fd, sce->id.lib, seq->scene_camera);
                                if (seq->sound) {
                                        seq->scene_sound = NULL;
                                        if (seq->type == SEQ_HD_SOUND)
                                                seq->type = SEQ_SOUND;
                                        else
-                                               seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
+                                               seq->sound = newlibadr(fd, sce->id.lib, seq->sound);
                                        if (seq->sound) {
                                                seq->sound->id.us++;
                                                seq->scene_sound = sound_add_scene_sound_defaults(sce, seq);
                                        }
                                }
-                               seq->anim= NULL;
+                               seq->anim = NULL;
                        }
                        SEQ_END
 
 #ifdef DURIAN_CAMERA_SWITCH
-                       for (marker= sce->markers.first; marker; marker= marker->next) {
+                       for (marker = sce->markers.first; marker; marker = marker->next) {
                                if (marker->camera) {
-                                       marker->camera= newlibadr(fd, sce->id.lib, marker->camera);
+                                       marker->camera = newlibadr(fd, sce->id.lib, marker->camera);
                                }
                        }
 #else
                        (void)marker;
 #endif
-
+                       
                        seq_update_muting(sce->ed);
                        seq_update_sound_bounds_all(sce);
                        
@@ -4931,38 +4869,39 @@ static void lib_link_scene(FileData *fd, Main *main)
                                composite_patch(sce->nodetree, sce);
                        }
                        
-                       for (srl= sce->r.layers.first; srl; srl= srl->next) {
-                               srl->mat_override= newlibadr_us(fd, sce->id.lib, srl->mat_override);
-                               srl->light_override= newlibadr_us(fd, sce->id.lib, srl->light_override);
+                       for (srl = sce->r.layers.first; srl; srl = srl->next) {
+                               srl->mat_override = newlibadr_us(fd, sce->id.lib, srl->mat_override);
+                               srl->light_override = newlibadr_us(fd, sce->id.lib, srl->light_override);
                        }
                        /*Game Settings: Dome Warp Text*/
-                       sce->gm.dome.warptext= newlibadr(fd, sce->id.lib, sce->gm.dome.warptext);
-
+                       sce->gm.dome.warptext = newlibadr(fd, sce->id.lib, sce->gm.dome.warptext);
+                       
                        /* Motion Tracking */
-                       sce->clip= newlibadr_us(fd, sce->id.lib, sce->clip);
-
+                       sce->clip = newlibadr_us(fd, sce->id.lib, sce->clip);
+                       
                        sce->id.flag -= LIB_NEEDLINK;
                }
-
-               sce= sce->id.next;
        }
 }
 
 static void link_recurs_seq(FileData *fd, ListBase *lb)
 {
        Sequence *seq;
-
+       
        link_list(fd, lb);
-
-       for (seq=lb->first; seq; seq=seq->next)
+       
+       for (seq = lb->first; seq; seq = seq->next) {
                if (seq->seqbase.first)
                        link_recurs_seq(fd, &seq->seqbase);
+       }
 }
 
 static void direct_link_paint(FileData *fd, Paint **paint)
 {
-/* TODO. is this needed */
-       (*paint)= newdataadr(fd, (*paint));
+       /* TODO. is this needed */
+       (*paint) = newdataadr(fd, (*paint));
+       if (*paint && (*paint)->num_input_samples < 1)
+               (*paint)->num_input_samples = 1;
 }
 
 static void direct_link_scene(FileData *fd, Scene *sce)
@@ -4970,29 +4909,29 @@ static void direct_link_scene(FileData *fd, Scene *sce)
        Editing *ed;
        Sequence *seq;
        MetaStack *ms;
-
+       
        sce->theDag = NULL;
        sce->dagisvalid = 0;
-       sce->obedit= NULL;
-       sce->stats= NULL;
-       sce->fps_info= NULL;
-       sce->customdata_mask_modal= 0;
+       sce->obedit = NULL;
+       sce->stats = NULL;
+       sce->fps_info = NULL;
+       sce->customdata_mask_modal = 0;
        sce->lay_updated = 0;
-
+       
        sound_create_scene(sce);
-
+       
        /* set users to one by default, not in lib-link, this will increase it for compo nodes */
-       sce->id.us= 1;
-
+       sce->id.us = 1;
+       
        link_list(fd, &(sce->base));
        
-       sce->adt= newdataadr(fd, sce->adt);
+       sce->adt = newdataadr(fd, sce->adt);
        direct_link_animdata(fd, sce->adt);
        
        link_list(fd, &sce->keyingsets);
        direct_link_keyingsets(fd, &sce->keyingsets);
        
-       sce->basact= newdataadr(fd, sce->basact);
+       sce->basact = newdataadr(fd, sce->basact);
        
        sce->toolsettings= newdataadr(fd, sce->toolsettings);
        if (sce->toolsettings) {
@@ -5000,50 +4939,46 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                direct_link_paint(fd, (Paint**)&sce->toolsettings->vpaint);
                direct_link_paint(fd, (Paint**)&sce->toolsettings->wpaint);
                direct_link_paint(fd, (Paint**)&sce->toolsettings->uvsculpt);
-
-               sce->toolsettings->imapaint.paintcursor= NULL;
-               sce->toolsettings->particle.paintcursor= NULL;
+               
+               sce->toolsettings->imapaint.paintcursor = NULL;
+               sce->toolsettings->particle.paintcursor = NULL;
        }
 
        if (sce->ed) {
-               ListBase *old_seqbasep= &((Editing *)sce->ed)->seqbase;
+               ListBase *old_seqbasep = &((Editing *)sce->ed)->seqbase;
+               
+               ed = sce->ed = newdataadr(fd, sce->ed);
+               
+               ed->act_seq = newdataadr(fd, ed->act_seq);
                
-               ed= sce->ed= newdataadr(fd, sce->ed);
-
-               ed->act_seq= newdataadr(fd, ed->act_seq);
-
                /* recursive link sequences, lb will be correctly initialized */
                link_recurs_seq(fd, &ed->seqbase);
-
+               
                SEQ_BEGIN (ed, seq)
                {
                        seq->seq1= newdataadr(fd, seq->seq1);
                        seq->seq2= newdataadr(fd, seq->seq2);
                        seq->seq3= newdataadr(fd, seq->seq3);
                        /* a patch: after introduction of effects with 3 input strips */
-                       if (seq->seq3==NULL) seq->seq3= seq->seq2;
-
-                       seq->plugin= newdataadr(fd, seq->plugin);
-                       seq->effectdata= newdataadr(fd, seq->effectdata);
+                       if (seq->seq3 == NULL) seq->seq3 = seq->seq2;
+                       
+                       seq->plugin = newdataadr(fd, seq->plugin);
+                       seq->effectdata = newdataadr(fd, seq->effectdata);
                        
                        if (seq->type & SEQ_EFFECT)
                                seq->flag |= SEQ_EFFECT_NOT_LOADED;
-
+                       
                        if (seq->type == SEQ_SPEED) {
-                               SpeedControlVars *s= seq->effectdata;
-                               s->frameMap= NULL;
+                               SpeedControlVars *s = seq->effectdata;
+                               s->frameMap = NULL;
                        }
-
-                       seq->strip= newdataadr(fd, seq->strip);
+                       
+                       seq->strip = newdataadr(fd, seq->strip);
                        if (seq->strip && seq->strip->done==0) {
-                               seq->strip->done= 1;
-
-                               if (seq->type == SEQ_IMAGE ||
-                                  seq->type == SEQ_MOVIE ||
-                                  seq->type == SEQ_RAM_SOUND ||
-                                  seq->type == SEQ_HD_SOUND) {
-                                       seq->strip->stripdata = newdataadr(
-                                               fd, seq->strip->stripdata);
+                               seq->strip->done = TRUE;
+                               
+                               if (ELEM4(seq->type, SEQ_IMAGE, SEQ_MOVIE, SEQ_RAM_SOUND, SEQ_HD_SOUND)) {
+                                       seq->strip->stripdata = newdataadr(fd, seq->strip->stripdata);
                                }
                                else {
                                        seq->strip->stripdata = NULL;
@@ -5090,35 +5025,38 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                        char *poin;
                        intptr_t offset;
                        
-                       offset= ((intptr_t)&(temp.seqbase)) - ((intptr_t)&temp);
+                       offset = ((intptr_t)&(temp.seqbase)) - ((intptr_t)&temp);
                        
                        /* root pointer */
                        if (ed->seqbasep == old_seqbasep) {
-                               ed->seqbasep= &ed->seqbase;
+                               ed->seqbasep = &ed->seqbase;
                        }
                        else {
-                               
-                               poin= (char *)ed->seqbasep;
+                               poin = (char *)ed->seqbasep;
                                poin -= offset;
                                
-                               poin= newdataadr(fd, poin);
-                               if (poin) ed->seqbasep= (ListBase *)(poin+offset);
-                               else ed->seqbasep= &ed->seqbase;
+                               poin = newdataadr(fd, poin);
+                               if (poin)
+                                       ed->seqbasep = (ListBase *)(poin+offset);
+                               else
+                                       ed->seqbasep = &ed->seqbase;
                        }                       
                        /* stack */
                        link_list(fd, &(ed->metastack));
                        
-                       for (ms= ed->metastack.first; ms; ms= ms->next) {
-                               ms->parseq= newdataadr(fd, ms->parseq);
+                       for (ms = ed->metastack.first; ms; ms= ms->next) {
+                               ms->parseq = newdataadr(fd, ms->parseq);
                                
                                if (ms->oldbasep == old_seqbasep)
                                        ms->oldbasep= &ed->seqbase;
                                else {
-                                       poin= (char *)ms->oldbasep;
+                                       poin = (char *)ms->oldbasep;
                                        poin -= offset;
-                                       poin= newdataadr(fd, poin);
-                                       if (poin) ms->oldbasep= (ListBase *)(poin+offset);
-                                       else ms->oldbasep= &ed->seqbase;
+                                       poin = newdataadr(fd, poin);
+                                       if (poin) 
+                                               ms->oldbasep = (ListBase *)(poin+offset);
+                                       else 
+                                               ms->oldbasep = &ed->seqbase;
                                }
                        }
                }
@@ -5135,20 +5073,18 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                sce->r.qtcodecdata->cdParms = newdataadr(fd, sce->r.qtcodecdata->cdParms);
        }
        if (sce->r.ffcodecdata.properties) {
-               sce->r.ffcodecdata.properties = newdataadr(
-                       fd, sce->r.ffcodecdata.properties);
+               sce->r.ffcodecdata.properties = newdataadr(fd, sce->r.ffcodecdata.properties);
                if (sce->r.ffcodecdata.properties) { 
-                       IDP_DirectLinkProperty(
-                               sce->r.ffcodecdata.properties, 
+                       IDP_DirectLinkProperty(sce->r.ffcodecdata.properties, 
                                (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
                }
        }
-
+       
        link_list(fd, &(sce->markers));
        link_list(fd, &(sce->transform_spaces));
        link_list(fd, &(sce->r.layers));
-
-       sce->nodetree= newdataadr(fd, sce->nodetree);
+       
+       sce->nodetree = newdataadr(fd, sce->nodetree);
        if (sce->nodetree)
                direct_link_nodetree(fd, sce->nodetree);
 }
@@ -5159,44 +5095,44 @@ static void direct_link_windowmanager(FileData *fd, wmWindowManager *wm)
 {
        wmWindow *win;
        
-       wm->id.us= 1;
-       link_list(fd, &(wm->windows));
+       wm->id.us = 1;
+       link_list(fd, &wm->windows);
        
-       for (win= wm->windows.first; win; win= win->next) {
-               win->ghostwin= NULL;
-               win->eventstate= NULL;
-               win->curswin= NULL;
-               win->tweak= NULL;
-
-               win->queue.first= win->queue.last= NULL;
-               win->handlers.first= win->handlers.last= NULL;
-               win->modalhandlers.first= win->modalhandlers.last= NULL;
-               win->subwindows.first= win->subwindows.last= NULL;
-               win->gesture.first= win->gesture.last= NULL;
-
-               win->drawdata= NULL;
-               win->drawmethod= -1;
-               win->drawfail= 0;
+       for (win = wm->windows.first; win; win = win->next) {
+               win->ghostwin = NULL;
+               win->eventstate = NULL;
+               win->curswin = NULL;
+               win->tweak = NULL;
+               
+               win->queue.first = win->queue.last = NULL;
+               win->handlers.first = win->handlers.last = NULL;
+               win->modalhandlers.first = win->modalhandlers.last = NULL;
+               win->subwindows.first = win->subwindows.last = NULL;
+               win->gesture.first = win->gesture.last = NULL;
+               
+               win->drawdata = NULL;
+               win->drawmethod = -1;
+               win->drawfail = 0;
        }
        
-       wm->timers.first= wm->timers.last= NULL;
-       wm->operators.first= wm->operators.last= NULL;
-       wm->paintcursors.first= wm->paintcursors.last= NULL;
-       wm->queue.first= wm->queue.last= NULL;
+       wm->timers.first = wm->timers.last = NULL;
+       wm->operators.first = wm->operators.last = NULL;
+       wm->paintcursors.first = wm->paintcursors.last = NULL;
+       wm->queue.first = wm->queue.last = NULL;
        BKE_reports_init(&wm->reports, RPT_STORE);
-
-       wm->keyconfigs.first= wm->keyconfigs.last= NULL;
-       wm->defaultconf= NULL;
-       wm->addonconf= NULL;
-       wm->userconf= NULL;
-
-       wm->jobs.first= wm->jobs.last= NULL;
-       wm->drags.first= wm->drags.last= NULL;
        
-       wm->windrawable= NULL;
-       wm->winactive= NULL;
-       wm->initialized= 0;
-       wm->op_undo_depth= 0;
+       wm->keyconfigs.first = wm->keyconfigs.last = NULL;
+       wm->defaultconf = NULL;
+       wm->addonconf = NULL;
+       wm->userconf = NULL;
+       
+       wm->jobs.first = wm->jobs.last = NULL;
+       wm->drags.first = wm->drags.last = NULL;
+       
+       wm->windrawable = NULL;
+       wm->winactive = NULL;
+       wm->initialized = 0;
+       wm->op_undo_depth = 0;
 }
 
 static void lib_link_windowmanager(FileData *fd, Main *main)
@@ -5204,11 +5140,11 @@ static void lib_link_windowmanager(FileData *fd, Main *main)
        wmWindowManager *wm;
        wmWindow *win;
        
-       for (wm= main->wm.first; wm; wm= wm->id.next) {
+       for (wm = main->wm.first; wm; wm = wm->id.next) {
                if (wm->id.flag & LIB_NEEDLINK) {
-                       for (win= wm->windows.first; win; win= win->next)
-                               win->screen= newlibadr(fd, NULL, win->screen);
-
+                       for (win = wm->windows.first; win; win = win->next)
+                               win->screen = newlibadr(fd, NULL, win->screen);
+                       
                        wm->id.flag -= LIB_NEEDLINK;
                }
        }
@@ -5230,17 +5166,17 @@ static void direct_link_gpencil(FileData *fd, bGPdata *gpd)
        /* relink layers */
        link_list(fd, &gpd->layers);
        
-       for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
+       for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
                /* relink frames */
                link_list(fd, &gpl->frames);
-               gpl->actframe= newdataadr(fd, gpl->actframe);
+               gpl->actframe = newdataadr(fd, gpl->actframe);
                
-               for (gpf= gpl->frames.first; gpf; gpf= gpf->next) {
+               for (gpf = gpl->frames.first; gpf; gpf = gpf->next) {
                        /* relink strokes (and their points) */
                        link_list(fd, &gpf->strokes);
                        
-                       for (gps= gpf->strokes.first; gps; gps= gps->next) {
-                               gps->points= newdataadr(fd, gps->points);
+                       for (gps = gpf->strokes.first; gps; gps = gps->next) {
+                               gps->points = newdataadr(fd, gps->points);
                        }
                }
        }
@@ -5254,19 +5190,19 @@ static void butspace_version_132(SpaceButs *buts)
        buts->v2d.tot.ymin = 0.0f;
        buts->v2d.tot.xmax = 1279.0f;
        buts->v2d.tot.ymax = 228.0f;
-
-       buts->v2d.min[0]= 256.0f;
-       buts->v2d.min[1]= 42.0f;
-
-       buts->v2d.max[0]= 2048.0f;
-       buts->v2d.max[1]= 450.0f;
-
-       buts->v2d.minzoom= 0.5f;
-       buts->v2d.maxzoom= 1.21f;
-
-       buts->v2d.scroll= 0;
-       buts->v2d.keepzoom= 1;
-       buts->v2d.keeptot= 1;
+       
+       buts->v2d.min[0] = 256.0f;
+       buts->v2d.min[1] = 42.0f;
+       
+       buts->v2d.max[0] = 2048.0f;
+       buts->v2d.max[1] = 450.0f;
+       
+       buts->v2d.minzoom = 0.5f;
+       buts->v2d.maxzoom = 1.21f;
+       
+       buts->v2d.scroll = 0;
+       buts->v2d.keepzoom = 1;
+       buts->v2d.keeptot = 1;
 }
 
 /* note: file read without screens option G_FILE_NO_UI; 
@@ -5275,22 +5211,21 @@ static void lib_link_screen(FileData *fd, Main *main)
 {
        bScreen *sc;
        ScrArea *sa;
-
-       for (sc= main->screen.first; sc; sc= sc->id.next) {
+       
+       for (sc = main->screen.first; sc; sc = sc->id.next) {
                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 */
+                       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) {
+                       for (sa = sc->areabase.first; sa; sa = sa->next) {
                                SpaceLink *sl;
                                
-                               sa->full= newlibadr(fd, sc->id.lib, sa->full);
+                               sa->full = newlibadr(fd, sc->id.lib, sa->full);
                                
-                               for (sl= sa->spacedata.first; sl; sl= sl->next) {
-                                       if (sl->spacetype==SPACE_VIEW3D) {
-                                               View3D *v3d= (View3D*) sl;
+                               for (sl = sa->spacedata.first; sl; sl= sl->next) {
+                                       if (sl->spacetype == SPACE_VIEW3D) {
+                                               View3D *v3d = (View3D*) sl;
                                                BGpic *bgpic = NULL;
                                                
                                                v3d->camera= newlibadr(fd, sc->id.lib, v3d->camera);
@@ -5298,147 +5233,144 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                
                                                /* should be do_versions but not easy adding into the listbase */
                                                if (v3d->bgpic) {
-                                                       v3d->bgpic= newlibadr(fd, sc->id.lib, v3d->bgpic);
+                                                       v3d->bgpic = newlibadr(fd, sc->id.lib, v3d->bgpic);
                                                        BLI_addtail(&v3d->bgpicbase, bgpic);
-                                                       v3d->bgpic= NULL;
+                                                       v3d->bgpic = NULL;
                                                }
-
-                                               for (bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next) {
-                                                       bgpic->ima= newlibadr_us(fd, sc->id.lib, bgpic->ima);
-                                                       bgpic->clip= newlibadr_us(fd, sc->id.lib, bgpic->clip);
+                                               
+                                               for (bgpic = v3d->bgpicbase.first; bgpic; bgpic = bgpic->next) {
+                                                       bgpic->ima = newlibadr_us(fd, sc->id.lib, bgpic->ima);
+                                                       bgpic->clip = newlibadr_us(fd, sc->id.lib, bgpic->clip);
                                                }
                                                if (v3d->localvd) {
-                                                       v3d->localvd->camera= newlibadr(fd, sc->id.lib, v3d->localvd->camera);
+                                                       v3d->localvd->camera = newlibadr(fd, sc->id.lib, v3d->localvd->camera);
                                                }
                                        }
-                                       else if (sl->spacetype==SPACE_IPO) {
-                                               SpaceIpo *sipo= (SpaceIpo *)sl;
-                                               bDopeSheet *ads= sipo->ads;
+                                       else if (sl->spacetype == SPACE_IPO) {
+                                               SpaceIpo *sipo = (SpaceIpo *)sl;
+                                               bDopeSheet *ads = sipo->ads;
                                                
                                                if (ads) {
-                                                       ads->source= newlibadr(fd, sc->id.lib, ads->source);
-                                                       ads->filter_grp= newlibadr(fd, sc->id.lib, ads->filter_grp);
+                                                       ads->source = newlibadr(fd, sc->id.lib, ads->source);
+                                                       ads->filter_grp = newlibadr(fd, sc->id.lib, ads->filter_grp);
                                                }
                                        }
-                                       else if (sl->spacetype==SPACE_BUTS) {
-                                               SpaceButs *sbuts= (SpaceButs *)sl;
-                                               sbuts->pinid= newlibadr(fd, sc->id.lib, sbuts->pinid);
-                                               sbuts->mainbo= sbuts->mainb;
-                                               sbuts->mainbuser= sbuts->mainb;
-                                               if (main->versionfile<132)
+                                       else if (sl->spacetype == SPACE_BUTS) {
+                                               SpaceButs *sbuts = (SpaceButs *)sl;
+                                               sbuts->pinid = newlibadr(fd, sc->id.lib, sbuts->pinid);
+                                               sbuts->mainbo = sbuts->mainb;
+                                               sbuts->mainbuser = sbuts->mainb;
+                                               if (main->versionfile < 132)
                                                        butspace_version_132(sbuts);
                                        }
-                                       else if (sl->spacetype==SPACE_FILE) {
-                                               SpaceFile *sfile= (SpaceFile *)sl;
-                                               sfile->files= NULL;
-                                               sfile->op= NULL;
-                                               sfile->layout= NULL;
-                                               sfile->folders_prev= NULL;
-                                               sfile->folders_next= NULL;
+                                       else if (sl->spacetype == SPACE_FILE) {
+                                               SpaceFile *sfile = (SpaceFile *)sl;
+                                               sfile->files = NULL;
+                                               sfile->op = NULL;
+                                               sfile->layout = NULL;
+                                               sfile->folders_prev = NULL;
+                                               sfile->folders_next = NULL;
                                        }
-                                       else if (sl->spacetype==SPACE_ACTION) {
-                                               SpaceAction *saction= (SpaceAction *)sl;
-                                               bDopeSheet *ads= &saction->ads;
+                                       else if (sl->spacetype == SPACE_ACTION) {
+                                               SpaceAction *saction = (SpaceAction *)sl;
+                                               bDopeSheet *ads = &saction->ads;
                                                
                                                if (ads) {
-                                                       ads->source= newlibadr(fd, sc->id.lib, ads->source);
-                                                       ads->filter_grp= newlibadr(fd, sc->id.lib, ads->filter_grp);
+                                                       ads->source = newlibadr(fd, sc->id.lib, ads->source);
+                                                       ads->filter_grp = newlibadr(fd, sc->id.lib, ads->filter_grp);
                                                }
                                                
                                                saction->action = newlibadr(fd, sc->id.lib, saction->action);
                                        }
-                                       else if (sl->spacetype==SPACE_IMAGE) {
-                                               SpaceImage *sima= (SpaceImage *)sl;
-
-                                               sima->image= newlibadr_us(fd, sc->id.lib, sima->image);
+                                       else if (sl->spacetype == SPACE_IMAGE) {
+                                               SpaceImage *sima = (SpaceImage *)sl;
+                                               
+                                               sima->image = newlibadr_us(fd, sc->id.lib, sima->image);
                                                
                                                /* NOTE: pre-2.5, this was local data not lib data, but now we need this as lib data
                                                 * so fingers crossed this works fine!
                                                 */
-                                               sima->gpd= newlibadr_us(fd, sc->id.lib, sima->gpd);
+                                               sima->gpd = newlibadr_us(fd, sc->id.lib, sima->gpd);
                                        }
-                                       else if (sl->spacetype==SPACE_NLA) {
+                                       else if (sl->spacetype == SPACE_NLA) {
                                                SpaceNla *snla= (SpaceNla *)sl;
                                                bDopeSheet *ads= snla->ads;
                                                
                                                if (ads) {
-                                                       ads->source= newlibadr(fd, sc->id.lib, ads->source);
-                                                       ads->filter_grp= newlibadr(fd, sc->id.lib, ads->filter_grp);
+                                                       ads->source = newlibadr(fd, sc->id.lib, ads->source);
+                                                       ads->filter_grp = newlibadr(fd, sc->id.lib, ads->filter_grp);
                                                }
                                        }
-                                       else if (sl->spacetype==SPACE_TEXT) {
+                                       else if (sl->spacetype == SPACE_TEXT) {
                                                SpaceText *st= (SpaceText *)sl;
-
+                                               
                                                st->text= newlibadr(fd, sc->id.lib, st->text);
                                                st->drawcache= NULL;
-
                                        }
-                                       else if (sl->spacetype==SPACE_SCRIPT) {
-
-                                               SpaceScript *scpt= (SpaceScript *)sl;
+                                       else if (sl->spacetype == SPACE_SCRIPT) {
+                                               SpaceScript *scpt = (SpaceScript *)sl;
                                                /*scpt->script = NULL; - 2.45 set to null, better re-run the script */
                                                if (scpt->script) {
-                                                       scpt->script= newlibadr(fd, sc->id.lib, scpt->script);
+                                                       scpt->script = newlibadr(fd, sc->id.lib, scpt->script);
                                                        if (scpt->script) {
                                                                SCRIPT_SET_NULL(scpt->script);
                                                        }
                                                }
                                        }
-                                       else if (sl->spacetype==SPACE_OUTLINER) {
+                                       else if (sl->spacetype == SPACE_OUTLINER) {
                                                SpaceOops *so= (SpaceOops *)sl;
                                                TreeStoreElem *tselem;
                                                int a;
-
-                                               so->tree.first= so->tree.last= NULL;
-                                               so->search_tse.id= newlibadr(fd, NULL, so->search_tse.id);
+                                               
+                                               so->tree.first = so->tree.last= NULL;
+                                               so->search_tse.id = newlibadr(fd, NULL, so->search_tse.id);
                                                
                                                if (so->treestore) {
-                                                       tselem= so->treestore->data;
-                                                       for (a=0; a<so->treestore->usedelem; a++, tselem++) {
-                                                               tselem->id= newlibadr(fd, NULL, tselem->id);
+                                                       tselem = so->treestore->data;
+                                                       for (a=0; a < so->treestore->usedelem; a++, tselem++) {
+                                                               tselem->id = newlibadr(fd, NULL, tselem->id);
                                                        }
                                                }
                                        }
-                                       else if (sl->spacetype==SPACE_NODE) {
-                                               SpaceNode *snode= (SpaceNode *)sl;
+                                       else if (sl->spacetype == SPACE_NODE) {
+                                               SpaceNode *snode = (SpaceNode *)sl;
                                                
-                                               snode->id= newlibadr(fd, sc->id.lib, snode->id);
-                                               snode->edittree= NULL;
+                                               snode->id = newlibadr(fd, sc->id.lib, snode->id);
+                                               snode->edittree = NULL;
                                                
                                                if (ELEM3(snode->treetype, NTREE_COMPOSIT, NTREE_SHADER, NTREE_TEXTURE)) {
                                                        /* internal data, a bit patchy */
-                                                       snode->nodetree= NULL;
+                                                       snode->nodetree = NULL;
                                                        if (snode->id) {
                                                                if (GS(snode->id->name)==ID_MA)
-                                                                       snode->nodetree= ((Material *)snode->id)->nodetree;
+                                                                       snode->nodetree = ((Material *)snode->id)->nodetree;
                                                                else if (GS(snode->id->name)==ID_WO)
-                                                                       snode->nodetree= ((World *)snode->id)->nodetree;
+                                                                       snode->nodetree = ((World *)snode->id)->nodetree;
                                                                else if (GS(snode->id->name)==ID_LA)
-                                                                       snode->nodetree= ((Lamp *)snode->id)->nodetree;
+                                                                       snode->nodetree = ((Lamp *)snode->id)->nodetree;
                                                                else if (GS(snode->id->name)==ID_SCE)
-                                                                       snode->nodetree= ((Scene *)snode->id)->nodetree;
+                                                                       snode->nodetree = ((Scene *)snode->id)->nodetree;
                                                                else if (GS(snode->id->name)==ID_TE)
-                                                                       snode->nodetree= ((Tex *)snode->id)->nodetree;
+                                                                       snode->nodetree = ((Tex *)snode->id)->nodetree;
                                                        }
                                                }
                                                else {
-                                                       snode->nodetree= newlibadr_us(fd, sc->id.lib, snode->nodetree);
+                                                       snode->nodetree = newlibadr_us(fd, sc->id.lib, snode->nodetree);
                                                }
                                                
                                                snode->linkdrag.first = snode->linkdrag.last = NULL;
                                        }
-                                       else if (sl->spacetype==SPACE_CLIP) {
-                                               SpaceClip *sclip= (SpaceClip *)sl;
+                                       else if (sl->spacetype == SPACE_CLIP) {
+                                               SpaceClip *sclip = (SpaceClip *)sl;
 
-                                               sclip->clip= newlibadr_us(fd, sc->id.lib, sclip->clip);
-                                               sclip->mask= newlibadr_us(fd, sc->id.lib, sclip->mask);
+                                               sclip->clip = newlibadr_us(fd, sc->id.lib, sclip->clip);
+                                               sclip->mask = newlibadr_us(fd, sc->id.lib, sclip->mask);
 
                                                sclip->scopes.track_preview = NULL;
                                                sclip->draw_context = NULL;
                                                sclip->scopes.ok = 0;
                                        }
                                }
-                               sa= sa->next;
                        }
                        sc->id.flag -= LIB_NEEDLINK;
                }
@@ -5448,23 +5380,22 @@ static void lib_link_screen(FileData *fd, Main *main)
 /* Only for undo files, or to restore a screen after reading without UI... */
 static void *restore_pointer_by_name(Main *mainp, ID *id, int user)
 {
-               
        if (id) {
-               ListBase *lb= which_libbase(mainp, GS(id->name));
+               ListBase *lb = which_libbase(mainp, GS(id->name));
                
                if (lb) {       // there's still risk of checking corrupt mem (freed Ids in oops)
-                       ID *idn= lb->first;
-                       char *name= id->name+2;
+                       ID *idn = lb->first;
+                       char *name = id->name + 2;
                        
-                       while (idn) {
-                               if (idn->name[2]==name[0] && strcmp(idn->name+2, name)==0) {
-                                       if (idn->lib==id->lib) {
-                                               if (user && idn->us==0) idn->us++;
+                       for (; idn; idn = idn->next) {
+                               if (idn->name[2] == name[0] && strcmp(idn->name+2, name) == 0) {
+                                       if (idn->lib == id->lib) {
+                                               if (user && idn->us == 0) idn->us++;
                                                break;
                                        }
                                }
-                               idn= idn->next;
                        }
+                       
                        return idn;
                }
        }
@@ -5474,18 +5405,18 @@ static void *restore_pointer_by_name(Main *mainp, ID *id, int user)
 static int lib_link_seq_clipboard_cb(Sequence *seq, void *arg_pt)
 {
        Main *newmain = (Main *)arg_pt;
-
+       
        if (seq->sound) {
                seq->sound = restore_pointer_by_name(newmain, (ID *)seq->sound, 0);
                seq->sound->id.us++;
        }
-
+       
        if (seq->scene)
                seq->scene = restore_pointer_by_name(newmain, (ID *)seq->scene, 1);
-
+       
        if (seq->scene_camera)
                seq->scene_camera = restore_pointer_by_name(newmain, (ID *)seq->scene_camera, 1);
-
+       
        return 1;
 }
 
@@ -5504,56 +5435,55 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
        wmWindowManager *wm;
        bScreen *sc;
        ScrArea *sa;
-
+       
        /* first windowmanager */
-       for (wm= newmain->wm.first; wm; wm= wm->id.next) {
+       for (wm = newmain->wm.first; wm; wm = wm->id.next) {
                for (win= wm->windows.first; win; win= win->next) {
-                       win->screen= restore_pointer_by_name(newmain, (ID *)win->screen, 1);
+                       win->screen = restore_pointer_by_name(newmain, (ID *)win->screen, 1);
                        
-                       if (win->screen==NULL)
-                               win->screen= curscr