2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/blenloader/intern/writefile.c
37 * IFF-style structure (but not IFF compatible!)
41 * BLENDER_V100 12 bytes (versie 1.00)
42 * V = big endian, v = little endian
43 * _ = 4 byte pointer, - = 8 byte pointer
46 * datablocks: (also see struct #BHead).
49 * <bh.len> int, len data after BHead
50 * <bh.old> void, old pointer
52 * <bh.nr> int, in case of array: number of structs
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 .
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
72 * - write associated direct data
73 * (External file data)
75 * - write library block
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``.
93 # include <zlib.h> /* odd include order-issue */
94 # include "winsock2.h"
96 # include "BLI_winstuff.h"
98 # include <unistd.h> /* FreeBSD, for write() and close(). */
101 #include "BLI_utildefines.h"
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
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"
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"
161 #include "BKE_action.h"
162 #include "BKE_blender_version.h"
163 #include "BKE_bpath.h"
164 #include "BKE_curve.h"
165 #include "BKE_constraint.h"
166 #include "BKE_global.h" // for G
167 #include "BKE_group.h"
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"
182 #ifdef USE_NODE_COMPAT_CUSTOMNODES
183 #include "NOD_socket.h" /* for sock->default_value data */
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"
193 #include "readfile.h"
195 /* for SDNA_TYPE_FROM_STRUCT() macro */
196 #include "dna_type_offsets.h"
200 /* ********* my write, buffered writing with minimum size chunks ************ */
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 */
206 /** Use if we want to store how many bytes have been written to the file. */
207 // #define USE_WRITE_DATA_LEN
209 /* -------------------------------------------------------------------- */
210 /** \name Internal Write Wrapper's (Abstracts Compression)
218 typedef struct WriteWrap WriteWrap;
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);
233 #define FILE_HANDLE(ww) \
234 (ww)->_user_data.file_handle
236 static bool ww_open_none(WriteWrap *ww, const char *filepath)
240 file = BLI_open(filepath, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
243 FILE_HANDLE(ww) = file;
250 static bool ww_close_none(WriteWrap *ww)
252 return (close(FILE_HANDLE(ww)) != -1);
254 static size_t ww_write_none(WriteWrap *ww, const char *buf, size_t buf_len)
256 return write(FILE_HANDLE(ww), buf, buf_len);
261 #define FILE_HANDLE(ww) \
262 (ww)->_user_data.gz_handle
264 static bool ww_open_zlib(WriteWrap *ww, const char *filepath)
268 file = BLI_gzopen(filepath, "wb1");
270 if (file != Z_NULL) {
271 FILE_HANDLE(ww) = file;
278 static bool ww_close_zlib(WriteWrap *ww)
280 return (gzclose(FILE_HANDLE(ww)) == Z_OK);
282 static size_t ww_write_zlib(WriteWrap *ww, const char *buf, size_t buf_len)
284 return gzwrite(FILE_HANDLE(ww), buf, buf_len);
288 /* --- end compression types --- */
290 static void ww_handle_init(eWriteWrapType ww_type, WriteWrap *r_ww)
292 memset(r_ww, 0, sizeof(*r_ww));
297 r_ww->open = ww_open_zlib;
298 r_ww->close = ww_close_zlib;
299 r_ww->write = ww_write_zlib;
304 r_ww->open = ww_open_none;
305 r_ww->close = ww_close_none;
306 r_ww->write = ww_write_none;
314 /* -------------------------------------------------------------------- */
315 /** \name Write Data Type & Functions
319 const struct SDNA *sdna;
321 /** Use for file and memory writing (fixed size of #MYWRITE_BUFFER_SIZE). */
323 /** Number of bytes used in #WriteData.buf (flushed when exceeded). */
326 #ifdef USE_WRITE_DATA_LEN
327 /** Total number of bytes written. */
331 /** Set on unlikely case of an error (ignores further file writing). */
334 /** #MemFile writing (used for undo). */
338 /** Use to de-duplicate chunks when writing. */
339 MemFileChunk *compare_chunk;
341 /** When true, write to #WriteData.current, could also call 'is_undo'. */
345 * Wrap writing, so we can use zlib or
346 * other compression types later, see: G_FILE_COMPRESS
347 * Will be NULL for UNDO.
351 #ifdef USE_BMESH_SAVE_AS_COMPAT
352 bool use_mesh_compat; /* option to save with older mesh format */
356 static WriteData *writedata_new(WriteWrap *ww)
358 WriteData *wd = MEM_callocN(sizeof(*wd), "writedata");
360 wd->sdna = DNA_sdna_current_get();
364 wd->buf = MEM_mallocN(MYWRITE_BUFFER_SIZE, "wd->buf");
369 static void writedata_do_write(WriteData *wd, const void *mem, int memlen)
371 if ((wd == NULL) || wd->error || (mem == NULL) || memlen < 1) {
375 if (UNLIKELY(wd->error)) {
379 /* memory based save */
380 if (wd->use_memfile) {
381 memfile_chunk_add(wd->mem.current, mem, memlen, &wd->mem.compare_chunk);
384 if (wd->ww->write(wd->ww, mem, memlen) != memlen) {
390 static void writedata_free(WriteData *wd)
398 /* -------------------------------------------------------------------- */
399 /** \name Local Writing API 'mywrite'
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.
406 static void mywrite_flush(WriteData *wd)
408 if (wd->buf_used_len) {
409 writedata_do_write(wd, wd->buf, wd->buf_used_len);
410 wd->buf_used_len = 0;
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
419 static void mywrite(WriteData *wd, const void *adr, int len)
421 if (UNLIKELY(wd->error)) {
425 if (UNLIKELY(adr == NULL)) {
430 #ifdef USE_WRITE_DATA_LEN
431 wd->write_len += len;
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;
443 int writelen = MIN2(len, MYWRITE_MAX_CHUNK);
444 writedata_do_write(wd, adr, writelen);
445 adr = (const char *)adr + writelen;
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;
458 /* append data at end of buffer */
459 memcpy(&wd->buf[wd->buf_used_len], adr, len);
460 wd->buf_used_len += len;
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
470 static WriteData *mywrite_begin(WriteWrap *ww, MemFile *compare, MemFile *current)
472 WriteData *wd = writedata_new(ww);
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;
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
490 static bool mywrite_end(WriteData *wd)
492 if (wd->buf_used_len) {
493 writedata_do_write(wd, wd->buf, wd->buf_used_len);
494 wd->buf_used_len = 0;
497 const bool err = wd->error;
505 /* -------------------------------------------------------------------- */
506 /** \name Generic DNA File Writing
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)
516 BLI_assert(struct_nr > 0 && struct_nr < SDNA_TYPE_MAX);
518 if (adr == NULL || data == NULL || nr == 0) {
527 bh.SDNAnr = struct_nr;
528 sp = wd->sdna->structs[bh.SDNAnr];
530 bh.len = nr * wd->sdna->typelens[sp[0]];
536 mywrite(wd, &bh, sizeof(BHead));
537 mywrite(wd, data, bh.len);
540 static void writestruct_at_address_id(
541 WriteData *wd, int filecode, const char *structname, int nr,
542 const void *adr, const void *data)
544 if (adr == NULL || data == NULL || nr == 0) {
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);
554 writestruct_at_address_nr(wd, filecode, SDNAnr, nr, adr, data);
557 static void writestruct_nr(
558 WriteData *wd, int filecode, const int struct_nr, int nr,
561 writestruct_at_address_nr(wd, filecode, struct_nr, nr, adr, adr);
564 static void writestruct_id(
565 WriteData *wd, int filecode, const char *structname, int nr,
568 writestruct_at_address_id(wd, filecode, structname, nr, adr, adr);
571 static void writedata(WriteData *wd, int filecode, int len, const void *adr) /* do not use for structs */
575 if (adr == NULL || len == 0) {
579 /* align to 4 (writes uninitialized bytes in some cases) */
580 len = (len + 3) & ~3;
589 mywrite(wd, &bh, sizeof(BHead));
590 mywrite(wd, adr, len);
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)
596 const Link *link = lb->first;
599 writestruct_nr(wd, filecode, struct_nr, 1, link);
605 static void writelist_id(WriteData *wd, int filecode, const char *structname, const ListBase *lb)
607 const Link *link = lb->first;
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);
617 writestruct_nr(wd, filecode, struct_nr, 1, link);
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)
627 #define writestruct(wd, filecode, struct_id, nr, adr) \
628 writestruct_nr(wd, filecode, SDNA_TYPE_FROM_STRUCT(struct_id), nr, adr)
630 #define writelist(wd, filecode, struct_id, lb) \
631 writelist_nr(wd, filecode, SDNA_TYPE_FROM_STRUCT(struct_id), lb)
635 /* -------------------------------------------------------------------- */
636 /** \name Typed DNA File Writing
638 * These functions are used by blender's .blend system for file saving/loading.
641 void IDP_WriteProperty_OnlyData(const IDProperty *prop, void *wd);
642 void IDP_WriteProperty(const IDProperty *prop, void *wd);
644 static void IDP_WriteArray(const IDProperty *prop, void *wd)
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);
650 if (prop->subtype == IDP_GROUP) {
651 IDProperty **array = prop->data.pointer;
654 for (a = 0; a < prop->len; a++) {
655 IDP_WriteProperty(array[a], wd);
661 static void IDP_WriteIDPArray(const IDProperty *prop, void *wd)
663 /*REMEMBER to set totalen to len in the linking code!!*/
664 if (prop->data.pointer) {
665 const IDProperty *array = prop->data.pointer;
668 writestruct(wd, DATA, IDProperty, prop->len, array);
670 for (a = 0; a < prop->len; a++) {
671 IDP_WriteProperty_OnlyData(&array[a], wd);
676 static void IDP_WriteString(const IDProperty *prop, void *wd)
678 /*REMEMBER to set totalen to len in the linking code!!*/
679 writedata(wd, DATA, prop->len, prop->data.pointer);
682 static void IDP_WriteGroup(const IDProperty *prop, void *wd)
686 for (loop = prop->data.group.first; loop; loop = loop->next) {
687 IDP_WriteProperty(loop, wd);
691 /* Functions to read/write ID Properties */
692 void IDP_WriteProperty_OnlyData(const IDProperty *prop, void *wd)
694 switch (prop->type) {
696 IDP_WriteGroup(prop, wd);
699 IDP_WriteString(prop, wd);
702 IDP_WriteArray(prop, wd);
705 IDP_WriteIDPArray(prop, wd);
710 void IDP_WriteProperty(const IDProperty *prop, void *wd)
712 writestruct(wd, DATA, IDProperty, 1, prop);
713 IDP_WriteProperty_OnlyData(prop, wd);
716 static void write_iddata(void *wd, const ID *id)
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);
723 if (id->override_static) {
724 writestruct(wd, DATA, IDOverrideStatic, 1, id->override_static);
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);
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);
735 if (opop->subitem_local_name) {
736 writedata(wd, DATA, strlen(opop->subitem_local_name) + 1, opop->subitem_local_name);
743 static void write_previews(WriteData *wd, const PreviewImage *prv_orig)
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. */
749 PreviewImage prv = *prv_orig;
751 /* don't write out large previews if not requested */
752 if (!(U.flag & USER_SAVE_PREVIEWS)) {
757 writestruct_at_address(wd, DATA, PreviewImage, 1, prv_orig, &prv);
759 writedata(wd, DATA, prv.w[0] * prv.h[0] * sizeof(unsigned int), prv.rect[0]);
762 writedata(wd, DATA, prv.w[1] * prv.h[1] * sizeof(unsigned int), prv.rect[1]);
767 static void write_fmodifiers(WriteData *wd, ListBase *fmodifiers)
771 /* Write all modifiers first (for faster reloading) */
772 writelist(wd, DATA, FModifier, fmodifiers);
775 for (fcm = fmodifiers->first; fcm; fcm = fcm->next) {
776 const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(fcm);
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);
783 /* do any modifier specific stuff */
785 case FMODIFIER_TYPE_GENERATOR:
787 FMod_Generator *data = fcm->data;
789 /* write coefficients array */
790 if (data->coefficients) {
791 writedata(wd, DATA, sizeof(float) * (data->arraysize), data->coefficients);
796 case FMODIFIER_TYPE_ENVELOPE:
798 FMod_Envelope *data = fcm->data;
800 /* write envelope data */
802 writestruct(wd, DATA, FCM_EnvelopeData, data->totvert, data->data);
807 case FMODIFIER_TYPE_PYTHON:
809 FMod_Python *data = fcm->data;
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);
822 static void write_fcurves(WriteData *wd, ListBase *fcurves)
826 writelist(wd, DATA, FCurve, fcurves);
827 for (fcu = fcurves->first; fcu; fcu = fcu->next) {
830 writestruct(wd, DATA, BezTriple, fcu->totvert, fcu->bezt);
833 writestruct(wd, DATA, FPoint, fcu->totvert, fcu->fpt);
837 writedata(wd, DATA, strlen(fcu->rna_path) + 1, fcu->rna_path);
842 ChannelDriver *driver = fcu->driver;
845 writestruct(wd, DATA, ChannelDriver, 1, driver);
848 writelist(wd, DATA, DriverVar, &driver->variables);
849 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
850 DRIVER_TARGETS_USED_LOOPER(dvar)
852 if (dtar->rna_path) {
853 writedata(wd, DATA, strlen(dtar->rna_path) + 1, dtar->rna_path);
856 DRIVER_TARGETS_LOOPER_END
860 /* write F-Modifiers */
861 write_fmodifiers(wd, &fcu->modifiers);
865 static void write_action(WriteData *wd, bAction *act)
867 if (act->id.us > 0 || wd->use_memfile) {
868 writestruct(wd, ID_AC, bAction, 1, act);
869 write_iddata(wd, &act->id);
871 write_fcurves(wd, &act->curves);
873 for (bActionGroup *grp = act->groups.first; grp; grp = grp->next) {
874 writestruct(wd, DATA, bActionGroup, 1, grp);
877 for (TimeMarker *marker = act->markers.first; marker; marker = marker->next) {
878 writestruct(wd, DATA, TimeMarker, 1, marker);
883 static void write_keyingsets(WriteData *wd, ListBase *list)
888 for (ks = list->first; ks; ks = ks->next) {
890 writestruct(wd, DATA, KeyingSet, 1, ks);
893 for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
895 writestruct(wd, DATA, KS_Path, 1, ksp);
898 writedata(wd, DATA, strlen(ksp->rna_path) + 1, ksp->rna_path);
904 static void write_nlastrips(WriteData *wd, ListBase *strips)
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);
914 /* write the strip's children */
915 write_nlastrips(wd, &strip->strips);
919 static void write_nladata(WriteData *wd, ListBase *nlabase)
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);
928 /* write the track's strips */
929 write_nlastrips(wd, &nlt->strips);
933 static void write_animdata(WriteData *wd, AnimData *adt)
937 /* firstly, just write the AnimData block */
938 writestruct(wd, DATA, AnimData, 1, adt);
941 write_fcurves(wd, &adt->drivers);
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);
951 // TODO write the remaps (if they are needed)
954 write_nladata(wd, &adt->nla_tracks);
957 static void write_curvemapping_curves(WriteData *wd, CurveMapping *cumap)
959 for (int a = 0; a < CM_TOT; a++) {
960 writestruct(wd, DATA, CurveMapPoint, cumap->cm[a].totpoint, cumap->cm[a].curve);
964 static void write_curvemapping(WriteData *wd, CurveMapping *cumap)
966 writestruct(wd, DATA, CurveMapping, 1, cumap);
968 write_curvemapping_curves(wd, cumap);
971 static void write_node_socket(WriteData *wd, bNodeTree *UNUSED(ntree), bNode *node, bNodeSocket *sock)
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;
978 if (node->type == NODE_GROUP) {
979 bNodeTree *ngroup = (bNodeTree *)node->id;
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);
985 /* node group sockets now use the generic identifier string to verify group nodes,
986 * old blender uses the own_index.
988 sock->own_index = sock->groupsock->own_index;
994 /* actual socket writing */
995 writestruct(wd, DATA, bNodeSocket, 1, sock);
998 IDP_WriteProperty(sock->prop, wd);
1001 if (sock->default_value) {
1002 writedata(wd, DATA, MEM_allocN_len(sock->default_value), sock->default_value);
1005 static void write_node_socket_interface(WriteData *wd, bNodeTree *UNUSED(ntree), bNodeSocket *sock)
1007 #ifdef USE_NODE_COMPAT_CUSTOMNODES
1008 /* forward compatibility code, so older blenders still open */
1009 sock->stack_type = 1;
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);
1017 /* actual socket writing */
1018 writestruct(wd, DATA, bNodeSocket, 1, sock);
1021 IDP_WriteProperty(sock->prop, wd);
1024 if (sock->default_value) {
1025 writedata(wd, DATA, MEM_allocN_len(sock->default_value), sock->default_value);
1028 /* this is only direct data, tree itself should have been written */
1029 static void write_nodetree_nolib(WriteData *wd, bNodeTree *ntree)
1035 /* for link_list() speed, we write per list */
1038 write_animdata(wd, ntree->adt);
1041 for (node = ntree->nodes.first; node; node = node->next) {
1042 writestruct(wd, DATA, bNode, 1, node);
1045 IDP_WriteProperty(node->prop, wd);
1048 for (sock = node->inputs.first; sock; sock = sock->next) {
1049 write_node_socket(wd, ntree, node, sock);
1051 for (sock = node->outputs.first; sock; sock = sock->next) {
1052 write_node_socket(wd, ntree, node, sock);
1055 for (link = node->internal_links.first; link; link = link->next) {
1056 writestruct(wd, DATA, bNodeLink, 1, link);
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))
1064 write_curvemapping(wd, node->storage);
1066 else if (ntree->type == NTREE_SHADER &&
1067 (node->type == SH_NODE_SCRIPT))
1069 NodeShaderScript *nss = (NodeShaderScript *)node->storage;
1070 if (nss->bytecode) {
1071 writedata(wd, DATA, strlen(nss->bytecode) + 1, nss->bytecode);
1073 writestruct_id(wd, DATA, node->typeinfo->storagename, 1, node->storage);
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))
1078 write_curvemapping(wd, node->storage);
1080 else if ((ntree->type == NTREE_TEXTURE) &&
1081 (node->type == TEX_NODE_CURVE_RGB || node->type == TEX_NODE_CURVE_TIME))
1083 write_curvemapping(wd, node->storage);
1085 else if ((ntree->type == NTREE_COMPOSIT) &&
1086 (node->type == CMP_NODE_MOVIEDISTORTION))
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;
1101 ndg->angle = ndg->star_45;
1107 writestruct_id(wd, DATA, node->typeinfo->storagename, 1, node->storage);
1110 writestruct_id(wd, DATA, node->typeinfo->storagename, 1, node->storage);
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);
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);
1128 for (link = ntree->links.first; link; link = link->next) {
1129 writestruct(wd, DATA, bNodeLink, 1, link);
1132 for (sock = ntree->inputs.first; sock; sock = sock->next) {
1133 write_node_socket_interface(wd, ntree, sock);
1135 for (sock = ntree->outputs.first; sock; sock = sock->next) {
1136 write_node_socket_interface(wd, ntree, sock);
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).
1144 static void current_screen_compat(
1145 Main *mainvar, bool use_active_win,
1146 bScreen **r_screen, Scene **r_scene, ViewLayer **r_render_layer)
1148 wmWindowManager *wm;
1149 wmWindow *window = NULL;
1150 WorkSpace *workspace;
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;
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) {
1166 if (window == NULL) {
1167 window = wm->windows.first;
1171 window = wm->windows.first;
1174 workspace = (window) ? BKE_workspace_active_get(window->workspace_hook) : NULL;
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;
1181 typedef struct RenderInfo {
1184 char scene_name[MAX_ID_NAME - 2];
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)
1193 Scene *sce, *curscene = NULL;
1194 ViewLayer *render_layer;
1197 /* XXX in future, handle multiple windows with multiple screens? */
1198 current_screen_compat(mainvar, false, &curscreen, &curscene, &render_layer);
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));
1206 BLI_strncpy(data.scene_name, sce->id.name + 2, sizeof(data.scene_name));
1208 writedata(wd, REND, sizeof(data), &data);
1213 static void write_keymapitem(WriteData *wd, const wmKeyMapItem *kmi)
1215 writestruct(wd, DATA, wmKeyMapItem, 1, kmi);
1216 if (kmi->properties) {
1217 IDP_WriteProperty(kmi->properties, wd);
1221 static void write_userdef(WriteData *wd, const UserDef *userdef)
1223 writestruct(wd, USER, UserDef, 1, userdef);
1225 for (const bTheme *btheme = userdef->themes.first; btheme; btheme = btheme->next) {
1226 writestruct(wd, DATA, bTheme, 1, btheme);
1229 for (const wmKeyMap *keymap = userdef->user_keymaps.first; keymap; keymap = keymap->next) {
1230 writestruct(wd, DATA, wmKeyMap, 1, keymap);
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);
1237 if (kmdi->add_item) {
1238 write_keymapitem(wd, kmdi->add_item);
1242 for (const wmKeyMapItem *kmi = keymap->items.first; kmi; kmi = kmi->next) {
1243 write_keymapitem(wd, kmi);
1247 for (const bAddon *bext = userdef->addons.first; bext; bext = bext->next) {
1248 writestruct(wd, DATA, bAddon, 1, bext);
1250 IDP_WriteProperty(bext->prop, wd);
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);
1258 for (const uiStyle *style = userdef->uistyles.first; style; style = style->next) {
1259 writestruct(wd, DATA, uiStyle, 1, style);
1263 static void write_boid_state(WriteData *wd, BoidState *state)
1265 BoidRule *rule = state->rules.first;
1267 writestruct(wd, DATA, BoidState, 1, state);
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);
1275 case eBoidRuleType_AvoidCollision:
1276 writestruct(wd, DATA, BoidRuleAvoidCollision, 1, rule);
1278 case eBoidRuleType_FollowLeader:
1279 writestruct(wd, DATA, BoidRuleFollowLeader, 1, rule);
1281 case eBoidRuleType_AverageSpeed:
1282 writestruct(wd, DATA, BoidRuleAverageSpeed, 1, rule);
1284 case eBoidRuleType_Fight:
1285 writestruct(wd, DATA, BoidRuleFight, 1, rule);
1288 writestruct(wd, DATA, BoidRule, 1, rule);
1293 BoidCondition *cond = state->conditions.first;
1294 for (; cond; cond = cond->next) {
1295 writestruct(wd, DATA, BoidCondition, 1, cond);
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:
1311 static const char *ptcache_extra_struct[] = {
1315 static void write_pointcaches(WriteData *wd, ListBase *ptcaches)
1317 PointCache *cache = ptcaches->first;
1320 for (; cache; cache = cache->next) {
1321 writestruct(wd, DATA, PointCache, 1, cache);
1323 if ((cache->flag & PTCACHE_DISK_CACHE) == 0) {
1324 PTCacheMem *pm = cache->mem_cache.first;
1326 for (; pm; pm = pm->next) {
1327 PTCacheExtra *extra = pm->extradata.first;
1329 writestruct(wd, DATA, PTCacheMem, 1, pm);
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]);
1337 writestruct_id(wd, DATA, ptcache_data_struct[i], pm->totpoint, pm->data[i]);
1342 for (; extra; extra = extra->next) {
1343 if (ptcache_extra_struct[extra->type][0] == '\0') {
1346 writestruct(wd, DATA, PTCacheExtra, 1, extra);
1347 writestruct_id(wd, DATA, ptcache_extra_struct[extra->type], extra->totdata, extra->data);
1354 static void write_particlesettings(WriteData *wd, ParticleSettings *part)
1356 if (part->id.us > 0 || wd->use_memfile) {
1358 writestruct(wd, ID_PA, ParticleSettings, 1, part);
1359 write_iddata(wd, &part->id);
1362 write_animdata(wd, part->adt);
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);
1368 if (part->clumpcurve) {
1369 write_curvemapping(wd, part->clumpcurve);
1371 if (part->roughcurve) {
1372 write_curvemapping(wd, part->roughcurve);
1374 if (part->twistcurve) {
1375 write_curvemapping(wd, part->twistcurve);
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) {
1382 if (part->dup_group) { /* can be NULL if lining fails or set to None */
1383 FOREACH_GROUP_OBJECT_BEGIN(part->dup_group, object)
1385 if (object != dw->ob) {
1389 FOREACH_GROUP_OBJECT_END;
1392 writestruct(wd, DATA, ParticleDupliWeight, 1, dw);
1395 if (part->boids && part->phystype == PART_PHYS_BOIDS) {
1396 writestruct(wd, DATA, BoidSettings, 1, part->boids);
1398 for (BoidState *state = part->boids->states.first; state; state = state->next) {
1399 write_boid_state(wd, state);
1402 if (part->fluid && part->phystype == PART_PHYS_FLUID) {
1403 writestruct(wd, DATA, SPHFluidSettings, 1, part->fluid);
1406 for (int a = 0; a < MAX_MTEX; a++) {
1407 if (part->mtex[a]) {
1408 writestruct(wd, DATA, MTex, 1, part->mtex[a]);
1414 static void write_particlesystems(WriteData *wd, ListBase *particles)
1416 ParticleSystem *psys = particles->first;
1420 for (; psys; psys = psys->next) {
1421 writestruct(wd, DATA, ParticleSystem, 1, psys);
1423 if (psys->particles) {
1424 writestruct(wd, DATA, ParticleData, psys->totpart, psys->particles);
1426 if (psys->particles->hair) {
1427 ParticleData *pa = psys->particles;
1429 for (a = 0; a < psys->totpart; a++, pa++) {
1430 writestruct(wd, DATA, HairKey, pa->totkey, pa->hair);
1434 if (psys->particles->boid &&
1435 (psys->part->phystype == PART_PHYS_BOIDS))
1437 writestruct(wd, DATA, BoidParticle, psys->totpart, psys->particles->boid);
1440 if (psys->part->fluid &&
1441 (psys->part->phystype == PART_PHYS_FLUID) &&
1442 (psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS))
1444 writestruct(wd, DATA, ParticleSpring, psys->tot_fluidsprings, psys->fluid_springs);
1447 pt = psys->targets.first;
1448 for (; pt; pt = pt->next) {
1449 writestruct(wd, DATA, ParticleTarget, 1, pt);
1453 writestruct(wd, DATA, ChildParticle, psys->totchild, psys->child);
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);
1462 write_pointcaches(wd, &psys->ptcaches);
1466 static void write_motionpath(WriteData *wd, bMotionPath *mpath)
1469 if (mpath == NULL) {
1473 /* firstly, just write the motionpath struct */
1474 writestruct(wd, DATA, bMotionPath, 1, mpath);
1476 /* now write the array of data */
1477 writestruct(wd, DATA, bMotionPathVert, mpath->length, mpath->points);
1480 static void write_constraints(WriteData *wd, ListBase *conlist)
1484 for (con = conlist->first; con; con = con->next) {
1485 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
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);
1492 /* do any constraint specific stuff */
1493 switch (con->type) {
1494 case CONSTRAINT_TYPE_PYTHON:
1496 bPythonConstraint *data = con->data;
1497 bConstraintTarget *ct;
1500 for (ct = data->targets.first; ct; ct = ct->next) {
1501 writestruct(wd, DATA, bConstraintTarget, 1, ct);
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);
1510 case CONSTRAINT_TYPE_SPLINEIK:
1512 bSplineIKConstraint *data = con->data;
1514 /* write points array */
1515 writedata(wd, DATA, sizeof(float) * (data->numpoints), data->points);
1522 /* Write the constraint */
1523 writestruct(wd, DATA, bConstraint, 1, con);
1527 static void write_pose(WriteData *wd, bPose *pose)
1532 /* Write each channel */
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.*/
1542 IDP_WriteProperty(chan->prop, wd);
1545 write_constraints(wd, &chan->constraints);
1547 write_motionpath(wd, chan->mpath);
1549 /* prevent crashes with autosave,
1550 * when a bone duplicated in editmode has not yet been assigned to its posechannel */
1552 /* gets restored on read, for library armatures */
1553 chan->selectflag = chan->bone->flag & BONE_SELECTED;
1556 writestruct(wd, DATA, bPoseChannel, 1, chan);
1560 for (grp = pose->agroups.first; grp; grp = grp->next) {
1561 writestruct(wd, DATA, bActionGroup, 1, grp);
1564 /* write IK param */
1565 if (pose->ikparam) {
1566 const char *structname = BKE_pose_ikparam_get_name(pose);
1568 writestruct_id(wd, DATA, structname, 1, pose->ikparam);
1572 /* Write this pose */
1573 writestruct(wd, DATA, bPose, 1, pose);
1577 static void write_defgroups(WriteData *wd, ListBase *defbase)
1579 for (bDeformGroup *defgroup = defbase->first; defgroup; defgroup = defgroup->next) {
1580 writestruct(wd, DATA, bDeformGroup, 1, defgroup);
1584 static void write_fmaps(WriteData *wd, ListBase *fbase)
1586 for (bFaceMap *fmap = fbase->first; fmap; fmap = fmap->next) {
1587 writestruct(wd, DATA, bFaceMap, 1, fmap);
1591 static void write_modifiers(WriteData *wd, ListBase *modbase)
1595 if (modbase == NULL) {
1599 for (md = modbase->first; md; md = md->next) {
1600 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1605 writestruct_id(wd, DATA, mti->structName, 1, md);
1607 if (md->type == eModifierType_Hook) {
1608 HookModifierData *hmd = (HookModifierData *)md;
1610 if (hmd->curfalloff) {
1611 write_curvemapping(wd, hmd->curfalloff);
1614 writedata(wd, DATA, sizeof(int) * hmd->totindex, hmd->indexar);
1616 else if (md->type == eModifierType_Cloth) {
1617 ClothModifierData *clmd = (ClothModifierData *)md;
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);
1624 else if (md->type == eModifierType_Smoke) {
1625 SmokeModifierData *smd = (SmokeModifierData *)md;
1627 if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
1628 writestruct(wd, DATA, SmokeDomainSettings, 1, smd->domain);
1631 write_pointcaches(wd, &(smd->domain->ptcaches[0]));
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;
1638 write_pointcaches(wd, &(smd->domain->ptcaches[1]));
1640 if (smd->domain->coba) {
1641 writestruct(wd, DATA, ColorBand, 1, smd->domain->coba);
1645 /* cleanup the fake pointcache */
1646 BKE_ptcache_free_list(&smd->domain->ptcaches[1]);
1647 smd->domain->point_cache[1] = NULL;
1649 writestruct(wd, DATA, EffectorWeights, 1, smd->domain->effector_weights);
1652 else if (smd->type & MOD_SMOKE_TYPE_FLOW) {
1653 writestruct(wd, DATA, SmokeFlowSettings, 1, smd->flow);
1655 else if (smd->type & MOD_SMOKE_TYPE_COLL) {
1656 writestruct(wd, DATA, SmokeCollSettings, 1, smd->coll);
1659 else if (md->type == eModifierType_Fluidsim) {
1660 FluidsimModifierData *fluidmd = (FluidsimModifierData *)md;
1662 writestruct(wd, DATA, FluidsimSettings, 1, fluidmd->fss);
1664 else if (md->type == eModifierType_DynamicPaint) {
1665 DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
1668 DynamicPaintSurface *surface;
1669 writestruct(wd, DATA, DynamicPaintCanvasSettings, 1, pmd->canvas);
1671 /* write surfaces */
1672 for (surface = pmd->canvas->surfaces.first; surface; surface = surface->next) {
1673 writestruct(wd, DATA, DynamicPaintSurface, 1, surface);
1675 /* write caches and effector weights */
1676 for (surface = pmd->canvas->surfaces.first; surface; surface = surface->next) {
1677 write_pointcaches(wd, &(surface->ptcaches));
1679 writestruct(wd, DATA, EffectorWeights, 1, surface->effector_weights);
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);
1688 else if (md->type == eModifierType_Collision) {
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);
1699 else if (md->type == eModifierType_MeshDeform) {
1700 MeshDeformModifierData *mmd = (MeshDeformModifierData *)md;
1701 int size = mmd->dyngridsize;
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,
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);
1711 else if (md->type == eModifierType_Warp) {
1712 WarpModifierData *tmd = (WarpModifierData *)md;
1713 if (tmd->curfalloff) {
1714 write_curvemapping(wd, tmd->curfalloff);
1717 else if (md->type == eModifierType_WeightVGEdit) {
1718 WeightVGEditModifierData *wmd = (WeightVGEditModifierData *)md;
1720 if (wmd->cmap_curve) {
1721 write_curvemapping(wd, wmd->cmap_curve);
1724 else if (md->type == eModifierType_LaplacianDeform) {
1725 LaplacianDeformModifierData *lmd = (LaplacianDeformModifierData *)md;
1727 writedata(wd, DATA, sizeof(float) * lmd->total_verts * 3, lmd->vertexco);
1729 else if (md->type == eModifierType_CorrectiveSmooth) {
1730 CorrectiveSmoothModifierData *csmd = (CorrectiveSmoothModifierData *)md;
1732 if (csmd->bind_coords) {
1733 writedata(wd, DATA, sizeof(float[3]) * csmd->bind_coords_num, csmd->bind_coords);
1736 else if (md->type == eModifierType_SurfaceDeform) {
1737 SurfaceDeformModifierData *smd = (SurfaceDeformModifierData *)md;
1739 writestruct(wd, DATA, SDefVert, smd->numverts, smd->verts);
1742 for (int i = 0; i < smd->numverts; i++) {
1743 writestruct(wd, DATA, SDefBind, smd->verts[i].numbinds, smd->verts[i].binds);
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);
1749 if (smd->verts[i].binds[j].mode == MOD_SDEF_MODE_CENTROID ||
1750 smd->verts[i].binds[j].mode == MOD_SDEF_MODE_LOOPTRI)
1752 writedata(wd, DATA, sizeof(float) * 3, smd->verts[i].binds[j].vert_weights);
1755 writedata(wd, DATA, sizeof(float) * smd->verts[i].binds[j].numverts, smd->verts[i].binds[j].vert_weights);
1765 static void write_object(WriteData *wd, Object *ob)
1767 if (ob->id.us > 0 || wd->use_memfile) {
1769 writestruct(wd, ID_OB, Object, 1, ob);
1770 write_iddata(wd, &ob->id);
1773 write_animdata(wd, ob->adt);
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 */
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));
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);
1794 writestruct(wd, DATA, PartDeflect, 1, ob->pd);
1795 writestruct(wd, DATA, SoftBody, 1, ob->soft);
1797 write_pointcaches(wd, &ob->soft->ptcaches);
1798 writestruct(wd, DATA, EffectorWeights, 1, ob->soft->effector_weights);
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);
1805 if (ob->rigidbody_constraint) {
1806 writestruct(wd, DATA, RigidBodyCon, 1, ob->rigidbody_constraint);
1809 if (ob->type == OB_EMPTY && ob->empty_drawtype == OB_EMPTY_IMAGE) {
1810 writestruct(wd, DATA, ImageUser, 1, ob->iuser);
1813 write_particlesystems(wd, &ob->particlesystem);
1814 write_modifiers(wd, &ob->modifiers);
1816 writelist(wd, DATA, LinkData, &ob->pc_ids);
1817 writelist(wd, DATA, LodLevel, &ob->lodlevels);
1819 write_previews(wd, ob->preview);
1824 static void write_vfont(WriteData *wd, VFont *vf)
1826 if (vf->id.us > 0 || wd->use_memfile) {
1828 writestruct(wd, ID_VF, VFont, 1, vf);
1829 write_iddata(wd, &vf->id);
1832 if (vf->packedfile) {
1833 PackedFile *pf = vf->packedfile;
1834 writestruct(wd, DATA, PackedFile, 1, pf);
1835 writedata(wd, DATA, pf->size, pf->data);
1841 static void write_key(WriteData *wd, Key *key)
1843 if (key->id.us > 0 || wd->use_memfile) {
1845 writestruct(wd, ID_KE, Key, 1, key);
1846 write_iddata(wd, &key->id);
1849 write_animdata(wd, key->adt);
1853 for (KeyBlock *kb = key->block.first; kb; kb = kb->next) {
1854 writestruct(wd, DATA, KeyBlock, 1, kb);
1856 writedata(wd, DATA, kb->totelem * key->elemsize, kb->data);
1862 static void write_camera(WriteData *wd, Camera *cam)
1864 if (cam->id.us > 0 || wd->use_memfile) {
1866 writestruct(wd, ID_CA, Camera, 1, cam);
1867 write_iddata(wd, &cam->id);
1870 write_animdata(wd, cam->adt);
1873 for (CameraBGImage *bgpic = cam->bg_images.first; bgpic; bgpic = bgpic->next) {
1874 writestruct(wd, DATA, CameraBGImage, 1, bgpic);
1879 static void write_mball(WriteData *wd, MetaBall *mb)
1881 if (mb->id.us > 0 || wd->use_memfile) {
1883 writestruct(wd, ID_MB, MetaBall, 1, mb);
1884 write_iddata(wd, &mb->id);
1887 writedata(wd, DATA, sizeof(void *) * mb->totcol, mb->mat);
1889 write_animdata(wd, mb->adt);
1892 for (MetaElem *ml = mb->elems.first; ml; ml = ml->next) {
1893 writestruct(wd, DATA, MetaElem, 1, ml);
1898 static void write_curve(WriteData *wd, Curve *cu)
1900 if (cu->id.us > 0 || wd->use_memfile) {
1902 writestruct(wd, ID_CU, Curve, 1, cu);
1903 write_iddata(wd, &cu->id);
1906 writedata(wd, DATA, sizeof(void *) * cu->totcol, cu->mat);
1908 write_animdata(wd, cu->adt);
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);
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);
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);
1926 writestruct(wd, DATA, BPoint, nu->pntsu * nu->pntsv, nu->bp);
1928 writedata(wd, DATA, KNOTSU(nu) * sizeof(float), nu->knotsu);
1931 writedata(wd, DATA, KNOTSV(nu) * sizeof(float), nu->knotsv);
1939 static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
1943 /* Write the dvert list */
1944 writestruct(wd, DATA, MDeformVert, count, dvlist);
1946 /* Write deformation data for each dvert */
1947 for (int i = 0; i < count; i++) {
1949 writestruct(wd, DATA, MDeformWeight, dvlist[i].totweight, dvlist[i].dw);
1955 static void write_mdisps(WriteData *wd, int count, MDisps *mdlist, int external)
1960 writestruct(wd, DATA, MDisps, count, mdlist);
1961 for (i = 0; i < count; ++i) {
1962 MDisps *md = &mdlist[i];
1965 writedata(wd, DATA, sizeof(float) * 3 * md->totdisp, md->disps);
1970 writedata(wd, DATA, BLI_BITMAP_SIZE(md->totdisp), md->hidden);
1976 static void write_grid_paint_mask(WriteData *wd, int count, GridPaintMask *grid_paint_mask)
1978 if (grid_paint_mask) {
1981 writestruct(wd, DATA, GridPaintMask, count, grid_paint_mask);
1982 for (i = 0; i < count; ++i) {
1983 GridPaintMask *gpm = &grid_paint_mask[i];
1985 const int gridsize = BKE_ccg_gridsize(gpm->level);
1987 sizeof(*gpm->data) * gridsize * gridsize,
1994 static void write_customdata(
1995 WriteData *wd, ID *id, int count, CustomData *data, CustomDataLayer *layers,
1996 int partial_type, int partial_count)
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);
2005 writestruct_at_address(wd, DATA, CustomDataLayer, data->totlayer, data->layers, layers);
2007 for (i = 0; i < data->totlayer; i++) {
2008 CustomDataLayer *layer = &layers[i];
2009 const char *structname;
2010 int structnum, datasize;
2012 if (layer->type == CD_MDEFORMVERT) {
2013 /* layer types that allocate own memory need special handling */
2014 write_dverts(wd, count, layer->data);
2016 else if (layer->type == CD_MDISPS) {
2017 write_mdisps(wd, count, layer->data, layer->flag & CD_FLAG_EXTERNAL);
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);
2023 else if (layer->type == CD_GRID_PAINT_MASK) {
2024 write_grid_paint_mask(wd, count, layer->data);
2026 else if (layer->type == CD_FACEMAP) {
2027 const int *layer_data = layer->data;
2028 writedata(wd, DATA, sizeof(*layer_data) * count, layer_data);
2031 CustomData_file_write_info(layer->type, &structname, &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;
2040 datasize = structnum * partial_count;
2043 writestruct_id(wd, DATA, structname, datasize, layer->data);
2046 printf("%s error: layer '%s':%d - can't be written to file\n",
2047 __func__, structname, layer->type);
2052 if (data->external) {
2053 writestruct(wd, DATA, CustomDataExternal, 1, data->external);
2057 static void write_mesh(WriteData *wd, Mesh *mesh)
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 */
2062 const bool save_for_old_blender = false;
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];
2071 if (mesh->id.us > 0 || wd->use_memfile) {
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;
2080 #ifdef USE_BMESH_SAVE_WITHOUT_MFACE
2081 /* cache only - don't write */
2084 memset(&mesh->fdata, 0, sizeof(mesh->fdata));
2085 #endif /* USE_BMESH_SAVE_WITHOUT_MFACE */
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!
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));
2099 flayers = flayers_buff;
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));
2104 writestruct_at_address(wd, ID_ME, Mesh, 1, old_mesh, mesh);
2105 write_iddata(wd, &mesh->id);
2109 write_animdata(wd, mesh->adt);
2112 writedata(wd, DATA, sizeof(void *) * mesh->totcol, mesh->mat);
2113 writedata(wd, DATA, sizeof(MSelect) * mesh->totselect, mesh->mselect);
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);
2122 /* restore pointer */
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;
2139 CustomData_reset(&mesh->fdata);
2140 CustomData_reset(&mesh->pdata);
2141 CustomData_reset(&mesh->ldata);
2142 mesh->edit_btmesh = NULL;
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?
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);
2155 BKE_mesh_update_customdata_pointers(mesh, false);
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));
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));
2165 writestruct_at_address(wd, ID_ME, Mesh, 1, old_mesh, mesh);
2166 write_iddata(wd, &mesh->id);
2170 write_animdata(wd, mesh->adt);
2173 writedata(wd, DATA, sizeof(void *) * mesh->totcol, mesh->mat);
2174 /* writedata(wd, DATA, sizeof(MSelect) * mesh->totselect, mesh->mselect); */ /* pre-bmesh NULL's */
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 */
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);
2185 CustomData_free(&mesh->fdata, mesh->totface);
2188 /* restore pointer */
2190 #endif /* USE_BMESH_SAVE_AS_COMPAT */
2194 if (vlayers && vlayers != vlayers_buff) {
2197 if (elayers && elayers != elayers_buff) {
2200 if (flayers && flayers != flayers_buff) {
2203 if (llayers && llayers != llayers_buff) {
2206 if (players && players != players_buff) {
2211 static void write_lattice(WriteData *wd, Lattice *lt)
2213 if (lt->id.us > 0 || wd->use_memfile) {
2215 writestruct(wd, ID_LT, Lattice, 1, lt);
2216 write_iddata(wd, <->id);
2218 /* write animdata */
2220 write_animdata(wd, lt->adt);
2224 writestruct(wd, DATA, BPoint, lt->pntsu * lt->pntsv * lt->pntsw, lt->def);
2226 write_dverts(wd, lt->pntsu * lt->pntsv * lt->pntsw, lt->dvert);
2230 static void write_image(WriteData *wd, Image *ima)
2232 if (ima->id.us > 0 || wd->use_memfile) {
2233 ImagePackedFile *imapf;
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;
2243 writestruct(wd, ID_IM, Image, 1, ima);
2244 write_iddata(wd, &ima->id);
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);
2255 write_previews(wd, ima->preview);
2257 for (ImageView *iv = ima->views.first; iv; iv = iv->next) {
2258 writestruct(wd, DATA, ImageView, 1, iv);
2260 writestruct(wd, DATA, Stereo3dFormat, 1, ima->stereo3d_format);
2262 ima->packedfile = NULL;
2266 static void write_texture(WriteData *wd, Tex *tex)
2268 if (tex->id.us > 0 || wd->use_memfile) {
2270 writestruct(wd, ID_TE, Tex, 1, tex);
2271 write_iddata(wd, &tex->id);
2274 write_animdata(wd, tex->adt);
2279 writestruct(wd, DATA, ColorBand, 1, tex->coba);
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);
2288 write_previews(wd, tex->preview);
2292 static void write_material(WriteData *wd, Material *ma)
2294 if (ma->id.us > 0 || wd->use_memfile) {
2296 writestruct(wd, ID_MA, Material, 1, ma);
2297 write_iddata(wd, &ma->id);
2300 write_animdata(wd, ma->adt);
2303 /* nodetree is integral part of material, no libdata */
2305 writestruct(wd, DATA, bNodeTree, 1, ma->nodetree);
2306 write_nodetree_nolib(wd, ma->nodetree);
2309 write_previews(wd, ma->preview);
2313 static void write_world(WriteData *wd, World *wrld)
2315 if (wrld->id.us > 0 || wd->use_memfile) {
2317 writestruct(wd, ID_WO, World, 1, wrld);
2318 write_iddata(wd, &wrld->id);
2321 write_animdata(wd, wrld->adt);
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);
2330 write_previews(wd, wrld->preview);
2334 static void write_lamp(WriteData *wd, Lamp *la)
2336 if (la->id.us > 0 || wd->use_memfile) {
2338 writestruct(wd, ID_LA, Lamp, 1, la);
2339 write_iddata(wd, &la->id);
2342 write_animdata(wd, la->adt);
2345 if (la->curfalloff) {
2346 write_curvemapping(wd, la->curfalloff);
2349 /* nodetree is integral part of lamps, no libdata */
2351 writestruct(wd, DATA, bNodeTree, 1, la->nodetree);
2352 write_nodetree_nolib(wd, la->nodetree);
2355 write_previews(wd, la->preview);
2359 static void write_sequence_modifiers(WriteData *wd, ListBase *modbase)
2361 SequenceModifierData *smd;
2363 for (smd = modbase->first; smd; smd = smd->next) {
2364 const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
2367 writestruct_id(wd, DATA, smti->struct_name, 1, smd);
2369 if (smd->type == seqModifierType_Curves) {
2370 CurvesModifierData *cmd = (CurvesModifierData *)smd;
2372 write_curvemapping(wd, &cmd->curve_mapping);
2374 else if (smd->type == seqModifierType_HueCorrect) {
2375 HueCorrectModifierData *hcmd = (HueCorrectModifierData *)smd;
2377 write_curvemapping(wd, &hcmd->curve_mapping);
2381 writestruct(wd, DATA, SequenceModifierData, 1, smd);
2386 static void write_view_settings(WriteData *wd, ColorManagedViewSettings *view_settings)
2388 if (view_settings->curve_mapping) {
2389 write_curvemapping(wd, view_settings->curve_mapping);
2393 static void write_paint(WriteData *wd, Paint *p)
2395 if (p->cavity_curve) {
2396 write_curvemapping(wd, p->cavity_curve);
2400 static void write_scene_collection(WriteData *wd, SceneCollection *sc)
2402 writestruct(wd, DATA, SceneCollection, 1, sc);
2404 writelist(wd, DATA, LinkData, &sc->objects);
2406 for (SceneCollection *nsc = sc->scene_collections.first; nsc; nsc = nsc->next) {
2407 write_scene_collection(wd, nsc);
2411 static void write_layer_collections(WriteData *wd, ListBase *lb)
2413 for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
2414 writestruct(wd, DATA, LayerCollection, 1, lc);
2416 writelist(wd, DATA, LinkData, &lc->object_bases);
2418 write_layer_collections(wd, &lc->layer_collections);
2422 static void write_view_layer(WriteData *wd, ViewLayer *view_layer)
2424 writestruct(wd, DATA, ViewLayer, 1, view_layer);
2425 writelist(wd, DATA, Base, &view_layer->object_bases);
2427 if (view_layer->id_properties) {
2428 IDP_WriteProperty(view_layer->id_properties, wd);
2431 for (FreestyleModuleConfig *fmc = view_layer->freestyle_config.modules.first; fmc; fmc = fmc->next) {
2432 writestruct(wd, DATA, FreestyleModuleConfig, 1, fmc);
2435 for (FreestyleLineSet *fls = view_layer->freestyle_config.linesets.first; fls; fls = fls->next) {
2436 writestruct(wd, DATA, FreestyleLineSet, 1, fls);
2438 write_layer_collections(wd, &view_layer->layer_collections);
2441 static void write_scene(WriteData *wd, Scene *sce)
2444 writestruct(wd, ID_SCE, Scene, 1, sce);
2445 write_iddata(wd, &sce->id);
2448 write_animdata(wd, sce->adt);
2450 write_keyingsets(wd, &sce->keyingsets);
2453 ToolSettings *tos = sce->toolsettings;
2454 writestruct(wd, DATA, ToolSettings, 1, tos);
2456 writestruct(wd, DATA, VPaint, 1, tos->vpaint);
2457 write_paint(wd, &tos->vpaint->paint);
2460 writestruct(wd, DATA, VPaint, 1, tos->wpaint);
2461 write_paint(wd, &tos->wpaint->paint);
2464 writestruct(wd, DATA, Sculpt, 1, tos->sculpt);
2465 write_paint(wd, &tos->sculpt->paint);
2467 if (tos->uvsculpt) {
2468 writestruct(wd, DATA, UvSculpt, 1, tos->uvsculpt);
2469 write_paint(wd, &tos->uvsculpt->paint);
2471 /* write grease-pencil drawing brushes to file */
2472 writelist(wd, DATA, bGPDbrush, &tos->gp_brushes);
2473 for (bGPDbrush *brush = tos->gp_brushes.first; brush; brush = brush->next) {
2474 if (brush->cur_sensitivity) {
2475 write_curvemapping(wd, brush->cur_sensitivity);
2477 if (brush->cur_strength) {
2478 write_curvemapping(wd, brush->cur_strength);
2480 if (brush->cur_jitter) {
2481 write_curvemapping(wd, brush->cur_jitter);
2484 /* write grease-pencil custom ipo curve to file */
2485 if (tos->gp_interpolate.custom_ipo) {
2486 write_curvemapping(wd, tos->gp_interpolate.custom_ipo);
2490 write_paint(wd, &tos->imapaint.paint);
2492 Editing *ed = sce->ed;
2496 writestruct(wd, DATA, Editing, 1, ed);
2498 /* reset write flags too */
2503 seq->strip->done = false;
2505 writestruct(wd, DATA, Sequence, 1, seq);
2511 if (seq->strip && seq->strip->done == 0) {
2512 /* write strip with 'done' at 0 because readfile */
2514 if (seq->effectdata) {
2515 switch (seq->type) {
2516 case SEQ_TYPE_COLOR:
2517 writestruct(wd, DATA, SolidColorVars, 1, seq->effectdata);
2519 case SEQ_TYPE_SPEED:
2520 writestruct(wd, DATA, SpeedControlVars, 1, seq->effectdata);
2523 writestruct(wd, DATA, WipeVars, 1, seq->effectdata);
2526 writestruct(wd, DATA, GlowVars, 1, seq->effectdata);
2528 case SEQ_TYPE_TRANSFORM:
2529 writestruct(wd, DATA, TransformVars, 1, seq->effectdata);
2531 case SEQ_TYPE_GAUSSIAN_BLUR:
2532 writestruct(wd, DATA, GaussianBlurVars, 1, seq->effectdata);
2535 writestruct(wd, DATA, TextVars, 1, seq->effectdata);
2537 case SEQ_TYPE_COLORMIX:
2538 writestruct(wd, DATA, ColorMixVars, 1, seq->effectdata);
2543 writestruct(wd, DATA, Stereo3dFormat, 1, seq->stereo3d_format);
2545 Strip *strip = seq->strip;
2546 writestruct(wd, DATA, Strip, 1, strip);
2547 if (seq->flag & SEQ_USE_CROP && strip->crop) {
2548 writestruct(wd, DATA, StripCrop, 1, strip->crop);
2550 if (seq->flag & SEQ_USE_TRANSFORM && strip->transform) {
2551 writestruct(wd, DATA, StripTransform, 1, strip->transform);
2553 if (seq->flag & SEQ_USE_PROXY && strip->proxy) {
2554 writestruct(wd, DATA, StripProxy, 1, strip->proxy);
2556 if (seq->type == SEQ_TYPE_IMAGE) {
2557 writestruct(wd, DATA, StripElem,
2558 MEM_allocN_len(strip->stripdata) / sizeof(struct StripElem),
2561 else if (ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SOUND_HD)) {
2562 writestruct(wd, DATA, StripElem, 1, strip->stripdata);
2569 IDP_WriteProperty(seq->prop, wd);
2572 write_sequence_modifiers(wd, &seq->modifiers);
2576 /* new; meta stack too, even when its nasty restore code */
2577 for (MetaStack *ms = ed->metastack.first; ms; ms = ms->next) {
2578 writestruct(wd, DATA, MetaStack, 1, ms);
2582 if (sce->r.avicodecdata) {
2583 writestruct(wd, DATA, AviCodecData, 1, sce->r.avicodecdata);
2584 if (sce->r.avicodecdata->lpFormat) {
2585 writedata(wd, DATA, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat);
2587 if (sce->r.avicodecdata->lpParms) {
2588 writedata(wd, DATA, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms);
2591 if (sce->r.ffcodecdata.properties) {
2592 IDP_WriteProperty(sce->r.ffcodecdata.properties, wd);
2595 /* writing dynamic list of TimeMarkers to the blend file */
2596 for (TimeMarker *marker = sce->markers.first; marker; marker = marker->next) {
2597 writestruct(wd, DATA, TimeMarker, 1, marker);
2600 /* writing dynamic list of TransformOrientations to the blend file */
2601 for (TransformOrientation *ts = sce->transform_spaces.first; ts; ts = ts->next) {
2602 writestruct(wd, DATA, TransformOrientation, 1, ts);
2605 /* writing MultiView to the blend file */
2606 for (SceneRenderView *srv = sce->r.views.first; srv; srv = srv->next) {
2607 writestruct(wd, DATA, SceneRenderView, 1, srv);
2610 if (sce->nodetree) {
2611 writestruct(wd, DATA, bNodeTree, 1, sce->nodetree);
2612 write_nodetree_nolib(wd, sce->nodetree);
2615 write_view_settings(wd, &sce->view_settings);
2617 /* writing RigidBodyWorld data to the blend file */
2618 if (sce->rigidbody_world) {
2619 writestruct(wd, DATA, RigidBodyWorld, 1, sce->rigidbody_world);
2620 writestruct(wd, DATA, EffectorWeights, 1, sce->rigidbody_world->effector_weights);
2621 write_pointcaches(wd, &(sce->rigidbody_world->ptcaches));
2624 write_previews(wd, sce->preview);
2625 write_curvemapping_curves(wd, &sce->r.mblur_shutter_curve);
2626 write_scene_collection(wd, sce->collection);
2628 for (ViewLayer *view_layer = sce->view_layers.first; view_layer; view_layer = view_layer->next) {
2629 write_view_layer(wd, view_layer);
2632 /* Freed on doversion. */
2633 BLI_assert(sce->layer_properties == NULL);
2636 static void write_gpencil(WriteData *wd, bGPdata *gpd)
2638 if (gpd->id.us > 0 || wd->use_memfile) {
2639 /* write gpd data block to file */
2640 writestruct(wd, ID_GD, bGPdata, 1, gpd);
2641 write_iddata(wd, &gpd->id);
2644 write_animdata(wd, gpd->adt);
2647 /* write grease-pencil layers to file */
2648 writelist(wd, DATA, bGPDlayer, &gpd->layers);
2649 for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
2650 /* write this layer's frames to file */
2651 writelist(wd, DATA, bGPDframe, &gpl->frames);
2652 for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
2654 writelist(wd, DATA, bGPDstroke, &gpf->strokes);
2655 for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
2656 writestruct(wd, DATA, bGPDspoint, gps->totpoints, gps->points);
2661 /* write grease-pencil palettes */
2662 writelist(wd, DATA, bGPDpalette, &gpd->palettes);
2663 for (bGPDpalette *palette = gpd->palettes.first; palette; palette = palette->next) {
2664 writelist(wd, DATA, bGPDpalettecolor, &palette->colors);
2669 static void write_region(WriteData *wd, ARegion *ar, int spacetype)
2671 writestruct(wd, DATA, ARegion, 1, ar);
2673 if (ar->regiondata) {
2674 switch (spacetype) {
2676 if (ar->regiontype == RGN_TYPE_WINDOW) {
2677 RegionView3D *rv3d = ar->regiondata;
2678 writestruct(wd, DATA, RegionView3D, 1, rv3d);
2680 if (rv3d->localvd) {
2681 writestruct(wd, DATA, RegionView3D, 1, rv3d->localvd);
2684 writestruct(wd, DATA, BoundBox, 1, rv3d->clipbb);
2689 printf("regiondata write missing!\n");
2692 printf("regiondata write missing!\n");
2697 static void write_uilist(WriteData *wd, uiList *ui_list)
2699 writestruct(wd, DATA, uiList, 1, ui_list);
2701 if (ui_list->properties) {
2702 IDP_WriteProperty(ui_list->properties, wd);
2706 static void write_soops(WriteData *wd, SpaceOops *so)
2708 BLI_mempool *ts = so->treestore;
2711 SpaceOops so_flat = *so;
2713 int elems = BLI_mempool_len(ts);
2714 /* linearize mempool to array */
2715 TreeStoreElem *data = elems ? BLI_mempool_as_arrayN(ts, "TreeStoreElem") : NULL;
2718 /* In this block we use the memory location of the treestore
2719 * but _not_ its data, the addresses in this case are UUID's,
2720 * since we can't rely on malloc giving us different values each time.
2722 TreeStore ts_flat = {0};
2724 /* we know the treestore is at least as big as a pointer,
2725 * so offsetting works to give us a UUID. */
2726 void *data_addr = (void *)POINTER_OFFSET(ts, sizeof(void *));
2728 ts_flat.usedelem = elems;
2729 ts_flat.totelem = elems;
2730 ts_flat.data = data_addr;
2732 writestruct(wd, DATA, SpaceOops, 1, so);
2734 writestruct_at_address(wd, DATA, TreeStore, 1, ts, &ts_flat);
2735 writestruct_at_address(wd, DATA, TreeStoreElem, elems, data_addr, data);
2740 so_flat.treestore = NULL;
2741 writestruct_at_address(wd, DATA, SpaceOops, 1, so, &so_flat);
2745 writestruct(wd, DATA, SpaceOops, 1, so);
2749 static void write_area_regions(WriteData *wd, ScrArea *area)
2751 for (ARegion *region = area->regionbase.first; region; region = region->next) {
2752 write_region(wd, region, area->spacetype);
2754 for (Panel *pa = region->panels.first; pa; pa = pa->next) {
2755 writestruct(wd, DATA, Panel, 1, pa);
2758 for (PanelCategoryStack *pc_act = region->panels_category_active.first; pc_act; pc_act = pc_act->next) {
2759 writestruct(wd, DATA, PanelCategoryStack, 1, pc_act);
2762 for (uiList *ui_list = region->ui_lists.first; ui_list; ui_list = ui_list->next) {
2763 write_uilist(wd, ui_list);
2766 for (uiPreview *ui_preview = region->ui_previews.first; ui_preview; ui_preview = ui_preview->next) {
2767 writestruct(wd, DATA, uiPreview, 1, ui_preview);
2771 for (SpaceLink *sl = area->spacedata.first; sl; sl = sl->next) {
2772 for (ARegion *region = sl->regionbase.first; region; region = region->next) {
2773 write_region(wd, region, sl->spacetype);
2776 if (sl->spacetype == SPACE_VIEW3D) {
2777 View3D *v3d = (View3D *)sl;
2778 writestruct(wd, DATA, View3D, 1, v3d);
2781 writestruct(wd, DATA, View3D, 1, v3d->localvd);
2784 if (v3d->fx_settings.ssao) {
2785 writestruct(wd, DATA, GPUSSAOSettings, 1, v3d->fx_settings.ssao);
2787 if (v3d->fx_settings.dof) {
2788 writestruct(wd, DATA, GPUDOFSettings, 1, v3d->fx_settings.dof);
2791 else if (sl->spacetype == SPACE_IPO) {
2792 SpaceIpo *sipo = (SpaceIpo *)sl;
2793 ListBase tmpGhosts = sipo->ghostCurves;
2795 /* temporarily disable ghost curves when saving */
2796 sipo->ghostCurves.first = sipo->ghostCurves.last = NULL;
2798 writestruct(wd, DATA, SpaceIpo, 1, sl);
2800 writestruct(wd, DATA, bDopeSheet, 1, sipo->ads);
2803 /* reenable ghost curves */
2804 sipo->ghostCurves = tmpGhosts;
2806 else if (sl->spacetype == SPACE_BUTS) {
2807 writestruct(wd, DATA, SpaceButs, 1, sl);
2809 else if (sl->spacetype == SPACE_FILE) {
2810 SpaceFile *sfile = (SpaceFile *)sl;
2812 writestruct(wd, DATA, SpaceFile, 1, sl);
2813 if (sfile->params) {
2814 writestruct(wd, DATA, FileSelectParams, 1, sfile->params);
2817 else if (sl->spacetype == SPACE_SEQ) {
2818 writestruct(wd, DATA, SpaceSeq, 1, sl);
2820 else if (sl->spacetype == SPACE_OUTLINER) {
2821 SpaceOops *so = (SpaceOops *)sl;
2822 write_soops(wd, so);
2824 else if (sl->spacetype == SPACE_IMAGE) {
2825 writestruct(wd, DATA, SpaceImage, 1, sl);
2827 else if (sl->spacetype == SPACE_TEXT) {
2828 writestruct(wd, DATA, SpaceText, 1, sl);
2830 else if (sl->spacetype == SPACE_SCRIPT) {
2831 SpaceScript *scr = (SpaceScript *)sl;
2832 scr->but_refs = NULL;
2833 writestruct(wd, DATA, SpaceScript, 1, sl);
2835 else if (sl->spacetype == SPACE_ACTION) {
2836 writestruct(wd, DATA, SpaceAction, 1, sl);
2838 else if (sl->spacetype == SPACE_NLA) {
2839 SpaceNla *snla = (SpaceNla *)sl;
2841 writestruct(wd, DATA, SpaceNla, 1, snla);
2843 writestruct(wd, DATA, bDopeSheet, 1, snla->ads);
2846 else if (sl->spacetype == SPACE_NODE) {
2847 SpaceNode *snode = (SpaceNode *)sl;
2848 bNodeTreePath *path;
2849 writestruct(wd, DATA, SpaceNode, 1, snode);
2851 for (path = snode->treepath.first; path; path = path->next) {
2852 writestruct(wd, DATA, bNodeTreePath, 1, path);
2855 else if (sl->spacetype == SPACE_CONSOLE) {
2856 SpaceConsole *con = (SpaceConsole *)sl;
2859 for (cl = con->history.first; cl; cl = cl->next) {
2860 /* 'len_alloc' is invalid on write, set from 'len' on read */
2861 writestruct(wd, DATA, ConsoleLine, 1, cl);
2862 writedata(wd, DATA, cl->len + 1, cl->line);
2864 writestruct(wd, DATA, SpaceConsole, 1, sl);
2866 #ifdef WITH_TOPBAR_WRITING
2867 else if (sl->spacetype == SPACE_TOPBAR) {
2868 writestruct(wd, DATA, SpaceTopBar, 1, sl);
2871 else if (sl->spacetype == SPACE_USERPREF) {
2872 writestruct(wd, DATA, SpaceUserPref, 1, sl);
2874 else if (sl->spacetype == SPACE_CLIP) {
2875 writestruct(wd, DATA, SpaceClip, 1, sl);
2877 else if (sl->spacetype == SPACE_INFO) {
2878 writestruct(wd, DATA, SpaceInfo, 1, sl);
2883 static void write_area_map(WriteData *wd, ScrAreaMap *area_map)
2885 writelist(wd, DATA, ScrVert, &area_map->vertbase);
2886 writelist(wd, DATA, ScrEdge, &area_map->edgebase);
2887 for (ScrArea *area = area_map->areabase.first; area; area = area->next) {
2888 area->butspacetype = area->spacetype; /* Just for compatibility, will be reset below. */
2890 writestruct(wd, DATA, ScrArea, 1, area);
2892 #ifdef WITH_TOPBAR_WRITING
2893 writestruct(wd, DATA, ScrGlobalAreaData, 1, area->global);
2896 write_area_regions(wd, area);
2898 area->butspacetype = SPACE_EMPTY; /* Unset again, was changed above. */
2902 static void write_windowmanager(WriteData *wd, wmWindowManager *wm)
2904 writestruct(wd, ID_WM, wmWindowManager, 1, wm);
2905 write_iddata(wd, &wm->id);
2907 for (wmWindow *win = wm->windows.first; win; win = win->next) {
2908 #ifndef WITH_TOPBAR_WRITING
2909 /* Don't write global areas yet, while we make changes to them. */
2910 ScrAreaMap global_areas = win->global_areas;
2911 memset(&win->global_areas, 0, sizeof(win->global_areas));
2914 /* update deprecated screen member (for so loading in 2.7x uses the correct screen) */
2915 win->screen = BKE_workspace_active_screen_get(win->workspace_hook);
2917 writestruct(wd, DATA, wmWindow, 1, win);
2918 writestruct(wd, DATA, WorkSpaceInstanceHook, 1, win->workspace_hook);
2919 writestruct(wd, DATA, Stereo3dFormat, 1, win->stereo3d_format);
2921 #ifdef WITH_TOPBAR_WRITING
2922 write_area_map(wd, &win->global_areas);
2924 win->global_areas = global_areas;
2927 /* data is written, clear deprecated data again */
2932 static void write_screen(WriteData *wd, bScreen *sc)
2935 /* in 2.50+ files, the file identifier for screens is patched, forward compatibility */
2936 writestruct(wd, ID_SCRN, bScreen, 1, sc);
2937 write_iddata(wd, &sc->id);
2939 write_previews(wd, sc->preview);
2942 write_area_map(wd, AREAMAP_FROM_SCREEN(sc));
2945 static void write_bone(WriteData *wd, Bone *bone)
2947 /* PATCH for upward compatibility after 2.37+ armature recode */
2948 bone->size[0] = bone->size[1] = bone->size[2] = 1.0f;
2950 /* Write this bone */
2951 writestruct(wd, DATA, Bone, 1, bone);
2953 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
2954 * of library blocks that implement this.*/
2956 IDP_WriteProperty(bone->prop, wd);
2959 /* Write Children */
2960 for (Bone *cbone = bone->childbase.first; cbone; cbone = cbone->next) {
2961 write_bone(wd, cbone);
2965 static void write_armature(WriteData *wd, bArmature *arm)
2967 if (arm->id.us > 0 || wd->use_memfile) {
2968 writestruct(wd, ID_AR, bArmature, 1, arm);
2969 write_iddata(wd, &arm->id);
2972 write_animdata(wd, arm->adt);
2976 for (Bone *bone = arm->bonebase.first; bone; bone = bone->next) {
2977 write_bone(wd, bone);
2982 static void write_text(WriteData *wd, Text *text)
2984 if ((text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) {
2985 text->flags &= ~TXT_ISEXT;
2989 writestruct(wd, ID_TXT, Text, 1, text);
2990 write_iddata(wd, &text->id);
2993 writedata(wd, DATA, strlen(text->name) + 1, text->name);
2996 if (!(text->flags & TXT_ISEXT)) {
2997 /* now write the text data, in two steps for optimization in the readfunction */
2998 for (TextLine *tmp = text->lines.first; tmp; tmp = tmp->next) {
2999 writestruct(wd, DATA, TextLine, 1, tmp);
3002 for (TextLine *tmp = text->lines.first; tmp; tmp = tmp->next) {
3003 writedata(wd, DATA, tmp->len + 1, tmp->line);
3008 static void write_speaker(WriteData *wd, Speaker *spk)
3010 if (spk->id.us > 0 || wd->use_memfile) {
3012 writestruct(wd, ID_SPK, Speaker, 1, spk);
3013 write_iddata(wd, &spk->id);
3016 write_animdata(wd, spk->adt);
3021 static void write_sound(WriteData *wd, bSound *sound)
3023 if (sound->id.us > 0 || wd->use_memfile) {
3025 writestruct(wd, ID_SO, bSound, 1, sound);
3026 write_iddata(wd, &sound->id);
3028 if (sound->packedfile) {
3029 PackedFile *pf = sound->packedfile;
3030 writestruct(wd, DATA, PackedFile, 1, pf);
3031 writedata(wd, DATA, pf->size, pf->data);
3036 static void write_probe(WriteData *wd, LightProbe *prb)
3038 if (prb->id.us > 0 || wd->use_memfile) {
3040 writestruct(wd, ID_LP, LightProbe, 1, prb);
3041 write_iddata(wd, &prb->id);
3044 write_animdata(wd, prb->adt);
3049 static void write_group(WriteData *wd, Group *group)
3051 if (group->id.us > 0 || wd->use_memfile) {
3053 writestruct(wd, ID_GR, Group, 1, group);
3054 write_iddata(wd, &group->id);
3056 write_previews(wd, group->preview);
3057 write_scene_collection(wd, group->collection);
3058 write_view_layer(wd, group->view_layer);
3062 static void write_nodetree(WriteData *wd, bNodeTree *ntree)
3064 if (ntree->id.us > 0 || wd->use_memfile) {
3065 writestruct(wd, ID_NT, bNodeTree, 1, ntree);
3066 /* Note that trees directly used by other IDs (materials etc.) are not 'real' ID, they cannot
3067 * be linked, etc., so we write actual id data here only, for 'real' ID trees. */
3068 write_iddata(wd, &ntree->id);
3070 write_nodetree_nolib(wd, ntree);
3074 #ifdef USE_NODE_COMPAT_CUSTOMNODES
3075 static void customnodes_add_deprecated_data(Main *mainvar)
3077 FOREACH_NODETREE(mainvar, ntree, id) {
3078 bNodeLink *link, *last_link = ntree->links.last;
3080 /* only do this for node groups */
3081 if (id != &ntree->id) {
3085 /* Forward compatibility for group nodes: add links to node tree interface sockets.
3086 * These links are invalid by new rules (missing node pointer)!
3087 * They will be removed again in customnodes_free_deprecated_data,
3088 * cannot do this directly lest bNodeLink pointer mapping becomes ambiguous.
3089 * When loading files with such links in a new Blender version
3090 * they will be removed as well.
3092 for (link = ntree->links.first; link; link = link->next) {
3093 bNode *fromnode = link->fromnode, *tonode = link->tonode;
3094 bNodeSocket *fromsock = link->fromsock, *tosock = link->tosock;
3096 /* check both sides of the link, to handle direct input-to-output links */
3097 if (fromnode->type == NODE_GROUP_INPUT) {
3099 fromsock = ntreeFindSocketInterface(ntree, SOCK_IN, fromsock->identifier);
3101 /* only the active output node defines links */
3102 if (tonode->type == NODE_GROUP_OUTPUT && (tonode->flag & NODE_DO_OUTPUT)) {
3104 tosock = ntreeFindSocketInterface(ntree, SOCK_OUT, tosock->identifier);
3107 if (!fromnode || !tonode) {