Merging r39717 through r39983 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenloader / intern / readfile.c
index 664148157cbeb5435024350ff18e0e49e2803be9..b1e5f63e641efcd146ef833fe46b71b967f195fd 100644 (file)
@@ -90,6 +90,7 @@
 #include "DNA_space_types.h"
 #include "DNA_vfont_types.h"
 #include "DNA_world_types.h"
+#include "DNA_movieclip_types.h"
 
 #include "MEM_guardedalloc.h"
 
@@ -1036,6 +1037,8 @@ void blo_freefiledata(FileData *fd)
                        oldnewmap_free(fd->globmap);
                if (fd->imamap)
                        oldnewmap_free(fd->imamap);
+               if (fd->movieclipmap)
+                       oldnewmap_free(fd->movieclipmap);
                if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
                        oldnewmap_free(fd->libmap);
                if (fd->bheadmap)
@@ -1111,6 +1114,13 @@ static void *newimaadr(FileData *fd, void *adr)          /* used to restore image data a
        return NULL;
 }
 
+static void *newmclipadr(FileData *fd, void *adr)              /* used to restore movie clip data after undo */
+{
+       if(fd->movieclipmap && adr)
+               return oldnewmap_lookup_and_inc(fd->movieclipmap, adr);
+       return NULL;
+}
+
 
 static void *newlibadr(FileData *fd, void *lib, void *adr)             /* only lib data */
 {
@@ -1239,6 +1249,38 @@ void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
        }
 }
 
+void blo_make_movieclip_pointer_map(FileData *fd, Main *oldmain)
+{
+       MovieClip *clip= oldmain->movieclip.first;
+
+       fd->movieclipmap= oldnewmap_new();
+
+       for(;clip; clip= clip->id.next) {
+               if(clip->cache)
+                       oldnewmap_insert(fd->movieclipmap, clip->cache, clip->cache, 0);
+       }
+}
+
+/* set old main movie clips caches to zero if it has been restored */
+/* this works because freeing old main only happens after this call */
+void blo_end_movieclip_pointer_map(FileData *fd, Main *oldmain)
+{
+       OldNew *entry= fd->movieclipmap->entries;
+       MovieClip *clip= oldmain->movieclip.first;
+       int i;
+
+       /* used entries were restored, so we put them to zero */
+       for (i=0; i<fd->movieclipmap->nentries; i++, entry++) {
+               if (entry->nr>0)
+                               entry->newp= NULL;
+       }
+
+       for(;clip; clip= clip->id.next) {
+               clip->cache= newmclipadr(fd, clip->cache);
+       }
+}
+
+
 /* undo file support: add all library pointers in lookup */
 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
 {
@@ -4719,6 +4761,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
        if(sce->nodetree)
                direct_link_nodetree(fd, sce->nodetree);
        
+       sce->clip= newlibadr_us(fd, sce->id.lib, sce->clip);
 }
 
 /* ************ READ WM ***************** */
@@ -4873,6 +4916,7 @@ static void lib_link_screen(FileData *fd, Main *main)
 
                                                for(bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next) {
                                                        bgpic->ima= newlibadr_us(fd, sc->id.lib, bgpic->ima);
+                                                       bgpic->clip= newlibadr_us(fd, sc->id.lib, bgpic->clip);
                                                }
                                                if(v3d->localvd) {
                                                        v3d->localvd->camera= newlibadr(fd, sc->id.lib, v3d->localvd->camera);
@@ -5005,6 +5049,14 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                
                                                snode->linkdrag.first = snode->linkdrag.last = NULL;
                                        }
+                                       else if(sl->spacetype==SPACE_CLIP) {
+                                               SpaceClip *sclip= (SpaceClip *)sl;
+
+                                               sclip->clip= newlibadr_us(fd, sc->id.lib, sclip->clip);
+
+                                               sclip->scopes.track_preview = NULL;
+                                               sclip->scopes.ok = 0;
+                                       }
                                }
                                sa= sa->next;
                        }
@@ -5091,6 +5143,7 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                        
                                        for(bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next) {
                                                bgpic->ima= restore_pointer_by_name(newmain, (ID *)bgpic->ima, 1);
+                                               bgpic->clip= restore_pointer_by_name(newmain, (ID *)bgpic->clip, 1);
                                        }
                                        if(v3d->localvd) {
                                                /*Base *base;*/
@@ -5235,6 +5288,13 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                                snode->nodetree= restore_pointer_by_name(newmain, &snode->nodetree->id, 1);
                                        }
                                }
+                               else if(sl->spacetype==SPACE_CLIP) {
+                                       SpaceClip *sclip= (SpaceClip *)sl;
+
+                                       sclip->clip= restore_pointer_by_name(newmain, (ID *)sclip->clip, 1);
+
+                                       sclip->scopes.ok = 0;
+                               }
                        }
                        sa= sa->next;
                }
@@ -5713,6 +5773,51 @@ static void lib_link_group(FileData *fd, Main *main)
        }
 }
 
+/* ***************** READ MOVIECLIP *************** */
+
+static void direct_link_movieclip(FileData *fd, MovieClip *clip)
+{
+       MovieTracking *tracking= &clip->tracking;
+       MovieTrackingTrack *track;
+
+       if(fd->movieclipmap) clip->cache= newmclipadr(fd, clip->cache);
+       else clip->cache= NULL;
+
+       tracking->reconstruction.cameras= newdataadr(fd, tracking->reconstruction.cameras);
+
+       link_list(fd, &tracking->tracks);
+
+       track= tracking->tracks.first;
+       while(track) {
+               track->markers= newdataadr(fd, track->markers);
+
+               track= track->next;
+       }
+
+       clip->tracking.act_track= newdataadr(fd, clip->tracking.act_track);
+
+       clip->anim= NULL;
+       clip->tracking_context= NULL;
+
+       clip->tracking.stabilization.ok= 0;
+       clip->tracking.stabilization.scaleibuf= NULL;
+}
+
+static void lib_link_movieclip(FileData *fd, Main *main)
+{
+       MovieClip *clip;
+
+       clip= main->movieclip.first;
+       while(clip) {
+               if(clip->id.flag & LIB_NEEDLINK) {
+                       clip->gpd= newlibadr_us(fd, clip->id.lib, clip->gpd);
+
+                       clip->id.flag -= LIB_NEEDLINK;
+               }
+               clip= clip->id.next;
+       }
+}
+
 /* ************** GENERAL & MAIN ******************** */
 
 
@@ -5747,6 +5852,7 @@ static const char *dataname(short id_code)
                case ID_BR: return "Data from BR";
                case ID_PA: return "Data from PA";
                case ID_GD: return "Data from GD";
+               case ID_MC: return "Data from MC";
        }
        return "Data from Lib Block";
        
@@ -5916,6 +6022,9 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
                case ID_GD:
                        direct_link_gpencil(fd, (bGPdata *)id);
                        break;
+               case ID_MC:
+                       direct_link_movieclip(fd, (MovieClip *)id);
+                       break;
        }
        
        /*link direct data of ID properties*/
@@ -12008,7 +12117,58 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
        /* put compatibility code here until next subversion bump */
 
        {
+               bScreen *sc;
+               Camera *cam;
+               MovieClip *clip;
 
+               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) {
+                                       if(sl->spacetype==SPACE_VIEW3D) {
+                                               View3D *v3d= (View3D *)sl;
+
+                                               if(v3d->bundle_size==0.0f) {
+                                                       v3d->bundle_size= 0.1f;
+                                                       v3d->flag2 |= V3D_SHOW_RECONSTRUCTION;
+                                               }
+
+                                               if(v3d->bundle_drawtype==0)
+                                                       v3d->bundle_drawtype= OB_EMPTY_SPHERE;
+                                       }
+                                       else if(sl->spacetype==SPACE_CLIP) {
+                                               SpaceClip *sclip= (SpaceClip *)sl;
+                                               if(sclip->scopes.track_preview_height==0)
+                                                       sclip->scopes.track_preview_height= 120;
+                                       }
+                               }
+                       }
+               }
+
+               for (clip= main->movieclip.first; clip; clip= clip->id.next) {
+                       if(clip->aspx<1.0f) {
+                               clip->aspx= 1.0f;
+                               clip->aspy= 1.0f;
+                       }
+
+                       /* XXX: a bit hacky, probably include imbuf and use real constants are nicer */
+                       clip->proxy.build_tc_flags= 7;
+                       if(clip->proxy.build_size_flags==0)
+                               clip->proxy.build_size_flags= 1;
+
+                       if(clip->proxy.quality==0)
+                               clip->proxy.quality= 90;
+
+                       if(clip->tracking.camera.pixel_aspect<0.01f)
+                               clip->tracking.camera.pixel_aspect= 1.f;
+               }
+
+               for(cam= main->camera.first; cam; cam= cam->id.next) {
+                       if (cam->sensor_x < 0.01f) {
+                               cam->sensor_x = 32.f;
+                       }
+               }
        }
 
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
@@ -12055,6 +12215,7 @@ static void lib_link_all(FileData *fd, Main *main)
        lib_link_nodetree(fd, main);    /* has to be done after scene/materials, this will verify group nodes */
        lib_link_brush(fd, main);
        lib_link_particlesettings(fd, main);
+       lib_link_movieclip(fd, main);
 
        lib_link_mesh(fd, main);                /* as last: tpage images with users at zero */