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_property_types.h"
79 #include "DNA_text_types.h"
80 #include "DNA_view3d_types.h"
81 #include "DNA_screen_types.h"
82 #include "DNA_sensor_types.h"
83 #include "DNA_sdna_types.h"
84 #include "DNA_scene_types.h"
85 #include "DNA_sequence_types.h"
86 #include "DNA_smoke_types.h"
87 #include "DNA_sound_types.h"
88 #include "DNA_space_types.h"
89 #include "DNA_vfont_types.h"
90 #include "DNA_world_types.h"
92 #include "MEM_guardedalloc.h"
94 #include "BLI_blenlib.h"
96 #include "BLI_utildefines.h"
99 #include "BKE_action.h"
100 #include "BKE_armature.h"
101 #include "BKE_brush.h"
102 #include "BKE_colortools.h"
103 #include "BKE_constraint.h"
104 #include "BKE_context.h"
105 #include "BKE_curve.h"
106 #include "BKE_deform.h"
107 #include "BKE_effect.h" /* give_parteff */
108 #include "BKE_fcurve.h"
109 #include "BKE_global.h" // for G
110 #include "BKE_group.h"
111 #include "BKE_image.h"
112 #include "BKE_lattice.h"
113 #include "BKE_library.h" // for which_libbase
114 #include "BKE_idcode.h"
115 #include "BKE_material.h"
116 #include "BKE_main.h" // for Main
117 #include "BKE_mesh.h" // for ME_ defines (patching)
118 #include "BKE_modifier.h"
119 #include "BKE_multires.h"
120 #include "BKE_node.h" // for tree type defines
121 #include "BKE_object.h"
122 #include "BKE_paint.h"
123 #include "BKE_particle.h"
124 #include "BKE_pointcache.h"
125 #include "BKE_property.h" // for get_ob_property
126 #include "BKE_report.h"
127 #include "BKE_sca.h" // for init_actuator
128 #include "BKE_scene.h"
129 #include "BKE_screen.h"
130 #include "BKE_sequencer.h"
131 #include "BKE_texture.h" // for open_plugin_tex
132 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
133 #include "BKE_sound.h"
135 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
136 //XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
137 //XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
138 #include "BLO_readfile.h"
139 #include "BLO_undofile.h"
141 #include "readfile.h"
143 #include "PIL_time.h"
148 Remark: still a weak point is the newaddress() function, that doesnt solve reading from
149 multiple files at the same time
151 (added remark: oh, i thought that was solved? will look at that... (ton)
154 - Existing Library (Main) push or free
164 - read associated 'direct data'
165 - link direct data (internal and to LibBlock)
167 - read USER data, only when indicated (file is ~/X.XX/startup.blend)
169 - per Library (per Main)
172 - find LibBlocks and attach IDs to Main
173 - if external LibBlock
175 - or it's already read,
180 - read associated direct data
181 - link direct data (internal and to LibBlock)
183 - per Library with unread LibBlocks
188 - read associated direct data
189 - link direct data (internal and to LibBlock)
192 - link all LibBlocks and indirect pointers to libblocks
193 - initialize FileGlobal and copy pointers to Global
196 /* also occurs in library.c */
197 /* GS reads the memory pointed at in a specific ordering. There are,
198 * however two definitions for it. I have jotted them down here, both,
199 * but I think the first one is actually used. The thing is that
200 * big-endian systems might read this the wrong way round. OTOH, we
201 * constructed the IDs that are read out with this macro explicitly as
202 * well. I expect we'll sort it out soon... */
205 #define GS(a) (*((short *)(a)))
207 /* from misc_util: flip the bytes from x */
208 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
210 // only used here in readfile.c
211 #define SWITCH_LONGINT(a) { \
214 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
215 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
216 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
217 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
221 typedef struct OldNew {
226 typedef struct OldNewMap {
228 int nentries, entriessize;
234 /* local prototypes */
235 static void *read_struct(FileData *fd, BHead *bh, const char *blockname);
236 static void direct_link_modifiers(FileData *fd, ListBase *lb);
238 static OldNewMap *oldnewmap_new(void)
240 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
242 onm->entriessize= 1024;
243 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
248 static int verg_oldnewmap(const void *v1, const void *v2)
250 const struct OldNew *x1=v1, *x2=v2;
252 if( x1->old > x2->old) return 1;
253 else if( x1->old < x2->old) return -1;
258 static void oldnewmap_sort(FileData *fd)
260 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
261 fd->libmap->sorted= 1;
264 /* nr is zero for data, and ID code for libdata */
265 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
269 if(oldaddr==NULL || newaddr==NULL) return;
271 if (onm->nentries==onm->entriessize) {
272 int osize= onm->entriessize;
273 OldNew *oentries= onm->entries;
275 onm->entriessize*= 2;
276 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
278 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
282 entry= &onm->entries[onm->nentries++];
284 entry->newp= newaddr;
288 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
292 if(addr==NULL) return NULL;
294 if (onm->lasthit<onm->nentries-1) {
295 OldNew *entry= &onm->entries[++onm->lasthit];
297 if (entry->old==addr) {
303 for (i=0; i<onm->nentries; i++) {
304 OldNew *entry= &onm->entries[i];
306 if (entry->old==addr) {
317 /* for libdata, nr has ID code, no increment */
318 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
322 if(addr==NULL) return NULL;
324 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
326 OldNew entry_s, *entry;
330 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
334 if (id && (!lib || id->lib)) {
340 for (i=0; i<onm->nentries; i++) {
341 OldNew *entry= &onm->entries[i];
343 if (entry->old==addr) {
346 if (id && (!lib || id->lib)) {
355 static void oldnewmap_free_unused(OldNewMap *onm)
359 for (i=0; i<onm->nentries; i++) {
360 OldNew *entry= &onm->entries[i];
362 MEM_freeN(entry->newp);
368 static void oldnewmap_clear(OldNewMap *onm)
374 static void oldnewmap_free(OldNewMap *onm)
376 MEM_freeN(onm->entries);
382 static void read_libraries(FileData *basefd, ListBase *mainlist);
384 /* ************ help functions ***************** */
386 static void add_main_to_main(Main *mainvar, Main *from)
388 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
391 set_listbasepointers(mainvar, lbarray);
392 a= set_listbasepointers(from, fromarray);
394 BLI_movelisttolist(lbarray[a], fromarray[a]);
398 void blo_join_main(ListBase *mainlist)
400 Main *tojoin, *mainl;
403 mainl= mainlist->first;
404 while ((tojoin= mainl->next)) {
405 add_main_to_main(mainl, tojoin);
406 BLI_remlink(mainlist, tojoin);
411 static void split_libdata(ListBase *lb, Main *first)
423 if(mainvar->curlib==id->lib) {
424 lbn= which_libbase(mainvar, GS(id->name));
426 BLI_addtail(lbn, id);
429 mainvar= mainvar->next;
431 if(mainvar==NULL) printf("error split_libdata\n");
437 void blo_split_main(ListBase *mainlist, Main *main)
439 ListBase *lbarray[MAX_LIBARRAY];
443 mainlist->first= mainlist->last= main;
446 if(main->library.first==NULL)
449 for (lib= main->library.first; lib; lib= lib->id.next) {
450 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
451 libmain->curlib= lib;
452 BLI_addtail(mainlist, libmain);
455 i= set_listbasepointers(main, lbarray);
457 split_libdata(lbarray[i], main->next);
460 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
461 static void cleanup_path(const char *relabase, char *name)
463 char filename[FILE_MAXFILE];
465 BLI_splitdirstring(name, filename);
466 BLI_cleanup_dir(relabase, name);
467 strcat(name, filename);
470 static void read_file_version(FileData *fd, Main *main)
474 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
475 if (bhead->code==GLOB) {
476 FileGlobal *fg= read_struct(fd, bhead, "Global");
478 main->subversionfile= fg->subversion;
479 main->minversionfile= fg->minversion;
480 main->minsubversionfile= fg->minsubversion;
483 else if (bhead->code==ENDB)
490 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *filepath, const char *relabase)
494 char name1[FILE_MAXDIR+FILE_MAXFILE];
496 BLI_strncpy(name1, filepath, sizeof(name1));
497 cleanup_path(relabase, name1);
498 // printf("blo_find_main: original in %s\n", name);
499 // printf("blo_find_main: converted to %s\n", name1);
501 for (m= mainlist->first; m; m= m->next) {
502 char *libname= (m->curlib)?m->curlib->filepath:m->name;
504 if (BLI_path_cmp(name1, libname) == 0) {
505 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
510 m= MEM_callocN(sizeof(Main), "find_main");
511 BLI_addtail(mainlist, m);
513 lib= alloc_libblock(&m->library, ID_LI, "lib");
514 strncpy(lib->name, filepath, sizeof(lib->name)-1);
515 BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
519 read_file_version(fd, m);
521 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", filepath);
526 /* ************ FILE PARSING ****************** */
528 static void switch_endian_bh4(BHead4 *bhead)
530 /* the ID_.. codes */
531 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
533 if (bhead->code != ENDB) {
534 SWITCH_INT(bhead->len);
535 SWITCH_INT(bhead->SDNAnr);
536 SWITCH_INT(bhead->nr);
540 static void switch_endian_bh8(BHead8 *bhead)
542 /* the ID_.. codes */
543 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
545 if (bhead->code != ENDB) {
546 SWITCH_INT(bhead->len);
547 SWITCH_INT(bhead->SDNAnr);
548 SWITCH_INT(bhead->nr);
552 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
554 BHead4 *bhead4 = (BHead4 *) bhead;
555 #if defined(WIN32) && !defined(FREE_WINDOWS)
561 bhead4->code= bhead8->code;
562 bhead4->len= bhead8->len;
564 if (bhead4->code != ENDB) {
566 //perform a endian swap on 64bit pointers, otherwise the pointer might map to zero
567 //0x0000000000000000000012345678 would become 0x12345678000000000000000000000000
568 if (do_endian_swap) {
569 SWITCH_LONGINT(bhead8->old);
572 /* this patch is to avoid a long long being read from not-eight aligned positions
573 is necessary on any modern 64bit architecture) */
574 memcpy(&old, &bhead8->old, 8);
575 bhead4->old = (int) (old >> 3);
577 bhead4->SDNAnr= bhead8->SDNAnr;
578 bhead4->nr= bhead8->nr;
582 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
584 BHead8 *bhead8 = (BHead8 *) bhead;
586 bhead8->code= bhead4->code;
587 bhead8->len= bhead4->len;
589 if (bhead8->code != ENDB) {
590 bhead8->old= bhead4->old;
591 bhead8->SDNAnr= bhead4->SDNAnr;
592 bhead8->nr= bhead4->nr;
596 static BHeadN *get_bhead(FileData *fd)
598 BHeadN *new_bhead = NULL;
603 /* initializing to zero isn't strictly needed but shuts valgrind up
604 * since uninitialized memory gets compared */
609 // First read the bhead structure.
610 // Depending on the platform the file was written on this can
611 // be a big or little endian BHead4 or BHead8 structure.
613 // As usual 'ENDB' (the last *partial* bhead of the file)
614 // needs some special handling. We don't want to EOF just yet.
616 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
618 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
620 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
621 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
622 switch_endian_bh4(&bhead4);
625 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
626 bh8_from_bh4(&bhead, &bhead4);
628 memcpy(&bhead, &bhead4, sizeof(bhead));
636 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
638 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
639 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
640 switch_endian_bh8(&bhead8);
643 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
644 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
646 memcpy(&bhead, &bhead8, sizeof(bhead));
654 /* make sure people are not trying to pass bad blend files */
655 if (bhead.len < 0) fd->eof = 1;
657 // bhead now contains the (converted) bhead structure. Now read
658 // the associated data and put everything in a BHeadN (creative naming !)
661 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
663 new_bhead->next = new_bhead->prev = NULL;
664 new_bhead->bhead = bhead;
666 readsize = fd->read(fd, new_bhead + 1, bhead.len);
668 if (readsize != bhead.len) {
670 MEM_freeN(new_bhead);
680 // We've read a new block. Now add it to the list
684 BLI_addtail(&fd->listbase, new_bhead);
690 BHead *blo_firstbhead(FileData *fd)
696 // Read in a new block if necessary
698 new_bhead = fd->listbase.first;
699 if (new_bhead == NULL) {
700 new_bhead = get_bhead(fd);
704 bhead = &new_bhead->bhead;
710 BHead *blo_prevbhead(FileData *UNUSED(fd), BHead *thisblock)
712 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
713 BHeadN *prev= bheadn->prev;
715 return prev?&prev->bhead:NULL;
718 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
720 BHeadN *new_bhead = NULL;
724 // bhead is actually a sub part of BHeadN
725 // We calculate the BHeadN pointer from the BHead pointer below
726 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
728 // get the next BHeadN. If it doesn't exist we read in the next one
729 new_bhead = new_bhead->next;
730 if (new_bhead == NULL) {
731 new_bhead = get_bhead(fd);
736 // here we do the reverse:
737 // go from the BHeadN pointer to the BHead pointer
738 bhead = &new_bhead->bhead;
744 static void decode_blender_header(FileData *fd)
746 char header[SIZEOFBLENDERHEADER], num[4];
749 // read in the header data
750 readsize = fd->read(fd, header, sizeof(header));
752 if (readsize == sizeof(header)) {
753 if(strncmp(header, "BLENDER", 7) == 0) {
754 int remove_this_endian_test= 1;
756 fd->flags |= FD_FLAGS_FILE_OK;
758 // what size are pointers in the file ?
760 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
761 if (sizeof(void *) != 4) {
762 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
765 if (sizeof(void *) != 8) {
766 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
770 // is the file saved in a different endian
772 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
773 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
776 // get the version number
778 memcpy(num, header+9, 3);
780 fd->fileversion = atoi(num);
785 static int read_file_dna(FileData *fd)
789 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
790 if (bhead->code==DNA1) {
791 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
793 fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
796 fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
797 /* used to retrieve ID names from (bhead+1) */
798 fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
802 } else if (bhead->code==ENDB)
809 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
811 int readsize = read(filedata->filedes, buffer, size);
816 filedata->seek += readsize;
822 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
824 int readsize = gzread(filedata->gzfiledes, buffer, size);
829 filedata->seek += readsize;
835 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
837 // don't read more bytes then there are available in the buffer
838 int readsize = (int)MIN2(size, (unsigned int)(filedata->buffersize - filedata->seek));
840 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
841 filedata->seek += readsize;
846 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
848 static unsigned int seek= 1<<30; /* the current position */
849 static unsigned int offset= 0; /* size of previous chunks */
850 static MemFileChunk *chunk=NULL;
851 unsigned int chunkoffset, readsize, totread;
853 if(size==0) return 0;
855 if(seek != (unsigned int)filedata->seek) {
856 chunk= filedata->memfile->chunks.first;
860 if(seek + chunk->size > (unsigned) filedata->seek) break;
865 seek= filedata->seek;
872 /* first check if it's on the end if current chunk */
873 if(seek-offset == chunk->size) {
874 offset+= chunk->size;
878 /* debug, should never happen */
880 printf("illegal read, chunk zero\n");
884 chunkoffset= seek-offset;
885 readsize= size-totread;
887 /* data can be spread over multiple chunks, so clamp size
888 * to within this chunk, and then it will read further in
890 if(chunkoffset+readsize > chunk->size)
891 readsize= chunk->size-chunkoffset;
893 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
895 filedata->seek += readsize;
897 } while(totread < size);
905 static FileData *filedata_new(void)
907 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
910 fd->gzfiledes = NULL;
912 /* XXX, this doesn't need to be done all the time,
913 * but it keeps us reentrant, remove once we have
914 * a lib that provides a nice lock. - zr
916 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
918 fd->datamap = oldnewmap_new();
919 fd->globmap = oldnewmap_new();
920 fd->libmap = oldnewmap_new();
925 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
927 decode_blender_header(fd);
929 if (fd->flags & FD_FLAGS_FILE_OK) {
930 if (!read_file_dna(fd)) {
931 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", incomplete", fd->relabase);
932 blo_freefiledata(fd);
937 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", not a blend file", fd->relabase);
938 blo_freefiledata(fd);
945 /* cannot be called with relative paths anymore! */
946 /* on each new library added, it now checks for the current FileData and expands relativeness */
947 FileData *blo_openblenderfile(const char *filepath, ReportList *reports)
951 gzfile= gzopen(filepath, "rb");
953 if (gzfile == (gzFile)Z_NULL) {
954 BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", filepath, errno ? strerror(errno) : "Unknown error reading file");
957 FileData *fd = filedata_new();
958 fd->gzfiledes = gzfile;
959 fd->read = fd_read_gzip_from_file;
961 /* needed for library_append and read_libraries */
962 BLI_strncpy(fd->relabase, filepath, sizeof(fd->relabase));
964 return blo_decode_and_check(fd, reports);
968 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
970 if (!mem || memsize<SIZEOFBLENDERHEADER) {
971 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
974 FileData *fd= filedata_new();
976 fd->buffersize= memsize;
977 fd->read= fd_read_from_memory;
978 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
980 return blo_decode_and_check(fd, reports);
984 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
987 BKE_report(reports, RPT_ERROR, "Unable to open blend <memory>");
990 FileData *fd= filedata_new();
991 fd->memfile= memfile;
993 fd->read= fd_read_from_memfile;
994 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
996 return blo_decode_and_check(fd, reports);
1001 void blo_freefiledata(FileData *fd)
1005 if (fd->filedes != -1) {
1009 if (fd->gzfiledes != NULL)
1011 gzclose(fd->gzfiledes);
1014 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1015 MEM_freeN(fd->buffer);
1019 // Free all BHeadN data blocks
1020 BLI_freelistN(&fd->listbase);
1023 DNA_sdna_free(fd->memsdna);
1025 DNA_sdna_free(fd->filesdna);
1027 MEM_freeN(fd->compflags);
1030 oldnewmap_free(fd->datamap);
1032 oldnewmap_free(fd->globmap);
1034 oldnewmap_free(fd->imamap);
1035 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1036 oldnewmap_free(fd->libmap);
1038 MEM_freeN(fd->bheadmap);
1044 /* ************ DIV ****************** */
1046 int BLO_has_bfile_extension(char *str)
1048 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend") || BLI_testextensie(str, ".blend.gz"));
1051 int BLO_is_a_library(const char *path, char *dir, char *group)
1053 /* return ok when a blenderfile, in dir is the filename,
1054 * in group the type of libdata
1062 if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
1067 /* Find the last slash */
1068 fd= BLI_last_slash(dir);
1070 if(fd==NULL) return 0;
1072 if(BLO_has_bfile_extension(fd+1)) {
1073 /* the last part of the dir is a .blend file, no group follows */
1074 *fd= '/'; /* put back the removed slash separating the dir and the .blend file name */
1077 char *gp = fd+1; // in case we have a .blend file, gp points to the group
1079 /* Find the last slash */
1080 fd= BLI_last_slash(dir);
1081 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
1083 /* now we know that we are in a blend file and it is safe to
1084 assume that gp actually points to a group */
1085 if (BLI_streq("Screen", gp)==0)
1086 BLI_strncpy(group, gp, GROUP_MAX);
1091 /* ************** OLD POINTERS ******************* */
1093 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1095 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1098 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1100 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1103 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1105 if(fd->imamap && adr)
1106 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1111 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1113 return oldnewmap_liblookup(fd->libmap, adr, lib);
1116 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1118 ID *id= newlibadr(fd, lib, adr);
1126 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1130 for (i=0; i<fd->libmap->nentries; i++) {
1131 OldNew *entry= &fd->libmap->entries[i];
1133 if (old==entry->newp && entry->nr==ID_ID) {
1135 if(new) entry->nr= GS( ((ID *)new)->name );
1141 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1145 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1148 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1152 change_idid_adr_fd(fd, old, new);
1157 /* lib linked proxy objects point to our local data, we need
1158 * to clear that pointer before reading the undo memfile since
1159 * the object might be removed, it is set again in reading
1160 * if the local object still exists */
1161 void blo_clear_proxy_pointers_from_lib(Main *oldmain)
1163 Object *ob= oldmain->object.first;
1165 for(;ob; ob= ob->id.next)
1167 ob->proxy_from= NULL;
1170 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1172 Image *ima= oldmain->image.first;
1173 Scene *sce= oldmain->scene.first;
1176 fd->imamap= oldnewmap_new();
1178 for(;ima; ima= ima->id.next) {
1179 Link *ibuf= ima->ibufs.first;
1180 for(; ibuf; ibuf= ibuf->next)
1181 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1183 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1184 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
1186 oldnewmap_insert(fd->imamap, ima->renders[a], ima->renders[a], 0);
1188 for(; sce; sce= sce->id.next) {
1191 for(node= sce->nodetree->nodes.first; node; node= node->next)
1192 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1197 /* set old main image ibufs to zero if it has been restored */
1198 /* this works because freeing old main only happens after this call */
1199 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1201 OldNew *entry= fd->imamap->entries;
1202 Image *ima= oldmain->image.first;
1203 Scene *sce= oldmain->scene.first;
1206 /* used entries were restored, so we put them to zero */
1207 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1212 for(;ima; ima= ima->id.next) {
1215 /* this mirrors direct_link_image */
1216 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1218 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1219 BLI_remlink(&ima->ibufs, ibuf);
1221 ima->gputexture= NULL;
1224 for(i=0; i<IMA_MAX_RENDER_SLOT; i++)
1225 ima->renders[i]= newimaadr(fd, ima->renders[i]);
1227 ima->gputexture= newimaadr(fd, ima->gputexture);
1229 for(; sce; sce= sce->id.next) {
1232 for(node= sce->nodetree->nodes.first; node; node= node->next)
1233 node->preview= newimaadr(fd, node->preview);
1238 /* undo file support: add all library pointers in lookup */
1239 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1241 Main *ptr= mainlist->first;
1242 ListBase *lbarray[MAX_LIBARRAY];
1244 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1245 int i= set_listbasepointers(ptr, lbarray);
1248 for(id= lbarray[i]->first; id; id= id->next)
1249 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1255 /* ********** END OLD POINTERS ****************** */
1256 /* ********** READ FILE ****************** */
1258 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1260 int blocksize, nblocks;
1263 data= (char *)(bhead+1);
1264 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1268 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1274 static void *read_struct(FileData *fd, BHead *bh, const char *blockname)
1279 /* switch is based on file dna */
1280 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1281 switch_endian_structs(fd->filesdna, bh);
1283 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1284 if(fd->compflags[bh->SDNAnr]==2) {
1285 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1287 temp= MEM_mallocN(bh->len, blockname);
1288 memcpy(temp, (bh+1), bh->len);
1296 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1300 if(lb->first==NULL) return;
1302 lb->first= newdataadr(fd, lb->first);
1306 ln->next= newdataadr(fd, ln->next);
1314 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1319 if(lb->first==NULL) return;
1320 poin= newdataadr(fd, lb->first);
1322 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1329 poin= newdataadr(fd, ln->next);
1331 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1341 static void test_pointer_array(FileData *fd, void **mat)
1343 #if defined(WIN32) && !defined(FREE_WINDOWS)
1344 __int64 *lpoin, *lmat;
1346 long long *lpoin, *lmat;
1351 /* manually convert the pointer array in
1352 * the old dna format to a pointer array in
1353 * the new dna format.
1356 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1358 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1359 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1363 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1364 SWITCH_LONGINT(*lpoin);
1365 *ipoin= (int) ((*lpoin) >> 3);
1373 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1374 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1388 /* ************ READ ID Properties *************** */
1390 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1391 static void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1393 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1398 /*since we didn't save the extra buffer, set totallen to len.*/
1399 prop->totallen = prop->len;
1400 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1402 array= (IDProperty*) prop->data.pointer;
1404 /* note!, idp-arrays didn't exist in 2.4x, so the pointer will be cleared
1405 * theres not really anything we can do to correct this, at least dont crash */
1412 for(i=0; i<prop->len; i++)
1413 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
1416 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1421 /*since we didn't save the extra buffer, set totallen to len.*/
1422 prop->totallen = prop->len;
1423 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1425 if(prop->subtype == IDP_GROUP) {
1426 test_pointer_array(fd, prop->data.pointer);
1427 array= prop->data.pointer;
1429 for(i=0; i<prop->len; i++)
1430 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1432 else if(prop->subtype == IDP_DOUBLE) {
1433 if (switch_endian) {
1434 for (i=0; i<prop->len; i++) {
1435 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1440 if (switch_endian) {
1441 for (i=0; i<prop->len; i++) {
1442 SWITCH_INT(((int*)prop->data.pointer)[i]);
1448 static void IDP_DirectLinkString(IDProperty *prop, FileData *fd)
1450 /*since we didn't save the extra string buffer, set totallen to len.*/
1451 prop->totallen = prop->len;
1452 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1455 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1457 ListBase *lb = &prop->data.group;
1462 /*Link child id properties now*/
1463 for (loop=prop->data.group.first; loop; loop=loop->next) {
1464 IDP_DirectLinkProperty(loop, switch_endian, fd);
1468 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1470 switch (prop->type) {
1472 IDP_DirectLinkGroup(prop, switch_endian, fd);
1475 IDP_DirectLinkString(prop, fd);
1478 IDP_DirectLinkArray(prop, switch_endian, fd);
1481 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1484 /*erg, stupid doubles. since I'm storing them
1485 in the same field as int val; val2 in the
1486 IDPropertyData struct, they have to deal with
1487 endianness specifically
1489 in theory, val and val2 would've already been swapped
1490 if switch_endian is true, so we have to first unswap
1491 them then reswap them as a single 64-bit entity.
1494 if (switch_endian) {
1495 SWITCH_INT(prop->data.val);
1496 SWITCH_INT(prop->data.val2);
1497 SWITCH_LONGINT(prop->data.val);
1505 static void IDP_LibLinkProperty(IDProperty *UNUSED(prop), int UNUSED(switch_endian), FileData *UNUSED(fd))
1509 /* ************ READ CurveMapping *************** */
1511 /* cuma itself has been read! */
1512 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1516 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1517 cumap->flag &= ~CUMA_PREMULLED;
1519 for(a=0; a<CM_TOT; a++) {
1520 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1521 cumap->cm[a].table= NULL;
1522 cumap->cm[a].premultable= NULL;
1526 /* ************ READ Brush *************** */
1527 /* library brush linking after fileread */
1528 static void lib_link_brush(FileData *fd, Main *main)
1532 /* only link ID pointers */
1533 for(brush= main->brush.first; brush; brush= brush->id.next) {
1534 if(brush->id.flag & LIB_NEEDLINK) {
1535 brush->id.flag -= LIB_NEEDLINK;
1537 brush->mtex.tex= newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
1538 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1543 static void direct_link_brush(FileData *fd, Brush *brush)
1545 /* brush itself has been read */
1548 brush->curve= newdataadr(fd, brush->curve);
1550 direct_link_curvemapping(fd, brush->curve);
1552 brush_curve_preset(brush, CURVE_PRESET_SHARP);
1554 brush->preview= NULL;
1555 brush->icon_imbuf= NULL;
1558 static void direct_link_script(FileData *UNUSED(fd), Script *script)
1561 SCRIPT_SET_NULL(script)
1565 /* ************ READ PACKEDFILE *************** */
1567 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1569 PackedFile *pf= newdataadr(fd, oldpf);
1572 pf->data= newdataadr(fd, pf->data);
1578 /* ************ READ IMAGE PREVIEW *************** */
1580 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1582 PreviewImage *prv= newdataadr(fd, old_prv);
1586 for (i=0; i < NUM_ICON_SIZES; ++i) {
1588 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1596 /* ************ READ ANIMATION STUFF ***************** */
1598 /* Legacy Data Support (for Version Patching) ----------------------------- */
1600 // XXX depreceated - old animation system
1601 static void lib_link_ipo(FileData *fd, Main *main)
1605 ipo= main->ipo.first;
1607 if(ipo->id.flag & LIB_NEEDLINK) {
1609 for(icu= ipo->curve.first; icu; icu= icu->next) {
1611 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1613 ipo->id.flag -= LIB_NEEDLINK;
1619 // XXX depreceated - old animation system
1620 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1624 link_list(fd, &(ipo->curve));
1625 icu= ipo->curve.first;
1627 icu->bezt= newdataadr(fd, icu->bezt);
1628 icu->bp= newdataadr(fd, icu->bp);
1629 icu->driver= newdataadr(fd, icu->driver);
1634 // XXX depreceated - old animation system
1635 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1637 bActionStrip *strip;
1638 bActionModifier *amod;
1640 for (strip=striplist->first; strip; strip=strip->next){
1641 strip->object = newlibadr(fd, id->lib, strip->object);
1642 strip->act = newlibadr_us(fd, id->lib, strip->act);
1643 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1644 for(amod= strip->modifiers.first; amod; amod= amod->next)
1645 amod->ob= newlibadr(fd, id->lib, amod->ob);
1649 // XXX depreceated - old animation system
1650 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1652 bActionStrip *strip;
1654 link_list(fd, strips);
1656 for(strip= strips->first; strip; strip= strip->next)
1657 link_list(fd, &strip->modifiers);
1660 // XXX depreceated - old animation system
1661 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1663 bConstraintChannel *chan;
1665 for (chan=chanbase->first; chan; chan=chan->next){
1666 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1670 /* Data Linking ----------------------------- */
1672 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1676 for (fcm= list->first; fcm; fcm= fcm->next) {
1677 /* data for specific modifiers */
1678 switch (fcm->type) {
1679 case FMODIFIER_TYPE_PYTHON:
1681 FMod_Python *data= (FMod_Python *)fcm->data;
1682 data->script = newlibadr(fd, id->lib, data->script);
1689 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1696 /* relink ID-block references... */
1697 for (fcu= list->first; fcu; fcu= fcu->next) {
1700 ChannelDriver *driver= fcu->driver;
1703 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1704 DRIVER_TARGETS_LOOPER(dvar)
1706 /* only relink if still used */
1707 if (tarIndex < dvar->num_targets)
1708 dtar->id= newlibadr(fd, id->lib, dtar->id);
1712 DRIVER_TARGETS_LOOPER_END
1717 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1722 /* NOTE: this assumes that link_list has already been called on the list */
1723 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1727 for (fcm= list->first; fcm; fcm= fcm->next) {
1728 /* relink general data */
1729 fcm->data = newdataadr(fd, fcm->data);
1732 /* do relinking of data for specific types */
1733 switch (fcm->type) {
1734 case FMODIFIER_TYPE_GENERATOR:
1736 FMod_Generator *data= (FMod_Generator *)fcm->data;
1738 data->coefficients= newdataadr(fd, data->coefficients);
1741 case FMODIFIER_TYPE_ENVELOPE:
1743 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1745 data->data= newdataadr(fd, data->data);
1748 case FMODIFIER_TYPE_PYTHON:
1750 FMod_Python *data= (FMod_Python *)fcm->data;
1752 data->prop = newdataadr(fd, data->prop);
1753 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1760 /* NOTE: this assumes that link_list has already been called on the list */
1761 static void direct_link_fcurves(FileData *fd, ListBase *list)
1765 /* link F-Curve data to F-Curve again (non ID-libs) */
1766 for (fcu= list->first; fcu; fcu= fcu->next) {
1768 fcu->bezt= newdataadr(fd, fcu->bezt);
1769 fcu->fpt= newdataadr(fd, fcu->fpt);
1772 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1775 fcu->grp= newdataadr(fd, fcu->grp);
1778 fcu->driver= newdataadr(fd, fcu->driver);
1780 ChannelDriver *driver= fcu->driver;
1783 driver->expr_comp= NULL;
1785 /* relink variables, targets and their paths */
1786 link_list(fd, &driver->variables);
1787 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1788 DRIVER_TARGETS_LOOPER(dvar)
1790 /* only relink the targets being used */
1791 if (tarIndex < dvar->num_targets)
1792 dtar->rna_path= newdataadr(fd, dtar->rna_path);
1794 dtar->rna_path= NULL;
1796 DRIVER_TARGETS_LOOPER_END
1801 link_list(fd, &fcu->modifiers);
1802 direct_link_fmodifiers(fd, &fcu->modifiers);
1807 static void lib_link_action(FileData *fd, Main *main)
1810 bActionChannel *chan;
1812 for (act= main->action.first; act; act= act->id.next) {
1813 if (act->id.flag & LIB_NEEDLINK) {
1814 act->id.flag -= LIB_NEEDLINK;
1816 // XXX depreceated - old animation system <<<
1817 for (chan=act->chanbase.first; chan; chan=chan->next) {
1818 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1819 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1821 // >>> XXX depreceated - old animation system
1823 lib_link_fcurves(fd, &act->id, &act->curves);
1828 static void direct_link_action(FileData *fd, bAction *act)
1830 bActionChannel *achan; // XXX depreceated - old animation system
1833 link_list(fd, &act->curves);
1834 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1835 link_list(fd, &act->groups);
1836 link_list(fd, &act->markers);
1838 // XXX depreceated - old animation system <<<
1839 for (achan = act->chanbase.first; achan; achan=achan->next) {
1840 achan->grp= newdataadr(fd, achan->grp);
1842 link_list(fd, &achan->constraintChannels);
1844 // >>> XXX depreceated - old animation system
1846 direct_link_fcurves(fd, &act->curves);
1848 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1849 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1850 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1854 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
1858 for (strip= list->first; strip; strip= strip->next) {
1859 /* check strip's children */
1860 lib_link_nladata_strips(fd, id, &strip->strips);
1862 /* check strip's F-Curves */
1863 lib_link_fcurves(fd, id, &strip->fcurves);
1865 /* reassign the counted-reference to action */
1866 strip->act = newlibadr_us(fd, id->lib, strip->act);
1868 /* fix action id-root (i.e. if it comes from a pre 2.57 .blend file) */
1869 if ((strip->act) && (strip->act->idroot == 0))
1870 strip->act->idroot = GS(id->name);
1874 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
1878 /* we only care about the NLA strips inside the tracks */
1879 for (nlt= list->first; nlt; nlt= nlt->next) {
1880 lib_link_nladata_strips(fd, id, &nlt->strips);
1884 /* This handles Animato NLA-Strips linking
1885 * NOTE: this assumes that link_list has already been called on the list
1887 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
1891 for (strip= list->first; strip; strip= strip->next) {
1892 /* strip's child strips */
1893 link_list(fd, &strip->strips);
1894 direct_link_nladata_strips(fd, &strip->strips);
1896 /* strip's F-Curves */
1897 link_list(fd, &strip->fcurves);
1898 direct_link_fcurves(fd, &strip->fcurves);
1900 /* strip's F-Modifiers */
1901 link_list(fd, &strip->modifiers);
1902 direct_link_modifiers(fd, &strip->modifiers);
1906 /* NOTE: this assumes that link_list has already been called on the list */
1907 static void direct_link_nladata(FileData *fd, ListBase *list)
1911 for (nlt= list->first; nlt; nlt= nlt->next) {
1912 /* relink list of strips */
1913 link_list(fd, &nlt->strips);
1915 /* relink strip data */
1916 direct_link_nladata_strips(fd, &nlt->strips);
1922 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
1927 /* here, we're only interested in the ID pointer stored in some of the paths */
1928 for (ks= list->first; ks; ks= ks->next) {
1929 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1930 ksp->id= newlibadr(fd, id->lib, ksp->id);
1935 /* NOTE: this assumes that link_list has already been called on the list */
1936 static void direct_link_keyingsets(FileData *fd, ListBase *list)
1941 /* link KeyingSet data to KeyingSet again (non ID-libs) */
1942 for (ks= list->first; ks; ks= ks->next) {
1944 link_list(fd, &ks->paths);
1946 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1948 ksp->rna_path= newdataadr(fd, ksp->rna_path);
1955 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
1960 /* link action data */
1961 adt->action= newlibadr_us(fd, id->lib, adt->action);
1962 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
1964 /* fix action id-roots (i.e. if they come from a pre 2.57 .blend file) */
1965 if ((adt->action) && (adt->action->idroot == 0))
1966 adt->action->idroot = GS(id->name);
1967 if ((adt->tmpact) && (adt->tmpact->idroot == 0))
1968 adt->tmpact->idroot = GS(id->name);
1971 lib_link_fcurves(fd, id, &adt->drivers);
1973 /* overrides don't have lib-link for now, so no need to do anything */
1976 lib_link_nladata(fd, id, &adt->nla_tracks);
1979 static void direct_link_animdata(FileData *fd, AnimData *adt)
1981 /* NOTE: must have called newdataadr already before doing this... */
1986 link_list(fd, &adt->drivers);
1987 direct_link_fcurves(fd, &adt->drivers);
1989 /* link overrides */
1993 link_list(fd, &adt->nla_tracks);
1994 direct_link_nladata(fd, &adt->nla_tracks);
1996 /* relink active strip - even though strictly speaking this should only be used
1997 * if we're in 'tweaking mode', we need to be able to have this loaded back for
1998 * undo, but also since users may not exit tweakmode before saving (#24535)
2000 // TODO: it's not really nice that anyone should be able to save the file in this
2001 // state, but it's going to be too hard to enforce this single case...
2002 adt->actstrip= newdataadr(fd, adt->actstrip);
2005 /* ************ READ MOTION PATHS *************** */
2007 /* direct data for cache */
2008 static void direct_link_motionpath(FileData *fd, bMotionPath *mpath)
2014 /* relink points cache */
2015 mpath->points= newdataadr(fd, mpath->points);
2018 /* ************ READ NODE TREE *************** */
2020 /* singe node tree (also used for material/scene trees), ntree is not NULL */
2021 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
2025 if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
2027 ntree->gpd= newlibadr_us(fd, id->lib, ntree->gpd);
2029 for(node= ntree->nodes.first; node; node= node->next)
2030 node->id= newlibadr_us(fd, id->lib, node->id);
2033 /* library ntree linking after fileread */
2034 static void lib_link_nodetree(FileData *fd, Main *main)
2038 /* only link ID pointers */
2039 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2040 if(ntree->id.flag & LIB_NEEDLINK) {
2041 ntree->id.flag -= LIB_NEEDLINK;
2042 lib_link_ntree(fd, &ntree->id, ntree);
2047 /* updates group node socket own_index so that
2048 * external links to/from the group node are preserved.
2050 static void lib_node_do_versions_group(bNode *gnode)
2052 bNodeTree *ngroup= (bNodeTree*)gnode->id;
2054 bNodeSocket *sock, *gsock, *intsock;
2057 for (sock=gnode->outputs.first; sock; sock=sock->next) {
2058 int old_index = sock->to_index;
2059 for (gsock=ngroup->outputs.first; gsock; gsock=gsock->next) {
2060 if (gsock->link && gsock->link->fromsock->own_index == old_index) {
2061 sock->own_index = gsock->own_index;
2066 for (sock=gnode->inputs.first; sock; sock=sock->next) {
2067 int old_index = sock->to_index;
2068 /* can't use break in double loop */
2070 for (intnode=ngroup->nodes.first; intnode && !found; intnode=intnode->next) {
2071 for (intsock=intnode->inputs.first; intsock; intsock=intsock->next) {
2072 if (intsock->own_index == old_index && intsock->link) {
2073 sock->own_index = intsock->link->fromsock->own_index;
2082 /* updates external links for all group nodes in a tree */
2083 static void lib_nodetree_do_versions_group(bNodeTree *ntree)
2087 for (node=ntree->nodes.first; node; node=node->next) {
2088 if (node->type==NODE_GROUP) {
2089 bNodeTree *ngroup= (bNodeTree*)node->id;
2090 if (ngroup->flag & NTREE_DO_VERSIONS)
2091 lib_node_do_versions_group(node);
2096 /* verify types for nodes and groups, all data has to be read */
2097 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
2099 static void lib_verify_nodetree(Main *main, int UNUSED(open))
2106 /* this crashes blender on undo/redo
2108 reinit_nodesystem();
2111 /* now create the own typeinfo structs an verify nodes */
2112 /* here we still assume no groups in groups */
2113 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2114 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
2118 /*int has_old_groups=0;*/ /*UNUSED*/
2119 /* XXX this should actually be part of do_versions, but since we need
2120 * finished library linking, it is not possible there. Instead in do_versions
2121 * we have set the NTREE_DO_VERSIONS flag, so at this point we can do the
2122 * actual group node updates.
2124 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2125 if (ntree->flag & NTREE_DO_VERSIONS) {
2126 /* this adds copies and links from all unlinked internal sockets to group inputs/outputs. */
2127 nodeGroupExposeAllSockets(ntree);
2128 /*has_old_groups = 1;*/ /*UNUSED*/
2131 /* now verify all types in material trees, groups are set OK now */
2132 for(ma= main->mat.first; ma; ma= ma->id.next) {
2134 lib_nodetree_do_versions_group(ma->nodetree);
2136 /* and scene trees */
2137 for(sce= main->scene.first; sce; sce= sce->id.next) {
2139 lib_nodetree_do_versions_group(sce->nodetree);
2141 /* and texture trees */
2142 for(tx= main->tex.first; tx; tx= tx->id.next) {
2144 lib_nodetree_do_versions_group(tx->nodetree);
2147 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
2148 ntree->flag &= ~NTREE_DO_VERSIONS;
2151 /* now verify all types in material trees, groups are set OK now */
2152 for(ma= main->mat.first; ma; ma= ma->id.next) {
2154 ntreeVerifyTypes(ma->nodetree);
2156 /* and scene trees */
2157 for(sce= main->scene.first; sce; sce= sce->id.next) {
2159 ntreeVerifyTypes(sce->nodetree);
2161 /* and texture trees */
2162 for(tx= main->tex.first; tx; tx= tx->id.next) {
2164 ntreeVerifyTypes(tx->nodetree);
2170 /* ntree itself has been read! */
2171 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2173 /* note: writing and reading goes in sync, for speed */
2178 ntree->init= 0; /* to set callbacks and force setting types */
2179 ntree->progress= NULL;
2181 ntree->adt= newdataadr(fd, ntree->adt);
2182 direct_link_animdata(fd, ntree->adt);
2184 link_list(fd, &ntree->nodes);
2185 for(node= ntree->nodes.first; node; node= node->next) {
2186 if(node->type == NODE_DYNAMIC) {
2188 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
2189 node->typeinfo= NULL;
2192 node->storage= newdataadr(fd, node->storage);
2195 /* could be handlerized at some point */
2196 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
2197 direct_link_curvemapping(fd, node->storage);
2198 else if(ntree->type==NTREE_COMPOSIT) {
2199 if( ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
2200 direct_link_curvemapping(fd, node->storage);
2201 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2202 ((ImageUser *)node->storage)->ok= 1;
2204 else if( ntree->type==NTREE_TEXTURE) {
2205 if(node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME)
2206 direct_link_curvemapping(fd, node->storage);
2207 else if(node->type==TEX_NODE_IMAGE)
2208 ((ImageUser *)node->storage)->ok= 1;
2211 link_list(fd, &node->inputs);
2212 link_list(fd, &node->outputs);
2214 link_list(fd, &ntree->links);
2216 /* external sockets */
2217 link_list(fd, &ntree->inputs);
2218 link_list(fd, &ntree->outputs);
2220 /* and we connect the rest */
2221 for(node= ntree->nodes.first; node; node= node->next) {
2222 node->preview= newimaadr(fd, node->preview);
2224 for(sock= node->inputs.first; sock; sock= sock->next)
2225 sock->link= newdataadr(fd, sock->link);
2226 for(sock= node->outputs.first; sock; sock= sock->next)
2227 sock->ns.data= NULL;
2229 for(sock= ntree->outputs.first; sock; sock= sock->next)
2230 sock->link= newdataadr(fd, sock->link);
2232 for(link= ntree->links.first; link; link= link->next) {
2233 link->fromnode= newdataadr(fd, link->fromnode);
2234 link->tonode= newdataadr(fd, link->tonode);
2235 link->fromsock= newdataadr(fd, link->fromsock);
2236 link->tosock= newdataadr(fd, link->tosock);
2239 /* type verification is in lib-link */
2242 /* ************ READ ARMATURE ***************** */
2244 /* temp struct used to transport needed info to lib_link_constraint_cb() */
2245 typedef struct tConstraintLinkData {
2248 } tConstraintLinkData;
2249 /* callback function used to relink constraint ID-links */
2250 static void lib_link_constraint_cb(bConstraint *UNUSED(con), ID **idpoin, void *userdata)
2252 tConstraintLinkData *cld= (tConstraintLinkData *)userdata;
2253 *idpoin = newlibadr(cld->fd, cld->id->lib, *idpoin);
2256 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2258 tConstraintLinkData cld;
2262 for (con = conlist->first; con; con=con->next) {
2263 /* patch for error introduced by changing constraints (dunno how) */
2264 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2265 if(con->data==NULL) {
2266 con->type= CONSTRAINT_TYPE_NULL;
2268 /* own ipo, all constraints have it */
2269 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2272 /* relink all ID-blocks used by the constraints */
2276 id_loop_constraints(conlist, lib_link_constraint_cb, &cld);
2279 static void direct_link_constraints(FileData *fd, ListBase *lb)
2284 for (con=lb->first; con; con=con->next) {
2285 con->data = newdataadr(fd, con->data);
2287 switch (con->type) {
2288 case CONSTRAINT_TYPE_PYTHON:
2290 bPythonConstraint *data= con->data;
2292 link_list(fd, &data->targets);
2294 data->prop = newdataadr(fd, data->prop);
2296 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2299 case CONSTRAINT_TYPE_SPLINEIK:
2301 bSplineIKConstraint *data= con->data;
2303 data->points= newdataadr(fd, data->points);
2306 case CONSTRAINT_TYPE_KINEMATIC:
2308 con->lin_error = 0.f;
2309 con->rot_error = 0.f;
2311 case CONSTRAINT_TYPE_CHILDOF:
2313 /* XXX version patch, in older code this flag wasn't always set, and is inherent to type */
2314 if(con->ownspace == CONSTRAINT_SPACE_POSE)
2315 con->flag |= CONSTRAINT_SPACEONCE;
2322 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2324 bPoseChannel *pchan;
2325 bArmature *arm= ob->data;
2332 /* always rebuild to match proxy or lib changes */
2333 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2336 /* sync proxy layer */
2337 if(pose->proxy_layer)
2338 arm->layer = pose->proxy_layer;
2340 /* sync proxy active bone */
2341 if(pose->proxy_act_bone[0]) {
2342 Bone *bone = get_named_bone(arm, pose->proxy_act_bone);
2344 arm->act_bone = bone;
2348 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2349 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2351 /* hurms... loop in a loop, but yah... later... (ton) */
2352 pchan->bone= get_named_bone(arm, pchan->name);
2354 pchan->custom= newlibadr_us(fd, arm->id.lib, pchan->custom);
2355 if(pchan->bone==NULL)
2357 else if(ob->id.lib==NULL && arm->id.lib) {
2358 /* local pose selection copied to armature, bit hackish */
2359 pchan->bone->flag &= ~BONE_SELECTED;
2360 pchan->bone->flag |= pchan->selectflag;
2365 ob->recalc= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
2366 pose->flag |= POSE_RECALC;
2370 static void lib_link_armature(FileData *fd, Main *main)
2374 arm= main->armature.first;
2377 if(arm->id.flag & LIB_NEEDLINK) {
2378 if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
2379 arm->id.flag -= LIB_NEEDLINK;
2385 static void direct_link_bones(FileData *fd, Bone* bone)
2389 bone->parent= newdataadr(fd, bone->parent);
2390 bone->prop= newdataadr(fd, bone->prop);
2392 IDP_DirectLinkProperty(bone->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2394 bone->flag &= ~BONE_DRAW_ACTIVE;
2396 link_list(fd, &bone->childbase);
2398 for(child=bone->childbase.first; child; child=child->next)
2399 direct_link_bones(fd, child);
2402 static void direct_link_armature(FileData *fd, bArmature *arm)
2406 link_list(fd, &arm->bonebase);
2410 arm->adt= newdataadr(fd, arm->adt);
2411 direct_link_animdata(fd, arm->adt);
2413 bone=arm->bonebase.first;
2415 direct_link_bones(fd, bone);
2419 arm->act_bone= newdataadr(fd, arm->act_bone);
2420 arm->act_edbone= NULL;
2423 /* ************ READ CAMERA ***************** */
2425 static void lib_link_camera(FileData *fd, Main *main)
2429 ca= main->camera.first;
2431 if(ca->id.flag & LIB_NEEDLINK) {
2432 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2434 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2436 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2438 ca->id.flag -= LIB_NEEDLINK;
2444 static void direct_link_camera(FileData *fd, Camera *ca)
2446 ca->adt= newdataadr(fd, ca->adt);
2447 direct_link_animdata(fd, ca->adt);
2451 /* ************ READ LAMP ***************** */
2453 static void lib_link_lamp(FileData *fd, Main *main)
2459 la= main->lamp.first;
2461 if(la->id.flag & LIB_NEEDLINK) {
2462 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2464 for(a=0; a<MAX_MTEX; a++) {
2467 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2468 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2472 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2474 la->id.flag -= LIB_NEEDLINK;
2480 static void direct_link_lamp(FileData *fd, Lamp *la)
2484 la->adt= newdataadr(fd, la->adt);
2485 direct_link_animdata(fd, la->adt);
2487 for(a=0; a<MAX_MTEX; a++) {
2488 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2491 la->curfalloff= newdataadr(fd, la->curfalloff);
2493 direct_link_curvemapping(fd, la->curfalloff);
2495 la->preview = direct_link_preview_image(fd, la->preview);
2498 /* ************ READ keys ***************** */
2500 static void lib_link_key(FileData *fd, Main *main)
2504 key= main->key.first;
2506 if(key->id.flag & LIB_NEEDLINK) {
2507 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2509 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2510 key->from= newlibadr(fd, key->id.lib, key->from);
2512 key->id.flag -= LIB_NEEDLINK;
2518 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2521 char *data, *poin, *cp;
2523 elemsize= key->elemsize;
2526 for(a=0; a<kb->totelem; a++) {
2531 while( cp[0] ) { /* cp[0]==amount */
2533 switch(cp[1]) { /* cp[1]= type */
2539 SWITCH_INT((*poin));
2552 static void direct_link_key(FileData *fd, Key *key)
2556 link_list(fd, &(key->block));
2558 key->adt= newdataadr(fd, key->adt);
2559 direct_link_animdata(fd, key->adt);
2561 key->refkey= newdataadr(fd, key->refkey);
2563 kb= key->block.first;
2566 kb->data= newdataadr(fd, kb->data);
2568 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2569 switch_endian_keyblock(key, kb);
2575 /* ************ READ mball ***************** */
2577 static void lib_link_mball(FileData *fd, Main *main)
2582 mb= main->mball.first;
2584 if(mb->id.flag & LIB_NEEDLINK) {
2585 if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
2587 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2589 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2591 mb->id.flag -= LIB_NEEDLINK;
2597 static void direct_link_mball(FileData *fd, MetaBall *mb)
2599 mb->adt= newdataadr(fd, mb->adt);
2600 direct_link_animdata(fd, mb->adt);
2602 mb->mat= newdataadr(fd, mb->mat);
2603 test_pointer_array(fd, (void **)&mb->mat);
2605 link_list(fd, &(mb->elems));
2607 mb->disp.first= mb->disp.last= NULL;
2608 mb->editelems= NULL;
2610 /* mb->edit_elems.first= mb->edit_elems.last= NULL;*/
2614 /* ************ READ WORLD ***************** */
2616 static void lib_link_world(FileData *fd, Main *main)
2622 wrld= main->world.first;
2624 if(wrld->id.flag & LIB_NEEDLINK) {
2625 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2627 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2629 for(a=0; a<MAX_MTEX; a++) {
2630 mtex= wrld->mtex[a];
2632 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2633 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2637 wrld->id.flag -= LIB_NEEDLINK;
2639 wrld= wrld->id.next;
2643 static void direct_link_world(FileData *fd, World *wrld)
2647 wrld->adt= newdataadr(fd, wrld->adt);
2648 direct_link_animdata(fd, wrld->adt);
2650 for(a=0; a<MAX_MTEX; a++) {
2651 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2653 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2657 /* ************ READ VFONT ***************** */
2659 static void lib_link_vfont(FileData *UNUSED(fd), Main *main)
2663 vf= main->vfont.first;
2665 if(vf->id.flag & LIB_NEEDLINK) {
2666 vf->id.flag -= LIB_NEEDLINK;
2672 static void direct_link_vfont(FileData *fd, VFont *vf)
2675 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2678 /* ************ READ TEXT ****************** */
2680 static void lib_link_text(FileData *UNUSED(fd), Main *main)
2684 text= main->text.first;
2686 if(text->id.flag & LIB_NEEDLINK) {
2687 text->id.flag -= LIB_NEEDLINK;
2689 text= text->id.next;
2693 static void direct_link_text(FileData *fd, Text *text)
2697 text->name= newdataadr(fd, text->name);
2700 text->undo_len= TXT_INIT_UNDO;
2701 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2703 text->compiled= NULL;
2706 if(text->flags & TXT_ISEXT) {
2711 link_list(fd, &text->lines);
2712 link_list(fd, &text->markers);
2714 text->curl= newdataadr(fd, text->curl);
2715 text->sell= newdataadr(fd, text->sell);
2717 ln= text->lines.first;
2719 ln->line= newdataadr(fd, ln->line);
2722 if (ln->len != (int) strlen(ln->line)) {
2723 printf("Error loading text, line lengths differ\n");
2724 ln->len = strlen(ln->line);
2730 text->flags = (text->flags) & ~TXT_ISEXT;
2735 /* ************ READ IMAGE ***************** */
2737 static void lib_link_image(FileData *fd, Main *main)
2741 ima= main->image.first;
2743 if(ima->id.flag & LIB_NEEDLINK) {
2744 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2746 ima->id.flag -= LIB_NEEDLINK;
2752 static void link_ibuf_list(FileData *fd, ListBase *lb)
2756 if(lb->first==NULL) return;
2758 lb->first= newimaadr(fd, lb->first);
2762 ln->next= newimaadr(fd, ln->next);
2770 static void direct_link_image(FileData *fd, Image *ima)
2772 /* for undo system, pointers could be restored */
2774 link_ibuf_list(fd, &ima->ibufs);
2776 ima->ibufs.first= ima->ibufs.last= NULL;
2778 /* if not restored, we keep the binded opengl index */
2779 if(ima->ibufs.first==NULL) {
2781 ima->gputexture= NULL;
2788 /* undo system, try to restore render buffers */
2792 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
2793 ima->renders[a]= newimaadr(fd, ima->renders[a]);
2796 memset(ima->renders, 0, sizeof(ima->renders));
2797 ima->last_render_slot= ima->render_slot;
2800 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2801 ima->preview = direct_link_preview_image(fd, ima->preview);
2806 /* ************ READ CURVE ***************** */
2808 static void lib_link_curve(FileData *fd, Main *main)
2813 cu= main->curve.first;
2815 if(cu->id.flag & LIB_NEEDLINK) {
2816 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2818 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2820 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2821 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2822 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2823 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2824 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2825 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2826 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2828 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2829 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2831 cu->id.flag -= LIB_NEEDLINK;
2838 static void switch_endian_knots(Nurb *nu)
2845 SWITCH_INT(nu->knotsu[len]);
2851 SWITCH_INT(nu->knotsv[len]);
2856 static void direct_link_curve(FileData *fd, Curve *cu)
2861 cu->adt= newdataadr(fd, cu->adt);
2862 direct_link_animdata(fd, cu->adt);
2864 cu->mat= newdataadr(fd, cu->mat);
2865 test_pointer_array(fd, (void **)&cu->mat);
2866 cu->str= newdataadr(fd, cu->str);
2867 cu->strinfo= newdataadr(fd, cu->strinfo);
2868 cu->tb= newdataadr(fd, cu->tb);
2870 if(cu->vfont == NULL) link_list(fd, &(cu->nurb));
2872 cu->nurb.first=cu->nurb.last= NULL;
2874 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2876 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2883 cu->tb[0].w = cu->linewidth;
2885 if (cu->wordspace == 0.0f) cu->wordspace = 1.0f;
2888 cu->bev.first=cu->bev.last= NULL;
2889 cu->disp.first=cu->disp.last= NULL;
2897 nu->bezt= newdataadr(fd, nu->bezt);
2898 nu->bp= newdataadr(fd, nu->bp);
2899 nu->knotsu= newdataadr(fd, nu->knotsu);
2900 nu->knotsv= newdataadr(fd, nu->knotsv);
2901 if (cu->vfont == NULL) nu->charidx= nu->mat_nr;
2903 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2904 switch_endian_knots(nu);
2912 /* ************ READ TEX ***************** */
2914 static void lib_link_texture(FileData *fd, Main *main)
2918 tex= main->tex.first;
2920 if(tex->id.flag & LIB_NEEDLINK) {
2921 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
2923 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2924 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2925 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2927 tex->pd->object= newlibadr(fd, tex->id.lib, tex->pd->object);
2928 if(tex->vd) tex->vd->object= newlibadr(fd, tex->id.lib, tex->vd->object);
2931 lib_link_ntree(fd, &tex->id, tex->nodetree);
2933 tex->id.flag -= LIB_NEEDLINK;
2939 static void direct_link_texture(FileData *fd, Tex *tex)
2941 tex->adt= newdataadr(fd, tex->adt);
2942 direct_link_animdata(fd, tex->adt);
2944 tex->plugin= newdataadr(fd, tex->plugin);
2946 tex->plugin->handle= NULL;
2947 open_plugin_tex(tex->plugin);
2948 /* initialize data for this instance, if an initialization
2951 if (tex->plugin->instance_init)
2952 tex->plugin->instance_init((void *) tex->plugin->data);
2954 tex->coba= newdataadr(fd, tex->coba);
2955 tex->env= newdataadr(fd, tex->env);
2957 tex->env->ima= NULL;
2958 memset(tex->env->cube, 0, 6*sizeof(void *));
2961 tex->pd= newdataadr(fd, tex->pd);
2963 tex->pd->point_tree = NULL;
2964 tex->pd->coba= newdataadr(fd, tex->pd->coba);
2965 tex->pd->falloff_curve= newdataadr(fd, tex->pd->falloff_curve);
2966 if(tex->pd->falloff_curve) {
2967 direct_link_curvemapping(fd, tex->pd->falloff_curve);
2971 tex->vd= newdataadr(fd, tex->vd);
2973 tex->vd->dataset = NULL;
2977 tex->nodetree= newdataadr(fd, tex->nodetree);
2979 direct_link_nodetree(fd, tex->nodetree);
2981 tex->preview = direct_link_preview_image(fd, tex->preview);
2988 /* ************ READ MATERIAL ***************** */
2990 static void lib_link_material(FileData *fd, Main *main)
2996 ma= main->mat.first;
2998 if(ma->id.flag & LIB_NEEDLINK) {
2999 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
3001 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3002 of library blocks that implement this.*/
3003 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3005 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
3006 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
3008 for(a=0; a<MAX_MTEX; a++) {
3011 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
3012 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
3017 lib_link_ntree(fd, &ma->id, ma->nodetree);
3019 ma->id.flag -= LIB_NEEDLINK;
3025 static void direct_link_material(FileData *fd, Material *ma)
3029 ma->adt= newdataadr(fd, ma->adt);
3030 direct_link_animdata(fd, ma->adt);
3032 for(a=0; a<MAX_MTEX; a++) {
3033 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
3036 ma->ramp_col= newdataadr(fd, ma->ramp_col);
3037 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
3039 ma->nodetree= newdataadr(fd, ma->nodetree);
3041 direct_link_nodetree(fd, ma->nodetree);
3043 ma->preview = direct_link_preview_image(fd, ma->preview);
3044 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
3047 /* ************ READ PARTICLE SETTINGS ***************** */
3048 /* update this also to writefile.c */
3049 static const char *ptcache_data_struct[] = {
3050 "", // BPHYS_DATA_INDEX
3051 "", // BPHYS_DATA_LOCATION
3052 "", // BPHYS_DATA_VELOCITY
3053 "", // BPHYS_DATA_ROTATION
3054 "", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
3055 "", // BPHYS_DATA_SIZE:
3056 "", // BPHYS_DATA_TIMES:
3057 "BoidData" // case BPHYS_DATA_BOIDS:
3059 static void direct_link_pointcache(FileData *fd, PointCache *cache)
3061 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
3063 PTCacheExtra *extra;
3066 link_list(fd, &cache->mem_cache);
3068 pm = cache->mem_cache.first;
3070 for(; pm; pm=pm->next) {
3071 for(i=0; i<BPHYS_TOT_DATA; i++) {
3072 pm->data[i] = newdataadr(fd, pm->data[i]);
3074 /* the cache saves non-struct data without DNA */
3075 if(pm->data[i] && strcmp(ptcache_data_struct[i], "")==0 && (fd->flags & FD_FLAGS_SWITCH_ENDIAN)) {
3076 int j, tot= (BKE_ptcache_data_size (i) * pm->totpoint)/4; /* data_size returns bytes */
3077 int *poin= pm->data[i];
3079 for(j= 0; j<tot; j++)
3080 SWITCH_INT(poin[j]);
3084 link_list(fd, &pm->extradata);
3086 for(extra=pm->extradata.first; extra; extra=extra->next)
3087 extra->data = newdataadr(fd, extra->data);
3091 cache->mem_cache.first = cache->mem_cache.last = NULL;
3093 cache->flag &= ~PTCACHE_SIMULATION_VALID;
3096 cache->free_edit= NULL;
3097 cache->cached_frames= NULL;
3100 static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointCache **ocache, int force_disk)
3102 if(ptcaches->first) {
3103 PointCache *cache= NULL;
3104 link_list(fd, ptcaches);
3105 for(cache=ptcaches->first; cache; cache=cache->next) {
3106 direct_link_pointcache(fd, cache);
3108 cache->flag |= PTCACHE_DISK_CACHE;
3113 *ocache = newdataadr(fd, *ocache);
3116 /* old "single" caches need to be linked too */
3117 *ocache = newdataadr(fd, *ocache);
3118 direct_link_pointcache(fd, *ocache);
3120 (*ocache)->flag |= PTCACHE_DISK_CACHE;
3121 (*ocache)->step = 1;
3124 ptcaches->first = ptcaches->last = *ocache;
3128 static void lib_link_partdeflect(FileData *fd, ID *id, PartDeflect *pd)
3131 pd->tex=newlibadr_us(fd, id->lib, pd->tex);
3134 static void lib_link_particlesettings(FileData *fd, Main *main)
3136 ParticleSettings *part;
3137 ParticleDupliWeight *dw;
3141 part= main->particle.first;
3143 if(part->id.flag & LIB_NEEDLINK) {
3144 if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
3145 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
3147 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
3148 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
3149 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
3150 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
3152 lib_link_partdeflect(fd, &part->id, part->pd);
3153 lib_link_partdeflect(fd, &part->id, part->pd2);
3155 if(part->effector_weights)
3156 part->effector_weights->group = newlibadr(fd, part->id.lib, part->effector_weights->group);
3158 dw = part->dupliweights.first;
3159 for(; dw; dw=dw->next)
3160 dw->ob = newlibadr(fd, part->id.lib, dw->ob);
3163 BoidState *state = part->boids->states.first;
3165 for(; state; state=state->next) {
3166 rule = state->rules.first;
3167 for(; rule; rule=rule->next)
3168 switch(rule->type) {
3169 case eBoidRuleType_Goal:
3170 case eBoidRuleType_Avoid:
3172 BoidRuleGoalAvoid *brga = (BoidRuleGoalAvoid*)rule;
3173 brga->ob = newlibadr(fd, part->id.lib, brga->ob);
3176 case eBoidRuleType_FollowLeader:
3178 BoidRuleFollowLeader *brfl = (BoidRuleFollowLeader*)rule;
3179 brfl->ob = newlibadr(fd, part->id.lib, brfl->ob);
3186 for(a=0; a<MAX_MTEX; a++) {
3187 mtex= part->mtex[a];