svn merge ^/trunk/blender -r43294:43338
[blender.git] / source / blender / blenloader / intern / readfile.c
index 9695b90593bc70b8cbed403272361b86bf3f3a55..0328be060cc4832920f2d185d6114faf6f040da1 100644 (file)
@@ -39,6 +39,7 @@
 #include <fcntl.h> // for open
 #include <string.h> // for strrchr strncmp strstr
 #include <math.h> // for fabs
+#include <stdarg.h> /* for va_start/end */
 
 #ifndef WIN32
        #include <unistd.h> // for read close
@@ -251,6 +252,31 @@ static void *read_struct(FileData *fd, BHead *bh, const char *blockname);
 static void direct_link_modifiers(FileData *fd, ListBase *lb);
 static void convert_tface_mt(FileData *fd, Main *main);
 
+/* this function ensures that reports are printed,
+ * in the case of libraray linking errors this is important!
+ *
+ * bit kludge but better then doubling up on prints,
+ * we could alternatively have a versions of a report function which foces printing - campbell
+ */
+static void BKE_reportf_wrap(ReportList *reports, ReportType type, const char *format, ...)
+{
+       char fixed_buf[1024]; /* should be long enough */
+
+       va_list args;
+
+       va_start(args, format);
+       vsnprintf(fixed_buf, sizeof(fixed_buf), format, args);
+       va_end(args);
+
+       fixed_buf[sizeof(fixed_buf) - 1] = '\0';
+
+       BKE_report(reports, type, fixed_buf);
+
+       if(G.background==0) {
+               printf("%s\n", fixed_buf);
+       }
+}
+
 static OldNewMap *oldnewmap_new(void) 
 {
        OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
@@ -3810,6 +3836,36 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
        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!
+        * THIS IS FOR LOADING BMESH INTO OLDER FILES ONLY */
+       mesh->mpoly= newdataadr(fd, mesh->mpoly);
+       mesh->mloop= newdataadr(fd, mesh->mloop);
+
+       direct_link_customdata(fd, &mesh->pdata, mesh->totpoly);
+       direct_link_customdata(fd, &mesh->ldata, mesh->totloop);
+
+       if (mesh->mpoly) {
+               /* be clever and load polygons as mfaces */
+
+               mesh->totface= mesh_mpoly_to_mface(&mesh->fdata, &mesh->ldata, &mesh->pdata,
+                                                  mesh->totface, mesh->totloop, mesh->totpoly);
+
+               CustomData_free(&mesh->pdata, mesh->totpoly);
+               memset(&mesh->pdata, 0, sizeof(CustomData));
+               mesh->totpoly = 0;
+
+               CustomData_free(&mesh->ldata, mesh->totloop);
+               memset(&mesh->ldata, 0, sizeof(CustomData));
+               mesh->totloop = 0;
+
+               mesh_update_customdata_pointers(mesh);
+       }
+
+#endif
+
+
        mesh->bb= NULL;
        mesh->mselect = NULL;
        mesh->edit_btmesh= NULL;
@@ -4164,8 +4220,9 @@ static void lib_link_object(FileData *fd, Main *main)
                ob= ob->id.next;
        }
 
-       if(warn)
+       if(warn) {
                BKE_report(fd->reports, RPT_WARNING, "Warning in console");
+       }
 }
 
 
@@ -4746,8 +4803,9 @@ static void lib_link_scene(FileData *fd, Main *main)
                                base->object= newlibadr_us(fd, sce->id.lib, base->object);
                                
                                if(base->object==NULL) {
-                                       BKE_reportf(fd->reports, RPT_ERROR, "LIB ERROR: Object lost from scene:'%s\'\n", sce->id.name+2);
-                                       if(G.background==0) printf("LIB ERROR: base removed from scene:'%s\'\n", sce->id.name+2);
+                                       BKE_reportf_wrap(fd->reports, RPT_ERROR,
+                                                        "LIB ERROR: Object lost from scene:'%s\'\n",
+                                                        sce->id.name+2);
                                        BLI_remlink(&sce->base, base);
                                        if(base==sce->basact) sce->basact= NULL;
                                        MEM_freeN(base);
@@ -4760,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);
@@ -4772,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;
@@ -5847,8 +5905,9 @@ static void direct_link_library(FileData *fd, Library *lib, Main *main)
        for(newmain= fd->mainlist.first; newmain; newmain= newmain->next) {
                if(newmain->curlib) {
                        if(BLI_path_cmp(newmain->curlib->filepath, lib->filepath) == 0) {
-                               printf("Fixed error in file; multiple instances of lib:\n %s\n", lib->filepath);
-                               BKE_reportf(fd->reports, RPT_WARNING, "Library '%s', '%s' had multiple instances, save and reload!", lib->name, lib->filepath);
+                               BKE_reportf_wrap(fd->reports, RPT_WARNING,
+                                                "Library '%s', '%s' had multiple instances, save and reload!",
+                                                lib->name, lib->filepath);
 
                                change_idid_adr(&fd->mainlist, fd, lib, newmain->curlib);
 //                             change_idid_adr_fd(fd, lib, newmain->curlib);
@@ -6018,10 +6077,29 @@ static void lib_link_group(FileData *fd, Main *main)
 
 /* ***************** READ MOVIECLIP *************** */
 
+static void direct_link_movieReconstruction(FileData *fd, MovieTrackingReconstruction *reconstruction)
+{
+       reconstruction->cameras= newdataadr(fd, reconstruction->cameras);
+}
+
+static void direct_link_movieTracks(FileData *fd, ListBase *tracksbase)
+{
+       MovieTrackingTrack *track;
+
+       link_list(fd, tracksbase);
+
+       track= tracksbase->first;
+       while(track) {
+               track->markers= newdataadr(fd, track->markers);
+
+               track= track->next;
+       }
+}
+
 static void direct_link_movieclip(FileData *fd, MovieClip *clip)
 {
        MovieTracking *tracking= &clip->tracking;
-       MovieTrackingTrack *track;
+       MovieTrackingObject *object;
 
        if(fd->movieclipmap) clip->cache= newmclipadr(fd, clip->cache);
        else clip->cache= NULL;
@@ -6029,16 +6107,8 @@ static void direct_link_movieclip(FileData *fd, MovieClip *clip)
        if(fd->movieclipmap) clip->tracking.camera.intrinsics= newmclipadr(fd, clip->tracking.camera.intrinsics);
        else clip->tracking.camera.intrinsics= NULL;
 
-       tracking->reconstruction.cameras= newdataadr(fd, tracking->reconstruction.cameras);
-
-       link_list(fd, &tracking->tracks);
-
-       track= tracking->tracks.first;
-       while(track) {
-               track->markers= newdataadr(fd, track->markers);
-
-               track= track->next;
-       }
+       direct_link_movieTracks(fd, &tracking->tracks);
+       direct_link_movieReconstruction(fd, &tracking->reconstruction);
 
        clip->tracking.act_track= newdataadr(fd, clip->tracking.act_track);
 
@@ -6049,6 +6119,16 @@ static void direct_link_movieclip(FileData *fd, MovieClip *clip)
        clip->tracking.stabilization.ok= 0;
        clip->tracking.stabilization.scaleibuf= NULL;
        clip->tracking.stabilization.rot_track= newdataadr(fd, clip->tracking.stabilization.rot_track);
+
+       link_list(fd, &tracking->objects);
+
+       object= tracking->objects.first;
+       while(object) {
+               direct_link_movieTracks(fd, &object->tracks);
+               direct_link_movieReconstruction(fd, &object->reconstruction);
+
+               object= object->next;
+       }
 }
 
 static void lib_link_movieclip(FileData *fd, Main *main)
@@ -6559,14 +6639,14 @@ 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++;
                }
@@ -7546,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 */
@@ -9035,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++;
                                        }
                                }
@@ -10029,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);
@@ -10876,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;
@@ -12247,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_tessface_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)){
@@ -12778,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) */
@@ -12822,11 +12929,134 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
                {
                        MovieClip *clip;
-                       for(clip= main->movieclip.first; clip; clip= clip->id.next) {
+                       Object *ob;
+
+                       for (clip= main->movieclip.first; clip; clip= clip->id.next) {
+                               MovieTracking *tracking= &clip->tracking;
+                               MovieTrackingObject *tracking_object= tracking->objects.first;
+
                                clip->proxy.build_tc_flag|= IMB_TC_RECORD_RUN_NO_GAPS;
+
+                               if(!tracking->settings.object_distance)
+                                       tracking->settings.object_distance= 1.0f;
+
+                               if(tracking->objects.first == NULL)
+                                       BKE_tracking_new_object(tracking, "Camera");
+
+                               while(tracking_object) {
+                                       if(!tracking_object->scale)
+                                               tracking_object->scale= 1.0f;
+
+                                       tracking_object= tracking_object->next;
+                               }
+                       }
+
+                       for (ob= main->object.first; ob; ob= ob->id.next) {
+                               bConstraint *con;
+                               for (con= ob->constraints.first; con; con=con->next) {
+                                       bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
+
+                                       if(!cti)
+                                               continue;
+
+                                       if(cti->type==CONSTRAINT_TYPE_OBJECTSOLVER) {
+                                               bObjectSolverConstraint *data= (bObjectSolverConstraint *)con->data;
+
+                                               if(data->invmat[3][3]==0.0f)
+                                                       unit_m4(data->invmat);
+                                       }
+                               }
+                       }
+               }
+               {
+               /* Warn the user if he is using ["Text"] properties for Font objects */
+                       Object *ob;
+                       bProperty *prop;
+
+                       for (ob= main->object.first; ob; ob= ob->id.next) {
+                               if (ob->type == OB_FONT) {
+                                       prop = get_ob_property(ob, "Text");
+                                       if (prop) {
+                                               BKE_reportf_wrap(fd->reports, RPT_WARNING,
+                                                                "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);
+                                       }
+                               }
+                       }
+               }
+               {
+                       /* 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! */
        /* WATCH IT 2!: Userdef struct init has to be in editors/interface/resources.c! */
@@ -14278,8 +14508,9 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                if(fd==NULL) {
 
                                        /* printf and reports for now... its important users know this */
-                                       BKE_reportf(basefd->reports, RPT_INFO, "read library:  '%s', '%s'\n", mainptr->curlib->filepath, mainptr->curlib->name);
-                                       if(!G.background && basefd->reports) printf("read library: '%s', '%s'\n", mainptr->curlib->filepath, mainptr->curlib->name);
+                                       BKE_reportf_wrap(basefd->reports, RPT_INFO,
+                                                        "read library:  '%s', '%s'\n",
+                                                        mainptr->curlib->filepath, mainptr->curlib->name);
 
                                        fd= blo_openblenderfile(mainptr->curlib->filepath, basefd->reports);
                                        
@@ -14324,8 +14555,9 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                        else mainptr->curlib->filedata= NULL;
 
                                        if (fd==NULL) {
-                                               BKE_reportf(basefd->reports, RPT_ERROR, "Can't find lib '%s'\n", mainptr->curlib->filepath);
-                                               if(!G.background && basefd->reports) printf("ERROR: can't find lib %s \n", mainptr->curlib->filepath);
+                                               BKE_reportf_wrap(basefd->reports, RPT_ERROR,
+                                                                "Can't find lib '%s'\n",
+                                                                mainptr->curlib->filepath);
                                        }
                                }
                                if(fd) {
@@ -14342,8 +14574,10 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
 
                                                                append_id_part(fd, mainptr, id, &realid);
                                                                if (!realid) {
-                                                                       BKE_reportf(fd->reports, RPT_ERROR, "LIB ERROR: %s:'%s' missing from '%s'\n", BKE_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
-                                                                       if(!G.background && basefd->reports) printf("LIB ERROR: %s:'%s' missing from '%s'\n", BKE_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
+                                                                       BKE_reportf_wrap(fd->reports, RPT_ERROR,
+                                                                                        "LIB ERROR: %s:'%s' missing from '%s'\n",
+                                                                                        BKE_idcode_to_name(GS(id->name)),
+                                                                                        id->name+2, mainptr->curlib->filepath);
                                                                }
                                                                
                                                                change_idid_adr(mainlist, basefd, id, realid);
@@ -14379,13 +14613,9 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                ID *idn= id->next;
                                if(id->flag & LIB_READ) {
                                        BLI_remlink(lbarray[a], id);
-                                       BKE_reportf(basefd->reports, RPT_ERROR,
-                                                   "LIB ERROR: %s:'%s' unread libblock missing from '%s'\n",
-                                                   BKE_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
-                                       if (!G.background && basefd->reports) {
-                                               printf("LIB ERROR: %s:'%s' unread libblock missing from '%s'\n",
-                                                      BKE_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
-                                       }
+                                       BKE_reportf_wrap(basefd->reports, RPT_ERROR,
+                                                        "LIB ERROR: %s:'%s' unread libblock missing from '%s'\n",
+                                                        BKE_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
                                        change_idid_adr(mainlist, basefd, id, NULL);
 
                                        MEM_freeN(id);