Logging: replace 'fwrite' w/ 'write'
[blender.git] / source / blender / blenloader / intern / writefile.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/blenloader/intern/writefile.c
28  *  \ingroup blenloader
29  */
30
31
32 /**
33  *
34  * FILE FORMAT
35  * ===========
36  *
37  * IFF-style structure  (but not IFF compatible!)
38  *
39  * start file:
40  * <pre>
41  *     BLENDER_V100    12 bytes  (versie 1.00)
42  *                     V = big endian, v = little endian
43  *                     _ = 4 byte pointer, - = 8 byte pointer
44  * </pre>
45  *
46  * datablocks: (also see struct #BHead).
47  * <pre>
48  *     <bh.code>           4 chars
49  *     <bh.len>            int,  len data after BHead
50  *     <bh.old>            void,  old pointer
51  *     <bh.SDNAnr>         int
52  *     <bh.nr>             int, in case of array: number of structs
53  *     data
54  *     ...
55  *     ...
56  * </pre>
57  *
58  * Almost all data in Blender are structures. Each struct saved
59  * gets a BHead header.  With BHead the struct can be linked again
60  * and compared with StructDNA .
61  *
62  *
63  * WRITE
64  * =====
65  *
66  * Preferred writing order: (not really a must, but why would you do it random?)
67  * Any case: direct data is ALWAYS after the lib block
68  *
69  * (Local file data)
70  * - for each LibBlock
71  *   - write LibBlock
72  *   - write associated direct data
73  * (External file data)
74  * - per library
75  *   - write library block
76  *   - per LibBlock
77  *     - write the ID of LibBlock
78  * - write #TEST (#RenderInfo struct. 128x128 blend file preview is optional).
79  * - write #GLOB (#FileGlobal struct) (some global vars).
80  * - write #DNA1 (#SDNA struct)
81  * - write #USER (#UserDef struct) if filename is ``~/.config/blender/X.XX/config/startup.blend``.
82  */
83
84
85 #include <math.h>
86 #include <fcntl.h>
87 #include <limits.h>
88 #include <stdio.h>
89 #include <string.h>
90 #include <stdlib.h>
91
92 #ifdef WIN32
93 #  include <zlib.h>  /* odd include order-issue */
94 #  include "winsock2.h"
95 #  include <io.h>
96 #  include "BLI_winstuff.h"
97 #else
98 #  include <unistd.h>  /* FreeBSD, for write() and close(). */
99 #endif
100
101 #include "BLI_utildefines.h"
102
103 /* allow writefile to use deprecated functionality (for forward compatibility code) */
104 #define DNA_DEPRECATED_ALLOW
105
106 #include "DNA_anim_types.h"
107 #include "DNA_armature_types.h"
108 #include "DNA_actuator_types.h"
109 #include "DNA_brush_types.h"
110 #include "DNA_cachefile_types.h"
111 #include "DNA_camera_types.h"
112 #include "DNA_cloth_types.h"
113 #include "DNA_constraint_types.h"
114 #include "DNA_controller_types.h"
115 #include "DNA_dynamicpaint_types.h"
116 #include "DNA_genfile.h"
117 #include "DNA_group_types.h"
118 #include "DNA_gpencil_types.h"
119 #include "DNA_fileglobal_types.h"
120 #include "DNA_key_types.h"
121 #include "DNA_lattice_types.h"
122 #include "DNA_lamp_types.h"
123 #include "DNA_linestyle_types.h"
124 #include "DNA_meta_types.h"
125 #include "DNA_mesh_types.h"
126 #include "DNA_meshdata_types.h"
127 #include "DNA_material_types.h"
128 #include "DNA_node_types.h"
129 #include "DNA_object_types.h"
130 #include "DNA_object_force_types.h"
131 #include "DNA_packedFile_types.h"
132 #include "DNA_particle_types.h"
133 #include "DNA_property_types.h"
134 #include "DNA_rigidbody_types.h"
135 #include "DNA_scene_types.h"
136 #include "DNA_sdna_types.h"
137 #include "DNA_sequence_types.h"
138 #include "DNA_sensor_types.h"
139 #include "DNA_smoke_types.h"
140 #include "DNA_space_types.h"
141 #include "DNA_screen_types.h"
142 #include "DNA_speaker_types.h"
143 #include "DNA_sound_types.h"
144 #include "DNA_text_types.h"
145 #include "DNA_view3d_types.h"
146 #include "DNA_vfont_types.h"
147 #include "DNA_world_types.h"
148 #include "DNA_windowmanager_types.h"
149 #include "DNA_movieclip_types.h"
150 #include "DNA_mask_types.h"
151
152 #include "MEM_guardedalloc.h" // MEM_freeN
153 #include "BLI_bitmap.h"
154 #include "BLI_blenlib.h"
155 #include "BLI_linklist.h"
156 #include "BLI_mempool.h"
157
158 #include "BKE_action.h"
159 #include "BKE_blender_version.h"
160 #include "BKE_bpath.h"
161 #include "BKE_curve.h"
162 #include "BKE_constraint.h"
163 #include "BKE_global.h" // for G
164 #include "BKE_idcode.h"
165 #include "BKE_library.h" // for  set_listbasepointers
166 #include "BKE_main.h"
167 #include "BKE_node.h"
168 #include "BKE_report.h"
169 #include "BKE_sequencer.h"
170 #include "BKE_subsurf.h"
171 #include "BKE_modifier.h"
172 #include "BKE_fcurve.h"
173 #include "BKE_pointcache.h"
174 #include "BKE_mesh.h"
175
176 #ifdef USE_NODE_COMPAT_CUSTOMNODES
177 #include "NOD_socket.h"  /* for sock->default_value data */
178 #endif
179
180
181 #include "BLO_writefile.h"
182 #include "BLO_readfile.h"
183 #include "BLO_undofile.h"
184 #include "BLO_blend_defs.h"
185
186 #include "readfile.h"
187
188 /* for SDNA_TYPE_FROM_STRUCT() macro */
189 #include "dna_type_offsets.h"
190
191 #include <errno.h>
192
193 /* ********* my write, buffered writing with minimum size chunks ************ */
194
195 /* Use optimal allocation since blocks of this size are kept in memory for undo. */
196 #define MYWRITE_BUFFER_SIZE (MEM_SIZE_OPTIMAL(1 << 17))  /* 128kb */
197 #define MYWRITE_MAX_CHUNK   (MEM_SIZE_OPTIMAL(1 << 15))  /* ~32kb */
198
199 /** Use if we want to store how many bytes have been written to the file. */
200 // #define USE_WRITE_DATA_LEN
201
202 /* -------------------------------------------------------------------- */
203 /** \name Internal Write Wrapper's (Abstracts Compression)
204  * \{ */
205
206 typedef enum {
207         WW_WRAP_NONE = 1,
208         WW_WRAP_ZLIB,
209 } eWriteWrapType;
210
211 typedef struct WriteWrap WriteWrap;
212 struct WriteWrap {
213         /* callbacks */
214         bool   (*open)(WriteWrap *ww, const char *filepath);
215         bool   (*close)(WriteWrap *ww);
216         size_t (*write)(WriteWrap *ww, const char *data, size_t data_len);
217
218         /* internal */
219         union {
220                 int file_handle;
221                 gzFile gz_handle;
222         } _user_data;
223 };
224
225 /* none */
226 #define FILE_HANDLE(ww) \
227         (ww)->_user_data.file_handle
228
229 static bool ww_open_none(WriteWrap *ww, const char *filepath)
230 {
231         int file;
232
233         file = BLI_open(filepath, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
234
235         if (file != -1) {
236                 FILE_HANDLE(ww) = file;
237                 return true;
238         }
239         else {
240                 return false;
241         }
242 }
243 static bool ww_close_none(WriteWrap *ww)
244 {
245         return (close(FILE_HANDLE(ww)) != -1);
246 }
247 static size_t ww_write_none(WriteWrap *ww, const char *buf, size_t buf_len)
248 {
249         return write(FILE_HANDLE(ww), buf, buf_len);
250 }
251 #undef FILE_HANDLE
252
253 /* zlib */
254 #define FILE_HANDLE(ww) \
255         (ww)->_user_data.gz_handle
256
257 static bool ww_open_zlib(WriteWrap *ww, const char *filepath)
258 {
259         gzFile file;
260
261         file = BLI_gzopen(filepath, "wb1");
262
263         if (file != Z_NULL) {
264                 FILE_HANDLE(ww) = file;
265                 return true;
266         }
267         else {
268                 return false;
269         }
270 }
271 static bool ww_close_zlib(WriteWrap *ww)
272 {
273         return (gzclose(FILE_HANDLE(ww)) == Z_OK);
274 }
275 static size_t ww_write_zlib(WriteWrap *ww, const char *buf, size_t buf_len)
276 {
277         return gzwrite(FILE_HANDLE(ww), buf, buf_len);
278 }
279 #undef FILE_HANDLE
280
281 /* --- end compression types --- */
282
283 static void ww_handle_init(eWriteWrapType ww_type, WriteWrap *r_ww)
284 {
285         memset(r_ww, 0, sizeof(*r_ww));
286
287         switch (ww_type) {
288                 case WW_WRAP_ZLIB:
289                 {
290                         r_ww->open  = ww_open_zlib;
291                         r_ww->close = ww_close_zlib;
292                         r_ww->write = ww_write_zlib;
293                         break;
294                 }
295                 default:
296                 {
297                         r_ww->open  = ww_open_none;
298                         r_ww->close = ww_close_none;
299                         r_ww->write = ww_write_none;
300                         break;
301                 }
302         }
303 }
304
305 /** \} */
306
307 /* -------------------------------------------------------------------- */
308 /** \name Write Data Type & Functions
309  * \{ */
310
311 typedef struct {
312         const struct SDNA *sdna;
313
314         /** Use for file and memory writing (fixed size of #MYWRITE_BUFFER_SIZE). */
315         uchar *buf;
316         /** Number of bytes used in #WriteData.buf (flushed when exceeded). */
317         int buf_used_len;
318
319 #ifdef USE_WRITE_DATA_LEN
320         /** Total number of bytes written. */
321         size_t write_len;
322 #endif
323
324         /** Set on unlikely case of an error (ignores further file writing).  */
325         bool error;
326
327         /** #MemFile writing (used for undo). */
328         struct {
329                 MemFile      *current;
330                 MemFile      *compare;
331                 /** Use to de-duplicate chunks when writing. */
332                 MemFileChunk *compare_chunk;
333         } mem;
334         /** When true, write to #WriteData.current, could also call 'is_undo'. */
335         bool use_memfile;
336
337         /**
338          * Wrap writing, so we can use zlib or
339          * other compression types later, see: G_FILE_COMPRESS
340          * Will be NULL for UNDO.
341          */
342         WriteWrap *ww;
343
344 #ifdef USE_BMESH_SAVE_AS_COMPAT
345         bool use_mesh_compat; /* option to save with older mesh format */
346 #endif
347 } WriteData;
348
349 static WriteData *writedata_new(WriteWrap *ww)
350 {
351         WriteData *wd = MEM_callocN(sizeof(*wd), "writedata");
352
353         wd->sdna = DNA_sdna_current_get();
354
355         wd->ww = ww;
356
357         wd->buf = MEM_mallocN(MYWRITE_BUFFER_SIZE, "wd->buf");
358
359         return wd;
360 }
361
362 static void writedata_do_write(WriteData *wd, const void *mem, int memlen)
363 {
364         if ((wd == NULL) || wd->error || (mem == NULL) || memlen < 1) {
365                 return;
366         }
367
368         if (UNLIKELY(wd->error)) {
369                 return;
370         }
371
372         /* memory based save */
373         if (wd->use_memfile) {
374                 memfile_chunk_add(wd->mem.current, mem, memlen, &wd->mem.compare_chunk);
375         }
376         else {
377                 if (wd->ww->write(wd->ww, mem, memlen) != memlen) {
378                         wd->error = true;
379                 }
380         }
381 }
382
383 static void writedata_free(WriteData *wd)
384 {
385         MEM_freeN(wd->buf);
386         MEM_freeN(wd);
387 }
388
389 /** \} */
390
391 /* -------------------------------------------------------------------- */
392 /** \name Local Writing API 'mywrite'
393  * \{ */
394
395 /**
396  * Flush helps the de-duplicating memory for undo-save by logically segmenting data,
397  * so differences in one part of memory won't cause unrelated data to be duplicated.
398  */
399 static void mywrite_flush(WriteData *wd)
400 {
401         if (wd->buf_used_len) {
402                 writedata_do_write(wd, wd->buf, wd->buf_used_len);
403                 wd->buf_used_len = 0;
404         }
405 }
406
407 /**
408  * Low level WRITE(2) wrapper that buffers data
409  * \param adr Pointer to new chunk of data
410  * \param len Length of new chunk of data
411  */
412 static void mywrite(WriteData *wd, const void *adr, int len)
413 {
414         if (UNLIKELY(wd->error)) {
415                 return;
416         }
417
418         if (UNLIKELY(adr == NULL)) {
419                 BLI_assert(0);
420                 return;
421         }
422
423 #ifdef USE_WRITE_DATA_LEN
424         wd->write_len += len;
425 #endif
426
427         /* if we have a single big chunk, write existing data in
428          * buffer and write out big chunk in smaller pieces */
429         if (len > MYWRITE_MAX_CHUNK) {
430                 if (wd->buf_used_len) {
431                         writedata_do_write(wd, wd->buf, wd->buf_used_len);
432                         wd->buf_used_len = 0;
433                 }
434
435                 do {
436                         int writelen = MIN2(len, MYWRITE_MAX_CHUNK);
437                         writedata_do_write(wd, adr, writelen);
438                         adr = (const char *)adr + writelen;
439                         len -= writelen;
440                 } while (len > 0);
441
442                 return;
443         }
444
445         /* if data would overflow buffer, write out the buffer */
446         if (len + wd->buf_used_len > MYWRITE_BUFFER_SIZE - 1) {
447                 writedata_do_write(wd, wd->buf, wd->buf_used_len);
448                 wd->buf_used_len = 0;
449         }
450
451         /* append data at end of buffer */
452         memcpy(&wd->buf[wd->buf_used_len], adr, len);
453         wd->buf_used_len += len;
454 }
455
456 /**
457  * BeGiN initializer for mywrite
458  * \param ww: File write wrapper.
459  * \param compare Previous memory file (can be NULL).
460  * \param current The current memory file (can be NULL).
461  * \warning Talks to other functions with global parameters
462  */
463 static WriteData *mywrite_begin(WriteWrap *ww, MemFile *compare, MemFile *current)
464 {
465         WriteData *wd = writedata_new(ww);
466
467         if (current != NULL) {
468                 wd->mem.current = current;
469                 wd->mem.compare = compare;
470                 wd->mem.compare_chunk = compare ? compare->chunks.first : NULL;
471                 wd->use_memfile = true;
472         }
473
474         return wd;
475 }
476
477 /**
478  * END the mywrite wrapper
479  * \return 1 if write failed
480  * \return unknown global variable otherwise
481  * \warning Talks to other functions with global parameters
482  */
483 static bool mywrite_end(WriteData *wd)
484 {
485         if (wd->buf_used_len) {
486                 writedata_do_write(wd, wd->buf, wd->buf_used_len);
487                 wd->buf_used_len = 0;
488         }
489
490         const bool err = wd->error;
491         writedata_free(wd);
492
493         return err;
494 }
495
496 /** \} */
497
498 /* -------------------------------------------------------------------- */
499 /** \name Generic DNA File Writing
500  * \{ */
501
502 static void writestruct_at_address_nr(
503         WriteData *wd, int filecode, const int struct_nr, int nr,
504         const void *adr, const void *data)
505 {
506         BHead bh;
507         const short *sp;
508
509         BLI_assert(struct_nr > 0 && struct_nr < SDNA_TYPE_MAX);
510
511         if (adr == NULL || data == NULL || nr == 0) {
512                 return;
513         }
514
515         /* init BHead */
516         bh.code = filecode;
517         bh.old = adr;
518         bh.nr = nr;
519
520         bh.SDNAnr = struct_nr;
521         sp = wd->sdna->structs[bh.SDNAnr];
522
523         bh.len = nr * wd->sdna->typelens[sp[0]];
524
525         if (bh.len == 0) {
526                 return;
527         }
528
529         mywrite(wd, &bh, sizeof(BHead));
530         mywrite(wd, data, bh.len);
531 }
532
533 static void writestruct_at_address_id(
534         WriteData *wd, int filecode, const char *structname, int nr,
535         const void *adr, const void *data)
536 {
537         if (adr == NULL || data == NULL || nr == 0) {
538                 return;
539         }
540
541         const int SDNAnr = DNA_struct_find_nr(wd->sdna, structname);
542         if (UNLIKELY(SDNAnr == -1)) {
543                 printf("error: can't find SDNA code <%s>\n", structname);
544                 return;
545         }
546
547         writestruct_at_address_nr(wd, filecode, SDNAnr, nr, adr, data);
548 }
549
550 static void writestruct_nr(
551         WriteData *wd, int filecode, const int struct_nr, int nr,
552         const void *adr)
553 {
554         writestruct_at_address_nr(wd, filecode, struct_nr, nr, adr, adr);
555 }
556
557 static void writestruct_id(
558         WriteData *wd, int filecode, const char *structname, int nr,
559         const void *adr)
560 {
561         writestruct_at_address_id(wd, filecode, structname, nr, adr, adr);
562 }
563
564 static void writedata(WriteData *wd, int filecode, int len, const void *adr)  /* do not use for structs */
565 {
566         BHead bh;
567
568         if (adr == NULL || len == 0) {
569                 return;
570         }
571
572         /* align to 4 (writes uninitialized bytes in some cases) */
573         len = (len + 3) & ~3;
574
575         /* init BHead */
576         bh.code   = filecode;
577         bh.old    = adr;
578         bh.nr     = 1;
579         bh.SDNAnr = 0;
580         bh.len    = len;
581
582         mywrite(wd, &bh, sizeof(BHead));
583         mywrite(wd, adr, len);
584 }
585
586 /* use this to force writing of lists in same order as reading (using link_list) */
587 static void writelist_nr(WriteData *wd, int filecode, const int struct_nr, const ListBase *lb)
588 {
589         const Link *link = lb->first;
590
591         while (link) {
592                 writestruct_nr(wd, filecode, struct_nr, 1, link);
593                 link = link->next;
594         }
595 }
596
597 #if 0
598 static void writelist_id(WriteData *wd, int filecode, const char *structname, const ListBase *lb)
599 {
600         const Link *link = lb->first;
601         if (link) {
602
603                 const int struct_nr = DNA_struct_find_nr(wd->sdna, structname);
604                 if (struct_nr == -1) {
605                         printf("error: can't find SDNA code <%s>\n", structname);
606                         return;
607                 }
608
609                 while (link) {
610                         writestruct_nr(wd, filecode, struct_nr, 1, link);
611                         link = link->next;
612                 }
613         }
614 }
615 #endif
616
617 #define writestruct_at_address(wd, filecode, struct_id, nr, adr, data) \
618         writestruct_at_address_nr(wd, filecode, SDNA_TYPE_FROM_STRUCT(struct_id), nr, adr, data)
619
620 #define writestruct(wd, filecode, struct_id, nr, adr) \
621         writestruct_nr(wd, filecode, SDNA_TYPE_FROM_STRUCT(struct_id), nr, adr)
622
623 #define writelist(wd, filecode, struct_id, lb) \
624         writelist_nr(wd, filecode, SDNA_TYPE_FROM_STRUCT(struct_id), lb)
625
626 /** \} */
627
628 /* -------------------------------------------------------------------- */
629 /** \name Typed DNA File Writing
630  *
631  * These functions are used by blender's .blend system for file saving/loading.
632  * \{ */
633
634 void IDP_WriteProperty_OnlyData(const IDProperty *prop, void *wd);
635 void IDP_WriteProperty(const IDProperty *prop, void *wd);
636
637 static void IDP_WriteArray(const IDProperty *prop, void *wd)
638 {
639         /*REMEMBER to set totalen to len in the linking code!!*/
640         if (prop->data.pointer) {
641                 writedata(wd, DATA, MEM_allocN_len(prop->data.pointer), prop->data.pointer);
642
643                 if (prop->subtype == IDP_GROUP) {
644                         IDProperty **array = prop->data.pointer;
645                         int a;
646
647                         for (a = 0; a < prop->len; a++) {
648                                 IDP_WriteProperty(array[a], wd);
649                         }
650                 }
651         }
652 }
653
654 static void IDP_WriteIDPArray(const IDProperty *prop, void *wd)
655 {
656         /*REMEMBER to set totalen to len in the linking code!!*/
657         if (prop->data.pointer) {
658                 const IDProperty *array = prop->data.pointer;
659                 int a;
660
661                 writestruct(wd, DATA, IDProperty, prop->len, array);
662
663                 for (a = 0; a < prop->len; a++) {
664                         IDP_WriteProperty_OnlyData(&array[a], wd);
665                 }
666         }
667 }
668
669 static void IDP_WriteString(const IDProperty *prop, void *wd)
670 {
671         /*REMEMBER to set totalen to len in the linking code!!*/
672         writedata(wd, DATA, prop->len, prop->data.pointer);
673 }
674
675 static void IDP_WriteGroup(const IDProperty *prop, void *wd)
676 {
677         IDProperty *loop;
678
679         for (loop = prop->data.group.first; loop; loop = loop->next) {
680                 IDP_WriteProperty(loop, wd);
681         }
682 }
683
684 /* Functions to read/write ID Properties */
685 void IDP_WriteProperty_OnlyData(const IDProperty *prop, void *wd)
686 {
687         switch (prop->type) {
688                 case IDP_GROUP:
689                         IDP_WriteGroup(prop, wd);
690                         break;
691                 case IDP_STRING:
692                         IDP_WriteString(prop, wd);
693                         break;
694                 case IDP_ARRAY:
695                         IDP_WriteArray(prop, wd);
696                         break;
697                 case IDP_IDPARRAY:
698                         IDP_WriteIDPArray(prop, wd);
699                         break;
700         }
701 }
702
703 void IDP_WriteProperty(const IDProperty *prop, void *wd)
704 {
705         writestruct(wd, DATA, IDProperty, 1, prop);
706         IDP_WriteProperty_OnlyData(prop, wd);
707 }
708
709 static void write_iddata(void *wd, const ID *id)
710 {
711         /* ID_WM's id->properties are considered runtime only, and never written in .blend file. */
712         if (id->properties && !ELEM(GS(id->name), ID_WM)) {
713                 IDP_WriteProperty(id->properties, wd);
714         }
715 }
716
717 static void write_previews(WriteData *wd, const PreviewImage *prv_orig)
718 {
719         /* Note we write previews also for undo steps. It takes up some memory,
720          * but not doing so would causes all previews to be re-rendered after
721          * undo which is too expensive. */
722         if (prv_orig) {
723                 PreviewImage prv = *prv_orig;
724
725                 /* don't write out large previews if not requested */
726                 if (!(U.flag & USER_SAVE_PREVIEWS)) {
727                         prv.w[1] = 0;
728                         prv.h[1] = 0;
729                         prv.rect[1] = NULL;
730                 }
731                 writestruct_at_address(wd, DATA, PreviewImage, 1, prv_orig, &prv);
732                 if (prv.rect[0]) {
733                         writedata(wd, DATA, prv.w[0] * prv.h[0] * sizeof(unsigned int), prv.rect[0]);
734                 }
735                 if (prv.rect[1]) {
736                         writedata(wd, DATA, prv.w[1] * prv.h[1] * sizeof(unsigned int), prv.rect[1]);
737                 }
738         }
739 }
740
741 static void write_fmodifiers(WriteData *wd, ListBase *fmodifiers)
742 {
743         FModifier *fcm;
744
745         /* Write all modifiers first (for faster reloading) */
746         writelist(wd, DATA, FModifier, fmodifiers);
747
748         /* Modifiers */
749         for (fcm = fmodifiers->first; fcm; fcm = fcm->next) {
750                 const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(fcm);
751
752                 /* Write the specific data */
753                 if (fmi && fcm->data) {
754                         /* firstly, just write the plain fmi->data struct */
755                         writestruct_id(wd, DATA, fmi->structName, 1, fcm->data);
756
757                         /* do any modifier specific stuff */
758                         switch (fcm->type) {
759                                 case FMODIFIER_TYPE_GENERATOR:
760                                 {
761                                         FMod_Generator *data = fcm->data;
762
763                                         /* write coefficients array */
764                                         if (data->coefficients) {
765                                                 writedata(wd, DATA, sizeof(float) * (data->arraysize), data->coefficients);
766                                         }
767
768                                         break;
769                                 }
770                                 case FMODIFIER_TYPE_ENVELOPE:
771                                 {
772                                         FMod_Envelope *data = fcm->data;
773
774                                         /* write envelope data */
775                                         if (data->data) {
776                                                 writestruct(wd, DATA, FCM_EnvelopeData, data->totvert, data->data);
777                                         }
778
779                                         break;
780                                 }
781                                 case FMODIFIER_TYPE_PYTHON:
782                                 {
783                                         FMod_Python *data = fcm->data;
784
785                                         /* Write ID Properties -- and copy this comment EXACTLY for easy finding
786                                          * of library blocks that implement this.*/
787                                         IDP_WriteProperty(data->prop, wd);
788
789                                         break;
790                                 }
791                         }
792                 }
793         }
794 }
795
796 static void write_fcurves(WriteData *wd, ListBase *fcurves)
797 {
798         FCurve *fcu;
799
800         writelist(wd, DATA, FCurve, fcurves);
801         for (fcu = fcurves->first; fcu; fcu = fcu->next) {
802                 /* curve data */
803                 if (fcu->bezt) {
804                         writestruct(wd, DATA, BezTriple, fcu->totvert, fcu->bezt);
805                 }
806                 if (fcu->fpt) {
807                         writestruct(wd, DATA, FPoint, fcu->totvert, fcu->fpt);
808                 }
809
810                 if (fcu->rna_path) {
811                         writedata(wd, DATA, strlen(fcu->rna_path) + 1, fcu->rna_path);
812                 }
813
814                 /* driver data */
815                 if (fcu->driver) {
816                         ChannelDriver *driver = fcu->driver;
817                         DriverVar *dvar;
818
819                         writestruct(wd, DATA, ChannelDriver, 1, driver);
820
821                         /* variables */
822                         writelist(wd, DATA, DriverVar, &driver->variables);
823                         for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
824                                 DRIVER_TARGETS_USED_LOOPER(dvar)
825                                 {
826                                         if (dtar->rna_path) {
827                                                 writedata(wd, DATA, strlen(dtar->rna_path) + 1, dtar->rna_path);
828                                         }
829                                 }
830                                 DRIVER_TARGETS_LOOPER_END
831                         }
832                 }
833
834                 /* write F-Modifiers */
835                 write_fmodifiers(wd, &fcu->modifiers);
836         }
837 }
838
839 static void write_action(WriteData *wd, bAction *act)
840 {
841         if (act->id.us > 0 || wd->use_memfile) {
842                 writestruct(wd, ID_AC, bAction, 1, act);
843                 write_iddata(wd, &act->id);
844
845                 write_fcurves(wd, &act->curves);
846
847                 for (bActionGroup *grp = act->groups.first; grp; grp = grp->next) {
848                         writestruct(wd, DATA, bActionGroup, 1, grp);
849                 }
850
851                 for (TimeMarker *marker = act->markers.first; marker; marker = marker->next) {
852                         writestruct(wd, DATA, TimeMarker, 1, marker);
853                 }
854         }
855 }
856
857 static void write_keyingsets(WriteData *wd, ListBase *list)
858 {
859         KeyingSet *ks;
860         KS_Path *ksp;
861
862         for (ks = list->first; ks; ks = ks->next) {
863                 /* KeyingSet */
864                 writestruct(wd, DATA, KeyingSet, 1, ks);
865
866                 /* Paths */
867                 for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
868                         /* Path */
869                         writestruct(wd, DATA, KS_Path, 1, ksp);
870
871                         if (ksp->rna_path) {
872                                 writedata(wd, DATA, strlen(ksp->rna_path) + 1, ksp->rna_path);
873                         }
874                 }
875         }
876 }
877
878 static void write_nlastrips(WriteData *wd, ListBase *strips)
879 {
880         NlaStrip *strip;
881
882         writelist(wd, DATA, NlaStrip, strips);
883         for (strip = strips->first; strip; strip = strip->next) {
884                 /* write the strip's F-Curves and modifiers */
885                 write_fcurves(wd, &strip->fcurves);
886                 write_fmodifiers(wd, &strip->modifiers);
887
888                 /* write the strip's children */
889                 write_nlastrips(wd, &strip->strips);
890         }
891 }
892
893 static void write_nladata(WriteData *wd, ListBase *nlabase)
894 {
895         NlaTrack *nlt;
896
897         /* write all the tracks */
898         for (nlt = nlabase->first; nlt; nlt = nlt->next) {
899                 /* write the track first */
900                 writestruct(wd, DATA, NlaTrack, 1, nlt);
901
902                 /* write the track's strips */
903                 write_nlastrips(wd, &nlt->strips);
904         }
905 }
906
907 static void write_animdata(WriteData *wd, AnimData *adt)
908 {
909         AnimOverride *aor;
910
911         /* firstly, just write the AnimData block */
912         writestruct(wd, DATA, AnimData, 1, adt);
913
914         /* write drivers */
915         write_fcurves(wd, &adt->drivers);
916
917         /* write overrides */
918         // FIXME: are these needed?
919         for (aor = adt->overrides.first; aor; aor = aor->next) {
920                 /* overrides consist of base data + rna_path */
921                 writestruct(wd, DATA, AnimOverride, 1, aor);
922                 writedata(wd, DATA, strlen(aor->rna_path) + 1, aor->rna_path);
923         }
924
925         // TODO write the remaps (if they are needed)
926
927         /* write NLA data */
928         write_nladata(wd, &adt->nla_tracks);
929 }
930
931 static void write_curvemapping_curves(WriteData *wd, CurveMapping *cumap)
932 {
933         for (int a = 0; a < CM_TOT; a++) {
934                 writestruct(wd, DATA, CurveMapPoint, cumap->cm[a].totpoint, cumap->cm[a].curve);
935         }
936 }
937
938 static void write_curvemapping(WriteData *wd, CurveMapping *cumap)
939 {
940         writestruct(wd, DATA, CurveMapping, 1, cumap);
941
942         write_curvemapping_curves(wd, cumap);
943 }
944
945 static void write_node_socket(WriteData *wd, bNodeTree *UNUSED(ntree), bNode *node, bNodeSocket *sock)
946 {
947 #ifdef USE_NODE_COMPAT_CUSTOMNODES
948         /* forward compatibility code, so older blenders still open (not for undo) */
949         if (wd->use_memfile == false) {
950                 sock->stack_type = 1;
951
952                 if (node->type == NODE_GROUP) {
953                         bNodeTree *ngroup = (bNodeTree *)node->id;
954                         if (ngroup) {
955                                 /* for node groups: look up the deprecated groupsock pointer */
956                                 sock->groupsock = ntreeFindSocketInterface(ngroup, sock->in_out, sock->identifier);
957                                 BLI_assert(sock->groupsock != NULL);
958
959                                 /* node group sockets now use the generic identifier string to verify group nodes,
960                                  * old blender uses the own_index.
961                                  */
962                                 sock->own_index = sock->groupsock->own_index;
963                         }
964                 }
965         }
966 #endif
967
968         /* actual socket writing */
969         writestruct(wd, DATA, bNodeSocket, 1, sock);
970
971         if (sock->prop) {
972                 IDP_WriteProperty(sock->prop, wd);
973         }
974
975         if (sock->default_value) {
976                 writedata(wd, DATA, MEM_allocN_len(sock->default_value), sock->default_value);
977         }
978 }
979 static void write_node_socket_interface(WriteData *wd, bNodeTree *UNUSED(ntree), bNodeSocket *sock)
980 {
981 #ifdef USE_NODE_COMPAT_CUSTOMNODES
982         /* forward compatibility code, so older blenders still open */
983         sock->stack_type = 1;
984
985         /* Reconstruct the deprecated default_value structs in socket interface DNA. */
986         if (sock->default_value == NULL && sock->typeinfo) {
987                 node_socket_init_default_value(sock);
988         }
989 #endif
990
991         /* actual socket writing */
992         writestruct(wd, DATA, bNodeSocket, 1, sock);
993
994         if (sock->prop) {
995                 IDP_WriteProperty(sock->prop, wd);
996         }
997
998         if (sock->default_value) {
999                 writedata(wd, DATA, MEM_allocN_len(sock->default_value), sock->default_value);
1000         }
1001 }
1002 /* this is only direct data, tree itself should have been written */
1003 static void write_nodetree_nolib(WriteData *wd, bNodeTree *ntree)
1004 {
1005         bNode *node;
1006         bNodeSocket *sock;
1007         bNodeLink *link;
1008
1009         /* for link_list() speed, we write per list */
1010
1011         if (ntree->adt) {
1012                 write_animdata(wd, ntree->adt);
1013         }
1014
1015         for (node = ntree->nodes.first; node; node = node->next) {
1016                 writestruct(wd, DATA, bNode, 1, node);
1017
1018                 if (node->prop) {
1019                         IDP_WriteProperty(node->prop, wd);
1020                 }
1021
1022                 for (sock = node->inputs.first; sock; sock = sock->next) {
1023                         write_node_socket(wd, ntree, node, sock);
1024                 }
1025                 for (sock = node->outputs.first; sock; sock = sock->next) {
1026                         write_node_socket(wd, ntree, node, sock);
1027                 }
1028
1029                 for (link = node->internal_links.first; link; link = link->next) {
1030                         writestruct(wd, DATA, bNodeLink, 1, link);
1031                 }
1032
1033                 if (node->storage) {
1034                         /* could be handlerized at some point, now only 1 exception still */
1035                         if ((ntree->type == NTREE_SHADER) &&
1036                             ELEM(node->type, SH_NODE_CURVE_VEC, SH_NODE_CURVE_RGB))
1037                         {
1038                                 write_curvemapping(wd, node->storage);
1039                         }
1040                         else if (ntree->type == NTREE_SHADER &&
1041                                  (node->type == SH_NODE_SCRIPT))
1042                         {
1043                                 NodeShaderScript *nss = (NodeShaderScript *)node->storage;
1044                                 if (nss->bytecode) {
1045                                         writedata(wd, DATA, strlen(nss->bytecode) + 1, nss->bytecode);
1046                                 }
1047                                 writestruct_id(wd, DATA, node->typeinfo->storagename, 1, node->storage);
1048                         }
1049                         else if ((ntree->type == NTREE_COMPOSIT) &&
1050                                  ELEM(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
1051                         {
1052                                 write_curvemapping(wd, node->storage);
1053                         }
1054                         else if ((ntree->type == NTREE_TEXTURE) &&
1055                                  (node->type == TEX_NODE_CURVE_RGB || node->type == TEX_NODE_CURVE_TIME))
1056                         {
1057                                 write_curvemapping(wd, node->storage);
1058                         }
1059                         else if ((ntree->type == NTREE_COMPOSIT) &&
1060                                  (node->type == CMP_NODE_MOVIEDISTORTION))
1061                         {
1062                                 /* pass */
1063                         }
1064                         else if ((ntree->type == NTREE_COMPOSIT) && (node->type == CMP_NODE_GLARE)) {
1065                                 /* Simple forward compat for fix for T50736.
1066                                  * Not ideal (there is no ideal solution here), but should do for now. */
1067                                 NodeGlare *ndg = node->storage;
1068                                 /* Not in undo case. */
1069                                 if (wd->use_memfile == false) {
1070                                         switch (ndg->type) {
1071                                                 case 2:  /* Grrrr! magic numbers :( */
1072                                                         ndg->angle = ndg->streaks;
1073                                                         break;
1074                                                 case 0:
1075                                                         ndg->angle = ndg->star_45;
1076                                                         break;
1077                                                 default:
1078                                                         break;
1079                                         }
1080                                 }
1081                                 writestruct_id(wd, DATA, node->typeinfo->storagename, 1, node->storage);
1082                         }
1083                         else {
1084                                 writestruct_id(wd, DATA, node->typeinfo->storagename, 1, node->storage);
1085                         }
1086                 }
1087
1088                 if (node->type == CMP_NODE_OUTPUT_FILE) {
1089                         /* inputs have own storage data */
1090                         for (sock = node->inputs.first; sock; sock = sock->next) {
1091                                 writestruct(wd, DATA, NodeImageMultiFileSocket, 1, sock->storage);
1092                         }
1093                 }
1094                 if (ELEM(node->type, CMP_NODE_IMAGE, CMP_NODE_R_LAYERS)) {
1095                         /* write extra socket info */
1096                         for (sock = node->outputs.first; sock; sock = sock->next) {
1097                                 writestruct(wd, DATA, NodeImageLayer, 1, sock->storage);
1098                         }
1099                 }
1100         }
1101
1102         for (link = ntree->links.first; link; link = link->next) {
1103                 writestruct(wd, DATA, bNodeLink, 1, link);
1104         }
1105
1106         for (sock = ntree->inputs.first; sock; sock = sock->next) {
1107                 write_node_socket_interface(wd, ntree, sock);
1108         }
1109         for (sock = ntree->outputs.first; sock; sock = sock->next) {
1110                 write_node_socket_interface(wd, ntree, sock);
1111         }
1112 }
1113
1114 /**
1115  * Take care using 'use_active_win', since we wont want the currently active window
1116  * to change which scene renders (currently only used for undo).
1117  */
1118 static void current_screen_compat(Main *mainvar, bScreen **r_screen, bool use_active_win)
1119 {
1120         wmWindowManager *wm;
1121         wmWindow *window = NULL;
1122
1123         /* find a global current screen in the first open window, to have
1124          * a reasonable default for reading in older versions */
1125         wm = mainvar->wm.first;
1126
1127         if (wm) {
1128                 if (use_active_win) {
1129                         /* write the active window into the file, needed for multi-window undo T43424 */
1130                         for (window = wm->windows.first; window; window = window->next) {
1131                                 if (window->active) {
1132                                         break;
1133                                 }
1134                         }
1135
1136                         /* fallback */
1137                         if (window == NULL) {
1138                                 window = wm->windows.first;
1139                         }
1140                 }
1141                 else {
1142                         window = wm->windows.first;
1143                 }
1144         }
1145
1146         *r_screen = (window) ? window->screen : NULL;
1147 }
1148
1149 typedef struct RenderInfo {
1150         int sfra;
1151         int efra;
1152         char scene_name[MAX_ID_NAME - 2];
1153 } RenderInfo;
1154
1155 /* was for historic render-deamon feature,
1156  * now write because it can be easily extracted without
1157  * reading the whole blend file */
1158 static void write_renderinfo(WriteData *wd, Main *mainvar)
1159 {
1160         bScreen *curscreen;
1161         Scene *sce, *curscene = NULL;
1162         RenderInfo data;
1163
1164         /* XXX in future, handle multiple windows with multiple screens? */
1165         current_screen_compat(mainvar, &curscreen, false);
1166         if (curscreen) {
1167                 curscene = curscreen->scene;
1168         }
1169
1170         for (sce = mainvar->scene.first; sce; sce = sce->id.next) {
1171                 if (sce->id.lib == NULL && (sce == curscene || (sce->r.scemode & R_BG_RENDER))) {
1172                         data.sfra = sce->r.sfra;
1173                         data.efra = sce->r.efra;
1174                         memset(data.scene_name, 0, sizeof(data.scene_name));
1175
1176                         BLI_strncpy(data.scene_name, sce->id.name + 2, sizeof(data.scene_name));
1177
1178                         writedata(wd, REND, sizeof(data), &data);
1179                 }
1180         }
1181 }
1182
1183 static void write_keymapitem(WriteData *wd, const wmKeyMapItem *kmi)
1184 {
1185         writestruct(wd, DATA, wmKeyMapItem, 1, kmi);
1186         if (kmi->properties) {
1187                 IDP_WriteProperty(kmi->properties, wd);
1188         }
1189 }
1190
1191 static void write_userdef(WriteData *wd, const UserDef *userdef)
1192 {
1193         writestruct(wd, USER, UserDef, 1, userdef);
1194
1195         for (const bTheme *btheme = userdef->themes.first; btheme; btheme = btheme->next) {
1196                 writestruct(wd, DATA, bTheme, 1, btheme);
1197         }
1198
1199         for (const wmKeyMap *keymap = userdef->user_keymaps.first; keymap; keymap = keymap->next) {
1200                 writestruct(wd, DATA, wmKeyMap, 1, keymap);
1201
1202                 for (const wmKeyMapDiffItem *kmdi = keymap->diff_items.first; kmdi; kmdi = kmdi->next) {
1203                         writestruct(wd, DATA, wmKeyMapDiffItem, 1, kmdi);
1204                         if (kmdi->remove_item) {
1205                                 write_keymapitem(wd, kmdi->remove_item);
1206                         }
1207                         if (kmdi->add_item) {
1208                                 write_keymapitem(wd, kmdi->add_item);
1209                         }
1210                 }
1211
1212                 for (const wmKeyMapItem *kmi = keymap->items.first; kmi; kmi = kmi->next) {
1213                         write_keymapitem(wd, kmi);
1214                 }
1215         }
1216
1217         for (const bAddon *bext = userdef->addons.first; bext; bext = bext->next) {
1218                 writestruct(wd, DATA, bAddon, 1, bext);
1219                 if (bext->prop) {
1220                         IDP_WriteProperty(bext->prop, wd);
1221                 }
1222         }
1223
1224         for (const bPathCompare *path_cmp = userdef->autoexec_paths.first; path_cmp; path_cmp = path_cmp->next) {
1225                 writestruct(wd, DATA, bPathCompare, 1, path_cmp);
1226         }
1227
1228         for (const uiStyle *style = userdef->uistyles.first; style; style = style->next) {
1229                 writestruct(wd, DATA, uiStyle, 1, style);
1230         }
1231 }
1232
1233 static void write_boid_state(WriteData *wd, BoidState *state)
1234 {
1235         BoidRule *rule = state->rules.first;
1236
1237         writestruct(wd, DATA, BoidState, 1, state);
1238
1239         for (; rule; rule = rule->next) {
1240                 switch (rule->type) {
1241                         case eBoidRuleType_Goal:
1242                         case eBoidRuleType_Avoid:
1243                                 writestruct(wd, DATA, BoidRuleGoalAvoid, 1, rule);
1244                                 break;
1245                         case eBoidRuleType_AvoidCollision:
1246                                 writestruct(wd, DATA, BoidRuleAvoidCollision, 1, rule);
1247                                 break;
1248                         case eBoidRuleType_FollowLeader:
1249                                 writestruct(wd, DATA, BoidRuleFollowLeader, 1, rule);
1250                                 break;
1251                         case eBoidRuleType_AverageSpeed:
1252                                 writestruct(wd, DATA, BoidRuleAverageSpeed, 1, rule);
1253                                 break;
1254                         case eBoidRuleType_Fight:
1255                                 writestruct(wd, DATA, BoidRuleFight, 1, rule);
1256                                 break;
1257                         default:
1258                                 writestruct(wd, DATA, BoidRule, 1, rule);
1259                                 break;
1260                 }
1261         }
1262 #if 0
1263         BoidCondition *cond = state->conditions.first;
1264         for (; cond; cond = cond->next) {
1265                 writestruct(wd, DATA, BoidCondition, 1, cond);
1266         }
1267 #endif
1268 }
1269
1270 /* update this also to readfile.c */
1271 static const char *ptcache_data_struct[] = {
1272         "", // BPHYS_DATA_INDEX
1273         "", // BPHYS_DATA_LOCATION
1274         "", // BPHYS_DATA_VELOCITY
1275         "", // BPHYS_DATA_ROTATION
1276         "", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
1277         "", // BPHYS_DATA_SIZE:
1278         "", // BPHYS_DATA_TIMES:
1279         "BoidData" // case BPHYS_DATA_BOIDS:
1280 };
1281 static const char *ptcache_extra_struct[] = {
1282         "",
1283         "ParticleSpring"
1284 };
1285 static void write_pointcaches(WriteData *wd, ListBase *ptcaches)
1286 {
1287         PointCache *cache = ptcaches->first;
1288         int i;
1289
1290         for (; cache; cache = cache->next) {
1291                 writestruct(wd, DATA, PointCache, 1, cache);
1292
1293                 if ((cache->flag & PTCACHE_DISK_CACHE) == 0) {
1294                         PTCacheMem *pm = cache->mem_cache.first;
1295
1296                         for (; pm; pm = pm->next) {
1297                                 PTCacheExtra *extra = pm->extradata.first;
1298
1299                                 writestruct(wd, DATA, PTCacheMem, 1, pm);
1300
1301                                 for (i = 0; i < BPHYS_TOT_DATA; i++) {
1302                                         if (pm->data[i] && pm->data_types & (1 << i)) {
1303                                                 if (ptcache_data_struct[i][0] == '\0') {
1304                                                         writedata(wd, DATA, MEM_allocN_len(pm->data[i]), pm->data[i]);
1305                                                 }
1306                                                 else {
1307                                                         writestruct_id(wd, DATA, ptcache_data_struct[i], pm->totpoint, pm->data[i]);
1308                                                 }
1309                                         }
1310                                 }
1311
1312                                 for (; extra; extra = extra->next) {
1313                                         if (ptcache_extra_struct[extra->type][0] == '\0') {
1314                                                 continue;
1315                                         }
1316                                         writestruct(wd, DATA, PTCacheExtra, 1, extra);
1317                                         writestruct_id(wd, DATA, ptcache_extra_struct[extra->type], extra->totdata, extra->data);
1318                                 }
1319                         }
1320                 }
1321         }
1322 }
1323
1324 static void write_particlesettings(WriteData *wd, ParticleSettings *part)
1325 {
1326         if (part->id.us > 0 || wd->use_memfile) {
1327                 /* write LibData */
1328                 writestruct(wd, ID_PA, ParticleSettings, 1, part);
1329                 write_iddata(wd, &part->id);
1330
1331                 if (part->adt) {
1332                         write_animdata(wd, part->adt);
1333                 }
1334                 writestruct(wd, DATA, PartDeflect, 1, part->pd);
1335                 writestruct(wd, DATA, PartDeflect, 1, part->pd2);
1336                 writestruct(wd, DATA, EffectorWeights, 1, part->effector_weights);
1337
1338                 if (part->clumpcurve) {
1339                         write_curvemapping(wd, part->clumpcurve);
1340                 }
1341                 if (part->roughcurve) {
1342                         write_curvemapping(wd, part->roughcurve);
1343                 }
1344                 if (part->twistcurve) {
1345                         write_curvemapping(wd, part->twistcurve);
1346                 }
1347
1348                 for (ParticleDupliWeight *dw = part->dupliweights.first; dw; dw = dw->next) {
1349                         /* update indices, but only if dw->ob is set (can be NULL after loading e.g.) */
1350                         if (dw->ob != NULL) {
1351                                 dw->index = 0;
1352                                 if (part->dup_group) { /* can be NULL if lining fails or set to None */
1353                                         for (GroupObject *go = part->dup_group->gobject.first;
1354                                              go && go->ob != dw->ob;
1355                                              go = go->next, dw->index++);
1356                                 }
1357                         }
1358                         writestruct(wd, DATA, ParticleDupliWeight, 1, dw);
1359                 }
1360
1361                 if (part->boids && part->phystype == PART_PHYS_BOIDS) {
1362                         writestruct(wd, DATA, BoidSettings, 1, part->boids);
1363
1364                         for (BoidState *state = part->boids->states.first; state; state = state->next) {
1365                                 write_boid_state(wd, state);
1366                         }
1367                 }
1368                 if (part->fluid && part->phystype == PART_PHYS_FLUID) {
1369                         writestruct(wd, DATA, SPHFluidSettings, 1, part->fluid);
1370                 }
1371
1372                 for (int a = 0; a < MAX_MTEX; a++) {
1373                         if (part->mtex[a]) {
1374                                 writestruct(wd, DATA, MTex, 1, part->mtex[a]);
1375                         }
1376                 }
1377         }
1378 }
1379
1380 static void write_particlesystems(WriteData *wd, ListBase *particles)
1381 {
1382         ParticleSystem *psys = particles->first;
1383         ParticleTarget *pt;
1384         int a;
1385
1386         for (; psys; psys = psys->next) {
1387                 writestruct(wd, DATA, ParticleSystem, 1, psys);
1388
1389                 if (psys->particles) {
1390                         writestruct(wd, DATA, ParticleData, psys->totpart, psys->particles);
1391
1392                         if (psys->particles->hair) {
1393                                 ParticleData *pa = psys->particles;
1394
1395                                 for (a = 0; a < psys->totpart; a++, pa++) {
1396                                         writestruct(wd, DATA, HairKey, pa->totkey, pa->hair);
1397                                 }
1398                         }
1399
1400                         if (psys->particles->boid &&
1401                             (psys->part->phystype == PART_PHYS_BOIDS))
1402                         {
1403                                 writestruct(wd, DATA, BoidParticle, psys->totpart, psys->particles->boid);
1404                         }
1405
1406                         if (psys->part->fluid &&
1407                             (psys->part->phystype == PART_PHYS_FLUID) &&
1408                             (psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS))
1409                         {
1410                                 writestruct(wd, DATA, ParticleSpring, psys->tot_fluidsprings, psys->fluid_springs);
1411                         }
1412                 }
1413                 pt = psys->targets.first;
1414                 for (; pt; pt = pt->next) {
1415                         writestruct(wd, DATA, ParticleTarget, 1, pt);
1416                 }
1417
1418                 if (psys->child) {
1419                         writestruct(wd, DATA, ChildParticle, psys->totchild, psys->child);
1420                 }
1421
1422                 if (psys->clmd) {
1423                         writestruct(wd, DATA, ClothModifierData, 1, psys->clmd);
1424                         writestruct(wd, DATA, ClothSimSettings, 1, psys->clmd->sim_parms);
1425                         writestruct(wd, DATA, ClothCollSettings, 1, psys->clmd->coll_parms);
1426                 }
1427
1428                 write_pointcaches(wd, &psys->ptcaches);
1429         }
1430 }
1431
1432 static void write_properties(WriteData *wd, ListBase *lb)
1433 {
1434         bProperty *prop;
1435
1436         prop = lb->first;
1437         while (prop) {
1438                 writestruct(wd, DATA, bProperty, 1, prop);
1439
1440                 if (prop->poin && prop->poin != &prop->data) {
1441                         writedata(wd, DATA, MEM_allocN_len(prop->poin), prop->poin);
1442                 }
1443
1444                 prop = prop->next;
1445         }
1446 }
1447
1448 static void write_sensors(WriteData *wd, ListBase *lb)
1449 {
1450         bSensor *sens;
1451
1452         sens = lb->first;
1453         while (sens) {
1454                 writestruct(wd, DATA, bSensor, 1, sens);
1455
1456                 writedata(wd, DATA, sizeof(void *) * sens->totlinks, sens->links);
1457
1458                 switch (sens->type) {
1459                         case SENS_NEAR:
1460                                 writestruct(wd, DATA, bNearSensor, 1, sens->data);
1461                                 break;
1462                         case SENS_MOUSE:
1463                                 writestruct(wd, DATA, bMouseSensor, 1, sens->data);
1464                                 break;
1465                         case SENS_KEYBOARD:
1466                                 writestruct(wd, DATA, bKeyboardSensor, 1, sens->data);
1467                                 break;
1468                         case SENS_PROPERTY:
1469                                 writestruct(wd, DATA, bPropertySensor, 1, sens->data);
1470                                 break;
1471                         case SENS_ARMATURE:
1472                                 writestruct(wd, DATA, bArmatureSensor, 1, sens->data);
1473                                 break;
1474                         case SENS_ACTUATOR:
1475                                 writestruct(wd, DATA, bActuatorSensor, 1, sens->data);
1476                                 break;
1477                         case SENS_DELAY:
1478                                 writestruct(wd, DATA, bDelaySensor, 1, sens->data);
1479                                 break;
1480                         case SENS_COLLISION:
1481                                 writestruct(wd, DATA, bCollisionSensor, 1, sens->data);
1482                                 break;
1483                         case SENS_RADAR:
1484                                 writestruct(wd, DATA, bRadarSensor, 1, sens->data);
1485                                 break;
1486                         case SENS_RANDOM:
1487                                 writestruct(wd, DATA, bRandomSensor, 1, sens->data);
1488                                 break;
1489                         case SENS_RAY:
1490                                 writestruct(wd, DATA, bRaySensor, 1, sens->data);
1491                                 break;
1492                         case SENS_MESSAGE:
1493                                 writestruct(wd, DATA, bMessageSensor, 1, sens->data);
1494                                 break;
1495                         case SENS_JOYSTICK:
1496                                 writestruct(wd, DATA, bJoystickSensor, 1, sens->data);
1497                                 break;
1498                         default:
1499                                 ; /* error: don't know how to write this file */
1500                 }
1501
1502                 sens = sens->next;
1503         }
1504 }
1505
1506 static void write_controllers(WriteData *wd, ListBase *lb)
1507 {
1508         bController *cont;
1509
1510         cont = lb->first;
1511         while (cont) {
1512                 writestruct(wd, DATA, bController, 1, cont);
1513
1514                 writedata(wd, DATA, sizeof(void *) * cont->totlinks, cont->links);
1515
1516                 switch (cont->type) {
1517                         case CONT_EXPRESSION:
1518                                 writestruct(wd, DATA, bExpressionCont, 1, cont->data);
1519                                 break;
1520                         case CONT_PYTHON:
1521                                 writestruct(wd, DATA, bPythonCont, 1, cont->data);
1522                                 break;
1523                         default:
1524                                 ; /* error: don't know how to write this file */
1525                 }
1526
1527                 cont = cont->next;
1528         }
1529 }
1530
1531 static void write_actuators(WriteData *wd, ListBase *lb)
1532 {
1533         bActuator *act;
1534
1535         act = lb->first;
1536         while (act) {
1537                 writestruct(wd, DATA, bActuator, 1, act);
1538
1539                 switch (act->type) {
1540                         case ACT_ACTION:
1541                         case ACT_SHAPEACTION:
1542                                 writestruct(wd, DATA, bActionActuator, 1, act->data);
1543                                 break;
1544                         case ACT_SOUND:
1545                                 writestruct(wd, DATA, bSoundActuator, 1, act->data);
1546                                 break;
1547                         case ACT_OBJECT:
1548                                 writestruct(wd, DATA, bObjectActuator, 1, act->data);
1549                                 break;
1550                         case ACT_PROPERTY:
1551                                 writestruct(wd, DATA, bPropertyActuator, 1, act->data);
1552                                 break;
1553                         case ACT_CAMERA:
1554                                 writestruct(wd, DATA, bCameraActuator, 1, act->data);
1555                                 break;
1556                         case ACT_CONSTRAINT:
1557                                 writestruct(wd, DATA, bConstraintActuator, 1, act->data);
1558                                 break;
1559                         case ACT_EDIT_OBJECT:
1560                                 writestruct(wd, DATA, bEditObjectActuator, 1, act->data);
1561                                 break;
1562                         case ACT_SCENE:
1563                                 writestruct(wd, DATA, bSceneActuator, 1, act->data);
1564                                 break;
1565                         case ACT_GROUP:
1566                                 writestruct(wd, DATA, bGroupActuator, 1, act->data);
1567                                 break;
1568                         case ACT_RANDOM:
1569                                 writestruct(wd, DATA, bRandomActuator, 1, act->data);
1570                                 break;
1571                         case ACT_MESSAGE:
1572                                 writestruct(wd, DATA, bMessageActuator, 1, act->data);
1573                                 break;
1574                         case ACT_GAME:
1575                                 writestruct(wd, DATA, bGameActuator, 1, act->data);
1576                                 break;
1577                         case ACT_VISIBILITY:
1578                                 writestruct(wd, DATA, bVisibilityActuator, 1, act->data);
1579                                 break;
1580                         case ACT_2DFILTER:
1581                                 writestruct(wd, DATA, bTwoDFilterActuator, 1, act->data);
1582                                 break;
1583                         case ACT_PARENT:
1584                                 writestruct(wd, DATA, bParentActuator, 1, act->data);
1585                                 break;
1586                         case ACT_STATE:
1587                                 writestruct(wd, DATA, bStateActuator, 1, act->data);
1588                                 break;
1589                         case ACT_ARMATURE:
1590                                 writestruct(wd, DATA, bArmatureActuator, 1, act->data);
1591                                 break;
1592                         case ACT_STEERING:
1593                                 writestruct(wd, DATA, bSteeringActuator, 1, act->data);
1594                                 break;
1595                         case ACT_MOUSE:
1596                                 writestruct(wd, DATA, bMouseActuator, 1, act->data);
1597                                 break;
1598                         default:
1599                                 ; /* error: don't know how to write this file */
1600                 }
1601
1602                 act = act->next;
1603         }
1604 }
1605
1606 static void write_motionpath(WriteData *wd, bMotionPath *mpath)
1607 {
1608         /* sanity checks */
1609         if (mpath == NULL) {
1610                 return;
1611         }
1612
1613         /* firstly, just write the motionpath struct */
1614         writestruct(wd, DATA, bMotionPath, 1, mpath);
1615
1616         /* now write the array of data */
1617         writestruct(wd, DATA, bMotionPathVert, mpath->length, mpath->points);
1618 }
1619
1620 static void write_constraints(WriteData *wd, ListBase *conlist)
1621 {
1622         bConstraint *con;
1623
1624         for (con = conlist->first; con; con = con->next) {
1625                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
1626
1627                 /* Write the specific data */
1628                 if (cti && con->data) {
1629                         /* firstly, just write the plain con->data struct */
1630                         writestruct_id(wd, DATA, cti->structName, 1, con->data);
1631
1632                         /* do any constraint specific stuff */
1633                         switch (con->type) {
1634                                 case CONSTRAINT_TYPE_PYTHON:
1635                                 {
1636                                         bPythonConstraint *data = con->data;
1637                                         bConstraintTarget *ct;
1638
1639                                         /* write targets */
1640                                         for (ct = data->targets.first; ct; ct = ct->next) {
1641                                                 writestruct(wd, DATA, bConstraintTarget, 1, ct);
1642                                         }
1643
1644                                         /* Write ID Properties -- and copy this comment EXACTLY for easy finding
1645                                          * of library blocks that implement this.*/
1646                                         IDP_WriteProperty(data->prop, wd);
1647
1648                                         break;
1649                                 }
1650                                 case CONSTRAINT_TYPE_SPLINEIK:
1651                                 {
1652                                         bSplineIKConstraint *data = con->data;
1653
1654                                         /* write points array */
1655                                         writedata(wd, DATA, sizeof(float) * (data->numpoints), data->points);
1656
1657                                         break;
1658                                 }
1659                         }
1660                 }
1661
1662                 /* Write the constraint */
1663                 writestruct(wd, DATA, bConstraint, 1, con);
1664         }
1665 }
1666
1667 static void write_pose(WriteData *wd, bPose *pose)
1668 {
1669         bPoseChannel *chan;
1670         bActionGroup *grp;
1671
1672         /* Write each channel */
1673         if (pose == NULL) {
1674                 return;
1675         }
1676
1677         /* Write channels */
1678         for (chan = pose->chanbase.first; chan; chan = chan->next) {
1679                 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
1680                  * of library blocks that implement this.*/
1681                 if (chan->prop) {
1682                         IDP_WriteProperty(chan->prop, wd);
1683                 }
1684
1685                 write_constraints(wd, &chan->constraints);
1686
1687                 write_motionpath(wd, chan->mpath);
1688
1689                 /* prevent crashes with autosave,
1690                  * when a bone duplicated in editmode has not yet been assigned to its posechannel */
1691                 if (chan->bone) {
1692                         /* gets restored on read, for library armatures */
1693                         chan->selectflag = chan->bone->flag & BONE_SELECTED;
1694                 }
1695
1696                 writestruct(wd, DATA, bPoseChannel, 1, chan);
1697         }
1698
1699         /* Write groups */
1700         for (grp = pose->agroups.first; grp; grp = grp->next) {
1701                 writestruct(wd, DATA, bActionGroup, 1, grp);
1702         }
1703
1704         /* write IK param */
1705         if (pose->ikparam) {
1706                 const char *structname = BKE_pose_ikparam_get_name(pose);
1707                 if (structname) {
1708                         writestruct_id(wd, DATA, structname, 1, pose->ikparam);
1709                 }
1710         }
1711
1712         /* Write this pose */
1713         writestruct(wd, DATA, bPose, 1, pose);
1714
1715 }
1716
1717 static void write_defgroups(WriteData *wd, ListBase *defbase)
1718 {
1719         for (bDeformGroup *defgroup = defbase->first; defgroup; defgroup = defgroup->next) {
1720                 writestruct(wd, DATA, bDeformGroup, 1, defgroup);
1721         }
1722 }
1723
1724 static void write_modifiers(WriteData *wd, ListBase *modbase)
1725 {
1726         ModifierData *md;
1727
1728         if (modbase == NULL) {
1729                 return;
1730         }
1731
1732         for (md = modbase->first; md; md = md->next) {
1733                 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1734                 if (mti == NULL) {
1735                         return;
1736                 }
1737
1738                 writestruct_id(wd, DATA, mti->structName, 1, md);
1739
1740                 if (md->type == eModifierType_Hook) {
1741                         HookModifierData *hmd = (HookModifierData *)md;
1742
1743                         if (hmd->curfalloff) {
1744                                 write_curvemapping(wd, hmd->curfalloff);
1745                         }
1746
1747                         writedata(wd, DATA, sizeof(int) * hmd->totindex, hmd->indexar);
1748                 }
1749                 else if (md->type == eModifierType_Cloth) {
1750                         ClothModifierData *clmd = (ClothModifierData *)md;
1751
1752                         writestruct(wd, DATA, ClothSimSettings, 1, clmd->sim_parms);
1753                         writestruct(wd, DATA, ClothCollSettings, 1, clmd->coll_parms);
1754                         writestruct(wd, DATA, EffectorWeights, 1, clmd->sim_parms->effector_weights);
1755                         write_pointcaches(wd, &clmd->ptcaches);
1756                 }
1757                 else if (md->type == eModifierType_Smoke) {
1758                         SmokeModifierData *smd = (SmokeModifierData *)md;
1759
1760                         if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
1761                                 if (smd->domain) {
1762                                         write_pointcaches(wd, &(smd->domain->ptcaches[0]));
1763
1764                                         /* create fake pointcache so that old blender versions can read it */
1765                                         smd->domain->point_cache[1] = BKE_ptcache_add(&smd->domain->ptcaches[1]);
1766                                         smd->domain->point_cache[1]->flag |= PTCACHE_DISK_CACHE | PTCACHE_FAKE_SMOKE;
1767                                         smd->domain->point_cache[1]->step = 1;
1768
1769                                         write_pointcaches(wd, &(smd->domain->ptcaches[1]));
1770
1771                                         if (smd->domain->coba) {
1772                                                 writestruct(wd, DATA, ColorBand, 1, smd->domain->coba);
1773                                         }
1774                                 }
1775
1776                                 writestruct(wd, DATA, SmokeDomainSettings, 1, smd->domain);
1777
1778                                 if (smd->domain) {
1779                                         /* cleanup the fake pointcache */
1780                                         BKE_ptcache_free_list(&smd->domain->ptcaches[1]);
1781                                         smd->domain->point_cache[1] = NULL;
1782
1783                                         writestruct(wd, DATA, EffectorWeights, 1, smd->domain->effector_weights);
1784                                 }
1785                         }
1786                         else if (smd->type & MOD_SMOKE_TYPE_FLOW) {
1787                                 writestruct(wd, DATA, SmokeFlowSettings, 1, smd->flow);
1788                         }
1789                         else if (smd->type & MOD_SMOKE_TYPE_COLL) {
1790                                 writestruct(wd, DATA, SmokeCollSettings, 1, smd->coll);
1791                         }
1792                 }
1793                 else if (md->type == eModifierType_Fluidsim) {
1794                         FluidsimModifierData *fluidmd = (FluidsimModifierData *)md;
1795
1796                         writestruct(wd, DATA, FluidsimSettings, 1, fluidmd->fss);
1797                 }
1798                 else if (md->type == eModifierType_DynamicPaint) {
1799                         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
1800
1801                         if (pmd->canvas) {
1802                                 DynamicPaintSurface *surface;
1803                                 writestruct(wd, DATA, DynamicPaintCanvasSettings, 1, pmd->canvas);
1804
1805                                 /* write surfaces */
1806                                 for (surface = pmd->canvas->surfaces.first; surface; surface = surface->next) {
1807                                         writestruct(wd, DATA, DynamicPaintSurface, 1, surface);
1808                                 }
1809                                 /* write caches and effector weights */
1810                                 for (surface = pmd->canvas->surfaces.first; surface; surface = surface->next) {
1811                                         write_pointcaches(wd, &(surface->ptcaches));
1812
1813                                         writestruct(wd, DATA, EffectorWeights, 1, surface->effector_weights);
1814                                 }
1815                         }
1816                         if (pmd->brush) {
1817                                 writestruct(wd, DATA, DynamicPaintBrushSettings, 1, pmd->brush);
1818                                 writestruct(wd, DATA, ColorBand, 1, pmd->brush->paint_ramp);
1819                                 writestruct(wd, DATA, ColorBand, 1, pmd->brush->vel_ramp);
1820                         }
1821                 }
1822                 else if (md->type == eModifierType_Collision) {
1823
1824 #if 0
1825                         CollisionModifierData *collmd = (CollisionModifierData *)md;
1826                         // TODO: CollisionModifier should use pointcache
1827                         // + have proper reset events before enabling this
1828                         writestruct(wd, DATA, MVert, collmd->numverts, collmd->x);
1829                         writestruct(wd, DATA, MVert, collmd->numverts, collmd->xnew);
1830                         writestruct(wd, DATA, MFace, collmd->numfaces, collmd->mfaces);
1831 #endif
1832                 }
1833                 else if (md->type == eModifierType_MeshDeform) {
1834                         MeshDeformModifierData *mmd = (MeshDeformModifierData *)md;
1835                         int size = mmd->dyngridsize;
1836
1837                         writestruct(wd, DATA, MDefInfluence, mmd->totinfluence, mmd->bindinfluences);
1838                         writedata(wd, DATA, sizeof(int) * (mmd->totvert + 1), mmd->bindoffsets);
1839                         writedata(wd, DATA, sizeof(float) * 3 * mmd->totcagevert,
1840                                   mmd->bindcagecos);
1841                         writestruct(wd, DATA, MDefCell, size * size * size, mmd->dyngrid);
1842                         writestruct(wd, DATA, MDefInfluence, mmd->totinfluence, mmd->dyninfluences);
1843                         writedata(wd, DATA, sizeof(int) * mmd->totvert, mmd->dynverts);
1844                 }
1845                 else if (md->type == eModifierType_Warp) {
1846                         WarpModifierData *tmd = (WarpModifierData *)md;
1847                         if (tmd->curfalloff) {
1848                                 write_curvemapping(wd, tmd->curfalloff);
1849                         }
1850                 }
1851                 else if (md->type == eModifierType_WeightVGEdit) {
1852                         WeightVGEditModifierData *wmd = (WeightVGEditModifierData *)md;
1853
1854                         if (wmd->cmap_curve) {
1855                                 write_curvemapping(wd, wmd->cmap_curve);
1856                         }
1857                 }
1858                 else if (md->type == eModifierType_LaplacianDeform) {
1859                         LaplacianDeformModifierData *lmd = (LaplacianDeformModifierData *)md;
1860
1861                         writedata(wd, DATA, sizeof(float) * lmd->total_verts * 3, lmd->vertexco);
1862                 }
1863                 else if (md->type == eModifierType_CorrectiveSmooth) {
1864                         CorrectiveSmoothModifierData *csmd = (CorrectiveSmoothModifierData *)md;
1865
1866                         if (csmd->bind_coords) {
1867                                 writedata(wd, DATA, sizeof(float[3]) * csmd->bind_coords_num, csmd->bind_coords);
1868                         }
1869                 }
1870                 else if (md->type == eModifierType_SurfaceDeform) {
1871                         SurfaceDeformModifierData *smd = (SurfaceDeformModifierData *)md;
1872
1873                         writestruct(wd, DATA, SDefVert, smd->numverts, smd->verts);
1874
1875                         if (smd->verts) {
1876                                 for (int i = 0; i < smd->numverts; i++) {
1877                                         writestruct(wd, DATA, SDefBind, smd->verts[i].numbinds, smd->verts[i].binds);
1878
1879                                         if (smd->verts[i].binds) {
1880                                                 for (int j = 0; j < smd->verts[i].numbinds; j++) {
1881                                                         writedata(wd, DATA, sizeof(int) * smd->verts[i].binds[j].numverts, smd->verts[i].binds[j].vert_inds);
1882
1883                                                         if (smd->verts[i].binds[j].mode == MOD_SDEF_MODE_CENTROID ||
1884                                                             smd->verts[i].binds[j].mode == MOD_SDEF_MODE_LOOPTRI)
1885                                                         {
1886                                                                 writedata(wd, DATA, sizeof(float) * 3, smd->verts[i].binds[j].vert_weights);
1887                                                         }
1888                                                         else {
1889                                                                 writedata(wd, DATA, sizeof(float) * smd->verts[i].binds[j].numverts, smd->verts[i].binds[j].vert_weights);
1890                                                         }
1891                                                 }
1892                                         }
1893                                 }
1894                         }
1895                 }
1896         }
1897 }
1898
1899 static void write_object(WriteData *wd, Object *ob)
1900 {
1901         if (ob->id.us > 0 || wd->use_memfile) {
1902                 /* write LibData */
1903                 writestruct(wd, ID_OB, Object, 1, ob);
1904                 write_iddata(wd, &ob->id);
1905
1906                 if (ob->adt) {
1907                         write_animdata(wd, ob->adt);
1908                 }
1909
1910                 /* direct data */
1911                 writedata(wd, DATA, sizeof(void *) * ob->totcol, ob->mat);
1912                 writedata(wd, DATA, sizeof(char) * ob->totcol, ob->matbits);
1913                 /* write_effects(wd, &ob->effect); */ /* not used anymore */
1914                 write_properties(wd, &ob->prop);
1915                 write_sensors(wd, &ob->sensors);
1916                 write_controllers(wd, &ob->controllers);
1917                 write_actuators(wd, &ob->actuators);
1918
1919                 if (ob->type == OB_ARMATURE) {
1920                         bArmature *arm = ob->data;
1921                         if (arm && ob->pose && arm->act_bone) {
1922                                 BLI_strncpy(ob->pose->proxy_act_bone, arm->act_bone->name, sizeof(ob->pose->proxy_act_bone));
1923                         }
1924                 }
1925
1926                 write_pose(wd, ob->pose);
1927                 write_defgroups(wd, &ob->defbase);
1928                 write_constraints(wd, &ob->constraints);
1929                 write_motionpath(wd, ob->mpath);
1930
1931                 writestruct(wd, DATA, PartDeflect, 1, ob->pd);
1932                 writestruct(wd, DATA, SoftBody, 1, ob->soft);
1933                 if (ob->soft) {
1934                         write_pointcaches(wd, &ob->soft->ptcaches);
1935                         writestruct(wd, DATA, EffectorWeights, 1, ob->soft->effector_weights);
1936                 }
1937                 writestruct(wd, DATA, BulletSoftBody, 1, ob->bsoft);
1938
1939                 if (ob->rigidbody_object) {
1940                         /* TODO: if any extra data is added to handle duplis, will need separate function then */
1941                         writestruct(wd, DATA, RigidBodyOb, 1, ob->rigidbody_object);
1942                 }
1943                 if (ob->rigidbody_constraint) {
1944                         writestruct(wd, DATA, RigidBodyCon, 1, ob->rigidbody_constraint);
1945                 }
1946
1947                 if (ob->type == OB_EMPTY && ob->empty_drawtype == OB_EMPTY_IMAGE) {
1948                         writestruct(wd, DATA, ImageUser, 1, ob->iuser);
1949                 }
1950
1951                 write_particlesystems(wd, &ob->particlesystem);
1952                 write_modifiers(wd, &ob->modifiers);
1953
1954                 writelist(wd, DATA, LinkData, &ob->pc_ids);
1955                 writelist(wd, DATA, LodLevel, &ob->lodlevels);
1956
1957                 write_previews(wd, ob->preview);
1958         }
1959 }
1960
1961
1962 static void write_vfont(WriteData *wd, VFont *vf)
1963 {
1964         if (vf->id.us > 0 || wd->use_memfile) {
1965                 /* write LibData */
1966                 writestruct(wd, ID_VF, VFont, 1, vf);
1967                 write_iddata(wd, &vf->id);
1968
1969                 /* direct data */
1970                 if (vf->packedfile) {
1971                         PackedFile *pf = vf->packedfile;
1972                         writestruct(wd, DATA, PackedFile, 1, pf);
1973                         writedata(wd, DATA, pf->size, pf->data);
1974                 }
1975         }
1976 }
1977
1978
1979 static void write_key(WriteData *wd, Key *key)
1980 {
1981         if (key->id.us > 0 || wd->use_memfile) {
1982                 /* write LibData */
1983                 writestruct(wd, ID_KE, Key, 1, key);
1984                 write_iddata(wd, &key->id);
1985
1986                 if (key->adt) {
1987                         write_animdata(wd, key->adt);
1988                 }
1989
1990                 /* direct data */
1991                 for (KeyBlock *kb = key->block.first; kb; kb = kb->next) {
1992                         writestruct(wd, DATA, KeyBlock, 1, kb);
1993                         if (kb->data) {
1994                                 writedata(wd, DATA, kb->totelem * key->elemsize, kb->data);
1995                         }
1996                 }
1997         }
1998 }
1999
2000 static void write_camera(WriteData *wd, Camera *cam)
2001 {
2002         if (cam->id.us > 0 || wd->use_memfile) {
2003                 /* write LibData */
2004                 writestruct(wd, ID_CA, Camera, 1, cam);
2005                 write_iddata(wd, &cam->id);
2006
2007                 if (cam->adt) {
2008                         write_animdata(wd, cam->adt);
2009                 }
2010         }
2011 }
2012
2013 static void write_mball(WriteData *wd, MetaBall *mb)
2014 {
2015         if (mb->id.us > 0 || wd->use_memfile) {
2016                 /* write LibData */
2017                 writestruct(wd, ID_MB, MetaBall, 1, mb);
2018                 write_iddata(wd, &mb->id);
2019
2020                 /* direct data */
2021                 writedata(wd, DATA, sizeof(void *) * mb->totcol, mb->mat);
2022                 if (mb->adt) {
2023                         write_animdata(wd, mb->adt);
2024                 }
2025
2026                 for (MetaElem *ml = mb->elems.first; ml; ml = ml->next) {
2027                         writestruct(wd, DATA, MetaElem, 1, ml);
2028                 }
2029         }
2030 }
2031
2032 static void write_curve(WriteData *wd, Curve *cu)
2033 {
2034         if (cu->id.us > 0 || wd->use_memfile) {
2035                 /* write LibData */
2036                 writestruct(wd, ID_CU, Curve, 1, cu);
2037                 write_iddata(wd, &cu->id);
2038
2039                 /* direct data */
2040                 writedata(wd, DATA, sizeof(void *) * cu->totcol, cu->mat);
2041                 if (cu->adt) {
2042                         write_animdata(wd, cu->adt);
2043                 }
2044
2045                 if (cu->vfont) {
2046                         writedata(wd, DATA, cu->len + 1, cu->str);
2047                         writestruct(wd, DATA, CharInfo, cu->len_wchar + 1, cu->strinfo);
2048                         writestruct(wd, DATA, TextBox, cu->totbox, cu->tb);
2049                 }
2050                 else {
2051                         /* is also the order of reading */
2052                         for (Nurb *nu = cu->nurb.first; nu; nu = nu->next) {
2053                                 writestruct(wd, DATA, Nurb, 1, nu);
2054                         }
2055                         for (Nurb *nu = cu->nurb.first; nu; nu = nu->next) {
2056                                 if (nu->type == CU_BEZIER) {
2057                                         writestruct(wd, DATA, BezTriple, nu->pntsu, nu->bezt);
2058                                 }
2059                                 else {
2060                                         writestruct(wd, DATA, BPoint, nu->pntsu * nu->pntsv, nu->bp);
2061                                         if (nu->knotsu) {
2062                                                 writedata(wd, DATA, KNOTSU(nu) * sizeof(float), nu->knotsu);
2063                                         }
2064                                         if (nu->knotsv) {
2065                                                 writedata(wd, DATA, KNOTSV(nu) * sizeof(float), nu->knotsv);
2066                                         }
2067                                 }
2068                         }
2069                 }
2070         }
2071 }
2072
2073 static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
2074 {
2075         if (dvlist) {
2076
2077                 /* Write the dvert list */
2078                 writestruct(wd, DATA, MDeformVert, count, dvlist);
2079
2080                 /* Write deformation data for each dvert */
2081                 for (int i = 0; i < count; i++) {
2082                         if (dvlist[i].dw) {
2083                                 writestruct(wd, DATA, MDeformWeight, dvlist[i].totweight, dvlist[i].dw);
2084                         }
2085                 }
2086         }
2087 }
2088
2089 static void write_mdisps(WriteData *wd, int count, MDisps *mdlist, int external)
2090 {
2091         if (mdlist) {
2092                 int i;
2093
2094                 writestruct(wd, DATA, MDisps, count, mdlist);
2095                 for (i = 0; i < count; ++i) {
2096                         MDisps *md = &mdlist[i];
2097                         if (md->disps) {
2098                                 if (!external) {
2099                                         writedata(wd, DATA, sizeof(float) * 3 * md->totdisp, md->disps);
2100                                 }
2101                         }
2102
2103                         if (md->hidden) {
2104                                 writedata(wd, DATA, BLI_BITMAP_SIZE(md->totdisp), md->hidden);
2105                         }
2106                 }
2107         }
2108 }
2109
2110 static void write_grid_paint_mask(WriteData *wd, int count, GridPaintMask *grid_paint_mask)
2111 {
2112         if (grid_paint_mask) {
2113                 int i;
2114
2115                 writestruct(wd, DATA, GridPaintMask, count, grid_paint_mask);
2116                 for (i = 0; i < count; ++i) {
2117                         GridPaintMask *gpm = &grid_paint_mask[i];
2118                         if (gpm->data) {
2119                                 const int gridsize = BKE_ccg_gridsize(gpm->level);
2120                                 writedata(wd, DATA,
2121                                           sizeof(*gpm->data) * gridsize * gridsize,
2122                                           gpm->data);
2123                         }
2124                 }
2125         }
2126 }
2127
2128 static void write_customdata(
2129         WriteData *wd, ID *id, int count, CustomData *data, CustomDataLayer *layers,
2130         int partial_type, int partial_count)
2131 {
2132         int i;
2133
2134         /* write external customdata (not for undo) */
2135         if (data->external && (wd->use_memfile == false)) {
2136                 CustomData_external_write(data, id, CD_MASK_MESH, count, 0);
2137         }
2138
2139         writestruct_at_address(wd, DATA, CustomDataLayer, data->totlayer, data->layers, layers);
2140
2141         for (i = 0; i < data->totlayer; i++) {
2142                 CustomDataLayer *layer = &layers[i];
2143                 const char *structname;
2144                 int structnum, datasize;
2145
2146                 if (layer->type == CD_MDEFORMVERT) {
2147                         /* layer types that allocate own memory need special handling */
2148                         write_dverts(wd, count, layer->data);
2149                 }
2150                 else if (layer->type == CD_MDISPS) {
2151                         write_mdisps(wd, count, layer->data, layer->flag & CD_FLAG_EXTERNAL);
2152                 }
2153                 else if (layer->type == CD_PAINT_MASK) {
2154                         const float *layer_data = layer->data;
2155                         writedata(wd, DATA, sizeof(*layer_data) * count, layer_data);
2156                 }
2157                 else if (layer->type == CD_GRID_PAINT_MASK) {
2158                         write_grid_paint_mask(wd, count, layer->data);
2159                 }
2160                 else {
2161                         CustomData_file_write_info(layer->type, &structname, &structnum);
2162                         if (structnum) {
2163                                 /* when using partial visibility, the MEdge and MFace layers
2164                                  * are smaller than the original, so their type and count is
2165                                  * passed to make this work */
2166                                 if (layer->type != partial_type) {
2167                                         datasize = structnum * count;
2168                                 }
2169                                 else {
2170                                         datasize = structnum * partial_count;
2171                                 }
2172
2173                                 writestruct_id(wd, DATA, structname, datasize, layer->data);
2174                         }
2175                         else {
2176                                 printf("%s error: layer '%s':%d - can't be written to file\n",
2177                                        __func__, structname, layer->type);
2178                         }
2179                 }
2180         }
2181
2182         if (data->external) {
2183                 writestruct(wd, DATA, CustomDataExternal, 1, data->external);
2184         }
2185 }
2186
2187 static void write_mesh(WriteData *wd, Mesh *mesh)
2188 {
2189 #ifdef USE_BMESH_SAVE_AS_COMPAT
2190         const bool save_for_old_blender = wd->use_mesh_compat;  /* option to save with older mesh format */
2191 #else
2192         const bool save_for_old_blender = false;
2193 #endif
2194
2195         CustomDataLayer *vlayers = NULL, vlayers_buff[CD_TEMP_CHUNK_SIZE];
2196         CustomDataLayer *elayers = NULL, elayers_buff[CD_TEMP_CHUNK_SIZE];
2197         CustomDataLayer *flayers = NULL, flayers_buff[CD_TEMP_CHUNK_SIZE];
2198         CustomDataLayer *llayers = NULL, llayers_buff[CD_TEMP_CHUNK_SIZE];
2199         CustomDataLayer *players = NULL, players_buff[CD_TEMP_CHUNK_SIZE];
2200
2201         if (mesh->id.us > 0 || wd->use_memfile) {
2202                 /* write LibData */
2203                 if (!save_for_old_blender) {
2204                         /* write a copy of the mesh, don't modify in place because it is
2205                          * not thread safe for threaded renders that are reading this */
2206                         Mesh *old_mesh = mesh;
2207                         Mesh copy_mesh = *mesh;
2208                         mesh = &copy_mesh;
2209
2210 #ifdef USE_BMESH_SAVE_WITHOUT_MFACE
2211                         /* cache only - don't write */
2212                         mesh->mface = NULL;
2213                         mesh->totface = 0;
2214                         memset(&mesh->fdata, 0, sizeof(mesh->fdata));
2215 #endif /* USE_BMESH_SAVE_WITHOUT_MFACE */
2216
2217                         /**
2218                          * Those calls:
2219                          *   - Reduce mesh->xdata.totlayer to number of layers to write.
2220                          *   - Fill xlayers with those layers to be written.
2221                          * Note that mesh->xdata is from now on invalid for Blender, but this is why the whole mesh is
2222                          * a temp local copy!
2223                          */
2224                         CustomData_file_write_prepare(&mesh->vdata, &vlayers, vlayers_buff, ARRAY_SIZE(vlayers_buff));
2225                         CustomData_file_write_prepare(&mesh->edata, &elayers, elayers_buff, ARRAY_SIZE(elayers_buff));
2226 #ifndef USE_BMESH_SAVE_WITHOUT_MFACE  /* Do not copy org fdata in this case!!! */
2227                         CustomData_file_write_prepare(&mesh->fdata, &flayers, flayers_buff, ARRAY_SIZE(flayers_buff));
2228 #else
2229                         flayers = flayers_buff;
2230 #endif
2231                         CustomData_file_write_prepare(&mesh->ldata, &llayers, llayers_buff, ARRAY_SIZE(llayers_buff));
2232                         CustomData_file_write_prepare(&mesh->pdata, &players, players_buff, ARRAY_SIZE(players_buff));
2233
2234                         writestruct_at_address(wd, ID_ME, Mesh, 1, old_mesh, mesh);
2235                         write_iddata(wd, &mesh->id);
2236
2237                         /* direct data */
2238                         if (mesh->adt) {
2239                                 write_animdata(wd, mesh->adt);
2240                         }
2241
2242                         writedata(wd, DATA, sizeof(void *) * mesh->totcol, mesh->mat);
2243                         writedata(wd, DATA, sizeof(MSelect) * mesh->totselect, mesh->mselect);
2244
2245                         write_customdata(wd, &mesh->id, mesh->totvert, &mesh->vdata, vlayers, -1, 0);
2246                         write_customdata(wd, &mesh->id, mesh->totedge, &mesh->edata, elayers, -1, 0);
2247                         /* fdata is really a dummy - written so slots align */
2248                         write_customdata(wd, &mesh->id, mesh->totface, &mesh->fdata, flayers, -1, 0);
2249                         write_customdata(wd, &mesh->id, mesh->totloop, &mesh->ldata, llayers, -1, 0);
2250                         write_customdata(wd, &mesh->id, mesh->totpoly, &mesh->pdata, players, -1, 0);
2251
2252                         /* restore pointer */
2253                         mesh = old_mesh;
2254                 }
2255                 else {
2256
2257 #ifdef USE_BMESH_SAVE_AS_COMPAT
2258                         /* write a copy of the mesh, don't modify in place because it is
2259                          * not thread safe for threaded renders that are reading this */
2260                         Mesh *old_mesh = mesh;
2261                         Mesh copy_mesh = *mesh;
2262                         mesh = &copy_mesh;
2263
2264                         mesh->mpoly = NULL;
2265                         mesh->mface = NULL;
2266                         mesh->totface = 0;
2267                         mesh->totpoly = 0;
2268                         mesh->totloop = 0;
2269                         CustomData_reset(&mesh->fdata);
2270                         CustomData_reset(&mesh->pdata);
2271                         CustomData_reset(&mesh->ldata);
2272                         mesh->edit_btmesh = NULL;
2273
2274                         /* now fill in polys to mfaces */
2275                         /* XXX This breaks writing design, by using temp allocated memory, which will likely generate
2276                          *     duplicates in stored 'old' addresses.
2277                          *     This is very bad, but do not see easy way to avoid this, aside from generating those data
2278                          *     outside of save process itself.
2279                          *     Maybe we can live with this, though?
2280                          */
2281                         mesh->totface = BKE_mesh_mpoly_to_mface(
2282                                 &mesh->fdata, &old_mesh->ldata, &old_mesh->pdata,
2283                                 mesh->totface, old_mesh->totloop, old_mesh->totpoly);
2284
2285                         BKE_mesh_update_customdata_pointers(mesh, false);
2286
2287                         CustomData_file_write_prepare(&mesh->vdata, &vlayers, vlayers_buff, ARRAY_SIZE(vlayers_buff));
2288                         CustomData_file_write_prepare(&mesh->edata, &elayers, elayers_buff, ARRAY_SIZE(elayers_buff));
2289                         CustomData_file_write_prepare(&mesh->fdata, &flayers, flayers_buff, ARRAY_SIZE(flayers_buff));
2290 #if 0
2291                         CustomData_file_write_prepare(&mesh->ldata, &llayers, llayers_buff, ARRAY_SIZE(llayers_buff));
2292                         CustomData_file_write_prepare(&mesh->pdata, &players, players_buff, ARRAY_SIZE(players_buff));
2293 #endif
2294
2295                         writestruct_at_address(wd, ID_ME, Mesh, 1, old_mesh, mesh);
2296                         write_iddata(wd, &mesh->id);
2297
2298                         /* direct data */
2299                         if (mesh->adt) {
2300                                 write_animdata(wd, mesh->adt);
2301                         }
2302
2303                         writedata(wd, DATA, sizeof(void *) * mesh->totcol, mesh->mat);
2304                         /* writedata(wd, DATA, sizeof(MSelect) * mesh->totselect, mesh->mselect); */ /* pre-bmesh NULL's */
2305
2306                         write_customdata(wd, &mesh->id, mesh->totvert, &mesh->vdata, vlayers, -1, 0);
2307                         write_customdata(wd, &mesh->id, mesh->totedge, &mesh->edata, elayers, -1, 0);
2308                         write_customdata(wd, &mesh->id, mesh->totface, &mesh->fdata, flayers, -1, 0);
2309                         /* harmless for older blender versioins but _not_ writing these keeps file size down */
2310 #if 0
2311                         write_customdata(wd, &mesh->id, mesh->totloop, &mesh->ldata, llayers, -1, 0);
2312                         write_customdata(wd, &mesh->id, mesh->totpoly, &mesh->pdata, players, -1, 0);
2313 #endif
2314
2315                         CustomData_free(&mesh->fdata, mesh->totface);
2316                         flayers = NULL;
2317
2318                         /* restore pointer */
2319                         mesh = old_mesh;
2320 #endif /* USE_BMESH_SAVE_AS_COMPAT */
2321                 }
2322         }
2323
2324         if (vlayers && vlayers != vlayers_buff) {
2325                 MEM_freeN(vlayers);
2326         }
2327         if (elayers && elayers != elayers_buff) {
2328                 MEM_freeN(elayers);
2329         }
2330         if (flayers && flayers != flayers_buff) {
2331                 MEM_freeN(flayers);
2332         }
2333         if (llayers && llayers != llayers_buff) {
2334                 MEM_freeN(llayers);
2335         }
2336         if (players && players != players_buff) {
2337                 MEM_freeN(players);
2338         }
2339 }
2340
2341 static void write_lattice(WriteData *wd, Lattice *lt)
2342 {
2343         if (lt->id.us > 0 || wd->use_memfile) {
2344                 /* write LibData */
2345                 writestruct(wd, ID_LT, Lattice, 1, lt);
2346                 write_iddata(wd, &lt->id);
2347
2348                 /* write animdata */
2349                 if (lt->adt) {
2350                         write_animdata(wd, lt->adt);
2351                 }
2352
2353                 /* direct data */
2354                 writestruct(wd, DATA, BPoint, lt->pntsu * lt->pntsv * lt->pntsw, lt->def);
2355
2356                 write_dverts(wd, lt->pntsu * lt->pntsv * lt->pntsw, lt->dvert);
2357         }
2358 }
2359
2360 static void write_image(WriteData *wd, Image *ima)
2361 {
2362         if (ima->id.us > 0 || wd->use_memfile) {
2363                 ImagePackedFile *imapf;
2364
2365                 /* Some trickery to keep forward compatibility of packed images. */
2366                 BLI_assert(ima->packedfile == NULL);
2367                 if (ima->packedfiles.first != NULL) {
2368                         imapf = ima->packedfiles.first;
2369                         ima->packedfile = imapf->packedfile;
2370                 }
2371
2372                 /* write LibData */
2373                 writestruct(wd, ID_IM, Image, 1, ima);
2374                 write_iddata(wd, &ima->id);
2375
2376                 for (imapf = ima->packedfiles.first; imapf; imapf = imapf->next) {
2377                         writestruct(wd, DATA, ImagePackedFile, 1, imapf);
2378                         if (imapf->packedfile) {
2379                                 PackedFile *pf = imapf->packedfile;
2380                                 writestruct(wd, DATA, PackedFile, 1, pf);
2381                                 writedata(wd, DATA, pf->size, pf->data);
2382                         }
2383                 }
2384
2385                 write_previews(wd, ima->preview);
2386
2387                 for (ImageView *iv = ima->views.first; iv; iv = iv->next) {
2388                         writestruct(wd, DATA, ImageView, 1, iv);
2389                 }
2390                 writestruct(wd, DATA, Stereo3dFormat, 1, ima->stereo3d_format);
2391
2392                 ima->packedfile = NULL;
2393         }
2394 }
2395
2396 static void write_texture(WriteData *wd, Tex *tex)
2397 {
2398         if (tex->id.us > 0 || wd->use_memfile) {
2399                 /* write LibData */
2400                 writestruct(wd, ID_TE, Tex, 1, tex);
2401                 write_iddata(wd, &tex->id);
2402
2403                 if (tex->adt) {
2404                         write_animdata(wd, tex->adt);
2405                 }
2406
2407                 /* direct data */
2408                 if (tex->coba) {
2409                         writestruct(wd, DATA, ColorBand, 1, tex->coba);
2410                 }
2411                 if (tex->type == TEX_ENVMAP && tex->env) {
2412                         writestruct(wd, DATA, EnvMap, 1, tex->env);
2413                 }
2414                 if (tex->type == TEX_POINTDENSITY && tex->pd) {
2415                         writestruct(wd, DATA, PointDensity, 1, tex->pd);
2416                         if (tex->pd->coba) {
2417                                 writestruct(wd, DATA, ColorBand, 1, tex->pd->coba);
2418                         }
2419                         if (tex->pd->falloff_curve) {
2420                                 write_curvemapping(wd, tex->pd->falloff_curve);
2421                         }
2422                 }
2423                 if (tex->type == TEX_VOXELDATA) {
2424                         writestruct(wd, DATA, VoxelData, 1, tex->vd);
2425                 }
2426                 if (tex->type == TEX_OCEAN && tex->ot) {
2427                         writestruct(wd, DATA, OceanTex, 1, tex->ot);
2428                 }
2429
2430                 /* nodetree is integral part of texture, no libdata */
2431                 if (tex->nodetree) {
2432                         writestruct(wd, DATA, bNodeTree, 1, tex->nodetree);
2433                         write_nodetree_nolib(wd, tex->nodetree);
2434                 }
2435
2436                 write_previews(wd, tex->preview);
2437         }
2438 }
2439
2440 static void write_material(WriteData *wd, Material *ma)
2441 {
2442         if (ma->id.us > 0 || wd->use_memfile) {
2443                 /* write LibData */
2444                 writestruct(wd, ID_MA, Material, 1, ma);
2445                 write_iddata(wd, &ma->id);
2446
2447                 if (ma->adt) {
2448                         write_animdata(wd, ma->adt);
2449                 }
2450
2451                 for (int a = 0; a < MAX_MTEX; a++) {
2452                         if (ma->mtex[a]) {
2453                                 writestruct(wd, DATA, MTex, 1, ma->mtex[a]);
2454                         }
2455                 }
2456
2457                 if (ma->ramp_col) {
2458                         writestruct(wd, DATA, ColorBand, 1, ma->ramp_col);
2459                 }
2460                 if (ma->ramp_spec) {
2461                         writestruct(wd, DATA, ColorBand, 1, ma->ramp_spec);
2462                 }
2463
2464                 /* nodetree is integral part of material, no libdata */
2465                 if (ma->nodetree) {
2466                         writestruct(wd, DATA, bNodeTree, 1, ma->nodetree);
2467                         write_nodetree_nolib(wd, ma->nodetree);
2468                 }
2469
2470                 write_previews(wd, ma->preview);
2471         }
2472 }
2473
2474 static void write_world(WriteData *wd, World *wrld)
2475 {
2476         if (wrld->id.us > 0 || wd->use_memfile) {
2477                 /* write LibData */
2478                 writestruct(wd, ID_WO, World, 1, wrld);
2479                 write_iddata(wd, &wrld->id);
2480
2481                 if (wrld->adt) {
2482                         write_animdata(wd, wrld->adt);
2483                 }
2484
2485                 for (int a = 0; a < MAX_MTEX; a++) {
2486                         if (wrld->mtex[a]) {
2487                                 writestruct(wd, DATA, MTex, 1, wrld->mtex[a]);
2488                         }
2489                 }
2490
2491                 /* nodetree is integral part of world, no libdata */
2492                 if (wrld->nodetree) {
2493                         writestruct(wd, DATA, bNodeTree, 1, wrld->nodetree);
2494                         write_nodetree_nolib(wd, wrld->nodetree);
2495                 }
2496
2497                 write_previews(wd, wrld->preview);
2498         }
2499 }
2500
2501 static void write_lamp(WriteData *wd, Lamp *la)
2502 {
2503         if (la->id.us > 0 || wd->use_memfile) {
2504                 /* write LibData */
2505                 writestruct(wd, ID_LA, Lamp, 1, la);
2506                 write_iddata(wd, &la->id);
2507
2508                 if (la->adt) {
2509                         write_animdata(wd, la->adt);
2510                 }
2511
2512                 /* direct data */
2513                 for (int a = 0; a < MAX_MTEX; a++) {
2514                         if (la->mtex[a]) {
2515                                 writestruct(wd, DATA, MTex, 1, la->mtex[a]);
2516                         }
2517                 }
2518
2519                 if (la->curfalloff) {
2520                         write_curvemapping(wd, la->curfalloff);
2521                 }
2522
2523                 /* nodetree is integral part of lamps, no libdata */
2524                 if (la->nodetree) {
2525                         writestruct(wd, DATA, bNodeTree, 1, la->nodetree);
2526                         write_nodetree_nolib(wd, la->nodetree);
2527                 }
2528
2529                 write_previews(wd, la->preview);
2530         }
2531 }
2532
2533 static void write_sequence_modifiers(WriteData *wd, ListBase *modbase)
2534 {
2535         SequenceModifierData *smd;
2536
2537         for (smd = modbase->first; smd; smd = smd->next) {
2538                 const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
2539
2540                 if (smti) {
2541                         writestruct_id(wd, DATA, smti->struct_name, 1, smd);
2542
2543                         if (smd->type == seqModifierType_Curves) {
2544                                 CurvesModifierData *cmd = (CurvesModifierData *)smd;
2545
2546                                 write_curvemapping(wd, &cmd->curve_mapping);
2547                         }
2548                         else if (smd->type == seqModifierType_HueCorrect) {
2549                                 HueCorrectModifierData *hcmd = (HueCorrectModifierData *)smd;
2550
2551                                 write_curvemapping(wd, &hcmd->curve_mapping);
2552                         }
2553                 }
2554                 else {
2555                         writestruct(wd, DATA, SequenceModifierData, 1, smd);
2556                 }
2557         }
2558 }
2559
2560 static void write_view_settings(WriteData *wd, ColorManagedViewSettings *view_settings)
2561 {
2562         if (view_settings->curve_mapping) {
2563                 write_curvemapping(wd, view_settings->curve_mapping);
2564         }
2565 }
2566
2567 static void write_paint(WriteData *wd, Paint *p)
2568 {
2569         if (p->cavity_curve) {
2570                 write_curvemapping(wd, p->cavity_curve);
2571         }
2572 }
2573
2574 static void write_scene(WriteData *wd, Scene *sce)
2575 {
2576         /* write LibData */
2577         writestruct(wd, ID_SCE, Scene, 1, sce);
2578         write_iddata(wd, &sce->id);
2579
2580         if (sce->adt) {
2581                 write_animdata(wd, sce->adt);
2582         }
2583         write_keyingsets(wd, &sce->keyingsets);
2584
2585         /* direct data */
2586         for (Base *base = sce->base.first; base; base = base->next) {
2587                 writestruct(wd, DATA, Base, 1, base);
2588         }
2589
2590         ToolSettings *tos = sce->toolsettings;
2591         writestruct(wd, DATA, ToolSettings, 1, tos);
2592         if (tos->vpaint) {
2593                 writestruct(wd, DATA, VPaint, 1, tos->vpaint);
2594                 write_paint(wd, &tos->vpaint->paint);
2595         }
2596         if (tos->wpaint) {
2597                 writestruct(wd, DATA, VPaint, 1, tos->wpaint);
2598                 write_paint(wd, &tos->wpaint->paint);
2599         }
2600         if (tos->sculpt) {
2601                 writestruct(wd, DATA, Sculpt, 1, tos->sculpt);
2602                 write_paint(wd, &tos->sculpt->paint);
2603         }
2604         if (tos->uvsculpt) {
2605                 writestruct(wd, DATA, UvSculpt, 1, tos->uvsculpt);
2606                 write_paint(wd, &tos->uvsculpt->paint);
2607         }
2608         /* write grease-pencil drawing brushes to file */
2609         writelist(wd, DATA, bGPDbrush, &tos->gp_brushes);
2610         for (bGPDbrush *brush = tos->gp_brushes.first; brush; brush = brush->next) {
2611                 if (brush->cur_sensitivity) {
2612                         write_curvemapping(wd, brush->cur_sensitivity);
2613                 }
2614                 if (brush->cur_strength) {
2615                         write_curvemapping(wd, brush->cur_strength);
2616                 }
2617                 if (brush->cur_jitter) {
2618                         write_curvemapping(wd, brush->cur_jitter);
2619                 }
2620         }
2621         /* write grease-pencil custom ipo curve to file */
2622         if (tos->gp_interpolate.custom_ipo) {
2623                 write_curvemapping(wd, tos->gp_interpolate.custom_ipo);
2624         }
2625
2626
2627         write_paint(wd, &tos->imapaint.paint);
2628
2629         Editing *ed = sce->ed;
2630         if (ed) {
2631                 Sequence *seq;
2632
2633                 writestruct(wd, DATA, Editing, 1, ed);
2634
2635                 /* reset write flags too */
2636
2637                 SEQ_BEGIN(ed, seq)
2638                 {
2639                         if (seq->strip) {
2640                                 seq->strip->done = false;
2641                         }
2642                         writestruct(wd, DATA, Sequence, 1, seq);
2643                 }
2644                 SEQ_END
2645
2646                 SEQ_BEGIN(ed, seq)
2647                 {
2648                         if (seq->strip && seq->strip->done == 0) {
2649                                 /* write strip with 'done' at 0 because readfile */
2650
2651                                 if (seq->effectdata) {
2652                                         switch (seq->type) {
2653                                                 case SEQ_TYPE_COLOR:
2654                                                         writestruct(wd, DATA, SolidColorVars, 1, seq->effectdata);
2655                                                         break;
2656                                                 case SEQ_TYPE_SPEED:
2657                                                         writestruct(wd, DATA, SpeedControlVars, 1, seq->effectdata);
2658                                                         break;
2659                                                 case SEQ_TYPE_WIPE:
2660                                                         writestruct(wd, DATA, WipeVars, 1, seq->effectdata);
2661                                                         break;
2662                                                 case SEQ_TYPE_GLOW:
2663                                                         writestruct(wd, DATA, GlowVars, 1, seq->effectdata);
2664                                                         break;
2665                                                 case SEQ_TYPE_TRANSFORM:
2666                                                         writestruct(wd, DATA, TransformVars, 1, seq->effectdata);
2667                                                         break;
2668                                                 case SEQ_TYPE_GAUSSIAN_BLUR:
2669                                                         writestruct(wd, DATA, GaussianBlurVars, 1, seq->effectdata);
2670                                                         break;
2671                                                 case SEQ_TYPE_TEXT:
2672                                                         writestruct(wd, DATA, TextVars, 1, seq->effectdata);
2673                                                         break;
2674                                                 case SEQ_TYPE_COLORMIX:
2675                                                         writestruct(wd, DATA, ColorMixVars, 1, seq->effectdata);
2676                                                         break;
2677                                         }
2678                                 }
2679
2680                                 writestruct(wd, DATA, Stereo3dFormat, 1, seq->stereo3d_format);
2681
2682                                 Strip *strip = seq->strip;
2683                                 writestruct(wd, DATA, Strip, 1, strip);
2684                                 if (seq->flag & SEQ_USE_CROP && strip->crop) {
2685                                         writestruct(wd, DATA, StripCrop, 1, strip->crop);
2686                                 }
2687                                 if (seq->flag & SEQ_USE_TRANSFORM && strip->transform) {
2688                                         writestruct(wd, DATA, StripTransform, 1, strip->transform);
2689                                 }
2690                                 if (seq->flag & SEQ_USE_PROXY && strip->proxy) {
2691                                         writestruct(wd, DATA, StripProxy, 1, strip->proxy);
2692                                 }
2693                                 if (seq->type == SEQ_TYPE_IMAGE) {
2694                                         writestruct(wd, DATA, StripElem,
2695                                                     MEM_allocN_len(strip->stripdata) / sizeof(struct StripElem),
2696                                                     strip->stripdata);
2697                                 }
2698                                 else if (ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SOUND_HD)) {
2699                                         writestruct(wd, DATA, StripElem, 1, strip->stripdata);
2700                                 }
2701
2702                                 strip->done = true;
2703                         }
2704
2705                         if (seq->prop) {
2706                                 IDP_WriteProperty(seq->prop, wd);
2707                         }
2708
2709                         write_sequence_modifiers(wd, &seq->modifiers);
2710                 }
2711                 SEQ_END
2712
2713                 /* new; meta stack too, even when its nasty restore code */
2714                 for (MetaStack *ms = ed->metastack.first; ms; ms = ms->next) {
2715                         writestruct(wd, DATA, MetaStack, 1, ms);
2716                 }
2717         }
2718
2719         if (sce->r.avicodecdata) {
2720                 writestruct(wd, DATA, AviCodecData, 1, sce->r.avicodecdata);
2721                 if (sce->r.avicodecdata->lpFormat) {
2722                         writedata(wd, DATA, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat);
2723                 }
2724                 if (sce->r.avicodecdata->lpParms) {
2725                         writedata(wd, DATA, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms);
2726                 }
2727         }
2728         if (sce->r.ffcodecdata.properties) {
2729                 IDP_WriteProperty(sce->r.ffcodecdata.properties, wd);
2730         }
2731
2732         /* writing dynamic list of TimeMarkers to the blend file */
2733         for (TimeMarker *marker = sce->markers.first; marker; marker = marker->next) {
2734                 writestruct(wd, DATA, TimeMarker, 1, marker);
2735         }
2736
2737         /* writing dynamic list of TransformOrientations to the blend file */
2738         for (TransformOrientation *ts = sce->transform_spaces.first; ts; ts = ts->next) {
2739                 writestruct(wd, DATA, TransformOrientation, 1, ts);
2740         }
2741
2742         for (SceneRenderLayer *srl = sce->r.layers.first; srl; srl = srl->next) {
2743                 writestruct(wd, DATA, SceneRenderLayer, 1, srl);
2744                 if (srl->prop) {
2745                         IDP_WriteProperty(srl->prop, wd);
2746                 }
2747                 for (FreestyleModuleConfig *fmc = srl->freestyleConfig.modules.first; fmc; fmc = fmc->next) {
2748                         writestruct(wd, DATA, FreestyleModuleConfig, 1, fmc);
2749                 }
2750                 for (FreestyleLineSet *fls = srl->freestyleConfig.linesets.first; fls; fls = fls->next) {
2751                         writestruct(wd, DATA, FreestyleLineSet, 1, fls);
2752                 }
2753         }
2754
2755         /* writing MultiView to the blend file */
2756         for (SceneRenderView *srv = sce->r.views.first; srv; srv = srv->next) {
2757                 writestruct(wd, DATA, SceneRenderView, 1, srv);
2758         }
2759
2760         if (sce->nodetree) {
2761                 writestruct(wd, DATA, bNodeTree, 1, sce->nodetree);
2762                 write_nodetree_nolib(wd, sce->nodetree);
2763         }
2764
2765         write_view_settings(wd, &sce->view_settings);
2766
2767         /* writing RigidBodyWorld data to the blend file */
2768         if (sce->rigidbody_world) {
2769                 writestruct(wd, DATA, RigidBodyWorld, 1, sce->rigidbody_world);
2770                 writestruct(wd, DATA, EffectorWeights, 1, sce->rigidbody_world->effector_weights);
2771                 write_pointcaches(wd, &(sce->rigidbody_world->ptcaches));
2772         }
2773
2774         write_previews(wd, sce->preview);
2775         write_curvemapping_curves(wd, &sce->r.mblur_shutter_curve);
2776 }
2777
2778 static void write_gpencil(WriteData *wd, bGPdata *gpd)
2779 {
2780         if (gpd->id.us > 0 || wd->use_memfile) {
2781                 /* write gpd data block to file */
2782                 writestruct(wd, ID_GD, bGPdata, 1, gpd);
2783                 write_iddata(wd, &gpd->id);
2784
2785                 if (gpd->adt) {
2786                         write_animdata(wd, gpd->adt);
2787                 }
2788
2789                 /* write grease-pencil layers to file */
2790                 writelist(wd, DATA, bGPDlayer, &gpd->layers);
2791                 for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
2792                         /* write this layer's frames to file */
2793                         writelist(wd, DATA, bGPDframe, &gpl->frames);
2794                         for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
2795                                 /* write strokes */
2796                                 writelist(wd, DATA, bGPDstroke, &gpf->strokes);
2797                                 for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
2798                                         writestruct(wd, DATA, bGPDspoint, gps->totpoints, gps->points);
2799                                 }
2800                         }
2801                 }
2802
2803                 /* write grease-pencil palettes */
2804                 writelist(wd, DATA, bGPDpalette, &gpd->palettes);
2805                 for (bGPDpalette *palette = gpd->palettes.first; palette; palette = palette->next) {
2806                         writelist(wd, DATA, bGPDpalettecolor, &palette->colors);
2807                 }
2808         }
2809 }
2810
2811 static void write_windowmanager(WriteData *wd, wmWindowManager *wm)
2812 {
2813         writestruct(wd, ID_WM, wmWindowManager, 1, wm);
2814         write_iddata(wd, &wm->id);
2815
2816         for (wmWindow *win = wm->windows.first; win; win = win->next) {
2817                 writestruct(wd, DATA, wmWindow, 1, win);
2818                 writestruct(wd, DATA, Stereo3dFormat, 1, win->stereo3d_format);
2819         }
2820 }
2821
2822 static void write_region(WriteData *wd, ARegion *ar, int spacetype)
2823 {
2824         writestruct(wd, DATA, ARegion, 1, ar);
2825
2826         if (ar->regiondata) {
2827                 switch (spacetype) {
2828                         case SPACE_VIEW3D:
2829                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
2830                                         RegionView3D *rv3d = ar->regiondata;
2831                                         writestruct(wd, DATA, RegionView3D, 1, rv3d);
2832
2833                                         if (rv3d->localvd) {
2834                                                 writestruct(wd, DATA, RegionView3D, 1, rv3d->localvd);
2835                                         }
2836                                         if (rv3d->clipbb) {
2837                                                 writestruct(wd, DATA, BoundBox, 1, rv3d->clipbb);
2838                                         }
2839
2840                                 }
2841                                 else
2842                                         printf("regiondata write missing!\n");
2843                                 break;
2844                         default:
2845                                 printf("regiondata write missing!\n");
2846                 }
2847         }
2848 }
2849
2850 static void write_uilist(WriteData *wd, uiList *ui_list)
2851 {
2852         writestruct(wd, DATA, uiList, 1, ui_list);
2853
2854         if (ui_list->properties) {
2855                 IDP_WriteProperty(ui_list->properties, wd);
2856         }
2857 }
2858
2859 static void write_soops(WriteData *wd, SpaceOops *so)
2860 {
2861         BLI_mempool *ts = so->treestore;
2862
2863         if (ts) {
2864                 SpaceOops so_flat = *so;
2865
2866                 int elems = BLI_mempool_len(ts);
2867                 /* linearize mempool to array */
2868                 TreeStoreElem *data = elems ? BLI_mempool_as_arrayN(ts, "TreeStoreElem") : NULL;
2869
2870                 if (data) {
2871                         /* In this block we use the memory location of the treestore
2872                          * but _not_ its data, the addresses in this case are UUID's,
2873                          * since we can't rely on malloc giving us different values each time.
2874                          */
2875                         TreeStore ts_flat = {0};
2876
2877                         /* we know the treestore is at least as big as a pointer,
2878                          * so offsetting works to give us a UUID. */
2879                         void *data_addr = (void *)POINTER_OFFSET(ts, sizeof(void *));
2880
2881                         ts_flat.usedelem = elems;
2882                         ts_flat.totelem = elems;
2883                         ts_flat.data = data_addr;
2884
2885                         writestruct(wd, DATA, SpaceOops, 1, so);
2886
2887                         writestruct_at_address(wd, DATA, TreeStore, 1, ts, &ts_flat);
2888                         writestruct_at_address(wd, DATA, TreeStoreElem, elems, data_addr, data);
2889
2890                         MEM_freeN(data);
2891                 }
2892                 else {
2893                         so_flat.treestore = NULL;
2894                         writestruct_at_address(wd, DATA, SpaceOops, 1, so, &so_flat);
2895                 }
2896         }
2897         else {
2898                 writestruct(wd, DATA, SpaceOops, 1, so);
2899         }
2900 }
2901
2902 static void write_screen(WriteData *wd, bScreen *sc)
2903 {
2904         /* write LibData */
2905         /* in 2.50+ files, the file identifier for screens is patched, forward compatibility */
2906         writestruct(wd, ID_SCRN, bScreen, 1, sc);
2907         write_iddata(wd, &sc->id);
2908
2909         /* direct data */
2910         for (ScrVert *sv = sc->vertbase.first; sv; sv = sv->next) {
2911                 writestruct(wd, DATA, ScrVert, 1, sv);
2912         }
2913
2914         for (ScrEdge *se = sc->edgebase.first; se; se = se->next) {
2915                 writestruct(wd, DATA, ScrEdge, 1, se);
2916         }
2917
2918         for (ScrArea *sa = sc->areabase.first; sa; sa = sa->next) {
2919                 SpaceLink *sl;
2920                 Panel *pa;
2921                 uiList *ui_list;
2922                 uiPreview *ui_preview;
2923                 PanelCategoryStack *pc_act;
2924                 ARegion *ar;
2925
2926                 writestruct(wd, DATA, ScrArea, 1, sa);
2927
2928                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
2929                         write_region(wd, ar, sa->spacetype);
2930
2931                         for (pa = ar->panels.first; pa; pa = pa->next) {
2932                                 writestruct(wd, DATA, Panel, 1, pa);
2933                         }
2934
2935                         for (pc_act = ar->panels_category_active.first; pc_act; pc_act = pc_act->next) {
2936                                 writestruct(wd, DATA, PanelCategoryStack, 1, pc_act);
2937                         }
2938
2939                         for (ui_list = ar->ui_lists.first; ui_list; ui_list = ui_list->next) {
2940                                 write_uilist(wd, ui_list);
2941                         }
2942
2943                         for (ui_preview = ar->ui_previews.first; ui_preview; ui_preview = ui_preview->next) {
2944                                 writestruct(wd, DATA, uiPreview, 1, ui_preview);
2945                         }
2946                 }
2947
2948                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
2949                         for (ar = sl->regionbase.first; ar; ar = ar->next) {
2950                                 write_region(wd, ar, sl->spacetype);
2951                         }
2952
2953                         if (sl->spacetype == SPACE_VIEW3D) {
2954                                 View3D *v3d = (View3D *)sl;
2955                                 BGpic *bgpic;
2956                                 writestruct(wd, DATA, View3D, 1, v3d);
2957                                 for (bgpic = v3d->bgpicbase.first; bgpic; bgpic = bgpic->next) {
2958                                         writestruct(wd, DATA, BGpic, 1, bgpic);
2959                                 }
2960                                 if (v3d->localvd) {
2961                                         writestruct(wd, DATA, View3D, 1, v3d->localvd);
2962                                 }
2963
2964                                 if (v3d->fx_settings.ssao) {
2965                                         writestruct(wd, DATA, GPUSSAOSettings, 1, v3d->fx_settings.ssao);
2966                                 }
2967                                 if (v3d->fx_settings.dof) {
2968                                         writestruct(wd, DATA, GPUDOFSettings, 1, v3d->fx_settings.dof);
2969                                 }
2970                         }
2971                         else if (sl->spacetype == SPACE_IPO) {
2972                                 SpaceIpo *sipo = (SpaceIpo *)sl;
2973                                 ListBase tmpGhosts = sipo->ghostCurves;
2974
2975                                 /* temporarily disable ghost curves when saving */
2976                                 sipo->ghostCurves.first = sipo->ghostCurves.last = NULL;
2977
2978                                 writestruct(wd, DATA, SpaceIpo, 1, sl);
2979                                 if (sipo->ads) {
2980                                         writestruct(wd, DATA, bDopeSheet, 1, sipo->ads);
2981                                 }
2982
2983                                 /* reenable ghost curves */
2984                                 sipo->ghostCurves = tmpGhosts;
2985                         }
2986                         else if (sl->spacetype == SPACE_BUTS) {
2987                                 writestruct(wd, DATA, SpaceButs, 1, sl);
2988                         }
2989                         else if (sl->spacetype == SPACE_FILE) {
2990                                 SpaceFile *sfile = (SpaceFile *)sl;
2991
2992                                 writestruct(wd, DATA, SpaceFile, 1, sl);
2993                                 if (sfile->params) {
2994                                         writestruct(wd, DATA, FileSelectParams, 1, sfile->params);
2995                                 }
2996                         }
2997                         else if (sl->spacetype == SPACE_SEQ) {
2998                                 writestruct(wd, DATA, SpaceSeq, 1, sl);
2999                         }
3000                         else if (sl->spacetype == SPACE_OUTLINER) {
3001                                 SpaceOops *so = (SpaceOops *)sl;
3002                                 write_soops(wd, so);
3003                         }
3004                         else if (sl->spacetype == SPACE_IMAGE) {
3005                                 writestruct(wd, DATA, SpaceImage, 1, sl);
3006                         }
3007                         else if (sl->spacetype == SPACE_TEXT) {
3008                                 writestruct(wd, DATA, SpaceText, 1, sl);
3009                         }
3010                         else if (sl->spacetype == SPACE_SCRIPT) {
3011                                 SpaceScript *scr = (SpaceScript *)sl;
3012                                 scr->but_refs = NULL;
3013                                 writestruct(wd, DATA, SpaceScript, 1, sl);
3014                         }
3015                         else if (sl->spacetype == SPACE_ACTION) {
3016                                 writestruct(wd, DATA, SpaceAction, 1, sl);
3017                         }
3018                         else if (sl->spacetype == SPACE_NLA) {
3019                                 SpaceNla *snla = (SpaceNla *)sl;
3020
3021                                 writestruct(wd, DATA, SpaceNla, 1, snla);
3022                                 if (snla->ads) {
3023                                         writestruct(wd, DATA, bDopeSheet, 1, snla->ads);
3024                                 }
3025                         }
3026                         else if (sl->spacetype == SPACE_TIME) {
3027                                 writestruct(wd, DATA, SpaceTime, 1, sl);
3028                         }
3029                         else if (sl->spacetype == SPACE_NODE) {
3030                                 SpaceNode *snode = (SpaceNode *)sl;
3031                                 bNodeTreePath *path;
3032                                 writestruct(wd, DATA, SpaceNode, 1, snode);
3033
3034                                 for (path = snode->treepath.first; path; path = path->next) {
3035                                         writestruct(wd, DATA, bNodeTreePath, 1, path);
3036                                 }
3037                         }
3038                         else if (sl->spacetype == SPACE_LOGIC) {
3039                                 writestruct(wd, DATA, SpaceLogic, 1, sl);
3040                         }
3041                         else if (sl->spacetype == SPACE_CONSOLE) {
3042                                 SpaceConsole *con = (SpaceConsole *)sl;
3043                                 ConsoleLine *cl;
3044
3045                                 for (cl = con->history.first; cl; cl = cl->next) {
3046                                         /* 'len_alloc' is invalid on write, set from 'len' on read */
3047                                         writestruct(wd, DATA, ConsoleLine, 1, cl);
3048                                         writedata(wd, DATA, cl->len + 1, cl->line);
3049                                 }
3050                                 writestruct(wd, DATA, SpaceConsole, 1, sl);
3051
3052                         }
3053                         else if (sl->spacet