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
33 * FILEFORMAT: IFF-style structure (but not IFF compatible!)
36 * BLENDER_V100 12 bytes (versie 1.00)
37 * V = big endian, v = little endian
38 * _ = 4 byte pointer, - = 8 byte pointer
40 * datablocks: also see struct BHead
42 * <bh.len> int, len data after BHead
43 * <bh.old> void, old pointer
45 * <bh.nr> int, in case of array: amount of structs
50 * Almost all data in Blender are structures. Each struct saved
51 * gets a BHead header. With BHead the struct can be linked again
52 * and compared with StructDNA .
56 * Preferred writing order: (not really a must, but why would you do it random?)
57 * Any case: direct data is ALWAYS after the lib block
62 * - write associated direct data
63 * (External file data)
65 * - write library block
67 * - write the ID of LibBlock
68 * - write TEST (128x128, blend file preview, optional)
69 * - write FileGlobal (some global vars)
71 * - write USER if filename is ~/X.XX/config/startup.blend
83 # include <zlib.h> /* odd include order-issue */
84 # include "winsock2.h"
86 # include "BLI_winstuff.h"
88 # include <unistd.h> /* FreeBSD, for write() and close(). */
91 #include "BLI_utildefines.h"
93 /* allow writefile to use deprecated functionality (for forward compatibility code) */
94 #define DNA_DEPRECATED_ALLOW
96 #include "DNA_anim_types.h"
97 #include "DNA_armature_types.h"
98 #include "DNA_actuator_types.h"
99 #include "DNA_brush_types.h"
100 #include "DNA_camera_types.h"
101 #include "DNA_cloth_types.h"
102 #include "DNA_constraint_types.h"
103 #include "DNA_controller_types.h"
104 #include "DNA_dynamicpaint_types.h"
105 #include "DNA_genfile.h"
106 #include "DNA_group_types.h"
107 #include "DNA_gpencil_types.h"
108 #include "DNA_fileglobal_types.h"
109 #include "DNA_key_types.h"
110 #include "DNA_lattice_types.h"
111 #include "DNA_lamp_types.h"
112 #include "DNA_linestyle_types.h"
113 #include "DNA_meta_types.h"
114 #include "DNA_mesh_types.h"
115 #include "DNA_meshdata_types.h"
116 #include "DNA_material_types.h"
117 #include "DNA_node_types.h"
118 #include "DNA_object_types.h"
119 #include "DNA_object_force.h"
120 #include "DNA_packedFile_types.h"
121 #include "DNA_particle_types.h"
122 #include "DNA_property_types.h"
123 #include "DNA_rigidbody_types.h"
124 #include "DNA_scene_types.h"
125 #include "DNA_sdna_types.h"
126 #include "DNA_sequence_types.h"
127 #include "DNA_sensor_types.h"
128 #include "DNA_smoke_types.h"
129 #include "DNA_space_types.h"
130 #include "DNA_screen_types.h"
131 #include "DNA_speaker_types.h"
132 #include "DNA_sound_types.h"
133 #include "DNA_text_types.h"
134 #include "DNA_view3d_types.h"
135 #include "DNA_vfont_types.h"
136 #include "DNA_world_types.h"
137 #include "DNA_windowmanager_types.h"
138 #include "DNA_movieclip_types.h"
139 #include "DNA_mask_types.h"
141 #include "MEM_guardedalloc.h" // MEM_freeN
142 #include "BLI_bitmap.h"
143 #include "BLI_blenlib.h"
144 #include "BLI_linklist.h"
145 #include "BLI_mempool.h"
147 #include "BKE_action.h"
148 #include "BKE_blender.h"
149 #include "BKE_bpath.h"
150 #include "BKE_curve.h"
151 #include "BKE_constraint.h"
152 #include "BKE_global.h" // for G
153 #include "BKE_library.h" // for set_listbasepointers
154 #include "BKE_main.h"
155 #include "BKE_node.h"
156 #include "BKE_report.h"
157 #include "BKE_sequencer.h"
158 #include "BKE_subsurf.h"
159 #include "BKE_modifier.h"
160 #include "BKE_fcurve.h"
161 #include "BKE_pointcache.h"
162 #include "BKE_mesh.h"
164 #ifdef USE_NODE_COMPAT_CUSTOMNODES
165 #include "NOD_socket.h" /* for sock->default_value data */
169 #include "BLO_writefile.h"
170 #include "BLO_readfile.h"
171 #include "BLO_undofile.h"
172 #include "BLO_blend_defs.h"
174 #include "readfile.h"
178 /* ********* my write, buffered writing with minimum size chunks ************ */
180 #define MYWRITE_BUFFER_SIZE 100000
181 #define MYWRITE_MAX_CHUNK 32768
185 /** \name Small API to handle compression.
193 typedef struct WriteWrap WriteWrap;
196 bool (*open)(WriteWrap *ww, const char *filepath);
197 bool (*close)(WriteWrap *ww);
198 size_t (*write)(WriteWrap *ww, const char *data, size_t data_len);
208 #define FILE_HANDLE(ww) \
209 (ww)->_user_data.file_handle
211 static bool ww_open_none(WriteWrap *ww, const char *filepath)
215 file = BLI_open(filepath, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
218 FILE_HANDLE(ww) = file;
225 static bool ww_close_none(WriteWrap *ww)
227 return (close(FILE_HANDLE(ww)) != -1);
229 static size_t ww_write_none(WriteWrap *ww, const char *buf, size_t buf_len)
231 return write(FILE_HANDLE(ww), buf, buf_len);
236 #define FILE_HANDLE(ww) \
237 (ww)->_user_data.gz_handle
239 static bool ww_open_zlib(WriteWrap *ww, const char *filepath)
243 file = BLI_gzopen(filepath, "wb1");
245 if (file != Z_NULL) {
246 FILE_HANDLE(ww) = file;
253 static bool ww_close_zlib(WriteWrap *ww)
255 return (gzclose(FILE_HANDLE(ww)) == Z_OK);
257 static size_t ww_write_zlib(WriteWrap *ww, const char *buf, size_t buf_len)
259 return gzwrite(FILE_HANDLE(ww), buf, buf_len);
263 /* --- end compression types --- */
265 static void ww_handle_init(eWriteWrapType ww_type, WriteWrap *r_ww)
267 memset(r_ww, 0, sizeof(*r_ww));
272 r_ww->open = ww_open_zlib;
273 r_ww->close = ww_close_zlib;
274 r_ww->write = ww_write_zlib;
279 r_ww->open = ww_open_none;
280 r_ww->close = ww_close_none;
281 r_ww->write = ww_write_none;
296 MemFile *compare, *current;
298 int tot, count, error, memsize;
300 /* Wrap writing, so we can use zlib or
301 * other compression types later, see: G_FILE_COMPRESS
302 * Will be NULL for UNDO. */
305 #ifdef USE_BMESH_SAVE_AS_COMPAT
306 char use_mesh_compat; /* option to save with older mesh format */
310 static WriteData *writedata_new(WriteWrap *ww)
312 WriteData *wd= MEM_callocN(sizeof(*wd), "writedata");
314 /* XXX, see note about this in readfile.c, remove
315 * once we have an xp lock - zr
318 if (wd == NULL) return NULL;
320 wd->sdna = DNA_sdna_from_data(DNAstr, DNAlen, false);
324 wd->buf= MEM_mallocN(MYWRITE_BUFFER_SIZE, "wd->buf");
329 static void writedata_do_write(WriteData *wd, const void *mem, int memlen)
331 if ((wd == NULL) || wd->error || (mem == NULL) || memlen < 1) return;
332 if (wd->error) return;
334 /* memory based save */
336 memfile_chunk_add(NULL, wd->current, mem, memlen);
339 if (wd->ww->write(wd->ww, mem, memlen) != memlen) {
345 static void writedata_free(WriteData *wd)
347 DNA_sdna_free(wd->sdna);
356 * Low level WRITE(2) wrapper that buffers data
357 * \param adr Pointer to new chunk of data
358 * \param len Length of new chunk of data
359 * \warning Talks to other functions with global parameters
362 #define MYWRITE_FLUSH NULL
364 static void mywrite(WriteData *wd, const void *adr, int len)
366 if (wd->error) return;
368 /* flush helps compression for undo-save */
369 if (adr==MYWRITE_FLUSH) {
371 writedata_do_write(wd, wd->buf, wd->count);
379 /* if we have a single big chunk, write existing data in
380 * buffer and write out big chunk in smaller pieces */
381 if (len>MYWRITE_MAX_CHUNK) {
383 writedata_do_write(wd, wd->buf, wd->count);
388 int writelen= MIN2(len, MYWRITE_MAX_CHUNK);
389 writedata_do_write(wd, adr, writelen);
390 adr = (const char *)adr + writelen;
397 /* if data would overflow buffer, write out the buffer */
398 if (len+wd->count>MYWRITE_BUFFER_SIZE-1) {
399 writedata_do_write(wd, wd->buf, wd->count);
403 /* append data at end of buffer */
404 memcpy(&wd->buf[wd->count], adr, len);
409 * BeGiN initializer for mywrite
410 * \param ww: File write wrapper.
411 * \param compare Previous memory file (can be NULL).
412 * \param current The current memory file (can be NULL).
413 * \warning Talks to other functions with global parameters
415 static WriteData *bgnwrite(WriteWrap *ww, MemFile *compare, MemFile *current)
417 WriteData *wd= writedata_new(ww);
419 if (wd == NULL) return NULL;
421 wd->compare= compare;
422 wd->current= current;
423 /* this inits comparing */
424 memfile_chunk_add(compare, NULL, NULL, 0);
430 * END the mywrite wrapper
431 * \return 1 if write failed
432 * \return unknown global variable otherwise
433 * \warning Talks to other functions with global parameters
435 static int endwrite(WriteData *wd)
440 writedata_do_write(wd, wd->buf, wd->count);
450 /* ********** WRITE FILE ****************** */
452 static void writestruct_at_address(WriteData *wd, int filecode, const char *structname, int nr, void *adr, void *data)
457 if (adr==NULL || data==NULL || nr==0) return;
464 bh.SDNAnr= DNA_struct_find_nr(wd->sdna, structname);
465 if (bh.SDNAnr== -1) {
466 printf("error: can't find SDNA code <%s>\n", structname);
469 sp= wd->sdna->structs[bh.SDNAnr];
471 bh.len= nr*wd->sdna->typelens[sp[0]];
473 if (bh.len==0) return;
475 mywrite(wd, &bh, sizeof(BHead));
476 mywrite(wd, data, bh.len);
479 static void writestruct(WriteData *wd, int filecode, const char *structname, int nr, void *adr)
481 writestruct_at_address(wd, filecode, structname, nr, adr, adr);
484 static void writedata(WriteData *wd, int filecode, int len, const void *adr) /* do not use for structs */
488 if (adr==NULL) return;
491 /* align to 4 (writes uninitialized bytes in some cases) */
492 len = (len + 3) & ~3;
496 bh.old = (void *)adr; /* this is safe to cast from const */
501 mywrite(wd, &bh, sizeof(BHead));
502 mywrite(wd, adr, len);
505 /* use this to force writing of lists in same order as reading (using link_list) */
506 static void writelist(WriteData *wd, int filecode, const char *structname, ListBase *lb)
508 Link *link = lb->first;
511 writestruct(wd, filecode, structname, 1, link);
516 /* *************** writing some direct data structs used in more code parts **************** */
517 /*These functions are used by blender's .blend system for file saving/loading.*/
518 void IDP_WriteProperty_OnlyData(IDProperty *prop, void *wd);
519 void IDP_WriteProperty(IDProperty *prop, void *wd);
521 static void IDP_WriteArray(IDProperty *prop, void *wd)
523 /*REMEMBER to set totalen to len in the linking code!!*/
524 if (prop->data.pointer) {
525 writedata(wd, DATA, MEM_allocN_len(prop->data.pointer), prop->data.pointer);
527 if (prop->subtype == IDP_GROUP) {
528 IDProperty **array= prop->data.pointer;
531 for (a=0; a<prop->len; a++)
532 IDP_WriteProperty(array[a], wd);
537 static void IDP_WriteIDPArray(IDProperty *prop, void *wd)
539 /*REMEMBER to set totalen to len in the linking code!!*/
540 if (prop->data.pointer) {
541 IDProperty *array = prop->data.pointer;
544 writestruct(wd, DATA, "IDProperty", prop->len, array);
546 for (a=0; a<prop->len; a++)
547 IDP_WriteProperty_OnlyData(&array[a], wd);
551 static void IDP_WriteString(IDProperty *prop, void *wd)
553 /*REMEMBER to set totalen to len in the linking code!!*/
554 writedata(wd, DATA, prop->len, prop->data.pointer);
557 static void IDP_WriteGroup(IDProperty *prop, void *wd)
561 for (loop=prop->data.group.first; loop; loop=loop->next) {
562 IDP_WriteProperty(loop, wd);
566 /* Functions to read/write ID Properties */
567 void IDP_WriteProperty_OnlyData(IDProperty *prop, void *wd)
569 switch (prop->type) {
571 IDP_WriteGroup(prop, wd);
574 IDP_WriteString(prop, wd);
577 IDP_WriteArray(prop, wd);
580 IDP_WriteIDPArray(prop, wd);
585 void IDP_WriteProperty(IDProperty *prop, void *wd)
587 writestruct(wd, DATA, "IDProperty", 1, prop);
588 IDP_WriteProperty_OnlyData(prop, wd);
591 static void write_fmodifiers(WriteData *wd, ListBase *fmodifiers)
595 /* Write all modifiers first (for faster reloading) */
596 writelist(wd, DATA, "FModifier", fmodifiers);
599 for (fcm= fmodifiers->first; fcm; fcm= fcm->next) {
600 const FModifierTypeInfo *fmi= fmodifier_get_typeinfo(fcm);
602 /* Write the specific data */
603 if (fmi && fcm->data) {
604 /* firstly, just write the plain fmi->data struct */
605 writestruct(wd, DATA, fmi->structName, 1, fcm->data);
607 /* do any modifier specific stuff */
609 case FMODIFIER_TYPE_GENERATOR:
611 FMod_Generator *data= (FMod_Generator *)fcm->data;
613 /* write coefficients array */
614 if (data->coefficients)
615 writedata(wd, DATA, sizeof(float)*(data->arraysize), data->coefficients);
618 case FMODIFIER_TYPE_ENVELOPE:
620 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
622 /* write envelope data */
624 writestruct(wd, DATA, "FCM_EnvelopeData", data->totvert, data->data);
627 case FMODIFIER_TYPE_PYTHON:
629 FMod_Python *data = (FMod_Python *)fcm->data;
631 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
632 * of library blocks that implement this.*/
633 IDP_WriteProperty(data->prop, wd);
641 static void write_fcurves(WriteData *wd, ListBase *fcurves)
645 writelist(wd, DATA, "FCurve", fcurves);
646 for (fcu=fcurves->first; fcu; fcu=fcu->next) {
649 writestruct(wd, DATA, "BezTriple", fcu->totvert, fcu->bezt);
651 writestruct(wd, DATA, "FPoint", fcu->totvert, fcu->fpt);
654 writedata(wd, DATA, strlen(fcu->rna_path)+1, fcu->rna_path);
658 ChannelDriver *driver= fcu->driver;
661 writestruct(wd, DATA, "ChannelDriver", 1, driver);
664 writelist(wd, DATA, "DriverVar", &driver->variables);
665 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
666 DRIVER_TARGETS_USED_LOOPER(dvar)
669 writedata(wd, DATA, strlen(dtar->rna_path)+1, dtar->rna_path);
671 DRIVER_TARGETS_LOOPER_END
675 /* write F-Modifiers */
676 write_fmodifiers(wd, &fcu->modifiers);
680 static void write_actions(WriteData *wd, ListBase *idbase)
686 for (act=idbase->first; act; act= act->id.next) {
687 if (act->id.us>0 || wd->current) {
688 writestruct(wd, ID_AC, "bAction", 1, act);
689 if (act->id.properties) IDP_WriteProperty(act->id.properties, wd);
691 write_fcurves(wd, &act->curves);
693 for (grp=act->groups.first; grp; grp=grp->next) {
694 writestruct(wd, DATA, "bActionGroup", 1, grp);
697 for (marker=act->markers.first; marker; marker=marker->next) {
698 writestruct(wd, DATA, "TimeMarker", 1, marker);
703 /* flush helps the compression for undo-save */
704 mywrite(wd, MYWRITE_FLUSH, 0);
707 static void write_keyingsets(WriteData *wd, ListBase *list)
712 for (ks= list->first; ks; ks= ks->next) {
714 writestruct(wd, DATA, "KeyingSet", 1, ks);
717 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
719 writestruct(wd, DATA, "KS_Path", 1, ksp);
722 writedata(wd, DATA, strlen(ksp->rna_path)+1, ksp->rna_path);
727 static void write_nlastrips(WriteData *wd, ListBase *strips)
731 writelist(wd, DATA, "NlaStrip", strips);
732 for (strip= strips->first; strip; strip= strip->next) {
733 /* write the strip's F-Curves and modifiers */
734 write_fcurves(wd, &strip->fcurves);
735 write_fmodifiers(wd, &strip->modifiers);
737 /* write the strip's children */
738 write_nlastrips(wd, &strip->strips);
742 static void write_nladata(WriteData *wd, ListBase *nlabase)
746 /* write all the tracks */
747 for (nlt= nlabase->first; nlt; nlt= nlt->next) {
748 /* write the track first */
749 writestruct(wd, DATA, "NlaTrack", 1, nlt);
751 /* write the track's strips */
752 write_nlastrips(wd, &nlt->strips);
756 static void write_animdata(WriteData *wd, AnimData *adt)
760 /* firstly, just write the AnimData block */
761 writestruct(wd, DATA, "AnimData", 1, adt);
764 write_fcurves(wd, &adt->drivers);
766 /* write overrides */
767 // FIXME: are these needed?
768 for (aor= adt->overrides.first; aor; aor= aor->next) {
769 /* overrides consist of base data + rna_path */
770 writestruct(wd, DATA, "AnimOverride", 1, aor);
771 writedata(wd, DATA, strlen(aor->rna_path)+1, aor->rna_path);
774 // TODO write the remaps (if they are needed)
777 write_nladata(wd, &adt->nla_tracks);
780 static void write_curvemapping_curves(WriteData *wd, CurveMapping *cumap)
784 for (a = 0; a < CM_TOT; a++)
785 writestruct(wd, DATA, "CurveMapPoint", cumap->cm[a].totpoint, cumap->cm[a].curve);
788 static void write_curvemapping(WriteData *wd, CurveMapping *cumap)
790 writestruct(wd, DATA, "CurveMapping", 1, cumap);
792 write_curvemapping_curves(wd, cumap);
795 static void write_node_socket(WriteData *wd, bNodeTree *UNUSED(ntree), bNode *node, bNodeSocket *sock)
797 #ifdef USE_NODE_COMPAT_CUSTOMNODES
798 /* forward compatibility code, so older blenders still open */
799 sock->stack_type = 1;
801 if (node->type == NODE_GROUP) {
802 bNodeTree *ngroup = (bNodeTree *)node->id;
804 /* for node groups: look up the deprecated groupsock pointer */
805 sock->groupsock = ntreeFindSocketInterface(ngroup, sock->in_out, sock->identifier);
806 BLI_assert(sock->groupsock != NULL);
808 /* node group sockets now use the generic identifier string to verify group nodes,
809 * old blender uses the own_index.
811 sock->own_index = sock->groupsock->own_index;
816 /* actual socket writing */
817 writestruct(wd, DATA, "bNodeSocket", 1, sock);
820 IDP_WriteProperty(sock->prop, wd);
822 if (sock->default_value)
823 writedata(wd, DATA, MEM_allocN_len(sock->default_value), sock->default_value);
825 static void write_node_socket_interface(WriteData *wd, bNodeTree *UNUSED(ntree), bNodeSocket *sock)
827 #ifdef USE_NODE_COMPAT_CUSTOMNODES
828 /* forward compatibility code, so older blenders still open */
829 sock->stack_type = 1;
831 /* Reconstruct the deprecated default_value structs in socket interface DNA. */
832 if (sock->default_value == NULL && sock->typeinfo) {
833 node_socket_init_default_value(sock);
837 /* actual socket writing */
838 writestruct(wd, DATA, "bNodeSocket", 1, sock);
841 IDP_WriteProperty(sock->prop, wd);
843 if (sock->default_value)
844 writedata(wd, DATA, MEM_allocN_len(sock->default_value), sock->default_value);
846 /* this is only direct data, tree itself should have been written */
847 static void write_nodetree(WriteData *wd, bNodeTree *ntree)
853 /* for link_list() speed, we write per list */
855 if (ntree->adt) write_animdata(wd, ntree->adt);
857 for (node = ntree->nodes.first; node; node = node->next) {
858 writestruct(wd, DATA, "bNode", 1, node);
861 IDP_WriteProperty(node->prop, wd);
863 for (sock= node->inputs.first; sock; sock= sock->next)
864 write_node_socket(wd, ntree, node, sock);
865 for (sock= node->outputs.first; sock; sock= sock->next)
866 write_node_socket(wd, ntree, node, sock);
868 for (link = node->internal_links.first; link; link = link->next)
869 writestruct(wd, DATA, "bNodeLink", 1, link);
871 /* could be handlerized at some point, now only 1 exception still */
872 if (ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
873 write_curvemapping(wd, node->storage);
874 else if (ntree->type==NTREE_SHADER && node->type==SH_NODE_SCRIPT) {
875 NodeShaderScript *nss = (NodeShaderScript *)node->storage;
877 writedata(wd, DATA, strlen(nss->bytecode)+1, nss->bytecode);
878 writestruct(wd, DATA, node->typeinfo->storagename, 1, node->storage);
880 else if (ntree->type==NTREE_COMPOSIT && ELEM(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
881 write_curvemapping(wd, node->storage);
882 else if (ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) )
883 write_curvemapping(wd, node->storage);
884 else if (ntree->type==NTREE_COMPOSIT && node->type==CMP_NODE_MOVIEDISTORTION) {
888 writestruct(wd, DATA, node->typeinfo->storagename, 1, node->storage);
891 if (node->type==CMP_NODE_OUTPUT_FILE) {
892 /* inputs have own storage data */
893 for (sock = node->inputs.first; sock; sock = sock->next)
894 writestruct(wd, DATA, "NodeImageMultiFileSocket", 1, sock->storage);
896 if (node->type==CMP_NODE_IMAGE) {
897 /* write extra socket info */
898 for (sock = node->outputs.first; sock; sock = sock->next)
899 writestruct(wd, DATA, "NodeImageLayer", 1, sock->storage);
903 for (link= ntree->links.first; link; link= link->next)
904 writestruct(wd, DATA, "bNodeLink", 1, link);
906 for (sock = ntree->inputs.first; sock; sock = sock->next)
907 write_node_socket_interface(wd, ntree, sock);
908 for (sock = ntree->outputs.first; sock; sock = sock->next)
909 write_node_socket_interface(wd, ntree, sock);
913 * Take care using 'use_active_win', since we wont want the currently active window
914 * to change which scene renders (currently only used for undo).
916 static void current_screen_compat(Main *mainvar, bScreen **r_screen, bool use_active_win)
919 wmWindow *window = NULL;
921 /* find a global current screen in the first open window, to have
922 * a reasonable default for reading in older versions */
923 wm = mainvar->wm.first;
926 if (use_active_win) {
927 /* write the active window into the file, needed for multi-window undo T43424 */
928 for (window = wm->windows.first; window; window = window->next) {
929 if (window->active) {
935 if (window == NULL) {
936 window = wm->windows.first;
940 window = wm->windows.first;
944 *r_screen = (window) ? window->screen : NULL;
947 typedef struct RenderInfo {
950 char scene_name[MAX_ID_NAME - 2];
953 /* was for historic render-deamon feature,
954 * now write because it can be easily extracted without
955 * reading the whole blend file */
956 static void write_renderinfo(WriteData *wd, Main *mainvar)
959 Scene *sce, *curscene = NULL;
962 /* XXX in future, handle multiple windows with multiple screens? */
963 current_screen_compat(mainvar, &curscreen, false);
964 if (curscreen) curscene = curscreen->scene;
966 for (sce= mainvar->scene.first; sce; sce= sce->id.next) {
967 if (sce->id.lib == NULL && (sce == curscene || (sce->r.scemode & R_BG_RENDER))) {
968 data.sfra = sce->r.sfra;
969 data.efra = sce->r.efra;
970 memset(data.scene_name, 0, sizeof(data.scene_name));
972 BLI_strncpy(data.scene_name, sce->id.name + 2, sizeof(data.scene_name));
974 writedata(wd, REND, sizeof(data), &data);
979 static void write_keymapitem(WriteData *wd, wmKeyMapItem *kmi)
981 writestruct(wd, DATA, "wmKeyMapItem", 1, kmi);
983 IDP_WriteProperty(kmi->properties, wd);
986 static void write_userdef(WriteData *wd)
991 wmKeyMapDiffItem *kmdi;
993 bPathCompare *path_cmp;
996 writestruct(wd, USER, "UserDef", 1, &U);
998 for (btheme= U.themes.first; btheme; btheme=btheme->next)
999 writestruct(wd, DATA, "bTheme", 1, btheme);
1001 for (keymap= U.user_keymaps.first; keymap; keymap=keymap->next) {
1002 writestruct(wd, DATA, "wmKeyMap", 1, keymap);
1004 for (kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next) {
1005 writestruct(wd, DATA, "wmKeyMapDiffItem", 1, kmdi);
1006 if (kmdi->remove_item)
1007 write_keymapitem(wd, kmdi->remove_item);
1009 write_keymapitem(wd, kmdi->add_item);
1012 for (kmi=keymap->items.first; kmi; kmi=kmi->next)
1013 write_keymapitem(wd, kmi);
1016 for (bext= U.addons.first; bext; bext=bext->next) {
1017 writestruct(wd, DATA, "bAddon", 1, bext);
1019 IDP_WriteProperty(bext->prop, wd);
1023 for (path_cmp = U.autoexec_paths.first; path_cmp; path_cmp = path_cmp->next) {
1024 writestruct(wd, DATA, "bPathCompare", 1, path_cmp);
1027 for (style= U.uistyles.first; style; style= style->next) {
1028 writestruct(wd, DATA, "uiStyle", 1, style);
1032 static void write_boid_state(WriteData *wd, BoidState *state)
1034 BoidRule *rule = state->rules.first;
1035 //BoidCondition *cond = state->conditions.first;
1037 writestruct(wd, DATA, "BoidState", 1, state);
1039 for (; rule; rule=rule->next) {
1040 switch (rule->type) {
1041 case eBoidRuleType_Goal:
1042 case eBoidRuleType_Avoid:
1043 writestruct(wd, DATA, "BoidRuleGoalAvoid", 1, rule);
1045 case eBoidRuleType_AvoidCollision:
1046 writestruct(wd, DATA, "BoidRuleAvoidCollision", 1, rule);
1048 case eBoidRuleType_FollowLeader:
1049 writestruct(wd, DATA, "BoidRuleFollowLeader", 1, rule);
1051 case eBoidRuleType_AverageSpeed:
1052 writestruct(wd, DATA, "BoidRuleAverageSpeed", 1, rule);
1054 case eBoidRuleType_Fight:
1055 writestruct(wd, DATA, "BoidRuleFight", 1, rule);
1058 writestruct(wd, DATA, "BoidRule", 1, rule);
1062 //for (; cond; cond=cond->next)
1063 // writestruct(wd, DATA, "BoidCondition", 1, cond);
1066 /* update this also to readfile.c */
1067 static const char *ptcache_data_struct[] = {
1068 "", // BPHYS_DATA_INDEX
1069 "", // BPHYS_DATA_LOCATION
1070 "", // BPHYS_DATA_VELOCITY
1071 "", // BPHYS_DATA_ROTATION
1072 "", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
1073 "", // BPHYS_DATA_SIZE:
1074 "", // BPHYS_DATA_TIMES:
1075 "BoidData" // case BPHYS_DATA_BOIDS:
1077 static const char *ptcache_extra_struct[] = {
1081 static void write_pointcaches(WriteData *wd, ListBase *ptcaches)
1083 PointCache *cache = ptcaches->first;
1086 for (; cache; cache=cache->next) {
1087 writestruct(wd, DATA, "PointCache", 1, cache);
1089 if ((cache->flag & PTCACHE_DISK_CACHE)==0) {
1090 PTCacheMem *pm = cache->mem_cache.first;
1092 for (; pm; pm=pm->next) {
1093 PTCacheExtra *extra = pm->extradata.first;
1095 writestruct(wd, DATA, "PTCacheMem", 1, pm);
1097 for (i=0; i<BPHYS_TOT_DATA; i++) {
1098 if (pm->data[i] && pm->data_types & (1<<i)) {
1099 if (ptcache_data_struct[i][0] == '\0')
1100 writedata(wd, DATA, MEM_allocN_len(pm->data[i]), pm->data[i]);
1102 writestruct(wd, DATA, ptcache_data_struct[i], pm->totpoint, pm->data[i]);
1106 for (; extra; extra=extra->next) {
1107 if (ptcache_extra_struct[extra->type][0] == '\0')
1109 writestruct(wd, DATA, "PTCacheExtra", 1, extra);
1110 writestruct(wd, DATA, ptcache_extra_struct[extra->type], extra->totdata, extra->data);
1116 static void write_particlesettings(WriteData *wd, ListBase *idbase)
1118 ParticleSettings *part;
1119 ParticleDupliWeight *dw;
1123 part= idbase->first;
1125 if (part->id.us>0 || wd->current) {
1127 writestruct(wd, ID_PA, "ParticleSettings", 1, part);
1128 if (part->id.properties) IDP_WriteProperty(part->id.properties, wd);
1129 if (part->adt) write_animdata(wd, part->adt);
1130 writestruct(wd, DATA, "PartDeflect", 1, part->pd);
1131 writestruct(wd, DATA, "PartDeflect", 1, part->pd2);
1132 writestruct(wd, DATA, "EffectorWeights", 1, part->effector_weights);
1134 if (part->clumpcurve)
1135 write_curvemapping(wd, part->clumpcurve);
1136 if (part->roughcurve)
1137 write_curvemapping(wd, part->roughcurve);
1139 dw = part->dupliweights.first;
1140 for (; dw; dw=dw->next) {
1141 /* update indices */
1143 if (part->dup_group) { /* can be NULL if lining fails or set to None */
1144 go = part->dup_group->gobject.first;
1145 while (go && go->ob != dw->ob) {
1150 writestruct(wd, DATA, "ParticleDupliWeight", 1, dw);
1153 if (part->boids && part->phystype == PART_PHYS_BOIDS) {
1154 BoidState *state = part->boids->states.first;
1156 writestruct(wd, DATA, "BoidSettings", 1, part->boids);
1158 for (; state; state=state->next)
1159 write_boid_state(wd, state);
1161 if (part->fluid && part->phystype == PART_PHYS_FLUID) {
1162 writestruct(wd, DATA, "SPHFluidSettings", 1, part->fluid);
1165 for (a=0; a<MAX_MTEX; a++) {
1166 if (part->mtex[a]) writestruct(wd, DATA, "MTex", 1, part->mtex[a]);
1169 part= part->id.next;
1172 static void write_particlesystems(WriteData *wd, ListBase *particles)
1174 ParticleSystem *psys= particles->first;
1178 for (; psys; psys=psys->next) {
1179 writestruct(wd, DATA, "ParticleSystem", 1, psys);
1181 if (psys->particles) {
1182 writestruct(wd, DATA, "ParticleData", psys->totpart, psys->particles);
1184 if (psys->particles->hair) {
1185 ParticleData *pa = psys->particles;
1187 for (a=0; a<psys->totpart; a++, pa++)
1188 writestruct(wd, DATA, "HairKey", pa->totkey, pa->hair);
1191 if (psys->particles->boid && psys->part->phystype == PART_PHYS_BOIDS)
1192 writestruct(wd, DATA, "BoidParticle", psys->totpart, psys->particles->boid);
1194 if (psys->part->fluid && psys->part->phystype == PART_PHYS_FLUID && (psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS))
1195 writestruct(wd, DATA, "ParticleSpring", psys->tot_fluidsprings, psys->fluid_springs);
1197 pt = psys->targets.first;
1198 for (; pt; pt=pt->next)
1199 writestruct(wd, DATA, "ParticleTarget", 1, pt);
1201 if (psys->child) writestruct(wd, DATA, "ChildParticle", psys->totchild, psys->child);
1204 writestruct(wd, DATA, "ClothModifierData", 1, psys->clmd);
1205 writestruct(wd, DATA, "ClothSimSettings", 1, psys->clmd->sim_parms);
1206 writestruct(wd, DATA, "ClothCollSettings", 1, psys->clmd->coll_parms);
1209 write_pointcaches(wd, &psys->ptcaches);
1213 static void write_properties(WriteData *wd, ListBase *lb)
1219 writestruct(wd, DATA, "bProperty", 1, prop);
1221 if (prop->poin && prop->poin != &prop->data)
1222 writedata(wd, DATA, MEM_allocN_len(prop->poin), prop->poin);
1228 static void write_sensors(WriteData *wd, ListBase *lb)
1234 writestruct(wd, DATA, "bSensor", 1, sens);
1236 writedata(wd, DATA, sizeof(void *)*sens->totlinks, sens->links);
1238 switch (sens->type) {
1240 writestruct(wd, DATA, "bNearSensor", 1, sens->data);
1243 writestruct(wd, DATA, "bMouseSensor", 1, sens->data);
1246 writestruct(wd, DATA, "bKeyboardSensor", 1, sens->data);
1249 writestruct(wd, DATA, "bPropertySensor", 1, sens->data);
1252 writestruct(wd, DATA, "bArmatureSensor", 1, sens->data);
1255 writestruct(wd, DATA, "bActuatorSensor", 1, sens->data);
1258 writestruct(wd, DATA, "bDelaySensor", 1, sens->data);
1260 case SENS_COLLISION:
1261 writestruct(wd, DATA, "bCollisionSensor", 1, sens->data);
1264 writestruct(wd, DATA, "bRadarSensor", 1, sens->data);
1267 writestruct(wd, DATA, "bRandomSensor", 1, sens->data);
1270 writestruct(wd, DATA, "bRaySensor", 1, sens->data);
1273 writestruct(wd, DATA, "bMessageSensor", 1, sens->data);
1276 writestruct(wd, DATA, "bJoystickSensor", 1, sens->data);
1279 ; /* error: don't know how to write this file */
1286 static void write_controllers(WriteData *wd, ListBase *lb)
1292 writestruct(wd, DATA, "bController", 1, cont);
1294 writedata(wd, DATA, sizeof(void *)*cont->totlinks, cont->links);
1296 switch (cont->type) {
1297 case CONT_EXPRESSION:
1298 writestruct(wd, DATA, "bExpressionCont", 1, cont->data);
1301 writestruct(wd, DATA, "bPythonCont", 1, cont->data);
1304 ; /* error: don't know how to write this file */
1311 static void write_actuators(WriteData *wd, ListBase *lb)
1317 writestruct(wd, DATA, "bActuator", 1, act);
1319 switch (act->type) {
1321 case ACT_SHAPEACTION:
1322 writestruct(wd, DATA, "bActionActuator", 1, act->data);
1325 writestruct(wd, DATA, "bSoundActuator", 1, act->data);
1328 writestruct(wd, DATA, "bObjectActuator", 1, act->data);
1331 writestruct(wd, DATA, "bIpoActuator", 1, act->data);
1334 writestruct(wd, DATA, "bPropertyActuator", 1, act->data);
1337 writestruct(wd, DATA, "bCameraActuator", 1, act->data);
1339 case ACT_CONSTRAINT:
1340 writestruct(wd, DATA, "bConstraintActuator", 1, act->data);
1342 case ACT_EDIT_OBJECT:
1343 writestruct(wd, DATA, "bEditObjectActuator", 1, act->data);
1346 writestruct(wd, DATA, "bSceneActuator", 1, act->data);
1349 writestruct(wd, DATA, "bGroupActuator", 1, act->data);
1352 writestruct(wd, DATA, "bRandomActuator", 1, act->data);
1355 writestruct(wd, DATA, "bMessageActuator", 1, act->data);
1358 writestruct(wd, DATA, "bGameActuator", 1, act->data);
1360 case ACT_VISIBILITY:
1361 writestruct(wd, DATA, "bVisibilityActuator", 1, act->data);
1364 writestruct(wd, DATA, "bTwoDFilterActuator", 1, act->data);
1367 writestruct(wd, DATA, "bParentActuator", 1, act->data);
1370 writestruct(wd, DATA, "bStateActuator", 1, act->data);
1373 writestruct(wd, DATA, "bArmatureActuator", 1, act->data);
1376 writestruct(wd, DATA, "bSteeringActuator", 1, act->data);
1379 writestruct(wd, DATA, "bMouseActuator", 1, act->data);
1382 ; /* error: don't know how to write this file */
1389 static void write_motionpath(WriteData *wd, bMotionPath *mpath)
1395 /* firstly, just write the motionpath struct */
1396 writestruct(wd, DATA, "bMotionPath", 1, mpath);
1398 /* now write the array of data */
1399 writestruct(wd, DATA, "bMotionPathVert", mpath->length, mpath->points);
1402 static void write_constraints(WriteData *wd, ListBase *conlist)
1406 for (con=conlist->first; con; con=con->next) {
1407 const bConstraintTypeInfo *cti= BKE_constraint_typeinfo_get(con);
1409 /* Write the specific data */
1410 if (cti && con->data) {
1411 /* firstly, just write the plain con->data struct */
1412 writestruct(wd, DATA, cti->structName, 1, con->data);
1414 /* do any constraint specific stuff */
1415 switch (con->type) {
1416 case CONSTRAINT_TYPE_PYTHON:
1418 bPythonConstraint *data = (bPythonConstraint *)con->data;
1419 bConstraintTarget *ct;
1422 for (ct= data->targets.first; ct; ct= ct->next)
1423 writestruct(wd, DATA, "bConstraintTarget", 1, ct);
1425 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
1426 * of library blocks that implement this.*/
1427 IDP_WriteProperty(data->prop, wd);
1430 case CONSTRAINT_TYPE_SPLINEIK:
1432 bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
1434 /* write points array */
1435 writedata(wd, DATA, sizeof(float)*(data->numpoints), data->points);
1441 /* Write the constraint */
1442 writestruct(wd, DATA, "bConstraint", 1, con);
1446 static void write_pose(WriteData *wd, bPose *pose)
1451 /* Write each channel */
1455 /* Write channels */
1456 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1457 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
1458 * of library blocks that implement this.*/
1460 IDP_WriteProperty(chan->prop, wd);
1462 write_constraints(wd, &chan->constraints);
1464 write_motionpath(wd, chan->mpath);
1466 /* prevent crashes with autosave, when a bone duplicated in editmode has not yet been assigned to its posechannel */
1468 chan->selectflag= chan->bone->flag & BONE_SELECTED; /* gets restored on read, for library armatures */
1470 writestruct(wd, DATA, "bPoseChannel", 1, chan);
1474 for (grp=pose->agroups.first; grp; grp=grp->next)
1475 writestruct(wd, DATA, "bActionGroup", 1, grp);
1477 /* write IK param */
1478 if (pose->ikparam) {
1479 const char *structname = BKE_pose_ikparam_get_name(pose);
1481 writestruct(wd, DATA, structname, 1, pose->ikparam);
1484 /* Write this pose */
1485 writestruct(wd, DATA, "bPose", 1, pose);
1489 static void write_defgroups(WriteData *wd, ListBase *defbase)
1491 bDeformGroup *defgroup;
1493 for (defgroup=defbase->first; defgroup; defgroup=defgroup->next)
1494 writestruct(wd, DATA, "bDeformGroup", 1, defgroup);
1497 static void write_modifiers(WriteData *wd, ListBase *modbase)
1501 if (modbase == NULL) return;
1502 for (md=modbase->first; md; md= md->next) {
1503 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1504 if (mti == NULL) return;
1506 writestruct(wd, DATA, mti->structName, 1, md);
1508 if (md->type==eModifierType_Hook) {
1509 HookModifierData *hmd = (HookModifierData*) md;
1511 if (hmd->curfalloff) {
1512 write_curvemapping(wd, hmd->curfalloff);
1515 writedata(wd, DATA, sizeof(int)*hmd->totindex, hmd->indexar);
1517 else if (md->type==eModifierType_Cloth) {
1518 ClothModifierData *clmd = (ClothModifierData*) md;
1520 writestruct(wd, DATA, "ClothSimSettings", 1, clmd->sim_parms);
1521 writestruct(wd, DATA, "ClothCollSettings", 1, clmd->coll_parms);
1522 writestruct(wd, DATA, "EffectorWeights", 1, clmd->sim_parms->effector_weights);
1523 write_pointcaches(wd, &clmd->ptcaches);
1525 else if (md->type==eModifierType_Smoke) {
1526 SmokeModifierData *smd = (SmokeModifierData*) md;
1528 if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
1530 write_pointcaches(wd, &(smd->domain->ptcaches[0]));
1532 /* create fake pointcache so that old blender versions can read it */
1533 smd->domain->point_cache[1] = BKE_ptcache_add(&smd->domain->ptcaches[1]);
1534 smd->domain->point_cache[1]->flag |= PTCACHE_DISK_CACHE|PTCACHE_FAKE_SMOKE;
1535 smd->domain->point_cache[1]->step = 1;
1537 write_pointcaches(wd, &(smd->domain->ptcaches[1]));
1540 writestruct(wd, DATA, "SmokeDomainSettings", 1, smd->domain);
1543 /* cleanup the fake pointcache */
1544 BKE_ptcache_free_list(&smd->domain->ptcaches[1]);
1545 smd->domain->point_cache[1] = NULL;
1547 writestruct(wd, DATA, "EffectorWeights", 1, smd->domain->effector_weights);
1550 else if (smd->type & MOD_SMOKE_TYPE_FLOW)
1551 writestruct(wd, DATA, "SmokeFlowSettings", 1, smd->flow);
1552 else if (smd->type & MOD_SMOKE_TYPE_COLL)
1553 writestruct(wd, DATA, "SmokeCollSettings", 1, smd->coll);
1555 else if (md->type==eModifierType_Fluidsim) {
1556 FluidsimModifierData *fluidmd = (FluidsimModifierData*) md;
1558 writestruct(wd, DATA, "FluidsimSettings", 1, fluidmd->fss);
1560 else if (md->type==eModifierType_DynamicPaint) {
1561 DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
1564 DynamicPaintSurface *surface;
1565 writestruct(wd, DATA, "DynamicPaintCanvasSettings", 1, pmd->canvas);
1567 /* write surfaces */
1568 for (surface=pmd->canvas->surfaces.first; surface; surface=surface->next)
1569 writestruct(wd, DATA, "DynamicPaintSurface", 1, surface);
1570 /* write caches and effector weights */
1571 for (surface=pmd->canvas->surfaces.first; surface; surface=surface->next) {
1572 write_pointcaches(wd, &(surface->ptcaches));
1574 writestruct(wd, DATA, "EffectorWeights", 1, surface->effector_weights);
1578 writestruct(wd, DATA, "DynamicPaintBrushSettings", 1, pmd->brush);
1579 writestruct(wd, DATA, "ColorBand", 1, pmd->brush->paint_ramp);
1580 writestruct(wd, DATA, "ColorBand", 1, pmd->brush->vel_ramp);
1583 else if (md->type==eModifierType_Collision) {
1586 CollisionModifierData *collmd = (CollisionModifierData*) md;
1587 // TODO: CollisionModifier should use pointcache
1588 // + have proper reset events before enabling this
1589 writestruct(wd, DATA, "MVert", collmd->numverts, collmd->x);
1590 writestruct(wd, DATA, "MVert", collmd->numverts, collmd->xnew);
1591 writestruct(wd, DATA, "MFace", collmd->numfaces, collmd->mfaces);
1594 else if (md->type==eModifierType_MeshDeform) {
1595 MeshDeformModifierData *mmd = (MeshDeformModifierData*) md;
1596 int size = mmd->dyngridsize;
1598 writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->bindinfluences);
1599 writedata(wd, DATA, sizeof(int) * (mmd->totvert + 1), mmd->bindoffsets);
1600 writedata(wd, DATA, sizeof(float) * 3 * mmd->totcagevert,
1602 writestruct(wd, DATA, "MDefCell", size*size*size, mmd->dyngrid);
1603 writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->dyninfluences);
1604 writedata(wd, DATA, sizeof(int)*mmd->totvert, mmd->dynverts);
1606 else if (md->type==eModifierType_Warp) {
1607 WarpModifierData *tmd = (WarpModifierData*) md;
1608 if (tmd->curfalloff) {
1609 write_curvemapping(wd, tmd->curfalloff);
1612 else if (md->type==eModifierType_WeightVGEdit) {
1613 WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
1615 if (wmd->cmap_curve)
1616 write_curvemapping(wd, wmd->cmap_curve);
1618 else if (md->type==eModifierType_LaplacianDeform) {
1619 LaplacianDeformModifierData *lmd = (LaplacianDeformModifierData*) md;
1621 writedata(wd, DATA, sizeof(float)*lmd->total_verts * 3, lmd->vertexco);
1623 else if (md->type == eModifierType_CorrectiveSmooth) {
1624 CorrectiveSmoothModifierData *csmd = (CorrectiveSmoothModifierData *)md;
1626 if (csmd->bind_coords) {
1627 writedata(wd, DATA, sizeof(float[3]) * csmd->bind_coords_num, csmd->bind_coords);
1633 static void write_objects(WriteData *wd, ListBase *idbase)
1639 if (ob->id.us>0 || wd->current) {
1641 writestruct(wd, ID_OB, "Object", 1, ob);
1643 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
1644 * of library blocks that implement this.*/
1645 if (ob->id.properties) IDP_WriteProperty(ob->id.properties, wd);
1647 if (ob->adt) write_animdata(wd, ob->adt);
1650 writedata(wd, DATA, sizeof(void *)*ob->totcol, ob->mat);
1651 writedata(wd, DATA, sizeof(char)*ob->totcol, ob->matbits);
1652 /* write_effects(wd, &ob->effect); */ /* not used anymore */
1653 write_properties(wd, &ob->prop);
1654 write_sensors(wd, &ob->sensors);
1655 write_controllers(wd, &ob->controllers);
1656 write_actuators(wd, &ob->actuators);
1658 if (ob->type == OB_ARMATURE) {
1659 bArmature *arm = ob->data;
1660 if (arm && ob->pose && arm->act_bone) {
1661 BLI_strncpy(ob->pose->proxy_act_bone, arm->act_bone->name, sizeof(ob->pose->proxy_act_bone));
1665 write_pose(wd, ob->pose);
1666 write_defgroups(wd, &ob->defbase);
1667 write_constraints(wd, &ob->constraints);
1668 write_motionpath(wd, ob->mpath);
1670 writestruct(wd, DATA, "PartDeflect", 1, ob->pd);
1671 writestruct(wd, DATA, "SoftBody", 1, ob->soft);
1673 write_pointcaches(wd, &ob->soft->ptcaches);
1674 writestruct(wd, DATA, "EffectorWeights", 1, ob->soft->effector_weights);
1676 writestruct(wd, DATA, "BulletSoftBody", 1, ob->bsoft);
1678 if (ob->rigidbody_object) {
1679 // TODO: if any extra data is added to handle duplis, will need separate function then
1680 writestruct(wd, DATA, "RigidBodyOb", 1, ob->rigidbody_object);
1682 if (ob->rigidbody_constraint) {
1683 writestruct(wd, DATA, "RigidBodyCon", 1, ob->rigidbody_constraint);
1686 if (ob->type == OB_EMPTY && ob->empty_drawtype == OB_EMPTY_IMAGE) {
1687 writestruct(wd, DATA, "ImageUser", 1, ob->iuser);
1690 write_particlesystems(wd, &ob->particlesystem);
1691 write_modifiers(wd, &ob->modifiers);
1693 writelist(wd, DATA, "LinkData", &ob->pc_ids);
1694 writelist(wd, DATA, "LodLevel", &ob->lodlevels);
1699 /* flush helps the compression for undo-save */
1700 mywrite(wd, MYWRITE_FLUSH, 0);
1704 static void write_vfonts(WriteData *wd, ListBase *idbase)
1711 if (vf->id.us>0 || wd->current) {
1713 writestruct(wd, ID_VF, "VFont", 1, vf);
1714 if (vf->id.properties) IDP_WriteProperty(vf->id.properties, wd);
1718 if (vf->packedfile) {
1719 pf = vf->packedfile;
1720 writestruct(wd, DATA, "PackedFile", 1, pf);
1721 writedata(wd, DATA, pf->size, pf->data);
1730 static void write_keys(WriteData *wd, ListBase *idbase)
1737 if (key->id.us>0 || wd->current) {
1739 writestruct(wd, ID_KE, "Key", 1, key);
1740 if (key->id.properties) IDP_WriteProperty(key->id.properties, wd);
1742 if (key->adt) write_animdata(wd, key->adt);
1745 kb= key->block.first;
1747 writestruct(wd, DATA, "KeyBlock", 1, kb);
1748 if (kb->data) writedata(wd, DATA, kb->totelem*key->elemsize, kb->data);
1755 /* flush helps the compression for undo-save */
1756 mywrite(wd, MYWRITE_FLUSH, 0);
1759 static void write_cameras(WriteData *wd, ListBase *idbase)
1765 if (cam->id.us>0 || wd->current) {
1767 writestruct(wd, ID_CA, "Camera", 1, cam);
1768 if (cam->id.properties) IDP_WriteProperty(cam->id.properties, wd);
1770 if (cam->adt) write_animdata(wd, cam->adt);
1777 static void write_mballs(WriteData *wd, ListBase *idbase)
1784 if (mb->id.us>0 || wd->current) {
1786 writestruct(wd, ID_MB, "MetaBall", 1, mb);
1787 if (mb->id.properties) IDP_WriteProperty(mb->id.properties, wd);
1790 writedata(wd, DATA, sizeof(void *)*mb->totcol, mb->mat);
1791 if (mb->adt) write_animdata(wd, mb->adt);
1793 ml= mb->elems.first;
1795 writestruct(wd, DATA, "MetaElem", 1, ml);
1803 static void write_curves(WriteData *wd, ListBase *idbase)
1810 if (cu->id.us>0 || wd->current) {
1812 writestruct(wd, ID_CU, "Curve", 1, cu);
1815 writedata(wd, DATA, sizeof(void *)*cu->totcol, cu->mat);
1816 if (cu->id.properties) IDP_WriteProperty(cu->id.properties, wd);
1817 if (cu->adt) write_animdata(wd, cu->adt);
1820 writedata(wd, DATA, cu->len + 1, cu->str);
1821 writestruct(wd, DATA, "CharInfo", cu->len_wchar + 1, cu->strinfo);
1822 writestruct(wd, DATA, "TextBox", cu->totbox, cu->tb);
1825 /* is also the order of reading */
1828 writestruct(wd, DATA, "Nurb", 1, nu);
1833 if (nu->type == CU_BEZIER)
1834 writestruct(wd, DATA, "BezTriple", nu->pntsu, nu->bezt);
1836 writestruct(wd, DATA, "BPoint", nu->pntsu*nu->pntsv, nu->bp);
1837 if (nu->knotsu) writedata(wd, DATA, KNOTSU(nu)*sizeof(float), nu->knotsu);
1838 if (nu->knotsv) writedata(wd, DATA, KNOTSV(nu)*sizeof(float), nu->knotsv);
1847 /* flush helps the compression for undo-save */
1848 mywrite(wd, MYWRITE_FLUSH, 0);
1851 static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
1856 /* Write the dvert list */
1857 writestruct(wd, DATA, "MDeformVert", count, dvlist);
1859 /* Write deformation data for each dvert */
1860 for (i=0; i<count; i++) {
1862 writestruct(wd, DATA, "MDeformWeight", dvlist[i].totweight, dvlist[i].dw);
1867 static void write_mdisps(WriteData *wd, int count, MDisps *mdlist, int external)
1872 writestruct(wd, DATA, "MDisps", count, mdlist);
1873 for (i = 0; i < count; ++i) {
1874 MDisps *md = &mdlist[i];
1877 writedata(wd, DATA, sizeof(float) * 3 * md->totdisp, md->disps);
1881 writedata(wd, DATA, BLI_BITMAP_SIZE(md->totdisp), md->hidden);
1886 static void write_grid_paint_mask(WriteData *wd, int count, GridPaintMask *grid_paint_mask)
1888 if (grid_paint_mask) {
1891 writestruct(wd, DATA, "GridPaintMask", count, grid_paint_mask);
1892 for (i = 0; i < count; ++i) {
1893 GridPaintMask *gpm = &grid_paint_mask[i];
1895 const int gridsize = BKE_ccg_gridsize(gpm->level);
1897 sizeof(*gpm->data) * gridsize * gridsize,
1904 static void write_customdata(WriteData *wd, ID *id, int count, CustomData *data, int partial_type, int partial_count)
1908 int nofree_buff[128];
1911 /* write external customdata (not for undo) */
1912 if (data->external && !wd->current)
1913 CustomData_external_write(data, id, CD_MASK_MESH, count, 0);
1915 if (data->totlayer > ARRAY_SIZE(nofree_buff)) {
1916 nofree = MEM_mallocN(sizeof(*nofree) * (size_t)data->totlayer, __func__);
1919 nofree = nofree_buff;
1922 for (i = 0; i < data->totlayer; i++) {
1923 nofree[i] = (data->layers[i].flag & CD_FLAG_NOFREE);
1924 data->layers[i].flag &= ~CD_FLAG_NOFREE;
1927 writestruct(wd, DATA, "CustomDataLayer", data->maxlayer, data->layers);
1929 for (i = 0; i < data->totlayer; i++) {
1930 data->layers[i].flag |= nofree[i];
1933 for (i = 0; i < data->totlayer; i++) {
1934 CustomDataLayer *layer= &data->layers[i];
1935 const char *structname;
1936 int structnum, datasize;
1938 if (layer->type == CD_MDEFORMVERT) {
1939 /* layer types that allocate own memory need special handling */
1940 write_dverts(wd, count, layer->data);
1942 else if (layer->type == CD_MDISPS) {
1943 write_mdisps(wd, count, layer->data, layer->flag & CD_FLAG_EXTERNAL);
1945 else if (layer->type == CD_PAINT_MASK) {
1946 const float *layer_data = layer->data;
1947 writedata(wd, DATA, sizeof(*layer_data) * count, layer_data);
1949 else if (layer->type == CD_GRID_PAINT_MASK) {
1950 write_grid_paint_mask(wd, count, layer->data);
1953 CustomData_file_write_info(layer->type, &structname, &structnum);
1955 /* when using partial visibility, the MEdge and MFace layers
1956 * are smaller than the original, so their type and count is
1957 * passed to make this work */
1958 if (layer->type != partial_type) datasize= structnum*count;
1959 else datasize= structnum*partial_count;
1961 writestruct(wd, DATA, structname, datasize, layer->data);
1964 printf("%s error: layer '%s':%d - can't be written to file\n",
1965 __func__, structname, layer->type);
1971 writestruct(wd, DATA, "CustomDataExternal", 1, data->external);
1973 if (nofree != nofree_buff) {
1978 static void write_meshes(WriteData *wd, ListBase *idbase)
1981 int save_for_old_blender= 0;
1983 #ifdef USE_BMESH_SAVE_AS_COMPAT
1984 save_for_old_blender = wd->use_mesh_compat; /* option to save with older mesh format */
1987 mesh= idbase->first;
1989 if (mesh->id.us>0 || wd->current) {
1991 if (!save_for_old_blender) {
1992 /* write a copy of the mesh, don't modify in place because it is
1993 * not thread safe for threaded renders that are reading this */
1994 Mesh *old_mesh = mesh;
1995 Mesh copy_mesh = *mesh;
1998 #ifdef USE_BMESH_SAVE_WITHOUT_MFACE
1999 /* cache only - don't write */
2002 memset(&mesh->fdata, 0, sizeof(mesh->fdata));
2003 #endif /* USE_BMESH_SAVE_WITHOUT_MFACE */
2005 /* Bummer! We need to do the copy *before* writing mesh's struct itself,
2006 * because we eliminate NO_COPY & TEMPORARY layers here, which means
2007 * **number of layers (data.totlayer) may be smaller!**
2008 * If we do not do that, we can get crash by buffer-overflow on reading, see T44461. */
2009 CustomData_copy(&old_mesh->vdata, &mesh->vdata, CD_MASK_EVERYTHING, CD_REFERENCE, mesh->totvert);
2010 CustomData_copy(&old_mesh->edata, &mesh->edata, CD_MASK_EVERYTHING, CD_REFERENCE, mesh->totedge);
2011 #ifndef USE_BMESH_SAVE_WITHOUT_MFACE /* Do not copy org fdata in this case!!! */
2012 CustomData_copy(&old_mesh->fdata, &mesh->fdata, CD_MASK_EVERYTHING, CD_REFERENCE, mesh->totface);
2014 CustomData_copy(&old_mesh->ldata, &mesh->ldata, CD_MASK_EVERYTHING, CD_REFERENCE, mesh->totloop);
2015 CustomData_copy(&old_mesh->pdata, &mesh->pdata, CD_MASK_EVERYTHING, CD_REFERENCE, mesh->totpoly);
2017 writestruct_at_address(wd, ID_ME, "Mesh", 1, old_mesh, mesh);
2020 if (mesh->id.properties) IDP_WriteProperty(mesh->id.properties, wd);
2021 if (mesh->adt) write_animdata(wd, mesh->adt);
2023 writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
2024 writedata(wd, DATA, sizeof(MSelect) * mesh->totselect, mesh->mselect);
2026 write_customdata(wd, &mesh->id, mesh->totvert, &mesh->vdata, -1, 0);
2027 write_customdata(wd, &mesh->id, mesh->totedge, &mesh->edata, -1, 0);
2028 /* fdata is really a dummy - written so slots align */
2029 write_customdata(wd, &mesh->id, mesh->totface, &mesh->fdata, -1, 0);
2030 write_customdata(wd, &mesh->id, mesh->totloop, &mesh->ldata, -1, 0);
2031 write_customdata(wd, &mesh->id, mesh->totpoly, &mesh->pdata, -1, 0);
2033 CustomData_free(&mesh->vdata, mesh->totvert);
2034 CustomData_free(&mesh->edata, mesh->totedge);
2035 #ifndef USE_BMESH_SAVE_WITHOUT_MFACE
2036 CustomData_free(&mesh->fdata, mesh->totface);
2038 CustomData_free(&mesh->ldata, mesh->totloop);
2039 CustomData_free(&mesh->pdata, mesh->totpoly);
2041 /* restore pointer */
2046 #ifdef USE_BMESH_SAVE_AS_COMPAT
2047 /* write a copy of the mesh, don't modify in place because it is
2048 * not thread safe for threaded renders that are reading this */
2049 Mesh *old_mesh = mesh;
2050 Mesh copy_mesh = *mesh;
2058 CustomData_reset(&mesh->fdata);
2059 CustomData_reset(&mesh->pdata);
2060 CustomData_reset(&mesh->ldata);
2061 mesh->edit_btmesh = NULL;
2063 /* now fill in polys to mfaces */
2064 mesh->totface = BKE_mesh_mpoly_to_mface(&mesh->fdata, &old_mesh->ldata, &old_mesh->pdata,
2065 mesh->totface, old_mesh->totloop, old_mesh->totpoly);
2067 BKE_mesh_update_customdata_pointers(mesh, false);
2069 /* See comment above. Note that loop/poly data are ignored here, and face ones are already handled. */
2070 CustomData_copy(&old_mesh->vdata, &mesh->vdata, CD_MASK_EVERYTHING, CD_REFERENCE, mesh->totvert);
2071 CustomData_copy(&old_mesh->edata, &mesh->edata, CD_MASK_EVERYTHING, CD_REFERENCE, mesh->totedge);
2073 writestruct_at_address(wd, ID_ME, "Mesh", 1, old_mesh, mesh);
2076 if (mesh->id.properties) IDP_WriteProperty(mesh->id.properties, wd);
2077 if (mesh->adt) write_animdata(wd, mesh->adt);
2079 writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
2080 /* writedata(wd, DATA, sizeof(MSelect) * mesh->totselect, mesh->mselect); */ /* pre-bmesh NULL's */
2082 write_customdata(wd, &mesh->id, mesh->totvert, &mesh->vdata, -1, 0);
2083 write_customdata(wd, &mesh->id, mesh->totedge, &mesh->edata, -1, 0);
2084 write_customdata(wd, &mesh->id, mesh->totface, &mesh->fdata, -1, 0);
2085 /* harmless for older blender versioins but _not_ writing these keeps file size down */
2087 write_customdata(wd, &mesh->id, mesh->totloop, &mesh->ldata, -1, 0);
2088 write_customdata(wd, &mesh->id, mesh->totpoly, &mesh->pdata, -1, 0);
2091 CustomData_free(&mesh->vdata, mesh->totvert);
2092 CustomData_free(&mesh->edata, mesh->totedge);
2093 CustomData_free(&mesh->fdata, mesh->totface);
2095 /* restore pointer */
2097 #endif /* USE_BMESH_SAVE_AS_COMPAT */
2100 mesh= mesh->id.next;
2104 static void write_lattices(WriteData *wd, ListBase *idbase)
2110 if (lt->id.us>0 || wd->current) {
2112 writestruct(wd, ID_LT, "Lattice", 1, lt);
2113 if (lt->id.properties) IDP_WriteProperty(lt->id.properties, wd);
2115 /* write animdata */
2116 if (lt->adt) write_animdata(wd, lt->adt);
2119 writestruct(wd, DATA, "BPoint", lt->pntsu*lt->pntsv*lt->pntsw, lt->def);
2121 write_dverts(wd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
2128 static void write_previews(WriteData *wd, PreviewImage *prv)
2130 /* Never write previews in undo steps! */
2131 if (prv && !wd->current) {
2132 short w = prv->w[1];
2133 short h = prv->h[1];
2134 unsigned int *rect = prv->rect[1];
2135 /* don't write out large previews if not requested */
2136 if (!(U.flag & USER_SAVE_PREVIEWS)) {
2139 prv->rect[1] = NULL;
2141 writestruct(wd, DATA, "PreviewImage", 1, prv);
2142 if (prv->rect[0]) writedata(wd, DATA, prv->w[0]*prv->h[0]*sizeof(unsigned int), prv->rect[0]);
2143 if (prv->rect[1]) writedata(wd, DATA, prv->w[1]*prv->h[1]*sizeof(unsigned int), prv->rect[1]);
2145 /* restore preview, we still want to keep it in memory even if not saved to file */
2146 if (!(U.flag & USER_SAVE_PREVIEWS) ) {
2149 prv->rect[1] = rect;
2154 static void write_images(WriteData *wd, ListBase *idbase)
2159 ImagePackedFile *imapf;
2163 if (ima->id.us>0 || wd->current) {
2164 /* Some trickery to keep forward compatibility of packed images. */
2165 BLI_assert(ima->packedfile == NULL);
2166 if (ima->packedfiles.first != NULL) {
2167 imapf = ima->packedfiles.first;
2168 ima->packedfile = imapf->packedfile;
2172 writestruct(wd, ID_IM, "Image", 1, ima);
2173 if (ima->id.properties) IDP_WriteProperty(ima->id.properties, wd);
2175 for (imapf = ima->packedfiles.first; imapf; imapf = imapf->next) {
2176 writestruct(wd, DATA, "ImagePackedFile", 1, imapf);
2177 if (imapf->packedfile) {
2178 pf = imapf->packedfile;
2179 writestruct(wd, DATA, "PackedFile", 1, pf);
2180 writedata(wd, DATA, pf->size, pf->data);
2184 write_previews(wd, ima->preview);
2186 for (iv = ima->views.first; iv; iv = iv->next)
2187 writestruct(wd, DATA, "ImageView", 1, iv);
2188 writestruct(wd, DATA, "Stereo3dFormat", 1, ima->stereo3d_format);
2190 ima->packedfile = NULL;
2194 /* flush helps the compression for undo-save */
2195 mywrite(wd, MYWRITE_FLUSH, 0);
2198 static void write_textures(WriteData *wd, ListBase *idbase)
2204 if (tex->id.us>0 || wd->current) {
2206 writestruct(wd, ID_TE, "Tex", 1, tex);
2207 if (tex->id.properties) IDP_WriteProperty(tex->id.properties, wd);
2209 if (tex->adt) write_animdata(wd, tex->adt);
2212 if (tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
2213 if (tex->type == TEX_ENVMAP && tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
2214 if (tex->type == TEX_POINTDENSITY && tex->pd) {
2215 writestruct(wd, DATA, "PointDensity", 1, tex->pd);
2216 if (tex->pd->coba) writestruct(wd, DATA, "ColorBand", 1, tex->pd->coba);
2217 if (tex->pd->falloff_curve) write_curvemapping(wd, tex->pd->falloff_curve);
2219 if (tex->type == TEX_VOXELDATA) writestruct(wd, DATA, "VoxelData", 1, tex->vd);
2220 if (tex->type == TEX_OCEAN && tex->ot) writestruct(wd, DATA, "OceanTex", 1, tex->ot);
2222 /* nodetree is integral part of texture, no libdata */
2223 if (tex->nodetree) {
2224 writestruct(wd, DATA, "bNodeTree", 1, tex->nodetree);
2225 write_nodetree(wd, tex->nodetree);
2228 write_previews(wd, tex->preview);
2233 /* flush helps the compression for undo-save */
2234 mywrite(wd, MYWRITE_FLUSH, 0);
2237 static void write_materials(WriteData *wd, ListBase *idbase)
2244 if (ma->id.us>0 || wd->current) {
2246 writestruct(wd, ID_MA, "Material", 1, ma);
2248 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
2249 * of library blocks that implement this.*/
2250 /* manually set head group property to IDP_GROUP, just in case it hadn't been
2252 if (ma->id.properties) IDP_WriteProperty(ma->id.properties, wd);
2254 if (ma->adt) write_animdata(wd, ma->adt);
2256 for (a=0; a<MAX_MTEX; a++) {
2257 if (ma->mtex[a]) writestruct(wd, DATA, "MTex", 1, ma->mtex[a]);
2260 if (ma->ramp_col) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_col);
2261 if (ma->ramp_spec) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_spec);
2263 /* nodetree is integral part of material, no libdata */
2265 writestruct(wd, DATA, "bNodeTree", 1, ma->nodetree);
2266 write_nodetree(wd, ma->nodetree);
2269 write_previews(wd, ma->preview);
2275 static void write_worlds(WriteData *wd, ListBase *idbase)
2280 wrld= idbase->first;
2282 if (wrld->id.us>0 || wd->current) {
2284 writestruct(wd, ID_WO, "World", 1, wrld);
2285 if (wrld->id.properties) IDP_WriteProperty(wrld->id.properties, wd);
2287 if (wrld->adt) write_animdata(wd, wrld->adt);
2289 for (a=0; a<MAX_MTEX; a++) {
2290 if (wrld->mtex[a]) writestruct(wd, DATA, "MTex", 1, wrld->mtex[a]);
2293 /* nodetree is integral part of world, no libdata */
2294 if (wrld->nodetree) {
2295 writestruct(wd, DATA, "bNodeTree", 1, wrld->nodetree);
2296 write_nodetree(wd, wrld->nodetree);
2299 write_previews(wd, wrld->preview);
2301 wrld= wrld->id.next;
2305 static void write_lamps(WriteData *wd, ListBase *idbase)
2312 if (la->id.us>0 || wd->current) {
2314 writestruct(wd, ID_LA, "Lamp", 1, la);
2315 if (la->id.properties) IDP_WriteProperty(la->id.properties, wd);
2317 if (la->adt) write_animdata(wd, la->adt);
2320 for (a=0; a<MAX_MTEX; a++) {
2321 if (la->mtex[a]) writestruct(wd, DATA, "MTex", 1, la->mtex[a]);
2325 write_curvemapping(wd, la->curfalloff);
2327 /* nodetree is integral part of lamps, no libdata */
2329 writestruct(wd, DATA, "bNodeTree", 1, la->nodetree);
2330 write_nodetree(wd, la->nodetree);
2333 write_previews(wd, la->preview);
2340 static void write_sequence_modifiers(WriteData *wd, ListBase *modbase)
2342 SequenceModifierData *smd;
2344 for (smd = modbase->first; smd; smd = smd->next) {
2345 const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
2348 writestruct(wd, DATA, smti->struct_name, 1, smd);
2350 if (smd->type == seqModifierType_Curves) {
2351 CurvesModifierData *cmd = (CurvesModifierData *) smd;
2353 write_curvemapping(wd, &cmd->curve_mapping);
2355 else if (smd->type == seqModifierType_HueCorrect) {
2356 HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
2358 write_curvemapping(wd, &hcmd->curve_mapping);
2362 writestruct(wd, DATA, "SequenceModifierData", 1, smd);
2367 static void write_view_settings(WriteData *wd, ColorManagedViewSettings *view_settings)
2369 if (view_settings->curve_mapping) {
2370 write_curvemapping(wd, view_settings->curve_mapping);
2374 static void write_paint(WriteData *wd, Paint *p)
2376 if (p->cavity_curve)
2377 write_curvemapping(wd, p->cavity_curve);
2380 static void write_scenes(WriteData *wd, ListBase *scebase)
2389 TransformOrientation *ts;
2390 SceneRenderLayer *srl;
2391 SceneRenderView *srv;
2393 FreestyleModuleConfig *fmc;
2394 FreestyleLineSet *fls;
2396 sce= scebase->first;
2399 writestruct(wd, ID_SCE, "Scene", 1, sce);
2400 if (sce->id.properties) IDP_WriteProperty(sce->id.properties, wd);
2402 if (sce->adt) write_animdata(wd, sce->adt);
2403 write_keyingsets(wd, &sce->keyingsets);
2406 base= sce->base.first;
2408 writestruct(wd, DATA, "Base", 1, base);
2412 tos = sce->toolsettings;
2413 writestruct(wd, DATA, "ToolSettings", 1, tos);
2415 writestruct(wd, DATA, "VPaint", 1, tos->vpaint);
2416 write_paint (wd, &tos->vpaint->paint);
2419 writestruct(wd, DATA, "VPaint", 1, tos->wpaint);
2420 write_paint (wd, &tos->wpaint->paint);
2423 writestruct(wd, DATA, "Sculpt", 1, tos->sculpt);
2424 write_paint (wd, &tos->sculpt->paint);
2426 if (tos->uvsculpt) {
2427 writestruct(wd, DATA, "UvSculpt", 1, tos->uvsculpt);
2428 write_paint (wd, &tos->uvsculpt->paint);
2431 write_paint(wd, &tos->imapaint.paint);
2435 writestruct(wd, DATA, "Editing", 1, ed);
2437 /* reset write flags too */
2441 if (seq->strip) seq->strip->done = false;
2442 writestruct(wd, DATA, "Sequence", 1, seq);
2448 if (seq->strip && seq->strip->done==0) {
2449 /* write strip with 'done' at 0 because readfile */
2451 if (seq->effectdata) {
2452 switch (seq->type) {
2453 case SEQ_TYPE_COLOR:
2454 writestruct(wd, DATA, "SolidColorVars", 1, seq->effectdata);
2456 case SEQ_TYPE_SPEED:
2457 writestruct(wd, DATA, "SpeedControlVars", 1, seq->effectdata);
2460 writestruct(wd, DATA, "WipeVars", 1, seq->effectdata);
2463 writestruct(wd, DATA, "GlowVars", 1, seq->effectdata);
2465 case SEQ_TYPE_TRANSFORM:
2466 writestruct(wd, DATA, "TransformVars", 1, seq->effectdata);
2468 case SEQ_TYPE_GAUSSIAN_BLUR:
2469 writestruct(wd, DATA, "GaussianBlurVars", 1, seq->effectdata);
2474 writestruct(wd, DATA, "Stereo3dFormat", 1, seq->stereo3d_format);
2477 writestruct(wd, DATA, "Strip", 1, strip);
2478 if (seq->flag & SEQ_USE_CROP && strip->crop) {
2479 writestruct(wd, DATA, "StripCrop", 1, strip->crop);
2481 if (seq->flag & SEQ_USE_TRANSFORM && strip->transform) {
2482 writestruct(wd, DATA, "StripTransform", 1, strip->transform);
2484 if (seq->flag & SEQ_USE_PROXY && strip->proxy) {
2485 writestruct(wd, DATA, "StripProxy", 1, strip->proxy);
2487 if (seq->type==SEQ_TYPE_IMAGE)
2488 writestruct(wd, DATA, "StripElem", MEM_allocN_len(strip->stripdata) / sizeof(struct StripElem), strip->stripdata);
2489 else if (seq->type==SEQ_TYPE_MOVIE || seq->type==SEQ_TYPE_SOUND_RAM || seq->type == SEQ_TYPE_SOUND_HD)
2490 writestruct(wd, DATA, "StripElem", 1, strip->stripdata);
2496 IDP_WriteProperty(seq->prop, wd);
2499 write_sequence_modifiers(wd, &seq->modifiers);
2503 /* new; meta stack too, even when its nasty restore code */
2504 for (ms= ed->metastack.first; ms; ms= ms->next) {
2505 writestruct(wd, DATA, "MetaStack", 1, ms);
2509 if (sce->r.avicodecdata) {
2510 writestruct(wd, DATA, "AviCodecData", 1, sce->r.avicodecdata);
2511 if (sce->r.avicodecdata->lpFormat) writedata(wd, DATA, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat);
2512 if (sce->r.avicodecdata->lpParms) writedata(wd, DATA, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms);
2515 if (sce->r.qtcodecdata) {
2516 writestruct(wd, DATA, "QuicktimeCodecData", 1, sce->r.qtcodecdata);
2517 if (sce->r.qtcodecdata->cdParms) writedata(wd, DATA, sce->r.qtcodecdata->cdSize, sce->r.qtcodecdata->cdParms);
2519 if (sce->r.ffcodecdata.properties) {
2520 IDP_WriteProperty(sce->r.ffcodecdata.properties, wd);
2523 /* writing dynamic list of TimeMarkers to the blend file */
2524 for (marker= sce->markers.first; marker; marker= marker->next)
2525 writestruct(wd, DATA, "TimeMarker", 1, marker);
2527 /* writing dynamic list of TransformOrientations to the blend file */
2528 for (ts = sce->transform_spaces.first; ts; ts = ts->next)
2529 writestruct(wd, DATA, "TransformOrientation", 1, ts);
2531 for (srl = sce->r.layers.first; srl; srl = srl->next) {
2532 writestruct(wd, DATA, "SceneRenderLayer", 1, srl);
2533 for (fmc = srl->freestyleConfig.modules.first; fmc; fmc = fmc->next) {
2534 writestruct(wd, DATA, "FreestyleModuleConfig", 1, fmc);
2536 for (fls = srl->freestyleConfig.linesets.first; fls; fls = fls->next) {
2537 writestruct(wd, DATA, "FreestyleLineSet", 1, fls);
2541 /* writing MultiView to the blend file */
2542 for (srv = sce->r.views.first; srv; srv = srv->next)
2543 writestruct(wd, DATA, "SceneRenderView", 1, srv);
2545 if (sce->nodetree) {
2546 writestruct(wd, DATA, "bNodeTree", 1, sce->nodetree);
2547 write_nodetree(wd, sce->nodetree);
2550 write_view_settings(wd, &sce->view_settings);
2552 /* writing RigidBodyWorld data to the blend file */
2553 if (sce->rigidbody_world) {
2554 writestruct(wd, DATA, "RigidBodyWorld", 1, sce->rigidbody_world);
2555 writestruct(wd, DATA, "EffectorWeights", 1, sce->rigidbody_world->effector_weights);
2556 write_pointcaches(wd, &(sce->rigidbody_world->ptcaches));
2561 /* flush helps the compression for undo-save */
2562 mywrite(wd, MYWRITE_FLUSH, 0);
2565 static void write_gpencils(WriteData *wd, ListBase *lb)
2572 for (gpd= lb->first; gpd; gpd= gpd->id.next) {
2573 if (gpd->id.us>0 || wd->current) {
2574 /* write gpd data block to file */
2575 writestruct(wd, ID_GD, "bGPdata", 1, gpd);
2577 if (gpd->adt) write_animdata(wd, gpd->adt);
2579 /* write grease-pencil layers to file */
2580 writelist(wd, DATA, "bGPDlayer", &gpd->layers);
2581 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
2583 /* write this layer's frames to file */
2584 writelist(wd, DATA, "bGPDframe", &gpl->frames);
2585 for (gpf= gpl->frames.first; gpf; gpf= gpf->next) {
2588 writelist(wd, DATA, "bGPDstroke", &gpf->strokes);
2589 for (gps= gpf->strokes.first; gps; gps= gps->next) {
2590 writestruct(wd, DATA, "bGPDspoint", gps->totpoints, gps->points);
2598 static void write_windowmanagers(WriteData *wd, ListBase *lb)
2600 wmWindowManager *wm;
2603 for (wm= lb->first; wm; wm= wm->id.next) {
2604 writestruct(wd, ID_WM, "wmWindowManager", 1, wm);
2606 for (win= wm->windows.first; win; win= win->next) {
2607 writestruct(wd, DATA, "wmWindow", 1, win);
2608 writestruct(wd, DATA, "Stereo3dFormat", 1, win->stereo3d_format);
2613 static void write_region(WriteData *wd, ARegion *ar, int spacetype)
2615 writestruct(wd, DATA, "ARegion", 1, ar);
2617 if (ar->regiondata) {
2618 switch (spacetype) {
2620 if (ar->regiontype==RGN_TYPE_WINDOW) {
2621 RegionView3D *rv3d= ar->regiondata;
2622 writestruct(wd, DATA, "RegionView3D", 1, rv3d);
2625 writestruct(wd, DATA, "RegionView3D", 1, rv3d->localvd);
2627 writestruct(wd, DATA, "BoundBox", 1, rv3d->clipbb);
2631 printf("regiondata write missing!\n");
2634 printf("regiondata write missing!\n");
2639 static void write_uilist(WriteData *wd, uiList *ui_list)
2641 writestruct(wd, DATA, "uiList", 1, ui_list);
2643 if (ui_list->properties) {
2644 IDP_WriteProperty(ui_list->properties, wd);
2648 static void write_soops(WriteData *wd, SpaceOops *so, LinkNode **tmp_mem_list)
2650 BLI_mempool *ts = so->treestore;
2653 int elems = BLI_mempool_count(ts);
2654 /* linearize mempool to array */
2655 TreeStoreElem *data = elems ? BLI_mempool_as_arrayN(ts, "TreeStoreElem") : NULL;
2658 TreeStore *ts_flat = MEM_callocN(sizeof(TreeStore), "TreeStore");
2660 ts_flat->usedelem = elems;
2661 ts_flat->totelem = elems;
2662 ts_flat->data = data;
2664 /* temporarily replace mempool-treestore by flat-treestore */
2665 so->treestore = (BLI_mempool *)ts_flat;
2666 writestruct(wd, DATA, "SpaceOops", 1, so);
2668 writestruct(wd, DATA, "TreeStore", 1, ts_flat);
2669 writestruct(wd, DATA, "TreeStoreElem", elems, data);
2671 /* we do not free the pointers immediately, because if we have multiple
2672 * outliners in a screen we might get the same address on the next
2673 * malloc, which makes the address no longer unique and so invalid for
2674 * lookups on file read, causing crashes or double frees */
2675 BLI_linklist_prepend(tmp_mem_list, ts_flat);
2676 BLI_linklist_prepend(tmp_mem_list, data);
2679 so->treestore = NULL;
2680 writestruct(wd, DATA, "SpaceOops", 1, so);
2683 /* restore old treestore */
2687 writestruct(wd, DATA, "SpaceOops", 1, so);
2691 static void write_screens(WriteData *wd, ListBase *scrbase)
2697 LinkNode *tmp_mem_list = NULL;
2703 /* in 2.50+ files, the file identifier for screens is patched, forward compatibility */
2704 writestruct(wd, ID_SCRN, "Screen", 1, sc);
2705 if (sc->id.properties)
2706 IDP_WriteProperty(sc->id.properties, wd);
2709 for (sv= sc->vertbase.first; sv; sv= sv->next)
2710 writestruct(wd, DATA, "ScrVert", 1, sv);
2712 for (se= sc->edgebase.first; se; se= se->next)
2713 writestruct(wd, DATA, "ScrEdge", 1, se);
2715 for (sa= sc->areabase.first; sa; sa= sa->next) {
2719 uiPreview *ui_preview;
2720 PanelCategoryStack *pc_act;
2723 writestruct(wd, DATA, "ScrArea", 1, sa);
2725 for (ar= sa->regionbase.first; ar; ar= ar->next) {
2726 write_region(wd, ar, sa->spacetype);
2728 for (pa= ar->panels.first; pa; pa= pa->next)
2729 writestruct(wd, DATA, "Panel", 1, pa);
2731 for (pc_act = ar->panels_category_active.first; pc_act; pc_act = pc_act->next)
2732 writestruct(wd, DATA, "PanelCategoryStack", 1, pc_act);
2734 for (ui_list = ar->ui_lists.first; ui_list; ui_list = ui_list->next)
2735 write_uilist(wd, ui_list);
2737 for (ui_preview = ar->ui_previews.first; ui_preview; ui_preview = ui_preview->next)
2738 writestruct(wd, DATA, "uiPreview", 1, ui_preview);
2741 sl= sa->spacedata.first;
2743 for (ar= sl->regionbase.first; ar; ar= ar->next)
2744 write_region(wd, ar, sl->spacetype);
2746 if (sl->spacetype==SPACE_VIEW3D) {
2747 View3D *v3d= (View3D *) sl;
2749 writestruct(wd, DATA, "View3D", 1, v3d);
2750 for (bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
2751 writestruct(wd, DATA, "BGpic", 1, bgpic);
2752 if (v3d->localvd) writestruct(wd, DATA, "View3D", 1, v3d->localvd);
2754 if (v3d->fx_settings.ssao)
2755 writestruct(wd, DATA, "GPUSSAOSettings", 1, v3d->fx_settings.ssao);
2756 if (v3d->fx_settings.dof)
2757 writestruct(wd, DATA, "GPUDOFSettings", 1, v3d->fx_settings.dof);
2759 else if (sl->spacetype==SPACE_IPO) {
2760 SpaceIpo *sipo= (SpaceIpo *)sl;
2761 ListBase tmpGhosts = sipo->ghostCurves;
2763 /* temporarily disable ghost curves when saving */
2764 sipo->ghostCurves.first= sipo->ghostCurves.last= NULL;
2766 writestruct(wd, DATA, "SpaceIpo", 1, sl);
2767 if (sipo->ads) writestruct(wd, DATA, "bDopeSheet", 1, sipo->ads);
2769 /* reenable ghost curves */
2770 sipo->ghostCurves= tmpGhosts;
2772 else if (sl->spacetype==SPACE_BUTS) {
2773 writestruct(wd, DATA, "SpaceButs", 1, sl);
2775 else if (sl->spacetype==SPACE_FILE) {
2776 SpaceFile *sfile= (SpaceFile *)sl;
2778 writestruct(wd, DATA, "SpaceFile", 1, sl);
2780 writestruct(wd, DATA, "FileSelectParams", 1, sfile->params);
2782 else if (sl->spacetype==SPACE_SEQ) {
2783 writestruct(wd, DATA, "SpaceSeq", 1, sl);
2785 else if (sl->spacetype==SPACE_OUTLINER) {
2786 SpaceOops *so= (SpaceOops *)sl;
2787 write_soops(wd, so, &tmp_mem_list);
2789 else if (sl->spacetype==SPACE_IMAGE) {
2790 SpaceImage *sima= (SpaceImage *)sl;
2792 writestruct(wd, DATA, "SpaceImage", 1, sl);
2794 write_curvemapping(wd, sima->cumap);
2796 else if (sl->spacetype==SPACE_TEXT) {
2797 writestruct(wd, DATA, "SpaceText", 1, sl);
2799 else if (sl->spacetype==SPACE_SCRIPT) {
2800 SpaceScript *scr = (SpaceScript*)sl;
2801 scr->but_refs = NULL;
2802 writestruct(wd, DATA, "SpaceScript", 1, sl);
2804 else if (sl->spacetype==SPACE_ACTION) {
2805 writestruct(wd, DATA, "SpaceAction", 1, sl);
2807 else if (sl->spacetype==SPACE_NLA) {
2808 SpaceNla *snla= (SpaceNla *)sl;
2810 writestruct(wd, DATA, "SpaceNla", 1, snla);
2811 if (snla->ads) writestruct(wd, DATA, "bDopeSheet", 1, snla->ads);
2813 else if (sl->spacetype==SPACE_TIME) {
2814 writestruct(wd, DATA, "SpaceTime", 1, sl);
2816 else if (sl->spacetype==SPACE_NODE) {
2817 SpaceNode *snode = (SpaceNode *)sl;
2818 bNodeTreePath *path;
2819 writestruct(wd, DATA, "SpaceNode", 1, snode);
2821 for (path=snode->treepath.first; path; path=path->next)
2822 writestruct(wd, DATA, "bNodeTreePath", 1, path);
2824 else if (sl->spacetype==SPACE_LOGIC) {
2825 writestruct(wd, DATA, "SpaceLogic", 1, sl);
2827 else if (sl->spacetype==SPACE_CONSOLE) {
2828 SpaceConsole *con = (SpaceConsole*)sl;
2831 for (cl=con->history.first; cl; cl=cl->next) {
2832 /* 'len_alloc' is invalid on write, set from 'len' on read */
2833 writestruct(wd, DATA, "ConsoleLine", 1, cl);
2834 writedata(wd, DATA, cl->len+1, cl->line);
2836 writestruct(wd, DATA, "SpaceConsole", 1, sl);
2839 else if (sl->spacetype==SPACE_USERPREF) {
2840 writestruct(wd, DATA, "SpaceUserPref", 1, sl);
2842 else if (sl->spacetype==SPACE_CLIP) {
2843 writestruct(wd, DATA, "SpaceClip", 1, sl);
2845 else if (sl->spacetype == SPACE_INFO) {
2846 writestruct(wd, DATA, "SpaceInfo", 1, sl);
2856 BLI_linklist_freeN(tmp_mem_list);
2858 /* flush helps the compression for undo-save */
2859 mywrite(wd, MYWRITE_FLUSH, 0);
2862 static void write_libraries(WriteData *wd, Main *main)
2864 ListBase *lbarray[MAX_LIBARRAY];
2869 for (; main; main= main->next) {
2871 a=tot= set_listbasepointers(main, lbarray);
2873 /* test: is lib being used */
2874 if (main->curlib && main->curlib->packedfile)
2879 for (id= lbarray[tot]->first; id; id= id->next) {
2880 if (id->us>0 && (id->flag & LIB_EXTERN)) {
2885 if (found_one) break;
2889 /* to be able to restore quit.blend and temp saves, the packed blend has to be in undo buffers... */
2890 /* XXX needs rethink, just like save UI in undo files now - would be nice to append things only for the]
2891 * quit.blend and temp saves */
2893 writestruct(wd, ID_LI, "Library", 1, main->curlib);
2895 if (main->curlib->packedfile) {
2896 PackedFile *pf = main->curlib->packedfile;
2897 writestruct(wd, DATA, "PackedFile", 1, pf);
2898 writedata(wd, DATA, pf->size, pf->data);
2899 if (wd->current == NULL)
2900 printf("write packed .blend: %s\n", main->curlib->name);
2904 for (id= lbarray[a]->first; id; id= id->next) {
2905 if (id->us>0 && (id->flag & LIB_EXTERN)) {
2906 writestruct(wd, ID_ID, "ID", 1, id);
2914 static void write_bone(WriteData *wd, Bone *bone)
2918 // PATCH for upward compatibility after 2.37+ armature recode
2919 bone->size[0] = bone->size[1] = bone->size[2] = 1.0f;
2922 writestruct(wd, DATA, "Bone", 1, bone);
2924 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
2925 * of library blocks that implement this.*/
2927 IDP_WriteProperty(bone->prop, wd);
2930 cbone= bone->childbase.first;
2932 write_bone(wd, cbone);
2937 static void write_armatures(WriteData *wd, ListBase *idbase)
2944 if (arm->id.us>0 || wd->current) {
2945 writestruct(wd, ID_AR, "bArmature", 1, arm);
2946 if (arm->id.properties) IDP_WriteProperty(arm->id.properties, wd);
2948 if (arm->adt) write_animdata(wd, arm->adt);
2951 bone= arm->bonebase.first;
2953 write_bone(wd, bone);
2960 /* flush helps the compression for undo-save */
2961 mywrite(wd, MYWRITE_FLUSH, 0);