7 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version. The Blender
13 * Foundation also sells licenses for use in proprietary software under
14 * the Blender License. See http://www.blender.org/BL/ for information
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
27 * All rights reserved.
29 * The Original Code is: all of this file.
31 * Contributor(s): none yet.
33 * ***** END GPL/BL DUAL LICENSE BLOCK *****
37 FILEFORMAT: IFF-style structure (but not IFF compatible!)
40 BLENDER_V100 12 bytes (versie 1.00)
41 V = big endian, v = little endian
42 _ = 4 byte pointer, - = 8 byte pointer
44 datablocks: also see struct BHead
46 <bh.len> int, len data after BHead
47 <bh.old> void, old pointer
49 <bh.nr> int, in case of array: amount of structs
54 Almost all data in Blender are structures. Each struct saved
55 gets a BHead header. With BHead the struct can be linked again
56 and compared with StructDNA .
60 Preferred writing order: (not really a must, but why would you do it random?)
61 Any case: direct data is ALWAYS after the lib block
66 - write associated direct data
71 - write the ID of LibBlock
72 - write FileGlobal (some global vars)
74 - write USER if filename is ~/.B.blend
78 Important to know is that 'streaming' has been added to files, for Blender Publisher
92 #include "BLI_winstuff.h"
94 #include <process.h> // for getpid
103 #include "nla.h" // __NLA is defined
105 #include "DNA_armature_types.h"
106 #include "DNA_action_types.h"
107 #include "DNA_actuator_types.h"
108 #include "DNA_brush_types.h"
109 #include "DNA_camera_types.h"
110 #include "DNA_cloth_types.h"
111 #include "DNA_color_types.h"
112 #include "DNA_constraint_types.h"
113 #include "DNA_controller_types.h"
114 #include "DNA_curve_types.h"
115 #include "DNA_customdata_types.h"
116 #include "DNA_effect_types.h"
117 #include "DNA_group_types.h"
118 #include "DNA_image_types.h"
119 #include "DNA_ipo_types.h"
120 #include "DNA_fileglobal_types.h"
121 #include "DNA_key_types.h"
122 #include "DNA_lattice_types.h"
123 #include "DNA_listBase.h" /* for Listbase, the type of samples, ...*/
124 #include "DNA_lamp_types.h"
125 #include "DNA_meta_types.h"
126 #include "DNA_mesh_types.h"
127 #include "DNA_meshdata_types.h"
128 #include "DNA_material_types.h"
129 #include "DNA_modifier_types.h"
130 #include "DNA_nla_types.h"
131 #include "DNA_node_types.h"
132 #include "DNA_object_types.h"
133 #include "DNA_object_force.h"
134 #include "DNA_oops_types.h"
135 #include "DNA_packedFile_types.h"
136 #include "DNA_particle_types.h"
137 #include "DNA_property_types.h"
138 #include "DNA_scene_types.h"
139 #include "DNA_sdna_types.h"
140 #include "DNA_sequence_types.h"
141 #include "DNA_sensor_types.h"
142 #include "DNA_space_types.h"
143 #include "DNA_screen_types.h"
144 #include "DNA_sound_types.h"
145 #include "DNA_texture_types.h"
146 #include "DNA_text_types.h"
147 #include "DNA_view3d_types.h"
148 #include "DNA_vfont_types.h"
149 #include "DNA_userdef_types.h"
150 #include "DNA_world_types.h"
152 #include "MEM_guardedalloc.h" // MEM_freeN
153 #include "BLI_blenlib.h"
154 #include "BLI_linklist.h"
156 #include "BKE_action.h"
157 #include "BKE_bad_level_calls.h" // build_seqar (from WHILE_SEQ) free_oops error
158 #include "BKE_blender.h"
159 #include "BKE_cloth.h"
160 #include "BKE_curve.h"
161 #include "BKE_customdata.h"
162 #include "BKE_constraint.h"
163 #include "BKE_global.h" // for G
164 #include "BKE_library.h" // for set_listbasepointers
165 #include "BKE_main.h" // G.main
166 #include "BKE_node.h"
167 #include "BKE_packedFile.h" // for packAll
168 #include "BKE_screen.h" // for waitcursor
169 #include "BKE_scene.h" // for do_seq
170 #include "BKE_sound.h" /* ... and for samples */
171 #include "BKE_utildefines.h" // for defines
172 #include "BKE_modifier.h"
173 #include "BKE_idprop.h"
175 #include "BKE_verse.h"
176 #include "BIF_verse.h"
179 #include "GEN_messaging.h"
181 #include "BLO_writefile.h"
182 #include "BLO_readfile.h"
183 #include "BLO_undofile.h"
185 #include "readfile.h"
190 /* ********* my write, buffered writing with minimum 50k chunks ************ */
197 MemFile *compare, *current;
199 int tot, count, error, memsize;
202 static WriteData *writedata_new(int file)
204 extern unsigned char DNAstr[]; /* DNA.c */
207 WriteData *wd= MEM_callocN(sizeof(*wd), "writedata");
209 /* XXX, see note about this in readfile.c, remove
210 * once we have an xp lock - zr
213 if (wd == NULL) return NULL;
215 wd->sdna= dna_sdna_from_data(DNAstr, DNAlen, 0);
219 wd->buf= MEM_mallocN(100000, "wd->buf");
224 static void writedata_do_write(WriteData *wd, void *mem, int memlen)
226 if ((wd == NULL) || wd->error || (mem == NULL) || memlen < 1) return;
227 if (wd->error) return;
229 /* memory based save */
231 add_memfilechunk(NULL, wd->current, mem, memlen);
234 if (write(wd->file, mem, memlen) != memlen)
240 static void writedata_free(WriteData *wd)
242 dna_freestructDNA(wd->sdna);
253 * Low level WRITE(2) wrapper that buffers data
254 * @param adr Pointer to new chunk of data
255 * @param len Length of new chunk of data
256 * @warning Talks to other functions with global parameters
259 #define MYWRITE_FLUSH NULL
261 static void mywrite( WriteData *wd, void *adr, int len)
263 if (wd->error) return;
265 if(adr==MYWRITE_FLUSH) {
267 writedata_do_write(wd, wd->buf, wd->count);
277 writedata_do_write(wd, wd->buf, wd->count);
280 writedata_do_write(wd, adr, len);
283 if(len+wd->count>99999) {
284 writedata_do_write(wd, wd->buf, wd->count);
287 memcpy(&wd->buf[wd->count], adr, len);
293 * BeGiN initializer for mywrite
294 * @param file File descriptor
295 * @param write_flags Write parameters
296 * @warning Talks to other functions with global parameters
298 static WriteData *bgnwrite(int file, MemFile *compare, MemFile *current, int write_flags)
300 WriteData *wd= writedata_new(file);
302 if (wd == NULL) return NULL;
304 wd->compare= compare;
305 wd->current= current;
306 /* this inits comparing */
307 add_memfilechunk(compare, NULL, NULL, 0);
313 * END the mywrite wrapper
314 * @return 1 if write failed
315 * @return unknown global variable otherwise
316 * @warning Talks to other functions with global parameters
318 static int endwrite(WriteData *wd)
323 writedata_do_write(wd, wd->buf, wd->count);
333 /* ********** WRITE FILE ****************** */
335 static void writestruct(WriteData *wd, int filecode, char *structname, int nr, void *adr)
340 if(adr==NULL || nr==0) return;
347 bh.SDNAnr= dna_findstruct_nr(wd->sdna, structname);
349 printf("error: can't find SDNA code <%s>\n", structname);
352 sp= wd->sdna->structs[bh.SDNAnr];
354 bh.len= nr*wd->sdna->typelens[sp[0]];
356 if(bh.len==0) return;
358 mywrite(wd, &bh, sizeof(BHead));
359 mywrite(wd, adr, bh.len);
362 static void writedata(WriteData *wd, int filecode, int len, void *adr) /* do not use for structs */
379 mywrite(wd, &bh, sizeof(BHead));
380 if(len) mywrite(wd, adr, len);
383 /* *************** writing some direct data structs used in more code parts **************** */
384 /*These functions are used by blender's .blend system for file saving/loading.*/
385 void IDP_WriteProperty_OnlyData(IDProperty *prop, void *wd);
386 void IDP_WriteProperty(IDProperty *prop, void *wd);
388 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);
396 void IDP_WriteString(IDProperty *prop, void *wd)
398 /*REMEMBER to set totalen to len in the linking code!!*/
399 writedata(wd, DATA, prop->len+1, prop->data.pointer);
402 void IDP_WriteGroup(IDProperty *prop, void *wd)
406 for (loop=prop->data.group.first; loop; loop=loop->next) {
407 IDP_WriteProperty(loop, wd);
411 /* Functions to read/write ID Properties */
412 void IDP_WriteProperty_OnlyData(IDProperty *prop, void *wd)
414 switch (prop->type) {
416 IDP_WriteGroup(prop, wd);
419 IDP_WriteString(prop, wd);
422 IDP_WriteArray(prop, wd);
427 void IDP_WriteProperty(IDProperty *prop, void *wd)
429 writestruct(wd, DATA, "IDProperty", 1, prop);
430 IDP_WriteProperty_OnlyData(prop, wd);
433 static void write_curvemapping(WriteData *wd, CurveMapping *cumap)
437 writestruct(wd, DATA, "CurveMapping", 1, cumap);
438 for(a=0; a<CM_TOT; a++)
439 writestruct(wd, DATA, "CurveMapPoint", cumap->cm[a].totpoint, cumap->cm[a].curve);
442 /* this is only direct data, tree itself should have been written */
443 static void write_nodetree(WriteData *wd, bNodeTree *ntree)
449 /* for link_list() speed, we write per list */
451 for(node= ntree->nodes.first; node; node= node->next)
452 writestruct(wd, DATA, "bNode", 1, node);
454 for(node= ntree->nodes.first; node; node= node->next) {
456 /* could be handlerized at some point, now only 1 exception still */
457 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
458 write_curvemapping(wd, node->storage);
459 else if(ntree->type==NTREE_COMPOSIT && (node->type==CMP_NODE_TIME || node->type==CMP_NODE_CURVE_VEC || node->type==CMP_NODE_CURVE_RGB))
460 write_curvemapping(wd, node->storage);
462 writestruct(wd, DATA, node->typeinfo->storagename, 1, node->storage);
464 for(sock= node->inputs.first; sock; sock= sock->next)
465 writestruct(wd, DATA, "bNodeSocket", 1, sock);
466 for(sock= node->outputs.first; sock; sock= sock->next)
467 writestruct(wd, DATA, "bNodeSocket", 1, sock);
470 for(link= ntree->links.first; link; link= link->next)
471 writestruct(wd, DATA, "bNodeLink", 1, link);
474 static void write_scriptlink(WriteData *wd, ScriptLink *slink)
476 writedata(wd, DATA, sizeof(void *)*slink->totscript, slink->scripts);
477 writedata(wd, DATA, sizeof(short)*slink->totscript, slink->flag);
480 static void write_renderinfo(WriteData *wd) /* for renderdeamon */
485 sce= G.main->scene.first;
487 if(sce->id.lib==0 && ( sce==G.scene || (sce->r.scemode & R_BG_RENDER)) ) {
488 data[0]= sce->r.sfra;
489 data[1]= sce->r.efra;
491 memset(data+2, 0, sizeof(int)*6);
492 strncpy((char *)(data+2), sce->id.name+2, 21);
494 writedata(wd, REND, 32, data);
500 static void write_userdef(WriteData *wd)
504 writestruct(wd, USER, "UserDef", 1, &U);
506 btheme= U.themes.first;
508 writestruct(wd, DATA, "bTheme", 1, btheme);
509 btheme= btheme->next;
513 static void write_effects(WriteData *wd, ListBase *lb)
522 writestruct(wd, DATA, "PartEff", 1, eff);
525 writedata(wd, DATA, MEM_allocN_len(eff), eff);
532 static void write_particlesettings(WriteData *wd, ListBase *idbase)
534 ParticleSettings *part;
538 if(part->id.us>0 || wd->current) {
540 writestruct(wd, ID_PA, "ParticleSettings", 1, part);
541 writestruct(wd, DATA, "PartDeflect", 1, part->pd);
546 static void write_particlesystems(WriteData *wd, ListBase *particles)
548 ParticleSystem *psys= particles->first;
551 for(; psys; psys=psys->next) {
552 writestruct(wd, DATA, "ParticleSystem", 1, psys);
554 if(psys->particles) {
555 writestruct(wd, DATA, "ParticleData", psys->totpart ,psys->particles);
557 if(psys->particles->hair) {
558 ParticleData *pa = psys->particles;
560 for(a=0; a<psys->totpart; a++, pa++)
561 writestruct(wd, DATA, "HairKey", pa->totkey, pa->hair);
564 if(psys->child) writestruct(wd, DATA, "ChildParticle", psys->totchild ,psys->child);
565 writestruct(wd, DATA, "SoftBody", 1, psys->soft);
569 static void write_properties(WriteData *wd, ListBase *lb)
575 writestruct(wd, DATA, "bProperty", 1, prop);
577 if(prop->poin && prop->poin != &prop->data)
578 writedata(wd, DATA, MEM_allocN_len(prop->poin), prop->poin);
584 static void write_sensors(WriteData *wd, ListBase *lb)
590 writestruct(wd, DATA, "bSensor", 1, sens);
592 writedata(wd, DATA, sizeof(void *)*sens->totlinks, sens->links);
596 writestruct(wd, DATA, "bNearSensor", 1, sens->data);
599 writestruct(wd, DATA, "bMouseSensor", 1, sens->data);
602 writestruct(wd, DATA, "bTouchSensor", 1, sens->data);
605 writestruct(wd, DATA, "bKeyboardSensor", 1, sens->data);
608 writestruct(wd, DATA, "bPropertySensor", 1, sens->data);
611 writestruct(wd, DATA, "bCollisionSensor", 1, sens->data);
614 writestruct(wd, DATA, "bRadarSensor", 1, sens->data);
617 writestruct(wd, DATA, "bRandomSensor", 1, sens->data);
620 writestruct(wd, DATA, "bRaySensor", 1, sens->data);
623 writestruct(wd, DATA, "bMessageSensor", 1, sens->data);
626 writestruct(wd, DATA, "bJoystickSensor", 1, sens->data);
629 ; /* error: don't know how to write this file */
636 static void write_controllers(WriteData *wd, ListBase *lb)
642 writestruct(wd, DATA, "bController", 1, cont);
644 writedata(wd, DATA, sizeof(void *)*cont->totlinks, cont->links);
647 case CONT_EXPRESSION:
648 writestruct(wd, DATA, "bExpressionCont", 1, cont->data);
651 writestruct(wd, DATA, "bPythonCont", 1, cont->data);
654 ; /* error: don't know how to write this file */
661 static void write_actuators(WriteData *wd, ListBase *lb)
667 writestruct(wd, DATA, "bActuator", 1, act);
671 writestruct(wd, DATA, "bActionActuator", 1, act->data);
674 writestruct(wd, DATA, "bSoundActuator", 1, act->data);
677 writestruct(wd, DATA, "bCDActuator", 1, act->data);
680 writestruct(wd, DATA, "bObjectActuator", 1, act->data);
683 writestruct(wd, DATA, "bIpoActuator", 1, act->data);
686 writestruct(wd, DATA, "bPropertyActuator", 1, act->data);
689 writestruct(wd, DATA, "bCameraActuator", 1, act->data);
692 writestruct(wd, DATA, "bConstraintActuator", 1, act->data);
694 case ACT_EDIT_OBJECT:
695 writestruct(wd, DATA, "bEditObjectActuator", 1, act->data);
698 writestruct(wd, DATA, "bSceneActuator", 1, act->data);
701 writestruct(wd, DATA, "bGroupActuator", 1, act->data);
704 writestruct(wd, DATA, "bRandomActuator", 1, act->data);
707 writestruct(wd, DATA, "bMessageActuator", 1, act->data);
710 writestruct(wd, DATA, "bGameActuator", 1, act->data);
713 writestruct(wd, DATA, "bVisibilityActuator", 1, act->data);
716 ; /* error: don't know how to write this file */
723 static void write_nlastrips(WriteData *wd, ListBase *nlabase)
726 bActionModifier *amod;
728 for (strip=nlabase->first; strip; strip=strip->next)
729 writestruct(wd, DATA, "bActionStrip", 1, strip);
730 for (strip=nlabase->first; strip; strip=strip->next) {
731 for(amod= strip->modifiers.first; amod; amod= amod->next)
732 writestruct(wd, DATA, "bActionModifier", 1, amod);
736 static void write_constraints(WriteData *wd, ListBase *conlist)
740 for (con=conlist->first; con; con=con->next) {
741 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
743 /* Write the specific data */
744 if (cti && con->data) {
745 /* firstly, just write the plain con->data struct */
746 writestruct(wd, DATA, cti->structName, 1, con->data);
748 /* do any constraint specific stuff */
750 case CONSTRAINT_TYPE_PYTHON:
752 bPythonConstraint *data = (bPythonConstraint *)con->data;
753 bConstraintTarget *ct;
756 for (ct= data->targets.first; ct; ct= ct->next)
757 writestruct(wd, DATA, "bConstraintTarget", 1, ct);
759 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
760 of library blocks that implement this.*/
761 IDP_WriteProperty(data->prop, wd);
767 /* Write the constraint */
768 writestruct(wd, DATA, "bConstraint", 1, con);
772 static void write_pose(WriteData *wd, bPose *pose)
776 /* Write each channel */
781 for (chan=pose->chanbase.first; chan; chan=chan->next) {
782 write_constraints(wd, &chan->constraints);
784 /* prevent crashes with autosave, when a bone duplicated in editmode has not yet been assigned to its posechannel */
786 chan->selectflag= chan->bone->flag & (BONE_SELECTED|BONE_ACTIVE); /* gets restored on read, for library armatures */
788 writestruct(wd, DATA, "bPoseChannel", 1, chan);
791 /* Write this pose */
792 writestruct(wd, DATA, "bPose", 1, pose);
795 static void write_defgroups(WriteData *wd, ListBase *defbase)
797 bDeformGroup *defgroup;
799 for (defgroup=defbase->first; defgroup; defgroup=defgroup->next)
800 writestruct(wd, DATA, "bDeformGroup", 1, defgroup);
803 static void write_constraint_channels(WriteData *wd, ListBase *chanbase)
805 bConstraintChannel *chan;
807 for (chan = chanbase->first; chan; chan=chan->next)
808 writestruct(wd, DATA, "bConstraintChannel", 1, chan);
812 static void write_modifiers(WriteData *wd, ListBase *modbase)
816 if (modbase == NULL) return;
817 for (md=modbase->first; md; md= md->next) {
818 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
819 if (mti == NULL) return;
821 writestruct(wd, DATA, mti->structName, 1, md);
823 if (md->type==eModifierType_Hook) {
824 HookModifierData *hmd = (HookModifierData*) md;
826 writedata(wd, DATA, sizeof(int)*hmd->totindex, hmd->indexar);
828 else if(md->type==eModifierType_Cloth) {
829 ClothModifierData *clmd = (ClothModifierData*) md;
830 writestruct(wd, DATA, "SimulationSettings", 1, clmd->sim_parms);
831 writestruct(wd, DATA, "CollisionSettings", 1, clmd->coll_parms);
833 else if (md->type==eModifierType_MeshDeform) {
834 MeshDeformModifierData *mmd = (MeshDeformModifierData*) md;
835 int size = mmd->dyngridsize;
837 writedata(wd, DATA, sizeof(float)*mmd->totvert*mmd->totcagevert,
839 writedata(wd, DATA, sizeof(float)*3*mmd->totcagevert,
841 writestruct(wd, DATA, "MDefCell", size*size*size, mmd->dyngrid);
842 writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->dyninfluences);
843 writedata(wd, DATA, sizeof(int)*mmd->totvert, mmd->dynverts);
848 static void write_objects(WriteData *wd, ListBase *idbase)
854 if(ob->id.us>0 || wd->current) {
857 /* pointer at vnode stored in file have to be NULL */
858 struct VNode *vnode = (VNode*)ob->vnode;
859 if(vnode) ob->vnode = NULL;
861 writestruct(wd, ID_OB, "Object", 1, ob);
863 if(vnode) ob->vnode = (void*)vnode;
866 /*Write ID Properties -- and copy this comment EXACTLY for easy finding
867 of library blocks that implement this.*/
868 if (ob->id.properties) IDP_WriteProperty(ob->id.properties, wd);
871 writedata(wd, DATA, sizeof(void *)*ob->totcol, ob->mat);
872 write_effects(wd, &ob->effect);
873 write_properties(wd, &ob->prop);
874 write_sensors(wd, &ob->sensors);
875 write_controllers(wd, &ob->controllers);
876 write_actuators(wd, &ob->actuators);
877 write_scriptlink(wd, &ob->scriptlink);
878 write_pose(wd, ob->pose);
879 write_defgroups(wd, &ob->defbase);
880 write_constraints(wd, &ob->constraints);
881 write_constraint_channels(wd, &ob->constraintChannels);
882 write_nlastrips(wd, &ob->nlastrips);
884 writestruct(wd, DATA, "PartDeflect", 1, ob->pd);
885 writestruct(wd, DATA, "SoftBody", 1, ob->soft);
886 writestruct(wd, DATA, "FluidsimSettings", 1, ob->fluidsimSettings); // NT
888 write_particlesystems(wd, &ob->particlesystem);
889 write_modifiers(wd, &ob->modifiers);
894 /* flush helps the compression for undo-save */
895 mywrite(wd, MYWRITE_FLUSH, 0);
899 static void write_vfonts(WriteData *wd, ListBase *idbase)
906 if(vf->id.us>0 || wd->current) {
908 writestruct(wd, ID_VF, "VFont", 1, vf);
909 if (vf->id.properties) IDP_WriteProperty(vf->id.properties, wd);
913 if (vf->packedfile) {
915 writestruct(wd, DATA, "PackedFile", 1, pf);
916 writedata(wd, DATA, pf->size, pf->data);
924 static void write_ipos(WriteData *wd, ListBase *idbase)
931 if(ipo->id.us>0 || wd->current) {
933 writestruct(wd, ID_IP, "Ipo", 1, ipo);
934 if (ipo->id.properties) IDP_WriteProperty(ipo->id.properties, wd);
937 icu= ipo->curve.first;
939 writestruct(wd, DATA, "IpoCurve", 1, icu);
943 icu= ipo->curve.first;
945 if(icu->bezt) writestruct(wd, DATA, "BezTriple", icu->totvert, icu->bezt);
946 if(icu->bp) writestruct(wd, DATA, "BPoint", icu->totvert, icu->bp);
947 if(icu->driver) writestruct(wd, DATA, "IpoDriver", 1, icu->driver);
955 /* flush helps the compression for undo-save */
956 mywrite(wd, MYWRITE_FLUSH, 0);
959 static void write_keys(WriteData *wd, ListBase *idbase)
966 if(key->id.us>0 || wd->current) {
968 writestruct(wd, ID_KE, "Key", 1, key);
969 if (key->id.properties) IDP_WriteProperty(key->id.properties, wd);
972 kb= key->block.first;
974 writestruct(wd, DATA, "KeyBlock", 1, kb);
975 if(kb->data) writedata(wd, DATA, kb->totelem*key->elemsize, kb->data);
982 /* flush helps the compression for undo-save */
983 mywrite(wd, MYWRITE_FLUSH, 0);
986 static void write_cameras(WriteData *wd, ListBase *idbase)
992 if(cam->id.us>0 || wd->current) {
994 writestruct(wd, ID_CA, "Camera", 1, cam);
995 if (cam->id.properties) IDP_WriteProperty(cam->id.properties, wd);
998 write_scriptlink(wd, &cam->scriptlink);
1005 static void write_mballs(WriteData *wd, ListBase *idbase)
1012 if(mb->id.us>0 || wd->current) {
1014 writestruct(wd, ID_MB, "MetaBall", 1, mb);
1015 if (mb->id.properties) IDP_WriteProperty(mb->id.properties, wd);
1018 writedata(wd, DATA, sizeof(void *)*mb->totcol, mb->mat);
1020 ml= mb->elems.first;
1022 writestruct(wd, DATA, "MetaElem", 1, ml);
1030 int amount_of_chars(char *str)
1032 // Since the data is saved as UTF-8 to the cu->str
1033 // The cu->len is not same as the strlen(cu->str)
1037 static void write_curves(WriteData *wd, ListBase *idbase)
1044 if(cu->id.us>0 || wd->current) {
1046 writestruct(wd, ID_CU, "Curve", 1, cu);
1049 writedata(wd, DATA, sizeof(void *)*cu->totcol, cu->mat);
1050 if (cu->id.properties) IDP_WriteProperty(cu->id.properties, wd);
1053 writedata(wd, DATA, amount_of_chars(cu->str)+1, cu->str);
1054 writestruct(wd, DATA, "CharInfo", cu->len, cu->strinfo);
1055 writestruct(wd, DATA, "TextBox", cu->totbox, cu->tb);
1058 /* is also the order of reading */
1061 writestruct(wd, DATA, "Nurb", 1, nu);
1066 if( (nu->type & 7)==CU_BEZIER)
1067 writestruct(wd, DATA, "BezTriple", nu->pntsu, nu->bezt);
1069 writestruct(wd, DATA, "BPoint", nu->pntsu*nu->pntsv, nu->bp);
1070 if(nu->knotsu) writedata(wd, DATA, KNOTSU(nu)*sizeof(float), nu->knotsu);
1071 if(nu->knotsv) writedata(wd, DATA, KNOTSV(nu)*sizeof(float), nu->knotsv);
1080 /* flush helps the compression for undo-save */
1081 mywrite(wd, MYWRITE_FLUSH, 0);
1084 static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
1089 /* Write the dvert list */
1090 writestruct(wd, DATA, "MDeformVert", count, dvlist);
1092 /* Write deformation data for each dvert */
1093 for (i=0; i<count; i++) {
1095 writestruct(wd, DATA, "MDeformWeight", dvlist[i].totweight, dvlist[i].dw);
1100 static void write_customdata(WriteData *wd, int count, CustomData *data, int partial_type, int partial_count)
1104 writestruct(wd, DATA, "CustomDataLayer", data->maxlayer, data->layers);
1106 for (i=0; i<data->totlayer; i++) {
1107 CustomDataLayer *layer= &data->layers[i];
1109 int structnum, datasize;
1111 if (layer->type == CD_MDEFORMVERT) {
1112 /* layer types that allocate own memory need special handling */
1113 write_dverts(wd, count, layer->data);
1116 CustomData_file_write_info(layer->type, &structname, &structnum);
1118 /* when using partial visibility, the MEdge and MFace layers
1119 are smaller than the original, so their type and count is
1120 passed to make this work */
1121 if (layer->type != partial_type) datasize= structnum*count;
1122 else datasize= structnum*partial_count;
1124 writestruct(wd, DATA, structname, datasize, layer->data);
1127 printf("error: this CustomDataLayer must not be written to file\n");
1132 static void write_meshs(WriteData *wd, ListBase *idbase)
1137 mesh= idbase->first;
1139 if(mesh->id.us>0 || wd->current) {
1142 struct VNode *vnode = (VNode*)mesh->vnode;
1144 /* mesh has to be created from verse geometry node*/
1145 create_meshdata_from_geom_node(mesh, vnode);
1146 /* pointer at verse node can't be stored in file */
1151 writestruct(wd, ID_ME, "Mesh", 1, mesh);
1153 if(vnode) mesh->vnode = (void*)vnode;
1157 if (mesh->id.properties) IDP_WriteProperty(mesh->id.properties, wd);
1159 writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
1162 write_customdata(wd, mesh->pv->totvert, &mesh->vdata, -1, 0);
1163 write_customdata(wd, mesh->pv->totedge, &mesh->edata,
1164 CD_MEDGE, mesh->totedge);
1165 write_customdata(wd, mesh->pv->totface, &mesh->fdata,
1166 CD_MFACE, mesh->totface);
1169 write_customdata(wd, mesh->totvert, &mesh->vdata, -1, 0);
1170 write_customdata(wd, mesh->totedge, &mesh->edata, -1, 0);
1171 write_customdata(wd, mesh->totface, &mesh->fdata, -1, 0);
1175 writestruct(wd, DATA, "Multires", 1, mesh->mr);
1177 lvl= mesh->mr->levels.first;
1179 write_customdata(wd, lvl->totvert, &mesh->mr->vdata, -1, 0);
1180 write_customdata(wd, lvl->totface, &mesh->mr->fdata, -1, 0);
1181 writedata(wd, DATA, sizeof(short)*lvl->totedge, mesh->mr->edge_flags);
1182 writedata(wd, DATA, sizeof(char)*lvl->totedge, mesh->mr->edge_creases);
1185 for(; lvl; lvl= lvl->next) {
1186 writestruct(wd, DATA, "MultiresLevel", 1, lvl);
1187 writestruct(wd, DATA, "MultiresFace", lvl->totface, lvl->faces);
1188 writestruct(wd, DATA, "MultiresEdge", lvl->totedge, lvl->edges);
1189 writestruct(wd, DATA, "MultiresColFace", lvl->totface, lvl->colfaces);
1192 lvl= mesh->mr->levels.last;
1194 writestruct(wd, DATA, "MVert", lvl->totvert, mesh->mr->verts);
1199 writestruct(wd, DATA, "PartialVisibility", 1, mesh->pv);
1200 writedata(wd, DATA, sizeof(unsigned int)*mesh->pv->totvert, mesh->pv->vert_map);
1201 writedata(wd, DATA, sizeof(int)*mesh->pv->totedge, mesh->pv->edge_map);
1202 writestruct(wd, DATA, "MFace", mesh->pv->totface, mesh->pv->old_faces);
1203 writestruct(wd, DATA, "MEdge", mesh->pv->totedge, mesh->pv->old_edges);
1206 mesh= mesh->id.next;
1210 static void write_lattices(WriteData *wd, ListBase *idbase)
1216 if(lt->id.us>0 || wd->current) {
1218 writestruct(wd, ID_LT, "Lattice", 1, lt);
1219 if (lt->id.properties) IDP_WriteProperty(lt->id.properties, wd);
1222 writestruct(wd, DATA, "BPoint", lt->pntsu*lt->pntsv*lt->pntsw, lt->def);
1224 write_dverts(wd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
1231 static void write_previews(WriteData *wd, PreviewImage *prv)
1234 short w = prv->w[1];
1235 short h = prv->h[1];
1236 unsigned int *rect = prv->rect[1];
1237 /* don't write out large previews if not requested */
1238 if (!(U.flag & USER_SAVE_PREVIEWS) ) {
1241 prv->rect[1] = NULL;
1243 writestruct(wd, DATA, "PreviewImage", 1, prv);
1244 if (prv->rect[0]) writedata(wd, DATA, prv->w[0]*prv->h[0]*sizeof(unsigned int), prv->rect[0]);
1245 if (prv->rect[1]) writedata(wd, DATA, prv->w[1]*prv->h[1]*sizeof(unsigned int), prv->rect[1]);
1247 /* restore preview, we still want to keep it in memory even if not saved to file */
1248 if (!(U.flag & USER_SAVE_PREVIEWS) ) {
1251 prv->rect[1] = rect;
1256 static void write_images(WriteData *wd, ListBase *idbase)
1264 if(ima->id.us>0 || wd->current) {
1266 writestruct(wd, ID_IM, "Image", 1, ima);
1267 if (ima->id.properties) IDP_WriteProperty(ima->id.properties, wd);
1269 if (ima->packedfile) {
1270 pf = ima->packedfile;
1271 writestruct(wd, DATA, "PackedFile", 1, pf);
1272 writedata(wd, DATA, pf->size, pf->data);
1275 write_previews(wd, ima->preview);
1280 /* flush helps the compression for undo-save */
1281 mywrite(wd, MYWRITE_FLUSH, 0);
1284 static void write_textures(WriteData *wd, ListBase *idbase)
1290 if(tex->id.us>0 || wd->current) {
1292 writestruct(wd, ID_TE, "Tex", 1, tex);
1293 if (tex->id.properties) IDP_WriteProperty(tex->id.properties, wd);
1296 if(tex->plugin) writestruct(wd, DATA, "PluginTex", 1, tex->plugin);
1297 if(tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
1298 if(tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
1300 write_previews(wd, tex->preview);
1305 /* flush helps the compression for undo-save */
1306 mywrite(wd, MYWRITE_FLUSH, 0);
1309 static void write_materials(WriteData *wd, ListBase *idbase)
1316 if(ma->id.us>0 || wd->current) {
1318 writestruct(wd, ID_MA, "Material", 1, ma);
1320 /*Write ID Properties -- and copy this comment EXACTLY for easy finding
1321 of library blocks that implement this.*/
1322 /*manually set head group property to IDP_GROUP, just in case it hadn't been
1324 if (ma->id.properties) IDP_WriteProperty(ma->id.properties, wd);
1326 for(a=0; a<MAX_MTEX; a++) {
1327 if(ma->mtex[a]) writestruct(wd, DATA, "MTex", 1, ma->mtex[a]);
1330 if(ma->ramp_col) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_col);
1331 if(ma->ramp_spec) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_spec);
1333 write_scriptlink(wd, &ma->scriptlink);
1335 /* nodetree is integral part of material, no libdata */
1337 writestruct(wd, DATA, "bNodeTree", 1, ma->nodetree);
1338 write_nodetree(wd, ma->nodetree);
1341 write_previews(wd, ma->preview);
1347 static void write_worlds(WriteData *wd, ListBase *idbase)
1352 wrld= idbase->first;
1354 if(wrld->id.us>0 || wd->current) {
1356 writestruct(wd, ID_WO, "World", 1, wrld);
1357 if (wrld->id.properties) IDP_WriteProperty(wrld->id.properties, wd);
1359 for(a=0; a<MAX_MTEX; a++) {
1360 if(wrld->mtex[a]) writestruct(wd, DATA, "MTex", 1, wrld->mtex[a]);
1363 write_scriptlink(wd, &wrld->scriptlink);
1365 write_previews(wd, wrld->preview);
1368 wrld= wrld->id.next;
1372 static void write_lamps(WriteData *wd, ListBase *idbase)
1379 if(la->id.us>0 || wd->current) {
1381 writestruct(wd, ID_LA, "Lamp", 1, la);
1382 if (la->id.properties) IDP_WriteProperty(la->id.properties, wd);
1385 for(a=0; a<MAX_MTEX; a++) {
1386 if(la->mtex[a]) writestruct(wd, DATA, "MTex", 1, la->mtex[a]);
1390 write_curvemapping(wd, la->curfalloff);
1392 write_scriptlink(wd, &la->scriptlink);
1394 write_previews(wd, la->preview);
1402 static void write_scenes(WriteData *wd, ListBase *scebase)
1411 SceneRenderLayer *srl;
1414 sce= scebase->first;
1417 writestruct(wd, ID_SCE, "Scene", 1, sce);
1418 if (sce->id.properties) IDP_WriteProperty(sce->id.properties, wd);
1421 base= sce->base.first;
1423 writestruct(wd, DATA, "Base", 1, base);
1427 writestruct(wd, DATA, "Radio", 1, sce->radio);
1428 writestruct(wd, DATA, "ToolSettings", 1, sce->toolsettings);
1430 for(a=0; a<MAX_MTEX; ++a)
1431 writestruct(wd, DATA, "MTex", 1, sce->sculptdata.mtex[a]);
1432 if(sce->sculptdata.cumap)
1433 write_curvemapping(wd, sce->sculptdata.cumap);
1437 writestruct(wd, DATA, "Editing", 1, ed);
1439 /* reset write flags too */
1440 WHILE_SEQ(&ed->seqbase) {
1441 if(seq->strip) seq->strip->done= 0;
1442 writestruct(wd, DATA, "Sequence", 1, seq);
1446 WHILE_SEQ(&ed->seqbase) {
1447 if(seq->strip && seq->strip->done==0) {
1448 /* write strip with 'done' at 0 because readfile */
1450 if(seq->plugin) writestruct(wd, DATA, "PluginSeq", 1, seq->plugin);
1451 if(seq->effectdata) {
1454 writestruct(wd, DATA, "SolidColorVars", 1, seq->effectdata);
1457 writestruct(wd, DATA, "SpeedControlVars", 1, seq->effectdata);
1460 writestruct(wd, DATA, "WipeVars", 1, seq->effectdata);
1463 writestruct(wd, DATA, "GlowVars", 1, seq->effectdata);
1466 writestruct(wd, DATA, "TransformVars", 1, seq->effectdata);
1472 writestruct(wd, DATA, "Strip", 1, strip);
1473 if(seq->flag & SEQ_USE_CROP && strip->crop) {
1474 writestruct(wd, DATA, "StripCrop", 1, strip->crop);
1476 if(seq->flag & SEQ_USE_TRANSFORM && strip->transform) {
1477 writestruct(wd, DATA, "StripTransform", 1, strip->transform);
1479 if(seq->flag & SEQ_USE_PROXY && strip->proxy) {
1480 writestruct(wd, DATA, "StripProxy", 1, strip->proxy);
1482 if(seq->type==SEQ_IMAGE)
1483 writestruct(wd, DATA, "StripElem", strip->len, strip->stripdata);
1484 else if(seq->type==SEQ_MOVIE || seq->type==SEQ_RAM_SOUND || seq->type == SEQ_HD_SOUND)
1485 writestruct(wd, DATA, "StripElem", 1, strip->stripdata);
1492 /* new; meta stack too, even when its nasty restore code */
1493 for(ms= ed->metastack.first; ms; ms= ms->next) {
1494 writestruct(wd, DATA, "MetaStack", 1, ms);
1498 write_scriptlink(wd, &sce->scriptlink);
1500 if (sce->r.avicodecdata) {
1501 writestruct(wd, DATA, "AviCodecData", 1, sce->r.avicodecdata);
1502 if (sce->r.avicodecdata->lpFormat) writedata(wd, DATA, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat);
1503 if (sce->r.avicodecdata->lpParms) writedata(wd, DATA, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms);
1506 if (sce->r.qtcodecdata) {
1507 writestruct(wd, DATA, "QuicktimeCodecData", 1, sce->r.qtcodecdata);
1508 if (sce->r.qtcodecdata->cdParms) writedata(wd, DATA, sce->r.qtcodecdata->cdSize, sce->r.qtcodecdata->cdParms);
1511 /* writing dynamic list of TimeMarkers to the blend file */
1512 for(marker= sce->markers.first; marker; marker= marker->next)
1513 writestruct(wd, DATA, "TimeMarker", 1, marker);
1515 for(srl= sce->r.layers.first; srl; srl= srl->next)
1516 writestruct(wd, DATA, "SceneRenderLayer", 1, srl);
1519 writestruct(wd, DATA, "bNodeTree", 1, sce->nodetree);
1520 write_nodetree(wd, sce->nodetree);
1525 /* flush helps the compression for undo-save */
1526 mywrite(wd, MYWRITE_FLUSH, 0);
1529 static void write_screens(WriteData *wd, ListBase *scrbase)
1539 writestruct(wd, ID_SCR, "Screen", 1, sc);
1540 if (sc->id.properties) IDP_WriteProperty(sc->id.properties, wd);
1543 sv= sc->vertbase.first;
1545 writestruct(wd, DATA, "ScrVert", 1, sv);
1549 se= sc->edgebase.first;
1551 writestruct(wd, DATA, "ScrEdge", 1, se);
1555 sa= sc->areabase.first;
1560 writestruct(wd, DATA, "ScrArea", 1, sa);
1562 pa= sa->panels.first;
1564 writestruct(wd, DATA, "Panel", 1, pa);
1568 /* space handler scriptlinks */
1569 write_scriptlink(wd, &sa->scriptlink);
1571 sl= sa->spacedata.first;
1573 if(sl->spacetype==SPACE_VIEW3D) {
1574 View3D *v3d= (View3D*) sl;
1575 writestruct(wd, DATA, "View3D", 1, v3d);
1576 if(v3d->bgpic) writestruct(wd, DATA, "BGpic", 1, v3d->bgpic);
1577 if(v3d->localvd) writestruct(wd, DATA, "View3D", 1, v3d->localvd);
1578 if(v3d->clipbb) writestruct(wd, DATA, "BoundBox", 1, v3d->clipbb);
1580 else if(sl->spacetype==SPACE_IPO) {
1581 writestruct(wd, DATA, "SpaceIpo", 1, sl);
1583 else if(sl->spacetype==SPACE_BUTS) {
1584 writestruct(wd, DATA, "SpaceButs", 1, sl);
1586 else if(sl->spacetype==SPACE_FILE) {
1587 writestruct(wd, DATA, "SpaceFile", 1, sl);
1589 else if(sl->spacetype==SPACE_SEQ) {
1590 writestruct(wd, DATA, "SpaceSeq", 1, sl);
1592 else if(sl->spacetype==SPACE_OOPS) {
1593 SpaceOops *so= (SpaceOops *)sl;
1597 oops= so->oops.first;
1599 Oops *oopsn= oops->next;
1601 BLI_remlink(&so->oops, oops);
1607 /* ater cleanup, because of listbase! */
1608 writestruct(wd, DATA, "SpaceOops", 1, so);
1610 oops= so->oops.first;
1612 writestruct(wd, DATA, "Oops", 1, oops);
1617 writestruct(wd, DATA, "TreeStore", 1, so->treestore);
1618 if(so->treestore->data)
1619 writestruct(wd, DATA, "TreeStoreElem", so->treestore->usedelem, so->treestore->data);
1622 else if(sl->spacetype==SPACE_IMAGE) {
1623 SpaceImage *sima= (SpaceImage *)sl;
1625 writestruct(wd, DATA, "SpaceImage", 1, sl);
1627 write_curvemapping(wd, sima->cumap);
1629 else if(sl->spacetype==SPACE_IMASEL) {
1630 writestruct(wd, DATA, "SpaceImaSel", 1, sl);
1632 else if(sl->spacetype==SPACE_TEXT) {
1633 writestruct(wd, DATA, "SpaceText", 1, sl);
1635 else if(sl->spacetype==SPACE_SCRIPT) {
1636 SpaceScript *sc = (SpaceScript*)sl;
1637 sc->but_refs = NULL;
1638 writestruct(wd, DATA, "SpaceScript", 1, sl);
1640 else if(sl->spacetype==SPACE_ACTION) {
1641 writestruct(wd, DATA, "SpaceAction", 1, sl);
1643 else if(sl->spacetype==SPACE_SOUND) {
1644 writestruct(wd, DATA, "SpaceSound", 1, sl);
1646 else if(sl->spacetype==SPACE_NLA){
1647 writestruct(wd, DATA, "SpaceNla", 1, sl);
1649 else if(sl->spacetype==SPACE_TIME){
1650 writestruct(wd, DATA, "SpaceTime", 1, sl);
1652 else if(sl->spacetype==SPACE_NODE){
1653 writestruct(wd, DATA, "SpaceNode", 1, sl);
1665 static void write_libraries(WriteData *wd, Main *main)
1667 ListBase *lbarray[30];
1669 int a, tot, foundone;
1671 for(; main; main= main->next) {
1673 a=tot= set_listbasepointers(main, lbarray);
1675 /* test: is lib being used */
1678 for(id= lbarray[tot]->first; id; id= id->next) {
1679 if(id->us>0 && (id->flag & LIB_EXTERN)) {
1688 writestruct(wd, ID_LI, "Library", 1, main->curlib);
1691 for(id= lbarray[a]->first; id; id= id->next) {
1692 if(id->us>0 && (id->flag & LIB_EXTERN)) {
1693 writestruct(wd, ID_ID, "ID", 1, id);
1701 static void write_bone(WriteData *wd, Bone* bone)
1705 // PATCH for upward compatibility after 2.37+ armature recode
1706 bone->size[0]= bone->size[1]= bone->size[2]= 1.0f;
1709 writestruct(wd, DATA, "Bone", 1, bone);
1712 cbone= bone->childbase.first;
1714 write_bone(wd, cbone);
1719 static void write_armatures(WriteData *wd, ListBase *idbase)
1726 if (arm->id.us>0 || wd->current) {
1727 writestruct(wd, ID_AR, "bArmature", 1, arm);
1728 if (arm->id.properties) IDP_WriteProperty(arm->id.properties, wd);
1731 bone= arm->bonebase.first;
1733 write_bone(wd, bone);
1740 /* flush helps the compression for undo-save */
1741 mywrite(wd, MYWRITE_FLUSH, 0);
1744 static void write_actions(WriteData *wd, ListBase *idbase)
1747 bActionChannel *chan;
1750 for(act=idbase->first; act; act= act->id.next) {
1751 if (act->id.us>0 || wd->current) {
1752 writestruct(wd, ID_AC, "bAction", 1, act);
1753 if (act->id.properties) IDP_WriteProperty(act->id.properties, wd);
1755 for (chan=act->chanbase.first; chan; chan=chan->next) {
1756 writestruct(wd, DATA, "bActionChannel", 1, chan);
1757 write_constraint_channels(wd, &chan->constraintChannels);
1760 for (marker=act->markers.first; marker; marker=marker->next) {
1761 writestruct(wd, DATA, "TimeMarker", 1, marker);
1767 static void write_texts(WriteData *wd, ListBase *idbase)
1772 text= idbase->first;
1774 if ( (text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) text->flags &= ~TXT_ISEXT;
1777 writestruct(wd, ID_TXT, "Text", 1, text);
1778 if(text->name) writedata(wd, DATA, strlen(text->name)+1, text->name);
1779 if (text->id.properties) IDP_WriteProperty(text->id.properties, wd);
1781 if(!(text->flags & TXT_ISEXT)) {
1782 /* now write the text data, in two steps for optimization in the readfunction */
1783 tmp= text->lines.first;
1785 writestruct(wd, DATA, "TextLine", 1, tmp);
1789 tmp= text->lines.first;
1791 writedata(wd, DATA, tmp->len+1, tmp->line);
1795 text= text->id.next;
1798 /* flush helps the compression for undo-save */
1799 mywrite(wd, MYWRITE_FLUSH, 0);
1802 static void write_sounds(WriteData *wd, ListBase *idbase)
1809 // set all samples to unsaved status
1811 sample = samples->first; // samples is a global defined in sound.c
1813 sample->flags |= SAMPLE_NEEDS_SAVE;
1814 sample = sample->id.next;
1817 sound= idbase->first;
1819 if(sound->id.us>0 || wd->current) {
1820 // do we need to save the packedfile as well ?
1821 sample = sound->sample;
1823 if (sample->flags & SAMPLE_NEEDS_SAVE) {
1824 sound->newpackedfile = sample->packedfile;
1825 sample->flags &= ~SAMPLE_NEEDS_SAVE;
1827 sound->newpackedfile = NULL;
1832 writestruct(wd, ID_SO, "bSound", 1, sound);
1833 if (sound->id.properties) IDP_WriteProperty(sound->id.properties, wd);
1835 if (sound->newpackedfile) {
1836 pf = sound->newpackedfile;
1837 writestruct(wd, DATA, "PackedFile", 1, pf);
1838 writedata(wd, DATA, pf->size, pf->data);
1842 sound->newpackedfile = sample->packedfile;
1845 sound= sound->id.next;
1848 /* flush helps the compression for undo-save */
1849 mywrite(wd, MYWRITE_FLUSH, 0);
1852 static void write_groups(WriteData *wd, ListBase *idbase)
1857 for(group= idbase->first; group; group= group->id.next) {
1858 if(group->id.us>0 || wd->current) {
1860 writestruct(wd, ID_GR, "Group", 1, group);
1861 if (group->id.properties) IDP_WriteProperty(group->id.properties, wd);
1863 go= group->gobject.first;
1865 writestruct(wd, DATA, "GroupObject", 1, go);
1872 static void write_nodetrees(WriteData *wd, ListBase *idbase)
1876 for(ntree=idbase->first; ntree; ntree= ntree->id.next) {
1877 if (ntree->id.us>0 || wd->current) {
1878 writestruct(wd, ID_NT, "bNodeTree", 1, ntree);
1879 write_nodetree(wd, ntree);
1880 if (ntree->id.properties) IDP_WriteProperty(ntree->id.properties, wd);
1885 static void write_brushes(WriteData *wd, ListBase *idbase)
1890 for(brush=idbase->first; brush; brush= brush->id.next) {
1891 if(brush->id.us>0 || wd->current) {
1892 writestruct(wd, ID_BR, "Brush", 1, brush);
1893 if (brush->id.properties) IDP_WriteProperty(brush->id.properties, wd);
1894 for(a=0; a<MAX_MTEX; a++)
1896 writestruct(wd, DATA, "MTex", 1, brush->mtex[a]);
1901 static void write_global(WriteData *wd)
1906 fg.curscreen= G.curscreen;
1907 fg.curscene= G.scene;
1908 fg.displaymode= G.displaymode;
1909 fg.winpos= G.winpos;
1910 fg.fileflags= (G.fileflags & ~G_FILE_NO_UI); // prevent to save this, is not good convention, and feature with concerns...
1913 sprintf(subvstr, "%4d", BLENDER_SUBVERSION);
1914 memcpy(fg.subvstr, subvstr, 4);
1916 fg.subversion= BLENDER_SUBVERSION;
1917 fg.minversion= BLENDER_MINVERSION;
1918 fg.minsubversion= BLENDER_MINSUBVERSION;
1920 writestruct(wd, GLOB, "FileGlobal", 1, &fg);
1923 /* if MemFile * there's filesave to memory */
1924 static int write_file_handle(int handle, MemFile *compare, MemFile *current, int write_user_block, int write_flags)
1931 blo_split_main(&mainlist, G.main);
1933 wd= bgnwrite(handle, compare, current, write_flags);
1935 sprintf(buf, "BLENDER%c%c%.3d", (sizeof(void*)==8)?'-':'_', (G.order==B_ENDIAN)?'V':'v', G.version);
1936 mywrite(wd, buf, 12);
1938 write_renderinfo(wd);
1942 write_screens (wd, &G.main->screen); /* no UI save in undo */
1943 write_scenes (wd, &G.main->scene);
1944 write_curves (wd, &G.main->curve);
1945 write_mballs (wd, &G.main->mball);
1946 write_images (wd, &G.main->image);
1947 write_cameras (wd, &G.main->camera);
1948 write_lamps (wd, &G.main->lamp);
1949 write_lattices (wd, &G.main->latt);
1950 write_vfonts (wd, &G.main->vfont);
1951 write_ipos (wd, &G.main->ipo);
1952 write_keys (wd, &G.main->key);
1953 write_worlds (wd, &G.main->world);
1954 write_texts (wd, &G.main->text);
1955 write_sounds (wd, &G.main->sound);
1956 write_groups (wd, &G.main->group);
1957 write_armatures(wd, &G.main->armature);
1958 write_actions (wd, &G.main->action);
1959 write_objects (wd, &G.main->object);
1960 write_materials(wd, &G.main->mat);
1961 write_textures (wd, &G.main->tex);
1962 write_meshs (wd, &G.main->mesh);
1963 write_particlesettings(wd, &G.main->particle);
1964 write_nodetrees(wd, &G.main->nodetree);
1965 write_brushes (wd, &G.main->brush);
1967 write_libraries(wd, G.main->next); /* no library save in undo */
1969 if (write_user_block) {
1973 /* dna as last, because (to be implemented) test for which structs are written */
1974 writedata(wd, DNA1, wd->sdna->datalen, wd->sdna->data);
1977 memset(&bhead, 0, sizeof(BHead));
1979 mywrite(wd, &bhead, sizeof(BHead));
1981 blo_join_main(&mainlist);
1982 G.main= mainlist.first;
1984 return endwrite(wd);
1987 /* return: success (1) */
1988 int BLO_write_file(char *dir, int write_flags, char **error_r)
1990 char userfilename[FILE_MAXDIR+FILE_MAXFILE];
1991 char tempname[FILE_MAXDIR+FILE_MAXFILE];
1992 int file, err, write_user_block;
1994 sprintf(tempname, "%s@", dir);
1996 file = open(tempname,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
1998 *error_r= "Unable to open";
2002 BLI_make_file_string(G.sce, userfilename, BLI_gethome(), ".B.blend");
2004 write_user_block= BLI_streq(dir, userfilename);
2006 err= write_file_handle(file, NULL,NULL, write_user_block, write_flags);
2010 if(write_flags & G_FILE_COMPRESS)
2012 // compressed files have the same ending as regular files... only from 2.4!!!
2014 int ret = BLI_gzip(tempname, dir);
2017 *error_r= "Failed opening .gz file";
2021 *error_r= "Failed opening .blend file for compression";
2026 if(BLI_rename(tempname, dir) < 0) {
2027 *error_r= "Can't change old file. File saved with @";
2033 *error_r= strerror(errno);
2042 /* return: success (1) */
2043 int BLO_write_file_mem(MemFile *compare, MemFile *current, int write_flags, char **error_r)
2047 err= write_file_handle(0, compare, current, 0, write_flags);
2049 if(err==0) return 1;
2054 /* Runtime writing */
2057 #define PATHSEPERATOR "\\"
2059 #define PATHSEPERATOR "/"
2062 static char *get_install_dir(void) {
2063 extern char bprogname[];
2064 char *tmpname = BLI_strdup(bprogname);
2068 cut = strstr(tmpname, ".app");
2069 if (cut) cut[0] = 0;
2072 cut = BLI_last_slash(tmpname);
2083 static char *get_runtime_path(char *exename) {
2084 char *installpath= get_install_dir();
2089 char *path= MEM_mallocN(strlen(installpath)+strlen(PATHSEPERATOR)+strlen(exename)+1, "runtimepath");
2092 MEM_freeN(installpath);
2096 strcpy(path, installpath);
2097 strcat(path, PATHSEPERATOR);
2098 strcat(path, exename);
2100 MEM_freeN(installpath);
2108 static int recursive_copy_runtime(char *outname, char *exename, char **cause_r)
2110 char *cause = NULL, *runtime = get_runtime_path(exename);
2111 char command[2 * (FILE_MAXDIR+FILE_MAXFILE) + 32];
2115 cause= "Unable to find runtime";
2118 //printf("runtimepath %s\n", runtime);
2120 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
2122 cause= "Unable to find runtime";
2126 sprintf(command, "/bin/cp -R \"%s\" \"%s\"", runtime, outname);
2127 //printf("command %s\n", command);
2128 if (system(command) == -1) {
2129 cause = "Couldn't copy runtime";
2145 void BLO_write_runtime(char *file, char *exename) {
2146 char gamename[FILE_MAXDIR+FILE_MAXFILE];
2150 // remove existing file / bundle
2151 //printf("Delete file %s\n", file);
2152 BLI_delete(file, 0, TRUE);
2154 if (!recursive_copy_runtime(file, exename, &cause))
2157 strcpy(gamename, file);
2158 strcat(gamename, "/Contents/Resources/game.blend");
2159 //printf("gamename %s\n", gamename);
2160 outfd= open(gamename, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
2163 write_file_handle(outfd, NULL,NULL, 0, G.fileflags);
2165 if (write(outfd, " ", 1) != 1) {
2166 cause= "Unable to write to output file";
2170 cause = "Unable to open blenderfile";
2178 error("Unable to make runtime: %s", cause);
2181 #else /* !__APPLE__ */
2183 static int handle_append_runtime(int handle, char *exename, char **cause_r) {
2184 char *cause= NULL, *runtime= get_runtime_path(exename);
2185 unsigned char buf[1024];
2186 int count, progfd= -1;
2189 cause= "Unable to find runtime";
2193 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
2195 cause= "Unable to find runtime";
2199 while ((count= read(progfd, buf, sizeof(buf)))>0) {
2200 if (write(handle, buf, count)!=count) {
2201 cause= "Unable to write to output file";
2219 static int handle_write_msb_int(int handle, int i) {
2220 unsigned char buf[4];
2221 buf[0]= (i>>24)&0xFF;
2222 buf[1]= (i>>16)&0xFF;
2223 buf[2]= (i>>8)&0xFF;
2224 buf[3]= (i>>0)&0xFF;
2226 return (write(handle, buf, 4)==4);
2229 void BLO_write_runtime(char *file, char *exename) {
2230 int outfd= open(file, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
2235 cause= "Unable to open output file";
2238 if (!handle_append_runtime(outfd, exename, &cause))
2241 datastart= lseek(outfd, 0, SEEK_CUR);
2243 write_file_handle(outfd, NULL,NULL, 0, G.fileflags);
2245 if (!handle_write_msb_int(outfd, datastart) || (write(outfd, "BRUNTIME", 8)!=8)) {
2246 cause= "Unable to write to output file";
2255 error("Unable to make runtime: %s", cause);
2258 #endif /* !__APPLE__ */