4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/blenloader/intern/writefile.c
35 FILEFORMAT: IFF-style structure (but not IFF compatible!)
38 BLENDER_V100 12 bytes (versie 1.00)
39 V = big endian, v = little endian
40 _ = 4 byte pointer, - = 8 byte pointer
42 datablocks: also see struct BHead
44 <bh.len> int, len data after BHead
45 <bh.old> void, old pointer
47 <bh.nr> int, in case of array: amount of structs
52 Almost all data in Blender are structures. Each struct saved
53 gets a BHead header. With BHead the struct can be linked again
54 and compared with StructDNA .
58 Preferred writing order: (not really a must, but why would you do it random?)
59 Any case: direct data is ALWAYS after the lib block
64 - write associated direct data
69 - write the ID of LibBlock
70 - write TEST (128x128, blend file preview, optional)
71 - write FileGlobal (some global vars)
73 - write USER if filename is ~/X.XX/config/startup.blend
90 #include <process.h> // for getpid
91 #include "BLI_winstuff.h"
94 #include "DNA_anim_types.h"
95 #include "DNA_armature_types.h"
96 #include "DNA_actuator_types.h"
97 #include "DNA_brush_types.h"
98 #include "DNA_camera_types.h"
99 #include "DNA_cloth_types.h"
100 #include "DNA_constraint_types.h"
101 #include "DNA_controller_types.h"
102 #include "DNA_genfile.h"
103 #include "DNA_group_types.h"
104 #include "DNA_gpencil_types.h"
105 #include "DNA_fileglobal_types.h"
106 #include "DNA_key_types.h"
107 #include "DNA_lattice_types.h"
108 #include "DNA_lamp_types.h"
109 #include "DNA_meta_types.h"
110 #include "DNA_mesh_types.h"
111 #include "DNA_meshdata_types.h"
112 #include "DNA_material_types.h"
113 #include "DNA_node_types.h"
114 #include "DNA_object_types.h"
115 #include "DNA_object_force.h"
116 #include "DNA_packedFile_types.h"
117 #include "DNA_particle_types.h"
118 #include "DNA_property_types.h"
119 #include "DNA_scene_types.h"
120 #include "DNA_sdna_types.h"
121 #include "DNA_sequence_types.h"
122 #include "DNA_sensor_types.h"
123 #include "DNA_smoke_types.h"
124 #include "DNA_space_types.h"
125 #include "DNA_screen_types.h"
126 #include "DNA_speaker_types.h"
127 #include "DNA_sound_types.h"
128 #include "DNA_text_types.h"
129 #include "DNA_view3d_types.h"
130 #include "DNA_vfont_types.h"
131 #include "DNA_world_types.h"
132 #include "DNA_windowmanager_types.h"
134 #include "MEM_guardedalloc.h" // MEM_freeN
135 #include "BLI_blenlib.h"
136 #include "BLI_linklist.h"
137 #include "BLI_bpath.h"
138 #include "BLI_math.h"
139 #include "BLI_utildefines.h"
141 #include "BKE_action.h"
142 #include "BKE_blender.h"
143 #include "BKE_curve.h"
144 #include "BKE_constraint.h"
145 #include "BKE_global.h" // for G
146 #include "BKE_library.h" // for set_listbasepointers
147 #include "BKE_main.h"
148 #include "BKE_node.h"
149 #include "BKE_report.h"
150 #include "BKE_sequencer.h"
151 #include "BKE_utildefines.h"
152 #include "BKE_modifier.h"
153 #include "BKE_fcurve.h"
154 #include "BKE_pointcache.h"
156 #include "BLO_writefile.h"
157 #include "BLO_readfile.h"
158 #include "BLO_undofile.h"
160 #include "readfile.h"
164 /* ********* my write, buffered writing with minimum size chunks ************ */
166 #define MYWRITE_BUFFER_SIZE 100000
167 #define MYWRITE_MAX_CHUNK 32768
174 MemFile *compare, *current;
176 int tot, count, error, memsize;
179 static WriteData *writedata_new(int file)
181 WriteData *wd= MEM_callocN(sizeof(*wd), "writedata");
183 /* XXX, see note about this in readfile.c, remove
184 * once we have an xp lock - zr
187 if (wd == NULL) return NULL;
189 wd->sdna= DNA_sdna_from_data(DNAstr, DNAlen, 0);
193 wd->buf= MEM_mallocN(MYWRITE_BUFFER_SIZE, "wd->buf");
198 static void writedata_do_write(WriteData *wd, void *mem, int memlen)
200 if ((wd == NULL) || wd->error || (mem == NULL) || memlen < 1) return;
201 if (wd->error) return;
203 /* memory based save */
205 add_memfilechunk(NULL, wd->current, mem, memlen);
208 if (write(wd->file, mem, memlen) != memlen)
214 static void writedata_free(WriteData *wd)
216 DNA_sdna_free(wd->sdna);
225 * Low level WRITE(2) wrapper that buffers data
226 * @param adr Pointer to new chunk of data
227 * @param len Length of new chunk of data
228 * @warning Talks to other functions with global parameters
231 #define MYWRITE_FLUSH NULL
233 static void mywrite( WriteData *wd, void *adr, int len)
235 if (wd->error) return;
237 /* flush helps compression for undo-save */
238 if(adr==MYWRITE_FLUSH) {
240 writedata_do_write(wd, wd->buf, wd->count);
248 /* if we have a single big chunk, write existing data in
249 * buffer and write out big chunk in smaller pieces */
250 if(len>MYWRITE_MAX_CHUNK) {
252 writedata_do_write(wd, wd->buf, wd->count);
257 int writelen= MIN2(len, MYWRITE_MAX_CHUNK);
258 writedata_do_write(wd, adr, writelen);
259 adr = (char*)adr + writelen;
266 /* if data would overflow buffer, write out the buffer */
267 if(len+wd->count>MYWRITE_BUFFER_SIZE-1) {
268 writedata_do_write(wd, wd->buf, wd->count);
272 /* append data at end of buffer */
273 memcpy(&wd->buf[wd->count], adr, len);
278 * BeGiN initializer for mywrite
279 * @param file File descriptor
280 * @param write_flags Write parameters
281 * @warning Talks to other functions with global parameters
283 static WriteData *bgnwrite(int file, MemFile *compare, MemFile *current)
285 WriteData *wd= writedata_new(file);
287 if (wd == NULL) return NULL;
289 wd->compare= compare;
290 wd->current= current;
291 /* this inits comparing */
292 add_memfilechunk(compare, NULL, NULL, 0);
298 * END the mywrite wrapper
299 * @return 1 if write failed
300 * @return unknown global variable otherwise
301 * @warning Talks to other functions with global parameters
303 static int endwrite(WriteData *wd)
308 writedata_do_write(wd, wd->buf, wd->count);
318 /* ********** WRITE FILE ****************** */
320 static void writestruct(WriteData *wd, int filecode, const char *structname, int nr, void *adr)
325 if(adr==NULL || nr==0) return;
332 bh.SDNAnr= DNA_struct_find_nr(wd->sdna, structname);
334 printf("error: can't find SDNA code <%s>\n", structname);
337 sp= wd->sdna->structs[bh.SDNAnr];
339 bh.len= nr*wd->sdna->typelens[sp[0]];
341 if(bh.len==0) return;
343 mywrite(wd, &bh, sizeof(BHead));
344 mywrite(wd, adr, bh.len);
347 static void writedata(WriteData *wd, int filecode, int len, void *adr) /* do not use for structs */
351 if(adr==NULL) return;
364 mywrite(wd, &bh, sizeof(BHead));
365 if(len) mywrite(wd, adr, len);
368 /* *************** writing some direct data structs used in more code parts **************** */
369 /*These functions are used by blender's .blend system for file saving/loading.*/
370 void IDP_WriteProperty_OnlyData(IDProperty *prop, void *wd);
371 void IDP_WriteProperty(IDProperty *prop, void *wd);
373 static void IDP_WriteArray(IDProperty *prop, void *wd)
375 /*REMEMBER to set totalen to len in the linking code!!*/
376 if (prop->data.pointer) {
377 writedata(wd, DATA, MEM_allocN_len(prop->data.pointer), prop->data.pointer);
379 if(prop->subtype == IDP_GROUP) {
380 IDProperty **array= prop->data.pointer;
383 for(a=0; a<prop->len; a++)
384 IDP_WriteProperty(array[a], wd);
389 static void IDP_WriteIDPArray(IDProperty *prop, void *wd)
391 /*REMEMBER to set totalen to len in the linking code!!*/
392 if (prop->data.pointer) {
393 IDProperty *array = prop->data.pointer;
396 writestruct(wd, DATA, "IDProperty", prop->len, array);
398 for(a=0; a<prop->len; a++)
399 IDP_WriteProperty_OnlyData(&array[a], wd);
403 static void IDP_WriteString(IDProperty *prop, void *wd)
405 /*REMEMBER to set totalen to len in the linking code!!*/
406 writedata(wd, DATA, prop->len+1, prop->data.pointer);
409 static void IDP_WriteGroup(IDProperty *prop, void *wd)
413 for (loop=prop->data.group.first; loop; loop=loop->next) {
414 IDP_WriteProperty(loop, wd);
418 /* Functions to read/write ID Properties */
419 void IDP_WriteProperty_OnlyData(IDProperty *prop, void *wd)
421 switch (prop->type) {
423 IDP_WriteGroup(prop, wd);
426 IDP_WriteString(prop, wd);
429 IDP_WriteArray(prop, wd);
432 IDP_WriteIDPArray(prop, wd);
437 void IDP_WriteProperty(IDProperty *prop, void *wd)
439 writestruct(wd, DATA, "IDProperty", 1, prop);
440 IDP_WriteProperty_OnlyData(prop, wd);
443 static void write_fmodifiers(WriteData *wd, ListBase *fmodifiers)
448 for (fcm= fmodifiers->first; fcm; fcm= fcm->next) {
449 FModifierTypeInfo *fmi= fmodifier_get_typeinfo(fcm);
451 /* Write the specific data */
452 if (fmi && fcm->data) {
453 /* firstly, just write the plain fmi->data struct */
454 writestruct(wd, DATA, fmi->structName, 1, fcm->data);
456 /* do any modifier specific stuff */
458 case FMODIFIER_TYPE_GENERATOR:
460 FMod_Generator *data= (FMod_Generator *)fcm->data;
462 /* write coefficients array */
463 if (data->coefficients)
464 writedata(wd, DATA, sizeof(float)*(data->arraysize), data->coefficients);
467 case FMODIFIER_TYPE_ENVELOPE:
469 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
471 /* write envelope data */
473 writestruct(wd, DATA, "FCM_EnvelopeData", data->totvert, data->data);
476 case FMODIFIER_TYPE_PYTHON:
478 FMod_Python *data = (FMod_Python *)fcm->data;
480 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
481 of library blocks that implement this.*/
482 IDP_WriteProperty(data->prop, wd);
488 /* Write the modifier */
489 writestruct(wd, DATA, "FModifier", 1, fcm);
493 static void write_fcurves(WriteData *wd, ListBase *fcurves)
497 for (fcu=fcurves->first; fcu; fcu=fcu->next) {
499 writestruct(wd, DATA, "FCurve", 1, fcu);
503 writestruct(wd, DATA, "BezTriple", fcu->totvert, fcu->bezt);
505 writestruct(wd, DATA, "FPoint", fcu->totvert, fcu->fpt);
508 writedata(wd, DATA, strlen(fcu->rna_path)+1, fcu->rna_path);
512 ChannelDriver *driver= fcu->driver;
515 writestruct(wd, DATA, "ChannelDriver", 1, driver);
518 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
519 writestruct(wd, DATA, "DriverVar", 1, dvar);
521 DRIVER_TARGETS_USED_LOOPER(dvar)
524 writedata(wd, DATA, strlen(dtar->rna_path)+1, dtar->rna_path);
526 DRIVER_TARGETS_LOOPER_END
530 /* write F-Modifiers */
531 write_fmodifiers(wd, &fcu->modifiers);
535 static void write_actions(WriteData *wd, ListBase *idbase)
541 for(act=idbase->first; act; act= act->id.next) {
542 if (act->id.us>0 || wd->current) {
543 writestruct(wd, ID_AC, "bAction", 1, act);
544 if (act->id.properties) IDP_WriteProperty(act->id.properties, wd);
546 write_fcurves(wd, &act->curves);
548 for (grp=act->groups.first; grp; grp=grp->next) {
549 writestruct(wd, DATA, "bActionGroup", 1, grp);
552 for (marker=act->markers.first; marker; marker=marker->next) {
553 writestruct(wd, DATA, "TimeMarker", 1, marker);
558 /* flush helps the compression for undo-save */
559 mywrite(wd, MYWRITE_FLUSH, 0);
562 static void write_keyingsets(WriteData *wd, ListBase *list)
567 for (ks= list->first; ks; ks= ks->next) {
569 writestruct(wd, DATA, "KeyingSet", 1, ks);
572 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
574 writestruct(wd, DATA, "KS_Path", 1, ksp);
577 writedata(wd, DATA, strlen(ksp->rna_path)+1, ksp->rna_path);
582 static void write_nlastrips(WriteData *wd, ListBase *strips)
586 for (strip= strips->first; strip; strip= strip->next) {
587 /* write the strip first */
588 writestruct(wd, DATA, "NlaStrip", 1, strip);
590 /* write the strip's F-Curves and modifiers */
591 write_fcurves(wd, &strip->fcurves);
592 write_fmodifiers(wd, &strip->modifiers);
594 /* write the strip's children */
595 write_nlastrips(wd, &strip->strips);
599 static void write_nladata(WriteData *wd, ListBase *nlabase)
603 /* write all the tracks */
604 for (nlt= nlabase->first; nlt; nlt= nlt->next) {
605 /* write the track first */
606 writestruct(wd, DATA, "NlaTrack", 1, nlt);
608 /* write the track's strips */
609 write_nlastrips(wd, &nlt->strips);
613 static void write_animdata(WriteData *wd, AnimData *adt)
617 /* firstly, just write the AnimData block */
618 writestruct(wd, DATA, "AnimData", 1, adt);
621 write_fcurves(wd, &adt->drivers);
623 /* write overrides */
624 // FIXME: are these needed?
625 for (aor= adt->overrides.first; aor; aor= aor->next) {
626 /* overrides consist of base data + rna_path */
627 writestruct(wd, DATA, "AnimOverride", 1, aor);
628 writedata(wd, DATA, strlen(aor->rna_path)+1, aor->rna_path);
631 // TODO write the remaps (if they are needed)
634 write_nladata(wd, &adt->nla_tracks);
637 static void write_curvemapping(WriteData *wd, CurveMapping *cumap)
641 writestruct(wd, DATA, "CurveMapping", 1, cumap);
642 for(a=0; a<CM_TOT; a++)
643 writestruct(wd, DATA, "CurveMapPoint", cumap->cm[a].totpoint, cumap->cm[a].curve);
646 static void write_node_socket(WriteData *wd, bNodeSocket *sock)
648 bNodeSocketType *stype= ntreeGetSocketType(sock->type);
650 /* forward compatibility code, so older blenders still open */
651 sock->stack_type = 1;
653 if(sock->default_value) {
654 bNodeSocketValueFloat *valfloat;
655 bNodeSocketValueVector *valvector;
656 bNodeSocketValueRGBA *valrgba;
658 switch (sock->type) {
660 valfloat = sock->default_value;
661 sock->ns.vec[0] = valfloat->value;
662 sock->ns.min = valfloat->min;
663 sock->ns.max = valfloat->max;
666 valvector = sock->default_value;
667 copy_v3_v3(sock->ns.vec, valvector->value);
668 sock->ns.min = valvector->min;
669 sock->ns.max = valvector->max;
672 valrgba = sock->default_value;
673 copy_v4_v4(sock->ns.vec, valrgba->value);
680 /* actual socket writing */
681 writestruct(wd, DATA, "bNodeSocket", 1, sock);
682 if (sock->default_value)
683 writestruct(wd, DATA, stype->value_structname, 1, sock->default_value);
686 /* this is only direct data, tree itself should have been written */
687 static void write_nodetree(WriteData *wd, bNodeTree *ntree)
693 /* for link_list() speed, we write per list */
695 if(ntree->adt) write_animdata(wd, ntree->adt);
697 for(node= ntree->nodes.first; node; node= node->next)
698 writestruct(wd, DATA, "bNode", 1, node);
700 for(node= ntree->nodes.first; node; node= node->next) {
701 for(sock= node->inputs.first; sock; sock= sock->next)
702 write_node_socket(wd, sock);
703 for(sock= node->outputs.first; sock; sock= sock->next)
704 write_node_socket(wd, sock);
707 if(node->storage && node->type!=NODE_DYNAMIC) {
708 /* could be handlerized at some point, now only 1 exception still */
709 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
710 write_curvemapping(wd, node->storage);
711 else if(ntree->type==NTREE_COMPOSIT && ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
712 write_curvemapping(wd, node->storage);
713 else if(ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) )
714 write_curvemapping(wd, node->storage);
716 writestruct(wd, DATA, node->typeinfo->storagename, 1, node->storage);
720 for(link= ntree->links.first; link; link= link->next)
721 writestruct(wd, DATA, "bNodeLink", 1, link);
723 /* external sockets */
724 for(sock= ntree->inputs.first; sock; sock= sock->next)
725 write_node_socket(wd, sock);
726 for(sock= ntree->outputs.first; sock; sock= sock->next)
727 write_node_socket(wd, sock);
730 static void current_screen_compat(Main *mainvar, bScreen **screen)
735 /* find a global current screen in the first open window, to have
736 * a reasonable default for reading in older versions */
737 wm= mainvar->wm.first;
738 window= (wm)? wm->windows.first: NULL;
739 *screen= (window)? window->screen: NULL;
742 static void write_renderinfo(WriteData *wd, Main *mainvar) /* for renderdeamon */
748 /* XXX in future, handle multiple windows with multiple screnes? */
749 current_screen_compat(mainvar, &curscreen);
751 for(sce= mainvar->scene.first; sce; sce= sce->id.next) {
752 if(sce->id.lib==NULL && ( sce==curscreen->scene || (sce->r.scemode & R_BG_RENDER)) ) {
753 data[0]= sce->r.sfra;
754 data[1]= sce->r.efra;
756 memset(data+2, 0, sizeof(int)*6);
757 BLI_strncpy((char *)(data+2), sce->id.name+2, sizeof(sce->id.name)-2);
759 writedata(wd, REND, 32, data);
764 static void write_keymapitem(WriteData *wd, wmKeyMapItem *kmi)
766 writestruct(wd, DATA, "wmKeyMapItem", 1, kmi);
768 IDP_WriteProperty(kmi->properties, wd);
771 static void write_userdef(WriteData *wd)
776 wmKeyMapDiffItem *kmdi;
780 writestruct(wd, USER, "UserDef", 1, &U);
782 for(btheme= U.themes.first; btheme; btheme=btheme->next)
783 writestruct(wd, DATA, "bTheme", 1, btheme);
785 for(keymap= U.user_keymaps.first; keymap; keymap=keymap->next) {
786 writestruct(wd, DATA, "wmKeyMap", 1, keymap);
788 for(kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next) {
789 writestruct(wd, DATA, "wmKeyMapDiffItem", 1, kmdi);
790 if(kmdi->remove_item)
791 write_keymapitem(wd, kmdi->remove_item);
793 write_keymapitem(wd, kmdi->add_item);
796 for(kmi=keymap->items.first; kmi; kmi=kmi->next)
797 write_keymapitem(wd, kmi);
800 for(bext= U.addons.first; bext; bext=bext->next)
801 writestruct(wd, DATA, "bAddon", 1, bext);
803 for(style= U.uistyles.first; style; style= style->next) {
804 writestruct(wd, DATA, "uiStyle", 1, style);
808 static void write_boid_state(WriteData *wd, BoidState *state)
810 BoidRule *rule = state->rules.first;
811 //BoidCondition *cond = state->conditions.first;
813 writestruct(wd, DATA, "BoidState", 1, state);
815 for(; rule; rule=rule->next) {
817 case eBoidRuleType_Goal:
818 case eBoidRuleType_Avoid:
819 writestruct(wd, DATA, "BoidRuleGoalAvoid", 1, rule);
821 case eBoidRuleType_AvoidCollision:
822 writestruct(wd, DATA, "BoidRuleAvoidCollision", 1, rule);
824 case eBoidRuleType_FollowLeader:
825 writestruct(wd, DATA, "BoidRuleFollowLeader", 1, rule);
827 case eBoidRuleType_AverageSpeed:
828 writestruct(wd, DATA, "BoidRuleAverageSpeed", 1, rule);
830 case eBoidRuleType_Fight:
831 writestruct(wd, DATA, "BoidRuleFight", 1, rule);
834 writestruct(wd, DATA, "BoidRule", 1, rule);
838 //for(; cond; cond=cond->next)
839 // writestruct(wd, DATA, "BoidCondition", 1, cond);
842 /* update this also to readfile.c */
843 static const char *ptcache_data_struct[] = {
844 "", // BPHYS_DATA_INDEX
845 "", // BPHYS_DATA_LOCATION
846 "", // BPHYS_DATA_VELOCITY
847 "", // BPHYS_DATA_ROTATION
848 "", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
849 "", // BPHYS_DATA_SIZE:
850 "", // BPHYS_DATA_TIMES:
851 "BoidData" // case BPHYS_DATA_BOIDS:
853 static const char *ptcache_extra_struct[] = {
857 static void write_pointcaches(WriteData *wd, ListBase *ptcaches)
859 PointCache *cache = ptcaches->first;
862 for(; cache; cache=cache->next) {
863 writestruct(wd, DATA, "PointCache", 1, cache);
865 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
866 PTCacheMem *pm = cache->mem_cache.first;
868 for(; pm; pm=pm->next) {
869 PTCacheExtra *extra = pm->extradata.first;
871 writestruct(wd, DATA, "PTCacheMem", 1, pm);
873 for(i=0; i<BPHYS_TOT_DATA; i++) {
874 if(pm->data[i] && pm->data_types & (1<<i)) {
875 if(ptcache_data_struct[i][0]=='\0')
876 writedata(wd, DATA, MEM_allocN_len(pm->data[i]), pm->data[i]);
878 writestruct(wd, DATA, ptcache_data_struct[i], pm->totpoint, pm->data[i]);
882 for(; extra; extra=extra->next) {
883 if(ptcache_extra_struct[extra->type][0]=='\0')
885 writestruct(wd, DATA, "PTCacheExtra", 1, extra);
886 writestruct(wd, DATA, ptcache_extra_struct[extra->type], extra->totdata, extra->data);
892 static void write_particlesettings(WriteData *wd, ListBase *idbase)
894 ParticleSettings *part;
895 ParticleDupliWeight *dw;
901 if(part->id.us>0 || wd->current) {
903 writestruct(wd, ID_PA, "ParticleSettings", 1, part);
904 if (part->id.properties) IDP_WriteProperty(part->id.properties, wd);
905 if (part->adt) write_animdata(wd, part->adt);
906 writestruct(wd, DATA, "PartDeflect", 1, part->pd);
907 writestruct(wd, DATA, "PartDeflect", 1, part->pd2);
908 writestruct(wd, DATA, "EffectorWeights", 1, part->effector_weights);
910 dw = part->dupliweights.first;
911 for(; dw; dw=dw->next) {
914 if(part->dup_group) { /* can be NULL if lining fails or set to None */
915 go = part->dup_group->gobject.first;
916 while(go && go->ob != dw->ob) {
921 writestruct(wd, DATA, "ParticleDupliWeight", 1, dw);
924 if(part->boids && part->phystype == PART_PHYS_BOIDS) {
925 BoidState *state = part->boids->states.first;
927 writestruct(wd, DATA, "BoidSettings", 1, part->boids);
929 for(; state; state=state->next)
930 write_boid_state(wd, state);
932 if(part->fluid && part->phystype == PART_PHYS_FLUID){
933 writestruct(wd, DATA, "SPHFluidSettings", 1, part->fluid);
936 for(a=0; a<MAX_MTEX; a++) {
937 if(part->mtex[a]) writestruct(wd, DATA, "MTex", 1, part->mtex[a]);
943 static void write_particlesystems(WriteData *wd, ListBase *particles)
945 ParticleSystem *psys= particles->first;
949 for(; psys; psys=psys->next) {
950 writestruct(wd, DATA, "ParticleSystem", 1, psys);
952 if(psys->particles) {
953 writestruct(wd, DATA, "ParticleData", psys->totpart ,psys->particles);
955 if(psys->particles->hair) {
956 ParticleData *pa = psys->particles;
958 for(a=0; a<psys->totpart; a++, pa++)
959 writestruct(wd, DATA, "HairKey", pa->totkey, pa->hair);
962 if(psys->particles->boid && psys->part->phystype == PART_PHYS_BOIDS)
963 writestruct(wd, DATA, "BoidParticle", psys->totpart, psys->particles->boid);
965 if(psys->part->fluid && psys->part->phystype == PART_PHYS_FLUID && (psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS))
966 writestruct(wd, DATA, "ParticleSpring", psys->tot_fluidsprings, psys->fluid_springs);
968 pt = psys->targets.first;
969 for(; pt; pt=pt->next)
970 writestruct(wd, DATA, "ParticleTarget", 1, pt);
972 if(psys->child) writestruct(wd, DATA, "ChildParticle", psys->totchild ,psys->child);
975 writestruct(wd, DATA, "ClothModifierData", 1, psys->clmd);
976 writestruct(wd, DATA, "ClothSimSettings", 1, psys->clmd->sim_parms);
977 writestruct(wd, DATA, "ClothCollSettings", 1, psys->clmd->coll_parms);
980 write_pointcaches(wd, &psys->ptcaches);
984 static void write_properties(WriteData *wd, ListBase *lb)
990 writestruct(wd, DATA, "bProperty", 1, prop);
992 if(prop->poin && prop->poin != &prop->data)
993 writedata(wd, DATA, MEM_allocN_len(prop->poin), prop->poin);
999 static void write_sensors(WriteData *wd, ListBase *lb)
1005 writestruct(wd, DATA, "bSensor", 1, sens);
1007 writedata(wd, DATA, sizeof(void *)*sens->totlinks, sens->links);
1009 switch(sens->type) {
1011 writestruct(wd, DATA, "bNearSensor", 1, sens->data);
1014 writestruct(wd, DATA, "bMouseSensor", 1, sens->data);
1017 writestruct(wd, DATA, "bTouchSensor", 1, sens->data);
1020 writestruct(wd, DATA, "bKeyboardSensor", 1, sens->data);
1023 writestruct(wd, DATA, "bPropertySensor", 1, sens->data);
1026 writestruct(wd, DATA, "bArmatureSensor", 1, sens->data);
1029 writestruct(wd, DATA, "bActuatorSensor", 1, sens->data);
1032 writestruct(wd, DATA, "bDelaySensor", 1, sens->data);
1034 case SENS_COLLISION:
1035 writestruct(wd, DATA, "bCollisionSensor", 1, sens->data);
1038 writestruct(wd, DATA, "bRadarSensor", 1, sens->data);
1041 writestruct(wd, DATA, "bRandomSensor", 1, sens->data);
1044 writestruct(wd, DATA, "bRaySensor", 1, sens->data);
1047 writestruct(wd, DATA, "bMessageSensor", 1, sens->data);
1050 writestruct(wd, DATA, "bJoystickSensor", 1, sens->data);
1053 ; /* error: don't know how to write this file */
1060 static void write_controllers(WriteData *wd, ListBase *lb)
1066 writestruct(wd, DATA, "bController", 1, cont);
1068 writedata(wd, DATA, sizeof(void *)*cont->totlinks, cont->links);
1070 switch(cont->type) {
1071 case CONT_EXPRESSION:
1072 writestruct(wd, DATA, "bExpressionCont", 1, cont->data);
1075 writestruct(wd, DATA, "bPythonCont", 1, cont->data);
1078 ; /* error: don't know how to write this file */
1085 static void write_actuators(WriteData *wd, ListBase *lb)
1091 writestruct(wd, DATA, "bActuator", 1, act);
1095 case ACT_SHAPEACTION:
1096 writestruct(wd, DATA, "bActionActuator", 1, act->data);
1099 writestruct(wd, DATA, "bSoundActuator", 1, act->data);
1102 writestruct(wd, DATA, "bObjectActuator", 1, act->data);
1105 writestruct(wd, DATA, "bIpoActuator", 1, act->data);
1108 writestruct(wd, DATA, "bPropertyActuator", 1, act->data);
1111 writestruct(wd, DATA, "bCameraActuator", 1, act->data);
1113 case ACT_CONSTRAINT:
1114 writestruct(wd, DATA, "bConstraintActuator", 1, act->data);
1116 case ACT_EDIT_OBJECT:
1117 writestruct(wd, DATA, "bEditObjectActuator", 1, act->data);
1120 writestruct(wd, DATA, "bSceneActuator", 1, act->data);
1123 writestruct(wd, DATA, "bGroupActuator", 1, act->data);
1126 writestruct(wd, DATA, "bRandomActuator", 1, act->data);
1129 writestruct(wd, DATA, "bMessageActuator", 1, act->data);
1132 writestruct(wd, DATA, "bGameActuator", 1, act->data);
1134 case ACT_VISIBILITY:
1135 writestruct(wd, DATA, "bVisibilityActuator", 1, act->data);
1138 writestruct(wd, DATA, "bTwoDFilterActuator", 1, act->data);
1141 writestruct(wd, DATA, "bParentActuator", 1, act->data);
1144 writestruct(wd, DATA, "bStateActuator", 1, act->data);
1147 writestruct(wd, DATA, "bArmatureActuator", 1, act->data);
1150 writestruct(wd, DATA, "bSteeringActuator", 1, act->data);
1153 ; /* error: don't know how to write this file */
1160 static void write_motionpath(WriteData *wd, bMotionPath *mpath)
1166 /* firstly, just write the motionpath struct */
1167 writestruct(wd, DATA, "bMotionPath", 1, mpath);
1169 /* now write the array of data */
1170 writestruct(wd, DATA, "bMotionPathVert", mpath->length, mpath->points);
1173 static void write_constraints(WriteData *wd, ListBase *conlist)
1177 for (con=conlist->first; con; con=con->next) {
1178 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1180 /* Write the specific data */
1181 if (cti && con->data) {
1182 /* firstly, just write the plain con->data struct */
1183 writestruct(wd, DATA, cti->structName, 1, con->data);
1185 /* do any constraint specific stuff */
1186 switch (con->type) {
1187 case CONSTRAINT_TYPE_PYTHON:
1189 bPythonConstraint *data = (bPythonConstraint *)con->data;
1190 bConstraintTarget *ct;
1193 for (ct= data->targets.first; ct; ct= ct->next)
1194 writestruct(wd, DATA, "bConstraintTarget", 1, ct);
1196 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
1197 of library blocks that implement this.*/
1198 IDP_WriteProperty(data->prop, wd);
1201 case CONSTRAINT_TYPE_SPLINEIK:
1203 bSplineIKConstraint *data= (bSplineIKConstraint*)con->data;
1205 /* write points array */
1206 writedata(wd, DATA, sizeof(float)*(data->numpoints), data->points);
1212 /* Write the constraint */
1213 writestruct(wd, DATA, "bConstraint", 1, con);
1217 static void write_pose(WriteData *wd, bPose *pose)
1222 /* Write each channel */
1226 /* Write channels */
1227 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1228 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
1229 of library blocks that implement this.*/
1231 IDP_WriteProperty(chan->prop, wd);
1233 write_constraints(wd, &chan->constraints);
1235 write_motionpath(wd, chan->mpath);
1237 /* prevent crashes with autosave, when a bone duplicated in editmode has not yet been assigned to its posechannel */
1239 chan->selectflag= chan->bone->flag & BONE_SELECTED; /* gets restored on read, for library armatures */
1241 writestruct(wd, DATA, "bPoseChannel", 1, chan);
1245 for (grp=pose->agroups.first; grp; grp=grp->next)
1246 writestruct(wd, DATA, "bActionGroup", 1, grp);
1248 /* write IK param */
1249 if (pose->ikparam) {
1250 char *structname = (char *)get_ikparam_name(pose);
1252 writestruct(wd, DATA, structname, 1, pose->ikparam);
1255 /* Write this pose */
1256 writestruct(wd, DATA, "bPose", 1, pose);
1260 static void write_defgroups(WriteData *wd, ListBase *defbase)
1262 bDeformGroup *defgroup;
1264 for (defgroup=defbase->first; defgroup; defgroup=defgroup->next)
1265 writestruct(wd, DATA, "bDeformGroup", 1, defgroup);
1268 static void write_modifiers(WriteData *wd, ListBase *modbase)
1272 if (modbase == NULL) return;
1273 for (md=modbase->first; md; md= md->next) {
1274 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1275 if (mti == NULL) return;
1277 writestruct(wd, DATA, mti->structName, 1, md);
1279 if (md->type==eModifierType_Hook) {
1280 HookModifierData *hmd = (HookModifierData*) md;
1282 writedata(wd, DATA, sizeof(int)*hmd->totindex, hmd->indexar);
1284 else if(md->type==eModifierType_Cloth) {
1285 ClothModifierData *clmd = (ClothModifierData*) md;
1287 writestruct(wd, DATA, "ClothSimSettings", 1, clmd->sim_parms);
1288 writestruct(wd, DATA, "ClothCollSettings", 1, clmd->coll_parms);
1289 writestruct(wd, DATA, "EffectorWeights", 1, clmd->sim_parms->effector_weights);
1290 write_pointcaches(wd, &clmd->ptcaches);
1292 else if(md->type==eModifierType_Smoke) {
1293 SmokeModifierData *smd = (SmokeModifierData*) md;
1295 if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
1299 write_pointcaches(wd, &(smd->domain->ptcaches[0]));
1301 /* create fake pointcache so that old blender versions can read it */
1302 smd->domain->point_cache[1] = BKE_ptcache_add(&smd->domain->ptcaches[1]);
1303 smd->domain->point_cache[1]->flag |= PTCACHE_DISK_CACHE|PTCACHE_FAKE_SMOKE;
1304 smd->domain->point_cache[1]->step = 1;
1306 write_pointcaches(wd, &(smd->domain->ptcaches[1]));
1309 writestruct(wd, DATA, "SmokeDomainSettings", 1, smd->domain);
1312 /* cleanup the fake pointcache */
1313 BKE_ptcache_free_list(&smd->domain->ptcaches[1]);
1314 smd->domain->point_cache[1] = NULL;
1316 writestruct(wd, DATA, "EffectorWeights", 1, smd->domain->effector_weights);
1319 else if(smd->type & MOD_SMOKE_TYPE_FLOW)
1320 writestruct(wd, DATA, "SmokeFlowSettings", 1, smd->flow);
1321 else if(smd->type & MOD_SMOKE_TYPE_COLL)
1322 writestruct(wd, DATA, "SmokeCollSettings", 1, smd->coll);
1324 else if(md->type==eModifierType_Fluidsim) {
1325 FluidsimModifierData *fluidmd = (FluidsimModifierData*) md;
1327 writestruct(wd, DATA, "FluidsimSettings", 1, fluidmd->fss);
1329 else if (md->type==eModifierType_Collision) {
1332 CollisionModifierData *collmd = (CollisionModifierData*) md;
1333 // TODO: CollisionModifier should use pointcache
1334 // + have proper reset events before enabling this
1335 writestruct(wd, DATA, "MVert", collmd->numverts, collmd->x);
1336 writestruct(wd, DATA, "MVert", collmd->numverts, collmd->xnew);
1337 writestruct(wd, DATA, "MFace", collmd->numfaces, collmd->mfaces);
1340 else if (md->type==eModifierType_MeshDeform) {
1341 MeshDeformModifierData *mmd = (MeshDeformModifierData*) md;
1342 int size = mmd->dyngridsize;
1344 writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->bindinfluences);
1345 writedata(wd, DATA, sizeof(int)*(mmd->totvert+1), mmd->bindoffsets);
1346 writedata(wd, DATA, sizeof(float)*3*mmd->totcagevert,
1348 writestruct(wd, DATA, "MDefCell", size*size*size, mmd->dyngrid);
1349 writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->dyninfluences);
1350 writedata(wd, DATA, sizeof(int)*mmd->totvert, mmd->dynverts);
1352 else if (md->type==eModifierType_Warp) {
1353 WarpModifierData *tmd = (WarpModifierData*) md;
1354 if(tmd->curfalloff) {
1355 write_curvemapping(wd, tmd->curfalloff);
1358 else if (md->type==eModifierType_WeightVGEdit) {
1359 WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
1361 if (wmd->cmap_curve)
1362 write_curvemapping(wd, wmd->cmap_curve);
1367 static void write_objects(WriteData *wd, ListBase *idbase)
1373 if(ob->id.us>0 || wd->current) {
1375 writestruct(wd, ID_OB, "Object", 1, ob);
1377 /*Write ID Properties -- and copy this comment EXACTLY for easy finding
1378 of library blocks that implement this.*/
1379 if (ob->id.properties) IDP_WriteProperty(ob->id.properties, wd);
1381 if (ob->adt) write_animdata(wd, ob->adt);
1384 writedata(wd, DATA, sizeof(void *)*ob->totcol, ob->mat);
1385 writedata(wd, DATA, sizeof(char)*ob->totcol, ob->matbits);
1386 /* write_effects(wd, &ob->effect); */ /* not used anymore */
1387 write_properties(wd, &ob->prop);
1388 write_sensors(wd, &ob->sensors);
1389 write_controllers(wd, &ob->controllers);
1390 write_actuators(wd, &ob->actuators);
1392 if (ob->type == OB_ARMATURE) {
1393 bArmature *arm = ob->data;
1394 if (arm && ob->pose && arm->act_bone) {
1395 BLI_strncpy(ob->pose->proxy_act_bone, arm->act_bone->name, sizeof(ob->pose->proxy_act_bone));
1399 write_pose(wd, ob->pose);
1400 write_defgroups(wd, &ob->defbase);
1401 write_constraints(wd, &ob->constraints);
1402 write_motionpath(wd, ob->mpath);
1404 writestruct(wd, DATA, "PartDeflect", 1, ob->pd);
1405 writestruct(wd, DATA, "SoftBody", 1, ob->soft);
1407 write_pointcaches(wd, &ob->soft->ptcaches);
1408 writestruct(wd, DATA, "EffectorWeights", 1, ob->soft->effector_weights);
1410 writestruct(wd, DATA, "BulletSoftBody", 1, ob->bsoft);
1412 write_particlesystems(wd, &ob->particlesystem);
1413 write_modifiers(wd, &ob->modifiers);
1418 /* flush helps the compression for undo-save */
1419 mywrite(wd, MYWRITE_FLUSH, 0);
1423 static void write_vfonts(WriteData *wd, ListBase *idbase)
1430 if(vf->id.us>0 || wd->current) {
1432 writestruct(wd, ID_VF, "VFont", 1, vf);
1433 if (vf->id.properties) IDP_WriteProperty(vf->id.properties, wd);
1437 if (vf->packedfile) {
1438 pf = vf->packedfile;
1439 writestruct(wd, DATA, "PackedFile", 1, pf);
1440 writedata(wd, DATA, pf->size, pf->data);
1449 static void write_keys(WriteData *wd, ListBase *idbase)
1456 if(key->id.us>0 || wd->current) {
1458 writestruct(wd, ID_KE, "Key", 1, key);
1459 if (key->id.properties) IDP_WriteProperty(key->id.properties, wd);
1461 if (key->adt) write_animdata(wd, key->adt);
1464 kb= key->block.first;
1466 writestruct(wd, DATA, "KeyBlock", 1, kb);
1467 if(kb->data) writedata(wd, DATA, kb->totelem*key->elemsize, kb->data);
1474 /* flush helps the compression for undo-save */
1475 mywrite(wd, MYWRITE_FLUSH, 0);
1478 static void write_cameras(WriteData *wd, ListBase *idbase)
1484 if(cam->id.us>0 || wd->current) {
1486 writestruct(wd, ID_CA, "Camera", 1, cam);
1487 if (cam->id.properties) IDP_WriteProperty(cam->id.properties, wd);
1489 if (cam->adt) write_animdata(wd, cam->adt);
1496 static void write_mballs(WriteData *wd, ListBase *idbase)
1503 if(mb->id.us>0 || wd->current) {
1505 writestruct(wd, ID_MB, "MetaBall", 1, mb);
1506 if (mb->id.properties) IDP_WriteProperty(mb->id.properties, wd);
1509 writedata(wd, DATA, sizeof(void *)*mb->totcol, mb->mat);
1510 if (mb->adt) write_animdata(wd, mb->adt);
1512 ml= mb->elems.first;
1514 writestruct(wd, DATA, "MetaElem", 1, ml);
1522 static int amount_of_chars(char *str)
1524 // Since the data is saved as UTF-8 to the cu->str
1525 // The cu->len is not same as the strlen(cu->str)
1529 static void write_curves(WriteData *wd, ListBase *idbase)
1536 if(cu->id.us>0 || wd->current) {
1538 writestruct(wd, ID_CU, "Curve", 1, cu);
1541 writedata(wd, DATA, sizeof(void *)*cu->totcol, cu->mat);
1542 if (cu->id.properties) IDP_WriteProperty(cu->id.properties, wd);
1543 if (cu->adt) write_animdata(wd, cu->adt);
1546 writedata(wd, DATA, amount_of_chars(cu->str)+1, cu->str);
1547 writestruct(wd, DATA, "CharInfo", cu->len+1, cu->strinfo);
1548 writestruct(wd, DATA, "TextBox", cu->totbox, cu->tb);
1551 /* is also the order of reading */
1554 writestruct(wd, DATA, "Nurb", 1, nu);
1559 if(nu->type == CU_BEZIER)
1560 writestruct(wd, DATA, "BezTriple", nu->pntsu, nu->bezt);
1562 writestruct(wd, DATA, "BPoint", nu->pntsu*nu->pntsv, nu->bp);
1563 if(nu->knotsu) writedata(wd, DATA, KNOTSU(nu)*sizeof(float), nu->knotsu);
1564 if(nu->knotsv) writedata(wd, DATA, KNOTSV(nu)*sizeof(float), nu->knotsv);
1573 /* flush helps the compression for undo-save */
1574 mywrite(wd, MYWRITE_FLUSH, 0);
1577 static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
1582 /* Write the dvert list */
1583 writestruct(wd, DATA, "MDeformVert", count, dvlist);
1585 /* Write deformation data for each dvert */
1586 for (i=0; i<count; i++) {
1588 writestruct(wd, DATA, "MDeformWeight", dvlist[i].totweight, dvlist[i].dw);
1593 static void write_mdisps(WriteData *wd, int count, MDisps *mdlist, int external)
1598 writestruct(wd, DATA, "MDisps", count, mdlist);
1600 for(i = 0; i < count; ++i) {
1602 writedata(wd, DATA, sizeof(float)*3*mdlist[i].totdisp, mdlist[i].disps);
1608 static void write_customdata(WriteData *wd, ID *id, int count, CustomData *data, int partial_type, int partial_count)
1612 /* write external customdata (not for undo) */
1613 if(data->external && !wd->current)
1614 CustomData_external_write(data, id, CD_MASK_MESH, count, 0);
1616 writestruct(wd, DATA, "CustomDataLayer", data->maxlayer, data->layers);
1618 for (i=0; i<data->totlayer; i++) {
1619 CustomDataLayer *layer= &data->layers[i];
1620 const char *structname;
1621 int structnum, datasize;
1623 if (layer->type == CD_MDEFORMVERT) {
1624 /* layer types that allocate own memory need special handling */
1625 write_dverts(wd, count, layer->data);
1627 else if (layer->type == CD_MDISPS) {
1628 write_mdisps(wd, count, layer->data, layer->flag & CD_FLAG_EXTERNAL);
1631 CustomData_file_write_info(layer->type, &structname, &structnum);
1633 /* when using partial visibility, the MEdge and MFace layers
1634 are smaller than the original, so their type and count is
1635 passed to make this work */
1636 if (layer->type != partial_type) datasize= structnum*count;
1637 else datasize= structnum*partial_count;
1639 writestruct(wd, DATA, structname, datasize, layer->data);
1642 printf("error: this CustomDataLayer must not be written to file\n");
1647 writestruct(wd, DATA, "CustomDataExternal", 1, data->external);
1650 static void write_meshs(WriteData *wd, ListBase *idbase)
1654 mesh= idbase->first;
1656 if(mesh->id.us>0 || wd->current) {
1658 writestruct(wd, ID_ME, "Mesh", 1, mesh);
1661 if (mesh->id.properties) IDP_WriteProperty(mesh->id.properties, wd);
1662 if (mesh->adt) write_animdata(wd, mesh->adt);
1664 writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
1667 write_customdata(wd, &mesh->id, mesh->pv->totvert, &mesh->vdata, -1, 0);
1668 write_customdata(wd, &mesh->id, mesh->pv->totedge, &mesh->edata,
1669 CD_MEDGE, mesh->totedge);
1670 write_customdata(wd, &mesh->id, mesh->pv->totface, &mesh->fdata,
1671 CD_MFACE, mesh->totface);
1674 write_customdata(wd, &mesh->id, mesh->totvert, &mesh->vdata, -1, 0);
1675 write_customdata(wd, &mesh->id, mesh->totedge, &mesh->edata, -1, 0);
1676 write_customdata(wd, &mesh->id, mesh->totface, &mesh->fdata, -1, 0);
1681 writestruct(wd, DATA, "PartialVisibility", 1, mesh->pv);
1682 writedata(wd, DATA, sizeof(unsigned int)*mesh->pv->totvert, mesh->pv->vert_map);
1683 writedata(wd, DATA, sizeof(int)*mesh->pv->totedge, mesh->pv->edge_map);
1684 writestruct(wd, DATA, "MFace", mesh->pv->totface, mesh->pv->old_faces);
1685 writestruct(wd, DATA, "MEdge", mesh->pv->totedge, mesh->pv->old_edges);
1688 mesh= mesh->id.next;
1692 static void write_lattices(WriteData *wd, ListBase *idbase)
1698 if(lt->id.us>0 || wd->current) {
1700 writestruct(wd, ID_LT, "Lattice", 1, lt);
1701 if (lt->id.properties) IDP_WriteProperty(lt->id.properties, wd);
1703 /* write animdata */
1704 if (lt->adt) write_animdata(wd, lt->adt);
1707 writestruct(wd, DATA, "BPoint", lt->pntsu*lt->pntsv*lt->pntsw, lt->def);
1709 write_dverts(wd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
1716 static void write_previews(WriteData *wd, PreviewImage *prv)
1719 short w = prv->w[1];
1720 short h = prv->h[1];
1721 unsigned int *rect = prv->rect[1];
1722 /* don't write out large previews if not requested */
1723 if (!(U.flag & USER_SAVE_PREVIEWS) ) {
1726 prv->rect[1] = NULL;
1728 writestruct(wd, DATA, "PreviewImage", 1, prv);
1729 if (prv->rect[0]) writedata(wd, DATA, prv->w[0]*prv->h[0]*sizeof(unsigned int), prv->rect[0]);
1730 if (prv->rect[1]) writedata(wd, DATA, prv->w[1]*prv->h[1]*sizeof(unsigned int), prv->rect[1]);
1732 /* restore preview, we still want to keep it in memory even if not saved to file */
1733 if (!(U.flag & USER_SAVE_PREVIEWS) ) {
1736 prv->rect[1] = rect;
1741 static void write_images(WriteData *wd, ListBase *idbase)
1749 if(ima->id.us>0 || wd->current) {
1751 writestruct(wd, ID_IM, "Image", 1, ima);
1752 if (ima->id.properties) IDP_WriteProperty(ima->id.properties, wd);
1754 if (ima->packedfile) {
1755 pf = ima->packedfile;
1756 writestruct(wd, DATA, "PackedFile", 1, pf);
1757 writedata(wd, DATA, pf->size, pf->data);
1760 write_previews(wd, ima->preview);
1764 /* flush helps the compression for undo-save */
1765 mywrite(wd, MYWRITE_FLUSH, 0);
1768 static void write_textures(WriteData *wd, ListBase *idbase)
1774 if(tex->id.us>0 || wd->current) {
1776 writestruct(wd, ID_TE, "Tex", 1, tex);
1777 if (tex->id.properties) IDP_WriteProperty(tex->id.properties, wd);
1779 if (tex->adt) write_animdata(wd, tex->adt);
1782 if(tex->type == TEX_PLUGIN && tex->plugin) writestruct(wd, DATA, "PluginTex", 1, tex->plugin);
1783 if(tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
1784 if(tex->type == TEX_ENVMAP && tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
1785 if(tex->type == TEX_POINTDENSITY && tex->pd) {
1786 writestruct(wd, DATA, "PointDensity", 1, tex->pd);
1787 if(tex->pd->coba) writestruct(wd, DATA, "ColorBand", 1, tex->pd->coba);
1788 if(tex->pd->falloff_curve) write_curvemapping(wd, tex->pd->falloff_curve);
1790 if(tex->type == TEX_VOXELDATA) writestruct(wd, DATA, "VoxelData", 1, tex->vd);
1792 /* nodetree is integral part of texture, no libdata */
1794 writestruct(wd, DATA, "bNodeTree", 1, tex->nodetree);
1795 write_nodetree(wd, tex->nodetree);
1798 write_previews(wd, tex->preview);
1803 /* flush helps the compression for undo-save */
1804 mywrite(wd, MYWRITE_FLUSH, 0);
1807 static void write_materials(WriteData *wd, ListBase *idbase)
1814 if(ma->id.us>0 || wd->current) {
1816 writestruct(wd, ID_MA, "Material", 1, ma);
1818 /*Write ID Properties -- and copy this comment EXACTLY for easy finding
1819 of library blocks that implement this.*/
1820 /*manually set head group property to IDP_GROUP, just in case it hadn't been
1822 if (ma->id.properties) IDP_WriteProperty(ma->id.properties, wd);
1824 if (ma->adt) write_animdata(wd, ma->adt);
1826 for(a=0; a<MAX_MTEX; a++) {
1827 if(ma->mtex[a]) writestruct(wd, DATA, "MTex", 1, ma->mtex[a]);
1830 if(ma->ramp_col) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_col);
1831 if(ma->ramp_spec) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_spec);
1833 /* nodetree is integral part of material, no libdata */
1835 writestruct(wd, DATA, "bNodeTree", 1, ma->nodetree);
1836 write_nodetree(wd, ma->nodetree);
1839 write_previews(wd, ma->preview);
1845 static void write_worlds(WriteData *wd, ListBase *idbase)
1850 wrld= idbase->first;
1852 if(wrld->id.us>0 || wd->current) {
1854 writestruct(wd, ID_WO, "World", 1, wrld);
1855 if (wrld->id.properties) IDP_WriteProperty(wrld->id.properties, wd);
1857 if (wrld->adt) write_animdata(wd, wrld->adt);
1859 for(a=0; a<MAX_MTEX; a++) {
1860 if(wrld->mtex[a]) writestruct(wd, DATA, "MTex", 1, wrld->mtex[a]);
1863 /* nodetree is integral part of lamps, no libdata */
1864 if(wrld->nodetree) {
1865 writestruct(wd, DATA, "bNodeTree", 1, wrld->nodetree);
1866 write_nodetree(wd, wrld->nodetree);
1869 write_previews(wd, wrld->preview);
1871 wrld= wrld->id.next;
1875 static void write_lamps(WriteData *wd, ListBase *idbase)
1882 if(la->id.us>0 || wd->current) {
1884 writestruct(wd, ID_LA, "Lamp", 1, la);
1885 if (la->id.properties) IDP_WriteProperty(la->id.properties, wd);
1887 if (la->adt) write_animdata(wd, la->adt);
1890 for(a=0; a<MAX_MTEX; a++) {
1891 if(la->mtex[a]) writestruct(wd, DATA, "MTex", 1, la->mtex[a]);
1895 write_curvemapping(wd, la->curfalloff);
1897 /* nodetree is integral part of lamps, no libdata */
1899 writestruct(wd, DATA, "bNodeTree", 1, la->nodetree);
1900 write_nodetree(wd, la->nodetree);
1903 write_previews(wd, la->preview);
1911 static void write_scenes(WriteData *wd, ListBase *scebase)
1920 TransformOrientation *ts;
1921 SceneRenderLayer *srl;
1924 sce= scebase->first;
1927 writestruct(wd, ID_SCE, "Scene", 1, sce);
1928 if (sce->id.properties) IDP_WriteProperty(sce->id.properties, wd);
1930 if (sce->adt) write_animdata(wd, sce->adt);
1931 write_keyingsets(wd, &sce->keyingsets);
1934 base= sce->base.first;
1936 writestruct(wd, DATA, "Base", 1, base);
1940 tos = sce->toolsettings;
1941 writestruct(wd, DATA, "ToolSettings", 1, tos);
1943 writestruct(wd, DATA, "VPaint", 1, tos->vpaint);
1946 writestruct(wd, DATA, "VPaint", 1, tos->wpaint);
1949 writestruct(wd, DATA, "Sculpt", 1, tos->sculpt);
1952 // write_paint(wd, &tos->imapaint.paint);
1956 writestruct(wd, DATA, "Editing", 1, ed);
1958 /* reset write flags too */
1960 SEQ_BEGIN(ed, seq) {
1961 if(seq->strip) seq->strip->done= 0;
1962 writestruct(wd, DATA, "Sequence", 1, seq);
1966 SEQ_BEGIN(ed, seq) {
1967 if(seq->strip && seq->strip->done==0) {
1968 /* write strip with 'done' at 0 because readfile */
1970 if(seq->plugin) writestruct(wd, DATA, "PluginSeq", 1, seq->plugin);
1971 if(seq->effectdata) {
1974 writestruct(wd, DATA, "SolidColorVars", 1, seq->effectdata);
1977 writestruct(wd, DATA, "SpeedControlVars", 1, seq->effectdata);
1980 writestruct(wd, DATA, "WipeVars", 1, seq->effectdata);
1983 writestruct(wd, DATA, "GlowVars", 1, seq->effectdata);
1986 writestruct(wd, DATA, "TransformVars", 1, seq->effectdata);
1992 writestruct(wd, DATA, "Strip", 1, strip);
1993 if(seq->flag & SEQ_USE_CROP && strip->crop) {
1994 writestruct(wd, DATA, "StripCrop", 1, strip->crop);
1996 if(seq->flag & SEQ_USE_TRANSFORM && strip->transform) {
1997 writestruct(wd, DATA, "StripTransform", 1, strip->transform);
1999 if(seq->flag & SEQ_USE_PROXY && strip->proxy) {
2000 writestruct(wd, DATA, "StripProxy", 1, strip->proxy);
2002 if(seq->flag & SEQ_USE_COLOR_BALANCE && strip->color_balance) {
2003 writestruct(wd, DATA, "StripColorBalance", 1, strip->color_balance);
2005 if(seq->type==SEQ_IMAGE)
2006 writestruct(wd, DATA, "StripElem", MEM_allocN_len(strip->stripdata) / sizeof(struct StripElem), strip->stripdata);
2007 else if(seq->type==SEQ_MOVIE || seq->type==SEQ_RAM_SOUND || seq->type == SEQ_HD_SOUND)
2008 writestruct(wd, DATA, "StripElem", 1, strip->stripdata);
2015 /* new; meta stack too, even when its nasty restore code */
2016 for(ms= ed->metastack.first; ms; ms= ms->next) {
2017 writestruct(wd, DATA, "MetaStack", 1, ms);
2021 if (sce->r.avicodecdata) {
2022 writestruct(wd, DATA, "AviCodecData", 1, sce->r.avicodecdata);
2023 if (sce->r.avicodecdata->lpFormat) writedata(wd, DATA, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat);
2024 if (sce->r.avicodecdata->lpParms) writedata(wd, DATA, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms);
2027 if (sce->r.qtcodecdata) {
2028 writestruct(wd, DATA, "QuicktimeCodecData", 1, sce->r.qtcodecdata);
2029 if (sce->r.qtcodecdata->cdParms) writedata(wd, DATA, sce->r.qtcodecdata->cdSize, sce->r.qtcodecdata->cdParms);
2031 if (sce->r.ffcodecdata.properties) {
2032 IDP_WriteProperty(sce->r.ffcodecdata.properties, wd);
2035 /* writing dynamic list of TimeMarkers to the blend file */
2036 for(marker= sce->markers.first; marker; marker= marker->next)
2037 writestruct(wd, DATA, "TimeMarker", 1, marker);
2039 /* writing dynamic list of TransformOrientations to the blend file */
2040 for(ts = sce->transform_spaces.first; ts; ts = ts->next)
2041 writestruct(wd, DATA, "TransformOrientation", 1, ts);
2043 for(srl= sce->r.layers.first; srl; srl= srl->next)
2044 writestruct(wd, DATA, "SceneRenderLayer", 1, srl);
2047 writestruct(wd, DATA, "bNodeTree", 1, sce->nodetree);
2048 write_nodetree(wd, sce->nodetree);
2053 /* flush helps the compression for undo-save */
2054 mywrite(wd, MYWRITE_FLUSH, 0);
2057 static void write_gpencils(WriteData *wd, ListBase *lb)
2064 for (gpd= lb->first; gpd; gpd= gpd->id.next) {
2065 if (gpd->id.us>0 || wd->current) {
2066 /* write gpd data block to file */
2067 writestruct(wd, ID_GD, "bGPdata", 1, gpd);
2069 /* write grease-pencil layers to file */
2070 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
2071 writestruct(wd, DATA, "bGPDlayer", 1, gpl);
2073 /* write this layer's frames to file */
2074 for (gpf= gpl->frames.first; gpf; gpf= gpf->next) {
2075 writestruct(wd, DATA, "bGPDframe", 1, gpf);
2078 for (gps= gpf->strokes.first; gps; gps= gps->next) {
2079 writestruct(wd, DATA, "bGPDstroke", 1, gps);
2080 writestruct(wd, DATA, "bGPDspoint", gps->totpoints, gps->points);
2088 static void write_windowmanagers(WriteData *wd, ListBase *lb)
2090 wmWindowManager *wm;
2093 for(wm= lb->first; wm; wm= wm->id.next) {
2094 writestruct(wd, ID_WM, "wmWindowManager", 1, wm);
2096 for(win= wm->windows.first; win; win= win->next)
2097 writestruct(wd, DATA, "wmWindow", 1, win);
2101 static void write_region(WriteData *wd, ARegion *ar, int spacetype)
2103 writestruct(wd, DATA, "ARegion", 1, ar);
2105 if(ar->regiondata) {
2108 if(ar->regiontype==RGN_TYPE_WINDOW) {
2109 RegionView3D *rv3d= ar->regiondata;
2110 writestruct(wd, DATA, "RegionView3D", 1, rv3d);
2113 writestruct(wd, DATA, "RegionView3D", 1, rv3d->localvd);
2115 writestruct(wd, DATA, "BoundBox", 1, rv3d->clipbb);
2119 printf("regiondata write missing!\n");
2122 printf("regiondata write missing!\n");
2127 static void write_screens(WriteData *wd, ListBase *scrbase)
2138 /* in 2.50+ files, the file identifier for screens is patched, forward compatibility */
2139 writestruct(wd, ID_SCRN, "Screen", 1, sc);
2140 if (sc->id.properties)
2141 IDP_WriteProperty(sc->id.properties, wd);
2144 for(sv= sc->vertbase.first; sv; sv= sv->next)
2145 writestruct(wd, DATA, "ScrVert", 1, sv);
2147 for(se= sc->edgebase.first; se; se= se->next)
2148 writestruct(wd, DATA, "ScrEdge", 1, se);
2150 for(sa= sc->areabase.first; sa; sa= sa->next) {
2155 writestruct(wd, DATA, "ScrArea", 1, sa);
2157 for(ar= sa->regionbase.first; ar; ar= ar->next) {
2158 write_region(wd, ar, sa->spacetype);
2160 for(pa= ar->panels.first; pa; pa= pa->next)
2161 writestruct(wd, DATA, "Panel", 1, pa);
2164 sl= sa->spacedata.first;
2166 for(ar= sl->regionbase.first; ar; ar= ar->next)
2167 write_region(wd, ar, sl->spacetype);
2169 if(sl->spacetype==SPACE_VIEW3D) {
2170 View3D *v3d= (View3D *) sl;
2172 writestruct(wd, DATA, "View3D", 1, v3d);
2173 for (bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
2174 writestruct(wd, DATA, "BGpic", 1, bgpic);
2175 if(v3d->localvd) writestruct(wd, DATA, "View3D", 1, v3d->localvd);
2177 else if(sl->spacetype==SPACE_IPO) {
2178 SpaceIpo *sipo= (SpaceIpo *)sl;
2179 ListBase tmpGhosts = sipo->ghostCurves;
2181 /* temporarily disable ghost curves when saving */
2182 sipo->ghostCurves.first= sipo->ghostCurves.last= NULL;
2184 writestruct(wd, DATA, "SpaceIpo", 1, sl);
2185 if(sipo->ads) writestruct(wd, DATA, "bDopeSheet", 1, sipo->ads);
2187 /* reenable ghost curves */
2188 sipo->ghostCurves= tmpGhosts;
2190 else if(sl->spacetype==SPACE_BUTS) {
2191 writestruct(wd, DATA, "SpaceButs", 1, sl);
2193 else if(sl->spacetype==SPACE_FILE) {
2194 SpaceFile *sfile= (SpaceFile *)sl;
2196 writestruct(wd, DATA, "SpaceFile", 1, sl);
2198 writestruct(wd, DATA, "FileSelectParams", 1, sfile->params);
2200 else if(sl->spacetype==SPACE_SEQ) {
2201 writestruct(wd, DATA, "SpaceSeq", 1, sl);
2203 else if(sl->spacetype==SPACE_OUTLINER) {
2204 SpaceOops *so= (SpaceOops *)sl;
2206 writestruct(wd, DATA, "SpaceOops", 1, so);
2210 writestruct(wd, DATA, "TreeStore", 1, so->treestore);
2211 if(so->treestore->data)
2212 writestruct(wd, DATA, "TreeStoreElem", so->treestore->usedelem, so->treestore->data);
2215 else if(sl->spacetype==SPACE_IMAGE) {
2216 SpaceImage *sima= (SpaceImage *)sl;
2218 writestruct(wd, DATA, "SpaceImage", 1, sl);
2220 write_curvemapping(wd, sima->cumap);
2222 else if(sl->spacetype==SPACE_IMASEL) {
2223 // XXX: depreceated... do we still want to keep this?
2224 writestruct(wd, DATA, "SpaceImaSel", 1, sl);
2226 else if(sl->spacetype==SPACE_TEXT) {
2227 writestruct(wd, DATA, "SpaceText", 1, sl);
2229 else if(sl->spacetype==SPACE_SCRIPT) {
2230 SpaceScript *scr = (SpaceScript*)sl;
2231 scr->but_refs = NULL;
2232 writestruct(wd, DATA, "SpaceScript", 1, sl);
2234 else if(sl->spacetype==SPACE_ACTION) {
2235 writestruct(wd, DATA, "SpaceAction", 1, sl);
2237 else if(sl->spacetype==SPACE_SOUND) {
2238 writestruct(wd, DATA, "SpaceSound", 1, sl);
2240 else if(sl->spacetype==SPACE_NLA){
2241 SpaceNla *snla= (SpaceNla *)sl;
2243 writestruct(wd, DATA, "SpaceNla", 1, snla);
2244 if(snla->ads) writestruct(wd, DATA, "bDopeSheet", 1, snla->ads);
2246 else if(sl->spacetype==SPACE_TIME){
2247 writestruct(wd, DATA, "SpaceTime", 1, sl);
2249 else if(sl->spacetype==SPACE_NODE){
2250 writestruct(wd, DATA, "SpaceNode", 1, sl);
2252 else if(sl->spacetype==SPACE_LOGIC){
2253 writestruct(wd, DATA, "SpaceLogic", 1, sl);
2255 else if(sl->spacetype==SPACE_CONSOLE) {
2256 SpaceConsole *con = (SpaceConsole*)sl;
2259 for (cl=con->history.first; cl; cl=cl->next) {
2260 /* 'len_alloc' is invalid on write, set from 'len' on read */
2261 writestruct(wd, DATA, "ConsoleLine", 1, cl);
2262 writedata(wd, DATA, cl->len+1, cl->line);
2264 writestruct(wd, DATA, "SpaceConsole", 1, sl);
2267 else if(sl->spacetype==SPACE_USERPREF) {
2268 writestruct(wd, DATA, "SpaceUserPref", 1, sl);
2279 static void write_libraries(WriteData *wd, Main *main)
2281 ListBase *lbarray[30];
2283 int a, tot, foundone;
2285 for(; main; main= main->next) {
2287 a=tot= set_listbasepointers(main, lbarray);
2289 /* test: is lib being used */
2292 for(id= lbarray[tot]->first; id; id= id->next) {
2293 if(id->us>0 && (id->flag & LIB_EXTERN)) {
2302 writestruct(wd, ID_LI, "Library", 1, main->curlib);
2305 for(id= lbarray[a]->first; id; id= id->next) {
2306 if(id->us>0 && (id->flag & LIB_EXTERN)) {
2307 writestruct(wd, ID_ID, "ID", 1, id);
2315 static void write_bone(WriteData *wd, Bone* bone)
2319 // PATCH for upward compatibility after 2.37+ armature recode
2320 bone->size[0]= bone->size[1]= bone->size[2]= 1.0f;
2323 writestruct(wd, DATA, "Bone", 1, bone);
2325 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
2326 of library blocks that implement this.*/
2328 IDP_WriteProperty(bone->prop, wd);
2331 cbone= bone->childbase.first;
2333 write_bone(wd, cbone);
2338 static void write_armatures(WriteData *wd, ListBase *idbase)
2345 if (arm->id.us>0 || wd->current) {
2346 writestruct(wd, ID_AR, "bArmature", 1, arm);
2347 if (arm->id.properties) IDP_WriteProperty(arm->id.properties, wd);
2349 if (arm->adt) write_animdata(wd, arm->adt);
2352 bone= arm->bonebase.first;
2354 write_bone(wd, bone);
2361 /* flush helps the compression for undo-save */
2362 mywrite(wd, MYWRITE_FLUSH, 0);
2365 static void write_texts(WriteData *wd, ListBase *idbase)
2371 text= idbase->first;
2373 if ( (text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) text->flags &= ~TXT_ISEXT;
2376 writestruct(wd, ID_TXT, "Text", 1, text);
2377 if(text->name) writedata(wd, DATA, strlen(text->name)+1, text->name);
2378 if (text->id.properties) IDP_WriteProperty(text->id.properties, wd);
2380 if(!(text->flags & TXT_ISEXT)) {
2381 /* now write the text data, in two steps for optimization in the readfunction */
2382 tmp= text->lines.first;
2384 writestruct(wd, DATA, "TextLine", 1, tmp);
2388 tmp= text->lines.first;
2390 writedata(wd, DATA, tmp->len+1, tmp->line);
2395 mrk= text->markers.first;
2397 writestruct(wd, DATA, "TextMarker", 1, mrk);
2403 text= text->id.next;
2406 /* flush helps the compression for undo-save */
2407 mywrite(wd, MYWRITE_FLUSH, 0);
2410 static void write_speakers(WriteData *wd, ListBase *idbase)
2416 if(spk->id.us>0 || wd->current) {
2418 writestruct(wd, ID_SPK, "Speaker", 1, spk);
2419 if (spk->id.properties) IDP_WriteProperty(spk->id.properties, wd);
2421 if (spk->adt) write_animdata(wd, spk->adt);
2427 static void write_sounds(WriteData *wd, ListBase *idbase)
2433 sound= idbase->first;
2435 if(sound->id.us>0 || wd->current) {
2437 writestruct(wd, ID_SO, "bSound", 1, sound);
2438 if (sound->id.properties) IDP_WriteProperty(sound->id.properties, wd);
2440 if (sound->packedfile) {
2441 pf = sound->packedfile;
2442 writestruct(wd, DATA, "PackedFile", 1, pf);
2443 writedata(wd, DATA, pf->size, pf->data);
2446 sound= sound->id.next;
2449 /* flush helps the compression for undo-save */
2450 mywrite(wd, MYWRITE_FLUSH, 0);
2453 static void write_groups(WriteData *wd, ListBase *idbase)
2458 for(group= idbase->first; group; group= group->id.next) {
2459 if(group->id.us>0 || wd->current) {
2461 writestruct(wd, ID_GR, "Group", 1, group);
2462 if (group->id.properties) IDP_WriteProperty(group->id.properties, wd);
2464 go= group->gobject.first;
2466 writestruct(wd, DATA, "GroupObject", 1, go);
2473 static void write_nodetrees(WriteData *wd, ListBase *idbase)
2477 for(ntree=idbase->first; ntree; ntree= ntree->id.next) {
2478 if (ntree->id.us>0 || wd->current) {
2479 writestruct(wd, ID_NT, "bNodeTree", 1, ntree);
2480 write_nodetree(wd, ntree);
2482 if (ntree->id.properties) IDP_WriteProperty(ntree->id.properties, wd);
2484 if (ntree->adt) write_animdata(wd, ntree->adt);
2489 static void write_brushes(WriteData *wd, ListBase *idbase)
2493 for(brush=idbase->first; brush; brush= brush->id.next) {
2494 if(brush->id.us>0 || wd->current) {
2495 writestruct(wd, ID_BR, "Brush", 1, brush);
2496 if (brush->id.properties) IDP_WriteProperty(brush->id.properties, wd);
2498 writestruct(wd, DATA, "MTex", 1, &brush->mtex);
2501 write_curvemapping(wd, brush->curve);
2506 static void write_scripts(WriteData *wd, ListBase *idbase)
2510 for(script=idbase->first; script; script= script->id.next) {
2511 if(script->id.us>0 || wd->current) {
2512 writestruct(wd, ID_SCRIPT, "Script", 1, script);
2513 if (script->id.properties) IDP_WriteProperty(script->id.properties, wd);
2518 /* context is usually defined by WM, two cases where no WM is available:
2519 * - for forward compatibility, curscreen has to be saved
2520 * - for undofile, curscene needs to be saved */
2521 static void write_global(WriteData *wd, int fileflags, Main *mainvar)
2527 /* prevent mem checkers from complaining */
2529 memset(fg.filename, 0, sizeof(fg.filename));
2531 current_screen_compat(mainvar, &screen);
2533 /* XXX still remap G */
2534 fg.curscreen= screen;
2535 fg.curscene= screen->scene;
2536 fg.displaymode= G.displaymode;
2537 fg.winpos= G.winpos;
2538 fg.fileflags= (fileflags & ~(G_FILE_NO_UI|G_FILE_RELATIVE_REMAP)); // prevent to save this, is not good convention, and feature with concerns...
2540 BLI_strncpy(fg.filename, mainvar->name, sizeof(fg.filename));
2542 sprintf(subvstr, "%4d", BLENDER_SUBVERSION);
2543 memcpy(fg.subvstr, subvstr, 4);
2545 fg.subversion= BLENDER_SUBVERSION;
2546 fg.minversion= BLENDER_MINVERSION;
2547 fg.minsubversion= BLENDER_MINSUBVERSION;
2548 #ifdef WITH_BUILDINFO
2550 extern char build_rev[];
2551 fg.revision= atoi(build_rev);
2556 writestruct(wd, GLOB, "FileGlobal", 1, &fg);
2559 /* preview image, first 2 values are width and height
2560 * second are an RGBA image (unsigned char)
2561 * note, this uses 'TEST' since new types will segfault on file load for older blender versions.
2563 static void write_thumb(WriteData *wd, int *img)
2566 writedata(wd, TEST, (2 + img[0] * img[1]) * sizeof(int), img);
2569 /* if MemFile * there's filesave to memory */
2570 static int write_file_handle(Main *mainvar, int handle, MemFile *compare, MemFile *current,
2571 int write_user_block, int write_flags, int *thumb)
2578 blo_split_main(&mainlist, mainvar);
2580 wd= bgnwrite(handle, compare, current);
2582 sprintf(buf, "BLENDER%c%c%.3d", (sizeof(void*)==8)?'-':'_', (ENDIAN_ORDER==B_ENDIAN)?'V':'v', BLENDER_VERSION);
2583 mywrite(wd, buf, 12);
2585 write_renderinfo(wd, mainvar);
2586 write_thumb(wd, thumb);
2587 write_global(wd, write_flags, mainvar);
2589 /* no UI save in undo */
2591 write_windowmanagers(wd, &mainvar->wm);
2592 write_screens (wd, &mainvar->screen);
2594 write_scenes (wd, &mainvar->scene);
2595 write_curves (wd, &mainvar->curve);
2596 write_mballs (wd, &mainvar->mball);
2597 write_images (wd, &mainvar->image);
2598 write_cameras (wd, &mainvar->camera);
2599 write_lamps (wd, &mainvar->lamp);
2600 write_lattices (wd, &mainvar->latt);
2601 write_vfonts (wd, &mainvar->vfont);
2602 write_keys (wd, &mainvar->key);
2603 write_worlds (wd, &mainvar->world);
2604 write_texts (wd, &mainvar->text);
2605 write_speakers (wd, &mainvar->speaker);
2606 write_sounds (wd, &mainvar->sound);
2607 write_groups (wd, &mainvar->group);
2608 write_armatures(wd, &mainvar->armature);
2609 write_actions (wd, &mainvar->action);
2610 write_objects (wd, &mainvar->object);
2611 write_materials(wd, &mainvar->mat);
2612 write_textures (wd, &mainvar->tex);
2613 write_meshs (wd, &mainvar->mesh);
2614 write_particlesettings(wd, &mainvar->particle);
2615 write_nodetrees(wd, &mainvar->nodetree);
2616 write_brushes (wd, &mainvar->brush);
2617 write_scripts (wd, &mainvar->script);
2618 write_gpencils (wd, &mainvar->gpencil);
2619 write_libraries(wd, mainvar->next);
2621 if (write_user_block) {
2625 /* dna as last, because (to be implemented) test for which structs are written */
2626 writedata(wd, DNA1, wd->sdna->datalen, wd->sdna->data);
2629 memset(&bhead, 0, sizeof(BHead));
2631 mywrite(wd, &bhead, sizeof(BHead));
2633 blo_join_main(&mainlist);
2635 return endwrite(wd);
2638 /* do reverse file history: .blend1 -> .blend2, .blend -> .blend1 */
2639 /* return: success(0), failure(1) */
2640 static int do_history(const char *name, ReportList *reports)
2642 char tempname1[FILE_MAXDIR+FILE_MAXFILE], tempname2[FILE_MAXDIR+FILE_MAXFILE];
2643 int hisnr= U.versions;
2645 if(U.versions==0) return 0;
2646 if(strlen(name)<2) {
2647 BKE_report(reports, RPT_ERROR, "Unable to make version backup: filename too short");
2652 BLI_snprintf(tempname1, sizeof(tempname1), "%s%d", name, hisnr-1);
2653 BLI_snprintf(tempname2, sizeof(tempname2), "%s%d", name, hisnr);
2655 if(BLI_rename(tempname1, tempname2)) {
2656 BKE_report(reports, RPT_ERROR, "Unable to make version backup");
2662 /* is needed when hisnr==1 */
2663 BLI_snprintf(tempname1, sizeof(tempname1), "%s%d", name, hisnr);
2665 if(BLI_rename(name, tempname1)) {
2666 BKE_report(reports, RPT_ERROR, "Unable to make version backup");
2673 /* return: success (1) */
2674 int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportList *reports, int *thumb)
2676 char userfilename[FILE_MAXDIR+FILE_MAXFILE];
2677 char tempname[FILE_MAXDIR+FILE_MAXFILE+1];
2678 int file, err, write_user_block;
2680 /* open temporary file, so we preserve the original in case we crash */
2681 BLI_snprintf(tempname, sizeof(tempname), "%s@", filepath);
2683 file = open(tempname,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
2685 BKE_reportf(reports, RPT_ERROR, "Can't open file %s for writing: %s.", tempname, strerror(errno));
2689 /* remapping of relative paths to new file location */
2690 if(write_flags & G_FILE_RELATIVE_REMAP) {
2691 char dir1[FILE_MAXDIR+FILE_MAXFILE];
2692 char dir2[FILE_MAXDIR+FILE_MAXFILE];
2693 BLI_split_dir_part(filepath, dir1, sizeof(dir1));
2694 BLI_split_dir_part(mainvar->name, dir2, sizeof(dir2));
2696 /* just incase there is some subtle difference */
2697 BLI_cleanup_dir(mainvar->name, dir1);
2698 BLI_cleanup_dir(mainvar->name, dir2);
2700 if(BLI_path_cmp(dir1, dir2)==0) {
2701 write_flags &= ~G_FILE_RELATIVE_REMAP;
2704 if(G.relbase_valid) {
2705 /* blend may not have been saved before. Tn this case
2706 * we should not have any relative paths, but if there
2707 * is somehow, an invalid or empty G.main->name it will
2708 * print an error, dont try make the absolute in this case. */
2709 makeFilesAbsolute(mainvar, G.main->name, NULL);
2714 userfilename[0]= '\0'; /* ensure its initialized */
2715 BLI_make_file_string(G.main->name, userfilename, BLI_get_folder_create(BLENDER_USER_CONFIG, NULL), BLENDER_STARTUP_FILE);
2716 write_user_block= (BLI_path_cmp(filepath, userfilename) == 0);
2718 if(write_flags & G_FILE_RELATIVE_REMAP)
2719 makeFilesRelative(mainvar, filepath, NULL); /* note, making relative to something OTHER then G.main->name */
2721 /* actual file writing */
2722 err= write_file_handle(mainvar, file, NULL,NULL, write_user_block, write_flags, thumb);
2726 BKE_report(reports, RPT_ERROR, strerror(errno));
2732 /* file save to temporary file was successful */
2733 /* now do reverse file history (move .blend1 -> .blend2, .blend -> .blend1) */
2734 if (write_flags & G_FILE_HISTORY) {
2735 int err_hist = do_history(filepath, reports);
2737 BKE_report(reports, RPT_ERROR, "Version backup failed. File saved with @");
2742 if(write_flags & G_FILE_COMPRESS) {
2743 /* compressed files have the same ending as regular files... only from 2.4!!! */
2744 char gzname[FILE_MAXDIR+FILE_MAXFILE+4];
2747 /* first write compressed to separate @.gz */
2748 BLI_snprintf(gzname, sizeof(gzname), "%s@.gz", filepath);
2749 ret = BLI_gzip(tempname, gzname);
2752 /* now rename to real file name, and delete temp @ file too */
2753 if(BLI_rename(gzname, filepath) != 0) {
2754 BKE_report(reports, RPT_ERROR, "Can't change old file. File saved with @.");
2758 BLI_delete(tempname, 0, 0);
2761 BKE_report(reports, RPT_ERROR, "Failed opening .gz file.");
2765 BKE_report(reports, RPT_ERROR, "Failed opening .blend file for compression.");
2769 else if(BLI_rename(tempname, filepath) != 0) {
2770 BKE_report(reports, RPT_ERROR, "Can't change old file. File saved with @");
2777 /* return: success (1) */
2778 int BLO_write_file_mem(Main *mainvar, MemFile *compare, MemFile *current, int write_flags)
2782 err= write_file_handle(mainvar, 0, compare, current, 0, write_flags, NULL);
2784 if(err==0) return 1;