2.5: Various
[blender.git] / source / blender / blenloader / intern / readfile.c
index 85d7883de24fb0f920e614e31f76f5d85a6259c4..c36a287b9811d6dc970e35a825a6fb3a7db2128d 100644 (file)
@@ -81,7 +81,7 @@
 #include "DNA_object_types.h"
 #include "DNA_object_force.h"
 #include "DNA_object_fluidsim.h" // NT
-#include "DNA_oops_types.h"
+#include "DNA_outliner_types.h"
 #include "DNA_object_force.h"
 #include "DNA_packedFile_types.h"
 #include "DNA_particle_types.h"
 #include "BKE_animsys.h"
 #include "BKE_action.h"
 #include "BKE_armature.h"
+#include "BKE_brush.h"
 #include "BKE_cdderivedmesh.h"
 #include "BKE_cloth.h"
 #include "BKE_colortools.h"
 
 #include "readfile.h"
 
+#include "PIL_time.h"
+
 #include <errno.h>
 
 /*
@@ -177,7 +180,7 @@ READ
                - read associated 'direct data'
                - link direct data (internal and to LibBlock)
 - read FileGlobal
-- read USER data, only when indicated (file is ~/.B.blend)
+- read USER data, only when indicated (file is ~/.B.blend or .B25.blend)
 - free file
 - per Library (per Main)
        - read file
@@ -610,10 +613,16 @@ static BHeadN *get_bhead(FileData *fd)
        BHead  bhead;
        BHeadN *new_bhead = 0;
        int readsize;
-
+       
        if (fd) {
                if ( ! fd->eof) {
 
+                       /* not strictly needed but shuts valgrind up
+                        * since uninitialized memory gets compared */
+                       memset(&bhead8, 0, sizeof(BHead8));
+                       memset(&bhead4, 0, sizeof(BHead4));
+                       memset(&bhead,  0, sizeof(BHead));
+                       
                        // First read the bhead structure.
                        // Depending on the platform the file was written on this can
                        // be a big or little endian BHead4 or BHead8 structure.
@@ -1488,6 +1497,8 @@ static void lib_link_brush(FileData *fd, Main *main)
                if(brush->id.flag & LIB_NEEDLINK) {
                        brush->id.flag -= LIB_NEEDLINK;
 
+                       brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
+                       
                        for(a=0; a<MAX_MTEX; a++) {
                                mtex= brush->mtex[a];
                                if(mtex)
@@ -1511,6 +1522,8 @@ static void direct_link_brush(FileData *fd, Brush *brush)
        brush->curve= newdataadr(fd, brush->curve);
        if(brush->curve)
                direct_link_curvemapping(fd, brush->curve);
+       else
+               brush_curve_preset(brush, BRUSH_PRESET_SHARP);
 }
 
 static void direct_link_script(FileData *fd, Script *script)
@@ -1519,147 +1532,6 @@ static void direct_link_script(FileData *fd, Script *script)
        SCRIPT_SET_NULL(script)
 }
 
-/* ************ READ NODE TREE *************** */
-
-/* singe node tree (also used for material/scene trees), ntree is not NULL */
-static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
-{
-       bNode *node;
-       
-       for(node= ntree->nodes.first; node; node= node->next)
-               node->id= newlibadr_us(fd, id->lib, node->id);
-}
-
-/* library ntree linking after fileread */
-static void lib_link_nodetree(FileData *fd, Main *main)
-{
-       bNodeTree *ntree;
-       
-       /* only link ID pointers */
-       for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
-               if(ntree->id.flag & LIB_NEEDLINK) {
-                       ntree->id.flag -= LIB_NEEDLINK;
-                       lib_link_ntree(fd, &ntree->id, ntree);
-               }
-       }
-}
-
-/* verify types for nodes and groups, all data has to be read */
-/* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
- * typedefs*/
-static void lib_verify_nodetree(Main *main, int open)
-{
-       Scene *sce;
-       Material *ma;
-       Tex *tx;
-       bNodeTree *ntree;
-
-       /* this crashes blender on undo/redo
-       if(open==1) {
-               reinit_nodesystem();
-       }*/
-       
-       /* now create the own typeinfo structs an verify nodes */
-       /* here we still assume no groups in groups */
-       for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
-               ntreeVerifyTypes(ntree);        /* internal nodes, no groups! */
-               ntreeMakeOwnType(ntree);        /* for group usage */
-       }
-       
-       /* now verify all types in material trees, groups are set OK now */
-       for(ma= main->mat.first; ma; ma= ma->id.next) {
-               if(ma->nodetree)
-                       ntreeVerifyTypes(ma->nodetree);
-       }
-       /* and scene trees */
-       for(sce= main->scene.first; sce; sce= sce->id.next) {
-               if(sce->nodetree)
-                       ntreeVerifyTypes(sce->nodetree);
-       }
-       /* and texture trees */
-       for(tx= main->tex.first; tx; tx= tx->id.next) {
-               if(tx->nodetree)
-                       ntreeVerifyTypes(tx->nodetree);
-       }
-}
-
-
-
-/* ntree itself has been read! */
-static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
-{
-       /* note: writing and reading goes in sync, for speed */
-       bNode *node;
-       bNodeSocket *sock;
-       bNodeLink *link;
-       
-       ntree->init= 0;         /* to set callbacks and force setting types */
-       ntree->owntype= NULL;
-       ntree->timecursor= NULL;
-       
-       link_list(fd, &ntree->nodes);
-       for(node= ntree->nodes.first; node; node= node->next) {
-               if(node->type == NODE_DYNAMIC) {
-                       node->custom1= 0;
-                       node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
-                       node->typeinfo= NULL;
-               }
-
-               node->storage= newdataadr(fd, node->storage);
-               if(node->storage) {
-                       
-                       /* could be handlerized at some point */
-                       if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
-                               direct_link_curvemapping(fd, node->storage);
-                       else if(ntree->type==NTREE_COMPOSIT) {
-                               if( ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB))
-                                       direct_link_curvemapping(fd, node->storage);
-                               else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
-                                       ((ImageUser *)node->storage)->ok= 1;
-                       }
-                       else if( ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) ) {
-                               direct_link_curvemapping(fd, node->storage);
-                       }
-               }
-               link_list(fd, &node->inputs);
-               link_list(fd, &node->outputs);
-       }
-       link_list(fd, &ntree->links);
-       
-       /* and we connect the rest */
-       for(node= ntree->nodes.first; node; node= node->next) {
-               node->preview= newimaadr(fd, node->preview);
-               node->lasty= 0;
-               for(sock= node->inputs.first; sock; sock= sock->next)
-                       sock->link= newdataadr(fd, sock->link);
-               for(sock= node->outputs.first; sock; sock= sock->next)
-                       sock->ns.data= NULL;
-       }
-       for(link= ntree->links.first; link; link= link->next) {
-               link->fromnode= newdataadr(fd, link->fromnode);
-               link->tonode= newdataadr(fd, link->tonode);
-               link->fromsock= newdataadr(fd, link->fromsock);
-               link->tosock= newdataadr(fd, link->tosock);
-       }
-       
-       /* set selin and selout */
-       for(node= ntree->nodes.first; node; node= node->next) {
-               for(sock= node->inputs.first; sock; sock= sock->next) {
-                       if(sock->flag & SOCK_SEL) {
-                               ntree->selin= sock;
-                               break;
-                       }
-               }
-               for(sock= node->outputs.first; sock; sock= sock->next) {
-                       if(sock->flag & SOCK_SEL) {
-                               ntree->selout= sock;
-                               break;
-                       }
-               }
-       }
-       
-       /* type verification is in lib-link */
-}
 
 /* ************ READ PACKEDFILE *************** */
 
@@ -1805,8 +1677,10 @@ static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
                /* driver data */
                if (fcu->driver) {
                        ChannelDriver *driver= fcu->driver;
-                       driver->id= newlibadr(fd, id->lib, driver->id); 
-                       driver->id2= newlibadr(fd, id->lib, driver->id2); 
+                       DriverTarget *dtar;
+                       
+                       for (dtar= driver->targets.first; dtar; dtar= dtar->next)
+                               dtar->id= newlibadr(fd, id->lib, dtar->id); 
                }
                
                /* modifiers */
@@ -1846,9 +1720,12 @@ static void direct_link_fcurves(FileData *fd, ListBase *list)
                fcu->driver= newdataadr(fd, fcu->driver);
                if (fcu->driver) {
                        ChannelDriver *driver= fcu->driver;
+                       DriverTarget *dtar;
                        
-                       driver->rna_path= newdataadr(fd, driver->rna_path);
-                       driver->rna_path2= newdataadr(fd, driver->rna_path2);
+                       /* relink targets and their paths */
+                       link_list(fd, &driver->targets);
+                       for (dtar= driver->targets.first; dtar; dtar= dtar->next)
+                               dtar->rna_path= newdataadr(fd, dtar->rna_path);
                }
                
                /* modifiers */
@@ -1856,6 +1733,7 @@ static void direct_link_fcurves(FileData *fd, ListBase *list)
                for (fcm= fcu->modifiers.first; fcm; fcm= fcm->next) {
                        /* relink general data */
                        fcm->data = newdataadr(fd, fcm->data);
+                       fcm->edata= NULL;
                        
                        /* do relinking of data for specific types */
                        switch (fcm->type) {
@@ -1863,7 +1741,14 @@ static void direct_link_fcurves(FileData *fd, ListBase *list)
                                {
                                        FMod_Generator *data= (FMod_Generator *)fcm->data;
                                        
-                                       data->poly_coefficients= newdataadr(fd, data->poly_coefficients);
+                                       data->coefficients= newdataadr(fd, data->coefficients);
+                               }
+                                       break;
+                               case FMODIFIER_TYPE_ENVELOPE:
+                               {
+                                       FMod_Envelope *data= (FMod_Envelope *)fcm->data;
+                                       
+                                       data->data= newdataadr(fd, data->data);
                                }
                                        break;
                                case FMODIFIER_TYPE_PYTHON:
@@ -1929,6 +1814,39 @@ static void direct_link_action(FileData *fd, bAction *act)
 
 /* ------- */
 
+static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
+{
+       KeyingSet *ks;
+       KS_Path *ksp;
+       
+       /* here, we're only interested in the ID pointer stored in some of the paths */
+       for (ks= list->first; ks; ks= ks->next) {
+               for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
+                       ksp->id= newlibadr(fd, id->lib, ksp->id); 
+               }
+       }
+}
+
+/* NOTE: this assumes that link_list has already been called on the list */
+static void direct_link_keyingsets(FileData *fd, ListBase *list)
+{
+       KeyingSet *ks;
+       KS_Path *ksp;
+       
+       /* link KeyingSet data to KeyingSet again (non ID-libs) */
+       for (ks= list->first; ks; ks= ks->next) {
+               /* paths */
+               link_list(fd, &ks->paths);
+               
+               for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
+                       /* rna path */
+                       ksp->rna_path= newdataadr(fd, ksp->rna_path);
+               }
+       }
+}
+
+/* ------- */
+
 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
 {
        if (adt == NULL)
@@ -1963,6 +1881,153 @@ static void direct_link_animdata(FileData *fd, AnimData *adt)
        // TODO...
 }      
 
+/* ************ READ NODE TREE *************** */
+
+/* singe node tree (also used for material/scene trees), ntree is not NULL */
+static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
+{
+       bNode *node;
+       
+       if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
+       
+       for(node= ntree->nodes.first; node; node= node->next)
+               node->id= newlibadr_us(fd, id->lib, node->id);
+}
+
+/* library ntree linking after fileread */
+static void lib_link_nodetree(FileData *fd, Main *main)
+{
+       bNodeTree *ntree;
+       
+       /* only link ID pointers */
+       for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
+               if(ntree->id.flag & LIB_NEEDLINK) {
+                       ntree->id.flag -= LIB_NEEDLINK;
+                       lib_link_ntree(fd, &ntree->id, ntree);
+               }
+       }
+}
+
+/* verify types for nodes and groups, all data has to be read */
+/* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
+* typedefs*/
+static void lib_verify_nodetree(Main *main, int open)
+{
+       Scene *sce;
+       Material *ma;
+       Tex *tx;
+       bNodeTree *ntree;
+       
+       /* this crashes blender on undo/redo
+               if(open==1) {
+                       reinit_nodesystem();
+               }*/
+       
+       /* now create the own typeinfo structs an verify nodes */
+       /* here we still assume no groups in groups */
+       for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
+               ntreeVerifyTypes(ntree);        /* internal nodes, no groups! */
+               ntreeMakeOwnType(ntree);        /* for group usage */
+       }
+       
+       /* now verify all types in material trees, groups are set OK now */
+       for(ma= main->mat.first; ma; ma= ma->id.next) {
+               if(ma->nodetree)
+                       ntreeVerifyTypes(ma->nodetree);
+       }
+       /* and scene trees */
+       for(sce= main->scene.first; sce; sce= sce->id.next) {
+               if(sce->nodetree)
+                       ntreeVerifyTypes(sce->nodetree);
+       }
+       /* and texture trees */
+       for(tx= main->tex.first; tx; tx= tx->id.next) {
+               if(tx->nodetree)
+                       ntreeVerifyTypes(tx->nodetree);
+       }
+}
+
+
+
+/* ntree itself has been read! */
+static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
+{
+       /* note: writing and reading goes in sync, for speed */
+       bNode *node;
+       bNodeSocket *sock;
+       bNodeLink *link;
+       
+       ntree->init= 0;         /* to set callbacks and force setting types */
+       ntree->owntype= NULL;
+       ntree->timecursor= NULL;
+       
+       ntree->adt= newdataadr(fd, ntree->adt);
+       direct_link_animdata(fd, ntree->adt);
+       
+       link_list(fd, &ntree->nodes);
+       for(node= ntree->nodes.first; node; node= node->next) {
+               if(node->type == NODE_DYNAMIC) {
+                       node->custom1= 0;
+                       node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
+                       node->typeinfo= NULL;
+               }
+               
+               node->storage= newdataadr(fd, node->storage);
+               if(node->storage) {
+                       
+                       /* could be handlerized at some point */
+                       if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
+                               direct_link_curvemapping(fd, node->storage);
+                       else if(ntree->type==NTREE_COMPOSIT) {
+                               if( ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB))
+                                       direct_link_curvemapping(fd, node->storage);
+                               else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
+                                       ((ImageUser *)node->storage)->ok= 1;
+                       }
+                       else if( ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) ) {
+                               direct_link_curvemapping(fd, node->storage);
+                       }
+               }
+               link_list(fd, &node->inputs);
+               link_list(fd, &node->outputs);
+       }
+       link_list(fd, &ntree->links);
+       
+       /* and we connect the rest */
+       for(node= ntree->nodes.first; node; node= node->next) {
+               node->preview= newimaadr(fd, node->preview);
+               node->lasty= 0;
+               for(sock= node->inputs.first; sock; sock= sock->next)
+                       sock->link= newdataadr(fd, sock->link);
+               for(sock= node->outputs.first; sock; sock= sock->next)
+                       sock->ns.data= NULL;
+       }
+       for(link= ntree->links.first; link; link= link->next) {
+               link->fromnode= newdataadr(fd, link->fromnode);
+               link->tonode= newdataadr(fd, link->tonode);
+               link->fromsock= newdataadr(fd, link->fromsock);
+               link->tosock= newdataadr(fd, link->tosock);
+       }
+       
+       /* set selin and selout */
+       for(node= ntree->nodes.first; node; node= node->next) {
+               for(sock= node->inputs.first; sock; sock= sock->next) {
+                       if(sock->flag & SOCK_SEL) {
+                               ntree->selin= sock;
+                               break;
+                       }
+               }
+               for(sock= node->outputs.first; sock; sock= sock->next) {
+                       if(sock->flag & SOCK_SEL) {
+                               ntree->selout= sock;
+                               break;
+                       }
+               }
+       }
+       
+       /* type verification is in lib-link */
+}
+
 /* ************ READ ARMATURE ***************** */
 
 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
@@ -2098,6 +2163,13 @@ static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
                                data->tar = newlibadr(fd, id->lib, data->tar);
                        }
                        break;
+               case CONSTRAINT_TYPE_SHRINKWRAP:
+                       {
+                               bShrinkwrapConstraint *data;
+                               data= ((bShrinkwrapConstraint*)con->data);
+                               data->target = newlibadr(fd, id->lib, data->target);
+                       }
+                       break;
                case CONSTRAINT_TYPE_NULL:
                        break;
                }
@@ -2114,9 +2186,12 @@ static void direct_link_constraints(FileData *fd, ListBase *lb)
                
                if (cons->type == CONSTRAINT_TYPE_PYTHON) {
                        bPythonConstraint *data= cons->data;
+                       
                        link_list(fd, &data->targets);
+                       
                        data->prop = newdataadr(fd, data->prop);
-                       IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
+                       if (data->prop)
+                               IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
                }
        }
 }
@@ -2288,7 +2363,8 @@ static void lib_link_key(FileData *fd, Main *main)
        key= main->key.first;
        while(key) {
                if(key->id.flag & LIB_NEEDLINK) {
-
+                       if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
+                       
                        key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
                        key->from= newlibadr(fd, key->id.lib, key->from);
 
@@ -2508,7 +2584,7 @@ static void direct_link_text(FileData *fd, Text *text)
                ln= ln->next;
        }
 
-       text->flags = (text->flags|TXT_ISTMP) & ~TXT_ISEXT;
+       text->flags = (text->flags) & ~TXT_ISEXT;
 
        text->id.us= 1;
 }
@@ -2582,6 +2658,7 @@ static void lib_link_curve(FileData *fd, Main *main)
        cu= main->curve.first;
        while(cu) {
                if(cu->id.flag & LIB_NEEDLINK) {
+                       if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
 
                        for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
 
@@ -2686,6 +2763,7 @@ static void lib_link_texture(FileData *fd, Main *main)
        tex= main->tex.first;
        while(tex) {
                if(tex->id.flag & LIB_NEEDLINK) {
+                       if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
 
                        tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
                        tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
@@ -2745,6 +2823,8 @@ static void lib_link_material(FileData *fd, Main *main)
        ma= main->mat.first;
        while(ma) {
                if(ma->id.flag & LIB_NEEDLINK) {
+                       if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
+
                        /*Link ID Properties -- and copy this comment EXACTLY for easy finding
                        of library blocks that implement this.*/
                        if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
@@ -2798,6 +2878,16 @@ static void direct_link_material(FileData *fd, Material *ma)
 
 static void direct_link_pointcache(FileData *fd, PointCache *cache)
 {
+       if((cache->flag & PTCACHE_DISK_CACHE)==0) {
+               PTCacheMem *pm;
+
+               link_list(fd, &cache->mem_cache);
+
+               pm = cache->mem_cache.first;
+
+               for(; pm; pm=pm->next)
+                       pm->data = newdataadr(fd, pm->data);
+       }
        cache->flag &= ~(PTCACHE_SIMULATION_VALID|PTCACHE_BAKE_EDIT_ACTIVE);
        cache->simframe= 0;
 }
@@ -2809,7 +2899,9 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
        part= main->particle.first;
        while(part) {
                if(part->id.flag & LIB_NEEDLINK) {
+                       if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
                        part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
+                       
                        part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
                        part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
                        part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
@@ -2822,6 +2914,7 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
 
 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
 {
+       part->adt= newdataadr(fd, part->adt);
        part->pd= newdataadr(fd, part->pd);
        part->pd2= newdataadr(fd, part->pd2);
 }
@@ -2894,6 +2987,7 @@ static void direct_link_particlesystems(FileData *fd, ListBase *particles)
                }
 
                psys->edit = 0;
+               psys->free_edit = NULL;
                psys->pathcache = 0;
                psys->childcache = 0;
                psys->pathcachebufs.first = psys->pathcachebufs.last = 0;
@@ -3061,10 +3155,8 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
                direct_link_dverts(fd, lvl->totvert, CustomData_get(&mesh->mr->vdata, 0, CD_MDEFORMVERT));
                direct_link_customdata(fd, &mesh->mr->fdata, lvl->totface);
                
-               if(!mesh->mr->edge_flags)
-                       mesh->mr->edge_flags= MEM_callocN(sizeof(short)*lvl->totedge, "Multires Edge Flags");
-               if(!mesh->mr->edge_creases)
-                       mesh->mr->edge_creases= MEM_callocN(sizeof(char)*lvl->totedge, "Multires Edge Creases");
+               mesh->mr->edge_flags= newdataadr(fd, mesh->mr->edge_flags);
+               mesh->mr->edge_creases= newdataadr(fd, mesh->mr->edge_creases);
 
                mesh->mr->verts = newdataadr(fd, mesh->mr->verts);
                        
@@ -3075,6 +3167,18 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
                        lvl->colfaces= newdataadr(fd, lvl->colfaces);
                }
        }
+
+       /* Gracefully handle corrupted mesh */
+       if(mesh->mr && !mesh->mr->verts) {
+               /* If totals match, simply load the current mesh verts into multires */
+               if(mesh->totvert == ((MultiresLevel*)mesh->mr->levels.last)->totvert)
+                       mesh->mr->verts = MEM_dupallocN(mesh->mvert);
+               else {
+                       /* Otherwise, we can't recover the data, silently remove multires */
+                       multires_free(mesh->mr);
+                       mesh->mr = NULL;
+               }
+       }
        
        if((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
                TFace *tf= mesh->tface;
@@ -3266,6 +3370,10 @@ static void lib_link_object(FileData *fd, Main *main)
                                        bAddObjectActuator *eoa= act->data;
                                        if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
                                }
+                               else if(act->type==ACT_OBJECT) {
+                                       bObjectActuator *oa= act->data;
+                                       oa->reference= newlibadr(fd, ob->id.lib, oa->reference);
+                               }
                                else if(act->type==ACT_EDIT_OBJECT) {
                                        bEditObjectActuator *eoa= act->data;
                                        if(eoa==NULL) {
@@ -3276,6 +3384,15 @@ static void lib_link_object(FileData *fd, Main *main)
                                                eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
                                        }
                                }
+                               else if(act->type==ACT_OBJECT) {
+                                       bObjectActuator *oa= act->data;
+                                       if(oa==NULL) {
+                                               init_actuator(act);
+                                       }
+                                       else {
+                                               oa->reference= newlibadr(fd, ob->id.lib, oa->reference);
+                                       }
+                               }
                                else if(act->type==ACT_SCENE) {
                                        bSceneActuator *sa= act->data;
                                        sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
@@ -3349,7 +3466,13 @@ static void direct_link_pose(FileData *fd, bPose *pose)
                pchan->bone= NULL;
                pchan->parent= newdataadr(fd, pchan->parent);
                pchan->child= newdataadr(fd, pchan->child);
+               
                direct_link_constraints(fd, &pchan->constraints);
+               
+               pchan->prop = newdataadr(fd, pchan->prop);
+               if (pchan->prop)
+                       IDP_DirectLinkProperty(pchan->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
+               
                pchan->iktree.first= pchan->iktree.last= NULL;
                pchan->path= NULL;
        }
@@ -3374,6 +3497,11 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
 
                        smd->emCache = smd->mCache = 0;
                }
+               else if (md->type==eModifierType_Armature) {
+                       ArmatureModifierData *amd = (ArmatureModifierData*) md;
+                       
+                       amd->prevCos= NULL;
+               }
                else if (md->type==eModifierType_Cloth) {
                        ClothModifierData *clmd = (ClothModifierData*) md;
                        
@@ -3424,6 +3552,12 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                        collmd->mfaces = NULL;
                        
                }
+               else if (md->type==eModifierType_Surface) {
+                       SurfaceModifierData *surmd = (SurfaceModifierData*) md;
+
+                       surmd->dm = NULL;
+                       surmd->bvhtree = NULL;
+               }
                else if (md->type==eModifierType_Hook) {
                        HookModifierData *hmd = (HookModifierData*) md;
 
@@ -3672,6 +3806,17 @@ static void direct_link_object(FileData *fd, Object *ob)
 
 /* ************ READ SCENE ***************** */
 
+/* patch for missing scene IDs, can't be in do-versions */
+static void composite_patch(bNodeTree *ntree, Scene *scene)
+{
+       bNode *node;
+       
+       for(node= ntree->nodes.first; node; node= node->next)
+               if(node->id==NULL && ELEM(node->type, CMP_NODE_R_LAYERS, CMP_NODE_COMPOSITE))
+                       node->id= &scene->id;
+}
+
+
 static void lib_link_scene(FileData *fd, Main *main)
 {
        Scene *sce;
@@ -3687,15 +3832,26 @@ static void lib_link_scene(FileData *fd, Main *main)
                        if (sce->id.properties) IDP_LibLinkProperty(sce->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
                        if (sce->adt) lib_link_animdata(fd, &sce->id, sce->adt);
                        
+                       lib_link_keyingsets(fd, &sce->id, &sce->keyingsets);
+                       
                        sce->camera= newlibadr(fd, sce->id.lib, sce->camera);
                        sce->world= newlibadr_us(fd, sce->id.lib, sce->world);
                        sce->set= newlibadr(fd, sce->id.lib, sce->set);
                        sce->ima= newlibadr_us(fd, sce->id.lib, sce->ima);
+                       
                        sce->toolsettings->imapaint.brush=
                                newlibadr_us(fd, sce->id.lib, sce->toolsettings->imapaint.brush);
                        if(sce->toolsettings->sculpt)
                                sce->toolsettings->sculpt->brush=
                                        newlibadr_us(fd, sce->id.lib, sce->toolsettings->sculpt->brush);
+                       if(sce->toolsettings->vpaint)
+                               sce->toolsettings->vpaint->brush=
+                                       newlibadr_us(fd, sce->id.lib, sce->toolsettings->vpaint->brush);
+                       if(sce->toolsettings->wpaint)
+                               sce->toolsettings->wpaint->brush=
+                                       newlibadr_us(fd, sce->id.lib, sce->toolsettings->wpaint->brush);
+                       
+                       sce->toolsettings->skgen_template = newlibadr(fd, sce->id.lib, sce->toolsettings->skgen_template);
 
                        for(base= sce->base.first; base; base= next) {
                                next= base->next;
@@ -3736,14 +3892,18 @@ static void lib_link_scene(FileData *fd, Main *main)
                        
                        lib_link_scriptlink(fd, &sce->id, &sce->scriptlink);
                        
-                       if(sce->nodetree)
+                       if(sce->nodetree) {
                                lib_link_ntree(fd, &sce->id, sce->nodetree);
+                               composite_patch(sce->nodetree, sce);
+                       }
                        
                        for(srl= sce->r.layers.first; srl; srl= srl->next) {
                                srl->mat_override= newlibadr_us(fd, sce->id.lib, srl->mat_override);
                                srl->light_override= newlibadr_us(fd, sce->id.lib, srl->light_override);
                        }
-                       
+                       /*Game Settings: Dome Warp Text*/
+                       sce->r.dometext= newlibadr_us(fd, sce->id.lib, sce->r.dometext);
+
                        sce->id.flag -= LIB_NEEDLINK;
                }
 
@@ -3770,6 +3930,8 @@ static void direct_link_scene(FileData *fd, Scene *sce)
 
        sce->theDag = NULL;
        sce->dagisvalid = 0;
+       sce->obedit= NULL;
+       
        /* set users to one by default, not in lib-link, this will increase it for compo nodes */
        sce->id.us= 1;
 
@@ -3778,15 +3940,19 @@ static void direct_link_scene(FileData *fd, Scene *sce)
        sce->adt= newdataadr(fd, sce->adt);
        direct_link_animdata(fd, sce->adt);
        
+       link_list(fd, &sce->keyingsets);
+       direct_link_keyingsets(fd, &sce->keyingsets);
+       
        sce->basact= newdataadr(fd, sce->basact);
-
-       sce->radio= newdataadr(fd, sce->radio);
        
        sce->toolsettings= newdataadr(fd, sce->toolsettings);
        if(sce->toolsettings) {
                sce->toolsettings->vpaint= newdataadr(fd, sce->toolsettings->vpaint);
                sce->toolsettings->wpaint= newdataadr(fd, sce->toolsettings->wpaint);
                sce->toolsettings->sculpt= newdataadr(fd, sce->toolsettings->sculpt);
+               sce->toolsettings->imapaint.paintcursor= NULL;
+               sce->toolsettings->particle.paintcursor= NULL;
+
                if(sce->toolsettings->sculpt)
                        sce->toolsettings->sculpt->session= MEM_callocN(sizeof(SculptSession), "reload sculpt session");
        }
@@ -3847,6 +4013,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                                if (seq->flag & SEQ_USE_PROXY) {
                                        seq->strip->proxy = newdataadr(
                                                fd, seq->strip->proxy);
+                                       seq->strip->proxy->anim = 0;
                                } else {
                                        seq->strip->proxy = 0;
                                }
@@ -3945,11 +4112,13 @@ static void direct_link_windowmanager(FileData *fd, wmWindowManager *wm)
                win->ghostwin= NULL;
                win->eventstate= NULL;
                win->curswin= NULL;
-               
+               win->tweak= NULL;
+
                win->timers.first= win->timers.last= NULL;
                win->queue.first= win->queue.last= NULL;
                win->handlers.first= win->handlers.last= NULL;
                win->subwindows.first= win->subwindows.last= NULL;
+               win->gesture.first= win->gesture.last= NULL;
 
                win->drawdata= NULL;
                win->drawmethod= -1;
@@ -3980,13 +4149,17 @@ static void lib_link_windowmanager(FileData *fd, Main *main)
 
 /* ****************** READ GREASE PENCIL ***************** */
 
-/* relinks grease-pencil data for 3d-view(s) - used for direct_link */
-static void link_gpencil(FileData *fd, bGPdata *gpd)
+/* relinks grease-pencil data - used for direct_link and old file linkage */
+static void direct_link_gpencil(FileData *fd, bGPdata *gpd)
 {
        bGPDlayer *gpl;
        bGPDframe *gpf;
        bGPDstroke *gps;
        
+       /* we must firstly have some grease-pencil data to link! */
+       if (gpd == NULL)
+               return;
+       
        /* relink layers */
        link_list(fd, &gpd->layers);
        
@@ -4074,6 +4247,7 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                SpaceButs *sbuts= (SpaceButs *)sl;
                                                sbuts->lockpoin= NULL;
                                                sbuts->ri= NULL;
+                                               sbuts->pinid= newlibadr(fd, sc->id.lib, sbuts->pinid);
                                                if(main->versionfile<132)
                                                        butspace_version_132(sbuts);
                                        }
@@ -4082,9 +4256,7 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                sfile->files= NULL;
                                                sfile->params= NULL;
                                                sfile->op= NULL;
-                                               /* sfile->returnfunc= NULL; 
-                                               sfile->menup= NULL;
-                                               sfile->pupmenu= NULL; */ /* XXX removed */
+                                               sfile->layout= NULL;
                                        }
                                        else if(sl->spacetype==SPACE_IMASEL) {
                                                SpaceImaSel *simasel= (SpaceImaSel *)sl;
@@ -4125,18 +4297,11 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                        }
                                                }
                                        }
-                                       else if(sl->spacetype==SPACE_OOPS) {
+                                       else if(sl->spacetype==SPACE_OUTLINER) {
                                                SpaceOops *so= (SpaceOops *)sl;
-                                               Oops *oops;
                                                TreeStoreElem *tselem;
                                                int a;
 
-                                               oops= so->oops.first;
-                                               while(oops) {
-                                                       oops->id= newlibadr(fd, NULL, oops->id);
-                                                       oops= oops->next;
-                                               }
-                                               so->lockpoin= NULL;
                                                so->tree.first= so->tree.last= NULL;
                                                so->search_tse.id= newlibadr(fd, NULL, so->search_tse.id);
                                                
@@ -4152,6 +4317,21 @@ static void lib_link_screen(FileData *fd, Main *main)
 
                                                ssound->sound= newlibadr_us(fd, sc->id.lib, ssound->sound);
                                        }
+                                       else if(sl->spacetype==SPACE_NODE) {
+                                               SpaceNode *snode= (SpaceNode *)sl;
+                                               
+                                               snode->id= newlibadr(fd, sc->id.lib, snode->id);
+                                               
+                                               /* internal data, a bit patchy */
+                                               if(snode->id) {
+                                                       if(GS(snode->id->name)==ID_MA)
+                                                               snode->nodetree= ((Material *)snode->id)->nodetree;
+                                                       else if(GS(snode->id->name)==ID_SCE)
+                                                               snode->nodetree= ((Scene *)snode->id)->nodetree;
+                                                       else if(GS(snode->id->name)==ID_TE)
+                                                               snode->nodetree= ((Tex *)snode->id)->nodetree;
+                                               }
+                                       }
                                }
                                sa= sa->next;
                        }
@@ -4242,44 +4422,60 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                                v3d->bgpic->ima= restore_pointer_by_name(newmain, (ID *)v3d->bgpic->ima, 1);
                                        }
                                        if(v3d->localvd) {
-                                               Base *base;
+                                               /*Base *base;*/
 
                                                v3d->localvd->camera= sc->scene->camera;
                                                
                                                /* localview can become invalid during undo/redo steps, so we exit it when no could be found */
+                                               /* XXX  regionlocalview ?
                                                for(base= sc->scene->base.first; base; base= base->next) {
                                                        if(base->lay & v3d->lay) break;
                                                }
                                                if(base==NULL) {
                                                        v3d->lay= v3d->localvd->lay;
                                                        v3d->layact= v3d->localvd->layact;
-                                                       MEM_freeN(v3d->localvd);
+                                                       MEM_freeN(v3d->localvd); 
                                                        v3d->localvd= NULL;
                                                        v3d->localview= 0;
                                                }
+                                               */
                                        }
                                        else if(v3d->scenelock) v3d->lay= sc->scene->lay;
-                                       
+
                                        /* not very nice, but could help */
                                        if((v3d->layact & v3d->lay)==0) v3d->layact= v3d->lay;
                                        
+                               }
+                               else if(sl->spacetype==SPACE_IPO) {
+                                       /* XXX animato */
+#if 0
+                                       SpaceIpo *sipo= (SpaceIpo *)sl;
+
+                                       sipo->ipo= restore_pointer_by_name(newmain, (ID *)sipo->ipo, 0);
+                                       if(sipo->blocktype==ID_SEQ) 
+                                               sipo->from= (ID *)find_sequence_from_ipo_helper(newmain, sipo->ipo);
+                                       else 
+                                               sipo->from= restore_pointer_by_name(newmain, (ID *)sipo->from, 0);
+                                       
+                                       // not free sipo->ipokey, creates dependency with src/
+                                       if(sipo->editipo) MEM_freeN(sipo->editipo);
+                                       sipo->editipo= NULL;
+#endif
                                }
                                else if(sl->spacetype==SPACE_BUTS) {
                                        SpaceButs *sbuts= (SpaceButs *)sl;
                                        sbuts->lockpoin= NULL;
+                                       sbuts->pinid = restore_pointer_by_name(newmain, sbuts->pinid, 0);
                                        //XXX if (sbuts->ri) sbuts->ri->curtile = 0;
                                }
                                else if(sl->spacetype==SPACE_FILE) {
                                        
                                        SpaceFile *sfile= (SpaceFile *)sl;
                                        sfile->files= NULL;
+                                       sfile->folders_prev= NULL;
+                                       sfile->folders_next= NULL;
                                        sfile->params= NULL;
                                        sfile->op= NULL;
-                                       /* XXX needs checking - best solve in filesel itself 
-                                       if(sfile->libfiledata)  
-                                               BLO_blendhandle_close(sfile->libfiledata);
-                                       sfile->libfiledata= 0;
-                                       */
                                }
                                else if(sl->spacetype==SPACE_IMASEL) {
                     SpaceImaSel *simasel= (SpaceImaSel *)sl;
@@ -4316,17 +4512,10 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                                SCRIPT_SET_NULL(scpt->script)
                                        }
                                }
-                               else if(sl->spacetype==SPACE_OOPS) {
+                               else if(sl->spacetype==SPACE_OUTLINER) {
                                        SpaceOops *so= (SpaceOops *)sl;
-                                       Oops *oops;
                                        int a;
                                        
-                                       oops= so->oops.first;
-                                       while(oops) {
-                                               oops->id= restore_pointer_by_name(newmain, (ID *)oops->id, 0);
-                                               oops= oops->next;
-                                       }
-                                       so->lockpoin= NULL;
                                        so->search_tse.id= restore_pointer_by_name(newmain, so->search_tse.id, 0);
                                        
                                        if(so->treestore) {
@@ -4345,8 +4534,19 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                else if(sl->spacetype==SPACE_NODE) {
                                        SpaceNode *snode= (SpaceNode *)sl;
                                        
-                                       snode->nodetree= snode->edittree= NULL;
-                                       snode->flag |= SNODE_DO_PREVIEW;
+                                       snode->id= restore_pointer_by_name(newmain, snode->id, 1);
+                                       snode->edittree= NULL;
+                                       
+                                       if(snode->id==NULL)
+                                               snode->nodetree= NULL;
+                                       else {
+                                               if(GS(snode->id->name)==ID_MA)
+                                                       snode->nodetree= ((Material *)snode->id)->nodetree;
+                                               else if(GS(snode->id->name)==ID_SCE)
+                                                       snode->nodetree= ((Scene *)snode->id)->nodetree;
+                                               else if(GS(snode->id->name)==ID_TE)
+                                                       snode->nodetree= ((Tex *)snode->id)->nodetree;
+                                       }
                                }
                        }
                        sa= sa->next;
@@ -4362,9 +4562,9 @@ static void direct_link_region(FileData *fd, ARegion *ar, int spacetype)
 
        for(pa= ar->panels.first; pa; pa=pa->next) {
                pa->paneltab= newdataadr(fd, pa->paneltab);
-               pa->active= 0;
-               pa->sortcounter= 0;
+               pa->runtime_flag= 0;
                pa->activedata= NULL;
+               pa->type= NULL;
        }
        
        ar->regiondata= newdataadr(fd, ar->regiondata);
@@ -4380,6 +4580,7 @@ static void direct_link_region(FileData *fd, ARegion *ar, int spacetype)
                        rv3d->ri= NULL;
                        rv3d->sms= NULL;
                        rv3d->smooth_timer= NULL;
+                       rv3d->lastmode= 0;
                }
        }
        
@@ -4409,7 +4610,6 @@ static void view3d_split_250(View3D *v3d, ListBase *regions)
                        rv3d->dist= v3d->dist;
                        VECCOPY(rv3d->ofs, v3d->ofs);
                        QUATCOPY(rv3d->viewquat, v3d->viewquat);
-                       Mat4One(rv3d->twmat);
                }
        }
 }
@@ -4419,7 +4619,6 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
        ScrArea *sa;
        ScrVert *sv;
        ScrEdge *se;
-       Oops *oops;
        int a;
        
        link_list(fd, &(sc->vertbase));
@@ -4474,7 +4673,7 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                /* 2.50: we now always add spacedata for info */
                if(sa->spacedata.first==NULL) {
                        SpaceInfo *sinfo= MEM_callocN(sizeof(SpaceInfo), "spaceinfo");
-                       sa->spacetype= SPACE_INFO;
+                       sa->spacetype= sinfo->spacetype= SPACE_INFO;
                        BLI_addtail(&sa->spacedata, sinfo);
                }
                /* add local view3d too */
@@ -4494,7 +4693,7 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                        v3d->bgpic->iuser.ok= 1;
                                if(v3d->gpd) {
                                        v3d->gpd= newdataadr(fd, v3d->gpd);
-                                       link_gpencil(fd, v3d->gpd);
+                                       direct_link_gpencil(fd, v3d->gpd);
                                }
                                v3d->localvd= newdataadr(fd, v3d->localvd);
                                v3d->afterdraw.first= v3d->afterdraw.last= NULL;
@@ -4506,17 +4705,11 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                SpaceIpo *sipo= (SpaceIpo*)sl;
                                
                                sipo->ads= newdataadr(fd, sipo->ads);
+                               sipo->ghostCurves.first= sipo->ghostCurves.last= NULL;
                        }
-                       else if (sl->spacetype==SPACE_OOPS) {
+                       else if (sl->spacetype==SPACE_OUTLINER) {
                                SpaceOops *soops= (SpaceOops*) sl;
                                
-                               link_list(fd, &(soops->oops));
-                               oops= soops->oops.first;
-                               while(oops) {
-                                       oops->link.first= oops->link.last= 0;
-                                       oops= oops->next;
-                               }
-                               
                                soops->treestore= newdataadr(fd, soops->treestore);
                                if(soops->treestore) {
                                        soops->treestore->data= newdataadr(fd, soops->treestore->data);
@@ -4529,11 +4722,12 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                SpaceImage *sima= (SpaceImage *)sl;
                                
                                sima->cumap= newdataadr(fd, sima->cumap);
-                               if(sima->cumap)
-                                       direct_link_curvemapping(fd, sima->cumap);
                                sima->gpd= newdataadr(fd, sima->gpd);
                                if (sima->gpd)
-                                       link_gpencil(fd, sima->gpd);
+                                       direct_link_gpencil(fd, sima->gpd);
+                               if(sima->cumap)
+                                       direct_link_curvemapping(fd, sima->cumap);
+                               sima->iuser.scene= NULL;
                                sima->iuser.ok= 1;
                        }
                        else if(sl->spacetype==SPACE_NODE) {
@@ -4541,18 +4735,29 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                
                                if(snode->gpd) {
                                        snode->gpd= newdataadr(fd, snode->gpd);
-                                       link_gpencil(fd, snode->gpd);
+                                       direct_link_gpencil(fd, snode->gpd);
                                }
                                snode->nodetree= snode->edittree= NULL;
-                               snode->flag |= SNODE_DO_PREVIEW;
+                       }
+                       else if(sl->spacetype==SPACE_LOGIC) {
+                               SpaceLogic *slogic= (SpaceLogic *)sl;
+                               
+                               if(slogic->gpd) {
+                                       slogic->gpd= newdataadr(fd, slogic->gpd);
+                                       direct_link_gpencil(fd, slogic->gpd);
+                               }
                        }
                        else if(sl->spacetype==SPACE_SEQ) {
                                SpaceSeq *sseq= (SpaceSeq *)sl;
                                if(sseq->gpd) {
                                        sseq->gpd= newdataadr(fd, sseq->gpd);
-                                       link_gpencil(fd, sseq->gpd);
+                                       direct_link_gpencil(fd, sseq->gpd);
                                }
                        }
+                       else if(sl->spacetype==SPACE_BUTS) {
+                               SpaceButs *sbuts= (SpaceButs *)sl;
+                               sbuts->path= NULL;
+                       }
                }
                
                sa->actionzones.first= sa->actionzones.last= NULL;
@@ -4727,6 +4932,7 @@ static char *dataname(short id_code)
                case ID_NT: return "Data from NT";
                case ID_BR: return "Data from BR";
                case ID_PA: return "Data from PA";
+               case ID_GD: return "Data from GD";
        }
        return "Data from Lib Block";
        
@@ -4883,6 +5089,9 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
                case ID_SCRIPT:
                        direct_link_script(fd, (Script*)id);
                        break;
+               case ID_GD:
+                       direct_link_gpencil(fd, (bGPdata *)id);
+                       break;
        }
        
        /*link direct data of ID properties*/
@@ -5383,20 +5592,15 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                /* temporarily hide it */
                                ar->flag = RGN_FLAG_HIDDEN;
                                break;
-                               
-                       case SPACE_FILE:
-                               /* channel (bookmarks/directories) region */
+#if 0
+                       case SPACE_BUTS:
+                               /* context UI region */
                                ar= MEM_callocN(sizeof(ARegion), "area region from do_versions");
                                BLI_addtail(lb, ar);
                                ar->regiontype= RGN_TYPE_CHANNELS;
-                               ar->alignment= RGN_ALIGN_LEFT;
-                               ar->v2d.scroll= V2D_SCROLL_RIGHT;
-                               /* button UI region */
-                               ar= MEM_callocN(sizeof(ARegion), "area region from do_versions");
-                               BLI_addtail(lb, ar);
-                               ar->regiontype= RGN_TYPE_UI;
                                ar->alignment= RGN_ALIGN_TOP;
                                break;
+#endif
                }
        }
 
@@ -5416,7 +5620,7 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                view3d_split_250((View3D *)sl, lb);
                                break;          
                                                
-                       case SPACE_OOPS:
+                       case SPACE_OUTLINER:
                        {
                                SpaceOops *soops= (SpaceOops *)sl;
                                
@@ -5429,8 +5633,6 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                ar->v2d.keeptot = V2D_KEEPTOT_STRICT;
                                ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
                                //ar->v2d.flag |= V2D_IS_INITIALISED;
-                               
-                               soops->type= SO_OUTLINER;
                        }
                                break;
                        case SPACE_TIME:
@@ -5454,11 +5656,7 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                /* init mainarea view2d */
                                ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
                                ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
-                               
-                               /* init dopesheet */
-                               // XXX move this elsewhere instead?
-                               sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
-                               
+                                                               
                                //ar->v2d.flag |= V2D_IS_INITIALISED;
                                break;
                        }
@@ -5488,7 +5686,7 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                /* we totally reinit the view for the Action Editor, as some old instances had some weird cruft set */
                                ar->v2d.tot.xmin= -20.0f;
                                ar->v2d.tot.ymin= (float)(-sa->winy);
-                               ar->v2d.tot.xmax= (float)(sa->winx);
+                               ar->v2d.tot.xmax= (float)((sa->winx > 120)? (sa->winx) : 120);
                                ar->v2d.tot.ymax= 0.0f;
                                
                                ar->v2d.cur= ar->v2d.tot;
@@ -5532,7 +5730,6 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                        {
                                SpaceButs *sbuts= (SpaceButs *)sl;
                                memcpy(&ar->v2d, &sbuts->v2d, sizeof(View2D));
-                               ar->v2d.keepzoom |= V2D_KEEPASPECT;
                                break;
                        }
                        case SPACE_FILE:
@@ -5547,6 +5744,11 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                ar->v2d.align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
                                ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
                                break;
+                       }
+                       case SPACE_TEXT:
+                       {
+                               SpaceText *st= (SpaceText *)sl;
+                               st->flags |= ST_FIND_WRAP;
                        }
                                //case SPACE_XXX: // FIXME... add other ones
                                //      memcpy(&ar->v2d, &((SpaceXxx *)sl)->v2d, sizeof(View2D));
@@ -5587,6 +5789,63 @@ static void do_versions_windowmanager_2_50(bScreen *screen)
        }
 }
 
+static void versions_gpencil_add_main(ListBase *lb, ID *id, char *name)
+{
+       
+       BLI_addtail(lb, id);
+       id->us= 1;
+       id->flag= LIB_FAKEUSER;
+       *( (short *)id->name )= ID_GD;
+       
+       new_id(lb, id, name);
+       /* alphabetic insterion: is in new_id */
+       
+       if(G.f & G_DEBUG)
+               printf("Converted GPencil to ID: %s\n", id->name+2);
+}
+
+static void do_versions_gpencil_2_50(Main *main, bScreen *screen)
+{
+       ScrArea *sa;
+       SpaceLink *sl;
+       
+       /* add regions */
+       for(sa= screen->areabase.first; sa; sa= sa->next) {
+               for(sl= sa->spacedata.first; sl; sl= sl->next) {
+                       if (sl->spacetype==SPACE_VIEW3D) {
+                               View3D *v3d= (View3D*) sl;
+                               if(v3d->gpd) {
+                                       versions_gpencil_add_main(&main->gpencil, (ID *)v3d->gpd, "GPencil View3D");
+                                       v3d->gpd= NULL;
+                               }
+                       }
+                       else if (sl->spacetype==SPACE_NODE) {
+                               SpaceNode *snode= (SpaceNode *)sl;
+                               if(snode->gpd) {
+                                       versions_gpencil_add_main(&main->gpencil, (ID *)snode->gpd, "GPencil Node");
+                                       snode->gpd= NULL;
+                               }
+                       }
+                       else if (sl->spacetype==SPACE_SEQ) {
+                               SpaceSeq *sseq= (SpaceSeq *)sl;
+                               if(sseq->gpd) {
+                                       versions_gpencil_add_main(&main->gpencil, (ID *)sseq->gpd, "GPencil Node");
+                                       sseq->gpd= NULL;
+                               }
+                       }
+                       else if (sl->spacetype==SPACE_IMAGE) {
+                               SpaceImage *sima= (SpaceImage *)sl;
+                               if(sima->gpd) {
+                                       versions_gpencil_add_main(&main->gpencil, (ID *)sima->gpd, "GPencil Image");
+                                       sima->gpd= NULL;
+                               }
+                       }
+               }
+       }               
+}
+
+
+
 static void do_versions(FileData *fd, Library *lib, Main *main)
 {
        /* WATCH IT!!!: pointers from libdata have not been converted */
@@ -5859,10 +6118,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                while(sl) {
                                        if(sl->spacetype==SPACE_TEXT) {
                                                SpaceText *st= (SpaceText*) sl;
-                                               if(st->font_id>1) {
-                                                       st->font_id= 0;
-                                                       st->lheight= 13;
-                                               }
+                                               st->lheight= 12;
                                        }
                                        sl= sl->next;
                                }
@@ -6567,26 +6823,26 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                        
                                                if(sbuts->mainb==BUTS_LAMP) {
                                                        sbuts->mainb= CONTEXT_SHADING;
-                                                       sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_LAMP;
+                                                       //sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_LAMP;
                                                }
                                                else if(sbuts->mainb==BUTS_MAT) {
                                                        sbuts->mainb= CONTEXT_SHADING;
-                                                       sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_MAT;
+                                                       //sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_MAT;
                                                }
                                                else if(sbuts->mainb==BUTS_TEX) {
                                                        sbuts->mainb= CONTEXT_SHADING;
-                                                       sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_TEX;
+                                                       //sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_TEX;
                                                }
                                                else if(sbuts->mainb==BUTS_ANIM) {
                                                        sbuts->mainb= CONTEXT_OBJECT;
                                                }
                                                else if(sbuts->mainb==BUTS_WORLD) {
                                                        sbuts->mainb= CONTEXT_SCENE;
-                                                       sbuts->tab[CONTEXT_SCENE]= TAB_SCENE_WORLD;
+                                                       //sbuts->tab[CONTEXT_SCENE]= TAB_SCENE_WORLD;
                                                }
                                                else if(sbuts->mainb==BUTS_RENDER) {
                                                        sbuts->mainb= CONTEXT_SCENE;
-                                                       sbuts->tab[CONTEXT_SCENE]= TAB_SCENE_RENDER;
+                                                       //sbuts->tab[CONTEXT_SCENE]= TAB_SCENE_RENDER;
                                                }
                                                else if(sbuts->mainb==BUTS_GAME) {
                                                        sbuts->mainb= CONTEXT_LOGIC;
@@ -6596,7 +6852,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                                }
                                                else if(sbuts->mainb==BUTS_RADIO) {
                                                        sbuts->mainb= CONTEXT_SHADING;
-                                                       sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_RAD;
+                                                       //sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_RAD;
                                                }
                                                else if(sbuts->mainb==BUTS_CONSTRAINT) {
                                                        sbuts->mainb= CONTEXT_OBJECT;
@@ -6831,22 +7087,14 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
        
 
        if(main->versionfile <= 234) {
-               Scene *sce;
                World *wo;
                bScreen *sc;
-               int set_zbuf_sel=0;
                
                // force sumo engine to be active
                for (wo = main->world.first; wo; wo= wo->id.next) {
                        if(wo->physicsEngine==0) wo->physicsEngine = 2;
                }
                
-               for (sce= main->scene.first; sce; sce= sce->id.next) {
-                       if(sce->selectmode==0) {
-                               sce->selectmode= SCE_SELECT_VERTEX;
-                               set_zbuf_sel= 1;
-                       }
-               }
                for (sc= main->screen.first; sc; sc= sc->id.next) {
                        ScrArea *sa;
                        for (sa= sc->areabase.first; sa; sa= sa->next) {
@@ -6854,7 +7102,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                for (sl= sa->spacedata.first; sl; sl= sl->next) {
                                        if(sl->spacetype==SPACE_VIEW3D) {
                                                View3D *v3d= (View3D *)sl;
-                                               if(set_zbuf_sel) v3d->flag |= V3D_ZBUF_SELECT;
+                                               v3d->flag |= V3D_ZBUF_SELECT;
                                        }
                                        else if(sl->spacetype==SPACE_TEXT) {
                                                SpaceText *st= (SpaceText *)sl;
@@ -6889,16 +7137,10 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
        }
        if(main->versionfile <= 236) {
                Object *ob;
-               Scene *sce= main->scene.first;
                Camera *cam= main->camera.first;
                Material *ma;
                bScreen *sc;
 
-               while(sce) {
-                       if(sce->editbutsize==0.0) sce->editbutsize= 0.1f;
-                       
-                       sce= sce->id.next;
-               }
                while(cam) {
                        if(cam->ortho_scale==0.0) {
                                cam->ortho_scale= 256.0f/cam->lens;
@@ -6918,12 +7160,6 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                                View3D *v3d= (View3D *)sl;
                                                if(v3d->twtype==0) v3d->twtype= V3D_MANIP_TRANSLATE;
                                        }
-#ifndef SHOWDEPGRAPH
-                                       else if(sl->spacetype==SPACE_OOPS) {
-                                               if ( ((SpaceOops *)sl)->type==SO_DEPSGRAPH)
-                                                        ((SpaceOops *)sl)->type=SO_OOPS;
-                                       }
-#endif                         
                                        else if(sl->spacetype==SPACE_TIME) {
                                                SpaceTime *stime= (SpaceTime *)sl;
                                                if(stime->redraws==0)
@@ -7994,28 +8230,6 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                ima->flag |= IMA_DO_PREMUL;
                        }
                }
-
-               if (main->versionfile < 245 || main->subversionfile < 12)
-               {
-                       /* initialize skeleton generation toolsettings */
-                       for(sce=main->scene.first; sce; sce = sce->id.next)
-                       {
-                               sce->toolsettings->skgen_resolution = 50;
-                               sce->toolsettings->skgen_threshold_internal     = 0.01f;
-                               sce->toolsettings->skgen_threshold_external     = 0.01f;
-                               sce->toolsettings->skgen_angle_limit                    = 45.0f;
-                               sce->toolsettings->skgen_length_ratio                   = 1.3f;
-                               sce->toolsettings->skgen_length_limit                   = 1.5f;
-                               sce->toolsettings->skgen_correlation_limit              = 0.98f;
-                               sce->toolsettings->skgen_symmetry_limit                 = 0.1f;
-                               sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
-                               sce->toolsettings->skgen_postpro_passes = 1;
-                               sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_SUB_CORRELATION;
-                               sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
-                               sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
-                               sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
-                       }
-               }
        }
        
        /* sanity check for skgen
@@ -8251,7 +8465,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
 
                                                part->draw_as = PART_DRAW_PATH;
                                                part->type = PART_HAIR;
-                                               psys->recalc |= PSYS_RECALC_HAIR;
+                                               psys->recalc |= PSYS_RECALC_REDO;
 
                                                part->normfac *= fac;
                                                part->randfac *= fac;
@@ -8552,15 +8766,6 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        }
                }
        }
-       /* autokey mode settings now used from scene, but need to be initialised off userprefs */
-       if (main->versionfile < 247 || (main->versionfile == 247 && main->subversionfile < 8)) {
-               Scene *sce;
-               
-               for (sce= main->scene.first; sce; sce= sce->id.next) {
-                       if (sce->autokey_mode == 0)
-                               sce->autokey_mode= U.autokey_mode;
-               }
-       }
 
        if (main->versionfile < 247 || (main->versionfile == 247 && main->subversionfile < 9)) {
                Lamp *la= main->lamp.first;
@@ -8616,6 +8821,31 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                for (sce= main->scene.first; sce; sce= sce->id.next) {
                        sce->toolsettings->imapaint.seam_bleed = 2;
                        sce->toolsettings->imapaint.normal_angle = 80;
+
+                       /* initialize skeleton generation toolsettings */
+                       sce->toolsettings->skgen_resolution = 250;
+                       sce->toolsettings->skgen_threshold_internal     = 0.1f;
+                       sce->toolsettings->skgen_threshold_external     = 0.1f;
+                       sce->toolsettings->skgen_angle_limit                    = 30.0f;
+                       sce->toolsettings->skgen_length_ratio                   = 1.3f;
+                       sce->toolsettings->skgen_length_limit                   = 1.5f;
+                       sce->toolsettings->skgen_correlation_limit              = 0.98f;
+                       sce->toolsettings->skgen_symmetry_limit                 = 0.1f;
+                       sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
+                       sce->toolsettings->skgen_postpro_passes = 3;
+                       sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_SUB_CORRELATION|SKGEN_HARMONIC;
+                       sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
+                       sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
+                       sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
+
+                       
+                       sce->toolsettings->skgen_retarget_angle_weight = 1.0f;
+                       sce->toolsettings->skgen_retarget_length_weight = 1.0f;
+                       sce->toolsettings->skgen_retarget_distance_weight = 1.0f;
+       
+                       /* Skeleton Sketching */
+                       sce->toolsettings->bone_sketching = 0;
+                       sce->toolsettings->skgen_retarget_roll = SK_RETARGET_ROLL_VIEW;
                }
        }
        if (main->versionfile < 248 || (main->versionfile == 248 && main->subversionfile < 3)) {
@@ -8656,28 +8886,131 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
        }
 
+       if (main->versionfile < 248 || (main->versionfile == 248 && main->subversionfile < 3)) {
+               Object *ob;
+
+               /* Adjustments needed after Bullets update */
+               for(ob = main->object.first; ob; ob= ob->id.next) {
+                       ob->damping *= 0.635f;
+                       ob->rdamping = 0.1 + (0.8f * ob->rdamping);
+               }
+       }
+       
+       if (main->versionfile < 248 || (main->versionfile == 248 && main->subversionfile < 4)) {
+               Scene *sce;
+               World *wrld;
+
+               /*  Dome (Fisheye) default parameters  */
+               for (sce= main->scene.first; sce; sce= sce->id.next) {
+                       sce->r.domeangle = 180;
+                       sce->r.domemode = 1;
+                       sce->r.domeres = 4;
+                       sce->r.domeresbuf = 1.0f;
+                       sce->r.dometilt = 0;
+               }
+               /* DBVT culling by default */
+               for(wrld=main->world.first; wrld; wrld= wrld->id.next) {
+                       wrld->mode |= WO_DBVT_CULLING;
+                       wrld->occlusionRes = 128;
+               }
+       }
+
+       if (main->versionfile < 248 || (main->versionfile == 248 && main->subversionfile < 5)) {
+               Object *ob;
+               World *wrld;
+               for(ob = main->object.first; ob; ob= ob->id.next) {
+                       ob->m_contactProcessingThreshold = 1.; //pad3 is used for m_contactProcessingThreshold
+                       if(ob->parent) {
+                               /* check if top parent has compound shape set and if yes, set this object
+                                  to compound shaper as well (was the behaviour before, now it's optional) */
+                               Object *parent= newlibadr(fd, lib, ob->parent);
+                               while (parent && parent->parent != NULL) {
+                                       parent = newlibadr(fd, lib, parent->parent);
+                               }
+                               if(parent) {
+                                       if (parent->gameflag & OB_CHILD)
+                                               ob->gameflag |= OB_CHILD;
+                               }
+                       }
+               }
+               for(wrld=main->world.first; wrld; wrld= wrld->id.next) {
+                       wrld->ticrate = 60;
+                       wrld->maxlogicstep = 5;
+                       wrld->physubstep = 1;
+                       wrld->maxphystep = 5;
+               }
+       }
+
+       if (main->versionfile < 249) {
+               Scene *sce;
+               for (sce= main->scene.first; sce; sce= sce->id.next)
+                       sce->r.renderer= 0;
+               
+       }
+       
+       // correct introduce of seed for wind force
+       if (main->versionfile < 249 && main->subversionfile < 1) {
+               Object *ob;
+               for(ob = main->object.first; ob; ob= ob->id.next) {
+                       if(ob->pd)
+                               ob->pd->seed = ((unsigned int)(ceil(PIL_check_seconds_timer()))+1) % 128;
+               }
+       
+       }
+
+       if (main->versionfile < 249 && main->subversionfile < 2) {
+               Scene *sce= main->scene.first;
+               Sequence *seq;
+               Editing *ed;
+               
+               while(sce) {
+                       ed= sce->ed;
+                       if(ed) {
+                               SEQP_BEGIN(ed, seq) {
+                                       if (seq->strip && seq->strip->proxy){
+                                               if (sce->r.size != 100.0) {
+                                                       seq->strip->proxy->size
+                                                               = sce->r.size;
+                                               } else {
+                                                       seq->strip->proxy->size
+                                                               = 25.0;
+                                               }
+                                               seq->strip->proxy->quality =90;
+                                       }
+                               }
+                               SEQ_END
+                       }
+                       
+                       sce= sce->id.next;
+               }
+
+       }
+
        if (main->versionfile < 250) {
                bScreen *screen;
                Scene *scene;
                Material *ma;
+               Mesh *me;
                Scene *sce;
                Tex *tx;
+               ParticleSettings *part;
+               Object *ob;
+               PTCacheID *pid;
+               ListBase pidlist;
                
-               for(screen= main->screen.first; screen; screen= screen->id.next)
+               for(screen= main->screen.first; screen; screen= screen->id.next) {
                        do_versions_windowmanager_2_50(screen);
+                       do_versions_gpencil_2_50(main, screen);
+               }
                
                /* old Animation System (using IPO's) needs to be converted to the new Animato system 
                 * (NOTE: conversion code in blenkernel/intern/ipo.c for now)
                 */
                //do_versions_ipos_to_animato(main);
                
-               /* struct audio data moved to renderdata */
-               for(scene= main->scene.first; scene; scene= scene->id.next) {
+               /* toolsettings */
+               for(scene= main->scene.first; scene; scene= scene->id.next)
                        scene->r.audio = scene->audio;
-                       
-                       if(!scene->toolsettings->uv_selectmode)
-                               scene->toolsettings->uv_selectmode= UV_SELECT_VERTEX;
-               }
                
                /* shader, composit and texture node trees have id.name empty, put something in
                 * to have them show in RNA viewer and accessible otherwise.
@@ -8696,12 +9029,48 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        if(tx->nodetree && strlen(tx->nodetree->id.name)==0)
                                strcpy(tx->nodetree->id.name, "NTTexture Nodetree");
                }
+               
+               /* copy standard draw flag to meshes(used to be global, is not available here) */
+               for(me= main->mesh.first; me; me= me->id.next) {
+                       me->drawflag= ME_DRAWEDGES|ME_DRAWFACES|ME_DRAWCREASES;
+               }
+
+               /* particle draw and render types */
+               for(part= main->particle.first; part; part= part->id.next) {
+                       if(part->draw_as) {
+                               if(part->draw_as == PART_DRAW_DOT) {
+                                       part->ren_as = PART_DRAW_HALO;
+                                       part->draw_as = PART_DRAW_REND;
+                               }
+                               else if(part->draw_as <= PART_DRAW_AXIS) {
+                                       part->ren_as = PART_DRAW_HALO;
+                               }
+                               else {
+                                       part->ren_as = part->draw_as;
+                                       part->draw_as = PART_DRAW_REND;
+                               }
+                       }
+                       part->path_end = 1.0f;
+               }
+               /* set old pointcaches to have disk cache flag */
+               for(ob = main->object.first; ob; ob= ob->id.next) {
+
+                       BKE_ptcache_ids_from_object(&pidlist, ob);
+
+                       for(pid=pidlist.first; pid; pid=pid->next)
+                               pid->cache->flag |= PTCACHE_DISK_CACHE;
+
+                       BLI_freelistN(&pidlist);
+               }
        }
 
        /* TODO: should be moved into one of the version blocks once this branch moves to trunk and we can
           bump the version (or sub-version.) */
        {
                Object *ob;
+               Material *ma;
+               Scene *sce;
+               ToolSettings *ts;
                int i;
 
                for(ob = main->object.first; ob; ob = ob->id.next) {
@@ -8711,7 +9080,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                void *olddata = ob->data;
                                ob->data = me;
 
-                               if(me && me->mr) {
+                               if(me && me->id.lib==NULL && me->mr) { /* XXX - library meshes crash on loading most yoFrankie levels, the multires pointer gets invalid -  Campbell */
                                        MultiresLevel *lvl;
                                        ModifierData *md;
                                        MultiresModifierData *mmd;
@@ -8755,7 +9124,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                        
                                        multires_load_old(dm, me->mr);
 
-                                       *MultiresDM_get_flags(dm) |= MULTIRES_DM_UPDATE_ALWAYS;
+                                       MultiresDM_mark_as_modified(dm);
                                        dm->release(dm);
                                        orig->release(orig);
 
@@ -8767,8 +9136,26 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                ob->data = olddata;
                        }
                }
+
+               for(ma = main->mat.first; ma; ma = ma->id.next) {
+                       if(ma->mode & MA_HALO) {
+                               ma->material_type= MA_TYPE_HALO;
+                               ma->mode &= ~MA_HALO;
+                       }
+               }
+
+               for(sce = main->scene.first; sce; sce = sce->id.next) {
+                       ts= sce->toolsettings;
+                       if(ts->normalsize == 0.0 || !ts->uv_selectmode || ts->vgroup_weight == 0.0) {
+                               ts->normalsize= 0.1f;
+                               ts->selectmode= SCE_SELECT_VERTEX;
+                               ts->autokey_mode= U.autokey_mode;
+                               ts->uv_selectmode= UV_SELECT_VERTEX;
+                               ts->vgroup_weight= 1.0f;
+                       }
+               }
        }
-                                      
+
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
        /* WATCH IT 2!: Userdef struct init has to be in src/usiblender.c! */
 
@@ -8816,7 +9203,10 @@ static BHead *read_userdef(BlendFileData *bfd, FileData *fd, BHead *bhead)
 
        bfd->user= read_struct(fd, bhead, "user def");
        bfd->user->themes.first= bfd->user->themes.last= NULL;
-
+       // XXX
+       bfd->user->uifonts.first= bfd->user->uifonts.last= NULL;
+       bfd->user->uistyles.first= bfd->user->uistyles.last= NULL;
+       
        bhead = blo_nextbhead(fd, bhead);
 
                /* read all attached data */
@@ -9081,6 +9471,19 @@ static void expand_action(FileData *fd, Main *mainvar, bAction *act)
        }
 }
 
+static void expand_keyingsets(FileData *fd, Main *mainvar, ListBase *list)
+{
+       KeyingSet *ks;
+       KS_Path *ksp;
+       
+       /* expand the ID-pointers in KeyingSets's paths */
+       for (ks= list->first; ks; ks= ks->next) {
+               for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
+                       expand_doit(fd, mainvar, ksp->id);
+               }
+       }
+}
+
 static void expand_animdata(FileData *fd, Main *mainvar, AnimData *adt)
 {
        FCurve *fcd;
@@ -9091,9 +9494,10 @@ static void expand_animdata(FileData *fd, Main *mainvar, AnimData *adt)
        /* drivers - assume that these F-Curves have driver data to be in this list... */
        for (fcd= adt->drivers.first; fcd; fcd= fcd->next) {
                ChannelDriver *driver= fcd->driver;
+               DriverTarget *dtar;
                
-               expand_doit(fd, mainvar, driver->id);
-               expand_doit(fd, mainvar, driver->id2);
+               for (dtar= driver->targets.first; dtar; dtar= dtar->next)
+                       expand_doit(fd, mainvar, dtar->id);
        }
 }      
 
@@ -9103,6 +9507,8 @@ static void expand_particlesettings(FileData *fd, Main *mainvar, ParticleSetting
        expand_doit(fd, mainvar, part->dup_group);
        expand_doit(fd, mainvar, part->eff_group);
        expand_doit(fd, mainvar, part->bb_ob);
+       
+       expand_animdata(fd, mainvar, part->adt);
 }
 
 static void expand_group(FileData *fd, Main *mainvar, Group *group)
@@ -9126,6 +9532,9 @@ static void expand_nodetree(FileData *fd, Main *mainvar, bNodeTree *ntree)
 {
        bNode *node;
        
+       if(ntree->adt)
+               expand_animdata(fd, mainvar, ntree->adt);
+       
        for(node= ntree->nodes.first; node; node= node->next)
                if(node->id && node->type!=CMP_NODE_R_LAYERS)
                        expand_doit(fd, mainvar, node->id);
@@ -9395,6 +9804,12 @@ static void expand_constraints(FileData *fd, Main *mainvar, ListBase *lb)
                                expand_doit(fd, mainvar, data->tar);
                        }
                        break;
+               case CONSTRAINT_TYPE_SHRINKWRAP:
+                       {
+                               bShrinkwrapConstraint *data = (bShrinkwrapConstraint*)curcon->data;
+                               expand_doit(fd, mainvar, data->target);
+                       }
+                       break;
                default:
                        break;
                }
@@ -9569,6 +9984,10 @@ static void expand_object(FileData *fd, Main *mainvar, Object *ob)
                                expand_doit(fd, mainvar, eoa->me);
                        }
                }
+               else if(act->type==ACT_OBJECT) {
+                       bObjectActuator *oa= act->data;
+                       expand_doit(fd, mainvar, oa->reference);
+               }
                else if(act->type==ACT_SCENE) {
                        bSceneActuator *sa= act->data;
                        expand_doit(fd, mainvar, sa->camera);
@@ -9612,6 +10031,10 @@ static void expand_scene(FileData *fd, Main *mainvar, Scene *sce)
        
        if(sce->adt)
                expand_animdata(fd, mainvar, sce->adt);
+       expand_keyingsets(fd, mainvar, &sce->keyingsets);
+       
+       if(sce->set)
+               expand_doit(fd, mainvar, sce->set);
        
        if(sce->nodetree)
                expand_nodetree(fd, mainvar, sce->nodetree);
@@ -9620,6 +10043,9 @@ static void expand_scene(FileData *fd, Main *mainvar, Scene *sce)
                expand_doit(fd, mainvar, srl->mat_override);
                expand_doit(fd, mainvar, srl->light_override);
        }
+
+       if(sce->r.dometext)
+               expand_doit(fd, mainvar, sce->r.dometext);
 }
 
 static void expand_camera(FileData *fd, Main *mainvar, Camera *ca)