2.5: Various
[blender.git] / source / blender / blenloader / intern / readfile.c
index 4a0f8206613e3469c70aa2793ed195f47f344193..c36a287b9811d6dc970e35a825a6fb3a7db2128d 100644 (file)
 
 #include "readfile.h"
 
+#include "PIL_time.h"
+
 #include <errno.h>
 
 /*
@@ -178,7 +180,7 @@ READ
                - read associated 'direct data'
                - link direct data (internal and to LibBlock)
 - read FileGlobal
-- read USER data, only when indicated (file is ~/.B.blend)
+- read USER data, only when indicated (file is ~/.B.blend or .B25.blend)
 - free file
 - per Library (per Main)
        - read file
@@ -611,10 +613,16 @@ static BHeadN *get_bhead(FileData *fd)
        BHead  bhead;
        BHeadN *new_bhead = 0;
        int readsize;
-
+       
        if (fd) {
                if ( ! fd->eof) {
 
+                       /* not strictly needed but shuts valgrind up
+                        * since uninitialized memory gets compared */
+                       memset(&bhead8, 0, sizeof(BHead8));
+                       memset(&bhead4, 0, sizeof(BHead4));
+                       memset(&bhead,  0, sizeof(BHead));
+                       
                        // First read the bhead structure.
                        // Depending on the platform the file was written on this can
                        // be a big or little endian BHead4 or BHead8 structure.
@@ -1489,6 +1497,8 @@ static void lib_link_brush(FileData *fd, Main *main)
                if(brush->id.flag & LIB_NEEDLINK) {
                        brush->id.flag -= LIB_NEEDLINK;
 
+                       brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
+                       
                        for(a=0; a<MAX_MTEX; a++) {
                                mtex= brush->mtex[a];
                                if(mtex)
@@ -1667,8 +1677,10 @@ static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
                /* driver data */
                if (fcu->driver) {
                        ChannelDriver *driver= fcu->driver;
-                       driver->id= newlibadr(fd, id->lib, driver->id); 
-                       driver->id2= newlibadr(fd, id->lib, driver->id2); 
+                       DriverTarget *dtar;
+                       
+                       for (dtar= driver->targets.first; dtar; dtar= dtar->next)
+                               dtar->id= newlibadr(fd, id->lib, dtar->id); 
                }
                
                /* modifiers */
@@ -1708,9 +1720,12 @@ static void direct_link_fcurves(FileData *fd, ListBase *list)
                fcu->driver= newdataadr(fd, fcu->driver);
                if (fcu->driver) {
                        ChannelDriver *driver= fcu->driver;
+                       DriverTarget *dtar;
                        
-                       driver->rna_path= newdataadr(fd, driver->rna_path);
-                       driver->rna_path2= newdataadr(fd, driver->rna_path2);
+                       /* relink targets and their paths */
+                       link_list(fd, &driver->targets);
+                       for (dtar= driver->targets.first; dtar; dtar= dtar->next)
+                               dtar->rna_path= newdataadr(fd, dtar->rna_path);
                }
                
                /* modifiers */
@@ -1718,6 +1733,7 @@ static void direct_link_fcurves(FileData *fd, ListBase *list)
                for (fcm= fcu->modifiers.first; fcm; fcm= fcm->next) {
                        /* relink general data */
                        fcm->data = newdataadr(fd, fcm->data);
+                       fcm->edata= NULL;
                        
                        /* do relinking of data for specific types */
                        switch (fcm->type) {
@@ -2147,6 +2163,13 @@ static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
                                data->tar = newlibadr(fd, id->lib, data->tar);
                        }
                        break;
+               case CONSTRAINT_TYPE_SHRINKWRAP:
+                       {
+                               bShrinkwrapConstraint *data;
+                               data= ((bShrinkwrapConstraint*)con->data);
+                               data->target = newlibadr(fd, id->lib, data->target);
+                       }
+                       break;
                case CONSTRAINT_TYPE_NULL:
                        break;
                }
@@ -2163,9 +2186,12 @@ static void direct_link_constraints(FileData *fd, ListBase *lb)
                
                if (cons->type == CONSTRAINT_TYPE_PYTHON) {
                        bPythonConstraint *data= cons->data;
+                       
                        link_list(fd, &data->targets);
+                       
                        data->prop = newdataadr(fd, data->prop);
-                       IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
+                       if (data->prop)
+                               IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
                }
        }
 }
@@ -2852,6 +2878,16 @@ static void direct_link_material(FileData *fd, Material *ma)
 
 static void direct_link_pointcache(FileData *fd, PointCache *cache)
 {
+       if((cache->flag & PTCACHE_DISK_CACHE)==0) {
+               PTCacheMem *pm;
+
+               link_list(fd, &cache->mem_cache);
+
+               pm = cache->mem_cache.first;
+
+               for(; pm; pm=pm->next)
+                       pm->data = newdataadr(fd, pm->data);
+       }
        cache->flag &= ~(PTCACHE_SIMULATION_VALID|PTCACHE_BAKE_EDIT_ACTIVE);
        cache->simframe= 0;
 }
@@ -2863,7 +2899,9 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
        part= main->particle.first;
        while(part) {
                if(part->id.flag & LIB_NEEDLINK) {
+                       if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
                        part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
+                       
                        part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
                        part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
                        part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
@@ -2876,6 +2914,7 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
 
 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
 {
+       part->adt= newdataadr(fd, part->adt);
        part->pd= newdataadr(fd, part->pd);
        part->pd2= newdataadr(fd, part->pd2);
 }
@@ -3116,10 +3155,8 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
                direct_link_dverts(fd, lvl->totvert, CustomData_get(&mesh->mr->vdata, 0, CD_MDEFORMVERT));
                direct_link_customdata(fd, &mesh->mr->fdata, lvl->totface);
                
-               if(!mesh->mr->edge_flags)
-                       mesh->mr->edge_flags= MEM_callocN(sizeof(short)*lvl->totedge, "Multires Edge Flags");
-               if(!mesh->mr->edge_creases)
-                       mesh->mr->edge_creases= MEM_callocN(sizeof(char)*lvl->totedge, "Multires Edge Creases");
+               mesh->mr->edge_flags= newdataadr(fd, mesh->mr->edge_flags);
+               mesh->mr->edge_creases= newdataadr(fd, mesh->mr->edge_creases);
 
                mesh->mr->verts = newdataadr(fd, mesh->mr->verts);
                        
@@ -3130,6 +3167,18 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
                        lvl->colfaces= newdataadr(fd, lvl->colfaces);
                }
        }
+
+       /* Gracefully handle corrupted mesh */
+       if(mesh->mr && !mesh->mr->verts) {
+               /* If totals match, simply load the current mesh verts into multires */
+               if(mesh->totvert == ((MultiresLevel*)mesh->mr->levels.last)->totvert)
+                       mesh->mr->verts = MEM_dupallocN(mesh->mvert);
+               else {
+                       /* Otherwise, we can't recover the data, silently remove multires */
+                       multires_free(mesh->mr);
+                       mesh->mr = NULL;
+               }
+       }
        
        if((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
                TFace *tf= mesh->tface;
@@ -3321,6 +3370,10 @@ static void lib_link_object(FileData *fd, Main *main)
                                        bAddObjectActuator *eoa= act->data;
                                        if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
                                }
+                               else if(act->type==ACT_OBJECT) {
+                                       bObjectActuator *oa= act->data;
+                                       oa->reference= newlibadr(fd, ob->id.lib, oa->reference);
+                               }
                                else if(act->type==ACT_EDIT_OBJECT) {
                                        bEditObjectActuator *eoa= act->data;
                                        if(eoa==NULL) {
@@ -3331,6 +3384,15 @@ static void lib_link_object(FileData *fd, Main *main)
                                                eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
                                        }
                                }
+                               else if(act->type==ACT_OBJECT) {
+                                       bObjectActuator *oa= act->data;
+                                       if(oa==NULL) {
+                                               init_actuator(act);
+                                       }
+                                       else {
+                                               oa->reference= newlibadr(fd, ob->id.lib, oa->reference);
+                                       }
+                               }
                                else if(act->type==ACT_SCENE) {
                                        bSceneActuator *sa= act->data;
                                        sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
@@ -3404,7 +3466,13 @@ static void direct_link_pose(FileData *fd, bPose *pose)
                pchan->bone= NULL;
                pchan->parent= newdataadr(fd, pchan->parent);
                pchan->child= newdataadr(fd, pchan->child);
+               
                direct_link_constraints(fd, &pchan->constraints);
+               
+               pchan->prop = newdataadr(fd, pchan->prop);
+               if (pchan->prop)
+                       IDP_DirectLinkProperty(pchan->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
+               
                pchan->iktree.first= pchan->iktree.last= NULL;
                pchan->path= NULL;
        }
@@ -3429,6 +3497,11 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
 
                        smd->emCache = smd->mCache = 0;
                }
+               else if (md->type==eModifierType_Armature) {
+                       ArmatureModifierData *amd = (ArmatureModifierData*) md;
+                       
+                       amd->prevCos= NULL;
+               }
                else if (md->type==eModifierType_Cloth) {
                        ClothModifierData *clmd = (ClothModifierData*) md;
                        
@@ -3479,6 +3552,12 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
                        collmd->mfaces = NULL;
                        
                }
+               else if (md->type==eModifierType_Surface) {
+                       SurfaceModifierData *surmd = (SurfaceModifierData*) md;
+
+                       surmd->dm = NULL;
+                       surmd->bvhtree = NULL;
+               }
                else if (md->type==eModifierType_Hook) {
                        HookModifierData *hmd = (HookModifierData*) md;
 
@@ -3759,12 +3838,19 @@ static void lib_link_scene(FileData *fd, Main *main)
                        sce->world= newlibadr_us(fd, sce->id.lib, sce->world);
                        sce->set= newlibadr(fd, sce->id.lib, sce->set);
                        sce->ima= newlibadr_us(fd, sce->id.lib, sce->ima);
+                       
                        sce->toolsettings->imapaint.brush=
                                newlibadr_us(fd, sce->id.lib, sce->toolsettings->imapaint.brush);
                        if(sce->toolsettings->sculpt)
                                sce->toolsettings->sculpt->brush=
                                        newlibadr_us(fd, sce->id.lib, sce->toolsettings->sculpt->brush);
-
+                       if(sce->toolsettings->vpaint)
+                               sce->toolsettings->vpaint->brush=
+                                       newlibadr_us(fd, sce->id.lib, sce->toolsettings->vpaint->brush);
+                       if(sce->toolsettings->wpaint)
+                               sce->toolsettings->wpaint->brush=
+                                       newlibadr_us(fd, sce->id.lib, sce->toolsettings->wpaint->brush);
+                       
                        sce->toolsettings->skgen_template = newlibadr(fd, sce->id.lib, sce->toolsettings->skgen_template);
 
                        for(base= sce->base.first; base; base= next) {
@@ -3815,7 +3901,9 @@ static void lib_link_scene(FileData *fd, Main *main)
                                srl->mat_override= newlibadr_us(fd, sce->id.lib, srl->mat_override);
                                srl->light_override= newlibadr_us(fd, sce->id.lib, srl->light_override);
                        }
-                       
+                       /*Game Settings: Dome Warp Text*/
+                       sce->r.dometext= newlibadr_us(fd, sce->id.lib, sce->r.dometext);
+
                        sce->id.flag -= LIB_NEEDLINK;
                }
 
@@ -3842,6 +3930,8 @@ static void direct_link_scene(FileData *fd, Scene *sce)
 
        sce->theDag = NULL;
        sce->dagisvalid = 0;
+       sce->obedit= NULL;
+       
        /* set users to one by default, not in lib-link, this will increase it for compo nodes */
        sce->id.us= 1;
 
@@ -3854,8 +3944,6 @@ static void direct_link_scene(FileData *fd, Scene *sce)
        direct_link_keyingsets(fd, &sce->keyingsets);
        
        sce->basact= newdataadr(fd, sce->basact);
-
-       sce->radio= newdataadr(fd, sce->radio);
        
        sce->toolsettings= newdataadr(fd, sce->toolsettings);
        if(sce->toolsettings) {
@@ -3925,6 +4013,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                                if (seq->flag & SEQ_USE_PROXY) {
                                        seq->strip->proxy = newdataadr(
                                                fd, seq->strip->proxy);
+                                       seq->strip->proxy->anim = 0;
                                } else {
                                        seq->strip->proxy = 0;
                                }
@@ -4060,13 +4149,17 @@ static void lib_link_windowmanager(FileData *fd, Main *main)
 
 /* ****************** READ GREASE PENCIL ***************** */
 
-/* relinks grease-pencil data for 3d-view(s) - used for direct_link */
-static void link_gpencil(FileData *fd, bGPdata *gpd)
+/* relinks grease-pencil data - used for direct_link and old file linkage */
+static void direct_link_gpencil(FileData *fd, bGPdata *gpd)
 {
        bGPDlayer *gpl;
        bGPDframe *gpf;
        bGPDstroke *gps;
        
+       /* we must firstly have some grease-pencil data to link! */
+       if (gpd == NULL)
+               return;
+       
        /* relink layers */
        link_list(fd, &gpd->layers);
        
@@ -4154,6 +4247,7 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                SpaceButs *sbuts= (SpaceButs *)sl;
                                                sbuts->lockpoin= NULL;
                                                sbuts->ri= NULL;
+                                               sbuts->pinid= newlibadr(fd, sc->id.lib, sbuts->pinid);
                                                if(main->versionfile<132)
                                                        butspace_version_132(sbuts);
                                        }
@@ -4237,7 +4331,6 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                        else if(GS(snode->id->name)==ID_TE)
                                                                snode->nodetree= ((Tex *)snode->id)->nodetree;
                                                }
-                                               
                                        }
                                }
                                sa= sa->next;
@@ -4348,27 +4441,41 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                                */
                                        }
                                        else if(v3d->scenelock) v3d->lay= sc->scene->lay;
-                                       
+
                                        /* not very nice, but could help */
                                        if((v3d->layact & v3d->lay)==0) v3d->layact= v3d->lay;
                                        
+                               }
+                               else if(sl->spacetype==SPACE_IPO) {
+                                       /* XXX animato */
+#if 0
+                                       SpaceIpo *sipo= (SpaceIpo *)sl;
+
+                                       sipo->ipo= restore_pointer_by_name(newmain, (ID *)sipo->ipo, 0);
+                                       if(sipo->blocktype==ID_SEQ) 
+                                               sipo->from= (ID *)find_sequence_from_ipo_helper(newmain, sipo->ipo);
+                                       else 
+                                               sipo->from= restore_pointer_by_name(newmain, (ID *)sipo->from, 0);
+                                       
+                                       // not free sipo->ipokey, creates dependency with src/
+                                       if(sipo->editipo) MEM_freeN(sipo->editipo);
+                                       sipo->editipo= NULL;
+#endif
                                }
                                else if(sl->spacetype==SPACE_BUTS) {
                                        SpaceButs *sbuts= (SpaceButs *)sl;
                                        sbuts->lockpoin= NULL;
+                                       sbuts->pinid = restore_pointer_by_name(newmain, sbuts->pinid, 0);
                                        //XXX if (sbuts->ri) sbuts->ri->curtile = 0;
                                }
                                else if(sl->spacetype==SPACE_FILE) {
                                        
                                        SpaceFile *sfile= (SpaceFile *)sl;
                                        sfile->files= NULL;
+                                       sfile->folders_prev= NULL;
+                                       sfile->folders_next= NULL;
                                        sfile->params= NULL;
                                        sfile->op= NULL;
-                                       /* XXX needs checking - best solve in filesel itself 
-                                       if(sfile->libfiledata)  
-                                               BLO_blendhandle_close(sfile->libfiledata);
-                                       sfile->libfiledata= 0;
-                                       */
                                }
                                else if(sl->spacetype==SPACE_IMASEL) {
                     SpaceImaSel *simasel= (SpaceImaSel *)sl;
@@ -4455,8 +4562,7 @@ static void direct_link_region(FileData *fd, ARegion *ar, int spacetype)
 
        for(pa= ar->panels.first; pa; pa=pa->next) {
                pa->paneltab= newdataadr(fd, pa->paneltab);
-               pa->active= 0;
-               pa->sortcounter= 0;
+               pa->runtime_flag= 0;
                pa->activedata= NULL;
                pa->type= NULL;
        }
@@ -4504,7 +4610,6 @@ static void view3d_split_250(View3D *v3d, ListBase *regions)
                        rv3d->dist= v3d->dist;
                        VECCOPY(rv3d->ofs, v3d->ofs);
                        QUATCOPY(rv3d->viewquat, v3d->viewquat);
-                       Mat4One(rv3d->twmat);
                }
        }
 }
@@ -4588,7 +4693,7 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                        v3d->bgpic->iuser.ok= 1;
                                if(v3d->gpd) {
                                        v3d->gpd= newdataadr(fd, v3d->gpd);
-                                       link_gpencil(fd, v3d->gpd);
+                                       direct_link_gpencil(fd, v3d->gpd);
                                }
                                v3d->localvd= newdataadr(fd, v3d->localvd);
                                v3d->afterdraw.first= v3d->afterdraw.last= NULL;
@@ -4617,11 +4722,12 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                SpaceImage *sima= (SpaceImage *)sl;
                                
                                sima->cumap= newdataadr(fd, sima->cumap);
-                               if(sima->cumap)
-                                       direct_link_curvemapping(fd, sima->cumap);
                                sima->gpd= newdataadr(fd, sima->gpd);
                                if (sima->gpd)
-                                       link_gpencil(fd, sima->gpd);
+                                       direct_link_gpencil(fd, sima->gpd);
+                               if(sima->cumap)
+                                       direct_link_curvemapping(fd, sima->cumap);
+                               sima->iuser.scene= NULL;
                                sima->iuser.ok= 1;
                        }
                        else if(sl->spacetype==SPACE_NODE) {
@@ -4629,17 +4735,29 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                
                                if(snode->gpd) {
                                        snode->gpd= newdataadr(fd, snode->gpd);
-                                       link_gpencil(fd, snode->gpd);
+                                       direct_link_gpencil(fd, snode->gpd);
                                }
                                snode->nodetree= snode->edittree= NULL;
                        }
+                       else if(sl->spacetype==SPACE_LOGIC) {
+                               SpaceLogic *slogic= (SpaceLogic *)sl;
+                               
+                               if(slogic->gpd) {
+                                       slogic->gpd= newdataadr(fd, slogic->gpd);
+                                       direct_link_gpencil(fd, slogic->gpd);
+                               }
+                       }
                        else if(sl->spacetype==SPACE_SEQ) {
                                SpaceSeq *sseq= (SpaceSeq *)sl;
                                if(sseq->gpd) {
                                        sseq->gpd= newdataadr(fd, sseq->gpd);
-                                       link_gpencil(fd, sseq->gpd);
+                                       direct_link_gpencil(fd, sseq->gpd);
                                }
                        }
+                       else if(sl->spacetype==SPACE_BUTS) {
+                               SpaceButs *sbuts= (SpaceButs *)sl;
+                               sbuts->path= NULL;
+                       }
                }
                
                sa->actionzones.first= sa->actionzones.last= NULL;
@@ -4814,6 +4932,7 @@ static char *dataname(short id_code)
                case ID_NT: return "Data from NT";
                case ID_BR: return "Data from BR";
                case ID_PA: return "Data from PA";
+               case ID_GD: return "Data from GD";
        }
        return "Data from Lib Block";
        
@@ -4970,6 +5089,9 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
                case ID_SCRIPT:
                        direct_link_script(fd, (Script*)id);
                        break;
+               case ID_GD:
+                       direct_link_gpencil(fd, (bGPdata *)id);
+                       break;
        }
        
        /*link direct data of ID properties*/
@@ -5470,20 +5592,15 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                                /* temporarily hide it */
                                ar->flag = RGN_FLAG_HIDDEN;
                                break;
-                               
-                       case SPACE_FILE:
-                               /* channel (bookmarks/directories) region */
+#if 0
+                       case SPACE_BUTS:
+                               /* context UI region */
                                ar= MEM_callocN(sizeof(ARegion), "area region from do_versions");
                                BLI_addtail(lb, ar);
                                ar->regiontype= RGN_TYPE_CHANNELS;
-                               ar->alignment= RGN_ALIGN_LEFT;
-                               ar->v2d.scroll= V2D_SCROLL_RIGHT;
-                               /* button UI region */
-                               ar= MEM_callocN(sizeof(ARegion), "area region from do_versions");
-                               BLI_addtail(lb, ar);
-                               ar->regiontype= RGN_TYPE_UI;
                                ar->alignment= RGN_ALIGN_TOP;
                                break;
+#endif
                }
        }
 
@@ -5613,7 +5730,6 @@ static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
                        {
                                SpaceButs *sbuts= (SpaceButs *)sl;
                                memcpy(&ar->v2d, &sbuts->v2d, sizeof(View2D));
-                               ar->v2d.keepzoom |= V2D_KEEPASPECT;
                                break;
                        }
                        case SPACE_FILE:
@@ -5673,6 +5789,63 @@ static void do_versions_windowmanager_2_50(bScreen *screen)
        }
 }
 
+static void versions_gpencil_add_main(ListBase *lb, ID *id, char *name)
+{
+       
+       BLI_addtail(lb, id);
+       id->us= 1;
+       id->flag= LIB_FAKEUSER;
+       *( (short *)id->name )= ID_GD;
+       
+       new_id(lb, id, name);
+       /* alphabetic insterion: is in new_id */
+       
+       if(G.f & G_DEBUG)
+               printf("Converted GPencil to ID: %s\n", id->name+2);
+}
+
+static void do_versions_gpencil_2_50(Main *main, bScreen *screen)
+{
+       ScrArea *sa;
+       SpaceLink *sl;
+       
+       /* add regions */
+       for(sa= screen->areabase.first; sa; sa= sa->next) {
+               for(sl= sa->spacedata.first; sl; sl= sl->next) {
+                       if (sl->spacetype==SPACE_VIEW3D) {
+                               View3D *v3d= (View3D*) sl;
+                               if(v3d->gpd) {
+                                       versions_gpencil_add_main(&main->gpencil, (ID *)v3d->gpd, "GPencil View3D");
+                                       v3d->gpd= NULL;
+                               }
+                       }
+                       else if (sl->spacetype==SPACE_NODE) {
+                               SpaceNode *snode= (SpaceNode *)sl;
+                               if(snode->gpd) {
+                                       versions_gpencil_add_main(&main->gpencil, (ID *)snode->gpd, "GPencil Node");
+                                       snode->gpd= NULL;
+                               }
+                       }
+                       else if (sl->spacetype==SPACE_SEQ) {
+                               SpaceSeq *sseq= (SpaceSeq *)sl;
+                               if(sseq->gpd) {
+                                       versions_gpencil_add_main(&main->gpencil, (ID *)sseq->gpd, "GPencil Node");
+                                       sseq->gpd= NULL;
+                               }
+                       }
+                       else if (sl->spacetype==SPACE_IMAGE) {
+                               SpaceImage *sima= (SpaceImage *)sl;
+                               if(sima->gpd) {
+                                       versions_gpencil_add_main(&main->gpencil, (ID *)sima->gpd, "GPencil Image");
+                                       sima->gpd= NULL;
+                               }
+                       }
+               }
+       }               
+}
+
+
+
 static void do_versions(FileData *fd, Library *lib, Main *main)
 {
        /* WATCH IT!!!: pointers from libdata have not been converted */
@@ -6650,26 +6823,26 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                        
                                                if(sbuts->mainb==BUTS_LAMP) {
                                                        sbuts->mainb= CONTEXT_SHADING;
-                                                       sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_LAMP;
+                                                       //sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_LAMP;
                                                }
                                                else if(sbuts->mainb==BUTS_MAT) {
                                                        sbuts->mainb= CONTEXT_SHADING;
-                                                       sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_MAT;
+                                                       //sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_MAT;
                                                }
                                                else if(sbuts->mainb==BUTS_TEX) {
                                                        sbuts->mainb= CONTEXT_SHADING;
-                                                       sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_TEX;
+                                                       //sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_TEX;
                                                }
                                                else if(sbuts->mainb==BUTS_ANIM) {
                                                        sbuts->mainb= CONTEXT_OBJECT;
                                                }
                                                else if(sbuts->mainb==BUTS_WORLD) {
                                                        sbuts->mainb= CONTEXT_SCENE;
-                                                       sbuts->tab[CONTEXT_SCENE]= TAB_SCENE_WORLD;
+                                                       //sbuts->tab[CONTEXT_SCENE]= TAB_SCENE_WORLD;
                                                }
                                                else if(sbuts->mainb==BUTS_RENDER) {
                                                        sbuts->mainb= CONTEXT_SCENE;
-                                                       sbuts->tab[CONTEXT_SCENE]= TAB_SCENE_RENDER;
+                                                       //sbuts->tab[CONTEXT_SCENE]= TAB_SCENE_RENDER;
                                                }
                                                else if(sbuts->mainb==BUTS_GAME) {
                                                        sbuts->mainb= CONTEXT_LOGIC;
@@ -6679,7 +6852,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                                }
                                                else if(sbuts->mainb==BUTS_RADIO) {
                                                        sbuts->mainb= CONTEXT_SHADING;
-                                                       sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_RAD;
+                                                       //sbuts->tab[CONTEXT_SHADING]= TAB_SHADING_RAD;
                                                }
                                                else if(sbuts->mainb==BUTS_CONSTRAINT) {
                                                        sbuts->mainb= CONTEXT_OBJECT;
@@ -6914,22 +7087,14 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
        
 
        if(main->versionfile <= 234) {
-               Scene *sce;
                World *wo;
                bScreen *sc;
-               int set_zbuf_sel=0;
                
                // force sumo engine to be active
                for (wo = main->world.first; wo; wo= wo->id.next) {
                        if(wo->physicsEngine==0) wo->physicsEngine = 2;
                }
                
-               for (sce= main->scene.first; sce; sce= sce->id.next) {
-                       if(sce->selectmode==0) {
-                               sce->selectmode= SCE_SELECT_VERTEX;
-                               set_zbuf_sel= 1;
-                       }
-               }
                for (sc= main->screen.first; sc; sc= sc->id.next) {
                        ScrArea *sa;
                        for (sa= sc->areabase.first; sa; sa= sa->next) {
@@ -6937,7 +7102,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                for (sl= sa->spacedata.first; sl; sl= sl->next) {
                                        if(sl->spacetype==SPACE_VIEW3D) {
                                                View3D *v3d= (View3D *)sl;
-                                               if(set_zbuf_sel) v3d->flag |= V3D_ZBUF_SELECT;
+                                               v3d->flag |= V3D_ZBUF_SELECT;
                                        }
                                        else if(sl->spacetype==SPACE_TEXT) {
                                                SpaceText *st= (SpaceText *)sl;
@@ -6972,16 +7137,10 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
        }
        if(main->versionfile <= 236) {
                Object *ob;
-               Scene *sce= main->scene.first;
                Camera *cam= main->camera.first;
                Material *ma;
                bScreen *sc;
 
-               while(sce) {
-                       if(sce->editbutsize==0.0) sce->editbutsize= 0.1f;
-                       
-                       sce= sce->id.next;
-               }
                while(cam) {
                        if(cam->ortho_scale==0.0) {
                                cam->ortho_scale= 256.0f/cam->lens;
@@ -8306,7 +8465,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
 
                                                part->draw_as = PART_DRAW_PATH;
                                                part->type = PART_HAIR;
-                                               psys->recalc |= PSYS_RECALC_HAIR;
+                                               psys->recalc |= PSYS_RECALC_REDO;
 
                                                part->normfac *= fac;
                                                part->randfac *= fac;
@@ -8607,15 +8766,6 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        }
                }
        }
-       /* autokey mode settings now used from scene, but need to be initialised off userprefs */
-       if (main->versionfile < 247 || (main->versionfile == 247 && main->subversionfile < 8)) {
-               Scene *sce;
-               
-               for (sce= main->scene.first; sce; sce= sce->id.next) {
-                       if (sce->autokey_mode == 0)
-                               sce->autokey_mode= U.autokey_mode;
-               }
-       }
 
        if (main->versionfile < 247 || (main->versionfile == 247 && main->subversionfile < 9)) {
                Lamp *la= main->lamp.first;
@@ -8736,6 +8886,106 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
        }
 
+       if (main->versionfile < 248 || (main->versionfile == 248 && main->subversionfile < 3)) {
+               Object *ob;
+
+               /* Adjustments needed after Bullets update */
+               for(ob = main->object.first; ob; ob= ob->id.next) {
+                       ob->damping *= 0.635f;
+                       ob->rdamping = 0.1 + (0.8f * ob->rdamping);
+               }
+       }
+       
+       if (main->versionfile < 248 || (main->versionfile == 248 && main->subversionfile < 4)) {
+               Scene *sce;
+               World *wrld;
+
+               /*  Dome (Fisheye) default parameters  */
+               for (sce= main->scene.first; sce; sce= sce->id.next) {
+                       sce->r.domeangle = 180;
+                       sce->r.domemode = 1;
+                       sce->r.domeres = 4;
+                       sce->r.domeresbuf = 1.0f;
+                       sce->r.dometilt = 0;
+               }
+               /* DBVT culling by default */
+               for(wrld=main->world.first; wrld; wrld= wrld->id.next) {
+                       wrld->mode |= WO_DBVT_CULLING;
+                       wrld->occlusionRes = 128;
+               }
+       }
+
+       if (main->versionfile < 248 || (main->versionfile == 248 && main->subversionfile < 5)) {
+               Object *ob;
+               World *wrld;
+               for(ob = main->object.first; ob; ob= ob->id.next) {
+                       ob->m_contactProcessingThreshold = 1.; //pad3 is used for m_contactProcessingThreshold
+                       if(ob->parent) {
+                               /* check if top parent has compound shape set and if yes, set this object
+                                  to compound shaper as well (was the behaviour before, now it's optional) */
+                               Object *parent= newlibadr(fd, lib, ob->parent);
+                               while (parent && parent->parent != NULL) {
+                                       parent = newlibadr(fd, lib, parent->parent);
+                               }
+                               if(parent) {
+                                       if (parent->gameflag & OB_CHILD)
+                                               ob->gameflag |= OB_CHILD;
+                               }
+                       }
+               }
+               for(wrld=main->world.first; wrld; wrld= wrld->id.next) {
+                       wrld->ticrate = 60;
+                       wrld->maxlogicstep = 5;
+                       wrld->physubstep = 1;
+                       wrld->maxphystep = 5;
+               }
+       }
+
+       if (main->versionfile < 249) {
+               Scene *sce;
+               for (sce= main->scene.first; sce; sce= sce->id.next)
+                       sce->r.renderer= 0;
+               
+       }
+       
+       // correct introduce of seed for wind force
+       if (main->versionfile < 249 && main->subversionfile < 1) {
+               Object *ob;
+               for(ob = main->object.first; ob; ob= ob->id.next) {
+                       if(ob->pd)
+                               ob->pd->seed = ((unsigned int)(ceil(PIL_check_seconds_timer()))+1) % 128;
+               }
+       
+       }
+
+       if (main->versionfile < 249 && main->subversionfile < 2) {
+               Scene *sce= main->scene.first;
+               Sequence *seq;
+               Editing *ed;
+               
+               while(sce) {
+                       ed= sce->ed;
+                       if(ed) {
+                               SEQP_BEGIN(ed, seq) {
+                                       if (seq->strip && seq->strip->proxy){
+                                               if (sce->r.size != 100.0) {
+                                                       seq->strip->proxy->size
+                                                               = sce->r.size;
+                                               } else {
+                                                       seq->strip->proxy->size
+                                                               = 25.0;
+                                               }
+                                               seq->strip->proxy->quality =90;
+                                       }
+                               }
+                               SEQ_END
+                       }
+                       
+                       sce= sce->id.next;
+               }
+
+       }
+
        if (main->versionfile < 250) {
                bScreen *screen;
                Scene *scene;
@@ -8743,22 +8993,24 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                Mesh *me;
                Scene *sce;
                Tex *tx;
+               ParticleSettings *part;
+               Object *ob;
+               PTCacheID *pid;
+               ListBase pidlist;
                
-               for(screen= main->screen.first; screen; screen= screen->id.next)
+               for(screen= main->screen.first; screen; screen= screen->id.next) {
                        do_versions_windowmanager_2_50(screen);
+                       do_versions_gpencil_2_50(main, screen);
+               }
                
                /* old Animation System (using IPO's) needs to be converted to the new Animato system 
                 * (NOTE: conversion code in blenkernel/intern/ipo.c for now)
                 */
                //do_versions_ipos_to_animato(main);
                
-               /* struct audio data moved to renderdata */
-               for(scene= main->scene.first; scene; scene= scene->id.next) {
+               /* toolsettings */
+               for(scene= main->scene.first; scene; scene= scene->id.next)
                        scene->r.audio = scene->audio;
-                       
-                       if(!scene->toolsettings->uv_selectmode)
-                               scene->toolsettings->uv_selectmode= UV_SELECT_VERTEX;
-               }
                
                /* shader, composit and texture node trees have id.name empty, put something in
                 * to have them show in RNA viewer and accessible otherwise.
@@ -8782,12 +9034,43 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                for(me= main->mesh.first; me; me= me->id.next) {
                        me->drawflag= ME_DRAWEDGES|ME_DRAWFACES|ME_DRAWCREASES;
                }
+
+               /* particle draw and render types */
+               for(part= main->particle.first; part; part= part->id.next) {
+                       if(part->draw_as) {
+                               if(part->draw_as == PART_DRAW_DOT) {
+                                       part->ren_as = PART_DRAW_HALO;
+                                       part->draw_as = PART_DRAW_REND;
+                               }
+                               else if(part->draw_as <= PART_DRAW_AXIS) {
+                                       part->ren_as = PART_DRAW_HALO;
+                               }
+                               else {
+                                       part->ren_as = part->draw_as;
+                                       part->draw_as = PART_DRAW_REND;
+                               }
+                       }
+                       part->path_end = 1.0f;
+               }
+               /* set old pointcaches to have disk cache flag */
+               for(ob = main->object.first; ob; ob= ob->id.next) {
+
+                       BKE_ptcache_ids_from_object(&pidlist, ob);
+
+                       for(pid=pidlist.first; pid; pid=pid->next)
+                               pid->cache->flag |= PTCACHE_DISK_CACHE;
+
+                       BLI_freelistN(&pidlist);
+               }
        }
 
        /* TODO: should be moved into one of the version blocks once this branch moves to trunk and we can
           bump the version (or sub-version.) */
        {
                Object *ob;
+               Material *ma;
+               Scene *sce;
+               ToolSettings *ts;
                int i;
 
                for(ob = main->object.first; ob; ob = ob->id.next) {
@@ -8797,7 +9080,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                void *olddata = ob->data;
                                ob->data = me;
 
-                               if(me && me->mr) {
+                               if(me && me->id.lib==NULL && me->mr) { /* XXX - library meshes crash on loading most yoFrankie levels, the multires pointer gets invalid -  Campbell */
                                        MultiresLevel *lvl;
                                        ModifierData *md;
                                        MultiresModifierData *mmd;
@@ -8841,7 +9124,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                        
                                        multires_load_old(dm, me->mr);
 
-                                       *MultiresDM_get_flags(dm) |= MULTIRES_DM_UPDATE_ALWAYS;
+                                       MultiresDM_mark_as_modified(dm);
                                        dm->release(dm);
                                        orig->release(orig);
 
@@ -8853,8 +9136,26 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                ob->data = olddata;
                        }
                }
+
+               for(ma = main->mat.first; ma; ma = ma->id.next) {
+                       if(ma->mode & MA_HALO) {
+                               ma->material_type= MA_TYPE_HALO;
+                               ma->mode &= ~MA_HALO;
+                       }
+               }
+
+               for(sce = main->scene.first; sce; sce = sce->id.next) {
+                       ts= sce->toolsettings;
+                       if(ts->normalsize == 0.0 || !ts->uv_selectmode || ts->vgroup_weight == 0.0) {
+                               ts->normalsize= 0.1f;
+                               ts->selectmode= SCE_SELECT_VERTEX;
+                               ts->autokey_mode= U.autokey_mode;
+                               ts->uv_selectmode= UV_SELECT_VERTEX;
+                               ts->vgroup_weight= 1.0f;
+                       }
+               }
        }
-                                      
+
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
        /* WATCH IT 2!: Userdef struct init has to be in src/usiblender.c! */
 
@@ -8902,7 +9203,10 @@ static BHead *read_userdef(BlendFileData *bfd, FileData *fd, BHead *bhead)
 
        bfd->user= read_struct(fd, bhead, "user def");
        bfd->user->themes.first= bfd->user->themes.last= NULL;
-
+       // XXX
+       bfd->user->uifonts.first= bfd->user->uifonts.last= NULL;
+       bfd->user->uistyles.first= bfd->user->uistyles.last= NULL;
+       
        bhead = blo_nextbhead(fd, bhead);
 
                /* read all attached data */
@@ -9190,9 +9494,10 @@ static void expand_animdata(FileData *fd, Main *mainvar, AnimData *adt)
        /* drivers - assume that these F-Curves have driver data to be in this list... */
        for (fcd= adt->drivers.first; fcd; fcd= fcd->next) {
                ChannelDriver *driver= fcd->driver;
+               DriverTarget *dtar;
                
-               expand_doit(fd, mainvar, driver->id);
-               expand_doit(fd, mainvar, driver->id2);
+               for (dtar= driver->targets.first; dtar; dtar= dtar->next)
+                       expand_doit(fd, mainvar, dtar->id);
        }
 }      
 
@@ -9202,6 +9507,8 @@ static void expand_particlesettings(FileData *fd, Main *mainvar, ParticleSetting
        expand_doit(fd, mainvar, part->dup_group);
        expand_doit(fd, mainvar, part->eff_group);
        expand_doit(fd, mainvar, part->bb_ob);
+       
+       expand_animdata(fd, mainvar, part->adt);
 }
 
 static void expand_group(FileData *fd, Main *mainvar, Group *group)
@@ -9497,6 +9804,12 @@ static void expand_constraints(FileData *fd, Main *mainvar, ListBase *lb)
                                expand_doit(fd, mainvar, data->tar);
                        }
                        break;
+               case CONSTRAINT_TYPE_SHRINKWRAP:
+                       {
+                               bShrinkwrapConstraint *data = (bShrinkwrapConstraint*)curcon->data;
+                               expand_doit(fd, mainvar, data->target);
+                       }
+                       break;
                default:
                        break;
                }
@@ -9671,6 +9984,10 @@ static void expand_object(FileData *fd, Main *mainvar, Object *ob)
                                expand_doit(fd, mainvar, eoa->me);
                        }
                }
+               else if(act->type==ACT_OBJECT) {
+                       bObjectActuator *oa= act->data;
+                       expand_doit(fd, mainvar, oa->reference);
+               }
                else if(act->type==ACT_SCENE) {
                        bSceneActuator *sa= act->data;
                        expand_doit(fd, mainvar, sa->camera);
@@ -9726,6 +10043,9 @@ static void expand_scene(FileData *fd, Main *mainvar, Scene *sce)
                expand_doit(fd, mainvar, srl->mat_override);
                expand_doit(fd, mainvar, srl->light_override);
        }
+
+       if(sce->r.dometext)
+               expand_doit(fd, mainvar, sce->r.dometext);
 }
 
 static void expand_camera(FileData *fd, Main *mainvar, Camera *ca)