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 *****
33 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
34 #include <stdlib.h> // for getenv atoi
35 #include <stddef.h> // for offsetof
36 #include <fcntl.h> // for open
37 #include <string.h> // for strrchr strncmp strstr
38 #include <math.h> // for fabs
41 #include <unistd.h> // for read close
43 #include <io.h> // for open close read
45 #include "BLI_winstuff.h"
48 #include "DNA_anim_types.h"
49 #include "DNA_armature_types.h"
50 #include "DNA_actuator_types.h"
51 #include "DNA_brush_types.h"
52 #include "DNA_camera_types.h"
53 #include "DNA_cloth_types.h"
54 #include "DNA_controller_types.h"
55 #include "DNA_constraint_types.h"
56 #include "DNA_effect_types.h"
57 #include "DNA_fileglobal_types.h"
58 #include "DNA_genfile.h"
59 #include "DNA_group_types.h"
60 #include "DNA_gpencil_types.h"
61 #include "DNA_ipo_types.h"
62 #include "DNA_key_types.h"
63 #include "DNA_lattice_types.h"
64 #include "DNA_lamp_types.h"
65 #include "DNA_meta_types.h"
66 #include "DNA_material_types.h"
67 #include "DNA_mesh_types.h"
68 #include "DNA_meshdata_types.h"
69 #include "DNA_nla_types.h"
70 #include "DNA_node_types.h"
71 #include "DNA_object_fluidsim.h" // NT
72 #include "DNA_packedFile_types.h"
73 #include "DNA_property_types.h"
74 #include "DNA_text_types.h"
75 #include "DNA_view3d_types.h"
76 #include "DNA_screen_types.h"
77 #include "DNA_sensor_types.h"
78 #include "DNA_sdna_types.h"
79 #include "DNA_scene_types.h"
80 #include "DNA_sequence_types.h"
81 #include "DNA_smoke_types.h"
82 #include "DNA_sound_types.h"
83 #include "DNA_space_types.h"
84 #include "DNA_vfont_types.h"
85 #include "DNA_world_types.h"
87 #include "MEM_guardedalloc.h"
89 #include "BLI_blenlib.h"
91 #include "BLI_utildefines.h"
94 #include "BKE_action.h"
95 #include "BKE_armature.h"
96 #include "BKE_brush.h"
97 #include "BKE_colortools.h"
98 #include "BKE_constraint.h"
99 #include "BKE_context.h"
100 #include "BKE_curve.h"
101 #include "BKE_deform.h"
102 #include "BKE_effect.h" /* give_parteff */
103 #include "BKE_fcurve.h"
104 #include "BKE_global.h" // for G
105 #include "BKE_group.h"
106 #include "BKE_image.h"
107 #include "BKE_lattice.h"
108 #include "BKE_library.h" // for which_libbase
109 #include "BKE_idcode.h"
110 #include "BKE_material.h"
111 #include "BKE_main.h" // for Main
112 #include "BKE_mesh.h" // for ME_ defines (patching)
113 #include "BKE_modifier.h"
114 #include "BKE_multires.h"
115 #include "BKE_node.h" // for tree type defines
116 #include "BKE_object.h"
117 #include "BKE_paint.h"
118 #include "BKE_particle.h"
119 #include "BKE_pointcache.h"
120 #include "BKE_property.h" // for get_ob_property
121 #include "BKE_report.h"
122 #include "BKE_sca.h" // for init_actuator
123 #include "BKE_scene.h"
124 #include "BKE_screen.h"
125 #include "BKE_sequencer.h"
126 #include "BKE_texture.h" // for open_plugin_tex
127 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
128 #include "BKE_sound.h"
130 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
131 //XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
132 //XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
133 #include "BLO_readfile.h"
134 #include "BLO_undofile.h"
135 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
137 #include "readfile.h"
139 #include "PIL_time.h"
144 Remark: still a weak point is the newaddress() function, that doesnt solve reading from
145 multiple files at the same time
147 (added remark: oh, i thought that was solved? will look at that... (ton)
150 - Existing Library (Main) push or free
160 - read associated 'direct data'
161 - link direct data (internal and to LibBlock)
163 - read USER data, only when indicated (file is ~/X.XX/startup.blend)
165 - per Library (per Main)
168 - find LibBlocks and attach IDs to Main
169 - if external LibBlock
171 - or it's already read,
176 - read associated direct data
177 - link direct data (internal and to LibBlock)
179 - per Library with unread LibBlocks
184 - read associated direct data
185 - link direct data (internal and to LibBlock)
188 - link all LibBlocks and indirect pointers to libblocks
189 - initialize FileGlobal and copy pointers to Global
192 /* also occurs in library.c */
193 /* GS reads the memory pointed at in a specific ordering. There are,
194 * however two definitions for it. I have jotted them down here, both,
195 * but I think the first one is actually used. The thing is that
196 * big-endian systems might read this the wrong way round. OTOH, we
197 * constructed the IDs that are read out with this macro explicitly as
198 * well. I expect we'll sort it out soon... */
201 #define GS(a) (*((short *)(a)))
203 /* from misc_util: flip the bytes from x */
204 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
206 // only used here in readfile.c
207 #define SWITCH_LONGINT(a) { \
210 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
211 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
212 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
213 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
217 typedef struct OldNew {
222 typedef struct OldNewMap {
224 int nentries, entriessize;
230 /* local prototypes */
231 static void *read_struct(FileData *fd, BHead *bh, const char *blockname);
234 static OldNewMap *oldnewmap_new(void)
236 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
238 onm->entriessize= 1024;
239 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
244 static int verg_oldnewmap(const void *v1, const void *v2)
246 const struct OldNew *x1=v1, *x2=v2;
248 if( x1->old > x2->old) return 1;
249 else if( x1->old < x2->old) return -1;
254 static void oldnewmap_sort(FileData *fd)
256 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
257 fd->libmap->sorted= 1;
260 /* nr is zero for data, and ID code for libdata */
261 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
265 if(oldaddr==NULL || newaddr==NULL) return;
267 if (onm->nentries==onm->entriessize) {
268 int osize= onm->entriessize;
269 OldNew *oentries= onm->entries;
271 onm->entriessize*= 2;
272 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
274 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
278 entry= &onm->entries[onm->nentries++];
280 entry->newp= newaddr;
284 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
288 if (onm->lasthit<onm->nentries-1) {
289 OldNew *entry= &onm->entries[++onm->lasthit];
291 if (entry->old==addr) {
297 for (i=0; i<onm->nentries; i++) {
298 OldNew *entry= &onm->entries[i];
300 if (entry->old==addr) {
311 /* for libdata, nr has ID code, no increment */
312 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
316 if(addr==NULL) return NULL;
318 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
320 OldNew entry_s, *entry;
324 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
328 if (id && (!lib || id->lib)) {
334 for (i=0; i<onm->nentries; i++) {
335 OldNew *entry= &onm->entries[i];
337 if (entry->old==addr) {
340 if (id && (!lib || id->lib)) {
349 static void oldnewmap_free_unused(OldNewMap *onm)
353 for (i=0; i<onm->nentries; i++) {
354 OldNew *entry= &onm->entries[i];
356 MEM_freeN(entry->newp);
362 static void oldnewmap_clear(OldNewMap *onm)
368 static void oldnewmap_free(OldNewMap *onm)
370 MEM_freeN(onm->entries);
376 static void read_libraries(FileData *basefd, ListBase *mainlist);
378 /* ************ help functions ***************** */
380 static void add_main_to_main(Main *mainvar, Main *from)
382 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
385 set_listbasepointers(mainvar, lbarray);
386 a= set_listbasepointers(from, fromarray);
388 BLI_movelisttolist(lbarray[a], fromarray[a]);
392 void blo_join_main(ListBase *mainlist)
394 Main *tojoin, *mainl;
397 mainl= mainlist->first;
398 while ((tojoin= mainl->next)) {
399 add_main_to_main(mainl, tojoin);
400 BLI_remlink(mainlist, tojoin);
405 static void split_libdata(ListBase *lb, Main *first)
417 if(mainvar->curlib==id->lib) {
418 lbn= which_libbase(mainvar, GS(id->name));
420 BLI_addtail(lbn, id);
423 mainvar= mainvar->next;
425 if(mainvar==0) printf("error split_libdata\n");
431 void blo_split_main(ListBase *mainlist, Main *main)
433 ListBase *lbarray[MAX_LIBARRAY];
437 mainlist->first= mainlist->last= main;
440 if(main->library.first==NULL)
443 for (lib= main->library.first; lib; lib= lib->id.next) {
444 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
445 libmain->curlib= lib;
446 BLI_addtail(mainlist, libmain);
449 i= set_listbasepointers(main, lbarray);
451 split_libdata(lbarray[i], main->next);
454 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
455 static void cleanup_path(const char *relabase, char *name)
457 char filename[FILE_MAXFILE];
459 BLI_splitdirstring(name, filename);
460 BLI_cleanup_dir(relabase, name);
461 strcat(name, filename);
464 static void read_file_version(FileData *fd, Main *main)
468 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
469 if (bhead->code==GLOB) {
470 FileGlobal *fg= read_struct(fd, bhead, "Global");
472 main->subversionfile= fg->subversion;
473 main->minversionfile= fg->minversion;
474 main->minsubversionfile= fg->minsubversion;
477 else if (bhead->code==ENDB)
484 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *name, const char *relabase)
488 char name1[FILE_MAXDIR+FILE_MAXFILE];
490 strncpy(name1, name, sizeof(name1)-1);
491 cleanup_path(relabase, name1);
492 // printf("blo_find_main: original in %s\n", name);
493 // printf("blo_find_main: converted to %s\n", name1);
495 for (m= mainlist->first; m; m= m->next) {
496 char *libname= (m->curlib)?m->curlib->filepath:m->name;
498 if (BLI_streq(name1, libname)) {
499 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
504 m= MEM_callocN(sizeof(Main), "find_main");
505 BLI_addtail(mainlist, m);
507 lib= alloc_libblock(&m->library, ID_LI, "lib");
508 strncpy(lib->name, name, sizeof(lib->name)-1);
509 BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
513 read_file_version(fd, m);
515 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", name);
520 /* ************ FILE PARSING ****************** */
522 static void switch_endian_bh4(BHead4 *bhead)
524 /* the ID_.. codes */
525 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
527 if (bhead->code != ENDB) {
528 SWITCH_INT(bhead->len);
529 SWITCH_INT(bhead->SDNAnr);
530 SWITCH_INT(bhead->nr);
534 static void switch_endian_bh8(BHead8 *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 bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
548 BHead4 *bhead4 = (BHead4 *) bhead;
549 #if defined(WIN32) && !defined(FREE_WINDOWS)
555 bhead4->code= bhead8->code;
556 bhead4->len= bhead8->len;
558 if (bhead4->code != ENDB) {
560 //perform a endian swap on 64bit pointers, otherwise the pointer might map to zero
561 //0x0000000000000000000012345678 would become 0x12345678000000000000000000000000
562 if (do_endian_swap) {
563 SWITCH_LONGINT(bhead8->old);
566 /* this patch is to avoid a long long being read from not-eight aligned positions
567 is necessary on any modern 64bit architecture) */
568 memcpy(&old, &bhead8->old, 8);
569 bhead4->old = (int) (old >> 3);
571 bhead4->SDNAnr= bhead8->SDNAnr;
572 bhead4->nr= bhead8->nr;
576 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
578 BHead8 *bhead8 = (BHead8 *) bhead;
580 bhead8->code= bhead4->code;
581 bhead8->len= bhead4->len;
583 if (bhead8->code != ENDB) {
584 bhead8->old= bhead4->old;
585 bhead8->SDNAnr= bhead4->SDNAnr;
586 bhead8->nr= bhead4->nr;
590 static BHeadN *get_bhead(FileData *fd)
592 BHeadN *new_bhead = 0;
597 /* initializing to zero isn't strictly needed but shuts valgrind up
598 * since uninitialized memory gets compared */
603 // First read the bhead structure.
604 // Depending on the platform the file was written on this can
605 // be a big or little endian BHead4 or BHead8 structure.
607 // As usual 'ENDB' (the last *partial* bhead of the file)
608 // needs some special handling. We don't want to EOF just yet.
610 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
612 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
614 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
615 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
616 switch_endian_bh4(&bhead4);
619 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
620 bh8_from_bh4(&bhead, &bhead4);
622 memcpy(&bhead, &bhead4, sizeof(bhead));
630 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
632 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
633 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
634 switch_endian_bh8(&bhead8);
637 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
638 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
640 memcpy(&bhead, &bhead8, sizeof(bhead));
648 /* make sure people are not trying to pass bad blend files */
649 if (bhead.len < 0) fd->eof = 1;
651 // bhead now contains the (converted) bhead structure. Now read
652 // the associated data and put everything in a BHeadN (creative naming !)
655 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
657 new_bhead->next = new_bhead->prev = 0;
658 new_bhead->bhead = bhead;
660 readsize = fd->read(fd, new_bhead + 1, bhead.len);
662 if (readsize != bhead.len) {
664 MEM_freeN(new_bhead);
674 // We've read a new block. Now add it to the list
678 BLI_addtail(&fd->listbase, new_bhead);
684 BHead *blo_firstbhead(FileData *fd)
690 // Read in a new block if necessary
692 new_bhead = fd->listbase.first;
693 if (new_bhead == 0) {
694 new_bhead = get_bhead(fd);
698 bhead = &new_bhead->bhead;
704 BHead *blo_prevbhead(FileData *UNUSED(fd), BHead *thisblock)
706 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
707 BHeadN *prev= bheadn->prev;
709 return prev?&prev->bhead:NULL;
712 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
714 BHeadN *new_bhead = NULL;
718 // bhead is actually a sub part of BHeadN
719 // We calculate the BHeadN pointer from the BHead pointer below
720 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
722 // get the next BHeadN. If it doesn't exist we read in the next one
723 new_bhead = new_bhead->next;
724 if (new_bhead == 0) {
725 new_bhead = get_bhead(fd);
730 // here we do the reverse:
731 // go from the BHeadN pointer to the BHead pointer
732 bhead = &new_bhead->bhead;
738 static void decode_blender_header(FileData *fd)
740 char header[SIZEOFBLENDERHEADER], num[4];
743 // read in the header data
744 readsize = fd->read(fd, header, sizeof(header));
746 if (readsize == sizeof(header)) {
747 if(strncmp(header, "BLENDER", 7) == 0) {
748 int remove_this_endian_test= 1;
750 fd->flags |= FD_FLAGS_FILE_OK;
752 // what size are pointers in the file ?
754 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
755 if (sizeof(void *) != 4) {
756 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
759 if (sizeof(void *) != 8) {
760 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
764 // is the file saved in a different endian
766 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
767 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
770 // get the version number
772 memcpy(num, header+9, 3);
774 fd->fileversion = atoi(num);
779 static int read_file_dna(FileData *fd)
783 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
784 if (bhead->code==DNA1) {
785 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
787 fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
790 fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
791 /* used to retrieve ID names from (bhead+1) */
792 fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
796 } else if (bhead->code==ENDB)
803 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
805 int readsize = read(filedata->filedes, buffer, size);
810 filedata->seek += readsize;
816 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
818 int readsize = gzread(filedata->gzfiledes, buffer, size);
823 filedata->seek += readsize;
829 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
831 // don't read more bytes then there are available in the buffer
832 int readsize = (int)MIN2(size, (unsigned int)(filedata->buffersize - filedata->seek));
834 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
835 filedata->seek += readsize;
840 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
842 static unsigned int seek= 1<<30; /* the current position */
843 static unsigned int offset= 0; /* size of previous chunks */
844 static MemFileChunk *chunk=NULL;
845 unsigned int chunkoffset, readsize, totread;
847 if(size==0) return 0;
849 if(seek != (unsigned int)filedata->seek) {
850 chunk= filedata->memfile->chunks.first;
854 if(seek + chunk->size > (unsigned) filedata->seek) break;
859 seek= filedata->seek;
866 /* first check if it's on the end if current chunk */
867 if(seek-offset == chunk->size) {
868 offset+= chunk->size;
872 /* debug, should never happen */
874 printf("illegal read, chunk zero\n");
878 chunkoffset= seek-offset;
879 readsize= size-totread;
881 /* data can be spread over multiple chunks, so clamp size
882 * to within this chunk, and then it will read further in
884 if(chunkoffset+readsize > chunk->size)
885 readsize= chunk->size-chunkoffset;
887 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
889 filedata->seek += readsize;
891 } while(totread < size);
899 static FileData *filedata_new(void)
901 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
904 fd->gzfiledes = NULL;
906 /* XXX, this doesn't need to be done all the time,
907 * but it keeps us reentrant, remove once we have
908 * a lib that provides a nice lock. - zr
910 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
912 fd->datamap = oldnewmap_new();
913 fd->globmap = oldnewmap_new();
914 fd->libmap = oldnewmap_new();
919 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
921 decode_blender_header(fd);
923 if (fd->flags & FD_FLAGS_FILE_OK) {
924 if (!read_file_dna(fd)) {
925 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", incomplete", fd->relabase);
926 blo_freefiledata(fd);
931 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", not a blend file", fd->relabase);
932 blo_freefiledata(fd);
939 /* cannot be called with relative paths anymore! */
940 /* on each new library added, it now checks for the current FileData and expands relativeness */
941 FileData *blo_openblenderfile(const char *name, ReportList *reports)
945 gzfile= gzopen(name, "rb");
947 if (gzfile == Z_NULL) {
948 BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", name, errno ? strerror(errno) : "Unknown erro reading file");
951 FileData *fd = filedata_new();
952 fd->gzfiledes = gzfile;
953 fd->read = fd_read_gzip_from_file;
955 /* needed for library_append and read_libraries */
956 BLI_strncpy(fd->relabase, name, sizeof(fd->relabase));
958 return blo_decode_and_check(fd, reports);
962 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
964 if (!mem || memsize<SIZEOFBLENDERHEADER) {
965 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
968 FileData *fd= filedata_new();
970 fd->buffersize= memsize;
971 fd->read= fd_read_from_memory;
972 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
974 return blo_decode_and_check(fd, reports);
978 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
981 BKE_report(reports, RPT_ERROR, "Unable to open blend <memory>");
984 FileData *fd= filedata_new();
985 fd->memfile= memfile;
987 fd->read= fd_read_from_memfile;
988 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
990 return blo_decode_and_check(fd, reports);
995 void blo_freefiledata(FileData *fd)
999 if (fd->filedes != -1) {
1003 if (fd->gzfiledes != NULL)
1005 gzclose(fd->gzfiledes);
1008 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1009 MEM_freeN(fd->buffer);
1013 // Free all BHeadN data blocks
1014 BLI_freelistN(&fd->listbase);
1017 DNA_sdna_free(fd->memsdna);
1019 DNA_sdna_free(fd->filesdna);
1021 MEM_freeN(fd->compflags);
1024 oldnewmap_free(fd->datamap);
1026 oldnewmap_free(fd->globmap);
1028 oldnewmap_free(fd->imamap);
1029 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1030 oldnewmap_free(fd->libmap);
1032 MEM_freeN(fd->bheadmap);
1038 /* ************ DIV ****************** */
1040 int BLO_has_bfile_extension(char *str)
1042 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend") || BLI_testextensie(str, ".blend.gz"));
1045 int BLO_is_a_library(const char *path, char *dir, char *group)
1047 /* return ok when a blenderfile, in dir is the filename,
1048 * in group the type of libdata
1056 if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
1061 /* Find the last slash */
1062 fd= BLI_last_slash(dir);
1066 if(BLO_has_bfile_extension(fd+1)) {
1067 /* the last part of the dir is a .blend file, no group follows */
1068 *fd= '/'; /* put back the removed slash separating the dir and the .blend file name */
1071 char *gp = fd+1; // in case we have a .blend file, gp points to the group
1073 /* Find the last slash */
1074 fd= BLI_last_slash(dir);
1075 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
1077 /* now we know that we are in a blend file and it is safe to
1078 assume that gp actually points to a group */
1079 if (BLI_streq("Screen", gp)==0)
1080 BLI_strncpy(group, gp, GROUP_MAX);
1085 /* ************** OLD POINTERS ******************* */
1087 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1089 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1092 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1094 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1097 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1099 if(fd->imamap && adr)
1100 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1105 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1107 return oldnewmap_liblookup(fd->libmap, adr, lib);
1110 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1112 ID *id= newlibadr(fd, lib, adr);
1120 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1124 for (i=0; i<fd->libmap->nentries; i++) {
1125 OldNew *entry= &fd->libmap->entries[i];
1127 if (old==entry->newp && entry->nr==ID_ID) {
1129 if(new) entry->nr= GS( ((ID *)new)->name );
1135 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1139 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1142 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1146 change_idid_adr_fd(fd, old, new);
1151 /* lib linked proxy objects point to our local data, we need
1152 * to clear that pointer before reading the undo memfile since
1153 * the object might be removed, it is set again in reading
1154 * if the local object still exists */
1155 void blo_clear_proxy_pointers_from_lib(Main *oldmain)
1157 Object *ob= oldmain->object.first;
1159 for(;ob; ob= ob->id.next)
1161 ob->proxy_from= NULL;
1164 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1166 Image *ima= oldmain->image.first;
1167 Scene *sce= oldmain->scene.first;
1170 fd->imamap= oldnewmap_new();
1172 for(;ima; ima= ima->id.next) {
1173 Link *ibuf= ima->ibufs.first;
1174 for(; ibuf; ibuf= ibuf->next)
1175 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1177 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1178 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
1180 oldnewmap_insert(fd->imamap, ima->renders[a], ima->renders[a], 0);
1182 for(; sce; sce= sce->id.next) {
1185 for(node= sce->nodetree->nodes.first; node; node= node->next)
1186 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1191 /* set old main image ibufs to zero if it has been restored */
1192 /* this works because freeing old main only happens after this call */
1193 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1195 OldNew *entry= fd->imamap->entries;
1196 Image *ima= oldmain->image.first;
1197 Scene *sce= oldmain->scene.first;
1200 /* used entries were restored, so we put them to zero */
1201 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1206 for(;ima; ima= ima->id.next) {
1209 /* this mirrors direct_link_image */
1210 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1212 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1213 BLI_remlink(&ima->ibufs, ibuf);
1215 ima->gputexture= NULL;
1219 ima->gputexture= newimaadr(fd, ima->gputexture);
1220 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
1221 ima->renders[a]= newimaadr(fd, ima->renders[a]);
1223 for(; sce; sce= sce->id.next) {
1226 for(node= sce->nodetree->nodes.first; node; node= node->next)
1227 node->preview= newimaadr(fd, node->preview);
1232 /* undo file support: add all library pointers in lookup */
1233 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1235 Main *ptr= mainlist->first;
1236 ListBase *lbarray[MAX_LIBARRAY];
1238 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1239 int i= set_listbasepointers(ptr, lbarray);
1242 for(id= lbarray[i]->first; id; id= id->next)
1243 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1249 /* ********** END OLD POINTERS ****************** */
1250 /* ********** READ FILE ****************** */
1252 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1254 int blocksize, nblocks;
1257 data= (char *)(bhead+1);
1258 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1262 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1268 static void *read_struct(FileData *fd, BHead *bh, const char *blockname)
1273 /* switch is based on file dna */
1274 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1275 switch_endian_structs(fd->filesdna, bh);
1277 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1278 if(fd->compflags[bh->SDNAnr]==2) {
1279 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1281 temp= MEM_mallocN(bh->len, blockname);
1282 memcpy(temp, (bh+1), bh->len);
1290 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1294 if(lb->first==NULL) return;
1296 lb->first= newdataadr(fd, lb->first);
1300 ln->next= newdataadr(fd, ln->next);
1308 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1313 if(lb->first==0) return;
1314 poin= newdataadr(fd, lb->first);
1316 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1323 poin= newdataadr(fd, ln->next);
1325 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1335 static void test_pointer_array(FileData *fd, void **mat)
1337 #if defined(WIN32) && !defined(FREE_WINDOWS)
1338 __int64 *lpoin, *lmat;
1340 long long *lpoin, *lmat;
1345 /* manually convert the pointer array in
1346 * the old dna format to a pointer array in
1347 * the new dna format.
1350 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1352 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1353 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1357 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1358 SWITCH_LONGINT(*lpoin);
1359 *ipoin= (int) ((*lpoin) >> 3);
1367 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1368 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1382 /* ************ READ ID Properties *************** */
1384 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1385 static void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1387 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1392 /*since we didn't save the extra buffer, set totallen to len.*/
1393 prop->totallen = prop->len;
1394 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1396 array= (IDProperty*) prop->data.pointer;
1398 /* note!, idp-arrays didn't exist in 2.4x, so the pointer will be cleared
1399 * theres not really anything we can do to correct this, at least dont crash */
1406 for(i=0; i<prop->len; i++)
1407 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
1410 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1415 /*since we didn't save the extra buffer, set totallen to len.*/
1416 prop->totallen = prop->len;
1417 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1419 if(prop->subtype == IDP_GROUP) {
1420 test_pointer_array(fd, prop->data.pointer);
1421 array= prop->data.pointer;
1423 for(i=0; i<prop->len; i++)
1424 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1426 else if(prop->subtype == IDP_DOUBLE) {
1427 if (switch_endian) {
1428 for (i=0; i<prop->len; i++) {
1429 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1434 if (switch_endian) {
1435 for (i=0; i<prop->len; i++) {
1436 SWITCH_INT(((int*)prop->data.pointer)[i]);
1442 static void IDP_DirectLinkString(IDProperty *prop, FileData *fd)
1444 /*since we didn't save the extra string buffer, set totallen to len.*/
1445 prop->totallen = prop->len;
1446 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1449 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1451 ListBase *lb = &prop->data.group;
1456 /*Link child id properties now*/
1457 for (loop=prop->data.group.first; loop; loop=loop->next) {
1458 IDP_DirectLinkProperty(loop, switch_endian, fd);
1462 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1464 switch (prop->type) {
1466 IDP_DirectLinkGroup(prop, switch_endian, fd);
1469 IDP_DirectLinkString(prop, fd);
1472 IDP_DirectLinkArray(prop, switch_endian, fd);
1475 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1478 /*erg, stupid doubles. since I'm storing them
1479 in the same field as int val; val2 in the
1480 IDPropertyData struct, they have to deal with
1481 endianness specifically
1483 in theory, val and val2 would've already been swapped
1484 if switch_endian is true, so we have to first unswap
1485 them then reswap them as a single 64-bit entity.
1488 if (switch_endian) {
1489 SWITCH_INT(prop->data.val);
1490 SWITCH_INT(prop->data.val2);
1491 SWITCH_LONGINT(prop->data.val);
1499 void IDP_LibLinkProperty(IDProperty *UNUSED(prop), int UNUSED(switch_endian), FileData *UNUSED(fd))
1503 /* ************ READ CurveMapping *************** */
1505 /* cuma itself has been read! */
1506 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1510 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1511 cumap->flag &= ~CUMA_PREMULLED;
1513 for(a=0; a<CM_TOT; a++) {
1514 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1515 cumap->cm[a].table= NULL;
1516 cumap->cm[a].premultable= NULL;
1520 /* ************ READ Brush *************** */
1521 /* library brush linking after fileread */
1522 static void lib_link_brush(FileData *fd, Main *main)
1526 /* only link ID pointers */
1527 for(brush= main->brush.first; brush; brush= brush->id.next) {
1528 if(brush->id.flag & LIB_NEEDLINK) {
1529 brush->id.flag -= LIB_NEEDLINK;
1531 brush->mtex.tex= newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
1532 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1537 static void direct_link_brush(FileData *fd, Brush *brush)
1539 /* brush itself has been read */
1542 brush->curve= newdataadr(fd, brush->curve);
1544 direct_link_curvemapping(fd, brush->curve);
1546 brush_curve_preset(brush, CURVE_PRESET_SHARP);
1548 brush->preview= NULL;
1549 brush->icon_imbuf= NULL;
1552 static void direct_link_script(FileData *UNUSED(fd), Script *script)
1555 SCRIPT_SET_NULL(script)
1559 /* ************ READ PACKEDFILE *************** */
1561 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1563 PackedFile *pf= newdataadr(fd, oldpf);
1566 pf->data= newdataadr(fd, pf->data);
1572 /* ************ READ IMAGE PREVIEW *************** */
1574 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1576 PreviewImage *prv= newdataadr(fd, old_prv);
1580 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1582 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1590 /* ************ READ ANIMATION STUFF ***************** */
1592 /* Legacy Data Support (for Version Patching) ----------------------------- */
1594 // XXX depreceated - old animation system
1595 static void lib_link_ipo(FileData *fd, Main *main)
1599 ipo= main->ipo.first;
1601 if(ipo->id.flag & LIB_NEEDLINK) {
1603 for(icu= ipo->curve.first; icu; icu= icu->next) {
1605 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1607 ipo->id.flag -= LIB_NEEDLINK;
1613 // XXX depreceated - old animation system
1614 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1618 link_list(fd, &(ipo->curve));
1619 icu= ipo->curve.first;
1621 icu->bezt= newdataadr(fd, icu->bezt);
1622 icu->bp= newdataadr(fd, icu->bp);
1623 icu->driver= newdataadr(fd, icu->driver);
1628 // XXX depreceated - old animation system
1629 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1631 bActionStrip *strip;
1632 bActionModifier *amod;
1634 for (strip=striplist->first; strip; strip=strip->next){
1635 strip->object = newlibadr(fd, id->lib, strip->object);
1636 strip->act = newlibadr_us(fd, id->lib, strip->act);
1637 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1638 for(amod= strip->modifiers.first; amod; amod= amod->next)
1639 amod->ob= newlibadr(fd, id->lib, amod->ob);
1643 // XXX depreceated - old animation system
1644 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1646 bActionStrip *strip;
1648 link_list(fd, strips);
1650 for(strip= strips->first; strip; strip= strip->next)
1651 link_list(fd, &strip->modifiers);
1654 // XXX depreceated - old animation system
1655 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1657 bConstraintChannel *chan;
1659 for (chan=chanbase->first; chan; chan=chan->next){
1660 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1664 /* Data Linking ----------------------------- */
1666 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1670 for (fcm= list->first; fcm; fcm= fcm->next) {
1671 /* data for specific modifiers */
1672 switch (fcm->type) {
1673 case FMODIFIER_TYPE_PYTHON:
1675 FMod_Python *data= (FMod_Python *)fcm->data;
1676 data->script = newlibadr(fd, id->lib, data->script);
1683 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1690 /* relink ID-block references... */
1691 for (fcu= list->first; fcu; fcu= fcu->next) {
1694 ChannelDriver *driver= fcu->driver;
1697 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1698 DRIVER_TARGETS_LOOPER(dvar)
1700 /* only relink if still used */
1701 if (tarIndex < dvar->num_targets)
1702 dtar->id= newlibadr(fd, id->lib, dtar->id);
1706 DRIVER_TARGETS_LOOPER_END
1711 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1716 /* NOTE: this assumes that link_list has already been called on the list */
1717 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1721 for (fcm= list->first; fcm; fcm= fcm->next) {
1722 /* relink general data */
1723 fcm->data = newdataadr(fd, fcm->data);
1726 /* do relinking of data for specific types */
1727 switch (fcm->type) {
1728 case FMODIFIER_TYPE_GENERATOR:
1730 FMod_Generator *data= (FMod_Generator *)fcm->data;
1732 data->coefficients= newdataadr(fd, data->coefficients);
1735 case FMODIFIER_TYPE_ENVELOPE:
1737 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1739 data->data= newdataadr(fd, data->data);
1742 case FMODIFIER_TYPE_PYTHON:
1744 FMod_Python *data= (FMod_Python *)fcm->data;
1746 data->prop = newdataadr(fd, data->prop);
1747 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1754 /* NOTE: this assumes that link_list has already been called on the list */
1755 static void direct_link_fcurves(FileData *fd, ListBase *list)
1759 /* link F-Curve data to F-Curve again (non ID-libs) */
1760 for (fcu= list->first; fcu; fcu= fcu->next) {
1762 fcu->bezt= newdataadr(fd, fcu->bezt);
1763 fcu->fpt= newdataadr(fd, fcu->fpt);
1766 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1769 fcu->grp= newdataadr(fd, fcu->grp);
1772 fcu->driver= newdataadr(fd, fcu->driver);
1774 ChannelDriver *driver= fcu->driver;
1777 driver->expr_comp= NULL;
1779 /* relink variables, targets and their paths */
1780 link_list(fd, &driver->variables);
1781 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1782 DRIVER_TARGETS_LOOPER(dvar)
1784 /* only relink the targets being used */
1785 if (tarIndex < dvar->num_targets)
1786 dtar->rna_path= newdataadr(fd, dtar->rna_path);
1788 dtar->rna_path= NULL;
1790 DRIVER_TARGETS_LOOPER_END
1795 link_list(fd, &fcu->modifiers);
1796 direct_link_fmodifiers(fd, &fcu->modifiers);
1801 static void lib_link_action(FileData *fd, Main *main)
1804 bActionChannel *chan;
1806 for (act= main->action.first; act; act= act->id.next) {
1807 if (act->id.flag & LIB_NEEDLINK) {
1808 act->id.flag -= LIB_NEEDLINK;
1810 // XXX depreceated - old animation system <<<
1811 for (chan=act->chanbase.first; chan; chan=chan->next) {
1812 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1813 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1815 // >>> XXX depreceated - old animation system
1817 lib_link_fcurves(fd, &act->id, &act->curves);
1822 static void direct_link_action(FileData *fd, bAction *act)
1824 bActionChannel *achan; // XXX depreceated - old animation system
1827 link_list(fd, &act->curves);
1828 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1829 link_list(fd, &act->groups);
1830 link_list(fd, &act->markers);
1832 // XXX depreceated - old animation system <<<
1833 for (achan = act->chanbase.first; achan; achan=achan->next) {
1834 achan->grp= newdataadr(fd, achan->grp);
1836 link_list(fd, &achan->constraintChannels);
1838 // >>> XXX depreceated - old animation system
1840 direct_link_fcurves(fd, &act->curves);
1842 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1843 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1844 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1848 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
1852 for (strip= list->first; strip; strip= strip->next) {
1853 /* check strip's children */
1854 lib_link_nladata_strips(fd, id, &strip->strips);
1856 /* check strip's F-Curves */
1857 lib_link_fcurves(fd, id, &strip->fcurves);
1859 /* reassign the counted-reference to action */
1860 strip->act = newlibadr_us(fd, id->lib, strip->act);
1864 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
1868 /* we only care about the NLA strips inside the tracks */
1869 for (nlt= list->first; nlt; nlt= nlt->next) {
1870 lib_link_nladata_strips(fd, id, &nlt->strips);
1874 /* This handles Animato NLA-Strips linking
1875 * NOTE: this assumes that link_list has already been called on the list
1877 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
1881 for (strip= list->first; strip; strip= strip->next) {
1882 /* strip's child strips */
1883 link_list(fd, &strip->strips);
1884 direct_link_nladata_strips(fd, &strip->strips);
1886 /* strip's F-Curves */
1887 link_list(fd, &strip->fcurves);
1888 direct_link_fcurves(fd, &strip->fcurves);
1890 /* strip's F-Modifiers */
1891 link_list(fd, &strip->modifiers);
1892 direct_link_fcurves(fd, &strip->modifiers);
1896 /* NOTE: this assumes that link_list has already been called on the list */
1897 static void direct_link_nladata(FileData *fd, ListBase *list)
1901 for (nlt= list->first; nlt; nlt= nlt->next) {
1902 /* relink list of strips */
1903 link_list(fd, &nlt->strips);
1905 /* relink strip data */
1906 direct_link_nladata_strips(fd, &nlt->strips);
1912 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
1917 /* here, we're only interested in the ID pointer stored in some of the paths */
1918 for (ks= list->first; ks; ks= ks->next) {
1919 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1920 ksp->id= newlibadr(fd, id->lib, ksp->id);
1925 /* NOTE: this assumes that link_list has already been called on the list */
1926 static void direct_link_keyingsets(FileData *fd, ListBase *list)
1931 /* link KeyingSet data to KeyingSet again (non ID-libs) */
1932 for (ks= list->first; ks; ks= ks->next) {
1934 link_list(fd, &ks->paths);
1936 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1938 ksp->rna_path= newdataadr(fd, ksp->rna_path);
1945 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
1950 /* link action data */
1951 adt->action= newlibadr_us(fd, id->lib, adt->action);
1952 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
1955 lib_link_fcurves(fd, id, &adt->drivers);
1957 /* overrides don't have lib-link for now, so no need to do anything */
1960 lib_link_nladata(fd, id, &adt->nla_tracks);
1963 static void direct_link_animdata(FileData *fd, AnimData *adt)
1965 /* NOTE: must have called newdataadr already before doing this... */
1970 link_list(fd, &adt->drivers);
1971 direct_link_fcurves(fd, &adt->drivers);
1973 /* link overrides */
1977 link_list(fd, &adt->nla_tracks);
1978 direct_link_nladata(fd, &adt->nla_tracks);
1980 /* relink active strip - even though strictly speaking this should only be used
1981 * if we're in 'tweaking mode', we need to be able to have this loaded back for
1982 * undo, but also since users may not exit tweakmode before saving (#24535)
1984 // TODO: it's not really nice that anyone should be able to save the file in this
1985 // state, but it's going to be too hard to enforce this single case...
1986 adt->actstrip= newdataadr(fd, adt->actstrip);
1989 /* ************ READ MOTION PATHS *************** */
1991 /* direct data for cache */
1992 static void direct_link_motionpath(FileData *fd, bMotionPath *mpath)
1998 /* relink points cache */
1999 mpath->points= newdataadr(fd, mpath->points);
2002 /* ************ READ NODE TREE *************** */
2004 /* singe node tree (also used for material/scene trees), ntree is not NULL */
2005 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
2009 if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
2011 ntree->gpd= newlibadr_us(fd, id->lib, ntree->gpd);
2013 for(node= ntree->nodes.first; node; node= node->next)
2014 node->id= newlibadr_us(fd, id->lib, node->id);
2017 /* library ntree linking after fileread */
2018 static void lib_link_nodetree(FileData *fd, Main *main)
2022 /* only link ID pointers */
2023 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2024 if(ntree->id.flag & LIB_NEEDLINK) {
2025 ntree->id.flag -= LIB_NEEDLINK;
2026 lib_link_ntree(fd, &ntree->id, ntree);
2031 /* verify types for nodes and groups, all data has to be read */
2032 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
2034 static void lib_verify_nodetree(Main *main, int UNUSED(open))
2041 /* this crashes blender on undo/redo
2043 reinit_nodesystem();
2046 /* now create the own typeinfo structs an verify nodes */
2047 /* here we still assume no groups in groups */
2048 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2049 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
2050 ntreeMakeOwnType(ntree); /* for group usage */
2053 /* now verify all types in material trees, groups are set OK now */
2054 for(ma= main->mat.first; ma; ma= ma->id.next) {
2056 ntreeVerifyTypes(ma->nodetree);
2058 /* and scene trees */
2059 for(sce= main->scene.first; sce; sce= sce->id.next) {
2061 ntreeVerifyTypes(sce->nodetree);
2063 /* and texture trees */
2064 for(tx= main->tex.first; tx; tx= tx->id.next) {
2066 ntreeVerifyTypes(tx->nodetree);
2072 /* ntree itself has been read! */
2073 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2075 /* note: writing and reading goes in sync, for speed */
2080 ntree->init= 0; /* to set callbacks and force setting types */
2081 ntree->owntype= NULL;
2082 ntree->progress= NULL;
2084 ntree->adt= newdataadr(fd, ntree->adt);
2085 direct_link_animdata(fd, ntree->adt);
2087 link_list(fd, &ntree->nodes);
2088 for(node= ntree->nodes.first; node; node= node->next) {
2089 if(node->type == NODE_DYNAMIC) {
2091 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
2092 node->typeinfo= NULL;
2095 node->storage= newdataadr(fd, node->storage);
2098 /* could be handlerized at some point */
2099 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
2100 direct_link_curvemapping(fd, node->storage);
2101 else if(ntree->type==NTREE_COMPOSIT) {
2102 if( ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
2103 direct_link_curvemapping(fd, node->storage);
2104 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2105 ((ImageUser *)node->storage)->ok= 1;
2107 else if( ntree->type==NTREE_TEXTURE) {
2108 if(node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME)
2109 direct_link_curvemapping(fd, node->storage);
2110 else if(node->type==TEX_NODE_IMAGE)
2111 ((ImageUser *)node->storage)->ok= 1;
2114 link_list(fd, &node->inputs);
2115 link_list(fd, &node->outputs);
2117 link_list(fd, &ntree->links);
2119 /* and we connect the rest */
2120 for(node= ntree->nodes.first; node; node= node->next) {
2121 node->preview= newimaadr(fd, node->preview);
2123 for(sock= node->inputs.first; sock; sock= sock->next)
2124 sock->link= newdataadr(fd, sock->link);
2125 for(sock= node->outputs.first; sock; sock= sock->next)
2126 sock->ns.data= NULL;
2128 for(link= ntree->links.first; link; link= link->next) {
2129 link->fromnode= newdataadr(fd, link->fromnode);
2130 link->tonode= newdataadr(fd, link->tonode);
2131 link->fromsock= newdataadr(fd, link->fromsock);
2132 link->tosock= newdataadr(fd, link->tosock);
2135 /* type verification is in lib-link */
2138 /* ************ READ ARMATURE ***************** */
2140 /* temp struct used to transport needed info to lib_link_constraint_cb() */
2141 typedef struct tConstraintLinkData {
2144 } tConstraintLinkData;
2145 /* callback function used to relink constraint ID-links */
2146 static void lib_link_constraint_cb(bConstraint *UNUSED(con), ID **idpoin, void *userdata)
2148 tConstraintLinkData *cld= (tConstraintLinkData *)userdata;
2149 *idpoin = newlibadr(cld->fd, cld->id->lib, *idpoin);
2152 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2154 tConstraintLinkData cld;
2158 for (con = conlist->first; con; con=con->next) {
2159 /* patch for error introduced by changing constraints (dunno how) */
2160 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2161 if(con->data==NULL) {
2162 con->type= CONSTRAINT_TYPE_NULL;
2164 /* own ipo, all constraints have it */
2165 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2168 /* relink all ID-blocks used by the constraints */
2172 id_loop_constraints(conlist, lib_link_constraint_cb, &cld);
2175 static void direct_link_constraints(FileData *fd, ListBase *lb)
2180 for (con=lb->first; con; con=con->next) {
2181 con->data = newdataadr(fd, con->data);
2183 switch (con->type) {
2184 case CONSTRAINT_TYPE_PYTHON:
2186 bPythonConstraint *data= con->data;
2188 link_list(fd, &data->targets);
2190 data->prop = newdataadr(fd, data->prop);
2192 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2195 case CONSTRAINT_TYPE_SPLINEIK:
2197 bSplineIKConstraint *data= con->data;
2199 data->points= newdataadr(fd, data->points);
2202 case CONSTRAINT_TYPE_KINEMATIC:
2204 con->lin_error = 0.f;
2205 con->rot_error = 0.f;
2207 case CONSTRAINT_TYPE_CHILDOF:
2209 /* XXX version patch, in older code this flag wasn't always set, and is inherent to type */
2210 if(con->ownspace == CONSTRAINT_SPACE_POSE)
2211 con->flag |= CONSTRAINT_SPACEONCE;
2218 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2220 bPoseChannel *pchan;
2221 bArmature *arm= ob->data;
2228 /* always rebuild to match proxy or lib changes */
2229 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2232 /* sync proxy layer */
2233 if(pose->proxy_layer)
2234 arm->layer = pose->proxy_layer;
2236 /* sync proxy active bone */
2237 if(pose->proxy_act_bone[0]) {
2238 Bone *bone = get_named_bone(arm, pose->proxy_act_bone);
2240 arm->act_bone = bone;
2244 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2245 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2247 /* hurms... loop in a loop, but yah... later... (ton) */
2248 pchan->bone= get_named_bone(arm, pchan->name);
2250 pchan->custom= newlibadr_us(fd, arm->id.lib, pchan->custom);
2251 if(pchan->bone==NULL)
2253 else if(ob->id.lib==NULL && arm->id.lib) {
2254 /* local pose selection copied to armature, bit hackish */
2255 pchan->bone->flag &= ~BONE_SELECTED;
2256 pchan->bone->flag |= pchan->selectflag;
2261 ob->recalc= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
2262 pose->flag |= POSE_RECALC;
2266 static void lib_link_armature(FileData *fd, Main *main)
2270 arm= main->armature.first;
2273 if(arm->id.flag & LIB_NEEDLINK) {
2274 if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
2275 arm->id.flag -= LIB_NEEDLINK;
2281 static void direct_link_bones(FileData *fd, Bone* bone)
2285 bone->parent= newdataadr(fd, bone->parent);
2286 bone->prop= newdataadr(fd, bone->prop);
2288 IDP_DirectLinkProperty(bone->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2290 bone->flag &= ~BONE_DRAW_ACTIVE;
2292 link_list(fd, &bone->childbase);
2294 for(child=bone->childbase.first; child; child=child->next)
2295 direct_link_bones(fd, child);
2298 static void direct_link_armature(FileData *fd, bArmature *arm)
2302 link_list(fd, &arm->bonebase);
2306 arm->adt= newdataadr(fd, arm->adt);
2307 direct_link_animdata(fd, arm->adt);
2309 bone=arm->bonebase.first;
2311 direct_link_bones(fd, bone);
2315 arm->act_bone= newdataadr(fd, arm->act_bone);
2316 arm->act_edbone= NULL;
2319 /* ************ READ CAMERA ***************** */
2321 static void lib_link_camera(FileData *fd, Main *main)
2325 ca= main->camera.first;
2327 if(ca->id.flag & LIB_NEEDLINK) {
2328 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2330 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2332 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2334 ca->id.flag -= LIB_NEEDLINK;
2340 static void direct_link_camera(FileData *fd, Camera *ca)
2342 ca->adt= newdataadr(fd, ca->adt);
2343 direct_link_animdata(fd, ca->adt);
2347 /* ************ READ LAMP ***************** */
2349 static void lib_link_lamp(FileData *fd, Main *main)
2355 la= main->lamp.first;
2357 if(la->id.flag & LIB_NEEDLINK) {
2358 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2360 for(a=0; a<MAX_MTEX; a++) {
2363 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2364 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2368 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2370 la->id.flag -= LIB_NEEDLINK;
2376 static void direct_link_lamp(FileData *fd, Lamp *la)
2380 la->adt= newdataadr(fd, la->adt);
2381 direct_link_animdata(fd, la->adt);
2383 for(a=0; a<MAX_MTEX; a++) {
2384 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2387 la->curfalloff= newdataadr(fd, la->curfalloff);
2389 direct_link_curvemapping(fd, la->curfalloff);
2391 la->preview = direct_link_preview_image(fd, la->preview);
2394 /* ************ READ keys ***************** */
2396 static void lib_link_key(FileData *fd, Main *main)
2400 key= main->key.first;
2402 if(key->id.flag & LIB_NEEDLINK) {
2403 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2405 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2406 key->from= newlibadr(fd, key->id.lib, key->from);
2408 key->id.flag -= LIB_NEEDLINK;
2414 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2417 char *data, *poin, *cp;
2419 elemsize= key->elemsize;
2422 for(a=0; a<kb->totelem; a++) {
2427 while( cp[0] ) { /* cp[0]==amount */
2429 switch(cp[1]) { /* cp[1]= type */
2435 SWITCH_INT((*poin));
2448 static void direct_link_key(FileData *fd, Key *key)
2452 link_list(fd, &(key->block));
2454 key->adt= newdataadr(fd, key->adt);
2455 direct_link_animdata(fd, key->adt);
2457 key->refkey= newdataadr(fd, key->refkey);
2459 kb= key->block.first;
2462 kb->data= newdataadr(fd, kb->data);
2464 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2465 switch_endian_keyblock(key, kb);
2471 /* ************ READ mball ***************** */
2473 static void lib_link_mball(FileData *fd, Main *main)
2478 mb= main->mball.first;
2480 if(mb->id.flag & LIB_NEEDLINK) {
2481 if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
2483 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2485 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2487 mb->id.flag -= LIB_NEEDLINK;
2493 static void direct_link_mball(FileData *fd, MetaBall *mb)
2495 mb->adt= newdataadr(fd, mb->adt);
2496 direct_link_animdata(fd, mb->adt);
2498 mb->mat= newdataadr(fd, mb->mat);
2499 test_pointer_array(fd, (void **)&mb->mat);
2501 link_list(fd, &(mb->elems));
2503 mb->disp.first= mb->disp.last= NULL;
2504 mb->editelems= NULL;
2506 /* mb->edit_elems.first= mb->edit_elems.last= NULL;*/
2510 /* ************ READ WORLD ***************** */
2512 static void lib_link_world(FileData *fd, Main *main)
2518 wrld= main->world.first;
2520 if(wrld->id.flag & LIB_NEEDLINK) {
2521 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2523 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2525 for(a=0; a<MAX_MTEX; a++) {
2526 mtex= wrld->mtex[a];
2528 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2529 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2533 wrld->id.flag -= LIB_NEEDLINK;
2535 wrld= wrld->id.next;
2539 static void direct_link_world(FileData *fd, World *wrld)
2543 wrld->adt= newdataadr(fd, wrld->adt);
2544 direct_link_animdata(fd, wrld->adt);
2546 for(a=0; a<MAX_MTEX; a++) {
2547 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2549 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2553 /* ************ READ VFONT ***************** */
2555 static void lib_link_vfont(FileData *UNUSED(fd), Main *main)
2559 vf= main->vfont.first;
2561 if(vf->id.flag & LIB_NEEDLINK) {
2562 vf->id.flag -= LIB_NEEDLINK;
2568 static void direct_link_vfont(FileData *fd, VFont *vf)
2571 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2574 /* ************ READ TEXT ****************** */
2576 static void lib_link_text(FileData *UNUSED(fd), Main *main)
2580 text= main->text.first;
2582 if(text->id.flag & LIB_NEEDLINK) {
2583 text->id.flag -= LIB_NEEDLINK;
2585 text= text->id.next;
2589 static void direct_link_text(FileData *fd, Text *text)
2593 text->name= newdataadr(fd, text->name);
2596 text->undo_len= TXT_INIT_UNDO;
2597 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2599 text->compiled= NULL;
2602 if(text->flags & TXT_ISEXT) {
2607 link_list(fd, &text->lines);
2608 link_list(fd, &text->markers);
2610 text->curl= newdataadr(fd, text->curl);
2611 text->sell= newdataadr(fd, text->sell);
2613 ln= text->lines.first;
2615 ln->line= newdataadr(fd, ln->line);
2618 if (ln->len != (int) strlen(ln->line)) {
2619 printf("Error loading text, line lengths differ\n");
2620 ln->len = strlen(ln->line);
2626 text->flags = (text->flags) & ~TXT_ISEXT;
2631 /* ************ READ IMAGE ***************** */
2633 static void lib_link_image(FileData *fd, Main *main)
2637 ima= main->image.first;
2639 if(ima->id.flag & LIB_NEEDLINK) {
2640 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2642 ima->id.flag -= LIB_NEEDLINK;
2648 static void link_ibuf_list(FileData *fd, ListBase *lb)
2652 if(lb->first==NULL) return;
2654 lb->first= newimaadr(fd, lb->first);
2658 ln->next= newimaadr(fd, ln->next);
2666 static void direct_link_image(FileData *fd, Image *ima)
2668 /* for undo system, pointers could be restored */
2670 link_ibuf_list(fd, &ima->ibufs);
2672 ima->ibufs.first= ima->ibufs.last= NULL;
2674 /* if not restored, we keep the binded opengl index */
2675 if(ima->ibufs.first==NULL) {
2677 ima->gputexture= NULL;
2683 memset(ima->renders, 0, sizeof(ima->renders));
2684 ima->last_render_slot= ima->render_slot;
2686 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2687 ima->preview = direct_link_preview_image(fd, ima->preview);
2692 /* ************ READ CURVE ***************** */
2694 static void lib_link_curve(FileData *fd, Main *main)
2699 cu= main->curve.first;
2701 if(cu->id.flag & LIB_NEEDLINK) {
2702 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2704 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2706 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2707 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2708 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2709 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2710 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2711 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2712 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2714 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2715 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2717 cu->id.flag -= LIB_NEEDLINK;
2724 static void switch_endian_knots(Nurb *nu)
2731 SWITCH_INT(nu->knotsu[len]);
2737 SWITCH_INT(nu->knotsv[len]);
2742 static void direct_link_curve(FileData *fd, Curve *cu)
2747 cu->adt= newdataadr(fd, cu->adt);
2748 direct_link_animdata(fd, cu->adt);
2750 cu->mat= newdataadr(fd, cu->mat);
2751 test_pointer_array(fd, (void **)&cu->mat);
2752 cu->str= newdataadr(fd, cu->str);
2753 cu->strinfo= newdataadr(fd, cu->strinfo);
2754 cu->tb= newdataadr(fd, cu->tb);
2756 if(cu->vfont == NULL) link_list(fd, &(cu->nurb));
2758 cu->nurb.first=cu->nurb.last= 0;
2760 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2762 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2769 cu->tb[0].w = cu->linewidth;
2771 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2774 cu->bev.first=cu->bev.last= NULL;
2775 cu->disp.first=cu->disp.last= NULL;
2783 nu->bezt= newdataadr(fd, nu->bezt);
2784 nu->bp= newdataadr(fd, nu->bp);
2785 nu->knotsu= newdataadr(fd, nu->knotsu);
2786 nu->knotsv= newdataadr(fd, nu->knotsv);
2787 if (cu->vfont == NULL) nu->charidx= nu->mat_nr;
2789 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2790 switch_endian_knots(nu);
2798 /* ************ READ TEX ***************** */
2800 static void lib_link_texture(FileData *fd, Main *main)
2804 tex= main->tex.first;
2806 if(tex->id.flag & LIB_NEEDLINK) {
2807 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
2809 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2810 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2811 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2813 tex->pd->object= newlibadr(fd, tex->id.lib, tex->pd->object);
2814 if(tex->vd) tex->vd->object= newlibadr(fd, tex->id.lib, tex->vd->object);
2817 lib_link_ntree(fd, &tex->id, tex->nodetree);
2819 tex->id.flag -= LIB_NEEDLINK;
2825 static void direct_link_texture(FileData *fd, Tex *tex)
2827 tex->adt= newdataadr(fd, tex->adt);
2828 direct_link_animdata(fd, tex->adt);
2830 tex->plugin= newdataadr(fd, tex->plugin);
2832 tex->plugin->handle= 0;
2833 open_plugin_tex(tex->plugin);
2834 /* initialize data for this instance, if an initialization
2837 if (tex->plugin->instance_init)
2838 tex->plugin->instance_init((void *) tex->plugin->data);
2840 tex->coba= newdataadr(fd, tex->coba);
2841 tex->env= newdataadr(fd, tex->env);
2843 tex->env->ima= NULL;
2844 memset(tex->env->cube, 0, 6*sizeof(void *));
2847 tex->pd= newdataadr(fd, tex->pd);
2849 tex->pd->point_tree = NULL;
2850 tex->pd->coba= newdataadr(fd, tex->pd->coba);
2853 tex->vd= newdataadr(fd, tex->vd);
2855 tex->vd->dataset = NULL;
2859 tex->nodetree= newdataadr(fd, tex->nodetree);
2861 direct_link_nodetree(fd, tex->nodetree);
2863 tex->preview = direct_link_preview_image(fd, tex->preview);
2870 /* ************ READ MATERIAL ***************** */
2872 static void lib_link_material(FileData *fd, Main *main)
2878 ma= main->mat.first;
2880 if(ma->id.flag & LIB_NEEDLINK) {
2881 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
2883 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2884 of library blocks that implement this.*/
2885 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2887 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2888 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2890 for(a=0; a<MAX_MTEX; a++) {
2893 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2894 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2899 lib_link_ntree(fd, &ma->id, ma->nodetree);
2901 ma->id.flag -= LIB_NEEDLINK;
2907 static void direct_link_material(FileData *fd, Material *ma)
2911 ma->adt= newdataadr(fd, ma->adt);
2912 direct_link_animdata(fd, ma->adt);
2914 for(a=0; a<MAX_MTEX; a++) {
2915 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2918 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2919 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2921 ma->nodetree= newdataadr(fd, ma->nodetree);
2923 direct_link_nodetree(fd, ma->nodetree);
2925 ma->preview = direct_link_preview_image(fd, ma->preview);
2926 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
2929 /* ************ READ PARTICLE SETTINGS ***************** */
2930 /* update this also to writefile.c */
2931 static const char *ptcache_data_struct[] = {
2932 "", // BPHYS_DATA_INDEX
2933 "", // BPHYS_DATA_LOCATION
2934 "", // BPHYS_DATA_VELOCITY
2935 "", // BPHYS_DATA_ROTATION
2936 "", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
2937 "", // BPHYS_DATA_SIZE:
2938 "", // BPHYS_DATA_TIMES:
2939 "BoidData" // case BPHYS_DATA_BOIDS:
2941 static void direct_link_pointcache(FileData *fd, PointCache *cache)
2943 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
2945 PTCacheExtra *extra;
2948 link_list(fd, &cache->mem_cache);
2950 pm = cache->mem_cache.first;
2952 for(; pm; pm=pm->next) {
2953 for(i=0; i<BPHYS_TOT_DATA; i++) {
2954 pm->data[i] = newdataadr(fd, pm->data[i]);
2956 /* the cache saves non-struct data without DNA */
2957 if(pm->data[i] && strcmp(ptcache_data_struct[i], "")==0 && (fd->flags & FD_FLAGS_SWITCH_ENDIAN)) {
2958 int j, tot= (BKE_ptcache_data_size (i) * pm->totpoint)/4; /* data_size returns bytes */
2959 int *poin= pm->data[i];
2961 for(j= 0; j<tot; j++)
2962 SWITCH_INT(poin[j]);
2966 link_list(fd, &pm->extradata);
2968 for(extra=pm->extradata.first; extra; extra=extra->next)
2969 extra->data = newdataadr(fd, extra->data);
2973 cache->mem_cache.first = cache->mem_cache.last = NULL;
2975 cache->flag &= ~PTCACHE_SIMULATION_VALID;
2978 cache->free_edit= NULL;
2979 cache->cached_frames= NULL;
2982 static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointCache **ocache, int force_disk)
2984 PointCache *cache= NULL;
2986 if(ptcaches->first) {
2987 link_list(fd, ptcaches);
2988 for(cache=ptcaches->first; cache; cache=cache->next) {
2989 direct_link_pointcache(fd, cache);
2991 cache->flag |= PTCACHE_DISK_CACHE;
2996 *ocache = newdataadr(fd, *ocache);
2999 /* old "single" caches need to be linked too */
3000 *ocache = newdataadr(fd, *ocache);
3001 direct_link_pointcache(fd, *ocache);
3003 (*ocache)->flag |= PTCACHE_DISK_CACHE;
3007 ptcaches->first = ptcaches->last = *ocache;
3011 void lib_link_partdeflect(FileData *fd, ID *id, PartDeflect *pd)
3014 pd->tex=newlibadr_us(fd, id->lib, pd->tex);
3017 static void lib_link_particlesettings(FileData *fd, Main *main)
3019 ParticleSettings *part;
3020 ParticleDupliWeight *dw;
3022 part= main->particle.first;
3024 if(part->id.flag & LIB_NEEDLINK) {
3025 if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
3026 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
3028 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
3029 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
3030 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
3031 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
3033 lib_link_partdeflect(fd, &part->id, part->pd);
3034 lib_link_partdeflect(fd, &part->id, part->pd2);
3036 if(part->effector_weights)
3037 part->effector_weights->group = newlibadr(fd, part->id.lib, part->effector_weights->group);
3039 dw = part->dupliweights.first;
3040 for(; dw; dw=dw->next)
3041 dw->ob = newlibadr(fd, part->id.lib, dw->ob);
3044 BoidState *state = part->boids->states.first;
3046 for(; state; state=state->next) {
3047 rule = state->rules.first;
3048 for(; rule; rule=rule->next)
3049 switch(rule->type) {
3050 case eBoidRuleType_Goal:
3051 case eBoidRuleType_Avoid:
3053 BoidRuleGoalAvoid *brga = (BoidRuleGoalAvoid*)rule;
3054 brga->ob = newlibadr(fd, part->id.lib, brga->ob);
3057 case eBoidRuleType_FollowLeader:
3059 BoidRuleFollowLeader *brfl = (BoidRuleFollowLeader*)rule;
3060 brfl->ob = newlibadr(fd, part->id.lib, brfl->ob);
3066 part->id.flag -= LIB_NEEDLINK;
3068 part= part->id.next;
3072 static void direct_link_partdeflect(PartDeflect *pd)
3074 if(pd) pd->rng=NULL;
3077 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
3079 part->adt= newdataadr(fd, part->adt);
3080 part->pd= newdataadr(fd, part->pd);
3081 part->pd2= newdataadr(fd, part->pd2);
3083 direct_link_animdata(fd, part->adt);
3084 direct_link_partdeflect(part->pd);
3085 direct_link_partdeflect(part->pd2);
3087 part->effector_weights = newdataadr(fd, part->effector_weights);
3088 if(!part->effector_weights)
3089 part->effector_weights = BKE_add_effector_weights(part->eff_group);
3091 link_list(fd, &part->dupliweights);
3093 part->boids= newdataadr(fd, part->boids);
3094 part->fluid= newdataadr(fd, part->fluid);
3098 link_list(fd, &part->boids->states);
3100 for(state=part->boids->states.first; state; state=state->next) {
3101 link_list(fd, &state->rules);
3102 link_list(fd, &state->conditions);
3103 link_list(fd, &state->actions);
3108 static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase *particles)
3110 ParticleSystem *psys, *psysnext;
3112 for(psys=particles->first; psys; psys=psysnext){
3113 psysnext= psys->next;
3115 psys->part = newlibadr_us(fd, id->lib, psys->part);
3117 ParticleTarget *pt = psys->targets.first;
3119 for(; pt; pt=pt->next)
3120 pt->ob=newlibadr(fd, id->lib, pt->ob);
3122 psys->parent= newlibadr_us(fd, id->lib, psys->parent);
3123 psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
3126 /* XXX - from reading existing code this seems correct but intended usage of
3127 * pointcache should /w cloth should be added in 'ParticleSystem' - campbell */
3128 psys->clmd->point_cache= psys->pointcache;
3129 psys->clmd->ptcaches.first= psys->clmd->ptcaches.last= NULL;
3130 psys->clmd->coll_parms->group= newlibadr(fd, id->lib, psys->clmd->coll_parms->group);
3134 /* particle modifier must be removed before particle system */
3135 ParticleSystemModifierData *psmd= psys_get_modifier(ob,psys);
3136 BLI_remlink(&ob->modifiers, psmd);
3137 modifier_free((ModifierData *)psmd);
3139 BLI_remlink(particles, psys);
3144 static void direct_link_particlesystems(FileData *fd, ListBase *particles)
3146 ParticleSystem *psys;
3150 for(psys=particles->first; psys; psys=psys->next) {
3151 psys->particles=newdataadr(fd,psys->particles);
3153 if(psys->particles && psys->particles->hair){
3154 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
3155 pa->hair=newdataadr(fd,pa->hair);
3158 if(psys->particles && psys->particles->keys){
3159 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++) {
3164 psys->flag &= ~PSYS_KEYED;
3167 if(psys->particles && psys->particles->boid) {
3168 pa = psys->particles;
3169 pa->boid = newdataadr(fd, pa->boid);
3170 for(a=1,pa++; a<psys->totpart; a++, pa++)
3171 pa->boid = (pa-1)->boid + 1;
3173 else if(psys->particles) {
3174 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
3178 psys->fluid_springs = newdataadr(fd, psys->fluid_springs);
3180 psys->child = newdataadr(fd,psys->child);
3181 psys->effectors = NULL;
3183 link_list(fd, &psys->targets);
3186 psys->free_edit = NULL;
3187 psys->pathcache = NULL;
3188 psys->childcache = NULL;
3189 psys->pathcachebufs.first = psys->pathcachebufs.last = NULL;
3190 psys->childcachebufs.first = psys->childcachebufs.last = NULL;
3193 psys->renderdata = NULL;
3195 direct_link_pointcache_list(fd, &psys->ptcaches