== Constraints System ==
[blender.git] / source / blender / blenloader / intern / readfile.c
index 6ef6f18caeebe570a76d8c5ddf579a2f114fb34e..c140ebb804eed205c68e163e192b1e6e2c50aebf 100644 (file)
@@ -554,8 +554,7 @@ static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
                }
 
                /* this patch is to avoid a long long being read from not-eight aligned positions
-                  is necessary on SGI with -n32 compiling (no, is necessary on
-                  any modern 64bit architecture) */
+                  is necessary on any modern 64bit architecture) */
                memcpy(&old, &bhead8->old, 8);
                bhead4->old = (int) (old >> 3);
 
@@ -794,8 +793,12 @@ static int read_file_dna(FileData *fd)
                        int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
 
                        fd->filesdna= dna_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
-                       if (fd->filesdna)
+                       if (fd->filesdna) {
+                               
                                fd->compflags= dna_get_structDNA_compareflags(fd->filesdna, fd->memsdna);
+                               /* used to retrieve ID names from (bhead+1) */
+                               fd->id_name_offs= dna_elem_offset(fd->filesdna, "ID", "char", "name[]");
+                       }
 
                        return 1;
                } else if (bhead->code==ENDB)
@@ -1183,7 +1186,7 @@ static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
        int blocksize, nblocks;
        char *data;
 
-       data= (char *)(bhead+1); /*  BHEAD+DATA dependancy */
+       data= (char *)(bhead+1);
        blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
 
        nblocks= bhead->nr;
@@ -1199,6 +1202,7 @@ static void *read_struct(FileData *fd, BHead *bh, char *blockname)
        void *temp= NULL;
 
        if (bh->len) {
+               /* switch is based on file dna */
                if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
                        switch_endian_structs(fd->filesdna, bh);
 
@@ -1207,7 +1211,7 @@ static void *read_struct(FileData *fd, BHead *bh, char *blockname)
                                temp= dna_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
                        } else {
                                temp= MEM_mallocN(bh->len, blockname);
-                               memcpy(temp, (bh+1), bh->len); /*  BHEAD+DATA dependancy */
+                               memcpy(temp, (bh+1), bh->len);
                        }
                }
        }
@@ -1607,6 +1611,15 @@ static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
                }
 
                switch (con->type) {
+               case CONSTRAINT_TYPE_PYTHON:
+                       {
+                               bPythonConstraint *data;
+                               data= (bPythonConstraint*)con->data;
+                               data->tar = newlibadr(fd, id->lib, data->tar);
+                               data->text = newlibadr(fd, id->lib, data->text);
+                               //IDP_LibLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
+                       }
+                       break;
                case CONSTRAINT_TYPE_ACTION:
                        {
                                bActionConstraint *data;
@@ -1620,21 +1633,21 @@ static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
                                bLocateLikeConstraint *data;
                                data= ((bLocateLikeConstraint*)con->data);
                                data->tar = newlibadr(fd, id->lib, data->tar);
-                       };
+                       }
                        break;
                case CONSTRAINT_TYPE_ROTLIKE:
                        {
                                bRotateLikeConstraint *data;
                                data= ((bRotateLikeConstraint*)con->data);
                                data->tar = newlibadr(fd, id->lib, data->tar);
-                       };
+                       }
                        break;
                case CONSTRAINT_TYPE_SIZELIKE:
                        {
                                bSizeLikeConstraint *data;
                                data= ((bSizeLikeConstraint*)con->data);
                                data->tar = newlibadr(fd, id->lib, data->tar);
-                       };
+                       }
                        break;
                case CONSTRAINT_TYPE_KINEMATIC:
                        {
@@ -1662,62 +1675,43 @@ static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
                                bLockTrackConstraint *data;
                                data= ((bLockTrackConstraint*)con->data);
                                data->tar = newlibadr(fd, id->lib, data->tar);
-                       };
+                       }
                        break;
                case CONSTRAINT_TYPE_FOLLOWPATH:
                        {
                                bFollowPathConstraint *data;
                                data= ((bFollowPathConstraint*)con->data);
                                data->tar = newlibadr(fd, id->lib, data->tar);
-                       };
-                       break;
-               case CONSTRAINT_TYPE_DISTANCELIMIT:
-                       {
-                               bDistanceLimitConstraint *data;
-                               data= ((bDistanceLimitConstraint*)con->data);
-                               data->tar = newlibadr(fd, id->lib, data->tar);
-                       };
+                       }
                        break;
                case CONSTRAINT_TYPE_STRETCHTO:
                        {
                                bStretchToConstraint *data;
                                data= ((bStretchToConstraint*)con->data);
                                data->tar = newlibadr(fd, id->lib, data->tar);
-                       };
-                       break;
-               case CONSTRAINT_TYPE_LOCLIMIT:
-                       {
-                               bLocLimitConstraint *data;
-                               data= ((bLocLimitConstraint*)con->data);
-                       };
-                       break;
-               case CONSTRAINT_TYPE_ROTLIMIT:
-                       {
-                               bRotLimitConstraint *data;
-                               data= ((bRotLimitConstraint*)con->data);
-                       };
-                       break;
-               case CONSTRAINT_TYPE_SIZELIMIT:
-                       {
-                               bSizeLimitConstraint *data;
-                               data= ((bSizeLimitConstraint*)con->data);
-                       };
+                       }
                        break;
                case CONSTRAINT_TYPE_RIGIDBODYJOINT:
                        {
                                bRigidBodyJointConstraint *data;
                                data= ((bRigidBodyJointConstraint*)con->data);
                                data->tar = newlibadr(fd, id->lib, data->tar);
-                       };
+                       }
                        break;
                case CONSTRAINT_TYPE_CLAMPTO:
                        {
                                bClampToConstraint *data;
                                data= ((bClampToConstraint*)con->data);
                                data->tar = newlibadr(fd, id->lib, data->tar);
-                       };
+                       }
+                       break;
+               case CONSTRAINT_TYPE_CHILDOF:
+                       {
+                               bChildOfConstraint *data;
+                               data= ((bChildOfConstraint*)con->data);
+                               data->tar = newlibadr(fd, id->lib, data->tar);
+                       }
                        break;
-
                case CONSTRAINT_TYPE_NULL:
                        break;
                }
@@ -1731,6 +1725,11 @@ static void direct_link_constraints(FileData *fd, ListBase *lb)
        link_list(fd, lb);
        for (cons=lb->first; cons; cons=cons->next) {
                cons->data = newdataadr(fd, cons->data);
+               if (cons->type == CONSTRAINT_TYPE_PYTHON) {
+                       bPythonConstraint *data= cons->data;
+                       data->prop = newdataadr(fd, data->prop);
+                       IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
+               }
        }
 }
 
@@ -2568,15 +2567,17 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
                        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->verts = newdataadr(fd, mesh->mr->verts);
                        
                for(; lvl; lvl= lvl->next) {
                        lvl->verts= newdataadr(fd, lvl->verts);
                        lvl->faces= newdataadr(fd, lvl->faces);
                        lvl->edges= newdataadr(fd, lvl->edges);
                        lvl->colfaces= newdataadr(fd, lvl->colfaces);
-
-                       /* Recalculating the maps is faster than reading them from the file */
-                       multires_calc_level_maps(lvl);
+                       lvl->edge_boundary_states= NULL;
+                       lvl->vert_face_map = lvl->vert_edge_map = NULL;
+                       lvl->map_mem= NULL;
                }
        }
        
@@ -2682,7 +2683,7 @@ static void lib_link_object(FileData *fd, Main *main)
                                ob->type= OB_EMPTY;
                                warn= 1;
                                if(ob->id.lib) printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
-                               else printf("Object %s lost data. Lib:%x\n", ob->id.name+2, (unsigned int) ob->id.lib);
+                               else printf("Object %s lost data.", ob->id.name+2);
                                
                                if(ob->pose) {
                                        free_pose_channels(ob->pose);
@@ -3954,15 +3955,6 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
        ListBase *lb;
        char *allocname;
        
-       if(bhead->code==ID_ID) {
-               ID *linkedid= (ID *)(bhead + 1); /*  BHEAD+DATA dependancy */
-
-               lb= wich_libbase(main, GS(linkedid->name));
-       }
-       else {
-               lb= wich_libbase(main, bhead->code);
-       }
-
        /* read libblock */
        id = read_struct(fd, bhead, "lib block");
        if (id_r)
@@ -3971,6 +3963,15 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
                return blo_nextbhead(fd, bhead);
        
        oldnewmap_insert(fd->libmap, bhead->old, id, bhead->code);      /* for ID_ID check */
+       
+       /* do after read_struct, for dna reconstruct */
+       if(bhead->code==ID_ID) {
+               lb= wich_libbase(main, GS(id->name));
+       }
+       else {
+               lb= wich_libbase(main, bhead->code);
+       }
+       
        BLI_addtail(lb, id);
 
        /* clear first 8 bits */
@@ -4330,6 +4331,18 @@ static void customdata_version_242(Mesh *me)
        mesh_update_customdata_pointers(me);
 }
 
+/*only copy render texface layer from active*/
+static void customdata_version_243(Mesh *me)
+{
+       CustomDataLayer *layer;
+       int a;
+
+       for (a=0; a < me->fdata.totlayer; a++) {
+               layer= &me->fdata.layers[a];
+               layer->active_rnd = layer->active;
+       }
+}
+
 /* struct NodeImageAnim moved to ImageUser, and we make it default available */
 static void do_version_ntree_242_2(bNodeTree *ntree)
 {
@@ -6387,11 +6400,219 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
        }
        if(main->versionfile <= 243) {
+               Object *ob= main->object.first;
                Camera *cam = main->camera.first;
+               Material *ma;
                
                for(; cam; cam= cam->id.next) {
                        cam->angle= 360.0f * atan(16.0f/cam->lens) / M_PI;
                }
+
+               for(ma=main->mat.first; ma; ma= ma->id.next) {
+                       if(ma->sss_scale==0.0f) {
+                               ma->sss_radius[0]= 1.0f;
+                               ma->sss_radius[1]= 1.0f;
+                               ma->sss_radius[2]= 1.0f;
+                               ma->sss_col[0]= 0.8f;
+                               ma->sss_col[1]= 0.8f;
+                               ma->sss_col[2]= 0.8f;
+                               ma->sss_error= 0.05f;
+                               ma->sss_scale= 0.1f;
+                               ma->sss_ior= 1.3f;
+                               ma->sss_colfac= 1.0f;
+                               ma->sss_texfac= 0.0f;
+                       }
+                       if(ma->sss_front==0 && ma->sss_back==0) {
+                               ma->sss_front= 1.0f;
+                               ma->sss_back= 1.0f;
+                       }
+                       if(ma->sss_col[0]==0 && ma->sss_col[1]==0 && ma->sss_col[2]==0) {
+                               ma->sss_col[0]= ma->r;
+                               ma->sss_col[1]= ma->g;
+                               ma->sss_col[2]= ma->b;
+                       }
+               }
+               
+               for(; ob; ob= ob->id.next) {
+                       bDeformGroup *curdef;
+                       
+                       for(curdef= ob->defbase.first; curdef; curdef=curdef->next) {
+                               /* replace an empty-string name with unique name */
+                               if (curdef->name[0] == '\0') {
+                                       unique_vertexgroup_name(curdef, ob);
+                               }
+                       }
+
+                       if(main->versionfile < 243 || main->subversionfile < 1) {
+                               ModifierData *md;
+
+                               /* translate old mirror modifier axis values to new flags */
+                               for (md=ob->modifiers.first; md; md=md->next) {
+                                       if (md->type==eModifierType_Mirror) {
+                                               MirrorModifierData *mmd = (MirrorModifierData*) md;
+
+                                               switch(mmd->axis)
+                                               {
+                                               case 0:
+                                                       mmd->flag |= MOD_MIR_AXIS_X;
+                                                       break;
+                                               case 1:
+                                                       mmd->flag |= MOD_MIR_AXIS_Y;
+                                                       break;
+                                               case 2:
+                                                       mmd->flag |= MOD_MIR_AXIS_Z;
+                                                       break;
+                                               }
+
+                                               mmd->axis = 0;
+                                       }
+                               }
+                       }
+               }
+               
+               /* render layer added, this is not the active layer */
+               if(main->versionfile <= 243 || main->subversionfile < 2) {
+                       Mesh *me;
+                       for(me=main->mesh.first; me; me=me->id.next)
+                               customdata_version_243(me);
+               }
+       }
+       if(main->versionfile <= 244) {
+               Scene *sce;
+               bScreen *sc;
+               Object *ob;
+
+               if(main->versionfile != 244 || main->subversionfile < 2) {
+                       Mesh *me;
+                       
+                       for(sce= main->scene.first; sce; sce= sce->id.next)
+                               sce->r.mode |= R_SSS;
+
+                       /* Copy over old per-level multires vertex data
+                          into a single vertex array in struct Multires */
+                       
+                       for(me = main->mesh.first; me; me=me->id.next) {
+                               if(me->mr) {
+                                       MultiresLevel *lvl = me->mr->levels.last;
+                                       if(lvl) {
+                                               me->mr->verts = lvl->verts;
+                                               lvl->verts = NULL;
+                                               /* Don't need the other vert arrays */
+                                               for(lvl = lvl->prev; lvl; lvl = lvl->prev) {
+                                                       MEM_freeN(lvl->verts);
+                                                       lvl->verts = NULL;
+                                               }
+                                       }
+                               }
+                       }
+                       
+                       /* correct older action editors - incorrect scrolling */
+                       for(sc= main->screen.first; sc; sc= sc->id.next) {
+                               ScrArea *sa;
+                               sa= sc->areabase.first;
+                               while(sa) {
+                                       SpaceLink *sl;
+
+                                       for (sl= sa->spacedata.first; sl; sl= sl->next) {
+                                               if(sl->spacetype==SPACE_ACTION) {
+                                                       SpaceAction *saction= (SpaceAction*) sl;
+                                                       
+                                                       saction->v2d.tot.ymin= -1000.0;
+                                                       saction->v2d.tot.ymax= 0.0;
+                                                       
+                                                       saction->v2d.cur.ymin= -75.0;
+                                                       saction->v2d.cur.ymax= 5.0;
+                                               }
+                                       }
+                                       sa = sa->next;
+                               }
+                       }
+               }
+               if (main->subversionfile < 3) {
+                       for(ob = main->object.first; ob; ob= ob->id.next) {
+                               ListBase *list;
+                               list = &ob->constraints;
+                               float temp_size[3];
+
+                               /* fix up constraints due to constraint recode changes */
+                               if (list) {
+                                       bConstraint *curcon;
+                                       for (curcon = list->first; curcon; curcon=curcon->next) {
+                                               /* old CONSTRAINT_LOCAL check -> convert to CONSTRAINT_SPACE_LOCAL */
+                                               if (curcon->flag & 0x20) {
+                                                       curcon->ownspace = CONSTRAINT_SPACE_LOCAL;
+                                                       curcon->tarspace = CONSTRAINT_SPACE_LOCAL;
+                                               }
+                                               
+                                               switch (curcon->type) {
+                                                       case CONSTRAINT_TYPE_ACTION:
+                                                       {
+                                                               bActionConstraint *data= (bActionConstraint *)curcon->data;
+                                                               
+                                                               if (data->local)
+                                                                       curcon->tarspace = CONSTRAINT_SPACE_LOCAL;
+                                                       }                                                       
+                                                               break;
+                                                       case CONSTRAINT_TYPE_LOCLIMIT:
+                                                       {
+                                                               bLocLimitConstraint *data= (bLocLimitConstraint *)curcon->data;
+                                                               
+                                                               if (data->flag2) {
+                                                                       curcon->ownspace = CONSTRAINT_SPACE_LOCAL;
+                                                                       curcon->ownspace = CONSTRAINT_SPACE_LOCAL;
+                                                               }
+                                                       }
+                                                               break;
+                                               }       
+                                       }
+                               }
+                               
+                               /* correctly initialise constinv matrix */
+                               Mat4One(ob->constinv);
+
+                               if (ob->type == OB_ARMATURE) {
+                                       if (ob->pose) {
+                                               bConstraint *curcon;
+                                               bPoseChannel *pchan;
+                                               
+                                               for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
+                                                       /* make sure constraints are all up to date */
+                                                       for (curcon = pchan->constraints.first; curcon; curcon=curcon->next) {
+                                                               /* old CONSTRAINT_LOCAL check -> convert to CONSTRAINT_SPACE_LOCAL */
+                                                               if (curcon->flag & 0x20) {
+                                                                       curcon->ownspace = CONSTRAINT_SPACE_LOCAL;
+                                                                       curcon->tarspace = CONSTRAINT_SPACE_LOCAL;
+                                                               }
+                                                               
+                                                               switch (curcon->type) {
+                                                                       case CONSTRAINT_TYPE_ACTION:
+                                                                       {
+                                                                               bActionConstraint *data= (bActionConstraint *)curcon->data;
+                                                                               
+                                                                               if (data->local)
+                                                                                       curcon->tarspace = CONSTRAINT_SPACE_LOCAL;
+                                                                       }                                                       
+                                                                               break;
+                                                                       case CONSTRAINT_TYPE_LOCLIMIT:
+                                                                       {
+                                                                               bLocLimitConstraint *data= (bLocLimitConstraint *)curcon->data;
+                                                                               
+                                                                               if (data->flag2) {
+                                                                                       curcon->ownspace = CONSTRAINT_SPACE_LOCAL;
+                                                                                       curcon->ownspace = CONSTRAINT_SPACE_LOCAL;
+                                                                               }
+                                                                       }
+                                                                               break;
+                                                               }
+                                                       }
+                                                       
+                                                       /* correctly initialise constinv matrix */
+                                                       Mat4One(pchan->constinv);
+                                               }
+                                       }
+                               }
+                       }
+               }
        }
 
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
@@ -6548,22 +6769,27 @@ static BHead *find_bhead(FileData *fd, void *old)
        return NULL;
 }
 
-static ID *is_yet_read(Main *mainvar, BHead *bhead)
+char *bhead_id_name(FileData *fd, BHead *bhead)
+{
+       return ((char *)(bhead+1)) + fd->id_name_offs;
+}
+
+static ID *is_yet_read(FileData *fd, Main *mainvar, BHead *bhead)
 {
        ListBase *lb;
-       ID *idtest, *id;
+       char *idname= bhead_id_name(fd, bhead);
 
-       // BHEAD+DATA dependancy
-       idtest= (ID *)(bhead +1);
-       lb= wich_libbase(mainvar, GS(idtest->name));
+       lb= wich_libbase(mainvar, GS(idname));
+       
        if(lb) {
-               id= lb->first;
+               ID *id= lb->first;
                while(id) {
-                       if( strcmp(id->name, idtest->name)==0 ) return id;
+                       if( strcmp(id->name, idname)==0 ) 
+                               return id;
                        id= id->next;
                }
        }
-       return 0;
+       return NULL;
 }
 
 static void expand_doit(FileData *fd, Main *mainvar, void *old)
@@ -6578,16 +6804,12 @@ static void expand_doit(FileData *fd, Main *mainvar, void *old)
                        BHead *bheadlib= find_previous_lib(fd, bhead);
 
                        if(bheadlib) {
-                               // BHEAD+DATA dependancy
-                               Library *lib= (Library *)(bheadlib+1);
-                               /* ***************************** */
-                               /* we read the lib->name directly from the bhead, no DNA, potential danger (64 bits?) */
-                               /* ***************************** */
+                               Library *lib= read_struct(fd, bheadlib, "Library");
                                Main *ptr= blo_find_main(&fd->mainlist, lib->name, fd->filename);
 
-                               id= is_yet_read(ptr, bhead);
+                               id= is_yet_read(fd, ptr, bhead);
 
-                               if(id==0) {
+                               if(id==NULL) {
                                        read_libblock(fd, ptr, bhead, LIB_READ+LIB_INDIRECT, NULL);
                                        if(G.f & G_DEBUG) printf("expand_doit: other lib %s\n", lib->name);
                                        
@@ -6600,20 +6822,20 @@ static void expand_doit(FileData *fd, Main *mainvar, void *old)
                                        change_idid_adr_fd(fd, bhead->old, id);
                                        if(G.f & G_DEBUG) printf("expand_doit: already linked: %s lib: %s\n", id->name, lib->name);
                                }
+                               
+                               MEM_freeN(lib);
                        }
                }
                else {
-                       id= is_yet_read(mainvar, bhead);
+                       id= is_yet_read(fd, mainvar, bhead);
                        if(id==NULL) {
-                               // BHEAD+DATA dependancy
-                               id= (ID *)(bhead+1);
                                read_libblock(fd, mainvar, bhead, LIB_TESTIND, NULL);
                        }
                        else {
                                /* this is actually only needed on UI call? when ID was already read before, and another append
                                   happens which invokes same ID... in that case the lookup table needs this entry */
                                oldnewmap_insert(fd->libmap, bhead->old, id, 1);
-                               // printf("expand: already read %s\n", id->name);
+                               if(G.f & G_DEBUG) printf("expand: already read %s\n", id->name);
                        }
                }
        }
@@ -6786,6 +7008,15 @@ static void expand_constraints(FileData *fd, Main *mainvar, ListBase *lb)
 
        for (curcon=lb->first; curcon; curcon=curcon->next) {
                switch (curcon->type) {
+               case CONSTRAINT_TYPE_NULL:
+                       break;
+               case CONSTRAINT_TYPE_PYTHON:
+                       {
+                               bPythonConstraint *data = (bPythonConstraint*)curcon->data;
+                               expand_doit(fd, mainvar, data->tar);
+                               expand_doit(fd, mainvar, data->text);
+                       }
+                       break;
                case CONSTRAINT_TYPE_ACTION:
                        {
                                bActionConstraint *data = (bActionConstraint*)curcon->data;
@@ -6797,75 +7028,73 @@ static void expand_constraints(FileData *fd, Main *mainvar, ListBase *lb)
                        {
                                bLocateLikeConstraint *data = (bLocateLikeConstraint*)curcon->data;
                                expand_doit(fd, mainvar, data->tar);
-                               break;
                        }
+                       break;
                case CONSTRAINT_TYPE_ROTLIKE:
                        {
                                bRotateLikeConstraint *data = (bRotateLikeConstraint*)curcon->data;
                                expand_doit(fd, mainvar, data->tar);
-                               break;
                        }
+                       break;
                case CONSTRAINT_TYPE_SIZELIKE:
                        {
                                bSizeLikeConstraint *data = (bSizeLikeConstraint*)curcon->data;
                                expand_doit(fd, mainvar, data->tar);
-                               break;
                        }
+                       break;
                case CONSTRAINT_TYPE_KINEMATIC:
                        {
                                bKinematicConstraint *data = (bKinematicConstraint*)curcon->data;
                                expand_doit(fd, mainvar, data->tar);
-                               break;
                        }
+                       break;
                case CONSTRAINT_TYPE_TRACKTO:
                        {
                                bTrackToConstraint *data = (bTrackToConstraint*)curcon->data;
                                expand_doit(fd, mainvar, data->tar);
-                               break;
                        }
+                       break;
                case CONSTRAINT_TYPE_MINMAX:
                        {
                                bMinMaxConstraint *data = (bMinMaxConstraint*)curcon->data;
                                expand_doit(fd, mainvar, data->tar);
-                               break;
                        }
+                       break;
                case CONSTRAINT_TYPE_LOCKTRACK:
                        {
                                bLockTrackConstraint *data = (bLockTrackConstraint*)curcon->data;
                                expand_doit(fd, mainvar, data->tar);
-                               break;
                        }
+                       break;
                case CONSTRAINT_TYPE_FOLLOWPATH:
                        {
                                bFollowPathConstraint *data = (bFollowPathConstraint*)curcon->data;
                                expand_doit(fd, mainvar, data->tar);
-                               break;
-                       }
-               case CONSTRAINT_TYPE_DISTANCELIMIT:
-                       {
-                               bDistanceLimitConstraint *data = (bDistanceLimitConstraint*)curcon->data;
-                               expand_doit(fd, mainvar, data->tar);
-                               break;
                        }
+                       break;
                case CONSTRAINT_TYPE_STRETCHTO:
                        {
                                bStretchToConstraint *data = (bStretchToConstraint*)curcon->data;
                                expand_doit(fd, mainvar, data->tar);
-                               break;
                        }
+                       break;
                case CONSTRAINT_TYPE_RIGIDBODYJOINT:
                        {
                                bRigidBodyJointConstraint *data = (bRigidBodyJointConstraint*)curcon->data;
                                expand_doit(fd, mainvar, data->tar);
-                               break;
                        }
+                       break;
                case CONSTRAINT_TYPE_CLAMPTO:
                        {
                                bClampToConstraint *data = (bClampToConstraint*)curcon->data;
                                expand_doit(fd, mainvar, data->tar);
-                               break;
                        }
-               case CONSTRAINT_TYPE_NULL:
+                       break;
+               case CONSTRAINT_TYPE_CHILDOF:
+                       {
+                               bChildOfConstraint *data = (bChildOfConstraint*)curcon->data;
+                               expand_doit(fd, mainvar, data->tar);
+                       }
                        break;
                default:
                        break;
@@ -7236,19 +7465,19 @@ static void append_named_part(FileData *fd, Main *mainvar, Scene *scene, char *n
        Base *base;
        BHead *bhead;
        ID *id;
-       int afbreek=0;
+       int endloop=0;
 
        bhead = blo_firstbhead(fd);
-       while(bhead && afbreek==0) {
+       while(bhead && endloop==0) {
 
-               if(bhead->code==ENDB) afbreek= 1;
+               if(bhead->code==ENDB) endloop= 1;
                else if(bhead->code==idcode) {
-                       // BHEAD+DATA dependancy
-                       id= (ID *)(bhead+1);
-                       if(strcmp(id->name+2, name)==0) {
+                       char *idname= bhead_id_name(fd, bhead);
+                               
+                       if(strcmp(idname+2, name)==0) {
 
-                               id= is_yet_read(mainvar, bhead);
-                               if(id==0) {
+                               id= is_yet_read(fd, mainvar, bhead);
+                               if(id==NULL) {
                                        read_libblock(fd, mainvar, bhead, LIB_TESTEXT, NULL);
                                }
                                else {
@@ -7264,7 +7493,7 @@ static void append_named_part(FileData *fd, Main *mainvar, Scene *scene, char *n
                                        base= MEM_callocN( sizeof(Base), "app_nam_part");
                                        BLI_addtail(&scene->base, base);
 
-                                       if(id==0) ob= mainvar->object.last;
+                                       if(id==NULL) ob= mainvar->object.last;
                                        else ob= (Object *)id;
                                        
                                        /* this is bad code... G.vd nor G.scene should be used on this level... */
@@ -7282,7 +7511,7 @@ static void append_named_part(FileData *fd, Main *mainvar, Scene *scene, char *n
                                                /* do NOT make base active here! screws up GUI stuff, if you want it do it on src/ level */
                                        }
                                }
-                               afbreek= 1;
+                               endloop= 1;
                        }
                }
 
@@ -7296,9 +7525,8 @@ static void append_id_part(FileData *fd, Main *mainvar, ID *id, ID **id_r)
 
        for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
                if (bhead->code == GS(id->name)) {
-                       ID *idread= (ID *)(bhead+1); /*  BHEAD+DATA dependancy */
-
-                       if (BLI_streq(id->name, idread->name)) {
+                       
+                       if (BLI_streq(id->name, bhead_id_name(fd, bhead))) {
                                id->flag &= ~LIB_READ;
                                id->flag |= LIB_TEST;
 //                             printf("read lib block %s\n", id->name);
@@ -7480,7 +7708,6 @@ void BLO_library_append(SpaceFile *sfile, char *dir, int idcode)
                        }
                }
        }
-       DAG_scene_sort(G.scene);
 }
 
 /* ************* READ LIBRARY ************** */
@@ -7516,7 +7743,7 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                while(mainptr) {
                        int tot= mainvar_count_libread_blocks(mainptr);
                        
-                       //printf("found LIB_READ %s\n", mainptr->curlib->name);
+                       // printf("found LIB_READ %s\n", mainptr->curlib->name);
                        if(tot) {
                                FileData *fd= mainptr->curlib->filedata;