style cleanup
[blender.git] / source / blender / blenloader / intern / writefile.c
index 43f35462c7b72bf50c41c17999b503b49b9df971..b73485add3db1f1831d1897642aa79b0314a8d92 100644 (file)
 
 
 /*
-FILEFORMAT: IFF-style structure  (but not IFF compatible!)
-
-start file:
      BLENDER_V100    12 bytes  (versie 1.00)
                                      V = big endian, v = little endian
                                      _ = 4 byte pointer, - = 8 byte pointer
-
-datablocks:            also see struct BHead
      <bh.code>                       4 chars
      <bh.len>                        int,  len data after BHead
      <bh.old>                        void,  old pointer
      <bh.SDNAnr>                     int
      <bh.nr>                         int, in case of array: amount of structs
      data
      ...
      ...
-
-Almost all data in Blender are structures. Each struct saved
-gets a BHead header.  With BHead the struct can be linked again
-and compared with StructDNA .
-
-WRITE
-
-Preferred writing order: (not really a must, but why would you do it random?)
-Any case: direct data is ALWAYS after the lib block
-
-(Local file data)
-- for each LibBlock
      - write LibBlock
      - write associated direct data
-(External file data)
-- per library
      - write library block
      - per LibBlock
              - write the ID of LibBlock
-- write TEST (128x128, blend file preview, optional)
-- write FileGlobal (some global vars)
-- write SDNA
-- write USER if filename is ~/X.XX/config/startup.blend
-*/
+ * FILEFORMAT: IFF-style structure  (but not IFF compatible!)
+ *
+ * start file:
*     BLENDER_V100    12 bytes  (versie 1.00)
*                     V = big endian, v = little endian
*                     _ = 4 byte pointer, - = 8 byte pointer
+ *
+ * datablocks:     also see struct BHead
*     <bh.code>           4 chars
*     <bh.len>            int,  len data after BHead
*     <bh.old>            void,  old pointer
*     <bh.SDNAnr>         int
*     <bh.nr>             int, in case of array: amount of structs
*     data
*     ...
*     ...
+ *
+ * Almost all data in Blender are structures. Each struct saved
+ * gets a BHead header.  With BHead the struct can be linked again
+ * and compared with StructDNA .
+ *
+ * WRITE
+ *
+ * Preferred writing order: (not really a must, but why would you do it random?)
+ * Any case: direct data is ALWAYS after the lib block
+ *
+ * (Local file data)
+ * - for each LibBlock
*     - write LibBlock
*     - write associated direct data
+ * (External file data)
+ * - per library
*     - write library block
*     - per LibBlock
*         - write the ID of LibBlock
+ * - write TEST (128x128, blend file preview, optional)
+ * - write FileGlobal (some global vars)
+ * - write SDNA
+ * - write USER if filename is ~/X.XX/config/startup.blend
+ */
 
 
 #include <math.h>
@@ -133,6 +133,7 @@ Any case: direct data is ALWAYS after the lib block
 #include "DNA_world_types.h"
 #include "DNA_windowmanager_types.h"
 #include "DNA_movieclip_types.h"
+#include "DNA_mask_types.h"
 
 #include "MEM_guardedalloc.h" // MEM_freeN
 #include "BLI_bitmap.h"
@@ -152,7 +153,7 @@ Any case: direct data is ALWAYS after the lib block
 #include "BKE_node.h"
 #include "BKE_report.h"
 #include "BKE_sequencer.h"
-#include "BKE_utildefines.h"
+#include "BKE_subsurf.h"
 #include "BKE_modifier.h"
 #include "BKE_fcurve.h"
 #include "BKE_pointcache.h"
@@ -161,6 +162,7 @@ Any case: direct data is ALWAYS after the lib block
 #include "BLO_writefile.h"
 #include "BLO_readfile.h"
 #include "BLO_undofile.h"
+#include "BLO_blend_defs.h"
 
 #include "readfile.h"
 
@@ -644,15 +646,21 @@ static void write_animdata(WriteData *wd, AnimData *adt)
        write_nladata(wd, &adt->nla_tracks);
 }
 
-static void write_curvemapping(WriteData *wd, CurveMapping *cumap)
+static void write_curvemapping_curves(WriteData *wd, CurveMapping *cumap)
 {
        int a;
-       
-       writestruct(wd, DATA, "CurveMapping", 1, cumap);
-       for (a=0; a<CM_TOT; a++)
+
+       for (a = 0; a < CM_TOT; a++)
                writestruct(wd, DATA, "CurveMapPoint", cumap->cm[a].totpoint, cumap->cm[a].curve);
 }
 
+static void write_curvemapping(WriteData *wd, CurveMapping *cumap)
+{
+       writestruct(wd, DATA, "CurveMapping", 1, cumap);
+
+       write_curvemapping_curves(wd, cumap);
+}
+
 static void write_node_socket(WriteData *wd, bNodeSocket *sock)
 {
        bNodeSocketType *stype= ntreeGetSocketType(sock->type);
@@ -714,7 +722,7 @@ static void write_nodetree(WriteData *wd, bNodeTree *ntree)
                        write_node_socket(wd, sock);
 
                
-               if (node->storage && node->type!=NODE_DYNAMIC) {
+               if (node->storage) {
                        /* could be handlerized at some point, now only 1 exception still */
                        if (ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
                                write_curvemapping(wd, node->storage);
@@ -722,8 +730,9 @@ static void write_nodetree(WriteData *wd, bNodeTree *ntree)
                                write_curvemapping(wd, node->storage);
                        else if (ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) )
                                write_curvemapping(wd, node->storage);
-                       else if (ntree->type==NTREE_COMPOSIT && node->type==CMP_NODE_MOVIEDISTORTION)
-                               /* pass */;
+                       else if (ntree->type==NTREE_COMPOSIT && node->type==CMP_NODE_MOVIEDISTORTION) {
+                               /* pass */
+                       }
                        else
                                writestruct(wd, DATA, node->typeinfo->storagename, 1, node->storage);
                }
@@ -733,6 +742,11 @@ static void write_nodetree(WriteData *wd, bNodeTree *ntree)
                        for (sock=node->inputs.first; sock; sock=sock->next)
                                writestruct(wd, DATA, "NodeImageMultiFileSocket", 1, sock->storage);
                }
+               if (node->type==CMP_NODE_IMAGE) {
+                       /* write extra socket info */
+                       for (sock=node->outputs.first; sock; sock=sock->next)
+                               writestruct(wd, DATA, "NodeImageLayer", 1, sock->storage);
+               }
        }
        
        for (link= ntree->links.first; link; link= link->next)
@@ -773,7 +787,7 @@ static void write_renderinfo(WriteData *wd, Main *mainvar)          /* for renderdeamon
        current_screen_compat(mainvar, &curscreen);
 
        for (sce= mainvar->scene.first; sce; sce= sce->id.next) {
-               if (sce->id.lib==NULL  && ( sce==curscreen->scene || (sce->r.scemode & R_BG_RENDER)) ) {
+               if (sce->id.lib == NULL && (sce == curscreen->scene || (sce->r.scemode & R_BG_RENDER))) {
                        data.sfra = sce->r.sfra;
                        data.efra = sce->r.efra;
                        memset(data.scene_name, 0, sizeof(data.scene_name));
@@ -1271,7 +1285,7 @@ static void write_pose(WriteData *wd, bPose *pose)
 
        /* write IK param */
        if (pose->ikparam) {
-               char *structname = (char *)get_ikparam_name(pose);
+               char *structname = (char *)BKE_pose_ikparam_get_name(pose);
                if (structname)
                        writestruct(wd, DATA, structname, 1, pose->ikparam);
        }
@@ -1373,14 +1387,14 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
                } 
                else if (md->type==eModifierType_Collision) {
                        
-                       /*
+#if 0
                        CollisionModifierData *collmd = (CollisionModifierData*) md;
                        // TODO: CollisionModifier should use pointcache 
                        // + have proper reset events before enabling this
                        writestruct(wd, DATA, "MVert", collmd->numverts, collmd->x);
                        writestruct(wd, DATA, "MVert", collmd->numverts, collmd->xnew);
                        writestruct(wd, DATA, "MFace", collmd->numfaces, collmd->mfaces);
-                       */
+#endif
                }
                else if (md->type==eModifierType_MeshDeform) {
                        MeshDeformModifierData *mmd = (MeshDeformModifierData*) md;
@@ -1654,6 +1668,24 @@ static void write_mdisps(WriteData *wd, int count, MDisps *mdlist, int external)
        }
 }
 
+static void write_grid_paint_mask(WriteData *wd, int count, GridPaintMask *grid_paint_mask)
+{
+       if (grid_paint_mask) {
+               int i;
+               
+               writestruct(wd, DATA, "GridPaintMask", count, grid_paint_mask);
+               for (i = 0; i < count; ++i) {
+                       GridPaintMask *gpm = &grid_paint_mask[i];
+                       if (gpm->data) {
+                               const int gridsize = ccg_gridsize(gpm->level);
+                               writedata(wd, DATA,
+                                         sizeof(*gpm->data) * gridsize * gridsize,
+                                         gpm->data);
+                       }
+               }
+       }
+}
+
 static void write_customdata(WriteData *wd, ID *id, int count, CustomData *data, int partial_type, int partial_count)
 {
        int i;
@@ -1676,6 +1708,13 @@ static void write_customdata(WriteData *wd, ID *id, int count, CustomData *data,
                else if (layer->type == CD_MDISPS) {
                        write_mdisps(wd, count, layer->data, layer->flag & CD_FLAG_EXTERNAL);
                }
+               else if (layer->type == CD_PAINT_MASK) {
+                       float *layer_data = layer->data;
+                       writedata(wd, DATA, sizeof(*layer_data) * count, layer_data);
+               }
+               else if (layer->type == CD_GRID_PAINT_MASK) {
+                       write_grid_paint_mask(wd, count, layer->data);
+               }
                else {
                        CustomData_file_write_info(layer->type, &structname, &structnum);
                        if (structnum) {
@@ -1783,9 +1822,9 @@ static void write_meshs(WriteData *wd, ListBase *idbase)
                                /* backup */
 
 
-                               /* now fill in polys to mfaces*/
-                               mesh->totfacemesh_mpoly_to_mface(&mesh->fdata, &backup_mesh.ldata, &backup_mesh.pdata,
-                                                                  mesh->totface, backup_mesh.totloop, backup_mesh.totpoly);
+                               /* now fill in polys to mfaces */
+                               mesh->totface = BKE_mesh_mpoly_to_mface(&mesh->fdata, &backup_mesh.ldata, &backup_mesh.pdata,
+                                                                       mesh->totface, backup_mesh.totloop, backup_mesh.totpoly);
 
                                mesh_update_customdata_pointers(mesh, FALSE);
 
@@ -1926,7 +1965,6 @@ static void write_textures(WriteData *wd, ListBase *idbase)
                        if (tex->adt) write_animdata(wd, tex->adt);
 
                        /* direct data */
-                       if (tex->type == TEX_PLUGIN && tex->plugin) writestruct(wd, DATA, "PluginTex", 1, tex->plugin);
                        if (tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
                        if (tex->type == TEX_ENVMAP && tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
                        if (tex->type == TEX_POINTDENSITY && tex->pd) {
@@ -2055,6 +2093,39 @@ static void write_lamps(WriteData *wd, ListBase *idbase)
        }
 }
 
+static void write_sequence_modifiers(WriteData *wd, ListBase *modbase)
+{
+       SequenceModifierData *smd;
+
+       for (smd = modbase->first; smd; smd = smd->next) {
+               SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
+
+               if (smti) {
+                       writestruct(wd, DATA, smti->struct_name, 1, smd);
+
+                       if (smd->type == seqModifierType_Curves) {
+                               CurvesModifierData *cmd = (CurvesModifierData *) smd;
+
+                               write_curvemapping(wd, &cmd->curve_mapping);
+                       }
+                       else if (smd->type == seqModifierType_HueCorrect) {
+                               HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
+
+                               write_curvemapping(wd, &hcmd->curve_mapping);
+                       }
+               }
+               else {
+                       writestruct(wd, DATA, "SequenceModifierData", 1, smd);
+               }
+       }
+}
+
+static void write_view_settings(WriteData *wd, ColorManagedViewSettings *view_settings)
+{
+       if (view_settings->curve_mapping) {
+               write_curvemapping(wd, view_settings->curve_mapping);
+       }
+}
 
 static void write_scenes(WriteData *wd, ListBase *scebase)
 {
@@ -2108,32 +2179,33 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
                        
                        /* reset write flags too */
                        
-                       SEQ_BEGIN (ed, seq) {
-                               if (seq->strip) seq->strip->done= 0;
+                       SEQ_BEGIN (ed, seq)
+                       {
+                               if (seq->strip) seq->strip->done = FALSE;
                                writestruct(wd, DATA, "Sequence", 1, seq);
                        }
                        SEQ_END
                        
-                       SEQ_BEGIN (ed, seq) {
+                       SEQ_BEGIN (ed, seq)
+                       {
                                if (seq->strip && seq->strip->done==0) {
                                        /* write strip with 'done' at 0 because readfile */
                                        
-                                       if (seq->plugin) writestruct(wd, DATA, "PluginSeq", 1, seq->plugin);
                                        if (seq->effectdata) {
                                                switch (seq->type) {
-                                               case SEQ_COLOR:
+                                               case SEQ_TYPE_COLOR:
                                                        writestruct(wd, DATA, "SolidColorVars", 1, seq->effectdata);
                                                        break;
-                                               case SEQ_SPEED:
+                                               case SEQ_TYPE_SPEED:
                                                        writestruct(wd, DATA, "SpeedControlVars", 1, seq->effectdata);
                                                        break;
-                                               case SEQ_WIPE:
+                                               case SEQ_TYPE_WIPE:
                                                        writestruct(wd, DATA, "WipeVars", 1, seq->effectdata);
                                                        break;
-                                               case SEQ_GLOW:
+                                               case SEQ_TYPE_GLOW:
                                                        writestruct(wd, DATA, "GlowVars", 1, seq->effectdata);
                                                        break;
-                                               case SEQ_TRANSFORM:
+                                               case SEQ_TYPE_TRANSFORM:
                                                        writestruct(wd, DATA, "TransformVars", 1, seq->effectdata);
                                                        break;
                                                }
@@ -2150,16 +2222,15 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
                                        if (seq->flag & SEQ_USE_PROXY && strip->proxy) {
                                                writestruct(wd, DATA, "StripProxy", 1, strip->proxy);
                                        }
-                                       if (seq->flag & SEQ_USE_COLOR_BALANCE && strip->color_balance) {
-                                               writestruct(wd, DATA, "StripColorBalance", 1, strip->color_balance);
-                                       }
-                                       if (seq->type==SEQ_IMAGE)
+                                       if (seq->type==SEQ_TYPE_IMAGE)
                                                writestruct(wd, DATA, "StripElem", MEM_allocN_len(strip->stripdata) / sizeof(struct StripElem), strip->stripdata);
-                                       else if (seq->type==SEQ_MOVIE || seq->type==SEQ_RAM_SOUND || seq->type == SEQ_HD_SOUND)
+                                       else if (seq->type==SEQ_TYPE_MOVIE || seq->type==SEQ_TYPE_SOUND_RAM || seq->type == SEQ_TYPE_SOUND_HD)
                                                writestruct(wd, DATA, "StripElem", 1, strip->stripdata);
                                        
-                                       strip->done= 1;
+                                       strip->done = TRUE;
                                }
+
+                               write_sequence_modifiers(wd, &seq->modifiers);
                        }
                        SEQ_END
                                
@@ -2198,7 +2269,9 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
                        writestruct(wd, DATA, "bNodeTree", 1, sce->nodetree);
                        write_nodetree(wd, sce->nodetree);
                }
-               
+
+               write_view_settings(wd, &sce->view_settings);
+
                sce= sce->id.next;
        }
        /* flush helps the compression for undo-save */
@@ -2434,11 +2507,11 @@ static void write_libraries(WriteData *wd, Main *main)
                a=tot= set_listbasepointers(main, lbarray);
 
                /* test: is lib being used */
-               foundone= 0;
+               foundone = FALSE;
                while (tot--) {
                        for (id= lbarray[tot]->first; id; id= id->next) {
                                if (id->us>0 && (id->flag & LIB_EXTERN)) {
-                                       foundone= 1;
+                                       foundone = TRUE;
                                        break;
                                }
                        }
@@ -2459,7 +2532,7 @@ static void write_libraries(WriteData *wd, Main *main)
        }
 }
 
-static void write_bone(WriteData *wd, Bonebone)
+static void write_bone(WriteData *wd, Bone *bone)
 {
        Bone*   cbone;
 
@@ -2694,6 +2767,9 @@ static void write_movieclips(WriteData *wd, ListBase *idbase)
                        MovieTrackingObject *object;
                        writestruct(wd, ID_MC, "MovieClip", 1, clip);
 
+                       if (clip->id.properties)
+                               IDP_WriteProperty(clip->id.properties, wd);
+
                        if (clip->adt)
                                write_animdata(wd, clip->adt);
 
@@ -2718,6 +2794,59 @@ static void write_movieclips(WriteData *wd, ListBase *idbase)
        mywrite(wd, MYWRITE_FLUSH, 0);
 }
 
+static void write_masks(WriteData *wd, ListBase *idbase)
+{
+       Mask *mask;
+
+       mask = idbase->first;
+       while (mask) {
+               if (mask->id.us > 0 || wd->current) {
+                       MaskLayer *masklay;
+
+                       writestruct(wd, ID_MSK, "Mask", 1, mask);
+
+                       if (mask->adt)
+                               write_animdata(wd, mask->adt);
+
+                       for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
+                               MaskSpline *spline;
+                               MaskLayerShape *masklay_shape;
+
+                               writestruct(wd, DATA, "MaskLayer", 1, masklay);
+
+                               for (spline = masklay->splines.first; spline; spline = spline->next) {
+                                       int i;
+
+                                       void *points_deform = spline->points_deform;
+                                       spline->points_deform = NULL;
+
+                                       writestruct(wd, DATA, "MaskSpline", 1, spline);
+                                       writestruct(wd, DATA, "MaskSplinePoint", spline->tot_point, spline->points);
+
+                                       spline->points_deform = points_deform;
+
+                                       for (i = 0; i < spline->tot_point; i++) {
+                                               MaskSplinePoint *point = &spline->points[i];
+
+                                               if (point->tot_uw)
+                                                       writestruct(wd, DATA, "MaskSplinePointUW", point->tot_uw, point->uw);
+                                       }
+                               }
+
+                               for (masklay_shape = masklay->splines_shapes.first; masklay_shape; masklay_shape = masklay_shape->next) {
+                                       writestruct(wd, DATA, "MaskLayerShape", 1, masklay_shape);
+                                       writedata(wd, DATA, masklay_shape->tot_vert * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE, masklay_shape->data);
+                               }
+                       }
+               }
+
+               mask = mask->id.next;
+       }
+
+       /* flush helps the compression for undo-save */
+       mywrite(wd, MYWRITE_FLUSH, 0);
+}
+
 /* context is usually defined by WM, two cases where no WM is available:
  * - for forward compatibility, curscreen has to be saved
  * - for undofile, curscene needs to be saved */
@@ -2802,6 +2931,7 @@ static int write_file_handle(Main *mainvar, int handle, MemFile *compare, MemFil
                write_screens  (wd, &mainvar->screen);
        }
        write_movieclips (wd, &mainvar->movieclip);
+       write_masks    (wd, &mainvar->mask);
        write_scenes   (wd, &mainvar->scene);
        write_curves   (wd, &mainvar->curve);
        write_mballs   (wd, &mainvar->mball);
@@ -2893,7 +3023,7 @@ int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportL
 
        file = BLI_open(tempname, O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
        if (file == -1) {
-               BKE_reportf(reports, RPT_ERROR, "Can't open file %s for writing: %s.", tempname, strerror(errno));
+               BKE_reportf(reports, RPT_ERROR, "Can't open file %s for writing: %s", tempname, strerror(errno));
                return 0;
        }
 
@@ -2917,7 +3047,7 @@ int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportL
                                 * we should not have any relative paths, but if there
                                 * is somehow, an invalid or empty G.main->name it will
                                 * print an error, don't try make the absolute in this case. */
-                               makeFilesAbsolute(mainvar, G.main->name, NULL);
+                               BLI_bpath_absolute_convert(mainvar, G.main->name, NULL);
                        }
                }
        }
@@ -2926,7 +3056,7 @@ int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportL
        write_user_block= (BLI_path_cmp(filepath, userfilename) == 0);
 
        if (write_flags & G_FILE_RELATIVE_REMAP)
-               makeFilesRelative(mainvar, filepath, NULL); /* note, making relative to something OTHER then G.main->name */
+               BLI_bpath_relative_convert(mainvar, filepath, NULL); /* note, making relative to something OTHER then G.main->name */
 
        /* actual file writing */
        err= write_file_handle(mainvar, file, NULL, NULL, write_user_block, write_flags, thumb);
@@ -2944,7 +3074,7 @@ int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportL
        if (write_flags & G_FILE_HISTORY) { 
                int err_hist = do_history(filepath, reports);
                if (err_hist) {
-                       BKE_report(reports, RPT_ERROR, "Version backup failed. File saved with @");
+                       BKE_report(reports, RPT_ERROR, "Version backup failed (file saved with @)");
                        return 0;
                }
        }
@@ -2961,23 +3091,23 @@ int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportL
                if (0==ret) {
                        /* now rename to real file name, and delete temp @ file too */
                        if (BLI_rename(gzname, filepath) != 0) {
-                               BKE_report(reports, RPT_ERROR, "Can't change old file. File saved with @.");
+                               BKE_report(reports, RPT_ERROR, "Can't change old file (file saved with @)");
                                return 0;
                        }
 
                        BLI_delete(tempname, 0, 0);
                }
                else if (-1==ret) {
-                       BKE_report(reports, RPT_ERROR, "Failed opening .gz file.");
+                       BKE_report(reports, RPT_ERROR, "Failed opening .gz file");
                        return 0;
                }
                else if (-2==ret) {
-                       BKE_report(reports, RPT_ERROR, "Failed opening .blend file for compression.");
+                       BKE_report(reports, RPT_ERROR, "Failed opening .blend file for compression");
                        return 0;
                }
        }
        else if (BLI_rename(tempname, filepath) != 0) {
-               BKE_report(reports, RPT_ERROR, "Can't change old file. File saved with @");
+               BKE_report(reports, RPT_ERROR, "Can't change old file (file saved with @)");
                return 0;
        }