4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
39 #include "BLI_winstuff.h"
42 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
43 #include <stdlib.h> // for getenv atoi
44 #include <fcntl.h> // for open
45 #include <string.h> // for strrchr strncmp strstr
46 #include <math.h> // for fabs
49 #include <unistd.h> // for read close
50 #include <sys/param.h> // for MAXPATHLEN
52 #include <io.h> // for open close read
57 #include "DNA_action_types.h"
58 #include "DNA_armature_types.h"
60 #include "DNA_actuator_types.h"
61 #include "DNA_brush_types.h"
62 #include "DNA_camera_types.h"
63 #include "DNA_color_types.h"
64 #include "DNA_controller_types.h"
65 #include "DNA_constraint_types.h"
66 #include "DNA_curve_types.h"
67 #include "DNA_customdata_types.h"
68 #include "DNA_effect_types.h"
69 #include "DNA_fileglobal_types.h"
70 #include "DNA_group_types.h"
71 #include "DNA_ipo_types.h"
72 #include "DNA_image_types.h"
73 #include "DNA_key_types.h"
74 #include "DNA_lattice_types.h"
75 #include "DNA_lamp_types.h"
76 #include "DNA_meta_types.h"
77 #include "DNA_material_types.h"
78 #include "DNA_mesh_types.h"
79 #include "DNA_meshdata_types.h"
80 #include "DNA_modifier_types.h"
81 #include "DNA_nla_types.h"
82 #include "DNA_node_types.h"
83 #include "DNA_object_types.h"
84 #include "DNA_object_force.h"
85 #include "DNA_object_fluidsim.h" // NT
86 #include "DNA_oops_types.h"
87 #include "DNA_object_force.h"
88 #include "DNA_packedFile_types.h"
89 #include "DNA_property_types.h"
90 #include "DNA_text_types.h"
91 #include "DNA_view3d_types.h"
92 #include "DNA_screen_types.h"
93 #include "DNA_sensor_types.h"
94 #include "DNA_sdna_types.h"
95 #include "DNA_scene_types.h"
96 #include "DNA_sequence_types.h"
97 #include "DNA_sound_types.h"
98 #include "DNA_space_types.h"
99 #include "DNA_texture_types.h"
100 #include "DNA_userdef_types.h"
101 #include "DNA_vfont_types.h"
102 #include "DNA_world_types.h"
104 #include "MEM_guardedalloc.h"
105 #include "BLI_blenlib.h"
106 #include "BLI_arithb.h"
107 #include "BLI_storage_types.h" // for relname flags
109 #include "BDR_sculptmode.h"
111 #include "BKE_bad_level_calls.h" // for reopen_text build_seqar (from WHILE_SEQ) set_rects_butspace check_imasel_copy
113 #include "BKE_action.h"
114 #include "BKE_armature.h"
115 #include "BKE_colortools.h"
116 #include "BKE_constraint.h"
117 #include "BKE_curve.h"
118 #include "BKE_customdata.h"
119 #include "BKE_deform.h"
120 #include "BKE_depsgraph.h"
121 #include "BKE_effect.h" // for give_parteff
122 #include "BKE_global.h" // for G
123 #include "BKE_group.h"
124 #include "BKE_image.h"
125 #include "BKE_lattice.h"
126 #include "BKE_library.h" // for wich_libbase
127 #include "BKE_main.h" // for Main
128 #include "BKE_mesh.h" // for ME_ defines (patching)
129 #include "BKE_modifier.h"
130 #include "BKE_node.h" // for tree type defines
131 #include "BKE_object.h"
132 #include "BKE_property.h" // for get_property
133 #include "BKE_sca.h" // for init_actuator
134 #include "BKE_scene.h"
135 #include "BKE_softbody.h" // sbNew()
136 #include "BKE_texture.h" // for open_plugin_tex
137 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
138 #include "BKE_idprop.h"
140 #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
141 #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
142 #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
143 #include "BLO_readfile.h"
144 #include "BLO_undofile.h"
145 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
147 #include "multires.h"
149 #include "readfile.h"
153 #include "mydevice.h"
159 Remark: still a weak point is the newadress() function, that doesnt solve reading from
160 multiple files at the same time
162 (added remark: oh, i thought that was solved? will look at that... (ton)
165 - Existing Library (Main) push or free
175 - read associated 'direct data'
176 - link direct data (internal and to LibBlock)
178 - read USER data, only when indicated (file is ~/.B.blend)
180 - per Library (per Main)
183 - find LibBlocks and attach IDs to Main
184 - if external LibBlock
186 - or it's already read,
191 - read associated direct data
192 - link direct data (internal and to LibBlock)
194 - per Library with unread LibBlocks
199 - read associated direct data
200 - link direct data (internal and to LibBlock)
203 - link all LibBlocks and indirect pointers to libblocks
204 - initialize FileGlobal and copy pointers to Global
207 /* also occurs in library.c */
208 /* GS reads the memory pointed at in a specific ordering. There are,
209 * however two definitions for it. I have jotted them down here, both,
210 * but I think the first one is actually used. The thing is that
211 * big-endian systems might read this the wrong way round. OTOH, we
212 * constructed the IDs that are read out with this macro explicitly as
213 * well. I expect we'll sort it out soon... */
216 #define GS(a) (*((short *)(a)))
218 /* from misc_util: flip the bytes from x */
219 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
221 // only used here in readfile.c
222 #define SWITCH_LONGINT(a) { \
225 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
226 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
227 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
228 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
232 typedef struct OldNew {
237 typedef struct OldNewMap {
239 int nentries, entriessize;
245 /* local prototypes */
246 static void *read_struct(FileData *fd, BHead *bh, char *blockname);
249 static OldNewMap *oldnewmap_new(void)
251 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
253 onm->entriessize= 1024;
254 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
259 static int verg_oldnewmap(const void *v1, const void *v2)
261 const struct OldNew *x1=v1, *x2=v2;
263 if( x1->old > x2->old) return 1;
264 else if( x1->old < x2->old) return -1;
269 static void oldnewmap_sort(FileData *fd)
271 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
272 fd->libmap->sorted= 1;
275 /* nr is zero for data, and ID code for libdata */
276 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
280 if(oldaddr==NULL || newaddr==NULL) return;
282 if (onm->nentries==onm->entriessize) {
283 int osize= onm->entriessize;
284 OldNew *oentries= onm->entries;
286 onm->entriessize*= 2;
287 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
289 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
293 entry= &onm->entries[onm->nentries++];
295 entry->newp= newaddr;
299 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
303 if (onm->lasthit<onm->nentries-1) {
304 OldNew *entry= &onm->entries[++onm->lasthit];
306 if (entry->old==addr) {
312 for (i=0; i<onm->nentries; i++) {
313 OldNew *entry= &onm->entries[i];
315 if (entry->old==addr) {
326 /* for libdata, nr has ID code, no increment */
327 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
331 if(addr==NULL) return NULL;
333 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
335 OldNew entry_s, *entry;
339 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
343 if (id && (!lib || id->lib)) {
349 for (i=0; i<onm->nentries; i++) {
350 OldNew *entry= &onm->entries[i];
352 if (entry->old==addr) {
355 if (id && (!lib || id->lib)) {
364 static void oldnewmap_free_unused(OldNewMap *onm)
368 for (i=0; i<onm->nentries; i++) {
369 OldNew *entry= &onm->entries[i];
371 MEM_freeN(entry->newp);
377 static void oldnewmap_clear(OldNewMap *onm)
383 static void oldnewmap_free(OldNewMap *onm)
385 MEM_freeN(onm->entries);
391 static void read_libraries(FileData *basefd, ListBase *mainlist);
393 /* ************ help functions ***************** */
395 static void add_main_to_main(Main *mainvar, Main *from)
397 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
400 a= set_listbasepointers(mainvar, lbarray);
401 a= set_listbasepointers(from, fromarray);
403 addlisttolist(lbarray[a], fromarray[a]);
407 void blo_join_main(ListBase *mainlist)
409 Main *tojoin, *mainl;
412 mainl= mainlist->first;
413 while ((tojoin= mainl->next)) {
414 add_main_to_main(mainl, tojoin);
415 BLI_remlink(mainlist, tojoin);
420 static void split_libdata(ListBase *lb, Main *first)
432 if(mainvar->curlib==id->lib) {
433 lbn= wich_libbase(mainvar, GS(id->name));
435 BLI_addtail(lbn, id);
438 mainvar= mainvar->next;
440 if(mainvar==0) printf("error split_libdata\n");
446 void blo_split_main(ListBase *mainlist, Main *main)
448 ListBase *lbarray[MAX_LIBARRAY];
452 mainlist->first= mainlist->last= main;
455 if(main->library.first==NULL)
458 for (lib= main->library.first; lib; lib= lib->id.next) {
459 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
460 libmain->curlib= lib;
461 BLI_addtail(mainlist, libmain);
464 i= set_listbasepointers(main, lbarray);
466 split_libdata(lbarray[i], main->next);
469 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
470 static void cleanup_path(const char *relabase, char *name)
472 char filename[FILE_MAXFILE];
474 BLI_splitdirstring(name, filename);
475 BLI_cleanup_dir(relabase, name);
476 strcat(name, filename);
479 static Main *blo_find_main(ListBase *mainlist, const char *name, const char *relabase)
483 char name1[FILE_MAXDIR+FILE_MAXFILE];
485 strncpy(name1, name, sizeof(name1)-1);
486 cleanup_path(relabase, name1);
487 // printf("blo_find_main: original in %s\n", name);
488 // printf("blo_find_main: converted to %s\n", name1);
490 for (m= mainlist->first; m; m= m->next) {
491 char *libname= (m->curlib)?m->curlib->filename:m->name;
493 if (BLI_streq(name1, libname)) {
494 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
499 m= MEM_callocN(sizeof(Main), "find_main");
500 BLI_addtail(mainlist, m);
502 lib= alloc_libblock(&m->library, ID_LI, "lib");
503 strncpy(lib->name, name, sizeof(lib->name)-1);
504 BLI_strncpy(lib->filename, name1, sizeof(lib->filename));
508 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", name);
513 /* ************ FILE PARSING ****************** */
515 static void switch_endian_bh4(BHead4 *bhead)
517 /* the ID_.. codes */
518 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
520 if (bhead->code != ENDB) {
521 SWITCH_INT(bhead->len);
522 SWITCH_INT(bhead->SDNAnr);
523 SWITCH_INT(bhead->nr);
527 static void switch_endian_bh8(BHead8 *bhead)
529 /* the ID_.. codes */
530 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
532 if (bhead->code != ENDB) {
533 SWITCH_INT(bhead->len);
534 SWITCH_INT(bhead->SDNAnr);
535 SWITCH_INT(bhead->nr);
539 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
541 BHead4 *bhead4 = (BHead4 *) bhead;
542 #if defined(WIN32) && !defined(FREE_WINDOWS)
548 bhead4->code= bhead8->code;
549 bhead4->len= bhead8->len;
551 if (bhead4->code != ENDB) {
553 // why is this here ??
554 if (do_endian_swap) {
555 SWITCH_LONGINT(bhead8->old);
558 /* this patch is to avoid a long long being read from not-eight aligned positions
559 is necessary on any modern 64bit architecture) */
560 memcpy(&old, &bhead8->old, 8);
561 bhead4->old = (int) (old >> 3);
563 bhead4->SDNAnr= bhead8->SDNAnr;
564 bhead4->nr= bhead8->nr;
568 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
570 BHead8 *bhead8 = (BHead8 *) bhead;
572 bhead8->code= bhead4->code;
573 bhead8->len= bhead4->len;
575 if (bhead8->code != ENDB) {
576 bhead8->old= bhead4->old;
577 bhead8->SDNAnr= bhead4->SDNAnr;
578 bhead8->nr= bhead4->nr;
582 static BHeadN *get_bhead(FileData *fd)
587 BHeadN *new_bhead = 0;
593 // First read the bhead structure.
594 // Depending on the platform the file was written on this can
595 // be a big or little endian BHead4 or BHead8 structure.
597 // As usual 'ENDB' (the last *partial* bhead of the file)
598 // needs some special handling. We don't want to EOF just yet.
600 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
602 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
604 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
605 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
606 switch_endian_bh4(&bhead4);
609 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
610 bh8_from_bh4(&bhead, &bhead4);
612 memcpy(&bhead, &bhead4, sizeof(bhead));
620 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
622 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
623 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
624 switch_endian_bh8(&bhead8);
627 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
628 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
630 memcpy(&bhead, &bhead8, sizeof(bhead));
638 /* make sure people are not trying to pass bad blend files */
639 if (bhead.len < 0) fd->eof = 1;
641 // bhead now contains the (converted) bhead structure. Now read
642 // the associated data and put everything in a BHeadN (creative naming !)
645 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
647 new_bhead->next = new_bhead->prev = 0;
648 new_bhead->bhead = bhead;
650 readsize = fd->read(fd, new_bhead + 1, bhead.len);
652 if (readsize != bhead.len) {
654 MEM_freeN(new_bhead);
664 // We've read a new block. Now add it to the list
668 BLI_addtail(&fd->listbase, new_bhead);
674 BHead *blo_firstbhead(FileData *fd)
680 // Read in a new block if necessary
682 new_bhead = fd->listbase.first;
683 if (new_bhead == 0) {
684 new_bhead = get_bhead(fd);
688 bhead = &new_bhead->bhead;
694 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
696 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
697 BHeadN *prev= bheadn->prev;
699 return prev?&prev->bhead:NULL;
702 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
704 BHeadN *new_bhead = 0;
708 // bhead is actually a sub part of BHeadN
709 // We calculate the BHeadN pointer from the BHead pointer below
710 new_bhead = (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
712 // get the next BHeadN. If it doesn't exist we read in the next one
713 new_bhead = new_bhead->next;
714 if (new_bhead == 0) {
715 new_bhead = get_bhead(fd);
720 // here we do the reverse:
721 // go from the BHeadN pointer to the BHead pointer
722 bhead = &new_bhead->bhead;
729 static void get_blender_subversion(FileData *fd)
733 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
734 if (bhead->code==GLOB) {
735 FileGlobal *fg= read_struct(fd, bhead, "Global");
736 fd->filesubversion= fg->subversion;
737 fd->fileminversion= fg->minversion;
738 fd->fileminsubversion= fg->minsubversion;
742 else if (bhead->code==ENDB)
748 static void decode_blender_header(FileData *fd)
750 char header[SIZEOFBLENDERHEADER], num[4];
753 // read in the header data
754 readsize = fd->read(fd, header, sizeof(header));
756 if (readsize == sizeof(header)) {
757 if(strncmp(header, "BLENDER", 7) == 0) {
758 int remove_this_endian_test= 1;
760 fd->flags |= FD_FLAGS_FILE_OK;
762 // what size are pointers in the file ?
764 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
765 if (sizeof(void *) != 4) {
766 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
769 if (sizeof(void *) != 8) {
770 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
774 // is the file saved in a different endian
776 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
777 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
780 // get the version number
782 memcpy(num, header+9, 3);
784 fd->fileversion = atoi(num);
789 static int read_file_dna(FileData *fd)
793 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
794 if (bhead->code==DNA1) {
795 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
797 fd->filesdna= dna_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
800 fd->compflags= dna_get_structDNA_compareflags(fd->filesdna, fd->memsdna);
801 /* used to retrieve ID names from (bhead+1) */
802 fd->id_name_offs= dna_elem_offset(fd->filesdna, "ID", "char", "name[]");
806 } else if (bhead->code==ENDB)
813 static int fd_read_from_file(FileData *filedata, void *buffer, int size)
815 int readsize = read(filedata->filedes, buffer, size);
820 filedata->seek += readsize;
826 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, int size)
828 int readsize = gzread(filedata->gzfiledes, buffer, size);
833 filedata->seek += readsize;
839 static int fd_read_from_memory(FileData *filedata, void *buffer, int size)
841 // don't read more bytes then there are available in the buffer
842 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
844 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
845 filedata->seek += readsize;
850 static int fd_read_from_memfile(FileData *filedata, void *buffer, int size)
852 static unsigned int seek= 1<<30; /* the current position */
853 static unsigned int offset= 0; /* size of previous chunks */
854 static MemFileChunk *chunk=NULL;
856 if(size==0) return 0;
858 if(seek != (unsigned int)filedata->seek) {
859 chunk= filedata->memfile->chunks.first;
863 if(seek + chunk->size > (unsigned) filedata->seek) break;
868 seek= filedata->seek;
872 /* first check if it's on the end if current chunk */
873 if( seek-offset == chunk->size) {
874 offset+= chunk->size;
878 /* debug, should never happen */
880 printf("illegal read, chunk zero\n");
883 else if( (seek-offset)+size > chunk->size) {
884 size= chunk->size - (seek-offset);
885 printf("chunk too large, clipped to %d\n", size);
888 memcpy(buffer, chunk->buf + (seek-offset), size);
889 filedata->seek += size;
898 static FileData *filedata_new(void)
900 extern unsigned char DNAstr[]; /* DNA.c */
902 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
905 fd->gzfiledes = NULL;
907 /* XXX, this doesn't need to be done all the time,
908 * but it keeps us reentrant, remove once we have
909 * a lib that provides a nice lock. - zr
911 fd->memsdna = dna_sdna_from_data(DNAstr, DNAlen, 0);
913 fd->datamap = oldnewmap_new();
914 fd->globmap = oldnewmap_new();
915 fd->libmap = oldnewmap_new();
920 static FileData *blo_decode_and_check(FileData *fd, BlendReadError *error_r)
922 decode_blender_header(fd);
924 if (fd->flags & FD_FLAGS_FILE_OK) {
925 if (!read_file_dna(fd)) {
926 *error_r = BRE_INCOMPLETE;
927 blo_freefiledata(fd);
932 *error_r = BRE_NOT_A_BLEND;
933 blo_freefiledata(fd);
940 /* cannot be called with relative paths anymore! */
941 /* on each new library added, it now checks for the current FileData and expands relativeness */
942 FileData *blo_openblenderfile(char *name, BlendReadError *error_r)
946 gzfile= gzopen(name, "rb");
948 if (NULL == gzfile) {
949 *error_r = BRE_UNABLE_TO_OPEN;
952 FileData *fd = filedata_new();
953 fd->gzfiledes = gzfile;
954 BLI_strncpy(fd->filename, name, sizeof(fd->filename)); // now only in use by library append
955 fd->read = fd_read_gzip_from_file;
957 return blo_decode_and_check(fd, error_r);
961 FileData *blo_openblendermemory(void *mem, int memsize, BlendReadError *error_r)
963 if (!mem || memsize<SIZEOFBLENDERHEADER) {
964 *error_r = mem?BRE_UNABLE_TO_READ:BRE_UNABLE_TO_OPEN;
967 FileData *fd= filedata_new();
969 fd->buffersize= memsize;
970 fd->read= fd_read_from_memory;
971 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
973 return blo_decode_and_check(fd, error_r);
977 FileData *blo_openblendermemfile(MemFile *memfile, BlendReadError *error_r)
980 *error_r = BRE_UNABLE_TO_OPEN;
983 FileData *fd= filedata_new();
984 fd->memfile= memfile;
986 fd->read= fd_read_from_memfile;
987 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
989 return blo_decode_and_check(fd, error_r);
994 void blo_freefiledata(FileData *fd)
998 if (fd->filedes != -1) {
1002 if (fd->gzfiledes != NULL)
1004 gzclose(fd->gzfiledes);
1007 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1008 MEM_freeN(fd->buffer);
1012 // Free all BHeadN data blocks
1013 BLI_freelistN(&fd->listbase);
1016 dna_freestructDNA(fd->memsdna);
1018 dna_freestructDNA(fd->filesdna);
1020 MEM_freeN(fd->compflags);
1023 oldnewmap_free(fd->datamap);
1025 oldnewmap_free(fd->globmap);
1027 oldnewmap_free(fd->imamap);
1028 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1029 oldnewmap_free(fd->libmap);
1035 /* ************ DIV ****************** */
1037 int BLO_has_bfile_extension(char *str)
1039 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1042 /* ************** OLD POINTERS ******************* */
1044 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1046 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1049 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1051 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1054 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1056 if(fd->imamap && adr)
1057 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1062 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1064 return oldnewmap_liblookup(fd->libmap, adr, lib);
1067 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1069 ID *id= newlibadr(fd, lib, adr);
1077 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1081 for (i=0; i<fd->libmap->nentries; i++) {
1082 OldNew *entry= &fd->libmap->entries[i];
1084 if (old==entry->newp && entry->nr==ID_ID) {
1086 if(new) entry->nr= GS( ((ID *)new)->name );
1092 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1096 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1099 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1103 change_idid_adr_fd(fd, old, new);
1108 /* assumed; G.main still exists */
1109 void blo_make_image_pointer_map(FileData *fd)
1111 Image *ima= G.main->image.first;
1112 Scene *sce= G.main->scene.first;
1114 fd->imamap= oldnewmap_new();
1116 for(;ima; ima= ima->id.next) {
1117 Link *ibuf= ima->ibufs.first;
1118 for(; ibuf; ibuf= ibuf->next)
1119 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1121 for(; sce; sce= sce->id.next) {
1124 for(node= sce->nodetree->nodes.first; node; node= node->next)
1125 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1130 /* set G.main image ibufs to zero if it has been restored */
1131 /* this works because freeing G.main only happens after this call */
1132 void blo_end_image_pointer_map(FileData *fd)
1134 OldNew *entry= fd->imamap->entries;
1135 Image *ima= G.main->image.first;
1136 Scene *sce= G.main->scene.first;
1139 /* used entries were restored, so we put them to zero */
1140 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1145 for(;ima; ima= ima->id.next) {
1148 /* this mirrors direct_link_image */
1149 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1151 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1152 BLI_remlink(&ima->ibufs, ibuf);
1157 for(; sce; sce= sce->id.next) {
1160 for(node= sce->nodetree->nodes.first; node; node= node->next)
1161 node->preview= newimaadr(fd, node->preview);
1166 /* undo file support: add all library pointers in lookup */
1167 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1169 Main *ptr= mainlist->first;
1170 ListBase *lbarray[MAX_LIBARRAY];
1172 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1173 int i= set_listbasepointers(ptr, lbarray);
1176 for(id= lbarray[i]->first; id; id= id->next)
1177 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1183 /* ********** END OLD POINTERS ****************** */
1184 /* ********** READ FILE ****************** */
1186 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1188 int blocksize, nblocks;
1191 data= (char *)(bhead+1);
1192 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1196 dna_switch_endian_struct(filesdna, bhead->SDNAnr, data);
1202 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1207 /* switch is based on file dna */
1208 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1209 switch_endian_structs(fd->filesdna, bh);
1211 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1212 if(fd->compflags[bh->SDNAnr]==2) {
1213 temp= dna_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1215 temp= MEM_mallocN(bh->len, blockname);
1216 memcpy(temp, (bh+1), bh->len);
1224 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1228 if(lb->first==NULL) return;
1230 lb->first= newdataadr(fd, lb->first);
1234 ln->next= newdataadr(fd, ln->next);
1242 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1247 if(lb->first==0) return;
1248 poin= newdataadr(fd, lb->first);
1250 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1257 poin= newdataadr(fd, ln->next);
1259 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1269 static void test_pointer_array(FileData *fd, void **mat)
1271 #if defined(WIN32) && !defined(FREE_WINDOWS)
1272 __int64 *lpoin, *lmat;
1274 long long *lpoin, *lmat;
1276 int len, *ipoin, *imat;
1278 /* manually convert the pointer array in
1279 * the old dna format to a pointer array in
1280 * the new dna format.
1283 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1285 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1286 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1290 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1291 SWITCH_LONGINT(*lpoin);
1292 *ipoin= (int) ((*lpoin) >> 3);
1300 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1301 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1315 /* ************ READ ID Properties *************** */
1317 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd);
1318 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd);
1320 void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, void *fd)
1324 /*since we didn't save the extra buffer, set totallen to len.*/
1325 prop->totallen = prop->len;
1326 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1328 if (switch_endian) {
1329 for (i=0; i<prop->len; i++) {
1330 SWITCH_INT(((int*)prop->data.pointer)[i]);
1335 void IDP_DirectLinkString(IDProperty *prop, int switch_endian, void *fd)
1337 /*since we didn't save the extra string buffer, set totallen to len.*/
1338 prop->totallen = prop->len;
1339 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1342 void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, void *fd)
1344 ListBase *lb = &prop->data.group;
1349 /*Link child id properties now*/
1350 for (loop=prop->data.group.first; loop; loop=loop->next) {
1351 IDP_DirectLinkProperty(loop, switch_endian, fd);
1355 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd)
1357 switch (prop->type) {
1359 IDP_DirectLinkGroup(prop, switch_endian, fd);
1362 IDP_DirectLinkString(prop, switch_endian, fd);
1365 IDP_DirectLinkArray(prop, switch_endian, fd);
1371 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd)
1375 /* ************ READ Brush *************** */
1376 /* library brush linking after fileread */
1377 static void lib_link_brush(FileData *fd, Main *main)
1383 /* only link ID pointers */
1384 for(brush= main->brush.first; brush; brush= brush->id.next) {
1385 if(brush->id.flag & LIB_NEEDLINK) {
1386 brush->id.flag -= LIB_NEEDLINK;
1388 for(a=0; a<MAX_MTEX; a++) {
1389 mtex= brush->mtex[a];
1391 mtex->tex= newlibadr_us(fd, brush->id.lib, mtex->tex);
1397 static void direct_link_brush(FileData *fd, Brush *brush)
1399 /* brush itself has been read */
1402 for(a=0; a<MAX_MTEX; a++)
1403 brush->mtex[a]= newdataadr(fd, brush->mtex[a]);
1406 /* ************ READ CurveMapping *************** */
1408 /* cuma itself has been read! */
1409 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1413 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1414 cumap->flag &= ~CUMA_PREMULLED;
1416 for(a=0; a<CM_TOT; a++) {
1417 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1418 cumap->cm[a].table= NULL;
1422 /* ************ READ NODE TREE *************** */
1424 /* singe node tree (also used for material/scene trees), ntree is not NULL */
1425 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
1429 for(node= ntree->nodes.first; node; node= node->next)
1430 node->id= newlibadr_us(fd, id->lib, node->id);
1433 /* library ntree linking after fileread */
1434 static void lib_link_nodetree(FileData *fd, Main *main)
1438 /* only link ID pointers */
1439 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1440 if(ntree->id.flag & LIB_NEEDLINK) {
1441 ntree->id.flag -= LIB_NEEDLINK;
1442 lib_link_ntree(fd, &ntree->id, ntree);
1447 /* verify types for nodes and groups, all data has to be read */
1448 static void lib_verify_nodetree(Main *main)
1454 /* now create the own typeinfo structs an verify nodes */
1455 /* here we still assume no groups in groups */
1456 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1457 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
1458 ntreeMakeOwnType(ntree); /* for group usage */
1461 /* now verify all types in material trees, groups are set OK now */
1462 for(ma= main->mat.first; ma; ma= ma->id.next) {
1464 ntreeVerifyTypes(ma->nodetree);
1466 /* and scene trees */
1467 for(sce= main->scene.first; sce; sce= sce->id.next) {
1469 ntreeVerifyTypes(sce->nodetree);
1475 /* ntree itself has been read! */
1476 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
1478 /* note: writing and reading goes in sync, for speed */
1483 ntree->init= 0; /* to set callbacks and force setting types */
1484 ntree->owntype= NULL;
1485 ntree->timecursor= NULL;
1487 link_list(fd, &ntree->nodes);
1488 for(node= ntree->nodes.first; node; node= node->next) {
1489 node->storage= newdataadr(fd, node->storage);
1492 /* could be handlerized at some point */
1493 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
1494 direct_link_curvemapping(fd, node->storage);
1495 else if(ntree->type==NTREE_COMPOSIT) {
1496 if( ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB))
1497 direct_link_curvemapping(fd, node->storage);
1498 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
1499 ((ImageUser *)node->storage)->ok= 1;
1502 link_list(fd, &node->inputs);
1503 link_list(fd, &node->outputs);
1505 link_list(fd, &ntree->links);
1507 /* and we connect the rest */
1508 for(node= ntree->nodes.first; node; node= node->next) {
1509 node->preview= newimaadr(fd, node->preview);
1511 for(sock= node->inputs.first; sock; sock= sock->next)
1512 sock->link= newdataadr(fd, sock->link);
1513 for(sock= node->outputs.first; sock; sock= sock->next)
1514 sock->ns.data= NULL;
1516 for(link= ntree->links.first; link; link= link->next) {
1517 link->fromnode= newdataadr(fd, link->fromnode);
1518 link->tonode= newdataadr(fd, link->tonode);
1519 link->fromsock= newdataadr(fd, link->fromsock);
1520 link->tosock= newdataadr(fd, link->tosock);
1523 /* type verification is in lib-link */
1526 /* ************ READ PACKEDFILE *************** */
1528 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1530 PackedFile *pf= newdataadr(fd, oldpf);
1533 pf->data= newdataadr(fd, pf->data);
1539 /* ************ READ IMAGE PREVIEW *************** */
1541 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1543 PreviewImage *prv= newdataadr(fd, old_prv);
1547 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1549 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1557 /* ************ READ SCRIPTLINK *************** */
1559 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1563 for(i=0; i<slink->totscript; i++) {
1564 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1568 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1570 slink->scripts= newdataadr(fd, slink->scripts);
1571 test_pointer_array(fd, (void **)&slink->scripts);
1573 slink->flag= newdataadr(fd, slink->flag);
1575 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1578 for(a=0; a<slink->totscript; a++) {
1579 SWITCH_SHORT(slink->flag[a]);
1584 /* ************ READ ARMATURE ***************** */
1586 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1588 bActionStrip *strip;
1589 bActionModifier *amod;
1591 for (strip=striplist->first; strip; strip=strip->next){
1592 strip->object = newlibadr(fd, id->lib, strip->object);
1593 strip->act = newlibadr_us(fd, id->lib, strip->act);
1594 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1595 for(amod= strip->modifiers.first; amod; amod= amod->next)
1596 amod->ob= newlibadr(fd, id->lib, amod->ob);
1600 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1602 bConstraintChannel *chan;
1604 for (chan=chanbase->first; chan; chan=chan->next){
1605 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1609 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
1613 for (con = conlist->first; con; con=con->next) {
1614 /* patch for error introduced by changing constraints (dunno how) */
1615 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
1616 if(con->data==NULL) {
1617 con->type= CONSTRAINT_TYPE_NULL;
1620 switch (con->type) {
1621 case CONSTRAINT_TYPE_PYTHON:
1623 bPythonConstraint *data;
1624 data= (bPythonConstraint*)con->data;
1625 data->tar = newlibadr(fd, id->lib, data->tar);
1626 data->text = newlibadr(fd, id->lib, data->text);
1627 //IDP_LibLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1630 case CONSTRAINT_TYPE_ACTION:
1632 bActionConstraint *data;
1633 data= ((bActionConstraint*)con->data);
1634 data->tar = newlibadr(fd, id->lib, data->tar);
1635 data->act = newlibadr(fd, id->lib, data->act);
1638 case CONSTRAINT_TYPE_LOCLIKE:
1640 bLocateLikeConstraint *data;
1641 data= ((bLocateLikeConstraint*)con->data);
1642 data->tar = newlibadr(fd, id->lib, data->tar);
1645 case CONSTRAINT_TYPE_ROTLIKE:
1647 bRotateLikeConstraint *data;
1648 data= ((bRotateLikeConstraint*)con->data);
1649 data->tar = newlibadr(fd, id->lib, data->tar);
1652 case CONSTRAINT_TYPE_SIZELIKE:
1654 bSizeLikeConstraint *data;
1655 data= ((bSizeLikeConstraint*)con->data);
1656 data->tar = newlibadr(fd, id->lib, data->tar);
1659 case CONSTRAINT_TYPE_KINEMATIC:
1661 bKinematicConstraint *data;
1662 data = ((bKinematicConstraint*)con->data);
1663 data->tar = newlibadr(fd, id->lib, data->tar);
1666 case CONSTRAINT_TYPE_TRACKTO:
1668 bTrackToConstraint *data;
1669 data = ((bTrackToConstraint*)con->data);
1670 data->tar = newlibadr(fd, id->lib, data->tar);
1673 case CONSTRAINT_TYPE_MINMAX:
1675 bMinMaxConstraint *data;
1676 data = ((bMinMaxConstraint*)con->data);
1677 data->tar = newlibadr(fd, id->lib, data->tar);
1680 case CONSTRAINT_TYPE_LOCKTRACK:
1682 bLockTrackConstraint *data;
1683 data= ((bLockTrackConstraint*)con->data);
1684 data->tar = newlibadr(fd, id->lib, data->tar);
1687 case CONSTRAINT_TYPE_FOLLOWPATH:
1689 bFollowPathConstraint *data;
1690 data= ((bFollowPathConstraint*)con->data);
1691 data->tar = newlibadr(fd, id->lib, data->tar);
1694 case CONSTRAINT_TYPE_STRETCHTO:
1696 bStretchToConstraint *data;
1697 data= ((bStretchToConstraint*)con->data);
1698 data->tar = newlibadr(fd, id->lib, data->tar);
1701 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
1703 bRigidBodyJointConstraint *data;
1704 data= ((bRigidBodyJointConstraint*)con->data);
1705 data->tar = newlibadr(fd, id->lib, data->tar);
1708 case CONSTRAINT_TYPE_CLAMPTO:
1710 bClampToConstraint *data;
1711 data= ((bClampToConstraint*)con->data);
1712 data->tar = newlibadr(fd, id->lib, data->tar);
1715 case CONSTRAINT_TYPE_CHILDOF:
1717 bChildOfConstraint *data;
1718 data= ((bChildOfConstraint*)con->data);
1719 data->tar = newlibadr(fd, id->lib, data->tar);
1722 case CONSTRAINT_TYPE_TRANSFORM:
1724 bTransformConstraint *data;
1725 data= ((bTransformConstraint*)con->data);
1726 data->tar = newlibadr(fd, id->lib, data->tar);
1729 case CONSTRAINT_TYPE_NULL:
1735 static void direct_link_constraints(FileData *fd, ListBase *lb)
1740 for (cons=lb->first; cons; cons=cons->next) {
1741 cons->data = newdataadr(fd, cons->data);
1742 if (cons->type == CONSTRAINT_TYPE_PYTHON) {
1743 bPythonConstraint *data= cons->data;
1744 data->prop = newdataadr(fd, data->prop);
1745 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1750 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
1752 bPoseChannel *pchan;
1753 bArmature *arm= ob->data;
1759 /* always rebuild to match proxy or lib changes */
1760 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
1762 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
1763 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
1765 /* hurms... loop in a loop, but yah... later... (ton) */
1766 pchan->bone= get_named_bone(arm, pchan->name);
1768 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
1769 if(pchan->bone==NULL)
1771 else if(ob->id.lib==NULL && arm->id.lib) {
1772 /* local pose selection copied to armature, bit hackish */
1773 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1774 pchan->bone->flag |= pchan->selectflag;
1779 ob->recalc= OB_RECALC;
1780 pose->flag |= POSE_RECALC;
1784 static void lib_link_armature(FileData *fd, Main *main)
1788 arm= main->armature.first;
1791 if(arm->id.flag & LIB_NEEDLINK) {
1792 arm->id.flag -= LIB_NEEDLINK;
1798 static void lib_link_action(FileData *fd, Main *main)
1801 bActionChannel *chan;
1803 act= main->action.first;
1805 if(act->id.flag & LIB_NEEDLINK) {
1806 act->id.flag -= LIB_NEEDLINK;
1808 for (chan=act->chanbase.first; chan; chan=chan->next) {
1809 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1810 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1818 static void direct_link_bones(FileData *fd, Bone* bone)
1822 bone->parent= newdataadr(fd, bone->parent);
1824 link_list(fd, &bone->childbase);
1826 for (child=bone->childbase.first; child; child=child->next) {
1827 direct_link_bones(fd, child);
1832 static void direct_link_action(FileData *fd, bAction *act)
1834 bActionChannel *achan;
1836 link_list(fd, &act->chanbase);
1838 for (achan = act->chanbase.first; achan; achan=achan->next)
1839 link_list(fd, &achan->constraintChannels);
1843 static void direct_link_armature(FileData *fd, bArmature *arm)
1847 link_list(fd, &arm->bonebase);
1849 bone=arm->bonebase.first;
1851 direct_link_bones(fd, bone);
1856 /* ************ READ CAMERA ***************** */
1858 static void lib_link_camera(FileData *fd, Main *main)
1862 ca= main->camera.first;
1864 if(ca->id.flag & LIB_NEEDLINK) {
1866 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo);
1868 lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
1870 ca->id.flag -= LIB_NEEDLINK;
1876 static void direct_link_camera(FileData *fd, Camera *ca)
1878 direct_link_scriptlink(fd, &ca->scriptlink);
1882 /* ************ READ LAMP ***************** */
1884 static void lib_link_lamp(FileData *fd, Main *main)
1890 la= main->lamp.first;
1892 if(la->id.flag & LIB_NEEDLINK) {
1894 for(a=0; a<MAX_MTEX; a++) {
1897 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
1898 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
1902 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo);
1904 lib_link_scriptlink(fd, &la->id, &la->scriptlink);
1906 la->id.flag -= LIB_NEEDLINK;
1912 static void direct_link_lamp(FileData *fd, Lamp *la)
1916 direct_link_scriptlink(fd, &la->scriptlink);
1918 for(a=0; a<MAX_MTEX; a++) {
1919 la->mtex[a]= newdataadr(fd, la->mtex[a]);
1922 la->curfalloff= newdataadr(fd, la->curfalloff);
1924 direct_link_curvemapping(fd, la->curfalloff);
1926 la->preview = direct_link_preview_image(fd, la->preview);
1929 /* ************ READ keys ***************** */
1931 static void lib_link_key(FileData *fd, Main *main)
1935 key= main->key.first;
1937 if(key->id.flag & LIB_NEEDLINK) {
1939 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo);
1940 key->from= newlibadr(fd, key->id.lib, key->from);
1942 key->id.flag -= LIB_NEEDLINK;
1948 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
1951 char *data, *poin, *cp;
1953 elemsize= key->elemsize;
1956 for(a=0; a<kb->totelem; a++) {
1961 while( cp[0] ) { /* cp[0]==amount */
1963 switch(cp[1]) { /* cp[1]= type */
1969 SWITCH_INT((*poin));
1982 static void direct_link_key(FileData *fd, Key *key)
1986 link_list(fd, &(key->block));
1988 key->refkey= newdataadr(fd, key->refkey);
1990 kb= key->block.first;
1993 kb->data= newdataadr(fd, kb->data);
1995 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
1996 switch_endian_keyblock(key, kb);
2002 /* ************ READ mball ***************** */
2004 static void lib_link_mball(FileData *fd, Main *main)
2009 mb= main->mball.first;
2011 if(mb->id.flag & LIB_NEEDLINK) {
2013 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2015 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo);
2017 mb->id.flag -= LIB_NEEDLINK;
2023 static void direct_link_mball(FileData *fd, MetaBall *mb)
2025 mb->mat= newdataadr(fd, mb->mat);
2026 test_pointer_array(fd, (void **)&mb->mat);
2028 link_list(fd, &(mb->elems));
2030 mb->disp.first= mb->disp.last= 0;
2035 /* ************ READ WORLD ***************** */
2037 static void lib_link_world(FileData *fd, Main *main)
2043 wrld= main->world.first;
2045 if(wrld->id.flag & LIB_NEEDLINK) {
2047 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo);
2049 for(a=0; a<MAX_MTEX; a++) {
2050 mtex= wrld->mtex[a];
2052 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2053 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2057 lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
2059 wrld->id.flag -= LIB_NEEDLINK;
2061 wrld= wrld->id.next;
2065 static void direct_link_world(FileData *fd, World *wrld)
2069 direct_link_scriptlink(fd, &wrld->scriptlink);
2071 for(a=0; a<MAX_MTEX; a++) {
2072 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2074 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2078 /* ************ READ IPO ***************** */
2080 static void lib_link_ipo(FileData *fd, Main *main)
2084 ipo= main->ipo.first;
2086 if(ipo->id.flag & LIB_NEEDLINK) {
2088 for(icu= ipo->curve.first; icu; icu= icu->next) {
2090 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
2092 ipo->id.flag -= LIB_NEEDLINK;
2098 static void direct_link_ipo(FileData *fd, Ipo *ipo)
2102 link_list(fd, &(ipo->curve));
2103 icu= ipo->curve.first;
2105 icu->bezt= newdataadr(fd, icu->bezt);
2106 icu->bp= newdataadr(fd, icu->bp);
2107 icu->driver= newdataadr(fd, icu->driver);
2112 /* ************ READ VFONT ***************** */
2114 static void lib_link_vfont(FileData *fd, Main *main)
2118 vf= main->vfont.first;
2120 if(vf->id.flag & LIB_NEEDLINK) {
2121 vf->id.flag -= LIB_NEEDLINK;
2127 static void direct_link_vfont(FileData *fd, VFont *vf)
2130 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2133 /* ************ READ TEXT ****************** */
2135 static void lib_link_text(FileData *fd, Main *main)
2139 text= main->text.first;
2141 if(text->id.flag & LIB_NEEDLINK) {
2142 text->id.flag -= LIB_NEEDLINK;
2144 text= text->id.next;
2148 static void direct_link_text(FileData *fd, Text *text)
2152 text->name= newdataadr(fd, text->name);
2155 text->undo_len= TXT_INIT_UNDO;
2156 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2158 text->compiled= NULL;
2161 if(text->flags & TXT_ISEXT) {
2166 link_list(fd, &text->lines);
2168 text->curl= newdataadr(fd, text->curl);
2169 text->sell= newdataadr(fd, text->sell);
2171 ln= text->lines.first;
2173 ln->line= newdataadr(fd, ln->line);
2176 if (ln->len != (int) strlen(ln->line)) {
2177 printf("Error loading text, line lengths differ\n");
2178 ln->len = strlen(ln->line);
2184 text->flags = (text->flags|TXT_ISTMP) & ~TXT_ISEXT;
2189 /* ************ READ IMAGE ***************** */
2191 static void lib_link_image(FileData *fd, Main *main)
2195 ima= main->image.first;
2197 if(ima->id.flag & LIB_NEEDLINK) {
2198 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2200 ima->id.flag -= LIB_NEEDLINK;
2206 static void link_ibuf_list(FileData *fd, ListBase *lb)
2210 if(lb->first==NULL) return;
2212 lb->first= newimaadr(fd, lb->first);
2216 ln->next= newimaadr(fd, ln->next);
2224 static void direct_link_image(FileData *fd, Image *ima)
2226 /* for undo system, pointers could be restored */
2228 link_ibuf_list(fd, &ima->ibufs);
2230 ima->ibufs.first= ima->ibufs.last= NULL;
2232 /* if not restored, we keep the binded opengl index */
2233 if(ima->ibufs.first==NULL)
2240 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2241 ima->preview = direct_link_preview_image(fd, ima->preview);
2246 /* ************ READ CURVE ***************** */
2248 static void lib_link_curve(FileData *fd, Main *main)
2253 cu= main->curve.first;
2255 if(cu->id.flag & LIB_NEEDLINK) {
2257 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2259 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2260 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2261 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2262 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2263 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2264 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2265 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2267 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo);
2268 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2270 cu->id.flag -= LIB_NEEDLINK;
2277 static void switch_endian_knots(Nurb *nu)
2284 SWITCH_INT(nu->knotsu[len]);
2290 SWITCH_INT(nu->knotsv[len]);
2295 static void direct_link_curve(FileData *fd, Curve *cu)
2300 cu->mat= newdataadr(fd, cu->mat);
2301 test_pointer_array(fd, (void **)&cu->mat);
2302 cu->str= newdataadr(fd, cu->str);
2303 cu->strinfo= newdataadr(fd, cu->strinfo);
2304 cu->tb= newdataadr(fd, cu->tb);
2306 if(cu->vfont==0) link_list(fd, &(cu->nurb));
2308 cu->nurb.first=cu->nurb.last= 0;
2310 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2312 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2319 cu->tb[0].w = cu->linewidth;
2321 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2324 cu->bev.first=cu->bev.last= 0;
2325 cu->disp.first=cu->disp.last= 0;
2330 nu->bezt= newdataadr(fd, nu->bezt);
2331 nu->bp= newdataadr(fd, nu->bp);
2332 nu->knotsu= newdataadr(fd, nu->knotsu);
2333 nu->knotsv= newdataadr(fd, nu->knotsv);
2334 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2336 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2337 switch_endian_knots(nu);
2345 /* ************ READ TEX ***************** */
2347 static void lib_link_texture(FileData *fd, Main *main)
2351 tex= main->tex.first;
2353 if(tex->id.flag & LIB_NEEDLINK) {
2355 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2356 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2357 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2359 tex->id.flag -= LIB_NEEDLINK;
2365 static void direct_link_texture(FileData *fd, Tex *tex)
2367 tex->plugin= newdataadr(fd, tex->plugin);
2369 tex->plugin->handle= 0;
2370 open_plugin_tex(tex->plugin);
2371 /* initialize data for this instance, if an initialization
2374 if (tex->plugin->instance_init)
2375 tex->plugin->instance_init((void *) tex->plugin->data);
2377 tex->coba= newdataadr(fd, tex->coba);
2378 tex->env= newdataadr(fd, tex->env);
2380 tex->env->ima= NULL;
2381 memset(tex->env->cube, 0, 6*sizeof(void *));
2384 tex->preview = direct_link_preview_image(fd, tex->preview);
2391 /* ************ READ MATERIAL ***************** */
2393 static void lib_link_material(FileData *fd, Main *main)
2399 ma= main->mat.first;
2401 if(ma->id.flag & LIB_NEEDLINK) {
2402 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2403 of library blocks that implement this.*/
2404 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2406 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2407 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2409 for(a=0; a<MAX_MTEX; a++) {
2412 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2413 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2416 lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
2419 lib_link_ntree(fd, &ma->id, ma->nodetree);
2421 ma->id.flag -= LIB_NEEDLINK;
2427 static void direct_link_material(FileData *fd, Material *ma)
2431 for(a=0; a<MAX_MTEX; a++) {
2432 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2435 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2436 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2438 direct_link_scriptlink(fd, &ma->scriptlink);
2440 ma->nodetree= newdataadr(fd, ma->nodetree);
2442 direct_link_nodetree(fd, ma->nodetree);
2444 ma->preview = direct_link_preview_image(fd, ma->preview);
2447 /* ************ READ MESH ***************** */
2449 static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
2454 for (i=0; i<totface; i++, tf++) {
2455 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
2456 if(tf->tpage && tf->tpage->id.us==0)
2457 tf->tpage->id.us= 1;
2461 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
2464 for(i=0; i<fdata->totlayer; i++) {
2465 CustomDataLayer *layer = &fdata->layers[i];
2467 if(layer->type == CD_MTFACE)
2468 lib_link_mtface(fd, me, layer->data, totface);
2473 static void lib_link_mesh(FileData *fd, Main *main)
2477 me= main->mesh.first;
2479 if(me->id.flag & LIB_NEEDLINK) {
2482 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2483 of library blocks that implement this.*/
2484 if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2486 /* this check added for python created meshes */
2488 for(i=0; i<me->totcol; i++) {
2489 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
2494 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
2495 me->key= newlibadr_us(fd, me->id.lib, me->key);
2496 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
2498 lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
2499 if(me->mr && me->mr->levels.first)
2500 lib_link_customdata_mtface(fd, me, &me->mr->fdata,
2501 ((MultiresLevel*)me->mr->levels.first)->totface);
2503 me->id.flag -= LIB_NEEDLINK;
2509 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
2516 for (i=0; i<count; i++) {
2517 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
2519 mdverts[i].totweight=0;
2523 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
2527 data->layers= newdataadr(fd, data->layers);
2529 while (i < data->totlayer) {
2530 CustomDataLayer *layer = &data->layers[i];
2532 if (CustomData_verify_versions(data, i)) {
2533 layer->data = newdataadr(fd, layer->data);
2539 static void direct_link_mesh(FileData *fd, Mesh *mesh)
2541 mesh->mat= newdataadr(fd, mesh->mat);
2542 test_pointer_array(fd, (void **)&mesh->mat);
2544 mesh->mvert= newdataadr(fd, mesh->mvert);
2545 mesh->medge= newdataadr(fd, mesh->medge);
2546 mesh->mface= newdataadr(fd, mesh->mface);
2547 mesh->tface= newdataadr(fd, mesh->tface);
2548 mesh->mtface= newdataadr(fd, mesh->mtface);
2549 mesh->mcol= newdataadr(fd, mesh->mcol);
2550 mesh->msticky= newdataadr(fd, mesh->msticky);
2551 mesh->dvert= newdataadr(fd, mesh->dvert);
2553 /* Partial-mesh visibility (do this before using totvert, totface, or totedge!) */
2554 mesh->pv= newdataadr(fd, mesh->pv);
2556 mesh->pv->vert_map= newdataadr(fd, mesh->pv->vert_map);
2557 mesh->pv->edge_map= newdataadr(fd, mesh->pv->edge_map);
2558 mesh->pv->old_faces= newdataadr(fd, mesh->pv->old_faces);
2559 mesh->pv->old_edges= newdataadr(fd, mesh->pv->old_edges);
2562 /* normally direct_link_dverts should be called in direct_link_customdata,
2563 but for backwards compat in do_versions to work we do it here */
2564 direct_link_dverts(fd, mesh->pv ? mesh->pv->totvert : mesh->totvert, mesh->dvert);
2566 direct_link_customdata(fd, &mesh->vdata, mesh->pv ? mesh->pv->totvert : mesh->totvert);
2567 direct_link_customdata(fd, &mesh->edata, mesh->pv ? mesh->pv->totedge : mesh->totedge);
2568 direct_link_customdata(fd, &mesh->fdata, mesh->pv ? mesh->pv->totface : mesh->totface);
2571 mesh->mselect = NULL;
2574 mesh->mr= newdataadr(fd, mesh->mr);
2578 link_list(fd, &mesh->mr->levels);
2579 lvl= mesh->mr->levels.first;
2581 direct_link_customdata(fd, &mesh->mr->vdata, lvl->totvert);
2582 direct_link_dverts(fd, lvl->totvert, CustomData_get(&mesh->mr->vdata, 0, CD_MDEFORMVERT));
2583 direct_link_customdata(fd, &mesh->mr->fdata, lvl->totface);
2585 if(mesh->mr->edge_flags)
2586 mesh->mr->edge_flags= newdataadr(fd, mesh->mr->edge_flags);
2587 if(mesh->mr->edge_creases)
2588 mesh->mr->edge_creases= newdataadr(fd, mesh->mr->edge_creases);
2590 if(!mesh->mr->edge_flags)
2591 mesh->mr->edge_flags= MEM_callocN(sizeof(short)*lvl->totedge, "Multires Edge Flags");
2592 if(!mesh->mr->edge_creases)
2593 mesh->mr->edge_creases= MEM_callocN(sizeof(char)*lvl->totedge, "Multires Edge Creases");
2595 mesh->mr->verts = newdataadr(fd, mesh->mr->verts);
2597 for(; lvl; lvl= lvl->next) {
2598 lvl->verts= newdataadr(fd, lvl->verts);
2599 lvl->faces= newdataadr(fd, lvl->faces);
2600 lvl->edges= newdataadr(fd, lvl->edges);
2601 lvl->colfaces= newdataadr(fd, lvl->colfaces);
2602 lvl->edge_boundary_states= NULL;
2603 lvl->vert_face_map = lvl->vert_edge_map = NULL;
2608 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
2609 TFace *tf= mesh->tface;
2612 for (i=0; i< (mesh->pv ? mesh->pv->totface : mesh->totface); i++, tf++) {
2613 SWITCH_INT(tf->col[0]);
2614 SWITCH_INT(tf->col[1]);
2615 SWITCH_INT(tf->col[2]);
2616 SWITCH_INT(tf->col[3]);
2621 /* ************ READ LATTICE ***************** */
2623 static void lib_link_latt(FileData *fd, Main *main)
2627 lt= main->latt.first;
2629 if(lt->id.flag & LIB_NEEDLINK) {
2631 lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo);
2632 lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
2634 lt->id.flag -= LIB_NEEDLINK;
2640 static void direct_link_latt(FileData *fd, Lattice *lt)
2642 lt->def= newdataadr(fd, lt->def);
2644 lt->dvert= newdataadr(fd, lt->dvert);
2645 direct_link_dverts(fd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
2649 /* ************ READ OBJECT ***************** */
2651 static void lib_link_modifiers__linkModifiers(void *userData, Object *ob,
2654 FileData *fd = userData;
2656 *idpoin = newlibadr(fd, ob->id.lib, *idpoin);
2657 /* hardcoded bad exception; non-object modifier data gets user count (texture, displace) */
2658 if(*idpoin && GS((*idpoin)->name)!=ID_OB)
2661 static void lib_link_modifiers(FileData *fd, Object *ob)
2663 modifiers_foreachIDLink(ob, lib_link_modifiers__linkModifiers, fd);
2666 static void lib_link_object(FileData *fd, Main *main)
2676 ob= main->object.first;
2678 if(ob->id.flag & LIB_NEEDLINK) {
2679 if (ob->id.properties) IDP_LibLinkProperty(ob->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2681 ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
2682 ob->track= newlibadr(fd, ob->id.lib, ob->track);
2683 ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
2684 ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
2685 ob->dup_group= newlibadr_us(fd, ob->id.lib, ob->dup_group);
2687 ob->proxy= newlibadr_us(fd, ob->id.lib, ob->proxy);
2689 /* paranoia check, actually a proxy_from pointer should never be written... */
2690 if(ob->proxy->id.lib==NULL) {
2691 ob->proxy->proxy_from= NULL;
2695 /* this triggers object_update to always use a copy */
2696 ob->proxy->proxy_from= ob;
2697 /* force proxy updates after load/undo, a bit weak */
2698 ob->recalc= ob->proxy->recalc= OB_RECALC;
2701 ob->proxy_group= newlibadr(fd, ob->id.lib, ob->proxy_group);
2704 ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
2706 if(ob->data==NULL && poin!=NULL) {
2709 if(ob->id.lib) printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
2710 else printf("Object %s lost data.", ob->id.name+2);
2713 free_pose_channels(ob->pose);
2714 MEM_freeN(ob->pose);
2716 ob->flag &= ~OB_POSEMODE;
2719 for(a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
2721 ob->id.flag -= LIB_NEEDLINK;
2722 /* if id.us==0 a new base will be created later on */
2724 /* WARNING! Also check expand_object(), should reflect the stuff below. */
2725 lib_link_pose(fd, ob, ob->pose);
2726 lib_link_constraints(fd, &ob->id, &ob->constraints);
2727 lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
2728 lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
2730 for(paf= ob->effect.first; paf; paf= paf->next) {
2731 if(paf->type==EFF_PARTICLE) {
2732 paf->group= newlibadr_us(fd, ob->id.lib, paf->group);
2736 sens= ob->sensors.first;
2738 if(ob->id.lib==NULL) { // done in expand_main
2739 for(a=0; a<sens->totlinks; a++) {
2740 sens->links[a]= newglobadr(fd, sens->links[a]);
2743 if(sens->type==SENS_TOUCH) {
2744 bTouchSensor *ts= sens->data;
2745 ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
2747 else if(sens->type==SENS_MESSAGE) {
2748 bMessageSensor *ms= sens->data;
2750 newlibadr(fd, ob->id.lib, ms->fromObject);
2755 cont= ob->controllers.first;
2757 if(ob->id.lib==NULL) { // done in expand_main
2758 for(a=0; a<cont->totlinks; a++) {
2759 cont->links[a]= newglobadr(fd, cont->links[a]);
2762 if(cont->type==CONT_PYTHON) {
2763 bPythonCont *pc= cont->data;
2764 pc->text= newlibadr(fd, ob->id.lib, pc->text);
2772 act= ob->actuators.first;
2774 if(act->type==ACT_SOUND) {
2775 bSoundActuator *sa= act->data;
2776 sa->sound= newlibadr_us(fd, ob->id.lib, sa->sound);
2778 else if(act->type==ACT_CD) {
2779 /* bCDActuator *cda= act->data; */
2781 else if(act->type==ACT_GAME) {
2782 /* bGameActuator *ga= act->data; */
2784 else if(act->type==ACT_CAMERA) {
2785 bCameraActuator *ca= act->data;
2786 ca->ob= newlibadr(fd, ob->id.lib, ca->ob);
2788 /* leave this one, it's obsolete but necessary to read for conversion */
2789 else if(act->type==ACT_ADD_OBJECT) {
2790 bAddObjectActuator *eoa= act->data;
2791 if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2793 else if(act->type==ACT_EDIT_OBJECT) {
2794 bEditObjectActuator *eoa= act->data;
2799 eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2800 eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
2803 else if(act->type==ACT_SCENE) {
2804 bSceneActuator *sa= act->data;
2805 sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
2806 sa->scene= newlibadr(fd, ob->id.lib, sa->scene);
2808 else if(act->type==ACT_ACTION) {
2809 bActionActuator *aa= act->data;
2810 aa->act= newlibadr(fd, ob->id.lib, aa->act);
2812 else if(act->type==ACT_PROPERTY) {
2813 bPropertyActuator *pa= act->data;
2814 pa->ob= newlibadr(fd, ob->id.lib, pa->ob);
2816 else if(act->type==ACT_MESSAGE) {
2817 bMessageActuator *ma= act->data;
2818 ma->toObject= newlibadr(fd, ob->id.lib, ma->toObject);
2823 if(ob->fluidsimSettings) {
2824 ob->fluidsimSettings->ipo = newlibadr_us(fd, ob->id.lib, ob->fluidsimSettings->ipo);
2827 lib_link_scriptlink(fd, &ob->id, &ob->scriptlink);
2828 lib_link_modifiers(fd, ob);
2833 if(warn) error("WARNING IN CONSOLE");
2837 static void direct_link_pose(FileData *fd, bPose *pose) {
2839 bPoseChannel *pchan;
2844 link_list(fd, &pose->chanbase);
2846 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2848 pchan->parent= newdataadr(fd, pchan->parent);
2849 pchan->child= newdataadr(fd, pchan->child);
2850 direct_link_constraints(fd, &pchan->constraints);
2851 pchan->iktree.first= pchan->iktree.last= NULL;
2857 static void direct_link_modifiers(FileData *fd, ListBase *lb)
2863 for (md=lb->first; md; md=md->next) {
2866 /* if modifiers disappear, or for upward compatibility */
2867 if(NULL==modifierType_getInfo(md->type))
2868 md->type= eModifierType_None;
2870 if (md->type==eModifierType_Subsurf) {
2871 SubsurfModifierData *smd = (SubsurfModifierData*) md;
2873 smd->emCache = smd->mCache = 0;
2874 } else if (md->type==eModifierType_Hook) {
2875 HookModifierData *hmd = (HookModifierData*) md;
2877 hmd->indexar= newdataadr(fd, hmd->indexar);
2878 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2880 for(a=0; a<hmd->totindex; a++) {
2881 SWITCH_INT(hmd->indexar[a]);
2888 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
2890 bActionStrip *strip;
2892 link_list(fd, strips);
2894 for(strip= strips->first; strip; strip= strip->next)
2895 link_list(fd, &strip->modifiers);
2898 static void direct_link_object(FileData *fd, Object *ob)
2907 /* weak weak... this was only meant as draw flag, now is used in give_base too */
2908 ob->flag &= ~OB_FROMGROUP;
2910 ob->disp.first=ob->disp.last= NULL;
2912 ob->pose= newdataadr(fd, ob->pose);
2913 direct_link_pose(fd, ob->pose);
2915 link_list(fd, &ob->defbase);
2916 direct_link_nlastrips(fd, &ob->nlastrips);
2917 link_list(fd, &ob->constraintChannels);
2919 direct_link_scriptlink(fd, &ob->scriptlink);
2921 ob->mat= newdataadr(fd, ob->mat);
2922 test_pointer_array(fd, (void **)&ob->mat);
2924 /* do it here, below old data gets converted */
2925 direct_link_modifiers(fd, &ob->modifiers);
2927 link_list(fd, &ob->effect);
2928 paf= ob->effect.first;
2930 if(paf->type==EFF_PARTICLE) {
2933 if(paf->type==EFF_WAVE) {
2934 WaveEff *wav = (WaveEff*) paf;
2935 PartEff *next = paf->next;
2936 WaveModifierData *wmd = (WaveModifierData*) modifier_new(eModifierType_Wave);
2938 wmd->damp = wav->damp;
2939 wmd->flag = wav->flag;
2940 wmd->height = wav->height;
2941 wmd->lifetime = wav->lifetime;
2942 wmd->narrow = wav->narrow;
2943 wmd->speed = wav->speed;
2944 wmd->startx = wav->startx;
2945 wmd->starty = wav->startx;
2946 wmd->timeoffs = wav->timeoffs;
2947 wmd->width = wav->width;
2949 BLI_addtail(&ob->modifiers, wmd);
2951 BLI_remlink(&ob->effect, paf);
2957 if(paf->type==EFF_BUILD) {
2958 BuildEff *baf = (BuildEff*) paf;
2959 PartEff *next = paf->next;
2960 BuildModifierData *bmd = (BuildModifierData*) modifier_new(eModifierType_Build);
2962 bmd->start = baf->sfra;
2963 bmd->length = baf->len;
2967 BLI_addtail(&ob->modifiers, bmd);
2969 BLI_remlink(&ob->effect, paf);
2978 ob->pd= newdataadr(fd, ob->pd);
2979 ob->soft= newdataadr(fd, ob->soft);
2981 SoftBody *sb= ob->soft;
2983 sb->bpoint= NULL; // init pointers so it gets rebuilt nicely
2988 sb->keys= newdataadr(fd, sb->keys);
2989 test_pointer_array(fd, (void **)&sb->keys);
2991 for(a=0; a<sb->totkey; a++) {
2992 sb->keys[a]= newdataadr(fd, sb->keys[a]);
2996 ob->fluidsimSettings= newdataadr(fd, ob->fluidsimSettings); /* NT */
2997 if(ob->fluidsimSettings) {
2998 // reinit mesh pointers
2999 ob->fluidsimSettings->orgMesh = NULL; //ob->data;
3000 ob->fluidsimSettings->meshSurface = NULL;
3001 ob->fluidsimSettings->meshBB = NULL;
3002 ob->fluidsimSettings->meshSurfNormals = NULL;
3005 link_list(fd, &ob->prop);
3006 prop= ob->prop.first;
3008 prop->poin= newdataadr(fd, prop->poin);
3009 if(prop->poin==0) prop->poin= &prop->data;
3013 link_list(fd, &ob->sensors);
3014 sens= ob->sensors.first;
3016 sens->data= newdataadr(fd, sens->data);
3017 sens->links= newdataadr(fd, sens->links);
3018 test_pointer_array(fd, (void **)&sens->links);
3022 direct_link_constraints(fd, &ob->constraints);
3024 link_glob_list(fd, &ob->controllers);
3025 cont= ob->controllers.first;
3027 cont->data= newdataadr(fd, cont->data);
3028 cont->links= newdataadr(fd, cont->links);
3029 test_pointer_array(fd, (void **)&cont->links);
3033 link_glob_list(fd, &ob->actuators);
3034 act= ob->actuators.first;
3036 act->data= newdataadr(fd, act->data);
3040 link_list(fd, &ob->hooks);
3041 while (ob->hooks.first) {
3042 ObHook *hook = ob->hooks.first;
3043 HookModifierData *hmd = (HookModifierData*) modifier_new(eModifierType_Hook);
3045 hook->indexar= newdataadr(fd, hook->indexar);
3046 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
3048 for(a=0; a<hook->totindex; a++) {
3049 SWITCH_INT(hook->indexar[a]);
3053 /* Do conversion here because if we have loaded
3054 * a hook we need to make sure it gets converted
3055 * and free'd, regardless of version.
3057 VECCOPY(hmd->cent, hook->cent);
3058 hmd->falloff = hook->falloff;
3059 hmd->force = hook->force;
3060 hmd->indexar = hook->indexar;
3061 hmd->object = hook->parent;
3062 memcpy(hmd->parentinv, hook->parentinv, sizeof(hmd->parentinv));
3063 hmd->totindex = hook->totindex;
3065 BLI_addhead(&ob->modifiers, hmd);
3066 BLI_remlink(&ob->hooks, hook);
3072 ob->derivedDeform= NULL;
3073 ob->derivedFinal= NULL;
3076 /* ************ READ SCENE ***************** */
3078 static void lib_link_scene(FileData *fd, Main *main)
3084 SceneRenderLayer *srl;
3087 sce= main->scene.first;
3089 if(sce->id.flag & LIB_NEEDLINK) {
3090 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3091 of library blocks that implement this.*/
3092 if (sce->id.properties) IDP_LibLinkProperty(sce->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3094 sce->camera= newlibadr(fd, sce->id.lib, sce->camera);
3095 sce->world= newlibadr_us(fd, sce->id.lib, sce->world);
3096 sce->set= newlibadr(fd, sce->id.lib, sce->set);
3097 sce->ima= newlibadr_us(fd, sce->id.lib, sce->ima);
3098 sce->toolsettings->imapaint.brush=
3099 newlibadr_us(fd, sce->id.lib, sce->toolsettings->imapaint.brush);
3101 /* Sculptdata textures */
3102 for(a=0; a<MAX_MTEX; ++a) {
3103 MTex *mtex= sce->sculptdata.mtex[a];
3105 mtex->tex= newlibadr_us(fd, sce->id.lib, mtex->tex);
3108 for(base= sce->base.first; base; base= next) {
3111 /* base->object= newlibadr_us(fd, sce->id.lib, base->object); */
3112 base->object= newlibadr_us(fd, sce->id.lib, base->object);
3114 /* when save during radiotool, needs cleared */
3115 base->flag &= ~OB_RADIO;
3117 if(base->object==NULL) {
3118 printf("LIB ERROR: base removed\n");
3119 BLI_remlink(&sce->base, base);
3120 if(base==sce->basact) sce->basact= 0;
3127 WHILE_SEQ(&ed->seqbase) {
3128 if(seq->ipo) seq->ipo= newlibadr_us(fd, sce->id.lib, seq->ipo);
3129 if(seq->scene) seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
3131 seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
3133 seq->sound->id.us++;
3134 seq->sound->flags |= SOUND_FLAGS_SEQUENCE;
3143 lib_link_scriptlink(fd, &sce->id, &sce->scriptlink);
3146 lib_link_ntree(fd, &sce->id, sce->nodetree);
3148 for(srl= sce->r.layers.first; srl; srl= srl->next) {
3149 srl->mat_override= newlibadr_us(fd, sce->id.lib, srl->mat_override);
3150 srl->light_override= newlibadr_us(fd, sce->id.lib, srl->light_override);
3153 sce->id.flag -= LIB_NEEDLINK;
3160 static void link_recurs_seq(FileData *fd, ListBase *lb)
3167 if(seq->seqbase.first) link_recurs_seq(fd, &seq->seqbase);
3172 static void direct_link_scene(FileData *fd, Scene *sce)
3181 sce->dagisvalid = 0;
3182 /* set users to one by default, not in lib-link, this will increase it for compo nodes */
3185 link_list(fd, &(sce->base));
3187 sce->basact= newdataadr(fd, sce->basact);
3189 sce->radio= newdataadr(fd, sce->radio);
3191 sce->toolsettings= newdataadr(fd, sce->toolsettings);
3193 sce->sculptdata.session= NULL;
3194 /* SculptData textures */
3195 for(a=0; a<MAX_MTEX; ++a)
3196 sce->sculptdata.mtex[a]= newdataadr(fd,sce->sculptdata.mtex[a]);
3199 ListBase *old_seqbasep= &((Editing *)sce->ed)->seqbase;
3201 ed= sce->ed= newdataadr(fd, sce->ed);
3203 /* recursive link sequences, lb will be correctly initialized */
3204 link_recurs_seq(fd, &ed->seqbase);
3206 WHILE_SEQ(&ed->seqbase) {
3207 seq->seq1= newdataadr(fd, seq->seq1);
3208 seq->seq2= newdataadr(fd, seq->seq2);
3209 seq->seq3= newdataadr(fd, seq->seq3);
3210 /* a patch: after introduction of effects with 3 input strips */
3211 if(seq->seq3==0) seq->seq3= seq->seq2;
3215 seq->plugin= newdataadr(fd, seq->plugin);
3216 seq->effectdata= newdataadr(fd, seq->effectdata);
3218 if (seq->type & SEQ_EFFECT) {
3219 seq->flag |= SEQ_EFFECT_NOT_LOADED;
3222 seq->strip= newdataadr(fd, seq->strip);
3223 if(seq->strip && seq->strip->done==0) {
3224 seq->strip->done= 1;
3226 /* standard: strips from effects/metas are not written, but are mallocced */
3228 if(seq->type==SEQ_IMAGE) {
3229 seq->strip->stripdata= newdataadr(fd, seq->strip->stripdata);
3230 se= seq->strip->stripdata;
3232 for(a=0; a<seq->strip->len; a++, se++) {
3238 else if(seq->type==SEQ_MOVIE) {
3239 /* only first stripelem is in file */
3240 se= newdataadr(fd, seq->strip->stripdata);