Merged changes in the trunk up to revision 54802.
[blender.git] / source / blender / blenloader / intern / writefile.c
index b1dce046438ddda5240be48d397e509817e3abd3..50e1e229ebac89449eafb47f9e1ab5542be33568 100644 (file)
@@ -89,6 +89,8 @@
 #  include "BLI_winstuff.h"
 #endif
 
+#include "BLI_utildefines.h"
+
 /* allow writefile to use deprecated functionality (for forward compatibility code) */
 #define DNA_DEPRECATED_ALLOW
 
 #include "DNA_key_types.h"
 #include "DNA_lattice_types.h"
 #include "DNA_lamp_types.h"
+#ifdef WITH_FREESTYLE
+#  include "DNA_linestyle_types.h"
+#endif
 #include "DNA_meta_types.h"
 #include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_packedFile_types.h"
 #include "DNA_particle_types.h"
 #include "DNA_property_types.h"
+#include "DNA_rigidbody_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_sdna_types.h"
 #include "DNA_sequence_types.h"
@@ -197,7 +203,7 @@ static WriteData *writedata_new(int file)
 
        if (wd == NULL) return NULL;
 
-       wd->sdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
+       wd->sdna = DNA_sdna_from_data(DNAstr, DNAlen, false);
 
        wd->file= file;
 
@@ -850,8 +856,12 @@ static void write_userdef(WriteData *wd)
                        write_keymapitem(wd, kmi);
        }
 
-       for (bext= U.addons.first; bext; bext=bext->next)
+       for (bext= U.addons.first; bext; bext=bext->next) {
                writestruct(wd, DATA, "bAddon", 1, bext);
+               if (bext->prop) {
+                       IDP_WriteProperty(bext->prop, wd);
+               }
+       }
        
        for (style= U.uistyles.first; style; style= style->next) {
                writestruct(wd, DATA, "uiStyle", 1, style);
@@ -1416,8 +1426,8 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
                        int size = mmd->dyngridsize;
 
                        writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->bindinfluences);
-                       writedata(wd, DATA, sizeof(int)*(mmd->totvert+1), mmd->bindoffsets);
-                       writedata(wd, DATA, sizeof(float)*3*mmd->totcagevert,
+                       writedata(wd, DATA, sizeof(int) * (mmd->totvert + 1), mmd->bindoffsets);
+                       writedata(wd, DATA, sizeof(float) * 3 * mmd->totcagevert,
                                mmd->bindcagecos);
                        writestruct(wd, DATA, "MDefCell", size*size*size, mmd->dyngrid);
                        writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->dyninfluences);
@@ -1483,6 +1493,14 @@ static void write_objects(WriteData *wd, ListBase *idbase)
                        }
                        writestruct(wd, DATA, "BulletSoftBody", 1, ob->bsoft);
                        
+                       if (ob->rigidbody_object) {
+                               // TODO: if any extra data is added to handle duplis, will need separate function then
+                               writestruct(wd, DATA, "RigidBodyOb", 1, ob->rigidbody_object);
+                       }
+                       if (ob->rigidbody_constraint) {
+                               writestruct(wd, DATA, "RigidBodyCon", 1, ob->rigidbody_constraint);
+                       }
+
                        write_particlesystems(wd, &ob->particlesystem);
                        write_modifiers(wd, &ob->modifiers);
                }
@@ -1508,7 +1526,7 @@ static void write_vfonts(WriteData *wd, ListBase *idbase)
 
                        /* direct data */
 
-                       if (vf->packedfile && !wd->current) {
+                       if (vf->packedfile) {
                                pf = vf->packedfile;
                                writestruct(wd, DATA, "PackedFile", 1, pf);
                                writedata(wd, DATA, pf->size, pf->data);
@@ -1674,7 +1692,7 @@ static void write_mdisps(WriteData *wd, int count, MDisps *mdlist, int external)
                        MDisps *md = &mdlist[i];
                        if (md->disps) {
                                if (!external)
-                                       writedata(wd, DATA, sizeof(float)*3*md->totdisp, md->disps);
+                                       writedata(wd, DATA, sizeof(float) * 3 * md->totdisp, md->disps);
                        }
                        
                        if (md->hidden)
@@ -1958,7 +1976,7 @@ static void write_images(WriteData *wd, ListBase *idbase)
                        writestruct(wd, ID_IM, "Image", 1, ima);
                        if (ima->id.properties) IDP_WriteProperty(ima->id.properties, wd);
 
-                       if (ima->packedfile && !wd->current) {
+                       if (ima->packedfile) {
                                pf = ima->packedfile;
                                writestruct(wd, DATA, "PackedFile", 1, pf);
                                writedata(wd, DATA, pf->size, pf->data);
@@ -2283,8 +2301,25 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
                for (ts = sce->transform_spaces.first; ts; ts = ts->next)
                        writestruct(wd, DATA, "TransformOrientation", 1, ts);
                
-               for (srl= sce->r.layers.first; srl; srl= srl->next)
+               for (srl = sce->r.layers.first; srl; srl = srl->next) {
                        writestruct(wd, DATA, "SceneRenderLayer", 1, srl);
+                       
+#ifdef WITH_FREESTYLE
+                       {
+                               FreestyleModuleConfig *fmc;
+                               FreestyleLineSet *fls;
+
+                               for(fmc = srl->freestyleConfig.modules.first; fmc; fmc = fmc->next) {
+                                       writestruct(wd, DATA, "FreestyleModuleConfig", 1, fmc);
+                               }
+                       
+                               for(fls = srl->freestyleConfig.linesets.first; fls; fls = fls->next) {
+                                       writestruct(wd, DATA, "FreestyleLineSet", 1, fls);
+                               }
+
+                       }
+#endif
+               }
                
                if (sce->nodetree) {
                        writestruct(wd, DATA, "bNodeTree", 1, sce->nodetree);
@@ -2292,7 +2327,14 @@ static void write_scenes(WriteData *wd, ListBase *scebase)
                }
 
                write_view_settings(wd, &sce->view_settings);
-
+               
+               /* writing RigidBodyWorld data to the blend file */
+               if (sce->rigidbody_world) {
+                       writestruct(wd, DATA, "RigidBodyWorld", 1, sce->rigidbody_world);
+                       writestruct(wd, DATA, "EffectorWeights", 1, sce->rigidbody_world->effector_weights);
+                       write_pointcaches(wd, &(sce->rigidbody_world->ptcaches));
+               }
+               
                sce= sce->id.next;
        }
        /* flush helps the compression for undo-save */
@@ -2550,14 +2592,18 @@ static void write_libraries(WriteData *wd, Main *main)
                        }
                }
                
+               /* to be able to restore quit.blend and temp saves, the packed blend has to be in undo buffers... */
+               /* XXX needs rethink, just like save UI in undo files now - would be nice to append things only for the]
+                  quit.blend and temp saves */
                if (foundone) {
                        writestruct(wd, ID_LI, "Library", 1, main->curlib);
 
-                       if (main->curlib->packedfile && !wd->current) {
+                       if (main->curlib->packedfile) {
                                PackedFile *pf = main->curlib->packedfile;
                                writestruct(wd, DATA, "PackedFile", 1, pf);
                                writedata(wd, DATA, pf->size, pf->data);
-                               printf("write packed .blend: %s\n", main->curlib->name);
+                               if (wd->current == NULL)
+                                       printf("write packed .blend: %s\n", main->curlib->name);
                        }
                        
                        while (a--) {
@@ -2688,7 +2734,7 @@ static void write_sounds(WriteData *wd, ListBase *idbase)
                        writestruct(wd, ID_SO, "bSound", 1, sound);
                        if (sound->id.properties) IDP_WriteProperty(sound->id.properties, wd);
 
-                       if (sound->packedfile && !wd->current) {
+                       if (sound->packedfile) {
                                pf = sound->packedfile;
                                writestruct(wd, DATA, "PackedFile", 1, pf);
                                writedata(wd, DATA, pf->size, pf->data);
@@ -2878,6 +2924,209 @@ static void write_masks(WriteData *wd, ListBase *idbase)
        mywrite(wd, MYWRITE_FLUSH, 0);
 }
 
+#ifdef WITH_FREESTYLE
+static void write_linestyle_color_modifiers(WriteData *wd, ListBase *modifiers)
+{
+       LineStyleModifier *m;
+       const char *struct_name;
+
+       for (m = modifiers->first; m; m = m->next) {
+               switch (m->type) {
+               case LS_MODIFIER_ALONG_STROKE:
+                       struct_name = "LineStyleColorModifier_AlongStroke";
+                       break;
+               case LS_MODIFIER_DISTANCE_FROM_CAMERA:
+                       struct_name = "LineStyleColorModifier_DistanceFromCamera";
+                       break;
+               case LS_MODIFIER_DISTANCE_FROM_OBJECT:
+                       struct_name = "LineStyleColorModifier_DistanceFromObject";
+                       break;
+               case LS_MODIFIER_MATERIAL:
+                       struct_name = "LineStyleColorModifier_Material";
+                       break;
+               default:
+                       struct_name = "LineStyleColorModifier"; /* this should not happen */
+               }
+               writestruct(wd, DATA, struct_name, 1, m);
+       }
+       for (m = modifiers->first; m; m = m->next) {
+               switch (m->type) {
+               case LS_MODIFIER_ALONG_STROKE:
+                       writestruct(wd, DATA, "ColorBand", 1, ((LineStyleColorModifier_AlongStroke *)m)->color_ramp);
+                       break;
+               case LS_MODIFIER_DISTANCE_FROM_CAMERA:
+                       writestruct(wd, DATA, "ColorBand", 1, ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp);
+                       break;
+               case LS_MODIFIER_DISTANCE_FROM_OBJECT:
+                       writestruct(wd, DATA, "ColorBand", 1, ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp);
+                       break;
+               case LS_MODIFIER_MATERIAL:
+                       writestruct(wd, DATA, "ColorBand", 1, ((LineStyleColorModifier_Material *)m)->color_ramp);
+                       break;
+               }
+       }
+}
+
+static void write_linestyle_alpha_modifiers(WriteData *wd, ListBase *modifiers)
+{
+       LineStyleModifier *m;
+       const char *struct_name;
+
+       for (m = modifiers->first; m; m = m->next) {
+               switch (m->type) {
+               case LS_MODIFIER_ALONG_STROKE:
+                       struct_name = "LineStyleAlphaModifier_AlongStroke";
+                       break;
+               case LS_MODIFIER_DISTANCE_FROM_CAMERA:
+                       struct_name = "LineStyleAlphaModifier_DistanceFromCamera";
+                       break;
+               case LS_MODIFIER_DISTANCE_FROM_OBJECT:
+                       struct_name = "LineStyleAlphaModifier_DistanceFromObject";
+                       break;
+               case LS_MODIFIER_MATERIAL:
+                       struct_name = "LineStyleAlphaModifier_Material";
+                       break;
+               default:
+                       struct_name = "LineStyleAlphaModifier"; /* this should not happen */
+               }
+               writestruct(wd, DATA, struct_name, 1, m);
+       }
+       for (m = modifiers->first; m; m = m->next) {
+               switch (m->type) {
+               case LS_MODIFIER_ALONG_STROKE:
+                       write_curvemapping(wd, ((LineStyleAlphaModifier_AlongStroke *)m)->curve);
+                       break;
+               case LS_MODIFIER_DISTANCE_FROM_CAMERA:
+                       write_curvemapping(wd, ((LineStyleAlphaModifier_DistanceFromCamera *)m)->curve);
+                       break;
+               case LS_MODIFIER_DISTANCE_FROM_OBJECT:
+                       write_curvemapping(wd, ((LineStyleAlphaModifier_DistanceFromObject *)m)->curve);
+                       break;
+               case LS_MODIFIER_MATERIAL:
+                       write_curvemapping(wd, ((LineStyleAlphaModifier_Material *)m)->curve);
+                       break;
+               }
+       }
+}
+
+static void write_linestyle_thickness_modifiers(WriteData *wd, ListBase *modifiers)
+{
+       LineStyleModifier *m;
+       const char *struct_name;
+
+       for (m = modifiers->first; m; m = m->next) {
+               switch (m->type) {
+               case LS_MODIFIER_ALONG_STROKE:
+                       struct_name = "LineStyleThicknessModifier_AlongStroke";
+                       break;
+               case LS_MODIFIER_DISTANCE_FROM_CAMERA:
+                       struct_name = "LineStyleThicknessModifier_DistanceFromCamera";
+                       break;
+               case LS_MODIFIER_DISTANCE_FROM_OBJECT:
+                       struct_name = "LineStyleThicknessModifier_DistanceFromObject";
+                       break;
+               case LS_MODIFIER_MATERIAL:
+                       struct_name = "LineStyleThicknessModifier_Material";
+                       break;
+               case LS_MODIFIER_CALLIGRAPHY:
+                       struct_name = "LineStyleThicknessModifier_Calligraphy";
+                       break;
+               default:
+                       struct_name = "LineStyleThicknessModifier"; /* this should not happen */
+               }
+               writestruct(wd, DATA, struct_name, 1, m);
+       }
+       for (m = modifiers->first; m; m = m->next) {
+               switch (m->type) {
+               case LS_MODIFIER_ALONG_STROKE:
+                       write_curvemapping(wd, ((LineStyleThicknessModifier_AlongStroke *)m)->curve);
+                       break;
+               case LS_MODIFIER_DISTANCE_FROM_CAMERA:
+                       write_curvemapping(wd, ((LineStyleThicknessModifier_DistanceFromCamera *)m)->curve);
+                       break;
+               case LS_MODIFIER_DISTANCE_FROM_OBJECT:
+                       write_curvemapping(wd, ((LineStyleThicknessModifier_DistanceFromObject *)m)->curve);
+                       break;
+               case LS_MODIFIER_MATERIAL:
+                       write_curvemapping(wd, ((LineStyleThicknessModifier_Material *)m)->curve);
+                       break;
+               }
+       }
+}
+
+static void write_linestyle_geometry_modifiers(WriteData *wd, ListBase *modifiers)
+{
+       LineStyleModifier *m;
+       const char *struct_name;
+
+       for (m = modifiers->first; m; m = m->next) {
+               switch (m->type) {
+               case LS_MODIFIER_SAMPLING:
+                       struct_name = "LineStyleGeometryModifier_Sampling";
+                       break;
+               case LS_MODIFIER_BEZIER_CURVE:
+                       struct_name = "LineStyleGeometryModifier_BezierCurve";
+                       break;
+               case LS_MODIFIER_SINUS_DISPLACEMENT:
+                       struct_name = "LineStyleGeometryModifier_SinusDisplacement";
+                       break;
+               case LS_MODIFIER_SPATIAL_NOISE:
+                       struct_name = "LineStyleGeometryModifier_SpatialNoise";
+                       break;
+               case LS_MODIFIER_PERLIN_NOISE_1D:
+                       struct_name = "LineStyleGeometryModifier_PerlinNoise1D";
+                       break;
+               case LS_MODIFIER_PERLIN_NOISE_2D:
+                       struct_name = "LineStyleGeometryModifier_PerlinNoise2D";
+                       break;
+               case LS_MODIFIER_BACKBONE_STRETCHER:
+                       struct_name = "LineStyleGeometryModifier_BackboneStretcher";
+                       break;
+               case LS_MODIFIER_TIP_REMOVER:
+                       struct_name = "LineStyleGeometryModifier_TipRemover";
+                       break;
+               case LS_MODIFIER_POLYGONIZATION:
+                       struct_name = "LineStyleGeometryModifier_Polygonalization";
+                       break;
+               case LS_MODIFIER_GUIDING_LINES:
+                       struct_name = "LineStyleGeometryModifier_GuidingLines";
+                       break;
+               case LS_MODIFIER_BLUEPRINT:
+                       struct_name = "LineStyleGeometryModifier_Blueprint";
+                       break;
+               case LS_MODIFIER_2D_OFFSET:
+                       struct_name = "LineStyleGeometryModifier_2DOffset";
+                       break;
+               case LS_MODIFIER_2D_TRANSFORM:
+                       struct_name = "LineStyleGeometryModifier_2DTransform";
+                       break;
+               default:
+                       struct_name = "LineStyleGeometryModifier"; /* this should not happen */
+               }
+               writestruct(wd, DATA, struct_name, 1, m);
+       }
+}
+
+static void write_linestyles(WriteData *wd, ListBase *idbase)
+{
+       FreestyleLineStyle *linestyle;
+
+       for (linestyle = idbase->first; linestyle; linestyle = linestyle->id.next) {
+               if (linestyle->id.us>0 || wd->current) {
+                       writestruct(wd, ID_LS, "FreestyleLineStyle", 1, linestyle);
+                       if (linestyle->id.properties)
+                               IDP_WriteProperty(linestyle->id.properties, wd);
+                       if (linestyle->adt)
+                               write_animdata(wd, linestyle->adt);
+                       write_linestyle_color_modifiers(wd, &linestyle->color_modifiers);
+                       write_linestyle_alpha_modifiers(wd, &linestyle->alpha_modifiers);
+                       write_linestyle_thickness_modifiers(wd, &linestyle->thickness_modifiers);
+                       write_linestyle_geometry_modifiers(wd, &linestyle->geometry_modifiers);
+               }
+       }
+}
+#endif
+
 /* 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 */
@@ -2984,6 +3233,9 @@ static int write_file_handle(Main *mainvar, int handle, MemFile *compare, MemFil
        write_brushes  (wd, &mainvar->brush);
        write_scripts  (wd, &mainvar->script);
        write_gpencils (wd, &mainvar->gpencil);
+#ifdef WITH_FREESTYLE
+       write_linestyles(wd, &mainvar->linestyle);
+#endif
        write_libraries(wd,  mainvar->next);
 
        if (write_user_block) {