Merging r46111 through r46136 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenloader / intern / writefile.c
index fb484cc..0cd57ee 100644 (file)
@@ -81,14 +81,17 @@ Any case: direct data is ALWAYS after the lib block
 #include "zlib.h"
 
 #ifndef WIN32
-#include <unistd.h>
+#  include <unistd.h>
 #else
-#include "winsock2.h"
-#include <io.h>
-#include <process.h> // for getpid
-#include "BLI_winstuff.h"
+#  include "winsock2.h"
+#  include <io.h>
+#  include <process.h> // for getpid
+#  include "BLI_winstuff.h"
 #endif
 
+/* allow writefile to use deprecated functionality (for forward compatibility code) */
+#define DNA_DEPRECATED_ALLOW
+
 #include "DNA_anim_types.h"
 #include "DNA_armature_types.h"
 #include "DNA_actuator_types.h"
@@ -130,8 +133,10 @@ Any case: direct data is ALWAYS after the lib block
 #include "DNA_world_types.h"
 #include "DNA_windowmanager_types.h"
 #include "DNA_movieclip_types.h"
+#include "DNA_mask_types.h"
 
 #include "MEM_guardedalloc.h" // MEM_freeN
+#include "BLI_bitmap.h"
 #include "BLI_blenlib.h"
 #include "BLI_linklist.h"
 #include "BLI_bpath.h"
@@ -200,13 +205,13 @@ static WriteData *writedata_new(int file)
        return wd;
 }
 
-static void writedata_do_write(WriteData *wd, void *mem, int memlen)
+static void writedata_do_write(WriteData *wd, const void *mem, int memlen)
 {
        if ((wd == NULL) || wd->error || (mem == NULL) || memlen < 1) return;
        if (wd->error) return;
 
        /* memory based save */
-       if(wd->current) {
+       if (wd->current) {
                add_memfilechunk(NULL, wd->current, mem, memlen);
        }
        else {
@@ -228,20 +233,20 @@ static void writedata_free(WriteData *wd)
 
 /**
  * Low level WRITE(2) wrapper that buffers data
- * @param adr Pointer to new chunk of data
- * @param len Length of new chunk of data
- * @warning Talks to other functions with global parameters
+ * \param adr Pointer to new chunk of data
+ * \param len Length of new chunk of data
+ * \warning Talks to other functions with global parameters
  */
  
 #define MYWRITE_FLUSH          NULL
 
-static void mywrite( WriteData *wd, void *adr, int len)
+static void mywrite(WriteData *wd, const void *adr, int len)
 {
        if (wd->error) return;
 
        /* flush helps compression for undo-save */
-       if(adr==MYWRITE_FLUSH) {
-               if(wd->count) {
+       if (adr==MYWRITE_FLUSH) {
+               if (wd->count) {
                        writedata_do_write(wd, wd->buf, wd->count);
                        wd->count= 0;
                }
@@ -252,8 +257,8 @@ static void mywrite( WriteData *wd, void *adr, int len)
        
        /* if we have a single big chunk, write existing data in
         * buffer and write out big chunk in smaller pieces */
-       if(len>MYWRITE_MAX_CHUNK) {
-               if(wd->count) {
+       if (len>MYWRITE_MAX_CHUNK) {
+               if (wd->count) {
                        writedata_do_write(wd, wd->buf, wd->count);
                        wd->count= 0;
                }
@@ -261,15 +266,15 @@ static void mywrite( WriteData *wd, void *adr, int len)
                do {
                        int writelen= MIN2(len, MYWRITE_MAX_CHUNK);
                        writedata_do_write(wd, adr, writelen);
-                       adr = (char*)adr + writelen;
+                       adr = (const char *)adr + writelen;
                        len -= writelen;
-               } while(len > 0);
+               } while (len > 0);
 
                return;
        }
 
        /* if data would overflow buffer, write out the buffer */
-       if(len+wd->count>MYWRITE_BUFFER_SIZE-1) {
+       if (len+wd->count>MYWRITE_BUFFER_SIZE-1) {
                writedata_do_write(wd, wd->buf, wd->count);
                wd->count= 0;
        }
@@ -281,9 +286,10 @@ static void mywrite( WriteData *wd, void *adr, int len)
 
 /**
  * BeGiN initializer for mywrite
- * @param file File descriptor
- * @param write_flags Write parameters
- * @warning Talks to other functions with global parameters
+ * \param file File descriptor
+ * \param compare Previous memory file (can be NULL).
+ * \param current The current memory file (can be NULL).
+ * \warning Talks to other functions with global parameters
  */
 static WriteData *bgnwrite(int file, MemFile *compare, MemFile *current)
 {
@@ -301,9 +307,9 @@ static WriteData *bgnwrite(int file, MemFile *compare, MemFile *current)
 
 /**
  * END the mywrite wrapper
- * @return 1 if write failed
- * @return unknown global variable otherwise
- * @warning Talks to other functions with global parameters
+ * \return 1 if write failed
+ * \return unknown global variable otherwise
+ * \warning Talks to other functions with global parameters
  */
 static int endwrite(WriteData *wd)
 {
@@ -327,7 +333,7 @@ static void writestruct(WriteData *wd, int filecode, const char *structname, int
        BHead bh;
        short *sp;
 
-       if(adr==NULL || nr==0) return;
+       if (adr==NULL || nr==0) return;
 
        /* init BHead */
        bh.code= filecode;
@@ -335,7 +341,7 @@ static void writestruct(WriteData *wd, int filecode, const char *structname, int
        bh.nr= nr;
 
        bh.SDNAnr= DNA_struct_find_nr(wd->sdna, structname);
-       if(bh.SDNAnr== -1) {
+       if (bh.SDNAnr== -1) {
                printf("error: can't find SDNA code <%s>\n", structname);
                return;
        }
@@ -343,31 +349,31 @@ static void writestruct(WriteData *wd, int filecode, const char *structname, int
 
        bh.len= nr*wd->sdna->typelens[sp[0]];
 
-       if(bh.len==0) return;
+       if (bh.len==0) return;
 
        mywrite(wd, &bh, sizeof(BHead));
        mywrite(wd, adr, bh.len);
 }
 
-static void writedata(WriteData *wd, int filecode, int len, void *adr) /* do not use for structs */
+static void writedata(WriteData *wd, int filecode, int len, const void *adr)  /* do not use for structs */
 {
        BHead bh;
 
-       if(adr==NULL) return;
-       if(len==0) return;
+       if (adr==NULL) return;
+       if (len==0) return;
 
-       len+= 3;
-       len-= ( len % 4);
+       len += 3;
+       len -= (len % 4);
 
        /* init BHead */
-       bh.code= filecode;
-       bh.old= adr;
-       bh.nr= 1;
-       bh.SDNAnr= 0;
-       bh.len= len;
+       bh.code   = filecode;
+       bh.old    = (void *)adr;  /* this is safe to cast from const */
+       bh.nr     = 1;
+       bh.SDNAnr = 0;
+       bh.len    = len;
 
        mywrite(wd, &bh, sizeof(BHead));
-       if(len) mywrite(wd, adr, len);
+       if (len) mywrite(wd, adr, len);
 }
 
 /* *************** writing some direct data structs used in more code parts **************** */
@@ -381,11 +387,11 @@ static void IDP_WriteArray(IDProperty *prop, void *wd)
        if (prop->data.pointer) {
                writedata(wd, DATA, MEM_allocN_len(prop->data.pointer), prop->data.pointer);
 
-               if(prop->subtype == IDP_GROUP) {
+               if (prop->subtype == IDP_GROUP) {
                        IDProperty **array= prop->data.pointer;
                        int a;
 
-                       for(a=0; a<prop->len; a++)
+                       for (a=0; a<prop->len; a++)
                                IDP_WriteProperty(array[a], wd);
                }
        }
@@ -400,7 +406,7 @@ static void IDP_WriteIDPArray(IDProperty *prop, void *wd)
 
                writestruct(wd, DATA, "IDProperty", prop->len, array);
 
-               for(a=0; a<prop->len; a++)
+               for (a=0; a<prop->len; a++)
                        IDP_WriteProperty_OnlyData(&array[a], wd);
        }
 }
@@ -483,7 +489,7 @@ static void write_fmodifiers(WriteData *wd, ListBase *fmodifiers)
                                        FMod_Python *data = (FMod_Python *)fcm->data;
                                        
                                        /* Write ID Properties -- and copy this comment EXACTLY for easy finding
-                                        of library blocks that implement this.*/
+                                        of library blocks that implement this.*/
                                        IDP_WriteProperty(data->prop, wd);
                                }
                                        break;
@@ -543,7 +549,7 @@ static void write_actions(WriteData *wd, ListBase *idbase)
        bActionGroup *grp;
        TimeMarker *marker;
        
-       for(act=idbase->first; act; act= act->id.next) {
+       for (act=idbase->first; act; act= act->id.next) {
                if (act->id.us>0 || wd->current) {
                        writestruct(wd, ID_AC, "bAction", 1, act);
                        if (act->id.properties) IDP_WriteProperty(act->id.properties, wd);
@@ -644,7 +650,7 @@ static void write_curvemapping(WriteData *wd, CurveMapping *cumap)
        int a;
        
        writestruct(wd, DATA, "CurveMapping", 1, cumap);
-       for(a=0; a<CM_TOT; a++)
+       for (a=0; a<CM_TOT; a++)
                writestruct(wd, DATA, "CurveMapPoint", cumap->cm[a].totpoint, cumap->cm[a].curve);
 }
 
@@ -654,8 +660,8 @@ static void write_node_socket(WriteData *wd, bNodeSocket *sock)
 
        /* forward compatibility code, so older blenders still open */
        sock->stack_type = 1;
-
-       if(sock->default_value) {
+       
+       if (sock->default_value) {
                bNodeSocketValueFloat *valfloat;
                bNodeSocketValueVector *valvector;
                bNodeSocketValueRGBA *valrgba;
@@ -697,40 +703,46 @@ static void write_nodetree(WriteData *wd, bNodeTree *ntree)
        
        /* for link_list() speed, we write per list */
        
-       if(ntree->adt) write_animdata(wd, ntree->adt);
+       if (ntree->adt) write_animdata(wd, ntree->adt);
        
-       for(node= ntree->nodes.first; node; node= node->next)
+       for (node= ntree->nodes.first; node; node= node->next)
                writestruct(wd, DATA, "bNode", 1, node);
 
-       for(node= ntree->nodes.first; node; node= node->next) {
-               for(sock= node->inputs.first; sock; sock= sock->next)
+       for (node= ntree->nodes.first; node; node= node->next) {
+               for (sock= node->inputs.first; sock; sock= sock->next)
                        write_node_socket(wd, sock);
-               for(sock= node->outputs.first; sock; sock= sock->next)
+               for (sock= node->outputs.first; sock; sock= sock->next)
                        write_node_socket(wd, sock);
 
                
-               if(node->storage && node->type!=NODE_DYNAMIC) {
+               if (node->storage && node->type!=NODE_DYNAMIC) {
                        /* could be handlerized at some point, now only 1 exception still */
-                       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))
                                write_curvemapping(wd, node->storage);
-                       else if(ntree->type==NTREE_COMPOSIT && ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
+                       else if (ntree->type==NTREE_COMPOSIT && ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
                                write_curvemapping(wd, node->storage);
-                       else if(ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) )
+                       else if (ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) )
                                write_curvemapping(wd, node->storage);
-                       else if(ntree->type==NTREE_COMPOSIT && node->type==CMP_NODE_MOVIEDISTORTION)
-                               /* pass */ ;
+                       else if (ntree->type==NTREE_COMPOSIT && node->type==CMP_NODE_MOVIEDISTORTION)
+                               /* pass */;
                        else
                                writestruct(wd, DATA, node->typeinfo->storagename, 1, node->storage);
                }
+               
+               if (node->type==CMP_NODE_OUTPUT_FILE) {
+                       /* inputs have own storage data */
+                       for (sock=node->inputs.first; sock; sock=sock->next)
+                               writestruct(wd, DATA, "NodeImageMultiFileSocket", 1, sock->storage);
+               }
        }
        
-       for(link= ntree->links.first; link; link= link->next)
+       for (link= ntree->links.first; link; link= link->next)
                writestruct(wd, DATA, "bNodeLink", 1, link);
        
        /* external sockets */
-       for(sock= ntree->inputs.first; sock; sock= sock->next)
+       for (sock= ntree->inputs.first; sock; sock= sock->next)
                write_node_socket(wd, sock);
-       for(sock= ntree->outputs.first; sock; sock= sock->next)
+       for (sock= ntree->outputs.first; sock; sock= sock->next)
                write_node_socket(wd, sock);
 }
 
@@ -746,24 +758,30 @@ static void current_screen_compat(Main *mainvar, bScreen **screen)
        *screen= (window)? window->screen: NULL;
 }
 
+typedef struct RenderInfo {
+       int sfra;
+       int efra;
+       char scene_name[MAX_ID_NAME - 2];
+} RenderInfo;
+
 static void write_renderinfo(WriteData *wd, Main *mainvar)             /* for renderdeamon */
 {
        bScreen *curscreen;
        Scene *sce;
-       int data[8];
+       RenderInfo data;
 
        /* XXX in future, handle multiple windows with multiple screnes? */
        current_screen_compat(mainvar, &curscreen);
 
-       for(sce= mainvar->scene.first; sce; sce= sce->id.next) {
-               if(sce->id.lib==NULL  && ( sce==curscreen->scene || (sce->r.scemode & R_BG_RENDER)) ) {
-                       data[0]= sce->r.sfra;
-                       data[1]= sce->r.efra;
+       for (sce= mainvar->scene.first; sce; sce= sce->id.next) {
+               if (sce->id.lib==NULL  && ( sce==curscreen->scene || (sce->r.scemode & R_BG_RENDER)) ) {
+                       data.sfra = sce->r.sfra;
+                       data.efra = sce->r.efra;
+                       memset(data.scene_name, 0, sizeof(data.scene_name));
 
-                       memset(data+2, 0, sizeof(int)*6);
-                       BLI_strncpy((char *)(data+2), sce->id.name+2, sizeof(sce->id.name)-2);
+                       BLI_strncpy(data.scene_name, sce->id.name + 2, sizeof(data.scene_name));
 
-                       writedata(wd, REND, 32, data);
+                       writedata(wd, REND, sizeof(data), &data);
                }
        }
 }
@@ -771,7 +789,7 @@ static void write_renderinfo(WriteData *wd, Main *mainvar)          /* for renderdeamon
 static void write_keymapitem(WriteData *wd, wmKeyMapItem *kmi)
 {
        writestruct(wd, DATA, "wmKeyMapItem", 1, kmi);
-       if(kmi->properties)
+       if (kmi->properties)
                IDP_WriteProperty(kmi->properties, wd);
 }
 
@@ -786,28 +804,28 @@ static void write_userdef(WriteData *wd)
        
        writestruct(wd, USER, "UserDef", 1, &U);
 
-       for(btheme= U.themes.first; btheme; btheme=btheme->next)
+       for (btheme= U.themes.first; btheme; btheme=btheme->next)
                writestruct(wd, DATA, "bTheme", 1, btheme);
 
-       for(keymap= U.user_keymaps.first; keymap; keymap=keymap->next) {
+       for (keymap= U.user_keymaps.first; keymap; keymap=keymap->next) {
                writestruct(wd, DATA, "wmKeyMap", 1, keymap);
 
-               for(kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next) {
+               for (kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next) {
                        writestruct(wd, DATA, "wmKeyMapDiffItem", 1, kmdi);
-                       if(kmdi->remove_item)
+                       if (kmdi->remove_item)
                                write_keymapitem(wd, kmdi->remove_item);
-                       if(kmdi->add_item)
+                       if (kmdi->add_item)
                                write_keymapitem(wd, kmdi->add_item);
                }
 
-               for(kmi=keymap->items.first; kmi; kmi=kmi->next)
+               for (kmi=keymap->items.first; kmi; kmi=kmi->next)
                        write_keymapitem(wd, kmi);
        }
 
-       for(bext= U.addons.first; bext; bext=bext->next)
+       for (bext= U.addons.first; bext; bext=bext->next)
                writestruct(wd, DATA, "bAddon", 1, bext);
        
-       for(style= U.uistyles.first; style; style= style->next) {
+       for (style= U.uistyles.first; style; style= style->next) {
                writestruct(wd, DATA, "uiStyle", 1, style);
        }
 }
@@ -819,8 +837,8 @@ static void write_boid_state(WriteData *wd, BoidState *state)
 
        writestruct(wd, DATA, "BoidState", 1, state);
 
-       for(; rule; rule=rule->next) {
-               switch(rule->type) {
+       for (; rule; rule=rule->next) {
+               switch (rule->type) {
                        case eBoidRuleType_Goal:
                        case eBoidRuleType_Avoid:
                                writestruct(wd, DATA, "BoidRuleGoalAvoid", 1, rule);
@@ -842,7 +860,7 @@ static void write_boid_state(WriteData *wd, BoidState *state)
                                break;
                }
        }
-       //for(; cond; cond=cond->next)
+       //for (; cond; cond=cond->next)
        //      writestruct(wd, DATA, "BoidCondition", 1, cond);
 }
 
@@ -866,28 +884,28 @@ static void write_pointcaches(WriteData *wd, ListBase *ptcaches)
        PointCache *cache = ptcaches->first;
        int i;
 
-       for(; cache; cache=cache->next) {
+       for (; cache; cache=cache->next) {
                writestruct(wd, DATA, "PointCache", 1, cache);
 
-               if((cache->flag & PTCACHE_DISK_CACHE)==0) {
+               if ((cache->flag & PTCACHE_DISK_CACHE)==0) {
                        PTCacheMem *pm = cache->mem_cache.first;
 
-                       for(; pm; pm=pm->next) {
+                       for (; pm; pm=pm->next) {
                                PTCacheExtra *extra = pm->extradata.first;
 
                                writestruct(wd, DATA, "PTCacheMem", 1, pm);
                                
-                               for(i=0; i<BPHYS_TOT_DATA; i++) {
-                                       if(pm->data[i] && pm->data_types & (1<<i)) {
-                                               if(ptcache_data_struct[i][0]=='\0')
+                               for (i=0; i<BPHYS_TOT_DATA; i++) {
+                                       if (pm->data[i] && pm->data_types & (1<<i)) {
+                                               if (ptcache_data_struct[i][0]=='\0')
                                                        writedata(wd, DATA, MEM_allocN_len(pm->data[i]), pm->data[i]);
                                                else
                                                        writestruct(wd, DATA, ptcache_data_struct[i], pm->totpoint, pm->data[i]);
                                        }
                                }
 
-                               for(; extra; extra=extra->next) {
-                                       if(ptcache_extra_struct[extra->type][0]=='\0')
+                               for (; extra; extra=extra->next) {
+                                       if (ptcache_extra_struct[extra->type][0]=='\0')
                                                continue;
                                        writestruct(wd, DATA, "PTCacheExtra", 1, extra);
                                        writestruct(wd, DATA, ptcache_extra_struct[extra->type], extra->totdata, extra->data);
@@ -904,8 +922,8 @@ static void write_particlesettings(WriteData *wd, ListBase *idbase)
        int a;
 
        part= idbase->first;
-       while(part) {
-               if(part->id.us>0 || wd->current) {
+       while (part) {
+               if (part->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_PA, "ParticleSettings", 1, part);
                        if (part->id.properties) IDP_WriteProperty(part->id.properties, wd);
@@ -915,12 +933,12 @@ static void write_particlesettings(WriteData *wd, ListBase *idbase)
                        writestruct(wd, DATA, "EffectorWeights", 1, part->effector_weights);
 
                        dw = part->dupliweights.first;
-                       for(; dw; dw=dw->next) {
+                       for (; dw; dw=dw->next) {
                                /* update indices */
                                dw->index = 0;
-                               if(part->dup_group) { /* can be NULL if lining fails or set to None */
+                               if (part->dup_group) { /* can be NULL if lining fails or set to None */
                                        go = part->dup_group->gobject.first;
-                                       while(go && go->ob != dw->ob) {
+                                       while (go && go->ob != dw->ob) {
                                                go=go->next;
                                                dw->index++;
                                        }
@@ -928,20 +946,20 @@ static void write_particlesettings(WriteData *wd, ListBase *idbase)
                                writestruct(wd, DATA, "ParticleDupliWeight", 1, dw);
                        }
 
-                       if(part->boids && part->phystype == PART_PHYS_BOIDS) {
+                       if (part->boids && part->phystype == PART_PHYS_BOIDS) {
                                BoidState *state = part->boids->states.first;
 
                                writestruct(wd, DATA, "BoidSettings", 1, part->boids);
 
-                               for(; state; state=state->next)
+                               for (; state; state=state->next)
                                        write_boid_state(wd, state);
                        }
-                       if(part->fluid && part->phystype == PART_PHYS_FLUID){
+                       if (part->fluid && part->phystype == PART_PHYS_FLUID) {
                                writestruct(wd, DATA, "SPHFluidSettings", 1, part->fluid); 
                        }
 
-                       for(a=0; a<MAX_MTEX; a++) {
-                               if(part->mtex[a]) writestruct(wd, DATA, "MTex", 1, part->mtex[a]);
+                       for (a=0; a<MAX_MTEX; a++) {
+                               if (part->mtex[a]) writestruct(wd, DATA, "MTex", 1, part->mtex[a]);
                        }
                }
                part= part->id.next;
@@ -953,32 +971,32 @@ static void write_particlesystems(WriteData *wd, ListBase *particles)
        ParticleTarget *pt;
        int a;
 
-       for(; psys; psys=psys->next) {
+       for (; psys; psys=psys->next) {
                writestruct(wd, DATA, "ParticleSystem", 1, psys);
 
-               if(psys->particles) {
-                       writestruct(wd, DATA, "ParticleData", psys->totpart ,psys->particles);
+               if (psys->particles) {
+                       writestruct(wd, DATA, "ParticleData", psys->totpartpsys->particles);
 
-                       if(psys->particles->hair) {
+                       if (psys->particles->hair) {
                                ParticleData *pa = psys->particles;
 
-                               for(a=0; a<psys->totpart; a++, pa++)
+                               for (a=0; a<psys->totpart; a++, pa++)
                                        writestruct(wd, DATA, "HairKey", pa->totkey, pa->hair);
                        }
 
-                       if(psys->particles->boid && psys->part->phystype == PART_PHYS_BOIDS)
+                       if (psys->particles->boid && psys->part->phystype == PART_PHYS_BOIDS)
                                writestruct(wd, DATA, "BoidParticle", psys->totpart, psys->particles->boid);
 
-                       if(psys->part->fluid && psys->part->phystype == PART_PHYS_FLUID && (psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS))
+                       if (psys->part->fluid && psys->part->phystype == PART_PHYS_FLUID && (psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS))
                                writestruct(wd, DATA, "ParticleSpring", psys->tot_fluidsprings, psys->fluid_springs);
                }
                pt = psys->targets.first;
-               for(; pt; pt=pt->next)
+               for (; pt; pt=pt->next)
                        writestruct(wd, DATA, "ParticleTarget", 1, pt);
 
-               if(psys->child) writestruct(wd, DATA, "ChildParticle", psys->totchild ,psys->child);
+               if (psys->child) writestruct(wd, DATA, "ChildParticle", psys->totchild, psys->child);
 
-               if(psys->clmd) {
+               if (psys->clmd) {
                        writestruct(wd, DATA, "ClothModifierData", 1, psys->clmd);
                        writestruct(wd, DATA, "ClothSimSettings", 1, psys->clmd->sim_parms);
                        writestruct(wd, DATA, "ClothCollSettings", 1, psys->clmd->coll_parms);
@@ -993,10 +1011,10 @@ static void write_properties(WriteData *wd, ListBase *lb)
        bProperty *prop;
 
        prop= lb->first;
-       while(prop) {
+       while (prop) {
                writestruct(wd, DATA, "bProperty", 1, prop);
 
-               if(prop->poin && prop->poin != &prop->data)
+               if (prop->poin && prop->poin != &prop->data)
                        writedata(wd, DATA, MEM_allocN_len(prop->poin), prop->poin);
 
                prop= prop->next;
@@ -1008,12 +1026,12 @@ static void write_sensors(WriteData *wd, ListBase *lb)
        bSensor *sens;
 
        sens= lb->first;
-       while(sens) {
+       while (sens) {
                writestruct(wd, DATA, "bSensor", 1, sens);
 
                writedata(wd, DATA, sizeof(void *)*sens->totlinks, sens->links);
 
-               switch(sens->type) {
+               switch (sens->type) {
                case SENS_NEAR:
                        writestruct(wd, DATA, "bNearSensor", 1, sens->data);
                        break;
@@ -1069,12 +1087,12 @@ static void write_controllers(WriteData *wd, ListBase *lb)
        bController *cont;
 
        cont= lb->first;
-       while(cont) {
+       while (cont) {
                writestruct(wd, DATA, "bController", 1, cont);
 
                writedata(wd, DATA, sizeof(void *)*cont->totlinks, cont->links);
 
-               switch(cont->type) {
+               switch (cont->type) {
                case CONT_EXPRESSION:
                        writestruct(wd, DATA, "bExpressionCont", 1, cont->data);
                        break;
@@ -1094,10 +1112,10 @@ static void write_actuators(WriteData *wd, ListBase *lb)
        bActuator *act;
 
        act= lb->first;
-       while(act) {
+       while (act) {
                writestruct(wd, DATA, "bActuator", 1, act);
 
-               switch(act->type) {
+               switch (act->type) {
                case ACT_ACTION:
                case ACT_SHAPEACTION:
                        writestruct(wd, DATA, "bActionActuator", 1, act->data);
@@ -1201,7 +1219,7 @@ static void write_constraints(WriteData *wd, ListBase *conlist)
                                                writestruct(wd, DATA, "bConstraintTarget", 1, ct);
                                        
                                        /* Write ID Properties -- and copy this comment EXACTLY for easy finding
-                                        of library blocks that implement this.*/
+                                        of library blocks that implement this.*/
                                        IDP_WriteProperty(data->prop, wd);
                                }
                                        break;
@@ -1233,7 +1251,7 @@ static void write_pose(WriteData *wd, bPose *pose)
        /* Write channels */
        for (chan=pose->chanbase.first; chan; chan=chan->next) {
                /* Write ID Properties -- and copy this comment EXACTLY for easy finding
-                of library blocks that implement this.*/
+                of library blocks that implement this.*/
                if (chan->prop)
                        IDP_WriteProperty(chan->prop, wd);
                
@@ -1288,7 +1306,7 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
                        
                        writedata(wd, DATA, sizeof(int)*hmd->totindex, hmd->indexar);
                }
-               else if(md->type==eModifierType_Cloth) {
+               else if (md->type==eModifierType_Cloth) {
                        ClothModifierData *clmd = (ClothModifierData*) md;
                        
                        writestruct(wd, DATA, "ClothSimSettings", 1, clmd->sim_parms);
@@ -1296,13 +1314,11 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
                        writestruct(wd, DATA, "EffectorWeights", 1, clmd->sim_parms->effector_weights);
                        write_pointcaches(wd, &clmd->ptcaches);
                } 
-               else if(md->type==eModifierType_Smoke) {
+               else if (md->type==eModifierType_Smoke) {
                        SmokeModifierData *smd = (SmokeModifierData*) md;
                        
-                       if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
-                       {
-                               if(smd->domain)
-                               {
+                       if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
+                               if (smd->domain) {
                                        write_pointcaches(wd, &(smd->domain->ptcaches[0]));
 
                                        /* create fake pointcache so that old blender versions can read it */
@@ -1315,7 +1331,7 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
                                
                                writestruct(wd, DATA, "SmokeDomainSettings", 1, smd->domain);
 
-                               if(smd->domain) {
+                               if (smd->domain) {
                                        /* cleanup the fake pointcache */
                                        BKE_ptcache_free_list(&smd->domain->ptcaches[1]);
                                        smd->domain->point_cache[1] = NULL;
@@ -1323,21 +1339,20 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
                                        writestruct(wd, DATA, "EffectorWeights", 1, smd->domain->effector_weights);
                                }
                        }
-                       else if(smd->type & MOD_SMOKE_TYPE_FLOW)
+                       else if (smd->type & MOD_SMOKE_TYPE_FLOW)
                                writestruct(wd, DATA, "SmokeFlowSettings", 1, smd->flow);
-                       else if(smd->type & MOD_SMOKE_TYPE_COLL)
+                       else if (smd->type & MOD_SMOKE_TYPE_COLL)
                                writestruct(wd, DATA, "SmokeCollSettings", 1, smd->coll);
                } 
-               else if(md->type==eModifierType_Fluidsim) {
+               else if (md->type==eModifierType_Fluidsim) {
                        FluidsimModifierData *fluidmd = (FluidsimModifierData*) md;
                        
                        writestruct(wd, DATA, "FluidsimSettings", 1, fluidmd->fss);
                }
-               else if(md->type==eModifierType_DynamicPaint) {
+               else if (md->type==eModifierType_DynamicPaint) {
                        DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
                        
-                       if(pmd->canvas)
-                       {
+                       if (pmd->canvas) {
                                DynamicPaintSurface *surface;
                                writestruct(wd, DATA, "DynamicPaintCanvasSettings", 1, pmd->canvas);
                                
@@ -1351,8 +1366,7 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
                                        writestruct(wd, DATA, "EffectorWeights", 1, surface->effector_weights);
                                }
                        }
-                       if(pmd->brush)
-                       {
+                       if (pmd->brush) {
                                writestruct(wd, DATA, "DynamicPaintBrushSettings", 1, pmd->brush);
                                writestruct(wd, DATA, "ColorBand", 1, pmd->brush->paint_ramp);
                                writestruct(wd, DATA, "ColorBand", 1, pmd->brush->vel_ramp);
@@ -1383,7 +1397,7 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
                }
                else if (md->type==eModifierType_Warp) {
                        WarpModifierData *tmd = (WarpModifierData*) md;
-                       if(tmd->curfalloff) {
+                       if (tmd->curfalloff) {
                                write_curvemapping(wd, tmd->curfalloff);
                        }
                }
@@ -1401,13 +1415,13 @@ static void write_objects(WriteData *wd, ListBase *idbase)
        Object *ob;
        
        ob= idbase->first;
-       while(ob) {
-               if(ob->id.us>0 || wd->current) {
+       while (ob) {
+               if (ob->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_OB, "Object", 1, ob);
                        
-                       /*Write ID Properties -- and copy this comment EXACTLY for easy finding
-                         of library blocks that implement this.*/
+                       /* Write ID Properties -- and copy this comment EXACTLY for easy finding
+                        * of library blocks that implement this.*/
                        if (ob->id.properties) IDP_WriteProperty(ob->id.properties, wd);
                        
                        if (ob->adt) write_animdata(wd, ob->adt);
@@ -1435,7 +1449,7 @@ static void write_objects(WriteData *wd, ListBase *idbase)
                        
                        writestruct(wd, DATA, "PartDeflect", 1, ob->pd);
                        writestruct(wd, DATA, "SoftBody", 1, ob->soft);
-                       if(ob->soft) {
+                       if (ob->soft) {
                                write_pointcaches(wd, &ob->soft->ptcaches);
                                writestruct(wd, DATA, "EffectorWeights", 1, ob->soft->effector_weights);
                        }
@@ -1458,8 +1472,8 @@ static void write_vfonts(WriteData *wd, ListBase *idbase)
        PackedFile * pf;
 
        vf= idbase->first;
-       while(vf) {
-               if(vf->id.us>0 || wd->current) {
+       while (vf) {
+               if (vf->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_VF, "VFont", 1, vf);
                        if (vf->id.properties) IDP_WriteProperty(vf->id.properties, wd);
@@ -1484,8 +1498,8 @@ static void write_keys(WriteData *wd, ListBase *idbase)
        KeyBlock *kb;
 
        key= idbase->first;
-       while(key) {
-               if(key->id.us>0 || wd->current) {
+       while (key) {
+               if (key->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_KE, "Key", 1, key);
                        if (key->id.properties) IDP_WriteProperty(key->id.properties, wd);
@@ -1494,9 +1508,9 @@ static void write_keys(WriteData *wd, ListBase *idbase)
                        
                        /* direct data */
                        kb= key->block.first;
-                       while(kb) {
+                       while (kb) {
                                writestruct(wd, DATA, "KeyBlock", 1, kb);
-                               if(kb->data) writedata(wd, DATA, kb->totelem*key->elemsize, kb->data);
+                               if (kb->data) writedata(wd, DATA, kb->totelem*key->elemsize, kb->data);
                                kb= kb->next;
                        }
                }
@@ -1512,8 +1526,8 @@ static void write_cameras(WriteData *wd, ListBase *idbase)
        Camera *cam;
 
        cam= idbase->first;
-       while(cam) {
-               if(cam->id.us>0 || wd->current) {
+       while (cam) {
+               if (cam->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_CA, "Camera", 1, cam);
                        if (cam->id.properties) IDP_WriteProperty(cam->id.properties, wd);
@@ -1531,8 +1545,8 @@ static void write_mballs(WriteData *wd, ListBase *idbase)
        MetaElem *ml;
 
        mb= idbase->first;
-       while(mb) {
-               if(mb->id.us>0 || wd->current) {
+       while (mb) {
+               if (mb->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_MB, "MetaBall", 1, mb);
                        if (mb->id.properties) IDP_WriteProperty(mb->id.properties, wd);
@@ -1542,7 +1556,7 @@ static void write_mballs(WriteData *wd, ListBase *idbase)
                        if (mb->adt) write_animdata(wd, mb->adt);
 
                        ml= mb->elems.first;
-                       while(ml) {
+                       while (ml) {
                                writestruct(wd, DATA, "MetaElem", 1, ml);
                                ml= ml->next;
                        }
@@ -1564,8 +1578,8 @@ static void write_curves(WriteData *wd, ListBase *idbase)
        Nurb *nu;
 
        cu= idbase->first;
-       while(cu) {
-               if(cu->id.us>0 || wd->current) {
+       while (cu) {
+               if (cu->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_CU, "Curve", 1, cu);
                        
@@ -1574,7 +1588,7 @@ static void write_curves(WriteData *wd, ListBase *idbase)
                        if (cu->id.properties) IDP_WriteProperty(cu->id.properties, wd);
                        if (cu->adt) write_animdata(wd, cu->adt);
                        
-                       if(cu->vfont) {
+                       if (cu->vfont) {
                                writedata(wd, DATA, amount_of_chars(cu->str)+1, cu->str);
                                writestruct(wd, DATA, "CharInfo", cu->len+1, cu->strinfo);
                                writestruct(wd, DATA, "TextBox", cu->totbox, cu->tb);                           
@@ -1582,18 +1596,18 @@ static void write_curves(WriteData *wd, ListBase *idbase)
                        else {
                                /* is also the order of reading */
                                nu= cu->nurb.first;
-                               while(nu) {
+                               while (nu) {
                                        writestruct(wd, DATA, "Nurb", 1, nu);
                                        nu= nu->next;
                                }
                                nu= cu->nurb.first;
-                               while(nu) {
-                                       if(nu->type == CU_BEZIER)
+                               while (nu) {
+                                       if (nu->type == CU_BEZIER)
                                                writestruct(wd, DATA, "BezTriple", nu->pntsu, nu->bezt);
                                        else {
                                                writestruct(wd, DATA, "BPoint", nu->pntsu*nu->pntsv, nu->bp);
-                                               if(nu->knotsu) writedata(wd, DATA, KNOTSU(nu)*sizeof(float), nu->knotsu);
-                                               if(nu->knotsv) writedata(wd, DATA, KNOTSV(nu)*sizeof(float), nu->knotsv);
+                                               if (nu->knotsu) writedata(wd, DATA, KNOTSU(nu)*sizeof(float), nu->knotsu);
+                                               if (nu->knotsv) writedata(wd, DATA, KNOTSV(nu)*sizeof(float), nu->knotsv);
                                        }
                                        nu= nu->next;
                                }
@@ -1624,15 +1638,19 @@ static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
 
 static void write_mdisps(WriteData *wd, int count, MDisps *mdlist, int external)
 {
-       if(mdlist) {
+       if (mdlist) {
                int i;
                
                writestruct(wd, DATA, "MDisps", count, mdlist);
-               if(!external) {
-                       for(i = 0; i < count; ++i) {
-                               if(mdlist[i].disps)
-                                       writedata(wd, DATA, sizeof(float)*3*mdlist[i].totdisp, mdlist[i].disps);
+               for (i = 0; i < count; ++i) {
+                       MDisps *md = &mdlist[i];
+                       if (md->disps) {
+                               if (!external)
+                                       writedata(wd, DATA, sizeof(float)*3*md->totdisp, md->disps);
                        }
+                       
+                       if (md->hidden)
+                               writedata(wd, DATA, BLI_BITMAP_SIZE(md->totdisp), md->hidden);
                }
        }
 }
@@ -1642,7 +1660,7 @@ static void write_customdata(WriteData *wd, ID *id, int count, CustomData *data,
        int i;
 
        /* write external customdata (not for undo) */
-       if(data->external && !wd->current)
+       if (data->external && !wd->current)
                CustomData_external_write(data, id, CD_MASK_MESH, count, 0);
 
        writestruct(wd, DATA, "CustomDataLayer", data->maxlayer, data->layers);
@@ -1663,8 +1681,8 @@ static void write_customdata(WriteData *wd, ID *id, int count, CustomData *data,
                        CustomData_file_write_info(layer->type, &structname, &structnum);
                        if (structnum) {
                                /* when using partial visibility, the MEdge and MFace layers
-                                  are smaller than the original, so their type and count is
-                                  passed to make this work */
+                                * are smaller than the original, so their type and count is
+                                * passed to make this work */
                                if (layer->type != partial_type) datasize= structnum*count;
                                else datasize= structnum*partial_count;
 
@@ -1676,7 +1694,7 @@ static void write_customdata(WriteData *wd, ID *id, int count, CustomData *data,
                }
        }
 
-       if(data->external)
+       if (data->external)
                writestruct(wd, DATA, "CustomDataExternal", 1, data->external);
 }
 
@@ -1690,14 +1708,14 @@ static void write_meshs(WriteData *wd, ListBase *idbase)
 #endif
 
        mesh= idbase->first;
-       while(mesh) {
-               if(mesh->id.us>0 || wd->current) {
+       while (mesh) {
+               if (mesh->id.us>0 || wd->current) {
                        /* write LibData */
                        if (!save_for_old_blender) {
 
 #ifdef USE_BMESH_SAVE_WITHOUT_MFACE
                                Mesh backup_mesh = {{0}};
-                               /* cache only - dont write */
+                               /* cache only - don't write */
                                backup_mesh.mface = mesh->mface;
                                mesh->mface = NULL;
                                /* -- */
@@ -1713,6 +1731,7 @@ static void write_meshs(WriteData *wd, ListBase *idbase)
                                if (mesh->adt) write_animdata(wd, mesh->adt);
 
                                writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
+                               writedata(wd, DATA, sizeof(MSelect) * mesh->totselect, mesh->mselect);
 
                                write_customdata(wd, &mesh->id, mesh->totvert, &mesh->vdata, -1, 0);
                                write_customdata(wd, &mesh->id, mesh->totedge, &mesh->edata, -1, 0);
@@ -1722,7 +1741,7 @@ static void write_meshs(WriteData *wd, ListBase *idbase)
                                write_customdata(wd, &mesh->id, mesh->totpoly, &mesh->pdata, -1, 0);
 
 #ifdef USE_BMESH_SAVE_WITHOUT_MFACE
-                               /* cache only - dont write */
+                               /* cache only - don't write */
                                mesh->mface = backup_mesh.mface;
                                /* -- */
                                mesh->totface = backup_mesh.totface;
@@ -1778,15 +1797,16 @@ static void write_meshs(WriteData *wd, ListBase *idbase)
                                if (mesh->adt) write_animdata(wd, mesh->adt);
 
                                writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
+                               /* writedata(wd, DATA, sizeof(MSelect) * mesh->totselect, mesh->mselect); */ /* pre-bmesh NULL's */
 
                                write_customdata(wd, &mesh->id, mesh->totvert, &mesh->vdata, -1, 0);
                                write_customdata(wd, &mesh->id, mesh->totedge, &mesh->edata, -1, 0);
                                write_customdata(wd, &mesh->id, mesh->totface, &mesh->fdata, -1, 0);
                                /* harmless for older blender versioins but _not_ writing these keeps file size down */
-                               /*
+#if 0
                                write_customdata(wd, &mesh->id, mesh->totloop, &mesh->ldata, -1, 0);
                                write_customdata(wd, &mesh->id, mesh->totpoly, &mesh->pdata, -1, 0);
-                               */
+#endif
 
                                /* restore */
                                mesh->mpoly = backup_mesh.mpoly;
@@ -1822,8 +1842,8 @@ static void write_lattices(WriteData *wd, ListBase *idbase)
        Lattice *lt;
        
        lt= idbase->first;
-       while(lt) {
-               if(lt->id.us>0 || wd->current) {
+       while (lt) {
+               if (lt->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_LT, "Lattice", 1, lt);
                        if (lt->id.properties) IDP_WriteProperty(lt->id.properties, wd);
@@ -1848,7 +1868,7 @@ static void write_previews(WriteData *wd, PreviewImage *prv)
                short h = prv->h[1];
                unsigned int *rect = prv->rect[1];
                /* don't write out large previews if not requested */
-               if (!(U.flag & USER_SAVE_PREVIEWS) ) {
+               if (!(U.flag & USER_SAVE_PREVIEWS)) {
                        prv->w[1] = 0;
                        prv->h[1] = 0;
                        prv->rect[1] = NULL;
@@ -1873,8 +1893,8 @@ static void write_images(WriteData *wd, ListBase *idbase)
 
 
        ima= idbase->first;
-       while(ima) {
-               if(ima->id.us>0 || wd->current) {
+       while (ima) {
+               if (ima->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_IM, "Image", 1, ima);
                        if (ima->id.properties) IDP_WriteProperty(ima->id.properties, wd);
@@ -1898,8 +1918,8 @@ static void write_textures(WriteData *wd, ListBase *idbase)
        Tex *tex;
 
        tex= idbase->first;
-       while(tex) {
-               if(tex->id.us>0 || wd->current) {
+       while (tex) {
+               if (tex->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_TE, "Tex", 1, tex);
                        if (tex->id.properties) IDP_WriteProperty(tex->id.properties, wd);
@@ -1907,19 +1927,19 @@ static void write_textures(WriteData *wd, ListBase *idbase)
                        if (tex->adt) write_animdata(wd, tex->adt);
 
                        /* direct data */
-                       if(tex->type == TEX_PLUGIN && tex->plugin) writestruct(wd, DATA, "PluginTex", 1, tex->plugin);
-                       if(tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
-                       if(tex->type == TEX_ENVMAP && tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
-                       if(tex->type == TEX_POINTDENSITY && tex->pd) {
+                       if (tex->type == TEX_PLUGIN && tex->plugin) writestruct(wd, DATA, "PluginTex", 1, tex->plugin);
+                       if (tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
+                       if (tex->type == TEX_ENVMAP && tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
+                       if (tex->type == TEX_POINTDENSITY && tex->pd) {
                                writestruct(wd, DATA, "PointDensity", 1, tex->pd);
-                               if(tex->pd->coba) writestruct(wd, DATA, "ColorBand", 1, tex->pd->coba);
-                               if(tex->pd->falloff_curve) write_curvemapping(wd, tex->pd->falloff_curve);
+                               if (tex->pd->coba) writestruct(wd, DATA, "ColorBand", 1, tex->pd->coba);
+                               if (tex->pd->falloff_curve) write_curvemapping(wd, tex->pd->falloff_curve);
                        }
-                       if(tex->type == TEX_VOXELDATA) writestruct(wd, DATA, "VoxelData", 1, tex->vd);
-                       if(tex->type == TEX_OCEAN && tex->ot) writestruct(wd, DATA, "OceanTex", 1, tex->ot);
+                       if (tex->type == TEX_VOXELDATA) writestruct(wd, DATA, "VoxelData", 1, tex->vd);
+                       if (tex->type == TEX_OCEAN && tex->ot) writestruct(wd, DATA, "OceanTex", 1, tex->ot);
                        
                        /* nodetree is integral part of texture, no libdata */
-                       if(tex->nodetree) {
+                       if (tex->nodetree) {
                                writestruct(wd, DATA, "bNodeTree", 1, tex->nodetree);
                                write_nodetree(wd, tex->nodetree);
                        }
@@ -1939,28 +1959,28 @@ static void write_materials(WriteData *wd, ListBase *idbase)
        int a;
 
        ma= idbase->first;
-       while(ma) {
-               if(ma->id.us>0 || wd->current) {
+       while (ma) {
+               if (ma->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_MA, "Material", 1, ma);
                        
-                       /*Write ID Properties -- and copy this comment EXACTLY for easy finding
-                         of library blocks that implement this.*/
-                       /*manually set head group property to IDP_GROUP, just in case it hadn't been
-                         set yet :) */
+                       /* Write ID Properties -- and copy this comment EXACTLY for easy finding
+                        * of library blocks that implement this.*/
+                       /* manually set head group property to IDP_GROUP, just in case it hadn't been
+                        * set yet :) */
                        if (ma->id.properties) IDP_WriteProperty(ma->id.properties, wd);
                        
                        if (ma->adt) write_animdata(wd, ma->adt);
 
-                       for(a=0; a<MAX_MTEX; a++) {
-                               if(ma->mtex[a]) writestruct(wd, DATA, "MTex", 1, ma->mtex[a]);
+                       for (a=0; a<MAX_MTEX; a++) {
+                               if (ma->mtex[a]) writestruct(wd, DATA, "MTex", 1, ma->mtex[a]);
                        }
                        
-                       if(ma->ramp_col) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_col);
-                       if(ma->ramp_spec) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_spec);
+                       if (ma->ramp_col) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_col);
+                       if (ma->ramp_spec) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_spec);
                        
                        /* nodetree is integral part of material, no libdata */
-                       if(ma->nodetree) {
+                       if (ma->nodetree) {
                                writestruct(wd, DATA, "bNodeTree", 1, ma->nodetree);
                                write_nodetree(wd, ma->nodetree);
                        }
@@ -1977,20 +1997,20 @@ static void write_worlds(WriteData *wd, ListBase *idbase)
        int a;
 
        wrld= idbase->first;
-       while(wrld) {
-               if(wrld->id.us>0 || wd->current) {
+       while (wrld) {
+               if (wrld->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_WO, "World", 1, wrld);
                        if (wrld->id.properties) IDP_WriteProperty(wrld->id.properties, wd);
                        
                        if (wrld->adt) write_animdata(wd, wrld->adt);
                        
-                       for(a=0; a<MAX_MTEX; a++) {
-                               if(wrld->mtex[a]) writestruct(wd, DATA, "MTex", 1, wrld->mtex[a]);
+                       for (a=0; a<MAX_MTEX; a++) {
+                               if (wrld->mtex[a]) writestruct(wd, DATA, "MTex", 1, wrld->mtex[a]);
                        }
 
                        /* nodetree is integral part of lamps, no libdata */
-                       if(wrld->nodetree) {
+                       if (wrld->nodetree) {
                                writestruct(wd, DATA, "bNodeTree", 1, wrld->nodetree);
                                write_nodetree(wd, wrld->nodetree);
                        }
@@ -2007,8 +2027,8 @@ static void write_lamps(WriteData *wd, ListBase *idbase)
        int a;
 
        la= idbase->first;
-       while(la) {
-               if(la->id.us>0 || wd->current) {
+       while (la) {
+               if (la->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_LA, "Lamp", 1, la);
                        if (la->id.properties) IDP_WriteProperty(la->id.properties, wd);
@@ -2016,15 +2036,15 @@ static void write_lamps(WriteData *wd, ListBase *idbase)
                        if (la->adt) write_animdata(wd, la->adt);
                        
                        /* direct data */
-                       for(a=0; a<MAX_MTEX; a++) {
-                               if(la->mtex[a]) writestruct(wd, DATA, "MTex", 1, la->mtex[a]);
+                       for (a=0; a<MAX_MTEX; a++) {
+                               if (la->mtex[a]) writestruct(wd, DATA, "MTex", 1, la->mtex[a]);
                        }
                        
-                       if(la->curfalloff)
+                       if (la->curfalloff)
                                write_curvemapping(wd, la->curfalloff); 
                        
                        /* nodetree is integral part of lamps, no libdata */
-                       if(la->nodetree) {
+                       if (la->nodetree) {
                                writestruct(wd, DATA, "bNodeTree", 1, la->nodetree);
                                write_nodetree(wd, la->nodetree);
                        }
@@ -2051,7 +2071,7 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
        ToolSettings *tos;
        
        sce= scebase->first;
-       while(sce) {
+       while (sce) {
                /* write LibData */
                writestruct(wd, ID_SCE, "Scene", 1, sce);
                if (sce->id.properties) IDP_WriteProperty(sce->id.properties, wd);
@@ -2061,47 +2081,49 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
                
                /* direct data */
                base= sce->base.first;
-               while(base) {
+               while (base) {
                        writestruct(wd, DATA, "Base", 1, base);
                        base= base->next;
                }
                
                tos = sce->toolsettings;
                writestruct(wd, DATA, "ToolSettings", 1, tos);
-               if(tos->vpaint) {
+               if (tos->vpaint) {
                        writestruct(wd, DATA, "VPaint", 1, tos->vpaint);
                }
-               if(tos->wpaint) {
+               if (tos->wpaint) {
                        writestruct(wd, DATA, "VPaint", 1, tos->wpaint);
                }
-               if(tos->sculpt) {
+               if (tos->sculpt) {
                        writestruct(wd, DATA, "Sculpt", 1, tos->sculpt);
                }
-               if(tos->uvsculpt) {
+               if (tos->uvsculpt) {
                        writestruct(wd, DATA, "UvSculpt", 1, tos->uvsculpt);
                }
 
                // write_paint(wd, &tos->imapaint.paint);
 
                ed= sce->ed;
-               if(ed) {
+               if (ed) {
                        writestruct(wd, DATA, "Editing", 1, ed);
                        
                        /* reset write flags too */
                        
-                       SEQ_BEGIN(ed, seq) {
-                               if(seq->strip) seq->strip->done= 0;
+                       SEQ_BEGIN (ed, seq)
+                       {
+                               if (seq->strip) seq->strip->done= 0;
                                writestruct(wd, DATA, "Sequence", 1, seq);
                        }
                        SEQ_END
                        
-                       SEQ_BEGIN(ed, seq) {
-                               if(seq->strip && seq->strip->done==0) {
+                       SEQ_BEGIN (ed, seq)
+                       {
+                               if (seq->strip && seq->strip->done==0) {
                                        /* write strip with 'done' at 0 because readfile */
                                        
-                                       if(seq->plugin) writestruct(wd, DATA, "PluginSeq", 1, seq->plugin);
-                                       if(seq->effectdata) {
-                                               switch(seq->type){
+                                       if (seq->plugin) writestruct(wd, DATA, "PluginSeq", 1, seq->plugin);
+                                       if (seq->effectdata) {
+                                               switch (seq->type) {
                                                case SEQ_COLOR:
                                                        writestruct(wd, DATA, "SolidColorVars", 1, seq->effectdata);
                                                        break;
@@ -2122,21 +2144,21 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
                                        
                                        strip= seq->strip;
                                        writestruct(wd, DATA, "Strip", 1, strip);
-                                       if(seq->flag & SEQ_USE_CROP && strip->crop) {
+                                       if (seq->flag & SEQ_USE_CROP && strip->crop) {
                                                writestruct(wd, DATA, "StripCrop", 1, strip->crop);
                                        }
-                                       if(seq->flag & SEQ_USE_TRANSFORM && strip->transform) {
+                                       if (seq->flag & SEQ_USE_TRANSFORM && strip->transform) {
                                                writestruct(wd, DATA, "StripTransform", 1, strip->transform);
                                        }
-                                       if(seq->flag & SEQ_USE_PROXY && strip->proxy) {
+                                       if (seq->flag & SEQ_USE_PROXY && strip->proxy) {
                                                writestruct(wd, DATA, "StripProxy", 1, strip->proxy);
                                        }
-                                       if(seq->flag & SEQ_USE_COLOR_BALANCE && strip->color_balance) {
+                                       if (seq->flag & SEQ_USE_COLOR_BALANCE && strip->color_balance) {
                                                writestruct(wd, DATA, "StripColorBalance", 1, strip->color_balance);
                                        }
-                                       if(seq->type==SEQ_IMAGE)
+                                       if (seq->type==SEQ_IMAGE)
                                                writestruct(wd, DATA, "StripElem", MEM_allocN_len(strip->stripdata) / sizeof(struct StripElem), strip->stripdata);
-                                       else if(seq->type==SEQ_MOVIE || seq->type==SEQ_RAM_SOUND || seq->type == SEQ_HD_SOUND)
+                                       else if (seq->type==SEQ_MOVIE || seq->type==SEQ_RAM_SOUND || seq->type == SEQ_HD_SOUND)
                                                writestruct(wd, DATA, "StripElem", 1, strip->stripdata);
                                        
                                        strip->done= 1;
@@ -2145,7 +2167,7 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
                        SEQ_END
                                
                        /* new; meta stack too, even when its nasty restore code */
-                       for(ms= ed->metastack.first; ms; ms= ms->next) {
+                       for (ms= ed->metastack.first; ms; ms= ms->next) {
                                writestruct(wd, DATA, "MetaStack", 1, ms);
                        }
                }
@@ -2165,17 +2187,17 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
                }
 
                /* writing dynamic list of TimeMarkers to the blend file */
-               for(marker= sce->markers.first; marker; marker= marker->next)
+               for (marker= sce->markers.first; marker; marker= marker->next)
                        writestruct(wd, DATA, "TimeMarker", 1, marker);
                
                /* writing dynamic list of TransformOrientations to the blend file */
-               for(ts = sce->transform_spaces.first; ts; ts = ts->next)
+               for (ts = sce->transform_spaces.first; ts; ts = ts->next)
                        writestruct(wd, DATA, "TransformOrientation", 1, ts);
                
-               for(srl= sce->r.layers.first; srl; srl= srl->next)
+               for (srl= sce->r.layers.first; srl; srl= srl->next)
                        writestruct(wd, DATA, "SceneRenderLayer", 1, srl);
                
-               if(sce->nodetree) {
+               if (sce->nodetree) {
                        writestruct(wd, DATA, "bNodeTree", 1, sce->nodetree);
                        write_nodetree(wd, sce->nodetree);
                }
@@ -2222,10 +2244,10 @@ static void write_windowmanagers(WriteData *wd, ListBase *lb)
        wmWindowManager *wm;
        wmWindow *win;
        
-       for(wm= lb->first; wm; wm= wm->id.next) {
+       for (wm= lb->first; wm; wm= wm->id.next) {
                writestruct(wd, ID_WM, "wmWindowManager", 1, wm);
                
-               for(win= wm->windows.first; win; win= win->next)
+               for (win= wm->windows.first; win; win= win->next)
                        writestruct(wd, DATA, "wmWindow", 1, win);
        }
 }
@@ -2234,16 +2256,16 @@ static void write_region(WriteData *wd, ARegion *ar, int spacetype)
 {      
        writestruct(wd, DATA, "ARegion", 1, ar);
        
-       if(ar->regiondata) {
-               switch(spacetype) {
+       if (ar->regiondata) {
+               switch (spacetype) {
                        case SPACE_VIEW3D:
-                               if(ar->regiontype==RGN_TYPE_WINDOW) {
+                               if (ar->regiontype==RGN_TYPE_WINDOW) {
                                        RegionView3D *rv3d= ar->regiondata;
                                        writestruct(wd, DATA, "RegionView3D", 1, rv3d);
                                        
-                                       if(rv3d->localvd)
+                                       if (rv3d->localvd)
                                                writestruct(wd, DATA, "RegionView3D", 1, rv3d->localvd);
-                                       if(rv3d->clipbb) 
+                                       if (rv3d->clipbb)
                                                writestruct(wd, DATA, "BoundBox", 1, rv3d->clipbb);
 
                                }
@@ -2264,7 +2286,7 @@ static void write_screens(WriteData *wd, ListBase *scrbase)
        ScrEdge *se;
 
        sc= scrbase->first;
-       while(sc) {
+       while (sc) {
                
                /* write LibData */
                /* in 2.50+ files, the file identifier for screens is patched, forward compatibility */
@@ -2273,40 +2295,40 @@ static void write_screens(WriteData *wd, ListBase *scrbase)
                        IDP_WriteProperty(sc->id.properties, wd);
                
                /* direct data */
-               for(sv= sc->vertbase.first; sv; sv= sv->next)
+               for (sv= sc->vertbase.first; sv; sv= sv->next)
                        writestruct(wd, DATA, "ScrVert", 1, sv);
                
-               for(se= sc->edgebase.first; se; se= se->next) 
+               for (se= sc->edgebase.first; se; se= se->next)
                        writestruct(wd, DATA, "ScrEdge", 1, se);
                
-               for(sa= sc->areabase.first; sa; sa= sa->next) {
+               for (sa= sc->areabase.first; sa; sa= sa->next) {
                        SpaceLink *sl;
                        Panel *pa;
                        ARegion *ar;
                        
                        writestruct(wd, DATA, "ScrArea", 1, sa);
                        
-                       for(ar= sa->regionbase.first; ar; ar= ar->next) {
+                       for (ar= sa->regionbase.first; ar; ar= ar->next) {
                                write_region(wd, ar, sa->spacetype);
                                
-                               for(pa= ar->panels.first; pa; pa= pa->next)
+                               for (pa= ar->panels.first; pa; pa= pa->next)
                                        writestruct(wd, DATA, "Panel", 1, pa);
                        }
                        
                        sl= sa->spacedata.first;
-                       while(sl) {
-                               for(ar= sl->regionbase.first; ar; ar= ar->next)
+                       while (sl) {
+                               for (ar= sl->regionbase.first; ar; ar= ar->next)
                                        write_region(wd, ar, sl->spacetype);
                                
-                               if(sl->spacetype==SPACE_VIEW3D) {
+                               if (sl->spacetype==SPACE_VIEW3D) {
                                        View3D *v3d= (View3D *) sl;
                                        BGpic *bgpic;
                                        writestruct(wd, DATA, "View3D", 1, v3d);
                                        for (bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
                                                writestruct(wd, DATA, "BGpic", 1, bgpic);
-                                       if(v3d->localvd) writestruct(wd, DATA, "View3D", 1, v3d->localvd);
+                                       if (v3d->localvd) writestruct(wd, DATA, "View3D", 1, v3d->localvd);
                                }
-                               else if(sl->spacetype==SPACE_IPO) {
+                               else if (sl->spacetype==SPACE_IPO) {
                                        SpaceIpo *sipo= (SpaceIpo *)sl;
                                        ListBase tmpGhosts = sipo->ghostCurves;
                                        
@@ -2314,70 +2336,70 @@ static void write_screens(WriteData *wd, ListBase *scrbase)
                                        sipo->ghostCurves.first= sipo->ghostCurves.last= NULL;
                                        
                                        writestruct(wd, DATA, "SpaceIpo", 1, sl);
-                                       if(sipo->ads) writestruct(wd, DATA, "bDopeSheet", 1, sipo->ads);
+                                       if (sipo->ads) writestruct(wd, DATA, "bDopeSheet", 1, sipo->ads);
                                        
                                        /* reenable ghost curves */
                                        sipo->ghostCurves= tmpGhosts;
                                }
-                               else if(sl->spacetype==SPACE_BUTS) {
+                               else if (sl->spacetype==SPACE_BUTS) {
                                        writestruct(wd, DATA, "SpaceButs", 1, sl);
                                }
-                               else if(sl->spacetype==SPACE_FILE) {
+                               else if (sl->spacetype==SPACE_FILE) {
                                        SpaceFile *sfile= (SpaceFile *)sl;
 
                                        writestruct(wd, DATA, "SpaceFile", 1, sl);
-                                       if(sfile->params)
+                                       if (sfile->params)
                                                writestruct(wd, DATA, "FileSelectParams", 1, sfile->params);
                                }
-                               else if(sl->spacetype==SPACE_SEQ) {
+                               else if (sl->spacetype==SPACE_SEQ) {
                                        writestruct(wd, DATA, "SpaceSeq", 1, sl);
                                }
-                               else if(sl->spacetype==SPACE_OUTLINER) {
+                               else if (sl->spacetype==SPACE_OUTLINER) {
                                        SpaceOops *so= (SpaceOops *)sl;
                                        
                                        writestruct(wd, DATA, "SpaceOops", 1, so);
 
                                        /* outliner */
-                                       if(so->treestore) {
+                                       if (so->treestore) {
                                                writestruct(wd, DATA, "TreeStore", 1, so->treestore);
-                                               if(so->treestore->data)
+                                               if (so->treestore->data)
                                                        writestruct(wd, DATA, "TreeStoreElem", so->treestore->usedelem, so->treestore->data);
                                        }
                                }
-                               else if(sl->spacetype==SPACE_IMAGE) {
+                               else if (sl->spacetype==SPACE_IMAGE) {
                                        SpaceImage *sima= (SpaceImage *)sl;
                                        
                                        writestruct(wd, DATA, "SpaceImage", 1, sl);
-                                       if(sima->cumap)
+                                       if (sima->cumap)
                                                write_curvemapping(wd, sima->cumap);
                                }
-                               else if(sl->spacetype==SPACE_TEXT) {
+                               else if (sl->spacetype==SPACE_TEXT) {
                                        writestruct(wd, DATA, "SpaceText", 1, sl);
                                }
-                               else if(sl->spacetype==SPACE_SCRIPT) {
+                               else if (sl->spacetype==SPACE_SCRIPT) {
                                        SpaceScript *scr = (SpaceScript*)sl;
                                        scr->but_refs = NULL;
                                        writestruct(wd, DATA, "SpaceScript", 1, sl);
                                }
-                               else if(sl->spacetype==SPACE_ACTION) {
+                               else if (sl->spacetype==SPACE_ACTION) {
                                        writestruct(wd, DATA, "SpaceAction", 1, sl);
                                }
-                               else if(sl->spacetype==SPACE_NLA){
+                               else if (sl->spacetype==SPACE_NLA) {
                                        SpaceNla *snla= (SpaceNla *)sl;
                                        
                                        writestruct(wd, DATA, "SpaceNla", 1, snla);
-                                       if(snla->ads) writestruct(wd, DATA, "bDopeSheet", 1, snla->ads);
+                                       if (snla->ads) writestruct(wd, DATA, "bDopeSheet", 1, snla->ads);
                                }
-                               else if(sl->spacetype==SPACE_TIME){
+                               else if (sl->spacetype==SPACE_TIME) {
                                        writestruct(wd, DATA, "SpaceTime", 1, sl);
                                }
-                               else if(sl->spacetype==SPACE_NODE){
+                               else if (sl->spacetype==SPACE_NODE) {
                                        writestruct(wd, DATA, "SpaceNode", 1, sl);
                                }
-                               else if(sl->spacetype==SPACE_LOGIC){
+                               else if (sl->spacetype==SPACE_LOGIC) {
                                        writestruct(wd, DATA, "SpaceLogic", 1, sl);
                                }
-                               else if(sl->spacetype==SPACE_CONSOLE) {
+                               else if (sl->spacetype==SPACE_CONSOLE) {
                                        SpaceConsole *con = (SpaceConsole*)sl;
                                        ConsoleLine *cl;
 
@@ -2389,10 +2411,10 @@ static void write_screens(WriteData *wd, ListBase *scrbase)
                                        writestruct(wd, DATA, "SpaceConsole", 1, sl);
 
                                }
-                               else if(sl->spacetype==SPACE_USERPREF) {
+                               else if (sl->spacetype==SPACE_USERPREF) {
                                        writestruct(wd, DATA, "SpaceUserPref", 1, sl);
                                }
-                               else if(sl->spacetype==SPACE_CLIP) {
+                               else if (sl->spacetype==SPACE_CLIP) {
                                        writestruct(wd, DATA, "SpaceClip", 1, sl);
                                }
 
@@ -2410,28 +2432,28 @@ static void write_libraries(WriteData *wd, Main *main)
        ID *id;
        int a, tot, foundone;
 
-       for(; main; main= main->next) {
+       for (; main; main= main->next) {
 
                a=tot= set_listbasepointers(main, lbarray);
 
                /* test: is lib being used */
                foundone= 0;
-               while(tot--) {
-                       for(id= lbarray[tot]->first; id; id= id->next) {
-                               if(id->us>0 && (id->flag & LIB_EXTERN)) {
+               while (tot--) {
+                       for (id= lbarray[tot]->first; id; id= id->next) {
+                               if (id->us>0 && (id->flag & LIB_EXTERN)) {
                                        foundone= 1;
                                        break;
                                }
                        }
-                       if(foundone) break;
+                       if (foundone) break;
                }
 
-               if(foundone) {
+               if (foundone) {
                        writestruct(wd, ID_LI, "Library", 1, main->curlib);
 
-                       while(a--) {
-                               for(id= lbarray[a]->first; id; id= id->next) {
-                                       if(id->us>0 && (id->flag & LIB_EXTERN)) {
+                       while (a--) {
+                               for (id= lbarray[a]->first; id; id= id->next) {
+                                       if (id->us>0 && (id->flag & LIB_EXTERN)) {
                                                writestruct(wd, ID_ID, "ID", 1, id);
                                        }
                                }
@@ -2451,13 +2473,13 @@ static void write_bone(WriteData *wd, Bone* bone)
        writestruct(wd, DATA, "Bone", 1, bone);
 
        /* Write ID Properties -- and copy this comment EXACTLY for easy finding
-        of library blocks that implement this.*/
+        of library blocks that implement this.*/
        if (bone->prop)
                IDP_WriteProperty(bone->prop, wd);
        
        // Write Children
        cbone= bone->childbase.first;
-       while(cbone) {
+       while (cbone) {
                write_bone(wd, cbone);
                cbone= cbone->next;
        }
@@ -2478,7 +2500,7 @@ static void write_armatures(WriteData *wd, ListBase *idbase)
 
                        /* Direct data */
                        bone= arm->bonebase.first;
-                       while(bone) {
+                       while (bone) {
                                write_bone(wd, bone);
                                bone=bone->next;
                        }
@@ -2497,15 +2519,15 @@ static void write_texts(WriteData *wd, ListBase *idbase)
        TextMarker *mrk;
 
        text= idbase->first;
-       while(text) {
+       while (text) {
                if ( (text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) text->flags &= ~TXT_ISEXT;
 
                /* write LibData */
                writestruct(wd, ID_TXT, "Text", 1, text);
-               if(text->name) writedata(wd, DATA, strlen(text->name)+1, text->name);
+               if (text->name) writedata(wd, DATA, strlen(text->name)+1, text->name);
                if (text->id.properties) IDP_WriteProperty(text->id.properties, wd);
 
-               if(!(text->flags & TXT_ISEXT)) {
+               if (!(text->flags & TXT_ISEXT)) {
                        /* now write the text data, in two steps for optimization in the readfunction */
                        tmp= text->lines.first;
                        while (tmp) {
@@ -2540,8 +2562,8 @@ static void write_speakers(WriteData *wd, ListBase *idbase)
        Speaker *spk;
 
        spk= idbase->first;
-       while(spk) {
-               if(spk->id.us>0 || wd->current) {
+       while (spk) {
+               if (spk->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_SPK, "Speaker", 1, spk);
                        if (spk->id.properties) IDP_WriteProperty(spk->id.properties, wd);
@@ -2559,8 +2581,8 @@ static void write_sounds(WriteData *wd, ListBase *idbase)
        PackedFile * pf;
 
        sound= idbase->first;
-       while(sound) {
-               if(sound->id.us>0 || wd->current) {
+       while (sound) {
+               if (sound->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_SO, "bSound", 1, sound);
                        if (sound->id.properties) IDP_WriteProperty(sound->id.properties, wd);
@@ -2583,14 +2605,14 @@ static void write_groups(WriteData *wd, ListBase *idbase)
        Group *group;
        GroupObject *go;
 
-       for(group= idbase->first; group; group= group->id.next) {
-               if(group->id.us>0 || wd->current) {
+       for (group= idbase->first; group; group= group->id.next) {
+               if (group->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_GR, "Group", 1, group);
                        if (group->id.properties) IDP_WriteProperty(group->id.properties, wd);
 
                        go= group->gobject.first;
-                       while(go) {
+                       while (go) {
                                writestruct(wd, DATA, "GroupObject", 1, go);
                                go= go->next;
                        }
@@ -2602,7 +2624,7 @@ static void write_nodetrees(WriteData *wd, ListBase *idbase)
 {
        bNodeTree *ntree;
        
-       for(ntree=idbase->first; ntree; ntree= ntree->id.next) {
+       for (ntree=idbase->first; ntree; ntree= ntree->id.next) {
                if (ntree->id.us>0 || wd->current) {
                        writestruct(wd, ID_NT, "bNodeTree", 1, ntree);
                        write_nodetree(wd, ntree);
@@ -2618,14 +2640,14 @@ static void write_brushes(WriteData *wd, ListBase *idbase)
 {
        Brush *brush;
        
-       for(brush=idbase->first; brush; brush= brush->id.next) {
-               if(brush->id.us>0 || wd->current) {
+       for (brush=idbase->first; brush; brush= brush->id.next) {
+               if (brush->id.us>0 || wd->current) {
                        writestruct(wd, ID_BR, "Brush", 1, brush);
                        if (brush->id.properties) IDP_WriteProperty(brush->id.properties, wd);
                        
                        writestruct(wd, DATA, "MTex", 1, &brush->mtex);
                        
-                       if(brush->curve)
+                       if (brush->curve)
                                write_curvemapping(wd, brush->curve);
                }
        }
@@ -2635,8 +2657,8 @@ static void write_scripts(WriteData *wd, ListBase *idbase)
 {
        Script *script;
        
-       for(script=idbase->first; script; script= script->id.next) {
-               if(script->id.us>0 || wd->current) {
+       for (script=idbase->first; script; script= script->id.next) {
+               if (script->id.us>0 || wd->current) {
                        writestruct(wd, ID_SCRIPT, "Script", 1, script);
                        if (script->id.properties) IDP_WriteProperty(script->id.properties, wd);
                }
@@ -2648,19 +2670,31 @@ static void write_movieTracks(WriteData *wd, ListBase *tracks)
        MovieTrackingTrack *track;
 
        track= tracks->first;
-       while(track) {
+       while (track) {
                writestruct(wd, DATA, "MovieTrackingTrack", 1, track);
 
-               if(track->markers)
+               if (track->markers)
                        writestruct(wd, DATA, "MovieTrackingMarker", track->markersnr, track->markers);
 
                track= track->next;
        }
 }
 
+static void write_movieDopesheet(WriteData *wd, MovieTrackingDopesheet *dopesheet)
+{
+       MovieTrackingDopesheetChannel *channel;
+
+       channel = dopesheet->channels.first;
+       while (channel) {
+               writestruct(wd, DATA, "MovieTrackingDopesheetChannel", 1, channel);
+
+               channel = channel->next;
+       }
+}
+
 static void write_movieReconstruction(WriteData *wd, MovieTrackingReconstruction *reconstruction)
 {
-       if(reconstruction->camnr)
+       if (reconstruction->camnr)
                writestruct(wd, DATA, "MovieReconstructedCamera", reconstruction->camnr, reconstruction->cameras);
 }
 
@@ -2669,17 +2703,20 @@ static void write_movieclips(WriteData *wd, ListBase *idbase)
        MovieClip *clip;
 
        clip= idbase->first;
-       while(clip) {
-               if(clip->id.us>0 || wd->current) {
+       while (clip) {
+               if (clip->id.us>0 || wd->current) {
                        MovieTracking *tracking= &clip->tracking;
                        MovieTrackingObject *object;
                        writestruct(wd, ID_MC, "MovieClip", 1, clip);
 
+                       if (clip->adt)
+                               write_animdata(wd, clip->adt);
+
                        write_movieTracks(wd, &tracking->tracks);
                        write_movieReconstruction(wd, &tracking->reconstruction);
 
                        object= tracking->objects.first;
-                       while(object) {
+                       while (object) {
                                writestruct(wd, DATA, "MovieTrackingObject", 1, object);
 
                                write_movieTracks(wd, &object->tracks);
@@ -2687,6 +2724,8 @@ static void write_movieclips(WriteData *wd, ListBase *idbase)
 
                                object= object->next;
                        }
+
+                       write_movieDopesheet(wd, &tracking->dopesheet);
                }
 
                clip= clip->id.next;
@@ -2696,6 +2735,54 @@ static void write_movieclips(WriteData *wd, ListBase *idbase)
        mywrite(wd, MYWRITE_FLUSH, 0);
 }
 
+static void write_masks(WriteData *wd, ListBase *idbase)
+{
+       Mask *mask;
+
+       mask = idbase->first;
+       while (mask) {
+               if (mask->id.us > 0 || wd->current) {
+                       MaskShape *shape;
+
+                       writestruct(wd, ID_MSK, "Mask", 1, mask);
+
+                       if (mask->adt)
+                               write_animdata(wd, mask->adt);
+
+                       shape = mask->shapes.first;
+                       while (shape) {
+                               MaskSpline *spline;
+
+                               writestruct(wd, DATA, "MaskShape", 1, shape);
+
+                               spline = shape->splines.first;
+                               while (spline) {
+                                       int i;
+
+                                       writestruct(wd, DATA, "MaskSpline", 1, spline);
+                                       writestruct(wd, DATA, "MaskSplinePoint", spline->tot_point, spline->points);
+
+                                       for (i = 0; i < spline->tot_point; i++) {
+                                               MaskSplinePoint *point = &spline->points[i];
+
+                                               if (point->tot_uw)
+                                                       writestruct(wd, DATA, "MaskSplinePointUW", point->tot_uw, point->uw);
+                                       }
+
+                                       spline = spline->next;
+                               }
+
+                               shape = shape->next;
+                       }
+               }
+
+               mask = mask->id.next;
+       }
+
+       /* flush helps the compression for undo-save */
+       mywrite(wd, MYWRITE_FLUSH, 0);
+}
+
 /* context is usually defined by WM, two cases where no WM is available:
  * - for forward compatibility, curscreen has to be saved
  * - for undofile, curscene needs to be saved */
@@ -2744,15 +2831,15 @@ static void write_global(WriteData *wd, int fileflags, Main *mainvar)
  * second are an RGBA image (unsigned char)
  * note, this uses 'TEST' since new types will segfault on file load for older blender versions.
  */
-static void write_thumb(WriteData *wd, int *img)
+static void write_thumb(WriteData *wd, const int *img)
 {
-       if(img)
+       if (img)
                writedata(wd, TEST, (2 + img[0] * img[1]) * sizeof(int), img);
 }
 
 /* if MemFile * there's filesave to memory */
 static int write_file_handle(Main *mainvar, int handle, MemFile *compare, MemFile *current, 
-                                                        int write_user_block, int write_flags, int *thumb)
+                             int write_user_block, int write_flags, const int *thumb)
 {
        BHead bhead;
        ListBase mainlist;
@@ -2775,11 +2862,12 @@ static int write_file_handle(Main *mainvar, int handle, MemFile *compare, MemFil
        write_global(wd, write_flags, mainvar);
 
        /* no UI save in undo */
-       if(current==NULL) {
+       if (current==NULL) {
                write_windowmanagers(wd, &mainvar->wm);
                write_screens  (wd, &mainvar->screen);
        }
        write_movieclips (wd, &mainvar->movieclip);
+       write_masks    (wd, &mainvar->mask);
        write_scenes   (wd, &mainvar->scene);
        write_curves   (wd, &mainvar->curve);
        write_mballs   (wd, &mainvar->mball);
@@ -2831,17 +2919,17 @@ static int do_history(const char *name, ReportList *reports)
        char tempname1[FILE_MAX], tempname2[FILE_MAX];
        int hisnr= U.versions;
        
-       if(U.versions==0) return 0;
-       if(strlen(name)<2) {
+       if (U.versions==0) return 0;
+       if (strlen(name)<2) {
                BKE_report(reports, RPT_ERROR, "Unable to make version backup: filename too short");
                return 1;
        }
                
-       while(hisnr > 1) {
+       while (hisnr > 1) {
                BLI_snprintf(tempname1, sizeof(tempname1), "%s%d", name, hisnr-1);
                BLI_snprintf(tempname2, sizeof(tempname2), "%s%d", name, hisnr);
        
-               if(BLI_rename(tempname1, tempname2)) {
+               if (BLI_rename(tempname1, tempname2)) {
                        BKE_report(reports, RPT_ERROR, "Unable to make version backup");
                        return 1;
                }       
@@ -2851,7 +2939,7 @@ static int do_history(const char *name, ReportList *reports)
        /* is needed when hisnr==1 */
        BLI_snprintf(tempname1, sizeof(tempname1), "%s%d", name, hisnr);
 
-       if(BLI_rename(name, tempname1)) {
+       if (BLI_rename(name, tempname1)) {
                BKE_report(reports, RPT_ERROR, "Unable to make version backup");
                return 1;
        }
@@ -2860,7 +2948,7 @@ static int do_history(const char *name, ReportList *reports)
 }
 
 /* return: success (1) */
-int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportList *reports, int *thumb)
+int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportList *reports, const int *thumb)
 {
        char userfilename[FILE_MAX];
        char tempname[FILE_MAX+1];
@@ -2869,32 +2957,32 @@ int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportL
        /* open temporary file, so we preserve the original in case we crash */
        BLI_snprintf(tempname, sizeof(tempname), "%s@", filepath);
 
-       file = open(tempname,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
-       if(file == -1) {
+       file = BLI_open(tempname, O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
+       if (file == -1) {
                BKE_reportf(reports, RPT_ERROR, "Can't open file %s for writing: %s.", tempname, strerror(errno));
                return 0;
        }
 
        /* remapping of relative paths to new file location */
-       if(write_flags & G_FILE_RELATIVE_REMAP) {
+       if (write_flags & G_FILE_RELATIVE_REMAP) {
                char dir1[FILE_MAX];
                char dir2[FILE_MAX];
                BLI_split_dir_part(filepath, dir1, sizeof(dir1));
                BLI_split_dir_part(mainvar->name, dir2, sizeof(dir2));
 
-               /* just incase there is some subtle difference */
+               /* just in case there is some subtle difference */
                BLI_cleanup_dir(mainvar->name, dir1);
                BLI_cleanup_dir(mainvar->name, dir2);
 
-               if(BLI_path_cmp(dir1, dir2)==0) {
+               if (BLI_path_cmp(dir1, dir2)==0) {
                        write_flags &= ~G_FILE_RELATIVE_REMAP;
                }
                else {
-                       if(G.relbase_valid) {
+                       if (G.relbase_valid) {
                                /* blend may not have been saved before. Tn this case
                                 * we should not have any relative paths, but if there
                                 * is somehow, an invalid or empty G.main->name it will
-                                * print an error, dont try make the absolute in this case. */
+                                * print an error, don't try make the absolute in this case. */
                                makeFilesAbsolute(mainvar, G.main->name, NULL);
                        }
                }
@@ -2903,11 +2991,11 @@ int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportL
        BLI_make_file_string(G.main->name, userfilename, BLI_get_folder_create(BLENDER_USER_CONFIG, NULL), BLENDER_STARTUP_FILE);
        write_user_block= (BLI_path_cmp(filepath, userfilename) == 0);
 
-       if(write_flags & G_FILE_RELATIVE_REMAP)
+       if (write_flags & G_FILE_RELATIVE_REMAP)
                makeFilesRelative(mainvar, filepath, NULL); /* note, making relative to something OTHER then G.main->name */
 
        /* actual file writing */
-       err= write_file_handle(mainvar, file, NULL,NULL, write_user_block, write_flags, thumb);
+       err= write_file_handle(mainvar, file, NULL, NULL, write_user_block, write_flags, thumb);
        close(file);
 
        if (err) {
@@ -2927,7 +3015,7 @@ int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportL
                }
        }
 
-       if(write_flags & G_FILE_COMPRESS) {
+       if (write_flags & G_FILE_COMPRESS) {
                /* compressed files have the same ending as regular files... only from 2.4!!! */
                char gzname[FILE_MAX+4];
                int ret;
@@ -2936,25 +3024,25 @@ int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportL
                BLI_snprintf(gzname, sizeof(gzname), "%s@.gz", filepath);
                ret = BLI_file_gzip(tempname, gzname);
                
-               if(0==ret) {
+               if (0==ret) {
                        /* now rename to real file name, and delete temp @ file too */
-                       if(BLI_rename(gzname, filepath) != 0) {
+                       if (BLI_rename(gzname, filepath) != 0) {
                                BKE_report(reports, RPT_ERROR, "Can't change old file. File saved with @.");
                                return 0;
                        }
 
                        BLI_delete(tempname, 0, 0);
                }
-               else if(-1==ret) {
+               else if (-1==ret) {
                        BKE_report(reports, RPT_ERROR, "Failed opening .gz file.");
                        return 0;
                }
-               else if(-2==ret) {
+               else if (-2==ret) {
                        BKE_report(reports, RPT_ERROR, "Failed opening .blend file for compression.");
                        return 0;
                }
        }
-       else if(BLI_rename(tempname, filepath) != 0) {
+       else if (BLI_rename(tempname, filepath) != 0) {
                BKE_report(reports, RPT_ERROR, "Can't change old file. File saved with @");
                return 0;
        }
@@ -2969,6 +3057,6 @@ int BLO_write_file_mem(Main *mainvar, MemFile *compare, MemFile *current, int wr
 
        err= write_file_handle(mainvar, 0, compare, current, 0, write_flags, NULL);
        
-       if(err==0) return 1;
+       if (err==0) return 1;
        return 0;
 }