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