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_node.h" // for tree type defines
137 #include "BKE_object.h"
138 #include "BKE_particle.h"
139 #include "BKE_pointcache.h"
140 #include "BKE_property.h" // for get_ob_property
141 #include "BKE_sca.h" // for init_actuator
142 #include "BKE_scene.h"
143 #include "BKE_softbody.h" // sbNew()
144 #include "BKE_bullet.h" // bsbNew()
145 #include "BKE_sculpt.h"
146 #include "BKE_texture.h" // for open_plugin_tex
147 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
148 #include "BKE_idprop.h"
150 #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
151 #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
152 #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
153 #include "BLO_readfile.h"
154 #include "BLO_undofile.h"
155 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
157 #include "readfile.h"
161 #include "mydevice.h"
167 Remark: still a weak point is the newadress() function, that doesnt solve reading from
168 multiple files at the same time
170 (added remark: oh, i thought that was solved? will look at that... (ton)
173 - Existing Library (Main) push or free
183 - read associated 'direct data'
184 - link direct data (internal and to LibBlock)
186 - read USER data, only when indicated (file is ~/.B.blend)
188 - per Library (per Main)
191 - find LibBlocks and attach IDs to Main
192 - if external LibBlock
194 - or it's already read,
199 - read associated direct data
200 - link direct data (internal and to LibBlock)
202 - per Library with unread LibBlocks
207 - read associated direct data
208 - link direct data (internal and to LibBlock)
211 - link all LibBlocks and indirect pointers to libblocks
212 - initialize FileGlobal and copy pointers to Global
215 /* also occurs in library.c */
216 /* GS reads the memory pointed at in a specific ordering. There are,
217 * however two definitions for it. I have jotted them down here, both,
218 * but I think the first one is actually used. The thing is that
219 * big-endian systems might read this the wrong way round. OTOH, we
220 * constructed the IDs that are read out with this macro explicitly as
221 * well. I expect we'll sort it out soon... */
224 #define GS(a) (*((short *)(a)))
226 /* from misc_util: flip the bytes from x */
227 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
229 // only used here in readfile.c
230 #define SWITCH_LONGINT(a) { \
233 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
234 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
235 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
236 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
240 typedef struct OldNew {
245 typedef struct OldNewMap {
247 int nentries, entriessize;
253 /* local prototypes */
254 static void *read_struct(FileData *fd, BHead *bh, char *blockname);
257 static OldNewMap *oldnewmap_new(void)
259 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
261 onm->entriessize= 1024;
262 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
267 static int verg_oldnewmap(const void *v1, const void *v2)
269 const struct OldNew *x1=v1, *x2=v2;
271 if( x1->old > x2->old) return 1;
272 else if( x1->old < x2->old) return -1;
277 static void oldnewmap_sort(FileData *fd)
279 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
280 fd->libmap->sorted= 1;
283 /* nr is zero for data, and ID code for libdata */
284 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
288 if(oldaddr==NULL || newaddr==NULL) return;
290 if (onm->nentries==onm->entriessize) {
291 int osize= onm->entriessize;
292 OldNew *oentries= onm->entries;
294 onm->entriessize*= 2;
295 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
297 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
301 entry= &onm->entries[onm->nentries++];
303 entry->newp= newaddr;
307 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
311 if (onm->lasthit<onm->nentries-1) {
312 OldNew *entry= &onm->entries[++onm->lasthit];
314 if (entry->old==addr) {
320 for (i=0; i<onm->nentries; i++) {
321 OldNew *entry= &onm->entries[i];
323 if (entry->old==addr) {
334 /* for libdata, nr has ID code, no increment */
335 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
339 if(addr==NULL) return NULL;
341 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
343 OldNew entry_s, *entry;
347 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
351 if (id && (!lib || id->lib)) {
357 for (i=0; i<onm->nentries; i++) {
358 OldNew *entry= &onm->entries[i];
360 if (entry->old==addr) {
363 if (id && (!lib || id->lib)) {
372 static void oldnewmap_free_unused(OldNewMap *onm)
376 for (i=0; i<onm->nentries; i++) {
377 OldNew *entry= &onm->entries[i];
379 MEM_freeN(entry->newp);
385 static void oldnewmap_clear(OldNewMap *onm)
391 static void oldnewmap_free(OldNewMap *onm)
393 MEM_freeN(onm->entries);
399 static void read_libraries(FileData *basefd, ListBase *mainlist);
401 /* ************ help functions ***************** */
403 static void add_main_to_main(Main *mainvar, Main *from)
405 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
408 a= set_listbasepointers(mainvar, lbarray);
409 a= set_listbasepointers(from, fromarray);
411 addlisttolist(lbarray[a], fromarray[a]);
415 void blo_join_main(ListBase *mainlist)
417 Main *tojoin, *mainl;
420 mainl= mainlist->first;
421 while ((tojoin= mainl->next)) {
422 add_main_to_main(mainl, tojoin);
423 BLI_remlink(mainlist, tojoin);
428 static void split_libdata(ListBase *lb, Main *first)
440 if(mainvar->curlib==id->lib) {
441 lbn= wich_libbase(mainvar, GS(id->name));
443 BLI_addtail(lbn, id);
446 mainvar= mainvar->next;
448 if(mainvar==0) printf("error split_libdata\n");
454 void blo_split_main(ListBase *mainlist, Main *main)
456 ListBase *lbarray[MAX_LIBARRAY];
460 mainlist->first= mainlist->last= main;
463 if(main->library.first==NULL)
466 for (lib= main->library.first; lib; lib= lib->id.next) {
467 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
468 libmain->curlib= lib;
469 BLI_addtail(mainlist, libmain);
472 i= set_listbasepointers(main, lbarray);
474 split_libdata(lbarray[i], main->next);
477 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
478 static void cleanup_path(const char *relabase, char *name)
480 char filename[FILE_MAXFILE];
482 BLI_splitdirstring(name, filename);
483 BLI_cleanup_dir(relabase, name);
484 strcat(name, filename);
487 static void read_file_version(FileData *fd, Main *main)
491 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
492 if (bhead->code==GLOB) {
493 FileGlobal *fg= read_struct(fd, bhead, "Global");
495 main->subversionfile= fg->subversion;
496 main->minversionfile= fg->minversion;
497 main->minsubversionfile= fg->minsubversion;
500 else if (bhead->code==ENDB)
507 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *name, const char *relabase)
511 char name1[FILE_MAXDIR+FILE_MAXFILE];
513 strncpy(name1, name, sizeof(name1)-1);
514 cleanup_path(relabase, name1);
515 // printf("blo_find_main: original in %s\n", name);
516 // printf("blo_find_main: converted to %s\n", name1);
518 for (m= mainlist->first; m; m= m->next) {
519 char *libname= (m->curlib)?m->curlib->filename:m->name;
521 if (BLI_streq(name1, libname)) {
522 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
527 m= MEM_callocN(sizeof(Main), "find_main");
528 BLI_addtail(mainlist, m);
530 lib= alloc_libblock(&m->library, ID_LI, "lib");
531 strncpy(lib->name, name, sizeof(lib->name)-1);
532 BLI_strncpy(lib->filename, name1, sizeof(lib->filename));
536 read_file_version(fd, m);
538 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", name);
543 /* ************ FILE PARSING ****************** */
545 static void switch_endian_bh4(BHead4 *bhead)
547 /* the ID_.. codes */
548 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
550 if (bhead->code != ENDB) {
551 SWITCH_INT(bhead->len);
552 SWITCH_INT(bhead->SDNAnr);
553 SWITCH_INT(bhead->nr);
557 static void switch_endian_bh8(BHead8 *bhead)
559 /* the ID_.. codes */
560 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
562 if (bhead->code != ENDB) {
563 SWITCH_INT(bhead->len);
564 SWITCH_INT(bhead->SDNAnr);
565 SWITCH_INT(bhead->nr);
569 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
571 BHead4 *bhead4 = (BHead4 *) bhead;
572 #if defined(WIN32) && !defined(FREE_WINDOWS)
578 bhead4->code= bhead8->code;
579 bhead4->len= bhead8->len;
581 if (bhead4->code != ENDB) {
583 // why is this here ??
584 if (do_endian_swap) {
585 SWITCH_LONGINT(bhead8->old);
588 /* this patch is to avoid a long long being read from not-eight aligned positions
589 is necessary on any modern 64bit architecture) */
590 memcpy(&old, &bhead8->old, 8);
591 bhead4->old = (int) (old >> 3);
593 bhead4->SDNAnr= bhead8->SDNAnr;
594 bhead4->nr= bhead8->nr;
598 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
600 BHead8 *bhead8 = (BHead8 *) bhead;
602 bhead8->code= bhead4->code;
603 bhead8->len= bhead4->len;
605 if (bhead8->code != ENDB) {
606 bhead8->old= bhead4->old;
607 bhead8->SDNAnr= bhead4->SDNAnr;
608 bhead8->nr= bhead4->nr;
612 static BHeadN *get_bhead(FileData *fd)
617 BHeadN *new_bhead = 0;
623 // First read the bhead structure.
624 // Depending on the platform the file was written on this can
625 // be a big or little endian BHead4 or BHead8 structure.
627 // As usual 'ENDB' (the last *partial* bhead of the file)
628 // needs some special handling. We don't want to EOF just yet.
630 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
632 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
634 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
635 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
636 switch_endian_bh4(&bhead4);
639 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
640 bh8_from_bh4(&bhead, &bhead4);
642 memcpy(&bhead, &bhead4, sizeof(bhead));
650 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
652 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
653 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
654 switch_endian_bh8(&bhead8);
657 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
658 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
660 memcpy(&bhead, &bhead8, sizeof(bhead));
668 /* make sure people are not trying to pass bad blend files */
669 if (bhead.len < 0) fd->eof = 1;
671 // bhead now contains the (converted) bhead structure. Now read
672 // the associated data and put everything in a BHeadN (creative naming !)
675 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
677 new_bhead->next = new_bhead->prev = 0;
678 new_bhead->bhead = bhead;
680 readsize = fd->read(fd, new_bhead + 1, bhead.len);
682 if (readsize != bhead.len) {
684 MEM_freeN(new_bhead);
694 // We've read a new block. Now add it to the list
698 BLI_addtail(&fd->listbase, new_bhead);
704 BHead *blo_firstbhead(FileData *fd)
710 // Read in a new block if necessary
712 new_bhead = fd->listbase.first;
713 if (new_bhead == 0) {
714 new_bhead = get_bhead(fd);
718 bhead = &new_bhead->bhead;
724 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
726 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
727 BHeadN *prev= bheadn->prev;
729 return prev?&prev->bhead:NULL;
732 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
734 BHeadN *new_bhead = NULL;
738 // bhead is actually a sub part of BHeadN
739 // We calculate the BHeadN pointer from the BHead pointer below
740 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
742 // get the next BHeadN. If it doesn't exist we read in the next one
743 new_bhead = new_bhead->next;
744 if (new_bhead == 0) {
745 new_bhead = get_bhead(fd);
750 // here we do the reverse:
751 // go from the BHeadN pointer to the BHead pointer
752 bhead = &new_bhead->bhead;
758 static void decode_blender_header(FileData *fd)
760 char header[SIZEOFBLENDERHEADER], num[4];
763 // read in the header data
764 readsize = fd->read(fd, header, sizeof(header));
766 if (readsize == sizeof(header)) {
767 if(strncmp(header, "BLENDER", 7) == 0) {
768 int remove_this_endian_test= 1;
770 fd->flags |= FD_FLAGS_FILE_OK;
772 // what size are pointers in the file ?
774 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
775 if (sizeof(void *) != 4) {
776 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
779 if (sizeof(void *) != 8) {
780 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
784 // is the file saved in a different endian
786 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
787 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
790 // get the version number
792 memcpy(num, header+9, 3);
794 fd->fileversion = atoi(num);
799 static int read_file_dna(FileData *fd)
803 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
804 if (bhead->code==DNA1) {
805 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
807 fd->filesdna= dna_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
810 fd->compflags= dna_get_structDNA_compareflags(fd->filesdna, fd->memsdna);
811 /* used to retrieve ID names from (bhead+1) */
812 fd->id_name_offs= dna_elem_offset(fd->filesdna, "ID", "char", "name[]");
816 } else if (bhead->code==ENDB)
823 static int fd_read_from_file(FileData *filedata, void *buffer, int size)
825 int readsize = read(filedata->filedes, buffer, size);
830 filedata->seek += readsize;
836 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, int size)
838 int readsize = gzread(filedata->gzfiledes, buffer, size);
843 filedata->seek += readsize;
849 static int fd_read_from_memory(FileData *filedata, void *buffer, int size)
851 // don't read more bytes then there are available in the buffer
852 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
854 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
855 filedata->seek += readsize;
860 static int fd_read_from_memfile(FileData *filedata, void *buffer, int size)
862 static unsigned int seek= 1<<30; /* the current position */
863 static unsigned int offset= 0; /* size of previous chunks */
864 static MemFileChunk *chunk=NULL;
865 unsigned int chunkoffset, readsize, totread;
867 if(size==0) return 0;
869 if(seek != (unsigned int)filedata->seek) {
870 chunk= filedata->memfile->chunks.first;
874 if(seek + chunk->size > (unsigned) filedata->seek) break;
879 seek= filedata->seek;
886 /* first check if it's on the end if current chunk */
887 if(seek-offset == chunk->size) {
888 offset+= chunk->size;
892 /* debug, should never happen */
894 printf("illegal read, chunk zero\n");
898 chunkoffset= seek-offset;
899 readsize= size-totread;
901 /* data can be spread over multiple chunks, so clamp size
902 * to within this chunk, and then it will read further in
904 if(chunkoffset+readsize > chunk->size)
905 readsize= chunk->size-chunkoffset;
907 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
909 filedata->seek += readsize;
911 } while(totread < size);
919 static FileData *filedata_new(void)
921 extern unsigned char DNAstr[]; /* DNA.c */
923 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
926 fd->gzfiledes = NULL;
928 /* XXX, this doesn't need to be done all the time,
929 * but it keeps us reentrant, remove once we have
930 * a lib that provides a nice lock. - zr
932 fd->memsdna = dna_sdna_from_data(DNAstr, DNAlen, 0);
934 fd->datamap = oldnewmap_new();
935 fd->globmap = oldnewmap_new();
936 fd->libmap = oldnewmap_new();
941 static FileData *blo_decode_and_check(FileData *fd, BlendReadError *error_r)
943 decode_blender_header(fd);
945 if (fd->flags & FD_FLAGS_FILE_OK) {
946 if (!read_file_dna(fd)) {
947 *error_r = BRE_INCOMPLETE;
948 blo_freefiledata(fd);
953 *error_r = BRE_NOT_A_BLEND;
954 blo_freefiledata(fd);
961 /* cannot be called with relative paths anymore! */
962 /* on each new library added, it now checks for the current FileData and expands relativeness */
963 FileData *blo_openblenderfile(char *name, BlendReadError *error_r)
967 gzfile= gzopen(name, "rb");
969 if (NULL == gzfile) {
970 *error_r = BRE_UNABLE_TO_OPEN;
973 FileData *fd = filedata_new();
974 fd->gzfiledes = gzfile;
975 fd->read = fd_read_gzip_from_file;
977 /* needed for library_append and read_libraries */
978 BLI_strncpy(fd->filename, name, sizeof(fd->filename));
980 return blo_decode_and_check(fd, error_r);
984 FileData *blo_openblendermemory(void *mem, int memsize, BlendReadError *error_r)
986 if (!mem || memsize<SIZEOFBLENDERHEADER) {
987 *error_r = mem?BRE_UNABLE_TO_READ:BRE_UNABLE_TO_OPEN;
990 FileData *fd= filedata_new();
992 fd->buffersize= memsize;
993 fd->read= fd_read_from_memory;
994 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
996 return blo_decode_and_check(fd, error_r);
1000 FileData *blo_openblendermemfile(MemFile *memfile, BlendReadError *error_r)
1003 *error_r = BRE_UNABLE_TO_OPEN;
1006 FileData *fd= filedata_new();
1007 fd->memfile= memfile;
1009 fd->read= fd_read_from_memfile;
1010 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
1012 return blo_decode_and_check(fd, error_r);
1017 void blo_freefiledata(FileData *fd)
1021 if (fd->filedes != -1) {
1025 if (fd->gzfiledes != NULL)
1027 gzclose(fd->gzfiledes);
1030 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1031 MEM_freeN(fd->buffer);
1035 // Free all BHeadN data blocks
1036 BLI_freelistN(&fd->listbase);
1039 dna_freestructDNA(fd->memsdna);
1041 dna_freestructDNA(fd->filesdna);
1043 MEM_freeN(fd->compflags);
1046 oldnewmap_free(fd->datamap);
1048 oldnewmap_free(fd->globmap);
1050 oldnewmap_free(fd->imamap);
1051 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1052 oldnewmap_free(fd->libmap);
1054 MEM_freeN(fd->bheadmap);
1060 /* ************ DIV ****************** */
1062 int BLO_has_bfile_extension(char *str)
1064 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1067 /* ************** OLD POINTERS ******************* */
1069 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1071 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1074 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1076 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1079 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1081 if(fd->imamap && adr)
1082 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1087 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1089 return oldnewmap_liblookup(fd->libmap, adr, lib);
1092 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1094 ID *id= newlibadr(fd, lib, adr);
1102 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1106 for (i=0; i<fd->libmap->nentries; i++) {
1107 OldNew *entry= &fd->libmap->entries[i];
1109 if (old==entry->newp && entry->nr==ID_ID) {
1111 if(new) entry->nr= GS( ((ID *)new)->name );
1117 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1121 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1124 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1128 change_idid_adr_fd(fd, old, new);
1133 /* lib linked proxy objects point to our local data, we need
1134 * to clear that pointer before reading the undo memfile since
1135 * the object might be removed, it is set again in reading
1136 * if the local object still exists */
1137 void blo_clear_proxy_pointers_from_lib(FileData *fd)
1139 Object *ob= G.main->object.first;
1141 for(;ob; ob= ob->id.next)
1143 ob->proxy_from= NULL;
1146 /* assumed; G.main still exists */
1147 void blo_make_image_pointer_map(FileData *fd)
1149 Image *ima= G.main->image.first;
1150 Scene *sce= G.main->scene.first;
1152 fd->imamap= oldnewmap_new();
1154 for(;ima; ima= ima->id.next) {
1155 Link *ibuf= ima->ibufs.first;
1156 for(; ibuf; ibuf= ibuf->next)
1157 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1159 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1161 for(; sce; sce= sce->id.next) {
1164 for(node= sce->nodetree->nodes.first; node; node= node->next)
1165 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1170 /* set G.main image ibufs to zero if it has been restored */
1171 /* this works because freeing G.main only happens after this call */
1172 void blo_end_image_pointer_map(FileData *fd)
1174 OldNew *entry= fd->imamap->entries;
1175 Image *ima= G.main->image.first;
1176 Scene *sce= G.main->scene.first;
1179 /* used entries were restored, so we put them to zero */
1180 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1185 for(;ima; ima= ima->id.next) {
1188 /* this mirrors direct_link_image */
1189 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1191 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1192 BLI_remlink(&ima->ibufs, ibuf);
1194 ima->gputexture= NULL;
1198 ima->gputexture= newimaadr(fd, ima->gputexture);
1200 for(; sce; sce= sce->id.next) {
1203 for(node= sce->nodetree->nodes.first; node; node= node->next)
1204 node->preview= newimaadr(fd, node->preview);
1209 /* undo file support: add all library pointers in lookup */
1210 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1212 Main *ptr= mainlist->first;
1213 ListBase *lbarray[MAX_LIBARRAY];
1215 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1216 int i= set_listbasepointers(ptr, lbarray);
1219 for(id= lbarray[i]->first; id; id= id->next)
1220 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1226 /* ********** END OLD POINTERS ****************** */
1227 /* ********** READ FILE ****************** */
1229 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1231 int blocksize, nblocks;
1234 data= (char *)(bhead+1);
1235 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1239 dna_switch_endian_struct(filesdna, bhead->SDNAnr, data);
1245 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1250 /* switch is based on file dna */
1251 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1252 switch_endian_structs(fd->filesdna, bh);
1254 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1255 if(fd->compflags[bh->SDNAnr]==2) {
1256 temp= dna_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1258 temp= MEM_mallocN(bh->len, blockname);
1259 memcpy(temp, (bh+1), bh->len);
1267 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1271 if(lb->first==NULL) return;
1273 lb->first= newdataadr(fd, lb->first);
1277 ln->next= newdataadr(fd, ln->next);
1285 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1290 if(lb->first==0) return;
1291 poin= newdataadr(fd, lb->first);
1293 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1300 poin= newdataadr(fd, ln->next);
1302 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1312 static void test_pointer_array(FileData *fd, void **mat)
1314 #if defined(WIN32) && !defined(FREE_WINDOWS)
1315 __int64 *lpoin, *lmat;
1317 long long *lpoin, *lmat;
1319 int len, *ipoin, *imat;
1321 /* manually convert the pointer array in
1322 * the old dna format to a pointer array in
1323 * the new dna format.
1326 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1328 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1329 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1333 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1334 SWITCH_LONGINT(*lpoin);
1335 *ipoin= (int) ((*lpoin) >> 3);
1343 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1344 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1358 /* ************ READ ID Properties *************** */
1360 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd);
1361 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd);
1363 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, void *fd)
1367 /*since we didn't save the extra buffer, set totallen to len.*/
1368 prop->totallen = prop->len;
1369 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1371 if (switch_endian) {
1372 if (prop->subtype != IDP_DOUBLE) {
1373 for (i=0; i<prop->len; i++) {
1374 SWITCH_INT(((int*)prop->data.pointer)[i]);
1377 for (i=0; i<prop->len; i++) {
1378 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1384 static void IDP_DirectLinkString(IDProperty *prop, int switch_endian, void *fd)
1386 /*since we didn't save the extra string buffer, set totallen to len.*/
1387 prop->totallen = prop->len;
1388 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1391 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, void *fd)
1393 ListBase *lb = &prop->data.group;
1398 /*Link child id properties now*/
1399 for (loop=prop->data.group.first; loop; loop=loop->next) {
1400 IDP_DirectLinkProperty(loop, switch_endian, fd);
1404 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd)
1406 switch (prop->type) {
1408 IDP_DirectLinkGroup(prop, switch_endian, fd);
1411 IDP_DirectLinkString(prop, switch_endian, fd);
1414 IDP_DirectLinkArray(prop, switch_endian, fd);
1417 /*erg, stupid doubles. since I'm storing them
1418 in the same field as int val; val2 in the
1419 IDPropertyData struct, they have to deal with
1420 endianness specifically
1422 in theory, val and val2 would've already been swapped
1423 if switch_endian is true, so we have to first unswap
1424 them then reswap them as a single 64-bit entity.
1427 if (switch_endian) {
1428 SWITCH_INT(prop->data.val);
1429 SWITCH_INT(prop->data.val2);
1430 SWITCH_LONGINT(prop->data.val);
1438 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd)
1442 /* ************ READ Brush *************** */
1443 /* library brush linking after fileread */
1444 static void lib_link_brush(FileData *fd, Main *main)
1450 /* only link ID pointers */
1451 for(brush= main->brush.first; brush; brush= brush->id.next) {
1452 if(brush->id.flag & LIB_NEEDLINK) {
1453 brush->id.flag -= LIB_NEEDLINK;
1455 for(a=0; a<MAX_MTEX; a++) {
1456 mtex= brush->mtex[a];
1458 mtex->tex= newlibadr_us(fd, brush->id.lib, mtex->tex);
1464 static void direct_link_brush(FileData *fd, Brush *brush)
1466 /* brush itself has been read */
1469 for(a=0; a<MAX_MTEX; a++)
1470 brush->mtex[a]= newdataadr(fd, brush->mtex[a]);
1473 static void direct_link_script(FileData *fd, Script *script)
1476 SCRIPT_SET_NULL(script)
1479 /* ************ READ CurveMapping *************** */
1481 /* cuma itself has been read! */
1482 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1486 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1487 cumap->flag &= ~CUMA_PREMULLED;
1489 for(a=0; a<CM_TOT; a++) {
1490 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1491 cumap->cm[a].table= NULL;
1495 /* ************ READ NODE TREE *************** */
1497 /* singe node tree (also used for material/scene trees), ntree is not NULL */
1498 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
1502 for(node= ntree->nodes.first; node; node= node->next)
1503 node->id= newlibadr_us(fd, id->lib, node->id);
1506 /* library ntree linking after fileread */
1507 static void lib_link_nodetree(FileData *fd, Main *main)
1511 /* only link ID pointers */
1512 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1513 if(ntree->id.flag & LIB_NEEDLINK) {
1514 ntree->id.flag -= LIB_NEEDLINK;
1515 lib_link_ntree(fd, &ntree->id, ntree);
1520 /* verify types for nodes and groups, all data has to be read */
1521 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
1523 static void lib_verify_nodetree(Main *main, int open)
1530 /* this crashes blender on undo/redo
1532 reinit_nodesystem();
1535 /* now create the own typeinfo structs an verify nodes */
1536 /* here we still assume no groups in groups */
1537 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1538 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
1539 ntreeMakeOwnType(ntree); /* for group usage */
1542 /* now verify all types in material trees, groups are set OK now */
1543 for(ma= main->mat.first; ma; ma= ma->id.next) {
1545 ntreeVerifyTypes(ma->nodetree);
1547 /* and scene trees */
1548 for(sce= main->scene.first; sce; sce= sce->id.next) {
1550 ntreeVerifyTypes(sce->nodetree);
1552 /* and texture trees */
1553 for(tx= main->tex.first; tx; tx= tx->id.next) {
1555 ntreeVerifyTypes(tx->nodetree);
1561 /* ntree itself has been read! */
1562 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
1564 /* note: writing and reading goes in sync, for speed */
1569 ntree->init= 0; /* to set callbacks and force setting types */
1570 ntree->owntype= NULL;
1571 ntree->timecursor= NULL;
1573 link_list(fd, &ntree->nodes);
1574 for(node= ntree->nodes.first; node; node= node->next) {
1575 if(node->type == NODE_DYNAMIC) {
1577 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
1578 node->typeinfo= NULL;
1581 node->storage= newdataadr(fd, node->storage);
1584 /* could be handlerized at some point */
1585 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
1586 direct_link_curvemapping(fd, node->storage);
1587 else if(ntree->type==NTREE_COMPOSIT) {
1588 if( ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB))
1589 direct_link_curvemapping(fd, node->storage);
1590 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
1591 ((ImageUser *)node->storage)->ok= 1;
1593 else if( ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) ) {
1594 direct_link_curvemapping(fd, node->storage);
1597 link_list(fd, &node->inputs);
1598 link_list(fd, &node->outputs);
1600 link_list(fd, &ntree->links);
1602 /* and we connect the rest */
1603 for(node= ntree->nodes.first; node; node= node->next) {
1604 node->preview= newimaadr(fd, node->preview);
1606 for(sock= node->inputs.first; sock; sock= sock->next)
1607 sock->link= newdataadr(fd, sock->link);
1608 for(sock= node->outputs.first; sock; sock= sock->next)
1609 sock->ns.data= NULL;
1611 for(link= ntree->links.first; link; link= link->next) {
1612 link->fromnode= newdataadr(fd, link->fromnode);
1613 link->tonode= newdataadr(fd, link->tonode);
1614 link->fromsock= newdataadr(fd, link->fromsock);
1615 link->tosock= newdataadr(fd, link->tosock);
1618 /* set selin and selout */
1619 for(node= ntree->nodes.first; node; node= node->next) {
1620 for(sock= node->inputs.first; sock; sock= sock->next) {
1621 if(sock->flag & SOCK_SEL) {
1626 for(sock= node->outputs.first; sock; sock= sock->next) {
1627 if(sock->flag & SOCK_SEL) {
1628 ntree->selout= sock;
1634 /* type verification is in lib-link */
1637 /* ************ READ PACKEDFILE *************** */
1639 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1641 PackedFile *pf= newdataadr(fd, oldpf);
1644 pf->data= newdataadr(fd, pf->data);
1650 /* ************ READ IMAGE PREVIEW *************** */
1652 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1654 PreviewImage *prv= newdataadr(fd, old_prv);
1658 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1660 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1668 /* ************ READ SCRIPTLINK *************** */
1670 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1674 for(i=0; i<slink->totscript; i++) {
1675 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1679 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1681 slink->scripts= newdataadr(fd, slink->scripts);
1682 test_pointer_array(fd, (void **)&slink->scripts);
1684 slink->flag= newdataadr(fd, slink->flag);
1686 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1689 for(a=0; a<slink->totscript; a++) {
1690 SWITCH_SHORT(slink->flag[a]);
1695 /* ************ READ ARMATURE ***************** */
1697 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1699 bActionStrip *strip;
1700 bActionModifier *amod;
1702 for (strip=striplist->first; strip; strip=strip->next){
1703 strip->object = newlibadr(fd, id->lib, strip->object);
1704 strip->act = newlibadr_us(fd, id->lib, strip->act);
1705 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1706 for(amod= strip->modifiers.first; amod; amod= amod->next)
1707 amod->ob= newlibadr(fd, id->lib, amod->ob);
1711 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1713 bConstraintChannel *chan;
1715 for (chan=chanbase->first; chan; chan=chan->next){
1716 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1720 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
1724 for (con = conlist->first; con; con=con->next) {
1725 /* patch for error introduced by changing constraints (dunno how) */
1726 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
1727 if(con->data==NULL) {
1728 con->type= CONSTRAINT_TYPE_NULL;
1730 /* own ipo, all constraints have it */
1731 con->ipo= newlibadr_us(fd, id->lib, con->ipo);
1733 switch (con->type) {
1734 case CONSTRAINT_TYPE_PYTHON:
1736 bPythonConstraint *data= (bPythonConstraint*)con->data;
1737 bConstraintTarget *ct;
1739 for (ct= data->targets.first; ct; ct= ct->next)
1740 ct->tar = newlibadr(fd, id->lib, ct->tar);
1742 data->text = newlibadr(fd, id->lib, data->text);
1743 //IDP_LibLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1746 case CONSTRAINT_TYPE_ACTION:
1748 bActionConstraint *data;
1749 data= ((bActionConstraint*)con->data);
1750 data->tar = newlibadr(fd, id->lib, data->tar);
1751 data->act = newlibadr(fd, id->lib, data->act);
1754 case CONSTRAINT_TYPE_LOCLIKE:
1756 bLocateLikeConstraint *data;
1757 data= ((bLocateLikeConstraint*)con->data);
1758 data->tar = newlibadr(fd, id->lib, data->tar);
1761 case CONSTRAINT_TYPE_ROTLIKE:
1763 bRotateLikeConstraint *data;
1764 data= ((bRotateLikeConstraint*)con->data);
1765 data->tar = newlibadr(fd, id->lib, data->tar);
1768 case CONSTRAINT_TYPE_SIZELIKE:
1770 bSizeLikeConstraint *data;
1771 data= ((bSizeLikeConstraint*)con->data);
1772 data->tar = newlibadr(fd, id->lib, data->tar);
1775 case CONSTRAINT_TYPE_KINEMATIC:
1777 bKinematicConstraint *data;
1778 data = ((bKinematicConstraint*)con->data);
1779 data->tar = newlibadr(fd, id->lib, data->tar);
1780 data->poletar = newlibadr(fd, id->lib, data->poletar);
1783 case CONSTRAINT_TYPE_TRACKTO:
1785 bTrackToConstraint *data;
1786 data = ((bTrackToConstraint*)con->data);
1787 data->tar = newlibadr(fd, id->lib, data->tar);
1790 case CONSTRAINT_TYPE_MINMAX:
1792 bMinMaxConstraint *data;
1793 data = ((bMinMaxConstraint*)con->data);
1794 data->tar = newlibadr(fd, id->lib, data->tar);
1797 case CONSTRAINT_TYPE_LOCKTRACK:
1799 bLockTrackConstraint *data;
1800 data= ((bLockTrackConstraint*)con->data);
1801 data->tar = newlibadr(fd, id->lib, data->tar);
1804 case CONSTRAINT_TYPE_FOLLOWPATH:
1806 bFollowPathConstraint *data;
1807 data= ((bFollowPathConstraint*)con->data);
1808 data->tar = newlibadr(fd, id->lib, data->tar);
1811 case CONSTRAINT_TYPE_STRETCHTO:
1813 bStretchToConstraint *data;
1814 data= ((bStretchToConstraint*)con->data);
1815 data->tar = newlibadr(fd, id->lib, data->tar);
1818 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
1820 bRigidBodyJointConstraint *data;
1821 data= ((bRigidBodyJointConstraint*)con->data);
1822 data->tar = newlibadr(fd, id->lib, data->tar);
1825 case CONSTRAINT_TYPE_CLAMPTO:
1827 bClampToConstraint *data;
1828 data= ((bClampToConstraint*)con->data);
1829 data->tar = newlibadr(fd, id->lib, data->tar);
1832 case CONSTRAINT_TYPE_CHILDOF:
1834 bChildOfConstraint *data;
1835 data= ((bChildOfConstraint*)con->data);
1836 data->tar = newlibadr(fd, id->lib, data->tar);
1839 case CONSTRAINT_TYPE_TRANSFORM:
1841 bTransformConstraint *data;
1842 data= ((bTransformConstraint*)con->data);
1843 data->tar = newlibadr(fd, id->lib, data->tar);
1846 case CONSTRAINT_TYPE_DISTLIMIT:
1848 bDistLimitConstraint *data;
1849 data= ((bDistLimitConstraint*)con->data);
1850 data->tar = newlibadr(fd, id->lib, data->tar);
1853 case CONSTRAINT_TYPE_NULL:
1859 static void direct_link_constraints(FileData *fd, ListBase *lb)
1864 for (cons=lb->first; cons; cons=cons->next) {
1865 cons->data = newdataadr(fd, cons->data);
1867 if (cons->type == CONSTRAINT_TYPE_PYTHON) {
1868 bPythonConstraint *data= cons->data;
1869 link_list(fd, &data->targets);
1870 data->prop = newdataadr(fd, data->prop);
1871 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1876 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
1878 bPoseChannel *pchan;
1879 bArmature *arm= ob->data;
1885 /* always rebuild to match proxy or lib changes */
1886 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
1888 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
1889 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
1891 /* hurms... loop in a loop, but yah... later... (ton) */
1892 pchan->bone= get_named_bone(arm, pchan->name);
1894 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
1895 if(pchan->bone==NULL)
1897 else if(ob->id.lib==NULL && arm->id.lib) {
1898 /* local pose selection copied to armature, bit hackish */
1899 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1900 pchan->bone->flag |= pchan->selectflag;
1905 ob->recalc= OB_RECALC;
1906 pose->flag |= POSE_RECALC;
1910 static void lib_link_armature(FileData *fd, Main *main)
1914 arm= main->armature.first;
1917 if(arm->id.flag & LIB_NEEDLINK) {
1918 arm->id.flag -= LIB_NEEDLINK;
1924 static void lib_link_action(FileData *fd, Main *main)
1927 bActionChannel *chan;
1929 act= main->action.first;
1931 if(act->id.flag & LIB_NEEDLINK) {
1932 act->id.flag -= LIB_NEEDLINK;
1934 for (chan=act->chanbase.first; chan; chan=chan->next) {
1935 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1936 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1944 static void direct_link_bones(FileData *fd, Bone* bone)
1948 bone->parent= newdataadr(fd, bone->parent);
1950 link_list(fd, &bone->childbase);
1952 for (child=bone->childbase.first; child; child=child->next) {
1953 direct_link_bones(fd, child);
1958 static void direct_link_action(FileData *fd, bAction *act)
1960 bActionChannel *achan;
1963 link_list(fd, &act->chanbase);
1964 link_list(fd, &act->groups);
1965 link_list(fd, &act->markers);
1967 for (achan = act->chanbase.first; achan; achan=achan->next) {
1968 achan->grp= newdataadr(fd, achan->grp);
1970 link_list(fd, &achan->constraintChannels);
1973 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1974 if (agrp->channels.first) {
1975 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1976 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1981 static void direct_link_armature(FileData *fd, bArmature *arm)
1985 link_list(fd, &arm->bonebase);
1987 bone=arm->bonebase.first;
1989 direct_link_bones(fd, bone);
1994 /* ************ READ CAMERA ***************** */
1996 static void lib_link_camera(FileData *fd, Main *main)
2000 ca= main->camera.first;
2002 if(ca->id.flag & LIB_NEEDLINK) {
2004 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo);
2006 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2008 lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
2010 ca->id.flag -= LIB_NEEDLINK;
2016 static void direct_link_camera(FileData *fd, Camera *ca)
2018 direct_link_scriptlink(fd, &ca->scriptlink);
2022 /* ************ READ LAMP ***************** */
2024 static void lib_link_lamp(FileData *fd, Main *main)
2030 la= main->lamp.first;
2032 if(la->id.flag & LIB_NEEDLINK) {
2034 for(a=0; a<MAX_MTEX; a++) {
2037 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2038 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2042 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo);
2044 lib_link_scriptlink(fd, &la->id, &la->scriptlink);
2046 la->id.flag -= LIB_NEEDLINK;
2052 static void direct_link_lamp(FileData *fd, Lamp *la)
2056 direct_link_scriptlink(fd, &la->scriptlink);
2058 for(a=0; a<MAX_MTEX; a++) {
2059 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2062 la->curfalloff= newdataadr(fd, la->curfalloff);
2064 direct_link_curvemapping(fd, la->curfalloff);
2066 la->preview = direct_link_preview_image(fd, la->preview);
2069 /* ************ READ keys ***************** */
2071 static void lib_link_key(FileData *fd, Main *main)
2075 key= main->key.first;
2077 if(key->id.flag & LIB_NEEDLINK) {
2079 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo);
2080 key->from= newlibadr(fd, key->id.lib, key->from);
2082 key->id.flag -= LIB_NEEDLINK;
2088 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2091 char *data, *poin, *cp;
2093 elemsize= key->elemsize;
2096 for(a=0; a<kb->totelem; a++) {
2101 while( cp[0] ) { /* cp[0]==amount */
2103 switch(cp[1]) { /* cp[1]= type */
2109 SWITCH_INT((*poin));
2122 static void direct_link_key(FileData *fd, Key *key)
2126 link_list(fd, &(key->block));
2128 key->refkey= newdataadr(fd, key->refkey);
2130 kb= key->block.first;
2133 kb->data= newdataadr(fd, kb->data);
2135 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2136 switch_endian_keyblock(key, kb);
2142 /* ************ READ mball ***************** */
2144 static void lib_link_mball(FileData *fd, Main *main)
2149 mb= main->mball.first;
2151 if(mb->id.flag & LIB_NEEDLINK) {
2153 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2155 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo);
2157 mb->id.flag -= LIB_NEEDLINK;
2163 static void direct_link_mball(FileData *fd, MetaBall *mb)
2165 mb->mat= newdataadr(fd, mb->mat);
2166 test_pointer_array(fd, (void **)&mb->mat);
2168 link_list(fd, &(mb->elems));
2170 mb->disp.first= mb->disp.last= 0;
2175 /* ************ READ WORLD ***************** */
2177 static void lib_link_world(FileData *fd, Main *main)
2183 wrld= main->world.first;
2185 if(wrld->id.flag & LIB_NEEDLINK) {
2187 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo);
2189 for(a=0; a<MAX_MTEX; a++) {
2190 mtex= wrld->mtex[a];
2192 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2193 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2197 lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
2199 wrld->id.flag -= LIB_NEEDLINK;
2201 wrld= wrld->id.next;
2205 static void direct_link_world(FileData *fd, World *wrld)
2209 direct_link_scriptlink(fd, &wrld->scriptlink);
2211 for(a=0; a<MAX_MTEX; a++) {
2212 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2214 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2218 /* ************ READ IPO ***************** */
2220 static void lib_link_ipo(FileData *fd, Main *main)
2224 ipo= main->ipo.first;
2226 if(ipo->id.flag & LIB_NEEDLINK) {
2228 for(icu= ipo->curve.first; icu; icu= icu->next) {
2230 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
2232 ipo->id.flag -= LIB_NEEDLINK;
2238 static void direct_link_ipo(FileData *fd, Ipo *ipo)
2242 link_list(fd, &(ipo->curve));
2243 icu= ipo->curve.first;
2245 icu->bezt= newdataadr(fd, icu->bezt);
2246 icu->bp= newdataadr(fd, icu->bp);
2247 icu->driver= newdataadr(fd, icu->driver);
2252 /* ************ READ VFONT ***************** */
2254 static void lib_link_vfont(FileData *fd, Main *main)
2258 vf= main->vfont.first;
2260 if(vf->id.flag & LIB_NEEDLINK) {
2261 vf->id.flag -= LIB_NEEDLINK;
2267 static void direct_link_vfont(FileData *fd, VFont *vf)
2270 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2273 /* ************ READ TEXT ****************** */
2275 static void lib_link_text(FileData *fd, Main *main)
2279 text= main->text.first;
2281 if(text->id.flag & LIB_NEEDLINK) {
2282 text->id.flag -= LIB_NEEDLINK;
2284 text= text->id.next;
2288 static void direct_link_text(FileData *fd, Text *text)
2292 text->name= newdataadr(fd, text->name);
2295 text->undo_len= TXT_INIT_UNDO;
2296 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2298 text->compiled= NULL;
2301 if(text->flags & TXT_ISEXT) {
2306 link_list(fd, &text->lines);
2307 link_list(fd, &text->markers);
2309 text->curl= newdataadr(fd, text->curl);
2310 text->sell= newdataadr(fd, text->sell);
2312 ln= text->lines.first;
2314 ln->line= newdataadr(fd, ln->line);
2317 if (ln->len != (int) strlen(ln->line)) {
2318 printf("Error loading text, line lengths differ\n");
2319 ln->len = strlen(ln->line);
2325 text->flags = (text->flags|TXT_ISTMP) & ~TXT_ISEXT;
2330 /* ************ READ IMAGE ***************** */
2332 static void lib_link_image(FileData *fd, Main *main)
2336 ima= main->image.first;
2338 if(ima->id.flag & LIB_NEEDLINK) {
2339 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2341 ima->id.flag -= LIB_NEEDLINK;
2347 static void link_ibuf_list(FileData *fd, ListBase *lb)
2351 if(lb->first==NULL) return;
2353 lb->first= newimaadr(fd, lb->first);
2357 ln->next= newimaadr(fd, ln->next);
2365 static void direct_link_image(FileData *fd, Image *ima)
2367 /* for undo system, pointers could be restored */
2369 link_ibuf_list(fd, &ima->ibufs);
2371 ima->ibufs.first= ima->ibufs.last= NULL;
2373 /* if not restored, we keep the binded opengl index */
2374 if(ima->ibufs.first==NULL) {
2376 ima->gputexture= NULL;
2383 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2384 ima->preview = direct_link_preview_image(fd, ima->preview);
2389 /* ************ READ CURVE ***************** */
2391 static void lib_link_curve(FileData *fd, Main *main)
2396 cu= main->curve.first;
2398 if(cu->id.flag & LIB_NEEDLINK) {
2400 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2402 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2403 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2404 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2405 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2406 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2407 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2408 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2410 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo);
2411 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2413 cu->id.flag -= LIB_NEEDLINK;
2420 static void switch_endian_knots(Nurb *nu)
2427 SWITCH_INT(nu->knotsu[len]);
2433 SWITCH_INT(nu->knotsv[len]);
2438 static void direct_link_curve(FileData *fd, Curve *cu)
2443 cu->mat= newdataadr(fd, cu->mat);
2444 test_pointer_array(fd, (void **)&cu->mat);
2445 cu->str= newdataadr(fd, cu->str);
2446 cu->strinfo= newdataadr(fd, cu->strinfo);
2447 cu->tb= newdataadr(fd, cu->tb);
2449 if(cu->vfont==0) link_list(fd, &(cu->nurb));
2451 cu->nurb.first=cu->nurb.last= 0;
2453 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2455 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2462 cu->tb[0].w = cu->linewidth;
2464 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2467 cu->bev.first=cu->bev.last= 0;
2468 cu->disp.first=cu->disp.last= 0;
2473 nu->bezt= newdataadr(fd, nu->bezt);
2474 nu->bp= newdataadr(fd, nu->bp);
2475 nu->knotsu= newdataadr(fd, nu->knotsu);
2476 nu->knotsv= newdataadr(fd, nu->knotsv);
2477 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2479 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2480 switch_endian_knots(nu);
2488 /* ************ READ TEX ***************** */
2490 static void lib_link_texture(FileData *fd, Main *main)
2494 tex= main->tex.first;
2496 if(tex->id.flag & LIB_NEEDLINK) {
2498 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2499 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2500 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2503 lib_link_ntree(fd, &tex->id, tex->nodetree);
2505 tex->id.flag -= LIB_NEEDLINK;
2511 static void direct_link_texture(FileData *fd, Tex *tex)
2513 tex->plugin= newdataadr(fd, tex->plugin);
2515 tex->plugin->handle= 0;
2516 open_plugin_tex(tex->plugin);
2517 /* initialize data for this instance, if an initialization
2520 if (tex->plugin->instance_init)
2521 tex->plugin->instance_init((void *) tex->plugin->data);
2523 tex->coba= newdataadr(fd, tex->coba);
2524 tex->env= newdataadr(fd, tex->env);
2526 tex->env->ima= NULL;
2527 memset(tex->env->cube, 0, 6*sizeof(void *));
2531 tex->nodetree= newdataadr(fd, tex->nodetree);
2533 direct_link_nodetree(fd, tex->nodetree);
2535 tex->preview = direct_link_preview_image(fd, tex->preview);
2542 /* ************ READ MATERIAL ***************** */
2544 static void lib_link_material(FileData *fd, Main *main)
2550 ma= main->mat.first;
2552 if(ma->id.flag & LIB_NEEDLINK) {
2553 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2554 of library blocks that implement this.*/
2555 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2557 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2558 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2560 for(a=0; a<MAX_MTEX; a++) {
2563 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2564 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2567 lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
2570 lib_link_ntree(fd, &ma->id, ma->nodetree);
2572 ma->id.flag -= LIB_NEEDLINK;
2578 static void direct_link_material(FileData *fd, Material *ma)
2582 for(a=0; a<MAX_MTEX; a++) {
2583 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2586 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2587 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2589 direct_link_scriptlink(fd, &ma->scriptlink);
2591 ma->nodetree= newdataadr(fd, ma->nodetree);
2593 direct_link_nodetree(fd, ma->nodetree);
2595 ma->preview = direct_link_preview_image(fd, ma->preview);
2596 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
2599 /* ************ READ PARTICLE SETTINGS ***************** */
2601 static void direct_link_pointcache(FileData *fd, PointCache *cache)
2603 cache->flag &= ~(PTCACHE_SIMULATION_VALID|PTCACHE_BAKE_EDIT_ACTIVE);
2607 static void lib_link_particlesettings(FileData *fd, Main *main)
2609 ParticleSettings *part;
2611 part= main->particle.first;
2613 if(part->id.flag & LIB_NEEDLINK) {
2614 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo);
2615 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
2616 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
2617 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
2618 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
2619 part->id.flag -= LIB_NEEDLINK;
2621 part= part->id.next;
2625 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
2627 part->pd= newdataadr(fd, part->pd);
2628 part->pd2= newdataadr(fd, part->pd2);
2631 static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase *particles)
2633 ParticleSystem *psys, *psysnext;
2636 for(psys=particles->first; psys; psys=psysnext){
2639 psysnext= psys->next;
2641 psys->part = newlibadr_us(fd, id->lib, psys->part);
2643 psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
2644 psys->keyed_ob = newlibadr(fd, id->lib, psys->keyed_ob);
2646 for(a=0,pa=psys->particles; a<psys->totpart; a++,pa++){
2647 pa->stick_ob=newlibadr(fd, id->lib, pa->stick_ob);
2651 /* particle modifier must be removed before particle system */
2652 ParticleSystemModifierData *psmd= psys_get_modifier(ob,psys);
2653 BLI_remlink(&ob->modifiers, psmd);
2654 modifier_free((ModifierData *)psmd);
2656 BLI_remlink(particles, psys);
2661 static void direct_link_particlesystems(FileData *fd, ListBase *particles)
2663 ParticleSystem *psys;
2666 for(psys=particles->first; psys; psys=psys->next) {
2667 psys->particles=newdataadr(fd,psys->particles);
2668 if(psys->particles && psys->particles->hair){
2669 ParticleData *pa = psys->particles;
2670 for(a=0; a<psys->totpart; a++, pa++)
2671 pa->hair=newdataadr(fd,pa->hair);
2673 if(psys->particles && psys->particles->keys){
2674 ParticleData *pa = psys->particles;
2675 for(a=0; a<psys->totpart; a++, pa++) {
2680 psys->flag &= ~PSYS_KEYED;
2682 psys->child=newdataadr(fd,psys->child);
2683 psys->effectors.first=psys->effectors.last=0;
2685 psys->soft= newdataadr(fd, psys->soft);
2687 SoftBody *sb = psys->soft;
2688 sb->particles = psys;
2689 sb->bpoint= NULL; // init pointers so it gets rebuilt nicely
2693 sb->pointcache= newdataadr(fd, sb->pointcache);
2695 direct_link_pointcache(fd, sb->pointcache);
2699 psys->pathcache = 0;
2700 psys->childcache = 0;
2701 psys->pathcachebufs.first = psys->pathcachebufs.last = 0;
2702 psys->childcachebufs.first = psys->childcachebufs.last = 0;
2703 psys->reactevents.first = psys->reactevents.last = 0;
2705 psys->pointcache= newdataadr(fd, psys->pointcache);
2706 if(psys->pointcache)
2707 direct_link_pointcache(fd, psys->pointcache);
2712 /* ************ READ MESH ***************** */
2714 static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
2719 for (i=0; i<totface; i++, tf++) {
2720 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
2721 if(tf->tpage && tf->tpage->id.us==0)
2722 tf->tpage->id.us= 1;
2726 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
2729 for(i=0; i<fdata->totlayer; i++) {
2730 CustomDataLayer *layer = &fdata->layers[i];
2732 if(layer->type == CD_MTFACE)
2733 lib_link_mtface(fd, me, layer->data, totface);
2738 static void lib_link_mesh(FileData *fd, Main *main)
2742 me= main->mesh.first;
2744 if(me->id.flag & LIB_NEEDLINK) {
2747 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2748 of library blocks that implement this.*/
2749 if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2751 /* this check added for python created meshes */
2753 for(i=0; i<me->totcol; i++) {
2754 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
2759 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
2760 me->key= newlibadr_us(fd, me->id.lib, me->key);
2761 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
2763 lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
2764 if(me->mr && me->mr->levels.first)
2765 lib_link_customdata_mtface(fd, me, &me->mr->fdata,
2766 ((MultiresLevel*)me->mr->levels.first)->totface);
2768 me->id.flag -= LIB_NEEDLINK;
2774 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
2781 for (i=0; i<count; i++) {
2782 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
2784 mdverts[i].totweight=0;
2788 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
2792 data->layers= newdataadr(fd, data->layers);
2794 while (i < data->totlayer) {
2795 CustomDataLayer *layer = &data->layers[i];
2797 if (CustomData_verify_versions(data, i)) {
2798 layer->data = newdataadr(fd, layer->data);
2804 static void direct_link_mesh(FileData *fd, Mesh *mesh)
2806 mesh->mat= newdataadr(fd, mesh->mat);
2807 test_pointer_array(fd, (void **)&mesh->mat);
2809 mesh->mvert= newdataadr(fd, mesh->mvert);
2810 mesh->medge= newdataadr(fd, mesh->medge);
2811 mesh->mface= newdataadr(fd, mesh->mface);
2812 mesh->tface= newdataadr(fd, mesh->tface);
2813 mesh->mtface= newdataadr(fd, mesh->mtface);
2814 mesh->mcol= newdataadr(fd, mesh->mcol);
2815 mesh->msticky= newdataadr(fd, mesh->msticky);
2816 mesh->dvert= newdataadr(fd, mesh->dvert);
2818 /* Partial-mesh visibility (do this before using totvert, totface, or totedge!) */
2819 mesh->pv= newdataadr(fd, mesh->pv);
2821 mesh->pv->vert_map= newdataadr(fd, mesh->pv->vert_map);
2822 mesh->pv->edge_map= newdataadr(fd, mesh->pv->edge_map);
2823 mesh->pv->old_faces= newdataadr(fd, mesh->pv->old_faces);
2824 mesh->pv->old_edges= newdataadr(fd, mesh->pv->old_edges);
2827 /* normally direct_link_dverts should be called in direct_link_customdata,
2828 but for backwards compat in do_versions to work we do it here */
2829 direct_link_dverts(fd, mesh->pv ? mesh->pv->totvert : mesh->totvert, mesh->dvert);
2831 direct_link_customdata(fd, &mesh->vdata, mesh->pv ? mesh->pv->totvert : mesh->totvert);
2832 direct_link_customdata(fd, &mesh->edata, mesh->pv ? mesh->pv->totedge : mesh->totedge);
2833 direct_link_customdata(fd, &mesh->fdata, mesh->pv ? mesh->pv->totface : mesh->totface);
2836 mesh->mselect = NULL;
2839 mesh->mr= newdataadr(fd, mesh->mr);
2843 link_list(fd, &mesh->mr->levels);
2844 lvl= mesh->mr->levels.first;
2846 direct_link_customdata(fd, &mesh->mr->vdata, lvl->totvert);
2847 direct_link_dverts(fd, lvl->totvert, CustomData_get(&mesh->mr->vdata, 0, CD_MDEFORMVERT));
2848 direct_link_customdata(fd, &mesh->mr->fdata, lvl->totface);
2850 if(mesh->mr->edge_flags)
2851 mesh->mr->edge_flags= newdataadr(fd, mesh->mr->edge_flags);
2852 if(mesh->mr->edge_creases)
2853 mesh->mr->edge_creases= newdataadr(fd, mesh->mr->edge_creases);
2855 if(!mesh->mr->edge_flags)
2856 mesh->mr->edge_flags= MEM_callocN(sizeof(short)*lvl->totedge, "Multires Edge Flags");
2857 if(!mesh->mr->edge_creases)
2858 mesh->mr->edge_creases= MEM_callocN(sizeof(char)*lvl->totedge, "Multires Edge Creases");
2860 mesh->mr->verts = newdataadr(fd, mesh->mr->verts);
2862 for(; lvl; lvl= lvl->next) {
2863 lvl->verts= newdataadr(fd, lvl->verts);
2864 lvl->faces= newdataadr(fd, lvl->faces);
2865 lvl->edges= newdataadr(fd, lvl->edges);
2866 lvl->colfaces= newdataadr(fd, lvl->colfaces);
2867 lvl->edge_boundary_states= NULL;
2868 lvl->vert_face_map = lvl->vert_edge_map = NULL;
2873 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
2874 TFace *tf= mesh->tface;
2877 for (i=0; i< (mesh->pv ? mesh->pv->totface : mesh->totface); i++, tf++) {
2878 SWITCH_INT(tf->col[0]);
2879 SWITCH_INT(tf->col[1]);
2880 SWITCH_INT(tf->col[2]);
2881 SWITCH_INT(tf->col[3]);
2886 /* ************ READ LATTICE ***************** */
2888 static void lib_link_latt(FileData *fd, Main *main)
2892 lt= main->latt.first;
2894 if(lt->id.flag & LIB_NEEDLINK) {
2896 lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo);
2897 lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
2899 lt->id.flag -= LIB_NEEDLINK;
2905 static void direct_link_latt(FileData *fd, Lattice *lt)
2907 lt->def= newdataadr(fd, lt->def);
2909 lt->dvert= newdataadr(fd, lt->dvert);
2910 direct_link_dverts(fd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
2914 /* ************ READ OBJECT ***************** */
2916 static void lib_link_modifiers__linkModifiers(void *userData, Object *ob,
2919 FileData *fd = userData;
2921 *idpoin = newlibadr(fd, ob->id.lib, *idpoin);
2922 /* hardcoded bad exception; non-object modifier data gets user count (texture, displace) */
2923 if(*idpoin && GS((*idpoin)->name)!=ID_OB)
2926 static void lib_link_modifiers(FileData *fd, Object *ob)
2928 modifiers_foreachIDLink(ob, lib_link_modifiers__linkModifiers, fd);
2931 static void lib_link_object(FileData *fd, Main *main)
2941 ob= main->object.first;
2943 if(ob->id.flag & LIB_NEEDLINK) {
2944 if (ob->id.properties) IDP_LibLinkProperty(ob->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2946 ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
2947 ob->track= newlibadr(fd, ob->id.lib, ob->track);
2948 ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
2949 ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
2950 ob->poselib= newlibadr_us(fd, ob->id.lib, ob->poselib);
2951 ob->dup_group= newlibadr_us(fd, ob->id.lib, ob->dup_group);
2953 ob->proxy= newlibadr_us(fd, ob->id.lib, ob->proxy);
2955 /* paranoia check, actually a proxy_from pointer should never be written... */
2956 if(ob->proxy->id.lib==NULL) {
2957 ob->proxy->proxy_from= NULL;
2961 /* this triggers object_update to always use a copy */
2962 ob->proxy->proxy_from= ob;
2963 /* force proxy updates after load/undo, a bit weak */
2964 ob->recalc= ob->proxy->recalc= OB_RECALC;
2967 ob->proxy_group= newlibadr(fd, ob->id.lib, ob->proxy_group);
2970 ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
2972 if(ob->data==NULL && poin!=NULL) {
2975 if(ob->id.lib) printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
2976 else printf("Object %s lost data.", ob->id.name+2);
2979 free_pose(ob->pose);
2981 ob->flag &= ~OB_POSEMODE;
2984 for(a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
2986 ob->id.flag -= LIB_NEEDLINK;
2987 /* if id.us==0 a new base will be created later on */
2989 /* WARNING! Also check expand_object(), should reflect the stuff below. */
2990 lib_link_pose(fd, ob, ob->pose);
2991 lib_link_constraints(fd, &ob->id, &ob->constraints);
2992 lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
2993 lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
2995 for(paf= ob->effect.first; paf; paf= paf->next) {
2996 if(paf->type==EFF_PARTICLE) {
2997 paf->group= newlibadr_us(fd, ob->id.lib, paf->group);
3001 sens= ob->sensors.first;
3003 for(a=0; a<sens->totlinks; a++)
3004 sens->links[a]= newglobadr(fd, sens->links[a]);
3006 if(sens->type==SENS_TOUCH) {
3007 bTouchSensor *ts= sens->data;
3008 ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
3010 else if(sens->type==SENS_MESSAGE) {
3011 bMessageSensor *ms= sens->data;
3013 newlibadr(fd, ob->id.lib, ms->fromObject);
3018 cont= ob->controllers.first;
3020 for(a=0; a<cont->totlinks; a++)
3021 cont->links[a]= newglobadr(fd, cont->links[a]);
3023 if(cont->type==CONT_PYTHON) {
3024 bPythonCont *pc= cont->data;
3025 pc->text= newlibadr(fd, ob->id.lib, pc->text);
3033 act= ob->actuators.first;
3035 if(act->type==ACT_SOUND) {
3036 bSoundActuator *sa= act->data;
3037 sa->sound= newlibadr_us(fd, ob->id.lib, sa->sound);
3039 else if(act->type==ACT_CD) {
3040 /* bCDActuator *cda= act->data; */
3042 else if(act->type==ACT_GAME) {
3043 /* bGameActuator *ga= act->data; */
3045 else if(act->type==ACT_CAMERA) {
3046 bCameraActuator *ca= act->data;
3047 ca->ob= newlibadr(fd, ob->id.lib, ca->ob);
3049 /* leave this one, it's obsolete but necessary to read for conversion */
3050 else if(act->type==ACT_ADD_OBJECT) {
3051 bAddObjectActuator *eoa= act->data;
3052 if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
3054 else if(act->type==ACT_EDIT_OBJECT) {
3055 bEditObjectActuator *eoa= act->data;
3060 eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
3061 eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
3064 else if(act->type==ACT_SCENE) {
3065 bSceneActuator *sa= act->data;
3066 sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
3067 sa->scene= newlibadr(fd, ob->id.lib, sa->scene);
3069 else if(act->type==ACT_ACTION) {
3070 bActionActuator *aa= act->data;
3071 aa->act= newlibadr(fd, ob->id.lib, aa->act);
3073 else if(act->type==ACT_SHAPEACTION) {
3074 bActionActuator *aa= act->data;
3075 aa->act= newlibadr(fd, ob->id.lib, aa->act);
3077 else if(act->type==ACT_PROPERTY) {
3078 bPropertyActuator *pa= act->data;
3079 pa->ob= newlibadr(fd, ob->id.lib, pa->ob);
3081 else if(act->type==ACT_MESSAGE) {
3082 bMessageActuator *ma= act->data;
3083 ma->toObject= newlibadr(fd, ob->id.lib, ma->toObject);
3085 else if(act->type==ACT_2DFILTER){
3086 bTwoDFilterActuator *_2dfa = act->data;
3087 _2dfa->text= newlibadr(fd, ob->id.lib, _2dfa->text);
3089 else if(act->type==ACT_PARENT) {
3090 bParentActuator *parenta = act->data;
3091 parenta->ob = newlibadr(fd, ob->id.lib, parenta->ob);
3093 else if(act->type==ACT_STATE) {
3094 /* bStateActuator *statea = act->data; */
3100 FluidsimModifierData *fluidmd = (FluidsimModifierData *)modifiers_findByType(ob, eModifierType_Fluidsim);
3102 if(fluidmd && fluidmd->fss)
3103 fluidmd->fss->ipo = newlibadr_us(fd, ob->id.lib, fluidmd->fss->ipo);
3109 ob->pd->tex=newlibadr_us(fd, ob->id.lib, ob->pd->tex);
3111 lib_link_scriptlink(fd, &ob->id, &ob->scriptlink);
3112 lib_link_particlesystems(fd, ob, &ob->id, &ob->particlesystem);
3113 lib_link_modifiers(fd, ob);
3118 if(warn) error("WARNING IN CONSOLE");
3122 static void direct_link_pose(FileData *fd, bPose *pose)
3124 bPoseChannel *pchan;
3129 link_list(fd, &pose->chanbase);
3130 link_list(fd, &pose->agroups);
3132 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
3134 pchan->parent= newdataadr(fd, pchan->parent);
3135 pchan->child= newdataadr(fd, pchan->child);
3136 direct_link_constraints(fd, &pchan->constraints);
3137 pchan->iktree.first= pchan->iktree.last= NULL;
3142 static void direct_link_modifiers(FileData *fd, ListBase *lb)
3148 for (md=lb->first; md; md=md->next) {
3151 /* if modifiers disappear, or for upward compatibility */
3152 if(NULL==modifierType_getInfo(md->type))
3153 md->type= eModifierType_None;
3155 if (md->type==eModifierType_Subsurf) {
3156 SubsurfModifierData *smd = (SubsurfModifierData*) md;
3158 smd->emCache = smd->mCache = 0;
3160 else if (md->type==eModifierType_Cloth) {
3161 ClothModifierData *clmd = (ClothModifierData*) md;