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