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 *****
33 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
34 #include <stdlib.h> // for getenv atoi
35 #include <fcntl.h> // for open
36 #include <string.h> // for strrchr strncmp strstr
37 #include <math.h> // for fabs
40 #include <unistd.h> // for read close
41 #include <sys/param.h> // for MAXPATHLEN
43 #include <io.h> // for open close read
45 #include "BLI_winstuff.h"
48 #include "DNA_anim_types.h"
49 #include "DNA_action_types.h"
50 #include "DNA_armature_types.h"
52 #include "DNA_actuator_types.h"
53 #include "DNA_boid_types.h"
54 #include "DNA_brush_types.h"
55 #include "DNA_camera_types.h"
56 #include "DNA_cloth_types.h"
57 #include "DNA_color_types.h"
58 #include "DNA_controller_types.h"
59 #include "DNA_constraint_types.h"
60 #include "DNA_curve_types.h"
61 #include "DNA_customdata_types.h"
62 #include "DNA_effect_types.h"
63 #include "DNA_fileglobal_types.h"
64 #include "DNA_genfile.h"
65 #include "DNA_group_types.h"
66 #include "DNA_gpencil_types.h"
67 #include "DNA_ipo_types.h"
68 #include "DNA_image_types.h"
69 #include "DNA_key_types.h"
70 #include "DNA_lattice_types.h"
71 #include "DNA_lamp_types.h"
72 #include "DNA_meta_types.h"
73 #include "DNA_material_types.h"
74 #include "DNA_mesh_types.h"
75 #include "DNA_meshdata_types.h"
76 #include "DNA_modifier_types.h"
77 #include "DNA_nla_types.h"
78 #include "DNA_node_types.h"
79 #include "DNA_object_types.h"
80 #include "DNA_object_force.h"
81 #include "DNA_object_fluidsim.h" // NT
82 #include "DNA_outliner_types.h"
83 #include "DNA_object_force.h"
84 #include "DNA_packedFile_types.h"
85 #include "DNA_particle_types.h"
86 #include "DNA_property_types.h"
87 #include "DNA_text_types.h"
88 #include "DNA_view3d_types.h"
89 #include "DNA_screen_types.h"
90 #include "DNA_sensor_types.h"
91 #include "DNA_sdna_types.h"
92 #include "DNA_scene_types.h"
93 #include "DNA_sequence_types.h"
94 #include "DNA_smoke_types.h"
95 #include "DNA_sound_types.h"
96 #include "DNA_space_types.h"
97 #include "DNA_texture_types.h"
98 #include "DNA_userdef_types.h"
99 #include "DNA_vfont_types.h"
100 #include "DNA_world_types.h"
101 #include "DNA_windowmanager_types.h"
103 #include "MEM_guardedalloc.h"
104 #include "BLI_blenlib.h"
105 #include "BLI_arithb.h"
106 #include "BLI_storage_types.h" // for relname flags
108 #include "BKE_animsys.h"
109 #include "BKE_action.h"
110 #include "BKE_armature.h"
111 #include "BKE_brush.h"
112 #include "BKE_cdderivedmesh.h"
113 #include "BKE_cloth.h"
114 #include "BKE_colortools.h"
115 #include "BKE_constraint.h"
116 #include "BKE_context.h"
117 #include "BKE_curve.h"
118 #include "BKE_customdata.h"
119 #include "BKE_deform.h"
120 #include "BKE_depsgraph.h"
121 #include "BKE_effect.h" /* give_parteff */
122 #include "BKE_fcurve.h"
123 #include "BKE_global.h" // for G
124 #include "BKE_group.h"
125 #include "BKE_image.h"
127 #include "BKE_lattice.h"
128 #include "BKE_library.h" // for wich_libbase
129 #include "BKE_main.h" // for Main
130 #include "BKE_mesh.h" // for ME_ defines (patching)
131 #include "BKE_modifier.h"
132 #include "BKE_multires.h"
133 #include "BKE_node.h" // for tree type defines
134 #include "BKE_object.h"
135 #include "BKE_paint.h"
136 #include "BKE_particle.h"
137 #include "BKE_pointcache.h"
138 #include "BKE_property.h" // for get_ob_property
139 #include "BKE_report.h"
140 #include "BKE_sca.h" // for init_actuator
141 #include "BKE_scene.h"
142 #include "BKE_softbody.h" // sbNew()
143 #include "BKE_bullet.h" // bsbNew()
144 #include "BKE_sequence.h"
145 #include "BKE_texture.h" // for open_plugin_tex
146 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
147 #include "BKE_idprop.h"
149 #include "BKE_sound.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 int BLO_is_a_library(char *path, char *dir, char *group)
1071 /* return ok when a blenderfile, in dir is the filename,
1072 * in group the type of libdata
1080 if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
1085 /* Find the last slash */
1086 fd= (strrchr(dir, '/')>strrchr(dir, '\\'))?strrchr(dir, '/'):strrchr(dir, '\\');
1090 if(BLO_has_bfile_extension(fd+1)) {
1091 /* the last part of the dir is a .blend file, no group follows */
1092 *fd= '/'; /* put back the removed slash separating the dir and the .blend file name */
1095 char *gp = fd+1; // in case we have a .blend file, gp points to the group
1097 /* Find the last slash */
1098 fd= (strrchr(dir, '/')>strrchr(dir, '\\'))?strrchr(dir, '/'):strrchr(dir, '\\');
1099 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
1101 /* now we know that we are in a blend file and it is safe to
1102 assume that gp actually points to a group */
1103 if (BLI_streq("Screen", gp)==0)
1104 BLI_strncpy(group, gp, GROUP_MAX);
1109 /* ************** OLD POINTERS ******************* */
1111 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1113 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1116 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1118 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1121 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1123 if(fd->imamap && adr)
1124 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1129 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1131 return oldnewmap_liblookup(fd->libmap, adr, lib);
1134 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1136 ID *id= newlibadr(fd, lib, adr);
1144 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1148 for (i=0; i<fd->libmap->nentries; i++) {
1149 OldNew *entry= &fd->libmap->entries[i];
1151 if (old==entry->newp && entry->nr==ID_ID) {
1153 if(new) entry->nr= GS( ((ID *)new)->name );
1159 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1163 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1166 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1170 change_idid_adr_fd(fd, old, new);
1175 /* lib linked proxy objects point to our local data, we need
1176 * to clear that pointer before reading the undo memfile since
1177 * the object might be removed, it is set again in reading
1178 * if the local object still exists */
1179 void blo_clear_proxy_pointers_from_lib(FileData *fd, Main *oldmain)
1181 Object *ob= oldmain->object.first;
1183 for(;ob; ob= ob->id.next)
1185 ob->proxy_from= NULL;
1188 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1190 Image *ima= oldmain->image.first;
1191 Scene *sce= oldmain->scene.first;
1193 fd->imamap= oldnewmap_new();
1195 for(;ima; ima= ima->id.next) {
1196 Link *ibuf= ima->ibufs.first;
1197 for(; ibuf; ibuf= ibuf->next)
1198 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1200 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1202 for(; sce; sce= sce->id.next) {
1205 for(node= sce->nodetree->nodes.first; node; node= node->next)
1206 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1211 /* set old main image ibufs to zero if it has been restored */
1212 /* this works because freeing old main only happens after this call */
1213 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1215 OldNew *entry= fd->imamap->entries;
1216 Image *ima= oldmain->image.first;
1217 Scene *sce= oldmain->scene.first;
1220 /* used entries were restored, so we put them to zero */
1221 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1226 for(;ima; ima= ima->id.next) {
1229 /* this mirrors direct_link_image */
1230 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1232 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1233 BLI_remlink(&ima->ibufs, ibuf);
1235 ima->gputexture= NULL;
1239 ima->gputexture= newimaadr(fd, ima->gputexture);
1241 for(; sce; sce= sce->id.next) {
1244 for(node= sce->nodetree->nodes.first; node; node= node->next)
1245 node->preview= newimaadr(fd, node->preview);
1250 /* undo file support: add all library pointers in lookup */
1251 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1253 Main *ptr= mainlist->first;
1254 ListBase *lbarray[MAX_LIBARRAY];
1256 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1257 int i= set_listbasepointers(ptr, lbarray);
1260 for(id= lbarray[i]->first; id; id= id->next)
1261 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1267 /* ********** END OLD POINTERS ****************** */
1268 /* ********** READ FILE ****************** */
1270 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1272 int blocksize, nblocks;
1275 data= (char *)(bhead+1);
1276 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1280 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1286 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1291 /* switch is based on file dna */
1292 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1293 switch_endian_structs(fd->filesdna, bh);
1295 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1296 if(fd->compflags[bh->SDNAnr]==2) {
1297 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1299 temp= MEM_mallocN(bh->len, blockname);
1300 memcpy(temp, (bh+1), bh->len);
1308 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1312 if(lb->first==NULL) return;
1314 lb->first= newdataadr(fd, lb->first);
1318 ln->next= newdataadr(fd, ln->next);
1326 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1331 if(lb->first==0) return;
1332 poin= newdataadr(fd, lb->first);
1334 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1341 poin= newdataadr(fd, ln->next);
1343 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1353 static void test_pointer_array(FileData *fd, void **mat)
1355 #if defined(WIN32) && !defined(FREE_WINDOWS)
1356 __int64 *lpoin, *lmat;
1358 long long *lpoin, *lmat;
1360 int len, *ipoin, *imat;
1362 /* manually convert the pointer array in
1363 * the old dna format to a pointer array in
1364 * the new dna format.
1367 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1369 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1370 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1374 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1375 SWITCH_LONGINT(*lpoin);
1376 *ipoin= (int) ((*lpoin) >> 3);
1384 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1385 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1399 /* ************ READ ID Properties *************** */
1401 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1402 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1404 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1409 /*since we didn't save the extra buffer, set totallen to len.*/
1410 prop->totallen = prop->len;
1411 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1413 array= (IDProperty*) prop->data.pointer;
1415 for(i=0; i<prop->len; i++)
1416 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
1419 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1424 /*since we didn't save the extra buffer, set totallen to len.*/
1425 prop->totallen = prop->len;
1426 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1428 if(prop->subtype == IDP_GROUP) {
1429 test_pointer_array(fd, prop->data.pointer);
1430 array= prop->data.pointer;
1432 for(i=0; i<prop->len; i++)
1433 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1435 else if(prop->subtype == IDP_DOUBLE) {
1436 if (switch_endian) {
1437 for (i=0; i<prop->len; i++) {
1438 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1443 if (switch_endian) {
1444 for (i=0; i<prop->len; i++) {
1445 SWITCH_INT(((int*)prop->data.pointer)[i]);
1451 static void IDP_DirectLinkString(IDProperty *prop, int switch_endian, FileData *fd)
1453 /*since we didn't save the extra string buffer, set totallen to len.*/
1454 prop->totallen = prop->len;
1455 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1458 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1460 ListBase *lb = &prop->data.group;
1465 /*Link child id properties now*/
1466 for (loop=prop->data.group.first; loop; loop=loop->next) {
1467 IDP_DirectLinkProperty(loop, switch_endian, fd);
1471 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1473 switch (prop->type) {
1475 IDP_DirectLinkGroup(prop, switch_endian, fd);
1478 IDP_DirectLinkString(prop, switch_endian, fd);
1481 IDP_DirectLinkArray(prop, switch_endian, fd);
1484 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1487 /*erg, stupid doubles. since I'm storing them
1488 in the same field as int val; val2 in the
1489 IDPropertyData struct, they have to deal with
1490 endianness specifically
1492 in theory, val and val2 would've already been swapped
1493 if switch_endian is true, so we have to first unswap
1494 them then reswap them as a single 64-bit entity.
1497 if (switch_endian) {
1498 SWITCH_INT(prop->data.val);
1499 SWITCH_INT(prop->data.val2);
1500 SWITCH_LONGINT(prop->data.val);
1508 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1512 /* ************ READ CurveMapping *************** */
1514 /* cuma itself has been read! */
1515 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1519 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1520 cumap->flag &= ~CUMA_PREMULLED;
1522 for(a=0; a<CM_TOT; a++) {
1523 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1524 cumap->cm[a].table= NULL;
1528 /* ************ READ Brush *************** */
1529 /* library brush linking after fileread */
1530 static void lib_link_brush(FileData *fd, Main *main)
1536 /* only link ID pointers */
1537 for(brush= main->brush.first; brush; brush= brush->id.next) {
1538 if(brush->id.flag & LIB_NEEDLINK) {
1539 brush->id.flag -= LIB_NEEDLINK;
1541 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1543 for(a=0; a<MAX_MTEX; a++) {
1544 mtex= brush->mtex[a];
1546 mtex->tex= newlibadr_us(fd, brush->id.lib, mtex->tex);
1549 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1554 static void direct_link_brush(FileData *fd, Brush *brush)
1556 /* brush itself has been read */
1559 for(a=0; a<MAX_MTEX; a++)
1560 brush->mtex[a]= newdataadr(fd, brush->mtex[a]);
1563 brush->curve= newdataadr(fd, brush->curve);
1565 direct_link_curvemapping(fd, brush->curve);
1567 brush_curve_preset(brush, BRUSH_PRESET_SHARP);
1570 static void direct_link_script(FileData *fd, Script *script)
1573 SCRIPT_SET_NULL(script)
1577 /* ************ READ PACKEDFILE *************** */
1579 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1581 PackedFile *pf= newdataadr(fd, oldpf);
1584 pf->data= newdataadr(fd, pf->data);
1590 /* ************ READ IMAGE PREVIEW *************** */
1592 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1594 PreviewImage *prv= newdataadr(fd, old_prv);
1598 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1600 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1608 /* ************ READ ANIMATION STUFF ***************** */
1610 /* Legacy Data Support (for Version Patching) ----------------------------- */
1612 // XXX depreceated - old animation system
1613 static void lib_link_ipo(FileData *fd, Main *main)
1617 ipo= main->ipo.first;
1619 if(ipo->id.flag & LIB_NEEDLINK) {
1621 for(icu= ipo->curve.first; icu; icu= icu->next) {
1623 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1625 ipo->id.flag -= LIB_NEEDLINK;
1631 // XXX depreceated - old animation system
1632 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1636 link_list(fd, &(ipo->curve));
1637 icu= ipo->curve.first;
1639 icu->bezt= newdataadr(fd, icu->bezt);
1640 icu->bp= newdataadr(fd, icu->bp);
1641 icu->driver= newdataadr(fd, icu->driver);
1646 // XXX depreceated - old animation system
1647 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1649 bActionStrip *strip;
1650 bActionModifier *amod;
1652 for (strip=striplist->first; strip; strip=strip->next){
1653 strip->object = newlibadr(fd, id->lib, strip->object);
1654 strip->act = newlibadr_us(fd, id->lib, strip->act);
1655 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1656 for(amod= strip->modifiers.first; amod; amod= amod->next)
1657 amod->ob= newlibadr(fd, id->lib, amod->ob);
1661 // XXX depreceated - old animation system
1662 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1664 bActionStrip *strip;
1666 link_list(fd, strips);
1668 for(strip= strips->first; strip; strip= strip->next)
1669 link_list(fd, &strip->modifiers);
1672 // XXX depreceated - old animation system
1673 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1675 bConstraintChannel *chan;
1677 for (chan=chanbase->first; chan; chan=chan->next){
1678 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1682 /* Data Linking ----------------------------- */
1684 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1688 for (fcm= list->first; fcm; fcm= fcm->next) {
1689 /* data for specific modifiers */
1690 switch (fcm->type) {
1691 case FMODIFIER_TYPE_PYTHON:
1693 FMod_Python *data= (FMod_Python *)fcm->data;
1694 data->script = newlibadr(fd, id->lib, data->script);
1701 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1705 /* relink ID-block references... */
1706 for (fcu= list->first; fcu; fcu= fcu->next) {
1709 ChannelDriver *driver= fcu->driver;
1712 for (dtar= driver->targets.first; dtar; dtar= dtar->next)
1713 dtar->id= newlibadr(fd, id->lib, dtar->id);
1717 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1722 /* NOTE: this assumes that link_list has already been called on the list */
1723 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1727 for (fcm= list->first; fcm; fcm= fcm->next) {
1728 /* relink general data */
1729 fcm->data = newdataadr(fd, fcm->data);
1732 /* do relinking of data for specific types */
1733 switch (fcm->type) {
1734 case FMODIFIER_TYPE_GENERATOR:
1736 FMod_Generator *data= (FMod_Generator *)fcm->data;
1738 data->coefficients= newdataadr(fd, data->coefficients);
1741 case FMODIFIER_TYPE_ENVELOPE:
1743 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1745 data->data= newdataadr(fd, data->data);
1748 case FMODIFIER_TYPE_PYTHON:
1750 FMod_Python *data= (FMod_Python *)fcm->data;
1752 data->prop = newdataadr(fd, data->prop);
1753 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1760 /* NOTE: this assumes that link_list has already been called on the list */
1761 static void direct_link_fcurves(FileData *fd, ListBase *list)
1765 /* link F-Curve data to F-Curve again (non ID-libs) */
1766 for (fcu= list->first; fcu; fcu= fcu->next) {
1768 fcu->bezt= newdataadr(fd, fcu->bezt);
1769 fcu->fpt= newdataadr(fd, fcu->fpt);
1772 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1775 fcu->grp= newdataadr(fd, fcu->grp);
1778 fcu->driver= newdataadr(fd, fcu->driver);
1780 ChannelDriver *driver= fcu->driver;
1783 /* relink targets and their paths */
1784 link_list(fd, &driver->targets);
1785 for (dtar= driver->targets.first; dtar; dtar= dtar->next)
1786 dtar->rna_path= newdataadr(fd, dtar->rna_path);
1790 link_list(fd, &fcu->modifiers);
1791 direct_link_fmodifiers(fd, &fcu->modifiers);
1796 static void lib_link_action(FileData *fd, Main *main)
1799 bActionChannel *chan;
1801 for (act= main->action.first; act; act= act->id.next) {
1802 if (act->id.flag & LIB_NEEDLINK) {
1803 act->id.flag -= LIB_NEEDLINK;
1805 // XXX depreceated - old animation system <<<
1806 for (chan=act->chanbase.first; chan; chan=chan->next) {
1807 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1808 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1810 // >>> XXX depreceated - old animation system
1812 lib_link_fcurves(fd, &act->id, &act->curves);
1817 static void direct_link_action(FileData *fd, bAction *act)
1819 bActionChannel *achan; // XXX depreceated - old animation system
1822 link_list(fd, &act->curves);
1823 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1824 link_list(fd, &act->groups);
1825 link_list(fd, &act->markers);
1827 // XXX depreceated - old animation system <<<
1828 for (achan = act->chanbase.first; achan; achan=achan->next) {
1829 achan->grp= newdataadr(fd, achan->grp);
1831 link_list(fd, &achan->constraintChannels);
1833 // >>> XXX depreceated - old animation system
1835 direct_link_fcurves(fd, &act->curves);
1837 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1838 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1839 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1843 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
1847 for (strip= list->first; strip; strip= strip->next) {
1848 /* check strip's children */
1849 lib_link_nladata_strips(fd, id, &strip->strips);
1851 /* reassign the counted-reference to action */
1852 strip->act = newlibadr_us(fd, id->lib, strip->act);
1856 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
1860 /* we only care about the NLA strips inside the tracks */
1861 for (nlt= list->first; nlt; nlt= nlt->next) {
1862 lib_link_nladata_strips(fd, id, &nlt->strips);
1866 /* This handles Animato NLA-Strips linking
1867 * NOTE: this assumes that link_list has already been called on the list
1869 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
1873 for (strip= list->first; strip; strip= strip->next) {
1874 /* strip's child strips */
1875 link_list(fd, &strip->strips);
1876 direct_link_nladata_strips(fd, &strip->strips);
1878 /* strip's F-Curves */
1879 link_list(fd, &strip->fcurves);
1880 direct_link_fcurves(fd, &strip->fcurves);
1882 /* strip's F-Modifiers */
1883 link_list(fd, &strip->modifiers);
1884 direct_link_fcurves(fd, &strip->modifiers);
1888 /* NOTE: this assumes that link_list has already been called on the list */
1889 static void direct_link_nladata(FileData *fd, ListBase *list)
1893 for (nlt= list->first; nlt; nlt= nlt->next) {
1894 /* relink list of strips */
1895 link_list(fd, &nlt->strips);
1897 /* relink strip data */
1898 direct_link_nladata_strips(fd, &nlt->strips);
1904 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
1909 /* here, we're only interested in the ID pointer stored in some of the paths */
1910 for (ks= list->first; ks; ks= ks->next) {
1911 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1912 ksp->id= newlibadr(fd, id->lib, ksp->id);
1917 /* NOTE: this assumes that link_list has already been called on the list */
1918 static void direct_link_keyingsets(FileData *fd, ListBase *list)
1923 /* link KeyingSet data to KeyingSet again (non ID-libs) */
1924 for (ks= list->first; ks; ks= ks->next) {
1926 link_list(fd, &ks->paths);
1928 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1930 ksp->rna_path= newdataadr(fd, ksp->rna_path);
1937 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
1942 /* link action data */
1943 adt->action= newlibadr_us(fd, id->lib, adt->action);
1944 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
1947 lib_link_fcurves(fd, id, &adt->drivers);
1949 /* overrides don't have lib-link for now, so no need to do anything */
1952 lib_link_nladata(fd, id, &adt->nla_tracks);
1955 static void direct_link_animdata(FileData *fd, AnimData *adt)
1957 /* NOTE: must have called newdataadr already before doing this... */
1962 link_list(fd, &adt->drivers);
1963 direct_link_fcurves(fd, &adt->drivers);
1965 /* link overrides */
1969 link_list(fd, &adt->nla_tracks);
1970 direct_link_nladata(fd, &adt->nla_tracks);
1972 /* clear temp pointers that may have been set... */
1973 // TODO: it's probably only a small cost to reload this anyway...
1974 adt->actstrip= NULL;
1977 /* ************ READ NODE TREE *************** */
1979 /* singe node tree (also used for material/scene trees), ntree is not NULL */
1980 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
1984 if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
1986 for(node= ntree->nodes.first; node; node= node->next)
1987 node->id= newlibadr_us(fd, id->lib, node->id);
1990 /* library ntree linking after fileread */
1991 static void lib_link_nodetree(FileData *fd, Main *main)
1995 /* only link ID pointers */
1996 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1997 if(ntree->id.flag & LIB_NEEDLINK) {
1998 ntree->id.flag -= LIB_NEEDLINK;
1999 lib_link_ntree(fd, &ntree->id, ntree);
2004 /* verify types for nodes and groups, all data has to be read */
2005 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
2007 static void lib_verify_nodetree(Main *main, int open)
2014 /* this crashes blender on undo/redo
2016 reinit_nodesystem();
2019 /* now create the own typeinfo structs an verify nodes */
2020 /* here we still assume no groups in groups */
2021 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2022 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
2023 ntreeMakeOwnType(ntree); /* for group usage */
2026 /* now verify all types in material trees, groups are set OK now */
2027 for(ma= main->mat.first; ma; ma= ma->id.next) {
2029 ntreeVerifyTypes(ma->nodetree);
2031 /* and scene trees */
2032 for(sce= main->scene.first; sce; sce= sce->id.next) {
2034 ntreeVerifyTypes(sce->nodetree);
2036 /* and texture trees */
2037 for(tx= main->tex.first; tx; tx= tx->id.next) {
2039 ntreeVerifyTypes(tx->nodetree);
2045 /* ntree itself has been read! */
2046 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2048 /* note: writing and reading goes in sync, for speed */
2053 ntree->init= 0; /* to set callbacks and force setting types */
2054 ntree->owntype= NULL;
2055 ntree->timecursor= NULL;
2057 ntree->adt= newdataadr(fd, ntree->adt);
2058 direct_link_animdata(fd, ntree->adt);
2060 link_list(fd, &ntree->nodes);
2061 for(node= ntree->nodes.first; node; node= node->next) {
2062 if(node->type == NODE_DYNAMIC) {
2064 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
2065 node->typeinfo= NULL;
2068 node->storage= newdataadr(fd, node->storage);
2071 /* could be handlerized at some point */
2072 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
2073 direct_link_curvemapping(fd, node->storage);
2074 else if(ntree->type==NTREE_COMPOSIT) {
2075 if( ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB))
2076 direct_link_curvemapping(fd, node->storage);
2077 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2078 ((ImageUser *)node->storage)->ok= 1;
2080 else if( ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) ) {
2081 direct_link_curvemapping(fd, node->storage);
2084 link_list(fd, &node->inputs);
2085 link_list(fd, &node->outputs);
2087 link_list(fd, &ntree->links);
2089 /* and we connect the rest */
2090 for(node= ntree->nodes.first; node; node= node->next) {
2091 node->preview= newimaadr(fd, node->preview);
2093 for(sock= node->inputs.first; sock; sock= sock->next)
2094 sock->link= newdataadr(fd, sock->link);
2095 for(sock= node->outputs.first; sock; sock= sock->next)
2096 sock->ns.data= NULL;
2098 for(link= ntree->links.first; link; link= link->next) {
2099 link->fromnode= newdataadr(fd, link->fromnode);
2100 link->tonode= newdataadr(fd, link->tonode);
2101 link->fromsock= newdataadr(fd, link->fromsock);
2102 link->tosock= newdataadr(fd, link->tosock);
2105 /* set selin and selout */
2106 for(node= ntree->nodes.first; node; node= node->next) {
2107 for(sock= node->inputs.first; sock; sock= sock->next) {
2108 if(sock->flag & SOCK_SEL) {
2113 for(sock= node->outputs.first; sock; sock= sock->next) {
2114 if(sock->flag & SOCK_SEL) {
2115 ntree->selout= sock;
2121 /* type verification is in lib-link */
2124 /* ************ READ ARMATURE ***************** */
2126 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2130 for (con = conlist->first; con; con=con->next) {
2131 /* patch for error introduced by changing constraints (dunno how) */
2132 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2133 if(con->data==NULL) {
2134 con->type= CONSTRAINT_TYPE_NULL;
2136 /* own ipo, all constraints have it */
2137 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2139 switch (con->type) {
2140 case CONSTRAINT_TYPE_PYTHON:
2142 bPythonConstraint *data= (bPythonConstraint*)con->data;
2143 bConstraintTarget *ct;
2145 for (ct= data->targets.first; ct; ct= ct->next)
2146 ct->tar = newlibadr(fd, id->lib, ct->tar);
2148 data->text = newlibadr(fd, id->lib, data->text);
2149 //IDP_LibLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2152 case CONSTRAINT_TYPE_ACTION:
2154 bActionConstraint *data;
2155 data= ((bActionConstraint*)con->data);
2156 data->tar = newlibadr(fd, id->lib, data->tar);
2157 data->act = newlibadr(fd, id->lib, data->act);
2160 case CONSTRAINT_TYPE_LOCLIKE:
2162 bLocateLikeConstraint *data;
2163 data= ((bLocateLikeConstraint*)con->data);
2164 data->tar = newlibadr(fd, id->lib, data->tar);
2167 case CONSTRAINT_TYPE_ROTLIKE:
2169 bRotateLikeConstraint *data;
2170 data= ((bRotateLikeConstraint*)con->data);
2171 data->tar = newlibadr(fd, id->lib, data->tar);
2174 case CONSTRAINT_TYPE_SIZELIKE:
2176 bSizeLikeConstraint *data;
2177 data= ((bSizeLikeConstraint*)con->data);
2178 data->tar = newlibadr(fd, id->lib, data->tar);
2181 case CONSTRAINT_TYPE_KINEMATIC:
2183 bKinematicConstraint *data;
2184 data = ((bKinematicConstraint*)con->data);
2185 data->tar = newlibadr(fd, id->lib, data->tar);
2186 data->poletar = newlibadr(fd, id->lib, data->poletar);
2189 case CONSTRAINT_TYPE_TRACKTO:
2191 bTrackToConstraint *data;
2192 data = ((bTrackToConstraint*)con->data);
2193 data->tar = newlibadr(fd, id->lib, data->tar);
2196 case CONSTRAINT_TYPE_MINMAX:
2198 bMinMaxConstraint *data;
2199 data = ((bMinMaxConstraint*)con->data);
2200 data->tar = newlibadr(fd, id->lib, data->tar);
2203 case CONSTRAINT_TYPE_LOCKTRACK:
2205 bLockTrackConstraint *data;
2206 data= ((bLockTrackConstraint*)con->data);
2207 data->tar = newlibadr(fd, id->lib, data->tar);
2210 case CONSTRAINT_TYPE_FOLLOWPATH:
2212 bFollowPathConstraint *data;
2213 data= ((bFollowPathConstraint*)con->data);
2214 data->tar = newlibadr(fd, id->lib, data->tar);
2217 case CONSTRAINT_TYPE_STRETCHTO:
2219 bStretchToConstraint *data;
2220 data= ((bStretchToConstraint*)con->data);
2221 data->tar = newlibadr(fd, id->lib, data->tar);
2224 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
2226 bRigidBodyJointConstraint *data;
2227 data= ((bRigidBodyJointConstraint*)con->data);
2228 data->tar = newlibadr(fd, id->lib, data->tar);
2231 case CONSTRAINT_TYPE_CLAMPTO:
2233 bClampToConstraint *data;
2234 data= ((bClampToConstraint*)con->data);
2235 data->tar = newlibadr(fd, id->lib, data->tar);
2238 case CONSTRAINT_TYPE_CHILDOF:
2240 bChildOfConstraint *data;
2241 data= ((bChildOfConstraint*)con->data);
2242 data->tar = newlibadr(fd, id->lib, data->tar);
2245 case CONSTRAINT_TYPE_TRANSFORM:
2247 bTransformConstraint *data;
2248 data= ((bTransformConstraint*)con->data);
2249 data->tar = newlibadr(fd, id->lib, data->tar);
2252 case CONSTRAINT_TYPE_DISTLIMIT:
2254 bDistLimitConstraint *data;
2255 data= ((bDistLimitConstraint*)con->data);
2256 data->tar = newlibadr(fd, id->lib, data->tar);
2259 case CONSTRAINT_TYPE_SHRINKWRAP:
2261 bShrinkwrapConstraint *data;
2262 data= ((bShrinkwrapConstraint*)con->data);
2263 data->target = newlibadr(fd, id->lib, data->target);
2266 case CONSTRAINT_TYPE_NULL:
2272 static void direct_link_constraints(FileData *fd, ListBase *lb)
2277 for (cons=lb->first; cons; cons=cons->next) {
2278 cons->data = newdataadr(fd, cons->data);
2280 if (cons->type == CONSTRAINT_TYPE_PYTHON) {
2281 bPythonConstraint *data= cons->data;
2283 link_list(fd, &data->targets);
2285 data->prop = newdataadr(fd, data->prop);
2287 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2292 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2294 bPoseChannel *pchan;
2295 bArmature *arm= ob->data;
2301 /* always rebuild to match proxy or lib changes */
2302 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2304 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2305 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2307 /* hurms... loop in a loop, but yah... later... (ton) */
2308 pchan->bone= get_named_bone(arm, pchan->name);
2310 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
2311 if(pchan->bone==NULL)
2313 else if(ob->id.lib==NULL && arm->id.lib) {
2314 /* local pose selection copied to armature, bit hackish */
2315 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
2316 pchan->bone->flag |= pchan->selectflag;
2321 ob->recalc= OB_RECALC;
2322 pose->flag |= POSE_RECALC;
2326 static void lib_link_armature(FileData *fd, Main *main)
2330 arm= main->armature.first;
2333 if(arm->id.flag & LIB_NEEDLINK) {
2334 arm->id.flag -= LIB_NEEDLINK;
2340 static void direct_link_bones(FileData *fd, Bone* bone)
2344 bone->parent= newdataadr(fd, bone->parent);
2346 link_list(fd, &bone->childbase);
2348 for (child=bone->childbase.first; child; child=child->next) {
2349 direct_link_bones(fd, child);
2353 static void direct_link_armature(FileData *fd, bArmature *arm)
2357 link_list(fd, &arm->bonebase);
2361 bone=arm->bonebase.first;
2363 direct_link_bones(fd, bone);
2368 /* ************ READ CAMERA ***************** */
2370 static void lib_link_camera(FileData *fd, Main *main)
2374 ca= main->camera.first;
2376 if(ca->id.flag & LIB_NEEDLINK) {
2377 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2379 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2381 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2383 ca->id.flag -= LIB_NEEDLINK;
2389 static void direct_link_camera(FileData *fd, Camera *ca)
2391 ca->adt= newdataadr(fd, ca->adt);
2392 direct_link_animdata(fd, ca->adt);
2396 /* ************ READ LAMP ***************** */
2398 static void lib_link_lamp(FileData *fd, Main *main)
2404 la= main->lamp.first;
2406 if(la->id.flag & LIB_NEEDLINK) {
2407 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2409 for(a=0; a<MAX_MTEX; a++) {
2412 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2413 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2417 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2419 la->id.flag -= LIB_NEEDLINK;
2425 static void direct_link_lamp(FileData *fd, Lamp *la)
2429 la->adt= newdataadr(fd, la->adt);
2430 direct_link_animdata(fd, la->adt);
2432 for(a=0; a<MAX_MTEX; a++) {
2433 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2436 la->curfalloff= newdataadr(fd, la->curfalloff);
2438 direct_link_curvemapping(fd, la->curfalloff);
2440 la->preview = direct_link_preview_image(fd, la->preview);
2443 /* ************ READ keys ***************** */
2445 static void lib_link_key(FileData *fd, Main *main)
2449 key= main->key.first;
2451 if(key->id.flag & LIB_NEEDLINK) {
2452 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2454 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2455 key->from= newlibadr(fd, key->id.lib, key->from);
2457 key->id.flag -= LIB_NEEDLINK;
2463 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2466 char *data, *poin, *cp;
2468 elemsize= key->elemsize;
2471 for(a=0; a<kb->totelem; a++) {
2476 while( cp[0] ) { /* cp[0]==amount */
2478 switch(cp[1]) { /* cp[1]= type */
2484 SWITCH_INT((*poin));
2497 static void direct_link_key(FileData *fd, Key *key)
2501 link_list(fd, &(key->block));
2503 key->adt= newdataadr(fd, key->adt);
2504 direct_link_animdata(fd, key->adt);
2506 key->refkey= newdataadr(fd, key->refkey);
2508 kb= key->block.first;
2511 kb->data= newdataadr(fd, kb->data);
2513 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2514 switch_endian_keyblock(key, kb);
2520 /* ************ READ mball ***************** */
2522 static void lib_link_mball(FileData *fd, Main *main)
2527 mb= main->mball.first;
2529 if(mb->id.flag & LIB_NEEDLINK) {
2530 if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
2532 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2534 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2536 mb->id.flag -= LIB_NEEDLINK;
2542 static void direct_link_mball(FileData *fd, MetaBall *mb)
2544 mb->adt= newdataadr(fd, mb->adt);
2545 direct_link_animdata(fd, mb->adt);
2547 mb->mat= newdataadr(fd, mb->mat);
2548 test_pointer_array(fd, (void **)&mb->mat);
2550 link_list(fd, &(mb->elems));
2552 mb->disp.first= mb->disp.last= NULL;
2553 mb->editelems= NULL;
2555 /* mb->edit_elems.first= mb->edit_elems.last= NULL;*/
2559 /* ************ READ WORLD ***************** */
2561 static void lib_link_world(FileData *fd, Main *main)
2567 wrld= main->world.first;
2569 if(wrld->id.flag & LIB_NEEDLINK) {
2570 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2572 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2574 for(a=0; a<MAX_MTEX; a++) {
2575 mtex= wrld->mtex[a];
2577 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2578 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2582 wrld->id.flag -= LIB_NEEDLINK;
2584 wrld= wrld->id.next;
2588 static void direct_link_world(FileData *fd, World *wrld)
2592 wrld->adt= newdataadr(fd, wrld->adt);
2593 direct_link_animdata(fd, wrld->adt);
2595 for(a=0; a<MAX_MTEX; a++) {
2596 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2598 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2602 /* ************ READ VFONT ***************** */
2604 static void lib_link_vfont(FileData *fd, Main *main)
2608 vf= main->vfont.first;
2610 if(vf->id.flag & LIB_NEEDLINK) {
2611 vf->id.flag -= LIB_NEEDLINK;
2617 static void direct_link_vfont(FileData *fd, VFont *vf)
2620 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2623 /* ************ READ TEXT ****************** */
2625 static void lib_link_text(FileData *fd, Main *main)
2629 text= main->text.first;
2631 if(text->id.flag & LIB_NEEDLINK) {
2632 text->id.flag -= LIB_NEEDLINK;
2634 text= text->id.next;
2638 static void direct_link_text(FileData *fd, Text *text)
2642 text->name= newdataadr(fd, text->name);
2645 text->undo_len= TXT_INIT_UNDO;
2646 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2648 text->compiled= NULL;
2651 if(text->flags & TXT_ISEXT) {
2656 link_list(fd, &text->lines);
2657 link_list(fd, &text->markers);
2659 text->curl= newdataadr(fd, text->curl);
2660 text->sell= newdataadr(fd, text->sell);
2662 ln= text->lines.first;
2664 ln->line= newdataadr(fd, ln->line);
2667 if (ln->len != (int) strlen(ln->line)) {
2668 printf("Error loading text, line lengths differ\n");
2669 ln->len = strlen(ln->line);
2675 text->flags = (text->flags) & ~TXT_ISEXT;
2680 /* ************ READ IMAGE ***************** */
2682 static void lib_link_image(FileData *fd, Main *main)
2686 ima= main->image.first;
2688 if(ima->id.flag & LIB_NEEDLINK) {
2689 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2691 ima->id.flag -= LIB_NEEDLINK;
2697 static void link_ibuf_list(FileData *fd, ListBase *lb)
2701 if(lb->first==NULL) return;
2703 lb->first= newimaadr(fd, lb->first);
2707 ln->next= newimaadr(fd, ln->next);
2715 static void direct_link_image(FileData *fd, Image *ima)
2717 /* for undo system, pointers could be restored */
2719 link_ibuf_list(fd, &ima->ibufs);
2721 ima->ibufs.first= ima->ibufs.last= NULL;
2723 /* if not restored, we keep the binded opengl index */
2724 if(ima->ibufs.first==NULL) {
2726 ima->gputexture= NULL;
2732 ima->render_text= newdataadr(fd, ima->render_text);
2734 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2735 ima->preview = direct_link_preview_image(fd, ima->preview);
2740 /* ************ READ CURVE ***************** */
2742 static void lib_link_curve(FileData *fd, Main *main)
2747 cu= main->curve.first;
2749 if(cu->id.flag & LIB_NEEDLINK) {
2750 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2752 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2754 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2755 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2756 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2757 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2758 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2759 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2760 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2762 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2763 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2765 cu->id.flag -= LIB_NEEDLINK;
2772 static void switch_endian_knots(Nurb *nu)
2779 SWITCH_INT(nu->knotsu[len]);
2785 SWITCH_INT(nu->knotsv[len]);
2790 static void direct_link_curve(FileData *fd, Curve *cu)
2795 cu->adt= newdataadr(fd, cu->adt);
2796 direct_link_animdata(fd, cu->adt);
2798 cu->mat= newdataadr(fd, cu->mat);
2799 test_pointer_array(fd, (void **)&cu->mat);
2800 cu->str= newdataadr(fd, cu->str);
2801 cu->strinfo= newdataadr(fd, cu->strinfo);
2802 cu->tb= newdataadr(fd, cu->tb);
2804 if(cu->vfont==0) link_list(fd, &(cu->nurb));
2806 cu->nurb.first=cu->nurb.last= 0;
2808 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2810 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2817 cu->tb[0].w = cu->linewidth;
2819 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2822 cu->bev.first=cu->bev.last= NULL;
2823 cu->disp.first=cu->disp.last= NULL;
2825 cu->lastselbp= NULL;
2831 nu->bezt= newdataadr(fd, nu->bezt);
2832 nu->bp= newdataadr(fd, nu->bp);
2833 nu->knotsu= newdataadr(fd, nu->knotsu);
2834 nu->knotsv= newdataadr(fd, nu->knotsv);
2835 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2837 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2838 switch_endian_knots(nu);
2846 /* ************ READ TEX ***************** */
2848 static void lib_link_texture(FileData *fd, Main *main)
2852 tex= main->tex.first;
2854 if(tex->id.flag & LIB_NEEDLINK) {
2855 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
2857 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2858 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2859 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2861 tex->pd->object= newlibadr(fd, tex->id.lib, tex->pd->object);
2862 tex->pd->psys= newlibadr(fd, tex->id.lib, tex->pd->psys);
2864 if(tex->vd) tex->vd->object= newlibadr(fd, tex->id.lib, tex->vd->object);
2867 lib_link_ntree(fd, &tex->id, tex->nodetree);
2869 tex->id.flag -= LIB_NEEDLINK;
2875 static void direct_link_texture(FileData *fd, Tex *tex)
2877 tex->adt= newdataadr(fd, tex->adt);
2878 direct_link_animdata(fd, tex->adt);
2880 tex->plugin= newdataadr(fd, tex->plugin);
2882 tex->plugin->handle= 0;
2883 open_plugin_tex(tex->plugin);
2884 /* initialize data for this instance, if an initialization
2887 if (tex->plugin->instance_init)
2888 tex->plugin->instance_init((void *) tex->plugin->data);
2890 tex->coba= newdataadr(fd, tex->coba);
2891 tex->env= newdataadr(fd, tex->env);
2893 tex->env->ima= NULL;
2894 memset(tex->env->cube, 0, 6*sizeof(void *));
2897 tex->pd= newdataadr(fd, tex->pd);
2899 tex->pd->point_tree = NULL;
2900 tex->pd->coba= newdataadr(fd, tex->pd->coba);
2903 tex->vd= newdataadr(fd, tex->vd);
2905 tex->vd->dataset = NULL;
2908 tex->nodetree= newdataadr(fd, tex->nodetree);
2910 direct_link_nodetree(fd, tex->nodetree);
2912 tex->preview = direct_link_preview_image(fd, tex->preview);
2919 /* ************ READ MATERIAL ***************** */
2921 static void lib_link_material(FileData *fd, Main *main)
2927 ma= main->mat.first;
2929 if(ma->id.flag & LIB_NEEDLINK) {
2930 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
2932 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2933 of library blocks that implement this.*/
2934 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2936 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2937 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2939 for(a=0; a<MAX_MTEX; a++) {
2942 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2943 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2948 lib_link_ntree(fd, &ma->id, ma->nodetree);
2950 ma->id.flag -= LIB_NEEDLINK;
2956 static void direct_link_material(FileData *fd, Material *ma)
2960 ma->adt= newdataadr(fd, ma->adt);
2961 direct_link_animdata(fd, ma->adt);
2963 for(a=0; a<MAX_MTEX; a++) {
2964 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2967 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2968 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2970 ma->nodetree= newdataadr(fd, ma->nodetree);
2972 direct_link_nodetree(fd, ma->nodetree);
2974 ma->preview = direct_link_preview_image(fd, ma->preview);
2975 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
2978 /* ************ READ PARTICLE SETTINGS ***************** */
2980 static void direct_link_pointcache(FileData *fd, PointCache *cache)
2982 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
2986 link_list(fd, &cache->mem_cache);
2988 pm = cache->mem_cache.first;
2990 for(; pm; pm=pm->next) {
2992 pm->index_array = newdataadr(fd, pm->index_array);
2994 for(i=0; i<BPHYS_TOT_DATA; i++) {
2995 if(pm->data[i] && pm->data_types & (1<<i))
2996 pm->data[i] = newdataadr(fd, pm->data[i]);
3001 cache->mem_cache.first = cache->mem_cache.last = NULL;
3003 cache->flag &= ~PTCACHE_SIMULATION_VALID;
3006 cache->free_edit= NULL;
3009 static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointCache **ocache)
3013 if(ptcaches->first) {
3014 link_list(fd, ptcaches);
3015 for(cache=ptcaches->first; cache; cache=cache->next)
3016 direct_link_pointcache(fd, cache);
3018 *ocache = newdataadr(fd, *ocache);
3021 /* old "single" caches need to be linked too */
3022 *ocache = newdataadr(fd, *ocache);
3023 direct_link_pointcache(fd, *ocache);
3025 ptcaches->first = ptcaches->last = *ocache;
3029 static void lib_link_particlesettings(FileData *fd, Main *main)
3031 ParticleSettings *part;
3033 part= main->particle.first;
3035 if(part->id.flag & LIB_NEEDLINK) {
3036 if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
3037 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
3039 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
3040 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
3041 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
3042 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
3044 BoidState *state = part->boids->states.first;
3046 for(; state; state=state->next) {
3047 rule = state->rules.first;
3048 for(; rule; rule=rule->next)
3049 switch(rule->type) {
3050 case eBoidRuleType_Goal:
3051 case eBoidRuleType_Avoid:
3053 BoidRuleGoalAvoid *brga = (BoidRuleGoalAvoid*)rule;
3054 brga->ob = newlibadr(fd, part->id.lib, brga->ob);
3057 case eBoidRuleType_FollowLeader:
3059 BoidRuleFollowLeader *brfl = (BoidRuleFollowLeader*)rule;
3060 brfl->ob = newlibadr(fd, part->id.lib, brfl->ob);
3066 part->id.flag -= LIB_NEEDLINK;
3068 part= part->id.next;
3072 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
3074 part->adt= newdataadr(fd, part->adt);
3075 part->pd= newdataadr(fd, part->pd);
3076 part->pd2= newdataadr(fd, part->pd2);
3078 part->boids= newdataadr(fd, part->boids);
3082 link_list(fd, &part->boids->states);
3084 for(state=part->boids->states.first; state; state=state->next) {
3085 link_list(fd, &state->rules);
3086 link_list(fd, &state->conditions);
3087 link_list(fd, &state->actions);
3092 static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase *particles)
3094 ParticleSystem *psys, *psysnext;
3096 for(psys=particles->first; psys; psys=psysnext){
3097 psysnext= psys->next;
3099 psys->part = newlibadr_us(fd, id->lib, psys->part);
3101 ParticleTarget *pt = psys->targets.first;
3103 for(; pt; pt=pt->next)
3104 pt->ob=newlibadr(fd, id->lib, pt->ob);
3106 psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
3109 /* particle modifier must be removed before particle system */
3110 ParticleSystemModifierData *psmd= psys_get_modifier(ob,psys);
3111 BLI_remlink(&ob->modifiers, psmd);
3112 modifier_free((ModifierData *)psmd);
3114 BLI_remlink(particles, psys);
3119 static void direct_link_particlesystems(FileData *fd, ListBase *particles)
3121 ParticleSystem *psys;
3125 for(psys=particles->first; psys; psys=psys->next) {
3126 psys->particles=newdataadr(fd,psys->particles);
3128 if(psys->particles && psys->particles->hair){
3129 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
3130 pa->hair=newdataadr(fd,pa->hair);
3133 if(psys->particles && psys->particles->keys){
3134 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++) {
3139 psys->flag &= ~PSYS_KEYED;
3142 if(psys->particles && psys->particles->boid) {
3143 pa = psys->particles;
3144 pa->boid = newdataadr(fd, pa->boid);
3145 for(a=1,pa++; a<psys->totpart; a++, pa++)
3146 pa->boid = (pa-1)->boid + 1;
3149 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
3154 psys->child=newdataadr(fd,psys->child);
3155 psys->effectors.first=psys->effectors.last=0;
3157 link_list(fd, &psys->targets);
3160 psys->free_edit = NULL;
3161 psys->pathcache = 0;
3162 psys->childcache = 0;
3163 psys->pathcachebufs.first = psys->pathcachebufs.last = 0;
3164 psys->childcachebufs.first = psys->childcachebufs.last = 0;
3165 psys->reactevents.first = psys->reactevents.last = 0;
3169 direct_link_pointcache_list(fd, &psys->ptcaches, &psys->pointcache);
3172 psys->clmd = newdataadr(fd, psys->clmd);
3173 psys->clmd->clothObject = NULL;
3175 psys->clmd->sim_parms= newdataadr(fd, psys->clmd->sim_parms);
3176 psys->clmd->coll_parms= newdataadr(fd, psys->clmd->coll_parms);
3178 if(psys->clmd->sim_parms) {
3179 if(psys->clmd->sim_parms->presets > 10)
3180 psys->clmd->sim_parms->presets = 0;
3183 psys->hair_in_dm = psys->hair_out_dm = NULL;
3185 psys->clmd->point_cache = psys->pointcache;
3193 /* ************ READ MESH ***************** */
3195 static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
3200 for (i=0; i<totface; i++, tf++) {
3201 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
3202 if(tf->tpage && tf->tpage->id.us==0)
3203 tf->tpage->id.us= 1;
3207 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
3210 for(i=0; i<fdata->totlayer; i++) {
3211 CustomDataLayer *layer = &fdata->layers[i];
3213 if(layer->type == CD_MTFACE)
3214 lib_link_mtface(fd, me, layer->data, totface);
3219 static void lib_link_mesh(FileData *fd, Main *main)
3223 me= main->mesh.first;
3225 if(me->id.flag & LIB_NEEDLINK) {
3228 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3229 of library blocks that implement this.*/
3230 if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3232 /* this check added for python created meshes */
3234 for(i=0; i<me->totcol; i++) {
3235 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
3240 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
3241 me->key= newlibadr_us(fd, me->id.lib, me->key);
3242 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
3244 lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
3245 if(me->mr && me->mr->levels.first)
3246 lib_link_customdata_mtface(fd, me, &me->mr->fdata,
3247 ((MultiresLevel*)me->mr->levels.first)->totface);
3249 me->id.flag -= LIB_NEEDLINK;
3255 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
3262 for (i=0; i<count; i++) {
3263 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
3265 mdverts[i].totweight=0;
3269 static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps)