4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
34 #include "BLI_winstuff.h"
38 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
39 #include <stdlib.h> // for getenv atoi
40 #include <fcntl.h> // for open
41 #include <string.h> // for strrchr strncmp strstr
42 #include <math.h> // for fabs
45 #include <unistd.h> // for read close
46 #include <sys/param.h> // for MAXPATHLEN
48 #include <io.h> // for open close read
51 #include "DNA_anim_types.h"
52 #include "DNA_action_types.h"
53 #include "DNA_armature_types.h"
55 #include "DNA_actuator_types.h"
56 #include "DNA_brush_types.h"
57 #include "DNA_camera_types.h"
58 #include "DNA_cloth_types.h"
59 #include "DNA_color_types.h"
60 #include "DNA_controller_types.h"
61 #include "DNA_constraint_types.h"
62 #include "DNA_curve_types.h"
63 #include "DNA_customdata_types.h"
64 #include "DNA_effect_types.h"
65 #include "DNA_fileglobal_types.h"
66 #include "DNA_genfile.h"
67 #include "DNA_group_types.h"
68 #include "DNA_gpencil_types.h"
69 #include "DNA_ipo_types.h"
70 #include "DNA_image_types.h"
71 #include "DNA_key_types.h"
72 #include "DNA_lattice_types.h"
73 #include "DNA_lamp_types.h"
74 #include "DNA_meta_types.h"
75 #include "DNA_material_types.h"
76 #include "DNA_mesh_types.h"
77 #include "DNA_meshdata_types.h"
78 #include "DNA_modifier_types.h"
79 #include "DNA_nla_types.h"
80 #include "DNA_node_types.h"
81 #include "DNA_object_types.h"
82 #include "DNA_object_force.h"
83 #include "DNA_object_fluidsim.h" // NT
84 #include "DNA_outliner_types.h"
85 #include "DNA_object_force.h"
86 #include "DNA_packedFile_types.h"
87 #include "DNA_particle_types.h"
88 #include "DNA_property_types.h"
89 #include "DNA_text_types.h"
90 #include "DNA_view3d_types.h"
91 #include "DNA_screen_types.h"
92 #include "DNA_sensor_types.h"
93 #include "DNA_sdna_types.h"
94 #include "DNA_scene_types.h"
95 #include "DNA_sequence_types.h"
96 #include "DNA_sound_types.h"
97 #include "DNA_space_types.h"
98 #include "DNA_texture_types.h"
99 #include "DNA_userdef_types.h"
100 #include "DNA_vfont_types.h"
101 #include "DNA_world_types.h"
102 #include "DNA_windowmanager_types.h"
104 #include "MEM_guardedalloc.h"
105 #include "BLI_blenlib.h"
106 #include "BLI_arithb.h"
107 #include "BLI_storage_types.h" // for relname flags
109 #include "BKE_animsys.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_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_key.h" //void set_four_ipo
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_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_sculpt.h"
145 #include "BKE_sequence.h"
146 #include "BKE_texture.h" // for open_plugin_tex
147 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
148 #include "BKE_idprop.h"
150 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
151 //XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
152 //XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
153 #include "BLO_readfile.h"
154 #include "BLO_undofile.h"
155 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
157 #include "readfile.h"
159 #include "PIL_time.h"
164 Remark: still a weak point is the newadress() function, that doesnt solve reading from
165 multiple files at the same time
167 (added remark: oh, i thought that was solved? will look at that... (ton)
170 - Existing Library (Main) push or free
180 - read associated 'direct data'
181 - link direct data (internal and to LibBlock)
183 - read USER data, only when indicated (file is ~/.B.blend or .B25.blend)
185 - per Library (per Main)
188 - find LibBlocks and attach IDs to Main
189 - if external LibBlock
191 - or it's already read,
196 - read associated direct data
197 - link direct data (internal and to LibBlock)
199 - per Library with unread LibBlocks
204 - read associated direct data
205 - link direct data (internal and to LibBlock)
208 - link all LibBlocks and indirect pointers to libblocks
209 - initialize FileGlobal and copy pointers to Global
212 /* also occurs in library.c */
213 /* GS reads the memory pointed at in a specific ordering. There are,
214 * however two definitions for it. I have jotted them down here, both,
215 * but I think the first one is actually used. The thing is that
216 * big-endian systems might read this the wrong way round. OTOH, we
217 * constructed the IDs that are read out with this macro explicitly as
218 * well. I expect we'll sort it out soon... */
221 #define GS(a) (*((short *)(a)))
223 /* from misc_util: flip the bytes from x */
224 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
226 // only used here in readfile.c
227 #define SWITCH_LONGINT(a) { \
230 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
231 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
232 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
233 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
237 typedef struct OldNew {
242 typedef struct OldNewMap {
244 int nentries, entriessize;
250 /* local prototypes */
251 static void *read_struct(FileData *fd, BHead *bh, char *blockname);
254 static OldNewMap *oldnewmap_new(void)
256 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
258 onm->entriessize= 1024;
259 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
264 static int verg_oldnewmap(const void *v1, const void *v2)
266 const struct OldNew *x1=v1, *x2=v2;
268 if( x1->old > x2->old) return 1;
269 else if( x1->old < x2->old) return -1;
274 static void oldnewmap_sort(FileData *fd)
276 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
277 fd->libmap->sorted= 1;
280 /* nr is zero for data, and ID code for libdata */
281 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
285 if(oldaddr==NULL || newaddr==NULL) return;
287 if (onm->nentries==onm->entriessize) {
288 int osize= onm->entriessize;
289 OldNew *oentries= onm->entries;
291 onm->entriessize*= 2;
292 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
294 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
298 entry= &onm->entries[onm->nentries++];
300 entry->newp= newaddr;
304 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
308 if (onm->lasthit<onm->nentries-1) {
309 OldNew *entry= &onm->entries[++onm->lasthit];
311 if (entry->old==addr) {
317 for (i=0; i<onm->nentries; i++) {
318 OldNew *entry= &onm->entries[i];
320 if (entry->old==addr) {
331 /* for libdata, nr has ID code, no increment */
332 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
336 if(addr==NULL) return NULL;
338 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
340 OldNew entry_s, *entry;
344 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
348 if (id && (!lib || id->lib)) {
354 for (i=0; i<onm->nentries; i++) {
355 OldNew *entry= &onm->entries[i];
357 if (entry->old==addr) {
360 if (id && (!lib || id->lib)) {
369 static void oldnewmap_free_unused(OldNewMap *onm)
373 for (i=0; i<onm->nentries; i++) {
374 OldNew *entry= &onm->entries[i];
376 MEM_freeN(entry->newp);
382 static void oldnewmap_clear(OldNewMap *onm)
388 static void oldnewmap_free(OldNewMap *onm)
390 MEM_freeN(onm->entries);
396 static void read_libraries(FileData *basefd, ListBase *mainlist);
398 /* ************ help functions ***************** */
400 static void add_main_to_main(Main *mainvar, Main *from)
402 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
405 a= set_listbasepointers(mainvar, lbarray);
406 a= set_listbasepointers(from, fromarray);
408 addlisttolist(lbarray[a], fromarray[a]);
412 void blo_join_main(ListBase *mainlist)
414 Main *tojoin, *mainl;
417 mainl= mainlist->first;
418 while ((tojoin= mainl->next)) {
419 add_main_to_main(mainl, tojoin);
420 BLI_remlink(mainlist, tojoin);
425 static void split_libdata(ListBase *lb, Main *first)
437 if(mainvar->curlib==id->lib) {
438 lbn= wich_libbase(mainvar, GS(id->name));
440 BLI_addtail(lbn, id);
443 mainvar= mainvar->next;
445 if(mainvar==0) printf("error split_libdata\n");
451 void blo_split_main(ListBase *mainlist, Main *main)
453 ListBase *lbarray[MAX_LIBARRAY];
457 mainlist->first= mainlist->last= main;
460 if(main->library.first==NULL)
463 for (lib= main->library.first; lib; lib= lib->id.next) {
464 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
465 libmain->curlib= lib;
466 BLI_addtail(mainlist, libmain);
469 i= set_listbasepointers(main, lbarray);
471 split_libdata(lbarray[i], main->next);
474 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
475 static void cleanup_path(const char *relabase, char *name)
477 char filename[FILE_MAXFILE];
479 BLI_splitdirstring(name, filename);
480 BLI_cleanup_dir(relabase, name);
481 strcat(name, filename);
484 static void read_file_version(FileData *fd, Main *main)
488 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
489 if (bhead->code==GLOB) {
490 FileGlobal *fg= read_struct(fd, bhead, "Global");
492 main->subversionfile= fg->subversion;
493 main->minversionfile= fg->minversion;
494 main->minsubversionfile= fg->minsubversion;
497 else if (bhead->code==ENDB)
504 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *name, const char *relabase)
508 char name1[FILE_MAXDIR+FILE_MAXFILE];
510 strncpy(name1, name, sizeof(name1)-1);
511 cleanup_path(relabase, name1);
512 // printf("blo_find_main: original in %s\n", name);
513 // printf("blo_find_main: converted to %s\n", name1);
515 for (m= mainlist->first; m; m= m->next) {
516 char *libname= (m->curlib)?m->curlib->filename:m->name;
518 if (BLI_streq(name1, libname)) {
519 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
524 m= MEM_callocN(sizeof(Main), "find_main");
525 BLI_addtail(mainlist, m);
527 lib= alloc_libblock(&m->library, ID_LI, "lib");
528 strncpy(lib->name, name, sizeof(lib->name)-1);
529 BLI_strncpy(lib->filename, name1, sizeof(lib->filename));
533 read_file_version(fd, m);
535 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", name);
540 /* ************ FILE PARSING ****************** */
542 static void switch_endian_bh4(BHead4 *bhead)
544 /* the ID_.. codes */
545 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
547 if (bhead->code != ENDB) {
548 SWITCH_INT(bhead->len);
549 SWITCH_INT(bhead->SDNAnr);
550 SWITCH_INT(bhead->nr);
554 static void switch_endian_bh8(BHead8 *bhead)
556 /* the ID_.. codes */
557 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
559 if (bhead->code != ENDB) {
560 SWITCH_INT(bhead->len);
561 SWITCH_INT(bhead->SDNAnr);
562 SWITCH_INT(bhead->nr);
566 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
568 BHead4 *bhead4 = (BHead4 *) bhead;
569 #if defined(WIN32) && !defined(FREE_WINDOWS)
575 bhead4->code= bhead8->code;
576 bhead4->len= bhead8->len;
578 if (bhead4->code != ENDB) {
580 // why is this here ??
581 if (do_endian_swap) {
582 SWITCH_LONGINT(bhead8->old);
585 /* this patch is to avoid a long long being read from not-eight aligned positions
586 is necessary on any modern 64bit architecture) */
587 memcpy(&old, &bhead8->old, 8);
588 bhead4->old = (int) (old >> 3);
590 bhead4->SDNAnr= bhead8->SDNAnr;
591 bhead4->nr= bhead8->nr;
595 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
597 BHead8 *bhead8 = (BHead8 *) bhead;
599 bhead8->code= bhead4->code;
600 bhead8->len= bhead4->len;
602 if (bhead8->code != ENDB) {
603 bhead8->old= bhead4->old;
604 bhead8->SDNAnr= bhead4->SDNAnr;
605 bhead8->nr= bhead4->nr;
609 static BHeadN *get_bhead(FileData *fd)
614 BHeadN *new_bhead = 0;
620 /* not strictly needed but shuts valgrind up
621 * since uninitialized memory gets compared */
622 memset(&bhead8, 0, sizeof(BHead8));
623 memset(&bhead4, 0, sizeof(BHead4));
624 memset(&bhead, 0, sizeof(BHead));
626 // First read the bhead structure.
627 // Depending on the platform the file was written on this can
628 // be a big or little endian BHead4 or BHead8 structure.
630 // As usual 'ENDB' (the last *partial* bhead of the file)
631 // needs some special handling. We don't want to EOF just yet.
633 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
635 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
637 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
638 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
639 switch_endian_bh4(&bhead4);
642 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
643 bh8_from_bh4(&bhead, &bhead4);
645 memcpy(&bhead, &bhead4, sizeof(bhead));
653 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
655 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
656 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
657 switch_endian_bh8(&bhead8);
660 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
661 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
663 memcpy(&bhead, &bhead8, sizeof(bhead));
671 /* make sure people are not trying to pass bad blend files */
672 if (bhead.len < 0) fd->eof = 1;
674 // bhead now contains the (converted) bhead structure. Now read
675 // the associated data and put everything in a BHeadN (creative naming !)
678 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
680 new_bhead->next = new_bhead->prev = 0;
681 new_bhead->bhead = bhead;
683 readsize = fd->read(fd, new_bhead + 1, bhead.len);
685 if (readsize != bhead.len) {
687 MEM_freeN(new_bhead);
697 // We've read a new block. Now add it to the list
701 BLI_addtail(&fd->listbase, new_bhead);
707 BHead *blo_firstbhead(FileData *fd)
713 // Read in a new block if necessary
715 new_bhead = fd->listbase.first;
716 if (new_bhead == 0) {
717 new_bhead = get_bhead(fd);
721 bhead = &new_bhead->bhead;
727 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
729 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
730 BHeadN *prev= bheadn->prev;
732 return prev?&prev->bhead:NULL;
735 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
737 BHeadN *new_bhead = NULL;
741 // bhead is actually a sub part of BHeadN
742 // We calculate the BHeadN pointer from the BHead pointer below
743 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
745 // get the next BHeadN. If it doesn't exist we read in the next one
746 new_bhead = new_bhead->next;
747 if (new_bhead == 0) {
748 new_bhead = get_bhead(fd);
753 // here we do the reverse:
754 // go from the BHeadN pointer to the BHead pointer
755 bhead = &new_bhead->bhead;
761 static void decode_blender_header(FileData *fd)
763 char header[SIZEOFBLENDERHEADER], num[4];
766 // read in the header data
767 readsize = fd->read(fd, header, sizeof(header));
769 if (readsize == sizeof(header)) {
770 if(strncmp(header, "BLENDER", 7) == 0) {
771 int remove_this_endian_test= 1;
773 fd->flags |= FD_FLAGS_FILE_OK;
775 // what size are pointers in the file ?
777 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
778 if (sizeof(void *) != 4) {
779 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
782 if (sizeof(void *) != 8) {
783 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
787 // is the file saved in a different endian
789 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
790 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
793 // get the version number
795 memcpy(num, header+9, 3);
797 fd->fileversion = atoi(num);
802 static int read_file_dna(FileData *fd)
806 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
807 if (bhead->code==DNA1) {
808 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
810 fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
813 fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
814 /* used to retrieve ID names from (bhead+1) */
815 fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
819 } else if (bhead->code==ENDB)
826 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
828 int readsize = read(filedata->filedes, buffer, size);
833 filedata->seek += readsize;
839 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
841 int readsize = gzread(filedata->gzfiledes, buffer, size);
846 filedata->seek += readsize;
852 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
854 // don't read more bytes then there are available in the buffer
855 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
857 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
858 filedata->seek += readsize;
863 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
865 static unsigned int seek= 1<<30; /* the current position */
866 static unsigned int offset= 0; /* size of previous chunks */
867 static MemFileChunk *chunk=NULL;
868 unsigned int chunkoffset, readsize, totread;
870 if(size==0) return 0;
872 if(seek != (unsigned int)filedata->seek) {
873 chunk= filedata->memfile->chunks.first;
877 if(seek + chunk->size > (unsigned) filedata->seek) break;
882 seek= filedata->seek;
889 /* first check if it's on the end if current chunk */
890 if(seek-offset == chunk->size) {
891 offset+= chunk->size;
895 /* debug, should never happen */
897 printf("illegal read, chunk zero\n");
901 chunkoffset= seek-offset;
902 readsize= size-totread;
904 /* data can be spread over multiple chunks, so clamp size
905 * to within this chunk, and then it will read further in
907 if(chunkoffset+readsize > chunk->size)
908 readsize= chunk->size-chunkoffset;
910 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
912 filedata->seek += readsize;
914 } while(totread < size);
922 static FileData *filedata_new(void)
924 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
927 fd->gzfiledes = NULL;
929 /* XXX, this doesn't need to be done all the time,
930 * but it keeps us reentrant, remove once we have
931 * a lib that provides a nice lock. - zr
933 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
935 fd->datamap = oldnewmap_new();
936 fd->globmap = oldnewmap_new();
937 fd->libmap = oldnewmap_new();
942 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
944 decode_blender_header(fd);
946 if (fd->flags & FD_FLAGS_FILE_OK) {
947 if (!read_file_dna(fd)) {
948 BKE_report(reports, RPT_ERROR, "File incomplete");
949 blo_freefiledata(fd);
954 BKE_report(reports, RPT_ERROR, "File is not a Blender file");
955 blo_freefiledata(fd);
962 /* cannot be called with relative paths anymore! */
963 /* on each new library added, it now checks for the current FileData and expands relativeness */
964 FileData *blo_openblenderfile(char *name, ReportList *reports)
968 gzfile= gzopen(name, "rb");
970 if (NULL == gzfile) {
971 BKE_report(reports, RPT_ERROR, "Unable to open");
974 FileData *fd = filedata_new();
975 fd->gzfiledes = gzfile;
976 fd->read = fd_read_gzip_from_file;
978 /* needed for library_append and read_libraries */
979 BLI_strncpy(fd->filename, name, sizeof(fd->filename));
981 return blo_decode_and_check(fd, reports);
985 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
987 if (!mem || memsize<SIZEOFBLENDERHEADER) {
988 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
991 FileData *fd= filedata_new();
993 fd->buffersize= memsize;
994 fd->read= fd_read_from_memory;
995 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
997 return blo_decode_and_check(fd, reports);
1001 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
1004 BKE_report(reports, RPT_ERROR, "Unable to open");
1007 FileData *fd= filedata_new();
1008 fd->memfile= memfile;
1010 fd->read= fd_read_from_memfile;
1011 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
1013 return blo_decode_and_check(fd, reports);
1018 void blo_freefiledata(FileData *fd)
1022 if (fd->filedes != -1) {
1026 if (fd->gzfiledes != NULL)
1028 gzclose(fd->gzfiledes);
1031 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1032 MEM_freeN(fd->buffer);
1036 // Free all BHeadN data blocks
1037 BLI_freelistN(&fd->listbase);
1040 DNA_sdna_free(fd->memsdna);
1042 DNA_sdna_free(fd->filesdna);
1044 MEM_freeN(fd->compflags);
1047 oldnewmap_free(fd->datamap);
1049 oldnewmap_free(fd->globmap);
1051 oldnewmap_free(fd->imamap);
1052 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1053 oldnewmap_free(fd->libmap);
1055 MEM_freeN(fd->bheadmap);
1061 /* ************ DIV ****************** */
1063 int BLO_has_bfile_extension(char *str)
1065 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1068 /* ************** OLD POINTERS ******************* */
1070 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1072 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1075 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1077 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1080 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1082 if(fd->imamap && adr)
1083 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1088 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1090 return oldnewmap_liblookup(fd->libmap, adr, lib);
1093 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1095 ID *id= newlibadr(fd, lib, adr);
1103 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1107 for (i=0; i<fd->libmap->nentries; i++) {
1108 OldNew *entry= &fd->libmap->entries[i];
1110 if (old==entry->newp && entry->nr==ID_ID) {
1112 if(new) entry->nr= GS( ((ID *)new)->name );
1118 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1122 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1125 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1129 change_idid_adr_fd(fd, old, new);
1134 /* lib linked proxy objects point to our local data, we need
1135 * to clear that pointer before reading the undo memfile since
1136 * the object might be removed, it is set again in reading
1137 * if the local object still exists */
1138 void blo_clear_proxy_pointers_from_lib(FileData *fd, Main *oldmain)
1140 Object *ob= oldmain->object.first;
1142 for(;ob; ob= ob->id.next)
1144 ob->proxy_from= NULL;
1147 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1149 Image *ima= oldmain->image.first;
1150 Scene *sce= oldmain->scene.first;
1152 fd->imamap= oldnewmap_new();
1154 for(;ima; ima= ima->id.next) {
1155 Link *ibuf= ima->ibufs.first;
1156 for(; ibuf; ibuf= ibuf->next)
1157 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1159 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1161 for(; sce; sce= sce->id.next) {
1164 for(node= sce->nodetree->nodes.first; node; node= node->next)
1165 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1170 /* set old main image ibufs to zero if it has been restored */
1171 /* this works because freeing old main only happens after this call */
1172 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1174 OldNew *entry= fd->imamap->entries;
1175 Image *ima= oldmain->image.first;
1176 Scene *sce= oldmain->scene.first;
1179 /* used entries were restored, so we put them to zero */
1180 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1185 for(;ima; ima= ima->id.next) {
1188 /* this mirrors direct_link_image */
1189 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1191 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1192 BLI_remlink(&ima->ibufs, ibuf);
1194 ima->gputexture= NULL;
1198 ima->gputexture= newimaadr(fd, ima->gputexture);
1200 for(; sce; sce= sce->id.next) {
1203 for(node= sce->nodetree->nodes.first; node; node= node->next)
1204 node->preview= newimaadr(fd, node->preview);
1209 /* undo file support: add all library pointers in lookup */
1210 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1212 Main *ptr= mainlist->first;
1213 ListBase *lbarray[MAX_LIBARRAY];
1215 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1216 int i= set_listbasepointers(ptr, lbarray);
1219 for(id= lbarray[i]->first; id; id= id->next)
1220 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1226 /* ********** END OLD POINTERS ****************** */
1227 /* ********** READ FILE ****************** */
1229 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1231 int blocksize, nblocks;
1234 data= (char *)(bhead+1);
1235 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1239 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1245 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1250 /* switch is based on file dna */
1251 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1252 switch_endian_structs(fd->filesdna, bh);
1254 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1255 if(fd->compflags[bh->SDNAnr]==2) {
1256 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1258 temp= MEM_mallocN(bh->len, blockname);
1259 memcpy(temp, (bh+1), bh->len);
1267 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1271 if(lb->first==NULL) return;
1273 lb->first= newdataadr(fd, lb->first);
1277 ln->next= newdataadr(fd, ln->next);
1285 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1290 if(lb->first==0) return;
1291 poin= newdataadr(fd, lb->first);
1293 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1300 poin= newdataadr(fd, ln->next);
1302 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1312 static void test_pointer_array(FileData *fd, void **mat)
1314 #if defined(WIN32) && !defined(FREE_WINDOWS)
1315 __int64 *lpoin, *lmat;
1317 long long *lpoin, *lmat;
1319 int len, *ipoin, *imat;
1321 /* manually convert the pointer array in
1322 * the old dna format to a pointer array in
1323 * the new dna format.
1326 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1328 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1329 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1333 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1334 SWITCH_LONGINT(*lpoin);
1335 *ipoin= (int) ((*lpoin) >> 3);
1343 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1344 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1358 /* ************ READ ID Properties *************** */
1360 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1361 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1363 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1368 /*since we didn't save the extra buffer, set totallen to len.*/
1369 prop->totallen = prop->len;
1370 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1372 if (switch_endian) {
1373 test_pointer_array(fd, prop->data.pointer);
1374 array= (IDProperty**) prop->data.pointer;
1376 for(i=0; i<prop->len; i++)
1377 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1381 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1386 /*since we didn't save the extra buffer, set totallen to len.*/
1387 prop->totallen = prop->len;
1388 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1390 if (switch_endian) {
1391 if(prop->subtype == IDP_GROUP) {
1392 test_pointer_array(fd, prop->data.pointer);
1393 array= prop->data.pointer;
1395 for(i=0; i<prop->len; i++)
1396 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1398 else if(prop->subtype == IDP_DOUBLE) {
1399 for (i=0; i<prop->len; i++) {
1400 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1403 for (i=0; i<prop->len; i++) {
1404 SWITCH_INT(((int*)prop->data.pointer)[i]);
1410 static void IDP_DirectLinkString(IDProperty *prop, int switch_endian, FileData *fd)
1412 /*since we didn't save the extra string buffer, set totallen to len.*/
1413 prop->totallen = prop->len;
1414 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1417 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1419 ListBase *lb = &prop->data.group;
1424 /*Link child id properties now*/
1425 for (loop=prop->data.group.first; loop; loop=loop->next) {
1426 IDP_DirectLinkProperty(loop, switch_endian, fd);
1430 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1432 switch (prop->type) {
1434 IDP_DirectLinkGroup(prop, switch_endian, fd);
1437 IDP_DirectLinkString(prop, switch_endian, fd);
1440 IDP_DirectLinkArray(prop, switch_endian, fd);
1443 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1446 /*erg, stupid doubles. since I'm storing them
1447 in the same field as int val; val2 in the
1448 IDPropertyData struct, they have to deal with
1449 endianness specifically
1451 in theory, val and val2 would've already been swapped
1452 if switch_endian is true, so we have to first unswap
1453 them then reswap them as a single 64-bit entity.
1456 if (switch_endian) {
1457 SWITCH_INT(prop->data.val);
1458 SWITCH_INT(prop->data.val2);
1459 SWITCH_LONGINT(prop->data.val);
1467 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1471 /* ************ READ CurveMapping *************** */
1473 /* cuma itself has been read! */
1474 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1478 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1479 cumap->flag &= ~CUMA_PREMULLED;
1481 for(a=0; a<CM_TOT; a++) {
1482 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1483 cumap->cm[a].table= NULL;
1487 /* ************ READ Brush *************** */
1488 /* library brush linking after fileread */
1489 static void lib_link_brush(FileData *fd, Main *main)
1495 /* only link ID pointers */
1496 for(brush= main->brush.first; brush; brush= brush->id.next) {
1497 if(brush->id.flag & LIB_NEEDLINK) {
1498 brush->id.flag -= LIB_NEEDLINK;
1500 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1502 for(a=0; a<MAX_MTEX; a++) {
1503 mtex= brush->mtex[a];
1505 mtex->tex= newlibadr_us(fd, brush->id.lib, mtex->tex);
1508 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1513 static void direct_link_brush(FileData *fd, Brush *brush)
1515 /* brush itself has been read */
1518 for(a=0; a<MAX_MTEX; a++)
1519 brush->mtex[a]= newdataadr(fd, brush->mtex[a]);
1522 brush->curve= newdataadr(fd, brush->curve);
1524 direct_link_curvemapping(fd, brush->curve);
1526 brush_curve_preset(brush, BRUSH_PRESET_SHARP);
1529 static void direct_link_script(FileData *fd, Script *script)
1532 SCRIPT_SET_NULL(script)
1536 /* ************ READ PACKEDFILE *************** */
1538 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1540 PackedFile *pf= newdataadr(fd, oldpf);
1543 pf->data= newdataadr(fd, pf->data);
1549 /* ************ READ IMAGE PREVIEW *************** */
1551 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1553 PreviewImage *prv= newdataadr(fd, old_prv);
1557 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1559 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1567 /* ************ READ SCRIPTLINK *************** */
1569 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1573 for(i=0; i<slink->totscript; i++) {
1574 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1578 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1580 slink->scripts= newdataadr(fd, slink->scripts);
1581 test_pointer_array(fd, (void **)&slink->scripts);
1583 slink->flag= newdataadr(fd, slink->flag);
1585 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1588 for(a=0; a<slink->totscript; a++) {
1589 SWITCH_SHORT(slink->flag[a]);
1594 /* ************ READ ANIMATION STUFF ***************** */
1596 /* Legacy Data Support (for Version Patching) ----------------------------- */
1598 // XXX depreceated - old animation system
1599 static void lib_link_ipo(FileData *fd, Main *main)
1603 ipo= main->ipo.first;
1605 if(ipo->id.flag & LIB_NEEDLINK) {
1607 for(icu= ipo->curve.first; icu; icu= icu->next) {
1609 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1611 ipo->id.flag -= LIB_NEEDLINK;
1617 // XXX depreceated - old animation system
1618 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1622 link_list(fd, &(ipo->curve));
1623 icu= ipo->curve.first;
1625 icu->bezt= newdataadr(fd, icu->bezt);
1626 icu->bp= newdataadr(fd, icu->bp);
1627 icu->driver= newdataadr(fd, icu->driver);
1632 // XXX depreceated - old animation system
1633 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1635 bActionStrip *strip;
1636 bActionModifier *amod;
1638 for (strip=striplist->first; strip; strip=strip->next){
1639 strip->object = newlibadr(fd, id->lib, strip->object);
1640 strip->act = newlibadr_us(fd, id->lib, strip->act);
1641 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1642 for(amod= strip->modifiers.first; amod; amod= amod->next)
1643 amod->ob= newlibadr(fd, id->lib, amod->ob);
1647 // XXX depreceated - old animation system
1648 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1650 bActionStrip *strip;
1652 link_list(fd, strips);
1654 for(strip= strips->first; strip; strip= strip->next)
1655 link_list(fd, &strip->modifiers);
1658 // XXX depreceated - old animation system
1659 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1661 bConstraintChannel *chan;
1663 for (chan=chanbase->first; chan; chan=chan->next){
1664 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1668 /* Data Linking ----------------------------- */
1670 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1674 for (fcm= list->first; fcm; fcm= fcm->next) {
1675 /* data for specific modifiers */
1676 switch (fcm->type) {
1677 case FMODIFIER_TYPE_PYTHON:
1679 FMod_Python *data= (FMod_Python *)fcm->data;
1680 data->script = newlibadr(fd, id->lib, data->script);
1687 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1691 /* relink ID-block references... */
1692 for (fcu= list->first; fcu; fcu= fcu->next) {
1695 ChannelDriver *driver= fcu->driver;
1698 for (dtar= driver->targets.first; dtar; dtar= dtar->next)
1699 dtar->id= newlibadr(fd, id->lib, dtar->id);
1703 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1708 /* NOTE: this assumes that link_list has already been called on the list */
1709 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1713 for (fcm= list->first; fcm; fcm= fcm->next) {
1714 /* relink general data */
1715 fcm->data = newdataadr(fd, fcm->data);
1718 /* do relinking of data for specific types */
1719 switch (fcm->type) {
1720 case FMODIFIER_TYPE_GENERATOR:
1722 FMod_Generator *data= (FMod_Generator *)fcm->data;
1724 data->coefficients= newdataadr(fd, data->coefficients);
1727 case FMODIFIER_TYPE_ENVELOPE:
1729 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1731 data->data= newdataadr(fd, data->data);
1734 case FMODIFIER_TYPE_PYTHON:
1736 FMod_Python *data= (FMod_Python *)fcm->data;
1738 data->prop = newdataadr(fd, data->prop);
1739 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1746 /* NOTE: this assumes that link_list has already been called on the list */
1747 static void direct_link_fcurves(FileData *fd, ListBase *list)
1751 /* link F-Curve data to F-Curve again (non ID-libs) */
1752 for (fcu= list->first; fcu; fcu= fcu->next) {
1754 fcu->bezt= newdataadr(fd, fcu->bezt);
1755 fcu->fpt= newdataadr(fd, fcu->fpt);
1758 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1761 fcu->grp= newdataadr(fd, fcu->grp);
1764 fcu->driver= newdataadr(fd, fcu->driver);
1766 ChannelDriver *driver= fcu->driver;
1769 /* relink targets and their paths */
1770 link_list(fd, &driver->targets);
1771 for (dtar= driver->targets.first; dtar; dtar= dtar->next)
1772 dtar->rna_path= newdataadr(fd, dtar->rna_path);
1776 link_list(fd, &fcu->modifiers);
1777 direct_link_fmodifiers(fd, &fcu->modifiers);
1782 static void lib_link_action(FileData *fd, Main *main)
1785 bActionChannel *chan;
1787 for (act= main->action.first; act; act= act->id.next) {
1788 if (act->id.flag & LIB_NEEDLINK) {
1789 act->id.flag -= LIB_NEEDLINK;
1791 // XXX depreceated - old animation system <<<
1792 for (chan=act->chanbase.first; chan; chan=chan->next) {
1793 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1794 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1796 // >>> XXX depreceated - old animation system
1798 lib_link_fcurves(fd, &act->id, &act->curves);
1803 static void direct_link_action(FileData *fd, bAction *act)
1805 bActionChannel *achan; // XXX depreceated - old animation system
1808 link_list(fd, &act->curves);
1809 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1810 link_list(fd, &act->groups);
1811 link_list(fd, &act->markers);
1813 // XXX depreceated - old animation system <<<
1814 for (achan = act->chanbase.first; achan; achan=achan->next) {
1815 achan->grp= newdataadr(fd, achan->grp);
1817 link_list(fd, &achan->constraintChannels);
1819 // >>> XXX depreceated - old animation system
1821 direct_link_fcurves(fd, &act->curves);
1823 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1824 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1825 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1829 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
1833 for (strip= list->first; strip; strip= strip->next) {
1834 /* check strip's children */
1835 lib_link_nladata_strips(fd, id, &strip->strips);
1837 /* reassign the counted-reference to action */
1838 strip->act = newlibadr_us(fd, id->lib, strip->act);
1842 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
1846 /* we only care about the NLA strips inside the tracks */
1847 for (nlt= list->first; nlt; nlt= nlt->next) {
1848 lib_link_nladata_strips(fd, id, &nlt->strips);
1852 /* This handles Animato NLA-Strips linking
1853 * NOTE: this assumes that link_list has already been called on the list
1855 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
1859 for (strip= list->first; strip; strip= strip->next) {
1860 /* strip's child strips */
1861 link_list(fd, &strip->strips);
1862 direct_link_nladata_strips(fd, &strip->strips);
1864 /* strip's F-Curves */
1865 link_list(fd, &strip->fcurves);
1866 direct_link_fcurves(fd, &strip->fcurves);
1868 /* strip's F-Modifiers */
1869 link_list(fd, &strip->modifiers);
1870 direct_link_fcurves(fd, &strip->modifiers);
1874 /* NOTE: this assumes that link_list has already been called on the list */
1875 static void direct_link_nladata(FileData *fd, ListBase *list)
1879 for (nlt= list->first; nlt; nlt= nlt->next) {
1880 /* relink list of strips */
1881 link_list(fd, &nlt->strips);
1883 /* relink strip data */
1884 direct_link_nladata_strips(fd, &nlt->strips);
1890 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
1895 /* here, we're only interested in the ID pointer stored in some of the paths */
1896 for (ks= list->first; ks; ks= ks->next) {
1897 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1898 ksp->id= newlibadr(fd, id->lib, ksp->id);
1903 /* NOTE: this assumes that link_list has already been called on the list */
1904 static void direct_link_keyingsets(FileData *fd, ListBase *list)
1909 /* link KeyingSet data to KeyingSet again (non ID-libs) */
1910 for (ks= list->first; ks; ks= ks->next) {
1912 link_list(fd, &ks->paths);
1914 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1916 ksp->rna_path= newdataadr(fd, ksp->rna_path);
1923 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
1928 /* link action data */
1929 adt->action= newlibadr_us(fd, id->lib, adt->action);
1930 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
1933 lib_link_fcurves(fd, id, &adt->drivers);
1935 /* overrides don't have lib-link for now, so no need to do anything */
1938 lib_link_nladata(fd, id, &adt->nla_tracks);
1941 static void direct_link_animdata(FileData *fd, AnimData *adt)
1943 /* NOTE: must have called newdataadr already before doing this... */
1948 link_list(fd, &adt->drivers);
1949 direct_link_fcurves(fd, &adt->drivers);
1951 /* link overrides */
1955 link_list(fd, &adt->nla_tracks);
1956 direct_link_nladata(fd, &adt->nla_tracks);
1958 /* clear temp pointers that may have been set... */
1959 // TODO: it's probably only a small cost to reload this anyway...
1960 adt->actstrip= NULL;
1963 /* ************ READ NODE TREE *************** */
1965 /* singe node tree (also used for material/scene trees), ntree is not NULL */
1966 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
1970 if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
1972 for(node= ntree->nodes.first; node; node= node->next)
1973 node->id= newlibadr_us(fd, id->lib, node->id);
1976 /* library ntree linking after fileread */
1977 static void lib_link_nodetree(FileData *fd, Main *main)
1981 /* only link ID pointers */
1982 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1983 if(ntree->id.flag & LIB_NEEDLINK) {
1984 ntree->id.flag -= LIB_NEEDLINK;
1985 lib_link_ntree(fd, &ntree->id, ntree);
1990 /* verify types for nodes and groups, all data has to be read */
1991 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
1993 static void lib_verify_nodetree(Main *main, int open)
2000 /* this crashes blender on undo/redo
2002 reinit_nodesystem();
2005 /* now create the own typeinfo structs an verify nodes */
2006 /* here we still assume no groups in groups */
2007 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2008 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
2009 ntreeMakeOwnType(ntree); /* for group usage */
2012 /* now verify all types in material trees, groups are set OK now */
2013 for(ma= main->mat.first; ma; ma= ma->id.next) {
2015 ntreeVerifyTypes(ma->nodetree);
2017 /* and scene trees */
2018 for(sce= main->scene.first; sce; sce= sce->id.next) {
2020 ntreeVerifyTypes(sce->nodetree);
2022 /* and texture trees */
2023 for(tx= main->tex.first; tx; tx= tx->id.next) {
2025 ntreeVerifyTypes(tx->nodetree);
2031 /* ntree itself has been read! */
2032 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2034 /* note: writing and reading goes in sync, for speed */
2039 ntree->init= 0; /* to set callbacks and force setting types */
2040 ntree->owntype= NULL;
2041 ntree->timecursor= NULL;
2043 ntree->adt= newdataadr(fd, ntree->adt);
2044 direct_link_animdata(fd, ntree->adt);
2046 link_list(fd, &ntree->nodes);
2047 for(node= ntree->nodes.first; node; node= node->next) {
2048 if(node->type == NODE_DYNAMIC) {
2050 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
2051 node->typeinfo= NULL;
2054 node->storage= newdataadr(fd, node->storage);
2057 /* could be handlerized at some point */
2058 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
2059 direct_link_curvemapping(fd, node->storage);
2060 else if(ntree->type==NTREE_COMPOSIT) {
2061 if( ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB))
2062 direct_link_curvemapping(fd, node->storage);
2063 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2064 ((ImageUser *)node->storage)->ok= 1;
2066 else if( ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) ) {
2067 direct_link_curvemapping(fd, node->storage);
2070 link_list(fd, &node->inputs);
2071 link_list(fd, &node->outputs);
2073 link_list(fd, &ntree->links);
2075 /* and we connect the rest */
2076 for(node= ntree->nodes.first; node; node= node->next) {
2077 node->preview= newimaadr(fd, node->preview);
2079 for(sock= node->inputs.first; sock; sock= sock->next)
2080 sock->link= newdataadr(fd, sock->link);
2081 for(sock= node->outputs.first; sock; sock= sock->next)
2082 sock->ns.data= NULL;
2084 for(link= ntree->links.first; link; link= link->next) {
2085 link->fromnode= newdataadr(fd, link->fromnode);
2086 link->tonode= newdataadr(fd, link->tonode);
2087 link->fromsock= newdataadr(fd, link->fromsock);
2088 link->tosock= newdataadr(fd, link->tosock);
2091 /* set selin and selout */
2092 for(node= ntree->nodes.first; node; node= node->next) {
2093 for(sock= node->inputs.first; sock; sock= sock->next) {
2094 if(sock->flag & SOCK_SEL) {
2099 for(sock= node->outputs.first; sock; sock= sock->next) {
2100 if(sock->flag & SOCK_SEL) {
2101 ntree->selout= sock;
2107 /* type verification is in lib-link */
2110 /* ************ READ ARMATURE ***************** */
2112 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2116 for (con = conlist->first; con; con=con->next) {
2117 /* patch for error introduced by changing constraints (dunno how) */
2118 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2119 if(con->data==NULL) {
2120 con->type= CONSTRAINT_TYPE_NULL;
2122 /* own ipo, all constraints have it */
2123 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2125 switch (con->type) {
2126 case CONSTRAINT_TYPE_PYTHON:
2128 bPythonConstraint *data= (bPythonConstraint*)con->data;
2129 bConstraintTarget *ct;
2131 for (ct= data->targets.first; ct; ct= ct->next)
2132 ct->tar = newlibadr(fd, id->lib, ct->tar);
2134 data->text = newlibadr(fd, id->lib, data->text);
2135 //IDP_LibLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2138 case CONSTRAINT_TYPE_ACTION:
2140 bActionConstraint *data;
2141 data= ((bActionConstraint*)con->data);
2142 data->tar = newlibadr(fd, id->lib, data->tar);
2143 data->act = newlibadr(fd, id->lib, data->act);
2146 case CONSTRAINT_TYPE_LOCLIKE:
2148 bLocateLikeConstraint *data;
2149 data= ((bLocateLikeConstraint*)con->data);
2150 data->tar = newlibadr(fd, id->lib, data->tar);
2153 case CONSTRAINT_TYPE_ROTLIKE:
2155 bRotateLikeConstraint *data;
2156 data= ((bRotateLikeConstraint*)con->data);
2157 data->tar = newlibadr(fd, id->lib, data->tar);
2160 case CONSTRAINT_TYPE_SIZELIKE:
2162 bSizeLikeConstraint *data;
2163 data= ((bSizeLikeConstraint*)con->data);
2164 data->tar = newlibadr(fd, id->lib, data->tar);
2167 case CONSTRAINT_TYPE_KINEMATIC:
2169 bKinematicConstraint *data;
2170 data = ((bKinematicConstraint*)con->data);
2171 data->tar = newlibadr(fd, id->lib, data->tar);
2172 data->poletar = newlibadr(fd, id->lib, data->poletar);
2175 case CONSTRAINT_TYPE_TRACKTO:
2177 bTrackToConstraint *data;
2178 data = ((bTrackToConstraint*)con->data);
2179 data->tar = newlibadr(fd, id->lib, data->tar);
2182 case CONSTRAINT_TYPE_MINMAX:
2184 bMinMaxConstraint *data;
2185 data = ((bMinMaxConstraint*)con->data);
2186 data->tar = newlibadr(fd, id->lib, data->tar);
2189 case CONSTRAINT_TYPE_LOCKTRACK:
2191 bLockTrackConstraint *data;
2192 data= ((bLockTrackConstraint*)con->data);
2193 data->tar = newlibadr(fd, id->lib, data->tar);
2196 case CONSTRAINT_TYPE_FOLLOWPATH:
2198 bFollowPathConstraint *data;
2199 data= ((bFollowPathConstraint*)con->data);
2200 data->tar = newlibadr(fd, id->lib, data->tar);
2203 case CONSTRAINT_TYPE_STRETCHTO:
2205 bStretchToConstraint *data;
2206 data= ((bStretchToConstraint*)con->data);
2207 data->tar = newlibadr(fd, id->lib, data->tar);
2210 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
2212 bRigidBodyJointConstraint *data;
2213 data= ((bRigidBodyJointConstraint*)con->data);
2214 data->tar = newlibadr(fd, id->lib, data->tar);
2217 case CONSTRAINT_TYPE_CLAMPTO:
2219 bClampToConstraint *data;
2220 data= ((bClampToConstraint*)con->data);
2221 data->tar = newlibadr(fd, id->lib, data->tar);
2224 case CONSTRAINT_TYPE_CHILDOF:
2226 bChildOfConstraint *data;
2227 data= ((bChildOfConstraint*)con->data);
2228 data->tar = newlibadr(fd, id->lib, data->tar);
2231 case CONSTRAINT_TYPE_TRANSFORM:
2233 bTransformConstraint *data;
2234 data= ((bTransformConstraint*)con->data);
2235 data->tar = newlibadr(fd, id->lib, data->tar);
2238 case CONSTRAINT_TYPE_DISTLIMIT:
2240 bDistLimitConstraint *data;
2241 data= ((bDistLimitConstraint*)con->data);
2242 data->tar = newlibadr(fd, id->lib, data->tar);
2245 case CONSTRAINT_TYPE_SHRINKWRAP:
2247 bShrinkwrapConstraint *data;
2248 data= ((bShrinkwrapConstraint*)con->data);
2249 data->target = newlibadr(fd, id->lib, data->target);
2252 case CONSTRAINT_TYPE_NULL:
2258 static void direct_link_constraints(FileData *fd, ListBase *lb)
2263 for (cons=lb->first; cons; cons=cons->next) {
2264 cons->data = newdataadr(fd, cons->data);
2266 if (cons->type == CONSTRAINT_TYPE_PYTHON) {
2267 bPythonConstraint *data= cons->data;
2269 link_list(fd, &data->targets);
2271 data->prop = newdataadr(fd, data->prop);
2273 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2278 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2280 bPoseChannel *pchan;
2281 bArmature *arm= ob->data;
2287 /* always rebuild to match proxy or lib changes */
2288 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2290 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2291 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2293 /* hurms... loop in a loop, but yah... later... (ton) */
2294 pchan->bone= get_named_bone(arm, pchan->name);
2296 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
2297 if(pchan->bone==NULL)
2299 else if(ob->id.lib==NULL && arm->id.lib) {
2300 /* local pose selection copied to armature, bit hackish */
2301 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
2302 pchan->bone->flag |= pchan->selectflag;
2307 ob->recalc= OB_RECALC;
2308 pose->flag |= POSE_RECALC;
2312 static void lib_link_armature(FileData *fd, Main *main)
2316 arm= main->armature.first;
2319 if(arm->id.flag & LIB_NEEDLINK) {
2320 arm->id.flag -= LIB_NEEDLINK;
2326 static void direct_link_bones(FileData *fd, Bone* bone)
2330 bone->parent= newdataadr(fd, bone->parent);
2332 link_list(fd, &bone->childbase);
2334 for (child=bone->childbase.first; child; child=child->next) {
2335 direct_link_bones(fd, child);
2339 static void direct_link_armature(FileData *fd, bArmature *arm)
2343 link_list(fd, &arm->bonebase);
2346 bone=arm->bonebase.first;
2348 direct_link_bones(fd, bone);
2353 /* ************ READ CAMERA ***************** */
2355 static void lib_link_camera(FileData *fd, Main *main)
2359 ca= main->camera.first;
2361 if(ca->id.flag & LIB_NEEDLINK) {
2362 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2364 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2366 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2368 lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
2370 ca->id.flag -= LIB_NEEDLINK;
2376 static void direct_link_camera(FileData *fd, Camera *ca)
2378 ca->adt= newdataadr(fd, ca->adt);
2379 direct_link_animdata(fd, ca->adt);
2381 direct_link_scriptlink(fd, &ca->scriptlink);
2385 /* ************ READ LAMP ***************** */
2387 static void lib_link_lamp(FileData *fd, Main *main)
2393 la= main->lamp.first;
2395 if(la->id.flag & LIB_NEEDLINK) {
2396 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2398 for(a=0; a<MAX_MTEX; a++) {
2401 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2402 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2406 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2408 lib_link_scriptlink(fd, &la->id, &la->scriptlink);
2410 la->id.flag -= LIB_NEEDLINK;
2416 static void direct_link_lamp(FileData *fd, Lamp *la)
2420 la->adt= newdataadr(fd, la->adt);
2421 direct_link_animdata(fd, la->adt);
2423 direct_link_scriptlink(fd, &la->scriptlink);
2425 for(a=0; a<MAX_MTEX; a++) {
2426 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2429 la->curfalloff= newdataadr(fd, la->curfalloff);
2431 direct_link_curvemapping(fd, la->curfalloff);
2433 la->preview = direct_link_preview_image(fd, la->preview);
2436 /* ************ READ keys ***************** */
2438 static void lib_link_key(FileData *fd, Main *main)
2442 key= main->key.first;
2444 if(key->id.flag & LIB_NEEDLINK) {
2445 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2447 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2448 key->from= newlibadr(fd, key->id.lib, key->from);
2450 key->id.flag -= LIB_NEEDLINK;
2456 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2459 char *data, *poin, *cp;
2461 elemsize= key->elemsize;
2464 for(a=0; a<kb->totelem; a++) {
2469 while( cp[0] ) { /* cp[0]==amount */
2471 switch(cp[1]) { /* cp[1]= type */
2477 SWITCH_INT((*poin));
2490 static void direct_link_key(FileData *fd, Key *key)
2494 link_list(fd, &(key->block));
2496 key->adt= newdataadr(fd, key->adt);
2497 direct_link_animdata(fd, key->adt);
2499 key->refkey= newdataadr(fd, key->refkey);
2501 kb= key->block.first;
2504 kb->data= newdataadr(fd, kb->data);
2506 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2507 switch_endian_keyblock(key, kb);
2513 /* ************ READ mball ***************** */
2515 static void lib_link_mball(FileData *fd, Main *main)
2520 mb= main->mball.first;
2522 if(mb->id.flag & LIB_NEEDLINK) {
2524 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2526 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2528 mb->id.flag -= LIB_NEEDLINK;
2534 static void direct_link_mball(FileData *fd, MetaBall *mb)
2536 mb->mat= newdataadr(fd, mb->mat);
2537 test_pointer_array(fd, (void **)&mb->mat);
2539 link_list(fd, &(mb->elems));
2541 mb->disp.first= mb->disp.last= NULL;
2542 mb->editelems= NULL;
2546 /* ************ READ WORLD ***************** */
2548 static void lib_link_world(FileData *fd, Main *main)
2554 wrld= main->world.first;
2556 if(wrld->id.flag & LIB_NEEDLINK) {
2557 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2559 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2561 for(a=0; a<MAX_MTEX; a++) {
2562 mtex= wrld->mtex[a];
2564 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2565 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2569 lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
2571 wrld->id.flag -= LIB_NEEDLINK;
2573 wrld= wrld->id.next;
2577 static void direct_link_world(FileData *fd, World *wrld)
2581 wrld->adt= newdataadr(fd, wrld->adt);
2582 direct_link_animdata(fd, wrld->adt);
2584 direct_link_scriptlink(fd, &wrld->scriptlink);
2586 for(a=0; a<MAX_MTEX; a++) {
2587 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2589 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2593 /* ************ READ VFONT ***************** */
2595 static void lib_link_vfont(FileData *fd, Main *main)
2599 vf= main->vfont.first;
2601 if(vf->id.flag & LIB_NEEDLINK) {
2602 vf->id.flag -= LIB_NEEDLINK;
2608 static void direct_link_vfont(FileData *fd, VFont *vf)
2611 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2614 /* ************ READ TEXT ****************** */
2616 static void lib_link_text(FileData *fd, Main *main)
2620 text= main->text.first;
2622 if(text->id.flag & LIB_NEEDLINK) {
2623 text->id.flag -= LIB_NEEDLINK;
2625 text= text->id.next;
2629 static void direct_link_text(FileData *fd, Text *text)
2633 text->name= newdataadr(fd, text->name);
2636 text->undo_len= TXT_INIT_UNDO;
2637 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2639 text->compiled= NULL;
2642 if(text->flags & TXT_ISEXT) {
2647 link_list(fd, &text->lines);
2648 link_list(fd, &text->markers);
2650 text->curl= newdataadr(fd, text->curl);
2651 text->sell= newdataadr(fd, text->sell);
2653 ln= text->lines.first;
2655 ln->line= newdataadr(fd, ln->line);
2658 if (ln->len != (int) strlen(ln->line)) {
2659 printf("Error loading text, line lengths differ\n");
2660 ln->len = strlen(ln->line);
2666 text->flags = (text->flags) & ~TXT_ISEXT;
2671 /* ************ READ IMAGE ***************** */
2673 static void lib_link_image(FileData *fd, Main *main)
2677 ima= main->image.first;
2679 if(ima->id.flag & LIB_NEEDLINK) {
2680 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2682 ima->id.flag -= LIB_NEEDLINK;
2688 static void link_ibuf_list(FileData *fd, ListBase *lb)
2692 if(lb->first==NULL) return;
2694 lb->first= newimaadr(fd, lb->first);
2698 ln->next= newimaadr(fd, ln->next);
2706 static void direct_link_image(FileData *fd, Image *ima)
2708 /* for undo system, pointers could be restored */
2710 link_ibuf_list(fd, &ima->ibufs);
2712 ima->ibufs.first= ima->ibufs.last= NULL;
2714 /* if not restored, we keep the binded opengl index */
2715 if(ima->ibufs.first==NULL) {
2717 ima->gputexture= NULL;
2723 ima->render_text= newdataadr(fd, ima->render_text);
2725 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2726 ima->preview = direct_link_preview_image(fd, ima->preview);
2731 /* ************ READ CURVE ***************** */
2733 static void lib_link_curve(FileData *fd, Main *main)
2738 cu= main->curve.first;
2740 if(cu->id.flag & LIB_NEEDLINK) {
2741 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2743 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2745 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2746 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2747 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2748 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2749 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2750 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2751 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2753 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2754 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2756 cu->id.flag -= LIB_NEEDLINK;
2763 static void switch_endian_knots(Nurb *nu)
2770 SWITCH_INT(nu->knotsu[len]);
2776 SWITCH_INT(nu->knotsv[len]);
2781 static void direct_link_curve(FileData *fd, Curve *cu)
2786 cu->adt= newdataadr(fd, cu->adt);
2787 direct_link_animdata(fd, cu->adt);
2789 cu->mat= newdataadr(fd, cu->mat);
2790 test_pointer_array(fd, (void **)&cu->mat);
2791 cu->str= newdataadr(fd, cu->str);
2792 cu->strinfo= newdataadr(fd, cu->strinfo);
2793 cu->tb= newdataadr(fd, cu->tb);
2795 if(cu->vfont==0) link_list(fd, &(cu->nurb));
2797 cu->nurb.first=cu->nurb.last= 0;
2799 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2801 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2808 cu->tb[0].w = cu->linewidth;
2810 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2813 cu->bev.first=cu->bev.last= NULL;
2814 cu->disp.first=cu->disp.last= NULL;
2816 cu->lastselbp= NULL;
2822 nu->bezt= newdataadr(fd, nu->bezt);
2823 nu->bp= newdataadr(fd, nu->bp);
2824 nu->knotsu= newdataadr(fd, nu->knotsu);
2825 nu->knotsv= newdataadr(fd, nu->knotsv);
2826 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2828 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2829 switch_endian_knots(nu);
2837 /* ************ READ TEX ***************** */
2839 static void lib_link_texture(FileData *fd, Main *main)
2843 tex= main->tex.first;
2845 if(tex->id.flag & LIB_NEEDLINK) {
2846 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
2848 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2849 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2850 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2853 lib_link_ntree(fd, &tex->id, tex->nodetree);
2855 tex->id.flag -= LIB_NEEDLINK;
2861 static void direct_link_texture(FileData *fd, Tex *tex)
2863 tex->adt= newdataadr(fd, tex->adt);
2864 direct_link_animdata(fd, tex->adt);
2866 tex->plugin= newdataadr(fd, tex->plugin);
2868 tex->plugin->handle= 0;
2869 open_plugin_tex(tex->plugin);
2870 /* initialize data for this instance, if an initialization
2873 if (tex->plugin->instance_init)
2874 tex->plugin->instance_init((void *) tex->plugin->data);
2876 tex->coba= newdataadr(fd, tex->coba);
2877 tex->env= newdataadr(fd, tex->env);
2879 tex->env->ima= NULL;
2880 memset(tex->env->cube, 0, 6*sizeof(void *));
2884 tex->nodetree= newdataadr(fd, tex->nodetree);
2886 direct_link_nodetree(fd, tex->nodetree);
2888 tex->preview = direct_link_preview_image(fd, tex->preview);
2895 /* ************ READ MATERIAL ***************** */
2897 static void lib_link_material(FileData *fd, Main *main)
2903 ma= main->mat.first;
2905 if(ma->id.flag & LIB_NEEDLINK) {
2906 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
2908 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2909 of library blocks that implement this.*/
2910 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2912 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2913 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2915 for(a=0; a<MAX_MTEX; a++) {
2918 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2919 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2922 lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
2925 lib_link_ntree(fd, &ma->id, ma->nodetree);
2927 ma->id.flag -= LIB_NEEDLINK;
2933 static void direct_link_material(FileData *fd, Material *ma)
2937 ma->adt= newdataadr(fd, ma->adt);
2938 direct_link_animdata(fd, ma->adt);
2940 for(a=0; a<MAX_MTEX; a++) {
2941 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2944 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2945 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2947 direct_link_scriptlink(fd, &ma->scriptlink);
2949 ma->nodetree= newdataadr(fd, ma->nodetree);
2951 direct_link_nodetree(fd, ma->nodetree);
2953 ma->preview = direct_link_preview_image(fd, ma->preview);
2954 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
2957 /* ************ READ PARTICLE SETTINGS ***************** */
2959 static void direct_link_pointcache(FileData *fd, PointCache *cache)
2961 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
2964 link_list(fd, &cache->mem_cache);
2966 pm = cache->mem_cache.first;
2968 for(; pm; pm=pm->next)
2969 pm->data = newdataadr(fd, pm->data);
2972 cache->mem_cache.first = cache->mem_cache.last = NULL;
2974 cache->flag &= ~(PTCACHE_SIMULATION_VALID|PTCACHE_BAKE_EDIT_ACTIVE);
2978 static void lib_link_particlesettings(FileData *fd, Main *main)
2980 ParticleSettings *part;
2982 part= main->particle.first;
2984 if(part->id.flag & LIB_NEEDLINK) {
2985 if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
2986 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
2988 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
2989 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
2990 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
2991 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
2992 part->id.flag -= LIB_NEEDLINK;
2994 part= part->id.next;
2998 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
3000 part->adt= newdataadr(fd, part->adt);
3001 part->pd= newdataadr(fd, part->pd);
3002 part->pd2= newdataadr(fd, part->pd2);
3005 static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase *particles)
3007 ParticleSystem *psys, *psysnext;
3010 for(psys=particles->first; psys; psys=psysnext){
3013 psysnext= psys->next;
3015 psys->part = newlibadr_us(fd, id->lib, psys->part);
3017 KeyedParticleTarget *kpt = psys->keyed_targets.first;
3019 for(; kpt; kpt=kpt->next)
3020 kpt->ob=newlibadr(fd, id->lib, kpt->ob);
3022 psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
3024 for(a=0,pa=psys->particles; a<psys->totpart; a++,pa++){
3025 pa->stick_ob=newlibadr(fd, id->lib, pa->stick_ob);
3031 /* particle modifier must be removed before particle system */
3032 ParticleSystemModifierData *psmd= psys_get_modifier(ob,psys);
3033 BLI_remlink(&ob->modifiers, psmd);
3034 modifier_free((ModifierData *)psmd);
3036 BLI_remlink(particles, psys);
3041 static void direct_link_particlesystems(FileData *fd, ListBase *particles)
3043 ParticleSystem *psys;
3046 for(psys=particles->first; psys; psys=psys->next) {
3047 psys->particles=newdataadr(fd,psys->particles);
3048 if(psys->particles && psys->particles->hair){
3049 ParticleData *pa = psys->particles;
3050 for(a=0; a<psys->totpart; a++, pa++)
3051 pa->hair=newdataadr(fd,pa->hair);
3053 if(psys->particles && psys->particles->keys){
3054 ParticleData *pa = psys->particles;
3055 for(a=0; a<psys->totpart; a++, pa++) {
3060 psys->flag &= ~PSYS_KEYED;
3062 psys->child=newdataadr(fd,psys->child);
3063 psys->effectors.first=psys->effectors.last=0;
3065 psys->soft= newdataadr(fd, psys->soft);
3067 SoftBody *sb = psys->soft;
3068 sb->particles = psys;
3069 sb->bpoint= NULL; // init pointers so it gets rebuilt nicely
3073 sb->pointcache= newdataadr(fd, sb->pointcache);
3075 direct_link_pointcache(fd, sb->pointcache);
3078 link_list(fd, &psys->keyed_targets);
3081 psys->free_edit = NULL;
3082 psys->pathcache = 0;
3083 psys->childcache = 0;
3084 psys->pathcachebufs.first = psys->pathcachebufs.last = 0;
3085 psys->childcachebufs.first = psys->childcachebufs.last = 0;
3086 psys->reactevents.first = psys->reactevents.last = 0;
3088 psys->pointcache= newdataadr(fd, psys->pointcache);
3089 if(psys->pointcache)
3090 direct_link_pointcache(fd, psys->pointcache);
3095 /* ************ READ MESH ***************** */
3097 static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
3102 for (i=0; i<totface; i++, tf++) {
3103 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
3104 if(tf->tpage && tf->tpage->id.us==0)
3105 tf->tpage->id.us= 1;
3109 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
3112 for(i=0; i<fdata->totlayer; i++) {
3113 CustomDataLayer *layer = &fdata->layers[i];
3115 if(layer->type == CD_MTFACE)
3116 lib_link_mtface(fd, me, layer->data, totface);
3121 static void lib_link_mesh(FileData *fd, Main *main)
3125 me= main->mesh.first;
3127 if(me->id.flag & LIB_NEEDLINK) {
3130 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3131 of library blocks that implement this.*/
3132 if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3134 /* this check added for python created meshes */
3136 for(i=0; i<me->totcol; i++) {
3137 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
3142 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
3143 me->key= newlibadr_us(fd, me->id.lib, me->key);
3144 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
3146 lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
3147 if(me->mr && me->mr->levels.first)
3148 lib_link_customdata_mtface(fd, me, &me->mr->fdata,
3149 ((MultiresLevel*)me->mr->levels.first)->totface);
3151 me->id.flag -= LIB_NEEDLINK;
3157 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
3164 for (i=0; i<count; i++) {
3165 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
3167 mdverts[i].totweight=0;
3171 static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps)
3176 for(i = 0; i < count; ++i) {
3177 mdisps[i].disps = newdataadr(fd, mdisps[i].disps);
3178 if(!mdisps[i].disps)
3179 mdisps[i].totdisp = 0;
3184 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
3188 data->layers= newdataadr(fd, data->layers);
3190 while (i < data->totlayer) {
3191 CustomDataLayer *layer = &data->layers[i];
3193 if (CustomData_verify_versions(data, i)) {
3194 layer->data = newdataadr(fd, layer->data);
3195 if(layer->type == CD_MDISPS)
3196 direct_link_mdisps(fd, count, layer->data);
3202 static void direct_link_mesh(FileData *fd, Mesh *mesh)
3204 mesh->mat= newdataadr(fd, mesh->mat);
3205 test_pointer_array(fd, (void **)&mesh->mat);
3207 mesh->mvert= newdataadr(fd, mesh->mvert);
3208 mesh->medge= newdataadr(fd, mesh->medge);
3209 mesh->mface= newdataadr(fd, mesh->mface);
3210 mesh->tface= newdataadr(fd, mesh->tface);