svn merge ^/trunk/blender -r43294:43338
[blender.git] / source / blender / blenloader / intern / readfile.c
index 88cc908619bc1473cab86548d17bfca1ccc56b24..0328be060cc4832920f2d185d6114faf6f040da1 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"
@@ -2702,6 +2704,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);
                        
@@ -3624,6 +3636,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;
@@ -3651,10 +3683,32 @@ 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);
+                       }
+                       
+                       /*
+                        * Re-tesselate, even if the polys were just created from tessfaces, this
+                        * is important because it:
+                        *  - fill the CD_POLYINDEX layer
+                        *  - gives consistency of tessface between loading from a file and
+                        *    converting an edited BMesh back into a mesh (i.e. it replaces
+                        *    quad tessfaces in a loaded mesh immediately, instead of lazily
+                        *    waiting until edit mode has been entered/exited, making it easier
+                        *    to recognize problems that would otherwise only show up after edits).
+                        */
+                       me->totface = mesh_recalcTesselation(
+                               &me->fdata, &me->ldata, &me->pdata,
+                               me->mvert, me->totface, me->totloop, me->totpoly);
+
+                       mesh_update_customdata_pointers(me, TRUE);
+
                        me->id.flag -= LIB_NEEDLINK;
                }
                me= me->id.next;
@@ -3673,10 +3727,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;
                }
        }
@@ -3689,7 +3750,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 */
@@ -3739,12 +3811,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);
@@ -3756,7 +3833,9 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
        direct_link_customdata(fd, &mesh->vdata, mesh->totvert);
        direct_link_customdata(fd, &mesh->edata, mesh->totedge);
        direct_link_customdata(fd, &mesh->fdata, mesh->totface);
-
+       direct_link_customdata(fd, &mesh->ldata, mesh->totloop);
+       direct_link_customdata(fd, &mesh->pdata, mesh->totpoly);
+       
 
 #ifdef USE_BMESH_FORWARD_COMPAT
        /* NEVER ENABLE THIS CODE INTO BMESH!
@@ -3789,7 +3868,7 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
 
        mesh->bb= NULL;
        mesh->mselect = NULL;
-       mesh->edit_mesh= NULL;
+       mesh->edit_btmesh= NULL;
        
        /* Multires data */
        mesh->mr= newdataadr(fd, mesh->mr);
@@ -4739,7 +4818,7 @@ static void lib_link_scene(FileData *fd, Main *main)
                                if(seq->scene) {
                                        seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
                                        if(seq->scene) {
-                                               seq->scene_sound = sound_scene_add_scene_sound(sce, seq, seq->startdisp, seq->enddisp, seq->startofs + seq->anim_startofs);
+                                               seq->scene_sound = sound_scene_add_scene_sound_defaults(sce, seq);
                                        }
                                }
                                if(seq->scene_camera) seq->scene_camera= newlibadr(fd, sce->id.lib, seq->scene_camera);
@@ -4751,7 +4830,7 @@ static void lib_link_scene(FileData *fd, Main *main)
                                                seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
                                        if (seq->sound) {
                                                seq->sound->id.us++;
-                                               seq->scene_sound = sound_add_scene_sound(sce, seq, seq->startdisp, seq->enddisp, seq->startofs + seq->anim_startofs);
+                                               seq->scene_sound = sound_add_scene_sound_defaults(sce, seq);
                                        }
                                }
                                seq->anim= NULL;
@@ -6560,20 +6639,20 @@ static void customdata_version_242(Mesh *me)
                if (layer->type == CD_MTFACE) {
                        if (layer->name[0] == 0) {
                                if (mtfacen == 0) strcpy(layer->name, "UVMap");
-                               else sprintf(layer->name, "UVMap.%.3d", mtfacen);
+                               else BLI_snprintf(layer->name, sizeof(layer->name), "UVMap.%.3d", mtfacen);
                        }
                        mtfacen++;
                }
                else if (layer->type == CD_MCOL) {
                        if (layer->name[0] == 0) {
                                if (mcoln == 0) strcpy(layer->name, "Col");
-                               else sprintf(layer->name, "Col.%.3d", mcoln);
+                               else BLI_snprintf(layer->name, sizeof(layer->name), "Col.%.3d", mcoln);
                        }
                        mcoln++;
                }
        }
 
-       mesh_update_customdata_pointers(me);
+       mesh_update_customdata_pointers(me, TRUE);
 }
 
 /*only copy render texface layer from active*/
@@ -7547,6 +7626,32 @@ static void do_versions_nodetree_socket_use_flags_2_62(bNodeTree *ntree)
        }
 }
 
+/* set the SOCK_AUTO_HIDDEN flag on collapsed nodes */
+static void do_versions_nodetree_socket_auto_hidden_flags_2_62(bNodeTree *ntree)
+{
+       bNode *node;
+       bNodeSocket *sock;
+       
+       for (node=ntree->nodes.first; node; node=node->next) {
+               if (node->flag & NODE_HIDDEN) {
+                       for (sock=node->inputs.first; sock; sock=sock->next) {
+                               if (sock->link==NULL)
+                                       sock->flag |= SOCK_AUTO_HIDDEN;
+                       }
+                       for(sock=node->outputs.first; sock; sock= sock->next) {
+                               if(nodeCountSocketLinks(ntree, sock)==0)
+                                       sock->flag |= SOCK_AUTO_HIDDEN;
+                       }
+               }
+               else {
+                       for(sock=node->inputs.first; sock; sock= sock->next)
+                               sock->flag &= ~SOCK_AUTO_HIDDEN;
+                       for(sock=node->outputs.first; sock; sock= sock->next)
+                               sock->flag &= ~SOCK_AUTO_HIDDEN;
+               }
+       }
+}
+
 static void do_versions(FileData *fd, Library *lib, Main *main)
 {
        /* WATCH IT!!!: pointers from libdata have not been converted */
@@ -9036,8 +9141,9 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                                        strcpy(kb->name, "Basis");
                                        }
                                        else {
-                                               if(kb->name[0]==0)
-                                                       sprintf(kb->name, "Key %d", index);
+                                               if (kb->name[0]==0) {
+                                                       BLI_snprintf(kb->name, sizeof(kb->name), "Key %d", index);
+                                               }
                                                kb->adrcode= index++;
                                        }
                                }
@@ -10030,7 +10136,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                BLI_addtail(&ob->particlesystem, psys);
 
                                md= modifier_new(eModifierType_ParticleSystem);
-                               sprintf(md->name, "ParticleSystem %i", BLI_countlist(&ob->particlesystem));
+                               BLI_snprintf(md->name, sizeof(md->name), "ParticleSystem %i", BLI_countlist(&ob->particlesystem));
                                psmd= (ParticleSystemModifierData*) md;
                                psmd->psys=psys;
                                BLI_addtail(&ob->modifiers, md);
@@ -10877,7 +10983,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        sce->gm.dome.warptext = sce->r.dometext;
 
                        //Stand Alone
-                       sce->gm.fullscreen = sce->r.fullscreen;
+                       sce->gm.playerflag |= (sce->r.fullscreen?GAME_PLAYER_FULLSCREEN:0);
                        sce->gm.xplay = sce->r.xplay;
                        sce->gm.yplay = sce->r.yplay;
                        sce->gm.freqplay = sce->r.freqplay;
@@ -12248,7 +12354,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_normals_tessface(me->mvert, me->totvert, me->mface, me->totface, NULL);
        }
 
        if (main->versionfile < 256 || (main->versionfile == 256 && main->subversionfile < 2)){
@@ -12779,7 +12885,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
        }
 
-       /* put compatibility code here until next subversion bump */
+       if (main->versionfile < 261 || (main->versionfile == 261 && main->subversionfile < 1))
        {
                {
                        /* update use flags for node sockets (was only temporary before) */
@@ -12875,11 +12981,81 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                                                 "Game property name conflict in object: \"%s\".\nText objects reserve the "
                                                                 "[\"Text\"] game property to change their content through Logic Bricks.\n",
                                                                 ob->id.name+2);
-                                               break;
                                        }
                                }
                        }
                }
+               {
+                       /* set the SOCK_AUTO_HIDDEN flag on collapsed nodes */
+                       Scene *sce;
+                       Material *mat;
+                       Tex *tex;
+                       Lamp *lamp;
+                       World *world;
+                       bNodeTree *ntree;
+
+                       for (sce=main->scene.first; sce; sce=sce->id.next)
+                               if (sce->nodetree)
+                                       do_versions_nodetree_socket_auto_hidden_flags_2_62(sce->nodetree);
+
+                       for (mat=main->mat.first; mat; mat=mat->id.next)
+                               if (mat->nodetree)
+                                       do_versions_nodetree_socket_auto_hidden_flags_2_62(mat->nodetree);
+
+                       for (tex=main->tex.first; tex; tex=tex->id.next)
+                               if (tex->nodetree)
+                                       do_versions_nodetree_socket_auto_hidden_flags_2_62(tex->nodetree);
+
+                       for (lamp=main->lamp.first; lamp; lamp=lamp->id.next)
+                               if (lamp->nodetree)
+                                       do_versions_nodetree_socket_auto_hidden_flags_2_62(lamp->nodetree);
+
+                       for (world=main->world.first; world; world=world->id.next)
+                               if (world->nodetree)
+                                       do_versions_nodetree_socket_auto_hidden_flags_2_62(world->nodetree);
+
+                       for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next)
+                               do_versions_nodetree_socket_auto_hidden_flags_2_62(ntree);
+               }
+       }
+
+       if (main->versionfile < 261 || (main->versionfile == 261 && main->subversionfile < 2))
+       {
+               {
+                       /* convert Camera Actuator values to defines */
+                       Object *ob;
+                       bActuator *act;
+                       for(ob = main->object.first; ob; ob= ob->id.next) {
+                               for(act= ob->actuators.first; act; act= act->next) {
+                                       if (act->type == ACT_CAMERA) {
+                                               bCameraActuator *ba= act->data;
+
+                                               if(ba->axis==(float) 'x') ba->axis=OB_POSX;
+                                               else if (ba->axis==(float)'y') ba->axis=OB_POSY;
+                                               /* don't do an if/else to avoid imediate subversion bump*/
+//                                     ba->axis=((ba->axis == (float) 'x')?OB_POSX_X:OB_POSY);
+                                       }
+                               }
+                       }
+               }
+
+               {
+                       /* convert deprecated sculpt_paint_unified_* fields to
+                          UnifiedPaintSettings */
+                       Scene *scene;
+                       for(scene= main->scene.first; scene; scene= scene->id.next) {
+                               ToolSettings *ts= scene->toolsettings;
+                               UnifiedPaintSettings *ups= &ts->unified_paint_settings;
+                               ups->size= ts->sculpt_paint_unified_size;
+                               ups->unprojected_radius= ts->sculpt_paint_unified_unprojected_radius;
+                               ups->alpha= ts->sculpt_paint_unified_alpha;
+                               ups->flag= ts->sculpt_paint_settings;
+                       }
+               }
+       }
+       
+       /* put compatibility code here until next subversion bump */
+       {
        }
 
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */