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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *****
34 #include "BLI_winstuff.h"
38 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
39 #include <stdlib.h> // for getenv atoi
40 #include <fcntl.h> // for open
41 #include <string.h> // for strrchr strncmp strstr
42 #include <math.h> // for fabs
45 #include <unistd.h> // for read close
46 #include <sys/param.h> // for MAXPATHLEN
48 #include <io.h> // for open close read
51 #include "DNA_anim_types.h"
52 #include "DNA_action_types.h"
53 #include "DNA_armature_types.h"
55 #include "DNA_actuator_types.h"
56 #include "DNA_boid_types.h"
57 #include "DNA_brush_types.h"
58 #include "DNA_camera_types.h"
59 #include "DNA_cloth_types.h"
60 #include "DNA_color_types.h"
61 #include "DNA_controller_types.h"
62 #include "DNA_constraint_types.h"
63 #include "DNA_curve_types.h"
64 #include "DNA_customdata_types.h"
65 #include "DNA_effect_types.h"
66 #include "DNA_fileglobal_types.h"
67 #include "DNA_genfile.h"
68 #include "DNA_group_types.h"
69 #include "DNA_gpencil_types.h"
70 #include "DNA_ipo_types.h"
71 #include "DNA_image_types.h"
72 #include "DNA_key_types.h"
73 #include "DNA_lattice_types.h"
74 #include "DNA_lamp_types.h"
75 #include "DNA_meta_types.h"
76 #include "DNA_material_types.h"
77 #include "DNA_mesh_types.h"
78 #include "DNA_meshdata_types.h"
79 #include "DNA_modifier_types.h"
80 #include "DNA_nla_types.h"
81 #include "DNA_node_types.h"
82 #include "DNA_object_types.h"
83 #include "DNA_object_force.h"
84 #include "DNA_object_fluidsim.h" // NT
85 #include "DNA_outliner_types.h"
86 #include "DNA_object_force.h"
87 #include "DNA_packedFile_types.h"
88 #include "DNA_particle_types.h"
89 #include "DNA_property_types.h"
90 #include "DNA_text_types.h"
91 #include "DNA_view3d_types.h"
92 #include "DNA_screen_types.h"
93 #include "DNA_sensor_types.h"
94 #include "DNA_sdna_types.h"
95 #include "DNA_scene_types.h"
96 #include "DNA_sequence_types.h"
97 #include "DNA_sound_types.h"
98 #include "DNA_space_types.h"
99 #include "DNA_texture_types.h"
100 #include "DNA_userdef_types.h"
101 #include "DNA_vfont_types.h"
102 #include "DNA_world_types.h"
103 #include "DNA_windowmanager_types.h"
105 #include "MEM_guardedalloc.h"
106 #include "BLI_blenlib.h"
107 #include "BLI_arithb.h"
108 #include "BLI_storage_types.h" // for relname flags
110 #include "BKE_animsys.h"
111 #include "BKE_action.h"
112 #include "BKE_armature.h"
113 #include "BKE_brush.h"
114 #include "BKE_cdderivedmesh.h"
115 #include "BKE_cloth.h"
116 #include "BKE_colortools.h"
117 #include "BKE_constraint.h"
118 #include "BKE_curve.h"
119 #include "BKE_customdata.h"
120 #include "BKE_deform.h"
121 #include "BKE_depsgraph.h"
122 #include "BKE_effect.h" /* give_parteff */
123 #include "BKE_fcurve.h"
124 #include "BKE_global.h" // for G
125 #include "BKE_group.h"
126 #include "BKE_image.h"
128 #include "BKE_key.h" //void set_four_ipo
129 #include "BKE_lattice.h"
130 #include "BKE_library.h" // for wich_libbase
131 #include "BKE_main.h" // for Main
132 #include "BKE_mesh.h" // for ME_ defines (patching)
133 #include "BKE_modifier.h"
134 #include "BKE_multires.h"
135 #include "BKE_node.h" // for tree type defines
136 #include "BKE_object.h"
137 #include "BKE_particle.h"
138 #include "BKE_pointcache.h"
139 #include "BKE_property.h" // for get_ob_property
140 #include "BKE_report.h"
141 #include "BKE_sca.h" // for init_actuator
142 #include "BKE_scene.h"
143 #include "BKE_softbody.h" // sbNew()
144 #include "BKE_bullet.h" // bsbNew()
145 #include "BKE_sculpt.h"
146 #include "BKE_sequence.h"
147 #include "BKE_texture.h" // for open_plugin_tex
148 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
149 #include "BKE_idprop.h"
151 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
152 //XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
153 //XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
154 #include "BLO_readfile.h"
155 #include "BLO_undofile.h"
156 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
158 #include "readfile.h"
160 #include "PIL_time.h"
165 Remark: still a weak point is the newadress() function, that doesnt solve reading from
166 multiple files at the same time
168 (added remark: oh, i thought that was solved? will look at that... (ton)
171 - Existing Library (Main) push or free
181 - read associated 'direct data'
182 - link direct data (internal and to LibBlock)
184 - read USER data, only when indicated (file is ~/.B.blend or .B25.blend)
186 - per Library (per Main)
189 - find LibBlocks and attach IDs to Main
190 - if external LibBlock
192 - or it's already read,
197 - read associated direct data
198 - link direct data (internal and to LibBlock)
200 - per Library with unread LibBlocks
205 - read associated direct data
206 - link direct data (internal and to LibBlock)
209 - link all LibBlocks and indirect pointers to libblocks
210 - initialize FileGlobal and copy pointers to Global
213 /* also occurs in library.c */
214 /* GS reads the memory pointed at in a specific ordering. There are,
215 * however two definitions for it. I have jotted them down here, both,
216 * but I think the first one is actually used. The thing is that
217 * big-endian systems might read this the wrong way round. OTOH, we
218 * constructed the IDs that are read out with this macro explicitly as
219 * well. I expect we'll sort it out soon... */
222 #define GS(a) (*((short *)(a)))
224 /* from misc_util: flip the bytes from x */
225 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
227 // only used here in readfile.c
228 #define SWITCH_LONGINT(a) { \
231 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
232 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
233 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
234 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
238 typedef struct OldNew {
243 typedef struct OldNewMap {
245 int nentries, entriessize;
251 /* local prototypes */
252 static void *read_struct(FileData *fd, BHead *bh, char *blockname);
255 static OldNewMap *oldnewmap_new(void)
257 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
259 onm->entriessize= 1024;
260 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
265 static int verg_oldnewmap(const void *v1, const void *v2)
267 const struct OldNew *x1=v1, *x2=v2;
269 if( x1->old > x2->old) return 1;
270 else if( x1->old < x2->old) return -1;
275 static void oldnewmap_sort(FileData *fd)
277 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
278 fd->libmap->sorted= 1;
281 /* nr is zero for data, and ID code for libdata */
282 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
286 if(oldaddr==NULL || newaddr==NULL) return;
288 if (onm->nentries==onm->entriessize) {
289 int osize= onm->entriessize;
290 OldNew *oentries= onm->entries;
292 onm->entriessize*= 2;
293 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
295 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
299 entry= &onm->entries[onm->nentries++];
301 entry->newp= newaddr;
305 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
309 if (onm->lasthit<onm->nentries-1) {
310 OldNew *entry= &onm->entries[++onm->lasthit];
312 if (entry->old==addr) {
318 for (i=0; i<onm->nentries; i++) {
319 OldNew *entry= &onm->entries[i];
321 if (entry->old==addr) {
332 /* for libdata, nr has ID code, no increment */
333 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
337 if(addr==NULL) return NULL;
339 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
341 OldNew entry_s, *entry;
345 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
349 if (id && (!lib || id->lib)) {
355 for (i=0; i<onm->nentries; i++) {
356 OldNew *entry= &onm->entries[i];
358 if (entry->old==addr) {
361 if (id && (!lib || id->lib)) {
370 static void oldnewmap_free_unused(OldNewMap *onm)
374 for (i=0; i<onm->nentries; i++) {
375 OldNew *entry= &onm->entries[i];
377 MEM_freeN(entry->newp);
383 static void oldnewmap_clear(OldNewMap *onm)
389 static void oldnewmap_free(OldNewMap *onm)
391 MEM_freeN(onm->entries);
397 static void read_libraries(FileData *basefd, ListBase *mainlist);
399 /* ************ help functions ***************** */
401 static void add_main_to_main(Main *mainvar, Main *from)
403 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
406 a= set_listbasepointers(mainvar, lbarray);
407 a= set_listbasepointers(from, fromarray);
409 addlisttolist(lbarray[a], fromarray[a]);
413 void blo_join_main(ListBase *mainlist)
415 Main *tojoin, *mainl;
418 mainl= mainlist->first;
419 while ((tojoin= mainl->next)) {
420 add_main_to_main(mainl, tojoin);
421 BLI_remlink(mainlist, tojoin);
426 static void split_libdata(ListBase *lb, Main *first)
438 if(mainvar->curlib==id->lib) {
439 lbn= wich_libbase(mainvar, GS(id->name));
441 BLI_addtail(lbn, id);
444 mainvar= mainvar->next;
446 if(mainvar==0) printf("error split_libdata\n");
452 void blo_split_main(ListBase *mainlist, Main *main)
454 ListBase *lbarray[MAX_LIBARRAY];
458 mainlist->first= mainlist->last= main;
461 if(main->library.first==NULL)
464 for (lib= main->library.first; lib; lib= lib->id.next) {
465 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
466 libmain->curlib= lib;
467 BLI_addtail(mainlist, libmain);
470 i= set_listbasepointers(main, lbarray);
472 split_libdata(lbarray[i], main->next);
475 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
476 static void cleanup_path(const char *relabase, char *name)
478 char filename[FILE_MAXFILE];
480 BLI_splitdirstring(name, filename);
481 BLI_cleanup_dir(relabase, name);
482 strcat(name, filename);
485 static void read_file_version(FileData *fd, Main *main)
489 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
490 if (bhead->code==GLOB) {
491 FileGlobal *fg= read_struct(fd, bhead, "Global");
493 main->subversionfile= fg->subversion;
494 main->minversionfile= fg->minversion;
495 main->minsubversionfile= fg->minsubversion;
498 else if (bhead->code==ENDB)
505 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *name, const char *relabase)
509 char name1[FILE_MAXDIR+FILE_MAXFILE];
511 strncpy(name1, name, sizeof(name1)-1);
512 cleanup_path(relabase, name1);
513 // printf("blo_find_main: original in %s\n", name);
514 // printf("blo_find_main: converted to %s\n", name1);
516 for (m= mainlist->first; m; m= m->next) {
517 char *libname= (m->curlib)?m->curlib->filename:m->name;
519 if (BLI_streq(name1, libname)) {
520 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
525 m= MEM_callocN(sizeof(Main), "find_main");
526 BLI_addtail(mainlist, m);
528 lib= alloc_libblock(&m->library, ID_LI, "lib");
529 strncpy(lib->name, name, sizeof(lib->name)-1);
530 BLI_strncpy(lib->filename, name1, sizeof(lib->filename));
534 read_file_version(fd, m);
536 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", name);
541 /* ************ FILE PARSING ****************** */
543 static void switch_endian_bh4(BHead4 *bhead)
545 /* the ID_.. codes */
546 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
548 if (bhead->code != ENDB) {
549 SWITCH_INT(bhead->len);
550 SWITCH_INT(bhead->SDNAnr);
551 SWITCH_INT(bhead->nr);
555 static void switch_endian_bh8(BHead8 *bhead)
557 /* the ID_.. codes */
558 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
560 if (bhead->code != ENDB) {
561 SWITCH_INT(bhead->len);
562 SWITCH_INT(bhead->SDNAnr);
563 SWITCH_INT(bhead->nr);
567 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
569 BHead4 *bhead4 = (BHead4 *) bhead;
570 #if defined(WIN32) && !defined(FREE_WINDOWS)
576 bhead4->code= bhead8->code;
577 bhead4->len= bhead8->len;
579 if (bhead4->code != ENDB) {
581 // why is this here ??
582 if (do_endian_swap) {
583 SWITCH_LONGINT(bhead8->old);
586 /* this patch is to avoid a long long being read from not-eight aligned positions
587 is necessary on any modern 64bit architecture) */
588 memcpy(&old, &bhead8->old, 8);
589 bhead4->old = (int) (old >> 3);
591 bhead4->SDNAnr= bhead8->SDNAnr;
592 bhead4->nr= bhead8->nr;
596 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
598 BHead8 *bhead8 = (BHead8 *) bhead;
600 bhead8->code= bhead4->code;
601 bhead8->len= bhead4->len;
603 if (bhead8->code != ENDB) {
604 bhead8->old= bhead4->old;
605 bhead8->SDNAnr= bhead4->SDNAnr;
606 bhead8->nr= bhead4->nr;
610 static BHeadN *get_bhead(FileData *fd)
615 BHeadN *new_bhead = 0;
621 /* not strictly needed but shuts valgrind up
622 * since uninitialized memory gets compared */
623 memset(&bhead8, 0, sizeof(BHead8));
624 memset(&bhead4, 0, sizeof(BHead4));
625 memset(&bhead, 0, sizeof(BHead));
627 // First read the bhead structure.
628 // Depending on the platform the file was written on this can
629 // be a big or little endian BHead4 or BHead8 structure.
631 // As usual 'ENDB' (the last *partial* bhead of the file)
632 // needs some special handling. We don't want to EOF just yet.
634 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
636 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
638 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
639 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
640 switch_endian_bh4(&bhead4);
643 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
644 bh8_from_bh4(&bhead, &bhead4);
646 memcpy(&bhead, &bhead4, sizeof(bhead));
654 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
656 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
657 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
658 switch_endian_bh8(&bhead8);
661 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
662 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
664 memcpy(&bhead, &bhead8, sizeof(bhead));
672 /* make sure people are not trying to pass bad blend files */
673 if (bhead.len < 0) fd->eof = 1;
675 // bhead now contains the (converted) bhead structure. Now read
676 // the associated data and put everything in a BHeadN (creative naming !)
679 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
681 new_bhead->next = new_bhead->prev = 0;
682 new_bhead->bhead = bhead;
684 readsize = fd->read(fd, new_bhead + 1, bhead.len);
686 if (readsize != bhead.len) {
688 MEM_freeN(new_bhead);
698 // We've read a new block. Now add it to the list
702 BLI_addtail(&fd->listbase, new_bhead);
708 BHead *blo_firstbhead(FileData *fd)
714 // Read in a new block if necessary
716 new_bhead = fd->listbase.first;
717 if (new_bhead == 0) {
718 new_bhead = get_bhead(fd);
722 bhead = &new_bhead->bhead;
728 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
730 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
731 BHeadN *prev= bheadn->prev;
733 return prev?&prev->bhead:NULL;
736 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
738 BHeadN *new_bhead = NULL;
742 // bhead is actually a sub part of BHeadN
743 // We calculate the BHeadN pointer from the BHead pointer below
744 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
746 // get the next BHeadN. If it doesn't exist we read in the next one
747 new_bhead = new_bhead->next;
748 if (new_bhead == 0) {
749 new_bhead = get_bhead(fd);
754 // here we do the reverse:
755 // go from the BHeadN pointer to the BHead pointer
756 bhead = &new_bhead->bhead;
762 static void decode_blender_header(FileData *fd)
764 char header[SIZEOFBLENDERHEADER], num[4];
767 // read in the header data
768 readsize = fd->read(fd, header, sizeof(header));
770 if (readsize == sizeof(header)) {
771 if(strncmp(header, "BLENDER", 7) == 0) {
772 int remove_this_endian_test= 1;
774 fd->flags |= FD_FLAGS_FILE_OK;
776 // what size are pointers in the file ?
778 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
779 if (sizeof(void *) != 4) {
780 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
783 if (sizeof(void *) != 8) {
784 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
788 // is the file saved in a different endian
790 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
791 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
794 // get the version number
796 memcpy(num, header+9, 3);
798 fd->fileversion = atoi(num);
803 static int read_file_dna(FileData *fd)
807 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
808 if (bhead->code==DNA1) {
809 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
811 fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
814 fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
815 /* used to retrieve ID names from (bhead+1) */
816 fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
820 } else if (bhead->code==ENDB)
827 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
829 int readsize = read(filedata->filedes, buffer, size);
834 filedata->seek += readsize;
840 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
842 int readsize = gzread(filedata->gzfiledes, buffer, size);
847 filedata->seek += readsize;
853 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
855 // don't read more bytes then there are available in the buffer
856 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
858 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
859 filedata->seek += readsize;
864 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
866 static unsigned int seek= 1<<30; /* the current position */
867 static unsigned int offset= 0; /* size of previous chunks */
868 static MemFileChunk *chunk=NULL;
869 unsigned int chunkoffset, readsize, totread;
871 if(size==0) return 0;
873 if(seek != (unsigned int)filedata->seek) {
874 chunk= filedata->memfile->chunks.first;
878 if(seek + chunk->size > (unsigned) filedata->seek) break;
883 seek= filedata->seek;
890 /* first check if it's on the end if current chunk */
891 if(seek-offset == chunk->size) {
892 offset+= chunk->size;
896 /* debug, should never happen */
898 printf("illegal read, chunk zero\n");
902 chunkoffset= seek-offset;
903 readsize= size-totread;
905 /* data can be spread over multiple chunks, so clamp size
906 * to within this chunk, and then it will read further in
908 if(chunkoffset+readsize > chunk->size)
909 readsize= chunk->size-chunkoffset;
911 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
913 filedata->seek += readsize;
915 } while(totread < size);
923 static FileData *filedata_new(void)
925 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
928 fd->gzfiledes = NULL;
930 /* XXX, this doesn't need to be done all the time,
931 * but it keeps us reentrant, remove once we have
932 * a lib that provides a nice lock. - zr
934 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
936 fd->datamap = oldnewmap_new();
937 fd->globmap = oldnewmap_new();
938 fd->libmap = oldnewmap_new();
943 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
945 decode_blender_header(fd);
947 if (fd->flags & FD_FLAGS_FILE_OK) {
948 if (!read_file_dna(fd)) {
949 BKE_report(reports, RPT_ERROR, "File incomplete");
950 blo_freefiledata(fd);
955 BKE_report(reports, RPT_ERROR, "File is not a Blender file");
956 blo_freefiledata(fd);
963 /* cannot be called with relative paths anymore! */
964 /* on each new library added, it now checks for the current FileData and expands relativeness */
965 FileData *blo_openblenderfile(char *name, ReportList *reports)
969 gzfile= gzopen(name, "rb");
971 if (NULL == gzfile) {
972 BKE_report(reports, RPT_ERROR, "Unable to open");
975 FileData *fd = filedata_new();
976 fd->gzfiledes = gzfile;
977 fd->read = fd_read_gzip_from_file;
979 /* needed for library_append and read_libraries */
980 BLI_strncpy(fd->filename, name, sizeof(fd->filename));
982 return blo_decode_and_check(fd, reports);
986 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
988 if (!mem || memsize<SIZEOFBLENDERHEADER) {
989 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
992 FileData *fd= filedata_new();
994 fd->buffersize= memsize;
995 fd->read= fd_read_from_memory;
996 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
998 return blo_decode_and_check(fd, reports);
1002 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
1005 BKE_report(reports, RPT_ERROR, "Unable to open");
1008 FileData *fd= filedata_new();
1009 fd->memfile= memfile;
1011 fd->read= fd_read_from_memfile;
1012 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
1014 return blo_decode_and_check(fd, reports);
1019 void blo_freefiledata(FileData *fd)
1023 if (fd->filedes != -1) {
1027 if (fd->gzfiledes != NULL)
1029 gzclose(fd->gzfiledes);
1032 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1033 MEM_freeN(fd->buffer);
1037 // Free all BHeadN data blocks
1038 BLI_freelistN(&fd->listbase);
1041 DNA_sdna_free(fd->memsdna);
1043 DNA_sdna_free(fd->filesdna);
1045 MEM_freeN(fd->compflags);
1048 oldnewmap_free(fd->datamap);
1050 oldnewmap_free(fd->globmap);
1052 oldnewmap_free(fd->imamap);
1053 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1054 oldnewmap_free(fd->libmap);
1056 MEM_freeN(fd->bheadmap);
1062 /* ************ DIV ****************** */
1064 int BLO_has_bfile_extension(char *str)
1066 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1069 /* ************** OLD POINTERS ******************* */
1071 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1073 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1076 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1078 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1081 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1083 if(fd->imamap && adr)
1084 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1089 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1091 return oldnewmap_liblookup(fd->libmap, adr, lib);
1094 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1096 ID *id= newlibadr(fd, lib, adr);
1104 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1108 for (i=0; i<fd->libmap->nentries; i++) {
1109 OldNew *entry= &fd->libmap->entries[i];
1111 if (old==entry->newp && entry->nr==ID_ID) {
1113 if(new) entry->nr= GS( ((ID *)new)->name );
1119 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1123 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1126 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1130 change_idid_adr_fd(fd, old, new);
1135 /* lib linked proxy objects point to our local data, we need
1136 * to clear that pointer before reading the undo memfile since
1137 * the object might be removed, it is set again in reading
1138 * if the local object still exists */
1139 void blo_clear_proxy_pointers_from_lib(FileData *fd, Main *oldmain)
1141 Object *ob= oldmain->object.first;
1143 for(;ob; ob= ob->id.next)
1145 ob->proxy_from= NULL;
1148 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1150 Image *ima= oldmain->image.first;
1151 Scene *sce= oldmain->scene.first;
1153 fd->imamap= oldnewmap_new();
1155 for(;ima; ima= ima->id.next) {
1156 Link *ibuf= ima->ibufs.first;
1157 for(; ibuf; ibuf= ibuf->next)
1158 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1160 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1162 for(; sce; sce= sce->id.next) {
1165 for(node= sce->nodetree->nodes.first; node; node= node->next)
1166 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1171 /* set old main image ibufs to zero if it has been restored */
1172 /* this works because freeing old main only happens after this call */
1173 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1175 OldNew *entry= fd->imamap->entries;
1176 Image *ima= oldmain->image.first;
1177 Scene *sce= oldmain->scene.first;
1180 /* used entries were restored, so we put them to zero */
1181 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1186 for(;ima; ima= ima->id.next) {
1189 /* this mirrors direct_link_image */
1190 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1192 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1193 BLI_remlink(&ima->ibufs, ibuf);
1195 ima->gputexture= NULL;
1199 ima->gputexture= newimaadr(fd, ima->gputexture);
1201 for(; sce; sce= sce->id.next) {
1204 for(node= sce->nodetree->nodes.first; node; node= node->next)
1205 node->preview= newimaadr(fd, node->preview);
1210 /* undo file support: add all library pointers in lookup */
1211 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1213 Main *ptr= mainlist->first;
1214 ListBase *lbarray[MAX_LIBARRAY];
1216 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1217 int i= set_listbasepointers(ptr, lbarray);
1220 for(id= lbarray[i]->first; id; id= id->next)
1221 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1227 /* ********** END OLD POINTERS ****************** */
1228 /* ********** READ FILE ****************** */
1230 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1232 int blocksize, nblocks;
1235 data= (char *)(bhead+1);
1236 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1240 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1246 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1251 /* switch is based on file dna */
1252 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1253 switch_endian_structs(fd->filesdna, bh);
1255 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1256 if(fd->compflags[bh->SDNAnr]==2) {
1257 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1259 temp= MEM_mallocN(bh->len, blockname);
1260 memcpy(temp, (bh+1), bh->len);
1268 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1272 if(lb->first==NULL) return;
1274 lb->first= newdataadr(fd, lb->first);
1278 ln->next= newdataadr(fd, ln->next);
1286 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1291 if(lb->first==0) return;
1292 poin= newdataadr(fd, lb->first);
1294 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1301 poin= newdataadr(fd, ln->next);
1303 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1313 static void test_pointer_array(FileData *fd, void **mat)
1315 #if defined(WIN32) && !defined(FREE_WINDOWS)
1316 __int64 *lpoin, *lmat;
1318 long long *lpoin, *lmat;
1320 int len, *ipoin, *imat;
1322 /* manually convert the pointer array in
1323 * the old dna format to a pointer array in
1324 * the new dna format.
1327 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1329 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1330 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1334 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1335 SWITCH_LONGINT(*lpoin);
1336 *ipoin= (int) ((*lpoin) >> 3);
1344 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1345 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1359 /* ************ READ ID Properties *************** */
1361 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1362 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1364 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1369 /*since we didn't save the extra buffer, set totallen to len.*/
1370 prop->totallen = prop->len;
1371 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1373 if (switch_endian) {
1374 test_pointer_array(fd, prop->data.pointer);
1375 array= (IDProperty**) prop->data.pointer;
1377 for(i=0; i<prop->len; i++)
1378 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1382 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1387 /*since we didn't save the extra buffer, set totallen to len.*/
1388 prop->totallen = prop->len;
1389 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1391 if (switch_endian) {
1392 if(prop->subtype == IDP_GROUP) {
1393 test_pointer_array(fd, prop->data.pointer);
1394 array= prop->data.pointer;
1396 for(i=0; i<prop->len; i++)
1397 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1399 else if(prop->subtype == IDP_DOUBLE) {
1400 for (i=0; i<prop->len; i++) {
1401 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1404 for (i=0; i<prop->len; i++) {
1405 SWITCH_INT(((int*)prop->data.pointer)[i]);
1411 static void IDP_DirectLinkString(IDProperty *prop, int switch_endian, FileData *fd)
1413 /*since we didn't save the extra string buffer, set totallen to len.*/
1414 prop->totallen = prop->len;
1415 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1418 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1420 ListBase *lb = &prop->data.group;
1425 /*Link child id properties now*/
1426 for (loop=prop->data.group.first; loop; loop=loop->next) {
1427 IDP_DirectLinkProperty(loop, switch_endian, fd);
1431 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1433 switch (prop->type) {
1435 IDP_DirectLinkGroup(prop, switch_endian, fd);
1438 IDP_DirectLinkString(prop, switch_endian, fd);
1441 IDP_DirectLinkArray(prop, switch_endian, fd);
1444 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1447 /*erg, stupid doubles. since I'm storing them
1448 in the same field as int val; val2 in the
1449 IDPropertyData struct, they have to deal with
1450 endianness specifically
1452 in theory, val and val2 would've already been swapped
1453 if switch_endian is true, so we have to first unswap
1454 them then reswap them as a single 64-bit entity.
1457 if (switch_endian) {
1458 SWITCH_INT(prop->data.val);
1459 SWITCH_INT(prop->data.val2);
1460 SWITCH_LONGINT(prop->data.val);
1468 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1472 /* ************ READ CurveMapping *************** */
1474 /* cuma itself has been read! */
1475 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1479 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1480 cumap->flag &= ~CUMA_PREMULLED;
1482 for(a=0; a<CM_TOT; a++) {
1483 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1484 cumap->cm[a].table= NULL;
1488 /* ************ READ Brush *************** */
1489 /* library brush linking after fileread */
1490 static void lib_link_brush(FileData *fd, Main *main)
1496 /* only link ID pointers */
1497 for(brush= main->brush.first; brush; brush= brush->id.next) {
1498 if(brush->id.flag & LIB_NEEDLINK) {
1499 brush->id.flag -= LIB_NEEDLINK;
1501 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1503 for(a=0; a<MAX_MTEX; a++) {
1504 mtex= brush->mtex[a];
1506 mtex->tex= newlibadr_us(fd, brush->id.lib, mtex->tex);
1509 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1514 static void direct_link_brush(FileData *fd, Brush *brush)
1516 /* brush itself has been read */
1519 for(a=0; a<MAX_MTEX; a++)
1520 brush->mtex[a]= newdataadr(fd, brush->mtex[a]);
1523 brush->curve= newdataadr(fd, brush->curve);
1525 direct_link_curvemapping(fd, brush->curve);
1527 brush_curve_preset(brush, BRUSH_PRESET_SHARP);
1530 static void direct_link_script(FileData *fd, Script *script)
1533 SCRIPT_SET_NULL(script)
1537 /* ************ READ PACKEDFILE *************** */
1539 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1541 PackedFile *pf= newdataadr(fd, oldpf);
1544 pf->data= newdataadr(fd, pf->data);
1550 /* ************ READ IMAGE PREVIEW *************** */
1552 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1554 PreviewImage *prv= newdataadr(fd, old_prv);
1558 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1560 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1568 /* ************ READ SCRIPTLINK *************** */
1570 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1574 for(i=0; i<slink->totscript; i++) {
1575 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1579 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1581 slink->scripts= newdataadr(fd, slink->scripts);
1582 test_pointer_array(fd, (void **)&slink->scripts);
1584 slink->flag= newdataadr(fd, slink->flag);
1586 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1589 for(a=0; a<slink->totscript; a++) {
1590 SWITCH_SHORT(slink->flag[a]);
1595 /* ************ READ ANIMATION STUFF ***************** */
1597 /* Legacy Data Support (for Version Patching) ----------------------------- */
1599 // XXX depreceated - old animation system
1600 static void lib_link_ipo(FileData *fd, Main *main)
1604 ipo= main->ipo.first;
1606 if(ipo->id.flag & LIB_NEEDLINK) {
1608 for(icu= ipo->curve.first; icu; icu= icu->next) {
1610 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1612 ipo->id.flag -= LIB_NEEDLINK;
1618 // XXX depreceated - old animation system
1619 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1623 link_list(fd, &(ipo->curve));
1624 icu= ipo->curve.first;
1626 icu->bezt= newdataadr(fd, icu->bezt);
1627 icu->bp= newdataadr(fd, icu->bp);
1628 icu->driver= newdataadr(fd, icu->driver);
1633 // XXX depreceated - old animation system
1634 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1636 bActionStrip *strip;
1637 bActionModifier *amod;
1639 for (strip=striplist->first; strip; strip=strip->next){
1640 strip->object = newlibadr(fd, id->lib, strip->object);
1641 strip->act = newlibadr_us(fd, id->lib, strip->act);
1642 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1643 for(amod= strip->modifiers.first; amod; amod= amod->next)
1644 amod->ob= newlibadr(fd, id->lib, amod->ob);
1648 // XXX depreceated - old animation system
1649 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1651 bActionStrip *strip;
1653 link_list(fd, strips);
1655 for(strip= strips->first; strip; strip= strip->next)
1656 link_list(fd, &strip->modifiers);
1659 // XXX depreceated - old animation system
1660 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1662 bConstraintChannel *chan;
1664 for (chan=chanbase->first; chan; chan=chan->next){
1665 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1669 /* Data Linking ----------------------------- */
1671 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1675 for (fcm= list->first; fcm; fcm= fcm->next) {
1676 /* data for specific modifiers */
1677 switch (fcm->type) {
1678 case FMODIFIER_TYPE_PYTHON:
1680 FMod_Python *data= (FMod_Python *)fcm->data;
1681 data->script = newlibadr(fd, id->lib, data->script);
1688 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1692 /* relink ID-block references... */
1693 for (fcu= list->first; fcu; fcu= fcu->next) {
1696 ChannelDriver *driver= fcu->driver;
1699 for (dtar= driver->targets.first; dtar; dtar= dtar->next)
1700 dtar->id= newlibadr(fd, id->lib, dtar->id);
1704 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1709 /* NOTE: this assumes that link_list has already been called on the list */
1710 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1714 for (fcm= list->first; fcm; fcm= fcm->next) {
1715 /* relink general data */
1716 fcm->data = newdataadr(fd, fcm->data);
1719 /* do relinking of data for specific types */
1720 switch (fcm->type) {
1721 case FMODIFIER_TYPE_GENERATOR:
1723 FMod_Generator *data= (FMod_Generator *)fcm->data;
1725 data->coefficients= newdataadr(fd, data->coefficients);
1728 case FMODIFIER_TYPE_ENVELOPE:
1730 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1732 data->data= newdataadr(fd, data->data);
1735 case FMODIFIER_TYPE_PYTHON:
1737 FMod_Python *data= (FMod_Python *)fcm->data;
1739 data->prop = newdataadr(fd, data->prop);
1740 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1747 /* NOTE: this assumes that link_list has already been called on the list */
1748 static void direct_link_fcurves(FileData *fd, ListBase *list)
1752 /* link F-Curve data to F-Curve again (non ID-libs) */
1753 for (fcu= list->first; fcu; fcu= fcu->next) {
1755 fcu->bezt= newdataadr(fd, fcu->bezt);
1756 fcu->fpt= newdataadr(fd, fcu->fpt);
1759 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1762 fcu->grp= newdataadr(fd, fcu->grp);
1765 fcu->driver= newdataadr(fd, fcu->driver);
1767 ChannelDriver *driver= fcu->driver;
1770 /* relink targets and their paths */
1771 link_list(fd, &driver->targets);
1772 for (dtar= driver->targets.first; dtar; dtar= dtar->next)
1773 dtar->rna_path= newdataadr(fd, dtar->rna_path);
1777 link_list(fd, &fcu->modifiers);
1778 direct_link_fmodifiers(fd, &fcu->modifiers);
1783 static void lib_link_action(FileData *fd, Main *main)
1786 bActionChannel *chan;
1788 for (act= main->action.first; act; act= act->id.next) {
1789 if (act->id.flag & LIB_NEEDLINK) {
1790 act->id.flag -= LIB_NEEDLINK;
1792 // XXX depreceated - old animation system <<<
1793 for (chan=act->chanbase.first; chan; chan=chan->next) {
1794 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1795 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1797 // >>> XXX depreceated - old animation system
1799 lib_link_fcurves(fd, &act->id, &act->curves);
1804 static void direct_link_action(FileData *fd, bAction *act)
1806 bActionChannel *achan; // XXX depreceated - old animation system
1809 link_list(fd, &act->curves);
1810 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1811 link_list(fd, &act->groups);
1812 link_list(fd, &act->markers);
1814 // XXX depreceated - old animation system <<<
1815 for (achan = act->chanbase.first; achan; achan=achan->next) {
1816 achan->grp= newdataadr(fd, achan->grp);
1818 link_list(fd, &achan->constraintChannels);
1820 // >>> XXX depreceated - old animation system
1822 direct_link_fcurves(fd, &act->curves);
1824 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1825 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1826 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1830 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
1834 for (strip= list->first; strip; strip= strip->next) {
1835 /* check strip's children */
1836 lib_link_nladata_strips(fd, id, &strip->strips);
1838 /* reassign the counted-reference to action */
1839 strip->act = newlibadr_us(fd, id->lib, strip->act);
1843 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
1847 /* we only care about the NLA strips inside the tracks */
1848 for (nlt= list->first; nlt; nlt= nlt->next) {
1849 lib_link_nladata_strips(fd, id, &nlt->strips);
1853 /* This handles Animato NLA-Strips linking
1854 * NOTE: this assumes that link_list has already been called on the list
1856 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
1860 for (strip= list->first; strip; strip= strip->next) {
1861 /* strip's child strips */
1862 link_list(fd, &strip->strips);
1863 direct_link_nladata_strips(fd, &strip->strips);
1865 /* strip's F-Curves */
1866 link_list(fd, &strip->fcurves);
1867 direct_link_fcurves(fd, &strip->fcurves);
1869 /* strip's F-Modifiers */
1870 link_list(fd, &strip->modifiers);
1871 direct_link_fcurves(fd, &strip->modifiers);
1875 /* NOTE: this assumes that link_list has already been called on the list */
1876 static void direct_link_nladata(FileData *fd, ListBase *list)
1880 for (nlt= list->first; nlt; nlt= nlt->next) {
1881 /* relink list of strips */
1882 link_list(fd, &nlt->strips);
1884 /* relink strip data */
1885 direct_link_nladata_strips(fd, &nlt->strips);
1891 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
1896 /* here, we're only interested in the ID pointer stored in some of the paths */
1897 for (ks= list->first; ks; ks= ks->next) {
1898 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1899 ksp->id= newlibadr(fd, id->lib, ksp->id);
1904 /* NOTE: this assumes that link_list has already been called on the list */
1905 static void direct_link_keyingsets(FileData *fd, ListBase *list)
1910 /* link KeyingSet data to KeyingSet again (non ID-libs) */
1911 for (ks= list->first; ks; ks= ks->next) {
1913 link_list(fd, &ks->paths);
1915 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1917 ksp->rna_path= newdataadr(fd, ksp->rna_path);
1924 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
1929 /* link action data */
1930 adt->action= newlibadr_us(fd, id->lib, adt->action);
1931 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
1934 lib_link_fcurves(fd, id, &adt->drivers);
1936 /* overrides don't have lib-link for now, so no need to do anything */
1939 lib_link_nladata(fd, id, &adt->nla_tracks);
1942 static void direct_link_animdata(FileData *fd, AnimData *adt)
1944 /* NOTE: must have called newdataadr already before doing this... */
1949 link_list(fd, &adt->drivers);
1950 direct_link_fcurves(fd, &adt->drivers);
1952 /* link overrides */
1956 link_list(fd, &adt->nla_tracks);
1957 direct_link_nladata(fd, &adt->nla_tracks);
1959 /* clear temp pointers that may have been set... */
1960 // TODO: it's probably only a small cost to reload this anyway...
1961 adt->actstrip= NULL;
1964 /* ************ READ NODE TREE *************** */
1966 /* singe node tree (also used for material/scene trees), ntree is not NULL */
1967 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
1971 if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
1973 for(node= ntree->nodes.first; node; node= node->next)
1974 node->id= newlibadr_us(fd, id->lib, node->id);
1977 /* library ntree linking after fileread */
1978 static void lib_link_nodetree(FileData *fd, Main *main)
1982 /* only link ID pointers */
1983 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1984 if(ntree->id.flag & LIB_NEEDLINK) {
1985 ntree->id.flag -= LIB_NEEDLINK;
1986 lib_link_ntree(fd, &ntree->id, ntree);
1991 /* verify types for nodes and groups, all data has to be read */
1992 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
1994 static void lib_verify_nodetree(Main *main, int open)
2001 /* this crashes blender on undo/redo
2003 reinit_nodesystem();
2006 /* now create the own typeinfo structs an verify nodes */
2007 /* here we still assume no groups in groups */
2008 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2009 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
2010 ntreeMakeOwnType(ntree); /* for group usage */
2013 /* now verify all types in material trees, groups are set OK now */
2014 for(ma= main->mat.first; ma; ma= ma->id.next) {
2016 ntreeVerifyTypes(ma->nodetree);
2018 /* and scene trees */
2019 for(sce= main->scene.first; sce; sce= sce->id.next) {
2021 ntreeVerifyTypes(sce->nodetree);
2023 /* and texture trees */
2024 for(tx= main->tex.first; tx; tx= tx->id.next) {
2026 ntreeVerifyTypes(tx->nodetree);
2032 /* ntree itself has been read! */
2033 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2035 /* note: writing and reading goes in sync, for speed */
2040 ntree->init= 0; /* to set callbacks and force setting types */
2041 ntree->owntype= NULL;
2042 ntree->timecursor= NULL;
2044 ntree->adt= newdataadr(fd, ntree->adt);
2045 direct_link_animdata(fd, ntree->adt);
2047 link_list(fd, &ntree->nodes);
2048 for(node= ntree->nodes.first; node; node= node->next) {
2049 if(node->type == NODE_DYNAMIC) {
2051 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
2052 node->typeinfo= NULL;
2055 node->storage= newdataadr(fd, node->storage);
2058 /* could be handlerized at some point */
2059 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
2060 direct_link_curvemapping(fd, node->storage);
2061 else if(ntree->type==NTREE_COMPOSIT) {
2062 if( ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB))
2063 direct_link_curvemapping(fd, node->storage);
2064 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2065 ((ImageUser *)node->storage)->ok= 1;
2067 else if( ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) ) {
2068 direct_link_curvemapping(fd, node->storage);
2071 link_list(fd, &node->inputs);
2072 link_list(fd, &node->outputs);
2074 link_list(fd, &ntree->links);
2076 /* and we connect the rest */
2077 for(node= ntree->nodes.first; node; node= node->next) {
2078 node->preview= newimaadr(fd, node->preview);
2080 for(sock= node->inputs.first; sock; sock= sock->next)
2081 sock->link= newdataadr(fd, sock->link);
2082 for(sock= node->outputs.first; sock; sock= sock->next)
2083 sock->ns.data= NULL;
2085 for(link= ntree->links.first; link; link= link->next) {
2086 link->fromnode= newdataadr(fd, link->fromnode);
2087 link->tonode= newdataadr(fd, link->tonode);
2088 link->fromsock= newdataadr(fd, link->fromsock);
2089 link->tosock= newdataadr(fd, link->tosock);
2092 /* set selin and selout */
2093 for(node= ntree->nodes.first; node; node= node->next) {
2094 for(sock= node->inputs.first; sock; sock= sock->next) {
2095 if(sock->flag & SOCK_SEL) {
2100 for(sock= node->outputs.first; sock; sock= sock->next) {
2101 if(sock->flag & SOCK_SEL) {
2102 ntree->selout= sock;
2108 /* type verification is in lib-link */
2111 /* ************ READ ARMATURE ***************** */
2113 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2117 for (con = conlist->first; con; con=con->next) {
2118 /* patch for error introduced by changing constraints (dunno how) */
2119 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2120 if(con->data==NULL) {
2121 con->type= CONSTRAINT_TYPE_NULL;
2123 /* own ipo, all constraints have it */
2124 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2126 switch (con->type) {
2127 case CONSTRAINT_TYPE_PYTHON:
2129 bPythonConstraint *data= (bPythonConstraint*)con->data;
2130 bConstraintTarget *ct;
2132 for (ct= data->targets.first; ct; ct= ct->next)
2133 ct->tar = newlibadr(fd, id->lib, ct->tar);
2135 data->text = newlibadr(fd, id->lib, data->text);
2136 //IDP_LibLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2139 case CONSTRAINT_TYPE_ACTION:
2141 bActionConstraint *data;
2142 data= ((bActionConstraint*)con->data);
2143 data->tar = newlibadr(fd, id->lib, data->tar);
2144 data->act = newlibadr(fd, id->lib, data->act);
2147 case CONSTRAINT_TYPE_LOCLIKE:
2149 bLocateLikeConstraint *data;
2150 data= ((bLocateLikeConstraint*)con->data);
2151 data->tar = newlibadr(fd, id->lib, data->tar);
2154 case CONSTRAINT_TYPE_ROTLIKE:
2156 bRotateLikeConstraint *data;
2157 data= ((bRotateLikeConstraint*)con->data);
2158 data->tar = newlibadr(fd, id->lib, data->tar);
2161 case CONSTRAINT_TYPE_SIZELIKE:
2163 bSizeLikeConstraint *data;
2164 data= ((bSizeLikeConstraint*)con->data);
2165 data->tar = newlibadr(fd, id->lib, data->tar);
2168 case CONSTRAINT_TYPE_KINEMATIC:
2170 bKinematicConstraint *data;
2171 data = ((bKinematicConstraint*)con->data);
2172 data->tar = newlibadr(fd, id->lib, data->tar);
2173 data->poletar = newlibadr(fd, id->lib, data->poletar);
2176 case CONSTRAINT_TYPE_TRACKTO:
2178 bTrackToConstraint *data;
2179 data = ((bTrackToConstraint*)con->data);
2180 data->tar = newlibadr(fd, id->lib, data->tar);
2183 case CONSTRAINT_TYPE_MINMAX:
2185 bMinMaxConstraint *data;
2186 data = ((bMinMaxConstraint*)con->data);
2187 data->tar = newlibadr(fd, id->lib, data->tar);
2190 case CONSTRAINT_TYPE_LOCKTRACK:
2192 bLockTrackConstraint *data;
2193 data= ((bLockTrackConstraint*)con->data);
2194 data->tar = newlibadr(fd, id->lib, data->tar);
2197 case CONSTRAINT_TYPE_FOLLOWPATH:
2199 bFollowPathConstraint *data;
2200 data= ((bFollowPathConstraint*)con->data);
2201 data->tar = newlibadr(fd, id->lib, data->tar);
2204 case CONSTRAINT_TYPE_STRETCHTO:
2206 bStretchToConstraint *data;
2207 data= ((bStretchToConstraint*)con->data);
2208 data->tar = newlibadr(fd, id->lib, data->tar);
2211 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
2213 bRigidBodyJointConstraint *data;
2214 data= ((bRigidBodyJointConstraint*)con->data);
2215 data->tar = newlibadr(fd, id->lib, data->tar);
2218 case CONSTRAINT_TYPE_CLAMPTO:
2220 bClampToConstraint *data;
2221 data= ((bClampToConstraint*)con->data);
2222 data->tar = newlibadr(fd, id->lib, data->tar);
2225 case CONSTRAINT_TYPE_CHILDOF:
2227 bChildOfConstraint *data;
2228 data= ((bChildOfConstraint*)con->data);
2229 data->tar = newlibadr(fd, id->lib, data->tar);
2232 case CONSTRAINT_TYPE_TRANSFORM:
2234 bTransformConstraint *data;
2235 data= ((bTransformConstraint*)con->data);
2236 data->tar = newlibadr(fd, id->lib, data->tar);
2239 case CONSTRAINT_TYPE_DISTLIMIT:
2241 bDistLimitConstraint *data;
2242 data= ((bDistLimitConstraint*)con->data);
2243 data->tar = newlibadr(fd, id->lib, data->tar);
2246 case CONSTRAINT_TYPE_SHRINKWRAP:
2248 bShrinkwrapConstraint *data;
2249 data= ((bShrinkwrapConstraint*)con->data);
2250 data->target = newlibadr(fd, id->lib, data->target);
2253 case CONSTRAINT_TYPE_NULL:
2259 static void direct_link_constraints(FileData *fd, ListBase *lb)
2264 for (cons=lb->first; cons; cons=cons->next) {
2265 cons->data = newdataadr(fd, cons->data);
2267 if (cons->type == CONSTRAINT_TYPE_PYTHON) {
2268 bPythonConstraint *data= cons->data;
2270 link_list(fd, &data->targets);
2272 data->prop = newdataadr(fd, data->prop);
2274 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2279 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2281 bPoseChannel *pchan;
2282 bArmature *arm= ob->data;
2288 /* always rebuild to match proxy or lib changes */
2289 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2291 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2292 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2294 /* hurms... loop in a loop, but yah... later... (ton) */
2295 pchan->bone= get_named_bone(arm, pchan->name);
2297 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
2298 if(pchan->bone==NULL)
2300 else if(ob->id.lib==NULL && arm->id.lib) {
2301 /* local pose selection copied to armature, bit hackish */
2302 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
2303 pchan->bone->flag |= pchan->selectflag;
2308 ob->recalc= OB_RECALC;
2309 pose->flag |= POSE_RECALC;
2313 static void lib_link_armature(FileData *fd, Main *main)
2317 arm= main->armature.first;
2320 if(arm->id.flag & LIB_NEEDLINK) {
2321 arm->id.flag -= LIB_NEEDLINK;
2327 static void direct_link_bones(FileData *fd, Bone* bone)
2331 bone->parent= newdataadr(fd, bone->parent);
2333 link_list(fd, &bone->childbase);
2335 for (child=bone->childbase.first; child; child=child->next) {
2336 direct_link_bones(fd, child);
2340 static void direct_link_armature(FileData *fd, bArmature *arm)
2344 link_list(fd, &arm->bonebase);
2348 bone=arm->bonebase.first;
2350 direct_link_bones(fd, bone);
2355 /* ************ READ CAMERA ***************** */
2357 static void lib_link_camera(FileData *fd, Main *main)
2361 ca= main->camera.first;
2363 if(ca->id.flag & LIB_NEEDLINK) {
2364 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2366 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2368 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2370 lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
2372 ca->id.flag -= LIB_NEEDLINK;
2378 static void direct_link_camera(FileData *fd, Camera *ca)
2380 ca->adt= newdataadr(fd, ca->adt);
2381 direct_link_animdata(fd, ca->adt);
2383 direct_link_scriptlink(fd, &ca->scriptlink);
2387 /* ************ READ LAMP ***************** */
2389 static void lib_link_lamp(FileData *fd, Main *main)
2395 la= main->lamp.first;
2397 if(la->id.flag & LIB_NEEDLINK) {
2398 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2400 for(a=0; a<MAX_MTEX; a++) {
2403 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2404 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2408 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2410 lib_link_scriptlink(fd, &la->id, &la->scriptlink);
2412 la->id.flag -= LIB_NEEDLINK;
2418 static void direct_link_lamp(FileData *fd, Lamp *la)
2422 la->adt= newdataadr(fd, la->adt);
2423 direct_link_animdata(fd, la->adt);
2425 direct_link_scriptlink(fd, &la->scriptlink);
2427 for(a=0; a<MAX_MTEX; a++) {
2428 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2431 la->curfalloff= newdataadr(fd, la->curfalloff);
2433 direct_link_curvemapping(fd, la->curfalloff);
2435 la->preview = direct_link_preview_image(fd, la->preview);
2438 /* ************ READ keys ***************** */
2440 static void lib_link_key(FileData *fd, Main *main)
2444 key= main->key.first;
2446 if(key->id.flag & LIB_NEEDLINK) {
2447 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2449 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2450 key->from= newlibadr(fd, key->id.lib, key->from);
2452 key->id.flag -= LIB_NEEDLINK;
2458 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2461 char *data, *poin, *cp;
2463 elemsize= key->elemsize;
2466 for(a=0; a<kb->totelem; a++) {
2471 while( cp[0] ) { /* cp[0]==amount */
2473 switch(cp[1]) { /* cp[1]= type */
2479 SWITCH_INT((*poin));
2492 static void direct_link_key(FileData *fd, Key *key)
2496 link_list(fd, &(key->block));
2498 key->adt= newdataadr(fd, key->adt);
2499 direct_link_animdata(fd, key->adt);
2501 key->refkey= newdataadr(fd, key->refkey);
2503 kb= key->block.first;
2506 kb->data= newdataadr(fd, kb->data);
2508 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2509 switch_endian_keyblock(key, kb);
2515 /* ************ READ mball ***************** */
2517 static void lib_link_mball(FileData *fd, Main *main)
2522 mb= main->mball.first;
2524 if(mb->id.flag & LIB_NEEDLINK) {
2526 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2528 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2530 mb->id.flag -= LIB_NEEDLINK;
2536 static void direct_link_mball(FileData *fd, MetaBall *mb)
2538 mb->mat= newdataadr(fd, mb->mat);
2539 test_pointer_array(fd, (void **)&mb->mat);
2541 link_list(fd, &(mb->elems));
2543 mb->disp.first= mb->disp.last= NULL;
2544 mb->editelems= NULL;
2548 /* ************ READ WORLD ***************** */
2550 static void lib_link_world(FileData *fd, Main *main)
2556 wrld= main->world.first;
2558 if(wrld->id.flag & LIB_NEEDLINK) {
2559 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2561 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2563 for(a=0; a<MAX_MTEX; a++) {
2564 mtex= wrld->mtex[a];
2566 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2567 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2571 lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
2573 wrld->id.flag -= LIB_NEEDLINK;
2575 wrld= wrld->id.next;
2579 static void direct_link_world(FileData *fd, World *wrld)
2583 wrld->adt= newdataadr(fd, wrld->adt);
2584 direct_link_animdata(fd, wrld->adt);
2586 direct_link_scriptlink(fd, &wrld->scriptlink);
2588 for(a=0; a<MAX_MTEX; a++) {
2589 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2591 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2595 /* ************ READ VFONT ***************** */
2597 static void lib_link_vfont(FileData *fd, Main *main)
2601 vf= main->vfont.first;
2603 if(vf->id.flag & LIB_NEEDLINK) {
2604 vf->id.flag -= LIB_NEEDLINK;
2610 static void direct_link_vfont(FileData *fd, VFont *vf)
2613 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2616 /* ************ READ TEXT ****************** */
2618 static void lib_link_text(FileData *fd, Main *main)
2622 text= main->text.first;
2624 if(text->id.flag & LIB_NEEDLINK) {
2625 text->id.flag -= LIB_NEEDLINK;
2627 text= text->id.next;
2631 static void direct_link_text(FileData *fd, Text *text)
2635 text->name= newdataadr(fd, text->name);
2638 text->undo_len= TXT_INIT_UNDO;
2639 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2641 text->compiled= NULL;
2644 if(text->flags & TXT_ISEXT) {
2649 link_list(fd, &text->lines);
2650 link_list(fd, &text->markers);
2652 text->curl= newdataadr(fd, text->curl);
2653 text->sell= newdataadr(fd, text->sell);
2655 ln= text->lines.first;
2657 ln->line= newdataadr(fd, ln->line);
2660 if (ln->len != (int) strlen(ln->line)) {
2661 printf("Error loading text, line lengths differ\n");
2662 ln->len = strlen(ln->line);
2668 text->flags = (text->flags) & ~TXT_ISEXT;
2673 /* ************ READ IMAGE ***************** */
2675 static void lib_link_image(FileData *fd, Main *main)
2679 ima= main->image.first;
2681 if(ima->id.flag & LIB_NEEDLINK) {
2682 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2684 ima->id.flag -= LIB_NEEDLINK;
2690 static void link_ibuf_list(FileData *fd, ListBase *lb)
2694 if(lb->first==NULL) return;
2696 lb->first= newimaadr(fd, lb->first);
2700 ln->next= newimaadr(fd, ln->next);
2708 static void direct_link_image(FileData *fd, Image *ima)
2710 /* for undo system, pointers could be restored */
2712 link_ibuf_list(fd, &ima->ibufs);
2714 ima->ibufs.first= ima->ibufs.last= NULL;
2716 /* if not restored, we keep the binded opengl index */
2717 if(ima->ibufs.first==NULL) {
2719 ima->gputexture= NULL;
2725 ima->render_text= newdataadr(fd, ima->render_text);
2727 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2728 ima->preview = direct_link_preview_image(fd, ima->preview);
2733 /* ************ READ CURVE ***************** */
2735 static void lib_link_curve(FileData *fd, Main *main)
2740 cu= main->curve.first;
2742 if(cu->id.flag & LIB_NEEDLINK) {
2743 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2745 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2747 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2748 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2749 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2750 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2751 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2752 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2753 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2755 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2756 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2758 cu->id.flag -= LIB_NEEDLINK;
2765 static void switch_endian_knots(Nurb *nu)
2772 SWITCH_INT(nu->knotsu[len]);
2778 SWITCH_INT(nu->knotsv[len]);
2783 static void direct_link_curve(FileData *fd, Curve *cu)
2788 cu->adt= newdataadr(fd, cu->adt);
2789 direct_link_animdata(fd, cu->adt);
2791 cu->mat= newdataadr(fd, cu->mat);
2792 test_pointer_array(fd, (void **)&cu->mat);
2793 cu->str= newdataadr(fd, cu->str);
2794 cu->strinfo= newdataadr(fd, cu->strinfo);
2795 cu->tb= newdataadr(fd, cu->tb);
2797 if(cu->vfont==0) link_list(fd, &(cu->nurb));
2799 cu->nurb.first=cu->nurb.last= 0;
2801 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2803 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2810 cu->tb[0].w = cu->linewidth;
2812 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2815 cu->bev.first=cu->bev.last= NULL;
2816 cu->disp.first=cu->disp.last= NULL;
2818 cu->lastselbp= NULL;
2824 nu->bezt= newdataadr(fd, nu->bezt);
2825 nu->bp= newdataadr(fd, nu->bp);
2826 nu->knotsu= newdataadr(fd, nu->knotsu);
2827 nu->knotsv= newdataadr(fd, nu->knotsv);
2828 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2830 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2831 switch_endian_knots(nu);
2839 /* ************ READ TEX ***************** */
2841 static void lib_link_texture(FileData *fd, Main *main)
2845 tex= main->tex.first;
2847 if(tex->id.flag & LIB_NEEDLINK) {
2848 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
2850 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2851 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2852 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2855 lib_link_ntree(fd, &tex->id, tex->nodetree);
2857 tex->id.flag -= LIB_NEEDLINK;
2863 static void direct_link_texture(FileData *fd, Tex *tex)
2865 tex->adt= newdataadr(fd, tex->adt);
2866 direct_link_animdata(fd, tex->adt);
2868 tex->plugin= newdataadr(fd, tex->plugin);
2870 tex->plugin->handle= 0;
2871 open_plugin_tex(tex->plugin);
2872 /* initialize data for this instance, if an initialization
2875 if (tex->plugin->instance_init)
2876 tex->plugin->instance_init((void *) tex->plugin->data);
2878 tex->coba= newdataadr(fd, tex->coba);
2879 tex->env= newdataadr(fd, tex->env);
2881 tex->env->ima= NULL;
2882 memset(tex->env->cube, 0, 6*sizeof(void *));
2886 tex->nodetree= newdataadr(fd, tex->nodetree);
2888 direct_link_nodetree(fd, tex->nodetree);
2890 tex->preview = direct_link_preview_image(fd, tex->preview);
2897 /* ************ READ MATERIAL ***************** */
2899 static void lib_link_material(FileData *fd, Main *main)
2905 ma= main->mat.first;
2907 if(ma->id.flag & LIB_NEEDLINK) {
2908 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
2910 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2911 of library blocks that implement this.*/
2912 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2914 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2915 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2917 for(a=0; a<MAX_MTEX; a++) {
2920 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2921 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2924 lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
2927 lib_link_ntree(fd, &ma->id, ma->nodetree);
2929 ma->id.flag -= LIB_NEEDLINK;
2935 static void direct_link_material(FileData *fd, Material *ma)
2939 ma->adt= newdataadr(fd, ma->adt);
2940 direct_link_animdata(fd, ma->adt);
2942 for(a=0; a<MAX_MTEX; a++) {
2943 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2946 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2947 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2949 direct_link_scriptlink(fd, &ma->scriptlink);
2951 ma->nodetree= newdataadr(fd, ma->nodetree);
2953 direct_link_nodetree(fd, ma->nodetree);
2955 ma->preview = direct_link_preview_image(fd, ma->preview);
2956 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
2959 /* ************ READ PARTICLE SETTINGS ***************** */
2961 static void direct_link_pointcache(FileData *fd, PointCache *cache)
2963 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
2966 link_list(fd, &cache->mem_cache);
2968 pm = cache->mem_cache.first;
2970 for(; pm; pm=pm->next)
2971 pm->data = newdataadr(fd, pm->data);
2974 cache->mem_cache.first = cache->mem_cache.last = NULL;
2976 cache->flag &= ~(PTCACHE_SIMULATION_VALID|PTCACHE_BAKE_EDIT_ACTIVE);
2980 static void lib_link_particlesettings(FileData *fd, Main *main)
2982 ParticleSettings *part;
2984 part= main->particle.first;
2986 if(part->id.flag & LIB_NEEDLINK) {
2987 if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
2988 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
2990 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
2991 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
2992 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
2993 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
2995 BoidState *state = part->boids->states.first;
2997 for(; state; state=state->next) {
2998 rule = state->rules.first;
2999 for(; rule; rule=rule->next)
3000 switch(rule->type) {
3001 case eBoidRuleType_Goal:
3002 case eBoidRuleType_Avoid:
3004 BoidRuleGoalAvoid *brga = (BoidRuleGoalAvoid*)rule;
3005 brga->ob = newlibadr(fd, part->id.lib, brga->ob);
3008 case eBoidRuleType_FollowLeader:
3010 BoidRuleFollowLeader *brfl = (BoidRuleFollowLeader*)rule;
3011 brfl->ob = newlibadr(fd, part->id.lib, brfl->ob);
3017 part->id.flag -= LIB_NEEDLINK;
3019 part= part->id.next;
3023 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
3025 part->adt= newdataadr(fd, part->adt);
3026 part->pd= newdataadr(fd, part->pd);
3027 part->pd2= newdataadr(fd, part->pd2);
3029 part->boids= newdataadr(fd, part->boids);
3033 link_list(fd, &part->boids->states);
3035 for(state=part->boids->states.first; state; state=state->next) {
3036 link_list(fd, &state->rules);
3037 link_list(fd, &state->conditions);
3038 link_list(fd, &state->actions);
3043 static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase *particles)
3045 ParticleSystem *psys, *psysnext;
3048 for(psys=particles->first; psys; psys=psysnext){
3051 psysnext= psys->next;
3053 psys->part = newlibadr_us(fd, id->lib, psys->part);
3055 ParticleTarget *pt = psys->targets.first;
3057 for(; pt; pt=pt->next)
3058 pt->ob=newlibadr(fd, id->lib, pt->ob);
3060 psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
3062 for(a=0,pa=psys->particles; a<psys->totpart; a++,pa++){
3063 pa->stick_ob=newlibadr(fd, id->lib, pa->stick_ob);
3069 /* particle modifier must be removed before particle system */
3070 ParticleSystemModifierData *psmd= psys_get_modifier(ob,psys);
3071 BLI_remlink(&ob->modifiers, psmd);
3072 modifier_free((ModifierData *)psmd);
3074 BLI_remlink(particles, psys);
3079 static void direct_link_particlesystems(FileData *fd, ListBase *particles)
3081 ParticleSystem *psys;
3085 for(psys=particles->first; psys; psys=psys->next) {
3086 psys->particles=newdataadr(fd,psys->particles);
3088 if(psys->particles && psys->particles->hair){
3089 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
3090 pa->hair=newdataadr(fd,pa->hair);
3093 if(psys->particles && psys->particles->keys){
3094 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++) {
3099 psys->flag &= ~PSYS_KEYED;
3102 if(psys->particles->boid) {
3103 pa = psys->particles;
3104 pa->boid = newdataadr(fd, pa->boid);
3105 for(a=1,pa++; a<psys->totpart; a++, pa++)
3106 pa->boid = (pa-1)->boid + 1;
3109 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
3114 psys->child=newdataadr(fd,psys->child);
3115 psys->effectors.first=psys->effectors.last=0;
3117 psys->soft= newdataadr(fd, psys->soft);
3119 SoftBody *sb = psys->soft;
3120 sb->particles = psys;
3121 sb->bpoint= NULL; // init pointers so it gets rebuilt nicely
3125 sb->pointcache= newdataadr(fd, sb->pointcache);
3127 direct_link_pointcache(fd, sb->pointcache);
3130 link_list(fd, &psys->targets);
3133 psys->free_edit = NULL;
3134 psys->pathcache = 0;
3135 psys->childcache = 0;
3136 psys->pathcachebufs.first = psys->pathcachebufs.last = 0;
3137 psys->childcachebufs.first = psys->childcachebufs.last = 0;
3138 psys->reactevents.first = psys->reactevents.last = 0;
3140 psys->pointcache= newdataadr(fd, psys->pointcache);
3141 if(psys->pointcache)
3142 direct_link_pointcache(fd, psys->pointcache);
3149 /* ************ READ MESH ***************** */
3151 static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
3156 for (i=0; i<totface; i++, tf++) {
3157 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
3158 if(tf->tpage && tf->tpage->id.us==0)
3159 tf->tpage->id.us= 1;
3163 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
3166 for(i=0; i<fdata->totlayer; i++) {
3167 CustomDataLayer *layer = &fdata->layers[i];
3169 if(layer->type == CD_MTFACE)
3170 lib_link_mtface(fd, me, layer->data, totface);
3175 static void lib_link_mesh(FileData *fd, Main *main)
3179 me= main->mesh.first;
3181 if(me->id.flag & LIB_NEEDLINK) {
3184 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3185 of library blocks that implement this.*/
3186 if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3188 /* this check added for python created meshes */
3190 for(i=0; i<me->totcol; i++) {
3191 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);