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_math.h"
106 #include "BLI_storage_types.h" // for relname flags
108 #include "BKE_animsys.h"
109 #include "BKE_anim.h"
110 #include "BKE_action.h"
111 #include "BKE_armature.h"
112 #include "BKE_brush.h"
113 #include "BKE_cdderivedmesh.h"
114 #include "BKE_cloth.h"
115 #include "BKE_colortools.h"
116 #include "BKE_constraint.h"
117 #include "BKE_context.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_lattice.h"
129 #include "BKE_library.h" // for wich_libbase
130 #include "BKE_main.h" // for Main
131 #include "BKE_mesh.h" // for ME_ defines (patching)
132 #include "BKE_modifier.h"
133 #include "BKE_multires.h"
134 #include "BKE_node.h" // for tree type defines
135 #include "BKE_object.h"
136 #include "BKE_paint.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_screen.h"
144 #include "BKE_softbody.h" // sbNew()
145 #include "BKE_bullet.h" // bsbNew()
146 #include "BKE_sequencer.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 #include "BKE_sound.h"
153 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
154 //XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
155 //XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
156 #include "BLO_readfile.h"
157 #include "BLO_undofile.h"
158 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
160 #include "readfile.h"
162 #include "PIL_time.h"
167 Remark: still a weak point is the newadress() function, that doesnt solve reading from
168 multiple files at the same time
170 (added remark: oh, i thought that was solved? will look at that... (ton)
173 - Existing Library (Main) push or free
183 - read associated 'direct data'
184 - link direct data (internal and to LibBlock)
186 - read USER data, only when indicated (file is ~/.B.blend or .B25.blend)
188 - per Library (per Main)
191 - find LibBlocks and attach IDs to Main
192 - if external LibBlock
194 - or it's already read,
199 - read associated direct data
200 - link direct data (internal and to LibBlock)
202 - per Library with unread LibBlocks
207 - read associated direct data
208 - link direct data (internal and to LibBlock)
211 - link all LibBlocks and indirect pointers to libblocks
212 - initialize FileGlobal and copy pointers to Global
215 /* also occurs in library.c */
216 /* GS reads the memory pointed at in a specific ordering. There are,
217 * however two definitions for it. I have jotted them down here, both,
218 * but I think the first one is actually used. The thing is that
219 * big-endian systems might read this the wrong way round. OTOH, we
220 * constructed the IDs that are read out with this macro explicitly as
221 * well. I expect we'll sort it out soon... */
224 #define GS(a) (*((short *)(a)))
226 /* from misc_util: flip the bytes from x */
227 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
229 // only used here in readfile.c
230 #define SWITCH_LONGINT(a) { \
233 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
234 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
235 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
236 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
240 typedef struct OldNew {
245 typedef struct OldNewMap {
247 int nentries, entriessize;
253 /* local prototypes */
254 static void *read_struct(FileData *fd, BHead *bh, char *blockname);
257 static OldNewMap *oldnewmap_new(void)
259 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
261 onm->entriessize= 1024;
262 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
267 static int verg_oldnewmap(const void *v1, const void *v2)
269 const struct OldNew *x1=v1, *x2=v2;
271 if( x1->old > x2->old) return 1;
272 else if( x1->old < x2->old) return -1;
277 static void oldnewmap_sort(FileData *fd)
279 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
280 fd->libmap->sorted= 1;
283 /* nr is zero for data, and ID code for libdata */
284 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
288 if(oldaddr==NULL || newaddr==NULL) return;
290 if (onm->nentries==onm->entriessize) {
291 int osize= onm->entriessize;
292 OldNew *oentries= onm->entries;
294 onm->entriessize*= 2;
295 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
297 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
301 entry= &onm->entries[onm->nentries++];
303 entry->newp= newaddr;
307 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
311 if (onm->lasthit<onm->nentries-1) {
312 OldNew *entry= &onm->entries[++onm->lasthit];
314 if (entry->old==addr) {
320 for (i=0; i<onm->nentries; i++) {
321 OldNew *entry= &onm->entries[i];
323 if (entry->old==addr) {
334 /* for libdata, nr has ID code, no increment */
335 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
339 if(addr==NULL) return NULL;
341 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
343 OldNew entry_s, *entry;
347 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
351 if (id && (!lib || id->lib)) {
357 for (i=0; i<onm->nentries; i++) {
358 OldNew *entry= &onm->entries[i];
360 if (entry->old==addr) {
363 if (id && (!lib || id->lib)) {
372 static void oldnewmap_free_unused(OldNewMap *onm)
376 for (i=0; i<onm->nentries; i++) {
377 OldNew *entry= &onm->entries[i];
379 MEM_freeN(entry->newp);
385 static void oldnewmap_clear(OldNewMap *onm)
391 static void oldnewmap_free(OldNewMap *onm)
393 MEM_freeN(onm->entries);
399 static void read_libraries(FileData *basefd, ListBase *mainlist);
401 /* ************ help functions ***************** */
403 static void add_main_to_main(Main *mainvar, Main *from)
405 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
408 a= set_listbasepointers(mainvar, lbarray);
409 a= set_listbasepointers(from, fromarray);
411 addlisttolist(lbarray[a], fromarray[a]);
415 void blo_join_main(ListBase *mainlist)
417 Main *tojoin, *mainl;
420 mainl= mainlist->first;
421 while ((tojoin= mainl->next)) {
422 add_main_to_main(mainl, tojoin);
423 BLI_remlink(mainlist, tojoin);
428 static void split_libdata(ListBase *lb, Main *first)
440 if(mainvar->curlib==id->lib) {
441 lbn= wich_libbase(mainvar, GS(id->name));
443 BLI_addtail(lbn, id);
446 mainvar= mainvar->next;
448 if(mainvar==0) printf("error split_libdata\n");
454 void blo_split_main(ListBase *mainlist, Main *main)
456 ListBase *lbarray[MAX_LIBARRAY];
460 mainlist->first= mainlist->last= main;
463 if(main->library.first==NULL)
466 for (lib= main->library.first; lib; lib= lib->id.next) {
467 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
468 libmain->curlib= lib;
469 BLI_addtail(mainlist, libmain);
472 i= set_listbasepointers(main, lbarray);
474 split_libdata(lbarray[i], main->next);
477 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
478 static void cleanup_path(const char *relabase, char *name)
480 char filename[FILE_MAXFILE];
482 BLI_splitdirstring(name, filename);
483 BLI_cleanup_dir(relabase, name);
484 strcat(name, filename);
487 static void read_file_version(FileData *fd, Main *main)
491 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
492 if (bhead->code==GLOB) {
493 FileGlobal *fg= read_struct(fd, bhead, "Global");
495 main->subversionfile= fg->subversion;
496 main->minversionfile= fg->minversion;
497 main->minsubversionfile= fg->minsubversion;
500 else if (bhead->code==ENDB)
507 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *name, const char *relabase)
511 char name1[FILE_MAXDIR+FILE_MAXFILE];
513 strncpy(name1, name, sizeof(name1)-1);
514 cleanup_path(relabase, name1);
515 // printf("blo_find_main: original in %s\n", name);
516 // printf("blo_find_main: converted to %s\n", name1);
518 for (m= mainlist->first; m; m= m->next) {
519 char *libname= (m->curlib)?m->curlib->filename:m->name;
521 if (BLI_streq(name1, libname)) {
522 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
527 m= MEM_callocN(sizeof(Main), "find_main");
528 BLI_addtail(mainlist, m);
530 lib= alloc_libblock(&m->library, ID_LI, "lib");
531 strncpy(lib->name, name, sizeof(lib->name)-1);
532 BLI_strncpy(lib->filename, name1, sizeof(lib->filename));
536 read_file_version(fd, m);
538 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", name);
543 /* ************ FILE PARSING ****************** */
545 static void switch_endian_bh4(BHead4 *bhead)
547 /* the ID_.. codes */
548 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
550 if (bhead->code != ENDB) {
551 SWITCH_INT(bhead->len);
552 SWITCH_INT(bhead->SDNAnr);
553 SWITCH_INT(bhead->nr);
557 static void switch_endian_bh8(BHead8 *bhead)
559 /* the ID_.. codes */
560 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
562 if (bhead->code != ENDB) {
563 SWITCH_INT(bhead->len);
564 SWITCH_INT(bhead->SDNAnr);
565 SWITCH_INT(bhead->nr);
569 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
571 BHead4 *bhead4 = (BHead4 *) bhead;
572 #if defined(WIN32) && !defined(FREE_WINDOWS)
578 bhead4->code= bhead8->code;
579 bhead4->len= bhead8->len;
581 if (bhead4->code != ENDB) {
583 // why is this here ??
584 if (do_endian_swap) {
585 SWITCH_LONGINT(bhead8->old);
588 /* this patch is to avoid a long long being read from not-eight aligned positions
589 is necessary on any modern 64bit architecture) */
590 memcpy(&old, &bhead8->old, 8);
591 bhead4->old = (int) (old >> 3);
593 bhead4->SDNAnr= bhead8->SDNAnr;
594 bhead4->nr= bhead8->nr;
598 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
600 BHead8 *bhead8 = (BHead8 *) bhead;
602 bhead8->code= bhead4->code;
603 bhead8->len= bhead4->len;
605 if (bhead8->code != ENDB) {
606 bhead8->old= bhead4->old;
607 bhead8->SDNAnr= bhead4->SDNAnr;
608 bhead8->nr= bhead4->nr;
612 static BHeadN *get_bhead(FileData *fd)
617 BHeadN *new_bhead = 0;
623 /* not strictly needed but shuts valgrind up
624 * since uninitialized memory gets compared */
625 memset(&bhead8, 0, sizeof(BHead8));
626 memset(&bhead4, 0, sizeof(BHead4));
627 memset(&bhead, 0, sizeof(BHead));
629 // First read the bhead structure.
630 // Depending on the platform the file was written on this can
631 // be a big or little endian BHead4 or BHead8 structure.
633 // As usual 'ENDB' (the last *partial* bhead of the file)
634 // needs some special handling. We don't want to EOF just yet.
636 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
638 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
640 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
641 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
642 switch_endian_bh4(&bhead4);
645 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
646 bh8_from_bh4(&bhead, &bhead4);
648 memcpy(&bhead, &bhead4, sizeof(bhead));
656 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
658 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
659 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
660 switch_endian_bh8(&bhead8);
663 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
664 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
666 memcpy(&bhead, &bhead8, sizeof(bhead));
674 /* make sure people are not trying to pass bad blend files */
675 if (bhead.len < 0) fd->eof = 1;
677 // bhead now contains the (converted) bhead structure. Now read
678 // the associated data and put everything in a BHeadN (creative naming !)
681 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
683 new_bhead->next = new_bhead->prev = 0;
684 new_bhead->bhead = bhead;
686 readsize = fd->read(fd, new_bhead + 1, bhead.len);
688 if (readsize != bhead.len) {
690 MEM_freeN(new_bhead);
700 // We've read a new block. Now add it to the list
704 BLI_addtail(&fd->listbase, new_bhead);
710 BHead *blo_firstbhead(FileData *fd)
716 // Read in a new block if necessary
718 new_bhead = fd->listbase.first;
719 if (new_bhead == 0) {
720 new_bhead = get_bhead(fd);
724 bhead = &new_bhead->bhead;
730 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
732 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
733 BHeadN *prev= bheadn->prev;
735 return prev?&prev->bhead:NULL;
738 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
740 BHeadN *new_bhead = NULL;
744 // bhead is actually a sub part of BHeadN
745 // We calculate the BHeadN pointer from the BHead pointer below
746 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
748 // get the next BHeadN. If it doesn't exist we read in the next one
749 new_bhead = new_bhead->next;
750 if (new_bhead == 0) {
751 new_bhead = get_bhead(fd);
756 // here we do the reverse:
757 // go from the BHeadN pointer to the BHead pointer
758 bhead = &new_bhead->bhead;
764 static void decode_blender_header(FileData *fd)
766 char header[SIZEOFBLENDERHEADER], num[4];
769 // read in the header data
770 readsize = fd->read(fd, header, sizeof(header));
772 if (readsize == sizeof(header)) {
773 if(strncmp(header, "BLENDER", 7) == 0) {
774 int remove_this_endian_test= 1;
776 fd->flags |= FD_FLAGS_FILE_OK;
778 // what size are pointers in the file ?
780 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
781 if (sizeof(void *) != 4) {
782 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
785 if (sizeof(void *) != 8) {
786 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
790 // is the file saved in a different endian
792 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
793 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
796 // get the version number
798 memcpy(num, header+9, 3);
800 fd->fileversion = atoi(num);
805 static int read_file_dna(FileData *fd)
809 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
810 if (bhead->code==DNA1) {
811 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
813 fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
816 fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
817 /* used to retrieve ID names from (bhead+1) */
818 fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
822 } else if (bhead->code==ENDB)
829 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
831 int readsize = read(filedata->filedes, buffer, size);
836 filedata->seek += readsize;
842 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
844 int readsize = gzread(filedata->gzfiledes, buffer, size);
849 filedata->seek += readsize;
855 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
857 // don't read more bytes then there are available in the buffer
858 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
860 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
861 filedata->seek += readsize;
866 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
868 static unsigned int seek= 1<<30; /* the current position */
869 static unsigned int offset= 0; /* size of previous chunks */
870 static MemFileChunk *chunk=NULL;
871 unsigned int chunkoffset, readsize, totread;
873 if(size==0) return 0;
875 if(seek != (unsigned int)filedata->seek) {
876 chunk= filedata->memfile->chunks.first;
880 if(seek + chunk->size > (unsigned) filedata->seek) break;
885 seek= filedata->seek;
892 /* first check if it's on the end if current chunk */
893 if(seek-offset == chunk->size) {
894 offset+= chunk->size;
898 /* debug, should never happen */
900 printf("illegal read, chunk zero\n");
904 chunkoffset= seek-offset;
905 readsize= size-totread;
907 /* data can be spread over multiple chunks, so clamp size
908 * to within this chunk, and then it will read further in
910 if(chunkoffset+readsize > chunk->size)
911 readsize= chunk->size-chunkoffset;
913 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
915 filedata->seek += readsize;
917 } while(totread < size);
925 static FileData *filedata_new(void)
927 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
930 fd->gzfiledes = NULL;
932 /* XXX, this doesn't need to be done all the time,
933 * but it keeps us reentrant, remove once we have
934 * a lib that provides a nice lock. - zr
936 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
938 fd->datamap = oldnewmap_new();
939 fd->globmap = oldnewmap_new();
940 fd->libmap = oldnewmap_new();
945 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
947 decode_blender_header(fd);
949 if (fd->flags & FD_FLAGS_FILE_OK) {
950 if (!read_file_dna(fd)) {
951 BKE_report(reports, RPT_ERROR, "File incomplete");
952 blo_freefiledata(fd);
957 BKE_report(reports, RPT_ERROR, "File is not a Blender file");
958 blo_freefiledata(fd);
965 /* cannot be called with relative paths anymore! */
966 /* on each new library added, it now checks for the current FileData and expands relativeness */
967 FileData *blo_openblenderfile(char *name, ReportList *reports)
971 gzfile= gzopen(name, "rb");
973 if (NULL == gzfile) {
974 BKE_report(reports, RPT_ERROR, "Unable to open");
977 FileData *fd = filedata_new();
978 fd->gzfiledes = gzfile;
979 fd->read = fd_read_gzip_from_file;
981 /* needed for library_append and read_libraries */
982 BLI_strncpy(fd->relabase, name, sizeof(fd->relabase));
984 return blo_decode_and_check(fd, reports);
988 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
990 if (!mem || memsize<SIZEOFBLENDERHEADER) {
991 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
994 FileData *fd= filedata_new();
996 fd->buffersize= memsize;
997 fd->read= fd_read_from_memory;
998 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
1000 return blo_decode_and_check(fd, reports);
1004 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
1007 BKE_report(reports, RPT_ERROR, "Unable to open");
1010 FileData *fd= filedata_new();
1011 fd->memfile= memfile;
1013 fd->read= fd_read_from_memfile;
1014 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
1016 return blo_decode_and_check(fd, reports);
1021 void blo_freefiledata(FileData *fd)
1025 if (fd->filedes != -1) {
1029 if (fd->gzfiledes != NULL)
1031 gzclose(fd->gzfiledes);
1034 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1035 MEM_freeN(fd->buffer);
1039 // Free all BHeadN data blocks
1040 BLI_freelistN(&fd->listbase);
1043 DNA_sdna_free(fd->memsdna);
1045 DNA_sdna_free(fd->filesdna);
1047 MEM_freeN(fd->compflags);
1050 oldnewmap_free(fd->datamap);
1052 oldnewmap_free(fd->globmap);
1054 oldnewmap_free(fd->imamap);
1055 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1056 oldnewmap_free(fd->libmap);
1058 MEM_freeN(fd->bheadmap);
1064 /* ************ DIV ****************** */
1066 int BLO_has_bfile_extension(char *str)
1068 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1071 int BLO_is_a_library(char *path, char *dir, char *group)
1073 /* return ok when a blenderfile, in dir is the filename,
1074 * in group the type of libdata
1082 if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
1087 /* Find the last slash */
1088 fd= (strrchr(dir, '/')>strrchr(dir, '\\'))?strrchr(dir, '/'):strrchr(dir, '\\');
1092 if(BLO_has_bfile_extension(fd+1)) {
1093 /* the last part of the dir is a .blend file, no group follows */
1094 *fd= '/'; /* put back the removed slash separating the dir and the .blend file name */
1097 char *gp = fd+1; // in case we have a .blend file, gp points to the group
1099 /* Find the last slash */
1100 fd= (strrchr(dir, '/')>strrchr(dir, '\\'))?strrchr(dir, '/'):strrchr(dir, '\\');
1101 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
1103 /* now we know that we are in a blend file and it is safe to
1104 assume that gp actually points to a group */
1105 if (BLI_streq("Screen", gp)==0)
1106 BLI_strncpy(group, gp, GROUP_MAX);
1111 /* ************** OLD POINTERS ******************* */
1113 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1115 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1118 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1120 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1123 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1125 if(fd->imamap && adr)
1126 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1131 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1133 return oldnewmap_liblookup(fd->libmap, adr, lib);
1136 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1138 ID *id= newlibadr(fd, lib, adr);
1146 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1150 for (i=0; i<fd->libmap->nentries; i++) {
1151 OldNew *entry= &fd->libmap->entries[i];
1153 if (old==entry->newp && entry->nr==ID_ID) {
1155 if(new) entry->nr= GS( ((ID *)new)->name );
1161 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1165 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1168 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1172 change_idid_adr_fd(fd, old, new);
1177 /* lib linked proxy objects point to our local data, we need
1178 * to clear that pointer before reading the undo memfile since
1179 * the object might be removed, it is set again in reading
1180 * if the local object still exists */
1181 void blo_clear_proxy_pointers_from_lib(FileData *fd, Main *oldmain)
1183 Object *ob= oldmain->object.first;
1185 for(;ob; ob= ob->id.next)
1187 ob->proxy_from= NULL;
1190 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1192 Image *ima= oldmain->image.first;
1193 Scene *sce= oldmain->scene.first;
1195 fd->imamap= oldnewmap_new();
1197 for(;ima; ima= ima->id.next) {
1198 Link *ibuf= ima->ibufs.first;
1199 for(; ibuf; ibuf= ibuf->next)
1200 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1202 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1204 for(; sce; sce= sce->id.next) {
1207 for(node= sce->nodetree->nodes.first; node; node= node->next)
1208 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1213 /* set old main image ibufs to zero if it has been restored */
1214 /* this works because freeing old main only happens after this call */
1215 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1217 OldNew *entry= fd->imamap->entries;
1218 Image *ima= oldmain->image.first;
1219 Scene *sce= oldmain->scene.first;
1222 /* used entries were restored, so we put them to zero */
1223 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1228 for(;ima; ima= ima->id.next) {
1231 /* this mirrors direct_link_image */
1232 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1234 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1235 BLI_remlink(&ima->ibufs, ibuf);
1237 ima->gputexture= NULL;
1241 ima->gputexture= newimaadr(fd, ima->gputexture);
1243 for(; sce; sce= sce->id.next) {
1246 for(node= sce->nodetree->nodes.first; node; node= node->next)
1247 node->preview= newimaadr(fd, node->preview);
1252 /* undo file support: add all library pointers in lookup */
1253 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1255 Main *ptr= mainlist->first;
1256 ListBase *lbarray[MAX_LIBARRAY];
1258 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1259 int i= set_listbasepointers(ptr, lbarray);
1262 for(id= lbarray[i]->first; id; id= id->next)
1263 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1269 /* ********** END OLD POINTERS ****************** */
1270 /* ********** READ FILE ****************** */
1272 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1274 int blocksize, nblocks;
1277 data= (char *)(bhead+1);
1278 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1282 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1288 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1293 /* switch is based on file dna */
1294 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1295 switch_endian_structs(fd->filesdna, bh);
1297 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1298 if(fd->compflags[bh->SDNAnr]==2) {
1299 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1301 temp= MEM_mallocN(bh->len, blockname);
1302 memcpy(temp, (bh+1), bh->len);
1310 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1314 if(lb->first==NULL) return;
1316 lb->first= newdataadr(fd, lb->first);
1320 ln->next= newdataadr(fd, ln->next);
1328 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1333 if(lb->first==0) return;
1334 poin= newdataadr(fd, lb->first);
1336 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1343 poin= newdataadr(fd, ln->next);
1345 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1355 static void test_pointer_array(FileData *fd, void **mat)
1357 #if defined(WIN32) && !defined(FREE_WINDOWS)
1358 __int64 *lpoin, *lmat;
1360 long long *lpoin, *lmat;
1362 int len, *ipoin, *imat;
1364 /* manually convert the pointer array in
1365 * the old dna format to a pointer array in
1366 * the new dna format.
1369 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1371 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1372 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1376 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1377 SWITCH_LONGINT(*lpoin);
1378 *ipoin= (int) ((*lpoin) >> 3);
1386 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1387 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1401 /* ************ READ ID Properties *************** */
1403 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1404 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1406 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1411 /*since we didn't save the extra buffer, set totallen to len.*/
1412 prop->totallen = prop->len;
1413 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1415 array= (IDProperty*) prop->data.pointer;
1417 for(i=0; i<prop->len; i++)
1418 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
1421 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1426 /*since we didn't save the extra buffer, set totallen to len.*/
1427 prop->totallen = prop->len;
1428 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1430 if(prop->subtype == IDP_GROUP) {
1431 test_pointer_array(fd, prop->data.pointer);
1432 array= prop->data.pointer;
1434 for(i=0; i<prop->len; i++)
1435 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1437 else if(prop->subtype == IDP_DOUBLE) {
1438 if (switch_endian) {
1439 for (i=0; i<prop->len; i++) {
1440 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1445 if (switch_endian) {
1446 for (i=0; i<prop->len; i++) {
1447 SWITCH_INT(((int*)prop->data.pointer)[i]);
1453 static void IDP_DirectLinkString(IDProperty *prop, int switch_endian, FileData *fd)
1455 /*since we didn't save the extra string buffer, set totallen to len.*/
1456 prop->totallen = prop->len;
1457 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1460 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1462 ListBase *lb = &prop->data.group;
1467 /*Link child id properties now*/
1468 for (loop=prop->data.group.first; loop; loop=loop->next) {
1469 IDP_DirectLinkProperty(loop, switch_endian, fd);
1473 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1475 switch (prop->type) {
1477 IDP_DirectLinkGroup(prop, switch_endian, fd);
1480 IDP_DirectLinkString(prop, switch_endian, fd);
1483 IDP_DirectLinkArray(prop, switch_endian, fd);
1486 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1489 /*erg, stupid doubles. since I'm storing them
1490 in the same field as int val; val2 in the
1491 IDPropertyData struct, they have to deal with
1492 endianness specifically
1494 in theory, val and val2 would've already been swapped
1495 if switch_endian is true, so we have to first unswap
1496 them then reswap them as a single 64-bit entity.
1499 if (switch_endian) {
1500 SWITCH_INT(prop->data.val);
1501 SWITCH_INT(prop->data.val2);
1502 SWITCH_LONGINT(prop->data.val);
1510 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1514 /* ************ READ CurveMapping *************** */
1516 /* cuma itself has been read! */
1517 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1521 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1522 cumap->flag &= ~CUMA_PREMULLED;
1524 for(a=0; a<CM_TOT; a++) {
1525 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1526 cumap->cm[a].table= NULL;
1530 /* ************ READ Brush *************** */
1531 /* library brush linking after fileread */
1532 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->mtex.tex= newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
1542 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1547 static void direct_link_brush(FileData *fd, Brush *brush)
1549 /* brush itself has been read */
1552 brush->curve= newdataadr(fd, brush->curve);
1554 direct_link_curvemapping(fd, brush->curve);
1556 brush_curve_preset(brush, CURVE_PRESET_SHARP);
1559 static void direct_link_script(FileData *fd, Script *script)
1562 SCRIPT_SET_NULL(script)
1566 /* ************ READ PACKEDFILE *************** */
1568 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1570 PackedFile *pf= newdataadr(fd, oldpf);
1573 pf->data= newdataadr(fd, pf->data);
1579 /* ************ READ IMAGE PREVIEW *************** */
1581 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1583 PreviewImage *prv= newdataadr(fd, old_prv);
1587 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1589 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1597 /* ************ READ ANIMATION STUFF ***************** */
1599 /* Legacy Data Support (for Version Patching) ----------------------------- */
1601 // XXX depreceated - old animation system
1602 static void lib_link_ipo(FileData *fd, Main *main)
1606 ipo= main->ipo.first;
1608 if(ipo->id.flag & LIB_NEEDLINK) {
1610 for(icu= ipo->curve.first; icu; icu= icu->next) {
1612 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1614 ipo->id.flag -= LIB_NEEDLINK;
1620 // XXX depreceated - old animation system
1621 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1625 link_list(fd, &(ipo->curve));
1626 icu= ipo->curve.first;
1628 icu->bezt= newdataadr(fd, icu->bezt);
1629 icu->bp= newdataadr(fd, icu->bp);
1630 icu->driver= newdataadr(fd, icu->driver);
1635 // XXX depreceated - old animation system
1636 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1638 bActionStrip *strip;
1639 bActionModifier *amod;
1641 for (strip=striplist->first; strip; strip=strip->next){
1642 strip->object = newlibadr(fd, id->lib, strip->object);
1643 strip->act = newlibadr_us(fd, id->lib, strip->act);
1644 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1645 for(amod= strip->modifiers.first; amod; amod= amod->next)
1646 amod->ob= newlibadr(fd, id->lib, amod->ob);
1650 // XXX depreceated - old animation system
1651 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1653 bActionStrip *strip;
1655 link_list(fd, strips);
1657 for(strip= strips->first; strip; strip= strip->next)
1658 link_list(fd, &strip->modifiers);
1661 // XXX depreceated - old animation system
1662 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1664 bConstraintChannel *chan;
1666 for (chan=chanbase->first; chan; chan=chan->next){
1667 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1671 /* Data Linking ----------------------------- */
1673 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1677 for (fcm= list->first; fcm; fcm= fcm->next) {
1678 /* data for specific modifiers */
1679 switch (fcm->type) {
1680 case FMODIFIER_TYPE_PYTHON:
1682 FMod_Python *data= (FMod_Python *)fcm->data;
1683 data->script = newlibadr(fd, id->lib, data->script);
1690 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1697 /* relink ID-block references... */
1698 for (fcu= list->first; fcu; fcu= fcu->next) {
1701 ChannelDriver *driver= fcu->driver;
1704 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1705 DRIVER_TARGETS_LOOPER(dvar)
1707 /* only relink if still used */
1708 if (tarIndex < dvar->num_targets)
1709 dtar->id= newlibadr(fd, id->lib, dtar->id);
1713 DRIVER_TARGETS_LOOPER_END
1718 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1723 /* NOTE: this assumes that link_list has already been called on the list */
1724 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1728 for (fcm= list->first; fcm; fcm= fcm->next) {
1729 /* relink general data */
1730 fcm->data = newdataadr(fd, fcm->data);
1733 /* do relinking of data for specific types */
1734 switch (fcm->type) {
1735 case FMODIFIER_TYPE_GENERATOR:
1737 FMod_Generator *data= (FMod_Generator *)fcm->data;
1739 data->coefficients= newdataadr(fd, data->coefficients);
1742 case FMODIFIER_TYPE_ENVELOPE:
1744 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1746 data->data= newdataadr(fd, data->data);
1749 case FMODIFIER_TYPE_PYTHON:
1751 FMod_Python *data= (FMod_Python *)fcm->data;
1753 data->prop = newdataadr(fd, data->prop);
1754 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1761 /* NOTE: this assumes that link_list has already been called on the list */
1762 static void direct_link_fcurves(FileData *fd, ListBase *list)
1766 /* link F-Curve data to F-Curve again (non ID-libs) */
1767 for (fcu= list->first; fcu; fcu= fcu->next) {
1769 fcu->bezt= newdataadr(fd, fcu->bezt);
1770 fcu->fpt= newdataadr(fd, fcu->fpt);
1773 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1776 fcu->grp= newdataadr(fd, fcu->grp);
1779 fcu->driver= newdataadr(fd, fcu->driver);
1781 ChannelDriver *driver= fcu->driver;
1784 /* relink variables, targets and their paths */
1785 link_list(fd, &driver->variables);
1786 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1787 DRIVER_TARGETS_LOOPER(dvar)
1789 /* only relink the targets being used */
1790 if (tarIndex < dvar->num_targets)
1791 dtar->rna_path= newdataadr(fd, dtar->rna_path);
1793 dtar->rna_path= NULL;
1795 DRIVER_TARGETS_LOOPER_END
1800 link_list(fd, &fcu->modifiers);
1801 direct_link_fmodifiers(fd, &fcu->modifiers);
1806 static void lib_link_action(FileData *fd, Main *main)
1809 bActionChannel *chan;
1811 for (act= main->action.first; act; act= act->id.next) {
1812 if (act->id.flag & LIB_NEEDLINK) {
1813 act->id.flag -= LIB_NEEDLINK;
1815 // XXX depreceated - old animation system <<<
1816 for (chan=act->chanbase.first; chan; chan=chan->next) {
1817 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1818 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1820 // >>> XXX depreceated - old animation system
1822 lib_link_fcurves(fd, &act->id, &act->curves);
1827 static void direct_link_action(FileData *fd, bAction *act)
1829 bActionChannel *achan; // XXX depreceated - old animation system
1832 link_list(fd, &act->curves);
1833 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1834 link_list(fd, &act->groups);
1835 link_list(fd, &act->markers);
1837 // XXX depreceated - old animation system <<<
1838 for (achan = act->chanbase.first; achan; achan=achan->next) {
1839 achan->grp= newdataadr(fd, achan->grp);
1841 link_list(fd, &achan->constraintChannels);
1843 // >>> XXX depreceated - old animation system
1845 direct_link_fcurves(fd, &act->curves);
1847 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1848 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1849 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1853 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
1857 for (strip= list->first; strip; strip= strip->next) {
1858 /* check strip's children */
1859 lib_link_nladata_strips(fd, id, &strip->strips);
1861 /* check strip's F-Curves */
1862 lib_link_fcurves(fd, id, &strip->fcurves);
1864 /* reassign the counted-reference to action */
1865 strip->act = newlibadr_us(fd, id->lib, strip->act);
1869 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
1873 /* we only care about the NLA strips inside the tracks */
1874 for (nlt= list->first; nlt; nlt= nlt->next) {
1875 lib_link_nladata_strips(fd, id, &nlt->strips);
1879 /* This handles Animato NLA-Strips linking
1880 * NOTE: this assumes that link_list has already been called on the list
1882 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
1886 for (strip= list->first; strip; strip= strip->next) {
1887 /* strip's child strips */
1888 link_list(fd, &strip->strips);
1889 direct_link_nladata_strips(fd, &strip->strips);
1891 /* strip's F-Curves */
1892 link_list(fd, &strip->fcurves);
1893 direct_link_fcurves(fd, &strip->fcurves);
1895 /* strip's F-Modifiers */
1896 link_list(fd, &strip->modifiers);
1897 direct_link_fcurves(fd, &strip->modifiers);
1901 /* NOTE: this assumes that link_list has already been called on the list */
1902 static void direct_link_nladata(FileData *fd, ListBase *list)
1906 for (nlt= list->first; nlt; nlt= nlt->next) {
1907 /* relink list of strips */
1908 link_list(fd, &nlt->strips);
1910 /* relink strip data */
1911 direct_link_nladata_strips(fd, &nlt->strips);
1917 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
1922 /* here, we're only interested in the ID pointer stored in some of the paths */
1923 for (ks= list->first; ks; ks= ks->next) {
1924 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1925 ksp->id= newlibadr(fd, id->lib, ksp->id);
1930 /* NOTE: this assumes that link_list has already been called on the list */
1931 static void direct_link_keyingsets(FileData *fd, ListBase *list)
1936 /* link KeyingSet data to KeyingSet again (non ID-libs) */
1937 for (ks= list->first; ks; ks= ks->next) {
1939 link_list(fd, &ks->paths);
1941 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1943 ksp->rna_path= newdataadr(fd, ksp->rna_path);
1950 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
1955 /* link action data */
1956 adt->action= newlibadr_us(fd, id->lib, adt->action);
1957 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
1960 lib_link_fcurves(fd, id, &adt->drivers);
1962 /* overrides don't have lib-link for now, so no need to do anything */
1965 lib_link_nladata(fd, id, &adt->nla_tracks);
1968 static void direct_link_animdata(FileData *fd, AnimData *adt)
1970 /* NOTE: must have called newdataadr already before doing this... */
1975 link_list(fd, &adt->drivers);
1976 direct_link_fcurves(fd, &adt->drivers);
1978 /* link overrides */
1982 link_list(fd, &adt->nla_tracks);
1983 direct_link_nladata(fd, &adt->nla_tracks);
1985 /* clear temp pointers that may have been set... */
1986 // TODO: it's probably only a small cost to reload this anyway...
1987 adt->actstrip= NULL;
1990 /* ************ READ MOTION PATHS *************** */
1992 /* direct data for cache */
1993 static void direct_link_motionpath(FileData *fd, bMotionPath *mpath)
1999 /* relink points cache */
2000 mpath->points= newdataadr(fd, mpath->points);
2003 /* ************ READ NODE TREE *************** */
2005 /* singe node tree (also used for material/scene trees), ntree is not NULL */
2006 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
2010 if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
2012 ntree->gpd= newlibadr_us(fd, id->lib, ntree->gpd);
2014 for(node= ntree->nodes.first; node; node= node->next)
2015 node->id= newlibadr_us(fd, id->lib, node->id);
2018 /* library ntree linking after fileread */
2019 static void lib_link_nodetree(FileData *fd, Main *main)
2023 /* only link ID pointers */
2024 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2025 if(ntree->id.flag & LIB_NEEDLINK) {
2026 ntree->id.flag -= LIB_NEEDLINK;
2027 lib_link_ntree(fd, &ntree->id, ntree);
2032 /* verify types for nodes and groups, all data has to be read */
2033 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
2035 static void lib_verify_nodetree(Main *main, int open)
2042 /* this crashes blender on undo/redo
2044 reinit_nodesystem();
2047 /* now create the own typeinfo structs an verify nodes */
2048 /* here we still assume no groups in groups */
2049 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2050 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
2051 ntreeMakeOwnType(ntree); /* for group usage */
2054 /* now verify all types in material trees, groups are set OK now */
2055 for(ma= main->mat.first; ma; ma= ma->id.next) {
2057 ntreeVerifyTypes(ma->nodetree);
2059 /* and scene trees */
2060 for(sce= main->scene.first; sce; sce= sce->id.next) {
2062 ntreeVerifyTypes(sce->nodetree);
2064 /* and texture trees */
2065 for(tx= main->tex.first; tx; tx= tx->id.next) {
2067 ntreeVerifyTypes(tx->nodetree);
2073 /* ntree itself has been read! */
2074 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2076 /* note: writing and reading goes in sync, for speed */
2081 ntree->init= 0; /* to set callbacks and force setting types */
2082 ntree->owntype= NULL;
2083 ntree->timecursor= NULL;
2085 ntree->adt= newdataadr(fd, ntree->adt);
2086 direct_link_animdata(fd, ntree->adt);
2088 link_list(fd, &ntree->nodes);
2089 for(node= ntree->nodes.first; node; node= node->next) {
2090 if(node->type == NODE_DYNAMIC) {
2092 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
2093 node->typeinfo= NULL;
2096 node->storage= newdataadr(fd, node->storage);
2099 /* could be handlerized at some point */
2100 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
2101 direct_link_curvemapping(fd, node->storage);
2102 else if(ntree->type==NTREE_COMPOSIT) {
2103 if( ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB))
2104 direct_link_curvemapping(fd, node->storage);
2105 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2106 ((ImageUser *)node->storage)->ok= 1;
2108 else if( ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) ) {
2109 direct_link_curvemapping(fd, node->storage);
2112 link_list(fd, &node->inputs);
2113 link_list(fd, &node->outputs);
2115 link_list(fd, &ntree->links);
2117 /* and we connect the rest */
2118 for(node= ntree->nodes.first; node; node= node->next) {
2119 node->preview= newimaadr(fd, node->preview);
2121 for(sock= node->inputs.first; sock; sock= sock->next)
2122 sock->link= newdataadr(fd, sock->link);
2123 for(sock= node->outputs.first; sock; sock= sock->next)
2124 sock->ns.data= NULL;
2126 for(link= ntree->links.first; link; link= link->next) {
2127 link->fromnode= newdataadr(fd, link->fromnode);
2128 link->tonode= newdataadr(fd, link->tonode);
2129 link->fromsock= newdataadr(fd, link->fromsock);
2130 link->tosock= newdataadr(fd, link->tosock);
2133 /* type verification is in lib-link */
2136 /* ************ READ ARMATURE ***************** */
2138 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2142 for (con = conlist->first; con; con=con->next) {
2143 /* patch for error introduced by changing constraints (dunno how) */
2144 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2145 if(con->data==NULL) {
2146 con->type= CONSTRAINT_TYPE_NULL;
2148 /* own ipo, all constraints have it */
2149 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2151 switch (con->type) {
2152 case CONSTRAINT_TYPE_PYTHON:
2154 bPythonConstraint *data= (bPythonConstraint*)con->data;
2155 bConstraintTarget *ct;
2157 for (ct= data->targets.first; ct; ct= ct->next)
2158 ct->tar = newlibadr(fd, id->lib, ct->tar);
2160 data->text = newlibadr(fd, id->lib, data->text);
2161 //IDP_LibLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2164 case CONSTRAINT_TYPE_ACTION:
2166 bActionConstraint *data;
2167 data= ((bActionConstraint*)con->data);
2168 data->tar = newlibadr(fd, id->lib, data->tar);
2169 data->act = newlibadr(fd, id->lib, data->act);
2172 case CONSTRAINT_TYPE_LOCLIKE:
2174 bLocateLikeConstraint *data;
2175 data= ((bLocateLikeConstraint*)con->data);
2176 data->tar = newlibadr(fd, id->lib, data->tar);
2179 case CONSTRAINT_TYPE_ROTLIKE:
2181 bRotateLikeConstraint *data;
2182 data= ((bRotateLikeConstraint*)con->data);
2183 data->tar = newlibadr(fd, id->lib, data->tar);
2186 case CONSTRAINT_TYPE_SIZELIKE:
2188 bSizeLikeConstraint *data;
2189 data= ((bSizeLikeConstraint*)con->data);
2190 data->tar = newlibadr(fd, id->lib, data->tar);
2193 case CONSTRAINT_TYPE_KINEMATIC:
2195 bKinematicConstraint *data;
2196 data = ((bKinematicConstraint*)con->data);
2197 data->tar = newlibadr(fd, id->lib, data->tar);
2198 data->poletar = newlibadr(fd, id->lib, data->poletar);
2199 con->lin_error = 0.f;
2200 con->rot_error = 0.f;
2203 case CONSTRAINT_TYPE_TRACKTO:
2205 bTrackToConstraint *data;
2206 data = ((bTrackToConstraint*)con->data);
2207 data->tar = newlibadr(fd, id->lib, data->tar);
2210 case CONSTRAINT_TYPE_MINMAX:
2212 bMinMaxConstraint *data;
2213 data = ((bMinMaxConstraint*)con->data);
2214 data->tar = newlibadr(fd, id->lib, data->tar);
2217 case CONSTRAINT_TYPE_LOCKTRACK:
2219 bLockTrackConstraint *data;
2220 data= ((bLockTrackConstraint*)con->data);
2221 data->tar = newlibadr(fd, id->lib, data->tar);
2224 case CONSTRAINT_TYPE_FOLLOWPATH:
2226 bFollowPathConstraint *data;
2227 data= ((bFollowPathConstraint*)con->data);
2228 data->tar = newlibadr(fd, id->lib, data->tar);
2231 case CONSTRAINT_TYPE_STRETCHTO:
2233 bStretchToConstraint *data;
2234 data= ((bStretchToConstraint*)con->data);
2235 data->tar = newlibadr(fd, id->lib, data->tar);
2238 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
2240 bRigidBodyJointConstraint *data;
2241 data= ((bRigidBodyJointConstraint*)con->data);
2242 data->tar = newlibadr(fd, id->lib, data->tar);
2245 case CONSTRAINT_TYPE_CLAMPTO:
2247 bClampToConstraint *data;
2248 data= ((bClampToConstraint*)con->data);
2249 data->tar = newlibadr(fd, id->lib, data->tar);
2252 case CONSTRAINT_TYPE_CHILDOF:
2254 bChildOfConstraint *data;
2255 data= ((bChildOfConstraint*)con->data);
2256 data->tar = newlibadr(fd, id->lib, data->tar);
2259 case CONSTRAINT_TYPE_TRANSFORM:
2261 bTransformConstraint *data;
2262 data= ((bTransformConstraint*)con->data);
2263 data->tar = newlibadr(fd, id->lib, data->tar);
2266 case CONSTRAINT_TYPE_DISTLIMIT:
2268 bDistLimitConstraint *data;
2269 data= ((bDistLimitConstraint*)con->data);
2270 data->tar = newlibadr(fd, id->lib, data->tar);
2273 case CONSTRAINT_TYPE_SHRINKWRAP:
2275 bShrinkwrapConstraint *data;
2276 data= ((bShrinkwrapConstraint*)con->data);
2277 data->target = newlibadr(fd, id->lib, data->target);
2280 case CONSTRAINT_TYPE_DAMPTRACK:
2282 bDampTrackConstraint *data;
2283 data= ((bDampTrackConstraint*)con->data);
2284 data->tar = newlibadr(fd, id->lib, data->tar);
2287 case CONSTRAINT_TYPE_SPLINEIK:
2289 bSplineIKConstraint *data;
2290 data= ((bSplineIKConstraint*)con->data);
2291 data->tar = newlibadr(fd, id->lib, data->tar);
2294 case CONSTRAINT_TYPE_TRANSLIKE:
2296 bTransLikeConstraint *data;
2297 data= ((bTransLikeConstraint*)con->data);
2298 data->tar = newlibadr(fd, id->lib, data->tar);
2301 case CONSTRAINT_TYPE_NULL:
2307 static void direct_link_constraints(FileData *fd, ListBase *lb)
2312 for (cons=lb->first; cons; cons=cons->next) {
2313 cons->data = newdataadr(fd, cons->data);
2315 if (cons->type == CONSTRAINT_TYPE_PYTHON) {
2316 bPythonConstraint *data= cons->data;
2318 link_list(fd, &data->targets);
2320 data->prop = newdataadr(fd, data->prop);
2322 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2324 else if (cons->type == CONSTRAINT_TYPE_SPLINEIK) {
2325 bSplineIKConstraint *data= cons->data;
2327 data->points= newdataadr(fd, data->points);
2332 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2334 bPoseChannel *pchan;
2335 bArmature *arm= ob->data;
2341 /* always rebuild to match proxy or lib changes */
2342 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2344 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2345 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2347 /* hurms... loop in a loop, but yah... later... (ton) */
2348 pchan->bone= get_named_bone(arm, pchan->name);
2350 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
2351 if(pchan->bone==NULL)
2353 else if(ob->id.lib==NULL && arm->id.lib) {
2354 /* local pose selection copied to armature, bit hackish */
2355 pchan->bone->flag &= ~BONE_SELECTED;
2356 pchan->bone->flag |= pchan->selectflag;
2361 ob->recalc= OB_RECALC;
2362 pose->flag |= POSE_RECALC;
2366 static void lib_link_armature(FileData *fd, Main *main)
2370 arm= main->armature.first;
2373 if(arm->id.flag & LIB_NEEDLINK) {
2374 if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
2375 arm->id.flag -= LIB_NEEDLINK;
2381 static void direct_link_bones(FileData *fd, Bone* bone)
2385 bone->parent= newdataadr(fd, bone->parent);
2386 bone->prop= newdataadr(fd, bone->prop);
2388 IDP_DirectLinkProperty(bone->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2390 bone->flag &= ~BONE_DRAW_ACTIVE;
2392 link_list(fd, &bone->childbase);
2394 for(child=bone->childbase.first; child; child=child->next)
2395 direct_link_bones(fd, child);
2398 static void direct_link_armature(FileData *fd, bArmature *arm)
2402 link_list(fd, &arm->bonebase);
2406 arm->adt= newdataadr(fd, arm->adt);
2407 direct_link_animdata(fd, arm->adt);
2409 bone=arm->bonebase.first;
2411 direct_link_bones(fd, bone);
2415 arm->act_bone= newdataadr(fd, arm->act_bone);
2416 arm->act_edbone= NULL;
2419 /* ************ READ CAMERA ***************** */
2421 static void lib_link_camera(FileData *fd, Main *main)
2425 ca= main->camera.first;
2427 if(ca->id.flag & LIB_NEEDLINK) {
2428 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2430 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2432 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2434 ca->id.flag -= LIB_NEEDLINK;
2440 static void direct_link_camera(FileData *fd, Camera *ca)
2442 ca->adt= newdataadr(fd, ca->adt);
2443 direct_link_animdata(fd, ca->adt);
2447 /* ************ READ LAMP ***************** */
2449 static void lib_link_lamp(FileData *fd, Main *main)
2455 la= main->lamp.first;
2457 if(la->id.flag & LIB_NEEDLINK) {
2458 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2460 for(a=0; a<MAX_MTEX; a++) {
2463 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2464 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2468 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2470 la->id.flag -= LIB_NEEDLINK;
2476 static void direct_link_lamp(FileData *fd, Lamp *la)
2480 la->adt= newdataadr(fd, la->adt);
2481 direct_link_animdata(fd, la->adt);
2483 for(a=0; a<MAX_MTEX; a++) {
2484 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2487 la->curfalloff= newdataadr(fd, la->curfalloff);
2489 direct_link_curvemapping(fd, la->curfalloff);
2491 la->preview = direct_link_preview_image(fd, la->preview);
2494 /* ************ READ keys ***************** */
2496 static void lib_link_key(FileData *fd, Main *main)
2500 key= main->key.first;
2502 if(key->id.flag & LIB_NEEDLINK) {
2503 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2505 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2506 key->from= newlibadr(fd, key->id.lib, key->from);
2508 key->id.flag -= LIB_NEEDLINK;
2514 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2517 char *data, *poin, *cp;
2519 elemsize= key->elemsize;
2522 for(a=0; a<kb->totelem; a++) {
2527 while( cp[0] ) { /* cp[0]==amount */
2529 switch(cp[1]) { /* cp[1]= type */
2535 SWITCH_INT((*poin));
2548 static void direct_link_key(FileData *fd, Key *key)
2552 link_list(fd, &(key->block));
2554 key->adt= newdataadr(fd, key->adt);
2555 direct_link_animdata(fd, key->adt);
2557 key->refkey= newdataadr(fd, key->refkey);
2559 kb= key->block.first;
2562 kb->data= newdataadr(fd, kb->data);
2564 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2565 switch_endian_keyblock(key, kb);
2571 /* ************ READ mball ***************** */
2573 static void lib_link_mball(FileData *fd, Main *main)
2578 mb= main->mball.first;
2580 if(mb->id.flag & LIB_NEEDLINK) {
2581 if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
2583 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2585 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2587 mb->id.flag -= LIB_NEEDLINK;
2593 static void direct_link_mball(FileData *fd, MetaBall *mb)
2595 mb->adt= newdataadr(fd, mb->adt);
2596 direct_link_animdata(fd, mb->adt);
2598 mb->mat= newdataadr(fd, mb->mat);
2599 test_pointer_array(fd, (void **)&mb->mat);
2601 link_list(fd, &(mb->elems));
2603 mb->disp.first= mb->disp.last= NULL;
2604 mb->editelems= NULL;
2606 /* mb->edit_elems.first= mb->edit_elems.last= NULL;*/
2610 /* ************ READ WORLD ***************** */
2612 static void lib_link_world(FileData *fd, Main *main)
2618 wrld= main->world.first;
2620 if(wrld->id.flag & LIB_NEEDLINK) {
2621 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2623 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2625 for(a=0; a<MAX_MTEX; a++) {
2626 mtex= wrld->mtex[a];
2628 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2629 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2633 wrld->id.flag -= LIB_NEEDLINK;
2635 wrld= wrld->id.next;
2639 static void direct_link_world(FileData *fd, World *wrld)
2643 wrld->adt= newdataadr(fd, wrld->adt);
2644 direct_link_animdata(fd, wrld->adt);
2646 for(a=0; a<MAX_MTEX; a++) {
2647 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2649 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2653 /* ************ READ VFONT ***************** */
2655 static void lib_link_vfont(FileData *fd, Main *main)
2659 vf= main->vfont.first;
2661 if(vf->id.flag & LIB_NEEDLINK) {
2662 vf->id.flag -= LIB_NEEDLINK;
2668 static void direct_link_vfont(FileData *fd, VFont *vf)
2671 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2674 /* ************ READ TEXT ****************** */
2676 static void lib_link_text(FileData *fd, Main *main)
2680 text= main->text.first;
2682 if(text->id.flag & LIB_NEEDLINK) {
2683 text->id.flag -= LIB_NEEDLINK;
2685 text= text->id.next;
2689 static void direct_link_text(FileData *fd, Text *text)
2693 text->name= newdataadr(fd, text->name);
2696 text->undo_len= TXT_INIT_UNDO;
2697 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2699 text->compiled= NULL;
2702 if(text->flags & TXT_ISEXT) {
2707 link_list(fd, &text->lines);
2708 link_list(fd, &text->markers);
2710 text->curl= newdataadr(fd, text->curl);
2711 text->sell= newdataadr(fd, text->sell);
2713 ln= text->lines.first;
2715 ln->line= newdataadr(fd, ln->line);
2718 if (ln->len != (int) strlen(ln->line)) {
2719 printf("Error loading text, line lengths differ\n");
2720 ln->len = strlen(ln->line);
2726 text->flags = (text->flags) & ~TXT_ISEXT;
2731 /* ************ READ IMAGE ***************** */
2733 static void lib_link_image(FileData *fd, Main *main)
2737 ima= main->image.first;
2739 if(ima->id.flag & LIB_NEEDLINK) {
2740 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2742 ima->id.flag -= LIB_NEEDLINK;
2748 static void link_ibuf_list(FileData *fd, ListBase *lb)
2752 if(lb->first==NULL) return;
2754 lb->first= newimaadr(fd, lb->first);
2758 ln->next= newimaadr(fd, ln->next);
2766 static void direct_link_image(FileData *fd, Image *ima)
2768 /* for undo system, pointers could be restored */
2770 link_ibuf_list(fd, &ima->ibufs);
2772 ima->ibufs.first= ima->ibufs.last= NULL;
2774 /* if not restored, we keep the binded opengl index */
2775 if(ima->ibufs.first==NULL) {
2777 ima->gputexture= NULL;
2783 ima->render_text= newdataadr(fd, ima->render_text);
2785 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2786 ima->preview = direct_link_preview_image(fd, ima->preview);
2791 /* ************ READ CURVE ***************** */
2793 static void lib_link_curve(FileData *fd, Main *main)
2798 cu= main->curve.first;
2800 if(cu->id.flag & LIB_NEEDLINK) {
2801 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2803 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2805 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2806 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2807 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2808 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2809 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2810 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2811 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2813 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2814 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2816 cu->id.flag -= LIB_NEEDLINK;
2823 static void switch_endian_knots(Nurb *nu)
2830 SWITCH_INT(nu->knotsu[len]);
2836 SWITCH_INT(nu->knotsv[len]);
2841 static void direct_link_curve(FileData *fd, Curve *cu)
2846 cu->adt= newdataadr(fd, cu->adt);
2847 direct_link_animdata(fd, cu->adt);
2849 cu->mat= newdataadr(fd, cu->mat);
2850 test_pointer_array(fd, (void **)&cu->mat);
2851 cu->str= newdataadr(fd, cu->str);
2852 cu->strinfo= newdataadr(fd, cu->strinfo);
2853 cu->tb= newdataadr(fd, cu->tb);
2855 if(cu->vfont==0) link_list(fd, &(cu->nurb));
2857 cu->nurb.first=cu->nurb.last= 0;
2859 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2861 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2868 cu->tb[0].w = cu->linewidth;
2870 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2873 cu->bev.first=cu->bev.last= NULL;
2874 cu->disp.first=cu->disp.last= NULL;
2876 cu->lastselbp= NULL;
2882 nu->bezt= newdataadr(fd, nu->bezt);
2883 nu->bp= newdataadr(fd, nu->bp);
2884 nu->knotsu= newdataadr(fd, nu->knotsu);
2885 nu->knotsv= newdataadr(fd, nu->knotsv);
2886 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2888 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2889 switch_endian_knots(nu);
2897 /* ************ READ TEX ***************** */
2899 static void lib_link_texture(FileData *fd, Main *main)
2903 tex= main->tex.first;
2905 if(tex->id.flag & LIB_NEEDLINK) {
2906 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
2908 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2909 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2910 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2912 tex->pd->object= newlibadr(fd, tex->id.lib, tex->pd->object);
2913 if(tex->vd) tex->vd->object= newlibadr(fd, tex->id.lib, tex->vd->object);
2916 lib_link_ntree(fd, &tex->id, tex->nodetree);
2918 tex->id.flag -= LIB_NEEDLINK;
2924 static void direct_link_texture(FileData *fd, Tex *tex)
2926 tex->adt= newdataadr(fd, tex->adt);
2927 direct_link_animdata(fd, tex->adt);
2929 tex->plugin= newdataadr(fd, tex->plugin);
2931 tex->plugin->handle= 0;
2932 open_plugin_tex(tex->plugin);
2933 /* initialize data for this instance, if an initialization
2936 if (tex->plugin->instance_init)
2937 tex->plugin->instance_init((void *) tex->plugin->data);
2939 tex->coba= newdataadr(fd, tex->coba);
2940 tex->env= newdataadr(fd, tex->env);
2942 tex->env->ima= NULL;
2943 memset(tex->env->cube, 0, 6*sizeof(void *));
2946 tex->pd= newdataadr(fd, tex->pd);
2948 tex->pd->point_tree = NULL;
2949 tex->pd->coba= newdataadr(fd, tex->pd->coba);
2952 tex->vd= newdataadr(fd, tex->vd);
2954 tex->vd->dataset = NULL;
2957 tex->nodetree= newdataadr(fd, tex->nodetree);
2959 direct_link_nodetree(fd, tex->nodetree);
2961 tex->preview = direct_link_preview_image(fd, tex->preview);
2968 /* ************ READ MATERIAL ***************** */
2970 static void lib_link_material(FileData *fd, Main *main)
2976 ma= main->mat.first;
2978 if(ma->id.flag & LIB_NEEDLINK) {
2979 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
2981 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2982 of library blocks that implement this.*/
2983 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2985 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2986 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2988 for(a=0; a<MAX_MTEX; a++) {
2991 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2992 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2997 lib_link_ntree(fd, &ma->id, ma->nodetree);
2999 ma->id.flag -= LIB_NEEDLINK;
3005 static void direct_link_material(FileData *fd, Material *ma)
3009 ma->adt= newdataadr(fd, ma->adt);
3010 direct_link_animdata(fd, ma->adt);
3012 for(a=0; a<MAX_MTEX; a++) {
3013 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
3016 ma->ramp_col= newdataadr(fd, ma->ramp_col);
3017 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
3019 ma->nodetree= newdataadr(fd, ma->nodetree);
3021 direct_link_nodetree(fd, ma->nodetree);
3023 ma->preview = direct_link_preview_image(fd, ma->preview);
3024 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
3027 /* ************ READ PARTICLE SETTINGS ***************** */
3029 static void direct_link_pointcache(FileData *fd, PointCache *cache)
3031 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
3035 link_list(fd, &cache->mem_cache);
3037 pm = cache->mem_cache.first;
3039 for(; pm; pm=pm->next) {
3041 pm->index_array = newdataadr(fd, pm->index_array);
3043 for(i=0; i<BPHYS_TOT_DATA; i++) {
3044 if(pm->data[i] && pm->data_types & (1<<i))
3045 pm->data[i] = newdataadr(fd, pm->data[i]);
3050 cache->mem_cache.first = cache->mem_cache.last = NULL;
3052 cache->flag &= ~PTCACHE_SIMULATION_VALID;
3055 cache->free_edit= NULL;
3058 static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointCache **ocache)
3062 if(ptcaches->first) {
3063 link_list(fd, ptcaches);
3064 for(cache=ptcaches->first; cache; cache=cache->next)
3065 direct_link_pointcache(fd, cache);
3067 *ocache = newdataadr(fd, *ocache);
3070 /* old "single" caches need to be linked too */
3071 *ocache = newdataadr(fd, *ocache);
3072 direct_link_pointcache(fd, *ocache);
3074 ptcaches->first = ptcaches->last = *ocache;
3078 void lib_link_partdeflect(FileData *fd, ID *id, PartDeflect *pd)
3081 pd->tex=newlibadr_us(fd, id->lib, pd->tex);
3084 static void lib_link_particlesettings(FileData *fd, Main *main)
3086 ParticleSettings *part;
3087 ParticleDupliWeight *dw;
3089 part= main->particle.first;
3091 if(part->id.flag & LIB_NEEDLINK) {
3092 if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
3093 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
3095 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
3096 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
3097 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
3098 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
3100 lib_link_partdeflect(fd, &part->id, part->pd);
3101 lib_link_partdeflect(fd, &part->id, part->pd2);
3103 if(part->effector_weights)
3104 part->effector_weights->group = newlibadr(fd, part->id.lib, part->effector_weights->group);
3106 dw = part->dupliweights.first;
3107 for(; dw; dw=dw->next)
3108 dw->ob = newlibadr(fd, part->id.lib, dw->ob);
3111 BoidState *state = part->boids->states.first;
3113 for(; state; state=state->next) {
3114 rule = state->rules.first;
3115 for(; rule; rule=rule->next)
3116 switch(rule->type) {
3117 case eBoidRuleType_Goal:
3118 case eBoidRuleType_Avoid:
3120 BoidRuleGoalAvoid *brga = (BoidRuleGoalAvoid*)rule;
3121 brga->ob = newlibadr(fd, part->id.lib, brga->ob);
3124 case eBoidRuleType_FollowLeader:
3126 BoidRuleFollowLeader *brfl = (BoidRuleFollowLeader*)rule;
3127 brfl->ob = newlibadr(fd, part->id.lib, brfl->ob);
3133 part->id.flag -= LIB_NEEDLINK;
3135 part= part->id.next;
3139 static void direct_link_partdeflect(PartDeflect *pd)
3141 if(pd) pd->rng=NULL;
3144 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
3146 part->adt= newdataadr(fd, part->adt);
3147 part->pd= newdataadr(fd, part->pd);
3148 part->pd2= newdataadr(fd, part->pd2);
3150 direct_link_partdeflect(part->pd);
3151 direct_link_partdeflect(part->pd2);
3153 part->effector_weights = newdataadr(fd, part->effector_weights);
3154 if(!part->effector_weights)
3155 part->effector_weights = BKE_add_effector_weights(part->eff_group);
3157 link_list(fd, &part->dupliweights);
3159 part->boids= newdataadr(fd, part->boids);
3163 link_list(fd, &part->boids->states);
3165 for(state=part->boids->states.first; state; state=state->next) {
3166 link_list(fd, &state->rules);
3167 link_list(fd, &state->conditions);
3168 link_list(fd, &state->actions);
3173 static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase *particles)
3175 ParticleSystem *psys, *psysnext;
3177 for(psys=particles->first; psys; psys=psysnext){
3178 psysnext= psys->next;
3180 psys->part = newlibadr_us(fd, id->lib, psys->part);
3182 ParticleTarget *pt = psys->targets.first;
3184 for(; pt; pt=pt->next)
3185 pt->ob=newlibadr(fd, id->lib, pt->ob);
3187 psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
3190 /* particle modifier must be removed before particle system */
3191 ParticleSystemModifierData *psmd= psys_get_modifier(ob,psys);
3192 BLI_remlink(&ob->modifiers, psmd);
3193 modifier_free((ModifierData *)psmd);
3195 BLI_remlink(particles, psys);
3200 static void direct_link_particlesystems(FileData *fd, ListBase *particles)
3202 ParticleSystem *psys;
3206 for(psys=particles->first; psys; psys=psys->next) {
3207 psys->particles=newdataadr(fd,psys->particles);
3209 if(psys->particles && psys->particles->hair){
3210 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
3211 pa->hair=newdataadr(fd,pa->hair);
3214 if(psys->particles && psys->particles->keys){
3215 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++) {
3220 psys->flag &= ~PSYS_KEYED;
3223 if(psys->particles && psys->particles->boid) {
3224 pa = psys->particles;
3225 pa->boid = newdataadr(fd, pa->boid);
3226 for(a=1,pa++; a<psys->totpart; a++, pa++)
3227 pa->boid = (pa-1)->boid + 1;
3230 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
3235 psys->child = newdataadr(fd,psys->child);
3236 psys->effectors = NULL;
3238 link_list(fd, &psys->targets);
3241 psys->free_edit = NULL;
3242 psys->pathcache = NULL;
3243 psys->childcache = NULL;
3244 psys->pathcachebufs.first = psys->pathcachebufs.last = NULL;
3245 psys->childcachebufs.first = psys->childcachebufs.last = NULL;
3249 direct_link_pointcache_list(fd, &psys->ptcaches, &psys->pointcache);
3252 psys->clmd = newdataadr(fd, psys->clmd);
3253 psys->clmd->clothObject = NULL;
3255 psys->clmd->sim_parms= newdataadr(fd, psys->clmd->sim_parms);
3256 psys->clmd->coll_parms= newdataadr(fd, psys->clmd->coll_parms);
3258 if(psys->clmd->sim_parms) {
3259 if(psys->clmd->sim_parms->presets > 10)
3260 psys->clmd->sim_parms->presets = 0;