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
42 #include <sys/param.h> // for MAXPATHLEN
44 #include <io.h> // for open close read
46 #include "BLI_winstuff.h"
49 #include "DNA_anim_types.h"
50 #include "DNA_armature_types.h"
51 #include "DNA_actuator_types.h"
52 #include "DNA_brush_types.h"
53 #include "DNA_camera_types.h"
54 #include "DNA_cloth_types.h"
55 #include "DNA_controller_types.h"
56 #include "DNA_constraint_types.h"
57 #include "DNA_effect_types.h"
58 #include "DNA_fileglobal_types.h"
59 #include "DNA_genfile.h"
60 #include "DNA_group_types.h"
61 #include "DNA_gpencil_types.h"
62 #include "DNA_ipo_types.h"
63 #include "DNA_key_types.h"
64 #include "DNA_lattice_types.h"
65 #include "DNA_lamp_types.h"
66 #include "DNA_linestyle_types.h"
67 #include "DNA_meta_types.h"
68 #include "DNA_material_types.h"
69 #include "DNA_mesh_types.h"
70 #include "DNA_meshdata_types.h"
71 #include "DNA_nla_types.h"
72 #include "DNA_node_types.h"
73 #include "DNA_object_fluidsim.h" // NT
74 #include "DNA_packedFile_types.h"
75 #include "DNA_property_types.h"
76 #include "DNA_text_types.h"
77 #include "DNA_view3d_types.h"
78 #include "DNA_screen_types.h"
79 #include "DNA_sensor_types.h"
80 #include "DNA_sdna_types.h"
81 #include "DNA_scene_types.h"
82 #include "DNA_sequence_types.h"
83 #include "DNA_smoke_types.h"
84 #include "DNA_sound_types.h"
85 #include "DNA_space_types.h"
86 #include "DNA_vfont_types.h"
87 #include "DNA_world_types.h"
89 #include "MEM_guardedalloc.h"
91 #include "BLI_blenlib.h"
95 #include "BKE_action.h"
96 #include "BKE_armature.h"
97 #include "BKE_brush.h"
98 #include "BKE_colortools.h"
99 #include "BKE_constraint.h"
100 #include "BKE_context.h"
101 #include "BKE_curve.h"
102 #include "BKE_deform.h"
103 #include "BKE_effect.h" /* give_parteff */
104 #include "BKE_fcurve.h"
105 #include "BKE_global.h" // for G
106 #include "BKE_group.h"
107 #include "BKE_image.h"
108 #include "BKE_lattice.h"
109 #include "BKE_library.h" // for which_libbase
110 #include "BKE_idcode.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, 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 a= set_listbasepointers(mainvar, lbarray);
386 a= set_listbasepointers(from, fromarray);
388 addlisttolist(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)
595 BHeadN *new_bhead = 0;
601 /* not strictly needed but shuts valgrind up
602 * since uninitialized memory gets compared */
603 memset(&bhead8, 0, sizeof(BHead8));
604 memset(&bhead4, 0, sizeof(BHead4));
605 memset(&bhead, 0, sizeof(BHead));
607 // First read the bhead structure.
608 // Depending on the platform the file was written on this can
609 // be a big or little endian BHead4 or BHead8 structure.
611 // As usual 'ENDB' (the last *partial* bhead of the file)
612 // needs some special handling. We don't want to EOF just yet.
614 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
616 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
618 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
619 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
620 switch_endian_bh4(&bhead4);
623 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
624 bh8_from_bh4(&bhead, &bhead4);
626 memcpy(&bhead, &bhead4, sizeof(bhead));
634 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
636 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
637 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
638 switch_endian_bh8(&bhead8);
641 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
642 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
644 memcpy(&bhead, &bhead8, sizeof(bhead));
652 /* make sure people are not trying to pass bad blend files */
653 if (bhead.len < 0) fd->eof = 1;
655 // bhead now contains the (converted) bhead structure. Now read
656 // the associated data and put everything in a BHeadN (creative naming !)
659 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
661 new_bhead->next = new_bhead->prev = 0;
662 new_bhead->bhead = bhead;
664 readsize = fd->read(fd, new_bhead + 1, bhead.len);
666 if (readsize != bhead.len) {
668 MEM_freeN(new_bhead);
678 // We've read a new block. Now add it to the list
682 BLI_addtail(&fd->listbase, new_bhead);
688 BHead *blo_firstbhead(FileData *fd)
694 // Read in a new block if necessary
696 new_bhead = fd->listbase.first;
697 if (new_bhead == 0) {
698 new_bhead = get_bhead(fd);
702 bhead = &new_bhead->bhead;
708 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
710 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
711 BHeadN *prev= bheadn->prev;
713 return prev?&prev->bhead:NULL;
716 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
718 BHeadN *new_bhead = NULL;
722 // bhead is actually a sub part of BHeadN
723 // We calculate the BHeadN pointer from the BHead pointer below
724 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
726 // get the next BHeadN. If it doesn't exist we read in the next one
727 new_bhead = new_bhead->next;
728 if (new_bhead == 0) {
729 new_bhead = get_bhead(fd);
734 // here we do the reverse:
735 // go from the BHeadN pointer to the BHead pointer
736 bhead = &new_bhead->bhead;
742 static void decode_blender_header(FileData *fd)
744 char header[SIZEOFBLENDERHEADER], num[4];
747 // read in the header data
748 readsize = fd->read(fd, header, sizeof(header));
750 if (readsize == sizeof(header)) {
751 if(strncmp(header, "BLENDER", 7) == 0) {
752 int remove_this_endian_test= 1;
754 fd->flags |= FD_FLAGS_FILE_OK;
756 // what size are pointers in the file ?
758 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
759 if (sizeof(void *) != 4) {
760 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
763 if (sizeof(void *) != 8) {
764 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
768 // is the file saved in a different endian
770 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
771 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
774 // get the version number
776 memcpy(num, header+9, 3);
778 fd->fileversion = atoi(num);
783 static int read_file_dna(FileData *fd)
787 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
788 if (bhead->code==DNA1) {
789 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
791 fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
794 fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
795 /* used to retrieve ID names from (bhead+1) */
796 fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
800 } else if (bhead->code==ENDB)
807 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
809 int readsize = read(filedata->filedes, buffer, size);
814 filedata->seek += readsize;
820 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
822 int readsize = gzread(filedata->gzfiledes, buffer, size);
827 filedata->seek += readsize;
833 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
835 // don't read more bytes then there are available in the buffer
836 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
838 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
839 filedata->seek += readsize;
844 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
846 static unsigned int seek= 1<<30; /* the current position */
847 static unsigned int offset= 0; /* size of previous chunks */
848 static MemFileChunk *chunk=NULL;
849 unsigned int chunkoffset, readsize, totread;
851 if(size==0) return 0;
853 if(seek != (unsigned int)filedata->seek) {
854 chunk= filedata->memfile->chunks.first;
858 if(seek + chunk->size > (unsigned) filedata->seek) break;
863 seek= filedata->seek;
870 /* first check if it's on the end if current chunk */
871 if(seek-offset == chunk->size) {
872 offset+= chunk->size;
876 /* debug, should never happen */
878 printf("illegal read, chunk zero\n");
882 chunkoffset= seek-offset;
883 readsize= size-totread;
885 /* data can be spread over multiple chunks, so clamp size
886 * to within this chunk, and then it will read further in
888 if(chunkoffset+readsize > chunk->size)
889 readsize= chunk->size-chunkoffset;
891 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
893 filedata->seek += readsize;
895 } while(totread < size);
903 static FileData *filedata_new(void)
905 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
908 fd->gzfiledes = NULL;
910 /* XXX, this doesn't need to be done all the time,
911 * but it keeps us reentrant, remove once we have
912 * a lib that provides a nice lock. - zr
914 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
916 fd->datamap = oldnewmap_new();
917 fd->globmap = oldnewmap_new();
918 fd->libmap = oldnewmap_new();
923 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
925 decode_blender_header(fd);
927 if (fd->flags & FD_FLAGS_FILE_OK) {
928 if (!read_file_dna(fd)) {
929 BKE_report(reports, RPT_ERROR, "File incomplete");
930 blo_freefiledata(fd);
935 BKE_report(reports, RPT_ERROR, "File is not a Blender file");
936 blo_freefiledata(fd);
943 /* cannot be called with relative paths anymore! */
944 /* on each new library added, it now checks for the current FileData and expands relativeness */
945 FileData *blo_openblenderfile(char *name, ReportList *reports)
949 gzfile= gzopen(name, "rb");
951 if (gzfile == Z_NULL) {
952 BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", name, errno ? strerror(errno) : "Unknown erro reading file");
955 FileData *fd = filedata_new();
956 fd->gzfiledes = gzfile;
957 fd->read = fd_read_gzip_from_file;
959 /* needed for library_append and read_libraries */
960 BLI_strncpy(fd->relabase, name, sizeof(fd->relabase));
962 return blo_decode_and_check(fd, reports);
966 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
968 if (!mem || memsize<SIZEOFBLENDERHEADER) {
969 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
972 FileData *fd= filedata_new();
974 fd->buffersize= memsize;
975 fd->read= fd_read_from_memory;
976 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
978 return blo_decode_and_check(fd, reports);
982 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
985 BKE_report(reports, RPT_ERROR, "Unable to open blend <memory>");
988 FileData *fd= filedata_new();
989 fd->memfile= memfile;
991 fd->read= fd_read_from_memfile;
992 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
994 return blo_decode_and_check(fd, reports);
999 void blo_freefiledata(FileData *fd)
1003 if (fd->filedes != -1) {
1007 if (fd->gzfiledes != NULL)
1009 gzclose(fd->gzfiledes);
1012 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1013 MEM_freeN(fd->buffer);
1017 // Free all BHeadN data blocks
1018 BLI_freelistN(&fd->listbase);
1021 DNA_sdna_free(fd->memsdna);
1023 DNA_sdna_free(fd->filesdna);
1025 MEM_freeN(fd->compflags);
1028 oldnewmap_free(fd->datamap);
1030 oldnewmap_free(fd->globmap);
1032 oldnewmap_free(fd->imamap);
1033 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1034 oldnewmap_free(fd->libmap);
1036 MEM_freeN(fd->bheadmap);
1042 /* ************ DIV ****************** */
1044 int BLO_has_bfile_extension(char *str)
1046 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1049 int BLO_is_a_library(const char *path, char *dir, char *group)
1051 /* return ok when a blenderfile, in dir is the filename,
1052 * in group the type of libdata
1060 if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
1065 /* Find the last slash */
1066 fd= BLI_last_slash(dir);
1070 if(BLO_has_bfile_extension(fd+1)) {
1071 /* the last part of the dir is a .blend file, no group follows */
1072 *fd= '/'; /* put back the removed slash separating the dir and the .blend file name */
1075 char *gp = fd+1; // in case we have a .blend file, gp points to the group
1077 /* Find the last slash */
1078 fd= BLI_last_slash(dir);
1079 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
1081 /* now we know that we are in a blend file and it is safe to
1082 assume that gp actually points to a group */
1083 if (BLI_streq("Screen", gp)==0)
1084 BLI_strncpy(group, gp, GROUP_MAX);
1089 /* ************** OLD POINTERS ******************* */
1091 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1093 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1096 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1098 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1101 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1103 if(fd->imamap && adr)
1104 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1109 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1111 return oldnewmap_liblookup(fd->libmap, adr, lib);
1114 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1116 ID *id= newlibadr(fd, lib, adr);
1124 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1128 for (i=0; i<fd->libmap->nentries; i++) {
1129 OldNew *entry= &fd->libmap->entries[i];
1131 if (old==entry->newp && entry->nr==ID_ID) {
1133 if(new) entry->nr= GS( ((ID *)new)->name );
1139 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1143 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1146 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1150 change_idid_adr_fd(fd, old, new);
1155 /* lib linked proxy objects point to our local data, we need
1156 * to clear that pointer before reading the undo memfile since
1157 * the object might be removed, it is set again in reading
1158 * if the local object still exists */
1159 void blo_clear_proxy_pointers_from_lib(FileData *fd, Main *oldmain)
1161 Object *ob= oldmain->object.first;
1163 for(;ob; ob= ob->id.next)
1165 ob->proxy_from= NULL;
1168 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1170 Image *ima= oldmain->image.first;
1171 Scene *sce= oldmain->scene.first;
1174 fd->imamap= oldnewmap_new();
1176 for(;ima; ima= ima->id.next) {
1177 Link *ibuf= ima->ibufs.first;
1178 for(; ibuf; ibuf= ibuf->next)
1179 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1181 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1182 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
1184 oldnewmap_insert(fd->imamap, ima->renders[a], ima->renders[a], 0);
1186 for(; sce; sce= sce->id.next) {
1189 for(node= sce->nodetree->nodes.first; node; node= node->next)
1190 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1195 /* set old main image ibufs to zero if it has been restored */
1196 /* this works because freeing old main only happens after this call */
1197 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1199 OldNew *entry= fd->imamap->entries;
1200 Image *ima= oldmain->image.first;
1201 Scene *sce= oldmain->scene.first;
1204 /* used entries were restored, so we put them to zero */
1205 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1210 for(;ima; ima= ima->id.next) {
1213 /* this mirrors direct_link_image */
1214 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1216 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1217 BLI_remlink(&ima->ibufs, ibuf);
1219 ima->gputexture= NULL;
1223 ima->gputexture= newimaadr(fd, ima->gputexture);
1224 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
1225 ima->renders[a]= newimaadr(fd, ima->renders[a]);
1227 for(; sce; sce= sce->id.next) {
1230 for(node= sce->nodetree->nodes.first; node; node= node->next)
1231 node->preview= newimaadr(fd, node->preview);
1236 /* undo file support: add all library pointers in lookup */
1237 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1239 Main *ptr= mainlist->first;
1240 ListBase *lbarray[MAX_LIBARRAY];
1242 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1243 int i= set_listbasepointers(ptr, lbarray);
1246 for(id= lbarray[i]->first; id; id= id->next)
1247 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1253 /* ********** END OLD POINTERS ****************** */
1254 /* ********** READ FILE ****************** */
1256 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1258 int blocksize, nblocks;
1261 data= (char *)(bhead+1);
1262 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1266 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1272 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1277 /* switch is based on file dna */
1278 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1279 switch_endian_structs(fd->filesdna, bh);
1281 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1282 if(fd->compflags[bh->SDNAnr]==2) {
1283 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1285 temp= MEM_mallocN(bh->len, blockname);
1286 memcpy(temp, (bh+1), bh->len);
1294 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1298 if(lb->first==NULL) return;
1300 lb->first= newdataadr(fd, lb->first);
1304 ln->next= newdataadr(fd, ln->next);
1312 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1317 if(lb->first==0) return;
1318 poin= newdataadr(fd, lb->first);
1320 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1327 poin= newdataadr(fd, ln->next);
1329 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1339 static void test_pointer_array(FileData *fd, void **mat)
1341 #if defined(WIN32) && !defined(FREE_WINDOWS)
1342 __int64 *lpoin, *lmat;
1344 long long *lpoin, *lmat;
1349 /* manually convert the pointer array in
1350 * the old dna format to a pointer array in
1351 * the new dna format.
1354 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1356 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1357 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1361 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1362 SWITCH_LONGINT(*lpoin);
1363 *ipoin= (int) ((*lpoin) >> 3);
1371 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1372 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1386 /* ************ READ ID Properties *************** */
1388 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1389 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1391 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1396 /*since we didn't save the extra buffer, set totallen to len.*/
1397 prop->totallen = prop->len;
1398 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1400 array= (IDProperty*) prop->data.pointer;
1402 /* note!, idp-arrays didn't exist in 2.4x, so the pointer will be cleared
1403 * theres not really anything we can do to correct this, at least dont crash */
1410 for(i=0; i<prop->len; i++)
1411 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
1414 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1419 /*since we didn't save the extra buffer, set totallen to len.*/
1420 prop->totallen = prop->len;
1421 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1423 if(prop->subtype == IDP_GROUP) {
1424 test_pointer_array(fd, prop->data.pointer);
1425 array= prop->data.pointer;
1427 for(i=0; i<prop->len; i++)
1428 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1430 else if(prop->subtype == IDP_DOUBLE) {
1431 if (switch_endian) {
1432 for (i=0; i<prop->len; i++) {
1433 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1438 if (switch_endian) {
1439 for (i=0; i<prop->len; i++) {
1440 SWITCH_INT(((int*)prop->data.pointer)[i]);
1446 static void IDP_DirectLinkString(IDProperty *prop, int switch_endian, FileData *fd)
1448 /*since we didn't save the extra string buffer, set totallen to len.*/
1449 prop->totallen = prop->len;
1450 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1453 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1455 ListBase *lb = &prop->data.group;
1460 /*Link child id properties now*/
1461 for (loop=prop->data.group.first; loop; loop=loop->next) {
1462 IDP_DirectLinkProperty(loop, switch_endian, fd);
1466 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1468 switch (prop->type) {
1470 IDP_DirectLinkGroup(prop, switch_endian, fd);
1473 IDP_DirectLinkString(prop, switch_endian, fd);
1476 IDP_DirectLinkArray(prop, switch_endian, fd);
1479 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1482 /*erg, stupid doubles. since I'm storing them
1483 in the same field as int val; val2 in the
1484 IDPropertyData struct, they have to deal with
1485 endianness specifically
1487 in theory, val and val2 would've already been swapped
1488 if switch_endian is true, so we have to first unswap
1489 them then reswap them as a single 64-bit entity.
1492 if (switch_endian) {
1493 SWITCH_INT(prop->data.val);
1494 SWITCH_INT(prop->data.val2);
1495 SWITCH_LONGINT(prop->data.val);
1503 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1507 /* ************ READ CurveMapping *************** */
1509 /* cuma itself has been read! */
1510 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1514 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1515 cumap->flag &= ~CUMA_PREMULLED;
1517 for(a=0; a<CM_TOT; a++) {
1518 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1519 cumap->cm[a].table= NULL;
1520 cumap->cm[a].premultable= NULL;
1524 /* ************ READ Brush *************** */
1525 /* library brush linking after fileread */
1526 static void lib_link_brush(FileData *fd, Main *main)
1530 /* only link ID pointers */
1531 for(brush= main->brush.first; brush; brush= brush->id.next) {
1532 if(brush->id.flag & LIB_NEEDLINK) {
1533 brush->id.flag -= LIB_NEEDLINK;
1535 brush->mtex.tex= newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
1536 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1541 static void direct_link_brush(FileData *fd, Brush *brush)
1543 /* brush itself has been read */
1546 brush->curve= newdataadr(fd, brush->curve);
1548 direct_link_curvemapping(fd, brush->curve);
1550 brush_curve_preset(brush, CURVE_PRESET_SHARP);
1552 brush->preview= NULL;
1553 brush->icon_imbuf= NULL;
1556 static void direct_link_script(FileData *fd, Script *script)
1559 SCRIPT_SET_NULL(script)
1563 /* ************ READ PACKEDFILE *************** */
1565 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1567 PackedFile *pf= newdataadr(fd, oldpf);
1570 pf->data= newdataadr(fd, pf->data);
1576 /* ************ READ IMAGE PREVIEW *************** */
1578 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1580 PreviewImage *prv= newdataadr(fd, old_prv);
1584 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1586 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1594 /* ************ READ ANIMATION STUFF ***************** */
1596 /* Legacy Data Support (for Version Patching) ----------------------------- */
1598 // XXX depreceated - old animation system
1599 static void lib_link_ipo(FileData *fd, Main *main)
1603 ipo= main->ipo.first;
1605 if(ipo->id.flag & LIB_NEEDLINK) {
1607 for(icu= ipo->curve.first; icu; icu= icu->next) {
1609 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1611 ipo->id.flag -= LIB_NEEDLINK;
1617 // XXX depreceated - old animation system
1618 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1622 link_list(fd, &(ipo->curve));
1623 icu= ipo->curve.first;
1625 icu->bezt= newdataadr(fd, icu->bezt);
1626 icu->bp= newdataadr(fd, icu->bp);
1627 icu->driver= newdataadr(fd, icu->driver);
1632 // XXX depreceated - old animation system
1633 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1635 bActionStrip *strip;
1636 bActionModifier *amod;
1638 for (strip=striplist->first; strip; strip=strip->next){
1639 strip->object = newlibadr(fd, id->lib, strip->object);
1640 strip->act = newlibadr_us(fd, id->lib, strip->act);
1641 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1642 for(amod= strip->modifiers.first; amod; amod= amod->next)
1643 amod->ob= newlibadr(fd, id->lib, amod->ob);
1647 // XXX depreceated - old animation system
1648 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1650 bActionStrip *strip;
1652 link_list(fd, strips);
1654 for(strip= strips->first; strip; strip= strip->next)
1655 link_list(fd, &strip->modifiers);
1658 // XXX depreceated - old animation system
1659 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1661 bConstraintChannel *chan;
1663 for (chan=chanbase->first; chan; chan=chan->next){
1664 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1668 /* Data Linking ----------------------------- */
1670 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1674 for (fcm= list->first; fcm; fcm= fcm->next) {
1675 /* data for specific modifiers */
1676 switch (fcm->type) {
1677 case FMODIFIER_TYPE_PYTHON:
1679 FMod_Python *data= (FMod_Python *)fcm->data;
1680 data->script = newlibadr(fd, id->lib, data->script);
1687 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1694 /* relink ID-block references... */
1695 for (fcu= list->first; fcu; fcu= fcu->next) {
1698 ChannelDriver *driver= fcu->driver;
1701 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1702 DRIVER_TARGETS_LOOPER(dvar)
1704 /* only relink if still used */
1705 if (tarIndex < dvar->num_targets)
1706 dtar->id= newlibadr(fd, id->lib, dtar->id);
1710 DRIVER_TARGETS_LOOPER_END
1715 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1720 /* NOTE: this assumes that link_list has already been called on the list */
1721 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1725 for (fcm= list->first; fcm; fcm= fcm->next) {
1726 /* relink general data */
1727 fcm->data = newdataadr(fd, fcm->data);
1730 /* do relinking of data for specific types */
1731 switch (fcm->type) {
1732 case FMODIFIER_TYPE_GENERATOR:
1734 FMod_Generator *data= (FMod_Generator *)fcm->data;
1736 data->coefficients= newdataadr(fd, data->coefficients);
1739 case FMODIFIER_TYPE_ENVELOPE:
1741 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1743 data->data= newdataadr(fd, data->data);
1746 case FMODIFIER_TYPE_PYTHON:
1748 FMod_Python *data= (FMod_Python *)fcm->data;
1750 data->prop = newdataadr(fd, data->prop);
1751 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1758 /* NOTE: this assumes that link_list has already been called on the list */
1759 static void direct_link_fcurves(FileData *fd, ListBase *list)
1763 /* link F-Curve data to F-Curve again (non ID-libs) */
1764 for (fcu= list->first; fcu; fcu= fcu->next) {
1766 fcu->bezt= newdataadr(fd, fcu->bezt);
1767 fcu->fpt= newdataadr(fd, fcu->fpt);
1770 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1773 fcu->grp= newdataadr(fd, fcu->grp);
1776 fcu->driver= newdataadr(fd, fcu->driver);
1778 ChannelDriver *driver= fcu->driver;
1781 driver->expr_comp= NULL;
1783 /* relink variables, targets and their paths */
1784 link_list(fd, &driver->variables);
1785 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1786 DRIVER_TARGETS_LOOPER(dvar)
1788 /* only relink the targets being used */
1789 if (tarIndex < dvar->num_targets)
1790 dtar->rna_path= newdataadr(fd, dtar->rna_path);
1792 dtar->rna_path= NULL;
1794 DRIVER_TARGETS_LOOPER_END
1799 link_list(fd, &fcu->modifiers);
1800 direct_link_fmodifiers(fd, &fcu->modifiers);
1805 static void lib_link_action(FileData *fd, Main *main)
1808 bActionChannel *chan;
1810 for (act= main->action.first; act; act= act->id.next) {
1811 if (act->id.flag & LIB_NEEDLINK) {
1812 act->id.flag -= LIB_NEEDLINK;
1814 // XXX depreceated - old animation system <<<
1815 for (chan=act->chanbase.first; chan; chan=chan->next) {
1816 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1817 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1819 // >>> XXX depreceated - old animation system
1821 lib_link_fcurves(fd, &act->id, &act->curves);
1826 static void direct_link_action(FileData *fd, bAction *act)
1828 bActionChannel *achan; // XXX depreceated - old animation system
1831 link_list(fd, &act->curves);
1832 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1833 link_list(fd, &act->groups);
1834 link_list(fd, &act->markers);
1836 // XXX depreceated - old animation system <<<
1837 for (achan = act->chanbase.first; achan; achan=achan->next) {
1838 achan->grp= newdataadr(fd, achan->grp);
1840 link_list(fd, &achan->constraintChannels);
1842 // >>> XXX depreceated - old animation system
1844 direct_link_fcurves(fd, &act->curves);
1846 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1847 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1848 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1852 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
1856 for (strip= list->first; strip; strip= strip->next) {
1857 /* check strip's children */
1858 lib_link_nladata_strips(fd, id, &strip->strips);
1860 /* check strip's F-Curves */
1861 lib_link_fcurves(fd, id, &strip->fcurves);
1863 /* reassign the counted-reference to action */
1864 strip->act = newlibadr_us(fd, id->lib, strip->act);
1868 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
1872 /* we only care about the NLA strips inside the tracks */
1873 for (nlt= list->first; nlt; nlt= nlt->next) {
1874 lib_link_nladata_strips(fd, id, &nlt->strips);
1878 /* This handles Animato NLA-Strips linking
1879 * NOTE: this assumes that link_list has already been called on the list
1881 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
1885 for (strip= list->first; strip; strip= strip->next) {
1886 /* strip's child strips */
1887 link_list(fd, &strip->strips);
1888 direct_link_nladata_strips(fd, &strip->strips);
1890 /* strip's F-Curves */
1891 link_list(fd, &strip->fcurves);
1892 direct_link_fcurves(fd, &strip->fcurves);
1894 /* strip's F-Modifiers */
1895 link_list(fd, &strip->modifiers);
1896 direct_link_fcurves(fd, &strip->modifiers);
1900 /* NOTE: this assumes that link_list has already been called on the list */
1901 static void direct_link_nladata(FileData *fd, ListBase *list)
1905 for (nlt= list->first; nlt; nlt= nlt->next) {
1906 /* relink list of strips */
1907 link_list(fd, &nlt->strips);
1909 /* relink strip data */
1910 direct_link_nladata_strips(fd, &nlt->strips);
1916 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
1921 /* here, we're only interested in the ID pointer stored in some of the paths */
1922 for (ks= list->first; ks; ks= ks->next) {
1923 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1924 ksp->id= newlibadr(fd, id->lib, ksp->id);
1929 /* NOTE: this assumes that link_list has already been called on the list */
1930 static void direct_link_keyingsets(FileData *fd, ListBase *list)
1935 /* link KeyingSet data to KeyingSet again (non ID-libs) */
1936 for (ks= list->first; ks; ks= ks->next) {
1938 link_list(fd, &ks->paths);
1940 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1942 ksp->rna_path= newdataadr(fd, ksp->rna_path);
1949 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
1954 /* link action data */
1955 adt->action= newlibadr_us(fd, id->lib, adt->action);
1956 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
1959 lib_link_fcurves(fd, id, &adt->drivers);
1961 /* overrides don't have lib-link for now, so no need to do anything */
1964 lib_link_nladata(fd, id, &adt->nla_tracks);
1967 static void direct_link_animdata(FileData *fd, AnimData *adt)
1969 /* NOTE: must have called newdataadr already before doing this... */
1974 link_list(fd, &adt->drivers);
1975 direct_link_fcurves(fd, &adt->drivers);
1977 /* link overrides */
1981 link_list(fd, &adt->nla_tracks);
1982 direct_link_nladata(fd, &adt->nla_tracks);
1984 /* clear temp pointers that may have been set... */
1985 // TODO: it's probably only a small cost to reload this anyway...
1986 adt->actstrip= NULL;
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 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 && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) ) {
2108 direct_link_curvemapping(fd, node->storage);
2111 link_list(fd, &node->inputs);
2112 link_list(fd, &node->outputs);
2114 link_list(fd, &ntree->links);
2116 /* and we connect the rest */
2117 for(node= ntree->nodes.first; node; node= node->next) {
2118 node->preview= newimaadr(fd, node->preview);
2120 for(sock= node->inputs.first; sock; sock= sock->next)
2121 sock->link= newdataadr(fd, sock->link);
2122 for(sock= node->outputs.first; sock; sock= sock->next)
2123 sock->ns.data= NULL;
2125 for(link= ntree->links.first; link; link= link->next) {
2126 link->fromnode= newdataadr(fd, link->fromnode);
2127 link->tonode= newdataadr(fd, link->tonode);
2128 link->fromsock= newdataadr(fd, link->fromsock);
2129 link->tosock= newdataadr(fd, link->tosock);
2132 /* type verification is in lib-link */
2135 /* ************ READ ARMATURE ***************** */
2137 /* temp struct used to transport needed info to lib_link_constraint_cb() */
2138 typedef struct tConstraintLinkData {
2141 } tConstraintLinkData;
2142 /* callback function used to relink constraint ID-links */
2143 static void lib_link_constraint_cb(bConstraint *con, ID **idpoin, void *userdata)
2145 tConstraintLinkData *cld= (tConstraintLinkData *)userdata;
2146 *idpoin = newlibadr(cld->fd, cld->id->lib, *idpoin);
2149 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2151 tConstraintLinkData cld;
2155 for (con = conlist->first; con; con=con->next) {
2156 /* patch for error introduced by changing constraints (dunno how) */
2157 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2158 if(con->data==NULL) {
2159 con->type= CONSTRAINT_TYPE_NULL;
2161 /* own ipo, all constraints have it */
2162 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2165 /* relink all ID-blocks used by the constraints */
2169 id_loop_constraints(conlist, lib_link_constraint_cb, &cld);
2172 static void direct_link_constraints(FileData *fd, ListBase *lb)
2177 for (con=lb->first; con; con=con->next) {
2178 con->data = newdataadr(fd, con->data);
2180 switch (con->type) {
2181 case CONSTRAINT_TYPE_PYTHON:
2183 bPythonConstraint *data= con->data;
2185 link_list(fd, &data->targets);
2187 data->prop = newdataadr(fd, data->prop);
2189 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2192 case CONSTRAINT_TYPE_SPLINEIK:
2194 bSplineIKConstraint *data= con->data;
2196 data->points= newdataadr(fd, data->points);
2199 case CONSTRAINT_TYPE_KINEMATIC:
2201 con->lin_error = 0.f;
2202 con->rot_error = 0.f;
2209 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2211 bPoseChannel *pchan;
2212 bArmature *arm= ob->data;
2218 /* always rebuild to match proxy or lib changes */
2219 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2221 if (ob->proxy && pose->proxy_act_bone[0]) {
2222 Bone *bone = get_named_bone(arm, pose->proxy_act_bone);
2224 arm->act_bone = bone;
2227 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2228 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2230 /* hurms... loop in a loop, but yah... later... (ton) */
2231 pchan->bone= get_named_bone(arm, pchan->name);
2233 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
2234 if(pchan->bone==NULL)
2236 else if(ob->id.lib==NULL && arm->id.lib) {
2237 /* local pose selection copied to armature, bit hackish */
2238 pchan->bone->flag &= ~BONE_SELECTED;
2239 pchan->bone->flag |= pchan->selectflag;
2244 ob->recalc= OB_RECALC_ALL;
2245 pose->flag |= POSE_RECALC;
2249 static void lib_link_armature(FileData *fd, Main *main)
2253 arm= main->armature.first;
2256 if(arm->id.flag & LIB_NEEDLINK) {
2257 if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
2258 arm->id.flag -= LIB_NEEDLINK;
2264 static void direct_link_bones(FileData *fd, Bone* bone)
2268 bone->parent= newdataadr(fd, bone->parent);
2269 bone->prop= newdataadr(fd, bone->prop);
2271 IDP_DirectLinkProperty(bone->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2273 bone->flag &= ~BONE_DRAW_ACTIVE;
2275 link_list(fd, &bone->childbase);
2277 for(child=bone->childbase.first; child; child=child->next)
2278 direct_link_bones(fd, child);
2281 static void direct_link_armature(FileData *fd, bArmature *arm)
2285 link_list(fd, &arm->bonebase);
2289 arm->adt= newdataadr(fd, arm->adt);
2290 direct_link_animdata(fd, arm->adt);
2292 bone=arm->bonebase.first;
2294 direct_link_bones(fd, bone);
2298 arm->act_bone= newdataadr(fd, arm->act_bone);
2299 arm->act_edbone= NULL;
2302 /* ************ READ CAMERA ***************** */
2304 static void lib_link_camera(FileData *fd, Main *main)
2308 ca= main->camera.first;
2310 if(ca->id.flag & LIB_NEEDLINK) {
2311 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2313 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2315 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2317 ca->id.flag -= LIB_NEEDLINK;
2323 static void direct_link_camera(FileData *fd, Camera *ca)
2325 ca->adt= newdataadr(fd, ca->adt);
2326 direct_link_animdata(fd, ca->adt);
2330 /* ************ READ LAMP ***************** */
2332 static void lib_link_lamp(FileData *fd, Main *main)
2338 la= main->lamp.first;
2340 if(la->id.flag & LIB_NEEDLINK) {
2341 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2343 for(a=0; a<MAX_MTEX; a++) {
2346 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2347 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2351 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2353 la->id.flag -= LIB_NEEDLINK;
2359 static void direct_link_lamp(FileData *fd, Lamp *la)
2363 la->adt= newdataadr(fd, la->adt);
2364 direct_link_animdata(fd, la->adt);
2366 for(a=0; a<MAX_MTEX; a++) {
2367 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2370 la->curfalloff= newdataadr(fd, la->curfalloff);
2372 direct_link_curvemapping(fd, la->curfalloff);
2374 la->preview = direct_link_preview_image(fd, la->preview);
2377 /* ************ READ keys ***************** */
2379 static void lib_link_key(FileData *fd, Main *main)
2383 key= main->key.first;
2385 if(key->id.flag & LIB_NEEDLINK) {
2386 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2388 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2389 key->from= newlibadr(fd, key->id.lib, key->from);
2391 key->id.flag -= LIB_NEEDLINK;
2397 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2400 char *data, *poin, *cp;
2402 elemsize= key->elemsize;
2405 for(a=0; a<kb->totelem; a++) {
2410 while( cp[0] ) { /* cp[0]==amount */
2412 switch(cp[1]) { /* cp[1]= type */
2418 SWITCH_INT((*poin));
2431 static void direct_link_key(FileData *fd, Key *key)
2435 link_list(fd, &(key->block));
2437 key->adt= newdataadr(fd, key->adt);
2438 direct_link_animdata(fd, key->adt);
2440 key->refkey= newdataadr(fd, key->refkey);
2442 kb= key->block.first;
2445 kb->data= newdataadr(fd, kb->data);
2447 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2448 switch_endian_keyblock(key, kb);
2454 /* ************ READ mball ***************** */
2456 static void lib_link_mball(FileData *fd, Main *main)
2461 mb= main->mball.first;
2463 if(mb->id.flag & LIB_NEEDLINK) {
2464 if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
2466 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2468 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2470 mb->id.flag -= LIB_NEEDLINK;
2476 static void direct_link_mball(FileData *fd, MetaBall *mb)
2478 mb->adt= newdataadr(fd, mb->adt);
2479 direct_link_animdata(fd, mb->adt);
2481 mb->mat= newdataadr(fd, mb->mat);
2482 test_pointer_array(fd, (void **)&mb->mat);
2484 link_list(fd, &(mb->elems));
2486 mb->disp.first= mb->disp.last= NULL;
2487 mb->editelems= NULL;
2489 /* mb->edit_elems.first= mb->edit_elems.last= NULL;*/
2493 /* ************ READ WORLD ***************** */
2495 static void lib_link_world(FileData *fd, Main *main)
2501 wrld= main->world.first;
2503 if(wrld->id.flag & LIB_NEEDLINK) {
2504 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2506 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2508 for(a=0; a<MAX_MTEX; a++) {
2509 mtex= wrld->mtex[a];
2511 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2512 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2516 wrld->id.flag -= LIB_NEEDLINK;
2518 wrld= wrld->id.next;
2522 static void direct_link_world(FileData *fd, World *wrld)
2526 wrld->adt= newdataadr(fd, wrld->adt);
2527 direct_link_animdata(fd, wrld->adt);
2529 for(a=0; a<MAX_MTEX; a++) {
2530 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2532 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2536 /* ************ READ VFONT ***************** */
2538 static void lib_link_vfont(FileData *fd, Main *main)
2542 vf= main->vfont.first;
2544 if(vf->id.flag & LIB_NEEDLINK) {
2545 vf->id.flag -= LIB_NEEDLINK;
2551 static void direct_link_vfont(FileData *fd, VFont *vf)
2554 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2557 /* ************ READ TEXT ****************** */
2559 static void lib_link_text(FileData *fd, Main *main)
2563 text= main->text.first;
2565 if(text->id.flag & LIB_NEEDLINK) {
2566 text->id.flag -= LIB_NEEDLINK;
2568 text= text->id.next;
2572 static void direct_link_text(FileData *fd, Text *text)
2576 text->name= newdataadr(fd, text->name);
2579 text->undo_len= TXT_INIT_UNDO;
2580 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2582 text->compiled= NULL;
2585 if(text->flags & TXT_ISEXT) {
2590 link_list(fd, &text->lines);
2591 link_list(fd, &text->markers);
2593 text->curl= newdataadr(fd, text->curl);
2594 text->sell= newdataadr(fd, text->sell);
2596 ln= text->lines.first;
2598 ln->line= newdataadr(fd, ln->line);
2601 if (ln->len != (int) strlen(ln->line)) {
2602 printf("Error loading text, line lengths differ\n");
2603 ln->len = strlen(ln->line);
2609 text->flags = (text->flags) & ~TXT_ISEXT;
2614 /* ************ READ IMAGE ***************** */
2616 static void lib_link_image(FileData *fd, Main *main)
2620 ima= main->image.first;
2622 if(ima->id.flag & LIB_NEEDLINK) {
2623 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2625 ima->id.flag -= LIB_NEEDLINK;
2631 static void link_ibuf_list(FileData *fd, ListBase *lb)
2635 if(lb->first==NULL) return;
2637 lb->first= newimaadr(fd, lb->first);
2641 ln->next= newimaadr(fd, ln->next);
2649 static void direct_link_image(FileData *fd, Image *ima)
2651 /* for undo system, pointers could be restored */
2653 link_ibuf_list(fd, &ima->ibufs);
2655 ima->ibufs.first= ima->ibufs.last= NULL;
2657 /* if not restored, we keep the binded opengl index */
2658 if(ima->ibufs.first==NULL) {
2660 ima->gputexture= NULL;
2666 memset(ima->renders, 0, sizeof(ima->renders));
2667 ima->last_render_slot= ima->render_slot;
2669 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2670 ima->preview = direct_link_preview_image(fd, ima->preview);
2675 /* ************ READ CURVE ***************** */
2677 static void lib_link_curve(FileData *fd, Main *main)
2682 cu= main->curve.first;
2684 if(cu->id.flag & LIB_NEEDLINK) {
2685 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2687 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2689 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2690 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2691 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2692 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2693 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2694 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2695 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2697 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2698 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2700 cu->id.flag -= LIB_NEEDLINK;
2707 static void switch_endian_knots(Nurb *nu)
2714 SWITCH_INT(nu->knotsu[len]);
2720 SWITCH_INT(nu->knotsv[len]);
2725 static void direct_link_curve(FileData *fd, Curve *cu)
2730 cu->adt= newdataadr(fd, cu->adt);
2731 direct_link_animdata(fd, cu->adt);
2733 cu->mat= newdataadr(fd, cu->mat);
2734 test_pointer_array(fd, (void **)&cu->mat);
2735 cu->str= newdataadr(fd, cu->str);
2736 cu->strinfo= newdataadr(fd, cu->strinfo);
2737 cu->tb= newdataadr(fd, cu->tb);
2739 if(cu->vfont == NULL) link_list(fd, &(cu->nurb));
2741 cu->nurb.first=cu->nurb.last= 0;
2743 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2745 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2752 cu->tb[0].w = cu->linewidth;
2754 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2757 cu->bev.first=cu->bev.last= NULL;
2758 cu->disp.first=cu->disp.last= NULL;
2766 nu->bezt= newdataadr(fd, nu->bezt);
2767 nu->bp= newdataadr(fd, nu->bp);
2768 nu->knotsu= newdataadr(fd, nu->knotsu);
2769 nu->knotsv= newdataadr(fd, nu->knotsv);
2770 if (cu->vfont == NULL) nu->charidx= nu->mat_nr;
2772 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2773 switch_endian_knots(nu);
2781 /* ************ READ TEX ***************** */
2783 static void lib_link_texture(FileData *fd, Main *main)
2787 tex= main->tex.first;
2789 if(tex->id.flag & LIB_NEEDLINK) {
2790 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
2792 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2793 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2794 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2796 tex->pd->object= newlibadr(fd, tex->id.lib, tex->pd->object);
2797 if(tex->vd) tex->vd->object= newlibadr(fd, tex->id.lib, tex->vd->object);
2800 lib_link_ntree(fd, &tex->id, tex->nodetree);
2802 tex->id.flag -= LIB_NEEDLINK;
2808 static void direct_link_texture(FileData *fd, Tex *tex)
2810 tex->adt= newdataadr(fd, tex->adt);
2811 direct_link_animdata(fd, tex->adt);
2813 tex->plugin= newdataadr(fd, tex->plugin);
2815 tex->plugin->handle= 0;
2816 open_plugin_tex(tex->plugin);
2817 /* initialize data for this instance, if an initialization
2820 if (tex->plugin->instance_init)
2821 tex->plugin->instance_init((void *) tex->plugin->data);
2823 tex->coba= newdataadr(fd, tex->coba);
2824 tex->env= newdataadr(fd, tex->env);
2826 tex->env->ima= NULL;
2827 memset(tex->env->cube, 0, 6*sizeof(void *));
2830 tex->pd= newdataadr(fd, tex->pd);
2832 tex->pd->point_tree = NULL;
2833 tex->pd->coba= newdataadr(fd, tex->pd->coba);
2836 tex->vd= newdataadr(fd, tex->vd);
2838 tex->vd->dataset = NULL;
2842 tex->nodetree= newdataadr(fd, tex->nodetree);
2844 direct_link_nodetree(fd, tex->nodetree);
2846 tex->preview = direct_link_preview_image(fd, tex->preview);
2853 /* ************ READ MATERIAL ***************** */
2855 static void lib_link_material(FileData *fd, Main *main)
2861 ma= main->mat.first;
2863 if(ma->id.flag & LIB_NEEDLINK) {
2864 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
2866 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2867 of library blocks that implement this.*/
2868 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2870 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2871 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2873 for(a=0; a<MAX_MTEX; a++) {
2876 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2877 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2882 lib_link_ntree(fd, &ma->id, ma->nodetree);
2884 ma->id.flag -= LIB_NEEDLINK;
2890 static void direct_link_material(FileData *fd, Material *ma)
2894 ma->adt= newdataadr(fd, ma->adt);
2895 direct_link_animdata(fd, ma->adt);
2897 for(a=0; a<MAX_MTEX; a++) {
2898 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2901 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2902 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2904 ma->nodetree= newdataadr(fd, ma->nodetree);
2906 direct_link_nodetree(fd, ma->nodetree);
2908 ma->preview = direct_link_preview_image(fd, ma->preview);
2909 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
2912 /* ************ READ PARTICLE SETTINGS ***************** */
2914 static void direct_link_pointcache(FileData *fd, PointCache *cache)
2916 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
2920 link_list(fd, &cache->mem_cache);
2922 pm = cache->mem_cache.first;
2924 for(; pm; pm=pm->next) {
2926 pm->index_array = newdataadr(fd, pm->index_array);
2928 for(i=0; i<BPHYS_TOT_DATA; i++) {
2929 if(pm->data[i] && pm->data_types & (1<<i))
2930 pm->data[i] = newdataadr(fd, pm->data[i]);
2935 cache->mem_cache.first = cache->mem_cache.last = NULL;
2937 cache->flag &= ~PTCACHE_SIMULATION_VALID;
2940 cache->free_edit= NULL;
2941 cache->cached_frames= NULL;
2944 static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointCache **ocache)
2948 if(ptcaches->first) {
2949 link_list(fd, ptcaches);
2950 for(cache=ptcaches->first; cache; cache=cache->next)
2951 direct_link_pointcache(fd, cache);
2953 *ocache = newdataadr(fd, *ocache);
2956 /* old "single" caches need to be linked too */
2957 *ocache = newdataadr(fd, *ocache);
2958 direct_link_pointcache(fd, *ocache);
2960 ptcaches->first = ptcaches->last = *ocache;
2964 void lib_link_partdeflect(FileData *fd, ID *id, PartDeflect *pd)
2967 pd->tex=newlibadr_us(fd, id->lib, pd->tex);
2970 static void lib_link_particlesettings(FileData *fd, Main *main)
2972 ParticleSettings *part;
2973 ParticleDupliWeight *dw;
2975 part= main->particle.first;
2977 if(part->id.flag & LIB_NEEDLINK) {
2978 if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
2979 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
2981 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
2982 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
2983 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
2984 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
2986 lib_link_partdeflect(fd, &part->id, part->pd);
2987 lib_link_partdeflect(fd, &part->id, part->pd2);
2989 if(part->effector_weights)
2990 part->effector_weights->group = newlibadr(fd, part->id.lib, part->effector_weights->group);
2992 dw = part->dupliweights.first;
2993 for(; dw; dw=dw->next)
2994 dw->ob = newlibadr(fd, part->id.lib, dw->ob);
2997 BoidState *state = part->boids->states.first;
2999 for(; state; state=state->next) {
3000 rule = state->rules.first;
3001 for(; rule; rule=rule->next)
3002 switch(rule->type) {
3003 case eBoidRuleType_Goal:
3004 case eBoidRuleType_Avoid:
3006 BoidRuleGoalAvoid *brga = (BoidRuleGoalAvoid*)rule;
3007 brga->ob = newlibadr(fd, part->id.lib, brga->ob);
3010 case eBoidRuleType_FollowLeader:
3012 BoidRuleFollowLeader *brfl = (BoidRuleFollowLeader*)rule;
3013 brfl->ob = newlibadr(fd, part->id.lib, brfl->ob);
3019 part->id.flag -= LIB_NEEDLINK;
3021 part= part->id.next;
3025 static void direct_link_partdeflect(PartDeflect *pd)
3027 if(pd) pd->rng=NULL;
3030 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
3032 part->adt= newdataadr(fd, part->adt);
3033 part->pd= newdataadr(fd, part->pd);
3034 part->pd2= newdataadr(fd, part->pd2);
3036 direct_link_animdata(fd, part->adt);
3037 direct_link_partdeflect(part->pd);
3038 direct_link_partdeflect(part->pd2);
3040 part->effector_weights = newdataadr(fd, part->effector_weights);
3041 if(!part->effector_weights)
3042 part->effector_weights = BKE_add_effector_weights(part->eff_group);
3044 link_list(fd, &part->dupliweights);
3046 part->boids= newdataadr(fd, part->boids);
3047 part->fluid= newdataadr(fd, part->fluid);
3051 link_list(fd, &part->boids->states);
3053 for(state=part->boids->states.first; state; state=state->next) {
3054 link_list(fd, &state->rules);
3055 link_list(fd, &state->conditions);
3056 link_list(fd, &state->actions);
3061 static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase *particles)
3063 ParticleSystem *psys, *psysnext;
3065 for(psys=particles->first; psys; psys=psysnext){
3066 psysnext= psys->next;
3068 psys->part = newlibadr_us(fd, id->lib, psys->part);
3070 ParticleTarget *pt = psys->targets.first;
3072 for(; pt; pt=pt->next)
3073 pt->ob=newlibadr(fd, id->lib, pt->ob);
3075 psys->parent= newlibadr_us(fd, id->lib, psys->parent);
3076 psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
3079 /* XXX - from reading existing code this seems correct but intended usage of
3080 * pointcache should /w cloth should be added in 'ParticleSystem' - campbell */
3081 psys->clmd->point_cache= psys->pointcache;
3082 psys->clmd->ptcaches.first= psys->clmd->ptcaches.last= NULL;
3083 psys->clmd->coll_parms->group= newlibadr(fd, id->lib, psys->clmd->coll_parms->group);
3087 /* particle modifier must be removed before particle system */
3088 ParticleSystemModifierData *psmd= psys_get_modifier(ob,psys);
3089 BLI_remlink(&ob->modifiers, psmd);
3090 modifier_free((ModifierData *)psmd);
3092 BLI_remlink(particles, psys);
3097 static void direct_link_particlesystems(FileData *fd, ListBase *particles)
3099 ParticleSystem *psys;
3103 for(psys=particles->first; psys; psys=psys->next) {
3104 psys->particles=newdataadr(fd,psys->particles);
3106 if(psys->particles && psys->particles->hair){
3107 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
3108 pa->hair=newdataadr(fd,pa->hair);
3111 if(psys->particles && psys->particles->keys){
3112 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++) {
3117 psys->flag &= ~PSYS_KEYED;
3120 if(psys->particles && psys->particles->boid) {
3121 pa = psys->particles;
3122 pa->boid = newdataadr(fd, pa->boid);
3123 for(a=1,pa++; a<psys->totpart; a++, pa++)
3124 pa->boid = (pa-1)->boid + 1;
3127 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
3132 psys->child = newdataadr(fd,psys->child);
3133 psys->effectors = NULL;
3135 link_list(fd, &psys->targets);
3138 psys->free_edit = NULL;
3139 psys->pathcache = NULL;
3140 psys->childcache = NULL;
3141 psys->pathcachebufs.first = psys->pathcachebufs.last = NULL;
3142 psys->childcachebufs.first = psys->childcachebufs.last = NULL;
3146 direct_link_pointcache_list(fd, &psys->ptcaches, &psys->pointcache);
3149 psys->clmd = newdataadr(fd, psys->clmd);
3150 psys->clmd->clothObject = NULL;
3152 psys->clmd->sim_parms= newdataadr(fd, psys->clmd->sim_parms);
3153 psys->clmd->sim_parms->effector_weights = NULL;
3154 psys->clmd->coll_parms= newdataadr(fd, psys->clmd->coll_parms);
3156 if(psys->clmd->sim_parms) {
3157 if(psys->clmd->sim_parms->presets > 10)
3158 psys->clmd->sim_parms->presets = 0;
3161 psys->hair_in_dm = psys->hair_out_dm = NULL;
3163 psys->clmd->point_cache = psys->pointcache;
3171 /* ************ READ MESH ***************** */
3173 static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
3178 for (i=0; i<totface; i++, tf++) {
3179 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
3180 if(tf->tpage && tf->tpage->id.us==0)
3181 tf->tpage->id.us= 1;
3185 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
3188 for(i=0; i<fdata->totlayer; i++) {
3189 CustomDataLayer *layer = &fdata->layers[i];
3191 if(layer->type == CD_MTFACE)
3192 lib_link_mtface(fd, me, layer->data, totface);
3197 static void lib_link_mesh(FileData *fd, Main *main)
3201 me= main->mesh.first;
3203 if(me->id.flag & LIB_NEEDLINK) {
3206 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3207 of library blocks that implement this.*/
3208 if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3209 if (me->adt) lib_link_animdata(fd, &me->id, me->adt);
3211 /* this check added for python created meshes */
3213 for(i=0; i<me->totcol; i++) {
3214 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
3219 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
3220 me->key= newlibadr_us(fd, me->id.lib, me->key);
3221 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
3223 lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
3224 if(me->mr && me->mr->levels.first)
3225 lib_link_customdata_mtface(fd, me, &me->mr->fdata,
3226 ((MultiresLevel*)me->mr->levels.first)->totface);
3228 me->id.flag -= LIB_NEEDLINK;
3234 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
3241 for (i=0; i<count; i++) {
3242 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
3244 mdverts[i].totweight=0;
3248 static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps, int external)
3253 for(i = 0; i < count; ++i) {
3254 mdisps[i].disps = newdataadr(fd, mdisps[i].disps);
3255 if(!external && !mdisps[i].disps)
3256 mdisps[i].totdisp = 0;
3261 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
3265 data->layers= newdataadr(fd, data->layers);
3266 data->external= newdataadr(fd, data->external);
3268 while (i < data->totlayer) {
3269 CustomDataLayer *layer = &data->layers[i];
3271 if(layer->flag & CD_FLAG_EXTERNAL)
3272 layer->flag &= ~CD_FLAG_IN_MEMORY;
3274 if (CustomData_verify_versions(data, i)) {
3275 layer->data = newdataadr(fd, layer->data);
3276 if(layer->type == CD_MDISPS)
3277 direct_link_mdisps(fd, count, layer->data, layer->flag & CD_FLAG_EXTERNAL);
3283 static void direct_link_mesh(FileData *fd, Mesh *mesh)
3285 mesh->mat= newdataadr(fd, mesh->mat);
3286 test_pointer_array(fd, (void **)&mesh->mat);
3288 mesh->mvert= newdataadr(fd, mesh->mvert);
3289 mesh->medge= newdataadr(fd, mesh->medge);
3290 mesh->mface= newdataadr(fd, mesh->mface);
3291 mesh->tface= newdataadr(fd, mesh->tface);