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