Animato - Basic version patching support
[blender.git] / source / blender / blenloader / intern / readfile.c
index 5d33deec821d1587cf1b9d7c2a8148d908cfc7ca..23f22523e194b2fc7e9d18755b1cf629d67a9647 100644 (file)
@@ -48,6 +48,7 @@
     #include <io.h> // for open close read
 #endif
 
+#include "DNA_anim_types.h"
 #include "DNA_action_types.h"
 #include "DNA_armature_types.h"
 #include "DNA_ID.h"
 #include "BLI_arithb.h"
 #include "BLI_storage_types.h" // for relname flags
 
+#include "BKE_animsys.h"
 #include "BKE_action.h"
 #include "BKE_armature.h"
+#include "BKE_cdderivedmesh.h"
 #include "BKE_cloth.h"
 #include "BKE_colortools.h"
 #include "BKE_constraint.h"
 #include "BKE_deform.h"
 #include "BKE_depsgraph.h"
 #include "BKE_effect.h" /* give_parteff */
+#include "BKE_fcurve.h"
 #include "BKE_global.h" // for G
 #include "BKE_group.h"
 #include "BKE_image.h"
+#include "BKE_ipo.h" 
 #include "BKE_key.h" //void set_four_ipo
 #include "BKE_lattice.h"
 #include "BKE_library.h" // for wich_libbase
 #include "BKE_main.h" // for Main
 #include "BKE_mesh.h" // for ME_ defines (patching)
 #include "BKE_modifier.h"
+#include "BKE_multires.h"
 #include "BKE_node.h" // for tree type defines
 #include "BKE_object.h"
 #include "BKE_particle.h"
 #include "BKE_pointcache.h"
 #include "BKE_property.h" // for get_ob_property
+#include "BKE_report.h"
 #include "BKE_sca.h" // for init_actuator
 #include "BKE_scene.h"
 #include "BKE_softbody.h"      // sbNew()
 #include "BKE_bullet.h"                // bsbNew()
 #include "BKE_sculpt.h"
+#include "BKE_sequence.h"
 #include "BKE_texture.h" // for open_plugin_tex
 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
 #include "BKE_idprop.h"
 
 #include "readfile.h"
 
-//XXX #include "wm_event_types.h"
-
 #include <errno.h>
 
 /*
@@ -808,7 +814,7 @@ static int read_file_dna(FileData *fd)
        return 0;
 }
 
-static int fd_read_from_file(FileData *filedata, void *buffer, int size)
+static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
 {
        int readsize = read(filedata->filedes, buffer, size);
 
@@ -821,7 +827,7 @@ static int fd_read_from_file(FileData *filedata, void *buffer, int size)
        return (readsize);
 }
 
-static int fd_read_gzip_from_file(FileData *filedata, void *buffer, int size)
+static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
 {
        int readsize = gzread(filedata->gzfiledes, buffer, size);
 
@@ -834,7 +840,7 @@ static int fd_read_gzip_from_file(FileData *filedata, void *buffer, int size)
        return (readsize);
 }
 
-static int fd_read_from_memory(FileData *filedata, void *buffer, int size)
+static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
 {
                // don't read more bytes then there are available in the buffer
        int readsize = MIN2(size, filedata->buffersize - filedata->seek);
@@ -845,7 +851,7 @@ static int fd_read_from_memory(FileData *filedata, void *buffer, int size)
        return (readsize);
 }
 
-static int fd_read_from_memfile(FileData *filedata, void *buffer, int size)
+static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
 {
        static unsigned int seek= 1<<30;        /* the current position */
        static unsigned int offset= 0;          /* size of previous chunks */
@@ -924,19 +930,19 @@ static FileData *filedata_new(void)
        return fd;
 }
 
-static FileData *blo_decode_and_check(FileData *fd, BlendReadError *error_r)
+static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
 {
        decode_blender_header(fd);
 
        if (fd->flags & FD_FLAGS_FILE_OK) {
                if (!read_file_dna(fd)) {
-                       *error_r = BRE_INCOMPLETE;
+                       BKE_report(reports, RPT_ERROR, "File incomplete");
                        blo_freefiledata(fd);
                        fd= NULL;
                }
        } 
        else {
-               *error_r = BRE_NOT_A_BLEND;
+               BKE_report(reports, RPT_ERROR, "File is not a Blender file");
                blo_freefiledata(fd);
                fd= NULL;
        }
@@ -946,14 +952,14 @@ static FileData *blo_decode_and_check(FileData *fd, BlendReadError *error_r)
 
 /* cannot be called with relative paths anymore! */
 /* on each new library added, it now checks for the current FileData and expands relativeness */
-FileData *blo_openblenderfile(char *name, BlendReadError *error_r)
+FileData *blo_openblenderfile(char *name, ReportList *reports)
 {
        gzFile gzfile;
        
        gzfile= gzopen(name, "rb");
 
        if (NULL == gzfile) {
-               *error_r = BRE_UNABLE_TO_OPEN;
+               BKE_report(reports, RPT_ERROR, "Unable to open");
                return NULL;
        } else {
                FileData *fd = filedata_new();
@@ -963,14 +969,14 @@ FileData *blo_openblenderfile(char *name, BlendReadError *error_r)
                /* needed for library_append and read_libraries */
                BLI_strncpy(fd->filename, name, sizeof(fd->filename));
 
-               return blo_decode_and_check(fd, error_r);
+               return blo_decode_and_check(fd, reports);
        }
 }
 
-FileData *blo_openblendermemory(void *mem, int memsize, BlendReadError *error_r)
+FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
 {
        if (!mem || memsize<SIZEOFBLENDERHEADER) {
-               *error_r = mem?BRE_UNABLE_TO_READ:BRE_UNABLE_TO_OPEN;
+               BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
                return NULL;
        } else {
                FileData *fd= filedata_new();
@@ -979,14 +985,14 @@ FileData *blo_openblendermemory(void *mem, int memsize, BlendReadError *error_r)
                fd->read= fd_read_from_memory;
                fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
 
-               return blo_decode_and_check(fd, error_r);
+               return blo_decode_and_check(fd, reports);
        }
 }
 
-FileData *blo_openblendermemfile(MemFile *memfile, BlendReadError *error_r)
+FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
 {
        if (!memfile) {
-               *error_r = BRE_UNABLE_TO_OPEN;
+               BKE_report(reports, RPT_ERROR, "Unable to open");
                return NULL;
        } else {
                FileData *fd= filedata_new();
@@ -995,7 +1001,7 @@ FileData *blo_openblendermemfile(MemFile *memfile, BlendReadError *error_r)
                fd->read= fd_read_from_memfile;
                fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
 
-               return blo_decode_and_check(fd, error_r);
+               return blo_decode_and_check(fd, reports);
        }
 }
 
@@ -1120,20 +1126,19 @@ static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, voi
  * to clear that pointer before reading the undo memfile since
  * the object might be removed, it is set again in reading
  * if the local object still exists */
-void blo_clear_proxy_pointers_from_lib(FileData *fd)
+void blo_clear_proxy_pointers_from_lib(FileData *fd, Main *oldmain)
 {
-       Object *ob= G.main->object.first;
+       Object *ob= oldmain->object.first;
        
        for(;ob; ob= ob->id.next)
                if(ob->id.lib)
                        ob->proxy_from= NULL;
 }
 
-/* assumed; G.main still exists */
-void blo_make_image_pointer_map(FileData *fd)
+void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
 {
-       Image *ima= G.main->image.first;
-       Scene *sce= G.main->scene.first;
+       Image *ima= oldmain->image.first;
+       Scene *sce= oldmain->scene.first;
        
        fd->imamap= oldnewmap_new();
        
@@ -1153,13 +1158,13 @@ void blo_make_image_pointer_map(FileData *fd)
        }
 }
 
-/* set G.main image ibufs to zero if it has been restored */
-/* this works because freeing G.main only happens after this call */
-void blo_end_image_pointer_map(FileData *fd)
+/* set old main image ibufs to zero if it has been restored */
+/* this works because freeing old main only happens after this call */
+void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
 {
        OldNew *entry= fd->imamap->entries;
-       Image *ima= G.main->image.first;
-       Scene *sce= G.main->scene.first;
+       Image *ima= oldmain->image.first;
+       Scene *sce= oldmain->scene.first;
        int i;
        
        /* used entries were restored, so we put them to zero */
@@ -1343,11 +1348,30 @@ static void test_pointer_array(FileData *fd, void **mat)
 
 /* ************ READ ID Properties *************** */
 
-void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd);
-void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd);
+void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
+void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
+
+static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
+{
+       IDProperty **array;
+       int i;
+
+       /*since we didn't save the extra buffer, set totallen to len.*/
+       prop->totallen = prop->len;
+       prop->data.pointer = newdataadr(fd, prop->data.pointer);
+
+       if (switch_endian) {
+               test_pointer_array(fd, prop->data.pointer);
+               array= (IDProperty**) prop->data.pointer;
+
+               for(i=0; i<prop->len; i++)
+                       IDP_DirectLinkProperty(array[i], switch_endian, fd);
+       }
+}
 
-static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, void *fd)
+static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
 {
+       IDProperty **array;
        int i;
 
        /*since we didn't save the extra buffer, set totallen to len.*/
@@ -1355,26 +1379,33 @@ static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, void *fd)
        prop->data.pointer = newdataadr(fd, prop->data.pointer);
 
        if (switch_endian) {
-               if (prop->subtype != IDP_DOUBLE) {
+               if(prop->subtype == IDP_GROUP) {
+                       test_pointer_array(fd, prop->data.pointer);
+                       array= prop->data.pointer;
+
+                       for(i=0; i<prop->len; i++)
+                               IDP_DirectLinkProperty(array[i], switch_endian, fd);
+               }
+               else if(prop->subtype == IDP_DOUBLE) {
                        for (i=0; i<prop->len; i++) {
-                               SWITCH_INT(((int*)prop->data.pointer)[i]);
+                               SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
                        }
                } else {
                        for (i=0; i<prop->len; i++) {
-                               SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
+                               SWITCH_INT(((int*)prop->data.pointer)[i]);
                        }
                }
        }
 }
 
-static void IDP_DirectLinkString(IDProperty *prop, int switch_endian, void *fd)
+static void IDP_DirectLinkString(IDProperty *prop, int switch_endian, FileData *fd)
 {
        /*since we didn't save the extra string buffer, set totallen to len.*/
        prop->totallen = prop->len;
        prop->data.pointer = newdataadr(fd, prop->data.pointer);
 }
 
-static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, void *fd)
+static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
 {
        ListBase *lb = &prop->data.group;
        IDProperty *loop;
@@ -1387,7 +1418,7 @@ static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, void *fd)
        }
 }
 
-void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd)
+void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
 {
        switch (prop->type) {
                case IDP_GROUP:
@@ -1399,6 +1430,9 @@ void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd)
                case IDP_ARRAY:
                        IDP_DirectLinkArray(prop, switch_endian, fd);
                        break;
+               case IDP_IDPARRAY:
+                       IDP_DirectLinkIDPArray(prop, switch_endian, fd);
+                       break;
                case IDP_DOUBLE:
                        /*erg, stupid doubles.  since I'm storing them
                         in the same field as int val; val2 in the
@@ -1421,8 +1455,24 @@ void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd)
 }
 
 /*stub function*/
-void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd)
+void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
+{
+}
+
+/* ************ READ CurveMapping *************** */
+
+/* cuma itself has been read! */
+static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
 {
+       int a;
+       
+       /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
+       cumap->flag &= ~CUMA_PREMULLED;
+       
+       for(a=0; a<CM_TOT; a++) {
+               cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
+               cumap->cm[a].table= NULL;
+       }
 }
 
 /* ************ READ Brush *************** */
@@ -1443,6 +1493,8 @@ static void lib_link_brush(FileData *fd, Main *main)
                                if(mtex)
                                        mtex->tex= newlibadr_us(fd, brush->id.lib, mtex->tex);
                        }
+
+                       brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
                }
        }
 }
@@ -1454,6 +1506,11 @@ static void direct_link_brush(FileData *fd, Brush *brush)
 
        for(a=0; a<MAX_MTEX; a++)
                brush->mtex[a]= newdataadr(fd, brush->mtex[a]);
+
+       /* fallof curve */
+       brush->curve= newdataadr(fd, brush->curve);
+       if(brush->curve)
+               direct_link_curvemapping(fd, brush->curve);
 }
 
 static void direct_link_script(FileData *fd, Script *script)
@@ -1462,22 +1519,6 @@ static void direct_link_script(FileData *fd, Script *script)
        SCRIPT_SET_NULL(script)
 }
 
-/* ************ READ CurveMapping *************** */
-
-/* cuma itself has been read! */
-static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
-{
-       int a;
-       
-       /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
-       cumap->flag &= ~CUMA_PREMULLED;
-       
-       for(a=0; a<CM_TOT; a++) {
-               cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
-               cumap->cm[a].table= NULL;
-       }
-}
-
 /* ************ READ NODE TREE *************** */
 
 /* singe node tree (also used for material/scene trees), ntree is not NULL */
@@ -1678,8 +1719,45 @@ static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
        }
 }
 
-/* ************ READ ARMATURE ***************** */
+/* ************ READ ANIMATION STUFF ***************** */
+
+/* Legacy Data Support (for Version Patching) ----------------------------- */
 
+// XXX depreceated - old animation system
+static void lib_link_ipo(FileData *fd, Main *main)
+{
+       Ipo *ipo;
+
+       ipo= main->ipo.first;
+       while(ipo) {
+               if(ipo->id.flag & LIB_NEEDLINK) {
+                       IpoCurve *icu;
+                       for(icu= ipo->curve.first; icu; icu= icu->next) {
+                               if(icu->driver)
+                                       icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
+                       }
+                       ipo->id.flag -= LIB_NEEDLINK;
+               }
+               ipo= ipo->id.next;
+       }
+}
+
+// XXX depreceated - old animation system
+static void direct_link_ipo(FileData *fd, Ipo *ipo)
+{
+       IpoCurve *icu;
+
+       link_list(fd, &(ipo->curve));
+       icu= ipo->curve.first;
+       while(icu) {
+               icu->bezt= newdataadr(fd, icu->bezt);
+               icu->bp= newdataadr(fd, icu->bp);
+               icu->driver= newdataadr(fd, icu->driver);
+               icu= icu->next;
+       }
+}
+
+// XXX depreceated - old animation system
 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
 {
        bActionStrip *strip;
@@ -1694,6 +1772,18 @@ static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
        }
 }
 
+// XXX depreceated - old animation system
+static void direct_link_nlastrips(FileData *fd, ListBase *strips)
+{
+       bActionStrip *strip;
+       
+       link_list(fd, strips);
+       
+       for(strip= strips->first; strip; strip= strip->next)
+               link_list(fd, &strip->modifiers);
+}
+
+// XXX depreceated - old animation system
 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
 {
        bConstraintChannel *chan;
@@ -1703,6 +1793,178 @@ static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbas
        }
 }
 
+/* Data Linking ----------------------------- */
+
+static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list) 
+{
+       FCurve *fcu;
+       FModifier *fcm;
+       
+       /* relink ID-block references... */
+       for (fcu= list->first; fcu; fcu= fcu->next) {
+               /* 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); 
+               }
+               
+               /* modifiers */
+               for (fcm= fcu->modifiers.first; fcm; fcm= fcm->next) {
+                       /* data for specific modifiers */
+                       switch (fcm->type) {
+                               case FMODIFIER_TYPE_PYTHON:
+                               {
+                                       FMod_Python *data= (FMod_Python *)fcm->data;
+                                       data->script = newlibadr(fd, id->lib, data->script);
+                               }
+                                       break;
+                       }
+               }
+       }
+}
+
+/* NOTE: this assumes that link_list has already been called on the list */
+static void direct_link_fcurves(FileData *fd, ListBase *list)
+{
+       FCurve *fcu;
+       FModifier *fcm;
+       
+       /* link F-Curve data to F-Curve again (non ID-libs) */
+       for (fcu= list->first; fcu; fcu= fcu->next) {
+               /* curve data */
+               fcu->bezt= newdataadr(fd, fcu->bezt);
+               fcu->fpt= newdataadr(fd, fcu->fpt);
+               
+               /* rna path */
+               fcu->rna_path= newdataadr(fd, fcu->rna_path);
+               
+               /* group */
+               fcu->grp= newdataadr(fd, fcu->grp);
+               
+               /* driver */
+               fcu->driver= newdataadr(fd, fcu->driver);
+               if (fcu->driver) {
+                       ChannelDriver *driver= fcu->driver;
+                       
+                       driver->rna_path= newdataadr(fd, driver->rna_path);
+                       driver->rna_path2= newdataadr(fd, driver->rna_path2);
+               }
+               
+               /* modifiers */
+               link_list(fd, &fcu->modifiers);
+               for (fcm= fcu->modifiers.first; fcm; fcm= fcm->next) {
+                       /* relink general data */
+                       fcm->data = newdataadr(fd, fcm->data);
+                       
+                       /* do relinking of data for specific types */
+                       switch (fcm->type) {
+                               case FMODIFIER_TYPE_GENERATOR:
+                               {
+                                       FMod_Generator *data= (FMod_Generator *)fcm->data;
+                                       
+                                       data->poly_coefficients= newdataadr(fd, data->poly_coefficients);
+                               }
+                                       break;
+                               case FMODIFIER_TYPE_PYTHON:
+                               {
+                                       FMod_Python *data= (FMod_Python *)fcm->data;
+                                       
+                                       data->prop = newdataadr(fd, data->prop);
+                                       IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
+                               }
+                                       break;
+                       }
+               }
+       }
+}
+
+
+static void lib_link_action(FileData *fd, Main *main)
+{
+       bAction *act;
+       bActionChannel *chan;
+
+       for (act= main->action.first; act; act= act->id.next) {
+               if (act->id.flag & LIB_NEEDLINK) {
+                       act->id.flag -= LIB_NEEDLINK;
+                       
+// XXX depreceated - old animation system <<<
+                       for (chan=act->chanbase.first; chan; chan=chan->next) {
+                               chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
+                               lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
+                       }
+// >>> XXX depreceated - old animation system
+                       
+                       lib_link_fcurves(fd, &act->id, &act->curves);
+               }
+       }
+}
+
+static void direct_link_action(FileData *fd, bAction *act)
+{
+       bActionChannel *achan; // XXX depreceated - old animation system
+       bActionGroup *agrp;
+
+       link_list(fd, &act->curves);
+       link_list(fd, &act->chanbase); // XXX depreceated - old animation system
+       link_list(fd, &act->groups);
+       link_list(fd, &act->markers);
+
+// XXX depreceated - old animation system <<<
+       for (achan = act->chanbase.first; achan; achan=achan->next) {
+               achan->grp= newdataadr(fd, achan->grp);
+               
+               link_list(fd, &achan->constraintChannels);
+       }
+// >>> XXX depreceated - old animation system
+
+       direct_link_fcurves(fd, &act->curves);
+       
+       for (agrp = act->groups.first; agrp; agrp= agrp->next) {
+               agrp->channels.first= newdataadr(fd, agrp->channels.first);
+               agrp->channels.last= newdataadr(fd, agrp->channels.last);
+       }
+}
+
+/* ------- */
+
+static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
+{
+       if (adt == NULL)
+               return;
+       
+       /* link action data */
+       adt->action= newlibadr_us(fd, id->lib, adt->action);
+       
+       /* link drivers */
+       lib_link_fcurves(fd, id, &adt->drivers);
+       
+       /* overrides don't have lib-link for now, so no need to do anything */
+       
+       /* link NLA-data */
+       // TODO... 
+}
+
+static void direct_link_animdata(FileData *fd, AnimData *adt)
+{
+       /* NOTE: must have called newdataadr already before doing this... */
+       if (adt == NULL)
+               return;
+       
+       /* link drivers */
+       link_list(fd, &adt->drivers);
+       direct_link_fcurves(fd, &adt->drivers);
+       
+       /* link overrides */
+       // TODO...
+       
+       /* link NLA-data */
+       // TODO...
+}      
+
+/* ************ READ ARMATURE ***************** */
+
 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
 {
        bConstraint *con;
@@ -1714,7 +1976,7 @@ static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
                        con->type= CONSTRAINT_TYPE_NULL;
                }
                /* own ipo, all constraints have it */
-               con->ipo= newlibadr_us(fd, id->lib, con->ipo);
+               con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
                
                switch (con->type) {
                case CONSTRAINT_TYPE_PYTHON:
@@ -1907,26 +2169,6 @@ static void lib_link_armature(FileData *fd, Main *main)
        }
 }
 
-static void lib_link_action(FileData *fd, Main *main)
-{
-       bAction *act;
-       bActionChannel *chan;
-
-       act= main->action.first;
-       while(act) {
-               if(act->id.flag & LIB_NEEDLINK) {
-                       act->id.flag -= LIB_NEEDLINK;
-                       
-                       for (chan=act->chanbase.first; chan; chan=chan->next) {
-                               chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
-                               lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
-                       }
-                       
-               }
-               act= act->id.next;
-       }
-}
-
 static void direct_link_bones(FileData *fd, Bone* bone)
 {
        Bone    *child;
@@ -1940,36 +2182,13 @@ static void direct_link_bones(FileData *fd, Bone* bone)
        }
 }
 
-
-static void direct_link_action(FileData *fd, bAction *act)
-{
-       bActionChannel *achan;
-       bActionGroup *agrp;
-
-       link_list(fd, &act->chanbase);
-       link_list(fd, &act->groups);
-       link_list(fd, &act->markers);
-
-       for (achan = act->chanbase.first; achan; achan=achan->next) {
-               achan->grp= newdataadr(fd, achan->grp);
-               
-               link_list(fd, &achan->constraintChannels);
-       }
-               
-       for (agrp = act->groups.first; agrp; agrp= agrp->next) {
-               if (agrp->channels.first) {
-                       agrp->channels.first= newdataadr(fd, agrp->channels.first);
-                       agrp->channels.last= newdataadr(fd, agrp->channels.last);
-               }
-       }
-}
-
 static void direct_link_armature(FileData *fd, bArmature *arm)
 {
        Bone    *bone;
 
        link_list(fd, &arm->bonebase);
-
+       arm->edbo= NULL;
+       
        bone=arm->bonebase.first;
        while (bone) {
                direct_link_bones(fd, bone);
@@ -1986,13 +2205,14 @@ static void lib_link_camera(FileData *fd, Main *main)
        ca= main->camera.first;
        while(ca) {
                if(ca->id.flag & LIB_NEEDLINK) {
-
-                       ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo);
+                       if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
+                       
+                       ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
                        
                        ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
                        
                        lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
-
+                       
                        ca->id.flag -= LIB_NEEDLINK;
                }
                ca= ca->id.next;
@@ -2001,6 +2221,9 @@ static void lib_link_camera(FileData *fd, Main *main)
 
 static void direct_link_camera(FileData *fd, Camera *ca)
 {
+       ca->adt= newdataadr(fd, ca->adt);
+       direct_link_animdata(fd, ca->adt);
+       
        direct_link_scriptlink(fd, &ca->scriptlink);
 }
 
@@ -2016,7 +2239,8 @@ static void lib_link_lamp(FileData *fd, Main *main)
        la= main->lamp.first;
        while(la) {
                if(la->id.flag & LIB_NEEDLINK) {
-
+                       if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
+                       
                        for(a=0; a<MAX_MTEX; a++) {
                                mtex= la->mtex[a];
                                if(mtex) {
@@ -2024,11 +2248,11 @@ static void lib_link_lamp(FileData *fd, Main *main)
                                        mtex->object= newlibadr(fd, la->id.lib, mtex->object);
                                }
                        }
-
-                       la->ipo= newlibadr_us(fd, la->id.lib, la->ipo);
-
+                       
+                       la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
+                       
                        lib_link_scriptlink(fd, &la->id, &la->scriptlink);
-
+                       
                        la->id.flag -= LIB_NEEDLINK;
                }
                la= la->id.next;
@@ -2038,7 +2262,10 @@ static void lib_link_lamp(FileData *fd, Main *main)
 static void direct_link_lamp(FileData *fd, Lamp *la)
 {
        int a;
-
+       
+       la->adt= newdataadr(fd, la->adt);
+       direct_link_animdata(fd, la->adt);
+       
        direct_link_scriptlink(fd, &la->scriptlink);
 
        for(a=0; a<MAX_MTEX; a++) {
@@ -2062,7 +2289,7 @@ static void lib_link_key(FileData *fd, Main *main)
        while(key) {
                if(key->id.flag & LIB_NEEDLINK) {
 
-                       key->ipo= newlibadr_us(fd, key->id.lib, key->ipo);
+                       key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
                        key->from= newlibadr(fd, key->id.lib, key->from);
 
                        key->id.flag -= LIB_NEEDLINK;
@@ -2111,6 +2338,9 @@ static void direct_link_key(FileData *fd, Key *key)
 
        link_list(fd, &(key->block));
 
+       key->adt= newdataadr(fd, key->adt);
+       direct_link_animdata(fd, key->adt);
+       
        key->refkey= newdataadr(fd, key->refkey);
 
        kb= key->block.first;
@@ -2138,7 +2368,7 @@ static void lib_link_mball(FileData *fd, Main *main)
 
                        for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
 
-                       mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo);
+                       mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
 
                        mb->id.flag -= LIB_NEEDLINK;
                }
@@ -2153,9 +2383,9 @@ static void direct_link_mball(FileData *fd, MetaBall *mb)
 
        link_list(fd, &(mb->elems));
 
-       mb->disp.first= mb->disp.last= 0;
-
-       mb->bb= 0;
+       mb->disp.first= mb->disp.last= NULL;
+       mb->editelems= NULL;
+       mb->bb= NULL;
 }
 
 /* ************ READ WORLD ***************** */
@@ -2169,9 +2399,10 @@ static void lib_link_world(FileData *fd, Main *main)
        wrld= main->world.first;
        while(wrld) {
                if(wrld->id.flag & LIB_NEEDLINK) {
-
-                       wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo);
-
+                       if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
+                       
+                       wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
+                       
                        for(a=0; a<MAX_MTEX; a++) {
                                mtex= wrld->mtex[a];
                                if(mtex) {
@@ -2179,9 +2410,9 @@ static void lib_link_world(FileData *fd, Main *main)
                                        mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
                                }
                        }
-
+                       
                        lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
-
+                       
                        wrld->id.flag -= LIB_NEEDLINK;
                }
                wrld= wrld->id.next;
@@ -2192,6 +2423,9 @@ static void direct_link_world(FileData *fd, World *wrld)
 {
        int a;
 
+       wrld->adt= newdataadr(fd, wrld->adt);
+       direct_link_animdata(fd, wrld->adt);
+       
        direct_link_scriptlink(fd, &wrld->scriptlink);
 
        for(a=0; a<MAX_MTEX; a++) {
@@ -2201,40 +2435,6 @@ static void direct_link_world(FileData *fd, World *wrld)
 }
 
 
-/* ************ READ IPO ***************** */
-
-static void lib_link_ipo(FileData *fd, Main *main)
-{
-       Ipo *ipo;
-
-       ipo= main->ipo.first;
-       while(ipo) {
-               if(ipo->id.flag & LIB_NEEDLINK) {
-                       IpoCurve *icu;
-                       for(icu= ipo->curve.first; icu; icu= icu->next) {
-                               if(icu->driver)
-                                       icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
-                       }
-                       ipo->id.flag -= LIB_NEEDLINK;
-               }
-               ipo= ipo->id.next;
-       }
-}
-
-static void direct_link_ipo(FileData *fd, Ipo *ipo)
-{
-       IpoCurve *icu;
-
-       link_list(fd, &(ipo->curve));
-       icu= ipo->curve.first;
-       while(icu) {
-               icu->bezt= newdataadr(fd, icu->bezt);
-               icu->bp= newdataadr(fd, icu->bp);
-               icu->driver= newdataadr(fd, icu->driver);
-               icu= icu->next;
-       }
-}
-
 /* ************ READ VFONT ***************** */
 
 static void lib_link_vfont(FileData *fd, Main *main)
@@ -2393,7 +2593,7 @@ static void lib_link_curve(FileData *fd, Main *main)
                        cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
                        cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
 
-                       cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo);
+                       cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
                        cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
 
                        cu->id.flag -= LIB_NEEDLINK;
@@ -2425,7 +2625,10 @@ static void direct_link_curve(FileData *fd, Curve *cu)
 {
        Nurb *nu;
        TextBox *tb;
-
+       
+       cu->adt= newdataadr(fd, cu->adt);
+       direct_link_animdata(fd, cu->adt);
+       
        cu->mat= newdataadr(fd, cu->mat);
        test_pointer_array(fd, (void **)&cu->mat);
        cu->str= newdataadr(fd, cu->str);
@@ -2450,10 +2653,13 @@ static void direct_link_curve(FileData *fd, Curve *cu)
                if (cu->wordspace == 0.0) cu->wordspace = 1.0;
        }
 
-       cu->bev.first=cu->bev.last= 0;
-       cu->disp.first=cu->disp.last= 0;
-       cu->path= 0;
-
+       cu->bev.first=cu->bev.last= NULL;
+       cu->disp.first=cu->disp.last= NULL;
+       cu->editnurb= NULL;
+       cu->lastselbp= NULL;
+       cu->path= NULL;
+       cu->editstr= NULL;
+       
        nu= cu->nurb.first;
        while(nu) {
                nu->bezt= newdataadr(fd, nu->bezt);
@@ -2496,6 +2702,9 @@ static void lib_link_texture(FileData *fd, Main *main)
 
 static void direct_link_texture(FileData *fd, Tex *tex)
 {
+       tex->adt= newdataadr(fd, tex->adt);
+       direct_link_animdata(fd, tex->adt);
+       
        tex->plugin= newdataadr(fd, tex->plugin);
        if(tex->plugin) {
                tex->plugin->handle= 0;
@@ -2565,6 +2774,9 @@ static void direct_link_material(FileData *fd, Material *ma)
 {
        int a;
 
+       ma->adt= newdataadr(fd, ma->adt);
+       direct_link_animdata(fd, ma->adt);
+       
        for(a=0; a<MAX_MTEX; a++) {
                ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
        }
@@ -2597,7 +2809,7 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
        part= main->particle.first;
        while(part) {
                if(part->id.flag & LIB_NEEDLINK) {
-                       part->ipo= newlibadr_us(fd, part->id.lib, part->ipo);
+                       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);
@@ -2771,6 +2983,19 @@ static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
        }
 }
 
+static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps)
+{
+       if(mdisps) {
+               int i;
+
+               for(i = 0; i < count; ++i) {
+                       mdisps[i].disps = newdataadr(fd, mdisps[i].disps);
+                       if(!mdisps[i].disps)
+                               mdisps[i].totdisp = 0;
+               }
+       }       
+}
+
 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
 {
        int i = 0;
@@ -2782,6 +3007,8 @@ static void direct_link_customdata(FileData *fd, CustomData *data, int count)
 
                if (CustomData_verify_versions(data, i)) {
                        layer->data = newdataadr(fd, layer->data);
+                       if(layer->type == CD_MDISPS)
+                               direct_link_mdisps(fd, count, layer->data);
                        i++;
                }
        }
@@ -2820,7 +3047,8 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
 
        mesh->bb= NULL;
        mesh->mselect = NULL;
-
+       mesh->edit_mesh= NULL;
+       
        /* Multires data */
        mesh->mr= newdataadr(fd, mesh->mr);
        if(mesh->mr) {
@@ -2833,11 +3061,6 @@ 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= newdataadr(fd, mesh->mr->edge_flags);
-               if(mesh->mr->edge_creases)
-                       mesh->mr->edge_creases= newdataadr(fd, mesh->mr->edge_creases);
-               
                if(!mesh->mr->edge_flags)
                        mesh->mr->edge_flags= MEM_callocN(sizeof(short)*lvl->totedge, "Multires Edge Flags");
                if(!mesh->mr->edge_creases)
@@ -2850,15 +3073,12 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
                        lvl->faces= newdataadr(fd, lvl->faces);
                        lvl->edges= newdataadr(fd, lvl->edges);
                        lvl->colfaces= newdataadr(fd, lvl->colfaces);
-                       lvl->edge_boundary_states= NULL;
-                       lvl->vert_face_map = lvl->vert_edge_map = NULL;
-                       lvl->map_mem= NULL;
                }
        }
        
        if((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
                TFace *tf= mesh->tface;
-               int i;
+               unsigned int i;
 
                for (i=0; i< (mesh->pv ? mesh->pv->totface : mesh->totface); i++, tf++) {
                        SWITCH_INT(tf->col[0]);
@@ -2879,7 +3099,7 @@ static void lib_link_latt(FileData *fd, Main *main)
        while(lt) {
                if(lt->id.flag & LIB_NEEDLINK) {
                        
-                       lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo);
+                       lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo); // XXX depreceated - old animation system
                        lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
                        
                        lt->id.flag -= LIB_NEEDLINK;
@@ -2894,6 +3114,8 @@ static void direct_link_latt(FileData *fd, Lattice *lt)
        
        lt->dvert= newdataadr(fd, lt->dvert);
        direct_link_dverts(fd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
+       
+       lt->editlatt= NULL;
 }
 
 
@@ -2928,11 +3150,15 @@ static void lib_link_object(FileData *fd, Main *main)
        while(ob) {
                if(ob->id.flag & LIB_NEEDLINK) {
                        if (ob->id.properties) IDP_LibLinkProperty(ob->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
+                       if (ob->adt) lib_link_animdata(fd, &ob->id, ob->adt);
+                       
+// XXX depreceated - old animation system <<<                  
+                       ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
+                       ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
+// >>> XXX depreceated - old animation system
 
                        ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
                        ob->track= newlibadr(fd, ob->id.lib, ob->track);
-                       ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
-                       ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
                        ob->poselib= newlibadr_us(fd, ob->id.lib, ob->poselib);
                        ob->dup_group= newlibadr_us(fd, ob->id.lib, ob->dup_group);
                        
@@ -2951,7 +3177,7 @@ static void lib_link_object(FileData *fd, Main *main)
                                }
                        }
                        ob->proxy_group= newlibadr(fd, ob->id.lib, ob->proxy_group);
-
+                       
                        poin= ob->data;
                        ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
                           
@@ -2968,15 +3194,18 @@ static void lib_link_object(FileData *fd, Main *main)
                                }
                        }
                        for(a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
-
+                       
                        ob->id.flag -= LIB_NEEDLINK;
                        /* if id.us==0 a new base will be created later on */
-
+                       
                        /* WARNING! Also check expand_object(), should reflect the stuff below. */
                        lib_link_pose(fd, ob, ob->pose);
                        lib_link_constraints(fd, &ob->id, &ob->constraints);
-                       lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
+                       
+// XXX depreceated - old animation system <<<  
                        lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
+                       lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
+// >>> XXX depreceated - old animation system
 
                        for(paf= ob->effect.first; paf; paf= paf->next) {
                                if(paf->type==EFF_PARTICLE) {
@@ -3101,7 +3330,8 @@ static void lib_link_object(FileData *fd, Main *main)
                ob= ob->id.next;
        }
 
-       if(warn); //XXX error("WARNING IN CONSOLE");
+       if(warn)
+               BKE_report(fd->reports, RPT_WARNING, "Warning in console");
 }
 
 
@@ -3133,6 +3363,7 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
 
        for (md=lb->first; md; md=md->next) {
                md->error = NULL;
+               md->scene = NULL;
                
                /* if modifiers disappear, or for upward compatibility */
                if(NULL==modifierType_getInfo(md->type))
@@ -3236,19 +3467,15 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                                                SWITCH_INT(mmd->dynverts[a])
                        }
                }
+               else if (md->type==eModifierType_Multires) {
+                       MultiresModifierData *mmd = (MultiresModifierData*) md;
+                       
+                       mmd->undo_verts = newdataadr(fd, mmd->undo_verts);
+                       mmd->undo_signal = !!mmd->undo_verts;
+               }
        }
 }
 
-static void direct_link_nlastrips(FileData *fd, ListBase *strips)
-{
-       bActionStrip *strip;
-       
-       link_list(fd, strips);
-       
-       for(strip= strips->first; strip; strip= strip->next)
-               link_list(fd, &strip->modifiers);
-}
-
 static void direct_link_object(FileData *fd, Object *ob)
 {
        PartEff *paf;
@@ -3262,13 +3489,18 @@ static void direct_link_object(FileData *fd, Object *ob)
        ob->flag &= ~OB_FROMGROUP;
 
        ob->disp.first=ob->disp.last= NULL;
-
+       
+       ob->adt= newdataadr(fd, ob->adt);
+       direct_link_animdata(fd, ob->adt);
+       
        ob->pose= newdataadr(fd, ob->pose);
        direct_link_pose(fd, ob->pose);
 
        link_list(fd, &ob->defbase);
+// XXX depreceated - old animation system <<<
        direct_link_nlastrips(fd, &ob->nlastrips);
        link_list(fd, &ob->constraintChannels);
+// >>> XXX depreceated - old animation system 
 
        direct_link_scriptlink(fd, &ob->scriptlink);
 
@@ -3443,10 +3675,8 @@ static void lib_link_scene(FileData *fd, Main *main)
 {
        Scene *sce;
        Base *base, *next;
-       Editing *ed;
        Sequence *seq;
        SceneRenderLayer *srl;
-       int a;
        
        sce= main->scene.first;
        while(sce) {
@@ -3454,20 +3684,17 @@ static void lib_link_scene(FileData *fd, Main *main)
                        /*Link ID Properties -- and copy this comment EXACTLY for easy finding
                        of library blocks that implement this.*/
                        if (sce->id.properties) IDP_LibLinkProperty(sce->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
-
+                       if (sce->adt) lib_link_animdata(fd, &sce->id, sce->adt);
+                       
                        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);
-
-                       /* Sculptdata textures */
-                       for(a=0; a<MAX_MTEX; ++a) {
-                               MTex *mtex= sce->sculptdata.mtex[a];
-                               if(mtex)
-                                       mtex->tex= newlibadr_us(fd, sce->id.lib, mtex->tex);
-                       }
+                       if(sce->toolsettings->sculpt)
+                               sce->toolsettings->sculpt->brush=
+                                       newlibadr_us(fd, sce->id.lib, sce->toolsettings->sculpt->brush);
 
                        for(base= sce->base.first; base; base= next) {
                                next= base->next;
@@ -3485,24 +3712,26 @@ static void lib_link_scene(FileData *fd, Main *main)
                                        MEM_freeN(base);
                                }
                        }
-
-                       ed= sce->ed;
-                       if(ed) {
-                               WHILE_SEQ(&ed->seqbase) { //XXX todo replace WHILE_SEQ
-                                       if(seq->ipo) seq->ipo= newlibadr_us(fd, sce->id.lib, seq->ipo);
-                                       if(seq->scene) seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
-                                       if(seq->sound) {
-                                               seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
-                                               if (seq->sound) {
-                                                       seq->sound->id.us++;
-                                                       seq->sound->flags |= SOUND_FLAGS_SEQUENCE;
-                                               }
+                       
+                       if (sce->ed) {
+                               Editing *ed= sce->ed;
+                               ed->act_seq= NULL; //   ed->act_seq=  newlibadr(fd, ed->act_seq); // FIXME
+                       }
+
+                       SEQ_BEGIN(sce->ed, seq) {
+                               if(seq->ipo) seq->ipo= newlibadr_us(fd, sce->id.lib, seq->ipo);
+                               if(seq->scene) seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
+                               if(seq->sound) {
+                                       seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
+                                       if (seq->sound) {
+                                               seq->sound->id.us++;
+                                               seq->sound->flags |= SOUND_FLAGS_SEQUENCE;
                                        }
-                                       seq->anim= 0;
-                                       seq->hdaudio = 0;
                                }
-                               END_SEQ
+                               seq->anim= 0;
+                               seq->hdaudio = 0;
                        }
+                       SEQ_END
                        
                        lib_link_scriptlink(fd, &sce->id, &sce->scriptlink);
                        
@@ -3526,11 +3755,10 @@ static void link_recurs_seq(FileData *fd, ListBase *lb)
        Sequence *seq;
 
        link_list(fd, lb);
-       seq= lb->first;
-       while(seq) {
-               if(seq->seqbase.first) link_recurs_seq(fd, &seq->seqbase);
-               seq= seq->next;
-       }
+
+       for(seq=lb->first; seq; seq=seq->next)
+               if(seq->seqbase.first)
+                       link_recurs_seq(fd, &seq->seqbase);
 }
 
 static void direct_link_scene(FileData *fd, Scene *sce)
@@ -3538,7 +3766,6 @@ static void direct_link_scene(FileData *fd, Scene *sce)
        Editing *ed;
        Sequence *seq;
        MetaStack *ms;
-       int a;
 
        sce->theDag = NULL;
        sce->dagisvalid = 0;
@@ -3546,33 +3773,33 @@ static void direct_link_scene(FileData *fd, Scene *sce)
        sce->id.us= 1;
 
        link_list(fd, &(sce->base));
-
+       
+       sce->adt= newdataadr(fd, sce->adt);
+       direct_link_animdata(fd, sce->adt);
+       
        sce->basact= newdataadr(fd, sce->basact);
 
        sce->radio= newdataadr(fd, sce->radio);
        
        sce->toolsettings= newdataadr(fd, sce->toolsettings);
-
-       sce->sculptdata.session= NULL;
-       /* SculptData textures */
-       for(a=0; a<MAX_MTEX; ++a)
-               sce->sculptdata.mtex[a]= newdataadr(fd,sce->sculptdata.mtex[a]);
-       /* Sculpt intensity curve */
-       sce->sculptdata.cumap= newdataadr(fd, sce->sculptdata.cumap);
-       if(sce->sculptdata.cumap)
-               direct_link_curvemapping(fd, sce->sculptdata.cumap);
-       else
-               ; //XXX sculpt_reset_curve(&sce->sculptdata);
+       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);
+               if(sce->toolsettings->sculpt)
+                       sce->toolsettings->sculpt->session= MEM_callocN(sizeof(SculptSession), "reload sculpt session");
+       }
 
        if(sce->ed) {
                ListBase *old_seqbasep= &((Editing *)sce->ed)->seqbase;
                
                ed= sce->ed= newdataadr(fd, sce->ed);
+               ed->act_seq= NULL; //           ed->act_seq=  newdataadr(fd, ed->act_seq); // FIXME
 
                /* recursive link sequences, lb will be correctly initialized */
                link_recurs_seq(fd, &ed->seqbase);
 
-               WHILE_SEQ(&ed->seqbase) { //XXX todo replace WHILE_SEQ
+               SEQ_BEGIN(ed, seq) {
                        seq->seq1= newdataadr(fd, seq->seq1);
                        seq->seq2= newdataadr(fd, seq->seq2);
                        seq->seq3= newdataadr(fd, seq->seq3);
@@ -3630,7 +3857,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                                }
                        }
                }
-               END_SEQ
+               SEQ_END
                
                /* link metastack, slight abuse of structs here, have to restore pointer to internal part in struct */
                {
@@ -3712,32 +3939,29 @@ static void direct_link_scene(FileData *fd, Scene *sce)
 
    This is needed, to make Ipo-Pinning work for Sequence-Ipos...
 */
+// XXX old animation system - depreceated stuff...
 static Sequence * find_sequence_from_ipo_helper(Main * main, Ipo * ipo)
 {
-       Editing *ed;
-       Sequence *seq = NULL;
-
-       Scene * sce= main->scene.first;
-       while(sce) {
-               if(sce->ed) {
-                       int found = 0;
-
-                       ed= sce->ed;
+       Sequence *seq;
+       Scene *sce;
+       
+       for(sce=main->scene.first; sce; sce=sce->id.next) {
+               int found = 0;
 
-                       WHILE_SEQ(&ed->seqbase) { //XXX todo replace WHILE_SEQ
-                               if (seq->ipo == ipo) {
-                                       found = 1;
-                                       break;
-                               }
-                       } 
-                       END_SEQ
-                       if (found) {
+               SEQ_BEGIN(sce->ed, seq) {
+                       if (seq->ipo == ipo) {
+                               found = 1;
                                break;
                        }
-                       seq = NULL;
+               } 
+               SEQ_END
+
+               if (found) {
+                       break;
                }
-               sce= sce->id.next;
+               seq = NULL;
        }
+
        if (seq)
         return seq;
        else
@@ -3778,14 +4002,18 @@ static void direct_link_windowmanager(FileData *fd, wmWindowManager *wm)
                win->eventstate= NULL;
                win->curswin= 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->drawtex= 0;
+               win->drawmethod= 0;
        }
        
        wm->operators.first= wm->operators.last= NULL;
        wm->keymaps.first= wm->keymaps.last= NULL;
-       
+       wm->paintcursors.first= wm->paintcursors.last= NULL;
        wm->queue.first= wm->queue.last= NULL;
        wm->reports.first= wm->reports.last= NULL;
        
@@ -3805,7 +4033,7 @@ static void lib_link_windowmanager(FileData *fd, Main *main)
        }
 }
 
-/* ************ READ SCREEN ***************** */
+/* ****************** READ GREASE PENCIL ***************** */
 
 /* relinks grease-pencil data for 3d-view(s) - used for direct_link */
 static void link_gpencil(FileData *fd, bGPdata *gpd)
@@ -3833,6 +4061,29 @@ static void link_gpencil(FileData *fd, bGPdata *gpd)
        }
 }
 
+/* ****************** READ SCREEN ***************** */
+
+static void butspace_version_132(SpaceButs *buts)
+{
+       buts->v2d.tot.xmin= 0.0f;
+       buts->v2d.tot.ymin= 0.0f;
+       buts->v2d.tot.xmax= 1279.0f;
+       buts->v2d.tot.ymax= 228.0f;
+
+       buts->v2d.min[0]= 256.0f;
+       buts->v2d.min[1]= 42.0f;
+
+       buts->v2d.max[0]= 2048.0f;
+       buts->v2d.max[1]= 450.0f;
+
+       buts->v2d.minzoom= 0.5f;
+       buts->v2d.maxzoom= 1.21f;
+
+       buts->v2d.scroll= 0;
+       buts->v2d.keepzoom= 1;
+       buts->v2d.keeptot= 1;
+}
+
 /* note: file read without screens option G_FILE_NO_UI; 
    check lib pointers in call below */
 static void lib_link_screen(FileData *fd, Main *main)
@@ -3867,8 +4118,6 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                if(v3d->localvd) {
                                                        v3d->localvd->camera= newlibadr(fd, sc->id.lib, v3d->localvd->camera);
                                                }
-                                               v3d->depths= NULL;
-                                               v3d->ri= NULL;
                                        }
                                        else if(sl->spacetype==SPACE_IPO) {
                                                SpaceIpo *sipo= (SpaceIpo *)sl;
@@ -3884,16 +4133,17 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                SpaceButs *sbuts= (SpaceButs *)sl;
                                                sbuts->lockpoin= NULL;
                                                sbuts->ri= NULL;
-// XXX                                         if(main->versionfile<132) set_rects_butspace(sbuts);
+                                               if(main->versionfile<132)
+                                                       butspace_version_132(sbuts);
                                        }
                                        else if(sl->spacetype==SPACE_FILE) {
                                                SpaceFile *sfile= (SpaceFile *)sl;
-
-                                               sfile->filelist= NULL;
-                                               sfile->libfiledata= NULL;
-                                               sfile->returnfunc= NULL;
+                                               sfile->files= NULL;
+                                               sfile->params= NULL;
+                                               sfile->op= NULL;
+                                               /* sfile->returnfunc= NULL; 
                                                sfile->menup= NULL;
-                                               sfile->pupmenu= NULL;
+                                               sfile->pupmenu= NULL; */ /* XXX removed */
                                        }
                                        else if(sl->spacetype==SPACE_IMASEL) {
                                                SpaceImaSel *simasel= (SpaceImaSel *)sl;
@@ -3907,6 +4157,7 @@ static void lib_link_screen(FileData *fd, Main *main)
                                        else if(sl->spacetype==SPACE_ACTION) {
                                                SpaceAction *saction= (SpaceAction *)sl;
                                                saction->action = newlibadr(fd, sc->id.lib, saction->action);
+                                               saction->ads.source= newlibadr(fd, sc->id.lib, saction->ads.source);
                                        }
                                        else if(sl->spacetype==SPACE_IMAGE) {
                                                SpaceImage *sima= (SpaceImage *)sl;
@@ -3997,11 +4248,26 @@ static void *restore_pointer_by_name(Main *mainp, ID *id, int user)
 /* called from kernel/blender.c */
 /* used to link a file (without UI) to the current UI */
 /* note that it assumes the old pointers in UI are still valid, so old Main is not freed */
-void lib_link_screen_restore(Main *newmain, Scene *curscene)
+void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
 {
+       wmWindow *win;
+       wmWindowManager *wm;
        bScreen *sc;
        ScrArea *sa;
 
+       /* first windowmanager */
+       for(wm= newmain->wm.first; wm; wm= wm->id.next) {
+               for(win= wm->windows.first; win; win= win->next) {
+                       win->screen= restore_pointer_by_name(newmain, (ID *)win->screen, 1);
+                       
+                       if(win->screen==NULL)
+                               win->screen= curscreen;
+
+                       win->screen->winid= win->winid;
+               }
+       }
+       
+       
        for(sc= newmain->screen.first; sc; sc= sc->id.next) {
                
                sc->scene= restore_pointer_by_name(newmain, (ID *)sc->scene, 1);
@@ -4074,10 +4340,16 @@ void lib_link_screen_restore(Main *newmain, Scene *curscene)
                                        //XXX if (sbuts->ri) sbuts->ri->curtile = 0;
                                }
                                else if(sl->spacetype==SPACE_FILE) {
+                                       
                                        SpaceFile *sfile= (SpaceFile *)sl;
+                                       sfile->files= 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;
@@ -4088,6 +4360,7 @@ void lib_link_screen_restore(Main *newmain, Scene *curscene)
                                else if(sl->spacetype==SPACE_ACTION) {
                                        SpaceAction *saction= (SpaceAction *)sl;
                                        saction->action = restore_pointer_by_name(newmain, (ID *)saction->action, 1);
+                                       saction->ads.source= restore_pointer_by_name(newmain, (ID *)saction->ads.source, 1);
                                }
                                else if(sl->spacetype==SPACE_IMAGE) {
                                        SpaceImage *sima= (SpaceImage *)sl;
@@ -4151,6 +4424,64 @@ void lib_link_screen_restore(Main *newmain, Scene *curscene)
        }
 }
 
+static void direct_link_region(FileData *fd, ARegion *ar, int spacetype)
+{
+       Panel *pa;
+
+       link_list(fd, &(ar->panels));
+
+       for(pa= ar->panels.first; pa; pa=pa->next) {
+               pa->paneltab= newdataadr(fd, pa->paneltab);
+               pa->active= 0;
+               pa->sortcounter= 0;
+               pa->activedata= NULL;
+       }
+       
+       ar->regiondata= newdataadr(fd, ar->regiondata);
+       if(ar->regiondata) {
+               if(spacetype==SPACE_VIEW3D) {
+                       RegionView3D *rv3d= ar->regiondata;
+                       
+                       rv3d->localvd= newdataadr(fd, rv3d->localvd);
+                       rv3d->clipbb= newdataadr(fd, rv3d->clipbb);
+                       
+                       rv3d->depths= NULL;
+                       rv3d->retopo_view_data= NULL;
+                       rv3d->ri= NULL;
+                       rv3d->sms= NULL;
+                       rv3d->smooth_timer= NULL;
+               }
+       }
+       
+       ar->handlers.first= ar->handlers.last= NULL;
+       ar->uiblocks.first= ar->uiblocks.last= NULL;
+       ar->headerstr= NULL;
+       ar->swinid= 0;
+       ar->type= NULL;
+       ar->swap= 0;
+}
+
+/* for the saved 2.50 files without regiondata */
+/* and as patch for 2.48 and older */
+static void view3d_split_250(View3D *v3d, ListBase *regions)
+{
+       ARegion *ar;
+       
+       for(ar= regions->first; ar; ar= ar->next) {
+               if(ar->regiontype==RGN_TYPE_WINDOW && ar->regiondata==NULL) {
+                       RegionView3D *rv3d;
+                       
+                       rv3d= ar->regiondata= MEM_callocN(sizeof(RegionView3D), "region v3d");
+                       rv3d->persp= v3d->persp;
+                       rv3d->view= v3d->view;
+                       rv3d->dist= v3d->dist;
+                       VECCOPY(rv3d->ofs, v3d->ofs);
+                       QUATCOPY(rv3d->viewquat, v3d->viewquat);
+                       Mat4One(rv3d->twmat);
+               }
+       }
+}
+
 static void direct_link_screen(FileData *fd, bScreen *sc)
 {
        ScrArea *sa;
@@ -4163,6 +4494,7 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
        link_list(fd, &(sc->edgebase));
        link_list(fd, &(sc->areabase));
        sc->regionbase.first= sc->regionbase.last= NULL;
+       sc->context= NULL;
 
        sc->mainwin= sc->subwinactive= 0;       /* indices */
        
@@ -4193,29 +4525,35 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
 
        /* areas */
        for(sa= sc->areabase.first; sa; sa= sa->next) {
-               Panel *pa;
                SpaceLink *sl;
                ARegion *ar;
 
                link_list(fd, &(sa->spacedata));
-               link_list(fd, &(sa->panels));
                link_list(fd, &(sa->regionbase));
 
                sa->handlers.first= sa->handlers.last= NULL;
-               sa->uiblocks.first= sa->uiblocks.last= NULL;
                sa->type= NULL; /* spacetype callbacks */
                
-               /* accident can happen when read/save new file with older version */
-               if(sa->spacedata.first==NULL && sa->spacetype>SPACE_NLA)
-                       sa->spacetype= SPACE_EMPTY;
+               for(ar= sa->regionbase.first; ar; ar= ar->next)
+                       direct_link_region(fd, ar, sa->spacetype);
                
-               for(pa= sa->panels.first; pa; pa=pa->next) {
-                       pa->paneltab= newdataadr(fd, pa->paneltab);
-                       pa->active= 0;
-                       pa->sortcounter= 0;
-               }
+               /* accident can happen when read/save new file with older version */
+               /* 2.50: we now always add spacedata for info */
+               if(sa->spacedata.first==NULL) {
+                       SpaceInfo *sinfo= MEM_callocN(sizeof(SpaceInfo), "spaceinfo");
+                       sa->spacetype= SPACE_INFO;
+                       BLI_addtail(&sa->spacedata, sinfo);
+               }
+               /* add local view3d too */
+               else if(sa->spacetype==SPACE_VIEW3D)
+                       view3d_split_250(sa->spacedata.first, &sa->regionbase);
                
                for (sl= sa->spacedata.first; sl; sl= sl->next) {
+                       link_list(fd, &(sl->regionbase));
+
+                       for(ar= sl->regionbase.first; ar; ar= ar->next)
+                               direct_link_region(fd, ar, sl->spacetype);
+
                        if (sl->spacetype==SPACE_VIEW3D) {
                                View3D *v3d= (View3D*) sl;
                                v3d->bgpic= newdataadr(fd, v3d->bgpic);
@@ -4227,9 +4565,9 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                }
                                v3d->localvd= newdataadr(fd, v3d->localvd);
                                v3d->afterdraw.first= v3d->afterdraw.last= NULL;
-                               v3d->clipbb= newdataadr(fd, v3d->clipbb);
-                               v3d->retopo_view_data= NULL;
                                v3d->properties_storage= NULL;
+                               
+                               view3d_split_250(v3d, &sl->regionbase);
                        }
                        else if (sl->spacetype==SPACE_OOPS) {
                                SpaceOops *soops= (SpaceOops*) sl;
@@ -4279,14 +4617,6 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                        }
                }
                
-               for(ar= sa->regionbase.first; ar; ar= ar->next) {
-                       ar->handlers.first= ar->handlers.last= NULL;
-                       ar->uiblocks.first= ar->uiblocks.last= NULL;
-                       ar->regiondata= NULL;
-                       ar->swinid= 0;
-                       ar->type= NULL;
-               }
-               
                sa->actionzones.first= sa->actionzones.last= NULL;
 
                sa->v1= newdataadr(fd, sa->v1);
@@ -4317,7 +4647,8 @@ static void direct_link_library(FileData *fd, Library *lib, Main *main)
                                BLI_remlink(&main->library, lib);
                                MEM_freeN(lib);
                                
-                               //XXX error("Library had multiple instances, save and reload!");
+                               BKE_report(fd->reports, RPT_WARNING, "Library had multiple instances, save and reload!");
+
                                return;
                        }
                }
@@ -4382,7 +4713,7 @@ static void lib_link_sound(FileData *fd, Main *main)
        while(sound) {
                if(sound->id.flag & LIB_NEEDLINK) {
                        sound->id.flag -= LIB_NEEDLINK;
-                       sound->ipo= newlibadr_us(fd, sound->id.lib, sound->ipo);
+                       sound->ipo= newlibadr_us(fd, sound->id.lib, sound->ipo); // XXX depreceated - old animation system
                        sound->stream = 0;
                }
                sound= sound->id.next;
@@ -4510,9 +4841,20 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
        allocname= dataname(GS(id->name));
        
                /* read all data */
+       
        while(bhead && bhead->code==DATA) {
-               void *data= read_struct(fd, bhead, allocname);
-
+               void *data;
+#if 0          
+               /* XXX DUMB DEBUGGING OPTION TO GIVE NAMES for guarded malloc errors */         
+               short *sp= fd->filesdna->structs[bhead->SDNAnr];
+               char *allocname = fd->filesdna->types[ sp[0] ];
+               char *tmp= malloc(100);
+               
+               strcpy(tmp, allocname);
+               data= read_struct(fd, bhead, tmp);
+#endif
+               data= read_struct(fd, bhead, allocname);
+               
                if (data) {
                        oldnewmap_insert(fd->datamap, bhead->old, data, 0);
                }
@@ -4681,19 +5023,19 @@ static void vcol_to_fcol(Mesh *me)
 static int map_223_keybd_code_to_224_keybd_code(int code)
 {
        switch (code) {
-//XXX  case 312:       return F12KEY;
-//XXX          case 159:       return PADSLASHKEY;
-//XXX          case 161:       return PAD0;
-//XXX          case 154:       return PAD1;
-//XXX          case 150:       return PAD2;
-//XXX          case 155:       return PAD3;
-//XXX          case 151:       return PAD4;
-//XXX          case 156:       return PAD5;
-//XXX          case 152:       return PAD6;
-//XXX          case 157:       return PAD7;
-//XXX          case 153:       return PAD8;
-//XXX          case 158:       return PAD9;
-       default: return code;
+               case 312:       return 311; /* F12KEY */
+               case 159:       return 161; /* PADSLASHKEY */
+               case 161:       return 150; /* PAD0 */
+               case 154:       return 151; /* PAD1 */
+               case 150:       return 152; /* PAD2 */
+               case 155:       return 153; /* PAD3 */
+               case 151:       return 154; /* PAD4 */
+               case 156:       return 155; /* PAD5 */
+               case 152:       return 156; /* PAD6 */
+               case 157:       return 157; /* PAD7 */
+               case 153:       return 158; /* PAD8 */
+               case 158:       return 159; /* PAD9 */
+               default: return code;
        }
 }
 
@@ -5007,7 +5349,7 @@ static void alphasort_version_246(FileData *fd, Library *lib, Mesh *me)
        /* if we do, set alpha sort if the game engine did it before */
        for(a=0, mf=me->mface; a<me->totface; a++, mf++) {
                if(mf->mat_nr < me->totcol) {
-                       ma= newlibadr(fd, lib, me->mat[mf->mat_nr]);
+                       ma= newlibadr(fd, lib, me->mat[(int)mf->mat_nr]);
                        texalpha = 0;
 
                        /* we can't read from this if it comes from a library,
@@ -5052,12 +5394,11 @@ static void area_add_header_region(ScrArea *sa, ListBase *lb)
        
        /* initialise view2d data for header region, to allow panning */
        /* is copy from ui_view2d.c */
-       ar->v2d.keepaspect= 1;
-       ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM);
+       ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
        ar->v2d.keepofs = V2D_LOCKOFS_Y;
-       ar->v2d.keeptot = 2; // this keeps the view in place when region size changes...
-       ar->v2d.align = V2D_ALIGN_NO_NEG_X;
-       
+       ar->v2d.keeptot = V2D_KEEPTOT_STRICT; 
+       ar->v2d.align = V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y;
+       ar->v2d.flag = (V2D_PIXELOFS_X|V2D_PIXELOFS_Y);
 }
 
 /* 2.50 patch */
@@ -5072,15 +5413,48 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                ar= MEM_callocN(sizeof(ARegion), "area region from do_versions");
                                BLI_addtail(lb, ar);
                                ar->regiontype= RGN_TYPE_CHANNELS;
-                               ar->alignment= RGN_ALIGN_RIGHT;
-                               
+                               ar->alignment= RGN_ALIGN_LEFT; 
+                               ar->v2d.scroll= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
                                break;
                        case SPACE_ACTION:
+                               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_BOTTOM;
+                               ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
                                break;
                        case SPACE_NLA:
+                               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_BOTTOM;
+                               ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
+                               break;
+                       case SPACE_NODE:
+                               ar= MEM_callocN(sizeof(ARegion), "nodetree area for node");
+                               BLI_addtail(lb, ar);
+                               ar->regiontype= RGN_TYPE_CHANNELS;
+                               ar->alignment= RGN_ALIGN_LEFT;
+                               ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
+                               ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
+                       case SPACE_FILE:
+                               /* channel (bookmarks/directories) 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;
                }
        }
+
        /* main region */
        ar= MEM_callocN(sizeof(ARegion), "area region from do_versions");
        
@@ -5091,7 +5465,12 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
        
        if(sl) {
                /* if active spacetype has view2d data, copy that over to main region */
+               /* and we split view3d */
                switch(sl->spacetype) {
+                       case SPACE_VIEW3D:
+                               view3d_split_250((View3D *)sl, lb);
+                               break;          
+                                               
                        case SPACE_OOPS:
                        {
                                SpaceOops *soops= (SpaceOops *)sl;
@@ -5099,11 +5478,14 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                memcpy(&ar->v2d, &soops->v2d, sizeof(View2D));
                                
                                ar->v2d.scroll &= ~V2D_SCROLL_LEFT;
-                               ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_HORIZONTAL_O);
+                               ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM_O);
                                ar->v2d.align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
-                               ar->v2d.keepzoom |= (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y);
-                               ar->v2d.keeptot = 2;
-                               ar->v2d.keepaspect= 1;
+                               ar->v2d.keepzoom |= (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPASPECT);
+                               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:
@@ -5111,11 +5493,12 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                SpaceTime *stime= (SpaceTime *)sl;
                                memcpy(&ar->v2d, &stime->v2d, sizeof(View2D));
                                
-                               ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_BOTTOM);
+                               ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
                                ar->v2d.align |= V2D_ALIGN_NO_NEG_Y;
                                ar->v2d.keepofs |= V2D_LOCKOFS_Y;
                                ar->v2d.keepzoom |= V2D_LOCKZOOM_Y;
-                               ar->v2d.min[1]= ar->v2d.max[1]= 500.0;
+                               ar->v2d.tot.ymin= ar->v2d.cur.ymin= -10.0;
+                               ar->v2d.min[1]= ar->v2d.max[1]= 20.0;
                        }
                                break;
                        case SPACE_IPO:
@@ -5123,11 +5506,98 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                SpaceIpo *sipo= (SpaceIpo *)sl;
                                memcpy(&ar->v2d, &sipo->v2d, sizeof(View2D));
                                
-                               ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_BOTTOM);
-                               ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_LEFT);
+                               ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
+                               ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
+                               //ar->v2d.flag |= V2D_IS_INITIALISED;
+                               break;
+                       }
+                       case SPACE_SOUND:
+                       {
+                               SpaceSound *ssound= (SpaceSound *)sl;
+                               memcpy(&ar->v2d, &ssound->v2d, sizeof(View2D));
+                               
+                               ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
+                               ar->v2d.scroll |= (V2D_SCROLL_LEFT);
+                               //ar->v2d.flag |= V2D_IS_INITIALISED;
+                               break;
+                       }
+                       case SPACE_NLA:
+                       {
+                               SpaceNla *snla= (SpaceNla *)sl;
+                               memcpy(&ar->v2d, &snla->v2d, sizeof(View2D));
+                               
+                               ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
+                               ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
+                               ar->v2d.align = V2D_ALIGN_NO_POS_Y;
+                               ar->v2d.flag |= V2D_VIEWSYNC_AREA_VERTICAL;
+                               break;
+                       }
+                       case SPACE_ACTION:
+                       {
+                               /* 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.ymax= 0.0f;
+                               
+                               ar->v2d.cur= ar->v2d.tot;
+                               
+                               ar->v2d.min[0]= 0.0f;
+                               ar->v2d.min[1]= 0.0f;
+                               
+                               ar->v2d.max[0]= MAXFRAMEF;
+                               ar->v2d.max[1]= 10000.0f;
+                               
+                               ar->v2d.minzoom= 0.01f;
+                               ar->v2d.maxzoom= 50;
+                               ar->v2d.scroll = (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
+                               ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
+                               ar->v2d.keepzoom= V2D_LOCKZOOM_Y;
+                               ar->v2d.align= V2D_ALIGN_NO_POS_Y;
+                               ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
+                               break;
+                       }
+                       case SPACE_SEQ:
+                       {
+                               SpaceSeq *sseq= (SpaceSeq *)sl;
+                               memcpy(&ar->v2d, &sseq->v2d, sizeof(View2D));
+                               
+                               ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
+                               ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
+                               ar->v2d.align= V2D_ALIGN_NO_NEG_Y;
+                               ar->v2d.flag |= V2D_IS_INITIALISED;
+                               break;
+                       }
+                       case SPACE_NODE:
+                       {
+                               SpaceNode *snode= (SpaceNode *)sl;
+                               memcpy(&ar->v2d, &snode->v2d, sizeof(View2D));
                                
+                               ar->v2d.scroll= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
+                               ar->v2d.keepzoom= V2D_KEEPZOOM|V2D_KEEPASPECT;
+                               break;
+                       }
+                       case SPACE_BUTS:
+                       {
+                               SpaceButs *sbuts= (SpaceButs *)sl;
+                               memcpy(&ar->v2d, &sbuts->v2d, sizeof(View2D));
+                               ar->v2d.keepzoom |= V2D_KEEPASPECT;
+                               break;
                        }
-                       //case SPACE_XXX: // FIXME... add other ones
+                       case SPACE_FILE:
+                       {
+                               // SpaceFile *sfile= (SpaceFile *)sl;
+                               ar->v2d.tot.xmin = ar->v2d.tot.ymin = 0;
+                               ar->v2d.tot.xmax = ar->winx;
+                               ar->v2d.tot.ymax = ar->winy;
+                               ar->v2d.cur = ar->v2d.tot;
+                               ar->regiontype= RGN_TYPE_WINDOW;
+                               ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM_O);
+                               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_XXX: // FIXME... add other ones
                                //      memcpy(&ar->v2d, &((SpaceXxx *)sl)->v2d, sizeof(View2D));
                                //      break;
                }
@@ -5137,7 +5607,6 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
 static void do_versions_windowmanager_2_50(bScreen *screen)
 {
        ScrArea *sa;
-       ARegion *ar;
        SpaceLink *sl;
        
        /* add regions */
@@ -5149,6 +5618,12 @@ static void do_versions_windowmanager_2_50(bScreen *screen)
                
                area_add_window_regions(sa, sa->spacedata.first, &sa->regionbase);
                
+               /* space imageselect is depricated */
+               for(sl= sa->spacedata.first; sl; sl= sl->next) {
+                       if(sl->spacetype==SPACE_IMASEL)
+                               sl->spacetype= SPACE_INFO;      /* spacedata then matches */
+               }               
+               
                /* pushed back spaces also need regions! */
                if(sa->spacedata.first) {
                        sl= sa->spacedata.first;
@@ -5488,7 +5963,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                while(sl) {
                                        if(sl->spacetype==SPACE_BUTS) {
                                                SpaceButs *sbuts= (SpaceButs*) sl;
-                                               //XXX sbuts->scaflag= BUTS_SENS_LINK|BUTS_SENS_ACT|BUTS_CONT_ACT|BUTS_ACT_ACT|BUTS_ACT_LINK;
+                                               sbuts->scaflag= BUTS_SENS_LINK|BUTS_SENS_ACT|BUTS_CONT_ACT|BUTS_ACT_ACT|BUTS_ACT_LINK;
                                        }
                                        sl= sl->next;
                                }
@@ -5767,8 +6242,8 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                while (ob) {
                        prop= ob->prop.first;
                        while(prop) {
-                               if (prop->type == PROP_TIME) {
-                                       // convert old PROP_TIME values from int to float
+                               if (prop->type == GPROP_TIME) {
+                                       // convert old GPROP_TIME values from int to float
                                        *((float *)&prop->data) = (float) prop->data;
                                }
 
@@ -6136,52 +6611,52 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                        if (sl->spacetype==SPACE_BUTS) {
                                                SpaceButs *sbuts= (SpaceButs *) sl;
 
-//XXX                                          sbuts->v2d.maxzoom= 1.2f;
-//XXX                                          sbuts->align= 1;        /* horizontal default */
-//XXX                                          
-//XXX                                          if(sbuts->mainb==BUTS_LAMP) {
-//XXX                                                  sbuts->mainb= CONTEXT_SHADING;
-//XXX                                                  sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_LAMP;
-//XXX                                          }
-//XXX                                          else if(sbuts->mainb==BUTS_MAT) {
-//XXX                                                  sbuts->mainb= CONTEXT_SHADING;
-//XXX                                                  sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_MAT;
-//XXX                                          }
-//XXX                                          else if(sbuts->mainb==BUTS_TEX) {
-//XXX                                                  sbuts->mainb= CONTEXT_SHADING;
-//XXX                                                  sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_TEX;
-//XXX                                          }
-//XXX                                          else if(sbuts->mainb==BUTS_ANIM) {
-//XXX                                                  sbuts->mainb= CONTEXT_OBJECT;
-//XXX                                          }
-//XXX                                          else if(sbuts->mainb==BUTS_WORLD) {
-//XXX                                                  sbuts->mainb= CONTEXT_SCENE;
-//XXX                                                  sbuts->tab[CONTEXT_SCENE]= TAB_SCENE_WORLD;
-//XXX                                          }
-//XXX                                          else if(sbuts->mainb==BUTS_RENDER) {
-//XXX                                                  sbuts->mainb= CONTEXT_SCENE;
-//XXX                                                  sbuts->tab[CONTEXT_SCENE]= TAB_SCENE_RENDER;
-//XXX                                          }
-//XXX                                          else if(sbuts->mainb==BUTS_GAME) {
-//XXX                                                  sbuts->mainb= CONTEXT_LOGIC;
-//XXX                                          }
-//XXX                                          else if(sbuts->mainb==BUTS_FPAINT) {
-//XXX                                                  sbuts->mainb= CONTEXT_EDITING;
-//XXX                                          }
-//XXX                                          else if(sbuts->mainb==BUTS_RADIO) {
-//XXX                                                  sbuts->mainb= CONTEXT_SHADING;
-//XXX                                                  sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_RAD;
-//XXX                                          }
-//XXX                                          else if(sbuts->mainb==BUTS_CONSTRAINT) {
-//XXX                                                  sbuts->mainb= CONTEXT_OBJECT;
-//XXX                                          }
-//XXX                                          else if(sbuts->mainb==BUTS_SCRIPT) {
-//XXX                                                  sbuts->mainb= CONTEXT_OBJECT;
-//XXX                                          }
-//XXX                                          else if(sbuts->mainb==BUTS_EDIT) {
-//XXX                                                  sbuts->mainb= CONTEXT_EDITING;
-//XXX                                          }
-//XXX                                          else sbuts->mainb= CONTEXT_SCENE;
+                                               sbuts->v2d.maxzoom= 1.2f;
+                                               sbuts->align= 1;        /* horizontal default */
+                                       
+                                               if(sbuts->mainb==BUTS_LAMP) {
+                                                       sbuts->mainb= CONTEXT_SHADING;
+                                                       sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_LAMP;
+                                               }
+                                               else if(sbuts->mainb==BUTS_MAT) {
+                                                       sbuts->mainb= CONTEXT_SHADING;
+                                                       sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_MAT;
+                                               }
+                                               else if(sbuts->mainb==BUTS_TEX) {
+                                                       sbuts->mainb= CONTEXT_SHADING;
+                                                       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;
+                                               }
+                                               else if(sbuts->mainb==BUTS_RENDER) {
+                                                       sbuts->mainb= CONTEXT_SCENE;
+                                                       sbuts->tab[CONTEXT_SCENE]= TAB_SCENE_RENDER;
+                                               }
+                                               else if(sbuts->mainb==BUTS_GAME) {
+                                                       sbuts->mainb= CONTEXT_LOGIC;
+                                               }
+                                               else if(sbuts->mainb==BUTS_FPAINT) {
+                                                       sbuts->mainb= CONTEXT_EDITING;
+                                               }
+                                               else if(sbuts->mainb==BUTS_RADIO) {
+                                                       sbuts->mainb= CONTEXT_SHADING;
+                                                       sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_RAD;
+                                               }
+                                               else if(sbuts->mainb==BUTS_CONSTRAINT) {
+                                                       sbuts->mainb= CONTEXT_OBJECT;
+                                               }
+                                               else if(sbuts->mainb==BUTS_SCRIPT) {
+                                                       sbuts->mainb= CONTEXT_OBJECT;
+                                               }
+                                               else if(sbuts->mainb==BUTS_EDIT) {
+                                                       sbuts->mainb= CONTEXT_EDITING;
+                                               }
+                                               else sbuts->mainb= CONTEXT_SCENE;
                                        }
                                }
                        }
@@ -6451,10 +6926,11 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                while(sce) {
                        ed= sce->ed;
                        if(ed) {
-                               WHILE_SEQ(&ed->seqbase) { //XXX todo replace WHILE_SEQ
-                                       if(seq->type==SEQ_IMAGE || seq->type==SEQ_MOVIE) seq->flag |= SEQ_MAKE_PREMUL;
+                               SEQ_BEGIN(sce->ed, seq) {
+                                       if(seq->type==SEQ_IMAGE || seq->type==SEQ_MOVIE)
+                                               seq->flag |= SEQ_MAKE_PREMUL;
                                }
-                               END_SEQ
+                               SEQ_END
                        }
                        
                        sce= sce->id.next;
@@ -7178,7 +7654,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                Material *ma;
                
                for(; cam; cam= cam->id.next) {
-                       cam->angle= 360.0f * atan(16.0f/cam->lens) / M_PI;
+                       cam->angle= 360.0f * (float)atan(16.0f/cam->lens) / (float)M_PI;
                }
 
                for(ma=main->mat.first; ma; ma= ma->id.next) {
@@ -7303,7 +7779,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                else
                                        la->ray_samp_method = LA_SAMP_HALTON;
                                
-                               la->adapt_thresh = 0.001;
+                               la->adapt_thresh = 0.001f;
                        }
                }
        }
@@ -7410,14 +7886,14 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                        for (sl= sa->spacedata.first; sl; sl= sl->next) {
                                                if(sl->spacetype==SPACE_IMASEL) {
                                                        SpaceImaSel *simasel= (SpaceImaSel*) sl;
-                                                       simasel->blockscale= 0.7;
+                                                       simasel->blockscale= 0.7f;
                                                        /* view 2D */
-                                                       simasel->v2d.tot.xmin=  -10.0;
-                                                       simasel->v2d.tot.ymin=  -10.0;
+                                                       simasel->v2d.tot.xmin=  -10.0f;
+                                                       simasel->v2d.tot.ymin=  -10.0f;
                                                        simasel->v2d.tot.xmax= (float)sa->winx + 10.0f;
                                                        simasel->v2d.tot.ymax= (float)sa->winy + 10.0f;                                         
-                                                       simasel->v2d.cur.xmin=  0.0;
-                                                       simasel->v2d.cur.ymin=  0.0;
+                                                       simasel->v2d.cur.xmin=  0.0f;
+                                                       simasel->v2d.cur.ymin=  0.0f;
                                                        simasel->v2d.cur.xmax= (float)sa->winx;
                                                        simasel->v2d.cur.ymax= (float)sa->winy;                                         
                                                        simasel->v2d.min[0]= 1.0;
@@ -7427,8 +7903,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                                        simasel->v2d.minzoom= 0.5f;
                                                        simasel->v2d.maxzoom= 1.21f;                                            
                                                        simasel->v2d.scroll= 0;
-                                                       simasel->v2d.keepaspect= 1;
-                                                       simasel->v2d.keepzoom= 1;
+                                                       simasel->v2d.keepzoom= V2D_KEEPZOOM|V2D_KEEPASPECT;
                                                        simasel->v2d.keeptot= 0;
                                                        simasel->prv_h = 96;
                                                        simasel->prv_w = 96;
@@ -7497,11 +7972,11 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                
                for(ma=main->mat.first; ma; ma= ma->id.next) {
                        if(ma->samp_gloss_mir == 0) {
-                               ma->gloss_mir = ma->gloss_tra= 1.0;
-                               ma->aniso_gloss_mir = 1.0;
+                               ma->gloss_mir = ma->gloss_tra= 1.0f;
+                               ma->aniso_gloss_mir = 1.0f;
                                ma->samp_gloss_mir = ma->samp_gloss_tra= 18;
-                               ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005;
-                               ma->dist_mir = 0.0;
+                               ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005f;
+                               ma->dist_mir = 0.0f;
                                ma->fadeto_mir = MA_RAYMIR_FADETOSKY;
                        }
 
@@ -7640,9 +8115,9 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                /* foreground color needs to be somthing other then black */
                Scene *sce;
                for(sce= main->scene.first; sce; sce=sce->id.next) {
-                       sce->r.fg_stamp[0] = sce->r.fg_stamp[1] = sce->r.fg_stamp[2] = 0.8;
-                       sce->r.fg_stamp[3] = 1.0; /* dont use text alpha yet */
-                       sce->r.bg_stamp[3] = 0.25; /* make sure the background has full alpha */
+                       sce->r.fg_stamp[0] = sce->r.fg_stamp[1] = sce->r.fg_stamp[2] = 0.8f;
+                       sce->r.fg_stamp[3] = 1.0f; /* dont use text alpha yet */
+                       sce->r.bg_stamp[3] = 0.25f; /* make sure the background has full alpha */
                }
        }
 
@@ -7951,22 +8426,15 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
        }
 
        if ((main->versionfile < 245) || (main->versionfile == 245 && main->subversionfile < 14)) {
-               Scene *sce= main->scene.first;
+               Scene *sce;
                Sequence *seq;
-               Editing *ed;
                
-               while(sce) {
-                       ed= sce->ed;
-                       if(ed) {
-                               WHILE_SEQ(&ed->seqbase) {
-                                       if (seq->blend_mode == 0) {
-                                               seq->blend_opacity = 100.0;
-                                       }
-                               }
-                               END_SEQ
+               for(sce=main->scene.first; sce; sce=sce->id.next) {
+                       SEQ_BEGIN(sce->ed, seq) {
+                               if (seq->blend_mode == 0)
+                                       seq->blend_opacity = 100.0f;
                        }
-                       
-                       sce= sce->id.next;
+                       SEQ_END
                }
        }
        
@@ -8054,7 +8522,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                Object *ob;
                for(ob = main->object.first; ob; ob= ob->id.next) {
                        if(ob->pd && (ob->pd->forcefield == PFIELD_WIND))
-                               ob->pd->f_noise = 0.0;
+                               ob->pd->f_noise = 0.0f;
                }
        }
 
@@ -8062,7 +8530,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                Object *ob;
                for(ob = main->object.first; ob; ob= ob->id.next) {
                        ob->gameflag |= OB_COLLISION;
-                       ob->margin = 0.06;
+                       ob->margin = 0.06f;
                }
        }
 
@@ -8175,28 +8643,180 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                for(la=main->lamp.first; la; la= la->id.next) {
                        if(la->atm_turbidity == 0.0) {
                                la->sun_effect_type = 0;
-                               la->horizon_brightness = 1.0;
-                               la->spread = 1.0;
-                               la->sun_brightness = 1.0;
-                               la->sun_size = 1.0;
-                               la->backscattered_light = 1.0;
-                               la->atm_turbidity = 2.0;
-                               la->atm_inscattering_factor = 1.0;
-                               la->atm_extinction_factor = 1.0;
-                               la->atm_distance_factor = 1.0;
-                               la->sun_intensity = 1.0;
+                               la->horizon_brightness = 1.0f;
+                               la->spread = 1.0f;
+                               la->sun_brightness = 1.0f;
+                               la->sun_size = 1.0f;
+                               la->backscattered_light = 1.0f;
+                               la->atm_turbidity = 2.0f;
+                               la->atm_inscattering_factor = 1.0f;
+                               la->atm_extinction_factor = 1.0f;
+                               la->atm_distance_factor = 1.0f;
+                               la->sun_intensity = 1.0f;
+                       }
+               }
+       }
+
+       if (main->versionfile < 248 || (main->versionfile == 248 && main->subversionfile < 2)) {
+               Scene *sce;
+               
+               /* Note, these will need to be added for painting */
+               for (sce= main->scene.first; sce; sce= sce->id.next) {
+                       sce->toolsettings->imapaint.seam_bleed = 2;
+                       sce->toolsettings->imapaint.normal_angle = 80;
+               }
+       }
+       if (main->versionfile < 248 || (main->versionfile == 248 && main->subversionfile < 3)) {
+               bScreen *sc;
+               
+               /* adjust default settings for Animation Editors */
+               for (sc= main->screen.first; sc; sc= sc->id.next) {
+                       ScrArea *sa;
+                       
+                       for (sa= sc->areabase.first; sa; sa= sa->next) { 
+                               SpaceLink *sl;
+                               
+                               for (sl= sa->spacedata.first; sl; sl= sl->next) {
+                                       switch (sl->spacetype) {
+                                               case SPACE_ACTION:
+                                               {
+                                                       SpaceAction *sact= (SpaceAction *)sl;
+                                                       
+                                                       sact->mode= SACTCONT_DOPESHEET;
+                                                       sact->autosnap= SACTSNAP_FRAME;
+                                               }
+                                                       break;
+                                               case SPACE_IPO:
+                                               {
+                                                       SpaceIpo *sipo= (SpaceIpo *)sl;
+                                                       sipo->autosnap= SACTSNAP_FRAME;
+                                               }
+                                                       break;
+                                               case SPACE_NLA:
+                                               {
+                                                       SpaceNla *snla= (SpaceNla *)sl;
+                                                       snla->autosnap= SACTSNAP_FRAME;
+                                               }
+                                                       break;
+                                       }
+                               }
                        }
                }
        }
 
        if (main->versionfile < 250) {
                bScreen *screen;
+               Scene *scene;
+               Material *ma;
+               Scene *sce;
+               Tex *tx;
                
                for(screen= main->screen.first; screen; screen= screen->id.next)
                        do_versions_windowmanager_2_50(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) {
+                       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.
+                */
+               for(ma= main->mat.first; ma; ma= ma->id.next) {
+                       if(ma->nodetree && strlen(ma->nodetree->id.name)==0)
+                               strcpy(ma->nodetree->id.name, "NTShader Nodetree");
+               }
+               /* and composit trees */
+               for(sce= main->scene.first; sce; sce= sce->id.next) {
+                       if(sce->nodetree && strlen(sce->nodetree->id.name)==0)
+                               strcpy(sce->nodetree->id.name, "NTComposit Nodetree");
+               }
+               /* and texture trees */
+               for(tx= main->tex.first; tx; tx= tx->id.next) {
+                       if(tx->nodetree && strlen(tx->nodetree->id.name)==0)
+                               strcpy(tx->nodetree->id.name, "NTTexture Nodetree");
+               }
        }
-       
-       
+
+       /* 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;
+               int i;
+
+               for(ob = main->object.first; ob; ob = ob->id.next) {
+
+                       if(ob->type == OB_MESH) {
+                               Mesh *me = newlibadr(fd, lib, ob->data);
+                               void *olddata = ob->data;
+                               ob->data = me;
+
+                               if(me && me->mr) {
+                                       MultiresLevel *lvl;
+                                       ModifierData *md;
+                                       MultiresModifierData *mmd;
+                                       DerivedMesh *dm, *orig;
+
+                                       /* Load original level into the mesh */
+                                       lvl = me->mr->levels.first;
+                                       CustomData_free_layers(&me->vdata, CD_MVERT, lvl->totvert);
+                                       CustomData_free_layers(&me->edata, CD_MEDGE, lvl->totedge);
+                                       CustomData_free_layers(&me->fdata, CD_MFACE, lvl->totface);
+                                       me->totvert = lvl->totvert;
+                                       me->totedge = lvl->totedge;
+                                       me->totface = lvl->totface;
+                                       me->mvert = CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, me->totvert);
+                                       me->medge = CustomData_add_layer(&me->edata, CD_MEDGE, CD_CALLOC, NULL, me->totedge);
+                                       me->mface = CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC, NULL, me->totface);
+                                       memcpy(me->mvert, me->mr->verts, sizeof(MVert) * me->totvert);
+                                       for(i = 0; i < me->totedge; ++i) {
+                                               me->medge[i].v1 = lvl->edges[i].v[0];
+                                               me->medge[i].v2 = lvl->edges[i].v[1];
+                                       }
+                                       for(i = 0; i < me->totface; ++i) {
+                                               me->mface[i].v1 = lvl->faces[i].v[0];
+                                               me->mface[i].v2 = lvl->faces[i].v[1];
+                                               me->mface[i].v3 = lvl->faces[i].v[2];
+                                               me->mface[i].v4 = lvl->faces[i].v[3];
+                                       }
+
+                                       /* Add a multires modifier to the object */
+                                       md = ob->modifiers.first;
+                                       while(md && modifierType_getInfo(md->type)->type == eModifierTypeType_OnlyDeform)
+                                               md = md->next;                          
+                                       mmd = (MultiresModifierData*)modifier_new(eModifierType_Multires);
+                                       BLI_insertlinkbefore(&ob->modifiers, md, mmd);
+
+                                       multiresModifier_subdivide(mmd, ob, me->mr->level_count - 1, 1, 0);
+
+                                       mmd->lvl = mmd->totlvl;
+                                       orig = CDDM_from_mesh(me, NULL);
+                                       dm = multires_dm_create_from_derived(mmd, orig, me, 0, 0);
+                                       
+                                       multires_load_old(dm, me->mr);
+
+                                       *MultiresDM_get_flags(dm) |= MULTIRES_DM_UPDATE_ALWAYS;
+                                       dm->release(dm);
+                                       orig->release(orig);
+
+                                       /* Remove the old multires */
+                                       multires_free(me->mr);
+                                       me->mr = NULL;
+                               }
+
+                               ob->data = olddata;
+                       }
+               }
+       }
+                                      
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
        /* WATCH IT 2!: Userdef struct init has to be in src/usiblender.c! */
 
@@ -8216,7 +8836,7 @@ static void lib_link_all(FileData *fd, Main *main)
        lib_link_material(fd, main);
        lib_link_texture(fd, main);
        lib_link_image(fd, main);
-       lib_link_ipo(fd, main);
+       lib_link_ipo(fd, main);         // XXX depreceated... still needs to be maintained for version patches still
        lib_link_key(fd, main);
        lib_link_world(fd, main);
        lib_link_lamp(fd, main);
@@ -8258,11 +8878,10 @@ static BHead *read_userdef(BlendFileData *bfd, FileData *fd, BHead *bhead)
        return bhead;
 }
 
-BlendFileData *blo_read_file_internal(FileData *fd, BlendReadError *error_r)
+BlendFileData *blo_read_file_internal(FileData *fd)
 {
        BHead *bhead= blo_firstbhead(fd);
        BlendFileData *bfd;
-       FileGlobal *fg = (FileGlobal *)NULL;
 
        bfd= MEM_callocN(sizeof(BlendFileData), "blendfiledata");
        bfd->main= MEM_callocN(sizeof(Main), "main");
@@ -8479,14 +9098,9 @@ static void expand_doit(FileData *fd, Main *mainvar, void *old)
        }
 }
 
-static void expand_particlesettings(FileData *fd, Main *mainvar, ParticleSettings *part)
-{
-       expand_doit(fd, mainvar, part->dup_ob);
-       expand_doit(fd, mainvar, part->dup_group);
-       expand_doit(fd, mainvar, part->eff_group);
-       expand_doit(fd, mainvar, part->bb_ob);
-}
 
+
+// XXX depreceated - old animation system
 static void expand_ipo(FileData *fd, Main *mainvar, Ipo *ipo)
 {
        IpoCurve *icu;
@@ -8496,6 +9110,50 @@ static void expand_ipo(FileData *fd, Main *mainvar, Ipo *ipo)
        }
 }
 
+// XXX depreceated - old animation system
+static void expand_constraint_channels(FileData *fd, Main *mainvar, ListBase *chanbase)
+{
+       bConstraintChannel *chan;
+       for (chan=chanbase->first; chan; chan=chan->next) {
+               expand_doit(fd, mainvar, chan->ipo);
+       }
+}
+
+// XXX depreceated - old animation system
+static void expand_action(FileData *fd, Main *mainvar, bAction *act)
+{
+       bActionChannel *chan;
+       
+       for (chan=act->chanbase.first; chan; chan=chan->next) {
+               expand_doit(fd, mainvar, chan->ipo);
+               expand_constraint_channels(fd, mainvar, &chan->constraintChannels);
+       }
+}
+
+static void expand_animdata(FileData *fd, Main *mainvar, AnimData *adt)
+{
+       FCurve *fcd;
+       
+       /* own action */
+       expand_doit(fd, mainvar, adt->action);
+       
+       /* 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;
+               
+               expand_doit(fd, mainvar, driver->id);
+               expand_doit(fd, mainvar, driver->id2);
+       }
+}      
+
+static void expand_particlesettings(FileData *fd, Main *mainvar, ParticleSettings *part)
+{
+       expand_doit(fd, mainvar, part->dup_ob);
+       expand_doit(fd, mainvar, part->dup_group);
+       expand_doit(fd, mainvar, part->eff_group);
+       expand_doit(fd, mainvar, part->bb_ob);
+}
+
 static void expand_group(FileData *fd, Main *mainvar, Group *group)
 {
        GroupObject *go;
@@ -8507,7 +9165,10 @@ static void expand_group(FileData *fd, Main *mainvar, Group *group)
 
 static void expand_key(FileData *fd, Main *mainvar, Key *key)
 {
-       expand_doit(fd, mainvar, key->ipo);
+       expand_doit(fd, mainvar, key->ipo); // XXX depreceated - old animation system
+       
+       if(key->adt)
+               expand_animdata(fd, mainvar, key->adt);
 }
 
 static void expand_nodetree(FileData *fd, Main *mainvar, bNodeTree *ntree)
@@ -8523,7 +9184,10 @@ static void expand_nodetree(FileData *fd, Main *mainvar, bNodeTree *ntree)
 static void expand_texture(FileData *fd, Main *mainvar, Tex *tex)
 {
        expand_doit(fd, mainvar, tex->ima);
-       expand_doit(fd, mainvar, tex->ipo);
+       expand_doit(fd, mainvar, tex->ipo); // XXX depreceated - old animation system
+       
+       if(tex->adt)
+               expand_animdata(fd, mainvar, tex->adt);
        
        if(tex->nodetree)
                expand_nodetree(fd, mainvar, tex->nodetree);
@@ -8550,7 +9214,10 @@ static void expand_material(FileData *fd, Main *mainvar, Material *ma)
                }
        }
        
-       expand_doit(fd, mainvar, ma->ipo);
+       expand_doit(fd, mainvar, ma->ipo); // XXX depreceated - old animation system
+       
+       if(ma->adt)
+               expand_animdata(fd, mainvar, ma->adt);
        
        if(ma->nodetree)
                expand_nodetree(fd, mainvar, ma->nodetree);
@@ -8566,12 +9233,16 @@ static void expand_lamp(FileData *fd, Main *mainvar, Lamp *la)
                        expand_doit(fd, mainvar, la->mtex[a]->object);
                }
        }
-       expand_doit(fd, mainvar, la->ipo);
+       
+       expand_doit(fd, mainvar, la->ipo); // XXX depreceated - old animation system
+       
+       if (la->adt)
+               expand_animdata(fd, mainvar, la->adt);
 }
 
 static void expand_lattice(FileData *fd, Main *mainvar, Lattice *lt)
 {
-       expand_doit(fd, mainvar, lt->ipo);
+       expand_doit(fd, mainvar, lt->ipo); // XXX depreceated - old animation system
        expand_doit(fd, mainvar, lt->key);
 }
 
@@ -8586,7 +9257,11 @@ static void expand_world(FileData *fd, Main *mainvar, World *wrld)
                        expand_doit(fd, mainvar, wrld->mtex[a]->object);
                }
        }
-       expand_doit(fd, mainvar, wrld->ipo);
+       
+       expand_doit(fd, mainvar, wrld->ipo); // XXX depreceated - old animation system
+       
+       if (wrld->adt)
+               expand_animdata(fd, mainvar, wrld->adt);
 }
 
 
@@ -8606,15 +9281,19 @@ static void expand_curve(FileData *fd, Main *mainvar, Curve *cu)
        for(a=0; a<cu->totcol; a++) {
                expand_doit(fd, mainvar, cu->mat[a]);
        }
+       
        expand_doit(fd, mainvar, cu->vfont);
        expand_doit(fd, mainvar, cu->vfontb);   
        expand_doit(fd, mainvar, cu->vfonti);
        expand_doit(fd, mainvar, cu->vfontbi);
        expand_doit(fd, mainvar, cu->key);
-       expand_doit(fd, mainvar, cu->ipo);
+       expand_doit(fd, mainvar, cu->ipo); // XXX depreceated - old animation system
        expand_doit(fd, mainvar, cu->bevobj);
        expand_doit(fd, mainvar, cu->taperobj);
        expand_doit(fd, mainvar, cu->textoncurve);
+       
+       if(cu->adt)
+               expand_animdata(fd, mainvar, cu->adt);
 }
 
 static void expand_mesh(FileData *fd, Main *mainvar, Mesh *me)
@@ -8657,7 +9336,7 @@ static void expand_constraints(FileData *fd, Main *mainvar, ListBase *lb)
        for (curcon=lb->first; curcon; curcon=curcon->next) {
                
                if (curcon->ipo)
-                       expand_doit(fd, mainvar, curcon->ipo);
+                       expand_doit(fd, mainvar, curcon->ipo); // XXX depreceated - old animation system
                
                switch (curcon->type) {
                case CONSTRAINT_TYPE_NULL:
@@ -8803,23 +9482,6 @@ static void expand_armature(FileData *fd, Main *mainvar, bArmature *arm)
        }
 }
 
-static void expand_constraint_channels(FileData *fd, Main *mainvar, ListBase *chanbase)
-{
-       bConstraintChannel *chan;
-       for (chan=chanbase->first; chan; chan=chan->next){
-               expand_doit(fd, mainvar, chan->ipo);
-       }
-}
-
-static void expand_action(FileData *fd, Main *mainvar, bAction *act)
-{
-       bActionChannel *chan;
-       for (chan=act->chanbase.first; chan; chan=chan->next) {
-               expand_doit(fd, mainvar, chan->ipo);
-               expand_constraint_channels(fd, mainvar, &chan->constraintChannels);
-       }
-}
-
 static void expand_modifier(FileData *fd, Main *mainvar, ModifierData *md)
 {
        if (md->type==eModifierType_Lattice) {
@@ -8873,16 +9535,19 @@ static void expand_object(FileData *fd, Main *mainvar, Object *ob)
 
 
        expand_doit(fd, mainvar, ob->data);
-       expand_doit(fd, mainvar, ob->ipo);
-       expand_doit(fd, mainvar, ob->action);
-       expand_doit(fd, mainvar, ob->poselib);
-
+       
        for (md=ob->modifiers.first; md; md=md->next) {
                expand_modifier(fd, mainvar, md);
        }
 
        expand_pose(fd, mainvar, ob->pose);
+       expand_doit(fd, mainvar, ob->poselib);
        expand_constraints(fd, mainvar, &ob->constraints);
+       
+// XXX depreceated - old animation system (for version patching only) 
+       expand_doit(fd, mainvar, ob->ipo);
+       expand_doit(fd, mainvar, ob->action);
+       
        expand_constraint_channels(fd, mainvar, &ob->constraintChannels);
 
        for (strip=ob->nlastrips.first; strip; strip=strip->next){
@@ -8890,7 +9555,11 @@ static void expand_object(FileData *fd, Main *mainvar, Object *ob)
                expand_doit(fd, mainvar, strip->act);
                expand_doit(fd, mainvar, strip->ipo);
        }
-
+// XXX depreceated - old animation system (for version patching only)
+       
+       if(ob->adt)
+               expand_animdata(fd, mainvar, ob->adt);
+       
        for(a=0; a<ob->totcol; a++) {
                expand_doit(fd, mainvar, ob->mat[a]);
        }
@@ -8990,6 +9659,9 @@ static void expand_scene(FileData *fd, Main *mainvar, Scene *sce)
        expand_doit(fd, mainvar, sce->camera);
        expand_doit(fd, mainvar, sce->world);
        
+       if(sce->adt)
+               expand_animdata(fd, mainvar, sce->adt);
+       
        if(sce->nodetree)
                expand_nodetree(fd, mainvar, sce->nodetree);
        
@@ -8997,17 +9669,19 @@ static void expand_scene(FileData *fd, Main *mainvar, Scene *sce)
                expand_doit(fd, mainvar, srl->mat_override);
                expand_doit(fd, mainvar, srl->light_override);
        }
-                               
 }
 
 static void expand_camera(FileData *fd, Main *mainvar, Camera *ca)
 {
-       expand_doit(fd, mainvar, ca->ipo);
+       expand_doit(fd, mainvar, ca->ipo); // XXX depreceated - old animation system
+       
+       if(ca->adt)
+               expand_animdata(fd, mainvar, ca->adt);
 }
 
 static void expand_sound(FileData *fd, Main *mainvar, bSound *snd)
 {
-       expand_doit(fd, mainvar, snd->ipo);
+       expand_doit(fd, mainvar, snd->ipo); // XXX depreceated - old animation system
 }
 
 
@@ -9074,7 +9748,7 @@ static void expand_main(FileData *fd, Main *mainvar)
                                                expand_armature(fd, mainvar, (bArmature *)id);
                                                break;
                                        case ID_AC:
-                                               expand_action(fd, mainvar, (bAction *)id);
+                                               expand_action(fd, mainvar, (bAction *)id); // XXX depreceated - old animation system
                                                break;
                                        case ID_GR:
                                                expand_group(fd, mainvar, (Group *)id);
@@ -9086,7 +9760,7 @@ static void expand_main(FileData *fd, Main *mainvar)
                                                expand_brush(fd, mainvar, (Brush *)id);
                                                break;
                                        case ID_IP:
-                                               expand_ipo(fd, mainvar, (Ipo *)id);
+                                               expand_ipo(fd, mainvar, (Ipo *)id); // XXX depreceated - old animation system
                                                break;
                                        case ID_PA:
                                                expand_particlesettings(fd, mainvar, (ParticleSettings *)id);
@@ -9102,24 +9776,24 @@ static void expand_main(FileData *fd, Main *mainvar)
        }
 }
 
-static int object_in_any_scene(Object *ob)
+static int object_in_any_scene(Main *mainvar, Object *ob)
 {
        Scene *sce;
        
-       for(sce= G.main->scene.first; sce; sce= sce->id.next)
+       for(sce= mainvar->scene.first; sce; sce= sce->id.next)
                if(object_in_scene(ob, sce))
                        return 1;
        return 0;
 }
 
 /* when *lib set, it also does objects that were in the appended group */
-static void give_base_to_objects(Scene *sce, ListBase *lb, Library *lib, int is_group_append)
+static void give_base_to_objects(Main *mainvar, Scene *sce, Library *lib, int is_group_append)
 {
        Object *ob;
        Base *base;
 
        /* give all objects which are LIB_INDIRECT a base, or for a group when *lib has been set */
-       for(ob= lb->first; ob; ob= ob->id.next) {
+       for(ob= mainvar->object.first; ob; ob= ob->id.next) {
                
                if( ob->id.flag & LIB_INDIRECT ) {
                        
@@ -9135,7 +9809,7 @@ static void give_base_to_objects(Scene *sce, ListBase *lb, Library *lib, int is_
                                if(ob->id.us==0)
                                        do_it= 1;
                                else if(ob->id.us==1 && lib)
-                                       if(ob->id.lib==lib && (ob->flag & OB_FROMGROUP) && object_in_any_scene(ob)==0)
+                                       if(ob->id.lib==lib && (ob->flag & OB_FROMGROUP) && object_in_any_scene(mainvar, ob)==0)
                                                do_it= 1;
                                                
                                if(do_it) {
@@ -9193,9 +9867,9 @@ static void append_named_part(FileData *fd, Main *mainvar, Scene *scene, char *n
                                        else ob= (Object *)id;
                                        
                                        /* XXX use context to find view3d->lay */
-                                       if((flag & FILE_ACTIVELAY)) {
-                                               scene->lay;
-                                       }
+                                       //if((flag & FILE_ACTIVELAY)) {
+                                       //      scene->lay;
+                                       //}
                                        base->lay= ob->lay;
                                        base->object= ob;
                                        ob->id.us++;
@@ -9236,14 +9910,14 @@ static void append_id_part(FileData *fd, Main *mainvar, ID *id, ID **id_r)
 
 /* common routine to append/link something from a library */
 
-static Library* library_append(Scene *scene, char* file, char *dir, int idcode,
+static Library* library_append(Main *mainvar, Scene *scene, char* file, char *dir, int idcode,
                int totsel, FileData **fd, struct direntry* filelist, int totfile, short flag)
 {
        Main *mainl;
        Library *curlib;
 
        /* make mains */
-       blo_split_main(&(*fd)->mainlist, G.main);
+       blo_split_main(&(*fd)->mainlist, mainvar);
 
        /* which one do we need? */
        mainl = blo_find_main(*fd, &(*fd)->mainlist, dir, G.sce);
@@ -9280,21 +9954,21 @@ static Library* library_append(Scene *scene, char* file, char *dir, int idcode,
        }
 
        blo_join_main(&(*fd)->mainlist);
-       G.main= (*fd)->mainlist.first;
+       mainvar= (*fd)->mainlist.first;
 
-       lib_link_all(*fd, G.main);
-       lib_verify_nodetree(G.main, 0);
-       fix_relpaths_library(G.sce, G.main); /* make all relative paths, relative to the open blend file */
+       lib_link_all(*fd, mainvar);
+       lib_verify_nodetree(mainvar, 0);
+       fix_relpaths_library(G.sce, mainvar); /* make all relative paths, relative to the open blend file */
 
        /* give a base to loose objects. If group append, do it for objects too */
        if(idcode==ID_GR) {
                if (flag & FILE_LINK) {
-                       give_base_to_objects(scene, &(G.main->object), NULL, 0);
+                       give_base_to_objects(mainvar, scene, NULL, 0);
                } else {
-                       give_base_to_objects(scene, &(G.main->object), curlib, 1);
+                       give_base_to_objects(mainvar, scene, curlib, 1);
                }       
        } else {
-               give_base_to_objects(scene, &(G.main->object), NULL, 0);
+               give_base_to_objects(mainvar, scene, NULL, 0);
        }
        /* has been removed... erm, why? s..ton) */
        /* 20040907: looks like they are give base already in append_named_part(); -Nathan L */
@@ -9315,26 +9989,26 @@ static Library* library_append(Scene *scene, char* file, char *dir, int idcode,
 /* this should probably be moved into the Python code anyway */
 
 void BLO_script_library_append(BlendHandle **bh, char *dir, char *name, 
-               int idcode, short flag, Scene *scene )
+               int idcode, short flag, Main *mainvar, Scene *scene, ReportList *reports)
 {
+       FileData *fd= (FileData*)(*bh);
+
        /* try to append the requested object */
-       library_append(scene, name, dir, idcode, 0, (FileData **)bh, NULL, 0, flag );
+       fd->reports= reports;
+       library_append(mainvar, scene, name, dir, idcode, 0, &fd, NULL, 0, flag );
+       if(fd) fd->reports= NULL;
 
        /* do we need to do this? */
        DAG_scene_sort(scene);
-}
 
-/* append to scene */
-/* dir is a full path */       
-void BLO_library_append(SpaceFile *sfile, char *dir, int idcode, Scene *scene)
-{
-       BLO_library_append_(&sfile->libfiledata, sfile->filelist, sfile->totfile, 
-                                               dir, sfile->file, sfile->flag, idcode, scene);
+       *bh= (BlendHandle*)fd;
 }
 
-void BLO_library_append_(BlendHandle** libfiledata, struct direntry* filelist, int totfile, 
-                                                char *dir, char* file, short flag, int idcode, Scene *scene)
+/* append to scene */
+void BLO_library_append(BlendHandle** bh, struct direntry* filelist, int totfile, 
+                                                char *dir, char* file, short flag, int idcode, Main *mainvar, Scene *scene, ReportList *reports)
 {
+       FileData *fd= (FileData*)(*bh);
        Library *curlib;
        Base *centerbase;
        Object *ob;
@@ -9354,12 +10028,12 @@ void BLO_library_append_(BlendHandle** libfiledata, struct direntry* filelist, i
                                if( strcmp(filelist[a].relname, file)==0) break;
                        }
                        if(a==totfile) {
-                               //XXX error("Wrong indicated name");
+                               BKE_report(reports, RPT_ERROR, "Wrong indicated name");
                                return;
                        }
                }
                else {
-                       //XXX error("Nothing indicated");
+                       BKE_report(reports, RPT_ERROR, "Nothing indicated");
                        return;
                }
        }
@@ -9367,7 +10041,11 @@ void BLO_library_append_(BlendHandle** libfiledata, struct direntry* filelist, i
        
        if(flag & FILE_AUTOSELECT) scene_deselect_all(scene);
 
-       curlib = library_append(scene, file, dir, idcode, totsel, (FileData**) libfiledata, filelist, totfile,flag );
+       fd->reports= reports;
+       curlib = library_append(mainvar, scene, file, dir, idcode, totsel, &fd, filelist, totfile,flag );
+       if(fd) fd->reports= NULL;
+
+       *bh= (BlendHandle*)fd;
 
        /* when not linking (appending)... */
        if((flag & FILE_LINK)==0) {
@@ -9446,9 +10124,12 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                FileData *fd= mainptr->curlib->filedata;
 
                                if(fd==NULL) {
-                                       BlendReadError err;
+                                       ReportList reports;
+
                                        printf("read library: lib %s\n", mainptr->curlib->name);
-                                       fd= blo_openblenderfile(mainptr->curlib->filename, &err);
+                                       fd= blo_openblenderfile(mainptr->curlib->filename, &reports);
+                                       fd->reports= basefd->reports;
+
                                        if (fd) {
                                                if (fd->libmap)
                                                        oldnewmap_free(fd->libmap);
@@ -9547,7 +10228,7 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
 
 /* reading runtime */
 
-BlendFileData *blo_read_blendafterruntime(int file, char *name, int actualsize, BlendReadError *error_r) 
+BlendFileData *blo_read_blendafterruntime(int file, char *name, int actualsize, ReportList *reports)
 {
        BlendFileData *bfd = NULL;
        FileData *fd = filedata_new();
@@ -9558,11 +10239,12 @@ BlendFileData *blo_read_blendafterruntime(int file, char *name, int actualsize,
        /* needed for library_append and read_libraries */
        BLI_strncpy(fd->filename, name, sizeof(fd->filename));
 
-       fd = blo_decode_and_check(fd, error_r);
+       fd = blo_decode_and_check(fd, reports);
        if (!fd)
                return NULL;
 
-       bfd= blo_read_file_internal(fd, error_r);
+       fd->reports= reports;
+       bfd= blo_read_file_internal(fd);
        blo_freefiledata(fd);
 
        return bfd;