4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/blenloader/intern/writefile.c
35 FILEFORMAT: IFF-style structure (but not IFF compatible!)
38 BLENDER_V100 12 bytes (versie 1.00)
39 V = big endian, v = little endian
40 _ = 4 byte pointer, - = 8 byte pointer
42 datablocks: also see struct BHead
44 <bh.len> int, len data after BHead
45 <bh.old> void, old pointer
47 <bh.nr> int, in case of array: amount of structs
52 Almost all data in Blender are structures. Each struct saved
53 gets a BHead header. With BHead the struct can be linked again
54 and compared with StructDNA .
58 Preferred writing order: (not really a must, but why would you do it random?)
59 Any case: direct data is ALWAYS after the lib block
64 - write associated direct data
69 - write the ID of LibBlock
70 - write TEST (128x128, blend file preview, optional)
71 - write FileGlobal (some global vars)
73 - write USER if filename is ~/X.XX/config/startup.blend
90 #include <process.h> // for getpid
91 #include "BLI_winstuff.h"
94 #include "DNA_anim_types.h"
95 #include "DNA_armature_types.h"
96 #include "DNA_actuator_types.h"
97 #include "DNA_brush_types.h"
98 #include "DNA_camera_types.h"
99 #include "DNA_cloth_types.h"
100 #include "DNA_constraint_types.h"
101 #include "DNA_controller_types.h"
102 #include "DNA_genfile.h"
103 #include "DNA_group_types.h"
104 #include "DNA_gpencil_types.h"
105 #include "DNA_fileglobal_types.h"
106 #include "DNA_key_types.h"
107 #include "DNA_lattice_types.h"
108 #include "DNA_lamp_types.h"
109 #include "DNA_meta_types.h"
110 #include "DNA_mesh_types.h"
111 #include "DNA_meshdata_types.h"
112 #include "DNA_material_types.h"
113 #include "DNA_node_types.h"
114 #include "DNA_object_types.h"
115 #include "DNA_object_force.h"
116 #include "DNA_packedFile_types.h"
117 #include "DNA_particle_types.h"
118 #include "DNA_property_types.h"
119 #include "DNA_scene_types.h"
120 #include "DNA_sdna_types.h"
121 #include "DNA_sequence_types.h"
122 #include "DNA_sensor_types.h"
123 #include "DNA_smoke_types.h"
124 #include "DNA_space_types.h"
125 #include "DNA_screen_types.h"
126 #include "DNA_sound_types.h"
127 #include "DNA_text_types.h"
128 #include "DNA_view3d_types.h"
129 #include "DNA_vfont_types.h"
130 #include "DNA_world_types.h"
131 #include "DNA_windowmanager_types.h"
133 #include "MEM_guardedalloc.h" // MEM_freeN
134 #include "BLI_blenlib.h"
135 #include "BLI_linklist.h"
136 #include "BLI_bpath.h"
137 #include "BLI_utildefines.h"
139 #include "BKE_action.h"
140 #include "BKE_blender.h"
141 #include "BKE_curve.h"
142 #include "BKE_constraint.h"
143 #include "BKE_global.h" // for G
144 #include "BKE_library.h" // for set_listbasepointers
145 #include "BKE_main.h"
146 #include "BKE_node.h"
147 #include "BKE_report.h"
148 #include "BKE_sequencer.h"
149 #include "BKE_utildefines.h"
150 #include "BKE_modifier.h"
151 #include "BKE_fcurve.h"
152 #include "BKE_pointcache.h"
154 #include "BLO_writefile.h"
155 #include "BLO_readfile.h"
156 #include "BLO_undofile.h"
158 #include "readfile.h"
162 /* ********* my write, buffered writing with minimum size chunks ************ */
164 #define MYWRITE_BUFFER_SIZE 100000
165 #define MYWRITE_MAX_CHUNK 32768
172 MemFile *compare, *current;
174 int tot, count, error, memsize;
177 static WriteData *writedata_new(int file)
179 WriteData *wd= MEM_callocN(sizeof(*wd), "writedata");
181 /* XXX, see note about this in readfile.c, remove
182 * once we have an xp lock - zr
185 if (wd == NULL) return NULL;
187 wd->sdna= DNA_sdna_from_data(DNAstr, DNAlen, 0);
191 wd->buf= MEM_mallocN(MYWRITE_BUFFER_SIZE, "wd->buf");
196 static void writedata_do_write(WriteData *wd, void *mem, int memlen)
198 if ((wd == NULL) || wd->error || (mem == NULL) || memlen < 1) return;
199 if (wd->error) return;
201 /* memory based save */
203 add_memfilechunk(NULL, wd->current, mem, memlen);
206 if (write(wd->file, mem, memlen) != memlen)
212 static void writedata_free(WriteData *wd)
214 DNA_sdna_free(wd->sdna);
223 * Low level WRITE(2) wrapper that buffers data
224 * @param adr Pointer to new chunk of data
225 * @param len Length of new chunk of data
226 * @warning Talks to other functions with global parameters
229 #define MYWRITE_FLUSH NULL
231 static void mywrite( WriteData *wd, void *adr, int len)
233 if (wd->error) return;
235 /* flush helps compression for undo-save */
236 if(adr==MYWRITE_FLUSH) {
238 writedata_do_write(wd, wd->buf, wd->count);
246 /* if we have a single big chunk, write existing data in
247 * buffer and write out big chunk in smaller pieces */
248 if(len>MYWRITE_MAX_CHUNK) {
250 writedata_do_write(wd, wd->buf, wd->count);
255 int writelen= MIN2(len, MYWRITE_MAX_CHUNK);
256 writedata_do_write(wd, adr, writelen);
257 adr = (char*)adr + writelen;
264 /* if data would overflow buffer, write out the buffer */
265 if(len+wd->count>MYWRITE_BUFFER_SIZE-1) {
266 writedata_do_write(wd, wd->buf, wd->count);
270 /* append data at end of buffer */
271 memcpy(&wd->buf[wd->count], adr, len);
276 * BeGiN initializer for mywrite
277 * @param file File descriptor
278 * @param write_flags Write parameters
279 * @warning Talks to other functions with global parameters
281 static WriteData *bgnwrite(int file, MemFile *compare, MemFile *current)
283 WriteData *wd= writedata_new(file);
285 if (wd == NULL) return NULL;
287 wd->compare= compare;
288 wd->current= current;
289 /* this inits comparing */
290 add_memfilechunk(compare, NULL, NULL, 0);
296 * END the mywrite wrapper
297 * @return 1 if write failed
298 * @return unknown global variable otherwise
299 * @warning Talks to other functions with global parameters
301 static int endwrite(WriteData *wd)
306 writedata_do_write(wd, wd->buf, wd->count);
316 /* ********** WRITE FILE ****************** */
318 static void writestruct(WriteData *wd, int filecode, const char *structname, int nr, void *adr)
323 if(adr==NULL || nr==0) return;
330 bh.SDNAnr= DNA_struct_find_nr(wd->sdna, structname);
332 printf("error: can't find SDNA code <%s>\n", structname);
335 sp= wd->sdna->structs[bh.SDNAnr];
337 bh.len= nr*wd->sdna->typelens[sp[0]];
339 if(bh.len==0) return;
341 mywrite(wd, &bh, sizeof(BHead));
342 mywrite(wd, adr, bh.len);
345 static void writedata(WriteData *wd, int filecode, int len, void *adr) /* do not use for structs */
349 if(adr==NULL) return;
362 mywrite(wd, &bh, sizeof(BHead));
363 if(len) mywrite(wd, adr, len);
366 /* *************** writing some direct data structs used in more code parts **************** */
367 /*These functions are used by blender's .blend system for file saving/loading.*/
368 void IDP_WriteProperty_OnlyData(IDProperty *prop, void *wd);
369 void IDP_WriteProperty(IDProperty *prop, void *wd);
371 static void IDP_WriteArray(IDProperty *prop, void *wd)
373 /*REMEMBER to set totalen to len in the linking code!!*/
374 if (prop->data.pointer) {
375 writedata(wd, DATA, MEM_allocN_len(prop->data.pointer), prop->data.pointer);
377 if(prop->subtype == IDP_GROUP) {
378 IDProperty **array= prop->data.pointer;
381 for(a=0; a<prop->len; a++)
382 IDP_WriteProperty(array[a], wd);
387 static void IDP_WriteIDPArray(IDProperty *prop, void *wd)
389 /*REMEMBER to set totalen to len in the linking code!!*/
390 if (prop->data.pointer) {
391 IDProperty *array = prop->data.pointer;
394 writestruct(wd, DATA, "IDProperty", prop->len, array);
396 for(a=0; a<prop->len; a++)
397 IDP_WriteProperty_OnlyData(&array[a], wd);
401 static void IDP_WriteString(IDProperty *prop, void *wd)
403 /*REMEMBER to set totalen to len in the linking code!!*/
404 writedata(wd, DATA, prop->len+1, prop->data.pointer);
407 static void IDP_WriteGroup(IDProperty *prop, void *wd)
411 for (loop=prop->data.group.first; loop; loop=loop->next) {
412 IDP_WriteProperty(loop, wd);
416 /* Functions to read/write ID Properties */
417 void IDP_WriteProperty_OnlyData(IDProperty *prop, void *wd)
419 switch (prop->type) {
421 IDP_WriteGroup(prop, wd);
424 IDP_WriteString(prop, wd);
427 IDP_WriteArray(prop, wd);
430 IDP_WriteIDPArray(prop, wd);
435 void IDP_WriteProperty(IDProperty *prop, void *wd)
437 writestruct(wd, DATA, "IDProperty", 1, prop);
438 IDP_WriteProperty_OnlyData(prop, wd);
441 static void write_fmodifiers(WriteData *wd, ListBase *fmodifiers)
446 for (fcm= fmodifiers->first; fcm; fcm= fcm->next) {
447 FModifierTypeInfo *fmi= fmodifier_get_typeinfo(fcm);
449 /* Write the specific data */
450 if (fmi && fcm->data) {
451 /* firstly, just write the plain fmi->data struct */
452 writestruct(wd, DATA, fmi->structName, 1, fcm->data);
454 /* do any modifier specific stuff */
456 case FMODIFIER_TYPE_GENERATOR:
458 FMod_Generator *data= (FMod_Generator *)fcm->data;
460 /* write coefficients array */
461 if (data->coefficients)
462 writedata(wd, DATA, sizeof(float)*(data->arraysize), data->coefficients);
465 case FMODIFIER_TYPE_ENVELOPE:
467 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
469 /* write envelope data */
471 writestruct(wd, DATA, "FCM_EnvelopeData", data->totvert, data->data);
474 case FMODIFIER_TYPE_PYTHON:
476 FMod_Python *data = (FMod_Python *)fcm->data;
478 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
479 of library blocks that implement this.*/
480 IDP_WriteProperty(data->prop, wd);
486 /* Write the modifier */
487 writestruct(wd, DATA, "FModifier", 1, fcm);
491 static void write_fcurves(WriteData *wd, ListBase *fcurves)
495 for (fcu=fcurves->first; fcu; fcu=fcu->next) {
497 writestruct(wd, DATA, "FCurve", 1, fcu);
501 writestruct(wd, DATA, "BezTriple", fcu->totvert, fcu->bezt);
503 writestruct(wd, DATA, "FPoint", fcu->totvert, fcu->fpt);
506 writedata(wd, DATA, strlen(fcu->rna_path)+1, fcu->rna_path);
510 ChannelDriver *driver= fcu->driver;
513 writestruct(wd, DATA, "ChannelDriver", 1, driver);
516 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
517 writestruct(wd, DATA, "DriverVar", 1, dvar);
519 DRIVER_TARGETS_USED_LOOPER(dvar)
522 writedata(wd, DATA, strlen(dtar->rna_path)+1, dtar->rna_path);
524 DRIVER_TARGETS_LOOPER_END
528 /* write F-Modifiers */
529 write_fmodifiers(wd, &fcu->modifiers);
533 static void write_actions(WriteData *wd, ListBase *idbase)
539 for(act=idbase->first; act; act= act->id.next) {
540 if (act->id.us>0 || wd->current) {
541 writestruct(wd, ID_AC, "bAction", 1, act);
542 if (act->id.properties) IDP_WriteProperty(act->id.properties, wd);
544 write_fcurves(wd, &act->curves);
546 for (grp=act->groups.first; grp; grp=grp->next) {
547 writestruct(wd, DATA, "bActionGroup", 1, grp);
550 for (marker=act->markers.first; marker; marker=marker->next) {
551 writestruct(wd, DATA, "TimeMarker", 1, marker);
556 /* flush helps the compression for undo-save */
557 mywrite(wd, MYWRITE_FLUSH, 0);
560 static void write_keyingsets(WriteData *wd, ListBase *list)
565 for (ks= list->first; ks; ks= ks->next) {
567 writestruct(wd, DATA, "KeyingSet", 1, ks);
570 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
572 writestruct(wd, DATA, "KS_Path", 1, ksp);
575 writedata(wd, DATA, strlen(ksp->rna_path)+1, ksp->rna_path);
580 static void write_nlastrips(WriteData *wd, ListBase *strips)
584 for (strip= strips->first; strip; strip= strip->next) {
585 /* write the strip first */
586 writestruct(wd, DATA, "NlaStrip", 1, strip);
588 /* write the strip's F-Curves and modifiers */
589 write_fcurves(wd, &strip->fcurves);
590 write_fmodifiers(wd, &strip->modifiers);
592 /* write the strip's children */
593 write_nlastrips(wd, &strip->strips);
597 static void write_nladata(WriteData *wd, ListBase *nlabase)
601 /* write all the tracks */
602 for (nlt= nlabase->first; nlt; nlt= nlt->next) {
603 /* write the track first */
604 writestruct(wd, DATA, "NlaTrack", 1, nlt);
606 /* write the track's strips */
607 write_nlastrips(wd, &nlt->strips);
611 static void write_animdata(WriteData *wd, AnimData *adt)
615 /* firstly, just write the AnimData block */
616 writestruct(wd, DATA, "AnimData", 1, adt);
619 write_fcurves(wd, &adt->drivers);
621 /* write overrides */
622 // FIXME: are these needed?
623 for (aor= adt->overrides.first; aor; aor= aor->next) {
624 /* overrides consist of base data + rna_path */
625 writestruct(wd, DATA, "AnimOverride", 1, aor);
626 writedata(wd, DATA, strlen(aor->rna_path)+1, aor->rna_path);
629 // TODO write the remaps (if they are needed)
632 write_nladata(wd, &adt->nla_tracks);
635 static void write_curvemapping(WriteData *wd, CurveMapping *cumap)
639 writestruct(wd, DATA, "CurveMapping", 1, cumap);
640 for(a=0; a<CM_TOT; a++)
641 writestruct(wd, DATA, "CurveMapPoint", cumap->cm[a].totpoint, cumap->cm[a].curve);
644 /* this is only direct data, tree itself should have been written */
645 static void write_nodetree(WriteData *wd, bNodeTree *ntree)
651 /* for link_list() speed, we write per list */
653 if(ntree->adt) write_animdata(wd, ntree->adt);
655 for(node= ntree->nodes.first; node; node= node->next)
656 writestruct(wd, DATA, "bNode", 1, node);
658 for(node= ntree->nodes.first; node; node= node->next) {
659 if(node->storage && node->type!=NODE_DYNAMIC) {
660 /* could be handlerized at some point, now only 1 exception still */
661 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
662 write_curvemapping(wd, node->storage);
663 else if(ntree->type==NTREE_COMPOSIT && ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
664 write_curvemapping(wd, node->storage);
665 else if(ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) )
666 write_curvemapping(wd, node->storage);
668 writestruct(wd, DATA, node->typeinfo->storagename, 1, node->storage);
670 for(sock= node->inputs.first; sock; sock= sock->next)
671 writestruct(wd, DATA, "bNodeSocket", 1, sock);
672 for(sock= node->outputs.first; sock; sock= sock->next)
673 writestruct(wd, DATA, "bNodeSocket", 1, sock);
676 for(link= ntree->links.first; link; link= link->next)
677 writestruct(wd, DATA, "bNodeLink", 1, link);
679 /* external sockets */
680 for(sock= ntree->inputs.first; sock; sock= sock->next)
681 writestruct(wd, DATA, "bNodeSocket", 1, sock);
682 for(sock= ntree->outputs.first; sock; sock= sock->next)
683 writestruct(wd, DATA, "bNodeSocket", 1, sock);
686 static void current_screen_compat(Main *mainvar, bScreen **screen)
691 /* find a global current screen in the first open window, to have
692 * a reasonable default for reading in older versions */
693 wm= mainvar->wm.first;
694 window= (wm)? wm->windows.first: NULL;
695 *screen= (window)? window->screen: NULL;
698 static void write_renderinfo(WriteData *wd, Main *mainvar) /* for renderdeamon */
704 /* XXX in future, handle multiple windows with multiple screnes? */
705 current_screen_compat(mainvar, &curscreen);
707 for(sce= mainvar->scene.first; sce; sce= sce->id.next) {
708 if(sce->id.lib==NULL && ( sce==curscreen->scene || (sce->r.scemode & R_BG_RENDER)) ) {
709 data[0]= sce->r.sfra;
710 data[1]= sce->r.efra;
712 memset(data+2, 0, sizeof(int)*6);
713 BLI_strncpy((char *)(data+2), sce->id.name+2, sizeof(sce->id.name)-2);
715 writedata(wd, REND, 32, data);
720 static void write_userdef(WriteData *wd)
727 writestruct(wd, USER, "UserDef", 1, &U);
729 for(btheme= U.themes.first; btheme; btheme=btheme->next)
730 writestruct(wd, DATA, "bTheme", 1, btheme);
732 for(keymap= U.keymaps.first; keymap; keymap=keymap->next) {
733 writestruct(wd, DATA, "wmKeyMap", 1, keymap);
735 for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
736 writestruct(wd, DATA, "wmKeyMapItem", 1, kmi);
739 IDP_WriteProperty(kmi->properties, wd);
743 for(bext= U.addons.first; bext; bext=bext->next)
744 writestruct(wd, DATA, "bAddon", 1, bext);
747 static void write_boid_state(WriteData *wd, BoidState *state)
749 BoidRule *rule = state->rules.first;
750 //BoidCondition *cond = state->conditions.first;
752 writestruct(wd, DATA, "BoidState", 1, state);
754 for(; rule; rule=rule->next) {
756 case eBoidRuleType_Goal:
757 case eBoidRuleType_Avoid:
758 writestruct(wd, DATA, "BoidRuleGoalAvoid", 1, rule);
760 case eBoidRuleType_AvoidCollision:
761 writestruct(wd, DATA, "BoidRuleAvoidCollision", 1, rule);
763 case eBoidRuleType_FollowLeader:
764 writestruct(wd, DATA, "BoidRuleFollowLeader", 1, rule);
766 case eBoidRuleType_AverageSpeed:
767 writestruct(wd, DATA, "BoidRuleAverageSpeed", 1, rule);
769 case eBoidRuleType_Fight:
770 writestruct(wd, DATA, "BoidRuleFight", 1, rule);
773 writestruct(wd, DATA, "BoidRule", 1, rule);
777 //for(; cond; cond=cond->next)
778 // writestruct(wd, DATA, "BoidCondition", 1, cond);
781 /* update this also to readfile.c */
782 static const char *ptcache_data_struct[] = {
783 "", // BPHYS_DATA_INDEX
784 "", // BPHYS_DATA_LOCATION
785 "", // BPHYS_DATA_VELOCITY
786 "", // BPHYS_DATA_ROTATION
787 "", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
788 "", // BPHYS_DATA_SIZE:
789 "", // BPHYS_DATA_TIMES:
790 "BoidData" // case BPHYS_DATA_BOIDS:
792 static const char *ptcache_extra_struct[] = {
796 static void write_pointcaches(WriteData *wd, ListBase *ptcaches)
798 PointCache *cache = ptcaches->first;
801 for(; cache; cache=cache->next) {
802 writestruct(wd, DATA, "PointCache", 1, cache);
804 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
805 PTCacheMem *pm = cache->mem_cache.first;
807 for(; pm; pm=pm->next) {
808 PTCacheExtra *extra = pm->extradata.first;
810 writestruct(wd, DATA, "PTCacheMem", 1, pm);
812 for(i=0; i<BPHYS_TOT_DATA; i++) {
813 if(pm->data[i] && pm->data_types & (1<<i)) {
814 if(strcmp(ptcache_data_struct[i], "")==0)
815 writedata(wd, DATA, MEM_allocN_len(pm->data[i]), pm->data[i]);
817 writestruct(wd, DATA, ptcache_data_struct[i], pm->totpoint, pm->data[i]);
821 for(; extra; extra=extra->next) {
822 if(strcmp(ptcache_extra_struct[extra->type], "")==0)
824 writestruct(wd, DATA, "PTCacheExtra", 1, extra);
825 writestruct(wd, DATA, ptcache_extra_struct[extra->type], extra->totdata, extra->data);
831 static void write_particlesettings(WriteData *wd, ListBase *idbase)
833 ParticleSettings *part;
834 ParticleDupliWeight *dw;
839 if(part->id.us>0 || wd->current) {
841 writestruct(wd, ID_PA, "ParticleSettings", 1, part);
842 if (part->id.properties) IDP_WriteProperty(part->id.properties, wd);
843 if (part->adt) write_animdata(wd, part->adt);
844 writestruct(wd, DATA, "PartDeflect", 1, part->pd);
845 writestruct(wd, DATA, "PartDeflect", 1, part->pd2);
846 writestruct(wd, DATA, "EffectorWeights", 1, part->effector_weights);
848 dw = part->dupliweights.first;
849 for(; dw; dw=dw->next)
850 writestruct(wd, DATA, "ParticleDupliWeight", 1, dw);
852 if(part->boids && part->phystype == PART_PHYS_BOIDS) {
853 BoidState *state = part->boids->states.first;
855 writestruct(wd, DATA, "BoidSettings", 1, part->boids);
857 for(; state; state=state->next)
858 write_boid_state(wd, state);
860 if(part->fluid && part->phystype == PART_PHYS_FLUID){
861 writestruct(wd, DATA, "SPHFluidSettings", 1, part->fluid);
864 for(a=0; a<MAX_MTEX; a++) {
865 if(part->mtex[a]) writestruct(wd, DATA, "MTex", 1, part->mtex[a]);
871 static void write_particlesystems(WriteData *wd, ListBase *particles)
873 ParticleSystem *psys= particles->first;
877 for(; psys; psys=psys->next) {
878 writestruct(wd, DATA, "ParticleSystem", 1, psys);
880 if(psys->particles) {
881 writestruct(wd, DATA, "ParticleData", psys->totpart ,psys->particles);
883 if(psys->particles->hair) {
884 ParticleData *pa = psys->particles;
886 for(a=0; a<psys->totpart; a++, pa++)
887 writestruct(wd, DATA, "HairKey", pa->totkey, pa->hair);
890 if(psys->particles->boid && psys->part->phystype == PART_PHYS_BOIDS)
891 writestruct(wd, DATA, "BoidParticle", psys->totpart, psys->particles->boid);
893 if(psys->part->fluid && psys->part->phystype == PART_PHYS_FLUID && (psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS))
894 writestruct(wd, DATA, "ParticleSpring", psys->tot_fluidsprings, psys->fluid_springs);
896 pt = psys->targets.first;
897 for(; pt; pt=pt->next)
898 writestruct(wd, DATA, "ParticleTarget", 1, pt);
900 if(psys->child) writestruct(wd, DATA, "ChildParticle", psys->totchild ,psys->child);
903 writestruct(wd, DATA, "ClothModifierData", 1, psys->clmd);
904 writestruct(wd, DATA, "ClothSimSettings", 1, psys->clmd->sim_parms);
905 writestruct(wd, DATA, "ClothCollSettings", 1, psys->clmd->coll_parms);
908 write_pointcaches(wd, &psys->ptcaches);
912 static void write_properties(WriteData *wd, ListBase *lb)
918 writestruct(wd, DATA, "bProperty", 1, prop);
920 if(prop->poin && prop->poin != &prop->data)
921 writedata(wd, DATA, MEM_allocN_len(prop->poin), prop->poin);
927 static void write_sensors(WriteData *wd, ListBase *lb)
933 writestruct(wd, DATA, "bSensor", 1, sens);
935 writedata(wd, DATA, sizeof(void *)*sens->totlinks, sens->links);
939 writestruct(wd, DATA, "bNearSensor", 1, sens->data);
942 writestruct(wd, DATA, "bMouseSensor", 1, sens->data);
945 writestruct(wd, DATA, "bTouchSensor", 1, sens->data);
948 writestruct(wd, DATA, "bKeyboardSensor", 1, sens->data);
951 writestruct(wd, DATA, "bPropertySensor", 1, sens->data);
954 writestruct(wd, DATA, "bArmatureSensor", 1, sens->data);
957 writestruct(wd, DATA, "bActuatorSensor", 1, sens->data);
960 writestruct(wd, DATA, "bDelaySensor", 1, sens->data);
963 writestruct(wd, DATA, "bCollisionSensor", 1, sens->data);
966 writestruct(wd, DATA, "bRadarSensor", 1, sens->data);
969 writestruct(wd, DATA, "bRandomSensor", 1, sens->data);
972 writestruct(wd, DATA, "bRaySensor", 1, sens->data);
975 writestruct(wd, DATA, "bMessageSensor", 1, sens->data);
978 writestruct(wd, DATA, "bJoystickSensor", 1, sens->data);
981 ; /* error: don't know how to write this file */
988 static void write_controllers(WriteData *wd, ListBase *lb)
994 writestruct(wd, DATA, "bController", 1, cont);
996 writedata(wd, DATA, sizeof(void *)*cont->totlinks, cont->links);
999 case CONT_EXPRESSION:
1000 writestruct(wd, DATA, "bExpressionCont", 1, cont->data);
1003 writestruct(wd, DATA, "bPythonCont", 1, cont->data);
1006 ; /* error: don't know how to write this file */
1013 static void write_actuators(WriteData *wd, ListBase *lb)
1019 writestruct(wd, DATA, "bActuator", 1, act);
1023 case ACT_SHAPEACTION:
1024 writestruct(wd, DATA, "bActionActuator", 1, act->data);
1027 writestruct(wd, DATA, "bSoundActuator", 1, act->data);
1030 writestruct(wd, DATA, "bObjectActuator", 1, act->data);
1033 writestruct(wd, DATA, "bIpoActuator", 1, act->data);
1036 writestruct(wd, DATA, "bPropertyActuator", 1, act->data);
1039 writestruct(wd, DATA, "bCameraActuator", 1, act->data);
1041 case ACT_CONSTRAINT:
1042 writestruct(wd, DATA, "bConstraintActuator", 1, act->data);
1044 case ACT_EDIT_OBJECT:
1045 writestruct(wd, DATA, "bEditObjectActuator", 1, act->data);
1048 writestruct(wd, DATA, "bSceneActuator", 1, act->data);
1051 writestruct(wd, DATA, "bGroupActuator", 1, act->data);
1054 writestruct(wd, DATA, "bRandomActuator", 1, act->data);
1057 writestruct(wd, DATA, "bMessageActuator", 1, act->data);
1060 writestruct(wd, DATA, "bGameActuator", 1, act->data);
1062 case ACT_VISIBILITY:
1063 writestruct(wd, DATA, "bVisibilityActuator", 1, act->data);
1066 writestruct(wd, DATA, "bTwoDFilterActuator", 1, act->data);
1069 writestruct(wd, DATA, "bParentActuator", 1, act->data);
1072 writestruct(wd, DATA, "bStateActuator", 1, act->data);
1075 writestruct(wd, DATA, "bArmatureActuator", 1, act->data);
1078 ; /* error: don't know how to write this file */
1085 static void write_motionpath(WriteData *wd, bMotionPath *mpath)
1091 /* firstly, just write the motionpath struct */
1092 writestruct(wd, DATA, "bMotionPath", 1, mpath);
1094 /* now write the array of data */
1095 writestruct(wd, DATA, "bMotionPathVert", mpath->length, mpath->points);
1098 static void write_constraints(WriteData *wd, ListBase *conlist)
1102 for (con=conlist->first; con; con=con->next) {
1103 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1105 /* Write the specific data */
1106 if (cti && con->data) {
1107 /* firstly, just write the plain con->data struct */
1108 writestruct(wd, DATA, cti->structName, 1, con->data);
1110 /* do any constraint specific stuff */
1111 switch (con->type) {
1112 case CONSTRAINT_TYPE_PYTHON:
1114 bPythonConstraint *data = (bPythonConstraint *)con->data;
1115 bConstraintTarget *ct;
1118 for (ct= data->targets.first; ct; ct= ct->next)
1119 writestruct(wd, DATA, "bConstraintTarget", 1, ct);
1121 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
1122 of library blocks that implement this.*/
1123 IDP_WriteProperty(data->prop, wd);
1126 case CONSTRAINT_TYPE_SPLINEIK:
1128 bSplineIKConstraint *data= (bSplineIKConstraint*)con->data;
1130 /* write points array */
1131 writedata(wd, DATA, sizeof(float)*(data->numpoints), data->points);
1137 /* Write the constraint */
1138 writestruct(wd, DATA, "bConstraint", 1, con);
1142 static void write_pose(WriteData *wd, bPose *pose)
1147 /* Write each channel */
1151 /* Write channels */
1152 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1153 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
1154 of library blocks that implement this.*/
1156 IDP_WriteProperty(chan->prop, wd);
1158 write_constraints(wd, &chan->constraints);
1160 write_motionpath(wd, chan->mpath);
1162 /* prevent crashes with autosave, when a bone duplicated in editmode has not yet been assigned to its posechannel */
1164 chan->selectflag= chan->bone->flag & BONE_SELECTED; /* gets restored on read, for library armatures */
1166 writestruct(wd, DATA, "bPoseChannel", 1, chan);
1170 for (grp=pose->agroups.first; grp; grp=grp->next)
1171 writestruct(wd, DATA, "bActionGroup", 1, grp);
1173 /* write IK param */
1174 if (pose->ikparam) {
1175 char *structname = (char *)get_ikparam_name(pose);
1177 writestruct(wd, DATA, structname, 1, pose->ikparam);
1180 /* Write this pose */
1181 writestruct(wd, DATA, "bPose", 1, pose);
1185 static void write_defgroups(WriteData *wd, ListBase *defbase)
1187 bDeformGroup *defgroup;
1189 for (defgroup=defbase->first; defgroup; defgroup=defgroup->next)
1190 writestruct(wd, DATA, "bDeformGroup", 1, defgroup);
1193 static void write_modifiers(WriteData *wd, ListBase *modbase)
1197 if (modbase == NULL) return;
1198 for (md=modbase->first; md; md= md->next) {
1199 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1200 if (mti == NULL) return;
1202 writestruct(wd, DATA, mti->structName, 1, md);
1204 if (md->type==eModifierType_Hook) {
1205 HookModifierData *hmd = (HookModifierData*) md;
1207 writedata(wd, DATA, sizeof(int)*hmd->totindex, hmd->indexar);
1209 else if(md->type==eModifierType_Cloth) {
1210 ClothModifierData *clmd = (ClothModifierData*) md;
1212 writestruct(wd, DATA, "ClothSimSettings", 1, clmd->sim_parms);
1213 writestruct(wd, DATA, "ClothCollSettings", 1, clmd->coll_parms);
1214 writestruct(wd, DATA, "EffectorWeights", 1, clmd->sim_parms->effector_weights);
1215 write_pointcaches(wd, &clmd->ptcaches);
1217 else if(md->type==eModifierType_Smoke) {
1218 SmokeModifierData *smd = (SmokeModifierData*) md;
1220 if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
1224 write_pointcaches(wd, &(smd->domain->ptcaches[0]));
1226 /* create fake pointcache so that old blender versions can read it */
1227 smd->domain->point_cache[1] = BKE_ptcache_add(&smd->domain->ptcaches[1]);
1228 smd->domain->point_cache[1]->flag |= PTCACHE_DISK_CACHE|PTCACHE_FAKE_SMOKE;
1229 smd->domain->point_cache[1]->step = 1;
1231 write_pointcaches(wd, &(smd->domain->ptcaches[1]));
1234 writestruct(wd, DATA, "SmokeDomainSettings", 1, smd->domain);
1237 /* cleanup the fake pointcache */
1238 BKE_ptcache_free_list(&smd->domain->ptcaches[1]);
1239 smd->domain->point_cache[1] = NULL;
1241 writestruct(wd, DATA, "EffectorWeights", 1, smd->domain->effector_weights);
1244 else if(smd->type & MOD_SMOKE_TYPE_FLOW)
1245 writestruct(wd, DATA, "SmokeFlowSettings", 1, smd->flow);
1246 else if(smd->type & MOD_SMOKE_TYPE_COLL)
1247 writestruct(wd, DATA, "SmokeCollSettings", 1, smd->coll);
1249 else if(md->type==eModifierType_Fluidsim) {
1250 FluidsimModifierData *fluidmd = (FluidsimModifierData*) md;
1252 writestruct(wd, DATA, "FluidsimSettings", 1, fluidmd->fss);
1254 else if (md->type==eModifierType_Collision) {
1257 CollisionModifierData *collmd = (CollisionModifierData*) md;
1258 // TODO: CollisionModifier should use pointcache
1259 // + have proper reset events before enabling this
1260 writestruct(wd, DATA, "MVert", collmd->numverts, collmd->x);
1261 writestruct(wd, DATA, "MVert", collmd->numverts, collmd->xnew);
1262 writestruct(wd, DATA, "MFace", collmd->numfaces, collmd->mfaces);
1265 else if (md->type==eModifierType_MeshDeform) {
1266 MeshDeformModifierData *mmd = (MeshDeformModifierData*) md;
1267 int size = mmd->dyngridsize;
1269 writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->bindinfluences);
1270 writedata(wd, DATA, sizeof(int)*(mmd->totvert+1), mmd->bindoffsets);
1271 writedata(wd, DATA, sizeof(float)*3*mmd->totcagevert,
1273 writestruct(wd, DATA, "MDefCell", size*size*size, mmd->dyngrid);
1274 writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->dyninfluences);
1275 writedata(wd, DATA, sizeof(int)*mmd->totvert, mmd->dynverts);
1280 static void write_objects(WriteData *wd, ListBase *idbase)
1286 if(ob->id.us>0 || wd->current) {
1288 writestruct(wd, ID_OB, "Object", 1, ob);
1290 /*Write ID Properties -- and copy this comment EXACTLY for easy finding
1291 of library blocks that implement this.*/
1292 if (ob->id.properties) IDP_WriteProperty(ob->id.properties, wd);
1294 if (ob->adt) write_animdata(wd, ob->adt);
1297 writedata(wd, DATA, sizeof(void *)*ob->totcol, ob->mat);
1298 writedata(wd, DATA, sizeof(char)*ob->totcol, ob->matbits);
1299 /* write_effects(wd, &ob->effect); */ /* not used anymore */
1300 write_properties(wd, &ob->prop);
1301 write_sensors(wd, &ob->sensors);
1302 write_controllers(wd, &ob->controllers);
1303 write_actuators(wd, &ob->actuators);
1305 if (ob->type == OB_ARMATURE) {
1306 bArmature *arm = ob->data;
1307 if (arm && ob->pose && arm->act_bone) {
1308 BLI_strncpy(ob->pose->proxy_act_bone, arm->act_bone->name, sizeof(ob->pose->proxy_act_bone));
1312 write_pose(wd, ob->pose);
1313 write_defgroups(wd, &ob->defbase);
1314 write_constraints(wd, &ob->constraints);
1315 write_motionpath(wd, ob->mpath);
1317 writestruct(wd, DATA, "PartDeflect", 1, ob->pd);
1318 writestruct(wd, DATA, "SoftBody", 1, ob->soft);
1320 write_pointcaches(wd, &ob->soft->ptcaches);
1321 writestruct(wd, DATA, "EffectorWeights", 1, ob->soft->effector_weights);
1323 writestruct(wd, DATA, "BulletSoftBody", 1, ob->bsoft);
1325 write_particlesystems(wd, &ob->particlesystem);
1326 write_modifiers(wd, &ob->modifiers);
1331 /* flush helps the compression for undo-save */
1332 mywrite(wd, MYWRITE_FLUSH, 0);
1336 static void write_vfonts(WriteData *wd, ListBase *idbase)
1343 if(vf->id.us>0 || wd->current) {
1345 writestruct(wd, ID_VF, "VFont", 1, vf);
1346 if (vf->id.properties) IDP_WriteProperty(vf->id.properties, wd);
1350 if (vf->packedfile) {
1351 pf = vf->packedfile;
1352 writestruct(wd, DATA, "PackedFile", 1, pf);
1353 writedata(wd, DATA, pf->size, pf->data);
1362 static void write_keys(WriteData *wd, ListBase *idbase)
1369 if(key->id.us>0 || wd->current) {
1371 writestruct(wd, ID_KE, "Key", 1, key);
1372 if (key->id.properties) IDP_WriteProperty(key->id.properties, wd);
1374 if (key->adt) write_animdata(wd, key->adt);
1377 kb= key->block.first;
1379 writestruct(wd, DATA, "KeyBlock", 1, kb);
1380 if(kb->data) writedata(wd, DATA, kb->totelem*key->elemsize, kb->data);
1387 /* flush helps the compression for undo-save */
1388 mywrite(wd, MYWRITE_FLUSH, 0);
1391 static void write_cameras(WriteData *wd, ListBase *idbase)
1397 if(cam->id.us>0 || wd->current) {
1399 writestruct(wd, ID_CA, "Camera", 1, cam);
1400 if (cam->id.properties) IDP_WriteProperty(cam->id.properties, wd);
1402 if (cam->adt) write_animdata(wd, cam->adt);
1409 static void write_mballs(WriteData *wd, ListBase *idbase)
1416 if(mb->id.us>0 || wd->current) {
1418 writestruct(wd, ID_MB, "MetaBall", 1, mb);
1419 if (mb->id.properties) IDP_WriteProperty(mb->id.properties, wd);
1422 writedata(wd, DATA, sizeof(void *)*mb->totcol, mb->mat);
1423 if (mb->adt) write_animdata(wd, mb->adt);
1425 ml= mb->elems.first;
1427 writestruct(wd, DATA, "MetaElem", 1, ml);
1435 static int amount_of_chars(char *str)
1437 // Since the data is saved as UTF-8 to the cu->str
1438 // The cu->len is not same as the strlen(cu->str)
1442 static void write_curves(WriteData *wd, ListBase *idbase)
1449 if(cu->id.us>0 || wd->current) {
1451 writestruct(wd, ID_CU, "Curve", 1, cu);
1454 writedata(wd, DATA, sizeof(void *)*cu->totcol, cu->mat);
1455 if (cu->id.properties) IDP_WriteProperty(cu->id.properties, wd);
1456 if (cu->adt) write_animdata(wd, cu->adt);
1459 writedata(wd, DATA, amount_of_chars(cu->str)+1, cu->str);
1460 writestruct(wd, DATA, "CharInfo", cu->len+1, cu->strinfo);
1461 writestruct(wd, DATA, "TextBox", cu->totbox, cu->tb);
1464 /* is also the order of reading */
1467 writestruct(wd, DATA, "Nurb", 1, nu);
1472 if(nu->type == CU_BEZIER)
1473 writestruct(wd, DATA, "BezTriple", nu->pntsu, nu->bezt);
1475 writestruct(wd, DATA, "BPoint", nu->pntsu*nu->pntsv, nu->bp);
1476 if(nu->knotsu) writedata(wd, DATA, KNOTSU(nu)*sizeof(float), nu->knotsu);
1477 if(nu->knotsv) writedata(wd, DATA, KNOTSV(nu)*sizeof(float), nu->knotsv);
1486 /* flush helps the compression for undo-save */
1487 mywrite(wd, MYWRITE_FLUSH, 0);
1490 static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
1495 /* Write the dvert list */
1496 writestruct(wd, DATA, "MDeformVert", count, dvlist);
1498 /* Write deformation data for each dvert */
1499 for (i=0; i<count; i++) {
1501 writestruct(wd, DATA, "MDeformWeight", dvlist[i].totweight, dvlist[i].dw);
1506 static void write_mdisps(WriteData *wd, int count, MDisps *mdlist, int external)
1511 writestruct(wd, DATA, "MDisps", count, mdlist);
1513 for(i = 0; i < count; ++i) {
1515 writedata(wd, DATA, sizeof(float)*3*mdlist[i].totdisp, mdlist[i].disps);
1521 static void write_customdata(WriteData *wd, ID *id, int count, CustomData *data, int partial_type, int partial_count)
1525 /* write external customdata (not for undo) */
1526 if(data->external && !wd->current)
1527 CustomData_external_write(data, id, CD_MASK_MESH, count, 0);
1529 writestruct(wd, DATA, "CustomDataLayer", data->maxlayer, data->layers);
1531 for (i=0; i<data->totlayer; i++) {
1532 CustomDataLayer *layer= &data->layers[i];
1533 const char *structname;
1534 int structnum, datasize;
1536 if (layer->type == CD_MDEFORMVERT) {
1537 /* layer types that allocate own memory need special handling */
1538 write_dverts(wd, count, layer->data);
1540 else if (layer->type == CD_MDISPS) {
1541 write_mdisps(wd, count, layer->data, layer->flag & CD_FLAG_EXTERNAL);
1544 CustomData_file_write_info(layer->type, &structname, &structnum);
1546 /* when using partial visibility, the MEdge and MFace layers
1547 are smaller than the original, so their type and count is
1548 passed to make this work */
1549 if (layer->type != partial_type) datasize= structnum*count;
1550 else datasize= structnum*partial_count;
1552 writestruct(wd, DATA, structname, datasize, layer->data);
1555 printf("error: this CustomDataLayer must not be written to file\n");
1560 writestruct(wd, DATA, "CustomDataExternal", 1, data->external);
1563 static void write_meshs(WriteData *wd, ListBase *idbase)
1567 mesh= idbase->first;
1569 if(mesh->id.us>0 || wd->current) {
1571 writestruct(wd, ID_ME, "Mesh", 1, mesh);
1574 if (mesh->id.properties) IDP_WriteProperty(mesh->id.properties, wd);
1575 if (mesh->adt) write_animdata(wd, mesh->adt);
1577 writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
1580 write_customdata(wd, &mesh->id, mesh->pv->totvert, &mesh->vdata, -1, 0);
1581 write_customdata(wd, &mesh->id, mesh->pv->totedge, &mesh->edata,
1582 CD_MEDGE, mesh->totedge);
1583 write_customdata(wd, &mesh->id, mesh->pv->totface, &mesh->fdata,
1584 CD_MFACE, mesh->totface);
1587 write_customdata(wd, &mesh->id, mesh->totvert, &mesh->vdata, -1, 0);
1588 write_customdata(wd, &mesh->id, mesh->totedge, &mesh->edata, -1, 0);
1589 write_customdata(wd, &mesh->id, mesh->totface, &mesh->fdata, -1, 0);
1594 writestruct(wd, DATA, "PartialVisibility", 1, mesh->pv);
1595 writedata(wd, DATA, sizeof(unsigned int)*mesh->pv->totvert, mesh->pv->vert_map);
1596 writedata(wd, DATA, sizeof(int)*mesh->pv->totedge, mesh->pv->edge_map);
1597 writestruct(wd, DATA, "MFace", mesh->pv->totface, mesh->pv->old_faces);
1598 writestruct(wd, DATA, "MEdge", mesh->pv->totedge, mesh->pv->old_edges);
1601 mesh= mesh->id.next;
1605 static void write_lattices(WriteData *wd, ListBase *idbase)
1611 if(lt->id.us>0 || wd->current) {
1613 writestruct(wd, ID_LT, "Lattice", 1, lt);
1614 if (lt->id.properties) IDP_WriteProperty(lt->id.properties, wd);
1616 /* write animdata */
1617 if (lt->adt) write_animdata(wd, lt->adt);
1620 writestruct(wd, DATA, "BPoint", lt->pntsu*lt->pntsv*lt->pntsw, lt->def);
1622 write_dverts(wd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
1629 static void write_previews(WriteData *wd, PreviewImage *prv)
1632 short w = prv->w[1];
1633 short h = prv->h[1];
1634 unsigned int *rect = prv->rect[1];
1635 /* don't write out large previews if not requested */
1636 if (!(U.flag & USER_SAVE_PREVIEWS) ) {
1639 prv->rect[1] = NULL;
1641 writestruct(wd, DATA, "PreviewImage", 1, prv);
1642 if (prv->rect[0]) writedata(wd, DATA, prv->w[0]*prv->h[0]*sizeof(unsigned int), prv->rect[0]);
1643 if (prv->rect[1]) writedata(wd, DATA, prv->w[1]*prv->h[1]*sizeof(unsigned int), prv->rect[1]);
1645 /* restore preview, we still want to keep it in memory even if not saved to file */
1646 if (!(U.flag & USER_SAVE_PREVIEWS) ) {
1649 prv->rect[1] = rect;
1654 static void write_images(WriteData *wd, ListBase *idbase)
1662 if(ima->id.us>0 || wd->current) {
1664 writestruct(wd, ID_IM, "Image", 1, ima);
1665 if (ima->id.properties) IDP_WriteProperty(ima->id.properties, wd);
1667 if (ima->packedfile) {
1668 pf = ima->packedfile;
1669 writestruct(wd, DATA, "PackedFile", 1, pf);
1670 writedata(wd, DATA, pf->size, pf->data);
1673 write_previews(wd, ima->preview);
1677 /* flush helps the compression for undo-save */
1678 mywrite(wd, MYWRITE_FLUSH, 0);
1681 static void write_textures(WriteData *wd, ListBase *idbase)
1687 if(tex->id.us>0 || wd->current) {
1689 writestruct(wd, ID_TE, "Tex", 1, tex);
1690 if (tex->id.properties) IDP_WriteProperty(tex->id.properties, wd);
1692 if (tex->adt) write_animdata(wd, tex->adt);
1695 if(tex->type == TEX_PLUGIN && tex->plugin) writestruct(wd, DATA, "PluginTex", 1, tex->plugin);
1696 if(tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
1697 if(tex->type == TEX_ENVMAP && tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
1698 if(tex->type == TEX_POINTDENSITY && tex->pd) {
1699 writestruct(wd, DATA, "PointDensity", 1, tex->pd);
1700 if(tex->pd->coba) writestruct(wd, DATA, "ColorBand", 1, tex->pd->coba);
1701 if(tex->pd->falloff_curve) write_curvemapping(wd, tex->pd->falloff_curve);
1703 if(tex->type == TEX_VOXELDATA && tex->vd) writestruct(wd, DATA, "VoxelData", 1, tex->vd);
1705 /* nodetree is integral part of texture, no libdata */
1707 writestruct(wd, DATA, "bNodeTree", 1, tex->nodetree);
1708 write_nodetree(wd, tex->nodetree);
1711 write_previews(wd, tex->preview);
1716 /* flush helps the compression for undo-save */
1717 mywrite(wd, MYWRITE_FLUSH, 0);
1720 static void write_materials(WriteData *wd, ListBase *idbase)
1727 if(ma->id.us>0 || wd->current) {
1729 writestruct(wd, ID_MA, "Material", 1, ma);
1731 /*Write ID Properties -- and copy this comment EXACTLY for easy finding
1732 of library blocks that implement this.*/
1733 /*manually set head group property to IDP_GROUP, just in case it hadn't been
1735 if (ma->id.properties) IDP_WriteProperty(ma->id.properties, wd);
1737 if (ma->adt) write_animdata(wd, ma->adt);
1739 for(a=0; a<MAX_MTEX; a++) {
1740 if(ma->mtex[a]) writestruct(wd, DATA, "MTex", 1, ma->mtex[a]);
1743 if(ma->ramp_col) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_col);
1744 if(ma->ramp_spec) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_spec);
1746 /* nodetree is integral part of material, no libdata */
1748 writestruct(wd, DATA, "bNodeTree", 1, ma->nodetree);
1749 write_nodetree(wd, ma->nodetree);
1752 write_previews(wd, ma->preview);
1758 static void write_worlds(WriteData *wd, ListBase *idbase)
1763 wrld= idbase->first;
1765 if(wrld->id.us>0 || wd->current) {
1767 writestruct(wd, ID_WO, "World", 1, wrld);
1768 if (wrld->id.properties) IDP_WriteProperty(wrld->id.properties, wd);
1770 if (wrld->adt) write_animdata(wd, wrld->adt);
1772 for(a=0; a<MAX_MTEX; a++) {
1773 if(wrld->mtex[a]) writestruct(wd, DATA, "MTex", 1, wrld->mtex[a]);
1776 write_previews(wd, wrld->preview);
1778 wrld= wrld->id.next;
1782 static void write_lamps(WriteData *wd, ListBase *idbase)
1789 if(la->id.us>0 || wd->current) {
1791 writestruct(wd, ID_LA, "Lamp", 1, la);
1792 if (la->id.properties) IDP_WriteProperty(la->id.properties, wd);
1794 if (la->adt) write_animdata(wd, la->adt);
1797 for(a=0; a<MAX_MTEX; a++) {
1798 if(la->mtex[a]) writestruct(wd, DATA, "MTex", 1, la->mtex[a]);
1802 write_curvemapping(wd, la->curfalloff);
1804 write_previews(wd, la->preview);
1812 static void write_scenes(WriteData *wd, ListBase *scebase)
1821 TransformOrientation *ts;
1822 SceneRenderLayer *srl;
1825 sce= scebase->first;
1828 writestruct(wd, ID_SCE, "Scene", 1, sce);
1829 if (sce->id.properties) IDP_WriteProperty(sce->id.properties, wd);
1831 if (sce->adt) write_animdata(wd, sce->adt);
1832 write_keyingsets(wd, &sce->keyingsets);
1835 base= sce->base.first;
1837 writestruct(wd, DATA, "Base", 1, base);
1841 tos = sce->toolsettings;
1842 writestruct(wd, DATA, "ToolSettings", 1, tos);
1844 writestruct(wd, DATA, "VPaint", 1, tos->vpaint);
1847 writestruct(wd, DATA, "VPaint", 1, tos->wpaint);
1850 writestruct(wd, DATA, "Sculpt", 1, tos->sculpt);
1853 // write_paint(wd, &tos->imapaint.paint);
1857 writestruct(wd, DATA, "Editing", 1, ed);
1859 /* reset write flags too */
1861 SEQ_BEGIN(ed, seq) {
1862 if(seq->strip) seq->strip->done= 0;
1863 writestruct(wd, DATA, "Sequence", 1, seq);
1867 SEQ_BEGIN(ed, seq) {
1868 if(seq->strip && seq->strip->done==0) {
1869 /* write strip with 'done' at 0 because readfile */
1871 if(seq->plugin) writestruct(wd, DATA, "PluginSeq", 1, seq->plugin);
1872 if(seq->effectdata) {
1875 writestruct(wd, DATA, "SolidColorVars", 1, seq->effectdata);
1878 writestruct(wd, DATA, "SpeedControlVars", 1, seq->effectdata);
1881 writestruct(wd, DATA, "WipeVars", 1, seq->effectdata);
1884 writestruct(wd, DATA, "GlowVars", 1, seq->effectdata);
1887 writestruct(wd, DATA, "TransformVars", 1, seq->effectdata);
1893 writestruct(wd, DATA, "Strip", 1, strip);
1894 if(seq->flag & SEQ_USE_CROP && strip->crop) {
1895 writestruct(wd, DATA, "StripCrop", 1, strip->crop);
1897 if(seq->flag & SEQ_USE_TRANSFORM && strip->transform) {
1898 writestruct(wd, DATA, "StripTransform", 1, strip->transform);
1900 if(seq->flag & SEQ_USE_PROXY && strip->proxy) {
1901 writestruct(wd, DATA, "StripProxy", 1, strip->proxy);
1903 if(seq->flag & SEQ_USE_COLOR_BALANCE && strip->color_balance) {
1904 writestruct(wd, DATA, "StripColorBalance", 1, strip->color_balance);
1906 if(seq->type==SEQ_IMAGE)
1907 writestruct(wd, DATA, "StripElem", MEM_allocN_len(strip->stripdata) / sizeof(struct StripElem), strip->stripdata);
1908 else if(seq->type==SEQ_MOVIE || seq->type==SEQ_RAM_SOUND || seq->type == SEQ_HD_SOUND)
1909 writestruct(wd, DATA, "StripElem", 1, strip->stripdata);
1916 /* new; meta stack too, even when its nasty restore code */
1917 for(ms= ed->metastack.first; ms; ms= ms->next) {
1918 writestruct(wd, DATA, "MetaStack", 1, ms);
1922 if (sce->r.avicodecdata) {
1923 writestruct(wd, DATA, "AviCodecData", 1, sce->r.avicodecdata);
1924 if (sce->r.avicodecdata->lpFormat) writedata(wd, DATA, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat);
1925 if (sce->r.avicodecdata->lpParms) writedata(wd, DATA, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms);
1928 if (sce->r.qtcodecdata) {
1929 writestruct(wd, DATA, "QuicktimeCodecData", 1, sce->r.qtcodecdata);
1930 if (sce->r.qtcodecdata->cdParms) writedata(wd, DATA, sce->r.qtcodecdata->cdSize, sce->r.qtcodecdata->cdParms);
1932 if (sce->r.ffcodecdata.properties) {
1933 IDP_WriteProperty(sce->r.ffcodecdata.properties, wd);
1936 /* writing dynamic list of TimeMarkers to the blend file */
1937 for(marker= sce->markers.first; marker; marker= marker->next)
1938 writestruct(wd, DATA, "TimeMarker", 1, marker);
1940 /* writing dynamic list of TransformOrientations to the blend file */
1941 for(ts = sce->transform_spaces.first; ts; ts = ts->next)
1942 writestruct(wd, DATA, "TransformOrientation", 1, ts);
1944 for(srl= sce->r.layers.first; srl; srl= srl->next)
1945 writestruct(wd, DATA, "SceneRenderLayer", 1, srl);
1948 writestruct(wd, DATA, "bNodeTree", 1, sce->nodetree);
1949 write_nodetree(wd, sce->nodetree);
1954 /* flush helps the compression for undo-save */
1955 mywrite(wd, MYWRITE_FLUSH, 0);
1958 static void write_gpencils(WriteData *wd, ListBase *lb)
1965 for (gpd= lb->first; gpd; gpd= gpd->id.next) {
1966 if (gpd->id.us>0 || wd->current) {
1967 /* write gpd data block to file */
1968 writestruct(wd, ID_GD, "bGPdata", 1, gpd);
1970 /* write grease-pencil layers to file */
1971 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
1972 writestruct(wd, DATA, "bGPDlayer", 1, gpl);
1974 /* write this layer's frames to file */
1975 for (gpf= gpl->frames.first; gpf; gpf= gpf->next) {
1976 writestruct(wd, DATA, "bGPDframe", 1, gpf);
1979 for (gps= gpf->strokes.first; gps; gps= gps->next) {
1980 writestruct(wd, DATA, "bGPDstroke", 1, gps);
1981 writestruct(wd, DATA, "bGPDspoint", gps->totpoints, gps->points);
1989 static void write_windowmanagers(WriteData *wd, ListBase *lb)
1991 wmWindowManager *wm;
1994 for(wm= lb->first; wm; wm= wm->id.next) {
1995 writestruct(wd, ID_WM, "wmWindowManager", 1, wm);
1997 for(win= wm->windows.first; win; win= win->next)
1998 writestruct(wd, DATA, "wmWindow", 1, win);
2002 static void write_region(WriteData *wd, ARegion *ar, int spacetype)
2004 writestruct(wd, DATA, "ARegion", 1, ar);
2006 if(ar->regiondata) {
2009 if(ar->regiontype==RGN_TYPE_WINDOW) {
2010 RegionView3D *rv3d= ar->regiondata;
2011 writestruct(wd, DATA, "RegionView3D", 1, rv3d);
2014 writestruct(wd, DATA, "RegionView3D", 1, rv3d->localvd);
2016 writestruct(wd, DATA, "BoundBox", 1, rv3d->clipbb);
2020 printf("regiondata write missing!\n");
2023 printf("regiondata write missing!\n");
2028 static void write_screens(WriteData *wd, ListBase *scrbase)
2039 /* in 2.50+ files, the file identifier for screens is patched, forward compatibility */
2040 writestruct(wd, ID_SCRN, "Screen", 1, sc);
2041 if (sc->id.properties)
2042 IDP_WriteProperty(sc->id.properties, wd);
2045 for(sv= sc->vertbase.first; sv; sv= sv->next)
2046 writestruct(wd, DATA, "ScrVert", 1, sv);
2048 for(se= sc->edgebase.first; se; se= se->next)
2049 writestruct(wd, DATA, "ScrEdge", 1, se);
2051 for(sa= sc->areabase.first; sa; sa= sa->next) {
2056 writestruct(wd, DATA, "ScrArea", 1, sa);
2058 for(ar= sa->regionbase.first; ar; ar= ar->next) {
2059 write_region(wd, ar, sa->spacetype);
2061 for(pa= ar->panels.first; pa; pa= pa->next)
2062 writestruct(wd, DATA, "Panel", 1, pa);
2065 sl= sa->spacedata.first;
2067 for(ar= sl->regionbase.first; ar; ar= ar->next)
2068 write_region(wd, ar, sl->spacetype);
2070 if(sl->spacetype==SPACE_VIEW3D) {
2071 View3D *v3d= (View3D *) sl;
2073 writestruct(wd, DATA, "View3D", 1, v3d);
2074 for (bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
2075 writestruct(wd, DATA, "BGpic", 1, bgpic);
2076 if(v3d->localvd) writestruct(wd, DATA, "View3D", 1, v3d->localvd);
2078 else if(sl->spacetype==SPACE_IPO) {
2079 SpaceIpo *sipo= (SpaceIpo *)sl;
2080 ListBase tmpGhosts = sipo->ghostCurves;
2082 /* temporarily disable ghost curves when saving */
2083 sipo->ghostCurves.first= sipo->ghostCurves.last= NULL;
2085 writestruct(wd, DATA, "SpaceIpo", 1, sl);
2086 if(sipo->ads) writestruct(wd, DATA, "bDopeSheet", 1, sipo->ads);
2088 /* reenable ghost curves */
2089 sipo->ghostCurves= tmpGhosts;
2091 else if(sl->spacetype==SPACE_BUTS) {
2092 writestruct(wd, DATA, "SpaceButs", 1, sl);
2094 else if(sl->spacetype==SPACE_FILE) {
2095 writestruct(wd, DATA, "SpaceFile", 1, sl);
2097 else if(sl->spacetype==SPACE_SEQ) {
2098 writestruct(wd, DATA, "SpaceSeq", 1, sl);
2100 else if(sl->spacetype==SPACE_OUTLINER) {
2101 SpaceOops *so= (SpaceOops *)sl;
2103 writestruct(wd, DATA, "SpaceOops", 1, so);
2107 writestruct(wd, DATA, "TreeStore", 1, so->treestore);
2108 if(so->treestore->data)
2109 writestruct(wd, DATA, "TreeStoreElem", so->treestore->usedelem, so->treestore->data);
2112 else if(sl->spacetype==SPACE_IMAGE) {
2113 SpaceImage *sima= (SpaceImage *)sl;
2115 writestruct(wd, DATA, "SpaceImage", 1, sl);
2117 write_curvemapping(wd, sima->cumap);
2119 else if(sl->spacetype==SPACE_IMASEL) {
2120 // XXX: depreceated... do we still want to keep this?
2121 writestruct(wd, DATA, "SpaceImaSel", 1, sl);
2123 else if(sl->spacetype==SPACE_TEXT) {
2124 writestruct(wd, DATA, "SpaceText", 1, sl);
2126 else if(sl->spacetype==SPACE_SCRIPT) {
2127 SpaceScript *sc = (SpaceScript*)sl;
2128 sc->but_refs = NULL;
2129 writestruct(wd, DATA, "SpaceScript", 1, sl);
2131 else if(sl->spacetype==SPACE_ACTION) {
2132 writestruct(wd, DATA, "SpaceAction", 1, sl);
2134 else if(sl->spacetype==SPACE_SOUND) {
2135 writestruct(wd, DATA, "SpaceSound", 1, sl);
2137 else if(sl->spacetype==SPACE_NLA){
2138 SpaceNla *snla= (SpaceNla *)sl;
2140 writestruct(wd, DATA, "SpaceNla", 1, snla);
2141 if(snla->ads) writestruct(wd, DATA, "bDopeSheet", 1, snla->ads);
2143 else if(sl->spacetype==SPACE_TIME){
2144 writestruct(wd, DATA, "SpaceTime", 1, sl);
2146 else if(sl->spacetype==SPACE_NODE){
2147 writestruct(wd, DATA, "SpaceNode", 1, sl);
2149 else if(sl->spacetype==SPACE_LOGIC){
2150 writestruct(wd, DATA, "SpaceLogic", 1, sl);
2152 else if(sl->spacetype==SPACE_CONSOLE) {
2153 SpaceConsole *con = (SpaceConsole*)sl;
2156 for (cl=con->history.first; cl; cl=cl->next) {
2157 /* 'len_alloc' is invalid on write, set from 'len' on read */
2158 writestruct(wd, DATA, "ConsoleLine", 1, cl);
2159 writedata(wd, DATA, cl->len+1, cl->line);
2161 writestruct(wd, DATA, "SpaceConsole", 1, sl);
2164 else if(sl->spacetype==SPACE_USERPREF) {
2165 writestruct(wd, DATA, "SpaceUserPref", 1, sl);
2176 static void write_libraries(WriteData *wd, Main *main)
2178 ListBase *lbarray[30];
2180 int a, tot, foundone;
2182 for(; main; main= main->next) {
2184 a=tot= set_listbasepointers(main, lbarray);
2186 /* test: is lib being used */
2189 for(id= lbarray[tot]->first; id; id= id->next) {
2190 if(id->us>0 && (id->flag & LIB_EXTERN)) {
2199 writestruct(wd, ID_LI, "Library", 1, main->curlib);
2202 for(id= lbarray[a]->first; id; id= id->next) {
2203 if(id->us>0 && (id->flag & LIB_EXTERN)) {
2204 writestruct(wd, ID_ID, "ID", 1, id);
2212 static void write_bone(WriteData *wd, Bone* bone)
2216 // PATCH for upward compatibility after 2.37+ armature recode
2217 bone->size[0]= bone->size[1]= bone->size[2]= 1.0f;
2220 writestruct(wd, DATA, "Bone", 1, bone);
2222 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
2223 of library blocks that implement this.*/
2225 IDP_WriteProperty(bone->prop, wd);
2228 cbone= bone->childbase.first;
2230 write_bone(wd, cbone);
2235 static void write_armatures(WriteData *wd, ListBase *idbase)
2242 if (arm->id.us>0 || wd->current) {
2243 writestruct(wd, ID_AR, "bArmature", 1, arm);
2244 if (arm->id.properties) IDP_WriteProperty(arm->id.properties, wd);
2246 if (arm->adt) write_animdata(wd, arm->adt);
2249 bone= arm->bonebase.first;
2251 write_bone(wd, bone);
2258 /* flush helps the compression for undo-save */
2259 mywrite(wd, MYWRITE_FLUSH, 0);
2262 static void write_texts(WriteData *wd, ListBase *idbase)
2268 text= idbase->first;
2270 if ( (text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) text->flags &= ~TXT_ISEXT;
2273 writestruct(wd, ID_TXT, "Text", 1, text);
2274 if(text->name) writedata(wd, DATA, strlen(text->name)+1, text->name);
2275 if (text->id.properties) IDP_WriteProperty(text->id.properties, wd);
2277 if(!(text->flags & TXT_ISEXT)) {
2278 /* now write the text data, in two steps for optimization in the readfunction */
2279 tmp= text->lines.first;
2281 writestruct(wd, DATA, "TextLine", 1, tmp);
2285 tmp= text->lines.first;
2287 writedata(wd, DATA, tmp->len+1, tmp->line);
2292 mrk= text->markers.first;
2294 writestruct(wd, DATA, "TextMarker", 1, mrk);
2300 text= text->id.next;
2303 /* flush helps the compression for undo-save */
2304 mywrite(wd, MYWRITE_FLUSH, 0);
2307 static void write_sounds(WriteData *wd, ListBase *idbase)
2313 sound= idbase->first;
2315 if(sound->id.us>0 || wd->current) {
2317 writestruct(wd, ID_SO, "bSound", 1, sound);
2318 if (sound->id.properties) IDP_WriteProperty(sound->id.properties, wd);
2320 if (sound->packedfile) {
2321 pf = sound->packedfile;
2322 writestruct(wd, DATA, "PackedFile", 1, pf);
2323 writedata(wd, DATA, pf->size, pf->data);
2326 sound= sound->id.next;
2329 /* flush helps the compression for undo-save */
2330 mywrite(wd, MYWRITE_FLUSH, 0);
2333 static void write_groups(WriteData *wd, ListBase *idbase)
2338 for(group= idbase->first; group; group= group->id.next) {
2339 if(group->id.us>0 || wd->current) {
2341 writestruct(wd, ID_GR, "Group", 1, group);
2342 if (group->id.properties) IDP_WriteProperty(group->id.properties, wd);
2344 go= group->gobject.first;
2346 writestruct(wd, DATA, "GroupObject", 1, go);
2353 static void write_nodetrees(WriteData *wd, ListBase *idbase)
2357 for(ntree=idbase->first; ntree; ntree= ntree->id.next) {
2358 if (ntree->id.us>0 || wd->current) {
2359 writestruct(wd, ID_NT, "bNodeTree", 1, ntree);
2360 write_nodetree(wd, ntree);
2362 if (ntree->id.properties) IDP_WriteProperty(ntree->id.properties, wd);
2364 if (ntree->adt) write_animdata(wd, ntree->adt);
2369 static void write_brushes(WriteData *wd, ListBase *idbase)
2373 for(brush=idbase->first; brush; brush= brush->id.next) {
2374 if(brush->id.us>0 || wd->current) {
2375 writestruct(wd, ID_BR, "Brush", 1, brush);
2376 if (brush->id.properties) IDP_WriteProperty(brush->id.properties, wd);
2378 writestruct(wd, DATA, "MTex", 1, &brush->mtex);
2381 write_curvemapping(wd, brush->curve);
2386 static void write_scripts(WriteData *wd, ListBase *idbase)
2390 for(script=idbase->first; script; script= script->id.next) {
2391 if(script->id.us>0 || wd->current) {
2392 writestruct(wd, ID_SCRIPT, "Script", 1, script);
2393 if (script->id.properties) IDP_WriteProperty(script->id.properties, wd);
2398 /* context is usually defined by WM, two cases where no WM is available:
2399 * - for forward compatibility, curscreen has to be saved
2400 * - for undofile, curscene needs to be saved */
2401 static void write_global(WriteData *wd, int fileflags, Main *mainvar)
2407 /* prevent mem checkers from complaining */
2409 memset(fg.filename, 0, sizeof(fg.filename));
2411 current_screen_compat(mainvar, &screen);
2413 /* XXX still remap G */
2414 fg.curscreen= screen;
2415 fg.curscene= screen->scene;
2416 fg.displaymode= G.displaymode;
2417 fg.winpos= G.winpos;
2418 fg.fileflags= (fileflags & ~(G_FILE_NO_UI|G_FILE_RELATIVE_REMAP)); // prevent to save this, is not good convention, and feature with concerns...
2420 BLI_strncpy(fg.filename, mainvar->name, sizeof(fg.filename));
2422 sprintf(subvstr, "%4d", BLENDER_SUBVERSION);
2423 memcpy(fg.subvstr, subvstr, 4);
2425 fg.subversion= BLENDER_SUBVERSION;
2426 fg.minversion= BLENDER_MINVERSION;
2427 fg.minsubversion= BLENDER_MINSUBVERSION;
2428 #ifdef NAN_BUILDINFO
2430 extern char build_rev[];
2431 fg.revision= atoi(build_rev);
2436 writestruct(wd, GLOB, "FileGlobal", 1, &fg);
2439 /* preview image, first 2 values are width and height
2440 * second are an RGBA image (unsigned char)
2441 * note, this uses 'TEST' since new types will segfault on file load for older blender versions.
2443 static void write_thumb(WriteData *wd, int *img)
2446 writedata(wd, TEST, (2 + img[0] * img[1]) * sizeof(int), img);
2449 /* if MemFile * there's filesave to memory */
2450 static int write_file_handle(Main *mainvar, int handle, MemFile *compare, MemFile *current,
2451 int write_user_block, int write_flags, int *thumb)
2458 blo_split_main(&mainlist, mainvar);
2460 wd= bgnwrite(handle, compare, current);
2462 sprintf(buf, "BLENDER%c%c%.3d", (sizeof(void*)==8)?'-':'_', (ENDIAN_ORDER==B_ENDIAN)?'V':'v', BLENDER_VERSION);
2463 mywrite(wd, buf, 12);
2465 write_renderinfo(wd, mainvar);
2466 write_thumb(wd, thumb);
2467 write_global(wd, write_flags, mainvar);
2469 /* no UI save in undo */
2471 write_windowmanagers(wd, &mainvar->wm);
2472 write_screens (wd, &mainvar->screen);
2474 write_scenes (wd, &mainvar->scene);
2475 write_curves (wd, &mainvar->curve);
2476 write_mballs (wd, &mainvar->mball);
2477 write_images (wd, &mainvar->image);
2478 write_cameras (wd, &mainvar->camera);
2479 write_lamps (wd, &mainvar->lamp);
2480 write_lattices (wd, &mainvar->latt);
2481 write_vfonts (wd, &mainvar->vfont);
2482 write_keys (wd, &mainvar->key);
2483 write_worlds (wd, &mainvar->world);
2484 write_texts (wd, &mainvar->text);
2485 write_sounds (wd, &mainvar->sound);
2486 write_groups (wd, &mainvar->group);
2487 write_armatures(wd, &mainvar->armature);
2488 write_actions (wd, &mainvar->action);
2489 write_objects (wd, &mainvar->object);
2490 write_materials(wd, &mainvar->mat);
2491 write_textures (wd, &mainvar->tex);
2492 write_meshs (wd, &mainvar->mesh);
2493 write_particlesettings(wd, &mainvar->particle);
2494 write_nodetrees(wd, &mainvar->nodetree);
2495 write_brushes (wd, &mainvar->brush);
2496 write_scripts (wd, &mainvar->script);
2497 write_gpencils (wd, &mainvar->gpencil);
2498 write_libraries(wd, mainvar->next);
2500 if (write_user_block) {
2504 /* dna as last, because (to be implemented) test for which structs are written */
2505 writedata(wd, DNA1, wd->sdna->datalen, wd->sdna->data);
2508 memset(&bhead, 0, sizeof(BHead));
2510 mywrite(wd, &bhead, sizeof(BHead));
2512 blo_join_main(&mainlist);
2514 return endwrite(wd);
2517 /* return: success (1) */
2518 int BLO_write_file(Main *mainvar, char *dir, int write_flags, ReportList *reports, int *thumb)
2520 char userfilename[FILE_MAXDIR+FILE_MAXFILE];
2521 char tempname[FILE_MAXDIR+FILE_MAXFILE+1];
2522 int file, err, write_user_block;
2524 /* open temporary file, so we preserve the original in case we crash */
2525 BLI_snprintf(tempname, sizeof(tempname), "%s@", dir);
2527 file = open(tempname,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
2529 BKE_reportf(reports, RPT_ERROR, "Can't open file %s for writing: %s.", tempname, strerror(errno));
2533 /* remapping of relative paths to new file location */
2534 if(write_flags & G_FILE_RELATIVE_REMAP) {
2535 char dir1[FILE_MAXDIR+FILE_MAXFILE];
2536 char dir2[FILE_MAXDIR+FILE_MAXFILE];
2537 BLI_split_dirfile(dir, dir1, NULL);
2538 BLI_split_dirfile(mainvar->name, dir2, NULL);
2540 /* just incase there is some subtle difference */
2541 BLI_cleanup_dir(mainvar->name, dir1);
2542 BLI_cleanup_dir(mainvar->name, dir2);
2544 if(strcmp(dir1, dir2)==0)
2545 write_flags &= ~G_FILE_RELATIVE_REMAP;
2547 makeFilesAbsolute(mainvar, G.main->name, NULL);
2550 BLI_make_file_string(G.main->name, userfilename, BLI_get_folder_create(BLENDER_USER_CONFIG, NULL), BLENDER_STARTUP_FILE);
2551 write_user_block= (BLI_path_cmp(dir, userfilename) == 0);
2553 if(write_flags & G_FILE_RELATIVE_REMAP)
2554 makeFilesRelative(mainvar, dir, NULL); /* note, making relative to something OTHER then G.main->name */
2556 /* actual file writing */
2557 err= write_file_handle(mainvar, file, NULL,NULL, write_user_block, write_flags, thumb);
2560 /* rename/compress */
2562 if(write_flags & G_FILE_COMPRESS) {
2563 /* compressed files have the same ending as regular files... only from 2.4!!! */
2564 char gzname[FILE_MAXDIR+FILE_MAXFILE+4];
2567 /* first write compressed to separate @.gz */
2568 BLI_snprintf(gzname, sizeof(gzname), "%s@.gz", dir);
2569 ret = BLI_gzip(tempname, gzname);
2572 /* now rename to real file name, and delete temp @ file too */
2573 if(BLI_rename(gzname, dir) != 0) {
2574 BKE_report(reports, RPT_ERROR, "Can't change old file. File saved with @.");
2578 BLI_delete(tempname, 0, 0);
2581 BKE_report(reports, RPT_ERROR, "Failed opening .gz file.");
2585 BKE_report(reports, RPT_ERROR, "Failed opening .blend file for compression.");
2589 else if(BLI_rename(tempname, dir) != 0) {
2590 BKE_report(reports, RPT_ERROR, "Can't change old file. File saved with @");
2596 BKE_report(reports, RPT_ERROR, strerror(errno));
2605 /* return: success (1) */
2606 int BLO_write_file_mem(Main *mainvar, MemFile *compare, MemFile *current, int write_flags)
2610 err= write_file_handle(mainvar, 0, compare, current, 0, write_flags, NULL);
2612 if(err==0) return 1;
2617 /* Runtime writing */
2619 static char *get_runtime_path(char *exename) {
2620 char *installpath= get_install_dir();
2626 char *path= BLI_sprintfN("%s%c%s", installpath, SEP, exename);
2629 MEM_freeN(installpath);
2633 MEM_freeN(installpath);
2641 static int recursive_copy_runtime(const char *outname, char *exename, ReportList *reports)
2643 char *runtime = get_runtime_path(exename);
2644 char command[2 * (FILE_MAXDIR+FILE_MAXFILE) + 32];
2645 int progfd = -1, error= 0;
2648 BKE_report(reports, RPT_ERROR, "Unable to find runtime");
2652 //printf("runtimepath %s\n", runtime);
2654 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
2656 BKE_report(reports, RPT_ERROR, "Unable to find runtime");
2661 sprintf(command, "/bin/cp -R \"%s\" \"%s\"", runtime, outname);
2662 //printf("command %s\n", command);
2663 if (system(command) == -1) {
2664 BKE_report(reports, RPT_ERROR, "Couldn't copy runtime");
2677 int BLO_write_runtime(Main *mainvar, const char *file, char *exename, ReportList *reports)
2679 char gamename[FILE_MAXDIR+FILE_MAXFILE];
2680 int outfd = -1, error= 0;
2682 // remove existing file / bundle
2683 //printf("Delete file %s\n", file);
2684 BLI_delete(file, 0, TRUE);
2686 if (!recursive_copy_runtime(file, exename, reports)) {
2691 BLI_snprintf(gamename, sizeof(gamename), "%s/Contents/Resources/game.blend", file);
2692 //printf("gamename %s\n", gamename);
2693 outfd= open(gamename, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
2696 write_file_handle(mainvar, outfd, NULL,NULL, 0, G.fileflags, NULL);
2698 if (write(outfd, " ", 1) != 1) {
2699 BKE_report(reports, RPT_ERROR, "Unable to write to output file.");
2704 BKE_report(reports, RPT_ERROR, "Unable to open blenderfile.");
2712 BKE_reports_prepend(reports, "Unable to make runtime: ");
2716 #else /* !__APPLE__ */
2718 static int handle_append_runtime(int handle, char *exename, ReportList *reports)
2720 char *runtime= get_runtime_path(exename);
2721 unsigned char buf[1024];
2722 int count, progfd= -1, error= 0;
2724 if (!BLI_exists(runtime)) {
2725 BKE_report(reports, RPT_ERROR, "Unable to find runtime.");
2730 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
2732 BKE_report(reports, RPT_ERROR, "Unable to find runtime.@");
2737 while ((count= read(progfd, buf, sizeof(buf)))>0) {
2738 if (write(handle, buf, count)!=count) {
2739 BKE_report(reports, RPT_ERROR, "Unable to write to output file.");
2754 static int handle_write_msb_int(int handle, int i)
2756 unsigned char buf[4];
2757 buf[0]= (i>>24)&0xFF;
2758 buf[1]= (i>>16)&0xFF;
2759 buf[2]= (i>>8)&0xFF;
2760 buf[3]= (i>>0)&0xFF;
2762 return (write(handle, buf, 4)==4);
2765 int BLO_write_runtime(Main *mainvar, const char *file, char *exename, ReportList *reports)
2767 int outfd= open(file, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
2768 int datastart, error= 0;
2771 BKE_report(reports, RPT_ERROR, "Unable to open output file.");
2775 if (!handle_append_runtime(outfd, exename, reports)) {
2780 datastart= lseek(outfd, 0, SEEK_CUR);
2782 write_file_handle(mainvar, outfd, NULL,NULL, 0, G.fileflags, NULL);
2784 if (!handle_write_msb_int(outfd, datastart) || (write(outfd, "BRUNTIME", 8)!=8)) {
2785 BKE_report(reports, RPT_ERROR, "Unable to write to output file.");
2794 BKE_reports_prepend(reports, "Unable to make runtime: ");
2798 #endif /* !__APPLE__ */