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.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
39 #include "BLI_winstuff.h"
42 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
43 #include <stdlib.h> // for getenv atoi
44 #include <fcntl.h> // for open
45 #include <string.h> // for strrchr strncmp strstr
46 #include <math.h> // for fabs
49 #include <unistd.h> // for read close
50 #include <sys/param.h> // for MAXPATHLEN
52 #include <io.h> // for open close read
57 #include "DNA_action_types.h"
58 #include "DNA_armature_types.h"
60 #include "DNA_actuator_types.h"
61 #include "DNA_brush_types.h"
62 #include "DNA_camera_types.h"
63 #include "DNA_cloth_types.h"
64 #include "DNA_color_types.h"
65 #include "DNA_controller_types.h"
66 #include "DNA_constraint_types.h"
67 #include "DNA_curve_types.h"
68 #include "DNA_customdata_types.h"
69 #include "DNA_effect_types.h"
70 #include "DNA_fileglobal_types.h"
71 #include "DNA_group_types.h"
72 #include "DNA_ipo_types.h"
73 #include "DNA_image_types.h"
74 #include "DNA_key_types.h"
75 #include "DNA_lattice_types.h"
76 #include "DNA_lamp_types.h"
77 #include "DNA_meta_types.h"
78 #include "DNA_material_types.h"
79 #include "DNA_mesh_types.h"
80 #include "DNA_meshdata_types.h"
81 #include "DNA_modifier_types.h"
82 #include "DNA_nla_types.h"
83 #include "DNA_node_types.h"
84 #include "DNA_object_types.h"
85 #include "DNA_object_force.h"
86 #include "DNA_object_fluidsim.h" // NT
87 #include "DNA_oops_types.h"
88 #include "DNA_object_force.h"
89 #include "DNA_packedFile_types.h"
90 #include "DNA_property_types.h"
91 #include "DNA_text_types.h"
92 #include "DNA_view3d_types.h"
93 #include "DNA_screen_types.h"
94 #include "DNA_sensor_types.h"
95 #include "DNA_sdna_types.h"
96 #include "DNA_scene_types.h"
97 #include "DNA_sequence_types.h"
98 #include "DNA_sound_types.h"
99 #include "DNA_space_types.h"
100 #include "DNA_texture_types.h"
101 #include "DNA_userdef_types.h"
102 #include "DNA_vfont_types.h"
103 #include "DNA_world_types.h"
105 #include "MEM_guardedalloc.h"
106 #include "BLI_blenlib.h"
107 #include "BLI_arithb.h"
108 #include "BLI_storage_types.h" // for relname flags
110 #include "BDR_sculptmode.h"
112 #include "BKE_bad_level_calls.h" // for reopen_text build_seqar (from WHILE_SEQ) set_rects_butspace check_imasel_copy
114 #include "BKE_action.h"
115 #include "BKE_armature.h"
116 #include "BKE_cloth.h"
117 #include "BKE_colortools.h"
118 #include "BKE_constraint.h"
119 #include "BKE_curve.h"
120 #include "BKE_customdata.h"
121 #include "BKE_deform.h"
122 #include "BKE_depsgraph.h"
123 #include "BKE_effect.h" // for give_parteff
124 #include "BKE_global.h" // for G
125 #include "BKE_group.h"
126 #include "BKE_image.h"
127 #include "BKE_lattice.h"
128 #include "BKE_library.h" // for wich_libbase
129 #include "BKE_main.h" // for Main
130 #include "BKE_mesh.h" // for ME_ defines (patching)
131 #include "BKE_modifier.h"
132 #include "BKE_node.h" // for tree type defines
133 #include "BKE_object.h"
134 #include "BKE_property.h" // for get_property
135 #include "BKE_sca.h" // for init_actuator
136 #include "BKE_scene.h"
137 #include "BKE_softbody.h" // sbNew()
138 #include "BKE_texture.h" // for open_plugin_tex
139 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
140 #include "BKE_idprop.h"
142 #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
143 #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
144 #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
145 #include "BLO_readfile.h"
146 #include "BLO_undofile.h"
147 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
149 #include "multires.h"
151 #include "readfile.h"
155 #include "mydevice.h"
161 Remark: still a weak point is the newadress() function, that doesnt solve reading from
162 multiple files at the same time
164 (added remark: oh, i thought that was solved? will look at that... (ton)
167 - Existing Library (Main) push or free
177 - read associated 'direct data'
178 - link direct data (internal and to LibBlock)
180 - read USER data, only when indicated (file is ~/.B.blend)
182 - per Library (per Main)
185 - find LibBlocks and attach IDs to Main
186 - if external LibBlock
188 - or it's already read,
193 - read associated direct data
194 - link direct data (internal and to LibBlock)
196 - per Library with unread LibBlocks
201 - read associated direct data
202 - link direct data (internal and to LibBlock)
205 - link all LibBlocks and indirect pointers to libblocks
206 - initialize FileGlobal and copy pointers to Global
209 /* also occurs in library.c */
210 /* GS reads the memory pointed at in a specific ordering. There are,
211 * however two definitions for it. I have jotted them down here, both,
212 * but I think the first one is actually used. The thing is that
213 * big-endian systems might read this the wrong way round. OTOH, we
214 * constructed the IDs that are read out with this macro explicitly as
215 * well. I expect we'll sort it out soon... */
218 #define GS(a) (*((short *)(a)))
220 /* from misc_util: flip the bytes from x */
221 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
223 // only used here in readfile.c
224 #define SWITCH_LONGINT(a) { \
227 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
228 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
229 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
230 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
234 typedef struct OldNew {
239 typedef struct OldNewMap {
241 int nentries, entriessize;
247 /* local prototypes */
248 static void *read_struct(FileData *fd, BHead *bh, char *blockname);
251 static OldNewMap *oldnewmap_new(void)
253 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
255 onm->entriessize= 1024;
256 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
261 static int verg_oldnewmap(const void *v1, const void *v2)
263 const struct OldNew *x1=v1, *x2=v2;
265 if( x1->old > x2->old) return 1;
266 else if( x1->old < x2->old) return -1;
271 static void oldnewmap_sort(FileData *fd)
273 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
274 fd->libmap->sorted= 1;
277 /* nr is zero for data, and ID code for libdata */
278 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
282 if(oldaddr==NULL || newaddr==NULL) return;
284 if (onm->nentries==onm->entriessize) {
285 int osize= onm->entriessize;
286 OldNew *oentries= onm->entries;
288 onm->entriessize*= 2;
289 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
291 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
295 entry= &onm->entries[onm->nentries++];
297 entry->newp= newaddr;
301 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
305 if (onm->lasthit<onm->nentries-1) {
306 OldNew *entry= &onm->entries[++onm->lasthit];
308 if (entry->old==addr) {
314 for (i=0; i<onm->nentries; i++) {
315 OldNew *entry= &onm->entries[i];
317 if (entry->old==addr) {
328 /* for libdata, nr has ID code, no increment */
329 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
333 if(addr==NULL) return NULL;
335 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
337 OldNew entry_s, *entry;
341 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
345 if (id && (!lib || id->lib)) {
351 for (i=0; i<onm->nentries; i++) {
352 OldNew *entry= &onm->entries[i];
354 if (entry->old==addr) {
357 if (id && (!lib || id->lib)) {
366 static void oldnewmap_free_unused(OldNewMap *onm)
370 for (i=0; i<onm->nentries; i++) {
371 OldNew *entry= &onm->entries[i];
373 MEM_freeN(entry->newp);
379 static void oldnewmap_clear(OldNewMap *onm)
385 static void oldnewmap_free(OldNewMap *onm)
387 MEM_freeN(onm->entries);
393 static void read_libraries(FileData *basefd, ListBase *mainlist);
395 /* ************ help functions ***************** */
397 static void add_main_to_main(Main *mainvar, Main *from)
399 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
402 a= set_listbasepointers(mainvar, lbarray);
403 a= set_listbasepointers(from, fromarray);
405 addlisttolist(lbarray[a], fromarray[a]);
409 void blo_join_main(ListBase *mainlist)
411 Main *tojoin, *mainl;
414 mainl= mainlist->first;
415 while ((tojoin= mainl->next)) {
416 add_main_to_main(mainl, tojoin);
417 BLI_remlink(mainlist, tojoin);
422 static void split_libdata(ListBase *lb, Main *first)
434 if(mainvar->curlib==id->lib) {
435 lbn= wich_libbase(mainvar, GS(id->name));
437 BLI_addtail(lbn, id);
440 mainvar= mainvar->next;
442 if(mainvar==0) printf("error split_libdata\n");
448 void blo_split_main(ListBase *mainlist, Main *main)
450 ListBase *lbarray[MAX_LIBARRAY];
454 mainlist->first= mainlist->last= main;
457 if(main->library.first==NULL)
460 for (lib= main->library.first; lib; lib= lib->id.next) {
461 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
462 libmain->curlib= lib;
463 BLI_addtail(mainlist, libmain);
466 i= set_listbasepointers(main, lbarray);
468 split_libdata(lbarray[i], main->next);
471 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
472 static void cleanup_path(const char *relabase, char *name)
474 char filename[FILE_MAXFILE];
476 BLI_splitdirstring(name, filename);
477 BLI_cleanup_dir(relabase, name);
478 strcat(name, filename);
481 static Main *blo_find_main(ListBase *mainlist, const char *name, const char *relabase)
485 char name1[FILE_MAXDIR+FILE_MAXFILE];
487 strncpy(name1, name, sizeof(name1)-1);
488 cleanup_path(relabase, name1);
489 // printf("blo_find_main: original in %s\n", name);
490 // printf("blo_find_main: converted to %s\n", name1);
492 for (m= mainlist->first; m; m= m->next) {
493 char *libname= (m->curlib)?m->curlib->filename:m->name;
495 if (BLI_streq(name1, libname)) {
496 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
501 m= MEM_callocN(sizeof(Main), "find_main");
502 BLI_addtail(mainlist, m);
504 lib= alloc_libblock(&m->library, ID_LI, "lib");
505 strncpy(lib->name, name, sizeof(lib->name)-1);
506 BLI_strncpy(lib->filename, name1, sizeof(lib->filename));
510 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", name);
515 /* ************ FILE PARSING ****************** */
517 static void switch_endian_bh4(BHead4 *bhead)
519 /* the ID_.. codes */
520 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
522 if (bhead->code != ENDB) {
523 SWITCH_INT(bhead->len);
524 SWITCH_INT(bhead->SDNAnr);
525 SWITCH_INT(bhead->nr);
529 static void switch_endian_bh8(BHead8 *bhead)
531 /* the ID_.. codes */
532 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
534 if (bhead->code != ENDB) {
535 SWITCH_INT(bhead->len);
536 SWITCH_INT(bhead->SDNAnr);
537 SWITCH_INT(bhead->nr);
541 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
543 BHead4 *bhead4 = (BHead4 *) bhead;
544 #if defined(WIN32) && !defined(FREE_WINDOWS)
550 bhead4->code= bhead8->code;
551 bhead4->len= bhead8->len;
553 if (bhead4->code != ENDB) {
555 // why is this here ??
556 if (do_endian_swap) {
557 SWITCH_LONGINT(bhead8->old);
560 /* this patch is to avoid a long long being read from not-eight aligned positions
561 is necessary on any modern 64bit architecture) */
562 memcpy(&old, &bhead8->old, 8);
563 bhead4->old = (int) (old >> 3);
565 bhead4->SDNAnr= bhead8->SDNAnr;
566 bhead4->nr= bhead8->nr;
570 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
572 BHead8 *bhead8 = (BHead8 *) bhead;
574 bhead8->code= bhead4->code;
575 bhead8->len= bhead4->len;
577 if (bhead8->code != ENDB) {
578 bhead8->old= bhead4->old;
579 bhead8->SDNAnr= bhead4->SDNAnr;
580 bhead8->nr= bhead4->nr;
584 static BHeadN *get_bhead(FileData *fd)
589 BHeadN *new_bhead = 0;
595 // First read the bhead structure.
596 // Depending on the platform the file was written on this can
597 // be a big or little endian BHead4 or BHead8 structure.
599 // As usual 'ENDB' (the last *partial* bhead of the file)
600 // needs some special handling. We don't want to EOF just yet.
602 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
604 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
606 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
607 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
608 switch_endian_bh4(&bhead4);
611 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
612 bh8_from_bh4(&bhead, &bhead4);
614 memcpy(&bhead, &bhead4, sizeof(bhead));
622 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
624 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
625 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
626 switch_endian_bh8(&bhead8);
629 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
630 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
632 memcpy(&bhead, &bhead8, sizeof(bhead));
640 /* make sure people are not trying to pass bad blend files */
641 if (bhead.len < 0) fd->eof = 1;
643 // bhead now contains the (converted) bhead structure. Now read
644 // the associated data and put everything in a BHeadN (creative naming !)
647 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
649 new_bhead->next = new_bhead->prev = 0;
650 new_bhead->bhead = bhead;
652 readsize = fd->read(fd, new_bhead + 1, bhead.len);
654 if (readsize != bhead.len) {
656 MEM_freeN(new_bhead);
666 // We've read a new block. Now add it to the list
670 BLI_addtail(&fd->listbase, new_bhead);
676 BHead *blo_firstbhead(FileData *fd)
682 // Read in a new block if necessary
684 new_bhead = fd->listbase.first;
685 if (new_bhead == 0) {
686 new_bhead = get_bhead(fd);
690 bhead = &new_bhead->bhead;
696 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
698 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
699 BHeadN *prev= bheadn->prev;
701 return prev?&prev->bhead:NULL;
704 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
706 BHeadN *new_bhead = 0;
710 // bhead is actually a sub part of BHeadN
711 // We calculate the BHeadN pointer from the BHead pointer below
712 new_bhead = (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
714 // get the next BHeadN. If it doesn't exist we read in the next one
715 new_bhead = new_bhead->next;
716 if (new_bhead == 0) {
717 new_bhead = get_bhead(fd);
722 // here we do the reverse:
723 // go from the BHeadN pointer to the BHead pointer
724 bhead = &new_bhead->bhead;
731 static void get_blender_subversion(FileData *fd)
735 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
736 if (bhead->code==GLOB) {
737 FileGlobal *fg= read_struct(fd, bhead, "Global");
738 fd->filesubversion= fg->subversion;
739 fd->fileminversion= fg->minversion;
740 fd->fileminsubversion= fg->minsubversion;
744 else if (bhead->code==ENDB)
750 static void decode_blender_header(FileData *fd)
752 char header[SIZEOFBLENDERHEADER], num[4];
755 // read in the header data
756 readsize = fd->read(fd, header, sizeof(header));
758 if (readsize == sizeof(header)) {
759 if(strncmp(header, "BLENDER", 7) == 0) {
760 int remove_this_endian_test= 1;
762 fd->flags |= FD_FLAGS_FILE_OK;
764 // what size are pointers in the file ?
766 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
767 if (sizeof(void *) != 4) {
768 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
771 if (sizeof(void *) != 8) {
772 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
776 // is the file saved in a different endian
778 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
779 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
782 // get the version number
784 memcpy(num, header+9, 3);
786 fd->fileversion = atoi(num);
791 static int read_file_dna(FileData *fd)
795 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
796 if (bhead->code==DNA1) {
797 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
799 fd->filesdna= dna_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
802 fd->compflags= dna_get_structDNA_compareflags(fd->filesdna, fd->memsdna);
803 /* used to retrieve ID names from (bhead+1) */
804 fd->id_name_offs= dna_elem_offset(fd->filesdna, "ID", "char", "name[]");
808 } else if (bhead->code==ENDB)
815 static int fd_read_from_file(FileData *filedata, void *buffer, int size)
817 int readsize = read(filedata->filedes, buffer, size);
822 filedata->seek += readsize;
828 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, int size)
830 int readsize = gzread(filedata->gzfiledes, buffer, size);
835 filedata->seek += readsize;
841 static int fd_read_from_memory(FileData *filedata, void *buffer, int size)
843 // don't read more bytes then there are available in the buffer
844 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
846 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
847 filedata->seek += readsize;
852 static int fd_read_from_memfile(FileData *filedata, void *buffer, int size)
854 static unsigned int seek= 1<<30; /* the current position */
855 static unsigned int offset= 0; /* size of previous chunks */
856 static MemFileChunk *chunk=NULL;
858 if(size==0) return 0;
860 if(seek != (unsigned int)filedata->seek) {
861 chunk= filedata->memfile->chunks.first;
865 if(seek + chunk->size > (unsigned) filedata->seek) break;
870 seek= filedata->seek;
874 /* first check if it's on the end if current chunk */
875 if( seek-offset == chunk->size) {
876 offset+= chunk->size;
880 /* debug, should never happen */
882 printf("illegal read, chunk zero\n");
885 else if( (seek-offset)+size > chunk->size) {
886 size= chunk->size - (seek-offset);
887 printf("chunk too large, clipped to %d\n", size);
890 memcpy(buffer, chunk->buf + (seek-offset), size);
891 filedata->seek += size;
900 static FileData *filedata_new(void)
902 extern unsigned char DNAstr[]; /* DNA.c */
904 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
907 fd->gzfiledes = NULL;
909 /* XXX, this doesn't need to be done all the time,
910 * but it keeps us reentrant, remove once we have
911 * a lib that provides a nice lock. - zr
913 fd->memsdna = dna_sdna_from_data(DNAstr, DNAlen, 0);
915 fd->datamap = oldnewmap_new();
916 fd->globmap = oldnewmap_new();
917 fd->libmap = oldnewmap_new();
922 static FileData *blo_decode_and_check(FileData *fd, BlendReadError *error_r)
924 decode_blender_header(fd);
926 if (fd->flags & FD_FLAGS_FILE_OK) {
927 if (!read_file_dna(fd)) {
928 *error_r = BRE_INCOMPLETE;
929 blo_freefiledata(fd);
934 *error_r = BRE_NOT_A_BLEND;
935 blo_freefiledata(fd);
942 /* cannot be called with relative paths anymore! */
943 /* on each new library added, it now checks for the current FileData and expands relativeness */
944 FileData *blo_openblenderfile(char *name, BlendReadError *error_r)
948 gzfile= gzopen(name, "rb");
950 if (NULL == gzfile) {
951 *error_r = BRE_UNABLE_TO_OPEN;
954 FileData *fd = filedata_new();
955 fd->gzfiledes = gzfile;
956 BLI_strncpy(fd->filename, name, sizeof(fd->filename)); // now only in use by library append
957 fd->read = fd_read_gzip_from_file;
959 return blo_decode_and_check(fd, error_r);
963 FileData *blo_openblendermemory(void *mem, int memsize, BlendReadError *error_r)
965 if (!mem || memsize<SIZEOFBLENDERHEADER) {
966 *error_r = mem?BRE_UNABLE_TO_READ:BRE_UNABLE_TO_OPEN;
969 FileData *fd= filedata_new();
971 fd->buffersize= memsize;
972 fd->read= fd_read_from_memory;
973 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
975 return blo_decode_and_check(fd, error_r);
979 FileData *blo_openblendermemfile(MemFile *memfile, BlendReadError *error_r)
982 *error_r = BRE_UNABLE_TO_OPEN;
985 FileData *fd= filedata_new();
986 fd->memfile= memfile;
988 fd->read= fd_read_from_memfile;
989 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
991 return blo_decode_and_check(fd, error_r);
996 void blo_freefiledata(FileData *fd)
1000 if (fd->filedes != -1) {
1004 if (fd->gzfiledes != NULL)
1006 gzclose(fd->gzfiledes);
1009 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1010 MEM_freeN(fd->buffer);
1014 // Free all BHeadN data blocks
1015 BLI_freelistN(&fd->listbase);
1018 dna_freestructDNA(fd->memsdna);
1020 dna_freestructDNA(fd->filesdna);
1022 MEM_freeN(fd->compflags);
1025 oldnewmap_free(fd->datamap);
1027 oldnewmap_free(fd->globmap);
1029 oldnewmap_free(fd->imamap);
1030 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1031 oldnewmap_free(fd->libmap);
1037 /* ************ DIV ****************** */
1039 int BLO_has_bfile_extension(char *str)
1041 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1044 /* ************** OLD POINTERS ******************* */
1046 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1048 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1051 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1053 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1056 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1058 if(fd->imamap && adr)
1059 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1064 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1066 return oldnewmap_liblookup(fd->libmap, adr, lib);
1069 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1071 ID *id= newlibadr(fd, lib, adr);
1079 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1083 for (i=0; i<fd->libmap->nentries; i++) {
1084 OldNew *entry= &fd->libmap->entries[i];
1086 if (old==entry->newp && entry->nr==ID_ID) {
1088 if(new) entry->nr= GS( ((ID *)new)->name );
1094 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1098 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1101 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1105 change_idid_adr_fd(fd, old, new);
1110 /* assumed; G.main still exists */
1111 void blo_make_image_pointer_map(FileData *fd)
1113 Image *ima= G.main->image.first;
1114 Scene *sce= G.main->scene.first;
1116 fd->imamap= oldnewmap_new();
1118 for(;ima; ima= ima->id.next) {
1119 Link *ibuf= ima->ibufs.first;
1120 for(; ibuf; ibuf= ibuf->next)
1121 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1123 for(; sce; sce= sce->id.next) {
1126 for(node= sce->nodetree->nodes.first; node; node= node->next)
1127 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1132 /* set G.main image ibufs to zero if it has been restored */
1133 /* this works because freeing G.main only happens after this call */
1134 void blo_end_image_pointer_map(FileData *fd)
1136 OldNew *entry= fd->imamap->entries;
1137 Image *ima= G.main->image.first;
1138 Scene *sce= G.main->scene.first;
1141 /* used entries were restored, so we put them to zero */
1142 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1147 for(;ima; ima= ima->id.next) {
1150 /* this mirrors direct_link_image */
1151 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1153 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1154 BLI_remlink(&ima->ibufs, ibuf);
1159 for(; sce; sce= sce->id.next) {
1162 for(node= sce->nodetree->nodes.first; node; node= node->next)
1163 node->preview= newimaadr(fd, node->preview);
1168 /* undo file support: add all library pointers in lookup */
1169 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1171 Main *ptr= mainlist->first;
1172 ListBase *lbarray[MAX_LIBARRAY];
1174 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1175 int i= set_listbasepointers(ptr, lbarray);
1178 for(id= lbarray[i]->first; id; id= id->next)
1179 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1185 /* ********** END OLD POINTERS ****************** */
1186 /* ********** READ FILE ****************** */
1188 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1190 int blocksize, nblocks;
1193 data= (char *)(bhead+1);
1194 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1198 dna_switch_endian_struct(filesdna, bhead->SDNAnr, data);
1204 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1209 /* switch is based on file dna */
1210 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1211 switch_endian_structs(fd->filesdna, bh);
1213 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1214 if(fd->compflags[bh->SDNAnr]==2) {
1215 temp= dna_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1217 temp= MEM_mallocN(bh->len, blockname);
1218 memcpy(temp, (bh+1), bh->len);
1226 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1230 if(lb->first==NULL) return;
1232 lb->first= newdataadr(fd, lb->first);
1236 ln->next= newdataadr(fd, ln->next);
1244 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1249 if(lb->first==0) return;
1250 poin= newdataadr(fd, lb->first);
1252 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1259 poin= newdataadr(fd, ln->next);
1261 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1271 static void test_pointer_array(FileData *fd, void **mat)
1273 #if defined(WIN32) && !defined(FREE_WINDOWS)
1274 __int64 *lpoin, *lmat;
1276 long long *lpoin, *lmat;
1278 int len, *ipoin, *imat;
1280 /* manually convert the pointer array in
1281 * the old dna format to a pointer array in
1282 * the new dna format.
1285 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1287 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1288 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1292 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1293 SWITCH_LONGINT(*lpoin);
1294 *ipoin= (int) ((*lpoin) >> 3);
1302 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1303 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1317 /* ************ READ ID Properties *************** */
1319 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd);
1320 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd);
1322 void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, void *fd)
1326 /*since we didn't save the extra buffer, set totallen to len.*/
1327 prop->totallen = prop->len;
1328 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1330 if (switch_endian) {
1331 for (i=0; i<prop->len; i++) {
1332 SWITCH_INT(((int*)prop->data.pointer)[i]);
1337 void IDP_DirectLinkString(IDProperty *prop, int switch_endian, void *fd)
1339 /*since we didn't save the extra string buffer, set totallen to len.*/
1340 prop->totallen = prop->len;
1341 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1344 void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, void *fd)
1346 ListBase *lb = &prop->data.group;
1351 /*Link child id properties now*/
1352 for (loop=prop->data.group.first; loop; loop=loop->next) {
1353 IDP_DirectLinkProperty(loop, switch_endian, fd);
1357 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd)
1359 switch (prop->type) {
1361 IDP_DirectLinkGroup(prop, switch_endian, fd);
1364 IDP_DirectLinkString(prop, switch_endian, fd);
1367 IDP_DirectLinkArray(prop, switch_endian, fd);
1373 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd)
1377 /* ************ READ Brush *************** */
1378 /* library brush linking after fileread */
1379 static void lib_link_brush(FileData *fd, Main *main)
1385 /* only link ID pointers */
1386 for(brush= main->brush.first; brush; brush= brush->id.next) {
1387 if(brush->id.flag & LIB_NEEDLINK) {
1388 brush->id.flag -= LIB_NEEDLINK;
1390 for(a=0; a<MAX_MTEX; a++) {
1391 mtex= brush->mtex[a];
1393 mtex->tex= newlibadr_us(fd, brush->id.lib, mtex->tex);
1399 static void direct_link_brush(FileData *fd, Brush *brush)
1401 /* brush itself has been read */
1404 for(a=0; a<MAX_MTEX; a++)
1405 brush->mtex[a]= newdataadr(fd, brush->mtex[a]);
1408 /* ************ READ CurveMapping *************** */
1410 /* cuma itself has been read! */
1411 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1415 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1416 cumap->flag &= ~CUMA_PREMULLED;
1418 for(a=0; a<CM_TOT; a++) {
1419 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1420 cumap->cm[a].table= NULL;
1424 /* ************ READ NODE TREE *************** */
1426 /* singe node tree (also used for material/scene trees), ntree is not NULL */
1427 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
1431 for(node= ntree->nodes.first; node; node= node->next)
1432 node->id= newlibadr_us(fd, id->lib, node->id);
1435 /* library ntree linking after fileread */
1436 static void lib_link_nodetree(FileData *fd, Main *main)
1440 /* only link ID pointers */
1441 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1442 if(ntree->id.flag & LIB_NEEDLINK) {
1443 ntree->id.flag -= LIB_NEEDLINK;
1444 lib_link_ntree(fd, &ntree->id, ntree);
1449 /* verify types for nodes and groups, all data has to be read */
1450 static void lib_verify_nodetree(Main *main)
1456 /* now create the own typeinfo structs an verify nodes */
1457 /* here we still assume no groups in groups */
1458 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1459 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
1460 ntreeMakeOwnType(ntree); /* for group usage */
1463 /* now verify all types in material trees, groups are set OK now */
1464 for(ma= main->mat.first; ma; ma= ma->id.next) {
1466 ntreeVerifyTypes(ma->nodetree);
1468 /* and scene trees */
1469 for(sce= main->scene.first; sce; sce= sce->id.next) {
1471 ntreeVerifyTypes(sce->nodetree);
1477 /* ntree itself has been read! */
1478 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
1480 /* note: writing and reading goes in sync, for speed */
1485 ntree->init= 0; /* to set callbacks and force setting types */
1486 ntree->owntype= NULL;
1487 ntree->timecursor= NULL;
1489 link_list(fd, &ntree->nodes);
1490 for(node= ntree->nodes.first; node; node= node->next) {
1491 node->storage= newdataadr(fd, node->storage);
1494 /* could be handlerized at some point */
1495 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
1496 direct_link_curvemapping(fd, node->storage);
1497 else if(ntree->type==NTREE_COMPOSIT) {
1498 if( ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB))
1499 direct_link_curvemapping(fd, node->storage);
1500 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
1501 ((ImageUser *)node->storage)->ok= 1;
1504 link_list(fd, &node->inputs);
1505 link_list(fd, &node->outputs);
1507 link_list(fd, &ntree->links);
1509 /* and we connect the rest */
1510 for(node= ntree->nodes.first; node; node= node->next) {
1511 node->preview= newimaadr(fd, node->preview);
1513 for(sock= node->inputs.first; sock; sock= sock->next)
1514 sock->link= newdataadr(fd, sock->link);
1515 for(sock= node->outputs.first; sock; sock= sock->next)
1516 sock->ns.data= NULL;
1518 for(link= ntree->links.first; link; link= link->next) {
1519 link->fromnode= newdataadr(fd, link->fromnode);
1520 link->tonode= newdataadr(fd, link->tonode);
1521 link->fromsock= newdataadr(fd, link->fromsock);
1522 link->tosock= newdataadr(fd, link->tosock);
1525 /* type verification is in lib-link */
1528 /* ************ READ PACKEDFILE *************** */
1530 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1532 PackedFile *pf= newdataadr(fd, oldpf);
1535 pf->data= newdataadr(fd, pf->data);
1541 /* ************ READ IMAGE PREVIEW *************** */
1543 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1545 PreviewImage *prv= newdataadr(fd, old_prv);
1549 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1551 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1559 /* ************ READ SCRIPTLINK *************** */
1561 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1565 for(i=0; i<slink->totscript; i++) {
1566 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1570 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1572 slink->scripts= newdataadr(fd, slink->scripts);
1573 test_pointer_array(fd, (void **)&slink->scripts);
1575 slink->flag= newdataadr(fd, slink->flag);
1577 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1580 for(a=0; a<slink->totscript; a++) {
1581 SWITCH_SHORT(slink->flag[a]);
1586 /* ************ READ ARMATURE ***************** */
1588 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1590 bActionStrip *strip;
1591 bActionModifier *amod;
1593 for (strip=striplist->first; strip; strip=strip->next){
1594 strip->object = newlibadr(fd, id->lib, strip->object);
1595 strip->act = newlibadr_us(fd, id->lib, strip->act);
1596 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1597 for(amod= strip->modifiers.first; amod; amod= amod->next)
1598 amod->ob= newlibadr(fd, id->lib, amod->ob);
1602 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1604 bConstraintChannel *chan;
1606 for (chan=chanbase->first; chan; chan=chan->next){
1607 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1611 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
1615 for (con = conlist->first; con; con=con->next) {
1616 /* patch for error introduced by changing constraints (dunno how) */
1617 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
1618 if(con->data==NULL) {
1619 con->type= CONSTRAINT_TYPE_NULL;
1622 switch (con->type) {
1623 case CONSTRAINT_TYPE_PYTHON:
1625 bPythonConstraint *data= (bPythonConstraint*)con->data;
1626 bConstraintTarget *ct;
1628 for (ct= data->targets.first; ct; ct= ct->next)
1629 ct->tar = newlibadr(fd, id->lib, ct->tar);
1631 data->text = newlibadr(fd, id->lib, data->text);
1632 //IDP_LibLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1635 case CONSTRAINT_TYPE_ACTION:
1637 bActionConstraint *data;
1638 data= ((bActionConstraint*)con->data);
1639 data->tar = newlibadr(fd, id->lib, data->tar);
1640 data->act = newlibadr(fd, id->lib, data->act);
1643 case CONSTRAINT_TYPE_LOCLIKE:
1645 bLocateLikeConstraint *data;
1646 data= ((bLocateLikeConstraint*)con->data);
1647 data->tar = newlibadr(fd, id->lib, data->tar);
1650 case CONSTRAINT_TYPE_ROTLIKE:
1652 bRotateLikeConstraint *data;
1653 data= ((bRotateLikeConstraint*)con->data);
1654 data->tar = newlibadr(fd, id->lib, data->tar);
1657 case CONSTRAINT_TYPE_SIZELIKE:
1659 bSizeLikeConstraint *data;
1660 data= ((bSizeLikeConstraint*)con->data);
1661 data->tar = newlibadr(fd, id->lib, data->tar);
1664 case CONSTRAINT_TYPE_KINEMATIC:
1666 bKinematicConstraint *data;
1667 data = ((bKinematicConstraint*)con->data);
1668 data->tar = newlibadr(fd, id->lib, data->tar);
1669 data->poletar = newlibadr(fd, id->lib, data->poletar);
1672 case CONSTRAINT_TYPE_TRACKTO:
1674 bTrackToConstraint *data;
1675 data = ((bTrackToConstraint*)con->data);
1676 data->tar = newlibadr(fd, id->lib, data->tar);
1679 case CONSTRAINT_TYPE_MINMAX:
1681 bMinMaxConstraint *data;
1682 data = ((bMinMaxConstraint*)con->data);
1683 data->tar = newlibadr(fd, id->lib, data->tar);
1686 case CONSTRAINT_TYPE_LOCKTRACK:
1688 bLockTrackConstraint *data;
1689 data= ((bLockTrackConstraint*)con->data);
1690 data->tar = newlibadr(fd, id->lib, data->tar);
1693 case CONSTRAINT_TYPE_FOLLOWPATH:
1695 bFollowPathConstraint *data;
1696 data= ((bFollowPathConstraint*)con->data);
1697 data->tar = newlibadr(fd, id->lib, data->tar);
1700 case CONSTRAINT_TYPE_STRETCHTO:
1702 bStretchToConstraint *data;
1703 data= ((bStretchToConstraint*)con->data);
1704 data->tar = newlibadr(fd, id->lib, data->tar);
1707 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
1709 bRigidBodyJointConstraint *data;
1710 data= ((bRigidBodyJointConstraint*)con->data);
1711 data->tar = newlibadr(fd, id->lib, data->tar);
1714 case CONSTRAINT_TYPE_CLAMPTO:
1716 bClampToConstraint *data;
1717 data= ((bClampToConstraint*)con->data);
1718 data->tar = newlibadr(fd, id->lib, data->tar);
1721 case CONSTRAINT_TYPE_CHILDOF:
1723 bChildOfConstraint *data;
1724 data= ((bChildOfConstraint*)con->data);
1725 data->tar = newlibadr(fd, id->lib, data->tar);
1728 case CONSTRAINT_TYPE_TRANSFORM:
1730 bTransformConstraint *data;
1731 data= ((bTransformConstraint*)con->data);
1732 data->tar = newlibadr(fd, id->lib, data->tar);
1735 case CONSTRAINT_TYPE_NULL:
1741 static void direct_link_constraints(FileData *fd, ListBase *lb)
1746 for (cons=lb->first; cons; cons=cons->next) {
1747 cons->data = newdataadr(fd, cons->data);
1748 if (cons->type == CONSTRAINT_TYPE_PYTHON) {
1749 bPythonConstraint *data= cons->data;
1750 link_list(fd, &data->targets);
1751 data->prop = newdataadr(fd, data->prop);
1752 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1757 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
1759 bPoseChannel *pchan;
1760 bArmature *arm= ob->data;
1766 /* always rebuild to match proxy or lib changes */
1767 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
1769 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
1770 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
1772 /* hurms... loop in a loop, but yah... later... (ton) */
1773 pchan->bone= get_named_bone(arm, pchan->name);
1775 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
1776 if(pchan->bone==NULL)
1778 else if(ob->id.lib==NULL && arm->id.lib) {
1779 /* local pose selection copied to armature, bit hackish */
1780 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1781 pchan->bone->flag |= pchan->selectflag;
1786 ob->recalc= OB_RECALC;
1787 pose->flag |= POSE_RECALC;
1791 static void lib_link_armature(FileData *fd, Main *main)
1795 arm= main->armature.first;
1798 if(arm->id.flag & LIB_NEEDLINK) {
1799 arm->id.flag -= LIB_NEEDLINK;
1805 static void lib_link_action(FileData *fd, Main *main)
1808 bActionChannel *chan;
1810 act= main->action.first;
1812 if(act->id.flag & LIB_NEEDLINK) {
1813 act->id.flag -= LIB_NEEDLINK;
1815 for (chan=act->chanbase.first; chan; chan=chan->next) {
1816 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1817 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1825 static void direct_link_bones(FileData *fd, Bone* bone)
1829 bone->parent= newdataadr(fd, bone->parent);
1831 link_list(fd, &bone->childbase);
1833 for (child=bone->childbase.first; child; child=child->next) {
1834 direct_link_bones(fd, child);
1839 static void direct_link_action(FileData *fd, bAction *act)
1841 bActionChannel *achan;
1843 link_list(fd, &act->chanbase);
1845 for (achan = act->chanbase.first; achan; achan=achan->next)
1846 link_list(fd, &achan->constraintChannels);
1850 static void direct_link_armature(FileData *fd, bArmature *arm)
1854 link_list(fd, &arm->bonebase);
1856 bone=arm->bonebase.first;
1858 direct_link_bones(fd, bone);
1863 /* ************ READ CAMERA ***************** */
1865 static void lib_link_camera(FileData *fd, Main *main)
1869 ca= main->camera.first;
1871 if(ca->id.flag & LIB_NEEDLINK) {
1873 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo);
1875 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
1877 lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
1879 ca->id.flag -= LIB_NEEDLINK;
1885 static void direct_link_camera(FileData *fd, Camera *ca)
1887 direct_link_scriptlink(fd, &ca->scriptlink);
1891 /* ************ READ LAMP ***************** */
1893 static void lib_link_lamp(FileData *fd, Main *main)
1899 la= main->lamp.first;
1901 if(la->id.flag & LIB_NEEDLINK) {
1903 for(a=0; a<MAX_MTEX; a++) {
1906 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
1907 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
1911 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo);
1913 lib_link_scriptlink(fd, &la->id, &la->scriptlink);
1915 la->id.flag -= LIB_NEEDLINK;
1921 static void direct_link_lamp(FileData *fd, Lamp *la)
1925 direct_link_scriptlink(fd, &la->scriptlink);
1927 for(a=0; a<MAX_MTEX; a++) {
1928 la->mtex[a]= newdataadr(fd, la->mtex[a]);
1931 la->curfalloff= newdataadr(fd, la->curfalloff);
1933 direct_link_curvemapping(fd, la->curfalloff);
1935 la->preview = direct_link_preview_image(fd, la->preview);
1938 /* ************ READ keys ***************** */
1940 static void lib_link_key(FileData *fd, Main *main)
1944 key= main->key.first;
1946 if(key->id.flag & LIB_NEEDLINK) {
1948 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo);
1949 key->from= newlibadr(fd, key->id.lib, key->from);
1951 key->id.flag -= LIB_NEEDLINK;
1957 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
1960 char *data, *poin, *cp;
1962 elemsize= key->elemsize;
1965 for(a=0; a<kb->totelem; a++) {
1970 while( cp[0] ) { /* cp[0]==amount */
1972 switch(cp[1]) { /* cp[1]= type */
1978 SWITCH_INT((*poin));
1991 static void direct_link_key(FileData *fd, Key *key)
1995 link_list(fd, &(key->block));
1997 key->refkey= newdataadr(fd, key->refkey);
1999 kb= key->block.first;
2002 kb->data= newdataadr(fd, kb->data);
2004 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2005 switch_endian_keyblock(key, kb);
2011 /* ************ READ mball ***************** */
2013 static void lib_link_mball(FileData *fd, Main *main)
2018 mb= main->mball.first;
2020 if(mb->id.flag & LIB_NEEDLINK) {
2022 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2024 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo);
2026 mb->id.flag -= LIB_NEEDLINK;
2032 static void direct_link_mball(FileData *fd, MetaBall *mb)
2034 mb->mat= newdataadr(fd, mb->mat);
2035 test_pointer_array(fd, (void **)&mb->mat);
2037 link_list(fd, &(mb->elems));
2039 mb->disp.first= mb->disp.last= 0;
2044 /* ************ READ WORLD ***************** */
2046 static void lib_link_world(FileData *fd, Main *main)
2052 wrld= main->world.first;
2054 if(wrld->id.flag & LIB_NEEDLINK) {
2056 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo);
2058 for(a=0; a<MAX_MTEX; a++) {
2059 mtex= wrld->mtex[a];
2061 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2062 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2066 lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
2068 wrld->id.flag -= LIB_NEEDLINK;
2070 wrld= wrld->id.next;
2074 static void direct_link_world(FileData *fd, World *wrld)
2078 direct_link_scriptlink(fd, &wrld->scriptlink);
2080 for(a=0; a<MAX_MTEX; a++) {
2081 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2083 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2087 /* ************ READ IPO ***************** */
2089 static void lib_link_ipo(FileData *fd, Main *main)
2093 ipo= main->ipo.first;
2095 if(ipo->id.flag & LIB_NEEDLINK) {
2097 for(icu= ipo->curve.first; icu; icu= icu->next) {
2099 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
2101 ipo->id.flag -= LIB_NEEDLINK;
2107 static void direct_link_ipo(FileData *fd, Ipo *ipo)
2111 link_list(fd, &(ipo->curve));
2112 icu= ipo->curve.first;
2114 icu->bezt= newdataadr(fd, icu->bezt);
2115 icu->bp= newdataadr(fd, icu->bp);
2116 icu->driver= newdataadr(fd, icu->driver);
2121 /* ************ READ VFONT ***************** */
2123 static void lib_link_vfont(FileData *fd, Main *main)
2127 vf= main->vfont.first;
2129 if(vf->id.flag & LIB_NEEDLINK) {
2130 vf->id.flag -= LIB_NEEDLINK;
2136 static void direct_link_vfont(FileData *fd, VFont *vf)
2139 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2142 /* ************ READ TEXT ****************** */
2144 static void lib_link_text(FileData *fd, Main *main)
2148 text= main->text.first;
2150 if(text->id.flag & LIB_NEEDLINK) {
2151 text->id.flag -= LIB_NEEDLINK;
2153 text= text->id.next;
2157 static void direct_link_text(FileData *fd, Text *text)
2161 text->name= newdataadr(fd, text->name);
2164 text->undo_len= TXT_INIT_UNDO;
2165 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2167 text->compiled= NULL;
2170 if(text->flags & TXT_ISEXT) {
2175 link_list(fd, &text->lines);
2177 text->curl= newdataadr(fd, text->curl);
2178 text->sell= newdataadr(fd, text->sell);
2180 ln= text->lines.first;
2182 ln->line= newdataadr(fd, ln->line);
2185 if (ln->len != (int) strlen(ln->line)) {
2186 printf("Error loading text, line lengths differ\n");
2187 ln->len = strlen(ln->line);
2193 text->flags = (text->flags|TXT_ISTMP) & ~TXT_ISEXT;
2198 /* ************ READ IMAGE ***************** */
2200 static void lib_link_image(FileData *fd, Main *main)
2204 ima= main->image.first;
2206 if(ima->id.flag & LIB_NEEDLINK) {
2207 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2209 ima->id.flag -= LIB_NEEDLINK;
2215 static void link_ibuf_list(FileData *fd, ListBase *lb)
2219 if(lb->first==NULL) return;
2221 lb->first= newimaadr(fd, lb->first);
2225 ln->next= newimaadr(fd, ln->next);
2233 static void direct_link_image(FileData *fd, Image *ima)
2235 /* for undo system, pointers could be restored */
2237 link_ibuf_list(fd, &ima->ibufs);
2239 ima->ibufs.first= ima->ibufs.last= NULL;
2241 /* if not restored, we keep the binded opengl index */
2242 if(ima->ibufs.first==NULL)
2249 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2250 ima->preview = direct_link_preview_image(fd, ima->preview);
2255 /* ************ READ CURVE ***************** */
2257 static void lib_link_curve(FileData *fd, Main *main)
2262 cu= main->curve.first;
2264 if(cu->id.flag & LIB_NEEDLINK) {
2266 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2268 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2269 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2270 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2271 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2272 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2273 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2274 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2276 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo);
2277 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2279 cu->id.flag -= LIB_NEEDLINK;
2286 static void switch_endian_knots(Nurb *nu)
2293 SWITCH_INT(nu->knotsu[len]);
2299 SWITCH_INT(nu->knotsv[len]);
2304 static void direct_link_curve(FileData *fd, Curve *cu)
2309 cu->mat= newdataadr(fd, cu->mat);
2310 test_pointer_array(fd, (void **)&cu->mat);
2311 cu->str= newdataadr(fd, cu->str);
2312 cu->strinfo= newdataadr(fd, cu->strinfo);
2313 cu->tb= newdataadr(fd, cu->tb);
2315 if(cu->vfont==0) link_list(fd, &(cu->nurb));
2317 cu->nurb.first=cu->nurb.last= 0;
2319 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2321 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2328 cu->tb[0].w = cu->linewidth;
2330 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2333 cu->bev.first=cu->bev.last= 0;
2334 cu->disp.first=cu->disp.last= 0;
2339 nu->bezt= newdataadr(fd, nu->bezt);
2340 nu->bp= newdataadr(fd, nu->bp);
2341 nu->knotsu= newdataadr(fd, nu->knotsu);
2342 nu->knotsv= newdataadr(fd, nu->knotsv);
2343 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2345 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2346 switch_endian_knots(nu);
2354 /* ************ READ TEX ***************** */
2356 static void lib_link_texture(FileData *fd, Main *main)
2360 tex= main->tex.first;
2362 if(tex->id.flag & LIB_NEEDLINK) {
2364 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2365 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2366 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2368 tex->id.flag -= LIB_NEEDLINK;
2374 static void direct_link_texture(FileData *fd, Tex *tex)
2376 tex->plugin= newdataadr(fd, tex->plugin);
2378 tex->plugin->handle= 0;
2379 open_plugin_tex(tex->plugin);
2380 /* initialize data for this instance, if an initialization
2383 if (tex->plugin->instance_init)
2384 tex->plugin->instance_init((void *) tex->plugin->data);
2386 tex->coba= newdataadr(fd, tex->coba);
2387 tex->env= newdataadr(fd, tex->env);
2389 tex->env->ima= NULL;
2390 memset(tex->env->cube, 0, 6*sizeof(void *));
2393 tex->preview = direct_link_preview_image(fd, tex->preview);
2400 /* ************ READ MATERIAL ***************** */
2402 static void lib_link_material(FileData *fd, Main *main)
2408 ma= main->mat.first;
2410 if(ma->id.flag & LIB_NEEDLINK) {
2411 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2412 of library blocks that implement this.*/
2413 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2415 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2416 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2418 for(a=0; a<MAX_MTEX; a++) {
2421 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2422 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2425 lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
2428 lib_link_ntree(fd, &ma->id, ma->nodetree);
2430 ma->id.flag -= LIB_NEEDLINK;
2436 static void direct_link_material(FileData *fd, Material *ma)
2440 for(a=0; a<MAX_MTEX; a++) {
2441 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2444 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2445 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2447 direct_link_scriptlink(fd, &ma->scriptlink);
2449 ma->nodetree= newdataadr(fd, ma->nodetree);
2451 direct_link_nodetree(fd, ma->nodetree);
2453 ma->preview = direct_link_preview_image(fd, ma->preview);
2456 /* ************ READ MESH ***************** */
2458 static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
2463 for (i=0; i<totface; i++, tf++) {
2464 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
2465 if(tf->tpage && tf->tpage->id.us==0)
2466 tf->tpage->id.us= 1;
2470 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
2473 for(i=0; i<fdata->totlayer; i++) {
2474 CustomDataLayer *layer = &fdata->layers[i];
2476 if(layer->type == CD_MTFACE)
2477 lib_link_mtface(fd, me, layer->data, totface);
2482 static void lib_link_mesh(FileData *fd, Main *main)
2486 me= main->mesh.first;
2488 if(me->id.flag & LIB_NEEDLINK) {
2491 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2492 of library blocks that implement this.*/
2493 if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2495 /* this check added for python created meshes */
2497 for(i=0; i<me->totcol; i++) {
2498 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
2503 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
2504 me->key= newlibadr_us(fd, me->id.lib, me->key);
2505 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
2507 lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
2508 if(me->mr && me->mr->levels.first)
2509 lib_link_customdata_mtface(fd, me, &me->mr->fdata,
2510 ((MultiresLevel*)me->mr->levels.first)->totface);
2512 me->id.flag -= LIB_NEEDLINK;
2518 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
2525 for (i=0; i<count; i++) {
2526 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
2528 mdverts[i].totweight=0;
2532 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
2536 data->layers= newdataadr(fd, data->layers);
2538 while (i < data->totlayer) {
2539 CustomDataLayer *layer = &data->layers[i];
2541 if (CustomData_verify_versions(data, i)) {
2542 layer->data = newdataadr(fd, layer->data);
2548 static void direct_link_mesh(FileData *fd, Mesh *mesh)
2550 mesh->mat= newdataadr(fd, mesh->mat);
2551 test_pointer_array(fd, (void **)&mesh->mat);
2553 mesh->mvert= newdataadr(fd, mesh->mvert);
2554 mesh->medge= newdataadr(fd, mesh->medge);
2555 mesh->mface= newdataadr(fd, mesh->mface);
2556 mesh->tface= newdataadr(fd, mesh->tface);
2557 mesh->mtface= newdataadr(fd, mesh->mtface);
2558 mesh->mcol= newdataadr(fd, mesh->mcol);
2559 mesh->msticky= newdataadr(fd, mesh->msticky);
2560 mesh->dvert= newdataadr(fd, mesh->dvert);
2562 /* Partial-mesh visibility (do this before using totvert, totface, or totedge!) */
2563 mesh->pv= newdataadr(fd, mesh->pv);
2565 mesh->pv->vert_map= newdataadr(fd, mesh->pv->vert_map);
2566 mesh->pv->edge_map= newdataadr(fd, mesh->pv->edge_map);
2567 mesh->pv->old_faces= newdataadr(fd, mesh->pv->old_faces);
2568 mesh->pv->old_edges= newdataadr(fd, mesh->pv->old_edges);
2571 /* normally direct_link_dverts should be called in direct_link_customdata,
2572 but for backwards compat in do_versions to work we do it here */
2573 direct_link_dverts(fd, mesh->pv ? mesh->pv->totvert : mesh->totvert, mesh->dvert);
2575 direct_link_customdata(fd, &mesh->vdata, mesh->pv ? mesh->pv->totvert : mesh->totvert);
2576 direct_link_customdata(fd, &mesh->edata, mesh->pv ? mesh->pv->totedge : mesh->totedge);
2577 direct_link_customdata(fd, &mesh->fdata, mesh->pv ? mesh->pv->totface : mesh->totface);
2580 mesh->mselect = NULL;
2583 mesh->mr= newdataadr(fd, mesh->mr);
2587 link_list(fd, &mesh->mr->levels);
2588 lvl= mesh->mr->levels.first;
2590 direct_link_customdata(fd, &mesh->mr->vdata, lvl->totvert);
2591 direct_link_dverts(fd, lvl->totvert, CustomData_get(&mesh->mr->vdata, 0, CD_MDEFORMVERT));
2592 direct_link_customdata(fd, &mesh->mr->fdata, lvl->totface);
2594 if(mesh->mr->edge_flags)
2595 mesh->mr->edge_flags= newdataadr(fd, mesh->mr->edge_flags);
2596 if(mesh->mr->edge_creases)
2597 mesh->mr->edge_creases= newdataadr(fd, mesh->mr->edge_creases);
2599 if(!mesh->mr->edge_flags)
2600 mesh->mr->edge_flags= MEM_callocN(sizeof(short)*lvl->totedge, "Multires Edge Flags");
2601 if(!mesh->mr->edge_creases)
2602 mesh->mr->edge_creases= MEM_callocN(sizeof(char)*lvl->totedge, "Multires Edge Creases");
2604 mesh->mr->verts = newdataadr(fd, mesh->mr->verts);
2606 for(; lvl; lvl= lvl->next) {
2607 lvl->verts= newdataadr(fd, lvl->verts);
2608 lvl->faces= newdataadr(fd, lvl->faces);
2609 lvl->edges= newdataadr(fd, lvl->edges);
2610 lvl->colfaces= newdataadr(fd, lvl->colfaces);
2611 lvl->edge_boundary_states= NULL;
2612 lvl->vert_face_map = lvl->vert_edge_map = NULL;
2617 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
2618 TFace *tf= mesh->tface;
2621 for (i=0; i< (mesh->pv ? mesh->pv->totface : mesh->totface); i++, tf++) {
2622 SWITCH_INT(tf->col[0]);
2623 SWITCH_INT(tf->col[1]);
2624 SWITCH_INT(tf->col[2]);
2625 SWITCH_INT(tf->col[3]);
2630 /* ************ READ LATTICE ***************** */
2632 static void lib_link_latt(FileData *fd, Main *main)
2636 lt= main->latt.first;
2638 if(lt->id.flag & LIB_NEEDLINK) {
2640 lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo);
2641 lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
2643 lt->id.flag -= LIB_NEEDLINK;
2649 static void direct_link_latt(FileData *fd, Lattice *lt)
2651 lt->def= newdataadr(fd, lt->def);
2653 lt->dvert= newdataadr(fd, lt->dvert);
2654 direct_link_dverts(fd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
2658 /* ************ READ OBJECT ***************** */
2660 static void lib_link_modifiers__linkModifiers(void *userData, Object *ob,
2663 FileData *fd = userData;
2665 *idpoin = newlibadr(fd, ob->id.lib, *idpoin);
2666 /* hardcoded bad exception; non-object modifier data gets user count (texture, displace) */
2667 if(*idpoin && GS((*idpoin)->name)!=ID_OB)
2670 static void lib_link_modifiers(FileData *fd, Object *ob)
2672 modifiers_foreachIDLink(ob, lib_link_modifiers__linkModifiers, fd);
2675 static void lib_link_object(FileData *fd, Main *main)
2685 ob= main->object.first;
2687 if(ob->id.flag & LIB_NEEDLINK) {
2688 if (ob->id.properties) IDP_LibLinkProperty(ob->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2690 ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
2691 ob->track= newlibadr(fd, ob->id.lib, ob->track);
2692 ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
2693 ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
2694 ob->dup_group= newlibadr_us(fd, ob->id.lib, ob->dup_group);
2696 ob->proxy= newlibadr_us(fd, ob->id.lib, ob->proxy);
2698 /* paranoia check, actually a proxy_from pointer should never be written... */
2699 if(ob->proxy->id.lib==NULL) {
2700 ob->proxy->proxy_from= NULL;
2704 /* this triggers object_update to always use a copy */
2705 ob->proxy->proxy_from= ob;
2706 /* force proxy updates after load/undo, a bit weak */
2707 ob->recalc= ob->proxy->recalc= OB_RECALC;
2710 ob->proxy_group= newlibadr(fd, ob->id.lib, ob->proxy_group);
2713 ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
2715 if(ob->data==NULL && poin!=NULL) {
2718 if(ob->id.lib) printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
2719 else printf("Object %s lost data.", ob->id.name+2);
2722 free_pose_channels(ob->pose);
2723 MEM_freeN(ob->pose);
2725 ob->flag &= ~OB_POSEMODE;
2728 for(a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
2730 ob->id.flag -= LIB_NEEDLINK;
2731 /* if id.us==0 a new base will be created later on */
2733 /* WARNING! Also check expand_object(), should reflect the stuff below. */
2734 lib_link_pose(fd, ob, ob->pose);
2735 lib_link_constraints(fd, &ob->id, &ob->constraints);
2736 lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
2737 lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
2739 for(paf= ob->effect.first; paf; paf= paf->next) {
2740 if(paf->type==EFF_PARTICLE) {
2741 paf->group= newlibadr_us(fd, ob->id.lib, paf->group);
2745 sens= ob->sensors.first;
2747 if(ob->id.lib==NULL) { // done in expand_main
2748 for(a=0; a<sens->totlinks; a++) {
2749 sens->links[a]= newglobadr(fd, sens->links[a]);
2752 if(sens->type==SENS_TOUCH) {
2753 bTouchSensor *ts= sens->data;
2754 ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
2756 else if(sens->type==SENS_MESSAGE) {
2757 bMessageSensor *ms= sens->data;
2759 newlibadr(fd, ob->id.lib, ms->fromObject);
2764 cont= ob->controllers.first;
2766 if(ob->id.lib==NULL) { // done in expand_main
2767 for(a=0; a<cont->totlinks; a++) {
2768 cont->links[a]= newglobadr(fd, cont->links[a]);
2771 if(cont->type==CONT_PYTHON) {
2772 bPythonCont *pc= cont->data;
2773 pc->text= newlibadr(fd, ob->id.lib, pc->text);
2781 act= ob->actuators.first;
2783 if(act->type==ACT_SOUND) {
2784 bSoundActuator *sa= act->data;
2785 sa->sound= newlibadr_us(fd, ob->id.lib, sa->sound);
2787 else if(act->type==ACT_CD) {
2788 /* bCDActuator *cda= act->data; */
2790 else if(act->type==ACT_GAME) {
2791 /* bGameActuator *ga= act->data; */
2793 else if(act->type==ACT_CAMERA) {
2794 bCameraActuator *ca= act->data;
2795 ca->ob= newlibadr(fd, ob->id.lib, ca->ob);
2797 /* leave this one, it's obsolete but necessary to read for conversion */
2798 else if(act->type==ACT_ADD_OBJECT) {
2799 bAddObjectActuator *eoa= act->data;
2800 if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2802 else if(act->type==ACT_EDIT_OBJECT) {
2803 bEditObjectActuator *eoa= act->data;
2808 eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2809 eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
2812 else if(act->type==ACT_SCENE) {
2813 bSceneActuator *sa= act->data;
2814 sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
2815 sa->scene= newlibadr(fd, ob->id.lib, sa->scene);
2817 else if(act->type==ACT_ACTION) {
2818 bActionActuator *aa= act->data;
2819 aa->act= newlibadr(fd, ob->id.lib, aa->act);
2821 else if(act->type==ACT_PROPERTY) {
2822 bPropertyActuator *pa= act->data;
2823 pa->ob= newlibadr(fd, ob->id.lib, pa->ob);
2825 else if(act->type==ACT_MESSAGE) {
2826 bMessageActuator *ma= act->data;
2827 ma->toObject= newlibadr(fd, ob->id.lib, ma->toObject);
2832 if(ob->fluidsimSettings) {
2833 ob->fluidsimSettings->ipo = newlibadr_us(fd, ob->id.lib, ob->fluidsimSettings->ipo);
2836 lib_link_scriptlink(fd, &ob->id, &ob->scriptlink);
2837 lib_link_modifiers(fd, ob);
2842 if(warn) error("WARNING IN CONSOLE");
2846 static void direct_link_pose(FileData *fd, bPose *pose) {
2848 bPoseChannel *pchan;
2853 link_list(fd, &pose->chanbase);
2855 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2857 pchan->parent= newdataadr(fd, pchan->parent);
2858 pchan->child= newdataadr(fd, pchan->child);
2859 direct_link_constraints(fd, &pchan->constraints);
2860 pchan->iktree.first= pchan->iktree.last= NULL;
2866 static void direct_link_modifiers(FileData *fd, ListBase *lb)
2872 for (md=lb->first; md; md=md->next) {
2875 /* if modifiers disappear, or for upward compatibility */
2876 if(NULL==modifierType_getInfo(md->type))
2877 md->type= eModifierType_None;
2879 if (md->type==eModifierType_Subsurf) {
2880 SubsurfModifierData *smd = (SubsurfModifierData*) md;
2882 smd->emCache = smd->mCache = 0;
2884 else if (md->type==eModifierType_Cloth) {
2885 ClothModifierData *clmd = (ClothModifierData*) md;
2887 clmd->clothObject = NULL;
2889 clmd->sim_parms= newdataadr(fd, clmd->sim_parms);
2890 clmd->coll_parms= newdataadr(fd, clmd->coll_parms);
2893 else if (md->type==eModifierType_Collision) {
2894 CollisionModifierData *collmd = (CollisionModifierData*) md;
2897 collmd->xnew = NULL;
2898 collmd->current_x = NULL;
2899 collmd->current_xnew = NULL;
2901 collmd->numverts = 0;
2902 collmd->tree = NULL;
2904 else if (md->type==eModifierType_Hook) {
2905 HookModifierData *hmd = (HookModifierData*) md;
2907 hmd->indexar= newdataadr(fd, hmd->indexar);
2908 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2910 for(a=0; a<hmd->totindex; a++) {
2911 SWITCH_INT(hmd->indexar[a]);
2915 else if (md->type==eModifierType_MeshDeform) {
2916 MeshDeformModifierData *mmd = (MeshDeformModifierData*) md;
2918 mmd->bindweights= newdataadr(fd, mmd->bindweights);
2919 mmd->bindcos= newdataadr(fd, mmd->bindcos);
2920 mmd->dyngrid= newdataadr(fd, mmd->dyngrid);
2921 mmd->dyninfluences= newdataadr(fd, mmd->dyninfluences);
2922 mmd->dynverts= newdataadr(fd, mmd->dynverts);
2924 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2927 if(mmd->bindweights)
2928 for(a=0; a<mmd->totcagevert*mmd->totvert; a++)
2929 SWITCH_INT(mmd->bindweights[a])
2931 for(a=0; a<mmd->totcagevert*3; a++)
2932 SWITCH_INT(mmd->bindcos[a])
2934 for(a=0; a<mmd->totvert; a++)
2935 SWITCH_INT(mmd->dynverts[a])
2941 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
2943 bActionStrip *strip;
2945 link_list(fd, strips);
2947 for(strip= strips->first; strip; strip= strip->next)
2948 link_list(fd, &strip->modifiers);
2951 static void direct_link_object(FileData *fd, Object *ob)
2960 /* weak weak... this was only meant as draw flag, now is used in give_base too */
2961 ob->flag &= ~OB_FROMGROUP;
2963 ob->disp.first=ob->disp.last= NULL;
2965 ob->pose= newdataadr(fd, ob->pose);
2966 direct_link_pose(fd, ob->pose);
2968 link_list(fd, &ob->defbase);
2969 direct_link_nlastrips(fd, &ob->nlastrips);
2970 link_list(fd, &ob->constraintChannels);
2972 direct_link_scriptlink(fd, &ob->scriptlink);
2974 ob->mat= newdataadr(fd, ob->mat);
2975 test_pointer_array(fd, (void **)&ob->mat);
2977 /* do it here, below old data gets converted */
2978 direct_link_modifiers(fd, &ob->modifiers);
2980 link_list(fd, &ob->effect);
2981 paf= ob->effect.first;
2983 if(paf->type==EFF_PARTICLE) {
2986 if(paf->type==EFF_WAVE) {
2987 WaveEff *wav = (WaveEff*) paf;
2988 PartEff *next = paf->next;
2989 WaveModifierData *wmd = (WaveModifierData*) modifier_new(eModifierType_Wave);
2991 wmd->damp = wav->damp;
2992 wmd->flag = wav->flag;
2993 wmd->height = wav->height;
2994 wmd->lifetime = wav->lifetime;
2995 wmd->narrow = wav->narrow;
2996 wmd->speed = wav->speed;
2997 wmd->startx = wav->startx;
2998 wmd->starty = wav->startx;
2999 wmd->timeoffs = wav->timeoffs;
3000 wmd->width = wav->width;
3002 BLI_addtail(&ob->modifiers, wmd);
3004 BLI_remlink(&ob->effect, paf);
3010 if(paf->type==EFF_BUILD) {
3011 BuildEff *baf = (BuildEff*) paf;
3012 PartEff *next = paf->next;
3013 BuildModifierData *bmd = (BuildModifierData*) modifier_new(eModifierType_Build);
3015 bmd->start = baf->sfra;
3016 bmd->length = baf->len;
3020 BLI_addtail(&ob->modifiers, bmd);
3022 BLI_remlink(&ob->effect, paf);
3031 ob->pd= newdataadr(fd, ob->pd);
3032 ob->soft= newdataadr(fd, ob->soft);
3034 SoftBody *sb= ob->soft;
3036 sb->bpoint= NULL; // init pointers so it gets rebuilt nicely
3040 sb->keys= newdataadr(fd, sb->keys);
3041 test_pointer_array(fd, (void **)&sb->keys);
3043 for(a=0; a<sb->totkey; a++) {
3044 sb->keys[a]= newdataadr(fd, sb->keys[a]);
3048 ob->fluidsimSettings= newdataadr(fd, ob->fluidsimSettings); /* NT */
3049 if(ob->fluidsimSettings) {
3050 // reinit mesh pointers
3051 ob->fluidsimSettings->orgMesh = NULL; //ob->data;
3052 ob->fluidsimSettings->meshSurface = NULL;
3053 ob->fluidsimSettings->meshBB = NULL;
3054 ob->fluidsimSettings->meshSurfNormals = NULL;
3057 link_list(fd, &ob->prop);
3058 prop= ob->prop.first;
3060 prop->poin= newdataadr(fd, prop->poin);
3061 if(prop->poin==0) prop->poin= &prop->data;
3065 link_list(fd, &ob->sensors);
3066 sens= ob->sensors.first;
3068 sens->data= newdataadr(fd, sens->data);
3069 sens->links= newdataadr(fd, sens->links);
3070 test_pointer_array(fd, (void **)&sens->links);
3074 direct_link_constraints(fd, &ob->constraints);
3076 link_glob_list(fd, &ob->controllers);
3077 cont= ob->controllers.first;
3079 cont->data= newdataadr(fd, cont->data);
3080 cont->links= newdataadr(fd, cont->links);
3081 test_pointer_array(fd, (void **)&cont->links);
3085 link_glob_list(fd, &ob->actuators);
3086 act= ob->actuators.first;
3088 act->data= newdataadr(fd, act->data);
3092 link_list(fd, &ob->hooks);
3093 while (ob->hooks.first) {
3094 ObHook *hook = ob->hooks.first;
3095 HookModifierData *hmd = (HookModifierData*) modifier_new(eModifierType_Hook);
3097 hook->indexar= newdataadr(fd, hook->indexar);
3098 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
3100 for(a=0; a<hook->totindex; a++) {
3101 SWITCH_INT(hook->indexar[a]);
3105 /* Do conversion here because if we have loaded
3106 * a hook we need to make sure it gets converted
3107 * and free'd, regardless of version.
3109 VECCOPY(hmd->cent, hook->cent);
3110 hmd->falloff = hook->falloff;
3111 hmd->force = hook->force;
3112 hmd->indexar = hook->indexar;
3113 hmd->object = hook->parent;
3114 memcpy(hmd->parentinv, hook->parentinv, sizeof(hmd->parentinv));
3115 hmd->totindex = hook->totindex;
3117 BLI_addhead(&ob->modifiers, hmd);
3118 BLI_remlink(&ob->hooks, hook);
3124 ob->derivedDeform= NULL;
3125 ob->derivedFinal= NULL;
3128 /* ************ READ SCENE ***************** */
3130 static void lib_link_scene(FileData *fd, Main *main)
3136 SceneRenderLayer *srl;
3139 sce= main->scene.first;
3141 if(sce->id.flag & LIB_NEEDLINK) {
3142 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3143 of library blocks that implement this.*/
3144 if (sce->id.properties) IDP_LibLinkProperty(sce->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3146 sce->camera= newlibadr(fd, sce->id.lib, sce->camera);
3147 sce->world= newlibadr_us(fd, sce->id.lib, sce->world);
3148 sce->set= newlibadr(fd, sce->id.lib, sce->set);
3149 sce->ima= newlibadr_us(fd, sce->id.lib, sce->ima);
3150 sce->toolsettings->imapaint.brush=
3151 newlibadr_us(fd, sce->id.lib, sce->toolsettings->imapaint.brush);
3153 /* Sculptdata textures */
3154 for(a=0; a<MAX_MTEX; ++a) {
3155 MTex *mtex= sce->sculptdata.mtex[a];
3157 mtex->tex= newlibadr_us(fd, sce->id.lib, mtex->tex);
3160 for(base= sce->base.first; base; base= next) {
3163 /* base->object= newlibadr_us(fd, sce->id.lib, base->object); */
3164 base->object= newlibadr_us(fd, sce->id.lib, base->object);
3166 /* when save during radiotool, needs cleared */
3167 base->flag &= ~OB_RADIO;
3169 if(base->object==NULL) {
3170 printf("LIB ERROR: base removed\n");