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"
43 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
44 #include <stdlib.h> // for getenv atoi
45 #include <fcntl.h> // for open
46 #include <string.h> // for strrchr strncmp strstr
47 #include <math.h> // for fabs
50 #include <unistd.h> // for read close
51 #include <sys/param.h> // for MAXPATHLEN
53 #include <io.h> // for open close read
58 #include "DNA_action_types.h"
59 #include "DNA_armature_types.h"
61 #include "DNA_actuator_types.h"
62 #include "DNA_brush_types.h"
63 #include "DNA_camera_types.h"
64 #include "DNA_cloth_types.h"
65 #include "DNA_color_types.h"
66 #include "DNA_controller_types.h"
67 #include "DNA_constraint_types.h"
68 #include "DNA_curve_types.h"
69 #include "DNA_customdata_types.h"
70 #include "DNA_effect_types.h"
71 #include "DNA_fileglobal_types.h"
72 #include "DNA_group_types.h"
73 #include "DNA_gpencil_types.h"
74 #include "DNA_ipo_types.h"
75 #include "DNA_image_types.h"
76 #include "DNA_key_types.h"
77 #include "DNA_lattice_types.h"
78 #include "DNA_lamp_types.h"
79 #include "DNA_meta_types.h"
80 #include "DNA_material_types.h"
81 #include "DNA_mesh_types.h"
82 #include "DNA_meshdata_types.h"
83 #include "DNA_modifier_types.h"
84 #include "DNA_nla_types.h"
85 #include "DNA_node_types.h"
86 #include "DNA_object_types.h"
87 #include "DNA_object_force.h"
88 #include "DNA_object_fluidsim.h" // NT
89 #include "DNA_oops_types.h"
90 #include "DNA_object_force.h"
91 #include "DNA_packedFile_types.h"
92 #include "DNA_particle_types.h"
93 #include "DNA_property_types.h"
94 #include "DNA_text_types.h"
95 #include "DNA_view3d_types.h"
96 #include "DNA_screen_types.h"
97 #include "DNA_sensor_types.h"
98 #include "DNA_sdna_types.h"
99 #include "DNA_scene_types.h"
100 #include "DNA_sequence_types.h"
101 #include "DNA_sound_types.h"
102 #include "DNA_space_types.h"
103 #include "DNA_texture_types.h"
104 #include "DNA_userdef_types.h"
105 #include "DNA_vfont_types.h"
106 #include "DNA_world_types.h"
108 #include "MEM_guardedalloc.h"
109 #include "BLI_blenlib.h"
110 #include "BLI_arithb.h"
111 #include "BLI_storage_types.h" // for relname flags
113 #include "BDR_sculptmode.h"
115 #include "BKE_bad_level_calls.h" // for reopen_text build_seqar (from WHILE_SEQ) set_rects_butspace check_imasel_copy
117 #include "BKE_action.h"
118 #include "BKE_armature.h"
119 #include "BKE_cloth.h"
120 #include "BKE_colortools.h"
121 #include "BKE_constraint.h"
122 #include "BKE_curve.h"
123 #include "BKE_customdata.h"
124 #include "BKE_deform.h"
125 #include "BKE_depsgraph.h"
126 #include "BKE_effect.h" /* give_parteff */
127 #include "BKE_global.h" // for G
128 #include "BKE_group.h"
129 #include "BKE_image.h"
130 #include "BKE_key.h" //void set_four_ipo
131 #include "BKE_lattice.h"
132 #include "BKE_library.h" // for wich_libbase
133 #include "BKE_main.h" // for Main
134 #include "BKE_mesh.h" // for ME_ defines (patching)
135 #include "BKE_modifier.h"
136 #include "BKE_multires.h" // for multires_free
137 #include "BKE_node.h" // for tree type defines
138 #include "BKE_object.h"
139 #include "BKE_particle.h"
140 #include "BKE_pointcache.h"
141 #include "BKE_property.h" // for get_ob_property
142 #include "BKE_sca.h" // for init_actuator
143 #include "BKE_scene.h"
144 #include "BKE_softbody.h" // sbNew()
145 #include "BKE_bullet.h" // bsbNew()
146 #include "BKE_sculpt.h"
147 #include "BKE_texture.h" // for open_plugin_tex
148 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
149 #include "BKE_idprop.h"
151 #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
152 #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
153 #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
154 #include "BLO_readfile.h"
155 #include "BLO_undofile.h"
156 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
158 #include "readfile.h"
162 #include "mydevice.h"
168 Remark: still a weak point is the newadress() function, that doesnt solve reading from
169 multiple files at the same time
171 (added remark: oh, i thought that was solved? will look at that... (ton)
174 - Existing Library (Main) push or free
184 - read associated 'direct data'
185 - link direct data (internal and to LibBlock)
187 - read USER data, only when indicated (file is ~/.B.blend)
189 - per Library (per Main)
192 - find LibBlocks and attach IDs to Main
193 - if external LibBlock
195 - or it's already read,
200 - read associated direct data
201 - link direct data (internal and to LibBlock)
203 - per Library with unread LibBlocks
208 - read associated direct data
209 - link direct data (internal and to LibBlock)
212 - link all LibBlocks and indirect pointers to libblocks
213 - initialize FileGlobal and copy pointers to Global
216 /* also occurs in library.c */
217 /* GS reads the memory pointed at in a specific ordering. There are,
218 * however two definitions for it. I have jotted them down here, both,
219 * but I think the first one is actually used. The thing is that
220 * big-endian systems might read this the wrong way round. OTOH, we
221 * constructed the IDs that are read out with this macro explicitly as
222 * well. I expect we'll sort it out soon... */
225 #define GS(a) (*((short *)(a)))
227 /* from misc_util: flip the bytes from x */
228 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
230 // only used here in readfile.c
231 #define SWITCH_LONGINT(a) { \
234 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
235 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
236 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
237 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
241 typedef struct OldNew {
246 typedef struct OldNewMap {
248 int nentries, entriessize;
254 /* local prototypes */
255 static void *read_struct(FileData *fd, BHead *bh, char *blockname);
258 static OldNewMap *oldnewmap_new(void)
260 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
262 onm->entriessize= 1024;
263 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
268 static int verg_oldnewmap(const void *v1, const void *v2)
270 const struct OldNew *x1=v1, *x2=v2;
272 if( x1->old > x2->old) return 1;
273 else if( x1->old < x2->old) return -1;
278 static void oldnewmap_sort(FileData *fd)
280 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
281 fd->libmap->sorted= 1;
284 /* nr is zero for data, and ID code for libdata */
285 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
289 if(oldaddr==NULL || newaddr==NULL) return;
291 if (onm->nentries==onm->entriessize) {
292 int osize= onm->entriessize;
293 OldNew *oentries= onm->entries;
295 onm->entriessize*= 2;
296 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
298 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
302 entry= &onm->entries[onm->nentries++];
304 entry->newp= newaddr;
308 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
312 if (onm->lasthit<onm->nentries-1) {
313 OldNew *entry= &onm->entries[++onm->lasthit];
315 if (entry->old==addr) {
321 for (i=0; i<onm->nentries; i++) {
322 OldNew *entry= &onm->entries[i];
324 if (entry->old==addr) {
335 /* for libdata, nr has ID code, no increment */
336 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
340 if(addr==NULL) return NULL;
342 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
344 OldNew entry_s, *entry;
348 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
352 if (id && (!lib || id->lib)) {
358 for (i=0; i<onm->nentries; i++) {
359 OldNew *entry= &onm->entries[i];
361 if (entry->old==addr) {
364 if (id && (!lib || id->lib)) {
373 static void oldnewmap_free_unused(OldNewMap *onm)
377 for (i=0; i<onm->nentries; i++) {
378 OldNew *entry= &onm->entries[i];
380 MEM_freeN(entry->newp);
386 static void oldnewmap_clear(OldNewMap *onm)
392 static void oldnewmap_free(OldNewMap *onm)
394 MEM_freeN(onm->entries);
400 static void read_libraries(FileData *basefd, ListBase *mainlist);
402 /* ************ help functions ***************** */
404 static void add_main_to_main(Main *mainvar, Main *from)
406 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
409 a= set_listbasepointers(mainvar, lbarray);
410 a= set_listbasepointers(from, fromarray);
412 addlisttolist(lbarray[a], fromarray[a]);
416 void blo_join_main(ListBase *mainlist)
418 Main *tojoin, *mainl;
421 mainl= mainlist->first;
422 while ((tojoin= mainl->next)) {
423 add_main_to_main(mainl, tojoin);
424 BLI_remlink(mainlist, tojoin);
429 static void split_libdata(ListBase *lb, Main *first)
441 if(mainvar->curlib==id->lib) {
442 lbn= wich_libbase(mainvar, GS(id->name));
444 BLI_addtail(lbn, id);
447 mainvar= mainvar->next;
449 if(mainvar==0) printf("error split_libdata\n");
455 void blo_split_main(ListBase *mainlist, Main *main)
457 ListBase *lbarray[MAX_LIBARRAY];
461 mainlist->first= mainlist->last= main;
464 if(main->library.first==NULL)
467 for (lib= main->library.first; lib; lib= lib->id.next) {
468 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
469 libmain->curlib= lib;
470 BLI_addtail(mainlist, libmain);
473 i= set_listbasepointers(main, lbarray);
475 split_libdata(lbarray[i], main->next);
478 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
479 static void cleanup_path(const char *relabase, char *name)
481 char filename[FILE_MAXFILE];
483 BLI_splitdirstring(name, filename);
484 BLI_cleanup_dir(relabase, name);
485 strcat(name, filename);
488 static void read_file_version(FileData *fd, Main *main)
492 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
493 if (bhead->code==GLOB) {
494 FileGlobal *fg= read_struct(fd, bhead, "Global");
496 main->subversionfile= fg->subversion;
497 main->minversionfile= fg->minversion;
498 main->minsubversionfile= fg->minsubversion;
501 else if (bhead->code==ENDB)
508 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *name, const char *relabase)
512 char name1[FILE_MAXDIR+FILE_MAXFILE];
514 strncpy(name1, name, sizeof(name1)-1);
515 cleanup_path(relabase, name1);
516 // printf("blo_find_main: original in %s\n", name);
517 // printf("blo_find_main: converted to %s\n", name1);
519 for (m= mainlist->first; m; m= m->next) {
520 char *libname= (m->curlib)?m->curlib->filename:m->name;
522 if (BLI_streq(name1, libname)) {
523 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
528 m= MEM_callocN(sizeof(Main), "find_main");
529 BLI_addtail(mainlist, m);
531 lib= alloc_libblock(&m->library, ID_LI, "lib");
532 strncpy(lib->name, name, sizeof(lib->name)-1);
533 BLI_strncpy(lib->filename, name1, sizeof(lib->filename));
537 read_file_version(fd, m);
539 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", name);
544 /* ************ FILE PARSING ****************** */
546 static void switch_endian_bh4(BHead4 *bhead)
548 /* the ID_.. codes */
549 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
551 if (bhead->code != ENDB) {
552 SWITCH_INT(bhead->len);
553 SWITCH_INT(bhead->SDNAnr);
554 SWITCH_INT(bhead->nr);
558 static void switch_endian_bh8(BHead8 *bhead)
560 /* the ID_.. codes */
561 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
563 if (bhead->code != ENDB) {
564 SWITCH_INT(bhead->len);
565 SWITCH_INT(bhead->SDNAnr);
566 SWITCH_INT(bhead->nr);
570 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
572 BHead4 *bhead4 = (BHead4 *) bhead;
573 #if defined(WIN32) && !defined(FREE_WINDOWS)
579 bhead4->code= bhead8->code;
580 bhead4->len= bhead8->len;
582 if (bhead4->code != ENDB) {
584 // why is this here ??
585 if (do_endian_swap) {
586 SWITCH_LONGINT(bhead8->old);
589 /* this patch is to avoid a long long being read from not-eight aligned positions
590 is necessary on any modern 64bit architecture) */
591 memcpy(&old, &bhead8->old, 8);
592 bhead4->old = (int) (old >> 3);
594 bhead4->SDNAnr= bhead8->SDNAnr;
595 bhead4->nr= bhead8->nr;
599 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
601 BHead8 *bhead8 = (BHead8 *) bhead;
603 bhead8->code= bhead4->code;
604 bhead8->len= bhead4->len;
606 if (bhead8->code != ENDB) {
607 bhead8->old= bhead4->old;
608 bhead8->SDNAnr= bhead4->SDNAnr;
609 bhead8->nr= bhead4->nr;
613 static BHeadN *get_bhead(FileData *fd)
618 BHeadN *new_bhead = 0;
624 /* not strictly needed but shuts valgrind up
625 * since uninitialized memory gets compared */
626 memset(&bhead8, 0, sizeof(BHead8));
627 memset(&bhead4, 0, sizeof(BHead4));
628 memset(&bhead, 0, sizeof(BHead));
630 // First read the bhead structure.
631 // Depending on the platform the file was written on this can
632 // be a big or little endian BHead4 or BHead8 structure.
634 // As usual 'ENDB' (the last *partial* bhead of the file)
635 // needs some special handling. We don't want to EOF just yet.
637 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
639 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
641 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
642 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
643 switch_endian_bh4(&bhead4);
646 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
647 bh8_from_bh4(&bhead, &bhead4);
649 memcpy(&bhead, &bhead4, sizeof(bhead));
657 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
659 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
660 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
661 switch_endian_bh8(&bhead8);
664 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
665 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
667 memcpy(&bhead, &bhead8, sizeof(bhead));
675 /* make sure people are not trying to pass bad blend files */
676 if (bhead.len < 0) fd->eof = 1;
678 // bhead now contains the (converted) bhead structure. Now read
679 // the associated data and put everything in a BHeadN (creative naming !)
682 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
684 new_bhead->next = new_bhead->prev = 0;
685 new_bhead->bhead = bhead;
687 readsize = fd->read(fd, new_bhead + 1, bhead.len);
689 if (readsize != bhead.len) {
691 MEM_freeN(new_bhead);
701 // We've read a new block. Now add it to the list
705 BLI_addtail(&fd->listbase, new_bhead);
711 BHead *blo_firstbhead(FileData *fd)
717 // Read in a new block if necessary
719 new_bhead = fd->listbase.first;
720 if (new_bhead == 0) {
721 new_bhead = get_bhead(fd);
725 bhead = &new_bhead->bhead;
731 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
733 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
734 BHeadN *prev= bheadn->prev;
736 return prev?&prev->bhead:NULL;
739 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
741 BHeadN *new_bhead = NULL;
745 // bhead is actually a sub part of BHeadN
746 // We calculate the BHeadN pointer from the BHead pointer below
747 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
749 // get the next BHeadN. If it doesn't exist we read in the next one
750 new_bhead = new_bhead->next;
751 if (new_bhead == 0) {
752 new_bhead = get_bhead(fd);
757 // here we do the reverse:
758 // go from the BHeadN pointer to the BHead pointer
759 bhead = &new_bhead->bhead;
765 static void decode_blender_header(FileData *fd)
767 char header[SIZEOFBLENDERHEADER], num[4];
770 // read in the header data
771 readsize = fd->read(fd, header, sizeof(header));
773 if (readsize == sizeof(header)) {
774 if(strncmp(header, "BLENDER", 7) == 0) {
775 int remove_this_endian_test= 1;
777 fd->flags |= FD_FLAGS_FILE_OK;
779 // what size are pointers in the file ?
781 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
782 if (sizeof(void *) != 4) {
783 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
786 if (sizeof(void *) != 8) {
787 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
791 // is the file saved in a different endian
793 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
794 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
797 // get the version number
799 memcpy(num, header+9, 3);
801 fd->fileversion = atoi(num);
806 static int read_file_dna(FileData *fd)
810 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
811 if (bhead->code==DNA1) {
812 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
814 fd->filesdna= dna_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
817 fd->compflags= dna_get_structDNA_compareflags(fd->filesdna, fd->memsdna);
818 /* used to retrieve ID names from (bhead+1) */
819 fd->id_name_offs= dna_elem_offset(fd->filesdna, "ID", "char", "name[]");
823 } else if (bhead->code==ENDB)
830 static int fd_read_from_file(FileData *filedata, void *buffer, int size)
832 int readsize = read(filedata->filedes, buffer, size);
837 filedata->seek += readsize;
843 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, int size)
845 int readsize = gzread(filedata->gzfiledes, buffer, size);
850 filedata->seek += readsize;
856 static int fd_read_from_memory(FileData *filedata, void *buffer, int size)
858 // don't read more bytes then there are available in the buffer
859 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
861 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
862 filedata->seek += readsize;
867 static int fd_read_from_memfile(FileData *filedata, void *buffer, int size)
869 static unsigned int seek= 1<<30; /* the current position */
870 static unsigned int offset= 0; /* size of previous chunks */
871 static MemFileChunk *chunk=NULL;
872 unsigned int chunkoffset, readsize, totread;
874 if(size==0) return 0;
876 if(seek != (unsigned int)filedata->seek) {
877 chunk= filedata->memfile->chunks.first;
881 if(seek + chunk->size > (unsigned) filedata->seek) break;
886 seek= filedata->seek;
893 /* first check if it's on the end if current chunk */
894 if(seek-offset == chunk->size) {
895 offset+= chunk->size;
899 /* debug, should never happen */
901 printf("illegal read, chunk zero\n");
905 chunkoffset= seek-offset;
906 readsize= size-totread;
908 /* data can be spread over multiple chunks, so clamp size
909 * to within this chunk, and then it will read further in
911 if(chunkoffset+readsize > chunk->size)
912 readsize= chunk->size-chunkoffset;
914 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
916 filedata->seek += readsize;
918 } while(totread < size);
926 static FileData *filedata_new(void)
928 extern unsigned char DNAstr[]; /* DNA.c */
930 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
933 fd->gzfiledes = NULL;
935 /* XXX, this doesn't need to be done all the time,
936 * but it keeps us reentrant, remove once we have
937 * a lib that provides a nice lock. - zr
939 fd->memsdna = dna_sdna_from_data(DNAstr, DNAlen, 0);
941 fd->datamap = oldnewmap_new();
942 fd->globmap = oldnewmap_new();
943 fd->libmap = oldnewmap_new();
948 static FileData *blo_decode_and_check(FileData *fd, BlendReadError *error_r)
950 decode_blender_header(fd);
952 if (fd->flags & FD_FLAGS_FILE_OK) {
953 if (!read_file_dna(fd)) {
954 *error_r = BRE_INCOMPLETE;
955 blo_freefiledata(fd);
960 *error_r = BRE_NOT_A_BLEND;
961 blo_freefiledata(fd);
968 /* cannot be called with relative paths anymore! */
969 /* on each new library added, it now checks for the current FileData and expands relativeness */
970 FileData *blo_openblenderfile(char *name, BlendReadError *error_r)
974 gzfile= gzopen(name, "rb");
976 if (NULL == gzfile) {
977 *error_r = BRE_UNABLE_TO_OPEN;
980 FileData *fd = filedata_new();
981 fd->gzfiledes = gzfile;
982 fd->read = fd_read_gzip_from_file;
984 /* needed for library_append and read_libraries */
985 BLI_strncpy(fd->filename, name, sizeof(fd->filename));
987 return blo_decode_and_check(fd, error_r);
991 FileData *blo_openblendermemory(void *mem, int memsize, BlendReadError *error_r)
993 if (!mem || memsize<SIZEOFBLENDERHEADER) {
994 *error_r = mem?BRE_UNABLE_TO_READ:BRE_UNABLE_TO_OPEN;
997 FileData *fd= filedata_new();
999 fd->buffersize= memsize;
1000 fd->read= fd_read_from_memory;
1001 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
1003 return blo_decode_and_check(fd, error_r);
1007 FileData *blo_openblendermemfile(MemFile *memfile, BlendReadError *error_r)
1010 *error_r = BRE_UNABLE_TO_OPEN;
1013 FileData *fd= filedata_new();
1014 fd->memfile= memfile;
1016 fd->read= fd_read_from_memfile;
1017 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
1019 return blo_decode_and_check(fd, error_r);
1024 void blo_freefiledata(FileData *fd)
1028 if (fd->filedes != -1) {
1032 if (fd->gzfiledes != NULL)
1034 gzclose(fd->gzfiledes);
1037 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1038 MEM_freeN(fd->buffer);
1042 // Free all BHeadN data blocks
1043 BLI_freelistN(&fd->listbase);
1046 dna_freestructDNA(fd->memsdna);
1048 dna_freestructDNA(fd->filesdna);
1050 MEM_freeN(fd->compflags);
1053 oldnewmap_free(fd->datamap);
1055 oldnewmap_free(fd->globmap);
1057 oldnewmap_free(fd->imamap);
1058 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1059 oldnewmap_free(fd->libmap);
1061 MEM_freeN(fd->bheadmap);
1067 /* ************ DIV ****************** */
1069 int BLO_has_bfile_extension(char *str)
1071 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1074 /* ************** OLD POINTERS ******************* */
1076 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1078 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1081 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1083 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1086 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1088 if(fd->imamap && adr)
1089 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1094 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1096 return oldnewmap_liblookup(fd->libmap, adr, lib);
1099 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1101 ID *id= newlibadr(fd, lib, adr);
1109 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1113 for (i=0; i<fd->libmap->nentries; i++) {
1114 OldNew *entry= &fd->libmap->entries[i];
1116 if (old==entry->newp && entry->nr==ID_ID) {
1118 if(new) entry->nr= GS( ((ID *)new)->name );
1124 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1128 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1131 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1135 change_idid_adr_fd(fd, old, new);
1140 /* lib linked proxy objects point to our local data, we need
1141 * to clear that pointer before reading the undo memfile since
1142 * the object might be removed, it is set again in reading
1143 * if the local object still exists */
1144 void blo_clear_proxy_pointers_from_lib(FileData *fd)
1146 Object *ob= G.main->object.first;
1148 for(;ob; ob= ob->id.next)
1150 ob->proxy_from= NULL;
1153 /* assumed; G.main still exists */
1154 void blo_make_image_pointer_map(FileData *fd)
1156 Image *ima= G.main->image.first;
1157 Scene *sce= G.main->scene.first;
1159 fd->imamap= oldnewmap_new();
1161 for(;ima; ima= ima->id.next) {
1162 Link *ibuf= ima->ibufs.first;
1163 for(; ibuf; ibuf= ibuf->next)
1164 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1166 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1168 for(; sce; sce= sce->id.next) {
1171 for(node= sce->nodetree->nodes.first; node; node= node->next)
1172 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1177 /* set G.main image ibufs to zero if it has been restored */
1178 /* this works because freeing G.main only happens after this call */
1179 void blo_end_image_pointer_map(FileData *fd)
1181 OldNew *entry= fd->imamap->entries;
1182 Image *ima= G.main->image.first;
1183 Scene *sce= G.main->scene.first;
1186 /* used entries were restored, so we put them to zero */
1187 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1192 for(;ima; ima= ima->id.next) {
1195 /* this mirrors direct_link_image */
1196 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1198 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1199 BLI_remlink(&ima->ibufs, ibuf);
1201 ima->gputexture= NULL;
1205 ima->gputexture= newimaadr(fd, ima->gputexture);
1207 for(; sce; sce= sce->id.next) {
1210 for(node= sce->nodetree->nodes.first; node; node= node->next)
1211 node->preview= newimaadr(fd, node->preview);
1216 /* undo file support: add all library pointers in lookup */
1217 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1219 Main *ptr= mainlist->first;
1220 ListBase *lbarray[MAX_LIBARRAY];
1222 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1223 int i= set_listbasepointers(ptr, lbarray);
1226 for(id= lbarray[i]->first; id; id= id->next)
1227 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1233 /* ********** END OLD POINTERS ****************** */
1234 /* ********** READ FILE ****************** */
1236 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1238 int blocksize, nblocks;
1241 data= (char *)(bhead+1);
1242 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1246 dna_switch_endian_struct(filesdna, bhead->SDNAnr, data);
1252 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1257 /* switch is based on file dna */
1258 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1259 switch_endian_structs(fd->filesdna, bh);
1261 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1262 if(fd->compflags[bh->SDNAnr]==2) {
1263 temp= dna_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1265 temp= MEM_mallocN(bh->len, blockname);
1266 memcpy(temp, (bh+1), bh->len);
1274 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1278 if(lb->first==NULL) return;
1280 lb->first= newdataadr(fd, lb->first);
1284 ln->next= newdataadr(fd, ln->next);
1292 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1297 if(lb->first==0) return;
1298 poin= newdataadr(fd, lb->first);
1300 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1307 poin= newdataadr(fd, ln->next);
1309 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1319 static void test_pointer_array(FileData *fd, void **mat)
1321 #if defined(WIN32) && !defined(FREE_WINDOWS)
1322 __int64 *lpoin, *lmat;
1324 long long *lpoin, *lmat;
1326 int len, *ipoin, *imat;
1328 /* manually convert the pointer array in
1329 * the old dna format to a pointer array in
1330 * the new dna format.
1333 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1335 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1336 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1340 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1341 SWITCH_LONGINT(*lpoin);
1342 *ipoin= (int) ((*lpoin) >> 3);
1350 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1351 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1365 /* ************ READ ID Properties *************** */
1367 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd);
1368 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd);
1370 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, void *fd)
1374 /*since we didn't save the extra buffer, set totallen to len.*/
1375 prop->totallen = prop->len;
1376 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1378 if (switch_endian) {
1379 if (prop->subtype != IDP_DOUBLE) {
1380 for (i=0; i<prop->len; i++) {
1381 SWITCH_INT(((int*)prop->data.pointer)[i]);
1384 for (i=0; i<prop->len; i++) {
1385 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1391 static void IDP_DirectLinkString(IDProperty *prop, int switch_endian, void *fd)
1393 /*since we didn't save the extra string buffer, set totallen to len.*/
1394 prop->totallen = prop->len;
1395 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1398 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, void *fd)
1400 ListBase *lb = &prop->data.group;
1405 /*Link child id properties now*/
1406 for (loop=prop->data.group.first; loop; loop=loop->next) {
1407 IDP_DirectLinkProperty(loop, switch_endian, fd);
1411 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd)
1413 switch (prop->type) {
1415 IDP_DirectLinkGroup(prop, switch_endian, fd);
1418 IDP_DirectLinkString(prop, switch_endian, fd);
1421 IDP_DirectLinkArray(prop, switch_endian, fd);
1424 /*erg, stupid doubles. since I'm storing them
1425 in the same field as int val; val2 in the
1426 IDPropertyData struct, they have to deal with
1427 endianness specifically
1429 in theory, val and val2 would've already been swapped
1430 if switch_endian is true, so we have to first unswap
1431 them then reswap them as a single 64-bit entity.
1434 if (switch_endian) {
1435 SWITCH_INT(prop->data.val);
1436 SWITCH_INT(prop->data.val2);
1437 SWITCH_LONGINT(prop->data.val);
1445 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd)
1449 /* ************ READ Brush *************** */
1450 /* library brush linking after fileread */
1451 static void lib_link_brush(FileData *fd, Main *main)
1457 /* only link ID pointers */
1458 for(brush= main->brush.first; brush; brush= brush->id.next) {
1459 if(brush->id.flag & LIB_NEEDLINK) {
1460 brush->id.flag -= LIB_NEEDLINK;
1462 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1464 for(a=0; a<MAX_MTEX; a++) {
1465 mtex= brush->mtex[a];
1467 mtex->tex= newlibadr_us(fd, brush->id.lib, mtex->tex);
1473 static void direct_link_brush(FileData *fd, Brush *brush)
1475 /* brush itself has been read */
1478 for(a=0; a<MAX_MTEX; a++)
1479 brush->mtex[a]= newdataadr(fd, brush->mtex[a]);
1482 static void direct_link_script(FileData *fd, Script *script)
1485 SCRIPT_SET_NULL(script)
1488 /* ************ READ CurveMapping *************** */
1490 /* cuma itself has been read! */
1491 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1495 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1496 cumap->flag &= ~CUMA_PREMULLED;
1498 for(a=0; a<CM_TOT; a++) {
1499 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1500 cumap->cm[a].table= NULL;
1504 /* ************ READ NODE TREE *************** */
1506 /* singe node tree (also used for material/scene trees), ntree is not NULL */
1507 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
1511 for(node= ntree->nodes.first; node; node= node->next)
1512 node->id= newlibadr_us(fd, id->lib, node->id);
1515 /* library ntree linking after fileread */
1516 static void lib_link_nodetree(FileData *fd, Main *main)
1520 /* only link ID pointers */
1521 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1522 if(ntree->id.flag & LIB_NEEDLINK) {
1523 ntree->id.flag -= LIB_NEEDLINK;
1524 lib_link_ntree(fd, &ntree->id, ntree);
1529 /* verify types for nodes and groups, all data has to be read */
1530 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
1532 static void lib_verify_nodetree(Main *main, int open)
1539 /* this crashes blender on undo/redo
1541 reinit_nodesystem();
1544 /* now create the own typeinfo structs an verify nodes */
1545 /* here we still assume no groups in groups */
1546 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1547 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
1548 ntreeMakeOwnType(ntree); /* for group usage */
1551 /* now verify all types in material trees, groups are set OK now */
1552 for(ma= main->mat.first; ma; ma= ma->id.next) {
1554 ntreeVerifyTypes(ma->nodetree);
1556 /* and scene trees */
1557 for(sce= main->scene.first; sce; sce= sce->id.next) {
1559 ntreeVerifyTypes(sce->nodetree);
1561 /* and texture trees */
1562 for(tx= main->tex.first; tx; tx= tx->id.next) {
1564 ntreeVerifyTypes(tx->nodetree);
1570 /* ntree itself has been read! */
1571 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
1573 /* note: writing and reading goes in sync, for speed */
1578 ntree->init= 0; /* to set callbacks and force setting types */
1579 ntree->owntype= NULL;
1580 ntree->timecursor= NULL;
1582 link_list(fd, &ntree->nodes);
1583 for(node= ntree->nodes.first; node; node= node->next) {
1584 if(node->type == NODE_DYNAMIC) {
1586 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
1587 node->typeinfo= NULL;
1590 node->storage= newdataadr(fd, node->storage);
1593 /* could be handlerized at some point */
1594 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
1595 direct_link_curvemapping(fd, node->storage);
1596 else if(ntree->type==NTREE_COMPOSIT) {
1597 if( ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB))
1598 direct_link_curvemapping(fd, node->storage);
1599 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
1600 ((ImageUser *)node->storage)->ok= 1;
1602 else if( ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) ) {
1603 direct_link_curvemapping(fd, node->storage);
1606 link_list(fd, &node->inputs);
1607 link_list(fd, &node->outputs);
1609 link_list(fd, &ntree->links);
1611 /* and we connect the rest */
1612 for(node= ntree->nodes.first; node; node= node->next) {
1613 node->preview= newimaadr(fd, node->preview);
1615 for(sock= node->inputs.first; sock; sock= sock->next)
1616 sock->link= newdataadr(fd, sock->link);
1617 for(sock= node->outputs.first; sock; sock= sock->next)
1618 sock->ns.data= NULL;
1620 for(link= ntree->links.first; link; link= link->next) {
1621 link->fromnode= newdataadr(fd, link->fromnode);
1622 link->tonode= newdataadr(fd, link->tonode);
1623 link->fromsock= newdataadr(fd, link->fromsock);
1624 link->tosock= newdataadr(fd, link->tosock);
1627 /* set selin and selout */
1628 for(node= ntree->nodes.first; node; node= node->next) {
1629 for(sock= node->inputs.first; sock; sock= sock->next) {
1630 if(sock->flag & SOCK_SEL) {
1635 for(sock= node->outputs.first; sock; sock= sock->next) {
1636 if(sock->flag & SOCK_SEL) {
1637 ntree->selout= sock;
1643 /* type verification is in lib-link */
1646 /* ************ READ PACKEDFILE *************** */
1648 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1650 PackedFile *pf= newdataadr(fd, oldpf);
1653 pf->data= newdataadr(fd, pf->data);
1659 /* ************ READ IMAGE PREVIEW *************** */
1661 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1663 PreviewImage *prv= newdataadr(fd, old_prv);
1667 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1669 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1677 /* ************ READ SCRIPTLINK *************** */
1679 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1683 for(i=0; i<slink->totscript; i++) {
1684 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1688 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1690 slink->scripts= newdataadr(fd, slink->scripts);
1691 test_pointer_array(fd, (void **)&slink->scripts);
1693 slink->flag= newdataadr(fd, slink->flag);
1695 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1698 for(a=0; a<slink->totscript; a++) {
1699 SWITCH_SHORT(slink->flag[a]);
1704 /* ************ READ ARMATURE ***************** */
1706 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1708 bActionStrip *strip;
1709 bActionModifier *amod;
1711 for (strip=striplist->first; strip; strip=strip->next){
1712 strip->object = newlibadr(fd, id->lib, strip->object);
1713 strip->act = newlibadr_us(fd, id->lib, strip->act);
1714 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1715 for(amod= strip->modifiers.first; amod; amod= amod->next)
1716 amod->ob= newlibadr(fd, id->lib, amod->ob);
1720 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1722 bConstraintChannel *chan;
1724 for (chan=chanbase->first; chan; chan=chan->next){
1725 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1729 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
1733 for (con = conlist->first; con; con=con->next) {
1734 /* patch for error introduced by changing constraints (dunno how) */
1735 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
1736 if(con->data==NULL) {
1737 con->type= CONSTRAINT_TYPE_NULL;
1739 /* own ipo, all constraints have it */
1740 con->ipo= newlibadr_us(fd, id->lib, con->ipo);
1742 switch (con->type) {
1743 case CONSTRAINT_TYPE_PYTHON:
1745 bPythonConstraint *data= (bPythonConstraint*)con->data;
1746 bConstraintTarget *ct;
1748 for (ct= data->targets.first; ct; ct= ct->next)
1749 ct->tar = newlibadr(fd, id->lib, ct->tar);
1751 data->text = newlibadr(fd, id->lib, data->text);
1752 //IDP_LibLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1755 case CONSTRAINT_TYPE_ACTION:
1757 bActionConstraint *data;
1758 data= ((bActionConstraint*)con->data);
1759 data->tar = newlibadr(fd, id->lib, data->tar);
1760 data->act = newlibadr(fd, id->lib, data->act);
1763 case CONSTRAINT_TYPE_LOCLIKE:
1765 bLocateLikeConstraint *data;
1766 data= ((bLocateLikeConstraint*)con->data);
1767 data->tar = newlibadr(fd, id->lib, data->tar);
1770 case CONSTRAINT_TYPE_ROTLIKE:
1772 bRotateLikeConstraint *data;
1773 data= ((bRotateLikeConstraint*)con->data);
1774 data->tar = newlibadr(fd, id->lib, data->tar);
1777 case CONSTRAINT_TYPE_SIZELIKE:
1779 bSizeLikeConstraint *data;
1780 data= ((bSizeLikeConstraint*)con->data);
1781 data->tar = newlibadr(fd, id->lib, data->tar);
1784 case CONSTRAINT_TYPE_KINEMATIC:
1786 bKinematicConstraint *data;
1787 data = ((bKinematicConstraint*)con->data);
1788 data->tar = newlibadr(fd, id->lib, data->tar);
1789 data->poletar = newlibadr(fd, id->lib, data->poletar);
1792 case CONSTRAINT_TYPE_TRACKTO:
1794 bTrackToConstraint *data;
1795 data = ((bTrackToConstraint*)con->data);
1796 data->tar = newlibadr(fd, id->lib, data->tar);
1799 case CONSTRAINT_TYPE_MINMAX:
1801 bMinMaxConstraint *data;
1802 data = ((bMinMaxConstraint*)con->data);
1803 data->tar = newlibadr(fd, id->lib, data->tar);
1806 case CONSTRAINT_TYPE_LOCKTRACK:
1808 bLockTrackConstraint *data;
1809 data= ((bLockTrackConstraint*)con->data);
1810 data->tar = newlibadr(fd, id->lib, data->tar);
1813 case CONSTRAINT_TYPE_FOLLOWPATH:
1815 bFollowPathConstraint *data;
1816 data= ((bFollowPathConstraint*)con->data);
1817 data->tar = newlibadr(fd, id->lib, data->tar);
1820 case CONSTRAINT_TYPE_STRETCHTO:
1822 bStretchToConstraint *data;
1823 data= ((bStretchToConstraint*)con->data);
1824 data->tar = newlibadr(fd, id->lib, data->tar);
1827 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
1829 bRigidBodyJointConstraint *data;
1830 data= ((bRigidBodyJointConstraint*)con->data);
1831 data->tar = newlibadr(fd, id->lib, data->tar);
1834 case CONSTRAINT_TYPE_CLAMPTO:
1836 bClampToConstraint *data;
1837 data= ((bClampToConstraint*)con->data);
1838 data->tar = newlibadr(fd, id->lib, data->tar);
1841 case CONSTRAINT_TYPE_CHILDOF:
1843 bChildOfConstraint *data;
1844 data= ((bChildOfConstraint*)con->data);
1845 data->tar = newlibadr(fd, id->lib, data->tar);
1848 case CONSTRAINT_TYPE_TRANSFORM:
1850 bTransformConstraint *data;
1851 data= ((bTransformConstraint*)con->data);
1852 data->tar = newlibadr(fd, id->lib, data->tar);
1855 case CONSTRAINT_TYPE_DISTLIMIT:
1857 bDistLimitConstraint *data;
1858 data= ((bDistLimitConstraint*)con->data);
1859 data->tar = newlibadr(fd, id->lib, data->tar);
1862 case CONSTRAINT_TYPE_SHRINKWRAP:
1864 bShrinkwrapConstraint *data;
1865 data= ((bShrinkwrapConstraint*)con->data);
1866 data->target = newlibadr(fd, id->lib, data->target);
1869 case CONSTRAINT_TYPE_NULL:
1875 static void direct_link_constraints(FileData *fd, ListBase *lb)
1880 for (cons=lb->first; cons; cons=cons->next) {
1881 cons->data = newdataadr(fd, cons->data);
1883 if (cons->type == CONSTRAINT_TYPE_PYTHON) {
1884 bPythonConstraint *data= cons->data;
1885 link_list(fd, &data->targets);
1886 data->prop = newdataadr(fd, data->prop);
1887 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1892 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
1894 bPoseChannel *pchan;
1895 bArmature *arm= ob->data;
1901 /* always rebuild to match proxy or lib changes */
1902 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
1904 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
1905 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
1907 /* hurms... loop in a loop, but yah... later... (ton) */
1908 pchan->bone= get_named_bone(arm, pchan->name);
1910 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
1911 if(pchan->bone==NULL)
1913 else if(ob->id.lib==NULL && arm->id.lib) {
1914 /* local pose selection copied to armature, bit hackish */
1915 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1916 pchan->bone->flag |= pchan->selectflag;
1921 ob->recalc= OB_RECALC;
1922 pose->flag |= POSE_RECALC;
1926 static void lib_link_armature(FileData *fd, Main *main)
1930 arm= main->armature.first;
1933 if(arm->id.flag & LIB_NEEDLINK) {
1934 arm->id.flag -= LIB_NEEDLINK;
1940 static void lib_link_action(FileData *fd, Main *main)
1943 bActionChannel *chan;
1945 act= main->action.first;
1947 if(act->id.flag & LIB_NEEDLINK) {
1948 act->id.flag -= LIB_NEEDLINK;
1950 for (chan=act->chanbase.first; chan; chan=chan->next) {
1951 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1952 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1960 static void direct_link_bones(FileData *fd, Bone* bone)
1964 bone->parent= newdataadr(fd, bone->parent);
1966 link_list(fd, &bone->childbase);
1968 for (child=bone->childbase.first; child; child=child->next) {
1969 direct_link_bones(fd, child);
1974 static void direct_link_action(FileData *fd, bAction *act)
1976 bActionChannel *achan;
1979 link_list(fd, &act->chanbase);
1980 link_list(fd, &act->groups);
1981 link_list(fd, &act->markers);
1983 for (achan = act->chanbase.first; achan; achan=achan->next) {
1984 achan->grp= newdataadr(fd, achan->grp);
1986 link_list(fd, &achan->constraintChannels);
1989 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1990 if (agrp->channels.first) {
1991 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1992 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1997 static void direct_link_armature(FileData *fd, bArmature *arm)
2001 link_list(fd, &arm->bonebase);
2003 bone=arm->bonebase.first;
2005 direct_link_bones(fd, bone);
2010 /* ************ READ CAMERA ***************** */
2012 static void lib_link_camera(FileData *fd, Main *main)
2016 ca= main->camera.first;
2018 if(ca->id.flag & LIB_NEEDLINK) {
2020 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo);
2022 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2024 lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
2026 ca->id.flag -= LIB_NEEDLINK;
2032 static void direct_link_camera(FileData *fd, Camera *ca)
2034 direct_link_scriptlink(fd, &ca->scriptlink);
2038 /* ************ READ LAMP ***************** */
2040 static void lib_link_lamp(FileData *fd, Main *main)
2046 la= main->lamp.first;
2048 if(la->id.flag & LIB_NEEDLINK) {
2050 for(a=0; a<MAX_MTEX; a++) {
2053 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2054 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2058 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo);
2060 lib_link_scriptlink(fd, &la->id, &la->scriptlink);
2062 la->id.flag -= LIB_NEEDLINK;
2068 static void direct_link_lamp(FileData *fd, Lamp *la)
2072 direct_link_scriptlink(fd, &la->scriptlink);
2074 for(a=0; a<MAX_MTEX; a++) {
2075 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2078 la->curfalloff= newdataadr(fd, la->curfalloff);
2080 direct_link_curvemapping(fd, la->curfalloff);
2082 la->preview = direct_link_preview_image(fd, la->preview);
2085 /* ************ READ keys ***************** */
2087 static void lib_link_key(FileData *fd, Main *main)
2091 key= main->key.first;
2093 if(key->id.flag & LIB_NEEDLINK) {
2095 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo);
2096 key->from= newlibadr(fd, key->id.lib, key->from);
2098 key->id.flag -= LIB_NEEDLINK;
2104 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2107 char *data, *poin, *cp;
2109 elemsize= key->elemsize;
2112 for(a=0; a<kb->totelem; a++) {
2117 while( cp[0] ) { /* cp[0]==amount */
2119 switch(cp[1]) { /* cp[1]= type */
2125 SWITCH_INT((*poin));
2138 static void direct_link_key(FileData *fd, Key *key)
2142 link_list(fd, &(key->block));
2144 key->refkey= newdataadr(fd, key->refkey);
2146 kb= key->block.first;
2149 kb->data= newdataadr(fd, kb->data);
2151 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2152 switch_endian_keyblock(key, kb);
2158 /* ************ READ mball ***************** */
2160 static void lib_link_mball(FileData *fd, Main *main)
2165 mb= main->mball.first;
2167 if(mb->id.flag & LIB_NEEDLINK) {
2169 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2171 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo);
2173 mb->id.flag -= LIB_NEEDLINK;
2179 static void direct_link_mball(FileData *fd, MetaBall *mb)
2181 mb->mat= newdataadr(fd, mb->mat);
2182 test_pointer_array(fd, (void **)&mb->mat);
2184 link_list(fd, &(mb->elems));
2186 mb->disp.first= mb->disp.last= 0;
2191 /* ************ READ WORLD ***************** */
2193 static void lib_link_world(FileData *fd, Main *main)
2199 wrld= main->world.first;
2201 if(wrld->id.flag & LIB_NEEDLINK) {
2203 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo);
2205 for(a=0; a<MAX_MTEX; a++) {
2206 mtex= wrld->mtex[a];
2208 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2209 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2213 lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
2215 wrld->id.flag -= LIB_NEEDLINK;
2217 wrld= wrld->id.next;
2221 static void direct_link_world(FileData *fd, World *wrld)
2225 direct_link_scriptlink(fd, &wrld->scriptlink);
2227 for(a=0; a<MAX_MTEX; a++) {
2228 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2230 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2234 /* ************ READ IPO ***************** */
2236 static void lib_link_ipo(FileData *fd, Main *main)
2240 ipo= main->ipo.first;
2242 if(ipo->id.flag & LIB_NEEDLINK) {
2244 for(icu= ipo->curve.first; icu; icu= icu->next) {
2246 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
2248 ipo->id.flag -= LIB_NEEDLINK;
2254 static void direct_link_ipo(FileData *fd, Ipo *ipo)
2258 link_list(fd, &(ipo->curve));
2259 icu= ipo->curve.first;
2261 icu->bezt= newdataadr(fd, icu->bezt);
2262 icu->bp= newdataadr(fd, icu->bp);
2263 icu->driver= newdataadr(fd, icu->driver);
2268 /* ************ READ VFONT ***************** */
2270 static void lib_link_vfont(FileData *fd, Main *main)
2274 vf= main->vfont.first;
2276 if(vf->id.flag & LIB_NEEDLINK) {
2277 vf->id.flag -= LIB_NEEDLINK;
2283 static void direct_link_vfont(FileData *fd, VFont *vf)
2286 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2289 /* ************ READ TEXT ****************** */
2291 static void lib_link_text(FileData *fd, Main *main)
2295 text= main->text.first;
2297 if(text->id.flag & LIB_NEEDLINK) {
2298 text->id.flag -= LIB_NEEDLINK;
2300 text= text->id.next;
2304 static void direct_link_text(FileData *fd, Text *text)
2308 text->name= newdataadr(fd, text->name);
2311 text->undo_len= TXT_INIT_UNDO;
2312 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2314 text->compiled= NULL;
2317 if(text->flags & TXT_ISEXT) {
2322 link_list(fd, &text->lines);
2323 link_list(fd, &text->markers);
2325 text->curl= newdataadr(fd, text->curl);
2326 text->sell= newdataadr(fd, text->sell);
2328 ln= text->lines.first;
2330 ln->line= newdataadr(fd, ln->line);
2333 if (ln->len != (int) strlen(ln->line)) {
2334 printf("Error loading text, line lengths differ\n");
2335 ln->len = strlen(ln->line);
2341 text->flags = (text->flags|TXT_ISTMP) & ~TXT_ISEXT;
2346 /* ************ READ IMAGE ***************** */
2348 static void lib_link_image(FileData *fd, Main *main)
2352 ima= main->image.first;
2354 if(ima->id.flag & LIB_NEEDLINK) {
2355 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2357 ima->id.flag -= LIB_NEEDLINK;
2363 static void link_ibuf_list(FileData *fd, ListBase *lb)
2367 if(lb->first==NULL) return;
2369 lb->first= newimaadr(fd, lb->first);
2373 ln->next= newimaadr(fd, ln->next);
2381 static void direct_link_image(FileData *fd, Image *ima)
2383 /* for undo system, pointers could be restored */
2385 link_ibuf_list(fd, &ima->ibufs);
2387 ima->ibufs.first= ima->ibufs.last= NULL;
2389 /* if not restored, we keep the binded opengl index */
2390 if(ima->ibufs.first==NULL) {
2392 ima->gputexture= NULL;
2399 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2400 ima->preview = direct_link_preview_image(fd, ima->preview);
2405 /* ************ READ CURVE ***************** */
2407 static void lib_link_curve(FileData *fd, Main *main)
2412 cu= main->curve.first;
2414 if(cu->id.flag & LIB_NEEDLINK) {
2416 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2418 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2419 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2420 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2421 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2422 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2423 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2424 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2426 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo);
2427 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2429 cu->id.flag -= LIB_NEEDLINK;
2436 static void switch_endian_knots(Nurb *nu)
2443 SWITCH_INT(nu->knotsu[len]);
2449 SWITCH_INT(nu->knotsv[len]);
2454 static void direct_link_curve(FileData *fd, Curve *cu)
2459 cu->mat= newdataadr(fd, cu->mat);
2460 test_pointer_array(fd, (void **)&cu->mat);
2461 cu->str= newdataadr(fd, cu->str);
2462 cu->strinfo= newdataadr(fd, cu->strinfo);
2463 cu->tb= newdataadr(fd, cu->tb);
2465 if(cu->vfont==0) link_list(fd, &(cu->nurb));
2467 cu->nurb.first=cu->nurb.last= 0;
2469 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2471 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2478 cu->tb[0].w = cu->linewidth;
2480 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2483 cu->bev.first=cu->bev.last= 0;
2484 cu->disp.first=cu->disp.last= 0;
2489 nu->bezt= newdataadr(fd, nu->bezt);
2490 nu->bp= newdataadr(fd, nu->bp);
2491 nu->knotsu= newdataadr(fd, nu->knotsu);
2492 nu->knotsv= newdataadr(fd, nu->knotsv);
2493 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2495 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2496 switch_endian_knots(nu);
2504 /* ************ READ TEX ***************** */
2506 static void lib_link_texture(FileData *fd, Main *main)
2510 tex= main->tex.first;
2512 if(tex->id.flag & LIB_NEEDLINK) {
2514 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2515 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2516 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2519 lib_link_ntree(fd, &tex->id, tex->nodetree);
2521 tex->id.flag -= LIB_NEEDLINK;
2527 static void direct_link_texture(FileData *fd, Tex *tex)
2529 tex->plugin= newdataadr(fd, tex->plugin);
2531 tex->plugin->handle= 0;
2532 open_plugin_tex(tex->plugin);
2533 /* initialize data for this instance, if an initialization
2536 if (tex->plugin->instance_init)
2537 tex->plugin->instance_init((void *) tex->plugin->data);
2539 tex->coba= newdataadr(fd, tex->coba);
2540 tex->env= newdataadr(fd, tex->env);
2542 tex->env->ima= NULL;
2543 memset(tex->env->cube, 0, 6*sizeof(void *));
2547 tex->nodetree= newdataadr(fd, tex->nodetree);
2549 direct_link_nodetree(fd, tex->nodetree);
2551 tex->preview = direct_link_preview_image(fd, tex->preview);
2558 /* ************ READ MATERIAL ***************** */
2560 static void lib_link_material(FileData *fd, Main *main)
2566 ma= main->mat.first;
2568 if(ma->id.flag & LIB_NEEDLINK) {
2569 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2570 of library blocks that implement this.*/
2571 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2573 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2574 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2576 for(a=0; a<MAX_MTEX; a++) {
2579 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2580 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2583 lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
2586 lib_link_ntree(fd, &ma->id, ma->nodetree);
2588 ma->id.flag -= LIB_NEEDLINK;
2594 static void direct_link_material(FileData *fd, Material *ma)
2598 for(a=0; a<MAX_MTEX; a++) {
2599 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2602 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2603 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2605 direct_link_scriptlink(fd, &ma->scriptlink);
2607 ma->nodetree= newdataadr(fd, ma->nodetree);
2609 direct_link_nodetree(fd, ma->nodetree);
2611 ma->preview = direct_link_preview_image(fd, ma->preview);
2612 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
2615 /* ************ READ PARTICLE SETTINGS ***************** */
2617 static void direct_link_pointcache(FileData *fd, PointCache *cache)
2619 cache->flag &= ~(PTCACHE_SIMULATION_VALID|PTCACHE_BAKE_EDIT_ACTIVE);
2623 static void lib_link_particlesettings(FileData *fd, Main *main)
2625 ParticleSettings *part;
2627 part= main->particle.first;
2629 if(part->id.flag & LIB_NEEDLINK) {
2630 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo);
2631 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
2632 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
2633 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
2634 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
2635 part->id.flag -= LIB_NEEDLINK;
2637 part= part->id.next;
2641 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
2643 part->pd= newdataadr(fd, part->pd);
2644 part->pd2= newdataadr(fd, part->pd2);
2647 static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase *particles)
2649 ParticleSystem *psys, *psysnext;
2652 for(psys=particles->first; psys; psys=psysnext){
2655 psysnext= psys->next;
2657 psys->part = newlibadr_us(fd, id->lib, psys->part);
2659 psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
2660 psys->keyed_ob = newlibadr(fd, id->lib, psys->keyed_ob);
2662 for(a=0,pa=psys->particles; a<psys->totpart; a++,pa++){
2663 pa->stick_ob=newlibadr(fd, id->lib, pa->stick_ob);
2667 /* particle modifier must be removed before particle system */
2668 ParticleSystemModifierData *psmd= psys_get_modifier(ob,psys);
2669 BLI_remlink(&ob->modifiers, psmd);
2670 modifier_free((ModifierData *)psmd);
2672 BLI_remlink(particles, psys);
2677 static void direct_link_particlesystems(FileData *fd, ListBase *particles)
2679 ParticleSystem *psys;
2682 for(psys=particles->first; psys; psys=psys->next) {
2683 psys->particles=newdataadr(fd,psys->particles);
2684 if(psys->particles && psys->particles->hair){
2685 ParticleData *pa = psys->particles;
2686 for(a=0; a<psys->totpart; a++, pa++)
2687 pa->hair=newdataadr(fd,pa->hair);
2689 if(psys->particles && psys->particles->keys){
2690 ParticleData *pa = psys->particles;
2691 for(a=0; a<psys->totpart; a++, pa++) {
2696 psys->flag &= ~PSYS_KEYED;
2698 psys->child=newdataadr(fd,psys->child);
2699 psys->effectors.first=psys->effectors.last=0;
2701 psys->soft= newdataadr(fd, psys->soft);
2703 SoftBody *sb = psys->soft;
2704 sb->particles = psys;
2705 sb->bpoint= NULL; // init pointers so it gets rebuilt nicely
2709 sb->pointcache= newdataadr(fd, sb->pointcache);
2711 direct_link_pointcache(fd, sb->pointcache);
2715 psys->pathcache = 0;
2716 psys->childcache = 0;
2717 psys->pathcachebufs.first = psys->pathcachebufs.last = 0;
2718 psys->childcachebufs.first = psys->childcachebufs.last = 0;
2719 psys->reactevents.first = psys->reactevents.last = 0;
2721 psys->pointcache= newdataadr(fd, psys->pointcache);
2722 if(psys->pointcache)
2723 direct_link_pointcache(fd, psys->pointcache);
2728 /* ************ READ MESH ***************** */
2730 static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
2735 for (i=0; i<totface; i++, tf++) {
2736 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
2737 if(tf->tpage && tf->tpage->id.us==0)
2738 tf->tpage->id.us= 1;
2742 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
2745 for(i=0; i<fdata->totlayer; i++) {
2746 CustomDataLayer *layer = &fdata->layers[i];
2748 if(layer->type == CD_MTFACE)
2749 lib_link_mtface(fd, me, layer->data, totface);
2754 static void lib_link_mesh(FileData *fd, Main *main)
2758 me= main->mesh.first;
2760 if(me->id.flag & LIB_NEEDLINK) {
2763 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2764 of library blocks that implement this.*/
2765 if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2767 /* this check added for python created meshes */
2769 for(i=0; i<me->totcol; i++) {
2770 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
2775 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
2776 me->key= newlibadr_us(fd, me->id.lib, me->key);
2777 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
2779 lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
2780 if(me->mr && me->mr->levels.first)
2781 lib_link_customdata_mtface(fd, me, &me->mr->fdata,
2782 ((MultiresLevel*)me->mr->levels.first)->totface);
2784 me->id.flag -= LIB_NEEDLINK;
2790 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
2797 for (i=0; i<count; i++) {
2798 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
2800 mdverts[i].totweight=0;
2804 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
2808 data->layers= newdataadr(fd, data->layers);
2810 while (i < data->totlayer) {
2811 CustomDataLayer *layer = &data->layers[i];
2813 if (CustomData_verify_versions(data, i)) {
2814 layer->data = newdataadr(fd, layer->data);
2820 static void direct_link_mesh(FileData *fd, Mesh *mesh)
2822 mesh->mat= newdataadr(fd, mesh->mat);
2823 test_pointer_array(fd, (void **)&mesh->mat);
2825 mesh->mvert= newdataadr(fd, mesh->mvert);
2826 mesh->medge= newdataadr(fd, mesh->medge);
2827 mesh->mface= newdataadr(fd, mesh->mface);
2828 mesh->tface= newdataadr(fd, mesh->tface);
2829 mesh->mtface= newdataadr(fd, mesh->mtface);
2830 mesh->mcol= newdataadr(fd, mesh->mcol);
2831 mesh->msticky= newdataadr(fd, mesh->msticky);
2832 mesh->dvert= newdataadr(fd, mesh->dvert);
2834 /* Partial-mesh visibility (do this before using totvert, totface, or totedge!) */
2835 mesh->pv= newdataadr(fd, mesh->pv);
2837 mesh->pv->vert_map= newdataadr(fd, mesh->pv->vert_map);
2838 mesh->pv->edge_map= newdataadr(fd, mesh->pv->edge_map);
2839 mesh->pv->old_faces= newdataadr(fd, mesh->pv->old_faces);
2840 mesh->pv->old_edges= newdataadr(fd, mesh->pv->old_edges);
2843 /* normally direct_link_dverts should be called in direct_link_customdata,
2844 but for backwards compat in do_versions to work we do it here */
2845 direct_link_dverts(fd, mesh->pv ? mesh->pv->totvert : mesh->totvert, mesh->dvert);
2847 direct_link_customdata(fd, &mesh->vdata, mesh->pv ? mesh->pv->totvert : mesh->totvert);
2848 direct_link_customdata(fd, &mesh->edata, mesh->pv ? mesh->pv->totedge : mesh->totedge);
2849 direct_link_customdata(fd, &mesh->fdata, mesh->pv ? mesh->pv->totface : mesh->totface);
2852 mesh->mselect = NULL;
2855 mesh->mr= newdataadr(fd, mesh->mr);
2859 link_list(fd, &mesh->mr->levels);
2860 lvl= mesh->mr->levels.first;
2862 direct_link_customdata(fd, &mesh->mr->vdata, lvl->totvert);
2863 direct_link_dverts(fd, lvl->totvert, CustomData_get(&mesh->mr->vdata, 0, CD_MDEFORMVERT));
2864 direct_link_customdata(fd, &mesh->mr->fdata, lvl->totface);
2866 if(mesh->mr->edge_flags)
2867 mesh->mr->edge_flags= newdataadr(fd, mesh->mr->edge_flags);
2868 if(mesh->mr->edge_creases)
2869 mesh->mr->edge_creases= newdataadr(fd, mesh->mr->edge_creases);
2871 if(!mesh->mr->edge_flags)
2872 mesh->mr->edge_flags= MEM_callocN(sizeof(short)*lvl->totedge, "Multires Edge Flags");
2873 if(!mesh->mr->edge_creases)
2874 mesh->mr->edge_creases= MEM_callocN(sizeof(char)*lvl->totedge, "Multires Edge Creases");
2876 mesh->mr->verts = newdataadr(fd, mesh->mr->verts);
2878 for(; lvl; lvl= lvl->next) {
2879 lvl->verts= newdataadr(fd, lvl->verts);
2880 lvl->faces= newdataadr(fd, lvl->faces);
2881 lvl->edges= newdataadr(fd, lvl->edges);
2882 lvl->colfaces= newdataadr(fd, lvl->colfaces);
2883 lvl->edge_boundary_states= NULL;
2884 lvl->vert_face_map = lvl->vert_edge_map = NULL;
2889 /* Gracefully handle corrupted mesh */
2890 if(mesh->mr && !mesh->mr->verts) {
2891 /* If totals match, simply load the current mesh verts into multires */
2892 if(mesh->totvert == ((MultiresLevel*)mesh->mr->levels.last)->totvert)
2893 mesh->mr->verts = MEM_dupallocN(mesh->mvert);
2895 /* Otherwise, we can't recover the data, silently remove multires */
2896 multires_free(mesh->mr);
2901 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
2902 TFace *tf= mesh->tface;
2905 for (i=0; i< (mesh->pv ? mesh->pv->totface : mesh->totface); i++, tf++) {
2906 SWITCH_INT(tf->col[0]);
2907 SWITCH_INT(tf->col[1]);
2908 SWITCH_INT(tf->col[2]);
2909 SWITCH_INT(tf->col[3]);
2914 /* ************ READ LATTICE ***************** */
2916 static void lib_link_latt(FileData *fd, Main *main)
2920 lt= main->latt.first;
2922 if(lt->id.flag & LIB_NEEDLINK) {
2924 lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo);
2925 lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
2927 lt->id.flag -= LIB_NEEDLINK;
2933 static void direct_link_latt(FileData *fd, Lattice *lt)
2935 lt->def= newdataadr(fd, lt->def);
2937 lt->dvert= newdataadr(fd, lt->dvert);
2938 direct_link_dverts(fd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
2942 /* ************ READ OBJECT ***************** */
2944 static void lib_link_modifiers__linkModifiers(void *userData, Object *ob,
2947 FileData *fd = userData;
2949 *idpoin = newlibadr(fd, ob->id.lib, *idpoin);
2950 /* hardcoded bad exception; non-object modifier data gets user count (texture, displace) */
2951 if(*idpoin && GS((*idpoin)->name)!=ID_OB)
2954 static void lib_link_modifiers(FileData *fd, Object *ob)
2956 modifiers_foreachIDLink(ob, lib_link_modifiers__linkModifiers, fd);
2959 static void lib_link_object(FileData *fd, Main *main)
2969 ob= main->object.first;
2971 if(ob->id.flag & LIB_NEEDLINK) {
2972 if (ob->id.properties) IDP_LibLinkProperty(ob->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2974 ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
2975 ob->track= newlibadr(fd, ob->id.lib, ob->track);
2976 ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
2977 ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
2978 ob->poselib= newlibadr_us(fd, ob->id.lib, ob->poselib);
2979 ob->dup_group= newlibadr_us(fd, ob->id.lib, ob->dup_group);
2981 ob->proxy= newlibadr_us(fd, ob->id.lib, ob->proxy);
2983 /* paranoia check, actually a proxy_from pointer should never be written... */
2984 if(ob->proxy->id.lib==NULL) {
2985 ob->proxy->proxy_from= NULL;
2989 /* this triggers object_update to always use a copy */
2990 ob->proxy->proxy_from= ob;
2991 /* force proxy updates after load/undo, a bit weak */
2992 ob->recalc= ob->proxy->recalc= OB_RECALC;
2995 ob->proxy_group= newlibadr(fd, ob->id.lib, ob->proxy_group);
2998 ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
3000 if(ob->data==NULL && poin!=NULL) {
3003 if(ob->id.lib) printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
3004 else printf("Object %s lost data.", ob->id.name+2);
3007 free_pose(ob->pose);
3009 ob->flag &= ~OB_POSEMODE;
3012 for(a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
3014 ob->id.flag -= LIB_NEEDLINK;
3015 /* if id.us==0 a new base will be created later on */
3017 /* WARNING! Also check expand_object(), should reflect the stuff below. */
3018 lib_link_pose(fd, ob, ob->pose);
3019 lib_link_constraints(fd, &ob->id, &ob->constraints);
3020 lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
3021 lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
3023 for(paf= ob->effect.first; paf; paf= paf->next) {
3024 if(paf->type==EFF_PARTICLE) {
3025 paf->group= newlibadr_us(fd, ob->id.lib, paf->group);
3029 sens= ob->sensors.first;
3031 for(a=0; a<sens->totlinks; a++)
3032 sens->links[a]= newglobadr(fd, sens->links[a]);
3034 if(sens->type==SENS_TOUCH) {
3035 bTouchSensor *ts= sens->data;
3036 ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
3038 else if(sens->type==SENS_MESSAGE) {
3039 bMessageSensor *ms= sens->data;
3041 newlibadr(fd, ob->id.lib, ms->fromObject);
3046 cont= ob->controllers.first;
3048 for(a=0; a<cont->totlinks; a++)
3049 cont->links[a]= newglobadr(fd, cont->links[a]);
3051 if(cont->type==CONT_PYTHON) {
3052 bPythonCont *pc= cont->data;
3053 pc->text= newlibadr(fd, ob->id.lib, pc->text);
3061 act= ob->actuators.first;
3063 if(act->type==ACT_SOUND) {
3064 bSoundActuator *sa= act->data;
3065 sa->sound= newlibadr_us(fd, ob->id.lib, sa->sound);
3067 else if(act->type==ACT_CD) {
3068 /* bCDActuator *cda= act->data; */
3070 else if(act->type==ACT_GAME) {
3071 /* bGameActuator *ga= act->data; */
3073 else if(act->type==ACT_CAMERA) {
3074 bCameraActuator *ca= act->data;
3075 ca->ob= newlibadr(fd, ob->id.lib, ca->ob);
3077 /* leave this one, it's obsolete but necessary to read for conversion */
3078 else if(act->type==ACT_ADD_OBJECT) {
3079 bAddObjectActuator *eoa= act->data;
3080 if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
3082 else if(act->type==ACT_OBJECT) {
3083 bObjectActuator *oa= act->data;
3084 oa->reference= newlibadr(fd, ob->id.lib, oa->reference);
3086 else if(act->type==ACT_EDIT_OBJECT) {
3087 bEditObjectActuator *eoa= act->data;
3092 eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
3093 eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
3096 else if(act->type==ACT_OBJECT) {
3097 bObjectActuator *oa= act->data;
3102 oa->reference= newlibadr(fd, ob->id.lib, oa->reference);
3105 else if(act->type==ACT_SCENE) {
3106 bSceneActuator *sa= act->data;
3107 sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
3108 sa->scene= newlibadr(fd, ob->id.lib, sa->scene);
3110 else if(act->type==ACT_ACTION) {
3111 bActionActuator *aa= act->data;
3112 aa->act= newlibadr(fd, ob->id.lib, aa->act);
3114 else if(act->type==ACT_SHAPEACTION) {
3115 bActionActuator *aa= act->data;
3116 aa->act= newlibadr(fd, ob->id.lib, aa->act);
3118 else if(act->type==ACT_PROPERTY) {
3119 bPropertyActuator *pa= act->data;
3120 pa->ob= newlibadr(fd, ob->id.lib, pa->ob);
3122 else if(act->type==ACT_MESSAGE) {
3123 bMessageActuator *ma= act->data;
3124 ma->toObject= newlibadr(fd, ob->id.lib, ma->toObject);
3126 else if(act->type==ACT_2DFILTER){
3127 bTwoDFilterActuator *_2dfa = act->data;
3128 _2dfa->text= newlibadr(fd, ob->id.lib, _2dfa->text);
3130 else if(act->type==ACT_PARENT) {
3131 bParentActuator *parenta = act->data;
3132 parenta->ob = newlibadr(fd, ob->id.lib, parenta->ob);
3134 else if(act->type==ACT_STATE) {
3135 /* bStateActuator *statea = act->data; */
3141 FluidsimModifierData *fluidmd = (FluidsimModifierData *)modifiers_findByType(ob, eModifierType_Fluidsim);
3143 if(fluidmd && fluidmd->fss)
3144 fluidmd->fss->ipo = newlibadr_us(fd, ob->id.lib, fluidmd->fss->ipo);
3150 ob->pd->tex=newlibadr_us(fd, ob->id.lib, ob->pd->tex);
3152 lib_link_scriptlink(fd, &ob->id, &ob->scriptlink);
3153 lib_link_particlesystems(fd, ob, &ob->id, &ob->particlesystem);
3154 lib_link_modifiers(fd, ob);
3159 if(warn) error("WARNING IN CONSOLE");
3163 static void direct_link_pose(FileData *fd, bPose *pose)
3165 bPoseChannel *pchan;
3170 link_list(fd, &pose->chanbase);
3171 link_list(fd, &pose->agroups);
3173 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
3175 pchan->parent= newdataadr(fd, pchan->parent);
3176 pchan->child= newdataadr(fd, pchan->child);
3177 direct_link_constraints(fd, &pchan->constraints);
3178 pchan->iktree.first= pchan->iktree.last= NULL;
3183 static void direct_link_modifiers(FileData *fd, ListBase *lb)
3189 for (md=lb->first; md; md=md->next) {
3192 /* if modifiers disappear, or for upward compatibility */
3193 if(NULL==modifierType_getInfo(md->type))
3194 md->type= eModifierType_None;
3196 if (md->type==eModifierType_Subsurf) {
3197 SubsurfModifierData *smd = (SubsurfModifierData*) md;
3199 smd->emCache = smd->mCache = 0;
3201 else if (md->type==eModifierType_Armature) {
3202 ArmatureModifierData *amd = (ArmatureModifierData*) md;
3206 else if (md->type==eModifierType_Cloth) {
3207 ClothModifierData *clmd = (ClothModifierData*) md;
3209 clmd->clothObject = NULL;
3211 clmd->sim_parms= newdataadr(fd, clmd->sim_parms);
3212 clmd->coll_parms= newdataadr(fd, clmd->coll_parms);
3213 clmd->point_cache= newdataadr(fd, clmd->point_cache);
3215 if(clmd->point_cache)
3216 direct_link_pointcache(fd, clmd->point_cache);
3218 if(clmd->sim_parms) {
3219 if(clmd->sim_parms->presets > 10)
3220 clmd->sim_parms->presets = 0;