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_constraint.h"
116 #include "BKE_curve.h"
117 #include "BKE_customdata.h"
118 #include "BKE_deform.h"
119 #include "BKE_depsgraph.h"
120 #include "BKE_effect.h" // for give_parteff
121 #include "BKE_global.h" // for G
122 #include "BKE_group.h"
123 #include "BKE_property.h" // for get_property
124 #include "BKE_lattice.h"
125 #include "BKE_library.h" // for wich_libbase
126 #include "BKE_main.h" // for Main
127 #include "BKE_mesh.h" // for ME_ defines (patching)
128 #include "BKE_modifier.h"
129 #include "BKE_node.h" // for tree type defines
130 #include "BKE_object.h"
131 #include "BKE_sca.h" // for init_actuator
132 #include "BKE_scene.h"
133 #include "BKE_softbody.h" // sbNew()
134 #include "BKE_texture.h" // for open_plugin_tex
135 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
136 #include "BKE_idprop.h"
138 #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
139 #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
140 #include "BLO_readfile.h"
141 #include "BLO_undofile.h"
142 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
144 #include "multires.h"
146 #include "readfile.h"
150 #include "mydevice.h"
156 Remark: still a weak point is the newadress() function, that doesnt solve reading from
157 multiple files at the same time
159 (added remark: oh, i thought that was solved? will look at that... (ton)
162 - Existing Library (Main) push or free
172 - read associated 'direct data'
173 - link direct data (internal and to LibBlock)
175 - read USER data, only when indicated (file is ~/.B.blend)
177 - per Library (per Main)
180 - find LibBlocks and attach IDs to Main
181 - if external LibBlock
183 - or it's already read,
188 - read associated direct data
189 - link direct data (internal and to LibBlock)
191 - per Library with unread LibBlocks
196 - read associated direct data
197 - link direct data (internal and to LibBlock)
200 - link all LibBlocks and indirect pointers to libblocks
201 - initialize FileGlobal and copy pointers to Global
204 /* also occurs in library.c */
205 /* GS reads the memory pointed at in a specific ordering. There are,
206 * however two definitions for it. I have jotted them down here, both,
207 * but I think the first one is actually used. The thing is that
208 * big-endian systems might read this the wrong way round. OTOH, we
209 * constructed the IDs that are read out with this macro explicitly as
210 * well. I expect we'll sort it out soon... */
213 #define GS(a) (*((short *)(a)))
215 /* from misc_util: flip the bytes from x */
216 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
218 // only used here in readfile.c
219 #define SWITCH_LONGINT(a) { \
222 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
223 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
224 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
225 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
229 typedef struct OldNew {
234 typedef struct OldNewMap {
236 int nentries, entriessize;
242 /* local prototypes */
243 extern short freeN(void *vmemh); /* defined in util.h */
246 static OldNewMap *oldnewmap_new(void)
248 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
250 onm->entriessize= 1024;
251 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
256 static int verg_oldnewmap(const void *v1, const void *v2)
258 const struct OldNew *x1=v1, *x2=v2;
260 if( x1->old > x2->old) return 1;
261 else if( x1->old < x2->old) return -1;
266 static void oldnewmap_sort(FileData *fd)
268 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
269 fd->libmap->sorted= 1;
272 /* nr is zero for data, and ID code for libdata */
273 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
277 if(oldaddr==NULL || newaddr==NULL) return;
279 if (onm->nentries==onm->entriessize) {
280 int osize= onm->entriessize;
281 OldNew *oentries= onm->entries;
283 onm->entriessize*= 2;
284 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
286 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
290 entry= &onm->entries[onm->nentries++];
292 entry->newp= newaddr;
296 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
300 if (onm->lasthit<onm->nentries-1) {
301 OldNew *entry= &onm->entries[++onm->lasthit];
303 if (entry->old==addr) {
309 for (i=0; i<onm->nentries; i++) {
310 OldNew *entry= &onm->entries[i];
312 if (entry->old==addr) {
323 /* for libdata, nr has ID code, no increment */
324 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
328 if(addr==NULL) return NULL;
330 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
332 OldNew entry_s, *entry;
336 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
340 if (id && (!lib || id->lib)) {
346 for (i=0; i<onm->nentries; i++) {
347 OldNew *entry= &onm->entries[i];
349 if (entry->old==addr) {
352 if (id && (!lib || id->lib)) {
361 static void oldnewmap_free_unused(OldNewMap *onm)
365 for (i=0; i<onm->nentries; i++) {
366 OldNew *entry= &onm->entries[i];
368 MEM_freeN(entry->newp);
374 static void oldnewmap_clear(OldNewMap *onm)
380 static void oldnewmap_free(OldNewMap *onm)
382 MEM_freeN(onm->entries);
388 static void read_libraries(FileData *basefd, ListBase *mainlist);
390 /* ************ help functions ***************** */
392 static void add_main_to_main(Main *mainvar, Main *from)
394 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
397 a= set_listbasepointers(mainvar, lbarray);
398 a= set_listbasepointers(from, fromarray);
400 addlisttolist(lbarray[a], fromarray[a]);
404 void blo_join_main(ListBase *mainlist)
406 Main *tojoin, *mainl= mainlist->first;
408 while ((tojoin= mainl->next)) {
409 add_main_to_main(mainl, tojoin);
410 BLI_remlink(mainlist, tojoin);
415 static void split_libdata(ListBase *lb, Main *first)
427 if(mainvar->curlib==id->lib) {
428 lbn= wich_libbase(mainvar, GS(id->name));
430 BLI_addtail(lbn, id);
433 mainvar= mainvar->next;
435 if(mainvar==0) printf("error split_libdata\n");
441 void blo_split_main(ListBase *mainlist, Main *main)
443 ListBase *lbarray[MAX_LIBARRAY];
447 mainlist->first= mainlist->last= main;
450 if(main->library.first==NULL)
453 for (lib= main->library.first; lib; lib= lib->id.next) {
454 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
455 libmain->curlib= lib;
456 BLI_addtail(mainlist, libmain);
459 i= set_listbasepointers(main, lbarray);
461 split_libdata(lbarray[i], main->next);
464 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
465 static void cleanup_path(const char *relabase, char *name)
467 char filename[FILE_MAXFILE];
469 BLI_splitdirstring(name, filename);
470 BLI_cleanup_dir(relabase, name);
471 strcat(name, filename);
474 static Main *blo_find_main(ListBase *mainlist, const char *name, const char *relabase)
478 char name1[FILE_MAXDIR+FILE_MAXFILE];
481 cleanup_path(relabase, name1);
482 // printf("blo_find_main: original in %s\n", name);
483 // printf("blo_find_main: converted to %s\n", name1);
485 for (m= mainlist->first; m; m= m->next) {
486 char *libname= (m->curlib)?m->curlib->filename:m->name;
488 if (BLI_streq(name1, libname)) {
489 printf("blo_find_main: found library %s\n", libname);
494 m= MEM_callocN(sizeof(Main), "find_main");
495 BLI_addtail(mainlist, m);
497 lib= alloc_libblock(&m->library, ID_LI, "lib");
498 strcpy(lib->name, name);
499 strcpy(lib->filename, name1);
503 printf("blo_find_main: added new lib %s\n", name);
508 /* ************ FILE PARSING ****************** */
510 static void switch_endian_bh4(BHead4 *bhead)
512 /* the ID_.. codes */
513 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
515 if (bhead->code != ENDB) {
516 SWITCH_INT(bhead->len);
517 SWITCH_INT(bhead->SDNAnr);
518 SWITCH_INT(bhead->nr);
522 static void switch_endian_bh8(BHead8 *bhead)
524 /* the ID_.. codes */
525 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
527 if (bhead->code != ENDB) {
528 SWITCH_INT(bhead->len);
529 SWITCH_INT(bhead->SDNAnr);
530 SWITCH_INT(bhead->nr);
534 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
536 BHead4 *bhead4 = (BHead4 *) bhead;
537 #if defined(WIN32) && !defined(FREE_WINDOWS)
543 bhead4->code= bhead8->code;
544 bhead4->len= bhead8->len;
546 if (bhead4->code != ENDB) {
548 // why is this here ??
549 if (do_endian_swap) {
550 SWITCH_LONGINT(bhead8->old);
553 /* this patch is to avoid a long long being read from not-eight aligned positions
554 is necessary on SGI with -n32 compiling (no, is necessary on
555 any modern 64bit architecture) */
556 memcpy(&old, &bhead8->old, 8);
557 bhead4->old = (int) (old >> 3);
559 bhead4->SDNAnr= bhead8->SDNAnr;
560 bhead4->nr= bhead8->nr;
564 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
566 BHead8 *bhead8 = (BHead8 *) bhead;
568 bhead8->code= bhead4->code;
569 bhead8->len= bhead4->len;
571 if (bhead8->code != ENDB) {
572 bhead8->old= bhead4->old;
573 bhead8->SDNAnr= bhead4->SDNAnr;
574 bhead8->nr= bhead4->nr;
578 static BHeadN *get_bhead(FileData *fd)
583 BHeadN *new_bhead = 0;
589 // First read the bhead structure.
590 // Depending on the platform the file was written on this can
591 // be a big or little endian BHead4 or BHead8 structure.
593 // As usual 'ENDB' (the last *partial* bhead of the file)
594 // needs some special handling. We don't want to EOF just yet.
596 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
598 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
600 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
601 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
602 switch_endian_bh4(&bhead4);
605 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
606 bh8_from_bh4(&bhead, &bhead4);
608 memcpy(&bhead, &bhead4, sizeof(bhead));
615 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
617 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
618 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
619 switch_endian_bh8(&bhead8);
622 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
623 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
625 memcpy(&bhead, &bhead8, sizeof(bhead));
632 /* make sure people are not trying to pass bad blend files */
633 if (bhead.len < 0) fd->eof = 1;
635 // bhead now contains the (converted) bhead structure. Now read
636 // the associated data and put everything in a BHeadN (creative naming !)
639 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
641 new_bhead->next = new_bhead->prev = 0;
642 new_bhead->bhead = bhead;
644 readsize = fd->read(fd, new_bhead + 1, bhead.len);
646 if (readsize != bhead.len) {
648 MEM_freeN(new_bhead);
658 // We've read a new block. Now add it to the list
662 BLI_addtail(&fd->listbase, new_bhead);
668 BHead *blo_firstbhead(FileData *fd)
674 // Read in a new block if necessary
676 new_bhead = fd->listbase.first;
677 if (new_bhead == 0) {
678 new_bhead = get_bhead(fd);
682 bhead = &new_bhead->bhead;
688 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
690 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
691 BHeadN *prev= bheadn->prev;
693 return prev?&prev->bhead:NULL;
696 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
698 BHeadN *new_bhead = 0;
702 // bhead is actually a sub part of BHeadN
703 // We calculate the BHeadN pointer from the BHead pointer below
704 new_bhead = (BHeadN *) (((char *) thisblock) - (int) (&((BHeadN*)0)->bhead));
706 // get the next BHeadN. If it doesn't exist we read in the next one
707 new_bhead = new_bhead->next;
708 if (new_bhead == 0) {
709 new_bhead = get_bhead(fd);
714 // here we do the reverse:
715 // go from the BHeadN pointer to the BHead pointer
716 bhead = &new_bhead->bhead;
722 static void decode_blender_header(FileData *fd)
724 char header[SIZEOFBLENDERHEADER], num[4];
727 // read in the header data
728 readsize = fd->read(fd, header, sizeof(header));
730 if (readsize == sizeof(header)) {
731 if(strncmp(header, "BLENDER", 7) == 0) {
732 int remove_this_endian_test= 1;
734 fd->flags |= FD_FLAGS_FILE_OK;
736 // what size are pointers in the file ?
738 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
739 if (sizeof(void *) != 4) {
740 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
743 if (sizeof(void *) != 8) {
744 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
748 // is the file saved in a different endian
750 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
751 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
754 // get the version number
756 memcpy(num, header+9, 3);
758 fd->fileversion = atoi(num);
763 static int read_file_dna(FileData *fd)
767 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
768 if (bhead->code==DNA1) {
769 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
771 fd->filesdna= dna_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
773 fd->compflags= dna_get_structDNA_compareflags(fd->filesdna, fd->memsdna);
776 } else if (bhead->code==ENDB)
782 static int fd_read_from_file(FileData *filedata, void *buffer, int size)
784 int readsize = read(filedata->filedes, buffer, size);
789 filedata->seek += readsize;
795 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, int size)
797 int readsize = gzread(filedata->gzfiledes, buffer, size);
802 filedata->seek += readsize;
808 static int fd_read_from_memory(FileData *filedata, void *buffer, int size)
810 // don't read more bytes then there are available in the buffer
811 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
813 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
814 filedata->seek += readsize;
819 static int fd_read_from_memfile(FileData *filedata, void *buffer, int size)
821 static unsigned int seek= 1<<30; /* the current position */
822 static unsigned int offset= 0; /* size of previous chunks */
823 static MemFileChunk *chunk=NULL;
825 if(size==0) return 0;
827 if(seek != (unsigned int)filedata->seek) {
828 chunk= filedata->memfile->chunks.first;
832 if(seek + chunk->size > (unsigned) filedata->seek) break;
837 seek= filedata->seek;
841 /* first check if it's on the end if current chunk */
842 if( seek-offset == chunk->size) {
843 offset+= chunk->size;
847 /* debug, should never happen */
849 printf("illegal read, chunk zero\n");
852 else if( (seek-offset)+size > chunk->size) {
853 size= chunk->size - (seek-offset);
854 printf("chunk too large, clipped to %d\n", size);
857 memcpy(buffer, chunk->buf + (seek-offset), size);
858 filedata->seek += size;
867 static FileData *filedata_new(void)
869 extern unsigned char DNAstr[]; /* DNA.c */
871 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
874 fd->gzfiledes = NULL;
876 /* XXX, this doesn't need to be done all the time,
877 * but it keeps us reentrant, remove once we have
878 * a lib that provides a nice lock. - zr
880 fd->memsdna = dna_sdna_from_data(DNAstr, DNAlen, 0);
882 fd->datamap = oldnewmap_new();
883 fd->globmap = oldnewmap_new();
884 fd->libmap = oldnewmap_new();
889 static FileData *blo_decode_and_check(FileData *fd, BlendReadError *error_r)
891 decode_blender_header(fd);
893 if (fd->flags & FD_FLAGS_FILE_OK) {
894 if (!read_file_dna(fd)) {
895 *error_r = BRE_INCOMPLETE;
896 blo_freefiledata(fd);
900 *error_r = BRE_NOT_A_BLEND;
901 blo_freefiledata(fd);
908 /* cannot be called with relative paths anymore! */
909 /* on each new library added, it now checks for the current FileData and expands relativeness */
910 FileData *blo_openblenderfile(char *name, BlendReadError *error_r)
914 gzfile= gzopen(name, "rb");
916 if (NULL == gzfile) {
917 *error_r = BRE_UNABLE_TO_OPEN;
920 FileData *fd = filedata_new();
921 fd->gzfiledes = gzfile;
922 BLI_strncpy(fd->filename, name, sizeof(fd->filename)); // now only in use by library append
923 fd->read = fd_read_gzip_from_file;
925 return blo_decode_and_check(fd, error_r);
929 FileData *blo_openblendermemory(void *mem, int memsize, BlendReadError *error_r)
931 if (!mem || memsize<SIZEOFBLENDERHEADER) {
932 *error_r = mem?BRE_UNABLE_TO_READ:BRE_UNABLE_TO_OPEN;
935 FileData *fd= filedata_new();
937 fd->buffersize= memsize;
938 fd->read= fd_read_from_memory;
939 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
941 return blo_decode_and_check(fd, error_r);
945 FileData *blo_openblendermemfile(MemFile *memfile, BlendReadError *error_r)
948 *error_r = BRE_UNABLE_TO_OPEN;
951 FileData *fd= filedata_new();
952 fd->memfile= memfile;
954 fd->read= fd_read_from_memfile;
955 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
957 return blo_decode_and_check(fd, error_r);
962 void blo_freefiledata(FileData *fd)
966 if (fd->filedes != -1) {
970 if (fd->gzfiledes != NULL)
972 gzclose(fd->gzfiledes);
975 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
976 MEM_freeN(fd->buffer);
980 // Free all BHeadN data blocks
981 BLI_freelistN(&fd->listbase);
984 dna_freestructDNA(fd->memsdna);
986 dna_freestructDNA(fd->filesdna);
988 MEM_freeN(fd->compflags);
991 oldnewmap_free(fd->datamap);
993 oldnewmap_free(fd->globmap);
995 oldnewmap_free(fd->imamap);
996 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
997 oldnewmap_free(fd->libmap);
1003 /* ************ DIV ****************** */
1005 int BLO_has_bfile_extension(char *str)
1007 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1010 /* ************** OLD POINTERS ******************* */
1012 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1014 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1017 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1019 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1022 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1024 if(fd->imamap && adr)
1025 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1030 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1032 return oldnewmap_liblookup(fd->libmap, adr, lib);
1035 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1037 ID *id= newlibadr(fd, lib, adr);
1045 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1049 for (i=0; i<fd->libmap->nentries; i++) {
1050 OldNew *entry= &fd->libmap->entries[i];
1052 if (old==entry->newp && entry->nr==ID_ID) {
1054 if(new) entry->nr= GS( ((ID *)new)->name );
1060 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1064 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1067 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1071 change_idid_adr_fd(fd, old, new);
1076 /* assumed; G.main still exists */
1077 void blo_make_image_pointer_map(FileData *fd)
1079 Image *ima= G.main->image.first;
1080 Scene *sce= G.main->scene.first;
1082 fd->imamap= oldnewmap_new();
1084 for(;ima; ima= ima->id.next) {
1086 oldnewmap_insert(fd->imamap, ima->ibuf, ima->ibuf, 0);
1088 for(; sce; sce= sce->id.next) {
1091 for(node= sce->nodetree->nodes.first; node; node= node->next)
1092 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1097 /* set G.main image ibufs to zero if it has been restored */
1098 /* this works because freeing G.main only happens after this call */
1099 void blo_end_image_pointer_map(FileData *fd)
1101 OldNew *entry= fd->imamap->entries;
1102 Image *ima= G.main->image.first;
1103 Scene *sce= G.main->scene.first;
1106 /* used entries were restored, so we put them to zero */
1107 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1112 for(;ima; ima= ima->id.next) {
1114 ima->ibuf= newimaadr(fd, ima->ibuf);
1115 /* this mirrors direct_link_image */
1120 for(; sce; sce= sce->id.next) {
1123 for(node= sce->nodetree->nodes.first; node; node= node->next)
1124 node->preview= newimaadr(fd, node->preview);
1129 /* undo file support: add all library pointers in lookup */
1130 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1132 Main *main= mainlist->first;
1133 ListBase *lbarray[MAX_LIBARRAY];
1135 for(main= main->next; main; main= main->next) {
1136 int i= set_listbasepointers(main, lbarray);
1139 for(id= lbarray[i]->first; id; id= id->next)
1140 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1146 /* ********** END OLD POINTERS ****************** */
1147 /* ********** READ FILE ****************** */
1149 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1151 int blocksize, nblocks;
1154 data= (char *)(bhead+1); /* BHEAD+DATA dependancy */
1155 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1159 dna_switch_endian_struct(filesdna, bhead->SDNAnr, data);
1165 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1170 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1171 switch_endian_structs(fd->filesdna, bh);
1173 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1174 if(fd->compflags[bh->SDNAnr]==2) {
1175 temp= dna_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1177 temp= MEM_mallocN(bh->len, blockname);
1178 memcpy(temp, (bh+1), bh->len); /* BHEAD+DATA dependancy */
1186 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1190 if(lb->first==0) return;
1192 lb->first= newdataadr(fd, lb->first);
1196 ln->next= newdataadr(fd, ln->next);
1204 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1209 if(lb->first==0) return;
1210 poin= newdataadr(fd, lb->first);
1212 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1219 poin= newdataadr(fd, ln->next);
1221 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1231 static void test_pointer_array(FileData *fd, void **mat)
1233 #if defined(WIN32) && !defined(FREE_WINDOWS)
1234 __int64 *lpoin, *lmat;
1236 long long *lpoin, *lmat;
1238 int len, *ipoin, *imat;
1240 /* manually convert the pointer array in
1241 * the old dna format to a pointer array in
1242 * the new dna format.
1245 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1247 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1248 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1252 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1253 SWITCH_LONGINT(*lpoin);
1254 *ipoin= (int) ((*lpoin) >> 3);
1262 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1263 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1277 /* ************ READ ID Properties *************** */
1279 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd);
1280 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd);
1282 void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, void *fd)
1286 /*since we didn't save the extra buffer, set totallen to len.*/
1287 prop->totallen = prop->len;
1288 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1290 if (switch_endian) {
1291 for (i=0; i<prop->len; i++) {
1292 SWITCH_INT(((int*)prop->data.pointer)[i]);
1297 void IDP_DirectLinkString(IDProperty *prop, int switch_endian, void *fd)
1299 /*since we didn't save the extra string buffer, set totallen to len.*/
1300 prop->totallen = prop->len;
1301 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1304 void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, void *fd)
1306 ListBase *lb = &prop->data.group;
1311 /*Link child id properties now*/
1312 for (loop=prop->data.group.first; loop; loop=loop->next) {
1313 IDP_DirectLinkProperty(loop, switch_endian, fd);
1317 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd)
1319 switch (prop->type) {
1321 IDP_DirectLinkGroup(prop, switch_endian, fd);
1324 IDP_DirectLinkString(prop, switch_endian, fd);
1327 IDP_DirectLinkArray(prop, switch_endian, fd);
1333 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd)
1337 /* ************ READ Brush *************** */
1338 /* library brush linking after fileread */
1339 static void lib_link_brush(FileData *fd, Main *main)
1345 /* only link ID pointers */
1346 for(brush= main->brush.first; brush; brush= brush->id.next) {
1347 if(brush->id.flag & LIB_NEEDLINK) {
1348 brush->id.flag -= LIB_NEEDLINK;
1350 for(a=0; a<MAX_MTEX; a++) {
1351 mtex= brush->mtex[a];
1353 mtex->tex= newlibadr_us(fd, brush->id.lib, mtex->tex);
1359 static void direct_link_brush(FileData *fd, Brush *brush)
1361 /* brush itself has been read */
1364 for(a=0; a<MAX_MTEX; a++)
1365 brush->mtex[a]= newdataadr(fd, brush->mtex[a]);
1368 /* ************ READ CurveMapping *************** */
1370 /* cuma itself has been read! */
1371 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1375 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1376 cumap->flag &= ~CUMA_PREMULLED;
1378 for(a=0; a<CM_TOT; a++) {
1379 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1380 cumap->cm[a].table= NULL;
1384 /* ************ READ NODE TREE *************** */
1386 /* singe node tree (also used for material/scene trees), ntree is not NULL */
1387 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
1391 for(node= ntree->nodes.first; node; node= node->next)
1392 node->id= newlibadr_us(fd, id->lib, node->id);
1395 /* library ntree linking after fileread */
1396 static void lib_link_nodetree(FileData *fd, Main *main)
1400 /* only link ID pointers */
1401 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1402 if(ntree->id.flag & LIB_NEEDLINK) {
1403 ntree->id.flag -= LIB_NEEDLINK;
1404 lib_link_ntree(fd, &ntree->id, ntree);
1409 /* verify types for nodes and groups, all data has to be read */
1410 static void lib_verify_nodetree(Main *main)
1416 /* now create the own typeinfo structs an verify nodes */
1417 /* here we still assume no groups in groups */
1418 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1419 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
1420 ntreeMakeOwnType(ntree); /* for group usage */
1423 /* now verify all types in material trees, groups are set OK now */
1424 for(ma= main->mat.first; ma; ma= ma->id.next) {
1426 ntreeVerifyTypes(ma->nodetree);
1428 /* and scene trees */
1429 for(sce= main->scene.first; sce; sce= sce->id.next) {
1431 ntreeVerifyTypes(sce->nodetree);
1437 /* ntree itself has been read! */
1438 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
1440 /* note: writing and reading goes in sync, for speed */
1445 ntree->init= 0; /* to set callbacks and force setting types */
1446 ntree->owntype= NULL;
1447 ntree->timecursor= NULL;
1449 link_list(fd, &ntree->nodes);
1450 for(node= ntree->nodes.first; node; node= node->next) {
1451 node->storage= newdataadr(fd, node->storage);
1454 /* could be handlerized at some point */
1455 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
1456 direct_link_curvemapping(fd, node->storage);
1457 else if(ntree->type==NTREE_COMPOSIT && (node->type==CMP_NODE_TIME || node->type==CMP_NODE_CURVE_VEC || node->type==CMP_NODE_CURVE_RGB))
1458 direct_link_curvemapping(fd, node->storage);
1461 link_list(fd, &node->inputs);
1462 link_list(fd, &node->outputs);
1464 link_list(fd, &ntree->links);
1466 /* and we connect the rest */
1467 for(node= ntree->nodes.first; node; node= node->next) {
1468 node->preview= newimaadr(fd, node->preview);
1470 for(sock= node->inputs.first; sock; sock= sock->next)
1471 sock->link= newdataadr(fd, sock->link);
1472 for(sock= node->outputs.first; sock; sock= sock->next)
1473 sock->ns.data= NULL;
1475 for(link= ntree->links.first; link; link= link->next) {
1476 link->fromnode= newdataadr(fd, link->fromnode);
1477 link->tonode= newdataadr(fd, link->tonode);
1478 link->fromsock= newdataadr(fd, link->fromsock);
1479 link->tosock= newdataadr(fd, link->tosock);
1482 /* type verification is in lib-link */
1485 /* ************ READ PACKEDFILE *************** */
1487 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1489 PackedFile *pf= newdataadr(fd, oldpf);
1492 pf->data= newdataadr(fd, pf->data);
1498 /* ************ READ IMAGE PREVIEW *************** */
1500 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1502 PreviewImage *prv= newdataadr(fd, old_prv);
1505 prv->rect = newdataadr(fd, prv->rect);
1511 /* ************ READ SCRIPTLINK *************** */
1513 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1517 for(i=0; i<slink->totscript; i++) {
1518 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1522 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1524 slink->scripts= newdataadr(fd, slink->scripts);
1525 test_pointer_array(fd, (void **)&slink->scripts);
1527 slink->flag= newdataadr(fd, slink->flag);
1529 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1532 for(a=0; a<slink->totscript; a++) {
1533 SWITCH_SHORT(slink->flag[a]);
1538 /* ************ READ ARMATURE ***************** */
1540 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1542 bActionStrip *strip;
1543 bActionModifier *amod;
1545 for (strip=striplist->first; strip; strip=strip->next){
1546 strip->object = newlibadr(fd, id->lib, strip->object);
1547 strip->act = newlibadr_us(fd, id->lib, strip->act);
1548 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1549 for(amod= strip->modifiers.first; amod; amod= amod->next)
1550 amod->ob= newlibadr(fd, id->lib, amod->ob);
1554 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1556 bConstraintChannel *chan;
1558 for (chan=chanbase->first; chan; chan=chan->next){
1559 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1563 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
1567 for (con = conlist->first; con; con=con->next) {
1568 /* patch for error introduced by changing constraints (dunno how) */
1569 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
1570 if(con->data==NULL) {
1571 con->type= CONSTRAINT_TYPE_NULL;
1574 switch (con->type) {
1575 case CONSTRAINT_TYPE_ACTION:
1577 bActionConstraint *data;
1578 data= ((bActionConstraint*)con->data);
1579 data->tar = newlibadr(fd, id->lib, data->tar);
1580 data->act = newlibadr(fd, id->lib, data->act);
1583 case CONSTRAINT_TYPE_LOCLIKE:
1585 bLocateLikeConstraint *data;
1586 data= ((bLocateLikeConstraint*)con->data);
1587 data->tar = newlibadr(fd, id->lib, data->tar);
1590 case CONSTRAINT_TYPE_ROTLIKE:
1592 bRotateLikeConstraint *data;
1593 data= ((bRotateLikeConstraint*)con->data);
1594 data->tar = newlibadr(fd, id->lib, data->tar);
1597 case CONSTRAINT_TYPE_SIZELIKE:
1599 bSizeLikeConstraint *data;
1600 data= ((bSizeLikeConstraint*)con->data);
1601 data->tar = newlibadr(fd, id->lib, data->tar);
1604 case CONSTRAINT_TYPE_KINEMATIC:
1606 bKinematicConstraint *data;
1607 data = ((bKinematicConstraint*)con->data);
1608 data->tar = newlibadr(fd, id->lib, data->tar);
1611 case CONSTRAINT_TYPE_TRACKTO:
1613 bTrackToConstraint *data;
1614 data = ((bTrackToConstraint*)con->data);
1615 data->tar = newlibadr(fd, id->lib, data->tar);
1618 case CONSTRAINT_TYPE_MINMAX:
1620 bMinMaxConstraint *data;
1621 data = ((bMinMaxConstraint*)con->data);
1622 data->tar = newlibadr(fd, id->lib, data->tar);
1625 case CONSTRAINT_TYPE_LOCKTRACK:
1627 bLockTrackConstraint *data;
1628 data= ((bLockTrackConstraint*)con->data);
1629 data->tar = newlibadr(fd, id->lib, data->tar);
1632 case CONSTRAINT_TYPE_FOLLOWPATH:
1634 bFollowPathConstraint *data;
1635 data= ((bFollowPathConstraint*)con->data);
1636 data->tar = newlibadr(fd, id->lib, data->tar);
1639 case CONSTRAINT_TYPE_DISTANCELIMIT:
1641 bDistanceLimitConstraint *data;
1642 data= ((bDistanceLimitConstraint*)con->data);
1643 data->tar = newlibadr(fd, id->lib, data->tar);
1646 case CONSTRAINT_TYPE_STRETCHTO:
1648 bStretchToConstraint *data;
1649 data= ((bStretchToConstraint*)con->data);
1650 data->tar = newlibadr(fd, id->lib, data->tar);
1653 case CONSTRAINT_TYPE_LOCLIMIT:
1655 bLocLimitConstraint *data;
1656 data= ((bLocLimitConstraint*)con->data);
1659 case CONSTRAINT_TYPE_ROTLIMIT:
1661 bRotLimitConstraint *data;
1662 data= ((bRotLimitConstraint*)con->data);
1665 case CONSTRAINT_TYPE_SIZELIMIT:
1667 bSizeLimitConstraint *data;
1668 data= ((bSizeLimitConstraint*)con->data);
1672 case CONSTRAINT_TYPE_NULL:
1678 static void direct_link_constraints(FileData *fd, ListBase *lb)
1683 for (cons=lb->first; cons; cons=cons->next) {
1684 cons->data = newdataadr(fd, cons->data);
1688 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
1690 bPoseChannel *pchan;
1691 bArmature *arm= ob->data;
1696 /* always rebuild to match lib changes */
1697 rebuild= (ob->id.lib==NULL && arm->id.lib);
1699 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
1700 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
1702 /* hurms... loop in a loop, but yah... later... (ton) */
1703 pchan->bone= get_named_bone(arm, pchan->name);
1705 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
1706 if(pchan->bone==NULL)
1708 else if(ob->id.lib==NULL && arm->id.lib) {
1709 /* local pose selection copied to armature, bit hackish */
1710 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1711 pchan->bone->flag |= pchan->selectflag;
1716 ob->recalc= OB_RECALC;
1717 pose->flag |= POSE_RECALC;
1721 static void lib_link_armature(FileData *fd, Main *main)
1725 arm= main->armature.first;
1728 if(arm->id.flag & LIB_NEEDLINK) {
1729 arm->id.flag -= LIB_NEEDLINK;
1735 static void lib_link_action(FileData *fd, Main *main)
1738 bActionChannel *chan;
1740 act= main->action.first;
1742 if(act->id.flag & LIB_NEEDLINK) {
1743 act->id.flag -= LIB_NEEDLINK;
1745 for (chan=act->chanbase.first; chan; chan=chan->next) {
1746 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1747 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1755 static void direct_link_bones(FileData *fd, Bone* bone)
1759 bone->parent= newdataadr(fd, bone->parent);
1761 link_list(fd, &bone->childbase);
1763 for (child=bone->childbase.first; child; child=child->next) {
1764 direct_link_bones(fd, child);
1769 static void direct_link_action(FileData *fd, bAction *act)
1771 bActionChannel *achan;
1773 link_list(fd, &act->chanbase);
1775 for (achan = act->chanbase.first; achan; achan=achan->next)
1776 link_list(fd, &achan->constraintChannels);
1780 static void direct_link_armature(FileData *fd, bArmature *arm)
1784 link_list(fd, &arm->bonebase);
1786 bone=arm->bonebase.first;
1788 direct_link_bones(fd, bone);
1793 /* ************ READ CAMERA ***************** */
1795 static void lib_link_camera(FileData *fd, Main *main)
1799 ca= main->camera.first;
1801 if(ca->id.flag & LIB_NEEDLINK) {
1803 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo);
1805 lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
1807 ca->id.flag -= LIB_NEEDLINK;
1813 static void direct_link_camera(FileData *fd, Camera *ca)
1815 direct_link_scriptlink(fd, &ca->scriptlink);
1819 /* ************ READ LAMP ***************** */
1821 static void lib_link_lamp(FileData *fd, Main *main)
1827 la= main->lamp.first;
1829 if(la->id.flag & LIB_NEEDLINK) {
1831 for(a=0; a<MAX_MTEX; a++) {
1834 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
1835 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
1839 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo);
1841 lib_link_scriptlink(fd, &la->id, &la->scriptlink);
1843 la->id.flag -= LIB_NEEDLINK;
1849 static void direct_link_lamp(FileData *fd, Lamp *la)
1853 direct_link_scriptlink(fd, &la->scriptlink);
1855 for(a=0; a<MAX_MTEX; a++) {
1856 la->mtex[a]= newdataadr(fd, la->mtex[a]);
1860 /* ************ READ keys ***************** */
1862 static void lib_link_key(FileData *fd, Main *main)
1866 key= main->key.first;
1868 if(key->id.flag & LIB_NEEDLINK) {
1870 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo);
1871 key->from= newlibadr(fd, key->id.lib, key->from);
1873 key->id.flag -= LIB_NEEDLINK;
1879 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
1882 char *data, *poin, *cp;
1884 elemsize= key->elemsize;
1887 for(a=0; a<kb->totelem; a++) {
1892 while( cp[0] ) { /* cp[0]==amount */
1894 switch(cp[1]) { /* cp[1]= type */
1900 SWITCH_INT((*poin));
1913 static void direct_link_key(FileData *fd, Key *key)
1917 link_list(fd, &(key->block));
1919 key->refkey= newdataadr(fd, key->refkey);
1921 kb= key->block.first;
1924 kb->data= newdataadr(fd, kb->data);
1926 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
1927 switch_endian_keyblock(key, kb);
1933 /* ************ READ mball ***************** */
1935 static void lib_link_mball(FileData *fd, Main *main)
1940 mb= main->mball.first;
1942 if(mb->id.flag & LIB_NEEDLINK) {
1944 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
1946 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo);
1948 mb->id.flag -= LIB_NEEDLINK;
1954 static void direct_link_mball(FileData *fd, MetaBall *mb)
1956 mb->mat= newdataadr(fd, mb->mat);
1957 test_pointer_array(fd, (void **)&mb->mat);
1959 link_list(fd, &(mb->elems));
1961 mb->disp.first= mb->disp.last= 0;
1966 /* ************ READ WORLD ***************** */
1968 static void lib_link_world(FileData *fd, Main *main)
1974 wrld= main->world.first;
1976 if(wrld->id.flag & LIB_NEEDLINK) {
1978 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo);
1980 for(a=0; a<MAX_MTEX; a++) {
1981 mtex= wrld->mtex[a];
1983 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
1984 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
1988 lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
1990 wrld->id.flag -= LIB_NEEDLINK;
1992 wrld= wrld->id.next;
1996 static void direct_link_world(FileData *fd, World *wrld)
2000 direct_link_scriptlink(fd, &wrld->scriptlink);
2002 for(a=0; a<MAX_MTEX; a++) {
2003 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2008 /* ************ READ IPO ***************** */
2010 static void lib_link_ipo(FileData *fd, Main *main)
2014 ipo= main->ipo.first;
2016 if(ipo->id.flag & LIB_NEEDLINK) {
2018 for(icu= ipo->curve.first; icu; icu= icu->next) {
2020 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
2022 ipo->id.flag -= LIB_NEEDLINK;
2028 static void direct_link_ipo(FileData *fd, Ipo *ipo)
2032 link_list(fd, &(ipo->curve));
2033 icu= ipo->curve.first;
2035 icu->bezt= newdataadr(fd, icu->bezt);
2036 icu->bp= newdataadr(fd, icu->bp);
2037 icu->driver= newdataadr(fd, icu->driver);
2042 /* ************ READ VFONT ***************** */
2044 static void lib_link_vfont(FileData *fd, Main *main)
2048 vf= main->vfont.first;
2050 if(vf->id.flag & LIB_NEEDLINK) {
2051 vf->id.flag -= LIB_NEEDLINK;
2057 static void direct_link_vfont(FileData *fd, VFont *vf)
2060 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2063 /* ************ READ TEXT ****************** */
2065 static void lib_link_text(FileData *fd, Main *main)
2069 text= main->text.first;
2071 if(text->id.flag & LIB_NEEDLINK) {
2072 text->id.flag -= LIB_NEEDLINK;
2074 text= text->id.next;
2078 static void direct_link_text(FileData *fd, Text *text)
2082 text->name= newdataadr(fd, text->name);
2085 text->undo_len= TXT_INIT_UNDO;
2086 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2088 text->compiled= NULL;
2091 if(text->flags & TXT_ISEXT) {
2096 link_list(fd, &text->lines);
2098 text->curl= newdataadr(fd, text->curl);
2099 text->sell= newdataadr(fd, text->sell);
2101 ln= text->lines.first;
2103 ln->line= newdataadr(fd, ln->line);
2106 if (ln->len != (int) strlen(ln->line)) {
2107 printf("Error loading text, line lengths differ\n");
2108 ln->len = strlen(ln->line);
2114 text->flags = (text->flags|TXT_ISTMP) & ~TXT_ISEXT;
2119 /* ************ READ IMAGE ***************** */
2121 static void lib_link_image(FileData *fd, Main *main)
2125 ima= main->image.first;
2127 if(ima->id.flag & LIB_NEEDLINK) {
2128 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2130 ima->id.flag -= LIB_NEEDLINK;
2136 static void direct_link_image(FileData *fd, Image *ima)
2138 /* for undo system, pointers could be restored */
2139 ima->ibuf= newimaadr(fd, ima->ibuf);
2140 /* if restored, we keep the binded opengl index */
2144 memset(ima->mipmap, 0, sizeof(ima->mipmap));
2148 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2149 ima->preview = direct_link_preview_image(fd, ima->preview);
2154 /* ************ READ CURVE ***************** */
2156 static void lib_link_curve(FileData *fd, Main *main)
2161 cu= main->curve.first;
2163 if(cu->id.flag & LIB_NEEDLINK) {
2165 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2167 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2168 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2169 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2170 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2171 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2172 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2173 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2175 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo);
2176 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2178 cu->id.flag -= LIB_NEEDLINK;
2185 static void switch_endian_knots(Nurb *nu)
2192 SWITCH_INT(nu->knotsu[len]);
2198 SWITCH_INT(nu->knotsv[len]);
2203 static void direct_link_curve(FileData *fd, Curve *cu)
2208 cu->mat= newdataadr(fd, cu->mat);
2209 test_pointer_array(fd, (void **)&cu->mat);
2210 cu->str= newdataadr(fd, cu->str);
2211 cu->strinfo= newdataadr(fd, cu->strinfo);
2212 cu->tb= newdataadr(fd, cu->tb);
2214 if(cu->vfont==0) link_list(fd, &(cu->nurb));
2216 cu->nurb.first=cu->nurb.last= 0;
2218 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2220 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2227 cu->tb[0].w = cu->linewidth;
2229 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2232 cu->bev.first=cu->bev.last= 0;
2233 cu->disp.first=cu->disp.last= 0;
2238 nu->bezt= newdataadr(fd, nu->bezt);
2239 nu->bp= newdataadr(fd, nu->bp);
2240 nu->knotsu= newdataadr(fd, nu->knotsu);
2241 nu->knotsv= newdataadr(fd, nu->knotsv);
2242 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2244 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2245 switch_endian_knots(nu);
2253 /* ************ READ TEX ***************** */
2255 static void lib_link_texture(FileData *fd, Main *main)
2259 tex= main->tex.first;
2261 if(tex->id.flag & LIB_NEEDLINK) {
2263 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2264 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2265 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2267 tex->id.flag -= LIB_NEEDLINK;
2273 static void direct_link_texture(FileData *fd, Tex *tex)
2275 tex->plugin= newdataadr(fd, tex->plugin);
2277 tex->plugin->handle= 0;
2278 open_plugin_tex(tex->plugin);
2279 /* initialize data for this instance, if an initialization
2282 if (tex->plugin->instance_init)
2283 tex->plugin->instance_init((void *) tex->plugin->data);
2285 tex->coba= newdataadr(fd, tex->coba);
2286 tex->env= newdataadr(fd, tex->env);
2289 memset(tex->env->cube, 0, 6*sizeof(void *));
2296 /* ************ READ MATERIAL ***************** */
2298 static void lib_link_material(FileData *fd, Main *main)
2304 ma= main->mat.first;
2306 if(ma->id.flag & LIB_NEEDLINK) {
2307 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2308 of library blocks that implement this.*/
2309 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2311 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2312 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2314 for(a=0; a<MAX_MTEX; a++) {
2317 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2318 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2321 lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
2324 lib_link_ntree(fd, &ma->id, ma->nodetree);
2326 ma->id.flag -= LIB_NEEDLINK;
2332 static void direct_link_material(FileData *fd, Material *ma)
2336 for(a=0; a<MAX_MTEX; a++) {
2337 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2340 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2341 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2343 direct_link_scriptlink(fd, &ma->scriptlink);
2345 ma->nodetree= newdataadr(fd, ma->nodetree);
2347 direct_link_nodetree(fd, ma->nodetree);
2350 /* ************ READ MESH ***************** */
2352 static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
2357 for (i=0; i<totface; i++, tf++) {
2358 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
2359 if(tf->tpage && tf->tpage->id.us==0)
2360 tf->tpage->id.us= 1;
2364 static void lib_link_mesh(FileData *fd, Main *main)
2368 me= main->mesh.first;
2370 if(me->id.flag & LIB_NEEDLINK) {
2373 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2374 of library blocks that implement this.*/
2375 if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2377 /* this check added for python created meshes */
2379 for(i=0; i<me->totcol; i++) {
2380 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
2385 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
2386 me->key= newlibadr_us(fd, me->id.lib, me->key);
2387 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
2389 for(i=0; i<me->fdata.totlayer; i++) {
2390 CustomDataLayer *layer = &me->fdata.layers[i];
2392 if(layer->type == CD_MTFACE)
2393 lib_link_mtface(fd, me, layer->data, me->totface);
2395 me->id.flag -= LIB_NEEDLINK;
2401 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
2408 for (i=0; i<count; i++) {
2409 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
2411 mdverts[i].totweight=0;
2415 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
2419 data->layers= newdataadr(fd, data->layers);
2421 while (i < data->totlayer) {
2422 CustomDataLayer *layer = &data->layers[i];
2424 if (layer->type < CD_NUMTYPES) {
2425 layer->data = newdataadr(fd, layer->data);
2429 /* delete layer with unknown type */
2431 CustomData_free_layer(data, layer->type, 0);
2436 static void direct_link_mesh(FileData *fd, Mesh *mesh)
2438 mesh->mat= newdataadr(fd, mesh->mat);
2439 test_pointer_array(fd, (void **)&mesh->mat);
2441 mesh->mvert= newdataadr(fd, mesh->mvert);
2442 mesh->medge= newdataadr(fd, mesh->medge);
2443 mesh->mface= newdataadr(fd, mesh->mface);
2444 mesh->tface= newdataadr(fd, mesh->tface);
2445 mesh->mtface= newdataadr(fd, mesh->mtface);
2446 mesh->mcol= newdataadr(fd, mesh->mcol);
2447 mesh->msticky= newdataadr(fd, mesh->msticky);
2448 mesh->dvert= newdataadr(fd, mesh->dvert);
2450 /* normally direct_link_dverts should be called in direct_link_customdata,
2451 but for backwards compat in do_versions to work we do it here */
2452 direct_link_dverts(fd, mesh->totvert, mesh->dvert);
2454 direct_link_customdata(fd, &mesh->vdata, mesh->totvert);
2455 direct_link_customdata(fd, &mesh->edata, mesh->totedge);
2456 direct_link_customdata(fd, &mesh->fdata, mesh->totface);
2460 mesh->mselect= NULL;
2463 mesh->mr= newdataadr(fd, mesh->mr);
2466 link_list(fd, &mesh->mr->levels);
2467 for(lvl= mesh->mr->levels.first; lvl; lvl= lvl->next) {
2468 lvl->verts= newdataadr(fd, lvl->verts);
2469 lvl->faces= newdataadr(fd, lvl->faces);
2470 lvl->edges= newdataadr(fd, lvl->edges);
2471 lvl->texcolfaces= newdataadr(fd, lvl->texcolfaces);
2473 /* Recalculating the maps is faster than reading them from the file */
2474 multires_calc_level_maps(lvl);
2479 mesh->pv= newdataadr(fd, mesh->pv);
2481 mesh->pv->vert_map= newdataadr(fd, mesh->pv->vert_map);
2482 mesh->pv->edge_map= newdataadr(fd, mesh->pv->edge_map);
2483 mesh->pv->old_faces= newdataadr(fd, mesh->pv->old_faces);
2484 mesh->pv->old_edges= newdataadr(fd, mesh->pv->old_edges);
2487 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
2488 TFace *tf= mesh->tface;
2491 for (i=0; i<mesh->totface; i++, tf++) {
2492 SWITCH_INT(tf->col[0]);
2493 SWITCH_INT(tf->col[1]);
2494 SWITCH_INT(tf->col[2]);
2495 SWITCH_INT(tf->col[3]);
2500 /* ************ READ LATTICE ***************** */
2502 static void lib_link_latt(FileData *fd, Main *main)
2506 lt= main->latt.first;
2508 if(lt->id.flag & LIB_NEEDLINK) {
2510 lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo);
2511 lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
2513 lt->id.flag -= LIB_NEEDLINK;
2519 static void direct_link_latt(FileData *fd, Lattice *lt)
2521 lt->def= newdataadr(fd, lt->def);
2523 lt->dvert= newdataadr(fd, lt->dvert);
2524 direct_link_dverts(fd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
2528 /* ************ READ OBJECT ***************** */
2530 static void lib_link_modifiers__linkModifiers(void *userData, Object *ob,
2533 FileData *fd = userData;
2535 *idpoin = newlibadr(fd, ob->id.lib, *idpoin);
2537 static void lib_link_modifiers(FileData *fd, Object *ob)
2539 modifiers_foreachIDLink(ob, lib_link_modifiers__linkModifiers, fd);
2542 static void lib_link_object(FileData *fd, Main *main)
2552 ob= main->object.first;
2554 if(ob->id.flag & LIB_NEEDLINK) {
2555 if (ob->id.properties) IDP_LibLinkProperty(ob->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2557 ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
2558 ob->track= newlibadr(fd, ob->id.lib, ob->track);
2559 ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
2560 ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
2561 ob->dup_group= newlibadr_us(fd, ob->id.lib, ob->dup_group);
2563 /* no proxy in library data, is default local data */
2564 ob->proxy= NULL; ob->proxy_group= NULL;
2567 ob->proxy= newlibadr_us(fd, ob->id.lib, ob->proxy);
2569 /* this triggers object_update to always use a copy */
2570 ob->proxy->proxy= ob;
2571 /* force proxy updates after load/undo, a bit weak */
2572 ob->recalc= ob->proxy->recalc= OB_RECALC;
2574 ob->proxy_group= newlibadr(fd, ob->id.lib, ob->proxy_group);
2577 ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
2579 if(ob->data==NULL && poin!=NULL) {
2582 if(ob->id.lib) printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
2583 else printf("Object %s lost data. Lib:%x\n", ob->id.name+2, (unsigned int) ob->id.lib);
2585 for(a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
2587 ob->id.flag -= LIB_NEEDLINK;
2588 /* if id.us==0 a new base will be created later on */
2590 /* WARNING! Also check expand_object(), should reflect the stuff below. */
2591 lib_link_pose(fd, ob, ob->pose);
2592 lib_link_constraints(fd, &ob->id, &ob->constraints);
2593 lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
2594 lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
2596 for(paf= ob->effect.first; paf; paf= paf->next) {
2597 if(paf->type==EFF_PARTICLE) {
2598 paf->group= newlibadr_us(fd, ob->id.lib, paf->group);
2602 sens= ob->sensors.first;
2604 if(ob->id.lib==NULL) { // done in expand_main
2605 for(a=0; a<sens->totlinks; a++) {
2606 sens->links[a]= newglobadr(fd, sens->links[a]);
2609 if(sens->type==SENS_TOUCH) {
2610 bTouchSensor *ts= sens->data;
2611 ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
2613 else if(sens->type==SENS_MESSAGE) {
2614 bMessageSensor *ms= sens->data;
2616 newlibadr(fd, ob->id.lib, ms->fromObject);
2621 cont= ob->controllers.first;
2623 if(ob->id.lib==NULL) { // done in expand_main
2624 for(a=0; a<cont->totlinks; a++) {
2625 cont->links[a]= newglobadr(fd, cont->links[a]);
2628 if(cont->type==CONT_PYTHON) {
2629 bPythonCont *pc= cont->data;
2630 pc->text= newlibadr(fd, ob->id.lib, pc->text);
2638 act= ob->actuators.first;
2640 if(act->type==ACT_SOUND) {
2641 bSoundActuator *sa= act->data;
2642 sa->sound= newlibadr_us(fd, ob->id.lib, sa->sound);
2644 else if(act->type==ACT_CD) {
2645 /* bCDActuator *cda= act->data; */
2647 else if(act->type==ACT_GAME) {
2648 /* bGameActuator *ga= act->data; */
2650 else if(act->type==ACT_CAMERA) {
2651 bCameraActuator *ca= act->data;
2652 ca->ob= newlibadr(fd, ob->id.lib, ca->ob);
2654 /* leave this one, it's obsolete but necessary to read for conversion */
2655 else if(act->type==ACT_ADD_OBJECT) {
2656 bAddObjectActuator *eoa= act->data;
2657 if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2659 else if(act->type==ACT_EDIT_OBJECT) {
2660 bEditObjectActuator *eoa= act->data;
2664 eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
2665 eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
2667 else if(act->type==ACT_SCENE) {
2668 bSceneActuator *sa= act->data;
2669 sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
2670 sa->scene= newlibadr(fd, ob->id.lib, sa->scene);
2672 else if(act->type==ACT_ACTION) {
2673 bActionActuator *aa= act->data;
2674 aa->act= newlibadr(fd, ob->id.lib, aa->act);
2676 else if(act->type==ACT_PROPERTY) {
2677 bPropertyActuator *pa= act->data;
2678 pa->ob= newlibadr(fd, ob->id.lib, pa->ob);
2680 else if(act->type==ACT_MESSAGE) {
2681 bMessageActuator *ma= act->data;
2682 ma->toObject= newlibadr(fd, ob->id.lib, ma->toObject);
2687 if(ob->fluidsimSettings) {
2688 ob->fluidsimSettings->ipo = newlibadr_us(fd, ob->id.lib, ob->fluidsimSettings->ipo);
2691 lib_link_scriptlink(fd, &ob->id, &ob->scriptlink);
2692 lib_link_modifiers(fd, ob);
2697 if(warn) error("WARNING IN CONSOLE");
2701 static void direct_link_pose(FileData *fd, bPose *pose) {
2703 bPoseChannel *pchan;
2708 link_list(fd, &pose->chanbase);
2710 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2712 pchan->parent= newdataadr(fd, pchan->parent);
2713 pchan->child= newdataadr(fd, pchan->child);
2714 direct_link_constraints(fd, &pchan->constraints);
2715 pchan->iktree.first= pchan->iktree.last= NULL;
2721 static void direct_link_modifiers(FileData *fd, ListBase *lb)
2727 for (md=lb->first; md; md=md->next) {
2730 /* if modifiers disappear, or for upward compatibility */
2731 if(NULL==modifierType_getInfo(md->type))
2732 md->type= eModifierType_None;
2734 if (md->type==eModifierType_Subsurf) {
2735 SubsurfModifierData *smd = (SubsurfModifierData*) md;
2737 smd->emCache = smd->mCache = 0;
2738 } else if (md->type==eModifierType_Hook) {
2739 HookModifierData *hmd = (HookModifierData*) md;
2741 hmd->indexar= newdataadr(fd, hmd->indexar);
2742 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2744 for(a=0; a<hmd->totindex; a++) {
2745 SWITCH_INT(hmd->indexar[a]);
2752 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
2754 bActionStrip *strip;
2756 link_list(fd, strips);
2758 for(strip= strips->first; strip; strip= strip->next)
2759 link_list(fd, &strip->modifiers);
2762 static void direct_link_object(FileData *fd, Object *ob)
2771 /* weak weak... this was only meant as draw flag, now is used in give_base too */
2772 ob->flag &= ~OB_FROMGROUP;
2774 ob->disp.first=ob->disp.last= NULL;
2776 ob->pose= newdataadr(fd, ob->pose);
2777 direct_link_pose(fd, ob->pose);
2779 link_list(fd, &ob->defbase);
2780 direct_link_nlastrips(fd, &ob->nlastrips);
2781 link_list(fd, &ob->constraintChannels);
2783 direct_link_scriptlink(fd, &ob->scriptlink);
2785 ob->mat= newdataadr(fd, ob->mat);
2786 test_pointer_array(fd, (void **)&ob->mat);
2788 /* do it here, below old data gets converted */
2789 direct_link_modifiers(fd, &ob->modifiers);
2791 link_list(fd, &ob->effect);
2792 paf= ob->effect.first;
2794 if(paf->type==EFF_PARTICLE) {
2797 if(paf->type==EFF_WAVE) {
2798 WaveEff *wav = (WaveEff*) paf;
2799 PartEff *next = paf->next;
2800 WaveModifierData *wmd = (WaveModifierData*) modifier_new(eModifierType_Wave);
2802 wmd->damp = wav->damp;
2803 wmd->flag = wav->flag;
2804 wmd->height = wav->height;
2805 wmd->lifetime = wav->lifetime;
2806 wmd->narrow = wav->narrow;
2807 wmd->speed = wav->speed;
2808 wmd->startx = wav->startx;
2809 wmd->starty = wav->startx;
2810 wmd->timeoffs = wav->timeoffs;
2811 wmd->width = wav->width;
2813 BLI_addtail(&ob->modifiers, wmd);
2815 BLI_remlink(&ob->effect, paf);
2821 if(paf->type==EFF_BUILD) {
2822 BuildEff *baf = (BuildEff*) paf;
2823 PartEff *next = paf->next;
2824 BuildModifierData *bmd = (BuildModifierData*) modifier_new(eModifierType_Build);
2826 bmd->start = baf->sfra;
2827 bmd->length = baf->len;
2831 BLI_addtail(&ob->modifiers, bmd);
2833 BLI_remlink(&ob->effect, paf);
2842 ob->pd= newdataadr(fd, ob->pd);
2843 ob->soft= newdataadr(fd, ob->soft);
2845 SoftBody *sb= ob->soft;
2847 sb->bpoint= NULL; // init pointers so it gets rebuilt nicely
2852 sb->keys= newdataadr(fd, sb->keys);
2853 test_pointer_array(fd, (void **)&sb->keys);
2855 for(a=0; a<sb->totkey; a++) {
2856 sb->keys[a]= newdataadr(fd, sb->keys[a]);
2860 ob->fluidsimSettings= newdataadr(fd, ob->fluidsimSettings); /* NT */
2861 if(ob->fluidsimSettings) {
2862 // reinit mesh pointers
2863 ob->fluidsimSettings->orgMesh = NULL; //ob->data;
2864 ob->fluidsimSettings->meshSurface = NULL;
2865 ob->fluidsimSettings->meshBB = NULL;
2866 ob->fluidsimSettings->meshSurfNormals = NULL;
2869 link_list(fd, &ob->prop);
2870 prop= ob->prop.first;
2872 prop->poin= newdataadr(fd, prop->poin);
2873 if(prop->poin==0) prop->poin= &prop->data;
2877 link_list(fd, &ob->sensors);
2878 sens= ob->sensors.first;
2880 sens->data= newdataadr(fd, sens->data);
2881 sens->links= newdataadr(fd, sens->links);
2882 test_pointer_array(fd, (void **)&sens->links);
2886 direct_link_constraints(fd, &ob->constraints);
2888 link_glob_list(fd, &ob->controllers);
2889 cont= ob->controllers.first;
2891 cont->data= newdataadr(fd, cont->data);
2892 cont->links= newdataadr(fd, cont->links);
2893 test_pointer_array(fd, (void **)&cont->links);
2897 link_glob_list(fd, &ob->actuators);
2898 act= ob->actuators.first;
2900 act->data= newdataadr(fd, act->data);
2904 link_list(fd, &ob->hooks);
2905 while (ob->hooks.first) {
2906 ObHook *hook = ob->hooks.first;
2907 HookModifierData *hmd = (HookModifierData*) modifier_new(eModifierType_Hook);
2909 hook->indexar= newdataadr(fd, hook->indexar);
2910 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2912 for(a=0; a<hook->totindex; a++) {
2913 SWITCH_INT(hook->indexar[a]);
2917 /* Do conversion here because if we have loaded
2918 * a hook we need to make sure it gets converted
2919 * and free'd, regardless of version.
2921 VECCOPY(hmd->cent, hook->cent);
2922 hmd->falloff = hook->falloff;
2923 hmd->force = hook->force;
2924 hmd->indexar = hook->indexar;
2925 hmd->object = hook->parent;
2926 memcpy(hmd->parentinv, hook->parentinv, sizeof(hmd->parentinv));
2927 hmd->totindex = hook->totindex;
2929 BLI_addhead(&ob->modifiers, hmd);
2930 BLI_remlink(&ob->hooks, hook);
2936 ob->derivedDeform= NULL;
2937 ob->derivedFinal= NULL;
2940 /* ************ READ SCENE ***************** */
2942 static void lib_link_scene(FileData *fd, Main *main)
2950 sce= main->scene.first;
2952 if(sce->id.flag & LIB_NEEDLINK) {
2953 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2954 of library blocks that implement this.*/
2955 if (sce->id.properties) IDP_LibLinkProperty(sce->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2957 sce->camera= newlibadr(fd, sce->id.lib, sce->camera);
2958 sce->world= newlibadr_us(fd, sce->id.lib, sce->world);
2959 sce->set= newlibadr(fd, sce->id.lib, sce->set);
2960 sce->ima= newlibadr_us(fd, sce->id.lib, sce->ima);
2961 sce->toolsettings->imapaint.brush=
2962 newlibadr_us(fd, sce->id.lib, sce->toolsettings->imapaint.brush);
2964 /* Sculptdata textures */
2965 for(a=0; a<MAX_MTEX; ++a) {
2966 MTex *mtex= sce->sculptdata.mtex[a];
2968 mtex->tex= newlibadr_us(fd, sce->id.lib, mtex->tex);
2971 base= sce->base.first;
2975 /* base->object= newlibadr_us(fd, sce->id.lib, base->object); */
2976 base->object= newlibadr_us(fd, sce->id.lib, base->object);
2978 /* when save during radiotool, needs cleared */
2979 base->flag &= ~OB_RADIO;
2981 if(base->object==NULL) {
2982 printf("LIB ERROR: base removed\n");
2983 BLI_remlink(&sce->base, base);
2984 if(base==sce->basact) sce->basact= 0;
2992 WHILE_SEQ(&ed->seqbase) {
2993 if(seq->ipo) seq->ipo= newlibadr_us(fd, sce->id.lib, seq->ipo);
2994 if(seq->scene) seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
2996 seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
2998 seq->sound->id.us++;
2999 seq->sound->flags |= SOUND_FLAGS_SEQUENCE;
3008 lib_link_scriptlink(fd, &sce->id, &sce->scriptlink);
3011 lib_link_ntree(fd, &sce->id, sce->nodetree);
3013 sce->id.flag -= LIB_NEEDLINK;
3020 static void link_recurs_seq(FileData *fd, ListBase *lb)
3027 if(seq->seqbase.first) link_recurs_seq(fd, &seq->seqbase);
3032 static void direct_link_scene(FileData *fd, Scene *sce)
3041 sce->dagisvalid = 0;
3042 /* set users to one by default, not in lib-link, this will increase it for compo nodes */
3045 link_list(fd, &(sce->base));
3047 sce->basact= newdataadr(fd, sce->basact);
3049 sce->radio= newdataadr(fd, sce->radio);
3051 sce->toolsettings= newdataadr(fd, sce->toolsettings);
3054 sce->sculptdata.active_ob= NULL;
3055 sce->sculptdata.vertex_users= NULL;
3056 sce->sculptdata.texrndr= NULL;
3057 sce->sculptdata.propset= 0;
3058 sce->sculptdata.undo= NULL;
3059 /* SculptData textures */
3060 for(a=0; a<MAX_MTEX; ++a)
3061 sce->sculptdata.mtex[a]= newdataadr(fd,sce->sculptdata.mtex[a]);
3064 ListBase *old_seqbasep= &((Editing *)sce->ed)->seqbase;
3066 ed= sce->ed= newdataadr(fd, sce->ed);
3068 /* recursive link sequences, lb will be correctly initialized */
3069 link_recurs_seq(fd, &ed->seqbase);
3071 WHILE_SEQ(&ed->seqbase) {
3072 seq->seq1= newdataadr(fd, seq->seq1);
3073 seq->seq2= newdataadr(fd, seq->seq2);
3074 seq->seq3= newdataadr(fd, seq->seq3);
3075 /* a patch: after introduction of effects with 3 input strips */
3076 if(seq->seq3==0) seq->seq3= seq->seq2;
3080 seq->plugin= newdataadr(fd, seq->plugin);
3081 seq->effectdata= newdataadr(fd, seq->effectdata);
3083 if (seq->type & SEQ_EFFECT) {
3084 seq->flag |= SEQ_EFFECT_NOT_LOADED;
3087 seq->strip= newdataadr(fd, seq->strip);
3088 if(seq->strip && seq->strip->done==0) {
3089 seq->strip->done= 1;
3091 /* standard: strips from effects/metas are not written, but are mallocced */
3093 if(seq->type==SEQ_IMAGE) {
3094 seq->strip->stripdata= newdataadr(fd, seq->strip->stripdata);
3095 se= seq->strip->stripdata;
3097 for(a=0; a<seq->strip->len; a++, se++) {
3103 else if(seq->type==SEQ_MOVIE) {
3104 /* only first stripelem is in file */
3105 se= newdataadr(fd, seq->strip->stripdata);
3108 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
3109 *seq->strip->stripdata= *se;
3112 se= seq->strip->stripdata;
3114 for(a=0; a<seq->strip->len; a++, se++) {
3121 else if(seq->type==SEQ_RAM_SOUND
3122 || seq->type == SEQ_HD_SOUND) {
3123 /* only first stripelem is in file */
3124 se= newdataadr(fd, seq->strip->stripdata);
3127 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
3128 *seq->strip->stripdata= *se;
3131 se= seq->strip->stripdata;
3133 for(a=0; a<seq->strip->len; a++, se++) {
3134 se->ok= 2; /* why? */
3141 seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
3147 /* link metastack, slight abuse of structs here, have to restore pointer to internal part in struct */
3153 offset= ((long)&(temp.seqbase)) - ((long)&temp);
3156 if(ed->seqbasep == old_seqbasep) {
3157 ed->seqbasep= &ed->seqbase;
3161 poin= (char *)ed->seqbasep;
3164 poin= newdataadr(fd, poin);
3165 if(poin) ed->seqbasep= (ListBase *)(poin+offset);
3166 else ed->seqbasep= &ed->seqbase;
3169 link_list(fd, &(ed->metastack));
3171 for(ms= ed->metastack.first; ms; ms= ms->next) {
3172 ms->parseq= newdataadr(fd, ms->parseq);
3174 if(ms->oldbasep == old_seqbasep)
3175 ms->oldbasep= &ed->seqbase;
3177 poin= (char *)ms->oldbasep;
3179 poin= newdataadr(fd, poin);
3180 if(poin) ms->oldbasep= (ListBase *)(poin+offset);
3181 else ms->oldbasep= &ed->seqbase;
3187 direct_link_scriptlink(fd, &sce->scriptlink);
3189 sce->r.avicodecdata = newdataadr(fd, sce->r.avicodecdata);
3190 if (sce->r.avicodecdata) {
3191 sce->r.avicodecdata->lpFormat = newdataadr(fd, sce->r.avicodecdata->lpFormat);
3192 sce->r.avicodecdata->lpParms = newdataadr(fd, sce->r.avicodecdata->lpParms);
3195 sce->r.qtcodecdata = newdataadr(fd, sce->r.qtcodecdata);
3196 if (sce->r.qtcodecdata) {
3197 sce->r.qtcodecdata->cdParms = newdataadr(fd, sce->r.qtcodecdata->cdParms);
3200 link_list(fd, &(sce->markers));
3201 link_list(fd, &(sce->r.layers));
3203 sce->nodetree= newdataadr(fd, sce->nodetree);
3205 direct_link_nodetree(fd, sce->nodetree);
3209 /* Nasty exception; IpoWindow stores a non-ID pointer in *from for sequence
3210 strips... bad code warning!
3212 We work around it by retrieving the missing pointer from the corresponding
3215 This is needed, to make Ipo-Pinning work for Sequence-Ipos...
3217 static Sequence * find_sequence_from_ipo_helper(Main * main, Ipo * ipo)
3220 Sequence *seq = NULL;
3222 Scene * sce= main->scene.first;
3229 WHILE_SEQ(&ed->seqbase) {