4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
30 /** \file blender/blenloader/intern/readfile.c
38 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
39 #include <stdlib.h> // for getenv atoi
40 #include <stddef.h> // for offsetof
41 #include <fcntl.h> // for open
42 #include <string.h> // for strrchr strncmp strstr
43 #include <math.h> // for fabs
46 #include <unistd.h> // for read close
48 #include <io.h> // for open close read
50 #include "BLI_winstuff.h"
53 #include "DNA_anim_types.h"
54 #include "DNA_armature_types.h"
55 #include "DNA_actuator_types.h"
56 #include "DNA_brush_types.h"
57 #include "DNA_camera_types.h"
58 #include "DNA_cloth_types.h"
59 #include "DNA_controller_types.h"
60 #include "DNA_constraint_types.h"
61 #include "DNA_effect_types.h"
62 #include "DNA_fileglobal_types.h"
63 #include "DNA_genfile.h"
64 #include "DNA_group_types.h"
65 #include "DNA_gpencil_types.h"
66 #include "DNA_ipo_types.h"
67 #include "DNA_key_types.h"
68 #include "DNA_lattice_types.h"
69 #include "DNA_lamp_types.h"
70 #include "DNA_meta_types.h"
71 #include "DNA_material_types.h"
72 #include "DNA_mesh_types.h"
73 #include "DNA_meshdata_types.h"
74 #include "DNA_nla_types.h"
75 #include "DNA_node_types.h"
76 #include "DNA_object_fluidsim.h" // NT
77 #include "DNA_packedFile_types.h"
78 #include "DNA_particle_types.h"
79 #include "DNA_property_types.h"
80 #include "DNA_text_types.h"
81 #include "DNA_view3d_types.h"
82 #include "DNA_screen_types.h"
83 #include "DNA_sensor_types.h"
84 #include "DNA_sdna_types.h"
85 #include "DNA_scene_types.h"
86 #include "DNA_sequence_types.h"
87 #include "DNA_smoke_types.h"
88 #include "DNA_speaker_types.h"
89 #include "DNA_sound_types.h"
90 #include "DNA_space_types.h"
91 #include "DNA_vfont_types.h"
92 #include "DNA_world_types.h"
93 #include "DNA_movieclip_types.h"
95 #include "MEM_guardedalloc.h"
97 #include "BLI_blenlib.h"
99 #include "BLI_utildefines.h"
101 #include "BKE_anim.h"
102 #include "BKE_action.h"
103 #include "BKE_armature.h"
104 #include "BKE_brush.h"
105 #include "BKE_colortools.h"
106 #include "BKE_constraint.h"
107 #include "BKE_context.h"
108 #include "BKE_curve.h"
109 #include "BKE_deform.h"
110 #include "BKE_effect.h" /* give_parteff */
111 #include "BKE_fcurve.h"
112 #include "BKE_global.h" // for G
113 #include "BKE_group.h"
114 #include "BKE_image.h"
115 #include "BKE_lattice.h"
116 #include "BKE_library.h" // for which_libbase
117 #include "BKE_idcode.h"
118 #include "BKE_material.h"
119 #include "BKE_main.h" // for Main
120 #include "BKE_mesh.h" // for ME_ defines (patching)
121 #include "BKE_modifier.h"
122 #include "BKE_multires.h"
123 #include "BKE_node.h" // for tree type defines
124 #include "BKE_object.h"
125 #include "BKE_paint.h"
126 #include "BKE_particle.h"
127 #include "BKE_pointcache.h"
128 #include "BKE_property.h" // for get_ob_property
129 #include "BKE_report.h"
130 #include "BKE_sca.h" // for init_actuator
131 #include "BKE_scene.h"
132 #include "BKE_screen.h"
133 #include "BKE_sequencer.h"
134 #include "BKE_texture.h" // for open_plugin_tex
135 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
136 #include "BKE_sound.h"
138 #include "NOD_socket.h"
140 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
141 //XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
142 //XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
143 #include "BLO_readfile.h"
144 #include "BLO_undofile.h"
146 #include "readfile.h"
148 #include "PIL_time.h"
153 Remark: still a weak point is the newaddress() function, that doesnt solve reading from
154 multiple files at the same time
156 (added remark: oh, i thought that was solved? will look at that... (ton)
159 - Existing Library (Main) push or free
169 - read associated 'direct data'
170 - link direct data (internal and to LibBlock)
172 - read USER data, only when indicated (file is ~/X.XX/startup.blend)
174 - per Library (per Main)
177 - find LibBlocks and attach IDs to Main
178 - if external LibBlock
180 - or it's already read,
185 - read associated direct data
186 - link direct data (internal and to LibBlock)
188 - per Library with unread LibBlocks
193 - read associated direct data
194 - link direct data (internal and to LibBlock)
197 - link all LibBlocks and indirect pointers to libblocks
198 - initialize FileGlobal and copy pointers to Global
201 /* also occurs in library.c */
202 /* GS reads the memory pointed at in a specific ordering. There are,
203 * however two definitions for it. I have jotted them down here, both,
204 * but I think the first one is actually used. The thing is that
205 * big-endian systems might read this the wrong way round. OTOH, we
206 * constructed the IDs that are read out with this macro explicitly as
207 * well. I expect we'll sort it out soon... */
210 #define GS(a) (*((short *)(a)))
212 /* from misc_util: flip the bytes from x */
213 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
215 // only used here in readfile.c
216 #define SWITCH_LONGINT(a) { \
219 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
220 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
221 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
222 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
226 typedef struct OldNew {
231 typedef struct OldNewMap {
233 int nentries, entriessize;
239 /* local prototypes */
240 static void *read_struct(FileData *fd, BHead *bh, const char *blockname);
241 static void direct_link_modifiers(FileData *fd, ListBase *lb);
242 static void convert_tface_mt(FileData *fd, Main *main);
244 static OldNewMap *oldnewmap_new(void)
246 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
248 onm->entriessize= 1024;
249 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
254 static int verg_oldnewmap(const void *v1, const void *v2)
256 const struct OldNew *x1=v1, *x2=v2;
258 if( x1->old > x2->old) return 1;
259 else if( x1->old < x2->old) return -1;
264 static void oldnewmap_sort(FileData *fd)
266 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
267 fd->libmap->sorted= 1;
270 /* nr is zero for data, and ID code for libdata */
271 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
275 if(oldaddr==NULL || newaddr==NULL) return;
277 if (onm->nentries==onm->entriessize) {
278 int osize= onm->entriessize;
279 OldNew *oentries= onm->entries;
281 onm->entriessize*= 2;
282 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
284 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
288 entry= &onm->entries[onm->nentries++];
290 entry->newp= newaddr;
294 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
298 if(addr==NULL) return NULL;
300 if (onm->lasthit<onm->nentries-1) {
301 OldNew *entry= &onm->entries[++onm->lasthit];
303 if (entry->old==addr) {
309 for (i=0; i<onm->nentries; i++) {
310 OldNew *entry= &onm->entries[i];
312 if (entry->old==addr) {
323 /* for libdata, nr has ID code, no increment */
324 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
328 if(addr==NULL) return NULL;
330 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
332 OldNew entry_s, *entry;
336 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
340 if (id && (!lib || id->lib)) {
346 for (i=0; i<onm->nentries; i++) {
347 OldNew *entry= &onm->entries[i];
349 if (entry->old==addr) {
352 if (id && (!lib || id->lib)) {
361 static void oldnewmap_free_unused(OldNewMap *onm)
365 for (i=0; i<onm->nentries; i++) {
366 OldNew *entry= &onm->entries[i];
368 MEM_freeN(entry->newp);
374 static void oldnewmap_clear(OldNewMap *onm)
380 static void oldnewmap_free(OldNewMap *onm)
382 MEM_freeN(onm->entries);
388 static void read_libraries(FileData *basefd, ListBase *mainlist);
390 /* ************ help functions ***************** */
392 static void add_main_to_main(Main *mainvar, Main *from)
394 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
397 set_listbasepointers(mainvar, lbarray);
398 a= set_listbasepointers(from, fromarray);
400 BLI_movelisttolist(lbarray[a], fromarray[a]);
404 void blo_join_main(ListBase *mainlist)
406 Main *tojoin, *mainl;
409 mainl= mainlist->first;
410 while ((tojoin= mainl->next)) {
411 add_main_to_main(mainl, tojoin);
412 BLI_remlink(mainlist, tojoin);
417 static void split_libdata(ListBase *lb, Main *first)
429 if(mainvar->curlib==id->lib) {
430 lbn= which_libbase(mainvar, GS(id->name));
432 BLI_addtail(lbn, id);
435 mainvar= mainvar->next;
437 if(mainvar==NULL) printf("error split_libdata\n");
443 void blo_split_main(ListBase *mainlist, Main *main)
445 ListBase *lbarray[MAX_LIBARRAY];
449 mainlist->first= mainlist->last= main;
452 if(main->library.first==NULL)
455 for (lib= main->library.first; lib; lib= lib->id.next) {
456 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
457 libmain->curlib= lib;
458 BLI_addtail(mainlist, libmain);
461 i= set_listbasepointers(main, lbarray);
463 split_libdata(lbarray[i], main->next);
466 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
467 static void cleanup_path(const char *relabase, char *name)
469 char filename[FILE_MAXFILE];
471 BLI_splitdirstring(name, filename);
472 BLI_cleanup_dir(relabase, name);
473 strcat(name, filename);
476 static void read_file_version(FileData *fd, Main *main)
480 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
481 if (bhead->code==GLOB) {
482 FileGlobal *fg= read_struct(fd, bhead, "Global");
484 main->subversionfile= fg->subversion;
485 main->minversionfile= fg->minversion;
486 main->minsubversionfile= fg->minsubversion;
489 else if (bhead->code==ENDB)
496 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *filepath, const char *relabase)
500 char name1[FILE_MAXDIR+FILE_MAXFILE];
502 BLI_strncpy(name1, filepath, sizeof(name1));
503 cleanup_path(relabase, name1);
504 // printf("blo_find_main: original in %s\n", name);
505 // printf("blo_find_main: converted to %s\n", name1);
507 for (m= mainlist->first; m; m= m->next) {
508 char *libname= (m->curlib)?m->curlib->filepath:m->name;
510 if (BLI_path_cmp(name1, libname) == 0) {
511 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
516 m= MEM_callocN(sizeof(Main), "find_main");
517 BLI_addtail(mainlist, m);
519 lib= alloc_libblock(&m->library, ID_LI, "lib");
520 BLI_strncpy(lib->name, filepath, sizeof(lib->name));
521 BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
525 read_file_version(fd, m);
527 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", filepath);
532 /* ************ FILE PARSING ****************** */
534 static void switch_endian_bh4(BHead4 *bhead)
536 /* the ID_.. codes */
537 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
539 if (bhead->code != ENDB) {
540 SWITCH_INT(bhead->len);
541 SWITCH_INT(bhead->SDNAnr);
542 SWITCH_INT(bhead->nr);
546 static void switch_endian_bh8(BHead8 *bhead)
548 /* the ID_.. codes */
549 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
551 if (bhead->code != ENDB) {
552 SWITCH_INT(bhead->len);
553 SWITCH_INT(bhead->SDNAnr);
554 SWITCH_INT(bhead->nr);
558 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
560 BHead4 *bhead4 = (BHead4 *) bhead;
561 #if defined(WIN32) && !defined(FREE_WINDOWS)
567 bhead4->code= bhead8->code;
568 bhead4->len= bhead8->len;
570 if (bhead4->code != ENDB) {
572 //perform a endian swap on 64bit pointers, otherwise the pointer might map to zero
573 //0x0000000000000000000012345678 would become 0x12345678000000000000000000000000
574 if (do_endian_swap) {
575 SWITCH_LONGINT(bhead8->old);
578 /* this patch is to avoid a long long being read from not-eight aligned positions
579 is necessary on any modern 64bit architecture) */
580 memcpy(&old, &bhead8->old, 8);
581 bhead4->old = (int) (old >> 3);
583 bhead4->SDNAnr= bhead8->SDNAnr;
584 bhead4->nr= bhead8->nr;
588 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
590 BHead8 *bhead8 = (BHead8 *) bhead;
592 bhead8->code= bhead4->code;
593 bhead8->len= bhead4->len;
595 if (bhead8->code != ENDB) {
596 bhead8->old= bhead4->old;
597 bhead8->SDNAnr= bhead4->SDNAnr;
598 bhead8->nr= bhead4->nr;
602 static BHeadN *get_bhead(FileData *fd)
604 BHeadN *new_bhead = NULL;
609 /* initializing to zero isn't strictly needed but shuts valgrind up
610 * since uninitialized memory gets compared */
615 // First read the bhead structure.
616 // Depending on the platform the file was written on this can
617 // be a big or little endian BHead4 or BHead8 structure.
619 // As usual 'ENDB' (the last *partial* bhead of the file)
620 // needs some special handling. We don't want to EOF just yet.
622 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
624 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
626 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
627 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
628 switch_endian_bh4(&bhead4);
631 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
632 bh8_from_bh4(&bhead, &bhead4);
634 memcpy(&bhead, &bhead4, sizeof(bhead));
642 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
644 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
645 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
646 switch_endian_bh8(&bhead8);
649 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
650 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
652 memcpy(&bhead, &bhead8, sizeof(bhead));
660 /* make sure people are not trying to pass bad blend files */
661 if (bhead.len < 0) fd->eof = 1;
663 // bhead now contains the (converted) bhead structure. Now read
664 // the associated data and put everything in a BHeadN (creative naming !)
667 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
669 new_bhead->next = new_bhead->prev = NULL;
670 new_bhead->bhead = bhead;
672 readsize = fd->read(fd, new_bhead + 1, bhead.len);
674 if (readsize != bhead.len) {
676 MEM_freeN(new_bhead);
686 // We've read a new block. Now add it to the list
690 BLI_addtail(&fd->listbase, new_bhead);
696 BHead *blo_firstbhead(FileData *fd)
702 // Read in a new block if necessary
704 new_bhead = fd->listbase.first;
705 if (new_bhead == NULL) {
706 new_bhead = get_bhead(fd);
710 bhead = &new_bhead->bhead;
716 BHead *blo_prevbhead(FileData *UNUSED(fd), BHead *thisblock)
718 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
719 BHeadN *prev= bheadn->prev;
721 return prev?&prev->bhead:NULL;
724 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
726 BHeadN *new_bhead = NULL;
730 // bhead is actually a sub part of BHeadN
731 // We calculate the BHeadN pointer from the BHead pointer below
732 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
734 // get the next BHeadN. If it doesn't exist we read in the next one
735 new_bhead = new_bhead->next;
736 if (new_bhead == NULL) {
737 new_bhead = get_bhead(fd);
742 // here we do the reverse:
743 // go from the BHeadN pointer to the BHead pointer
744 bhead = &new_bhead->bhead;
750 static void decode_blender_header(FileData *fd)
752 char header[SIZEOFBLENDERHEADER], num[4];
755 // read in the header data
756 readsize = fd->read(fd, header, sizeof(header));
758 if (readsize == sizeof(header)) {
759 if(strncmp(header, "BLENDER", 7) == 0) {
760 int remove_this_endian_test= 1;
762 fd->flags |= FD_FLAGS_FILE_OK;
764 // what size are pointers in the file ?
766 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
767 if (sizeof(void *) != 4) {
768 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
771 if (sizeof(void *) != 8) {
772 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
776 // is the file saved in a different endian
778 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
779 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
782 // get the version number
784 memcpy(num, header+9, 3);
786 fd->fileversion = atoi(num);
791 static int read_file_dna(FileData *fd)
795 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
796 if (bhead->code==DNA1) {
797 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
799 fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
802 fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
803 /* used to retrieve ID names from (bhead+1) */
804 fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
808 } else if (bhead->code==ENDB)
815 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
817 int readsize = read(filedata->filedes, buffer, size);
822 filedata->seek += readsize;
828 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
830 int readsize = gzread(filedata->gzfiledes, buffer, size);
835 filedata->seek += readsize;
841 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
843 // don't read more bytes then there are available in the buffer
844 int readsize = (int)MIN2(size, (unsigned int)(filedata->buffersize - filedata->seek));
846 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
847 filedata->seek += readsize;
852 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
854 static unsigned int seek= 1<<30; /* the current position */
855 static unsigned int offset= 0; /* size of previous chunks */
856 static MemFileChunk *chunk=NULL;
857 unsigned int chunkoffset, readsize, totread;
859 if(size==0) return 0;
861 if(seek != (unsigned int)filedata->seek) {
862 chunk= filedata->memfile->chunks.first;
866 if(seek + chunk->size > (unsigned) filedata->seek) break;
871 seek= filedata->seek;
878 /* first check if it's on the end if current chunk */
879 if(seek-offset == chunk->size) {
880 offset+= chunk->size;
884 /* debug, should never happen */
886 printf("illegal read, chunk zero\n");
890 chunkoffset= seek-offset;
891 readsize= size-totread;
893 /* data can be spread over multiple chunks, so clamp size
894 * to within this chunk, and then it will read further in
896 if(chunkoffset+readsize > chunk->size)
897 readsize= chunk->size-chunkoffset;
899 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
901 filedata->seek += readsize;
903 } while(totread < size);
911 static FileData *filedata_new(void)
913 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
916 fd->gzfiledes = NULL;
918 /* XXX, this doesn't need to be done all the time,
919 * but it keeps us reentrant, remove once we have
920 * a lib that provides a nice lock. - zr
922 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
924 fd->datamap = oldnewmap_new();
925 fd->globmap = oldnewmap_new();
926 fd->libmap = oldnewmap_new();
931 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
933 decode_blender_header(fd);
935 if (fd->flags & FD_FLAGS_FILE_OK) {
936 if (!read_file_dna(fd)) {
937 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", incomplete", fd->relabase);
938 blo_freefiledata(fd);
943 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", not a blend file", fd->relabase);
944 blo_freefiledata(fd);
951 /* cannot be called with relative paths anymore! */
952 /* on each new library added, it now checks for the current FileData and expands relativeness */
953 FileData *blo_openblenderfile(const char *filepath, ReportList *reports)
957 gzfile= gzopen(filepath, "rb");
959 if (gzfile == (gzFile)Z_NULL) {
960 BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", filepath, errno ? strerror(errno) : "Unknown error reading file");
963 FileData *fd = filedata_new();
964 fd->gzfiledes = gzfile;
965 fd->read = fd_read_gzip_from_file;
967 /* needed for library_append and read_libraries */
968 BLI_strncpy(fd->relabase, filepath, sizeof(fd->relabase));
970 return blo_decode_and_check(fd, reports);
974 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
976 if (!mem || memsize<SIZEOFBLENDERHEADER) {
977 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
980 FileData *fd= filedata_new();
982 fd->buffersize= memsize;
983 fd->read= fd_read_from_memory;
984 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
986 return blo_decode_and_check(fd, reports);
990 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
993 BKE_report(reports, RPT_ERROR, "Unable to open blend <memory>");
996 FileData *fd= filedata_new();
997 fd->memfile= memfile;
999 fd->read= fd_read_from_memfile;
1000 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
1002 return blo_decode_and_check(fd, reports);
1007 void blo_freefiledata(FileData *fd)
1011 if (fd->filedes != -1) {
1015 if (fd->gzfiledes != NULL)
1017 gzclose(fd->gzfiledes);
1020 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1021 MEM_freeN(fd->buffer);
1025 // Free all BHeadN data blocks
1026 BLI_freelistN(&fd->listbase);
1029 DNA_sdna_free(fd->memsdna);
1031 DNA_sdna_free(fd->filesdna);
1033 MEM_freeN(fd->compflags);
1036 oldnewmap_free(fd->datamap);
1038 oldnewmap_free(fd->globmap);
1040 oldnewmap_free(fd->imamap);
1041 if (fd->movieclipmap)
1042 oldnewmap_free(fd->movieclipmap);
1043 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1044 oldnewmap_free(fd->libmap);
1046 MEM_freeN(fd->bheadmap);
1052 /* ************ DIV ****************** */
1054 int BLO_has_bfile_extension(char *str)
1056 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend") || BLI_testextensie(str, ".blend.gz"));
1059 int BLO_is_a_library(const char *path, char *dir, char *group)
1061 /* return ok when a blenderfile, in dir is the filename,
1062 * in group the type of libdata
1070 if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
1075 /* Find the last slash */
1076 fd= BLI_last_slash(dir);
1078 if(fd==NULL) return 0;
1080 if(BLO_has_bfile_extension(fd+1)) {
1081 /* the last part of the dir is a .blend file, no group follows */
1082 *fd= '/'; /* put back the removed slash separating the dir and the .blend file name */
1085 char *gp = fd+1; // in case we have a .blend file, gp points to the group
1087 /* Find the last slash */
1088 fd= BLI_last_slash(dir);
1089 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
1091 /* now we know that we are in a blend file and it is safe to
1092 assume that gp actually points to a group */
1093 if (strcmp("Screen", gp)!=0)
1094 BLI_strncpy(group, gp, GROUP_MAX);
1099 /* ************** OLD POINTERS ******************* */
1101 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1103 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1106 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1108 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1111 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1113 if(fd->imamap && adr)
1114 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1118 static void *newmclipadr(FileData *fd, void *adr) /* used to restore movie clip data after undo */
1120 if(fd->movieclipmap && adr)
1121 return oldnewmap_lookup_and_inc(fd->movieclipmap, adr);
1126 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1128 return oldnewmap_liblookup(fd->libmap, adr, lib);
1131 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1133 ID *id= newlibadr(fd, lib, adr);
1141 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1145 for (i=0; i<fd->libmap->nentries; i++) {
1146 OldNew *entry= &fd->libmap->entries[i];
1148 if (old==entry->newp && entry->nr==ID_ID) {
1150 if(new) entry->nr= GS( ((ID *)new)->name );
1156 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1160 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1163 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1167 change_idid_adr_fd(fd, old, new);
1172 /* lib linked proxy objects point to our local data, we need
1173 * to clear that pointer before reading the undo memfile since
1174 * the object might be removed, it is set again in reading
1175 * if the local object still exists */
1176 void blo_clear_proxy_pointers_from_lib(Main *oldmain)
1178 Object *ob= oldmain->object.first;
1180 for(;ob; ob= ob->id.next)
1182 ob->proxy_from= NULL;
1185 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1187 Image *ima= oldmain->image.first;
1188 Scene *sce= oldmain->scene.first;
1191 fd->imamap= oldnewmap_new();
1193 for(;ima; ima= ima->id.next) {
1194 Link *ibuf= ima->ibufs.first;
1195 for(; ibuf; ibuf= ibuf->next)
1196 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1198 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1199 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
1201 oldnewmap_insert(fd->imamap, ima->renders[a], ima->renders[a], 0);
1203 for(; sce; sce= sce->id.next) {
1206 for(node= sce->nodetree->nodes.first; node; node= node->next)
1207 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1212 /* set old main image ibufs to zero if it has been restored */
1213 /* this works because freeing old main only happens after this call */
1214 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1216 OldNew *entry= fd->imamap->entries;
1217 Image *ima= oldmain->image.first;
1218 Scene *sce= oldmain->scene.first;
1221 /* used entries were restored, so we put them to zero */
1222 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1227 for(;ima; ima= ima->id.next) {
1230 /* this mirrors direct_link_image */
1231 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1233 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1234 BLI_remlink(&ima->ibufs, ibuf);
1236 ima->gputexture= NULL;
1239 for(i=0; i<IMA_MAX_RENDER_SLOT; i++)
1240 ima->renders[i]= newimaadr(fd, ima->renders[i]);
1242 ima->gputexture= newimaadr(fd, ima->gputexture);
1244 for(; sce; sce= sce->id.next) {
1247 for(node= sce->nodetree->nodes.first; node; node= node->next)
1248 node->preview= newimaadr(fd, node->preview);
1253 void blo_make_movieclip_pointer_map(FileData *fd, Main *oldmain)
1255 MovieClip *clip= oldmain->movieclip.first;
1257 fd->movieclipmap= oldnewmap_new();
1259 for(;clip; clip= clip->id.next) {
1261 oldnewmap_insert(fd->movieclipmap, clip->cache, clip->cache, 0);
1263 if(clip->tracking.camera.intrinsics)
1264 oldnewmap_insert(fd->movieclipmap, clip->tracking.camera.intrinsics, clip->tracking.camera.intrinsics, 0);
1268 /* set old main movie clips caches to zero if it has been restored */
1269 /* this works because freeing old main only happens after this call */
1270 void blo_end_movieclip_pointer_map(FileData *fd, Main *oldmain)
1272 OldNew *entry= fd->movieclipmap->entries;
1273 MovieClip *clip= oldmain->movieclip.first;
1276 /* used entries were restored, so we put them to zero */
1277 for (i=0; i<fd->movieclipmap->nentries; i++, entry++) {
1282 for(;clip; clip= clip->id.next) {
1283 clip->cache= newmclipadr(fd, clip->cache);
1284 clip->tracking.camera.intrinsics= newmclipadr(fd, clip->tracking.camera.intrinsics);
1289 /* undo file support: add all library pointers in lookup */
1290 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1292 Main *ptr= mainlist->first;
1293 ListBase *lbarray[MAX_LIBARRAY];
1295 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1296 int i= set_listbasepointers(ptr, lbarray);
1299 for(id= lbarray[i]->first; id; id= id->next)
1300 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1306 /* ********** END OLD POINTERS ****************** */
1307 /* ********** READ FILE ****************** */
1309 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1311 int blocksize, nblocks;
1314 data= (char *)(bhead+1);
1315 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1319 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1325 static void *read_struct(FileData *fd, BHead *bh, const char *blockname)
1330 /* switch is based on file dna */
1331 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1332 switch_endian_structs(fd->filesdna, bh);
1334 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1335 if(fd->compflags[bh->SDNAnr]==2) {
1336 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1338 temp= MEM_mallocN(bh->len, blockname);
1339 memcpy(temp, (bh+1), bh->len);
1347 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1351 if(lb->first==NULL) return;
1353 lb->first= newdataadr(fd, lb->first);
1357 ln->next= newdataadr(fd, ln->next);
1365 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1370 if(lb->first==NULL) return;
1371 poin= newdataadr(fd, lb->first);
1373 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1380 poin= newdataadr(fd, ln->next);
1382 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1392 static void test_pointer_array(FileData *fd, void **mat)
1394 #if defined(WIN32) && !defined(FREE_WINDOWS)
1395 __int64 *lpoin, *lmat;
1397 long long *lpoin, *lmat;
1402 /* manually convert the pointer array in
1403 * the old dna format to a pointer array in
1404 * the new dna format.
1407 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1409 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1410 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1414 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1415 SWITCH_LONGINT(*lpoin);
1416 *ipoin= (int) ((*lpoin) >> 3);
1424 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1425 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1439 /* ************ READ ID Properties *************** */
1441 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1442 static void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1444 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1449 /*since we didn't save the extra buffer, set totallen to len.*/
1450 prop->totallen = prop->len;
1451 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1453 array= (IDProperty*) prop->data.pointer;
1455 /* note!, idp-arrays didn't exist in 2.4x, so the pointer will be cleared
1456 * theres not really anything we can do to correct this, at least dont crash */
1463 for(i=0; i<prop->len; i++)
1464 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
1467 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1472 /*since we didn't save the extra buffer, set totallen to len.*/
1473 prop->totallen = prop->len;
1474 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1476 if(prop->subtype == IDP_GROUP) {
1477 test_pointer_array(fd, prop->data.pointer);
1478 array= prop->data.pointer;
1480 for(i=0; i<prop->len; i++)
1481 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1483 else if(prop->subtype == IDP_DOUBLE) {
1484 if (switch_endian) {
1485 for (i=0; i<prop->len; i++) {
1486 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1491 if (switch_endian) {
1492 for (i=0; i<prop->len; i++) {
1493 SWITCH_INT(((int*)prop->data.pointer)[i]);
1499 static void IDP_DirectLinkString(IDProperty *prop, FileData *fd)
1501 /*since we didn't save the extra string buffer, set totallen to len.*/
1502 prop->totallen = prop->len;
1503 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1506 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1508 ListBase *lb = &prop->data.group;
1513 /*Link child id properties now*/
1514 for (loop=prop->data.group.first; loop; loop=loop->next) {
1515 IDP_DirectLinkProperty(loop, switch_endian, fd);
1519 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1521 switch (prop->type) {
1523 IDP_DirectLinkGroup(prop, switch_endian, fd);
1526 IDP_DirectLinkString(prop, fd);
1529 IDP_DirectLinkArray(prop, switch_endian, fd);
1532 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1535 /*erg, stupid doubles. since I'm storing them
1536 in the same field as int val; val2 in the
1537 IDPropertyData struct, they have to deal with
1538 endianness specifically
1540 in theory, val and val2 would've already been swapped
1541 if switch_endian is true, so we have to first unswap
1542 them then reswap them as a single 64-bit entity.
1545 if (switch_endian) {
1546 SWITCH_INT(prop->data.val);
1547 SWITCH_INT(prop->data.val2);
1548 SWITCH_LONGINT(prop->data.val);
1556 static void IDP_LibLinkProperty(IDProperty *UNUSED(prop), int UNUSED(switch_endian), FileData *UNUSED(fd))
1560 /* ************ READ CurveMapping *************** */
1562 /* cuma itself has been read! */
1563 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1567 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1568 cumap->flag &= ~CUMA_PREMULLED;
1570 for(a=0; a<CM_TOT; a++) {
1571 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1572 cumap->cm[a].table= NULL;
1573 cumap->cm[a].premultable= NULL;
1577 /* ************ READ Brush *************** */
1578 /* library brush linking after fileread */
1579 static void lib_link_brush(FileData *fd, Main *main)
1583 /* only link ID pointers */
1584 for(brush= main->brush.first; brush; brush= brush->id.next) {
1585 if(brush->id.flag & LIB_NEEDLINK) {
1586 brush->id.flag -= LIB_NEEDLINK;
1588 brush->mtex.tex= newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
1589 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1594 static void direct_link_brush(FileData *fd, Brush *brush)
1596 /* brush itself has been read */
1599 brush->curve= newdataadr(fd, brush->curve);
1601 direct_link_curvemapping(fd, brush->curve);
1603 brush_curve_preset(brush, CURVE_PRESET_SHARP);
1605 brush->preview= NULL;
1606 brush->icon_imbuf= NULL;
1609 static void direct_link_script(FileData *UNUSED(fd), Script *script)
1612 SCRIPT_SET_NULL(script)
1616 /* ************ READ PACKEDFILE *************** */
1618 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1620 PackedFile *pf= newdataadr(fd, oldpf);
1623 pf->data= newdataadr(fd, pf->data);
1629 /* ************ READ IMAGE PREVIEW *************** */
1631 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1633 PreviewImage *prv= newdataadr(fd, old_prv);
1637 for (i=0; i < NUM_ICON_SIZES; ++i) {
1639 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1647 /* ************ READ ANIMATION STUFF ***************** */
1649 /* Legacy Data Support (for Version Patching) ----------------------------- */
1651 // XXX depreceated - old animation system
1652 static void lib_link_ipo(FileData *fd, Main *main)
1656 ipo= main->ipo.first;
1658 if(ipo->id.flag & LIB_NEEDLINK) {
1660 for(icu= ipo->curve.first; icu; icu= icu->next) {
1662 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1664 ipo->id.flag -= LIB_NEEDLINK;
1670 // XXX depreceated - old animation system
1671 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1675 link_list(fd, &(ipo->curve));
1676 icu= ipo->curve.first;
1678 icu->bezt= newdataadr(fd, icu->bezt);
1679 icu->bp= newdataadr(fd, icu->bp);
1680 icu->driver= newdataadr(fd, icu->driver);
1685 // XXX depreceated - old animation system
1686 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1688 bActionStrip *strip;
1689 bActionModifier *amod;
1691 for (strip=striplist->first; strip; strip=strip->next){
1692 strip->object = newlibadr(fd, id->lib, strip->object);
1693 strip->act = newlibadr_us(fd, id->lib, strip->act);
1694 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1695 for(amod= strip->modifiers.first; amod; amod= amod->next)
1696 amod->ob= newlibadr(fd, id->lib, amod->ob);
1700 // XXX depreceated - old animation system
1701 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1703 bActionStrip *strip;
1705 link_list(fd, strips);
1707 for(strip= strips->first; strip; strip= strip->next)
1708 link_list(fd, &strip->modifiers);
1711 // XXX depreceated - old animation system
1712 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1714 bConstraintChannel *chan;
1716 for (chan=chanbase->first; chan; chan=chan->next){
1717 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1721 /* Data Linking ----------------------------- */
1723 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1727 for (fcm= list->first; fcm; fcm= fcm->next) {
1728 /* data for specific modifiers */
1729 switch (fcm->type) {
1730 case FMODIFIER_TYPE_PYTHON:
1732 FMod_Python *data= (FMod_Python *)fcm->data;
1733 data->script = newlibadr(fd, id->lib, data->script);
1740 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1747 /* relink ID-block references... */
1748 for (fcu= list->first; fcu; fcu= fcu->next) {
1751 ChannelDriver *driver= fcu->driver;
1754 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1755 DRIVER_TARGETS_LOOPER(dvar)
1757 /* only relink if still used */
1758 if (tarIndex < dvar->num_targets)
1759 dtar->id= newlibadr(fd, id->lib, dtar->id);
1763 DRIVER_TARGETS_LOOPER_END
1768 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1773 /* NOTE: this assumes that link_list has already been called on the list */
1774 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1778 for (fcm= list->first; fcm; fcm= fcm->next) {
1779 /* relink general data */
1780 fcm->data = newdataadr(fd, fcm->data);
1783 /* do relinking of data for specific types */
1784 switch (fcm->type) {
1785 case FMODIFIER_TYPE_GENERATOR:
1787 FMod_Generator *data= (FMod_Generator *)fcm->data;
1789 data->coefficients= newdataadr(fd, data->coefficients);
1791 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1793 for(a = 0; a < data->arraysize; a++)
1794 SWITCH_INT(data->coefficients[a]);
1798 case FMODIFIER_TYPE_ENVELOPE:
1800 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1802 data->data= newdataadr(fd, data->data);
1805 case FMODIFIER_TYPE_PYTHON:
1807 FMod_Python *data= (FMod_Python *)fcm->data;
1809 data->prop = newdataadr(fd, data->prop);
1810 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1817 /* NOTE: this assumes that link_list has already been called on the list */
1818 static void direct_link_fcurves(FileData *fd, ListBase *list)
1822 /* link F-Curve data to F-Curve again (non ID-libs) */
1823 for (fcu= list->first; fcu; fcu= fcu->next) {
1825 fcu->bezt= newdataadr(fd, fcu->bezt);
1826 fcu->fpt= newdataadr(fd, fcu->fpt);
1829 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1832 fcu->grp= newdataadr(fd, fcu->grp);
1835 fcu->driver= newdataadr(fd, fcu->driver);
1837 ChannelDriver *driver= fcu->driver;
1840 driver->expr_comp= NULL;
1842 /* relink variables, targets and their paths */
1843 link_list(fd, &driver->variables);
1844 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1845 DRIVER_TARGETS_LOOPER(dvar)
1847 /* only relink the targets being used */
1848 if (tarIndex < dvar->num_targets)
1849 dtar->rna_path= newdataadr(fd, dtar->rna_path);
1851 dtar->rna_path= NULL;
1853 DRIVER_TARGETS_LOOPER_END
1858 link_list(fd, &fcu->modifiers);
1859 direct_link_fmodifiers(fd, &fcu->modifiers);
1864 static void lib_link_action(FileData *fd, Main *main)
1867 bActionChannel *chan;
1869 for (act= main->action.first; act; act= act->id.next) {
1870 if (act->id.flag & LIB_NEEDLINK) {
1871 act->id.flag -= LIB_NEEDLINK;
1873 // XXX depreceated - old animation system <<<
1874 for (chan=act->chanbase.first; chan; chan=chan->next) {
1875 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1876 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1878 // >>> XXX depreceated - old animation system
1880 lib_link_fcurves(fd, &act->id, &act->curves);
1885 static void direct_link_action(FileData *fd, bAction *act)
1887 bActionChannel *achan; // XXX depreceated - old animation system
1890 link_list(fd, &act->curves);
1891 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1892 link_list(fd, &act->groups);
1893 link_list(fd, &act->markers);
1895 // XXX depreceated - old animation system <<<
1896 for (achan = act->chanbase.first; achan; achan=achan->next) {
1897 achan->grp= newdataadr(fd, achan->grp);
1899 link_list(fd, &achan->constraintChannels);
1901 // >>> XXX depreceated - old animation system
1903 direct_link_fcurves(fd, &act->curves);
1905 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1906 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1907 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1911 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
1915 for (strip= list->first; strip; strip= strip->next) {
1916 /* check strip's children */
1917 lib_link_nladata_strips(fd, id, &strip->strips);
1919 /* check strip's F-Curves */
1920 lib_link_fcurves(fd, id, &strip->fcurves);
1922 /* reassign the counted-reference to action */
1923 strip->act = newlibadr_us(fd, id->lib, strip->act);
1925 /* fix action id-root (i.e. if it comes from a pre 2.57 .blend file) */
1926 if ((strip->act) && (strip->act->idroot == 0))
1927 strip->act->idroot = GS(id->name);
1931 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
1935 /* we only care about the NLA strips inside the tracks */
1936 for (nlt= list->first; nlt; nlt= nlt->next) {
1937 lib_link_nladata_strips(fd, id, &nlt->strips);
1941 /* This handles Animato NLA-Strips linking
1942 * NOTE: this assumes that link_list has already been called on the list
1944 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
1948 for (strip= list->first; strip; strip= strip->next) {
1949 /* strip's child strips */
1950 link_list(fd, &strip->strips);
1951 direct_link_nladata_strips(fd, &strip->strips);
1953 /* strip's F-Curves */
1954 link_list(fd, &strip->fcurves);
1955 direct_link_fcurves(fd, &strip->fcurves);
1957 /* strip's F-Modifiers */
1958 link_list(fd, &strip->modifiers);
1959 direct_link_modifiers(fd, &strip->modifiers);
1963 /* NOTE: this assumes that link_list has already been called on the list */
1964 static void direct_link_nladata(FileData *fd, ListBase *list)
1968 for (nlt= list->first; nlt; nlt= nlt->next) {
1969 /* relink list of strips */
1970 link_list(fd, &nlt->strips);
1972 /* relink strip data */
1973 direct_link_nladata_strips(fd, &nlt->strips);
1979 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
1984 /* here, we're only interested in the ID pointer stored in some of the paths */
1985 for (ks= list->first; ks; ks= ks->next) {
1986 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1987 ksp->id= newlibadr(fd, id->lib, ksp->id);
1992 /* NOTE: this assumes that link_list has already been called on the list */
1993 static void direct_link_keyingsets(FileData *fd, ListBase *list)
1998 /* link KeyingSet data to KeyingSet again (non ID-libs) */
1999 for (ks= list->first; ks; ks= ks->next) {
2001 link_list(fd, &ks->paths);
2003 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
2005 ksp->rna_path= newdataadr(fd, ksp->rna_path);
2012 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
2017 /* link action data */
2018 adt->action= newlibadr_us(fd, id->lib, adt->action);
2019 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
2021 /* fix action id-roots (i.e. if they come from a pre 2.57 .blend file) */
2022 if ((adt->action) && (adt->action->idroot == 0))
2023 adt->action->idroot = GS(id->name);
2024 if ((adt->tmpact) && (adt->tmpact->idroot == 0))
2025 adt->tmpact->idroot = GS(id->name);
2028 lib_link_fcurves(fd, id, &adt->drivers);
2030 /* overrides don't have lib-link for now, so no need to do anything */
2033 lib_link_nladata(fd, id, &adt->nla_tracks);
2036 static void direct_link_animdata(FileData *fd, AnimData *adt)
2038 /* NOTE: must have called newdataadr already before doing this... */
2043 link_list(fd, &adt->drivers);
2044 direct_link_fcurves(fd, &adt->drivers);
2046 /* link overrides */
2050 link_list(fd, &adt->nla_tracks);
2051 direct_link_nladata(fd, &adt->nla_tracks);
2053 /* relink active strip - even though strictly speaking this should only be used
2054 * if we're in 'tweaking mode', we need to be able to have this loaded back for
2055 * undo, but also since users may not exit tweakmode before saving (#24535)
2057 // TODO: it's not really nice that anyone should be able to save the file in this
2058 // state, but it's going to be too hard to enforce this single case...
2059 adt->actstrip= newdataadr(fd, adt->actstrip);
2062 /* ************ READ MOTION PATHS *************** */
2064 /* direct data for cache */
2065 static void direct_link_motionpath(FileData *fd, bMotionPath *mpath)
2071 /* relink points cache */
2072 mpath->points= newdataadr(fd, mpath->points);
2075 /* ************ READ NODE TREE *************** */
2077 /* singe node tree (also used for material/scene trees), ntree is not NULL */
2078 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
2082 if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
2084 ntree->gpd= newlibadr_us(fd, id->lib, ntree->gpd);
2086 for(node= ntree->nodes.first; node; node= node->next)
2087 node->id= newlibadr_us(fd, id->lib, node->id);
2090 /* library ntree linking after fileread */
2091 static void lib_link_nodetree(FileData *fd, Main *main)
2095 /* only link ID pointers */
2096 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2097 if(ntree->id.flag & LIB_NEEDLINK) {
2098 ntree->id.flag -= LIB_NEEDLINK;
2099 lib_link_ntree(fd, &ntree->id, ntree);
2104 static void lib_nodetree_init_types_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2108 ntreeInitTypes(ntree);
2110 /* XXX could be replaced by do_versions for new nodes */
2111 for (node=ntree->nodes.first; node; node=node->next)
2112 node_verify_socket_templates(ntree, node);
2115 /* updates group node socket own_index so that
2116 * external links to/from the group node are preserved.
2118 static void lib_node_do_versions_group_indices(bNode *gnode)
2120 bNodeTree *ngroup= (bNodeTree*)gnode->id;
2122 bNodeSocket *sock, *gsock, *intsock;
2125 for (sock=gnode->outputs.first; sock; sock=sock->next) {
2126 int old_index = sock->to_index;
2127 for (gsock=ngroup->outputs.first; gsock; gsock=gsock->next) {
2128 if (gsock->link && gsock->link->fromsock->own_index == old_index) {
2129 sock->own_index = gsock->own_index;
2134 for (sock=gnode->inputs.first; sock; sock=sock->next) {
2135 int old_index = sock->to_index;
2136 /* can't use break in double loop */
2138 for (intnode=ngroup->nodes.first; intnode && !found; intnode=intnode->next) {
2139 for (intsock=intnode->inputs.first; intsock; intsock=intsock->next) {
2140 if (intsock->own_index == old_index && intsock->link) {
2141 sock->own_index = intsock->link->fromsock->own_index;
2150 /* updates external links for all group nodes in a tree */
2151 static void lib_nodetree_do_versions_group_indices_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2155 for (node=ntree->nodes.first; node; node=node->next) {
2156 if (node->type==NODE_GROUP) {
2157 bNodeTree *ngroup= (bNodeTree*)node->id;
2158 if (ngroup && (ngroup->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE))
2159 lib_node_do_versions_group_indices(node);
2164 /* make an update call for the tree */
2165 static void lib_nodetree_do_versions_update_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2168 ntreeUpdateTree(ntree);
2171 /* verify types for nodes and groups, all data has to be read */
2172 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
2174 static void lib_verify_nodetree(Main *main, int UNUSED(open))
2178 bNodeTreeType *ntreetype;
2180 /* this crashes blender on undo/redo
2182 reinit_nodesystem();
2185 /* set node->typeinfo pointers */
2186 for (i=0; i < NUM_NTREE_TYPES; ++i) {
2187 ntreetype= ntreeGetType(i);
2188 if (ntreetype && ntreetype->foreach_nodetree)
2189 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_init_types_cb);
2191 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
2192 lib_nodetree_init_types_cb(NULL, NULL, ntree);
2195 int has_old_groups=0;
2196 /* XXX this should actually be part of do_versions, but since we need
2197 * finished library linking, it is not possible there. Instead in do_versions
2198 * we have set the NTREE_DO_VERSIONS flag, so at this point we can do the
2199 * actual group node updates.
2201 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2202 if (ntree->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE) {
2203 /* this adds copies and links from all unlinked internal sockets to group inputs/outputs. */
2204 node_group_expose_all_sockets(ntree);
2209 if (has_old_groups) {
2210 for (i=0; i < NUM_NTREE_TYPES; ++i) {
2211 ntreetype= ntreeGetType(i);
2212 if (ntreetype && ntreetype->foreach_nodetree)
2213 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_group_indices_cb);
2217 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
2218 ntree->flag &= ~NTREE_DO_VERSIONS_GROUP_EXPOSE;
2221 /* verify all group user nodes */
2222 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2223 ntreeVerifyNodes(main, &ntree->id);
2226 /* make update calls where necessary */
2228 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
2230 ntreeUpdateTree(ntree);
2231 for (i=0; i < NUM_NTREE_TYPES; ++i) {
2232 ntreetype= ntreeGetType(i);
2233 if (ntreetype && ntreetype->foreach_nodetree)
2234 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_update_cb);
2239 static void direct_link_node_socket(FileData *fd, bNodeSocket *sock)
2241 sock->link= newdataadr(fd, sock->link);
2242 sock->storage= newdataadr(fd, sock->storage);
2243 sock->default_value= newdataadr(fd, sock->default_value);
2247 /* ntree itself has been read! */
2248 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2250 /* note: writing and reading goes in sync, for speed */
2255 ntree->init= 0; /* to set callbacks and force setting types */
2256 ntree->progress= NULL;
2257 ntree->execdata= NULL;
2259 ntree->adt= newdataadr(fd, ntree->adt);
2260 direct_link_animdata(fd, ntree->adt);
2262 link_list(fd, &ntree->nodes);
2263 for(node= ntree->nodes.first; node; node= node->next) {
2264 if(node->type == NODE_DYNAMIC) {
2266 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
2269 node->typeinfo= NULL;
2271 link_list(fd, &node->inputs);
2272 link_list(fd, &node->outputs);
2274 node->storage= newdataadr(fd, node->storage);
2276 /* could be handlerized at some point */
2277 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
2278 direct_link_curvemapping(fd, node->storage);
2279 else if(ntree->type==NTREE_COMPOSIT) {
2280 if( ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
2281 direct_link_curvemapping(fd, node->storage);
2282 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2283 ((ImageUser *)node->storage)->ok= 1;
2285 else if( ntree->type==NTREE_TEXTURE) {
2286 if(node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME)
2287 direct_link_curvemapping(fd, node->storage);
2288 else if(node->type==TEX_NODE_IMAGE)
2289 ((ImageUser *)node->storage)->ok= 1;
2293 link_list(fd, &ntree->links);
2295 /* external sockets */
2296 link_list(fd, &ntree->inputs);
2297 link_list(fd, &ntree->outputs);
2299 /* and we connect the rest */
2300 for(node= ntree->nodes.first; node; node= node->next) {
2301 node->parent = newdataadr(fd, node->parent);
2302 node->preview= newimaadr(fd, node->preview);
2305 for(sock= node->inputs.first; sock; sock= sock->next)
2306 direct_link_node_socket(fd, sock);
2307 for(sock= node->outputs.first; sock; sock= sock->next)
2308 direct_link_node_socket(fd, sock);
2310 for(sock= ntree->inputs.first; sock; sock= sock->next)
2311 direct_link_node_socket(fd, sock);
2312 for(sock= ntree->outputs.first; sock; sock= sock->next)
2313 direct_link_node_socket(fd, sock);
2315 for(link= ntree->links.first; link; link= link->next) {
2316 link->fromnode= newdataadr(fd, link->fromnode);
2317 link->tonode= newdataadr(fd, link->tonode);
2318 link->fromsock= newdataadr(fd, link->fromsock);
2319 link->tosock= newdataadr(fd, link->tosock);
2322 /* type verification is in lib-link */
2325 /* ************ READ ARMATURE ***************** */
2327 /* temp struct used to transport needed info to lib_link_constraint_cb() */
2328 typedef struct tConstraintLinkData {
2331 } tConstraintLinkData;
2332 /* callback function used to relink constraint ID-links */
2333 static void lib_link_constraint_cb(bConstraint *UNUSED(con), ID **idpoin, void *userdata)
2335 tConstraintLinkData *cld= (tConstraintLinkData *)userdata;
2336 *idpoin = newlibadr(cld->fd, cld->id->lib, *idpoin);
2339 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2341 tConstraintLinkData cld;
2345 for (con = conlist->first; con; con=con->next) {
2346 /* patch for error introduced by changing constraints (dunno how) */
2347 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2348 if(con->data==NULL) {
2349 con->type= CONSTRAINT_TYPE_NULL;
2351 /* own ipo, all constraints have it */
2352 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2355 /* relink all ID-blocks used by the constraints */
2359 id_loop_constraints(conlist, lib_link_constraint_cb, &cld);
2362 static void direct_link_constraints(FileData *fd, ListBase *lb)
2367 for (con=lb->first; con; con=con->next) {
2368 con->data = newdataadr(fd, con->data);
2370 switch (con->type) {
2371 case CONSTRAINT_TYPE_PYTHON:
2373 bPythonConstraint *data= con->data;
2375 link_list(fd, &data->targets);
2377 data->prop = newdataadr(fd, data->prop);
2379 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2382 case CONSTRAINT_TYPE_SPLINEIK:
2384 bSplineIKConstraint *data= con->data;
2386 data->points= newdataadr(fd, data->points);
2389 case CONSTRAINT_TYPE_KINEMATIC:
2391 con->lin_error = 0.f;
2392 con->rot_error = 0.f;
2394 case CONSTRAINT_TYPE_CHILDOF:
2396 /* XXX version patch, in older code this flag wasn't always set, and is inherent to type */
2397 if(con->ownspace == CONSTRAINT_SPACE_POSE)
2398 con->flag |= CONSTRAINT_SPACEONCE;
2405 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2407 bPoseChannel *pchan;
2408 bArmature *arm= ob->data;
2415 /* always rebuild to match proxy or lib changes */
2416 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2419 /* sync proxy layer */
2420 if(pose->proxy_layer)
2421 arm->layer = pose->proxy_layer;
2423 /* sync proxy active bone */
2424 if(pose->proxy_act_bone[0]) {
2425 Bone *bone = get_named_bone(arm, pose->proxy_act_bone);
2427 arm->act_bone = bone;
2431 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2432 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2434 /* hurms... loop in a loop, but yah... later... (ton) */
2435 pchan->bone= get_named_bone(arm, pchan->name);
2437 pchan->custom= newlibadr_us(fd, arm->id.lib, pchan->custom);
2438 if(pchan->bone==NULL)
2440 else if(ob->id.lib==NULL && arm->id.lib) {
2441 /* local pose selection copied to armature, bit hackish */
2442 pchan->bone->flag &= ~BONE_SELECTED;
2443 pchan->bone->flag |= pchan->selectflag;
2448 ob->recalc= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
2449 pose->flag |= POSE_RECALC;
2453 static void lib_link_armature(FileData *fd, Main *main)
2457 arm= main->armature.first;
2460 if(arm->id.flag & LIB_NEEDLINK) {
2461 if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
2462 arm->id.flag -= LIB_NEEDLINK;
2468 static void direct_link_bones(FileData *fd, Bone* bone)
2472 bone->parent= newdataadr(fd, bone->parent);
2473 bone->prop= newdataadr(fd, bone->prop);
2475 IDP_DirectLinkProperty(bone->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2477 bone->flag &= ~BONE_DRAW_ACTIVE;
2479 link_list(fd, &bone->childbase);
2481 for(child=bone->childbase.first; child; child=child->next)
2482 direct_link_bones(fd, child);
2485 static void direct_link_armature(FileData *fd, bArmature *arm)
2489 link_list(fd, &arm->bonebase);
2493 arm->adt= newdataadr(fd, arm->adt);
2494 direct_link_animdata(fd, arm->adt);
2496 bone=arm->bonebase.first;
2498 direct_link_bones(fd, bone);
2502 arm->act_bone= newdataadr(fd, arm->act_bone);
2503 arm->act_edbone= NULL;
2506 /* ************ READ CAMERA ***************** */
2508 static void lib_link_camera(FileData *fd, Main *main)
2512 ca= main->camera.first;
2514 if(ca->id.flag & LIB_NEEDLINK) {
2515 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2517 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2519 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2521 ca->id.flag -= LIB_NEEDLINK;
2527 static void direct_link_camera(FileData *fd, Camera *ca)
2529 ca->adt= newdataadr(fd, ca->adt);
2530 direct_link_animdata(fd, ca->adt);
2534 /* ************ READ LAMP ***************** */
2536 static void lib_link_lamp(FileData *fd, Main *main)
2542 la= main->lamp.first;
2544 if(la->id.flag & LIB_NEEDLINK) {
2545 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2547 for(a=0; a<MAX_MTEX; a++) {
2550 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2551 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2555 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2557 la->id.flag -= LIB_NEEDLINK;
2563 static void direct_link_lamp(FileData *fd, Lamp *la)
2567 la->adt= newdataadr(fd, la->adt);
2568 direct_link_animdata(fd, la->adt);
2570 for(a=0; a<MAX_MTEX; a++) {
2571 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2574 la->curfalloff= newdataadr(fd, la->curfalloff);
2576 direct_link_curvemapping(fd, la->curfalloff);
2578 la->preview = direct_link_preview_image(fd, la->preview);
2581 /* ************ READ keys ***************** */
2583 static void lib_link_key(FileData *fd, Main *main)
2587 key= main->key.first;
2589 if(key->id.flag & LIB_NEEDLINK) {
2590 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2592 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2593 key->from= newlibadr(fd, key->id.lib, key->from);
2595 key->id.flag -= LIB_NEEDLINK;
2601 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2604 char *data, *poin, *cp;
2606 elemsize= key->elemsize;
2609 for(a=0; a<kb->totelem; a++) {
2614 while( cp[0] ) { /* cp[0]==amount */
2616 switch(cp[1]) { /* cp[1]= type */
2622 SWITCH_INT((*poin));
2635 static void direct_link_key(FileData *fd, Key *key)
2639 link_list(fd, &(key->block));
2641 key->adt= newdataadr(fd, key->adt);
2642 direct_link_animdata(fd, key->adt);
2644 key->refkey= newdataadr(fd, key->refkey);
2646 kb= key->block.first;
2649 kb->data= newdataadr(fd, kb->data);
2651 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2652 switch_endian_keyblock(key, kb);
2658 /* ************ READ mball ***************** */
2660 static void lib_link_mball(FileData *fd, Main *main)
2665 mb= main->mball.first;
2667 if(mb->id.flag & LIB_NEEDLINK) {
2668 if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
2670 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2672 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2674 mb->id.flag -= LIB_NEEDLINK;
2680 static void direct_link_mball(FileData *fd, MetaBall *mb)
2682 mb->adt= newdataadr(fd, mb->adt);
2683 direct_link_animdata(fd, mb->adt);
2685 mb->mat= newdataadr(fd, mb->mat);
2686 test_pointer_array(fd, (void **)&mb->mat);
2688 link_list(fd, &(mb->elems));
2690 mb->disp.first= mb->disp.last= NULL;
2691 mb->editelems= NULL;
2693 /* mb->edit_elems.first= mb->edit_elems.last= NULL;*/
2697 /* ************ READ WORLD ***************** */
2699 static void lib_link_world(FileData *fd, Main *main)
2705 wrld= main->world.first;
2707 if(wrld->id.flag & LIB_NEEDLINK) {
2708 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2710 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2712 for(a=0; a<MAX_MTEX; a++) {
2713 mtex= wrld->mtex[a];
2715 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2716 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2720 wrld->id.flag -= LIB_NEEDLINK;
2722 wrld= wrld->id.next;
2726 static void direct_link_world(FileData *fd, World *wrld)
2730 wrld->adt= newdataadr(fd, wrld->adt);
2731 direct_link_animdata(fd, wrld->adt);
2733 for(a=0; a<MAX_MTEX; a++) {
2734 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2736 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2740 /* ************ READ VFONT ***************** */
2742 static void lib_link_vfont(FileData *UNUSED(fd), Main *main)
2746 vf= main->vfont.first;
2748 if(vf->id.flag & LIB_NEEDLINK) {
2749 vf->id.flag -= LIB_NEEDLINK;
2755 static void direct_link_vfont(FileData *fd, VFont *vf)
2758 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2761 /* ************ READ TEXT ****************** */
2763 static void lib_link_text(FileData *UNUSED(fd), Main *main)
2767 text= main->text.first;
2769 if(text->id.flag & LIB_NEEDLINK) {
2770 text->id.flag -= LIB_NEEDLINK;
2772 text= text->id.next;
2776 static void direct_link_text(FileData *fd, Text *text)
2780 text->name= newdataadr(fd, text->name);
2783 text->undo_len= TXT_INIT_UNDO;
2784 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2786 text->compiled= NULL;
2789 if(text->flags & TXT_ISEXT) {
2794 link_list(fd, &text->lines);
2795 link_list(fd, &text->markers);
2797 text->curl= newdataadr(fd, text->curl);
2798 text->sell= newdataadr(fd, text->sell);
2800 ln= text->lines.first;
2802 ln->line= newdataadr(fd, ln->line);
2805 if (ln->len != (int) strlen(ln->line)) {
2806 printf("Error loading text, line lengths differ\n");
2807 ln->len = strlen(ln->line);
2813 text->flags = (text->flags) & ~TXT_ISEXT;
2818 /* ************ READ IMAGE ***************** */
2820 static void lib_link_image(FileData *fd, Main *main)
2824 ima= main->image.first;
2826 if(ima->id.flag & LIB_NEEDLINK) {
2827 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2829 ima->id.flag -= LIB_NEEDLINK;
2835 static void link_ibuf_list(FileData *fd, ListBase *lb)
2839 if(lb->first==NULL) return;
2841 lb->first= newimaadr(fd, lb->first);
2845 ln->next= newimaadr(fd, ln->next);
2853 static void direct_link_image(FileData *fd, Image *ima)
2855 /* for undo system, pointers could be restored */
2857 link_ibuf_list(fd, &ima->ibufs);
2859 ima->ibufs.first= ima->ibufs.last= NULL;
2861 /* if not restored, we keep the binded opengl index */
2862 if(ima->ibufs.first==NULL) {
2864 ima->gputexture= NULL;
2871 /* undo system, try to restore render buffers */
2875 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
2876 ima->renders[a]= newimaadr(fd, ima->renders[a]);
2879 memset(ima->renders, 0, sizeof(ima->renders));
2880 ima->last_render_slot= ima->render_slot;
2883 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2884 ima->preview = direct_link_preview_image(fd, ima->preview);
2889 /* ************ READ CURVE ***************** */
2891 static void lib_link_curve(FileData *fd, Main *main)
2896 cu= main->curve.first;
2898 if(cu->id.flag & LIB_NEEDLINK) {
2899 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2901 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2903 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2904 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2905 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2906 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2907 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2908 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2909 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2911 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2912 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2914 cu->id.flag -= LIB_NEEDLINK;
2921 static void switch_endian_knots(Nurb *nu)
2928 SWITCH_INT(nu->knotsu[len]);
2934 SWITCH_INT(nu->knotsv[len]);
2939 static void direct_link_curve(FileData *fd, Curve *cu)
2944 cu->adt= newdataadr(fd, cu->adt);
2945 direct_link_animdata(fd, cu->adt);
2947 cu->mat= newdataadr(fd, cu->mat);
2948 test_pointer_array(fd, (void **)&cu->mat);
2949 cu->str= newdataadr(fd, cu->str);
2950 cu->strinfo= newdataadr(fd, cu->strinfo);
2951 cu->tb= newdataadr(fd, cu->tb);
2953 if(cu->vfont == NULL) link_list(fd, &(cu->nurb));
2955 cu->nurb.first=cu->nurb.last= NULL;
2957 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2959 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2966 cu->tb[0].w = cu->linewidth;
2968 if (cu->wordspace == 0.0f) cu->wordspace = 1.0f;
2971 cu->bev.first=cu->bev.last= NULL;
2972 cu->disp.first=cu->disp.last= NULL;
2980 nu->bezt= newdataadr(fd, nu->bezt);
2981 nu->bp= newdataadr(fd, nu->bp);
2982 nu->knotsu= newdataadr(fd, nu->knotsu);
2983 nu->knotsv= newdataadr(fd, nu->knotsv);
2984 if (cu->vfont == NULL) nu->charidx= nu->mat_nr;
2986 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2987 switch_endian_knots(nu);
2995 /* ************ READ TEX ***************** */
2997 static void lib_link_texture(FileData *fd, Main *main)
3001 tex= main->tex.first;
3003 if(tex->id.flag & LIB_NEEDLINK) {
3004 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
3006 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
3007 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
3008 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
3010 tex->pd->object= newlibadr(fd, tex->id.lib, tex->pd->object);
3011 if(tex->vd) tex->vd->object= newlibadr(fd, tex->id.lib, tex->vd->object);
3014 lib_link_ntree(fd, &tex->id, tex->nodetree);
3016 tex->id.flag -= LIB_NEEDLINK;
3022 static void direct_link_texture(FileData *fd, Tex *tex)
3024 tex->adt= newdataadr(fd, tex->adt);
3025 direct_link_animdata(fd, tex->adt);
3027 tex->plugin= newdataadr(fd, tex->plugin);
3029 tex->plugin->handle= NULL;
3030 open_plugin_tex(tex->plugin);
3031 /* initialize data for this instance, if an initialization
3034 if (tex->plugin->instance_init)
3035 tex->plugin->instance_init((void *) tex->plugin->data);
3037 tex->coba= newdataadr(fd, tex->coba);
3038 tex->env= newdataadr(fd, tex->env);
3040 tex->env->ima= NULL;
3041 memset(tex->env->cube, 0, 6*sizeof(void *));
3044 tex->pd= newdataadr(fd, tex->pd);
3046 tex->pd->point_tree = NULL;
3047 tex->pd->coba= newdataadr(fd, tex->pd->coba);
3048 tex->pd->falloff_curve= newdataadr(fd, tex->pd->falloff_curve);
3049 if(tex->pd->falloff_curve) {
3050 direct_link_curvemapping(fd, tex->pd->falloff_curve);
3054 tex->vd= newdataadr(fd, tex->vd);
3056 tex->vd->dataset = NULL;
3059 if(tex->type == TEX_VOXELDATA)
3060 tex->vd= MEM_callocN(sizeof(VoxelData), "direct_link_texture VoxelData");
3063 tex->nodetree= newdataadr(fd, tex->nodetree);
3065 direct_link_nodetree(fd, tex->nodetree);
3067 tex->preview = direct_link_preview_image(fd, tex->preview);
3074 /* ************ READ MATERIAL ***************** */
3076 static void lib_link_material(FileData *fd, Main *main)
3082 ma= main->mat.first;
3084 if(ma->id.flag & LIB_NEEDLINK) {
3085 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
3087 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3088 of library blocks that implement this.*/
3089 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3091 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
3092 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
3094 for(a=0; a<MAX_MTEX; a++) {
3097 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
3098 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
3103 lib_link_ntree(fd, &ma->id, ma->nodetree);
3105 ma->id.flag -= LIB_NEEDLINK;
3111 static void direct_link_material(FileData *fd, Material *ma)
3115 ma->adt= newdataadr(fd, ma->adt);
3116 direct_link_animdata(fd, ma->adt);
3118 for(a=0; a<MAX_MTEX; a++) {
3119 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
3122 ma->ramp_col= newdataadr(fd, ma->ramp_col);
3123 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
3125 ma->nodetree= newdataadr(fd, ma->nodetree);
3127 direct_link_nodetree(fd, ma->nodetree);
3129 ma->preview = direct_link_preview_image(fd, ma->preview);
3130 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
3133 /* ************ READ PARTICLE SETTINGS ***************** */
3134 /* update this also to writefile.c */
3135 static const char *ptcache_data_struct[] = {
3136 "", // BPHYS_DATA_INDEX
3137 "", // BPHYS_DATA_LOCATION
3138 "", // BPHYS_DATA_VELOCITY
3139 "", // BPHYS_DATA_ROTATION
3140 "", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
3141 "", // BPHYS_DATA_SIZE:
3142 "", // BPHYS_DATA_TIMES:
3143 "BoidData" // case BPHYS_DATA_BOIDS:
3145 static void direct_link_pointcache(FileData *fd, PointCache *cache)
3147 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
3149 PTCacheExtra *extra;
3152 link_list(fd, &cache->mem_cache);
3154 pm = cache->mem_cache.first;
3156 for(; pm; pm=pm->next) {
3157 for(i=0; i<BPHYS_TOT_DATA; i++) {
3158 pm->data[i] = newdataadr(fd, pm->data[i]);
3160 /* the cache saves non-struct data without DNA */
3161 if(pm->data[i] && strcmp(ptcache_data_struct[i], "")==0 && (fd->flags & FD_FLAGS_SWITCH_ENDIAN)) {
3162 int j, tot= (BKE_ptcache_data_size (i) * pm->totpoint)/4; /* data_size returns bytes */
3163 int *poin= pm->data[i];
3165 for(j= 0; j<tot; j++)
3166 SWITCH_INT(poin[j]);
3170 link_list(fd, &pm->extradata);
3172 for(extra=pm->extradata.first; extra; extra=extra->next)
3173 extra->data = newdataadr(fd, extra->data);
3177 cache->mem_cache.first = cache->mem_cache.last = NULL;
3179 cache->flag &= ~PTCACHE_SIMULATION_VALID;
3182 cache->free_edit= NULL;
3183 cache->cached_frames= NULL;
3186 static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointCache **ocache, int force_disk)
3188 if(ptcaches->first) {
3189 PointCache *cache= NULL;
3190 link_list(fd, ptcaches);
3191 for(cache=ptcaches->first; cache; cache=cache->next) {
3192 direct_link_pointcache(fd, cache);
3194 cache->flag |= PTCACHE_DISK_CACHE;
3199 *ocache = newdataadr(fd, *ocache);
3202 /* old "single" caches need to be linked too */
3203 *ocache = newdataadr(fd, *ocache);
3204 direct_link_pointcache(fd, *ocache);
3206 (*ocache)->flag |= PTCACHE_DISK_CACHE;
3207 (*ocache)->step = 1;
3210 ptcaches->first = ptcaches->last = *ocache;