Merging r44227 through r45619 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenloader / intern / readfile.c
index d50a0633af428bf141d0c16de3c77eb8a9817444..f82f10e83f56a7e927a20744dd12ea1421deda4b 100644 (file)
 #include "BLI_utildefines.h"
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
+#include "BLI_edgehash.h"
 
 #include "BKE_anim.h"
 #include "BKE_action.h"
@@ -271,7 +272,7 @@ static void BKE_reportf_wrap(ReportList *reports, ReportType type, const char *f
 
        BKE_report(reports, type, fixed_buf);
 
-       if(G.background==0) {
+       if (G.background==0) {
                printf("%s\n", fixed_buf);
        }
 }
@@ -290,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;
 }
 
@@ -307,7 +308,7 @@ static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int n
 {
        OldNew *entry;
 
-       if(oldaddr==NULL || newaddr==NULL) return;
+       if (oldaddr==NULL || newaddr==NULL) return;
        
        if (onm->nentries==onm->entriessize) {
                int osize= onm->entriessize;
@@ -330,7 +331,7 @@ static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
 {
        int i;
 
-       if(addr==NULL) return NULL;
+       if (addr==NULL) return NULL;
 
        if (onm->lasthit<onm->nentries-1) {
                OldNew *entry= &onm->entries[++onm->lasthit];
@@ -360,16 +361,16 @@ 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) {
+       if (onm->sorted) {
                OldNew entry_s, *entry;
                
                entry_s.old= addr;
                
                entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
-               if(entry) {
+               if (entry) {
                        ID *id= entry->newp;
                        
                        if (id && (!lib || id->lib)) {
@@ -431,7 +432,7 @@ static void add_main_to_main(Main *mainvar, Main *from)
 
        set_listbasepointers(mainvar, lbarray);
        a= set_listbasepointers(from, fromarray);
-       while(a--) {
+       while (a--) {
                BLI_movelisttolist(lbarray[a], fromarray[a]);
        }
 }
@@ -456,12 +457,12 @@ static void split_libdata(ListBase *lb, Main *first)
        Main *mainvar;
 
        id= lb->first;
-       while(id) {
+       while (id) {
                idnext= id->next;
-               if(id->lib) {
+               if (id->lib) {
                        mainvar= first;
-                       while(mainvar) {
-                               if(mainvar->curlib==id->lib) {
+                       while (mainvar) {
+                               if (mainvar->curlib==id->lib) {
                                        lbn= which_libbase(mainvar, GS(id->name));
                                        BLI_remlink(lb, id);
                                        BLI_addtail(lbn, id);
@@ -469,7 +470,7 @@ static void split_libdata(ListBase *lb, Main *first)
                                }
                                mainvar= mainvar->next;
                        }
-                       if(mainvar==NULL) printf("error split_libdata\n");
+                       if (mainvar==NULL) printf("error split_libdata\n");
                }
                id= idnext;
        }
@@ -484,7 +485,7 @@ void blo_split_main(ListBase *mainlist, Main *main)
        mainlist->first= mainlist->last= main;
        main->next= NULL;
 
-       if(main->library.first==NULL)
+       if (main->library.first==NULL)
                return;
        
        for (lib= main->library.first; lib; lib= lib->id.next) {
@@ -494,7 +495,7 @@ void blo_split_main(ListBase *mainlist, Main *main)
        }
 
        i= set_listbasepointers(main, lbarray);
-       while(i--)
+       while (i--)
                split_libdata(lbarray[i], main->next);
 }
 
@@ -515,7 +516,7 @@ static void read_file_version(FileData *fd, Main *main)
        for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
                if (bhead->code==GLOB) {
                        FileGlobal *fg= read_struct(fd, bhead, "Global");
-                       if(fg) {
+                       if (fg) {
                                main->subversionfile= fg->subversion;
                                main->minversionfile= fg->minversion;
                                main->minsubversionfile= fg->minsubversion;
@@ -543,7 +544,7 @@ static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *filepat
                char *libname= (m->curlib)?m->curlib->filepath:m->name;
                
                if (BLI_path_cmp(name1, libname) == 0) {
-                       if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
+                       if (G.debug & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
                        return m;
                }
        }
@@ -559,7 +560,7 @@ static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *filepat
        
        read_file_version(fd, m);
        
-       if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", filepath);
+       if (G.debug & G_DEBUG) printf("blo_find_main: added new lib %s\n", filepath);
        return m;
 }
 
@@ -569,7 +570,7 @@ 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);
@@ -581,7 +582,7 @@ 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);
@@ -665,14 +666,17 @@ static BHeadN *get_bhead(FileData *fd)
 
                                        if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
                                                bh8_from_bh4(&bhead, &bhead4);
-                                       } else {
+                                       }
+                                       else {
                                                memcpy(&bhead, &bhead4, sizeof(bhead));
                                        }
-                               } else {
+                               }
+                               else {
                                        fd->eof = 1;
                                        bhead.len= 0;
                                }
-                       } else {
+                       }
+                       else {
                                bhead8.code = DATA;
                                readsize = fd->read(fd, &bhead8, sizeof(bhead8));
 
@@ -683,10 +687,12 @@ static BHeadN *get_bhead(FileData *fd)
 
                                        if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
                                                bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
-                                       } else {
+                                       }
+                                       else {
                                                memcpy(&bhead, &bhead8, sizeof(bhead));
                                        }
-                               } else {
+                               }
+                               else {
                                        fd->eof = 1;
                                        bhead.len= 0;
                                }
@@ -711,7 +717,8 @@ static BHeadN *get_bhead(FileData *fd)
                                                MEM_freeN(new_bhead);
                                                new_bhead = NULL;
                                        }
-                               } else {
+                               }
+                               else {
                                        fd->eof = 1;
                                }
                        }
@@ -791,18 +798,19 @@ static void decode_blender_header(FileData *fd)
        readsize = fd->read(fd, header, sizeof(header));
 
        if (readsize == sizeof(header)) {
-               if(strncmp(header, "BLENDER", 7) == 0) {
+               if (strncmp(header, "BLENDER", 7) == 0) {
                        int remove_this_endian_test= 1;
 
                        fd->flags |= FD_FLAGS_FILE_OK;
 
                        // what size are pointers in the file ?
-                       if(header[7]=='_') {
+                       if (header[7]=='_') {
                                fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
                                if (sizeof(void *) != 4) {
                                        fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
                                }
-                       } else {
+                       }
+                       else {
                                if (sizeof(void *) != 8) {
                                        fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
                                }
@@ -840,7 +848,8 @@ static int read_file_dna(FileData *fd)
                        }
 
                        return 1;
-               } else if (bhead->code==ENDB)
+               }
+               else if (bhead->code==ENDB)
                        break;
        }
 
@@ -853,7 +862,8 @@ static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size
 
        if (readsize < 0) {
                readsize = EOF;
-       } else {
+       }
+       else {
                filedata->seek += readsize;
        }
 
@@ -866,7 +876,8 @@ static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int
 
        if (readsize < 0) {
                readsize = EOF;
-       } else {
+       }
+       else {
                filedata->seek += readsize;
        }
 
@@ -891,14 +902,14 @@ static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int s
        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) {
+       if (seek != (unsigned int)filedata->seek) {
                chunk= filedata->memfile->chunks.first;
                seek= 0;
                
-               while(chunk) {
-                       if(seek + chunk->size > (unsigned) filedata->seek) break;
+               while (chunk) {
+                       if (seek + chunk->size > (unsigned) filedata->seek) break;
                        seek+= chunk->size;
                        chunk= chunk->next;
                }
@@ -906,18 +917,18 @@ static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int s
                seek= filedata->seek;
        }
        
-       if(chunk) {
+       if (chunk) {
                totread= 0;
 
                do {
                        /* first check if it's on the end if current chunk */
-                       if(seek-offset == chunk->size) {
+                       if (seek-offset == chunk->size) {
                                offset+= chunk->size;
                                chunk= chunk->next;
                        }
 
                        /* debug, should never happen */
-                       if(chunk==NULL) {
+                       if (chunk==NULL) {
                                printf("illegal read, chunk zero\n");
                                return 0;
                        }
@@ -928,14 +939,14 @@ static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int s
                        /* 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)
+                       if (chunkoffset+readsize > chunk->size)
                                readsize= chunk->size-chunkoffset;
 
                        memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
                        totread += readsize;
                        filedata->seek += readsize;
                        seek += readsize;
-               } while(totread < size);
+               } while (totread < size);
                
                return totread;
        }
@@ -951,7 +962,7 @@ static FileData *filedata_new(void)
        fd->gzfiledes = NULL;
 
                /* XXX, this doesn't need to be done all the time,
-                * but it keeps us reentrant,  remove once we have
+                * 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);
@@ -989,12 +1000,13 @@ FileData *blo_openblenderfile(const char *filepath, ReportList *reports)
 {
        gzFile gzfile;
        errno= 0;
-       gzfile= gzopen(filepath, "rb");
+       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;
-       } else {
+       }
+       else {
                FileData *fd = filedata_new();
                fd->gzfiledes = gzfile;
                fd->read = fd_read_gzip_from_file;
@@ -1011,7 +1023,8 @@ FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
        if (!mem || memsize<SIZEOFBLENDERHEADER) {
                BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
                return NULL;
-       } else {
+       }
+       else {
                FileData *fd= filedata_new();
                fd->buffer= mem;
                fd->buffersize= memsize;
@@ -1027,7 +1040,8 @@ FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
        if (!memfile) {
                BKE_report(reports, RPT_ERROR, "Unable to open blend <memory>");
                return NULL;
-       } else {
+       }
+       else {
                FileData *fd= filedata_new();
                fd->memfile= memfile;
 
@@ -1101,8 +1115,8 @@ int BLO_is_a_library(const char *path, char *dir, char *group)
        
        strcpy(dir, path);
        len= strlen(dir);
-       if(len<7) return 0;
-       if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
+       if (len<7) return 0;
+       if ( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
        
        group[0]= 0;
        dir[len-1]= 0;
@@ -1110,9 +1124,9 @@ int BLO_is_a_library(const char *path, char *dir, char *group)
        /* Find the last slash */
        fd= BLI_last_slash(dir);
 
-       if(fd==NULL) return 0;
+       if (fd==NULL) return 0;
        *fd= 0;
-       if(BLO_has_bfile_extension(fd+1)) {
+       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 */
        }
@@ -1145,14 +1159,14 @@ static void *newglobadr(FileData *fd, void *adr)                /* direct datablocks with glob
 
 static void *newimaadr(FileData *fd, void *adr)                /* used to restore image data after undo */
 {
-       if(fd->imamap && adr)
+       if (fd->imamap && adr)
                return oldnewmap_lookup_and_inc(fd->imamap, adr);
        return NULL;
 }
 
 static void *newmclipadr(FileData *fd, void *adr)              /* used to restore movie clip data after undo */
 {
-       if(fd->movieclipmap && adr)
+       if (fd->movieclipmap && adr)
                return oldnewmap_lookup_and_inc(fd->movieclipmap, adr);
        return NULL;
 }
@@ -1167,7 +1181,7 @@ static void *newlibadr_us(FileData *fd, void *lib, void *adr)     /* increases user
 {
        ID *id= newlibadr(fd, lib, adr);
 
-       if(id)
+       if (id)
                id->us++;
 
        return id;
@@ -1182,7 +1196,7 @@ static void change_idid_adr_fd(FileData *fd, void *old, void *new)
                
                if (old==entry->newp && entry->nr==ID_ID) {
                        entry->newp= new;
-                       if(new) entry->nr= GS( ((ID *)new)->name );
+                       if (new) entry->nr= GS( ((ID *)new)->name );
                        break;
                }
        }
@@ -1192,13 +1206,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;
+               if (mainptr->curlib) fd= mainptr->curlib->filedata;
                else fd= basefd;
                
-               if(fd) {
+               if (fd) {
                        change_idid_adr_fd(fd, old, new);
                }
        }
@@ -1212,8 +1226,8 @@ void blo_clear_proxy_pointers_from_lib(Main *oldmain)
 {
        Object *ob= oldmain->object.first;
        
-       for(;ob; ob= ob->id.next)
-               if(ob->id.lib)
+       for (;ob; ob= ob->id.next)
+               if (ob->id.lib)
                        ob->proxy_from= NULL;
 }
 
@@ -1225,20 +1239,20 @@ void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
        
        fd->imamap= oldnewmap_new();
        
-       for(;ima; ima= ima->id.next) {
+       for (;ima; ima= ima->id.next) {
                Link *ibuf= ima->ibufs.first;
-               for(; ibuf; ibuf= ibuf->next) 
+               for (; ibuf; ibuf= ibuf->next)
                        oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
-               if(ima->gputexture)
+               if (ima->gputexture)
                        oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
-               for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
-                       if(ima->renders[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) {
-               if(sce->nodetree) {
+       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);
                }
        }
@@ -1259,27 +1273,27 @@ void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
                        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) {
+               for (ibuf= ima->ibufs.first; ibuf; ibuf= next) {
                        next= ibuf->next;
-                       if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
+                       if (NULL==newimaadr(fd, ibuf)) {        /* so was restored */
                                BLI_remlink(&ima->ibufs, ibuf);
                                ima->bindcode= 0;
                                ima->gputexture= NULL;
                        }
                }
-               for(i=0; i<IMA_MAX_RENDER_SLOT; i++)
+               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) {
-               if(sce->nodetree) {
+       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)
                                node->preview= newimaadr(fd, node->preview);
                }
        }
@@ -1292,19 +1306,19 @@ void blo_make_movieclip_pointer_map(FileData *fd, Main *oldmain)
 
        fd->movieclipmap= oldnewmap_new();
 
-       for(;clip; clip= clip->id.next) {
-               if(clip->cache)
+       for (;clip; clip= clip->id.next) {
+               if (clip->cache)
                        oldnewmap_insert(fd->movieclipmap, clip->cache, clip->cache, 0);
 
-               if(clip->tracking.camera.intrinsics)
+               if (clip->tracking.camera.intrinsics)
                        oldnewmap_insert(fd->movieclipmap, clip->tracking.camera.intrinsics, clip->tracking.camera.intrinsics, 0);
        }
 
-       for(; sce; sce= sce->id.next) {
-               if(sce->nodetree) {
+       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);
                }
        }
@@ -1325,16 +1339,16 @@ void blo_end_movieclip_pointer_map(FileData *fd, Main *oldmain)
                                entry->newp= NULL;
        }
 
-       for(;clip; clip= clip->id.next) {
+       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) {
-               if(sce->nodetree) {
+       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)
                                        node->storage= newmclipadr(fd, node->storage);
                }
        }
@@ -1347,11 +1361,11 @@ void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
        Main *ptr= mainlist->first;
        ListBase *lbarray[MAX_LIBARRAY];
        
-       for(ptr= ptr->next; ptr; ptr= ptr->next) {
+       for (ptr= ptr->next; ptr; ptr= ptr->next) {
                int i= set_listbasepointers(ptr, lbarray);
-               while(i--) {
+               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));
                }
        }
@@ -1370,7 +1384,7 @@ static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
        blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
 
        nblocks= bhead->nr;
-       while(nblocks--) {
+       while (nblocks--) {
                DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
 
                data+= blocksize;
@@ -1387,9 +1401,10 @@ static void *read_struct(FileData *fd, BHead *bh, const char *blockname)
                        switch_endian_structs(fd->filesdna, bh);
 
                if (fd->compflags[bh->SDNAnr]) {        /* flag==0: doesn't exist anymore */
-                       if(fd->compflags[bh->SDNAnr]==2) {
+                       if (fd->compflags[bh->SDNAnr]==2) {
                                temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
-                       } else {
+                       }
+                       else {
                                temp= MEM_mallocN(bh->len, blockname);
                                memcpy(temp, (bh+1), bh->len);
                        }
@@ -1403,12 +1418,12 @@ static void link_list(FileData *fd, ListBase *lb)               /* only direct data */
 {
        Link *ln, *prev;
 
-       if(lb->first==NULL) return;
+       if (lb->first==NULL) return;
 
        lb->first= newdataadr(fd, lb->first);
        ln= lb->first;
        prev= NULL;
-       while(ln) {
+       while (ln) {
                ln->next= newdataadr(fd, ln->next);
                ln->prev= prev;
                prev= ln;
@@ -1422,18 +1437,18 @@ static void link_glob_list(FileData *fd, ListBase *lb)          /* for glob data */
        Link *ln, *prev;
        void *poin;
 
-       if(lb->first==NULL) return;
+       if (lb->first==NULL) return;
        poin= newdataadr(fd, lb->first);
-       if(lb->first) {
+       if (lb->first) {
                oldnewmap_insert(fd->globmap, lb->first, poin, 0);
        }
        lb->first= poin;
 
        ln= lb->first;
        prev= NULL;
-       while(ln) {
+       while (ln) {
                poin= newdataadr(fd, ln->next);
-               if(ln->next) {
+               if (ln->next) {
                        oldnewmap_insert(fd->globmap, ln->next, poin, 0);
                }
                ln->next= poin;
@@ -1458,15 +1473,15 @@ static void test_pointer_array(FileData *fd, void **mat)
                 * the old dna format to a pointer array in
                 * the new dna format.
                 */
-       if(*mat) {
+       if (*mat) {
                len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
 
-               if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
+               if (fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
                        ipoin=imat= MEM_mallocN( len*4, "newmatar");
                        lpoin= *mat;
 
-                       while(len-- > 0) {
-                               if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
+                       while (len-- > 0) {
+                               if ((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
                                        SWITCH_LONGINT(*lpoin);
                                *ipoin= (int) ((*lpoin) >> 3);
                                ipoin++;
@@ -1476,11 +1491,11 @@ static void test_pointer_array(FileData *fd, void **mat)
                        *mat= imat;
                }
 
-               if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
+               if (fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
                        lpoin=lmat= MEM_mallocN( len*8, "newmatar");
                        ipoin= *mat;
 
-                       while(len-- > 0) {
+                       while (len-- > 0) {
                                *lpoin= *ipoin;
                                ipoin++;
                                lpoin++;
@@ -1508,14 +1523,14 @@ static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData
        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 dont crash */
-       if(array==NULL) {
+        * theres not really anything we can do to correct this, at least don't crash */
+       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);
 }
 
@@ -1528,14 +1543,14 @@ static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *f
        prop->totallen = prop->len;
        prop->data.pointer = newdataadr(fd, prop->data.pointer);
 
-       if(prop->subtype == IDP_GROUP) {
+       if (prop->subtype == IDP_GROUP) {
                test_pointer_array(fd, prop->data.pointer);
                array= prop->data.pointer;
 
-               for(i=0; i<prop->len; i++)
+               for (i=0; i<prop->len; i++)
                        IDP_DirectLinkProperty(array[i], switch_endian, fd);
        }
-       else if(prop->subtype == IDP_DOUBLE) {
+       else if (prop->subtype == IDP_DOUBLE) {
                if (switch_endian) {
                        for (i=0; i<prop->len; i++) {
                                SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
@@ -1622,7 +1637,7 @@ 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++) {
+       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;
@@ -1636,8 +1651,8 @@ 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) {
-               if(brush->id.flag & LIB_NEEDLINK) {
+       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);
@@ -1652,7 +1667,7 @@ static void direct_link_brush(FileData *fd, Brush *brush)
 
        /* fallof curve */
        brush->curve= newdataadr(fd, brush->curve);
-       if(brush->curve)
+       if (brush->curve)
                direct_link_curvemapping(fd, brush->curve);
        else
                brush_curve_preset(brush, CURVE_PRESET_SHARP);
@@ -1664,7 +1679,7 @@ static void direct_link_brush(FileData *fd, Brush *brush)
 static void direct_link_script(FileData *UNUSED(fd), Script *script)
 {
        script->id.us = 1;
-       SCRIPT_SET_NULL(script)
+       SCRIPT_SET_NULL(script);
 }
 
 
@@ -1709,11 +1724,11 @@ static void lib_link_ipo(FileData *fd, Main *main)
        Ipo *ipo;
 
        ipo= main->ipo.first;
-       while(ipo) {
-               if(ipo->id.flag & LIB_NEEDLINK) {
+       while (ipo) {
+               if (ipo->id.flag & LIB_NEEDLINK) {
                        IpoCurve *icu;
-                       for(icu= ipo->curve.first; icu; icu= icu->next) {
-                               if(icu->driver)
+                       for (icu= ipo->curve.first; icu; icu= icu->next) {
+                               if (icu->driver)
                                        icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
                        }
                        ipo->id.flag -= LIB_NEEDLINK;
@@ -1729,7 +1744,7 @@ static void direct_link_ipo(FileData *fd, Ipo *ipo)
 
        link_list(fd, &(ipo->curve));
        icu= ipo->curve.first;
-       while(icu) {
+       while (icu) {
                icu->bezt= newdataadr(fd, icu->bezt);
                icu->bp= newdataadr(fd, icu->bp);
                icu->driver= newdataadr(fd, icu->driver);
@@ -1743,11 +1758,11 @@ static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
        bActionStrip *strip;
        bActionModifier *amod;
        
-       for (strip=striplist->first; strip; strip=strip->next){
+       for (strip=striplist->first; strip; strip=strip->next) {
                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)
+               for (amod= strip->modifiers.first; amod; amod= amod->next)
                        amod->ob= newlibadr(fd, id->lib, amod->ob);
        }
 }
@@ -1759,7 +1774,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);
 }
 
@@ -1768,7 +1783,7 @@ static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbas
 {
        bConstraintChannel *chan;
 
-       for (chan=chanbase->first; chan; chan=chan->next){
+       for (chan=chanbase->first; chan; chan=chan->next) {
                chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
        }
 }
@@ -1843,9 +1858,9 @@ static void direct_link_fmodifiers(FileData *fd, ListBase *list)
                                
                                data->coefficients= newdataadr(fd, data->coefficients);
 
-                               if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
+                               if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
                                        unsigned int a;
-                                       for(a = 0; a < data->arraysize; a++)
+                                       for (a = 0; a < data->arraysize; a++)
                                                SWITCH_INT(data->coefficients[a]);
                                }
                        }
@@ -2134,11 +2149,11 @@ static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
 {
        bNode *node;
        
-       if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
+       if (ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
        
        ntree->gpd= newlibadr_us(fd, id->lib, ntree->gpd);
        
-       for(node= ntree->nodes.first; node; node= node->next)
+       for (node= ntree->nodes.first; node; node= node->next)
                node->id= newlibadr_us(fd, id->lib, node->id);
 }
 
@@ -2148,8 +2163,8 @@ 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) {
-               if(ntree->id.flag & LIB_NEEDLINK) {
+       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);
                }
@@ -2283,7 +2298,7 @@ static void lib_verify_nodetree(Main *main, int UNUSED(open))
        bNodeTreeType *ntreetype;
 
        /* this crashes blender on undo/redo
-               if(open==1) {
+               if (open==1) {
                        reinit_nodesystem();
                }*/
        
@@ -2293,7 +2308,7 @@ static void lib_verify_nodetree(Main *main, int UNUSED(open))
                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);
        
        {
@@ -2303,7 +2318,7 @@ static void lib_verify_nodetree(Main *main, int UNUSED(open))
                 * 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);
@@ -2319,18 +2334,18 @@ static void lib_verify_nodetree(Main *main, int UNUSED(open))
                        }
                }
                
-               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);
 
@@ -2366,8 +2381,8 @@ static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
        direct_link_animdata(fd, ntree->adt);
        
        link_list(fd, &ntree->nodes);
-       for(node= ntree->nodes.first; node; node= node->next) {
-               if(node->type == NODE_DYNAMIC) {
+       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);
                }
@@ -2377,25 +2392,26 @@ static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
                link_list(fd, &node->inputs);
                link_list(fd, &node->outputs);
                
-               if(node->type == CMP_NODE_MOVIEDISTORTION) {
+               if (node->type == CMP_NODE_MOVIEDISTORTION) {
                        node->storage= newmclipadr(fd, node->storage);
-               } else
+               }
+               else
                        node->storage= newdataadr(fd, node->storage);
 
-               if(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))
+                       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))
+                       else if (ntree->type==NTREE_COMPOSIT) {
+                               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))
+                               else if (ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
                                        ((ImageUser *)node->storage)->ok= 1;
                        }
-                       else if( ntree->type==NTREE_TEXTURE) {
-                               if(node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME)
+                       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)
+                               else if (node->type==TEX_NODE_IMAGE)
                                        ((ImageUser *)node->storage)->ok= 1;
                        }
                }
@@ -2407,22 +2423,22 @@ 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;
                
-               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) {
+       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);
@@ -2455,7 +2471,7 @@ 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) {
+               if (con->data==NULL) {
                        con->type= CONSTRAINT_TYPE_NULL;
                }
                /* own ipo, all constraints have it */
@@ -2504,7 +2520,7 @@ static void direct_link_constraints(FileData *fd, ListBase *lb)
                        case CONSTRAINT_TYPE_CHILDOF:
                        {
                                /* XXX version patch, in older code this flag wasn't always set, and is inherent to type */
-                               if(con->ownspace == CONSTRAINT_SPACE_POSE)
+                               if (con->ownspace == CONSTRAINT_SPACE_POSE)
                                        con->flag |= CONSTRAINT_SPACEONCE;
                        }
                                break;
@@ -2525,13 +2541,13 @@ static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
        /* always rebuild to match proxy or lib changes */
        rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
 
-       if(ob->proxy) {
+       if (ob->proxy) {
                /* sync proxy layer */
-               if(pose->proxy_layer)
+               if (pose->proxy_layer)
                        arm->layer = pose->proxy_layer;
                
                /* sync proxy active bone */
-               if(pose->proxy_act_bone[0]) {
+               if (pose->proxy_act_bone[0]) {
                        Bone *bone = get_named_bone(arm, pose->proxy_act_bone);
                        if (bone)
                                arm->act_bone = bone;
@@ -2545,16 +2561,16 @@ static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
                pchan->bone= get_named_bone(arm, pchan->name);
                
                pchan->custom= newlibadr_us(fd, arm->id.lib, pchan->custom);
-               if(pchan->bone==NULL)
+               if (pchan->bone==NULL)
                        rebuild= 1;
-               else if(ob->id.lib==NULL && arm->id.lib) {
+               else if (ob->id.lib==NULL && arm->id.lib) {
                        /* local pose selection copied to armature, bit hackish */
                        pchan->bone->flag &= ~BONE_SELECTED;
                        pchan->bone->flag |= pchan->selectflag;
                }
        }
        
-       if(rebuild) {
+       if (rebuild) {
                ob->recalc= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
                pose->flag |= POSE_RECALC;
        }
@@ -2566,8 +2582,8 @@ static void lib_link_armature(FileData *fd, Main *main)
 
        arm= main->armature.first;
 
-       while(arm) {
-               if(arm->id.flag & LIB_NEEDLINK) {
+       while (arm) {
+               if (arm->id.flag & LIB_NEEDLINK) {
                        if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
                        arm->id.flag -= LIB_NEEDLINK;
                }
@@ -2581,14 +2597,14 @@ static void direct_link_bones(FileData *fd, Bone* bone)
 
        bone->parent= newdataadr(fd, bone->parent);
        bone->prop= newdataadr(fd, bone->prop);
-       if(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)
+       for (child=bone->childbase.first; child; child=child->next)
                direct_link_bones(fd, child);
 }
 
@@ -2620,8 +2636,8 @@ static void lib_link_camera(FileData *fd, Main *main)
        Camera *ca;
 
        ca= main->camera.first;
-       while(ca) {
-               if(ca->id.flag & LIB_NEEDLINK) {
+       while (ca) {
+               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
@@ -2650,13 +2666,13 @@ static void lib_link_lamp(FileData *fd, Main *main)
        int a;
 
        la= main->lamp.first;
-       while(la) {
-               if(la->id.flag & LIB_NEEDLINK) {
+       while (la) {
+               if (la->id.flag & LIB_NEEDLINK) {
                        if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
                        
-                       for(a=0; a<MAX_MTEX; a++) {
+                       for (a=0; a<MAX_MTEX; a++) {
                                mtex= la->mtex[a];
-                               if(mtex) {
+                               if (mtex) {
                                        mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
                                        mtex->object= newlibadr(fd, la->id.lib, mtex->object);
                                }
@@ -2664,7 +2680,7 @@ static void lib_link_lamp(FileData *fd, Main *main)
                        
                        la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
 
-                       if(la->nodetree)
+                       if (la->nodetree)
                                lib_link_ntree(fd, &la->id, la->nodetree);
                        
                        la->id.flag -= LIB_NEEDLINK;
@@ -2680,16 +2696,16 @@ static void direct_link_lamp(FileData *fd, Lamp *la)
        la->adt= newdataadr(fd, la->adt);
        direct_link_animdata(fd, la->adt);
 
-       for(a=0; a<MAX_MTEX; a++) {
+       for (a=0; a<MAX_MTEX; a++) {
                la->mtex[a]= newdataadr(fd, la->mtex[a]);
        }
        
        la->curfalloff= newdataadr(fd, la->curfalloff);
-       if(la->curfalloff)
+       if (la->curfalloff)
                direct_link_curvemapping(fd, la->curfalloff);
 
        la->nodetree= newdataadr(fd, la->nodetree);
-       if(la->nodetree)
+       if (la->nodetree)
                direct_link_nodetree(fd, la->nodetree);
        
        la->preview = direct_link_preview_image(fd, la->preview);
@@ -2702,9 +2718,19 @@ static void lib_link_key(FileData *fd, Main *main)
        Key *key;
 
        key= main->key.first;
-       while(key) {
-               if(key->id.flag & LIB_NEEDLINK) {
-                       if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
+       while (key) {
+               /*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);
@@ -2723,19 +2749,19 @@ static void switch_endian_keyblock(Key *key, KeyBlock *kb)
        elemsize= key->elemsize;
        data= kb->data;
 
-       for(a=0; a<kb->totelem; a++) {
+       for (a=0; a<kb->totelem; a++) {
 
                cp= key->elemstr;
                poin= data;
 
-               while( cp[0] ) {        /* cp[0]==amount */
+               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--) {
+                               while (b--) {
                                        SWITCH_INT((*poin));
                                        poin+= 4;
                                }
@@ -2761,11 +2787,11 @@ static void direct_link_key(FileData *fd, Key *key)
        key->refkey= newdataadr(fd, key->refkey);
 
        kb= key->block.first;
-       while(kb) {
+       while (kb) {
 
                kb->data= newdataadr(fd, kb->data);
                
-               if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
+               if (fd->flags & FD_FLAGS_SWITCH_ENDIAN)
                        switch_endian_keyblock(key, kb);
 
                kb= kb->next;
@@ -2780,11 +2806,11 @@ static void lib_link_mball(FileData *fd, Main *main)
        int a;
 
        mb= main->mball.first;
-       while(mb) {
-               if(mb->id.flag & LIB_NEEDLINK) {
+       while (mb) {
+               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]);
+                       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
 
@@ -2820,21 +2846,21 @@ static void lib_link_world(FileData *fd, Main *main)
        int a;
 
        wrld= main->world.first;
-       while(wrld) {
-               if(wrld->id.flag & LIB_NEEDLINK) {
+       while (wrld) {
+               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
                        
-                       for(a=0; a<MAX_MTEX; a++) {
+                       for (a=0; a<MAX_MTEX; a++) {
                                mtex= wrld->mtex[a];
-                               if(mtex) {
+                               if (mtex) {
                                        mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
                                        mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
                                }
                        }
 
-                       if(wrld->nodetree)
+                       if (wrld->nodetree)
                                lib_link_ntree(fd, &wrld->id, wrld->nodetree);
                        
                        wrld->id.flag -= LIB_NEEDLINK;
@@ -2850,12 +2876,12 @@ static void direct_link_world(FileData *fd, World *wrld)
        wrld->adt= newdataadr(fd, wrld->adt);
        direct_link_animdata(fd, wrld->adt);
 
-       for(a=0; a<MAX_MTEX; a++) {
+       for (a=0; a<MAX_MTEX; a++) {
                wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
        }
 
        wrld->nodetree= newdataadr(fd, wrld->nodetree);
-       if(wrld->nodetree)
+       if (wrld->nodetree)
                direct_link_nodetree(fd, wrld->nodetree);
 
        wrld->preview = direct_link_preview_image(fd, wrld->preview);
@@ -2869,8 +2895,8 @@ static void lib_link_vfont(FileData *UNUSED(fd), Main *main)
        VFont *vf;
 
        vf= main->vfont.first;
-       while(vf) {
-               if(vf->id.flag & LIB_NEEDLINK) {
+       while (vf) {
+               if (vf->id.flag & LIB_NEEDLINK) {
                        vf->id.flag -= LIB_NEEDLINK;
                }
                vf= vf->id.next;
@@ -2890,8 +2916,8 @@ static void lib_link_text(FileData *UNUSED(fd), Main *main)
        Text *text;
 
        text= main->text.first;
-       while(text) {
-               if(text->id.flag & LIB_NEEDLINK) {
+       while (text) {
+               if (text->id.flag & LIB_NEEDLINK) {
                        text->id.flag -= LIB_NEEDLINK;
                }
                text= text->id.next;
@@ -2911,9 +2937,10 @@ static void direct_link_text(FileData *fd, Text *text)
        text->compiled= NULL;
 
 /*
-       if(text->flags & TXT_ISEXT) {
+       if (text->flags & TXT_ISEXT) {
                reopen_text(text);
-       } else {
+               }
+               else {
 */
 
        link_list(fd, &text->lines);
@@ -2923,7 +2950,7 @@ static void direct_link_text(FileData *fd, Text *text)
        text->sell= newdataadr(fd, text->sell);
 
        ln= text->lines.first;
-       while(ln) {
+       while (ln) {
                ln->line= newdataadr(fd, ln->line);
                ln->format= NULL;
                
@@ -2948,7 +2975,7 @@ static void lib_link_image(FileData *fd, Main *main)
 
        ima= main->image.first;
        while (ima) {
-               if(ima->id.flag & LIB_NEEDLINK) {
+               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;
@@ -2961,12 +2988,12 @@ 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;
-       while(ln) {
+       while (ln) {
                ln->next= newimaadr(fd, ln->next);
                ln->prev= prev;
                prev= ln;
@@ -2978,13 +3005,13 @@ static void link_ibuf_list(FileData *fd, ListBase *lb)
 static void direct_link_image(FileData *fd, Image *ima)
 {
        /* for undo system, pointers could be restored */
-       if(fd->imamap)
+       if (fd->imamap)
                link_ibuf_list(fd, &ima->ibufs);
        else
                ima->ibufs.first= ima->ibufs.last= NULL;
        
        /* if not restored, we keep the binded opengl index */
-       if(ima->ibufs.first==NULL) {
+       if (ima->ibufs.first==NULL) {
                ima->bindcode= 0;
                ima->gputexture= NULL;
        }
@@ -2994,10 +3021,10 @@ static void direct_link_image(FileData *fd, Image *ima)
        ima->repbind= NULL;
        
        /* undo system, try to restore render buffers */
-       if(fd->imamap) {
+       if (fd->imamap) {
                int a;
                
-               for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
+               for (a=0; a<IMA_MAX_RENDER_SLOT; a++)
                        ima->renders[a]= newimaadr(fd, ima->renders[a]);
        }
        else {
@@ -3019,11 +3046,11 @@ static void lib_link_curve(FileData *fd, Main *main)
        int a;
 
        cu= main->curve.first;
-       while(cu) {
-               if(cu->id.flag & LIB_NEEDLINK) {
-                       if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
+       while (cu) {
+               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]);
+                       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);
@@ -3047,15 +3074,15 @@ static void switch_endian_knots(Nurb *nu)
 {
        int len;
 
-       if(nu->knotsu) {
+       if (nu->knotsu) {
                len= KNOTSU(nu);
-               while(len--) {
+               while (len--) {
                        SWITCH_INT(nu->knotsu[len]);
                }
        }
-       if(nu->knotsv) {
+       if (nu->knotsv) {
                len= KNOTSV(nu);
-               while(len--) {
+               while (len--) {
                        SWITCH_INT(nu->knotsv[len]);
                }
        }
@@ -3075,7 +3102,7 @@ static void direct_link_curve(FileData *fd, Curve *cu)
        cu->strinfo= newdataadr(fd, cu->strinfo);       
        cu->tb= newdataadr(fd, cu->tb);
 
-       if(cu->vfont == NULL) link_list(fd, &(cu->nurb));
+       if (cu->vfont == NULL) link_list(fd, &(cu->nurb));
        else {
                cu->nurb.first=cu->nurb.last= NULL;
 
@@ -3084,7 +3111,8 @@ static void direct_link_curve(FileData *fd, Curve *cu)
                        memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
                        MEM_freeN(cu->tb);
                        cu->tb= tb;                     
-               } else {
+               }
+               else {
                        cu->totbox = 1;
                        cu->actbox = 1;
                        cu->tb = tb;
@@ -3101,14 +3129,14 @@ static void direct_link_curve(FileData *fd, Curve *cu)
        cu->editfont= NULL;
        
        nu= cu->nurb.first;
-       while(nu) {
+       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);
                if (cu->vfont == NULL) nu->charidx= nu->mat_nr;
 
-               if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
+               if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
                        switch_endian_knots(nu);
                }
 
@@ -3124,20 +3152,20 @@ static void lib_link_texture(FileData *fd, Main *main)
        Tex *tex;
 
        tex= main->tex.first;
-       while(tex) {
-               if(tex->id.flag & LIB_NEEDLINK) {
-                       if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
+       while (tex) {
+               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);
-                       if(tex->pd)
+                       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);
+                       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)
+                       if (tex->nodetree)
                                lib_link_ntree(fd, &tex->id, tex->nodetree);
                        
                        tex->id.flag -= LIB_NEEDLINK;
@@ -3152,7 +3180,7 @@ static void direct_link_texture(FileData *fd, Tex *tex)
        direct_link_animdata(fd, tex->adt);
        
        tex->plugin= newdataadr(fd, tex->plugin);
-       if(tex->plugin) {
+       if (tex->plugin) {
                tex->plugin->handle= NULL;
                open_plugin_tex(tex->plugin);
                /* initialize data for this instance, if an initialization
@@ -3163,34 +3191,35 @@ static void direct_link_texture(FileData *fd, Tex *tex)
        }
        tex->coba= newdataadr(fd, tex->coba);
        tex->env= newdataadr(fd, tex->env);
-       if(tex->env) {
+       if (tex->env) {
                tex->env->ima= NULL;
                memset(tex->env->cube, 0, 6*sizeof(void *));
                tex->env->ok= 0;
        }
        tex->pd= newdataadr(fd, tex->pd);
-       if(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);
-               if(tex->pd->falloff_curve) {
+               if (tex->pd->falloff_curve) {
                        direct_link_curvemapping(fd, tex->pd->falloff_curve);
                }
        }
        
        tex->vd= newdataadr(fd, tex->vd);
-       if(tex->vd) {
+       if (tex->vd) {
                tex->vd->dataset = NULL;
                tex->vd->ok = 0;
-       } else {
-               if(tex->type == TEX_VOXELDATA)
+       }
+       else {
+               if (tex->type == TEX_VOXELDATA)
                        tex->vd= MEM_callocN(sizeof(VoxelData), "direct_link_texture VoxelData");
        }
        
        tex->ot= newdataadr(fd, tex->ot);
        
        tex->nodetree= newdataadr(fd, tex->nodetree);
-       if(tex->nodetree)
+       if (tex->nodetree)
                direct_link_nodetree(fd, tex->nodetree);
        
        tex->preview = direct_link_preview_image(fd, tex->preview);
@@ -3209,9 +3238,9 @@ static void lib_link_material(FileData *fd, Main *main)
        int a;
 
        ma= main->mat.first;
-       while(ma) {
-               if(ma->id.flag & LIB_NEEDLINK) {
-                       if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
+       while (ma) {
+               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.*/
@@ -3220,15 +3249,15 @@ static void lib_link_material(FileData *fd, Main *main)
                        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++) {
+                       for (a=0; a<MAX_MTEX; a++) {
                                mtex= ma->mtex[a];
-                               if(mtex) {
+                               if (mtex) {
                                        mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
                                        mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
                                }
                        }
                        
-                       if(ma->nodetree)
+                       if (ma->nodetree)
                                lib_link_ntree(fd, &ma->id, ma->nodetree);
                        
                        ma->id.flag -= LIB_NEEDLINK;
@@ -3244,7 +3273,7 @@ static void direct_link_material(FileData *fd, Material *ma)
        ma->adt= newdataadr(fd, ma->adt);
        direct_link_animdata(fd, ma->adt);
        
-       for(a=0; a<MAX_MTEX; a++) {
+       for (a=0; a<MAX_MTEX; a++) {
                ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
        }
 
@@ -3252,7 +3281,7 @@ static void direct_link_material(FileData *fd, Material *ma)
        ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
        
        ma->nodetree= newdataadr(fd, ma->nodetree);
-       if(ma->nodetree)
+       if (ma->nodetree)
                direct_link_nodetree(fd, ma->nodetree);
 
        ma->preview = direct_link_preview_image(fd, ma->preview);
@@ -3273,7 +3302,7 @@ static const char *ptcache_data_struct[] = {
 };
 static void direct_link_pointcache(FileData *fd, PointCache *cache)
 {
-       if((cache->flag & PTCACHE_DISK_CACHE)==0) {
+       if ((cache->flag & PTCACHE_DISK_CACHE)==0) {
                PTCacheMem *pm;
                PTCacheExtra *extra;
                int i;
@@ -3282,23 +3311,23 @@ static void direct_link_pointcache(FileData *fd, PointCache *cache)
 
                pm = cache->mem_cache.first;
 
-               for(; pm; pm=pm->next) {
-                       for(i=0; i<BPHYS_TOT_DATA; i++) {
+               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)) {
+                               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];
                                        
-                                       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)
+                       for (extra=pm->extradata.first; extra; extra=extra->next)
                                extra->data = newdataadr(fd, extra->data);
                }
        }
@@ -3314,12 +3343,12 @@ static void direct_link_pointcache(FileData *fd, PointCache *cache)
 
 static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointCache **ocache, int force_disk)
 {
-       if(ptcaches->first) {
+       if (ptcaches->first) {
                PointCache *cache= NULL;
                link_list(fd, ptcaches);
-               for(cache=ptcaches->first; cache; cache=cache->next) {
+               for (cache=ptcaches->first; cache; cache=cache->next) {
                        direct_link_pointcache(fd, cache);
-                       if(force_disk) {
+                       if (force_disk) {
                                cache->flag |= PTCACHE_DISK_CACHE;
                                cache->step = 1;
                        }
@@ -3327,11 +3356,11 @@ static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointC
 
                *ocache = newdataadr(fd, *ocache);
        }
-       else if(*ocache) {
+       else if (*ocache) {
                /* old "single" caches need to be linked too */
                *ocache = newdataadr(fd, *ocache);
                direct_link_pointcache(fd, *ocache);
-               if(force_disk) {
+               if (force_disk) {
                        (*ocache)->flag |= PTCACHE_DISK_CACHE;
                        (*ocache)->step = 1;
                }
@@ -3342,7 +3371,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)
+       if (pd && pd->tex)
                pd->tex=newlibadr_us(fd, id->lib, pd->tex);
 }
 
@@ -3354,8 +3383,8 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
        int a;
 
        part= main->particle.first;
-       while(part) {
-               if(part->id.flag & LIB_NEEDLINK) {
+       while (part) {
+               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
                        
@@ -3367,37 +3396,37 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
                        lib_link_partdeflect(fd, &part->id, part->pd);
                        lib_link_partdeflect(fd, &part->id, part->pd2);
 
-                       if(part->effector_weights)
+                       if (part->effector_weights)
                                part->effector_weights->group = newlibadr(fd, part->id.lib, part->effector_weights->group);
 
-                       if(part->dupliweights.first && part->dup_group) {
+                       if (part->dupliweights.first && part->dup_group) {
                                int index_ok = 0;
                                /* check for old files without indices (all indexes 0) */
                                dw = part->dupliweights.first;
-                               if(part->dupliweights.first == part->dupliweights.last) {
+                               if (part->dupliweights.first == part->dupliweights.last) {
                                        /* special case for only one object in the group */
                                        index_ok = 1;
                                }
                                else { 
-                                       for(; dw; dw=dw->next) {
-                                               if(dw->index > 0) {
+                                       for (; dw; dw=dw->next) {
+                                               if (dw->index > 0) {
                                                        index_ok = 1;
                                                        break;
                                                }
                                        }
                                }
 
-                               if(index_ok) {
+                               if (index_ok) {
                                        /* if we have indexes, let's use them */
                                        dw = part->dupliweights.first;
-                                       for(; dw; dw=dw->next) {
+                                       for (; dw; dw=dw->next) {
                                                GroupObject *go = (GroupObject *)BLI_findlink(&part->dup_group->gobject, dw->index);
                                                dw->ob = go ? go->ob : NULL;
                                        }
                                }
                                else {
                                        /* otherwise try to get objects from own library (won't work on library linked groups) */
-                                       for(; dw; dw=dw->next)
+                                       for (; dw; dw=dw->next)
                                                dw->ob = newlibadr(fd, part->id.lib, dw->ob);
                                }
                        }
@@ -3405,12 +3434,12 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
                                part->dupliweights.first = part->dupliweights.last = NULL;
                        }
 
-                       if(part->boids) {
+                       if (part->boids) {
                                BoidState *state = part->boids->states.first;
                                BoidRule *rule;
-                               for(; state; state=state->next) {
+                               for (; state; state=state->next) {
                                        rule = state->rules.first;
-                               for(; rule; rule=rule->next)
+                               for (; rule; rule=rule->next)
                                        switch(rule->type) {
                                                case eBoidRuleType_Goal:
                                                case eBoidRuleType_Avoid:
@@ -3429,9 +3458,9 @@ 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) {
+                               if (mtex) {
                                        mtex->tex = newlibadr_us(fd, part->id.lib, mtex->tex);
                                        mtex->object = newlibadr(fd, part->id.lib, mtex->object);
                                }
@@ -3445,7 +3474,7 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
 
 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)
@@ -3460,7 +3489,7 @@ static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
        direct_link_partdeflect(part->pd2);
 
        part->effector_weights = newdataadr(fd, part->effector_weights);
-       if(!part->effector_weights)
+       if (!part->effector_weights)
                part->effector_weights = BKE_add_effector_weights(part->eff_group);
 
        link_list(fd, &part->dupliweights);
@@ -3468,17 +3497,17 @@ static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
        part->boids= newdataadr(fd, part->boids);
        part->fluid= newdataadr(fd, part->fluid);
 
-       if(part->boids) {
+       if (part->boids) {
                BoidState *state;
                link_list(fd, &part->boids->states);
                
-               for(state=part->boids->states.first; state; state=state->next) {
+               for (state=part->boids->states.first; state; state=state->next) {
                        link_list(fd, &state->rules);
                        link_list(fd, &state->conditions);
                        link_list(fd, &state->actions);
                }
        }
-       for(a=0; a<MAX_MTEX; a++) {
+       for (a=0; a<MAX_MTEX; a++) {
                part->mtex[a]= newdataadr(fd, part->mtex[a]);
        }
 }
@@ -3487,20 +3516,20 @@ static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase
 {
        ParticleSystem *psys, *psysnext;
 
-       for(psys=particles->first; psys; psys=psysnext){
+       for (psys=particles->first; psys; psys=psysnext) {
                psysnext= psys->next;
                
                psys->part = newlibadr_us(fd, id->lib, psys->part);
-               if(psys->part) {
+               if (psys->part) {
                        ParticleTarget *pt = psys->targets.first;
 
-                       for(; pt; pt=pt->next)
+                       for (; pt; pt=pt->next)
                                pt->ob=newlibadr(fd, id->lib, pt->ob);
 
                        psys->parent= newlibadr_us(fd, id->lib, psys->parent);
                        psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
 
-                       if(psys->clmd) {
+                       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;
@@ -3525,16 +3554,16 @@ static void direct_link_particlesystems(FileData *fd, ListBase *particles)
        ParticleData *pa;
        int a;
 
-       for(psys=particles->first; psys; psys=psys->next) {
+       for (psys=particles->first; psys; psys=psys->next) {
                psys->particles=newdataadr(fd,psys->particles);
                
-               if(psys->particles && psys->particles->hair){
-                       for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
+               if (psys->particles && psys->particles->hair) {
+                       for (a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
                                pa->hair=newdataadr(fd,pa->hair);
                }
                
-               if(psys->particles && psys->particles->keys){
-                       for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++) {
+               if (psys->particles && psys->particles->keys) {
+                       for (a=0,pa=psys->particles; a<psys->totpart; a++, pa++) {
                                pa->keys= NULL;
                                pa->totkey= 0;
                        }
@@ -3542,14 +3571,14 @@ static void direct_link_particlesystems(FileData *fd, ListBase *particles)
                        psys->flag &= ~PSYS_KEYED;
                }
 
-               if(psys->particles && psys->particles->boid) {
+               if (psys->particles && psys->particles->boid) {
                        pa = psys->particles;
                        pa->boid = newdataadr(fd, pa->boid);
-                       for(a=1,pa++; a<psys->totpart; a++, pa++)
+                       for (a=1,pa++; a<psys->totpart; a++, pa++)
                                pa->boid = (pa-1)->boid + 1;
                }
-               else if(psys->particles) {
-                       for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
+               else if (psys->particles) {
+                       for (a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
                                pa->boid = NULL;
                }
 
@@ -3572,7 +3601,7 @@ static void direct_link_particlesystems(FileData *fd, ListBase *particles)
                
                direct_link_pointcache_list(fd, &psys->ptcaches, &psys->pointcache, 0);
 
-               if(psys->clmd) {
+               if (psys->clmd) {
                        psys->clmd = newdataadr(fd, psys->clmd);
                        psys->clmd->clothObject = NULL;
                        
@@ -3580,8 +3609,8 @@ static void direct_link_particlesystems(FileData *fd, ListBase *particles)
                        psys->clmd->sim_parms->effector_weights = NULL;
                        psys->clmd->coll_parms= newdataadr(fd, psys->clmd->coll_parms);
                        
-                       if(psys->clmd->sim_parms) {
-                               if(psys->clmd->sim_parms->presets > 10)
+                       if (psys->clmd->sim_parms) {
+                               if (psys->clmd->sim_parms->presets > 10)
                                        psys->clmd->sim_parms->presets = 0;
                        }
 
@@ -3608,7 +3637,7 @@ static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
         * to each image it used. - z0r */
        for (i=0; i<totface; i++, tf++) {
                tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
-               if(tf->tpage && tf->tpage->id.us==0)
+               if (tf->tpage && tf->tpage->id.us==0)
                        tf->tpage->id.us= 1;
        }
 }
@@ -3616,22 +3645,42 @@ 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)
+               if (layer->type == CD_MTFACE)
                        lib_link_mtface(fd, me, layer->data, totface);
        }
 
 }
 
+static void lib_link_customdata_mtpoly(FileData *fd, Mesh *me, CustomData *pdata, int totface)
+{
+       int 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);
+                               if (tf->tpage && tf->tpage->id.us==0)
+                                       tf->tpage->id.us= 1;
+                       }
+               }
+       }
+}
+
 static void lib_link_mesh(FileData *fd, Main *main)
 {
        Mesh *me;
 
        me= main->mesh.first;
-       while(me) {
-               if(me->id.flag & LIB_NEEDLINK) {
+       while (me) {
+               if (me->id.flag & LIB_NEEDLINK) {
                        int i;
 
                        /*Link ID Properties -- and copy this comment EXACTLY for easy finding
@@ -3640,8 +3689,8 @@ static void lib_link_mesh(FileData *fd, Main *main)
                        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++) {
+                       if (me->mat) {
+                               for (i=0; i<me->totcol; i++) {
                                        me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
                                }
                        }
@@ -3652,10 +3701,39 @@ static void lib_link_mesh(FileData *fd, Main *main)
                        me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
 
                        lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
-                       if(me->mr && me->mr->levels.first)
+                       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
+                                  external CustomData works */
+                               Main *gmain = G.main;
+                               G.main = main;
+                               
+                               BKE_mesh_convert_mfaces_to_mpolys(me);
+
+                               G.main = gmain;
+                       }
+                       
+                       /*
+                        * Re-tessellate, even if the polys were just created from tessfaces, this
+                        * is important because it:
+                        *  - fill the CD_POLYINDEX layer
+                        *  - gives consistency of tessface between loading from a file and
+                        *    converting an edited BMesh back into a mesh (i.e. it replaces
+                        *    quad tessfaces in a loaded mesh immediately, instead of lazily
+                        *    waiting until edit mode has been entered/exited, making it easier
+                        *    to recognize problems that would otherwise only show up after edits).
+                        */
+#ifdef USE_TESSFACE_DEFAULT
+                       BKE_mesh_tessface_calc(me);
+#else
+                       BKE_mesh_tessface_clear(me);
+#endif
+
                        me->id.flag -= LIB_NEEDLINK;
                }
                me= me->id.next;
@@ -3674,10 +3752,17 @@ static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
        }
 
        for (i= count; i > 0; i--, mdverts++) {
-               if(mdverts->dw) {
-                       mdverts->dw= newdataadr(fd, mdverts->dw);
+               /*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");
+                       memcpy(dw_tmp, dw, dw_len);
+                       mdverts->dw= dw_tmp;
+                       MEM_freeN(dw);
                }
-               if (mdverts->dw == NULL) {
+               else {
+                       mdverts->dw= NULL;
                        mdverts->totweight= 0;
                }
        }
@@ -3685,23 +3770,33 @@ static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
 
 static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps, int external)
 {
-       if(mdisps) {
+       if (mdisps) {
                int i;
 
-               for(i = 0; i < count; ++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
+                                  overwritten with the correct value in
+                                  bm_corners_to_loops() */
+                               float gridsize = sqrtf(mdisps[i].totdisp);
+                               mdisps[i].level = (int)(logf(gridsize - 1.0f) / 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++) {
+                               for (x=0;x<mdisps[i].totdisp*3;x++) {
                                        SWITCH_INT(*tmpdisps);
                                        tmpdisps++;
                                }
                        }
-                       if(!external && !mdisps[i].disps)
+                       if (!external && !mdisps[i].disps)
                                mdisps[i].totdisp = 0;
                }
        }
@@ -3718,12 +3813,12 @@ static void direct_link_customdata(FileData *fd, CustomData *data, int count)
        while (i < data->totlayer) {
                CustomDataLayer *layer = &data->layers[i];
 
-               if(layer->flag & CD_FLAG_EXTERNAL)
+               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)
+                       if (layer->type == CD_MDISPS)
                                direct_link_mdisps(fd, count, layer->data, layer->flag & CD_FLAG_EXTERNAL);
                        i++;
                }
@@ -3740,12 +3835,18 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
        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);
        direct_link_animdata(fd, mesh->adt);
@@ -3757,7 +3858,9 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
        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!
@@ -3789,12 +3892,11 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
 
 
        mesh->bb= NULL;
-       mesh->mselect = NULL;
-       mesh->edit_mesh= NULL;
+       mesh->edit_btmesh= NULL;
        
        /* Multires data */
        mesh->mr= newdataadr(fd, mesh->mr);
-       if(mesh->mr) {
+       if (mesh->mr) {
                MultiresLevel *lvl;
                
                link_list(fd, &mesh->mr->levels);
@@ -3815,13 +3917,13 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
                   because some saved files either do not have a verts
                   array, or the verts array contains out-of-date
                   data. */
-               if(mesh->totvert == ((MultiresLevel*)mesh->mr->levels.last)->totvert) {
-                       if(mesh->mr->verts)
+               if (mesh->totvert == ((MultiresLevel*)mesh->mr->levels.last)->totvert) {
+                       if (mesh->mr->verts)
                                MEM_freeN(mesh->mr->verts);
                        mesh->mr->verts = MEM_dupallocN(mesh->mvert);
                }
                        
-               for(; lvl; lvl= lvl->next) {
+               for (; lvl; lvl= lvl->next) {
                        lvl->verts= newdataadr(fd, lvl->verts);
                        lvl->faces= newdataadr(fd, lvl->faces);
                        lvl->edges= newdataadr(fd, lvl->edges);
@@ -3831,12 +3933,12 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
 
        /* if multires is present but has no valid vertex data,
         * there's no way to recover it; silently remove multires */
-       if(mesh->mr && !mesh->mr->verts) {
+       if (mesh->mr && !mesh->mr->verts) {
                multires_free(mesh->mr);
                mesh->mr = NULL;
        }
        
-       if((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
+       if ((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
                TFace *tf= mesh->tface;
                int i;
 
@@ -3856,9 +3958,9 @@ static void lib_link_latt(FileData *fd, Main *main)
        Lattice *lt;
        
        lt= main->latt.first;
-       while(lt) {
-               if(lt->id.flag & LIB_NEEDLINK) {
-                       if(lt->adt) lib_link_animdata(fd, &lt->id, lt->adt);
+       while (lt) {
+               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);
@@ -3892,7 +3994,7 @@ static void lib_link_modifiers__linkModifiers(void *userData, Object *ob,
 
        *idpoin = newlibadr(fd, ob->id.lib, *idpoin);
        /* hardcoded bad exception; non-object modifier data gets user count (texture, displace) */
-       if(*idpoin && GS((*idpoin)->name)!=ID_OB)
+       if (*idpoin && GS((*idpoin)->name)!=ID_OB)
                (*idpoin)->us++;
 }
 static void lib_link_modifiers(FileData *fd, Object *ob)
@@ -3911,8 +4013,8 @@ static void lib_link_object(FileData *fd, Main *main)
        int warn=0, a;
 
        ob= main->object.first;
-       while(ob) {
-               if(ob->id.flag & LIB_NEEDLINK) {
+       while (ob) {
+               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);
                        
@@ -3927,9 +4029,9 @@ static void lib_link_object(FileData *fd, Main *main)
                        ob->dup_group= newlibadr_us(fd, ob->id.lib, ob->dup_group);
                        
                        ob->proxy= newlibadr_us(fd, ob->id.lib, ob->proxy);
-                       if(ob->proxy) {
+                       if (ob->proxy) {
                                /* paranoia check, actually a proxy_from pointer should never be written... */
-                               if(ob->proxy->id.lib==NULL) {
+                               if (ob->proxy->id.lib==NULL) {
                                        ob->proxy->proxy_from= NULL;
                                        ob->proxy= NULL;
                                        
@@ -3950,8 +4052,8 @@ static void lib_link_object(FileData *fd, Main *main)
                        poin= ob->data;
                        ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
 
-                       if(ob->data==NULL && poin!=NULL) {
-                               if(ob->id.lib)
+                       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);
@@ -3959,20 +4061,20 @@ static void lib_link_object(FileData *fd, Main *main)
                                ob->type= OB_EMPTY;
                                warn= 1;
 
-                               if(ob->pose) {
+                               if (ob->pose) {
                                        free_pose(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) {
+                       if (ob->data && ob->id.lib != ((ID *)ob->data)->lib) {
                                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);
                                }
@@ -3993,22 +4095,22 @@ static void lib_link_object(FileData *fd, Main *main)
                        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) {
+                       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++)
+                       while (sens) {
+                               for (a=0; a<sens->totlinks; a++)
                                        sens->links[a]= newglobadr(fd, sens->links[a]);
 
-                               if(sens->type==SENS_TOUCH) {
+                               if (sens->type==SENS_TOUCH) {
                                        bTouchSensor *ts= sens->data;
                                        ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
                                }
-                               else if(sens->type==SENS_MESSAGE) {
+                               else if (sens->type==SENS_MESSAGE) {
                                        bMessageSensor *ms= sens->data;
                                        ms->fromObject=
                                                newlibadr(fd, ob->id.lib, ms->fromObject);
@@ -4017,11 +4119,11 @@ static void lib_link_object(FileData *fd, Main *main)
                        }
 
                        cont= ob->controllers.first;
-                       while(cont) {
-                               for(a=0; a<cont->totlinks; a++)
+                       while (cont) {
+                               for (a=0; a<cont->totlinks; a++)
                                        cont->links[a]= newglobadr(fd, cont->links[a]);
 
-                               if(cont->type==CONT_PYTHON) {
+                               if (cont->type==CONT_PYTHON) {
                                        bPythonCont *pc= cont->data;
                                        pc->text= newlibadr(fd, ob->id.lib, pc->text);
                                }
@@ -4032,35 +4134,35 @@ static void lib_link_object(FileData *fd, Main *main)
                        }
 
                        act= ob->actuators.first;
-                       while(act) {
-                               if(act->type==ACT_SOUND) {
+                       while (act) {
+                               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) {
+                               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) {
+                               else if (act->type==ACT_ADD_OBJECT) {
                                        bAddObjectActuator *eoa= act->data;
-                                       if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
+                                       if (eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
                                }
-                               else if(act->type==ACT_OBJECT) {
+                               else if (act->type==ACT_OBJECT) {
                                        bObjectActuator *oa= act->data;
-                                       if(oa==NULL) {
+                                       if (oa==NULL) {
                                                init_actuator(act);
                                        }
                                        else {
                                                oa->reference= newlibadr(fd, ob->id.lib, oa->reference);
                                        }
                                }
-                               else if(act->type==ACT_EDIT_OBJECT) {
+                               else if (act->type==ACT_EDIT_OBJECT) {
                                        bEditObjectActuator *eoa= act->data;
-                                       if(eoa==NULL) {
+                                       if (eoa==NULL) {
                                                init_actuator(act);
                                        }
                                        else {
@@ -4068,44 +4170,44 @@ static void lib_link_object(FileData *fd, Main *main)
                                                eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
                                        }
                                }
-                               else if(act->type==ACT_SCENE) {
+                               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) {
+                               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) {
+                               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) {
+                               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) {
+                               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);
@@ -4116,24 +4218,24 @@ static void lib_link_object(FileData *fd, Main *main)
                        {
                                FluidsimModifierData *fluidmd = (FluidsimModifierData *)modifiers_findByType(ob, eModifierType_Fluidsim);
                                
-                               if(fluidmd && fluidmd->fss) 
+                               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)
+                       if (ob->pd)
                                lib_link_partdeflect(fd, &ob->id, ob->pd);
 
-                       if(ob->soft)
+                       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);
@@ -4142,7 +4244,7 @@ static void lib_link_object(FileData *fd, Main *main)
                ob= ob->id.next;
        }
 
-       if(warn) {
+       if (warn) {
                BKE_report(fd->reports, RPT_WARNING, "Warning in console");
        }
 }
@@ -4179,7 +4281,7 @@ static void direct_link_pose(FileData *fd, bPose *pose)
                pchan->iktree.first= pchan->iktree.last= NULL;
                pchan->siktree.first= pchan->siktree.last= NULL;
                
-               /* incase this value changes in future, clamp else we get undefined behavior */
+               /* 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;
@@ -4199,7 +4301,7 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                md->scene = NULL;
                
                /* if modifiers disappear, or for upward compatibility */
-               if(NULL==modifierType_getInfo(md->type))
+               if (NULL==modifierType_getInfo(md->type))
                        md->type= eModifierType_None;
                        
                if (md->type==eModifierType_Subsurf) {
@@ -4222,15 +4324,15 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
 
                        direct_link_pointcache_list(fd, &clmd->ptcaches, &clmd->point_cache, 0);
                        
-                       if(clmd->sim_parms) {
-                               if(clmd->sim_parms->presets > 10)
+                       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) {
+                               if (!clmd->sim_parms->effector_weights) {
                                        clmd->sim_parms->effector_weights = BKE_add_effector_weights(NULL);
                                }
                        }
@@ -4239,7 +4341,7 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                        FluidsimModifierData *fluidmd = (FluidsimModifierData*) md;
                        
                        fluidmd->fss= newdataadr(fd, fluidmd->fss);
-                       if(fluidmd->fss) {
+                       if (fluidmd->fss) {
                                fluidmd->fss->fmd= fluidmd;
                                fluidmd->fss->meshVelocities = NULL;
                        }
@@ -4247,7 +4349,7 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                else if (md->type==eModifierType_Smoke) {
                        SmokeModifierData *smd = (SmokeModifierData*) md;
 
-                       if(smd->type==MOD_SMOKE_TYPE_DOMAIN)
+                       if (smd->type==MOD_SMOKE_TYPE_DOMAIN)
                        {
                                smd->flow = NULL;
                                smd->coll = NULL;
@@ -4262,16 +4364,16 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                                smd->domain->tex_wt = NULL;
 
                                smd->domain->effector_weights = newdataadr(fd, smd->domain->effector_weights);
-                               if(!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]) {
+                               if (smd->domain->ptcaches[1].first || smd->domain->point_cache[1]) {
+                                       if (smd->domain->point_cache[1]) {
                                                PointCache *cache = newdataadr(fd, smd->domain->point_cache[1]);
-                                               if(cache->flag & PTCACHE_FAKE_SMOKE)
+                                               if (cache->flag & PTCACHE_FAKE_SMOKE)
                                                        ; /* Smoke was already saved in "new format" and this cache is a fake one. */
                                                else
                                                        printf("High resolution smoke cache not available due to pointcache update. Please reset the simulation.\n");
@@ -4282,20 +4384,18 @@ 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);
-                               if(smd->coll)
+                               if (smd->coll)
                                {
                                        smd->coll->points = NULL;
                                        smd->coll->numpoints = 0;
@@ -4308,7 +4408,7 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                else if (md->type==eModifierType_DynamicPaint) {
                        DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
 
-                       if(pmd->canvas)
+                       if (pmd->canvas)
                        {
                                pmd->canvas = newdataadr(fd, pmd->canvas);
                                pmd->canvas->pmd = pmd;
@@ -4324,12 +4424,12 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                                                surface->data = NULL;
                                                direct_link_pointcache_list(fd, &(surface->ptcaches), &(surface->pointcache), 1);
 
-                                               if(!(surface->effector_weights = newdataadr(fd, surface->effector_weights)))
+                                               if (!(surface->effector_weights = newdataadr(fd, surface->effector_weights)))
                                                        surface->effector_weights = BKE_add_effector_weights(NULL);
                                        }
                                }
                        }
-                       if(pmd->brush)
+                       if (pmd->brush)
                        {
                                pmd->brush = newdataadr(fd, pmd->brush);
                                pmd->brush->pmd = pmd;
@@ -4378,20 +4478,22 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                        HookModifierData *hmd = (HookModifierData*) md;
 
                        hmd->indexar= newdataadr(fd, hmd->indexar);
-                       if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
+                       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) {
+               }
+               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) {
+               }
+               else if (md->type==eModifierType_Explode) {
                        ExplodeModifierData *psmd = (ExplodeModifierData*) md;
 
                        psmd->facepa=NULL;
@@ -4409,25 +4511,25 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                        mmd->bindweights= newdataadr(fd, mmd->bindweights);
                        mmd->bindcos= newdataadr(fd, mmd->bindcos);
 
-                       if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
+                       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])
-                               if(mmd->bindcagecos)
-                                       for(a=0; a<mmd->totcagevert*3; a++)
-                                               SWITCH_INT(mmd->bindcagecos[a])
-                               if(mmd->dynverts)
-                                       for(a=0; a<mmd->totvert; a++)
-                                               SWITCH_INT(mmd->dynverts[a])
+                               if (mmd->bindoffsets)
+                                       for (a=0; a<mmd->totvert+1; a++)
+                                               SWITCH_INT(mmd->bindoffsets[a]);
+                               if (mmd->bindcagecos)
+                                       for (a=0; a<mmd->totcagevert*3; a++)
+                                               SWITCH_INT(mmd->bindcagecos[a]);
+                               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])
-                               if(mmd->bindcos)
-                                       for(a=0; a<mmd->totcagevert*3; a++)
-                                               SWITCH_INT(mmd->bindcos[a])
+                               if (mmd->bindweights)
+                                       for (a=0; a<mmd->totcagevert*mmd->totvert; a++)
+                                               SWITCH_INT(mmd->bindweights[a]);
+                               if (mmd->bindcos)
+                                       for (a=0; a<mmd->totcagevert*3; a++)
+                                               SWITCH_INT(mmd->bindcos[a]);
                        }
                }
                else if (md->type==eModifierType_Ocean) {
@@ -4440,14 +4542,14 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                        WarpModifierData *tmd = (WarpModifierData *) md;
 
                        tmd->curfalloff= newdataadr(fd, tmd->curfalloff);
-                       if(tmd->curfalloff)
+                       if (tmd->curfalloff)
                                direct_link_curvemapping(fd, tmd->curfalloff);
                }
                else if (md->type==eModifierType_WeightVGEdit) {
                        WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
 
                        wmd->cmap_curve = newdataadr(fd, wmd->cmap_curve);
-                       if(wmd->cmap_curve)
+                       if (wmd->cmap_curve)
                                direct_link_curvemapping(fd, wmd->cmap_curve);
                }
        }
@@ -4466,7 +4568,7 @@ static void direct_link_object(FileData *fd, Object *ob)
 
        /* 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)
+       if (fd->memfile)
                ob->mode &= ~(OB_MODE_EDIT|OB_MODE_PARTICLE_EDIT);
        
        ob->disp.first=ob->disp.last= NULL;
@@ -4496,11 +4598,11 @@ 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) {
+       while (paf) {
+               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);
@@ -4524,7 +4626,7 @@ static void direct_link_object(FileData *fd, Object *ob)
                        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);
@@ -4548,7 +4650,7 @@ static void direct_link_object(FileData *fd, Object *ob)
        ob->pd= newdataadr(fd, ob->pd);
        direct_link_partdeflect(ob->pd);
        ob->soft= newdataadr(fd, ob->soft);
-       if(ob->soft) {
+       if (ob->soft) {
                SoftBody *sb= ob->soft;         
                
                sb->bpoint= NULL;       // init pointers so it gets rebuilt nicely
@@ -4558,15 +4660,15 @@ static void direct_link_object(FileData *fd, Object *ob)
                /* still have to be loaded to be compatible with old files */
                sb->keys= newdataadr(fd, sb->keys);
                test_pointer_array(fd, (void **)&sb->keys);
-               if(sb->keys) {
+               if (sb->keys) {
                        int a;
-                       for(a=0; a<sb->totkey; 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)
+               if (!sb->effector_weights)
                        sb->effector_weights = BKE_add_effector_weights(NULL);
 
                direct_link_pointcache_list(fd, &sb->ptcaches, &sb->pointcache, 0);
@@ -4579,15 +4681,15 @@ static void direct_link_object(FileData *fd, Object *ob)
        
        link_list(fd, &ob->prop);
        prop= ob->prop.first;
-       while(prop) {
+       while (prop) {
                prop->poin= newdataadr(fd, prop->poin);
-               if(prop->poin==NULL) prop->poin= &prop->data;
+               if (prop->poin==NULL) prop->poin= &prop->data;
                prop= prop->next;
        }
 
        link_list(fd, &ob->sensors);
        sens= ob->sensors.first;
-       while(sens) {
+       while (sens) {
                sens->data= newdataadr(fd, sens->data);
                sens->links= newdataadr(fd, sens->links);
                test_pointer_array(fd, (void **)&sens->links);
@@ -4600,11 +4702,12 @@ static void direct_link_object(FileData *fd, Object *ob)
        if (ob->init_state) {
                /* if a known first state is specified, set it so that the game will start ok */
                ob->state = ob->init_state;
-       } else if (!ob->state) {
+       }
+       else if (!ob->state) {
                ob->state = 1;
        }
        cont= ob->controllers.first;
-       while(cont) {
+       while (cont) {
                cont->data= newdataadr(fd, cont->data);
                cont->links= newdataadr(fd, cont->links);
                test_pointer_array(fd, (void **)&cont->links);
@@ -4615,7 +4718,7 @@ static void direct_link_object(FileData *fd, Object *ob)
 
        link_glob_list(fd, &ob->actuators);
        act= ob->actuators.first;
-       while(act) {
+       while (act) {
                act->data= newdataadr(fd, act->data);
                act= act->next;
        }
@@ -4626,16 +4729,16 @@ static void direct_link_object(FileData *fd, Object *ob)
                HookModifierData *hmd = (HookModifierData*) modifier_new(eModifierType_Hook);
 
                hook->indexar= newdataadr(fd, hook->indexar);
-               if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
+               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 free'd, regardless of version.
+                        * and freed, regardless of version.
                         */
                copy_v3_v3(hmd->cent, hook->cent);
                hmd->falloff = hook->falloff;
@@ -4660,10 +4763,10 @@ static void direct_link_object(FileData *fd, Object *ob)
        ob->gpulamp.first= ob->gpulamp.last= NULL;
        link_list(fd, &ob->pc_ids);
 
-       /* incase this value changes in future, clamp else we get undefined behavior */
+       /* 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) {
+       if (ob->sculpt) {
                ob->sculpt= MEM_callocN(sizeof(SculptSession), "reload sculpt session");
        }
 }
@@ -4675,14 +4778,14 @@ static void composite_patch(bNodeTree *ntree, Scene *scene)
 {
        bNode *node;
        
-       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))
+       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;
 }
 
 static void link_paint(FileData *fd, Scene *sce, Paint *p)
 {
-       if(p) {
+       if (p) {
                p->brush= newlibadr_us(fd, sce->id.lib, p->brush);
                p->paint_cursor= NULL;
        }
@@ -4697,8 +4800,8 @@ static void lib_link_scene(FileData *fd, Main *main)
        TimeMarker *marker;
        
        sce= main->scene.first;
-       while(sce) {
-               if(sce->id.flag & LIB_NEEDLINK) {
+       while (sce) {
+               if (sce->id.flag & LIB_NEEDLINK) {
                        /*Link ID Properties -- and copy this comment EXACTLY for easy finding
                        of library blocks that implement this.*/
                        if (sce->id.properties) IDP_LibLinkProperty(sce->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
@@ -4718,35 +4821,39 @@ static void lib_link_scene(FileData *fd, Main *main)
                        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) {
+                       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);
                                
-                               if(base->object==NULL) {
+                               if (base->object==NULL) {
                                        BKE_reportf_wrap(fd->reports, RPT_ERROR,
                                                         "LIB ERROR: Object lost from scene:'%s\'\n",
                                                         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) {
+                               if (seq->scene) {
                                        seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
-                                       if(seq->scene) {
+                                       if (seq->scene) {
                                                seq->scene_sound = sound_scene_add_scene_sound_defaults(sce, seq);
                                        }
                                }
-                               if(seq->scene_camera) seq->scene_camera= newlibadr(fd, sce->id.lib, seq->scene_camera);
-                               if(seq->sound) {
+                               if (seq->clip) {
+                                       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->sound) {
                                        seq->scene_sound = NULL;
-                                       if(seq->type == SEQ_HD_SOUND)
+                                       if (seq->type == SEQ_HD_SOUND)
                                                seq->type = SEQ_SOUND;
                                        else
                                                seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
@@ -4760,8 +4867,8 @@ static void lib_link_scene(FileData *fd, Main *main)
                        SEQ_END
 
 #ifdef DURIAN_CAMERA_SWITCH
-                       for(marker= sce->markers.first; marker; marker= marker->next) {
-                               if(marker->camera) {
+                       for (marker= sce->markers.first; marker; marker= marker->next) {
+                               if (marker->camera) {
                                        marker->camera= newlibadr(fd, sce->id.lib, marker->camera);
                                }
                        }
@@ -4772,12 +4879,12 @@ static void lib_link_scene(FileData *fd, Main *main)
                        seq_update_muting(sce->ed);
                        seq_update_sound_bounds_all(sce);
                        
-                       if(sce->nodetree) {
+                       if (sce->nodetree) {
                                lib_link_ntree(fd, &sce->id, sce->nodetree);
                                composite_patch(sce->nodetree, sce);
                        }
                        
-                       for(srl= sce->r.layers.first; srl; srl= srl->next) {
+                       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);
                        }
@@ -4800,8 +4907,8 @@ static void link_recurs_seq(FileData *fd, ListBase *lb)
 
        link_list(fd, lb);
 
-       for(seq=lb->first; seq; seq=seq->next)
-               if(seq->seqbase.first)
+       for (seq=lb->first; seq; seq=seq->next)
+               if (seq->seqbase.first)
                        link_recurs_seq(fd, &seq->seqbase);
 }
 
@@ -4841,7 +4948,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
        sce->basact= newdataadr(fd, sce->basact);
        
        sce->toolsettings= newdataadr(fd, sce->toolsettings);
-       if(sce->toolsettings) {
+       if (sce->toolsettings) {
                direct_link_paint(fd, (Paint**)&sce->toolsettings->sculpt);
                direct_link_paint(fd, (Paint**)&sce->toolsettings->vpaint);
                direct_link_paint(fd, (Paint**)&sce->toolsettings->wpaint);
@@ -4851,7 +4958,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                sce->toolsettings->particle.paintcursor= NULL;
        }
 
-       if(sce->ed) {
+       if (sce->ed) {
                ListBase *old_seqbasep= &((Editing *)sce->ed)->seqbase;
                
                ed= sce->ed= newdataadr(fd, sce->ed);
@@ -4866,55 +4973,60 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                        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;
+                       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)
+                       if (seq->type & SEQ_EFFECT)
                                seq->flag |= SEQ_EFFECT_NOT_LOADED;
 
-                       if(seq->type == SEQ_SPEED) {
+                       if (seq->type == SEQ_SPEED) {
                                SpeedControlVars *s= seq->effectdata;
                                s->frameMap= NULL;
                        }
 
                        seq->strip= newdataadr(fd, seq->strip);
-                       if(seq->strip && seq->strip->done==0) {
+                       if (seq->strip && seq->strip->done==0) {
                                seq->strip->done= 1;
 
-                               if(seq->type == SEQ_IMAGE ||
+                               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);
-                               } else {
+                               }
+                               else {
                                        seq->strip->stripdata = NULL;
                                }
                                if (seq->flag & SEQ_USE_CROP) {
                                        seq->strip->crop = newdataadr(
                                                fd, seq->strip->crop);
-                               } else {
+                               }
+                               else {
                                        seq->strip->crop = NULL;
                                }
                                if (seq->flag & SEQ_USE_TRANSFORM) {
                                        seq->strip->transform = newdataadr(
                                                fd, seq->strip->transform);
-                               } else {
+                               }
+                               else {
                                        seq->strip->transform = NULL;
                                }
                                if (seq->flag & SEQ_USE_PROXY) {
                                        seq->strip->proxy = newdataadr(
                                                fd, seq->strip->proxy);
                                        seq->strip->proxy->anim = NULL;
-                               } else {
+                               }
+                               else {
                                        seq->strip->proxy = NULL;
                                }
                                if (seq->flag & SEQ_USE_COLOR_BALANCE) {
                                        seq->strip->color_balance = newdataadr(
                                                fd, seq->strip->color_balance);
-                               } else {
+                               }
+                               else {
                                        seq->strip->color_balance = NULL;
                                }
                                if (seq->strip->color_balance) {
@@ -4933,7 +5045,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                        offset= ((intptr_t)&(temp.seqbase)) - ((intptr_t)&temp);
                        
                        /* root pointer */
-                       if(ed->seqbasep == old_seqbasep) {
+                       if (ed->seqbasep == old_seqbasep) {
                                ed->seqbasep= &ed->seqbase;
                        }
                        else {
@@ -4942,22 +5054,22 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                                poin -= offset;
                                
                                poin= newdataadr(fd, poin);
-                               if(poin) ed->seqbasep= (ListBase *)(poin+offset);
+                               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) {
+                       for (ms= ed->metastack.first; ms; ms= ms->next) {
                                ms->parseq= newdataadr(fd, ms->parseq);
                                
-                               if(ms->oldbasep == old_seqbasep)
+                               if (ms->oldbasep == old_seqbasep)
                                        ms->oldbasep= &ed->seqbase;
                                else {
                                        poin= (char *)ms->oldbasep;
                                        poin -= offset;
                                        poin= newdataadr(fd, poin);
-                                       if(poin) ms->oldbasep= (ListBase *)(poin+offset);
+                                       if (poin) ms->oldbasep= (ListBase *)(poin+offset);
                                        else ms->oldbasep= &ed->seqbase;
                                }
                        }
@@ -4989,7 +5101,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
        link_list(fd, &(sce->r.layers));
 
        sce->nodetree= newdataadr(fd, sce->nodetree);
-       if(sce->nodetree)
+       if (sce->nodetree)
                direct_link_nodetree(fd, sce->nodetree);
 }
 
@@ -5002,7 +5114,7 @@ static void direct_link_windowmanager(FileData *fd, wmWindowManager *wm)
        wm->id.us= 1;
        link_list(fd, &(wm->windows));
        
-       for(win= wm->windows.first; win; win= win->next) {
+       for (win= wm->windows.first; win; win= win->next) {
                win->ghostwin= NULL;
                win->eventstate= NULL;
                win->curswin= NULL;
@@ -5044,9 +5156,9 @@ static void lib_link_windowmanager(FileData *fd, Main *main)
        wmWindowManager *wm;
        wmWindow *win;
        
-       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)
+       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);
 
                        wm->id.flag -= LIB_NEEDLINK;
@@ -5090,10 +5202,10 @@ static void direct_link_gpencil(FileData *fd, bGPdata *gpd)
 
 static void butspace_version_132(SpaceButs *buts)
 {
-       buts->v2d.tot.xmin= 0.0f;
-       buts->v2d.tot.ymin= 0.0f;
-       buts->v2d.tot.xmax= 1279.0f;
-       buts->v2d.tot.ymax= 228.0f;
+       buts->v2d.tot.xmin = 0.0f;
+       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;
@@ -5116,20 +5228,20 @@ static void lib_link_screen(FileData *fd, Main *main)
        bScreen *sc;
        ScrArea *sa;
 
-       for(sc= main->screen.first; sc; sc= sc->id.next) {
-               if(sc->id.flag & LIB_NEEDLINK) {
+       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 */
                        
                        sa= sc->areabase.first;
-                       while(sa) {
+                       while (sa) {
                                SpaceLink *sl;
                                
                                sa->full= newlibadr(fd, sc->id.lib, sa->full);
                                
                                for (sl= sa->spacedata.first; sl; sl= sl->next) {
-                                       if(sl->spacetype==SPACE_VIEW3D) {
+                                       if (sl->spacetype==SPACE_VIEW3D) {
                                                View3D *v3d= (View3D*) sl;
                                                BGpic *bgpic = NULL;
                                                
@@ -5137,21 +5249,21 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                v3d->ob_centre= newlibadr(fd, sc->id.lib, v3d->ob_centre);
                                                
                                                /* should be do_versions but not easy adding into the listbase */
-                                               if(v3d->bgpic) {
+                                               if (v3d->bgpic) {
                                                        v3d->bgpic= newlibadr(fd, sc->id.lib, v3d->bgpic);
                                                        BLI_addtail(&v3d->bgpicbase, bgpic);
                                                        v3d->bgpic= NULL;
                                                }
 
-                                               for(bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next) {
+                                               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) {
+                                               if (v3d->localvd) {
                                                        v3d->localvd->camera= newlibadr(fd, sc->id.lib, v3d->localvd->camera);
                                                }
                                        }
-                                       else if(sl->spacetype==SPACE_IPO) {
+                                       else if (sl->spacetype==SPACE_IPO) {
                                                SpaceIpo *sipo= (SpaceIpo *)sl;
                                                bDopeSheet *ads= sipo->ads;
                                                
@@ -5160,15 +5272,15 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                        ads->filter_grp= newlibadr(fd, sc->id.lib, ads->filter_grp);
                                                }
                                        }
-                                       else if(sl->spacetype==SPACE_BUTS) {
+                                       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)
+                                               if (main->versionfile<132)
                                                        butspace_version_132(sbuts);
                                        }
-                                       else if(sl->spacetype==SPACE_FILE) {
+                                       else if (sl->spacetype==SPACE_FILE) {
                                                SpaceFile *sfile= (SpaceFile *)sl;
                                                sfile->files= NULL;
                                                sfile->op= NULL;
@@ -5176,7 +5288,7 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                sfile->folders_prev= NULL;
                                                sfile->folders_next= NULL;
                                        }
-                                       else if(sl->spacetype==SPACE_ACTION) {
+                                       else if (sl->spacetype==SPACE_ACTION) {
                                                SpaceAction *saction= (SpaceAction *)sl;
                                                bDopeSheet *ads= &saction->ads;
                                                
@@ -5187,7 +5299,7 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                
                                                saction->action = newlibadr(fd, sc->id.lib, saction->action);
                                        }
-                                       else if(sl->spacetype==SPACE_IMAGE) {
+                                       else if (sl->spacetype==SPACE_IMAGE) {
                                                SpaceImage *sima= (SpaceImage *)sl;
 
                                                sima->image= newlibadr_us(fd, sc->id.lib, sima->image);
@@ -5197,7 +5309,7 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                 */
                                                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;
                                                
@@ -5206,25 +5318,25 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                        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) {
+                                       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);
                                                        if (scpt->script) {
-                                                               SCRIPT_SET_NULL(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;
@@ -5232,14 +5344,14 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                so->tree.first= so->tree.last= NULL;
                                                so->search_tse.id= newlibadr(fd, NULL, so->search_tse.id);
                                                
-                                               if(so->treestore) {
+                                               if (so->treestore) {
                                                        tselem= so->treestore->data;
-                                                       for(a=0; a<so->treestore->usedelem; a++, tselem++) {
+                                                       for (a=0; a<so->treestore->usedelem; a++, tselem++) {
                                                                tselem->id= newlibadr(fd, NULL, tselem->id);
                                                        }
                                                }
                                        }
-                                       else if(sl->spacetype==SPACE_NODE) {
+                                       else if (sl->spacetype==SPACE_NODE) {
                                                SpaceNode *snode= (SpaceNode *)sl;
                                                
                                                snode->id= newlibadr(fd, sc->id.lib, snode->id);
@@ -5248,16 +5360,16 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                if (ELEM3(snode->treetype, NTREE_COMPOSIT, NTREE_SHADER, NTREE_TEXTURE)) {
                                                        /* internal data, a bit patchy */
                                                        snode->nodetree= NULL;
-                                                       if(snode->id) {
-                                                               if(GS(snode->id->name)==ID_MA)
+                                                       if (snode->id) {
+                                                               if (GS(snode->id->name)==ID_MA)
                                                                        snode->nodetree= ((Material *)snode->id)->nodetree;
-                                                               else if(GS(snode->id->name)==ID_WO)
+                                                               else if (GS(snode->id->name)==ID_WO)
                                                                        snode->nodetree= ((World *)snode->id)->nodetree;
-                                                               else if(GS(snode->id->name)==ID_LA)
+                                                               else if (GS(snode->id->name)==ID_LA)
                                                                        snode->nodetree= ((Lamp *)snode->id)->nodetree;
-                                                               else if(GS(snode->id->name)==ID_SCE)
+                                                               else if (GS(snode->id->name)==ID_SCE)
                                                                        snode->nodetree= ((Scene *)snode->id)->nodetree;
-                                                               else if(GS(snode->id->name)==ID_TE)
+                                                               else if (GS(snode->id->name)==ID_TE)
                                                                        snode->nodetree= ((Tex *)snode->id)->nodetree;
                                                        }
                                                }
@@ -5267,7 +5379,7 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                
                                                snode->linkdrag.first = snode->linkdrag.last = NULL;
                                        }
-                                       else if(sl->spacetype==SPACE_CLIP) {
+                                       else if (sl->spacetype==SPACE_CLIP) {
                                                SpaceClip *sclip= (SpaceClip *)sl;
 
                                                sclip->clip= newlibadr_us(fd, sc->id.lib, sclip->clip);
@@ -5288,17 +5400,17 @@ static void lib_link_screen(FileData *fd, Main *main)
 static void *restore_pointer_by_name(Main *mainp, ID *id, int user)
 {
                
-       if(id) {
+       if (id) {
                ListBase *lb= which_libbase(mainp, GS(id->name));
                
-               if(lb) {        // there's still risk of checking corrupt mem (freed Ids in oops)
+               if (lb) {       // there's still risk of checking corrupt mem (freed Ids in oops)
                        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++;
+                       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++;
                                                break;
                                        }
                                }
@@ -5314,15 +5426,15 @@ static int lib_link_seq_clipboard_cb(Sequence *seq, void *arg_pt)
 {
        Main *newmain = (Main *)arg_pt;
 
-       if(seq->sound) {
+       if (seq->sound) {
                seq->sound = restore_pointer_by_name(newmain, (ID *)seq->sound, 0);
                seq->sound->id.us++;
        }
 
-       if(seq->scene)
+       if (seq->scene)
                seq->scene = restore_pointer_by_name(newmain, (ID *)seq->scene, 1);
 
-       if(seq->scene_camera)
+       if (seq->scene_camera)
                seq->scene_camera = restore_pointer_by_name(newmain, (ID *)seq->scene_camera, 1);
 
        return 1;
@@ -5345,11 +5457,11 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
        ScrArea *sa;
 
        /* first windowmanager */
-       for(wm= newmain->wm.first; wm; wm= wm->id.next) {
-               for(win= wm->windows.first; win; win= win->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);
                        
-                       if(win->screen==NULL)
+                       if (win->screen==NULL)
                                win->screen= curscreen;
 
                        win->screen->winid= win->winid;
@@ -5357,49 +5469,49 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
        }
        
        
-       for(sc= newmain->screen.first; sc; sc= sc->id.next) {
+       for (sc= newmain->screen.first; sc; sc= sc->id.next) {
                Scene *oldscene= sc->scene;
 
                sc->scene= restore_pointer_by_name(newmain, (ID *)sc->scene, 1);
-               if(sc->scene==NULL)
+               if (sc->scene==NULL)
                        sc->scene= curscene;
 
                /* keep cursor location through undo */
                copy_v3_v3(sc->scene->cursor, oldscene->cursor);
 
                sa= sc->areabase.first;
-               while(sa) {
+               while (sa) {
                        SpaceLink *sl;
 
                        for (sl= sa->spacedata.first; sl; sl= sl->next) {
-                               if(sl->spacetype==SPACE_VIEW3D) {
+                               if (sl->spacetype==SPACE_VIEW3D) {
                                        View3D *v3d= (View3D*) sl;
                                        BGpic *bgpic;
                                        ARegion *ar;
                                        
-                                       if(v3d->scenelock)
+                                       if (v3d->scenelock)
                                                v3d->camera= NULL; /* always get from scene */
                                        else
                                                v3d->camera= restore_pointer_by_name(newmain, (ID *)v3d->camera, 1);
-                                       if(v3d->camera==NULL)
+                                       if (v3d->camera==NULL)
                                                v3d->camera= sc->scene->camera;
                                        v3d->ob_centre= restore_pointer_by_name(newmain, (ID *)v3d->ob_centre, 1);
                                        
-                                       for(bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next) {
+                                       for (bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next) {
                                                bgpic->ima= restore_pointer_by_name(newmain, (ID *)bgpic->ima, 1);
                                                bgpic->clip= restore_pointer_by_name(newmain, (ID *)bgpic->clip, 1);
                                        }
-                                       if(v3d->localvd) {
+                                       if (v3d->localvd) {
                                                /*Base *base;*/
 
                                                v3d->localvd->camera= sc->scene->camera;
                                                
                                                /* localview can become invalid during undo/redo steps, so we exit it when no could be found */
                                                /* XXX  regionlocalview ?
-                                               for(base= sc->scene->base.first; base; base= base->next) {
-                                                       if(base->lay & v3d->lay) break;
+                                               for (base= sc->scene->base.first; base; base= base->next) {
+                                                       if (base->lay & v3d->lay) break;
                                                }
-                                               if(base==NULL) {
+                                               if (base==NULL) {
                                                        v3d->lay= v3d->localvd->lay;
                                                        v3d->layact= v3d->localvd->layact;
                                                        MEM_freeN(v3d->localvd); 
@@ -5407,22 +5519,22 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                                }
                                                */
                                        }
-                                       else if(v3d->scenelock) v3d->lay= sc->scene->lay;
+                                       else if (v3d->scenelock) v3d->lay= sc->scene->lay;
 
                                        /* not very nice, but could help */
-                                       if((v3d->layact & v3d->lay)==0) v3d->layact= v3d->lay;
+                                       if ((v3d->layact & v3d->lay)==0) v3d->layact= v3d->lay;
                                        
                                        /* free render engines for now */
-                                       for(ar= sa->regionbase.first; ar; ar= ar->next) {
+                                       for (ar= sa->regionbase.first; ar; ar= ar->next) {
                                                RegionView3D *rv3d= ar->regiondata;
 
-                                               if(rv3d && rv3d->render_engine) {
+                                               if (rv3d && rv3d->render_engine) {
                                                        RE_engine_free(rv3d->render_engine);
                                                        rv3d->render_engine= NULL;
                                                }
                                        }
                                }
-                               else if(sl->spacetype==SPACE_IPO) {
+                               else if (sl->spacetype==SPACE_IPO) {
                                        SpaceIpo *sipo= (SpaceIpo *)sl;
                                        bDopeSheet *ads= sipo->ads;
                                        
@@ -5433,17 +5545,17 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                                        ads->filter_grp= restore_pointer_by_name(newmain, (ID *)ads->filter_grp, 0);
                                        }
                                }
-                               else if(sl->spacetype==SPACE_BUTS) {
+                               else if (sl->spacetype==SPACE_BUTS) {
                                        SpaceButs *sbuts= (SpaceButs *)sl;
                                        sbuts->pinid = restore_pointer_by_name(newmain, sbuts->pinid, 0);
                                        //XXX if (sbuts->ri) sbuts->ri->curtile = 0;
                                }
-                               else if(sl->spacetype==SPACE_FILE) {
+                               else if (sl->spacetype==SPACE_FILE) {
                                        
                                        SpaceFile *sfile= (SpaceFile *)sl;
                                        sfile->op= NULL;
                                }
-                               else if(sl->spacetype==SPACE_ACTION) {
+                               else if (sl->spacetype==SPACE_ACTION) {
                                        SpaceAction *saction= (SpaceAction *)sl;
                                        
                                        saction->action = restore_pointer_by_name(newmain, (ID *)saction->action, 1);
@@ -5452,11 +5564,15 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                        if (saction->ads.filter_grp)
                                                saction->ads.filter_grp= restore_pointer_by_name(newmain, (ID *)saction->ads.filter_grp, 0);
                                }
-                               else if(sl->spacetype==SPACE_IMAGE) {
+                               else if (sl->spacetype==SPACE_IMAGE) {
                                        SpaceImage *sima= (SpaceImage *)sl;
 
                                        sima->image= restore_pointer_by_name(newmain, (ID *)sima->image, 1);
 
+                                       /* this will be freed, not worth attempting to find same scene,
+                                        * since it gets initialized later */
+                                       sima->iuser.scene = NULL;
+
                                        sima->scopes.waveform_1 = NULL;
                                        sima->scopes.waveform_2 = NULL;
                                        sima->scopes.waveform_3 = NULL;
@@ -5468,7 +5584,7 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                         */
                                        sima->gpd= restore_pointer_by_name(newmain, (ID *)sima->gpd, 1);
                                }
-                               else if(sl->spacetype==SPACE_NLA){
+                               else if (sl->spacetype==SPACE_NLA) {
                                        SpaceNla *snla= (SpaceNla *)sl;
                                        bDopeSheet *ads= snla->ads;
                                        
@@ -5479,37 +5595,37 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                                        ads->filter_grp= restore_pointer_by_name(newmain, (ID *)ads->filter_grp, 0);
                                        }
                                }
-                               else if(sl->spacetype==SPACE_TEXT) {
+                               else if (sl->spacetype==SPACE_TEXT) {
                                        SpaceText *st= (SpaceText *)sl;
 
                                        st->text= restore_pointer_by_name(newmain, (ID *)st->text, 1);
-                                       if(st->text==NULL) st->text= newmain->text.first;
+                                       if (st->text==NULL) st->text= newmain->text.first;
                                }
-                               else if(sl->spacetype==SPACE_SCRIPT) {
+                               else if (sl->spacetype==SPACE_SCRIPT) {
                                        SpaceScript *scpt= (SpaceScript *)sl;
                                        
                                        scpt->script= restore_pointer_by_name(newmain, (ID *)scpt->script, 1);
                                        
                                        /*sc->script = NULL; - 2.45 set to null, better re-run the script */
                                        if (scpt->script) {
-                                               SCRIPT_SET_NULL(scpt->script)
+                                               SCRIPT_SET_NULL(scpt->script);
                                        }
                                }
-                               else if(sl->spacetype==SPACE_OUTLINER) {
+                               else if (sl->spacetype==SPACE_OUTLINER) {
                                        SpaceOops *so= (SpaceOops *)sl;
                                        int a;
                                        
                                        so->search_tse.id= restore_pointer_by_name(newmain, so->search_tse.id, 0);
                                        
-                                       if(so->treestore) {
+                                       if (so->treestore) {
                                                TreeStore *ts= so->treestore;
                                                TreeStoreElem *tselem=ts->data;
-                                               for(a=0; a<ts->usedelem; a++, tselem++) {
+                                               for (a=0; a<ts->usedelem; a++, tselem++) {
                                                        tselem->id= restore_pointer_by_name(newmain, tselem->id, 0);
                                                }
                                        }
                                }
-                               else if(sl->spacetype==SPACE_NODE) {
+                               else if (sl->spacetype==SPACE_NODE) {
                                        SpaceNode *snode= (SpaceNode *)sl;
                                        
                                        snode->id= restore_pointer_by_name(newmain, snode->id, 1);
@@ -5517,12 +5633,12 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                        
                                        if (ELEM3(snode->treetype, NTREE_COMPOSIT, NTREE_SHADER, NTREE_TEXTURE)) {
                                                snode->nodetree= NULL;
-                                               if(snode->id) {
-                                                       if(GS(snode->id->name)==ID_MA)
+                                               if (snode->id) {
+                                                       if (GS(snode->id->name)==ID_MA)
                                                                snode->nodetree= ((Material *)snode->id)->nodetree;
-                                                       else if(GS(snode->id->name)==ID_SCE)
+                                                       else if (GS(snode->id->name)==ID_SCE)
                                                                snode->nodetree= ((Scene *)snode->id)->nodetree;
-                                                       else if(GS(snode->id->name)==ID_TE)
+                                                       else if (GS(snode->id->name)==ID_TE)
                                                                snode->nodetree= ((Tex *)snode->id)->nodetree;
                                                }
                                        }
@@ -5530,7 +5646,7 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                                snode->nodetree= restore_pointer_by_name(newmain, &snode->nodetree->id, 1);
                                        }
                                }
-                               else if(sl->spacetype==SPACE_CLIP) {
+                               else if (sl->spacetype==SPACE_CLIP) {
                                        SpaceClip *sclip= (SpaceClip *)sl;
 
                                        sclip->clip= restore_pointer_by_name(newmain, (ID *)sclip->clip, 1);
@@ -5552,7 +5668,7 @@ static void direct_link_region(FileData *fd, ARegion *ar, int spacetype)
 
        link_list(fd, &(ar->panels));
 
-       for(pa= ar->panels.first; pa; pa=pa->next) {
+       for (pa= ar->panels.first; pa; pa=pa->next) {
                pa->paneltab= newdataadr(fd, pa->paneltab);
                pa->runtime_flag= 0;
                pa->activedata= NULL;
@@ -5560,8 +5676,8 @@ static void direct_link_region(FileData *fd, ARegion *ar, int spacetype)
        }
        
        ar->regiondata= newdataadr(fd, ar->regiondata);
-       if(ar->regiondata) {
-               if(spacetype==SPACE_VIEW3D) {
+       if (ar->regiondata) {
+               if (spacetype==SPACE_VIEW3D) {
                        RegionView3D *rv3d= ar->regiondata;
                        
                        rv3d->localvd= newdataadr(fd, rv3d->localvd);
@@ -5594,13 +5710,13 @@ static void view3d_split_250(View3D *v3d, ListBase *regions)
 {
        ARegion *ar;
        
-       for(ar= regions->first; ar; ar= ar->next) {
-               if(ar->regiontype==RGN_TYPE_WINDOW && ar->regiondata==NULL) {
+       for (ar= regions->first; ar; ar= ar->next) {
+               if (ar->regiontype==RGN_TYPE_WINDOW && ar->regiondata==NULL) {
                        RegionView3D *rv3d;
                        
                        rv3d= ar->regiondata= MEM_callocN(sizeof(RegionView3D), "region v3d patch");
-                       rv3d->persp= v3d->persp;
-                       rv3d->view= v3d->view;
+                       rv3d->persp= (char)v3d->persp;
+                       rv3d->view= (char)v3d->view;
                        rv3d->dist= v3d->dist;
                        copy_v3_v3(rv3d->ofs, v3d->ofs);
                        copy_qt_qt(rv3d->viewquat, v3d->viewquat);
@@ -5608,7 +5724,7 @@ static void view3d_split_250(View3D *v3d, ListBase *regions)
        }
 
        /* this was not initialized correct always */
-       if(v3d->twtype == 0)
+       if (v3d->twtype == 0)
                v3d->twtype= V3D_MANIP_TRANSLATE;
 }
 
@@ -5630,31 +5746,31 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
        
        /* hacky patch... but people have been saving files with the verse-blender,
           causing the handler to keep running for ever, with no means to disable it */
-       for(a=0; a<SCREEN_MAXHANDLER; a+=2) {
-               if( sc->handler[a]==SCREEN_HANDLER_VERSE) {
+       for (a=0; a<SCREEN_MAXHANDLER; a+=2) {
+               if ( sc->handler[a]==SCREEN_HANDLER_VERSE) {
                        sc->handler[a]= 0;
                        break;
                }
        }
        
        /* edges */
-       for(se= sc->edgebase.first; se; se= se->next) {
+       for (se= sc->edgebase.first; se; se= se->next) {
                se->v1= newdataadr(fd, se->v1);
                se->v2= newdataadr(fd, se->v2);
-               if( (intptr_t)se->v1 > (intptr_t)se->v2) {
+               if ( (intptr_t)se->v1 > (intptr_t)se->v2) {
                        sv= se->v1;
                        se->v1= se->v2;
                        se->v2= sv;
                }
 
-               if(se->v1==NULL) {
+               if (se->v1==NULL) {
                        printf("error reading screen... file corrupt\n");
                        se->v1= se->v2;
                }
        }
 
        /* areas */
-       for(sa= sc->areabase.first; sa; sa= sa->next) {
+       for (sa= sc->areabase.first; sa; sa= sa->next) {
                SpaceLink *sl;
                ARegion *ar;
 
@@ -5664,24 +5780,24 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                sa->handlers.first= sa->handlers.last= NULL;
                sa->type= NULL; /* spacetype callbacks */
                
-               for(ar= sa->regionbase.first; ar; ar= ar->next)
+               for (ar= sa->regionbase.first; ar; ar= ar->next)
                        direct_link_region(fd, ar, sa->spacetype);
                
                /* accident can happen when read/save new file with older version */
                /* 2.50: we now always add spacedata for info */
-               if(sa->spacedata.first==NULL) {
+               if (sa->spacedata.first==NULL) {
                        SpaceInfo *sinfo= MEM_callocN(sizeof(SpaceInfo), "spaceinfo");
                        sa->spacetype= sinfo->spacetype= SPACE_INFO;
                        BLI_addtail(&sa->spacedata, sinfo);
                }
                /* add local view3d too */
-               else if(sa->spacetype==SPACE_VIEW3D)
+               else if (sa->spacetype==SPACE_VIEW3D)
                        view3d_split_250(sa->spacedata.first, &sa->regionbase);
                
                for (sl= sa->spacedata.first; sl; sl= sl->next) {
                        link_list(fd, &(sl->regionbase));
 
-                       for(ar= sl->regionbase.first; ar; ar= ar->next)
+                       for (ar= sl->regionbase.first; ar; ar= ar->next)
                                direct_link_region(fd, ar, sl->spacetype);
 
                        if (sl->spacetype==SPACE_VIEW3D) {
@@ -5693,16 +5809,16 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                link_list(fd, &(v3d->bgpicbase));
 
                                /* should be do_versions except this doesnt fit well there */
-                               if(v3d->bgpic) {
+                               if (v3d->bgpic) {
                                        bgpic= newdataadr(fd, v3d->bgpic);
                                        BLI_addtail(&v3d->bgpicbase, bgpic);
                                        v3d->bgpic= NULL;
                                }
 
-                               for(bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
+                               for (bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
                                        bgpic->iuser.ok= 1;
 
-                               if(v3d->gpd) {
+                               if (v3d->gpd) {
                                        v3d->gpd= newdataadr(fd, v3d->gpd);
                                        direct_link_gpencil(fd, v3d->gpd);
                                }
@@ -5713,7 +5829,7 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                v3d->properties_storage= NULL;
 
                                /* render can be quite heavy, set to wire on load */
-                               if(v3d->drawtype == OB_RENDER)
+                               if (v3d->drawtype == OB_RENDER)
                                        v3d->drawtype = OB_WIRE;
                                
                                view3d_split_250(v3d, &sl->regionbase);
@@ -5733,18 +5849,18 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                SpaceOops *soops= (SpaceOops*) sl;
                                
                                soops->treestore= newdataadr(fd, soops->treestore);
-                               if(soops->treestore) {
+                               if (soops->treestore) {
                                        soops->treestore->data= newdataadr(fd, soops->treestore->data);
                                        /* we only saved what was used */
                                        soops->treestore->totelem= soops->treestore->usedelem;
                                        soops->storeflag |= SO_TREESTORE_CLEANUP;       // at first draw
                                }
                        }
-                       else if(sl->spacetype==SPACE_IMAGE) {
+                       else if (sl->spacetype==SPACE_IMAGE) {
                                SpaceImage *sima= (SpaceImage *)sl;
                                
                                sima->cumap= newdataadr(fd, sima->cumap);
-                               if(sima->cumap)
+                               if (sima->cumap)
                                        direct_link_curvemapping(fd, sima->cumap);
                                
                                sima->iuser.scene= NULL;
@@ -5764,52 +5880,52 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                        direct_link_gpencil(fd, sima->gpd);
 #endif
                        }
-                       else if(sl->spacetype==SPACE_NODE) {
+                       else if (sl->spacetype==SPACE_NODE) {
                                SpaceNode *snode= (SpaceNode *)sl;
                                
-                               if(snode->gpd) {
+                               if (snode->gpd) {
                                        snode->gpd= newdataadr(fd, snode->gpd);
                                        direct_link_gpencil(fd, snode->gpd);
                                }
                        }
-                       else if(sl->spacetype==SPACE_TIME) {
+                       else if (sl->spacetype==SPACE_TIME) {
                                SpaceTime *stime= (SpaceTime *)sl;
                                stime->caches.first= stime->caches.last= NULL;
                        }
-                       else if(sl->spacetype==SPACE_LOGIC) {
+                       else if (sl->spacetype==SPACE_LOGIC) {
                                SpaceLogic *slogic= (SpaceLogic *)sl;
                                        
-                               if(slogic->gpd) {
+                               if (slogic->gpd) {
                                        slogic->gpd= newdataadr(fd, slogic->gpd);
                                        direct_link_gpencil(fd, slogic->gpd);
                                }
                        }
-                       else if(sl->spacetype==SPACE_SEQ) {
+                       else if (sl->spacetype==SPACE_SEQ) {
                                SpaceSeq *sseq= (SpaceSeq *)sl;
-                               if(sseq->gpd) {
+                               if (sseq->gpd) {
                                        sseq->gpd= newdataadr(fd, sseq->gpd);
                                        direct_link_gpencil(fd, sseq->gpd);
                                }
                        }
-                       else if(sl->spacetype==SPACE_BUTS) {
+                       else if (sl->spacetype==SPACE_BUTS) {
                                SpaceButs *sbuts= (SpaceButs *)sl;
                                sbuts->path= NULL;
                                sbuts->texuser= NULL;
                        }
-                       else if(sl->spacetype==SPACE_CONSOLE) {
+                       else if (sl->spacetype==SPACE_CONSOLE) {
                                SpaceConsole *sconsole= (SpaceConsole *)sl;
                                ConsoleLine *cl, *cl_next;
                                
                                link_list(fd, &sconsole->scrollback);
                                link_list(fd, &sconsole->history);
                                
-                               //for(cl= sconsole->scrollback.first; cl; cl= cl->next)
+                               //for (cl= sconsole->scrollback.first; cl; cl= cl->next)
                                //      cl->line= newdataadr(fd, cl->line);
                                
                                /*comma expressions, (e.g. expr1, expr2, expr3) evalutate each expression,
                                  from left to right.  the right-most expression sets the result of the comma
                                  expression as a whole*/
-                               for(cl= sconsole->history.first; cl; cl= cl_next) {
+                               for (cl= sconsole->history.first; cl; cl= cl_next) {
                                        cl_next= cl->next;
                                        cl->line= newdataadr(fd, cl->line);
                                        if (cl->line) {
@@ -5822,7 +5938,7 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                        }
                                }
                        }
-                       else if(sl->spacetype==SPACE_FILE) {
+                       else if (sl->spacetype==SPACE_FILE) {
                                SpaceFile *sfile= (SpaceFile *)sl;
                                
                                /* this sort of info is probably irrelevant for reloading...
@@ -5852,9 +5968,9 @@ static void direct_link_library(FileData *fd, Library *lib, Main *main)
 {
        Main *newmain;
        
-       for(newmain= fd->mainlist.first; newmain; newmain= newmain->next) {
-               if(newmain->curlib) {
-                       if(BLI_path_cmp(newmain->curlib->filepath, lib->filepath) == 0) {
+       for (newmain= fd->mainlist.first; newmain; newmain= newmain->next) {
+               if (newmain->curlib) {
+                       if (BLI_path_cmp(newmain->curlib->filepath, lib->filepath) == 0) {
                                BKE_reportf_wrap(fd->reports, RPT_WARNING,
                                                 "Library '%s', '%s' had multiple instances, save and reload!",
                                                 lib->name, lib->filepath);
@@ -5888,7 +6004,7 @@ static void direct_link_library(FileData *fd, Library *lib, Main *main)
 static void lib_link_library(FileData *UNUSED(fd), Main *main)
 {
        Library *lib;
-       for(lib= main->library.first; lib; lib= lib->id.next) {
+       for (lib= main->library.first; lib; lib= lib->id.next) {
                lib->id.us= 1;
        }
 }
@@ -5899,7 +6015,7 @@ static void fix_relpaths_library(const char *basepath, Main *main)
        Library *lib;
        /* BLO_read_from_memory uses a blank filename */
        if (basepath==NULL || basepath[0] == '\0') {
-               for(lib= main->library.first; lib; lib= lib->id.next) {
+               for (lib= main->library.first; lib; lib= lib->id.next) {
                        /* when loading a linked lib into a file which has not been saved,
                         * there is nothing we can be relative to, so instead we need to make
                         * it absolute. This can happen when appending an object with a relative
@@ -5911,7 +6027,7 @@ static void fix_relpaths_library(const char *basepath, Main *main)
                }
        }
        else {
-               for(lib= main->library.first; lib; lib= lib->id.next) {
+               for (lib= main->library.first; lib; lib= lib->id.next) {
                        /* Libraries store both relative and abs paths, recreate relative paths,
                         * relative to the blend file since indirectly linked libs will be relative to their direct linked library */
                        if (strncmp(lib->name, "//", 2)==0) { /* if this is relative to begin with? */
@@ -5929,8 +6045,8 @@ static void lib_link_speaker(FileData *fd, Main *main)
        Speaker *spk;
 
        spk= main->speaker.first;
-       while(spk) {
-               if(spk->id.flag & LIB_NEEDLINK) {
+       while (spk) {
+               if (spk->id.flag & LIB_NEEDLINK) {
                        if (spk->adt) lib_link_animdata(fd, &spk->id, spk->adt);
 
                        spk->sound= newlibadr(fd, spk->id.lib, spk->sound);
@@ -5962,7 +6078,7 @@ static void direct_link_sound(FileData *fd, bSound *sound)
        sound->waveform = NULL;
 
        // versioning stuff, if there was a cache, then we enable caching:
-       if(sound->cache)
+       if (sound->cache)
        {
                sound->flags |= SOUND_FLAGS_CACHING;
                sound->cache = NULL;
@@ -5977,8 +6093,8 @@ static void lib_link_sound(FileData *fd, Main *main)
        bSound *sound;
 
        sound= main->sound.first;
-       while(sound) {
-               if(sound->id.flag & LIB_NEEDLINK) {
+       while (sound) {
+               if (sound->id.flag & LIB_NEEDLINK) {
                        sound->id.flag -= LIB_NEEDLINK;
                        sound->ipo= newlibadr_us(fd, sound->id.lib, sound->ipo); // XXX depreceated - old animation system
                        
@@ -6000,25 +6116,25 @@ static void lib_link_group(FileData *fd, Main *main)
        GroupObject *go;
        int add_us;
        
-       while(group) {
-               if(group->id.flag & LIB_NEEDLINK) {
+       while (group) {
+               if (group->id.flag & LIB_NEEDLINK) {
                        group->id.flag -= LIB_NEEDLINK;
                        
                        add_us= 0;
                        
                        go= group->gobject.first;
-                       while(go) {
+                       while (go) {
                                go->ob= newlibadr(fd, group->id.lib, go->ob);
-                               if(go->ob) {
+                               if (go->ob) {
                                        go->ob->flag |= OB_FROMGROUP;
                                        /* if group has an object, it increments user... */
                                        add_us= 1;
-                                       if(go->ob->id.us==0) 
+                                       if (go->ob->id.us==0)
                                                go->ob->id.us= 1;
                                }
                                go= go->next;
                        }
-                       if(add_us) group->id.us++;
+                       if (add_us) group->id.us++;
                        rem_from_group(group, NULL, NULL, NULL);        /* removes NULL entries */
                }
                group= group->id.next;
@@ -6039,7 +6155,7 @@ static void direct_link_movieTracks(FileData *fd, ListBase *tracksbase)
        link_list(fd, tracksbase);
 
        track= tracksbase->first;
-       while(track) {
+       while (track) {
                track->markers= newdataadr(fd, track->markers);
 
                track= track->next;
@@ -6053,10 +6169,10 @@ static void direct_link_movieclip(FileData *fd, MovieClip *clip)
 
        clip->adt= newdataadr(fd, clip->adt);
 
-       if(fd->movieclipmap) clip->cache= newmclipadr(fd, clip->cache);
+       if (fd->movieclipmap) clip->cache= newmclipadr(fd, clip->cache);
        else clip->cache= NULL;
 
-       if(fd->movieclipmap) clip->tracking.camera.intrinsics= newmclipadr(fd, clip->tracking.camera.intrinsics);
+       if (fd->movieclipmap) clip->tracking.camera.intrinsics= newmclipadr(fd, clip->tracking.camera.intrinsics);
        else clip->tracking.camera.intrinsics= NULL;
 
        direct_link_movieTracks(fd, &tracking->tracks);
@@ -6075,7 +6191,7 @@ static void direct_link_movieclip(FileData *fd, MovieClip *clip)
        link_list(fd, &tracking->objects);
 
        object= tracking->objects.first;
-       while(object) {
+       while (object) {
                direct_link_movieTracks(fd, &object->tracks);
                direct_link_movieReconstruction(fd, &object->reconstruction);
 
@@ -6088,8 +6204,8 @@ static void lib_link_movieclip(FileData *fd, Main *main)
        MovieClip *clip;
 
        clip= main->movieclip.first;
-       while(clip) {
-               if(clip->id.flag & LIB_NEEDLINK) {
+       while (clip) {
+               if (clip->id.flag & LIB_NEEDLINK) {
                        if (clip->adt)
                                lib_link_animdata(fd, &clip->id, clip->adt);
 
@@ -6145,7 +6261,7 @@ static BHead *read_data_into_oldnewmap(FileData *fd, BHead *bhead, const char *a
 {
        bhead = blo_nextbhead(fd, bhead);
 
-       while(bhead && bhead->code==DATA) {
+       while (bhead && bhead->code==DATA) {
                void *data;
 #if 0
                /* XXX DUMB DEBUGGING OPTION TO GIVE NAMES for guarded malloc errors */
@@ -6188,7 +6304,7 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
        oldnewmap_insert(fd->libmap, bhead->old, id, bhead->code);      /* for ID_ID check */
        
        /* do after read_struct, for dna reconstruct */
-       if(bhead->code==ID_ID) {
+       if (bhead->code==ID_ID) {
                lb= which_libbase(main, GS(id->name));
        }
        else {
@@ -6200,13 +6316,13 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
        /* clear first 8 bits */
        id->flag= (id->flag & 0xFF00) | flag | LIB_NEEDLINK;
        id->lib= main->curlib;
-       if(id->flag & LIB_FAKEUSER) id->us= 1;
+       if (id->flag & LIB_FAKEUSER) id->us= 1;
        else id->us= 0;
        id->icon_id = 0;
        id->flag &= ~(LIB_ID_RECALC|LIB_ID_RECALC_DATA);
 
        /* this case cannot be direct_linked: it's just the ID part */
-       if(bhead->code==ID_ID) {
+       if (bhead->code==ID_ID) {
                return blo_nextbhead(fd, bhead);
        }
 
@@ -6342,7 +6458,7 @@ static BHead *read_global(BlendFileData *bfd, FileData *fd, BHead *bhead)
        bfd->globalf= fg->globalf;
        BLI_strncpy(bfd->filename, fg->filename, sizeof(bfd->filename));
        
-       if(G.fileflags & G_FILE_RECOVER)
+       if (G.fileflags & G_FILE_RECOVER)
                BLI_strncpy(fd->relabase, fg->filename, sizeof(fd->relabase));
        
        bfd->curscreen= fg->curscreen;
@@ -6363,8 +6479,8 @@ static void link_global(FileData *fd, BlendFileData *bfd)
        bfd->curscreen= newlibadr(fd, NULL, bfd->curscreen);
        bfd->curscene= newlibadr(fd, NULL, bfd->curscene);
        // this happens in files older than 2.35
-       if(bfd->curscene==NULL) {
-               if(bfd->curscreen) bfd->curscene= bfd->curscreen->scene;
+       if (bfd->curscene==NULL) {
+               if (bfd->curscreen) bfd->curscene= bfd->curscreen->scene;
        }
 }
 
@@ -6374,12 +6490,12 @@ static void vcol_to_fcol(Mesh *me)
        unsigned int *mcol, *mcoln, *mcolmain;
        int a;
 
-       if(me->totface==0 || me->mcol==NULL) return;
+       if (me->totface==0 || me->mcol==NULL) return;
 
        mcoln= mcolmain= MEM_mallocN(4*sizeof(int)*me->totface, "mcoln");
        mcol = (unsigned int *)me->mcol;
        mface= me->mface;
-       for(a=me->totface; a>0; a--, mface++) {
+       for (a=me->totface; a>0; a--, mface++) {
                mcoln[0]= mcol[mface->v1];
                mcoln[1]= mcol[mface->v2];
                mcoln[2]= mcol[mface->v3];
@@ -6423,7 +6539,7 @@ static void do_version_bone_head_tail_237(Bone *bone)
        mul_v3_fl(vec, bone->length);
        add_v3_v3v3(bone->arm_tail, bone->arm_head, vec);
 
-       for(child= bone->childbase.first; child; child= child->next)
+       for (child= bone->childbase.first; child; child= child->next)
                do_version_bone_head_tail_237(child);
 }
 
@@ -6431,13 +6547,13 @@ static void bone_version_238(ListBase *lb)
 {
        Bone *bone;
        
-       for(bone= lb->first; bone; bone= bone->next) {
-               if(bone->rad_tail==0.0f && bone->rad_head==0.0f) {
+       for (bone= lb->first; bone; bone= bone->next) {
+               if (bone->rad_tail==0.0f && bone->rad_head==0.0f) {
                        bone->rad_head= 0.25f*bone->length;
                        bone->rad_tail= 0.1f*bone->length;
                        
                        bone->dist-= bone->rad_head;
-                       if(bone->dist<=0.0f) bone->dist= 0.0f;
+                       if (bone->dist<=0.0f) bone->dist= 0.0f;
                }
                bone_version_238(&bone->childbase);
        }
@@ -6447,8 +6563,8 @@ static void bone_version_239(ListBase *lb)
 {
        Bone *bone;
        
-       for(bone= lb->first; bone; bone= bone->next) {
-               if(bone->layer==0) 
+       for (bone= lb->first; bone; bone= bone->next) {
+               if (bone->layer==0)
                        bone->layer= 1;
                bone_version_239(&bone->childbase);
        }
@@ -6458,10 +6574,10 @@ static void ntree_version_241(bNodeTree *ntree)
 {
        bNode *node;
        
-       if(ntree->type==NTREE_COMPOSIT) {
-               for(node= ntree->nodes.first; node; node= node->next) {
-                       if(node->type==CMP_NODE_BLUR) {
-                               if(node->storage==NULL) {
+       if (ntree->type==NTREE_COMPOSIT) {
+               for (node= ntree->nodes.first; node; node= node->next) {
+                       if (node->type==CMP_NODE_BLUR) {
+                               if (node->storage==NULL) {
                                        NodeBlurData *nbd= MEM_callocN(sizeof(NodeBlurData), "node blur patch");
                                        nbd->sizex= node->custom1;
                                        nbd->sizey= node->custom2;
@@ -6469,8 +6585,8 @@ static void ntree_version_241(bNodeTree *ntree)
                                        node->storage= nbd;
                                }
                        }
-                       else if(node->type==CMP_NODE_VECBLUR) {
-                               if(node->storage==NULL) {
+                       else if (node->type==CMP_NODE_VECBLUR) {
+                               if (node->storage==NULL) {
                                        NodeBlurData *nbd= MEM_callocN(sizeof(NodeBlurData), "node blur patch");
                                        nbd->samples= node->custom1;
                                        nbd->maxspeed= node->custom2;
@@ -6486,49 +6602,24 @@ static void ntree_version_242(bNodeTree *ntree)
 {
        bNode *node;
        
-       if(ntree->type==NTREE_COMPOSIT) {
-               for(node= ntree->nodes.first; node; node= node->next) {
-                       if(node->type==CMP_NODE_HUE_SAT) {
-                               if(node->storage) {
+       if (ntree->type==NTREE_COMPOSIT) {
+               for (node= ntree->nodes.first; node; node= node->next) {
+                       if (node->type==CMP_NODE_HUE_SAT) {
+                               if (node->storage) {
                                        NodeHueSat *nhs= node->storage;
-                                       if(nhs->val==0.0f) nhs->val= 1.0f;
+                                       if (nhs->val==0.0f) nhs->val= 1.0f;
                                }
                        }
                }
        }
-       else if(ntree->type==NTREE_SHADER) {
-               for(node= ntree->nodes.first; node; node= node->next)
-                       if(node->type == SH_NODE_GEOMETRY && node->storage == NULL)
+       else if (ntree->type==NTREE_SHADER) {
+               for (node= ntree->nodes.first; node; node= node->next)
+                       if (node->type == SH_NODE_GEOMETRY && node->storage == NULL)
                                node->storage= MEM_callocN(sizeof(NodeGeometry), "NodeGeometry");
        }
        
 }
 
-
-/* somehow, probably importing via python, keyblock adrcodes are not in order */
-static void sort_shape_fix(Main *main)
-{
-       Key *key;
-       KeyBlock *kb;
-       int sorted= 0;
-       
-       while(sorted==0) {
-               sorted= 1;
-               for(key= main->key.first; key; key= key->id.next) {
-                       for(kb= key->block.first; kb; kb= kb->next) {
-                               if(kb->next && kb->adrcode>kb->next->adrcode) {
-                                       KeyBlock *next= kb->next;
-                                       BLI_remlink(&key->block, kb);
-                                       BLI_insertlink(&key->block, next, kb);
-                                       kb= next;
-                                       sorted= 0;
-                               }
-                       }
-               }
-               if(sorted==0) printf("warning, shape keys were sorted incorrect, fixed it!\n");
-       }
-}
-
 static void customdata_version_242(Mesh *me)
 {
        CustomDataLayer *layer;
@@ -6607,7 +6698,7 @@ static void customdata_version_242(Mesh *me)
                }
        }
 
-       mesh_update_customdata_pointers(me);
+       mesh_update_customdata_pointers(me, TRUE);
 }
 
 /*only copy render texface layer from active*/
@@ -6627,11 +6718,11 @@ static void do_version_ntree_242_2(bNodeTree *ntree)
 {
        bNode *node;
        
-       if(ntree->type==NTREE_COMPOSIT) {
-               for(node= ntree->nodes.first; node; node= node->next) {
-                       if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
+       if (ntree->type==NTREE_COMPOSIT) {
+               for (node= ntree->nodes.first; node; node= node->next) {
+                       if (ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
                                /* only image had storage */
-                               if(node->storage) {
+                               if (node->storage) {
                                        NodeImageAnim *nia= node->storage;
                                        ImageUser *iuser= MEM_callocN(sizeof(ImageUser), "ima user node");
 
@@ -6664,27 +6755,27 @@ static void ntree_version_245(FileData *fd, Library *lib, bNodeTree *ntree)
        Image *image;
        ImageUser *iuser;
 
-       if(ntree->type==NTREE_COMPOSIT) {
-               for(node= ntree->nodes.first; node; node= node->next) {
-                       if(node->type == CMP_NODE_ALPHAOVER) {
-                               if(!node->storage) {
+       if (ntree->type==NTREE_COMPOSIT) {
+               for (node= ntree->nodes.first; node; node= node->next) {
+                       if (node->type == CMP_NODE_ALPHAOVER) {
+                               if (!node->storage) {
                                        ntf= MEM_callocN(sizeof(NodeTwoFloats), "NodeTwoFloats");
                                        node->storage= ntf;
-                                       if(node->custom1)
+                                       if (node->custom1)
                                                ntf->x= 1.0f;
                                }
                        }
                        
                        /* fix for temporary flag changes during 245 cycle */
                        nodeid= newlibadr(fd, lib, node->id);
-                       if(node->storage && nodeid && GS(nodeid->name) == ID_IM) {
+                       if (node->storage && nodeid && GS(nodeid->name) == ID_IM) {
                                image= (Image*)nodeid;
                                iuser= node->storage;
-                               if(iuser->flag & IMA_OLD_PREMUL) {
+                               if (iuser->flag & IMA_OLD_PREMUL) {
                                        iuser->flag &= ~IMA_OLD_PREMUL;
                                        iuser->flag |= IMA_DO_PREMUL;
                                }
-                               if(iuser->flag & IMA_DO_PREMUL) {
+                               if (iuser->flag & IMA_DO_PREMUL) {
                                        image->flag &= ~IMA_OLD_PREMUL;
                                        image->flag |= IMA_DO_PREMUL;
                                }
@@ -6727,27 +6818,27 @@ static void alphasort_version_246(FileData *fd, Library *lib, Mesh *me)
        int a, b, texalpha;
 
        /* verify we have a tface layer */
-       for(b=0; b<me->fdata.totlayer; b++)
-               if(me->fdata.layers[b].type == CD_MTFACE)
+       for (b=0; b<me->fdata.totlayer; b++)
+               if (me->fdata.layers[b].type == CD_MTFACE)
                        break;
        
-       if(b == me->fdata.totlayer)
+       if (b == me->fdata.totlayer)
                return;
 
        /* if we do, set alpha sort if the game engine did it before */
-       for(a=0, mf=me->mface; a<me->totface; a++, mf++) {
-               if(mf->mat_nr < me->totcol) {
+       for (a=0, mf=me->mface; a<me->totface; a++, mf++) {
+               if (mf->mat_nr < me->totcol) {
                        ma= newlibadr(fd, lib, me->mat[mf->mat_nr]);
                        texalpha = 0;
 
                        /* we can't read from this if it comes from a library,
                         * because direct_link might not have happened on it,
                         * so ma->mtex is not pointing to valid memory yet */
-                       if(ma && ma->id.lib)
+                       if (ma && ma->id.lib)
                                ma= NULL;
 
-                       for(b=0; ma && b<MAX_MTEX; b++)
-                               if(ma->mtex && ma->mtex[b] && ma->mtex[b]->mapto & MAP_ALPHA)
+                       for (b=0; ma && b<MAX_MTEX; b++)
+                               if (ma->mtex && ma->mtex[b] && ma->mtex[b]->mapto & MAP_ALPHA)
                                        texalpha = 1;
                }
                else {
@@ -6755,13 +6846,13 @@ static void alphasort_version_246(FileData *fd, Library *lib, Mesh *me)
                        texalpha = 0;
                }
 
-               for(b=0; b<me->fdata.totlayer; b++) {
-                       if(me->fdata.layers[b].type == CD_MTFACE) {
+               for (b=0; b<me->fdata.totlayer; b++) {
+                       if (me->fdata.layers[b].type == CD_MTFACE) {
                                tf = ((MTFace*)me->fdata.layers[b].data) + a;
 
                                tf->mode &= ~TF_ALPHASORT;
-                               if(ma && (ma->mode & MA_ZTRANSP))
-                                       if(ELEM(tf->transp, TF_ALPHA, TF_ADD) || (texalpha && (tf->transp != TF_CLIP)))
+                               if (ma && (ma->mode & MA_ZTRANSP))
+                                       if (ELEM(tf->transp, TF_ALPHA, TF_ADD) || (texalpha && (tf->transp != TF_CLIP)))
                                                tf->mode |= TF_ALPHASORT;
                        }
                }
@@ -6775,12 +6866,12 @@ static void area_add_header_region(ScrArea *sa, ListBase *lb)
        
        BLI_addtail(lb, ar);
        ar->regiontype= RGN_TYPE_HEADER;
-       if(sa->headertype==HEADERDOWN)
+       if (sa->headertype==HEADERDOWN)
                ar->alignment= RGN_ALIGN_BOTTOM;
        else
                ar->alignment= RGN_ALIGN_TOP;
        
-       /* initialise view2d data for header region, to allow panning */
+       /* initialize view2d data for header region, to allow panning */
        /* is copy from ui_view2d.c */
        ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
        ar->v2d.keepofs = V2D_LOCKOFS_Y;
@@ -6799,10 +6890,10 @@ static void sequencer_init_preview_region(ARegion* ar)
        ar->v2d.keepzoom= V2D_KEEPASPECT | V2D_KEEPZOOM;
        ar->v2d.minzoom= 0.00001f;
        ar->v2d.maxzoom= 100000.0f;
-       ar->v2d.tot.xmin= -960.0f; /* 1920 width centered */
-       ar->v2d.tot.ymin= -540.0f; /* 1080 height centered */
-       ar->v2d.tot.xmax= 960.0f;
-       ar->v2d.tot.ymax= 540.0f;
+       ar->v2d.tot.xmin = -960.0f; /* 1920 width centered */
+       ar->v2d.tot.ymin = -540.0f; /* 1080 height centered */
+       ar->v2d.tot.xmax = 960.0f;
+       ar->v2d.tot.ymax = 540.0f;
        ar->v2d.min[0]= 0.0f;
        ar->v2d.min[1]= 0.0f;
        ar->v2d.max[0]= 12000.0f;
@@ -6818,7 +6909,7 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
        ARegion *ar;
        ARegion *ar_main;
 
-       if(sl) {
+       if (sl) {
                /* first channels for ipo action nla... */
                switch(sl->spacetype) {
                        case SPACE_IPO:
@@ -6939,7 +7030,7 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
        
        ar->regiontype= RGN_TYPE_WINDOW;
        
-       if(sl) {
+       if (sl) {
                /* if active spacetype has view2d data, copy that over to main region */
                /* and we split view3d */
                switch(sl->spacetype) {
@@ -6971,7 +7062,7 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                ar->v2d.align |= V2D_ALIGN_NO_NEG_Y;
                                ar->v2d.keepofs |= V2D_LOCKOFS_Y;
                                ar->v2d.keepzoom |= V2D_LOCKZOOM_Y;
-                               ar->v2d.tot.ymin= ar->v2d.cur.ymin= -10.0;
+                               ar->v2d.tot.ymin = ar->v2d.cur.ymin = -10.0;
                                ar->v2d.min[1]= ar->v2d.max[1]= 20.0;
                        }
                                break;
@@ -6998,8 +7089,8 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                SpaceNla *snla= (SpaceNla *)sl;
                                memcpy(&ar->v2d, &snla->v2d, sizeof(View2D));
                                
-                               ar->v2d.tot.ymin= (float)(-sa->winy)/3.0f;
-                               ar->v2d.tot.ymax= 0.0f;
+                               ar->v2d.tot.ymin = (float)(-sa->winy)/3.0f;
+                               ar->v2d.tot.ymax = 0.0f;
                                
                                ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
                                ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
@@ -7012,10 +7103,10 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                SpaceAction *saction= (SpaceAction *)sl;
                                
                                /* we totally reinit the view for the Action Editor, as some old instances had some weird cruft set */
-                               ar->v2d.tot.xmin= -20.0f;
-                               ar->v2d.tot.ymin= (float)(-sa->winy)/3.0f;
-                               ar->v2d.tot.xmax= (float)((sa->winx > 120)? (sa->winx) : 120);
-                               ar->v2d.tot.ymax= 0.0f;
+                               ar->v2d.tot.xmin = -20.0f;
+                               ar->v2d.tot.ymin = (float)(-sa->winy)/3.0f;
+                               ar->v2d.tot.xmax = (float)((sa->winx > 120)? (sa->winx) : 120);
+                               ar->v2d.tot.ymax = 0.0f;
                                
                                ar->v2d.cur= ar->v2d.tot;
                                
@@ -7100,23 +7191,23 @@ static void do_versions_windowmanager_2_50(bScreen *screen)
        SpaceLink *sl;
        
        /* add regions */
-       for(sa= screen->areabase.first; sa; sa= sa->next) {
+       for (sa= screen->areabase.first; sa; sa= sa->next) {
                
                /* we keep headertype variable to convert old files only */
-               if(sa->headertype)
+               if (sa->headertype)
                        area_add_header_region(sa, &sa->regionbase);
                
                area_add_window_regions(sa, sa->spacedata.first, &sa->regionbase);
                
                /* space imageselect is deprecated */
-               for(sl= sa->spacedata.first; sl; sl= sl->next) {
-                       if(sl->spacetype==SPACE_IMASEL)
+               for (sl= sa->spacedata.first; sl; sl= sl->next) {
+                       if (sl->spacetype==SPACE_IMASEL)
                                sl->spacetype= SPACE_EMPTY;     /* spacedata then matches */
                }
                
                /* space sound is deprecated */
-               for(sl= sa->spacedata.first; sl; sl= sl->next) {
-                       if(sl->spacetype==SPACE_SOUND)
+               for (sl= sa->spacedata.first; sl; sl= sl->next) {
+                       if (sl->spacetype==SPACE_SOUND)
                                sl->spacetype= SPACE_EMPTY;     /* spacedata then matches */
                }
                
@@ -7124,10 +7215,10 @@ static void do_versions_windowmanager_2_50(bScreen *screen)
                sa->butspacetype= sa->spacetype;
                
                /* pushed back spaces also need regions! */
-               if(sa->spacedata.first) {
+               if (sa->spacedata.first) {
                        sl= sa->spacedata.first;
-                       for(sl= sl->next; sl; sl= sl->next) {
-                               if(sa->headertype)
+                       for (sl= sl->next; sl; sl= sl->next) {
+                               if (sa->headertype)
                                        area_add_header_region(sa, &sl->regionbase);
                                area_add_window_regions(sa, sl, &sl->regionbase);
                        }
@@ -7146,7 +7237,7 @@ static void versions_gpencil_add_main(ListBase *lb, ID *id, const char *name)
        new_id(lb, id, name);
        /* alphabetic insterion: is in new_id */
        
-       if(G.f & G_DEBUG)
+       if (G.debug & G_DEBUG)
                printf("Converted GPencil to ID: %s\n", id->name+2);
 }
 
@@ -7156,25 +7247,25 @@ static void do_versions_gpencil_2_50(Main *main, bScreen *screen)
        SpaceLink *sl;
        
        /* add regions */
-       for(sa= screen->areabase.first; sa; sa= sa->next) {
-               for(sl= sa->spacedata.first; sl; sl= sl->next) {
+       for (sa= screen->areabase.first; sa; sa= sa->next) {
+               for (sl= sa->spacedata.first; sl; sl= sl->next) {
                        if (sl->spacetype==SPACE_VIEW3D) {
                                View3D *v3d= (View3D*) sl;
-                               if(v3d->gpd) {
+                               if (v3d->gpd) {
                                        versions_gpencil_add_main(&main->gpencil, (ID *)v3d->gpd, "GPencil View3D");
                                        v3d->gpd= NULL;
                                }
                        }
                        else if (sl->spacetype==SPACE_NODE) {
                                SpaceNode *snode= (SpaceNode *)sl;
-                               if(snode->gpd) {
+                               if (snode->gpd) {
                                        versions_gpencil_add_main(&main->gpencil, (ID *)snode->gpd, "GPencil Node");
                                        snode->gpd= NULL;
                                }
                        }
                        else if (sl->spacetype==SPACE_SEQ) {
                                SpaceSeq *sseq= (SpaceSeq *)sl;
-                               if(sseq->gpd) {
+                               if (sseq->gpd) {
                                        versions_gpencil_add_main(&main->gpencil, (ID *)sseq->gpd, "GPencil Node");
                                        sseq->gpd= NULL;
                                }
@@ -7182,7 +7273,7 @@ static void do_versions_gpencil_2_50(Main *main, bScreen *screen)
                        else if (sl->spacetype==SPACE_IMAGE) {
                                SpaceImage *sima= (SpaceImage *)sl;
 #if 0                  /* see comment on r28002 */
-                               if(sima->gpd) {
+                               if (sima->gpd) {
                                        versions_gpencil_add_main(&main->gpencil, (ID *)sima->gpd, "GPencil Image");
                                        sima->gpd= NULL;
                                }
@@ -7200,8 +7291,8 @@ static PartEff *do_version_give_parteff_245(Object *ob)
        PartEff *paf;
 
        paf= ob->effect.first;
-       while(paf) {
-               if(paf->type==EFF_PARTICLE) return paf;
+       while (paf) {
+               if (paf->type==EFF_PARTICLE) return paf;
                paf= paf->next;
        }
        return NULL;
@@ -7210,9 +7301,9 @@ static void do_version_free_effect_245(Effect *eff)
 {
        PartEff *paf;
 
-       if(eff->type==EFF_PARTICLE) {
+       if (eff->type==EFF_PARTICLE) {
                paf= (PartEff *)eff;
-               if(paf->keys) MEM_freeN(paf->keys);
+               if (paf->keys) MEM_freeN(paf->keys);
        }
        MEM_freeN(eff);
 }
@@ -7221,7 +7312,7 @@ static void do_version_free_effects_245(ListBase *lb)
        Effect *eff;
 
        eff= lb->first;
-       while(eff) {
+       while (eff) {
                BLI_remlink(lb, eff);
                do_version_free_effect_245(eff);
                eff= lb->first;
@@ -7234,20 +7325,20 @@ static void do_version_mtex_factor_2_50(MTex **mtex_array, short idtype)
        float varfac, colfac;
        int a, neg;
 
-       if(!mtex_array)
+       if (!mtex_array)
                return;
 
-       for(a=0; a<MAX_MTEX; a++) {
-               if(mtex_array[a]) {
+       for (a=0; a<MAX_MTEX; a++) {
+               if (mtex_array[a]) {
                        mtex= mtex_array[a];
 
                        neg= mtex->maptoneg;
                        varfac= mtex->varfac;
                        colfac= mtex->colfac;
 
-                       if(neg & MAP_DISP) mtex->dispfac= -mtex->dispfac;
-                       if(neg & MAP_NORM) mtex->norfac= -mtex->norfac;
-                       if(neg & MAP_WARP) mtex->warpfac= -mtex->warpfac;
+                       if (neg & MAP_DISP) mtex->dispfac= -mtex->dispfac;
+                       if (neg & MAP_NORM) mtex->norfac= -mtex->norfac;
+                       if (neg & MAP_WARP) mtex->warpfac= -mtex->warpfac;
 
                        mtex->colspecfac= (neg & MAP_COLSPEC)? -colfac: colfac;
                        mtex->mirrfac= (neg & MAP_COLMIR)? -colfac: colfac;
@@ -7282,11 +7373,11 @@ static void do_version_mtex_factor_2_50(MTex **mtex_array, short idtype)
                        mtex->zendownfac= (neg & WOMAP_ZENDOWN)? -colfac: colfac;
                        mtex->blendfac= (neg & WOMAP_BLEND)? -varfac: varfac;
 
-                       if(idtype == ID_MA)
+                       if (idtype == ID_MA)
                                mtex->colfac= (neg & MAP_COL)? -colfac: colfac;
-                       else if(idtype == ID_LA)
+                       else if (idtype == ID_LA)
                                mtex->colfac= (neg & LAMAP_COL)? -colfac: colfac;
-                       else if(idtype == ID_WO)
+                       else if (idtype == ID_WO)
                                mtex->colfac= (neg & WOMAP_HORIZ)? -colfac: colfac;
                }
        }
@@ -7298,12 +7389,12 @@ static void do_version_mdef_250(Main *main)
        ModifierData *md;
        MeshDeformModifierData *mmd;
 
-       for(ob= main->object.first; ob; ob=ob->id.next) {
-               for(md=ob->modifiers.first; md; md=md->next) {
-                       if(md->type == eModifierType_MeshDeform) {
+       for (ob= main->object.first; ob; ob=ob->id.next) {
+               for (md=ob->modifiers.first; md; md=md->next) {
+                       if (md->type == eModifierType_MeshDeform) {
                                mmd= (MeshDeformModifierData*)md;
 
-                               if(mmd->bindcos) {
+                               if (mmd->bindcos) {
                                        /* make bindcos NULL in order to trick older versions
                                           into thinking that the mesh was not bound yet */
                                        mmd->bindcagecos= mmd->bindcos;
@@ -7321,17 +7412,17 @@ static void do_version_constraints_radians_degrees_250(ListBase *lb)
        bConstraint *con;
 
        for     (con=lb->first; con; con=con->next) {
-               if(con->type==CONSTRAINT_TYPE_RIGIDBODYJOINT) {
+               if (con->type==CONSTRAINT_TYPE_RIGIDBODYJOINT) {
                        bRigidBodyJointConstraint *data = con->data;
                        data->axX *= (float)(M_PI/180.0);
                        data->axY *= (float)(M_PI/180.0);
                        data->axZ *= (float)(M_PI/180.0);
                }
-               else if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
+               else if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
                        bKinematicConstraint *data = con->data;
                        data->poleangle *= (float)(M_PI/180.0);
                }
-               else if(con->type==CONSTRAINT_TYPE_ROTLIMIT) {
+               else if (con->type==CONSTRAINT_TYPE_ROTLIMIT) {
                        bRotLimitConstraint *data = con->data;
 
                        data->xmin *= (float)(M_PI/180.0);
@@ -7368,7 +7459,7 @@ static void do_versions_seq_unique_name_all_strips(
 {
        Sequence * seq = seqbasep->first;
 
-       while(seq) {
+       while (seq) {
                seqbase_unique_name_recursive(&sce->ed->seqbase, seq);
                if (seq->seqbase.first) {
                        do_versions_seq_unique_name_all_strips(
@@ -7387,7 +7478,7 @@ static void do_version_bone_roll_256(Bone *bone)
        copy_m3_m4(submat, bone->arm_mat);
        mat3_to_vec_roll(submat, NULL, &bone->arm_roll);
        
-       for(child = bone->childbase.first; child; child = child->next)
+       for (child = bone->childbase.first; child; child = child->next)
                do_version_bone_roll_256(child);
 }
 
@@ -7412,7 +7503,7 @@ void convert_tface_mt(FileData *fd, Main *main)
                gmain = G.main;
                G.main = main;
 
-               if(!(do_version_tface(main, 1))) {
+               if (!(do_version_tface(main, 1))) {
                        BKE_report(fd->reports, RPT_WARNING, "Texface conversion problem. Error in console");
                }
 
@@ -7438,8 +7529,8 @@ static void do_version_ntree_tex_mapping_260(void *UNUSED(data), ID *UNUSED(id),
 {
        bNode *node;
 
-       for(node=ntree->nodes.first; node; node=node->next) {
-               if(node->type == SH_NODE_MAPPING) {
+       for (node=ntree->nodes.first; node; node=node->next) {
+               if (node->type == SH_NODE_MAPPING) {
                        TexMapping *tex_mapping;
 
                        tex_mapping= node->storage;
@@ -7594,35 +7685,126 @@ static void do_versions_nodetree_socket_auto_hidden_flags_2_62(bNodeTree *ntree)
                                if (sock->link==NULL)
                                        sock->flag |= SOCK_AUTO_HIDDEN;
                        }
-                       for(sock=node->outputs.first; sock; sock= sock->next) {
-                               if(nodeCountSocketLinks(ntree, sock)==0)
+                       for (sock=node->outputs.first; sock; sock= sock->next) {
+                               if (nodeCountSocketLinks(ntree, sock)==0)
                                        sock->flag |= SOCK_AUTO_HIDDEN;
                        }
                }
                else {
-                       for(sock=node->inputs.first; sock; sock= sock->next)
+                       for (sock=node->inputs.first; sock; sock= sock->next)
                                sock->flag &= ~SOCK_AUTO_HIDDEN;
-                       for(sock=node->outputs.first; sock; sock= sock->next)
+                       for (sock=node->outputs.first; sock; sock= sock->next)
                                sock->flag &= ~SOCK_AUTO_HIDDEN;
                }
        }
 }
 
+static void do_versions_nodetree_multi_file_output_format_2_62_1(Scene *sce, bNodeTree *ntree)
+{
+       bNode *node;
+       bNodeSocket *sock;
+       
+       for (node=ntree->nodes.first; node; node=node->next) {
+               if (node->type==CMP_NODE_OUTPUT_FILE) {
+                       /* previous CMP_NODE_OUTPUT_FILE nodes get converted to multi-file outputs */
+                       NodeImageFile *old_data = node->storage;
+                       NodeImageMultiFile *nimf= MEM_callocN(sizeof(NodeImageMultiFile), "node image multi file");
+                       bNodeSocket *old_image = BLI_findlink(&node->inputs, 0);
+                       bNodeSocket *old_z = BLI_findlink(&node->inputs, 1);
+                       bNodeSocket *sock;
+                       
+                       node->storage= nimf;
+                       
+                       BLI_strncpy(nimf->base_path, old_data->name, sizeof(nimf->base_path));
+                       nimf->format = old_data->im_format;
+                       
+                       /* if z buffer is saved, change the image type to multilayer exr.
+                        * XXX this is slightly messy, Z buffer was ignored before for anything but EXR and IRIS ...
+                        * i'm just assuming here that IRIZ means IRIS with z buffer ...
+                        */
+                       if (ELEM(old_data->im_format.imtype, R_IMF_IMTYPE_IRIZ, R_IMF_IMTYPE_OPENEXR)) {
+                               nimf->format.imtype = R_IMF_IMTYPE_MULTILAYER;
+                               sock = ntreeCompositOutputFileAddSocket(ntree, node, old_image->name, &nimf->format);
+                               if (old_image->link) {
+                                       old_image->link->tosock = sock;
+                                       sock->link = old_image->link;
+                               }
+                               sock = ntreeCompositOutputFileAddSocket(ntree, node, old_z->name, &nimf->format);
+                               if (old_z->link) {
+                                       old_z->link->tosock = sock;
+                                       sock->link = old_z->link;
+                               }
+                       }
+                       else {
+                               /* saves directly to base path, which is the old image output path */
+                               sock = ntreeCompositOutputFileAddSocket(ntree, node, "", &nimf->format);
+                               if (old_image->link) {
+                                       old_image->link->tosock = sock;
+                                       sock->link = old_image->link;
+                               }
+                       }
+                       
+                       nodeRemoveSocket(ntree, node, old_image);
+                       nodeRemoveSocket(ntree, node, old_z);
+                       MEM_freeN(old_data);
+               }
+               else if (node->type==CMP_NODE_OUTPUT_MULTI_FILE__DEPRECATED) {
+                       NodeImageMultiFile *nimf = node->storage;
+                       
+                       /* CMP_NODE_OUTPUT_MULTI_FILE has been redeclared as CMP_NODE_OUTPUT_FILE */
+                       node->type = CMP_NODE_OUTPUT_FILE;
+                       
+                       /* initialize the node-wide image format from render data, if available */
+                       if (sce)
+                               nimf->format = sce->r.im_format;
+                       
+                       /* transfer render format toggle to node format toggle */
+                       for (sock=node->inputs.first; sock; sock=sock->next) {
+                               NodeImageMultiFileSocket *simf = sock->storage;
+                               simf->use_node_format = simf->use_render_format;
+                       }
+                       
+                       /* we do have preview now */
+                       node->flag |= NODE_PREVIEW;
+               }
+       }
+}
+
+/* blue and red are swapped pre 2.62.1, be sane (red == red) now! */
+static void do_versions_mesh_mloopcol_swap_2_62_1(Mesh *me)
+{
+       CustomDataLayer *layer;
+       MLoopCol *mloopcol;
+       int a;
+       int i;
+
+       for (a = 0; a < me->ldata.totlayer; a++) {
+               layer = &me->ldata.layers[a];
+
+               if (layer->type == CD_MLOOPCOL) {
+                       mloopcol = (MLoopCol *)layer->data;
+                       for (i = 0; i < me->totloop; i++, mloopcol++) {
+                               SWAP(char, mloopcol->r, mloopcol->b);
+                       }
+               }
+       }
+}
+
 static void do_versions(FileData *fd, Library *lib, Main *main)
 {
        /* WATCH IT!!!: pointers from libdata have not been converted */
 
-       if(G.f & G_DEBUG)
+       if (G.debug & G_DEBUG)
                printf("read file %s\n  Version %d sub %d svn r%d\n", fd->relabase, main->versionfile, main->subversionfile, main->revision);
        
-       if(main->versionfile == 100) {
+       if (main->versionfile == 100) {
                /* tex->extend and tex->imageflag have changed: */
                Tex *tex = main->tex.first;
-               while(tex) {
-                       if(tex->id.flag & LIB_NEEDLINK) {
+               while (tex) {
+                       if (tex->id.flag & LIB_NEEDLINK) {
 
-                               if(tex->extend==0) {
-                                       if(tex->xrepeat || tex->yrepeat) tex->extend= TEX_REPEAT;
+                               if (tex->extend==0) {
+                                       if (tex->xrepeat || tex->yrepeat) tex->extend= TEX_REPEAT;
                                        else {
                                                tex->extend= TEX_EXTEND;
                                                tex->xrepeat= tex->yrepeat= 1;
@@ -7633,87 +7815,87 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        tex= tex->id.next;
                }
        }
-       if(main->versionfile <= 101) {
+       if (main->versionfile <= 101) {
                /* frame mapping */
                Scene *sce = main->scene.first;
-               while(sce) {
+               while (sce) {
                        sce->r.framapto= 100;
                        sce->r.images= 100;
                        sce->r.framelen= 1.0;
                        sce= sce->id.next;
                }
        }
-       if(main->versionfile <= 102) {
+       if (main->versionfile <= 102) {
                /* init halo's at 1.0 */
                Material *ma = main->mat.first;
-               while(ma) {
+               while (ma) {
                        ma->add= 1.0;
                        ma= ma->id.next;
                }
        }
-       if(main->versionfile <= 103) {
+       if (main->versionfile <= 103) {
                /* new variable in object: colbits */
                Object *ob = main->object.first;
                int a;
-               while(ob) {
+               while (ob) {
                        ob->colbits= 0;
-                       if(ob->totcol) {
-                               for(a=0; a<ob->totcol; a++) {
-                                       if(ob->mat[a]) ob->colbits |= (1<<a);
+                       if (ob->totcol) {
+                               for (a=0; a<ob->totcol; a++) {
+                                       if (ob->mat[a]) ob->colbits |= (1<<a);
                                }
                        }
                        ob= ob->id.next;
                }
        }
-       if(main->versionfile <= 104) {
+       if (main->versionfile <= 104) {
                /* timeoffs moved */
                Object *ob = main->object.first;
-               while(ob) {
-                       if(ob->transflag & 1) {
+               while (ob) {
+                       if (ob->transflag & 1) {
                                ob->transflag -= 1;
                                //ob->ipoflag |= OB_OFFS_OB;
                        }
                        ob= ob->id.next;
                }
        }
-       if(main->versionfile <= 105) {
+       if (main->versionfile <= 105) {
                Object *ob = main->object.first;
-               while(ob) {
+               while (ob) {
                        ob->dupon= 1; ob->dupoff= 0;
                        ob->dupsta= 1; ob->dupend= 100;
                        ob= ob->id.next;
                }
        }
-       if(main->versionfile <= 106) {
+       if (main->versionfile <= 106) {
                /* mcol changed */
                Mesh *me = main->mesh.first;
-               while(me) {
-                       if(me->mcol) vcol_to_fcol(me);
+               while (me) {
+                       if (me->mcol) vcol_to_fcol(me);
                        me= me->id.next;
                }
 
        }
-       if(main->versionfile <= 107) {
+       if (main->versionfile <= 107) {
                Object *ob;
                Scene *sce = main->scene.first;
-               while(sce) {
+               while (sce) {
                        sce->r.mode |= R_GAMMA;
                        sce= sce->id.next;
                }
                ob= main->object.first;
-               while(ob) {
+               while (ob) {
                        //ob->ipoflag |= OB_OFFS_PARENT;
-                       if(ob->dt==0) ob->dt= OB_SOLID;
+                       if (ob->dt==0) ob->dt= OB_SOLID;
                        ob= ob->id.next;
                }
 
        }
-       if(main->versionfile <= 109) {
+       if (main->versionfile <= 109) {
                /* new variable: gridlines */
                bScreen *sc = main->screen.first;
-               while(sc) {
+               while (sc) {
                        ScrArea *sa= sc->areabase.first;
-                       while(sa) {
+                       while (sa) {
                                SpaceLink *sl= sa->spacedata.first;
                                while (sl) {
                                        if (sl->spacetype==SPACE_VIEW3D) {
@@ -7728,17 +7910,17 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        sc= sc->id.next;
                }
        }
-       if(main->versionfile <= 113) {
+       if (main->versionfile <= 113) {
                Material *ma = main->mat.first;
-               while(ma) {
-                       if(ma->flaresize==0.0f) ma->flaresize= 1.0f;
+               while (ma) {
+                       if (ma->flaresize==0.0f) ma->flaresize= 1.0f;
                        ma->subsize= 1.0f;
                        ma->flareboost= 1.0f;
                        ma= ma->id.next;
                }
        }
 
-       if(main->versionfile <= 134) {
+       if (main->versionfile <= 134) {
                Tex *tex = main->tex.first;
                while (tex) {
                        if ((tex->rfac == 0.0f) &&
@@ -7752,7 +7934,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        tex = tex->id.next;
                }
        }
-       if(main->versionfile <= 140) {
+       if (main->versionfile <= 140) {
                /* r-g-b-fac in texture */
                Tex *tex = main->tex.first;
                while (tex) {
@@ -7767,54 +7949,54 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        tex = tex->id.next;
                }
        }
-       if(main->versionfile <= 153) {
+       if (main->versionfile <= 153) {
                Scene *sce = main->scene.first;
-               while(sce) {
-                       if(sce->r.blurfac==0.0f) sce->r.blurfac= 1.0f;
+               while (sce) {
+                       if (sce->r.blurfac==0.0f) sce->r.blurfac= 1.0f;
                        sce= sce->id.next;
                }
        }
-       if(main->versionfile <= 163) {
+       if (main->versionfile <= 163) {
                Scene *sce = main->scene.first;
-               while(sce) {
-                       if(sce->r.frs_sec==0) sce->r.frs_sec= 25;
+               while (sce) {
+                       if (sce->r.frs_sec==0) sce->r.frs_sec= 25;
                        sce= sce->id.next;
                }
        }
-       if(main->versionfile <= 164) {
+       if (main->versionfile <= 164) {
                Mesh *me= main->mesh.first;
-               while(me) {
+               while (me) {
                        me->smoothresh= 30;
                        me= me->id.next;
                }
        }
-       if(main->versionfile <= 165) {
+       if (main->versionfile <= 165) {
                Mesh *me= main->mesh.first;
                TFace *tface;
                int nr;
                char *cp;
 
-               while(me) {
-                       if(me->tface) {
+               while (me) {
+                       if (me->tface) {
                                nr= me->totface;
                                tface= me->tface;
-                               while(nr--) {
+                               while (nr--) {
                                        cp= (char *)&tface->col[0];
-                                       if(cp[1]>126) cp[1]= 255; else cp[1]*=2;
-                                       if(cp[2]>126) cp[2]= 255; else cp[2]*=2;
-                                       if(cp[3]>126) cp[3]= 255; else cp[3]*=2;
+                                       if (cp[1]>126) cp[1]= 255; else cp[1]*=2;
+                                       if (cp[2]>126) cp[2]= 255; else cp[2]*=2;
+                                       if (cp[3]>126) cp[3]= 255; else cp[3]*=2;
                                        cp= (char *)&tface->col[1];
-                                       if(cp[1]>126) cp[1]= 255; else cp[1]*=2;
-                                       if(cp[2]>126) cp[2]= 255; else cp[2]*=2;
-                                       if(cp[3]>126) cp[3]= 255; else cp[3]*=2;
+                                       if (cp[1]>126) cp[1]= 255; else cp[1]*=2;
+                                       if (cp[2]>126) cp[2]= 255; else cp[2]*=2;
+                                       if (cp[3]>126) cp[3]= 255; else cp[3]*=2;
                                        cp= (char *)&tface->col[2];
-                                       if(cp[1]>126) cp[1]= 255; else cp[1]*=2;
-                                       if(cp[2]>126) cp[2]= 255; else cp[2]*=2;
-                                       if(cp[3]>126) cp[3]= 255; else cp[3]*=2;
+                                       if (cp[1]>126) cp[1]= 255; else cp[1]*=2;
+                                       if (cp[2]>126) cp[2]= 255; else cp[2]*=2;
+                                       if (cp[3]>126) cp[3]= 255; else cp[3]*=2;
                                        cp= (char *)&tface->col[3];
-                                       if(cp[1]>126) cp[1]= 255; else cp[1]*=2;
-                                       if(cp[2]>126) cp[2]= 255; else cp[2]*=2;
-                                       if(cp[3]>126) cp[3]= 255; else cp[3]*=2;
+                                       if (cp[1]>126) cp[1]= 255; else cp[1]*=2;
+                                       if (cp[2]>126) cp[2]= 255; else cp[2]*=2;
+                                       if (cp[3]>126) cp[3]= 255; else cp[3]*=2;
 
                                        tface++;
                                }
@@ -7823,22 +8005,22 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
        }
 
-       if(main->versionfile <= 169) {
+       if (main->versionfile <= 169) {
                Mesh *me= main->mesh.first;
-               while(me) {
-                       if(me->subdiv==0) me->subdiv= 1;
+               while (me) {
+                       if (me->subdiv==0) me->subdiv= 1;
                        me= me->id.next;
                }
        }
 
-       if(main->versionfile <= 169) {
+       if (main->versionfile <= 169) {
                bScreen *sc= main->screen.first;
-               while(sc) {
+               while (sc) {
                        ScrArea *sa= sc->areabase.first;
-                       while(sa) {
+                       while (sa) {
                                SpaceLink *sl= sa->spacedata.first;
-                               while(sl) {
-                                       if(sl->spacetype==SPACE_IPO) {
+                               while (sl) {
+                                       if (sl->spacetype==SPACE_IPO) {
                                                SpaceIpo *sipo= (SpaceIpo*) sl;
                                                sipo->v2d.max[0]= 15000.0;
                                        }
@@ -7850,7 +8032,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
        }
 
-       if(main->versionfile <= 170) {
+       if (main->versionfile <= 170) {
                Object *ob = main->object.first;
                PartEff *paf;
                while (ob) {
@@ -7864,14 +8046,14 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
        }
 
-       if(main->versionfile <= 171) {
+       if (main->versionfile <= 171) {
                bScreen *sc= main->screen.first;
-               while(sc) {
+               while (sc) {
                        ScrArea *sa= sc->areabase.first;
-                       while(sa) {
+                       while (sa) {
                                SpaceLink *sl= sa->spacedata.first;
-                               while(sl) {
-                                       if(sl->spacetype==SPACE_TEXT) {
+                               while (sl) {
+                                       if (sl->spacetype==SPACE_TEXT) {
                                                SpaceText *st= (SpaceText*) sl;
                                                st->lheight= 12;
                                        }
@@ -7883,14 +8065,14 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
        }
 
-       if(main->versionfile <= 173) {
+       if (main->versionfile <= 173) {
                int a, b;
                Mesh *me= main->mesh.first;
-               while(me) {
-                       if(me->tface) {
+               while (me) {
+                       if (me->tface) {
                                TFace *tface= me->tface;
-                               for(a=0; a<me->totface; a++, tface++) {