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_controller_types.h"
109 #include "DNA_curve_types.h"
110 #include "DNA_constraint_types.h"
111 #include "DNA_camera_types.h"
112 #include "DNA_color_types.h"
113 #include "DNA_effect_types.h"
114 #include "DNA_group_types.h"
115 #include "DNA_image_types.h"
116 #include "DNA_ipo_types.h"
117 #include "DNA_fileglobal_types.h"
118 #include "DNA_key_types.h"
119 #include "DNA_lattice_types.h"
120 #include "DNA_listBase.h" /* for Listbase, the type of samples, ...*/
121 #include "DNA_lamp_types.h"
122 #include "DNA_meta_types.h"
123 #include "DNA_mesh_types.h"
124 #include "DNA_meshdata_types.h"
125 #include "DNA_material_types.h"
126 #include "DNA_modifier_types.h"
127 #include "DNA_nla_types.h"
128 #include "DNA_node_types.h"
129 #include "DNA_object_types.h"
130 #include "DNA_object_force.h"
131 #include "DNA_oops_types.h"
132 #include "DNA_packedFile_types.h"
133 #include "DNA_property_types.h"
134 #include "DNA_scene_types.h"
135 #include "DNA_sdna_types.h"
136 #include "DNA_sequence_types.h"
137 #include "DNA_sensor_types.h"
138 #include "DNA_space_types.h"
139 #include "DNA_screen_types.h"
140 #include "DNA_sound_types.h"
141 #include "DNA_texture_types.h"
142 #include "DNA_text_types.h"
143 #include "DNA_view3d_types.h"
144 #include "DNA_vfont_types.h"
145 #include "DNA_userdef_types.h"
146 #include "DNA_world_types.h"
148 #include "MEM_guardedalloc.h" // MEM_freeN
149 #include "BLI_blenlib.h"
150 #include "BLI_linklist.h"
152 #include "BKE_action.h"
153 #include "BKE_bad_level_calls.h" // build_seqar (from WHILE_SEQ) free_oops error
154 #include "BKE_curve.h"
155 #include "BKE_constraint.h"
156 #include "BKE_global.h" // for G
157 #include "BKE_library.h" // for set_listbasepointers
158 #include "BKE_main.h" // G.main
159 #include "BKE_node.h"
160 #include "BKE_packedFile.h" // for packAll
161 #include "BKE_screen.h" // for waitcursor
162 #include "BKE_scene.h" // for do_seq
163 #include "BKE_sound.h" /* ... and for samples */
164 #include "BKE_utildefines.h" // for defines
165 #include "BKE_modifier.h"
167 #include "GEN_messaging.h"
169 #include "BLO_writefile.h"
170 #include "BLO_readfile.h"
171 #include "BLO_undofile.h"
173 #include "readfile.h"
178 /* ********* my write, buffered writing with minimum 50k chunks ************ */
185 MemFile *compare, *current;
187 int tot, count, error, memsize;
190 static WriteData *writedata_new(int file)
192 extern char DNAstr[]; /* DNA.c */
195 WriteData *wd= MEM_callocN(sizeof(*wd), "writedata");
197 /* XXX, see note about this in readfile.c, remove
198 * once we have an xp lock - zr
200 wd->sdna= dna_sdna_from_data(DNAstr, DNAlen, 0);
204 wd->buf= MEM_mallocN(100000, "wd->buf");
209 static void writedata_do_write(WriteData *wd, void *mem, int memlen)
211 if (wd->error) return;
213 /* memory based save */
215 add_memfilechunk(NULL, wd->current, mem, memlen);
218 if (write(wd->file, mem, memlen) != memlen)
224 static void writedata_free(WriteData *wd)
226 dna_freestructDNA(wd->sdna);
237 * Low level WRITE(2) wrapper that buffers data
238 * @param adr Pointer to new chunk of data
239 * @param len Length of new chunk of data
240 * @warning Talks to other functions with global parameters
243 #define MYWRITE_FLUSH NULL
245 static void mywrite( WriteData *wd, void *adr, int len)
247 if (wd->error) return;
249 if(adr==MYWRITE_FLUSH) {
251 writedata_do_write(wd, wd->buf, wd->count);
261 writedata_do_write(wd, wd->buf, wd->count);
264 writedata_do_write(wd, adr, len);
267 if(len+wd->count>99999) {
268 writedata_do_write(wd, wd->buf, wd->count);
271 memcpy(&wd->buf[wd->count], adr, len);
277 * BeGiN initializer for mywrite
278 * @param file File descriptor
279 * @param write_flags Write parameters
280 * @warning Talks to other functions with global parameters
282 static WriteData *bgnwrite(int file, MemFile *compare, MemFile *current, int write_flags)
284 WriteData *wd= writedata_new(file);
286 wd->compare= compare;
287 wd->current= current;
288 /* this inits comparing */
289 add_memfilechunk(compare, NULL, NULL, 0);
295 * END the mywrite wrapper
296 * @return 1 if write failed
297 * @return unknown global variable otherwise
298 * @warning Talks to other functions with global parameters
300 static int endwrite(WriteData *wd)
305 writedata_do_write(wd, wd->buf, wd->count);
311 /* blender gods may live forever but this parent pointer died in the statement above
312 if(wd->current) printf("undo size %d\n", wd->current->size);
317 /* ********** WRITE FILE ****************** */
319 static void writestruct(WriteData *wd, int filecode, char *structname, int nr, void *adr)
324 if(adr==NULL || nr==0) return;
331 bh.SDNAnr= dna_findstruct_nr(wd->sdna, structname);
333 printf("error: can't find SDNA code <%s>\n", structname);
336 sp= wd->sdna->structs[bh.SDNAnr];
338 bh.len= nr*wd->sdna->typelens[sp[0]];
340 if(bh.len==0) return;
342 mywrite(wd, &bh, sizeof(BHead));
343 mywrite(wd, adr, bh.len);
346 static void writedata(WriteData *wd, int filecode, int len, void *adr) /* do not use for structs */
363 mywrite(wd, &bh, sizeof(BHead));
364 if(len) mywrite(wd, adr, len);
367 /* *************** writing some direct data structs used in more code parts **************** */
369 static void write_curvemapping(WriteData *wd, CurveMapping *cumap)
373 writestruct(wd, DATA, "CurveMapping", 1, cumap);
374 for(a=0; a<CM_TOT; a++)
375 writestruct(wd, DATA, "CurveMapPoint", cumap->cm[a].totpoint, cumap->cm[a].curve);
378 /* this is only direct data, tree itself should have been written */
379 static void write_nodetree(WriteData *wd, bNodeTree *ntree)
385 /* for link_list() speed, we write per list */
387 for(node= ntree->nodes.first; node; node= node->next)
388 writestruct(wd, DATA, "bNode", 1, node);
390 for(node= ntree->nodes.first; node; node= node->next) {
392 /* could be handlerized at some point, now only 1 exception still */
393 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
394 write_curvemapping(wd, node->storage);
395 else if(ntree->type==NTREE_COMPOSIT && (node->type==CMP_NODE_CURVE_VEC || node->type==CMP_NODE_CURVE_RGB))
396 write_curvemapping(wd, node->storage);
398 writestruct(wd, DATA, node->typeinfo->storagename, 1, node->storage);
400 for(sock= node->inputs.first; sock; sock= sock->next)
401 writestruct(wd, DATA, "bNodeSocket", 1, sock);
402 for(sock= node->outputs.first; sock; sock= sock->next)
403 writestruct(wd, DATA, "bNodeSocket", 1, sock);
406 for(link= ntree->links.first; link; link= link->next)
407 writestruct(wd, DATA, "bNodeLink", 1, link);
410 static void write_scriptlink(WriteData *wd, ScriptLink *slink)
412 writedata(wd, DATA, sizeof(void *)*slink->totscript, slink->scripts);
413 writedata(wd, DATA, sizeof(short)*slink->totscript, slink->flag);
416 static void write_renderinfo(WriteData *wd) /* for renderdaemon */
421 sce= G.main->scene.first;
423 if(sce->id.lib==0 && ( sce==G.scene || (sce->r.scemode & R_BG_RENDER)) ) {
424 data[0]= sce->r.sfra;
425 data[1]= sce->r.efra;
427 strncpy((char *)(data+2), sce->id.name+2, 23);
429 writedata(wd, REND, 32, data);
435 static void write_userdef(WriteData *wd)
439 writestruct(wd, USER, "UserDef", 1, &U);
441 btheme= U.themes.first;
443 writestruct(wd, DATA, "bTheme", 1, btheme);
444 btheme= btheme->next;
448 static void write_effects(WriteData *wd, ListBase *lb)
457 writestruct(wd, DATA, "PartEff", 1, eff);
460 writedata(wd, DATA, MEM_allocN_len(eff), eff);
467 static void write_properties(WriteData *wd, ListBase *lb)
473 writestruct(wd, DATA, "bProperty", 1, prop);
475 if(prop->poin && prop->poin != &prop->data)
476 writedata(wd, DATA, MEM_allocN_len(prop->poin), prop->poin);
482 static void write_sensors(WriteData *wd, ListBase *lb)
488 writestruct(wd, DATA, "bSensor", 1, sens);
490 writedata(wd, DATA, sizeof(void *)*sens->totlinks, sens->links);
494 writestruct(wd, DATA, "bNearSensor", 1, sens->data);
497 writestruct(wd, DATA, "bMouseSensor", 1, sens->data);
500 writestruct(wd, DATA, "bTouchSensor", 1, sens->data);
503 writestruct(wd, DATA, "bKeyboardSensor", 1, sens->data);
506 writestruct(wd, DATA, "bPropertySensor", 1, sens->data);
509 writestruct(wd, DATA, "bCollisionSensor", 1, sens->data);
512 writestruct(wd, DATA, "bRadarSensor", 1, sens->data);
515 writestruct(wd, DATA, "bRandomSensor", 1, sens->data);
518 writestruct(wd, DATA, "bRaySensor", 1, sens->data);
521 writestruct(wd, DATA, "bMessageSensor", 1, sens->data);
524 writestruct(wd, DATA, "bJoystickSensor", 1, sens->data);
527 ; /* error: don't know how to write this file */
534 static void write_controllers(WriteData *wd, ListBase *lb)
540 writestruct(wd, DATA, "bController", 1, cont);
542 writedata(wd, DATA, sizeof(void *)*cont->totlinks, cont->links);
545 case CONT_EXPRESSION:
546 writestruct(wd, DATA, "bExpressionCont", 1, cont->data);
549 writestruct(wd, DATA, "bPythonCont", 1, cont->data);
552 ; /* error: don't know how to write this file */
559 static void write_actuators(WriteData *wd, ListBase *lb)
565 writestruct(wd, DATA, "bActuator", 1, act);
569 writestruct(wd, DATA, "bActionActuator", 1, act->data);
572 writestruct(wd, DATA, "bSoundActuator", 1, act->data);
575 writestruct(wd, DATA, "bCDActuator", 1, act->data);
578 writestruct(wd, DATA, "bObjectActuator", 1, act->data);
581 writestruct(wd, DATA, "bIpoActuator", 1, act->data);
584 writestruct(wd, DATA, "bPropertyActuator", 1, act->data);
587 writestruct(wd, DATA, "bCameraActuator", 1, act->data);
590 writestruct(wd, DATA, "bConstraintActuator", 1, act->data);
592 case ACT_EDIT_OBJECT:
593 writestruct(wd, DATA, "bEditObjectActuator", 1, act->data);
596 writestruct(wd, DATA, "bSceneActuator", 1, act->data);
599 writestruct(wd, DATA, "bGroupActuator", 1, act->data);
602 writestruct(wd, DATA, "bRandomActuator", 1, act->data);
605 writestruct(wd, DATA, "bMessageActuator", 1, act->data);
608 writestruct(wd, DATA, "bGameActuator", 1, act->data);
611 writestruct(wd, DATA, "bVisibilityActuator", 1, act->data);
614 ; /* error: don't know how to write this file */
621 static void write_nlastrips(WriteData *wd, ListBase *nlabase)
625 for (strip=nlabase->first; strip; strip=strip->next)
626 writestruct(wd, DATA, "bActionStrip", 1, strip);
629 static void write_constraints(WriteData *wd, ListBase *conlist)
633 for (con=conlist->first; con; con=con->next) {
634 /* Write the specific data */
636 case CONSTRAINT_TYPE_NULL:
638 case CONSTRAINT_TYPE_TRACKTO:
639 writestruct(wd, DATA, "bTrackToConstraint", 1, con->data);
641 case CONSTRAINT_TYPE_KINEMATIC:
642 writestruct(wd, DATA, "bKinematicConstraint", 1, con->data);
644 case CONSTRAINT_TYPE_ROTLIKE:
645 writestruct(wd, DATA, "bRotateLikeConstraint", 1, con->data);
647 case CONSTRAINT_TYPE_LOCLIKE:
648 writestruct(wd, DATA, "bLocateLikeConstraint", 1, con->data);
650 case CONSTRAINT_TYPE_ACTION:
651 writestruct(wd, DATA, "bActionConstraint", 1, con->data);
653 case CONSTRAINT_TYPE_LOCKTRACK:
654 writestruct(wd, DATA, "bLockTrackConstraint", 1, con->data);
656 case CONSTRAINT_TYPE_FOLLOWPATH:
657 writestruct(wd, DATA, "bFollowPathConstraint", 1, con->data);
659 case CONSTRAINT_TYPE_STRETCHTO:
660 writestruct(wd, DATA, "bStretchToConstraint", 1, con->data);
662 case CONSTRAINT_TYPE_MINMAX:
663 writestruct(wd, DATA, "bMinMaxConstraint", 1, con->data);
668 /* Write the constraint */
669 writestruct(wd, DATA, "bConstraint", 1, con);
673 static void write_pose(WriteData *wd, bPose *pose)
677 /* Write each channel */
683 for (chan=pose->chanbase.first; chan; chan=chan->next) {
684 write_constraints(wd, &chan->constraints);
685 writestruct(wd, DATA, "bPoseChannel", 1, chan);
689 writestruct(wd, DATA, "bPose", 1, pose);
692 static void write_defgroups(WriteData *wd, ListBase *defbase)
694 bDeformGroup *defgroup;
696 for (defgroup=defbase->first; defgroup; defgroup=defgroup->next)
697 writestruct(wd, DATA, "bDeformGroup", 1, defgroup);
700 static void write_constraint_channels(WriteData *wd, ListBase *chanbase)
702 bConstraintChannel *chan;
704 for (chan = chanbase->first; chan; chan=chan->next)
705 writestruct(wd, DATA, "bConstraintChannel", 1, chan);
709 static void write_modifiers(WriteData *wd, ListBase *modbase)
713 for (md=modbase->first; md; md= md->next) {
714 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
716 writestruct(wd, DATA, mti->structName, 1, md);
718 if (md->type==eModifierType_Hook) {
719 HookModifierData *hmd = (HookModifierData*) md;
721 writedata(wd, DATA, sizeof(int)*hmd->totindex, hmd->indexar);
726 static void write_objects(WriteData *wd, ListBase *idbase)
733 if(ob->id.us>0 || wd->current) {
735 writestruct(wd, ID_OB, "Object", 1, ob);
738 writedata(wd, DATA, sizeof(void *)*ob->totcol, ob->mat);
739 write_effects(wd, &ob->effect);
740 write_properties(wd, &ob->prop);
741 write_sensors(wd, &ob->sensors);
742 write_controllers(wd, &ob->controllers);
743 write_actuators(wd, &ob->actuators);
744 write_scriptlink(wd, &ob->scriptlink);
745 write_pose(wd, ob->pose);
746 write_defgroups(wd, &ob->defbase);
747 write_constraints(wd, &ob->constraints);
748 write_constraint_channels(wd, &ob->constraintChannels);
749 write_nlastrips(wd, &ob->nlastrips);
751 writestruct(wd, DATA, "PartDeflect", 1, ob->pd);
752 writestruct(wd, DATA, "SoftBody", 1, ob->soft);
754 SoftBody *sb= ob->soft;
756 writedata(wd, DATA, sizeof(void *)*sb->totkey, sb->keys);
757 for(a=0; a<sb->totkey; a++) {
758 writestruct(wd, DATA, "SBVertex", sb->totpoint, sb->keys[a]);
762 writestruct(wd, DATA, "FluidsimSettings", 1, ob->fluidsimSettings); // NT
764 write_modifiers(wd, &ob->modifiers);
769 /* flush helps the compression for undo-save */
770 mywrite(wd, MYWRITE_FLUSH, 0);
774 static void write_vfonts(WriteData *wd, ListBase *idbase)
781 if(vf->id.us>0 || wd->current) {
783 writestruct(wd, ID_VF, "VFont", 1, vf);
787 if (vf->packedfile) {
789 writestruct(wd, DATA, "PackedFile", 1, pf);
790 writedata(wd, DATA, pf->size, pf->data);
798 static void write_ipos(WriteData *wd, ListBase *idbase)
805 if(ipo->id.us>0 || wd->current) {
807 writestruct(wd, ID_IP, "Ipo", 1, ipo);
810 icu= ipo->curve.first;
812 writestruct(wd, DATA, "IpoCurve", 1, icu);
816 icu= ipo->curve.first;
818 if(icu->bezt) writestruct(wd, DATA, "BezTriple", icu->totvert, icu->bezt);
819 if(icu->bp) writestruct(wd, DATA, "BPoint", icu->totvert, icu->bp);
820 if(icu->driver) writestruct(wd, DATA, "IpoDriver", 1, icu->driver);
828 /* flush helps the compression for undo-save */
829 mywrite(wd, MYWRITE_FLUSH, 0);
832 static void write_keys(WriteData *wd, ListBase *idbase)
839 if(key->id.us>0 || wd->current) {
841 writestruct(wd, ID_KE, "Key", 1, key);
844 kb= key->block.first;
846 writestruct(wd, DATA, "KeyBlock", 1, kb);
847 if(kb->data) writedata(wd, DATA, kb->totelem*key->elemsize, kb->data);
854 /* flush helps the compression for undo-save */
855 mywrite(wd, MYWRITE_FLUSH, 0);
858 static void write_cameras(WriteData *wd, ListBase *idbase)
864 if(cam->id.us>0 || wd->current) {
866 writestruct(wd, ID_CA, "Camera", 1, cam);
869 write_scriptlink(wd, &cam->scriptlink);
876 static void write_mballs(WriteData *wd, ListBase *idbase)
883 if(mb->id.us>0 || wd->current) {
885 writestruct(wd, ID_MB, "MetaBall", 1, mb);
888 writedata(wd, DATA, sizeof(void *)*mb->totcol, mb->mat);
892 writestruct(wd, DATA, "MetaElem", 1, ml);
900 int amount_of_chars(char *str)
902 // Since the data is saved as UTF-8 to the cu->str
903 // The cu->len is not same as the strlen(cu->str)
907 static void write_curves(WriteData *wd, ListBase *idbase)
914 if(cu->id.us>0 || wd->current) {
916 writestruct(wd, ID_CU, "Curve", 1, cu);
919 writedata(wd, DATA, sizeof(void *)*cu->totcol, cu->mat);
922 writedata(wd, DATA, amount_of_chars(cu->str)+1, cu->str);
923 writestruct(wd, DATA, "CharInfo", cu->len, cu->strinfo);
924 writestruct(wd, DATA, "TextBox", cu->totbox, cu->tb);
927 /* is also the order of reading */
930 writestruct(wd, DATA, "Nurb", 1, nu);
935 if( (nu->type & 7)==CU_BEZIER)
936 writestruct(wd, DATA, "BezTriple", nu->pntsu, nu->bezt);
938 writestruct(wd, DATA, "BPoint", nu->pntsu*nu->pntsv, nu->bp);
939 if(nu->knotsu) writedata(wd, DATA, KNOTSU(nu)*sizeof(float), nu->knotsu);
940 if(nu->knotsv) writedata(wd, DATA, KNOTSV(nu)*sizeof(float), nu->knotsv);
949 /* flush helps the compression for undo-save */
950 mywrite(wd, MYWRITE_FLUSH, 0);
953 static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
957 /* Write the dvert list */
958 writestruct(wd, DATA, "MDeformVert", count, dvlist);
960 /* Write deformation data for each dvert */
962 for (i=0; i<count; i++) {
964 writestruct(wd, DATA, "MDeformWeight", dvlist[i].totweight, dvlist[i].dw);
969 static void write_meshs(WriteData *wd, ListBase *idbase)
975 if(mesh->id.us>0 || wd->current) {
977 writestruct(wd, ID_ME, "Mesh", 1, mesh);
980 writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
982 writestruct(wd, DATA, "MVert", mesh->totvert, mesh->mvert);
983 writestruct(wd, DATA, "MEdge", mesh->totedge, mesh->medge);
984 writestruct(wd, DATA, "MFace", mesh->totface, mesh->mface);
985 writestruct(wd, DATA, "TFace", mesh->totface, mesh->tface);
986 writestruct(wd, DATA, "MCol", 4*mesh->totface, mesh->mcol);
987 writestruct(wd, DATA, "MSticky", mesh->totvert, mesh->msticky);
989 write_dverts(wd, mesh->totvert, mesh->dvert);
996 static void write_images(WriteData *wd, ListBase *idbase)
1003 if(ima->id.us>0 || wd->current) {
1005 writestruct(wd, ID_IM, "Image", 1, ima);
1007 if (ima->packedfile) {
1008 pf = ima->packedfile;
1009 writestruct(wd, DATA, "PackedFile", 1, pf);
1010 writedata(wd, DATA, pf->size, pf->data);
1015 /* flush helps the compression for undo-save */
1016 mywrite(wd, MYWRITE_FLUSH, 0);
1019 static void write_textures(WriteData *wd, ListBase *idbase)
1025 if(tex->id.us>0 || wd->current) {
1027 writestruct(wd, ID_TE, "Tex", 1, tex);
1030 if(tex->plugin) writestruct(wd, DATA, "PluginTex", 1, tex->plugin);
1031 if(tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
1032 if(tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
1037 /* flush helps the compression for undo-save */
1038 mywrite(wd, MYWRITE_FLUSH, 0);
1041 static void write_materials(WriteData *wd, ListBase *idbase)
1048 if(ma->id.us>0 || wd->current) {
1050 writestruct(wd, ID_MA, "Material", 1, ma);
1052 for(a=0; a<MAX_MTEX; a++) {
1053 if(ma->mtex[a]) writestruct(wd, DATA, "MTex", 1, ma->mtex[a]);
1056 if(ma->ramp_col) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_col);
1057 if(ma->ramp_spec) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_spec);
1059 write_scriptlink(wd, &ma->scriptlink);
1061 /* nodetree is integral part of material, no libdata */
1063 writestruct(wd, DATA, "bNodeTree", 1, ma->nodetree);
1064 write_nodetree(wd, ma->nodetree);
1071 static void write_worlds(WriteData *wd, ListBase *idbase)
1076 wrld= idbase->first;
1078 if(wrld->id.us>0 || wd->current) {
1080 writestruct(wd, ID_WO, "World", 1, wrld);
1082 for(a=0; a<MAX_MTEX; a++) {
1083 if(wrld->mtex[a]) writestruct(wd, DATA, "MTex", 1, wrld->mtex[a]);
1086 write_scriptlink(wd, &wrld->scriptlink);
1088 wrld= wrld->id.next;
1092 static void write_lamps(WriteData *wd, ListBase *idbase)
1099 if(la->id.us>0 || wd->current) {
1101 writestruct(wd, ID_LA, "Lamp", 1, la);
1104 for(a=0; a<MAX_MTEX; a++) {
1105 if(la->mtex[a]) writestruct(wd, DATA, "MTex", 1, la->mtex[a]);
1108 write_scriptlink(wd, &la->scriptlink);
1114 static void write_lattices(WriteData *wd, ListBase *idbase)
1120 if(lt->id.us>0 || wd->current) {
1122 writestruct(wd, ID_LT, "Lattice", 1, lt);
1125 writestruct(wd, DATA, "BPoint", lt->pntsu*lt->pntsv*lt->pntsw, lt->def);
1131 static void write_scenes(WriteData *wd, ListBase *scebase)
1141 sce= scebase->first;
1144 writestruct(wd, ID_SCE, "Scene", 1, sce);
1147 base= sce->base.first;
1149 writestruct(wd, DATA, "Base", 1, base);
1153 writestruct(wd, DATA, "Radio", 1, sce->radio);
1154 writestruct(wd, DATA, "ToolSettings", 1, sce->toolsettings);
1158 writestruct(wd, DATA, "Editing", 1, ed);
1160 /* reset write flags too */
1161 WHILE_SEQ(&ed->seqbase) {
1162 if(seq->strip) seq->strip->done= 0;
1163 writestruct(wd, DATA, "Sequence", 1, seq);
1167 WHILE_SEQ(&ed->seqbase) {
1168 if(seq->strip && seq->strip->done==0) {
1169 /* write strip with 'done' at 0 because readfile */
1171 if(seq->plugin) writestruct(wd, DATA, "PluginSeq", 1, seq->plugin);
1172 if(seq->effectdata) {
1175 writestruct(wd, DATA, "WipeVars", 1, seq->effectdata);
1178 writestruct(wd, DATA, "GlowVars", 1, seq->effectdata);
1184 writestruct(wd, DATA, "Strip", 1, strip);
1186 if(seq->type==SEQ_IMAGE)
1187 writestruct(wd, DATA, "StripElem", strip->len, strip->stripdata);
1188 else if(seq->type==SEQ_MOVIE || seq->type==SEQ_SOUND)
1189 writestruct(wd, DATA, "StripElem", 1, strip->stripdata);
1196 /* new; meta stack too, even when its nasty restore code */
1197 for(ms= ed->metastack.first; ms; ms= ms->next) {
1198 writestruct(wd, DATA, "MetaStack", 1, ms);
1202 write_scriptlink(wd, &sce->scriptlink);
1204 if (sce->r.avicodecdata) {
1205 writestruct(wd, DATA, "AviCodecData", 1, sce->r.avicodecdata);
1206 if (sce->r.avicodecdata->lpFormat) writedata(wd, DATA, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat);
1207 if (sce->r.avicodecdata->lpParms) writedata(wd, DATA, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms);
1210 if (sce->r.qtcodecdata) {
1211 writestruct(wd, DATA, "QuicktimeCodecData", 1, sce->r.qtcodecdata);
1212 if (sce->r.qtcodecdata->cdParms) writedata(wd, DATA, sce->r.qtcodecdata->cdSize, sce->r.qtcodecdata->cdParms);
1215 /* writing dynamic list of TimeMarkers to the blend file */
1216 marker= sce->markers.first;
1218 writestruct(wd, DATA, "TimeMarker", 1, marker);
1219 marker= marker->next;
1223 writestruct(wd, DATA, "bNodeTree", 1, sce->nodetree);
1224 write_nodetree(wd, sce->nodetree);
1229 /* flush helps the compression for undo-save */
1230 mywrite(wd, MYWRITE_FLUSH, 0);
1233 static void write_screens(WriteData *wd, ListBase *scrbase)
1243 writestruct(wd, ID_SCR, "Screen", 1, sc);
1246 sv= sc->vertbase.first;
1248 writestruct(wd, DATA, "ScrVert", 1, sv);
1252 se= sc->edgebase.first;
1254 writestruct(wd, DATA, "ScrEdge", 1, se);
1258 sa= sc->areabase.first;
1263 writestruct(wd, DATA, "ScrArea", 1, sa);
1265 pa= sa->panels.first;
1267 writestruct(wd, DATA, "Panel", 1, pa);
1271 /* space handler scriptlinks */
1272 write_scriptlink(wd, &sa->scriptlink);
1274 sl= sa->spacedata.first;
1276 if(sl->spacetype==SPACE_VIEW3D) {
1277 View3D *v3d= (View3D*) sl;
1278 writestruct(wd, DATA, "View3D", 1, v3d);
1279 if(v3d->bgpic) writestruct(wd, DATA, "BGpic", 1, v3d->bgpic);
1280 if(v3d->localvd) writestruct(wd, DATA, "View3D", 1, v3d->localvd);
1281 if(v3d->clipbb) writestruct(wd, DATA, "BoundBox", 1, v3d->clipbb);
1283 else if(sl->spacetype==SPACE_IPO) {
1284 writestruct(wd, DATA, "SpaceIpo", 1, sl);
1286 else if(sl->spacetype==SPACE_BUTS) {
1287 writestruct(wd, DATA, "SpaceButs", 1, sl);
1289 else if(sl->spacetype==SPACE_FILE) {
1290 writestruct(wd, DATA, "SpaceFile", 1, sl);
1292 else if(sl->spacetype==SPACE_SEQ) {
1293 writestruct(wd, DATA, "SpaceSeq", 1, sl);
1295 else if(sl->spacetype==SPACE_OOPS) {
1296 SpaceOops *so= (SpaceOops *)sl;
1300 oops= so->oops.first;
1302 Oops *oopsn= oops->next;
1304 BLI_remlink(&so->oops, oops);
1310 /* ater cleanup, because of listbase! */
1311 writestruct(wd, DATA, "SpaceOops", 1, so);
1313 oops= so->oops.first;
1315 writestruct(wd, DATA, "Oops", 1, oops);
1320 writestruct(wd, DATA, "TreeStore", 1, so->treestore);
1321 if(so->treestore->data)
1322 writestruct(wd, DATA, "TreeStoreElem", so->treestore->usedelem, so->treestore->data);
1325 else if(sl->spacetype==SPACE_IMAGE) {
1326 SpaceImage *sima= (SpaceImage *)sl;
1328 writestruct(wd, DATA, "SpaceImage", 1, sl);
1330 write_curvemapping(wd, sima->cumap);
1332 else if(sl->spacetype==SPACE_IMASEL) {
1333 writestruct(wd, DATA, "SpaceImaSel", 1, sl);
1335 else if(sl->spacetype==SPACE_TEXT) {
1336 writestruct(wd, DATA, "SpaceText", 1, sl);
1338 else if(sl->spacetype==SPACE_SCRIPT) {
1339 writestruct(wd, DATA, "SpaceScript", 1, sl);
1341 else if(sl->spacetype==SPACE_ACTION) {
1342 writestruct(wd, DATA, "SpaceAction", 1, sl);
1344 else if(sl->spacetype==SPACE_SOUND) {
1345 writestruct(wd, DATA, "SpaceSound", 1, sl);
1347 else if(sl->spacetype==SPACE_NLA){
1348 writestruct(wd, DATA, "SpaceNla", 1, sl);
1350 else if(sl->spacetype==SPACE_TIME){
1351 writestruct(wd, DATA, "SpaceTime", 1, sl);
1353 else if(sl->spacetype==SPACE_NODE){
1354 writestruct(wd, DATA, "SpaceNode", 1, sl);
1366 static void write_libraries(WriteData *wd, Main *main)
1368 ListBase *lbarray[30];
1370 int a, tot, foundone;
1374 a=tot= set_listbasepointers(main, lbarray);
1376 /* test: is lib being used */
1379 id= lbarray[tot]->first;
1381 if(id->us>0 && (id->flag & LIB_EXTERN)) {
1391 writestruct(wd, ID_LI, "Library", 1, main->curlib);
1394 id= lbarray[a]->first;
1396 if(G.rt==127 && GS(id->name)!=ID_GR) break;
1398 if(id->us>0 && (id->flag & LIB_EXTERN)) {
1400 writestruct(wd, ID_ID, "ID", 1, id);
1411 static void write_bone(WriteData *wd, Bone* bone)
1415 // PATCH for upward compatibility after 2.37+ armature recode
1416 bone->size[0]= bone->size[1]= bone->size[2]= 1.0f;
1419 writestruct(wd, DATA, "Bone", 1, bone);
1422 cbone= bone->childbase.first;
1424 write_bone(wd, cbone);
1429 static void write_armatures(WriteData *wd, ListBase *idbase)
1436 if (arm->id.us>0 || wd->current) {
1437 writestruct(wd, ID_AR, "bArmature", 1, arm);
1440 bone= arm->bonebase.first;
1442 write_bone(wd, bone);
1449 /* flush helps the compression for undo-save */
1450 mywrite(wd, MYWRITE_FLUSH, 0);
1453 static void write_actions(WriteData *wd, ListBase *idbase)
1456 bActionChannel *chan;
1458 for(act=idbase->first; act; act= act->id.next) {
1459 if (act->id.us>0 || wd->current) {
1460 writestruct(wd, ID_AC, "bAction", 1, act);
1462 for (chan=act->chanbase.first; chan; chan=chan->next) {
1463 writestruct(wd, DATA, "bActionChannel", 1, chan);
1464 write_constraint_channels(wd, &chan->constraintChannels);
1470 static void write_texts(WriteData *wd, ListBase *idbase)
1475 text= idbase->first;
1477 if ( (text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) text->flags &= ~TXT_ISEXT;
1480 writestruct(wd, ID_TXT, "Text", 1, text);
1481 if(text->name) writedata(wd, DATA, strlen(text->name)+1, text->name);
1483 if(!(text->flags & TXT_ISEXT)) {
1484 /* now write the text data, in two steps for optimization in the readfunction */
1485 tmp= text->lines.first;
1487 writestruct(wd, DATA, "TextLine", 1, tmp);
1491 tmp= text->lines.first;
1493 writedata(wd, DATA, tmp->len+1, tmp->line);
1497 text= text->id.next;
1500 /* flush helps the compression for undo-save */
1501 mywrite(wd, MYWRITE_FLUSH, 0);
1504 static void write_sounds(WriteData *wd, ListBase *idbase)
1511 // set all samples to unsaved status
1513 sample = samples->first;
1515 sample->flags |= SAMPLE_NEEDS_SAVE;
1516 sample = sample->id.next;
1519 sound= idbase->first;
1521 if(sound->id.us>0 || wd->current) {
1522 // do we need to save the packedfile as well ?
1523 sample = sound->sample;
1525 if (sample->flags & SAMPLE_NEEDS_SAVE) {
1526 sound->newpackedfile = sample->packedfile;
1527 sample->flags &= ~SAMPLE_NEEDS_SAVE;
1529 sound->newpackedfile = NULL;
1534 writestruct(wd, ID_SO, "bSound", 1, sound);
1536 if (sound->newpackedfile) {
1537 pf = sound->newpackedfile;
1538 writestruct(wd, DATA, "PackedFile", 1, pf);
1539 writedata(wd, DATA, pf->size, pf->data);
1543 sound->newpackedfile = sample->packedfile;
1546 sound= sound->id.next;
1549 /* flush helps the compression for undo-save */
1550 mywrite(wd, MYWRITE_FLUSH, 0);
1553 static void write_groups(WriteData *wd, ListBase *idbase)
1558 for(group= idbase->first; group; group= group->id.next) {
1559 if(group->id.us>0 || wd->current) {
1561 writestruct(wd, ID_GR, "Group", 1, group);
1563 go= group->gobject.first;
1565 writestruct(wd, DATA, "GroupObject", 1, go);
1572 static void write_nodetrees(WriteData *wd, ListBase *idbase)
1576 for(ntree=idbase->first; ntree; ntree= ntree->id.next) {
1577 if (ntree->id.us>0 || wd->current) {
1578 writestruct(wd, ID_NT, "bNodeTree", 1, ntree);
1579 write_nodetree(wd, ntree);
1584 static void write_global(WriteData *wd)
1588 fg.curscreen= G.curscreen;
1589 fg.curscene= G.scene;
1590 fg.displaymode= G.displaymode;
1591 fg.winpos= G.winpos;
1592 fg.fileflags= (G.fileflags & ~G_FILE_NO_UI); // prevent to save this, is not good convention, and feature with concerns...
1595 writestruct(wd, GLOB, "FileGlobal", 1, &fg);
1598 /* if *mem there's filesave to memory */
1599 static int write_file_handle(int handle, MemFile *compare, MemFile *current, int write_user_block, int write_flags)
1605 /* int data; */ /*unused*/
1607 mainlist.first= mainlist.last= G.main;
1610 blo_split_main(&mainlist);
1612 wd= bgnwrite(handle, compare, current, write_flags);
1614 sprintf(buf, "BLENDER%c%c%.3d", (sizeof(void*)==8)?'-':'_', (G.order==B_ENDIAN)?'V':'v', G.version);
1615 mywrite(wd, buf, 12);
1617 write_renderinfo(wd);
1620 write_screens (wd, &G.main->screen); // no UI save
1621 write_scenes (wd, &G.main->scene);
1622 write_curves (wd, &G.main->curve);
1623 write_mballs (wd, &G.main->mball);
1624 write_images (wd, &G.main->image);
1625 write_cameras (wd, &G.main->camera);
1626 write_lamps (wd, &G.main->lamp);
1627 write_lattices (wd, &G.main->latt);
1628 write_vfonts (wd, &G.main->vfont);
1629 write_ipos (wd, &G.main->ipo);
1630 write_keys (wd, &G.main->key);
1631 write_worlds (wd, &G.main->world);
1632 write_texts (wd, &G.main->text);
1633 write_sounds (wd, &G.main->sound);
1634 write_groups (wd, &G.main->group);
1635 write_armatures(wd, &G.main->armature);
1636 write_actions (wd, &G.main->action);
1637 write_objects (wd, &G.main->object);
1638 write_materials(wd, &G.main->mat);
1639 write_textures (wd, &G.main->tex);
1640 write_meshs (wd, &G.main->mesh);
1641 write_nodetrees(wd, &G.main->nodetree);
1642 write_libraries(wd, G.main->next);
1645 if (write_user_block) {
1649 /* dna as last, because (to be implemented) test for which structs are written */
1650 writedata(wd, DNA1, wd->sdna->datalen, wd->sdna->data);
1653 memset(&bhead, 0, sizeof(BHead));
1655 mywrite(wd, &bhead, sizeof(BHead));
1657 blo_join_main(&mainlist);
1658 G.main= mainlist.first;
1660 return endwrite(wd);
1663 /* return: success (1) */
1664 int BLO_write_file(char *dir, int write_flags, char **error_r)
1666 char userfilename[FILE_MAXDIR+FILE_MAXFILE];
1667 char tempname[FILE_MAXDIR+FILE_MAXFILE];
1668 int file, fout, write_user_block;
1670 sprintf(tempname, "%s@", dir);
1672 file = open(tempname,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
1674 *error_r= "Unable to open";
1678 BLI_make_file_string(G.sce, userfilename, BLI_gethome(), ".B.blend");
1680 write_user_block= BLI_streq(dir, userfilename);
1682 fout= write_file_handle(file, NULL,NULL, write_user_block, write_flags);
1686 if(write_flags & G_FILE_COMPRESS)
1688 // compressed files have the same ending as regular files... only from 2.4!!!
1690 int ret = BLI_gzip(tempname, dir);
1693 *error_r= "Failed opening .gz file";
1697 *error_r= "Failed opening .blend file for compression";
1702 if(BLI_rename(tempname, dir) < 0) {
1703 *error_r= "Can't change old file. File saved with @";
1711 *error_r= "Not enough diskspace";
1718 /* return: success (1) */
1719 int BLO_write_file_mem(MemFile *compare, MemFile *current, int write_flags, char **error_r)
1723 err= write_file_handle(0, compare, current, 0, write_flags);
1725 if(err==0) return 1;
1730 /* Runtime writing */
1733 #define PATHSEPERATOR "\\"
1735 #define PATHSEPERATOR "/"
1738 static char *get_install_dir(void) {
1739 extern char bprogname[];
1740 char *tmpname = BLI_strdup(bprogname);
1744 cut = strstr(tmpname, ".app");
1745 if (cut) cut[0] = 0;
1748 cut = BLI_last_slash(tmpname);
1759 static char *get_runtime_path(char *exename) {
1760 char *installpath= get_install_dir();
1765 char *path= MEM_mallocN(strlen(installpath)+strlen(PATHSEPERATOR)+strlen(exename)+1, "runtimepath");
1766 strcpy(path, installpath);
1767 strcat(path, PATHSEPERATOR);
1768 strcat(path, exename);
1770 MEM_freeN(installpath);
1778 static int recursive_copy_runtime(char *outname, char *exename, char **cause_r)
1780 char *cause = NULL, *runtime = get_runtime_path(exename);
1781 char command[2 * (FILE_MAXDIR+FILE_MAXFILE) + 32];
1785 cause= "Unable to find runtime";
1788 //printf("runtimepath %s\n", runtime);
1790 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
1792 cause= "Unable to find runtime";
1796 sprintf(command, "/bin/cp -R \"%s\" \"%s\"", runtime, outname);
1797 //printf("command %s\n", command);
1798 if (system(command) == -1) {
1799 cause = "Couldn't copy runtime";
1815 void BLO_write_runtime(char *file, char *exename) {
1816 char gamename[FILE_MAXDIR+FILE_MAXFILE];
1820 // remove existing file / bundle
1821 //printf("Delete file %s\n", file);
1822 BLI_delete(file, NULL, TRUE);
1824 if (!recursive_copy_runtime(file, exename, &cause))
1827 strcpy(gamename, file);
1828 strcat(gamename, "/Contents/Resources/game.blend");
1829 //printf("gamename %s\n", gamename);
1830 outfd= open(gamename, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
1833 write_file_handle(outfd, NULL,NULL, 0, G.fileflags);
1835 if (write(outfd, " ", 1) != 1) {
1836 cause= "Unable to write to output file";
1840 cause = "Unable to open blenderfile";
1848 error("Unable to make runtime: %s", cause);
1851 #else /* !__APPLE__ */
1853 static int handle_append_runtime(int handle, char *exename, char **cause_r) {
1854 char *cause= NULL, *runtime= get_runtime_path(exename);
1855 unsigned char buf[1024];
1856 int count, progfd= -1;
1859 cause= "Unable to find runtime";
1863 progfd= open(runtime, O_BINARY|O_RDONLY, 0);
1865 cause= "Unable to find runtime";
1869 while ((count= read(progfd, buf, sizeof(buf)))>0) {
1870 if (write(handle, buf, count)!=count) {
1871 cause= "Unable to write to output file";
1889 static int handle_write_msb_int(int handle, int i) {
1890 unsigned char buf[4];
1891 buf[0]= (i>>24)&0xFF;
1892 buf[1]= (i>>16)&0xFF;
1893 buf[2]= (i>>8)&0xFF;
1894 buf[3]= (i>>0)&0xFF;
1896 return (write(handle, buf, 4)==4);
1899 void BLO_write_runtime(char *file, char *exename) {
1900 int outfd= open(file, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0777);
1905 cause= "Unable to open output file";
1908 if (!handle_append_runtime(outfd, exename, &cause))
1911 datastart= lseek(outfd, 0, SEEK_CUR);
1913 write_file_handle(outfd, NULL,NULL, 0, G.fileflags);
1915 if (!handle_write_msb_int(outfd, datastart) || (write(outfd, "BRUNTIME", 8)!=8)) {
1916 cause= "Unable to write to output file";
1925 error("Unable to make runtime: %s", cause);
1928 #endif /* !__APPLE__ */