svn merge ^/trunk/blender -r40644:40720
[blender-staging.git] / source / blender / blenloader / intern / readfile.c
index 765fe7ada12e19496cc27e358c3f3e164c99d94d..4ecfc648ebfad8b3d3f7ea6645384598bc147f02 100644 (file)
 
 #include "MEM_guardedalloc.h"
 
+#include "BLI_utildefines.h"
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
-#include "BLI_utildefines.h"
+#include "BLI_cellalloc.h"
+#include "BLI_edgehash.h"
 
 #include "BKE_anim.h"
 #include "BKE_action.h"
@@ -2540,6 +2542,16 @@ static void lib_link_key(FileData *fd, Main *main)
 
        key= main->key.first;
        while(key) {
+               /*check if we need to generate unique ids for the shapekeys*/
+               if (!key->uidgen) {
+                       KeyBlock *block;
+
+                       key->uidgen = 1;
+                       for (block=key->block.first; block; block=block->next) {
+                               block->uid = key->uidgen++;
+                       }
+               }
+
                if(key->id.flag & LIB_NEEDLINK) {
                        if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
                        
@@ -3447,6 +3459,26 @@ static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata
 
 }
 
+static void lib_link_customdata_mtpoly(FileData *fd, Mesh *me, CustomData *pdata, int totface)
+{
+       int i;
+
+       for(i=0; i<pdata->totlayer; i++) {
+               CustomDataLayer *layer = &pdata->layers[i];
+               
+               if(layer->type == CD_MTEXPOLY) {
+                       MTexPoly *tf= layer->data;
+                       int i;
+
+                       for (i=0; i<totface; i++, tf++) {
+                               tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
+                               if(tf->tpage && tf->tpage->id.us==0)
+                                       tf->tpage->id.us= 1;
+                       }
+               }
+       }
+}
+
 static void lib_link_mesh(FileData *fd, Main *main)
 {
        Mesh *me;
@@ -3474,10 +3506,16 @@ static void lib_link_mesh(FileData *fd, Main *main)
                        me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
 
                        lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
+                       lib_link_customdata_mtpoly(fd, me, &me->pdata, me->totpoly);
                        if(me->mr && me->mr->levels.first)
                                lib_link_customdata_mtface(fd, me, &me->mr->fdata,
                                                           ((MultiresLevel*)me->mr->levels.first)->totface);
 
+                       /*check if we need to convert mfaces to mpolys*/
+                       if (me->totface && !me->totpoly) {
+                               convert_mfaces_to_mpolys(me);
+                       }
+                       
                        me->id.flag -= LIB_NEEDLINK;
                }
                me= me->id.next;
@@ -3496,10 +3534,17 @@ static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
        }
 
        for (i= count; i > 0; i--, mdverts++) {
-               if(mdverts->dw) {
-                       mdverts->dw= newdataadr(fd, mdverts->dw);
+               /*convert to vgroup allocation system*/
+               MDeformWeight *dw;
+               if(mdverts->dw && (dw= newdataadr(fd, mdverts->dw))) {
+                       const ssize_t dw_len= mdverts->totweight * sizeof(MDeformWeight);
+                       void *dw_tmp= BLI_cellalloc_malloc(dw_len, "direct_link_dverts");
+                       memcpy(dw_tmp, dw, dw_len);
+                       mdverts->dw= dw_tmp;
+                       MEM_freeN(dw);
                }
-               if (mdverts->dw == NULL) {
+               else {
+                       mdverts->dw= NULL;
                        mdverts->totweight= 0;
                }
        }
@@ -3512,7 +3557,18 @@ static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps, int exte
 
                for(i = 0; i < count; ++i) {
                        mdisps[i].disps = newdataadr(fd, mdisps[i].disps);
-
+                       
+                       /*put .disps into cellalloc system*/
+                       if (mdisps[i].disps) {
+                               float *disp2;
+                               
+                               disp2 = BLI_cellalloc_malloc(MEM_allocN_len(mdisps[i].disps), "cellalloc .disps copy");
+                               memcpy(disp2, mdisps[i].disps, MEM_allocN_len(mdisps[i].disps));
+                               
+                               MEM_freeN(mdisps[i].disps);
+                               mdisps[i].disps = (float (*)[3])disp2;
+                       }
+                       
                        if( (fd->flags & FD_FLAGS_SWITCH_ENDIAN) && (mdisps[i].disps) ) {
                                /* DNA_struct_switch_endian doesn't do endian swap for (*disps)[] */
                                /* this does swap for data written at write_mdisps() - readfile.c */
@@ -3529,6 +3585,7 @@ static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps, int exte
        }
 }
 
+/*this isn't really a public api function, so prototyped here*/
 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
 {
        int i = 0;
@@ -3549,6 +3606,8 @@ static void direct_link_customdata(FileData *fd, CustomData *data, int count)
                        i++;
                }
        }
+
+       CustomData_update_typemap(data);
 }
 
 static void direct_link_mesh(FileData *fd, Mesh *mesh)
@@ -3559,12 +3618,17 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
        mesh->mvert= newdataadr(fd, mesh->mvert);
        mesh->medge= newdataadr(fd, mesh->medge);
        mesh->mface= newdataadr(fd, mesh->mface);
+       mesh->mloop= newdataadr(fd, mesh->mloop);
+       mesh->mpoly= newdataadr(fd, mesh->mpoly);
        mesh->tface= newdataadr(fd, mesh->tface);
        mesh->mtface= newdataadr(fd, mesh->mtface);
        mesh->mcol= newdataadr(fd, mesh->mcol);
        mesh->msticky= newdataadr(fd, mesh->msticky);
        mesh->dvert= newdataadr(fd, mesh->dvert);
-       
+       mesh->mloopcol= newdataadr(fd, mesh->mloopcol);
+       mesh->mloopuv= newdataadr(fd, mesh->mloopuv);
+       mesh->mtpoly= newdataadr(fd, mesh->mtpoly);
+
        /* animdata */
        mesh->adt= newdataadr(fd, mesh->adt);
        direct_link_animdata(fd, mesh->adt);
@@ -3585,10 +3649,12 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
        direct_link_customdata(fd, &mesh->vdata, mesh->pv ? mesh->pv->totvert : mesh->totvert);
        direct_link_customdata(fd, &mesh->edata, mesh->pv ? mesh->pv->totedge : mesh->totedge);
        direct_link_customdata(fd, &mesh->fdata, mesh->pv ? mesh->pv->totface : mesh->totface);
-
+       direct_link_customdata(fd, &mesh->ldata, mesh->totloop);
+       direct_link_customdata(fd, &mesh->pdata, mesh->totpoly);
+       
        mesh->bb= NULL;
        mesh->mselect = NULL;
-       mesh->edit_mesh= NULL;
+       mesh->edit_btmesh= NULL;
        
        /* Multires data */
        mesh->mr= newdataadr(fd, mesh->mr);
@@ -11802,7 +11868,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                Mesh *me;
 
                for(me= main->mesh.first; me; me= me->id.next)
-                       mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
+                       mesh_calc_tessface_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
        }
 
        if (main->versionfile < 256 || (main->versionfile == 256 && main->subversionfile < 2)){