style cleanup
[blender.git] / source / blender / blenloader / intern / writefile.c
index 5c03fe0a6d7bbf1c7296e632d1ccbf26e217c3e1..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>
@@ -153,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"
@@ -162,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"
 
@@ -645,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);
@@ -715,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);
@@ -723,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);
                }
@@ -734,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)
@@ -774,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));
@@ -1374,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;
@@ -1655,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;
@@ -1677,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) {
@@ -1784,7 +1822,7 @@ static void write_meshs(WriteData *wd, ListBase *idbase)
                                /* backup */
 
 
-                               /* now fill in polys to mfaces*/
+                               /* 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);
 
@@ -1927,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) {
@@ -2056,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)
 {
@@ -2111,7 +2181,7 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
                        
                        SEQ_BEGIN (ed, seq)
                        {
-                               if (seq->strip) seq->strip->done= 0;
+                               if (seq->strip) seq->strip->done = FALSE;
                                writestruct(wd, DATA, "Sequence", 1, seq);
                        }
                        SEQ_END
@@ -2121,22 +2191,21 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
                                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;
                                                }
@@ -2153,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
                                
@@ -2201,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 */
@@ -2437,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;
                                }
                        }
@@ -2462,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;
 
@@ -2731,37 +2801,42 @@ static void write_masks(WriteData *wd, ListBase *idbase)
        mask = idbase->first;
        while (mask) {
                if (mask->id.us > 0 || wd->current) {
-                       MaskShape *shape;
+                       MaskLayer *masklay;
 
                        writestruct(wd, ID_MSK, "Mask", 1, mask);
 
                        if (mask->adt)
                                write_animdata(wd, mask->adt);
 
-                       shape = mask->shapes.first;
-                       while (shape) {
+                       for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
                                MaskSpline *spline;
+                               MaskLayerShape *masklay_shape;
 
-                               writestruct(wd, DATA, "MaskShape", 1, shape);
+                               writestruct(wd, DATA, "MaskLayer", 1, masklay);
 
-                               spline = shape->splines.first;
-                               while (spline) {
+                               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);
                                        }
-
-                                       spline = spline->next;
                                }
 
-                               shape = shape->next;
+                               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);
+                               }
                        }
                }
 
@@ -2948,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;
        }
 
@@ -2999,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;
                }
        }
@@ -3016,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;
        }