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