Merging r39717 through r39983 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenloader / intern / readfile.c
index 2f4545da221143dace791c1b73ea5b2fccd2efc1..b1e5f63e641efcd146ef833fe46b71b967f195fd 100644 (file)
@@ -85,6 +85,7 @@
 #include "DNA_scene_types.h"
 #include "DNA_sequence_types.h"
 #include "DNA_smoke_types.h"
+#include "DNA_speaker_types.h"
 #include "DNA_sound_types.h"
 #include "DNA_space_types.h"
 #include "DNA_vfont_types.h"
 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
 #include "BKE_sound.h"
 
+#include "NOD_socket.h"
+
 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
 //XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
 //XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
@@ -2093,10 +2096,21 @@ static void lib_link_nodetree(FileData *fd, Main *main)
        }
 }
 
+static void lib_nodetree_init_types_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
+{
+       bNode *node;
+       
+       ntreeInitTypes(ntree);
+       
+       /* XXX could be replaced by do_versions for new nodes */
+       for (node=ntree->nodes.first; node; node=node->next)
+               node_verify_socket_templates(ntree, node);
+}
+
 /* updates group node socket own_index so that
  * external links to/from the group node are preserved.
  */
-static void lib_node_do_versions_group(bNode *gnode)
+static void lib_node_do_versions_group_indices(bNode *gnode)
 {
        bNodeTree *ngroup= (bNodeTree*)gnode->id;
        bNode *intnode;
@@ -2129,92 +2143,101 @@ static void lib_node_do_versions_group(bNode *gnode)
 }
 
 /* updates external links for all group nodes in a tree */
-static void lib_nodetree_do_versions_group(bNodeTree *ntree)
+static void lib_nodetree_do_versions_group_indices_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
 {
        bNode *node;
        
        for (node=ntree->nodes.first; node; node=node->next) {
                if (node->type==NODE_GROUP) {
                        bNodeTree *ngroup= (bNodeTree*)node->id;
-                       if (ngroup && (ngroup->flag & NTREE_DO_VERSIONS))
-                               lib_node_do_versions_group(node);
+                       if (ngroup && (ngroup->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE))
+                               lib_node_do_versions_group_indices(node);
                }
        }
 }
 
+/* make an update call for the tree */
+static void lib_nodetree_do_versions_update_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
+{
+       if (ntree->update)
+               ntreeUpdateTree(ntree);
+}
+
 /* verify types for nodes and groups, all data has to be read */
 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
 * typedefs*/
 static void lib_verify_nodetree(Main *main, int UNUSED(open))
 {
-       Scene *sce;
-       Material *ma;
-       Tex *tx;
        bNodeTree *ntree;
-       
+       int i;
+       bNodeTreeType *ntreetype;
+
        /* this crashes blender on undo/redo
                if(open==1) {
                        reinit_nodesystem();
                }*/
        
-       /* now create the own typeinfo structs an verify nodes */
-       /* here we still assume no groups in groups */
-       for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
-               ntreeVerifyTypes(ntree);                /* internal nodes, no groups! */
+       /* set node->typeinfo pointers */
+       for (i=0; i < NUM_NTREE_TYPES; ++i) {
+               ntreetype= ntreeGetType(i);
+               if (ntreetype && ntreetype->foreach_nodetree)
+                       ntreetype->foreach_nodetree(main, NULL, lib_nodetree_init_types_cb);
        }
+       for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
+               ntreeInitTypes(ntree);
        
        {
-               /*int has_old_groups=0;*/ /*UNUSED*/
+               int has_old_groups=0;
                /* XXX this should actually be part of do_versions, but since we need
                 * finished library linking, it is not possible there. Instead in do_versions
                 * we have set the NTREE_DO_VERSIONS flag, so at this point we can do the
                 * actual group node updates.
                 */
                for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
-                       if (ntree->flag & NTREE_DO_VERSIONS) {
+                       if (ntree->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE) {
                                /* this adds copies and links from all unlinked internal sockets to group inputs/outputs. */
-                               nodeGroupExposeAllSockets(ntree);
-                               /*has_old_groups = 1;*/ /*UNUSED*/
+                               node_group_expose_all_sockets(ntree);
+                               has_old_groups = 1;
                        }
                }
-               /* now verify all types in material trees, groups are set OK now */
-               for(ma= main->mat.first; ma; ma= ma->id.next) {
-                       if(ma->nodetree)
-                               lib_nodetree_do_versions_group(ma->nodetree);
-               }
-               /* and scene trees */
-               for(sce= main->scene.first; sce; sce= sce->id.next) {
-                       if(sce->nodetree)
-                               lib_nodetree_do_versions_group(sce->nodetree);
-               }
-               /* and texture trees */
-               for(tx= main->tex.first; tx; tx= tx->id.next) {
-                       if(tx->nodetree)
-                               lib_nodetree_do_versions_group(tx->nodetree);
+               
+               if (has_old_groups) {
+                       for (i=0; i < NUM_NTREE_TYPES; ++i) {
+                               ntreetype= ntreeGetType(i);
+                               if (ntreetype && ntreetype->foreach_nodetree)
+                                       ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_group_indices_cb);
+                       }
                }
                
                for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
-                       ntree->flag &= ~NTREE_DO_VERSIONS;
-       }
-
-       /* now verify all types in material trees, groups are set OK now */
-       for(ma= main->mat.first; ma; ma= ma->id.next) {
-               if(ma->nodetree)
-                       ntreeVerifyTypes(ma->nodetree);
+                       ntree->flag &= ~NTREE_DO_VERSIONS_GROUP_EXPOSE;
        }
-       /* and scene trees */
-       for(sce= main->scene.first; sce; sce= sce->id.next) {
-               if(sce->nodetree)
-                       ntreeVerifyTypes(sce->nodetree);
+       
+       /* verify all group user nodes */
+       for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
+               ntreeVerifyNodes(main, &ntree->id);
        }
-       /* and texture trees */
-       for(tx= main->tex.first; tx; tx= tx->id.next) {
-               if(tx->nodetree)
-                       ntreeVerifyTypes(tx->nodetree);
+       
+       /* make update calls where necessary */
+       {
+               for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
+                       if (ntree->update)
+                               ntreeUpdateTree(ntree);
+               for (i=0; i < NUM_NTREE_TYPES; ++i) {
+                       ntreetype= ntreeGetType(i);
+                       if (ntreetype && ntreetype->foreach_nodetree)
+                               ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_update_cb);
+               }
        }
 }
 
-
+static void direct_link_node_socket(FileData *fd, bNodeSocket *sock)
+{
+       sock->link= newdataadr(fd, sock->link);
+       sock->storage= newdataadr(fd, sock->storage);
+       sock->default_value= newdataadr(fd, sock->default_value);
+       sock->cache= NULL;
+}
 
 /* ntree itself has been read! */
 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
@@ -2226,6 +2249,7 @@ static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
        
        ntree->init= 0;         /* to set callbacks and force setting types */
        ntree->progress= NULL;
+       ntree->execdata= NULL;
        
        ntree->adt= newdataadr(fd, ntree->adt);
        direct_link_animdata(fd, ntree->adt);
@@ -2235,12 +2259,15 @@ static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
                if(node->type == NODE_DYNAMIC) {
                        node->custom1= 0;
                        node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
-                       node->typeinfo= NULL;
                }
+
+               node->typeinfo= NULL;
+               
+               link_list(fd, &node->inputs);
+               link_list(fd, &node->outputs);
                
                node->storage= newdataadr(fd, node->storage);
                if(node->storage) {
-                       
                        /* could be handlerized at some point */
                        if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
                                direct_link_curvemapping(fd, node->storage);
@@ -2257,8 +2284,6 @@ static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
                                        ((ImageUser *)node->storage)->ok= 1;
                        }
                }
-               link_list(fd, &node->inputs);
-               link_list(fd, &node->outputs);
        }
        link_list(fd, &ntree->links);
        
@@ -2268,15 +2293,19 @@ static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
        
        /* and we connect the rest */
        for(node= ntree->nodes.first; node; node= node->next) {
+               node->parent = newdataadr(fd, node->parent);
                node->preview= newimaadr(fd, node->preview);
                node->lasty= 0;
+               
                for(sock= node->inputs.first; sock; sock= sock->next)
-                       sock->link= newdataadr(fd, sock->link);
+                       direct_link_node_socket(fd, sock);
                for(sock= node->outputs.first; sock; sock= sock->next)
-                       sock->ns.data= NULL;
+                       direct_link_node_socket(fd, sock);
        }
+       for(sock= ntree->inputs.first; sock; sock= sock->next)
+               direct_link_node_socket(fd, sock);
        for(sock= ntree->outputs.first; sock; sock= sock->next)
-               sock->link= newdataadr(fd, sock->link);
+               direct_link_node_socket(fd, sock);
        
        for(link= ntree->links.first; link; link= link->next) {
                link->fromnode= newdataadr(fd, link->fromnode);
@@ -4514,7 +4543,7 @@ static void lib_link_scene(FileData *fd, Main *main)
 #endif
 
                        if(sce->ed)
-                               seq_update_muting(sce, sce->ed);
+                               seq_update_muting(sce->ed);
                        
                        if(sce->nodetree) {
                                lib_link_ntree(fd, &sce->id, sce->nodetree);
@@ -4769,6 +4798,8 @@ static void direct_link_windowmanager(FileData *fd, wmWindowManager *wm)
 
        wm->keyconfigs.first= wm->keyconfigs.last= NULL;
        wm->defaultconf= NULL;
+       wm->addonconf= NULL;
+       wm->userconf= NULL;
 
        wm->jobs.first= wm->jobs.last= NULL;
        wm->drags.first= wm->drags.last= NULL;
@@ -4998,15 +5029,22 @@ static void lib_link_screen(FileData *fd, Main *main)
                                                SpaceNode *snode= (SpaceNode *)sl;
                                                
                                                snode->id= newlibadr(fd, sc->id.lib, snode->id);
+                                               snode->edittree= NULL;
                                                
-                                               /* internal data, a bit patchy */
-                                               if(snode->id) {
-                                                       if(GS(snode->id->name)==ID_MA)
-                                                               snode->nodetree= ((Material *)snode->id)->nodetree;
-                                                       else if(GS(snode->id->name)==ID_SCE)
-                                                               snode->nodetree= ((Scene *)snode->id)->nodetree;
-                                                       else if(GS(snode->id->name)==ID_TE)
-                                                               snode->nodetree= ((Tex *)snode->id)->nodetree;
+                                               if (ELEM3(snode->treetype, NTREE_COMPOSIT, NTREE_SHADER, NTREE_TEXTURE)) {
+                                                       /* internal data, a bit patchy */
+                                                       snode->nodetree= NULL;
+                                                       if(snode->id) {
+                                                               if(GS(snode->id->name)==ID_MA)
+                                                                       snode->nodetree= ((Material *)snode->id)->nodetree;
+                                                               else if(GS(snode->id->name)==ID_SCE)
+                                                                       snode->nodetree= ((Scene *)snode->id)->nodetree;
+                                                               else if(GS(snode->id->name)==ID_TE)
+                                                                       snode->nodetree= ((Tex *)snode->id)->nodetree;
+                                                       }
+                                               }
+                                               else {
+                                                       snode->nodetree= newlibadr_us(fd, sc->id.lib, snode->nodetree);
                                                }
                                                
                                                snode->linkdrag.first = snode->linkdrag.last = NULL;
@@ -5235,15 +5273,19 @@ void lib_link_screen_restore(Main *newmain, bScreen *curscreen, Scene *curscene)
                                        snode->id= restore_pointer_by_name(newmain, snode->id, 1);
                                        snode->edittree= NULL;
                                        
-                                       if(snode->id==NULL)
+                                       if (ELEM3(snode->treetype, NTREE_COMPOSIT, NTREE_SHADER, NTREE_TEXTURE)) {
                                                snode->nodetree= NULL;
+                                               if(snode->id) {
+                                                       if(GS(snode->id->name)==ID_MA)
+                                                               snode->nodetree= ((Material *)snode->id)->nodetree;
+                                                       else if(GS(snode->id->name)==ID_SCE)
+                                                               snode->nodetree= ((Scene *)snode->id)->nodetree;
+                                                       else if(GS(snode->id->name)==ID_TE)
+                                                               snode->nodetree= ((Tex *)snode->id)->nodetree;
+                                               }
+                                       }
                                        else {
-                                               if(GS(snode->id->name)==ID_MA)
-                                                       snode->nodetree= ((Material *)snode->id)->nodetree;
-                                               else if(GS(snode->id->name)==ID_SCE)
-                                                       snode->nodetree= ((Scene *)snode->id)->nodetree;
-                                               else if(GS(snode->id->name)==ID_TE)
-                                                       snode->nodetree= ((Tex *)snode->id)->nodetree;
+                                               snode->nodetree= restore_pointer_by_name(newmain, &snode->nodetree->id, 1);
                                        }
                                }
                                else if(sl->spacetype==SPACE_CLIP) {
@@ -5479,7 +5521,6 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                        snode->gpd= newdataadr(fd, snode->gpd);
                                        direct_link_gpencil(fd, snode->gpd);
                                }
-                               snode->nodetree= snode->edittree= NULL;
                        }
                        else if(sl->spacetype==SPACE_TIME) {
                                SpaceTime *stime= (SpaceTime *)sl;
@@ -5629,12 +5670,51 @@ static void fix_relpaths_library(const char *basepath, Main *main)
        }
 }
 
+/* ************ READ SPEAKER ***************** */
+
+static void lib_link_speaker(FileData *fd, Main *main)
+{
+       Speaker *spk;
+
+       spk= main->speaker.first;
+       while(spk) {
+               if(spk->id.flag & LIB_NEEDLINK) {
+                       if (spk->adt) lib_link_animdata(fd, &spk->id, spk->adt);
+
+                       spk->sound= newlibadr(fd, spk->id.lib, spk->sound);
+                       if (spk->sound) {
+                               spk->sound->id.us++;
+                       }
+
+                       spk->id.flag -= LIB_NEEDLINK;
+               }
+               spk= spk->id.next;
+       }
+}
+
+static void direct_link_speaker(FileData *fd, Speaker *spk)
+{
+       spk->adt= newdataadr(fd, spk->adt);
+       direct_link_animdata(fd, spk->adt);
+
+       /*spk->sound= newdataadr(fd, spk->sound);
+       direct_link_sound(fd, spk->sound);*/
+}
+
 /* ************** READ SOUND ******************* */
 
 static void direct_link_sound(FileData *fd, bSound *sound)
 {
        sound->handle = NULL;
        sound->playback_handle = NULL;
+       sound->waveform = NULL;
+
+       // versioning stuff, if there was a cache, then we enable caching:
+       if(sound->cache)
+       {
+               sound->flags |= SOUND_FLAGS_CACHING;
+               sound->cache = NULL;
+       }
 
        sound->packedfile = direct_link_packedfile(fd, sound->packedfile);
        sound->newpackedfile = direct_link_packedfile(fd, sound->newpackedfile);
@@ -5651,9 +5731,6 @@ static void lib_link_sound(FileData *fd, Main *main)
                        sound->ipo= newlibadr_us(fd, sound->id.lib, sound->ipo); // XXX depreceated - old animation system
                        
                        sound_load(main, sound);
-
-                       if(sound->cache)
-                               sound_cache(sound, 1);
                }
                sound= sound->id.next;
        }
@@ -5717,9 +5794,7 @@ static void direct_link_movieclip(FileData *fd, MovieClip *clip)
                track= track->next;
        }
 
-       clip->last_sel= newdataadr(fd, clip->last_sel);
-       if(clip->last_sel==NULL)
-               clip->sel_type= MCLIP_SEL_NONE;
+       clip->tracking.act_track= newdataadr(fd, clip->tracking.act_track);
 
        clip->anim= NULL;
        clip->tracking_context= NULL;
@@ -5728,13 +5803,15 @@ static void direct_link_movieclip(FileData *fd, MovieClip *clip)
        clip->tracking.stabilization.scaleibuf= NULL;
 }
 
-static void lib_link_movieclip(FileData *UNUSED(fd), Main *main)
+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;
@@ -5769,6 +5846,7 @@ static const char *dataname(short id_code)
                case ID_SCR: return "Data from SCR";
                case ID_VF: return "Data from VF";
                case ID_TXT     : return "Data from TXT";
+               case ID_SPK: return "Data from SPK";
                case ID_SO: return "Data from SO";
                case ID_NT: return "Data from NT";
                case ID_BR: return "Data from BR";
@@ -5914,6 +5992,9 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
                case ID_CA:
                        direct_link_camera(fd, (Camera *)id);
                        break;
+               case ID_SPK:
+                       direct_link_speaker(fd, (Speaker *)id);
+                       break;
                case ID_SO:
                        direct_link_sound(fd, (bSound *)id);
                        break;
@@ -6997,6 +7078,62 @@ static void do_version_bone_roll_256(Bone *bone)
                do_version_bone_roll_256(child);
 }
 
+static void do_versions_socket_default_value(bNodeSocket *sock)
+{
+       bNodeSocketValueFloat *valfloat;
+       bNodeSocketValueVector *valvector;
+       bNodeSocketValueRGBA *valrgba;
+       
+       if (sock->default_value)
+               return;
+       
+       switch (sock->type) {
+       case SOCK_FLOAT:
+               valfloat = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueFloat), "default socket value");
+               valfloat->value = sock->ns.vec[0];
+               valfloat->min = sock->ns.min;
+               valfloat->max = sock->ns.max;
+               valfloat->subtype = PROP_NONE;
+               break;
+       case SOCK_VECTOR:
+               valvector = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueVector), "default socket value");
+               copy_v3_v3(valvector->value, sock->ns.vec);
+               valvector->min = sock->ns.min;
+               valvector->max = sock->ns.max;
+               valvector->subtype = PROP_NONE;
+               break;
+       case SOCK_RGBA:
+               valrgba = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueRGBA), "default socket value");
+               copy_v4_v4(valrgba->value, sock->ns.vec);
+               break;
+       }
+}
+
+static void do_versions_nodetree_default_value(bNodeTree *ntree)
+{
+       bNode *node;
+       bNodeSocket *sock;
+       for (node=ntree->nodes.first; node; node=node->next) {
+               for (sock=node->inputs.first; sock; sock=sock->next)
+                       do_versions_socket_default_value(sock);
+               for (sock=node->outputs.first; sock; sock=sock->next)
+                       do_versions_socket_default_value(sock);
+       }
+       for (sock=ntree->inputs.first; sock; sock=sock->next)
+               do_versions_socket_default_value(sock);
+       for (sock=ntree->outputs.first; sock; sock=sock->next)
+               do_versions_socket_default_value(sock);
+}
+
+static void do_versions_nodetree_dynamic_sockets(bNodeTree *ntree)
+{
+       bNodeSocket *sock;
+       for (sock=ntree->inputs.first; sock; sock=sock->next)
+               sock->flag |= SOCK_DYNAMIC;
+       for (sock=ntree->outputs.first; sock; sock=sock->next)
+               sock->flag |= SOCK_DYNAMIC;
+}
+
 static void do_versions(FileData *fd, Library *lib, Main *main)
 {
        /* WATCH IT!!!: pointers from libdata have not been converted */
@@ -10058,12 +10195,6 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        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;
-                                               }
                                                seq->strip->proxy->quality =90;
                                        }
                                }
@@ -10153,7 +10284,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                           !(seq->flag & SEQ_USE_PROXY_CUSTOM_DIR))
                                        {
                                                
-                                               snprintf(seq->strip->proxy->dir, 
+                                               BLI_snprintf(seq->strip->proxy->dir, 
                                                         FILE_MAXDIR, "%s/BL_proxy", 
                                                         seq->strip->dir);
                                        }
@@ -10170,7 +10301,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                 * 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)
+                       if(ma->nodetree && ma->nodetree->id.name[0] == '\0')
                                strcpy(ma->nodetree->id.name, "NTShader Nodetree");
                        
                        /* which_output 0 is now "not specified" */
@@ -10184,7 +10315,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
                /* and composit trees */
                for(sce= main->scene.first; sce; sce= sce->id.next) {
-                       if(sce->nodetree && strlen(sce->nodetree->id.name)==0)
+                       if(sce->nodetree && sce->nodetree->id.name[0] == '\0')
                                strcpy(sce->nodetree->id.name, "NTCompositing Nodetree");
 
                        /* move to cameras */
@@ -10206,7 +10337,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        bNode *node;
 
                        if(tx->nodetree) {
-                               if(strlen(tx->nodetree->id.name)==0)
+                               if(tx->nodetree->id.name[0] == '\0')
                                        strcpy(tx->nodetree->id.name, "NTTexture Nodetree");
 
                                /* which_output 0 is now "not specified" */
@@ -11654,7 +11785,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                         * is done in lib_verify_nodetree, because at this point the internal
                         * nodes may not be up-to-date! (missing lib-link)
                         */
-                       ntree->flag |= NTREE_DO_VERSIONS;
+                       ntree->flag |= NTREE_DO_VERSIONS_GROUP_EXPOSE;
                }
        }
 
@@ -11777,12 +11908,12 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        Tex *tex;
                        for(tex= main->tex.first; tex; tex= tex->id.next) {
                                if(tex->pd) {
-                                       if (tex->pd->falloff_speed_scale == 0.0)
-                                               tex->pd->falloff_speed_scale = 100.0;
-
+                                       if (tex->pd->falloff_speed_scale == 0.0f)
+                                               tex->pd->falloff_speed_scale = 100.0f;
+                                       
                                        if (!tex->pd->falloff_curve) {
                                                tex->pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
-
+                                               
                                                tex->pd->falloff_curve->preset = CURVE_PRESET_LINE;
                                                tex->pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
                                                curvemap_reset(tex->pd->falloff_curve->cm, &tex->pd->falloff_curve->clipr, tex->pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
@@ -11815,7 +11946,174 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        }
                }
        }
-       
+
+       if (main->versionfile < 259 || (main->versionfile == 259 && main->subversionfile < 1)){
+               {
+                       Scene *scene;
+                       Sequence *seq;
+
+                       for (scene=main->scene.first; scene; scene=scene->id.next)
+                       {
+                               scene->r.ffcodecdata.audio_channels = 2;
+                               scene->audio.volume = 1.0f;
+                               SEQ_BEGIN(scene->ed, seq) {
+                                       seq->pitch = 1.0f;
+                               }
+                               SEQ_END
+                       }
+               }
+               {
+                       bScreen *screen;
+                       for(screen= main->screen.first; screen; screen= screen->id.next) {
+                               ScrArea *sa;
+                               /* add regions */
+                               for(sa= screen->areabase.first; sa; sa= sa->next) {
+                                       SpaceLink *sl= sa->spacedata.first;
+                                       if(sl->spacetype==SPACE_SEQ) {
+                                               ARegion *ar;
+                                               for (ar=sa->regionbase.first; ar; ar= ar->next) {
+                                                       if(ar->regiontype == RGN_TYPE_WINDOW) {
+                                                               if(ar->v2d.min[1] == 4.0f)
+                                                                       ar->v2d.min[1]= 0.5f;
+                                                       }
+                                               }
+                                       }
+                                       for (sl= sa->spacedata.first; sl; sl= sl->next) {
+                                               if(sl->spacetype==SPACE_SEQ) {
+                                                       ARegion *ar;
+                                                       for (ar=sl->regionbase.first; ar; ar= ar->next) {
+                                                               if(ar->regiontype == RGN_TYPE_WINDOW) {
+                                                                       if(ar->v2d.min[1] == 4.0f)
+                                                                               ar->v2d.min[1]= 0.5f;
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+               {
+                       /* Make "auto-clamped" handles a per-keyframe setting instead of per-FCurve 
+                        *
+                        * We're only patching F-Curves in Actions here, since it is assumed that most
+                        * drivers out there won't be using this (and if they are, they're in the minority).
+                        * While we should aim to fix everything ideally, in practice it's far too hard
+                        * to get to every animdata block, not to mention the performance hit that'd have
+                        */
+                       bAction *act;
+                       FCurve *fcu;
+                       
+                       for (act = main->action.first; act; act = act->id.next) {
+                               for (fcu = act->curves.first; fcu; fcu = fcu->next) {
+                                       BezTriple *bezt;
+                                       unsigned int i = 0;
+                                       
+                                       /* only need to touch curves that had this flag set */
+                                       if ((fcu->flag & FCURVE_AUTO_HANDLES) == 0)
+                                               continue;
+                                       if ((fcu->totvert == 0) || (fcu->bezt == NULL))
+                                               continue;
+                                               
+                                       /* only change auto-handles to auto-clamped */
+                                       for (bezt=fcu->bezt; i < fcu->totvert; i++, bezt++) {
+                                               if (bezt->h1 == HD_AUTO) bezt->h1 = HD_AUTO_ANIM;
+                                               if (bezt->h2 == HD_AUTO) bezt->h2 = HD_AUTO_ANIM;
+                                       }
+                                       
+                                       fcu->flag &= ~FCURVE_AUTO_HANDLES;
+                               }
+                       }
+               }
+               {
+                       /* convert fcurve and shape action actuators to action actuators */
+                       Object *ob;
+                       bActuator *act;
+                       bIpoActuator *ia;
+                       bActionActuator *aa;
+
+                       for (ob= main->object.first; ob; ob= ob->id.next) {
+                               for (act= ob->actuators.first; act; act= act->next) {
+                                       if (act->type == ACT_IPO) {
+                                               // Create the new actuator
+                                               ia= act->data;
+                                               aa= MEM_callocN(sizeof(bActionActuator), "fcurve -> action actuator do_version");
+
+                                               // Copy values
+                                               aa->type = ia->type;
+                                               aa->flag = ia->flag;
+                                               aa->sta = ia->sta;
+                                               aa->end = ia->end;
+                                               strcpy(aa->name, ia->name);
+                                               strcpy(aa->frameProp, ia->frameProp);
+                                               if (ob->adt)
+                                                       aa->act = ob->adt->action;
+
+                                               // Get rid of the old actuator
+                                               MEM_freeN(ia);
+
+                                               // Assign the new actuator
+                                               act->data = aa;
+                                               act->type= act->otype= ACT_ACTION;
+                                               
+                                       }
+                                       else if (act->type == ACT_SHAPEACTION)  {
+                                               act->type = act->otype = ACT_ACTION;
+                                       }
+                               }
+                       }
+               }
+       }
+
+       if (main->versionfile < 259 || (main->versionfile == 259 && main->subversionfile < 2)){
+               {
+                       /* Convert default socket values from bNodeStack */
+                       Scene *sce;
+                       Material *mat;
+                       Tex *tex;
+                       bNodeTree *ntree;
+                       for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next) {
+                               do_versions_nodetree_default_value(ntree);
+                               ntree->update |= NTREE_UPDATE;
+                       }
+                       for (sce=main->scene.first; sce; sce=sce->id.next)
+                               if (sce->nodetree) {
+                               do_versions_nodetree_default_value(sce->nodetree);
+                               sce->nodetree->update |= NTREE_UPDATE;
+                       }
+                       for (mat=main->mat.first; mat; mat=mat->id.next)
+                               if (mat->nodetree) {
+                               do_versions_nodetree_default_value(mat->nodetree);
+                               mat->nodetree->update |= NTREE_UPDATE;
+                       }
+                       for (tex=main->tex.first; tex; tex=tex->id.next)
+                               if (tex->nodetree) {
+                               do_versions_nodetree_default_value(tex->nodetree);
+                               tex->nodetree->update |= NTREE_UPDATE;
+                       }
+               }
+
+               /* add SOCK_DYNAMIC flag to existing group sockets */
+               {
+                       bNodeTree *ntree;
+                       /* only need to do this for trees in main, local trees are not used as groups */
+                       for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next) {
+                               do_versions_nodetree_dynamic_sockets(ntree);
+                               ntree->update |= NTREE_UPDATE;
+                       }
+               }
+
+               {
+                       /* Initialize group tree nodetypes.
+                        * These are used to distinguish tree types and
+                        * associate them with specific node types for polling.
+                        */
+                       bNodeTree *ntree;
+                       /* all node trees in main->nodetree are considered groups */
+                       for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next)
+                               ntree->nodetype = NODE_GROUP;
+               }
+       }
+
        /* put compatibility code here until next subversion bump */
 
        {
@@ -11853,12 +12151,22 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                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;
-                               cam->sensor_y = 18.f;
                        }
                }
        }
@@ -11898,6 +12206,7 @@ static void lib_link_all(FileData *fd, Main *main)
        lib_link_latt(fd, main);
        lib_link_text(fd, main);
        lib_link_camera(fd, main);
+       lib_link_speaker(fd, main);
        lib_link_sound(fd, main);
        lib_link_group(fd, main);
        lib_link_armature(fd, main);
@@ -11913,33 +12222,57 @@ static void lib_link_all(FileData *fd, Main *main)
        lib_link_library(fd, main);             /* only init users */
 }
 
+static void direct_link_keymapitem(FileData *fd, wmKeyMapItem *kmi)
+{
+       kmi->properties= newdataadr(fd, kmi->properties);
+       if(kmi->properties)
+               IDP_DirectLinkProperty(kmi->properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
+       kmi->ptr= NULL;
+       kmi->flag &= ~KMI_UPDATE;
+}
 
 static BHead *read_userdef(BlendFileData *bfd, FileData *fd, BHead *bhead)
 {
        UserDef *user;
        wmKeyMap *keymap;
        wmKeyMapItem *kmi;
+       wmKeyMapDiffItem *kmdi;
 
        bfd->user= user= read_struct(fd, bhead, "user def");
 
        /* read all data into fd->datamap */
        bhead= read_data_into_oldnewmap(fd, bhead, "user def");
 
+       if(user->keymaps.first) {
+               /* backwards compatibility */
+               user->user_keymaps= user->keymaps;
+               user->keymaps.first= user->keymaps.last= NULL;
+       }
+
        link_list(fd, &user->themes);
-       link_list(fd, &user->keymaps);
+       link_list(fd, &user->user_keymaps);
        link_list(fd, &user->addons);
 
-       for(keymap=user->keymaps.first; keymap; keymap=keymap->next) {
+       for(keymap=user->user_keymaps.first; keymap; keymap=keymap->next) {
                keymap->modal_items= NULL;
                keymap->poll= NULL;
+               keymap->flag &= ~KEYMAP_UPDATE;
 
+               link_list(fd, &keymap->diff_items);
                link_list(fd, &keymap->items);
-               for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
-                       kmi->properties= newdataadr(fd, kmi->properties);
-                       if(kmi->properties)
-                               IDP_DirectLinkProperty(kmi->properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
-                       kmi->ptr= NULL;
+               
+               for(kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next) {
+                       kmdi->remove_item= newdataadr(fd, kmdi->remove_item);
+                       kmdi->add_item= newdataadr(fd, kmdi->add_item);
+
+                       if(kmdi->remove_item)
+                               direct_link_keymapitem(fd, kmdi->remove_item);
+                       if(kmdi->add_item)
+                               direct_link_keymapitem(fd, kmdi->add_item);
                }
+
+               for(kmi=keymap->items.first; kmi; kmi=kmi->next)
+                       direct_link_keymapitem(fd, kmi);
        }
 
        // XXX
@@ -12815,6 +13148,14 @@ static void expand_camera(FileData *fd, Main *mainvar, Camera *ca)
                expand_animdata(fd, mainvar, ca->adt);
 }
 
+static void expand_speaker(FileData *fd, Main *mainvar, Speaker *spk)
+{
+       expand_doit(fd, mainvar, spk->sound);
+
+       if (spk->adt)
+               expand_animdata(fd, mainvar, spk->adt);
+}
+
 static void expand_sound(FileData *fd, Main *mainvar, bSound *snd)
 {
        expand_doit(fd, mainvar, snd->ipo); // XXX depreceated - old animation system
@@ -12877,6 +13218,9 @@ static void expand_main(FileData *fd, Main *mainvar)
                                        case ID_CA:
                                                expand_camera(fd, mainvar, (Camera *)id);
                                                break;
+                                       case ID_SPK:
+                                               expand_speaker(fd, mainvar,(Speaker *)id);
+                                               break;
                                        case ID_SO:
                                                expand_sound(fd, mainvar, (bSound *)id);
                                                break;
@@ -13126,9 +13470,8 @@ static void append_id_part(FileData *fd, Main *mainvar, ID *id, ID **id_r)
 
 /* common routine to append/link something from a library */
 
-static Main* library_append_begin(const bContext *C, FileData **fd, const char *filepath)
+static Main* library_append_begin(Main *mainvar, FileData **fd, const char *filepath)
 {
-       Main *mainvar= CTX_data_main(C);
        Main *mainl;
 
        /* make mains */
@@ -13144,64 +13487,17 @@ static Main* library_append_begin(const bContext *C, FileData **fd, const char *
        return mainl;
 }
 
-Main* BLO_library_append_begin(const bContext *C, BlendHandle** bh, const char *filepath)
+Main* BLO_library_append_begin(Main *mainvar, BlendHandle** bh, const char *filepath)
 {
        FileData *fd= (FileData*)(*bh);
-       return library_append_begin(C, &fd, filepath);
+       return library_append_begin(mainvar, &fd, filepath);
 }
 
-static void append_do_cursor(Scene *scene, Library *curlib, short flag)
-{
-       Base *centerbase;
-       Object *ob;
-       float *curs, centerloc[3], vec[3], min[3], max[3];
-       int count= 0;
-
-       /* when not linking (appending)... */
-       if(flag & FILE_LINK) 
-               return;
-
-       /* we're not appending at cursor */
-       if((flag & FILE_ATCURSOR) == 0) 
-               return;
-       
-       /* find the center of everything appended */
-       INIT_MINMAX(min, max);
-       centerbase= (scene->base.first);
-       while(centerbase) {
-               if(centerbase->object->id.lib==curlib && centerbase->object->parent==NULL) {
-                       VECCOPY(vec, centerbase->object->loc);
-                       DO_MINMAX(vec, min, max);
-                       count++;
-               }
-               centerbase= centerbase->next;
-       }
-       /* we haven't found any objects to move to cursor */
-       if(!count) 
-               return;
-       
-       /* move from the center of the appended objects to cursor */
-       mid_v3_v3v3(centerloc, min, max);
-       curs = scene->cursor;
-       VECSUB(centerloc,curs,centerloc);
-       
-       /* now translate the center of the objects */
-       centerbase= (scene->base.first);
-       while(centerbase) {
-               if(centerbase->object->id.lib==curlib && centerbase->object->parent==NULL) {
-                       ob= centerbase->object;
-                       ob->loc[0] += centerloc[0];
-                       ob->loc[1] += centerloc[1];
-                       ob->loc[2] += centerloc[2];
-               }
-               centerbase= centerbase->next;
-       }
-}
 
+/* Context == NULL signifies not to do any scene manipulation */
 static void library_append_end(const bContext *C, Main *mainl, FileData **fd, int idcode, short flag)
 {
        Main *mainvar;
-       Scene *scene= CTX_data_scene(C);
        Library *curlib;
 
        /* make main consistent */
@@ -13230,22 +13526,26 @@ static void library_append_end(const bContext *C, Main *mainl, FileData **fd, in
        lib_verify_nodetree(mainvar, FALSE);
        fix_relpaths_library(G.main->name, 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(scene) {
-               const short is_link= (flag & FILE_LINK) != 0;
-               if(idcode==ID_SCE) {
-                       /* dont instance anything when linking in scenes, assume the scene its self instances the data */
-               }
-               else {
-                       give_base_to_objects(mainvar, scene, curlib, idcode, is_link);
+       if(C) {
+               Scene *scene= CTX_data_scene(C);
 
-                       if (flag & FILE_GROUP_INSTANCE) {
-                               give_base_to_groups(mainvar, scene);
+               /* give a base to loose objects. If group append, do it for objects too */
+               if(scene) {
+                       const short is_link= (flag & FILE_LINK) != 0;
+                       if(idcode==ID_SCE) {
+                               /* dont instance anything when linking in scenes, assume the scene its self instances the data */
+                       }
+                       else {
+                               give_base_to_objects(mainvar, scene, curlib, idcode, is_link);
+
+                               if (flag & FILE_GROUP_INSTANCE) {
+                                       give_base_to_groups(mainvar, scene);
+                               }
                        }
                }
-       }
-       else {
-               printf("library_append_end, scene is NULL (objects wont get bases)\n");
+               else {
+                       printf("library_append_end, scene is NULL (objects wont get bases)\n");
+               }
        }
        /* has been removed... erm, why? s..ton) */
        /* 20040907: looks like they are give base already in append_named_part(); -Nathan L */
@@ -13256,8 +13556,6 @@ static void library_append_end(const bContext *C, Main *mainl, FileData **fd, in
                blo_freefiledata( *fd );
                *fd = NULL;
        }       
-
-       append_do_cursor(scene, curlib, flag);
 }
 
 void BLO_library_append_end(const bContext *C, struct Main *mainl, BlendHandle** bh, int idcode, short flag)