Merging r46111 through r46136 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenloader / intern / readfile.c
index 3b69c84..84bc089 100644 (file)
 #include <stdarg.h> /* for va_start/end */
 
 #ifndef WIN32
-       #include <unistd.h> // for read close
+#  include <unistd.h> // for read close
 #else
-       #include <io.h> // for open close read
-#include "winsock2.h"
-#include "BLI_winstuff.h"
+#  include <io.h> // for open close read
+#  include "winsock2.h"
+#  include "BLI_winstuff.h"
 #endif
 
 /* allow readfile to use deprecated functionality */
@@ -77,6 +77,7 @@
 #include "DNA_nla_types.h"
 #include "DNA_node_types.h"
 #include "DNA_object_fluidsim.h" // NT
+#include "DNA_object_types.h"
 #include "DNA_packedFile_types.h"
 #include "DNA_particle_types.h"
 #include "DNA_property_types.h"
@@ -94,6 +95,7 @@
 #include "DNA_vfont_types.h"
 #include "DNA_world_types.h"
 #include "DNA_movieclip_types.h"
+#include "DNA_mask_types.h"
 
 #include "MEM_guardedalloc.h"
 
@@ -272,7 +274,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);
        }
 }
@@ -291,8 +293,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;
 }
 
@@ -308,7 +310,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;
@@ -331,7 +333,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];
@@ -361,16 +363,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)) {
@@ -432,7 +434,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]);
        }
 }
@@ -457,12 +459,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);
@@ -470,7 +472,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;
        }
@@ -485,7 +487,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) {
@@ -495,7 +497,7 @@ void blo_split_main(ListBase *mainlist, Main *main)
        }
 
        i= set_listbasepointers(main, lbarray);
-       while(i--)
+       while (i--)
                split_libdata(lbarray[i], main->next);
 }
 
@@ -516,7 +518,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;
@@ -544,7 +546,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;
                }
        }
@@ -560,7 +562,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;
 }
 
@@ -570,7 +572,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);
@@ -582,7 +584,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);
@@ -612,7 +614,7 @@ static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
                }
 
                /* this patch is to avoid a long long being read from not-eight aligned positions
-                  is necessary on any modern 64bit architecture) */
+                * is necessary on any modern 64bit architecture) */
                memcpy(&old, &bhead8->old, 8);
                bhead4->old = (int) (old >> 3);
 
@@ -666,14 +668,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));
 
@@ -684,10 +689,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;
                                }
@@ -712,7 +719,8 @@ static BHeadN *get_bhead(FileData *fd)
                                                MEM_freeN(new_bhead);
                                                new_bhead = NULL;
                                        }
-                               } else {
+                               }
+                               else {
                                        fd->eof = 1;
                                }
                        }
@@ -792,18 +800,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;
                                }
@@ -841,7 +850,8 @@ static int read_file_dna(FileData *fd)
                        }
 
                        return 1;
-               } else if (bhead->code==ENDB)
+               }
+               else if (bhead->code==ENDB)
                        break;
        }
 
@@ -854,7 +864,8 @@ static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size
 
        if (readsize < 0) {
                readsize = EOF;
-       } else {
+       }
+       else {
                filedata->seek += readsize;
        }
 
@@ -867,7 +878,8 @@ static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int
 
        if (readsize < 0) {
                readsize = EOF;
-       } else {
+       }
+       else {
                filedata->seek += readsize;
        }
 
@@ -892,14 +904,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;
                }
@@ -907,18 +919,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;
                        }
@@ -929,14 +941,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;
        }
@@ -995,7 +1007,8 @@ FileData *blo_openblenderfile(const char *filepath, ReportList *reports)
        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;
@@ -1012,7 +1025,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;
@@ -1028,7 +1042,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;
 
@@ -1048,8 +1063,7 @@ void blo_freefiledata(FileData *fd)
                        close(fd->filedes);
                }
 
-               if (fd->gzfiledes != NULL)
-               {
+               if (fd->gzfiledes != NULL) {
                        gzclose(fd->gzfiledes);
                }
 
@@ -1102,8 +1116,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;
@@ -1111,9 +1125,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 */
        }
@@ -1125,7 +1139,7 @@ int BLO_is_a_library(const char *path, char *dir, char *group)
                if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
 
                /* now we know that we are in a blend file and it is safe to 
-                  assume that gp actually points to a group */
+                * assume that gp actually points to a group */
                if (strcmp("Screen", gp)!=0)
                        BLI_strncpy(group, gp, GROUP_MAX);
        }
@@ -1146,14 +1160,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;
 }
@@ -1168,7 +1182,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;
@@ -1183,7 +1197,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;
                }
        }
@@ -1193,13 +1207,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);
                }
        }
@@ -1213,8 +1227,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;
 }
 
@@ -1226,20 +1240,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);
                }
        }
@@ -1260,27 +1274,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);
                }
        }
@@ -1293,19 +1307,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);
                }
        }
@@ -1326,16 +1340,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);
                }
        }
@@ -1348,11 +1362,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));
                }
        }
@@ -1371,7 +1385,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;
@@ -1388,9 +1402,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);
                        }
@@ -1404,12 +1419,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;
@@ -1423,18 +1438,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;
@@ -1459,15 +1474,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) {
-                       ipoin=imat= MEM_mallocN( len*4, "newmatar");
+               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++;
@@ -1477,11 +1492,11 @@ static void test_pointer_array(FileData *fd, void **mat)
                        *mat= imat;
                }
 
-               if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
-                       lpoin=lmat= MEM_mallocN( len*8, "newmatar");
+               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++;
@@ -1510,13 +1525,13 @@ static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData
        
        /* note!, idp-arrays didn't exist in 2.4x, so the pointer will be cleared
         * theres not really anything we can do to correct this, at least don't crash */
-       if(array==NULL) {
+       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);
 }
 
@@ -1529,14 +1544,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]);
@@ -1592,7 +1607,7 @@ static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData
                         * in the same field as int val; val2 in the
                         * IDPropertyData struct, they have to deal with
                         * endianness specifically
-
+                        *
                         * in theory, val and val2 would've already been swapped
                         * if switch_endian is true, so we have to first unswap
                         * them then reswap them as a single 64-bit entity.
@@ -1623,7 +1638,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;
@@ -1637,8 +1652,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);
@@ -1653,7 +1668,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);
@@ -1665,7 +1680,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);
 }
 
 
@@ -1710,11 +1725,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;
@@ -1730,7 +1745,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);
@@ -1744,11 +1759,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);
        }
 }
@@ -1760,7 +1775,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);
 }
 
@@ -1769,7 +1784,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);
        }
 }
@@ -1844,9 +1859,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]);
                                }
                        }
@@ -2135,11 +2150,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);
 }
 
@@ -2149,8 +2164,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);
                }
@@ -2276,17 +2291,19 @@ static void lib_nodetree_do_versions_update_cb(void *UNUSED(data), ID *UNUSED(id
 
 /* verify types for nodes and groups, all data has to be read */
 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
-* typedefs*/
+ * typedefs */
 static void lib_verify_nodetree(Main *main, int UNUSED(open))
 {
        bNodeTree *ntree;
        int i;
        bNodeTreeType *ntreetype;
 
-       /* this crashes blender on undo/redo
-               if(open==1) {
+       /* this crashes blender on undo/redo */
+#if 0
+               if (open==1) {
                        reinit_nodesystem();
-               }*/
+               }
+#endif
        
        /* set node->typeinfo pointers */
        for (i=0; i < NUM_NTREE_TYPES; ++i) {
@@ -2294,7 +2311,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);
        
        {
@@ -2304,7 +2321,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);
@@ -2320,18 +2337,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);
 
@@ -2367,8 +2384,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);
                }
@@ -2378,25 +2395,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;
                        }
                }
@@ -2408,22 +2426,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);
@@ -2456,7 +2474,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 */
@@ -2505,7 +2523,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;
@@ -2526,13 +2544,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;
@@ -2546,16 +2564,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;
        }
@@ -2567,8 +2585,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;
                }
@@ -2582,14 +2600,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);
 }
 
@@ -2621,8 +2639,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
@@ -2651,13 +2669,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);
                                }
@@ -2665,7 +2683,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;
@@ -2681,16 +2699,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);
@@ -2703,7 +2721,7 @@ static void lib_link_key(FileData *fd, Main *main)
        Key *key;
 
        key= main->key.first;
-       while(key) {
+       while (key) {
                /*check if we need to generate unique ids for the shapekeys*/
                if (!key->uidgen) {
                        KeyBlock *block;
@@ -2714,8 +2732,8 @@ static void lib_link_key(FileData *fd, Main *main)
                        }
                }
 
-               if(key->id.flag & LIB_NEEDLINK) {
-                       if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
+               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);
@@ -2734,19 +2752,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 */
+                       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;
                                }
@@ -2772,11 +2790,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;
@@ -2791,11 +2809,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
 
@@ -2831,21 +2849,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;
@@ -2861,12 +2879,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);
@@ -2880,8 +2898,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;
@@ -2901,8 +2919,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;
@@ -2921,11 +2939,12 @@ static void direct_link_text(FileData *fd, Text *text)
 
        text->compiled= NULL;
 
-/*
-       if(text->flags & TXT_ISEXT) {
+#if 0
+       if (text->flags & TXT_ISEXT) {
                reopen_text(text);
-       } else {
-*/
+               }
+               else {
+#endif
 
        link_list(fd, &text->lines);
        link_list(fd, &text->markers);
@@ -2934,7 +2953,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;
                
@@ -2959,7 +2978,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;
@@ -2972,12 +2991,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;
@@ -2989,13 +3008,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;
        }
@@ -3005,10 +3024,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 {
@@ -3030,11 +3049,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);
@@ -3058,15 +3077,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]);
                }
        }
@@ -3086,7 +3105,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;
 
@@ -3095,7 +3114,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;
@@ -3112,14 +3132,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);
                }
 
@@ -3135,20 +3155,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;
@@ -3163,7 +3183,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
@@ -3174,34 +3194,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);
@@ -3220,26 +3241,26 @@ 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.*/
+                       /* Link ID Properties -- and copy this comment EXACTLY for easy finding
+                        * of library blocks that implement this.*/
                        if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
 
                        ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
                        ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
                        
-                       for(a=0; a<MAX_MTEX; a++) {
+                       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;
@@ -3255,7 +3276,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]);
        }
 
@@ -3263,7 +3284,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);
@@ -3284,7 +3305,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;
@@ -3293,23 +3314,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);
                }
        }
@@ -3325,12 +3346,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;
                        }
@@ -3338,11 +3359,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;
                }
@@ -3353,7 +3374,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);
 }
 
@@ -3365,8 +3386,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
                        
@@ -3378,37 +3399,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);
                                }
                        }
@@ -3416,13 +3437,13 @@ 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)
-                                       switch(rule->type) {
+                               for (; rule; rule=rule->next)
+                                       switch (rule->type) {
                                                case eBoidRuleType_Goal:
                                                case eBoidRuleType_Avoid:
                                                {
@@ -3440,9 +3461,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);
                                }
@@ -3456,7 +3477,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)
@@ -3471,7 +3492,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);
@@ -3479,17 +3500,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]);
        }
 }
@@ -3498,20 +3519,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;
@@ -3521,7 +3542,7 @@ static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase
                }
                else {
                        /* particle modifier must be removed before particle system */
-                       ParticleSystemModifierData *psmd= psys_get_modifier(ob,psys);
+                       ParticleSystemModifierData *psmd= psys_get_modifier(ob, psys);
                        BLI_remlink(&ob->modifiers, psmd);
                        modifier_free((ModifierData *)psmd);
 
@@ -3536,16 +3557,16 @@ static void direct_link_particlesystems(FileData *fd, ListBase *particles)
        ParticleData *pa;
        int a;
 
-       for(psys=particles->first; psys; psys=psys->next) {
-               psys->particles=newdataadr(fd,psys->particles);
+       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++)
-                               pa->hair=newdataadr(fd,pa->hair);
+               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;
                        }
@@ -3553,20 +3574,20 @@ 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;
                }
 
                psys->fluid_springs = newdataadr(fd, psys->fluid_springs);
 
-               psys->child = newdataadr(fd,psys->child);
+               psys->child = newdataadr(fd, psys->child);
                psys->effectors = NULL;
 
                link_list(fd, &psys->targets);
@@ -3583,7 +3604,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;
                        
@@ -3591,8 +3612,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;
                        }
 
@@ -3619,7 +3640,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;
        }
 }
@@ -3627,10 +3648,10 @@ 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);
        }
 
@@ -3640,16 +3661,16 @@ static void lib_link_customdata_mtpoly(FileData *fd, Mesh *me, CustomData *pdata
 {
        int i;
 
-       for(i=0; i<pdata->totlayer; i++) {
+       for (i=0; i<pdata->totlayer; i++) {
                CustomDataLayer *layer = &pdata->layers[i];
                
-               if(layer->type == CD_MTEXPOLY) {
+               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)
+                               if (tf->tpage && tf->tpage->id.us==0)
                                        tf->tpage->id.us= 1;
                        }
                }
@@ -3661,18 +3682,18 @@ 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
-                       of library blocks that implement this.*/
+                       /* Link ID Properties -- and copy this comment EXACTLY for easy finding
+                        * of library blocks that implement this.*/
                        if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
                        if (me->adt) lib_link_animdata(fd, &me->id, me->adt);
 
                        /* this check added for python created meshes */
-                       if(me->mat) {
-                               for(i=0; i<me->totcol; i++) {
+                       if (me->mat) {
+                               for (i=0; i<me->totcol; i++) {
                                        me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
                                }
                        }
@@ -3684,13 +3705,20 @@ static void lib_link_mesh(FileData *fd, Main *main)
 
                        lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
                        lib_link_customdata_mtpoly(fd, me, &me->pdata, me->totpoly);
-                       if(me->mr && me->mr->levels.first)
+                       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) {
-                               convert_mfaces_to_mpolys(me);
+                               /* 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;
                        }
                        
                        /*
@@ -3729,7 +3757,7 @@ static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
        for (i= count; i > 0; i--, mdverts++) {
                /*convert to vgroup allocation system*/
                MDeformWeight *dw;
-               if(mdverts->dw && (dw= newdataadr(fd, mdverts->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);
@@ -3745,33 +3773,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() */
+                                * 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;
                }
        }
@@ -3783,17 +3811,25 @@ static void direct_link_customdata(FileData *fd, CustomData *data, int count)
        int i = 0;
 
        data->layers= newdataadr(fd, data->layers);
+
+       /* annoying workaround for bug [#31079] loading legacy files with
+        * no polygons _but_ have stale customdata */
+       if (UNLIKELY(count == 0 && data->layers == NULL && data->totlayer != 0)) {
+               memset(data, 0, sizeof(*data));
+               return;
+       }
+
        data->external= newdataadr(fd, data->external);
 
        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++;
                }
@@ -3820,13 +3856,14 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
        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);
 
        /* normally direct_link_dverts should be called in direct_link_customdata,
-          but for backwards compat in do_versions to work we do it here */
+        * but for backwards compat in do_versions to work we do it here */
        direct_link_dverts(fd, mesh->totvert, mesh->dvert);
 
        direct_link_customdata(fd, &mesh->vdata, mesh->totvert);
@@ -3866,12 +3903,11 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
 
 
        mesh->bb= NULL;
-       mesh->mselect = 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);
@@ -3887,18 +3923,18 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
                mesh->mr->verts = newdataadr(fd, mesh->mr->verts);
                
                /* If mesh has the same number of vertices as the
-                  highest multires level, load the current mesh verts
-                  into multires and discard the old data. Needed
-                  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)
+                * highest multires level, load the current mesh verts
+                * into multires and discard the old data. Needed
+                * 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)
                                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);
@@ -3908,12 +3944,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;
 
@@ -3933,9 +3969,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);
@@ -3969,7 +4005,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)
@@ -3988,8 +4024,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);
                        
@@ -4004,9 +4040,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;
                                        
@@ -4027,8 +4063,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);
@@ -4036,20 +4072,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);
                                }
@@ -4070,22 +4106,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);
@@ -4094,11 +4130,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);
                                }
@@ -4109,35 +4145,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 {
@@ -4145,44 +4181,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);
@@ -4193,24 +4229,23 @@ 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);
@@ -4219,7 +4254,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");
        }
 }
@@ -4276,7 +4311,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) {
@@ -4299,15 +4334,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);
                                }
                        }
@@ -4316,7 +4351,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;
                        }
@@ -4324,8 +4359,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;
                                smd->domain = newdataadr(fd, smd->domain);
@@ -4339,16 +4373,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");
@@ -4370,8 +4404,7 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                                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;
                                }
@@ -4383,8 +4416,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;
                                pmd->canvas->dm = NULL;
@@ -4399,13 +4431,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;
                                pmd->brush->psys = newdataadr(fd, pmd->brush->psys);
@@ -4424,9 +4455,9 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                        collmd->xnew = newdataadr(fd, collmd->xnew);
                        collmd->mfaces = newdataadr(fd, collmd->mfaces);
                        
-                       collmd->current_x = MEM_callocN(sizeof(MVert)*collmd->numverts,"current_x");
-                       collmd->current_xnew = MEM_callocN(sizeof(MVert)*collmd->numverts,"current_xnew");
-                       collmd->current_v = MEM_callocN(sizeof(MVert)*collmd->numverts,"current_v");
+                       collmd->current_x = MEM_callocN(sizeof(MVert)*collmd->numverts, "current_x");
+                       collmd->current_xnew = MEM_callocN(sizeof(MVert)*collmd->numverts, "current_xnew");
+                       collmd->current_v = MEM_callocN(sizeof(MVert)*collmd->numverts, "current_v");
                        */
                        
                        collmd->x = NULL;
@@ -4453,20 +4484,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;
@@ -4484,24 +4517,24 @@ 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++)
+                               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++)
+                               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++)
+                               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++)
+                               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++)
+                               if (mmd->bindcos)
+                                       for (a=0; a<mmd->totcagevert*3; a++)
                                                SWITCH_INT(mmd->bindcos[a]);
                        }
                }
@@ -4515,14 +4548,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);
                }
        }
@@ -4540,8 +4573,8 @@ static void direct_link_object(FileData *fd, Object *ob)
        ob->flag &= ~OB_FROMGROUP;
 
        /* loading saved files with editmode enabled works, but for undo we like
-          to stay in object mode during undo presses so keep editmode disabled */
-       if(fd->memfile)
+        * to stay in object mode during undo presses so keep editmode disabled */
+       if (fd->memfile)
                ob->mode &= ~(OB_MODE_EDIT|OB_MODE_PARTICLE_EDIT);
        
        ob->disp.first=ob->disp.last= NULL;
@@ -4571,11 +4604,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);
@@ -4599,7 +4632,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);
@@ -4623,7 +4656,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
@@ -4633,15 +4666,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);
@@ -4650,19 +4683,19 @@ static void direct_link_object(FileData *fd, Object *ob)
        ob->fluidsimSettings= newdataadr(fd, ob->fluidsimSettings); /* NT */
 
        link_list(fd, &ob->particlesystem);
-       direct_link_particlesystems(fd,&ob->particlesystem);
+       direct_link_particlesystems(fd, &ob->particlesystem);
        
        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);
@@ -4675,11 +4708,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);
@@ -4690,7 +4724,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;
        }
@@ -4701,9 +4735,9 @@ 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]);
                        }
                }
@@ -4738,7 +4772,7 @@ static void direct_link_object(FileData *fd, Object *ob)
        /* 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");
        }
 }
@@ -4750,14 +4784,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;
        }
@@ -4772,10 +4806,10 @@ static void lib_link_scene(FileData *fd, Main *main)
        TimeMarker *marker;
        
        sce= main->scene.first;
-       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.*/
+       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);
                        if (sce->adt) lib_link_animdata(fd, &sce->id, sce->adt);
                        
@@ -4793,38 +4827,40 @@ 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);
+                       SEQ_BEGIN (sce->ed, seq)
+                       {
+                               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->clip) {
+                               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) {
+                               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);
@@ -4838,8 +4874,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);
                                }
                        }
@@ -4850,12 +4886,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);
                        }
@@ -4878,8 +4914,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);
 }
 
@@ -4919,7 +4955,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);
@@ -4929,7 +4965,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);
@@ -4939,60 +4975,66 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                /* recursive link sequences, lb will be correctly initialized */
                link_recurs_seq(fd, &ed->seqbase);
 
-               SEQ_BEGIN(ed, seq) {
+               SEQ_BEGIN (ed, seq)
+               {
                        seq->seq1= newdataadr(fd, seq->seq1);
                        seq->seq2= newdataadr(fd, seq->seq2);
                        seq->seq3= newdataadr(fd, seq->seq3);
                        /* a patch: after introduction of effects with 3 input strips */
-                       if(seq->seq3==NULL) seq->seq3= seq->seq2;
+                       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) {
@@ -5011,7 +5053,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 {
@@ -5020,22 +5062,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;
                                }
                        }
@@ -5067,7 +5109,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);
 }
 
@@ -5080,7 +5122,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;
@@ -5122,9 +5164,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;
@@ -5188,26 +5230,26 @@ static void butspace_version_132(SpaceButs *buts)
 }
 
 /* note: file read without screens option G_FILE_NO_UI; 
  check lib pointers in call below */
* check lib pointers in call below */
 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;
                                                
@@ -5215,21 +5257,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;
                                                
@@ -5238,15 +5280,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;
@@ -5254,7 +5296,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;
                                                
@@ -5265,7 +5307,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);
@@ -5275,7 +5317,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;
                                                
@@ -5284,25 +5326,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;
@@ -5310,14 +5352,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);
@@ -5326,16 +5368,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;
                                                        }
                                                }
@@ -5345,12 +5387,14 @@ 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);
+                                               sclip->mask= newlibadr_us(fd, sc->id.lib, sclip->mask);
 
                                                sclip->scopes.track_preview = NULL;
+                                               sclip->draw_context = NULL;
                                                sclip->scopes.ok = 0;
                                        }
                                }
@@ -5365,17 +5409,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;
                                        }
                                }
@@ -5391,15 +5435,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;
@@ -5422,11 +5466,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;
@@ -5434,49 +5478,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); 
@@ -5484,22 +5528,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;
                                        
@@ -5510,17 +5554,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);
@@ -5529,11 +5573,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;
@@ -5545,7 +5593,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;
                                        
@@ -5556,37 +5604,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);
@@ -5594,12 +5642,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;
                                                }
                                        }
@@ -5607,10 +5655,11 @@ 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);
+                                       sclip->mask= restore_pointer_by_name(newmain, (ID *)sclip->mask, 1);
 
                                        sclip->scopes.ok = 0;
                                }
@@ -5629,7 +5678,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;
@@ -5637,8 +5686,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);
@@ -5671,13 +5720,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);
@@ -5685,7 +5734,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;
 }
 
@@ -5706,32 +5755,32 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
        sc->swap= 0;
        
        /* 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) {
+        * 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) {
                        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;
 
@@ -5741,24 +5790,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) {
@@ -5770,16 +5819,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);
                                }
@@ -5790,7 +5839,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);
@@ -5810,18 +5859,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;
@@ -5841,52 +5890,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) {
+                               /* 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) {
                                        cl_next= cl->next;
                                        cl->line= newdataadr(fd, cl->line);
                                        if (cl->line) {
@@ -5899,7 +5948,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...
@@ -5929,9 +5978,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);
@@ -5965,7 +6014,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;
        }
 }
@@ -5976,7 +6025,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
@@ -5988,7 +6037,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? */
@@ -6006,8 +6055,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);
@@ -6026,8 +6075,10 @@ static void direct_link_speaker(FileData *fd, Speaker *spk)
        spk->adt= newdataadr(fd, spk->adt);
        direct_link_animdata(fd, spk->adt);
 
-       /*spk->sound= newdataadr(fd, spk->sound);
-       direct_link_sound(fd, spk->sound);*/
+#if 0
+       spk->sound= newdataadr(fd, spk->sound);
+       direct_link_sound(fd, spk->sound);
+#endif
 }
 
 /* ************** READ SOUND ******************* */
@@ -6039,8 +6090,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;
        }
@@ -6054,8 +6104,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
                        
@@ -6077,25 +6127,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;
@@ -6116,13 +6166,27 @@ 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;
        }
 }
 
+static void direct_link_movieDopesheet(FileData *fd, MovieTrackingDopesheet *dopesheet)
+{
+       MovieTrackingDopesheetChannel *channel;
+
+       link_list(fd, &dopesheet->channels);
+
+       channel = dopesheet->channels.first;
+       while (channel) {
+               channel->track = newdataadr(fd, channel->track);
+
+               channel = channel->next;
+       }
+}
+
 static void direct_link_movieclip(FileData *fd, MovieClip *clip)
 {
        MovieTracking *tracking= &clip->tracking;
@@ -6130,10 +6194,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);
@@ -6152,12 +6216,14 @@ 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);
 
                object= object->next;
        }
+
+       direct_link_movieDopesheet(fd, &clip->tracking.dopesheet);
 }
 
 static void lib_link_movieclip(FileData *fd, Main *main)
@@ -6165,8 +6231,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);
 
@@ -6178,13 +6244,96 @@ static void lib_link_movieclip(FileData *fd, Main *main)
        }
 }
 
+/* ***************** READ MOVIECLIP *************** */
+
+static void direct_link_mask(FileData *fd, Mask *mask)
+{
+       MaskShape *shape;
+
+       mask->adt = newdataadr(fd, mask->adt);
+
+       link_list(fd, &mask->shapes);
+
+       shape = mask->shapes.first;
+       while (shape) {
+               MaskSpline *spline;
+
+               link_list(fd, &shape->splines);
+
+               spline = shape->splines.first;
+               while (spline) {
+                       int i;
+
+                       spline->points = newdataadr(fd, spline->points);
+
+                       for (i = 0; i < spline->tot_point; i++) {
+                               MaskSplinePoint *point = &spline->points[i];
+
+                               if (point->tot_uw)
+                                       point->uw = newdataadr(fd, point->uw);
+                       }
+
+                       spline = spline->next;
+               }
+
+               shape->act_spline = newdataadr(fd, shape->act_spline);
+               shape->act_point = newdataadr(fd, shape->act_point);
+
+               shape = shape->next;
+       }
+}
+
+static void lib_link_mask_parent(FileData *fd, Mask *mask, MaskParent *parent)
+{
+       parent->id = newlibadr_us(fd, mask->id.lib, parent->id);
+}
+
+static void lib_link_mask(FileData *fd, Main *main)
+{
+       Mask *mask;
+
+       mask = main->mask.first;
+       while (mask) {
+               if(mask->id.flag & LIB_NEEDLINK) {
+                       MaskShape *shape;
+
+                       if (mask->adt)
+                               lib_link_animdata(fd, &mask->id, mask->adt);
+
+                       shape = mask->shapes.first;
+                       while (shape) {
+                               MaskSpline *spline;
+
+                               spline = shape->splines.first;
+                               while (spline) {
+                                       int i;
+
+                                       for (i = 0; i < spline->tot_point; i++) {
+                                               MaskSplinePoint *point = &spline->points[i];
+
+                                               lib_link_mask_parent(fd, mask, &point->parent);
+                                       }
+
+                                       lib_link_mask_parent(fd, mask, &spline->parent);
+
+                                       spline = spline->next;
+                               }
+
+                               shape = shape->next;
+                       }
+
+                       mask->id.flag -= LIB_NEEDLINK;
+               }
+               mask = mask->id.next;
+       }
+}
+
 /* ************** GENERAL & MAIN ******************** */
 
 
 static const char *dataname(short id_code)
 {
-       
-       switch( id_code ) {
+       switch ( id_code ) {
                case ID_OB: return "Data from OB";
                case ID_ME: return "Data from ME";
                case ID_IP: return "Data from IP";
@@ -6222,7 +6371,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 */
@@ -6265,7 +6414,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 {
@@ -6277,13 +6426,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);
        }
 
@@ -6294,7 +6443,7 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
        bhead= read_data_into_oldnewmap(fd, bhead, allocname);
 
        /* init pointers direct data */
-       switch( GS(id->name) ) {
+       switch (GS(id->name)) {
                case ID_WM:
                        direct_link_windowmanager(fd, (wmWindowManager *)id);
                        break;
@@ -6385,6 +6534,9 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
                case ID_MC:
                        direct_link_movieclip(fd, (MovieClip *)id);
                        break;
+               case ID_MSK:
+                       direct_link_mask(fd, (Mask *)id);
+                       break;
        }
        
        /*link direct data of ID properties*/
@@ -6419,7 +6571,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;
@@ -6440,8 +6592,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;
        }
 }
 
@@ -6451,12 +6603,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];
@@ -6500,7 +6652,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);
 }
 
@@ -6508,13 +6660,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);
        }
@@ -6524,8 +6676,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);
        }
@@ -6535,10 +6687,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;
@@ -6546,8 +6698,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;
@@ -6563,49 +6715,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;
@@ -6704,11 +6831,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");
 
@@ -6741,27 +6868,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;
                                }
@@ -6804,27 +6931,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 {
@@ -6832,13 +6959,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;
                        }
                }
@@ -6852,7 +6979,7 @@ 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;
@@ -6895,9 +7022,9 @@ 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) {
+               switch (sl->spacetype) {
                        case SPACE_IPO:
                                ar= MEM_callocN(sizeof(ARegion), "area region from do_versions");
                                BLI_addtail(lb, ar);
@@ -7016,10 +7143,10 @@ 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) {
+               switch (sl->spacetype) {
                        case SPACE_VIEW3D:
                                view3d_split_250((View3D *)sl, lb);
                                break;          
@@ -7177,23 +7304,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 */
                }
                
@@ -7201,10 +7328,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);
                        }
@@ -7223,7 +7350,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);
 }
 
@@ -7233,25 +7360,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;
                                }
@@ -7259,7 +7386,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;
                                }
@@ -7277,8 +7404,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;
@@ -7287,9 +7414,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);
 }
@@ -7298,7 +7425,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;
@@ -7311,20 +7438,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;
@@ -7359,11 +7486,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;
                }
        }
@@ -7375,14 +7502,14 @@ 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 */
+                                        * into thinking that the mesh was not bound yet */
                                        mmd->bindcagecos= mmd->bindcos;
                                        mmd->bindcos= NULL;
 
@@ -7398,17 +7525,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);
@@ -7425,8 +7552,7 @@ static void do_version_constraints_radians_degrees_250(ListBase *lb)
 static void do_version_old_trackto_to_constraints(Object *ob)
 {
        /* create new trackto constraint from the relationship */
-       if (ob->track)
-       {
+       if (ob->track) {
                bConstraint *con= add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
                bTrackToConstraint *data = con->data;
                
@@ -7445,7 +7571,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(
@@ -7464,7 +7590,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);
 }
 
@@ -7489,7 +7615,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");
                }
 
@@ -7515,8 +7641,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;
@@ -7659,32 +7785,6 @@ static void do_versions_nodetree_socket_use_flags_2_62(bNodeTree *ntree)
        }
 }
 
-/* set the SOCK_AUTO_HIDDEN flag on collapsed nodes */
-static void do_versions_nodetree_socket_auto_hidden_flags_2_62(bNodeTree *ntree)
-{
-       bNode *node;
-       bNodeSocket *sock;
-       
-       for (node=ntree->nodes.first; node; node=node->next) {
-               if (node->flag & NODE_HIDDEN) {
-                       for (sock=node->inputs.first; sock; sock=sock->next) {
-                               if (sock->link==NULL)
-                                       sock->flag |= SOCK_AUTO_HIDDEN;
-                       }
-                       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)
-                               sock->flag &= ~SOCK_AUTO_HIDDEN;
-                       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;
@@ -7764,12 +7864,12 @@ static void do_versions_mesh_mloopcol_swap_2_62_1(Mesh *me)
        int a;
        int i;
 
-       for(a = 0; a < me->ldata.totlayer; a++) {
+       for (a = 0; a < me->ldata.totlayer; a++) {
                layer = &me->ldata.layers[a];
 
-               if(layer->type == CD_MLOOPCOL) {
+               if (layer->type == CD_MLOOPCOL) {
                        mloopcol = (MLoopCol *)layer->data;
-                       for(i = 0; i < me->totloop; i++, mloopcol++) {
+                       for (i = 0; i < me->totloop; i++, mloopcol++) {
                                SWAP(char, mloopcol->r, mloopcol->b);
                        }
                }
@@ -7780,17 +7880,17 @@ 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;
@@ -7801,87 +7901,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) {
@@ -7896,17 +7996,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) &&
@@ -7920,7 +8020,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) {
@@ -7935,54 +8035,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++;
                                }
@@ -7991,22 +8091,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) {
+     &nb