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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *****
30 FILEFORMAT: IFF-style structure (but not IFF compatible!)
33 BLENDER_V100 12 bytes (versie 1.00)
34 V = big endian, v = little endian
35 _ = 4 byte pointer, - = 8 byte pointer
37 datablocks: also see struct BHead
39 <bh.len> int, len data after BHead
40 <bh.old> void, old pointer
42 <bh.nr> int, in case of array: amount of structs
47 Almost all data in Blender are structures. Each struct saved
48 gets a BHead header. With BHead the struct can be linked again
49 and compared with StructDNA .
53 Preferred writing order: (not really a must, but why would you do it random?)
54 Any case: direct data is ALWAYS after the lib block
59 - write associated direct data
64 - write the ID of LibBlock
65 - write FileGlobal (some global vars)
67 - write USER if filename is ~/.B.blend
81 #include "BLI_winstuff.h"
83 #include <process.h> // for getpid
92 #include "DNA_anim_types.h"
93 #include "DNA_armature_types.h"
94 #include "DNA_action_types.h"
95 #include "DNA_actuator_types.h"
96 #include "DNA_boid_types.h"
97 #include "DNA_brush_types.h"
98 #include "DNA_camera_types.h"
99 #include "DNA_cloth_types.h"
100 #include "DNA_color_types.h"
101 #include "DNA_constraint_types.h"
102 #include "DNA_controller_types.h"
103 #include "DNA_curve_types.h"
104 #include "DNA_customdata_types.h"
105 #include "DNA_effect_types.h"
106 #include "DNA_genfile.h"
107 #include "DNA_group_types.h"
108 #include "DNA_gpencil_types.h"
109 #include "DNA_image_types.h"
110 #include "DNA_ipo_types.h" // XXX depreceated - animsys
111 #include "DNA_fileglobal_types.h"
112 #include "DNA_key_types.h"
113 #include "DNA_lattice_types.h"
114 #include "DNA_listBase.h" /* for Listbase, the type of samples, ...*/
115 #include "DNA_lamp_types.h"
116 #include "DNA_meta_types.h"
117 #include "DNA_mesh_types.h"
118 #include "DNA_meshdata_types.h"
119 #include "DNA_material_types.h"
120 #include "DNA_modifier_types.h"
121 #include "DNA_nla_types.h"
122 #include "DNA_node_types.h"
123 #include "DNA_object_types.h"
124 #include "DNA_object_force.h"
125 #include "DNA_outliner_types.h"
126 #include "DNA_packedFile_types.h"
127 #include "DNA_particle_types.h"
128 #include "DNA_property_types.h"
129 #include "DNA_scene_types.h"
130 #include "DNA_sdna_types.h"
131 #include "DNA_sequence_types.h"
132 #include "DNA_sensor_types.h"
133 #include "DNA_space_types.h"
134 #include "DNA_screen_types.h"
135 #include "DNA_sound_types.h"
136 #include "DNA_texture_types.h"
137 #include "DNA_text_types.h"
138 #include "DNA_view3d_types.h"
139 #include "DNA_vfont_types.h"
140 #include "DNA_userdef_types.h"
141 #include "DNA_world_types.h"
142 #include "DNA_windowmanager_types.h"
144 #include "MEM_guardedalloc.h" // MEM_freeN
145 #include "BLI_blenlib.h"
146 #include "BLI_linklist.h"
148 #include "BKE_action.h"
149 #include "BKE_blender.h"
150 #include "BKE_cloth.h"
151 #include "BKE_curve.h"
152 #include "BKE_customdata.h"
153 #include "BKE_constraint.h"
154 #include "BKE_global.h" // for G
155 #include "BKE_library.h" // for set_listbasepointers
156 #include "BKE_main.h"
157 #include "BKE_node.h"
158 #include "BKE_packedFile.h" // for packAll
159 #include "BKE_report.h"
160 #include "BKE_screen.h" // for waitcursor
161 #include "BKE_sequence.h"
162 #include "BKE_sound.h" /* ... and for samples */
163 #include "BKE_utildefines.h" // for defines
164 #include "BKE_modifier.h"
165 #include "BKE_idprop.h"
166 #include "BKE_fcurve.h"
168 #include "BLO_writefile.h"
169 #include "BLO_readfile.h"
170 #include "BLO_undofile.h"
172 #include "readfile.h"
176 /* ********* my write, buffered writing with minimum size chunks ************ */
178 #define MYWRITE_BUFFER_SIZE 100000
179 #define MYWRITE_MAX_CHUNK 32768
186 MemFile *compare, *current;
188 int tot, count, error, memsize;
191 static WriteData *writedata_new(int file)
193 WriteData *wd= MEM_callocN(sizeof(*wd), "writedata");
195 /* XXX, see note about this in readfile.c, remove
196 * once we have an xp lock - zr
199 if (wd == NULL) return NULL;
201 wd->sdna= DNA_sdna_from_data(DNAstr, DNAlen, 0);
205 wd->buf= MEM_mallocN(MYWRITE_BUFFER_SIZE, "wd->buf");
210 static void writedata_do_write(WriteData *wd, void *mem, int memlen)
212 if ((wd == NULL) || wd->error || (mem == NULL) || memlen < 1) return;
213 if (wd->error) return;
215 /* memory based save */
217 add_memfilechunk(NULL, wd->current, mem, memlen);
220 if (write(wd->file, mem, memlen) != memlen)
226 static void writedata_free(WriteData *wd)
228 DNA_sdna_free(wd->sdna);
239 * Low level WRITE(2) wrapper that buffers data
240 * @param adr Pointer to new chunk of data
241 * @param len Length of new chunk of data
242 * @warning Talks to other functions with global parameters
245 #define MYWRITE_FLUSH NULL
247 static void mywrite( WriteData *wd, void *adr, int len)
249 if (wd->error) return;
251 /* flush helps compression for undo-save */
252 if(adr==MYWRITE_FLUSH) {
254 writedata_do_write(wd, wd->buf, wd->count);
262 /* if we have a single big chunk, write existing data in
263 * buffer and write out big chunk in smaller pieces */
264 if(len>MYWRITE_MAX_CHUNK) {
266 writedata_do_write(wd, wd->buf, wd->count);
271 int writelen= MIN2(len, MYWRITE_MAX_CHUNK);
272 writedata_do_write(wd, adr, writelen);
273 adr = (char*)adr + writelen;
280 /* if data would overflow buffer, write out the buffer */
281 if(len+wd->count>MYWRITE_BUFFER_SIZE-1) {
282 writedata_do_write(wd, wd->buf, wd->count);
286 /* append data at end of buffer */
287 memcpy(&wd->buf[wd->count], adr, len);
292 * BeGiN initializer for mywrite
293 * @param file File descriptor
294 * @param write_flags Write parameters
295 * @warning Talks to other functions with global parameters
297 static WriteData *bgnwrite(int file, MemFile *compare, MemFile *current, int write_flags)
299 WriteData *wd= writedata_new(file);
301 if (wd == NULL) return NULL;
303 wd->compare= compare;
304 wd->current= current;
305 /* this inits comparing */
306 add_memfilechunk(compare, NULL, NULL, 0);
312 * END the mywrite wrapper
313 * @return 1 if write failed
314 * @return unknown global variable otherwise
315 * @warning Talks to other functions with global parameters
317 static int endwrite(WriteData *wd)
322 writedata_do_write(wd, wd->buf, wd->count);
332 /* ********** WRITE FILE ****************** */
334 static void writestruct(WriteData *wd, int filecode, char *structname, int nr, void *adr)
339 if(adr==NULL || nr==0) return;
346 bh.SDNAnr= DNA_struct_find_nr(wd->sdna, structname);
348 printf("error: can't find SDNA code <%s>\n", structname);
351 sp= wd->sdna->structs[bh.SDNAnr];
353 bh.len= nr*wd->sdna->typelens[sp[0]];
355 if(bh.len==0) return;
357 mywrite(wd, &bh, sizeof(BHead));
358 mywrite(wd, adr, bh.len);
361 static void writedata(WriteData *wd, int filecode, int len, void *adr) /* do not use for structs */
378 mywrite(wd, &bh, sizeof(BHead));
379 if(len) mywrite(wd, adr, len);
382 /* *************** writing some direct data structs used in more code parts **************** */
383 /*These functions are used by blender's .blend system for file saving/loading.*/
384 void IDP_WriteProperty_OnlyData(IDProperty *prop, void *wd);
385 void IDP_WriteProperty(IDProperty *prop, void *wd);
386 static void write_animdata(WriteData *wd, AnimData *adt); // XXX code needs reshuffling, but not before NLA SoC is merged back into 2.5
388 static void IDP_WriteArray(IDProperty *prop, void *wd)
390 /*REMEMBER to set totalen to len in the linking code!!*/
391 if (prop->data.pointer) {
392 writedata(wd, DATA, MEM_allocN_len(prop->data.pointer), prop->data.pointer);
394 if(prop->subtype == IDP_GROUP) {
395 IDProperty **array= prop->data.pointer;
398 for(a=0; a<prop->len; a++)
399 IDP_WriteProperty(array[a], wd);
404 static void IDP_WriteIDPArray(IDProperty *prop, void *wd)
406 /*REMEMBER to set totalen to len in the linking code!!*/
407 if (prop->data.pointer) {
408 IDProperty **array = prop->data.pointer;
411 writedata(wd, DATA, MEM_allocN_len(prop->data.pointer), prop->data.pointer);
413 for(a=0; a<prop->len; a++)
414 IDP_WriteProperty(array[a], wd);
418 static void IDP_WriteString(IDProperty *prop, void *wd)
420 /*REMEMBER to set totalen to len in the linking code!!*/
421 writedata(wd, DATA, prop->len+1, prop->data.pointer);
424 static void IDP_WriteGroup(IDProperty *prop, void *wd)
428 for (loop=prop->data.group.first; loop; loop=loop->next) {
429 IDP_WriteProperty(loop, wd);
433 /* Functions to read/write ID Properties */
434 void IDP_WriteProperty_OnlyData(IDProperty *prop, void *wd)
436 switch (prop->type) {
438 IDP_WriteGroup(prop, wd);
441 IDP_WriteString(prop, wd);
444 IDP_WriteArray(prop, wd);
447 IDP_WriteIDPArray(prop, wd);
452 void IDP_WriteProperty(IDProperty *prop, void *wd)
454 writestruct(wd, DATA, "IDProperty", 1, prop);
455 IDP_WriteProperty_OnlyData(prop, wd);
458 static void write_curvemapping(WriteData *wd, CurveMapping *cumap)
462 writestruct(wd, DATA, "CurveMapping", 1, cumap);
463 for(a=0; a<CM_TOT; a++)
464 writestruct(wd, DATA, "CurveMapPoint", cumap->cm[a].totpoint, cumap->cm[a].curve);
467 /* this is only direct data, tree itself should have been written */
468 static void write_nodetree(WriteData *wd, bNodeTree *ntree)
474 /* for link_list() speed, we write per list */
476 for(node= ntree->nodes.first; node; node= node->next)
477 writestruct(wd, DATA, "bNode", 1, node);
479 for(node= ntree->nodes.first; node; node= node->next) {
480 if(node->storage && node->type!=NODE_DYNAMIC) {
481 /* could be handlerized at some point, now only 1 exception still */
482 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
483 write_curvemapping(wd, node->storage);
484 else if(ntree->type==NTREE_COMPOSIT && (node->type==CMP_NODE_TIME || node->type==CMP_NODE_CURVE_VEC || node->type==CMP_NODE_CURVE_RGB))
485 write_curvemapping(wd, node->storage);
486 else if(ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) )
487 write_curvemapping(wd, node->storage);
489 writestruct(wd, DATA, node->typeinfo->storagename, 1, node->storage);
491 for(sock= node->inputs.first; sock; sock= sock->next)
492 writestruct(wd, DATA, "bNodeSocket", 1, sock);
493 for(sock= node->outputs.first; sock; sock= sock->next)
494 writestruct(wd, DATA, "bNodeSocket", 1, sock);
497 for(link= ntree->links.first; link; link= link->next)
498 writestruct(wd, DATA, "bNodeLink", 1, link);
501 static void current_screen_compat(Main *mainvar, bScreen **screen)
506 /* find a global current screen in the first open window, to have
507 * a reasonable default for reading in older versions */
508 wm= mainvar->wm.first;
509 window= (wm)? wm->windows.first: NULL;
510 *screen= (window)? window->screen: NULL;
513 static void write_renderinfo(WriteData *wd, Main *mainvar) /* for renderdeamon */
519 /* XXX in future, handle multiple windows with multiple screnes? */
520 current_screen_compat(mainvar, &curscreen);
522 for(sce= mainvar->scene.first; sce; sce= sce->id.next) {
523 if(sce->id.lib==NULL && ( sce==curscreen->scene || (sce->r.scemode & R_BG_RENDER)) ) {
524 data[0]= sce->r.sfra;
525 data[1]= sce->r.efra;
527 memset(data+2, 0, sizeof(int)*6);
528 strncpy((char *)(data+2), sce->id.name+2, 21);
530 writedata(wd, REND, 32, data);
535 static void write_userdef(WriteData *wd)
539 writestruct(wd, USER, "UserDef", 1, &U);
541 btheme= U.themes.first;
543 writestruct(wd, DATA, "bTheme", 1, btheme);
544 btheme= btheme->next;
548 static void write_boid_state(WriteData *wd, BoidState *state)
550 BoidRule *rule = state->rules.first;
551 //BoidCondition *cond = state->conditions.first;
553 writestruct(wd, DATA, "BoidState", 1, state);
555 for(; rule; rule=rule->next) {
557 case eBoidRuleType_Goal:
558 case eBoidRuleType_Avoid:
559 writestruct(wd, DATA, "BoidRuleGoalAvoid", 1, rule);
561 case eBoidRuleType_AvoidCollision:
562 writestruct(wd, DATA, "BoidRuleAvoidCollision", 1, rule);
564 case eBoidRuleType_FollowLeader:
565 writestruct(wd, DATA, "BoidRuleFollowLeader", 1, rule);
567 case eBoidRuleType_AverageSpeed:
568 writestruct(wd, DATA, "BoidRuleAverageSpeed", 1, rule);
570 case eBoidRuleType_Fight:
571 writestruct(wd, DATA, "BoidRuleFight", 1, rule);
574 writestruct(wd, DATA, "BoidRule", 1, rule);
578 //for(; cond; cond=cond->next)
579 // writestruct(wd, DATA, "BoidCondition", 1, cond);
581 /* TODO: replace *cache with *cachelist once it's coded */
582 #define PTCACHE_WRITE_PSYS 0
583 #define PTCACHE_WRITE_CLOTH 1
584 static void write_pointcaches(WriteData *wd, PointCache *cache, int type)
586 writestruct(wd, DATA, "PointCache", 1, cache);
588 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
589 PTCacheMem *pm = cache->mem_cache.first;
591 for(; pm; pm=pm->next) {
592 writestruct(wd, DATA, "PTCacheMem", 1, pm);
593 if(type==PTCACHE_WRITE_PSYS)
594 writestruct(wd, DATA, "ParticleKey", pm->totpoint, pm->data);
595 else if(type==PTCACHE_WRITE_CLOTH)
596 writedata(wd, DATA, 9 * sizeof(float) * pm->totpoint, pm->data);
600 static void write_particlesettings(WriteData *wd, ListBase *idbase)
602 ParticleSettings *part;
606 if(part->id.us>0 || wd->current) {
608 writestruct(wd, ID_PA, "ParticleSettings", 1, part);
609 if (part->id.properties) IDP_WriteProperty(part->id.properties, wd);
610 if (part->adt) write_animdata(wd, part->adt);
611 writestruct(wd, DATA, "PartDeflect", 1, part->pd);
612 writestruct(wd, DATA, "PartDeflect", 1, part->pd2);
614 if(part->boids && part->phystype == PART_PHYS_BOIDS) {
615 BoidState *state = part->boids->states.first;
617 writestruct(wd, DATA, "BoidSettings", 1, part->boids);
619 for(; state; state=state->next)
620 write_boid_state(wd, state);
626 static void write_particlesystems(WriteData *wd, ListBase *particles)
628 ParticleSystem *psys= particles->first;
632 for(; psys; psys=psys->next) {
633 writestruct(wd, DATA, "ParticleSystem", 1, psys);
635 if(psys->particles) {
636 writestruct(wd, DATA, "ParticleData", psys->totpart ,psys->particles);
638 if(psys->particles->hair) {
639 ParticleData *pa = psys->particles;
641 for(a=0; a<psys->totpart; a++, pa++)
642 writestruct(wd, DATA, "HairKey", pa->totkey, pa->hair);
645 if(psys->particles->boid && psys->part->phystype == PART_PHYS_BOIDS)
646 writestruct(wd, DATA, "BoidData", psys->totpart, psys->particles->boid);
648 pt = psys->targets.first;
649 for(; pt; pt=pt->next)
650 writestruct(wd, DATA, "ParticleTarget", 1, pt);
652 if(psys->child) writestruct(wd, DATA, "ChildParticle", psys->totchild ,psys->child);
653 writestruct(wd, DATA, "SoftBody", 1, psys->soft);
654 if(psys->soft) write_pointcaches(wd, psys->soft->pointcache, PTCACHE_WRITE_PSYS);
655 write_pointcaches(wd, psys->pointcache, PTCACHE_WRITE_PSYS);
659 static void write_properties(WriteData *wd, ListBase *lb)
665 writestruct(wd, DATA, "bProperty", 1, prop);
667 if(prop->poin && prop->poin != &prop->data)
668 writedata(wd, DATA, MEM_allocN_len(prop->poin), prop->poin);
674 static void write_sensors(WriteData *wd, ListBase *lb)
680 writestruct(wd, DATA, "bSensor", 1, sens);
682 writedata(wd, DATA, sizeof(void *)*sens->totlinks, sens->links);
686 writestruct(wd, DATA, "bNearSensor", 1, sens->data);
689 writestruct(wd, DATA, "bMouseSensor", 1, sens->data);
692 writestruct(wd, DATA, "bTouchSensor", 1, sens->data);
695 writestruct(wd, DATA, "bKeyboardSensor", 1, sens->data);
698 writestruct(wd, DATA, "bPropertySensor", 1, sens->data);
701 writestruct(wd, DATA, "bActuatorSensor", 1, sens->data);
704 writestruct(wd, DATA, "bDelaySensor", 1, sens->data);
707 writestruct(wd, DATA, "bCollisionSensor", 1, sens->data);
710 writestruct(wd, DATA, "bRadarSensor", 1, sens->data);
713 writestruct(wd, DATA, "bRandomSensor", 1, sens->data);
716 writestruct(wd, DATA, "bRaySensor", 1, sens->data);
719 writestruct(wd, DATA, "bMessageSensor", 1, sens->data);
722 writestruct(wd, DATA, "bJoystickSensor", 1, sens->data);
725 ; /* error: don't know how to write this file */
732 static void write_controllers(WriteData *wd, ListBase *lb)
738 writestruct(wd, DATA, "bController", 1, cont);
740 writedata(wd, DATA, sizeof(void *)*cont->totlinks, cont->links);
743 case CONT_EXPRESSION:
744 writestruct(wd, DATA, "bExpressionCont", 1, cont->data);
747 writestruct(wd, DATA, "bPythonCont", 1, cont->data);
750 ; /* error: don't know how to write this file */
757 static void write_actuators(WriteData *wd, ListBase *lb)
763 writestruct(wd, DATA, "bActuator", 1, act);
767 case ACT_SHAPEACTION:
768 writestruct(wd, DATA, "bActionActuator", 1, act->data);
771 writestruct(wd, DATA, "bSoundActuator", 1, act->data);
774 writestruct(wd, DATA, "bCDActuator", 1, act->data);
777 writestruct(wd, DATA, "bObjectActuator", 1, act->data);
780 writestruct(wd, DATA, "bIpoActuator", 1, act->data);
783 writestruct(wd, DATA, "bPropertyActuator", 1, act->data);
786 writestruct(wd, DATA, "bCameraActuator", 1, act->data);
789 writestruct(wd, DATA, "bConstraintActuator", 1, act->data);
791 case ACT_EDIT_OBJECT:
792 writestruct(wd, DATA, "bEditObjectActuator", 1, act->data);
795 writestruct(wd, DATA, "bSceneActuator", 1, act->data);
798 writestruct(wd, DATA, "bGroupActuator", 1, act->data);
801 writestruct(wd, DATA, "bRandomActuator", 1, act->data);
804 writestruct(wd, DATA, "bMessageActuator", 1, act->data);
807 writestruct(wd, DATA, "bGameActuator", 1, act->data);
810 writestruct(wd, DATA, "bVisibilityActuator", 1, act->data);
813 writestruct(wd, DATA, "bTwoDFilterActuator", 1, act->data);
816 writestruct(wd, DATA, "bParentActuator", 1, act->data);
819 writestruct(wd, DATA, "bStateActuator", 1, act->data);
822 ; /* error: don't know how to write this file */
829 static void write_fmodifiers(WriteData *wd, ListBase *fmodifiers)
834 for (fcm= fmodifiers->first; fcm; fcm= fcm->next) {
835 FModifierTypeInfo *fmi= fmodifier_get_typeinfo(fcm);
837 /* Write the specific data */
838 if (fmi && fcm->data) {
839 /* firstly, just write the plain fmi->data struct */
840 writestruct(wd, DATA, fmi->structName, 1, fcm->data);
842 /* do any modifier specific stuff */
844 case FMODIFIER_TYPE_GENERATOR:
846 FMod_Generator *data= (FMod_Generator *)fcm->data;
848 /* write coefficients array */
849 if (data->coefficients)
850 writedata(wd, DATA, sizeof(float)*(data->arraysize), data->coefficients);
853 case FMODIFIER_TYPE_ENVELOPE:
855 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
857 /* write envelope data */
859 writedata(wd, DATA, sizeof(FCM_EnvelopeData)*(data->totvert), data->data);
862 case FMODIFIER_TYPE_PYTHON:
864 FMod_Python *data = (FMod_Python *)fcm->data;
866 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
867 of library blocks that implement this.*/
868 IDP_WriteProperty(data->prop, wd);
874 /* Write the modifier */
875 writestruct(wd, DATA, "FModifier", 1, fcm);
879 static void write_fcurves(WriteData *wd, ListBase *fcurves)
883 for (fcu=fcurves->first; fcu; fcu=fcu->next) {
885 writestruct(wd, DATA, "FCurve", 1, fcu);
889 writestruct(wd, DATA, "BezTriple", fcu->totvert, fcu->bezt);
891 writestruct(wd, DATA, "FPoint", fcu->totvert, fcu->fpt);
894 writedata(wd, DATA, strlen(fcu->rna_path)+1, fcu->rna_path);
898 ChannelDriver *driver= fcu->driver;
901 writestruct(wd, DATA, "ChannelDriver", 1, driver);
904 for (dtar= driver->targets.first; dtar; dtar= dtar->next) {
905 writestruct(wd, DATA, "DriverTarget", 1, dtar);
908 writedata(wd, DATA, strlen(dtar->rna_path)+1, dtar->rna_path);
912 /* write F-Modifiers */
913 write_fmodifiers(wd, &fcu->modifiers);
917 static void write_actions(WriteData *wd, ListBase *idbase)
923 for(act=idbase->first; act; act= act->id.next) {
924 if (act->id.us>0 || wd->current) {
925 writestruct(wd, ID_AC, "bAction", 1, act);
926 if (act->id.properties) IDP_WriteProperty(act->id.properties, wd);
928 write_fcurves(wd, &act->curves);
930 for (grp=act->groups.first; grp; grp=grp->next) {
931 writestruct(wd, DATA, "bActionGroup", 1, grp);
934 for (marker=act->markers.first; marker; marker=marker->next) {
935 writestruct(wd, DATA, "TimeMarker", 1, marker);
940 /* flush helps the compression for undo-save */
941 mywrite(wd, MYWRITE_FLUSH, 0);
944 static void write_keyingsets(WriteData *wd, ListBase *list)
949 for (ks= list->first; ks; ks= ks->next) {
951 writestruct(wd, DATA, "KeyingSet", 1, ks);
954 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
956 writestruct(wd, DATA, "KS_Path", 1, ksp);
959 writedata(wd, DATA, strlen(ksp->rna_path)+1, ksp->rna_path);
964 static void write_nlastrips(WriteData *wd, ListBase *strips)
968 for (strip= strips->first; strip; strip= strip->next) {
969 /* write the strip first */
970 writestruct(wd, DATA, "NlaStrip", 1, strip);
972 /* write the strip's F-Curves and modifiers */
973 write_fcurves(wd, &strip->fcurves);
974 write_fmodifiers(wd, &strip->modifiers);
976 /* write the strip's children */
977 write_nlastrips(wd, &strip->strips);
981 static void write_nladata(WriteData *wd, ListBase *nlabase)
985 /* write all the tracks */
986 for (nlt= nlabase->first; nlt; nlt= nlt->next) {
987 /* write the track first */
988 writestruct(wd, DATA, "NlaTrack", 1, nlt);
990 /* write the track's strips */
991 write_nlastrips(wd, &nlt->strips);
995 static void write_animdata(WriteData *wd, AnimData *adt)
999 /* firstly, just write the AnimData block */
1000 writestruct(wd, DATA, "AnimData", 1, adt);
1003 write_fcurves(wd, &adt->drivers);
1005 /* write overrides */
1006 // FIXME: are these needed?
1007 for (aor= adt->overrides.first; aor; aor= aor->next) {
1008 /* overrides consist of base data + rna_path */
1009 writestruct(wd, DATA, "AnimOverride", 1, aor);
1010 writedata(wd, DATA, strlen(aor->rna_path)+1, aor->rna_path);
1013 // TODO write the remaps (if they are needed)
1015 /* write NLA data */
1016 write_nladata(wd, &adt->nla_tracks);
1019 static void write_constraints(WriteData *wd, ListBase *conlist)
1023 for (con=conlist->first; con; con=con->next) {
1024 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1026 /* Write the specific data */
1027 if (cti && con->data) {
1028 /* firstly, just write the plain con->data struct */
1029 writestruct(wd, DATA, cti->structName, 1, con->data);
1031 /* do any constraint specific stuff */
1032 switch (con->type) {
1033 case CONSTRAINT_TYPE_PYTHON:
1035 bPythonConstraint *data = (bPythonConstraint *)con->data;
1036 bConstraintTarget *ct;
1039 for (ct= data->targets.first; ct; ct= ct->next)
1040 writestruct(wd, DATA, "bConstraintTarget", 1, ct);
1042 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
1043 of library blocks that implement this.*/
1044 IDP_WriteProperty(data->prop, wd);
1050 /* Write the constraint */
1051 writestruct(wd, DATA, "bConstraint", 1, con);
1055 static void write_pose(WriteData *wd, bPose *pose)
1060 /* Write each channel */
1064 /* Write channels */
1065 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1066 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
1067 of library blocks that implement this.*/
1069 IDP_WriteProperty(chan->prop, wd);
1071 write_constraints(wd, &chan->constraints);
1073 /* prevent crashes with autosave, when a bone duplicated in editmode has not yet been assigned to its posechannel */
1075 chan->selectflag= chan->bone->flag & (BONE_SELECTED|BONE_ACTIVE); /* gets restored on read, for library armatures */
1077 writestruct(wd, DATA, "bPoseChannel", 1, chan);
1081 for (grp=pose->agroups.first; grp; grp=grp->next)
1082 writestruct(wd, DATA, "bActionGroup", 1, grp);
1084 /* Write this pose */
1085 writestruct(wd, DATA, "bPose", 1, pose);
1088 static void write_defgroups(WriteData *wd, ListBase *defbase)
1090 bDeformGroup *defgroup;
1092 for (defgroup=defbase->first; defgroup; defgroup=defgroup->next)
1093 writestruct(wd, DATA, "bDeformGroup", 1, defgroup);
1096 static void write_modifiers(WriteData *wd, ListBase *modbase, int write_undo)
1100 if (modbase == NULL) return;
1101 for (md=modbase->first; md; md= md->next) {
1102 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1103 if (mti == NULL) return;
1105 writestruct(wd, DATA, mti->structName, 1, md);
1107 if (md->type==eModifierType_Hook) {
1108 HookModifierData *hmd = (HookModifierData*) md;
1110 writedata(wd, DATA, sizeof(int)*hmd->totindex, hmd->indexar);
1112 else if(md->type==eModifierType_Cloth) {
1113 ClothModifierData *clmd = (ClothModifierData*) md;
1115 writestruct(wd, DATA, "ClothSimSettings", 1, clmd->sim_parms);
1116 writestruct(wd, DATA, "ClothCollSettings", 1, clmd->coll_parms);
1117 write_pointcaches(wd, clmd->point_cache, PTCACHE_WRITE_CLOTH);
1119 else if(md->type==eModifierType_Fluidsim) {
1120 FluidsimModifierData *fluidmd = (FluidsimModifierData*) md;
1122 writestruct(wd, DATA, "FluidsimSettings", 1, fluidmd->fss);
1124 else if (md->type==eModifierType_Collision) {
1127 CollisionModifierData *collmd = (CollisionModifierData*) md;
1128 // TODO: CollisionModifier should use pointcache
1129 // + have proper reset events before enabling this
1130 writestruct(wd, DATA, "MVert", collmd->numverts, collmd->x);
1131 writestruct(wd, DATA, "MVert", collmd->numverts, collmd->xnew);
1132 writestruct(wd, DATA, "MFace", collmd->numfaces, collmd->mfaces);
1135 else if (md->type==eModifierType_MeshDeform) {
1136 MeshDeformModifierData *mmd = (MeshDeformModifierData*) md;
1137 int size = mmd->dyngridsize;
1139 writedata(wd, DATA, sizeof(float)*mmd->totvert*mmd->totcagevert,
1141 writedata(wd, DATA, sizeof(float)*3*mmd->totcagevert,
1143 writestruct(wd, DATA, "MDefCell", size*size*size, mmd->dyngrid);
1144 writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->dyninfluences);
1145 writedata(wd, DATA, sizeof(int)*mmd->totvert, mmd->dynverts);
1147 else if (md->type==eModifierType_Multires) {
1148 MultiresModifierData *mmd = (MultiresModifierData*) md;
1150 if(mmd->undo_verts && write_undo)
1151 writestruct(wd, DATA, "MVert", mmd->undo_verts_tot, mmd->undo_verts);
1156 static void write_objects(WriteData *wd, ListBase *idbase, int write_undo)
1162 if(ob->id.us>0 || wd->current) {
1164 writestruct(wd, ID_OB, "Object", 1, ob);
1166 /*Write ID Properties -- and copy this comment EXACTLY for easy finding
1167 of library blocks that implement this.*/
1168 if (ob->id.properties) IDP_WriteProperty(ob->id.properties, wd);
1170 if (ob->adt) write_animdata(wd, ob->adt);
1173 writedata(wd, DATA, sizeof(void *)*ob->totcol, ob->mat);
1174 writedata(wd, DATA, sizeof(char)*ob->totcol, ob->matbits);
1175 /* write_effects(wd, &ob->effect); */ /* not used anymore */
1176 write_properties(wd, &ob->prop);
1177 write_sensors(wd, &ob->sensors);
1178 write_controllers(wd, &ob->controllers);
1179 write_actuators(wd, &ob->actuators);
1180 write_pose(wd, ob->pose);
1181 write_defgroups(wd, &ob->defbase);
1182 write_constraints(wd, &ob->constraints);
1184 writestruct(wd, DATA, "PartDeflect", 1, ob->pd);
1185 writestruct(wd, DATA, "SoftBody", 1, ob->soft);
1186 if(ob->soft) writestruct(wd, DATA, "PointCache", 1, ob->soft->pointcache);
1187 writestruct(wd, DATA, "BulletSoftBody", 1, ob->bsoft);
1189 write_particlesystems(wd, &ob->particlesystem);
1190 write_modifiers(wd, &ob->modifiers, write_undo);
1195 /* flush helps the compression for undo-save */
1196 mywrite(wd, MYWRITE_FLUSH, 0);
1200 static void write_vfonts(WriteData *wd, ListBase *idbase)
1207 if(vf->id.us>0 || wd->current) {
1209 writestruct(wd, ID_VF, "VFont", 1, vf);
1210 if (vf->id.properties) IDP_WriteProperty(vf->id.properties, wd);
1214 if (vf->packedfile) {
1215 pf = vf->packedfile;
1216 writestruct(wd, DATA, "PackedFile", 1, pf);
1217 writedata(wd, DATA, pf->size, pf->data);
1226 static void write_keys(WriteData *wd, ListBase *idbase)
1233 if(key->id.us>0 || wd->current) {
1235 writestruct(wd, ID_KE, "Key", 1, key);
1236 if (key->id.properties) IDP_WriteProperty(key->id.properties, wd);
1238 if (key->adt) write_animdata(wd, key->adt);
1241 kb= key->block.first;
1243 writestruct(wd, DATA, "KeyBlock", 1, kb);
1244 if(kb->data) writedata(wd, DATA, kb->totelem*key->elemsize, kb->data);
1251 /* flush helps the compression for undo-save */
1252 mywrite(wd, MYWRITE_FLUSH, 0);
1255 static void write_cameras(WriteData *wd, ListBase *idbase)
1261 if(cam->id.us>0 || wd->current) {
1263 writestruct(wd, ID_CA, "Camera", 1, cam);
1264 if (cam->id.properties) IDP_WriteProperty(cam->id.properties, wd);
1266 if (cam->adt) write_animdata(wd, cam->adt);
1273 static void write_mballs(WriteData *wd, ListBase *idbase)
1280 if(mb->id.us>0 || wd->current) {
1282 writestruct(wd, ID_MB, "MetaBall", 1, mb);
1283 if (mb->id.properties) IDP_WriteProperty(mb->id.properties, wd);
1286 writedata(wd, DATA, sizeof(void *)*mb->totcol, mb->mat);
1288 ml= mb->elems.first;
1290 writestruct(wd, DATA, "MetaElem", 1, ml);
1298 static int amount_of_chars(char *str)
1300 // Since the data is saved as UTF-8 to the cu->str
1301 // The cu->len is not same as the strlen(cu->str)
1305 static void write_curves(WriteData *wd, ListBase *idbase)
1312 if(cu->id.us>0 || wd->current) {
1314 writestruct(wd, ID_CU, "Curve", 1, cu);
1317 writedata(wd, DATA, sizeof(void *)*cu->totcol, cu->mat);
1318 if (cu->id.properties) IDP_WriteProperty(cu->id.properties, wd);
1319 if (cu->adt) write_animdata(wd, cu->adt);
1322 writedata(wd, DATA, amount_of_chars(cu->str)+1, cu->str);
1323 writestruct(wd, DATA, "CharInfo", cu->len, cu->strinfo);
1324 writestruct(wd, DATA, "TextBox", cu->totbox, cu->tb);
1327 /* is also the order of reading */
1330 writestruct(wd, DATA, "Nurb", 1, nu);
1335 if( (nu->type & 7)==CU_BEZIER)
1336 writestruct(wd, DATA, "BezTriple", nu->pntsu, nu->bezt);
1338 writestruct(wd, DATA, "BPoint", nu->pntsu*nu->pntsv, nu->bp);
1339 if(nu->knotsu) writedata(wd, DATA, KNOTSU(nu)*sizeof(float), nu->knotsu);
1340 if(nu->knotsv) writedata(wd, DATA, KNOTSV(nu)*sizeof(float), nu->knotsv);
1349 /* flush helps the compression for undo-save */
1350 mywrite(wd, MYWRITE_FLUSH, 0);
1353 static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
1358 /* Write the dvert list */
1359 writestruct(wd, DATA, "MDeformVert", count, dvlist);
1361 /* Write deformation data for each dvert */
1362 for (i=0; i<count; i++) {
1364 writestruct(wd, DATA, "MDeformWeight", dvlist[i].totweight, dvlist[i].dw);
1369 static void write_mdisps(WriteData *wd, int count, MDisps *mdlist)
1374 writestruct(wd, DATA, "MDisps", count, mdlist);
1375 for(i = 0; i < count; ++i) {
1377 writedata(wd, DATA, sizeof(float)*3*mdlist[i].totdisp, mdlist[i].disps);
1382 static void write_customdata(WriteData *wd, int count, CustomData *data, int partial_type, int partial_count)
1386 writestruct(wd, DATA, "CustomDataLayer", data->maxlayer, data->layers);
1388 for (i=0; i<data->totlayer; i++) {
1389 CustomDataLayer *layer= &data->layers[i];
1391 int structnum, datasize;
1393 if (layer->type == CD_MDEFORMVERT) {
1394 /* layer types that allocate own memory need special handling */
1395 write_dverts(wd, count, layer->data);
1397 else if (layer->type == CD_MDISPS) {
1398 write_mdisps(wd, count, layer->data);
1401 CustomData_file_write_info(layer->type, &structname, &structnum);
1403 /* when using partial visibility, the MEdge and MFace layers
1404 are smaller than the original, so their type and count is
1405 passed to make this work */
1406 if (layer->type != partial_type) datasize= structnum*count;
1407 else datasize= structnum*partial_count;
1409 writestruct(wd, DATA, structname, datasize, layer->data);
1412 printf("error: this CustomDataLayer must not be written to file\n");
1417 static void write_meshs(WriteData *wd, ListBase *idbase)
1421 mesh= idbase->first;
1423 if(mesh->id.us>0 || wd->current) {
1425 writestruct(wd, ID_ME, "Mesh", 1, mesh);
1428 if (mesh->id.properties) IDP_WriteProperty(mesh->id.properties, wd);
1430 writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
1433 write_customdata(wd, mesh->pv->totvert, &mesh->vdata, -1, 0);
1434 write_customdata(wd, mesh->pv->totedge, &mesh->edata,
1435 CD_MEDGE, mesh->totedge);
1436 write_customdata(wd, mesh->pv->totface, &mesh->fdata,
1437 CD_MFACE, mesh->totface);
1440 write_customdata(wd, mesh->totvert, &mesh->vdata, -1, 0);
1441 write_customdata(wd, mesh->totedge, &mesh->edata, -1, 0);
1442 write_customdata(wd, mesh->totface, &mesh->fdata, -1, 0);
1447 writestruct(wd, DATA, "PartialVisibility", 1, mesh->pv);
1448 writedata(wd, DATA, sizeof(unsigned int)*mesh->pv->totvert, mesh->pv->vert_map);
1449 writedata(wd, DATA, sizeof(int)*mesh->pv->totedge, mesh->pv->edge_map);
1450 writestruct(wd, DATA, "MFace", mesh->pv->totface, mesh->pv->old_faces);
1451 writestruct(wd, DATA, "MEdge", mesh->pv->totedge, mesh->pv->old_edges);
1454 mesh= mesh->id.next;
1458 static void write_lattices(WriteData *wd, ListBase *idbase)
1464 if(lt->id.us>0 || wd->current) {
1466 writestruct(wd, ID_LT, "Lattice", 1, lt);
1467 if (lt->id.properties) IDP_WriteProperty(lt->id.properties, wd);
1470 writestruct(wd, DATA, "BPoint", lt->pntsu*lt->pntsv*lt->pntsw, lt->def);
1472 write_dverts(wd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
1479 static void write_previews(WriteData *wd, PreviewImage *prv)
1482 short w = prv->w[1];
1483 short h = prv->h[1];
1484 unsigned int *rect = prv->rect[1];
1485 /* don't write out large previews if not requested */
1486 if (!(U.flag & USER_SAVE_PREVIEWS) ) {
1489 prv->rect[1] = NULL;
1491 writestruct(wd, DATA, "PreviewImage", 1, prv);
1492 if (prv->rect[0]) writedata(wd, DATA, prv->w[0]*prv->h[0]*sizeof(unsigned int), prv->rect[0]);
1493 if (prv->rect[1]) writedata(wd, DATA, prv->w[1]*prv->h[1]*sizeof(unsigned int), prv->rect[1]);
1495 /* restore preview, we still want to keep it in memory even if not saved to file */
1496 if (!(U.flag & USER_SAVE_PREVIEWS) ) {
1499 prv->rect[1] = rect;
1504 static void write_images(WriteData *wd, ListBase *idbase)
1512 if(ima->id.us>0 || wd->current) {
1514 writestruct(wd, ID_IM, "Image", 1, ima);
1515 if (ima->id.properties) IDP_WriteProperty(ima->id.properties, wd);
1517 if (ima->packedfile) {
1518 pf = ima->packedfile;
1519 writestruct(wd, DATA, "PackedFile", 1, pf);
1520 writedata(wd, DATA, pf->size, pf->data);
1523 write_previews(wd, ima->preview);
1525 /* exception: render text only saved in undo files (wd->current) */
1526 if (ima->render_text && wd->current)
1527 writedata(wd, DATA, IMA_RW_MAXTEXT, ima->render_text);
1531 /* flush helps the compression for undo-save */
1532 mywrite(wd, MYWRITE_FLUSH, 0);
1535 static void write_textures(WriteData *wd, ListBase *idbase)
1541 if(tex->id.us>0 || wd->current) {
1543 writestruct(wd, ID_TE, "Tex", 1, tex);
1544 if (tex->id.properties) IDP_WriteProperty(tex->id.properties, wd);
1546 if (tex->adt) write_animdata(wd, tex->adt);
1549 if(tex->type == TEX_PLUGIN && tex->plugin) writestruct(wd, DATA, "PluginTex", 1, tex->plugin);
1550 if(tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
1551 if(tex->type == TEX_ENVMAP && tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
1553 /* nodetree is integral part of texture, no libdata */
1555 writestruct(wd, DATA, "bNodeTree", 1, tex->nodetree);
1556 write_nodetree(wd, tex->nodetree);
1559 write_previews(wd, tex->preview);
1564 /* flush helps the compression for undo-save */
1565 mywrite(wd, MYWRITE_FLUSH, 0);
1568 static void write_materials(WriteData *wd, ListBase *idbase)
1575 if(ma->id.us>0 || wd->current) {
1577 writestruct(wd, ID_MA, "Material", 1, ma);
1579 /*Write ID Properties -- and copy this comment EXACTLY for easy finding
1580 of library blocks that implement this.*/
1581 /*manually set head group property to IDP_GROUP, just in case it hadn't been
1583 if (ma->id.properties) IDP_WriteProperty(ma->id.properties, wd);
1585 if (ma->adt) write_animdata(wd, ma->adt);
1587 for(a=0; a<MAX_MTEX; a++) {
1588 if(ma->mtex[a]) writestruct(wd, DATA, "MTex", 1, ma->mtex[a]);
1591 if(ma->ramp_col) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_col);
1592 if(ma->ramp_spec) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_spec);
1594 /* nodetree is integral part of material, no libdata */
1596 writestruct(wd, DATA, "bNodeTree", 1, ma->nodetree);
1597 write_nodetree(wd, ma->nodetree);
1600 write_previews(wd, ma->preview);
1606 static void write_worlds(WriteData *wd, ListBase *idbase)
1611 wrld= idbase->first;
1613 if(wrld->id.us>0 || wd->current) {
1615 writestruct(wd, ID_WO, "World", 1, wrld);
1616 if (wrld->id.properties) IDP_WriteProperty(wrld->id.properties, wd);
1618 if (wrld->adt) write_animdata(wd, wrld->adt);
1620 for(a=0; a<MAX_MTEX; a++) {
1621 if(wrld->mtex[a]) writestruct(wd, DATA, "MTex", 1, wrld->mtex[a]);
1624 write_previews(wd, wrld->preview);
1626 wrld= wrld->id.next;
1630 static void write_lamps(WriteData *wd, ListBase *idbase)
1637 if(la->id.us>0 || wd->current) {
1639 writestruct(wd, ID_LA, "Lamp", 1, la);
1640 if (la->id.properties) IDP_WriteProperty(la->id.properties, wd);
1642 if (la->adt) write_animdata(wd, la->adt);
1645 for(a=0; a<MAX_MTEX; a++) {
1646 if(la->mtex[a]) writestruct(wd, DATA, "MTex", 1, la->mtex[a]);
1650 write_curvemapping(wd, la->curfalloff);
1652 write_previews(wd, la->preview);
1660 static void write_scenes(WriteData *wd, ListBase *scebase)
1669 TransformOrientation *ts;
1670 SceneRenderLayer *srl;
1672 sce= scebase->first;
1675 writestruct(wd, ID_SCE, "Scene", 1, sce);
1676 if (sce->id.properties) IDP_WriteProperty(sce->id.properties, wd);
1678 if (sce->adt) write_animdata(wd, sce->adt);
1679 write_keyingsets(wd, &sce->keyingsets);
1682 base= sce->base.first;
1684 writestruct(wd, DATA, "Base", 1, base);
1688 writestruct(wd, DATA, "ToolSettings", 1, sce->toolsettings);
1689 if(sce->toolsettings->vpaint)
1690 writestruct(wd, DATA, "VPaint", 1, sce->toolsettings->vpaint);
1691 if(sce->toolsettings->wpaint)
1692 writestruct(wd, DATA, "VPaint", 1, sce->toolsettings->wpaint);
1693 if(sce->toolsettings->sculpt)
1694 writestruct(wd, DATA, "Sculpt", 1, sce->toolsettings->sculpt);
1698 writestruct(wd, DATA, "Editing", 1, ed);
1700 /* reset write flags too */
1702 SEQ_BEGIN(ed, seq) {
1703 if(seq->strip) seq->strip->done= 0;
1704 writestruct(wd, DATA, "Sequence", 1, seq);
1708 SEQ_BEGIN(ed, seq) {
1709 if(seq->strip && seq->strip->done==0) {
1710 /* write strip with 'done' at 0 because readfile */
1712 if(seq->plugin) writestruct(wd, DATA, "PluginSeq", 1, seq->plugin);
1713 if(seq->effectdata) {
1716 writestruct(wd, DATA, "SolidColorVars", 1, seq->effectdata);
1719 writestruct(wd, DATA, "SpeedControlVars", 1, seq->effectdata);
1722 writestruct(wd, DATA, "WipeVars", 1, seq->effectdata);
1725 writestruct(wd, DATA, "GlowVars", 1, seq->effectdata);
1728 writestruct(wd, DATA, "TransformVars", 1, seq->effectdata);
1734 writestruct(wd, DATA, "Strip", 1, strip);
1735 if(seq->flag & SEQ_USE_CROP && strip->crop) {
1736 writestruct(wd, DATA, "StripCrop", 1, strip->crop);
1738 if(seq->flag & SEQ_USE_TRANSFORM && strip->transform) {
1739 writestruct(wd, DATA, "StripTransform", 1, strip->transform);
1741 if(seq->flag & SEQ_USE_PROXY && strip->proxy) {
1742 writestruct(wd, DATA, "StripProxy", 1, strip->proxy);
1744 if(seq->flag & SEQ_USE_COLOR_BALANCE && strip->color_balance) {
1745 writestruct(wd, DATA, "StripColorBalance", 1, strip->color_balance);
1747 if(seq->type==SEQ_IMAGE)
1748 writestruct(wd, DATA, "StripElem", MEM_allocN_len(strip->stripdata) / sizeof(struct StripElem), strip->stripdata);
1749 else if(seq->type==SEQ_MOVIE || seq->type==SEQ_RAM_SOUND || seq->type == SEQ_HD_SOUND)
1750 writestruct(wd, DATA, "StripElem", 1, strip->stripdata);
1757 /* new; meta stack too, even when its nasty restore code */
1758 for(ms= ed->metastack.first; ms; ms= ms->next) {
1759 writestruct(wd, DATA, "MetaStack", 1, ms);
1763 if (sce->r.avicodecdata) {
1764 writestruct(wd, DATA, "AviCodecData", 1, sce->r.avicodecdata);
1765 if (sce->r.avicodecdata->lpFormat) writedata(wd, DATA, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat);
1766 if (sce->r.avicodecdata->lpParms) writedata(wd, DATA, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms);
1769 if (sce->r.qtcodecdata) {
1770 writestruct(wd, DATA, "QuicktimeCodecData", 1, sce->r.qtcodecdata);
1771 if (sce->r.qtcodecdata->cdParms) writedata(wd, DATA, sce->r.qtcodecdata->cdSize, sce->r.qtcodecdata->cdParms);
1773 if (sce->r.ffcodecdata.properties) {
1774 IDP_WriteProperty(sce->r.ffcodecdata.properties, wd);
1777 /* writing dynamic list of TimeMarkers to the blend file */
1778 for(marker= sce->markers.first; marker; marker= marker->next)
1779 writestruct(wd, DATA, "TimeMarker", 1, marker);
1781 /* writing dynamic list of TransformOrientations to the blend file */
1782 for(ts = sce->transform_spaces.first; ts; ts = ts->next)
1783 writestruct(wd, DATA, "TransformOrientation", 1, ts);
1785 for(srl= sce->r.layers.first; srl; srl= srl->next)
1786 writestruct(wd, DATA, "SceneRenderLayer", 1, srl);
1789 writestruct(wd, DATA, "bNodeTree", 1, sce->nodetree);
1790 write_nodetree(wd, sce->nodetree);
1795 /* flush helps the compression for undo-save */
1796 mywrite(wd, MYWRITE_FLUSH, 0);
1799 static void write_gpencils(WriteData *wd, ListBase *lb)
1806 for (gpd= lb->first; gpd; gpd= gpd->id.next) {
1807 /* write gpd data block to file */
1808 writestruct(wd, ID_GD, "bGPdata", 1, gpd);
1810 /* write grease-pencil layers to file */
1811 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
1812 writestruct(wd, DATA, "bGPDlayer", 1, gpl);
1814 /* write this layer's frames to file */
1815 for (gpf= gpl->frames.first; gpf; gpf= gpf->next) {
1816 writestruct(wd, DATA, "bGPDframe", 1, gpf);
1819 for (gps= gpf->strokes.first; gps; gps= gps->next) {
1820 writestruct(wd, DATA, "bGPDstroke", 1, gps);
1821 writestruct(wd, DATA, "bGPDspoint", gps->totpoints, gps->points);
1828 static void write_windowmanagers(WriteData *wd, ListBase *lb)
1830 wmWindowManager *wm;
1833 for(wm= lb->first; wm; wm= wm->id.next) {
1834 writestruct(wd, ID_WM, "wmWindowManager", 1, wm);
1836 for(win= wm->windows.first; win; win= win->next)
1837 writestruct(wd, DATA, "wmWindow", 1, win);
1841 static void write_region(WriteData *wd, ARegion *ar, int spacetype)
1843 writestruct(wd, DATA, "ARegion", 1, ar);
1845 if(ar->regiondata) {
1848 if(ar->regiontype==RGN_TYPE_WINDOW) {
1849 RegionView3D *rv3d= ar->regiondata;
1850 writestruct(wd, DATA, "RegionView3D", 1, rv3d);
1853 writestruct(wd, DATA, "RegionView3D", 1, rv3d->localvd);
1855 writestruct(wd, DATA, "BoundBox", 1, rv3d->clipbb);
1859 printf("regiondata write missing!\n");
1862 printf("regiondata write missing!\n");
1867 static void write_screens(WriteData *wd, ListBase *scrbase)
1878 /* in 2.50+ files, the file identifier for screens is patched, forward compatibility */
1879 writestruct(wd, ID_SCRN, "Screen", 1, sc);
1880 if (sc->id.properties)
1881 IDP_WriteProperty(sc->id.properties, wd);
1884 for(sv= sc->vertbase.first; sv; sv= sv->next)
1885 writestruct(wd, DATA, "ScrVert", 1, sv);
1887 for(se= sc->edgebase.first; se; se= se->next)
1888 writestruct(wd, DATA, "ScrEdge", 1, se);
1890 for(sa= sc->areabase.first; sa; sa= sa->next) {
1895 writestruct(wd, DATA, "ScrArea", 1, sa);
1897 for(ar= sa->regionbase.first; ar; ar= ar->next) {
1898 write_region(wd, ar, sa->spacetype);
1900 for(pa= ar->panels.first; pa; pa= pa->next)
1901 writestruct(wd, DATA, "Panel", 1, pa);
1904 sl= sa->spacedata.first;
1906 for(ar= sl->regionbase.first; ar; ar= ar->next)
1907 write_region(wd, ar, sl->spacetype);
1909 if(sl->spacetype==SPACE_VIEW3D) {
1910 View3D *v3d= (View3D *) sl;
1911 writestruct(wd, DATA, "View3D", 1, v3d);
1912 if(v3d->bgpic) writestruct(wd, DATA, "BGpic", 1, v3d->bgpic);
1913 if(v3d->localvd) writestruct(wd, DATA, "View3D", 1, v3d->localvd);
1915 else if(sl->spacetype==SPACE_IPO) {
1916 SpaceIpo *sipo= (SpaceIpo *)sl;
1917 ListBase tmpGhosts = sipo->ghostCurves;
1919 /* temporarily disable ghost curves when saving */
1920 sipo->ghostCurves.first= sipo->ghostCurves.last= NULL;
1922 writestruct(wd, DATA, "SpaceIpo", 1, sl);
1923 if(sipo->ads) writestruct(wd, DATA, "bDopeSheet", 1, sipo->ads);
1925 /* reenable ghost curves */
1926 sipo->ghostCurves= tmpGhosts;
1928 else if(sl->spacetype==SPACE_BUTS) {
1929 writestruct(wd, DATA, "SpaceButs", 1, sl);
1931 else if(sl->spacetype==SPACE_FILE) {
1932 writestruct(wd, DATA, "SpaceFile", 1, sl);
1934 else if(sl->spacetype==SPACE_SEQ) {
1935 writestruct(wd, DATA, "SpaceSeq", 1, sl);
1937 else if(sl->spacetype==SPACE_OUTLINER) {
1938 SpaceOops *so= (SpaceOops *)sl;
1940 writestruct(wd, DATA, "SpaceOops", 1, so);
1944 writestruct(wd, DATA, "TreeStore", 1, so->treestore);
1945 if(so->treestore->data)
1946 writestruct(wd, DATA, "TreeStoreElem", so->treestore->usedelem, so->treestore->data);
1949 else if(sl->spacetype==SPACE_IMAGE) {
1950 SpaceImage *sima= (SpaceImage *)sl;
1952 writestruct(wd, DATA, "SpaceImage", 1, sl);
1954 write_curvemapping(wd, sima->cumap);
1956 else if(sl->spacetype==SPACE_IMASEL) {
1957 writestruct(wd, DATA, "SpaceImaSel", 1, sl);
1959 else if(sl->spacetype==SPACE_TEXT) {
1960 writestruct(wd, DATA, "SpaceText", 1, sl);
1962 else if(sl->spacetype==SPACE_SCRIPT) {
1963 SpaceScript *sc = (SpaceScript*)sl;
1964 sc->but_refs = NULL;
1965 writestruct(wd, DATA, "SpaceScript", 1, sl);
1967 else if(sl->spacetype==SPACE_ACTION) {
1968 writestruct(wd, DATA, "SpaceAction", 1, sl);
1970 else if(sl->spacetype==SPACE_SOUND) {
1971 writestruct(wd, DATA, "SpaceSound", 1, sl);
1973 else if(sl->spacetype==SPACE_NLA){
1974 SpaceNla *snla= (SpaceNla *)sl;
1976 writestruct(wd, DATA, "SpaceNla", 1, snla);
1977 if(snla->ads) writestruct(wd, DATA, "bDopeSheet", 1, snla->ads);
1979 else if(sl->spacetype==SPACE_TIME){
1980 writestruct(wd, DATA, "SpaceTime", 1, sl);
1982 else if(sl->spacetype==SPACE_NODE){
1983 writestruct(wd, DATA, "SpaceNode", 1, sl);
1985 else if(sl->spacetype==SPACE_LOGIC){
1986 writestruct(wd, DATA, "SpaceLogic", 1, sl);
1988 else if(sl->spacetype==SPACE_CONSOLE) {
1989 writestruct(wd, DATA, "SpaceConsole", 1, sl);
1999 static void write_libraries(WriteData *wd, Main *main)
2001 ListBase *lbarray[30];
2003 int a, tot, foundone;
2005 for(; main; main= main->next) {
2007 a=tot= set_listbasepointers(main, lbarray);
2009 /* test: is lib being used */
2012 for(id= lbarray[tot]->first; id; id= id->next) {
2013 if(id->us>0 && (id->flag & LIB_EXTERN)) {
2022 writestruct(wd, ID_LI, "Library", 1, main->curlib);
2025 for(id= lbarray[a]->first; id; id= id->next) {
2026 if(id->us>0 && (id->flag & LIB_EXTERN)) {
2027 writestruct(wd, ID_ID, "ID", 1, id);
2035 static void write_bone(WriteData *wd, Bone* bone)
2039 // PATCH for upward compatibility after 2.37+ armature recode
2040 bone->size[0]= bone->size[1]= bone->size[2]= 1.0f;
2043 writestruct(wd, DATA, "Bone", 1, bone);
2046 cbone= bone->childbase.first;
2048 write_bone(wd, cbone);
2053 static void write_armatures(WriteData *wd, ListBase *idbase)
2060 if (arm->id.us>0 || wd->current) {
2061 writestruct(wd, ID_AR, "bArmature", 1, arm);
2062 if (arm->id.properties) IDP_WriteProperty(arm->id.properties, wd);
2065 bone= arm->bonebase.first;
2067 write_bone(wd, bone);
2074 /* flush helps the compression for undo-save */
2075 mywrite(wd, MYWRITE_FLUSH, 0);
2078 static void write_texts(WriteData *wd, ListBase *idbase)
2084 text= idbase->first;
2086 if ( (text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) text->flags &= ~TXT_ISEXT;
2089 writestruct(wd, ID_TXT, "Text", 1, text);
2090 if(text->name) writedata(wd, DATA, strlen(text->name)+1, text->name);
2091 if (text->id.properties) IDP_WriteProperty(text->id.properties, wd);
2093 if(!(text->flags & TXT_ISEXT)) {
2094 /* now write the text data, in two steps for optimization in the readfunction */
2095 tmp= text->lines.first;
2097 writestruct(wd, DATA, "TextLine", 1, tmp);
2101 tmp= text->lines.first;
2103 writedata(wd, DATA, tmp->len+1, tmp->line);
2108 mrk= text->markers.first;
2110 writestruct(wd, DATA, "TextMarker", 1, mrk);
2116 text= text->id.next;
2119 /* flush helps the compression for undo-save */
2120 mywrite(wd, MYWRITE_FLUSH, 0);
2123 static void write_sounds(WriteData *wd, ListBase *idbase)
2130 // set all samples to unsaved status
2132 sample = samples->first; // samples is a global defined in sound.c
2134 sample->flags |= SAMPLE_NEEDS_SAVE;
2135 sample = sample->id.next;
2138 sound= idbase->first;
2140 if(sound->id.us>0 || wd->current) {
2141 // do we need to save the packedfile as well ?
2142 sample = sound->sample;
2144 if (sample->flags & SAMPLE_NEEDS_SAVE) {
2145 sound->newpackedfile = sample->packedfile;
2146 sample->flags &= ~SAMPLE_NEEDS_SAVE;
2148 sound->newpackedfile = NULL;
2153 writestruct(wd, ID_SO, "bSound", 1, sound);
2154 if (sound->id.properties) IDP_WriteProperty(sound->id.properties, wd);
2156 if (sound->newpackedfile) {
2157 pf = sound->newpackedfile;
2158 writestruct(wd, DATA, "PackedFile", 1, pf);
2159 writedata(wd, DATA, pf->size, pf->data);
2163 sound->newpackedfile = sample->packedfile;
2166 sound= sound->id.next;
2169 /* flush helps the compression for undo-save */
2170 mywrite(wd, MYWRITE_FLUSH, 0);
2173 static void write_groups(WriteData *wd, ListBase *idbase)
2178 for(group= idbase->first; group; group= group->id.next) {
2179 if(group->id.us>0 || wd->current) {
2181 writestruct(wd, ID_GR, "Group", 1, group);
2182 if (group->id.properties) IDP_WriteProperty(group->id.properties, wd);
2184 go= group->gobject.first;
2186 writestruct(wd, DATA, "GroupObject", 1, go);
2193 static void write_nodetrees(WriteData *wd, ListBase *idbase)
2197 for(ntree=idbase->first; ntree; ntree= ntree->id.next) {
2198 if (ntree->id.us>0 || wd->current) {
2199 writestruct(wd, ID_NT, "bNodeTree", 1, ntree);
2200 write_nodetree(wd, ntree);
2202 if (ntree->id.properties) IDP_WriteProperty(ntree->id.properties, wd);
2204 if (ntree->adt) write_animdata(wd, ntree->adt);
2209 static void write_brushes(WriteData *wd, ListBase *idbase)
2214 for(brush=idbase->first; brush; brush= brush->id.next) {
2215 if(brush->id.us>0 || wd->current) {
2216 writestruct(wd, ID_BR, "Brush", 1, brush);
2217 if (brush->id.properties) IDP_WriteProperty(brush->id.properties, wd);
2218 for(a=0; a<MAX_MTEX; a++)
2220 writestruct(wd, DATA, "MTex", 1, brush->mtex[a]);
2223 write_curvemapping(wd, brush->curve);
2228 static void write_scripts(WriteData *wd, ListBase *idbase)
2232 for(script=idbase->first; script; script= script->id.next) {
2233 if(script->id.us>0 || wd->current) {
2234 writestruct(wd, ID_SCRIPT, "Script", 1, script);
2235 if (script->id.properties) IDP_WriteProperty(script->id.properties, wd);
2240 /* context is usually defined by WM, two cases where no WM is available:
2241 * - for forward compatibility, curscreen has to be saved
2242 * - for undofile, curscene needs to be saved */
2243 static void write_global(WriteData *wd, Main *mainvar)
2249 current_screen_compat(mainvar, &screen);
2251 /* XXX still remap G */
2252 fg.curscreen= screen;
2253 fg.curscene= screen->scene;
2254 fg.displaymode= G.displaymode;
2255 fg.winpos= G.winpos;
2256 fg.fileflags= (G.fileflags & ~G_FILE_NO_UI); // prevent to save this, is not good convention, and feature with concerns...
2259 sprintf(subvstr, "%4d", BLENDER_SUBVERSION);
2260 memcpy(fg.subvstr, subvstr, 4);
2262 fg.subversion= BLENDER_SUBVERSION;
2263 fg.minversion= BLENDER_MINVERSION;
2264 fg.minsubversion= BLENDER_MINSUBVERSION;
2265 fg.pads= 0; /* prevent mem checkers from complaining */
2266 writestruct(wd, GLOB, "FileGlobal", 1, &fg);
2269 /* if MemFile * there's filesave to memory */
2270 static int write_file_handle(Main *mainvar, int handle, MemFile *compare, MemFile *current,
2271 int write_user_block, int write_flags)
2278 blo_split_main(&mainlist, mainvar);
2280 wd= bgnwrite(handle, compare, current, write_flags);
2282 sprintf(buf, "BLENDER%c%c%.3d", (sizeof(void*)==8)?'-':'_', (ENDIAN_ORDER==B_ENDIAN)?'V':'v', BLENDER_VERSION);
2283 mywrite(wd, buf, 12);
2285 write_renderinfo(wd, mainvar);
2286 write_global(wd, mainvar);
2288 /* no UI save in undo */
2290 write_windowmanagers(wd, &mainvar->wm);
2291 write_screens (wd, &mainvar->screen);
2293 write_scenes (wd, &mainvar->scene);
2294 write_curves (wd, &mainvar->curve);
2295 write_mballs (wd, &mainvar->mball);
2296 write_images (wd, &mainvar->image);
2297 write_cameras (wd, &mainvar->camera);
2298 write_lamps (wd, &mainvar->lamp);
2299 write_lattices (wd, &mainvar->latt);
2300 write_vfonts (wd, &mainvar->vfont);
2301 write_keys (wd, &mainvar->key);
2302 write_worlds (wd, &mainvar->world);
2303 write_texts (wd, &mainvar->text);
2304 write_sounds (wd, &mainvar->sound);
2305 write_groups (wd, &mainvar->group);
2306 write_armatures(wd, &mainvar->armature);
2307 write_actions (wd, &mainvar->action);
2308 write_objects (wd, &mainvar->object, (current != NULL));
2309 write_materials(wd, &mainvar->mat);
2310 write_textures (wd, &mainvar->tex);
2311 write_meshs (wd, &mainvar->mesh);
2312 write_particlesettings(wd, &mainvar->particle);
2313 write_nodetrees(wd, &mainvar->nodetree);
2314 write_brushes (wd, &mainvar->brush);
2315 write_scripts (wd, &mainvar->script);
2316 write_gpencils (wd, &mainvar->gpencil);
2318 write_libraries(wd, mainvar->next); /* no library save in undo */
2320 if (write_user_block) {
2324 /* dna as last, because (to be implemented) test for which structs are written */
2325 writedata(wd, DNA1, wd->sdna->datalen, wd->sdna->data);
2328 memset(&bhead, 0, sizeof(BHead));
2330 mywrite(wd, &bhead, sizeof(BHead));
2332 blo_join_main(&mainlist);
2334 return endwrite(wd);
2337 /* return: success (1) */
2338 int BLO_write_file(Main *mainvar, char *dir, int write_flags, ReportList *reports)
2340 char userfilename[FILE_MAXDIR+FILE_MAXFILE];
2341 char tempname[FILE_MAXDIR+FILE_MAXFILE];
2342 int file, err, write_user_block;
2344 sprintf(tempname, "%s@", dir);
2346 file = open(tempname,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
2348 BKE_report(reports, RPT_ERROR, "Unable to open file for writing.");
2352 BLI_make_file_string(G.sce, userfilename, BLI_gethome(), ".B25.blend");
2353 write_user_block= BLI_streq(dir, userfilename);
2355 err= write_file_handle(mainvar, file, NULL,NULL, write_user_block, write_flags);
2359 if(write_flags & G_FILE_COMPRESS)
2361 // compressed files have the same ending as regular files... only from 2.4!!!
2363 int ret = BLI_gzip(tempname, dir);
2366 BKE_report(reports, RPT_ERROR, "Failed opening .gz file.");
2370 BKE_report(reports, RPT_ERROR, "Failed opening .blend file for compression.");
2375 if(BLI_rename(tempname, dir) != 0) {
2376 BKE_report(reports, RPT_ERROR, "Can't change old file. File saved with @");
2382 BKE_report(reports, RPT_ERROR, strerror(errno));
2391 /* return: success (1) */
2392 int BLO_write_file_mem(Main *mainvar, MemFile *compare, MemFile *current, int write_flags, ReportList *reports)
2396 err= write_file_handle(mainvar, 0, compare, current, 0, write_flags);
2398 if(err==0) return 1;
2403 /* Runtime writing */
2406 #define PATHSEPERATOR "\\"
2408 #define PATHSEPERATOR "/"
2411 static char *get_runtime_path(char *exename) {
2412 char *installpath= get_install_dir();
2417 char *path= MEM_mallocN(strlen(installpath)+strlen(PATHSEPERATOR)+strlen(exename)+1, "runtimepath");
2420 MEM_freeN(installpath);
2424 strcpy(path, installpath);
2425 strcat(path, PATHSEPERATOR);
2426 strcat(path, exename);
2428 MEM_freeN(installpath);
2436 static int recursive_copy_runtime(char *outname, char *exename, ReportList *reports)
2438 char *runtime = get_runtime_path(exename);
2439 char command[2 * (FILE_MAXDIR+FILE_MAXFILE) + 32];
2440 int progfd = -1, error= 0;
2443 BKE_report(reports, RPT_ERROR, "Unable to find runtime");
2447 //printf("runtimepath %s\n", runtime);
2449 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
2451 BKE_report(reports, RPT_ERROR, "Unable to find runtime");
2456 sprintf(command, "/bin/cp -R \"%s\" \"%s\"", runtime, outname);
2457 //printf("command %s\n", command);
2458 if (system(command) == -1) {
2459 BKE_report(reports, RPT_ERROR, "Couldn't copy runtime");
2472 int BLO_write_runtime(Main *mainvar, char *file, char *exename, ReportList *reports)
2474 char gamename[FILE_MAXDIR+FILE_MAXFILE];
2475 int outfd = -1, error= 0;
2477 // remove existing file / bundle
2478 //printf("Delete file %s\n", file);
2479 BLI_delete(file, 0, TRUE);
2481 if (!recursive_copy_runtime(file, exename, reports)) {
2486 strcpy(gamename, file);
2487 strcat(gamename, "/Contents/Resources/game.blend");
2488 //printf("gamename %s\n", gamename);
2489 outfd= open(gamename, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
2492 write_file_handle(mainvar, outfd, NULL,NULL, 0, G.fileflags);
2494 if (write(outfd, " ", 1) != 1) {
2495 BKE_report(reports, RPT_ERROR, "Unable to write to output file.");
2500 BKE_report(reports, RPT_ERROR, "Unable to open blenderfile.");
2508 BKE_reports_prepend(reports, "Unable to make runtime: ");
2512 #else /* !__APPLE__ */
2514 static int handle_append_runtime(int handle, char *exename, ReportList *reports)
2516 char *runtime= get_runtime_path(exename);
2517 unsigned char buf[1024];
2518 int count, progfd= -1, error= 0;
2520 if (!BLI_exists(runtime)) {
2521 BKE_report(reports, RPT_ERROR, "Unable to find runtime.");
2526 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
2528 BKE_report(reports, RPT_ERROR, "Unable to find runtime.@");
2533 while ((count= read(progfd, buf, sizeof(buf)))>0) {
2534 if (write(handle, buf, count)!=count) {
2535 BKE_report(reports, RPT_ERROR, "Unable to write to output file.");
2550 static int handle_write_msb_int(int handle, int i)
2552 unsigned char buf[4];
2553 buf[0]= (i>>24)&0xFF;
2554 buf[1]= (i>>16)&0xFF;
2555 buf[2]= (i>>8)&0xFF;
2556 buf[3]= (i>>0)&0xFF;
2558 return (write(handle, buf, 4)==4);
2561 int BLO_write_runtime(Main *mainvar, char *file, char *exename, ReportList *reports)
2563 int outfd= open(file, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
2564 int datastart, error= 0;
2567 BKE_report(reports, RPT_ERROR, "Unable to open output file.");
2571 if (!handle_append_runtime(outfd, exename, reports)) {
2576 datastart= lseek(outfd, 0, SEEK_CUR);
2578 write_file_handle(mainvar, outfd, NULL,NULL, 0, G.fileflags);
2580 if (!handle_write_msb_int(outfd, datastart) || (write(outfd, "BRUNTIME", 8)!=8)) {
2581 BKE_report(reports, RPT_ERROR, "Unable to write to output file.");
2590 BKE_reports_prepend(reports, "Unable to make runtime: ");
2594 #endif /* !__APPLE__ */