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