Merged changes in the trunk up to revision 54802.
[blender.git] / source / blender / blenloader / intern / writefile.c
index cee61858467064b1ff089f7f419accbc79777c1d..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"
 #include "BLI_bitmap.h"
 #include "BLI_blenlib.h"
 #include "BLI_linklist.h"
-#include "BLI_bpath.h"
+#include "BKE_bpath.h"
 #include "BLI_math.h"
 #include "BLI_utildefines.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);
@@ -1228,7 +1238,7 @@ static void write_constraints(WriteData *wd, ListBase *conlist)
        bConstraint *con;
 
        for (con=conlist->first; con; con=con->next) {
-               bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
+               bConstraintTypeInfo *cti= BKE_constraint_get_typeinfo(con);
                
                /* Write the specific data */
                if (cti && con->data) {
@@ -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);
                }
@@ -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)
@@ -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 */
@@ -2395,6 +2437,7 @@ static void write_screens(WriteData *wd, ListBase *scrbase)
                for (sa= sc->areabase.first; sa; sa= sa->next) {
                        SpaceLink *sl;
                        Panel *pa;
+                       uiList *ui_list;
                        ARegion *ar;
                        
                        writestruct(wd, DATA, "ScrArea", 1, sa);
@@ -2404,6 +2447,9 @@ static void write_screens(WriteData *wd, ListBase *scrbase)
                                
                                for (pa= ar->panels.first; pa; pa= pa->next)
                                        writestruct(wd, DATA, "Panel", 1, pa);
+                               
+                               for (ui_list = ar->ui_lists.first; ui_list; ui_list = ui_list->next)
+                                       writestruct(wd, DATA, "uiList", 1, ui_list);
                        }
                        
                        sl= sa->spacedata.first;
@@ -2531,20 +2577,35 @@ static void write_libraries(WriteData *wd, Main *main)
                a=tot= set_listbasepointers(main, lbarray);
 
                /* test: is lib being used */
-               foundone = FALSE;
-               while (tot--) {
-                       for (id= lbarray[tot]->first; id; id= id->next) {
-                               if (id->us>0 && (id->flag & LIB_EXTERN)) {
-                                       foundone = TRUE;
-                                       break;
+               if (main->curlib && main->curlib->packedfile)
+                       foundone = TRUE;
+               else {
+                       foundone = FALSE;
+                       while (tot--) {
+                               for (id= lbarray[tot]->first; id; id= id->next) {
+                                       if (id->us>0 && (id->flag & LIB_EXTERN)) {
+                                               foundone = TRUE;
+                                               break;
+                                       }
                                }
+                               if (foundone) break;
                        }
-                       if (foundone) break;
                }
-
+               
+               /* 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) {
+                               PackedFile *pf = main->curlib->packedfile;
+                               writestruct(wd, DATA, "PackedFile", 1, pf);
+                               writedata(wd, DATA, pf->size, pf->data);
+                               if (wd->current == NULL)
+                                       printf("write packed .blend: %s\n", main->curlib->name);
+                       }
+                       
                        while (a--) {
                                for (id= lbarray[a]->first; id; id= id->next) {
                                        if (id->us>0 && (id->flag & LIB_EXTERN)) {
@@ -2863,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 */
@@ -2969,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) {
@@ -3031,7 +3298,7 @@ int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportL
 
        /* path backup/restore */
        void     *path_list_backup = NULL;
-       const int path_list_flag = (BLI_BPATH_TRAVERSE_SKIP_LIBRARY | BLI_BPATH_TRAVERSE_SKIP_MULTIFILE);
+       const int path_list_flag = (BKE_BPATH_TRAVERSE_SKIP_LIBRARY | BKE_BPATH_TRAVERSE_SKIP_MULTIFILE);
 
        /* open temporary file, so we preserve the original in case we crash */
        BLI_snprintf(tempname, sizeof(tempname), "%s@", filepath);
@@ -3044,7 +3311,7 @@ int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportL
 
        /* check if we need to backup and restore paths */
        if (UNLIKELY((write_flags & G_FILE_RELATIVE_REMAP) && (G_FILE_SAVE_COPY & write_flags))) {
-               path_list_backup = BLI_bpath_list_backup(mainvar, path_list_flag);
+               path_list_backup = BKE_bpath_list_backup(mainvar, path_list_flag);
        }
 
        /* remapping of relative paths to new file location */
@@ -3067,7 +3334,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. */
-                               BLI_bpath_absolute_convert(mainvar, G.main->name, NULL);
+                               BKE_bpath_absolute_convert(mainvar, G.main->name, NULL);
                        }
                }
        }
@@ -3075,15 +3342,15 @@ int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportL
        write_user_block= write_flags & G_FILE_USERPREFS;
 
        if (write_flags & G_FILE_RELATIVE_REMAP)
-               BLI_bpath_relative_convert(mainvar, filepath, NULL); /* note, making relative to something OTHER then G.main->name */
+               BKE_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);
        close(file);
 
        if (UNLIKELY(path_list_backup)) {
-               BLI_bpath_list_restore(mainvar, path_list_flag, path_list_backup);
-               BLI_bpath_list_free(path_list_backup);
+               BKE_bpath_list_restore(mainvar, path_list_flag, path_list_backup);
+               BKE_bpath_list_free(path_list_backup);
        }
 
        if (err) {