2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenloader/intern/readfile.c
36 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
37 #include <stdlib.h> // for getenv atoi
38 #include <stddef.h> // for offsetof
39 #include <fcntl.h> // for open
40 #include <string.h> // for strrchr strncmp strstr
41 #include <math.h> // for fabs
44 #include <unistd.h> // for read close
46 #include <io.h> // for open close read
48 #include "BLI_winstuff.h"
51 #include "DNA_anim_types.h"
52 #include "DNA_armature_types.h"
53 #include "DNA_actuator_types.h"
54 #include "DNA_brush_types.h"
55 #include "DNA_camera_types.h"
56 #include "DNA_cloth_types.h"
57 #include "DNA_controller_types.h"
58 #include "DNA_constraint_types.h"
59 #include "DNA_effect_types.h"
60 #include "DNA_fileglobal_types.h"
61 #include "DNA_genfile.h"
62 #include "DNA_group_types.h"
63 #include "DNA_gpencil_types.h"
64 #include "DNA_ipo_types.h"
65 #include "DNA_key_types.h"
66 #include "DNA_lattice_types.h"
67 #include "DNA_lamp_types.h"
68 #include "DNA_meta_types.h"
69 #include "DNA_material_types.h"
70 #include "DNA_mesh_types.h"
71 #include "DNA_meshdata_types.h"
72 #include "DNA_nla_types.h"
73 #include "DNA_node_types.h"
74 #include "DNA_object_fluidsim.h" // NT
75 #include "DNA_packedFile_types.h"
76 #include "DNA_particle_types.h"
77 #include "DNA_property_types.h"
78 #include "DNA_text_types.h"
79 #include "DNA_view3d_types.h"
80 #include "DNA_screen_types.h"
81 #include "DNA_sensor_types.h"
82 #include "DNA_sdna_types.h"
83 #include "DNA_scene_types.h"
84 #include "DNA_sequence_types.h"
85 #include "DNA_smoke_types.h"
86 #include "DNA_speaker_types.h"
87 #include "DNA_sound_types.h"
88 #include "DNA_space_types.h"
89 #include "DNA_vfont_types.h"
90 #include "DNA_world_types.h"
91 #include "DNA_movieclip_types.h"
93 #include "MEM_guardedalloc.h"
95 #include "BLI_utildefines.h"
96 #include "BLI_blenlib.h"
98 #include "BLI_cellalloc.h"
99 #include "BLI_edgehash.h"
101 #include "BKE_anim.h"
102 #include "BKE_action.h"
103 #include "BKE_armature.h"
104 #include "BKE_brush.h"
105 #include "BKE_colortools.h"
106 #include "BKE_constraint.h"
107 #include "BKE_context.h"
108 #include "BKE_curve.h"
109 #include "BKE_deform.h"
110 #include "BKE_effect.h" /* give_parteff */
111 #include "BKE_fcurve.h"
112 #include "BKE_global.h" // for G
113 #include "BKE_group.h"
114 #include "BKE_image.h"
115 #include "BKE_lattice.h"
116 #include "BKE_library.h" // for which_libbase
117 #include "BKE_idcode.h"
118 #include "BKE_material.h"
119 #include "BKE_main.h" // for Main
120 #include "BKE_mesh.h" // for ME_ defines (patching)
121 #include "BKE_modifier.h"
122 #include "BKE_multires.h"
123 #include "BKE_node.h" // for tree type defines
124 #include "BKE_object.h"
125 #include "BKE_paint.h"
126 #include "BKE_particle.h"
127 #include "BKE_pointcache.h"
128 #include "BKE_property.h" // for get_ob_property
129 #include "BKE_report.h"
130 #include "BKE_sca.h" // for init_actuator
131 #include "BKE_scene.h"
132 #include "BKE_screen.h"
133 #include "BKE_sequencer.h"
134 #include "BKE_texture.h" // for open_plugin_tex
135 #include "BKE_tracking.h"
136 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
137 #include "BKE_sound.h"
139 #include "NOD_socket.h"
141 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
142 //XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
143 //XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
144 #include "BLO_readfile.h"
145 #include "BLO_undofile.h"
147 #include "RE_engine.h"
149 #include "readfile.h"
151 #include "PIL_time.h"
156 Remark: still a weak point is the newaddress() 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 ~/X.XX/startup.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 static void *read_struct(FileData *fd, BHead *bh, const char *blockname);
244 static void direct_link_modifiers(FileData *fd, ListBase *lb);
245 static void convert_tface_mt(FileData *fd, Main *main);
247 static OldNewMap *oldnewmap_new(void)
249 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
251 onm->entriessize= 1024;
252 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
257 static int verg_oldnewmap(const void *v1, const void *v2)
259 const struct OldNew *x1=v1, *x2=v2;
261 if( x1->old > x2->old) return 1;
262 else if( x1->old < x2->old) return -1;
267 static void oldnewmap_sort(FileData *fd)
269 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
270 fd->libmap->sorted= 1;
273 /* nr is zero for data, and ID code for libdata */
274 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
278 if(oldaddr==NULL || newaddr==NULL) return;
280 if (onm->nentries==onm->entriessize) {
281 int osize= onm->entriessize;
282 OldNew *oentries= onm->entries;
284 onm->entriessize*= 2;
285 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
287 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
291 entry= &onm->entries[onm->nentries++];
293 entry->newp= newaddr;
297 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
301 if(addr==NULL) return NULL;
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 set_listbasepointers(mainvar, lbarray);
401 a= set_listbasepointers(from, fromarray);
403 BLI_movelisttolist(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= which_libbase(mainvar, GS(id->name));
435 BLI_addtail(lbn, id);
438 mainvar= mainvar->next;
440 if(mainvar==NULL) 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 void read_file_version(FileData *fd, Main *main)
483 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
484 if (bhead->code==GLOB) {
485 FileGlobal *fg= read_struct(fd, bhead, "Global");
487 main->subversionfile= fg->subversion;
488 main->minversionfile= fg->minversion;
489 main->minsubversionfile= fg->minsubversion;
492 else if (bhead->code==ENDB)
499 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *filepath, const char *relabase)
503 char name1[FILE_MAXDIR+FILE_MAXFILE];
505 BLI_strncpy(name1, filepath, sizeof(name1));
506 cleanup_path(relabase, name1);
507 // printf("blo_find_main: original in %s\n", name);
508 // printf("blo_find_main: converted to %s\n", name1);
510 for (m= mainlist->first; m; m= m->next) {
511 char *libname= (m->curlib)?m->curlib->filepath:m->name;
513 if (BLI_path_cmp(name1, libname) == 0) {
514 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
519 m= MEM_callocN(sizeof(Main), "find_main");
520 BLI_addtail(mainlist, m);
522 lib= alloc_libblock(&m->library, ID_LI, "lib");
523 BLI_strncpy(lib->name, filepath, sizeof(lib->name));
524 BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
528 read_file_version(fd, m);
530 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", filepath);
535 /* ************ FILE PARSING ****************** */
537 static void switch_endian_bh4(BHead4 *bhead)
539 /* the ID_.. codes */
540 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
542 if (bhead->code != ENDB) {
543 SWITCH_INT(bhead->len);
544 SWITCH_INT(bhead->SDNAnr);
545 SWITCH_INT(bhead->nr);
549 static void switch_endian_bh8(BHead8 *bhead)
551 /* the ID_.. codes */
552 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
554 if (bhead->code != ENDB) {
555 SWITCH_INT(bhead->len);
556 SWITCH_INT(bhead->SDNAnr);
557 SWITCH_INT(bhead->nr);
561 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
563 BHead4 *bhead4 = (BHead4 *) bhead;
564 #if defined(WIN32) && !defined(FREE_WINDOWS)
570 bhead4->code= bhead8->code;
571 bhead4->len= bhead8->len;
573 if (bhead4->code != ENDB) {
575 //perform a endian swap on 64bit pointers, otherwise the pointer might map to zero
576 //0x0000000000000000000012345678 would become 0x12345678000000000000000000000000
577 if (do_endian_swap) {
578 SWITCH_LONGINT(bhead8->old);
581 /* this patch is to avoid a long long being read from not-eight aligned positions
582 is necessary on any modern 64bit architecture) */
583 memcpy(&old, &bhead8->old, 8);
584 bhead4->old = (int) (old >> 3);
586 bhead4->SDNAnr= bhead8->SDNAnr;
587 bhead4->nr= bhead8->nr;
591 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
593 BHead8 *bhead8 = (BHead8 *) bhead;
595 bhead8->code= bhead4->code;
596 bhead8->len= bhead4->len;
598 if (bhead8->code != ENDB) {
599 bhead8->old= bhead4->old;
600 bhead8->SDNAnr= bhead4->SDNAnr;
601 bhead8->nr= bhead4->nr;
605 static BHeadN *get_bhead(FileData *fd)
607 BHeadN *new_bhead = NULL;
612 /* initializing to zero isn't strictly needed but shuts valgrind up
613 * since uninitialized memory gets compared */
618 // First read the bhead structure.
619 // Depending on the platform the file was written on this can
620 // be a big or little endian BHead4 or BHead8 structure.
622 // As usual 'ENDB' (the last *partial* bhead of the file)
623 // needs some special handling. We don't want to EOF just yet.
625 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
627 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
629 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
630 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
631 switch_endian_bh4(&bhead4);
634 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
635 bh8_from_bh4(&bhead, &bhead4);
637 memcpy(&bhead, &bhead4, sizeof(bhead));
645 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
647 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
648 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
649 switch_endian_bh8(&bhead8);
652 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
653 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
655 memcpy(&bhead, &bhead8, sizeof(bhead));
663 /* make sure people are not trying to pass bad blend files */
664 if (bhead.len < 0) fd->eof = 1;
666 // bhead now contains the (converted) bhead structure. Now read
667 // the associated data and put everything in a BHeadN (creative naming !)
670 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
672 new_bhead->next = new_bhead->prev = NULL;
673 new_bhead->bhead = bhead;
675 readsize = fd->read(fd, new_bhead + 1, bhead.len);
677 if (readsize != bhead.len) {
679 MEM_freeN(new_bhead);
689 // We've read a new block. Now add it to the list
693 BLI_addtail(&fd->listbase, new_bhead);
699 BHead *blo_firstbhead(FileData *fd)
705 // Read in a new block if necessary
707 new_bhead = fd->listbase.first;
708 if (new_bhead == NULL) {
709 new_bhead = get_bhead(fd);
713 bhead = &new_bhead->bhead;
719 BHead *blo_prevbhead(FileData *UNUSED(fd), BHead *thisblock)
721 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
722 BHeadN *prev= bheadn->prev;
724 return prev?&prev->bhead:NULL;
727 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
729 BHeadN *new_bhead = NULL;
733 // bhead is actually a sub part of BHeadN
734 // We calculate the BHeadN pointer from the BHead pointer below
735 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
737 // get the next BHeadN. If it doesn't exist we read in the next one
738 new_bhead = new_bhead->next;
739 if (new_bhead == NULL) {
740 new_bhead = get_bhead(fd);
745 // here we do the reverse:
746 // go from the BHeadN pointer to the BHead pointer
747 bhead = &new_bhead->bhead;
753 static void decode_blender_header(FileData *fd)
755 char header[SIZEOFBLENDERHEADER], num[4];
758 // read in the header data
759 readsize = fd->read(fd, header, sizeof(header));
761 if (readsize == sizeof(header)) {
762 if(strncmp(header, "BLENDER", 7) == 0) {
763 int remove_this_endian_test= 1;
765 fd->flags |= FD_FLAGS_FILE_OK;
767 // what size are pointers in the file ?
769 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
770 if (sizeof(void *) != 4) {
771 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
774 if (sizeof(void *) != 8) {
775 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
779 // is the file saved in a different endian
781 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
782 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
785 // get the version number
787 memcpy(num, header+9, 3);
789 fd->fileversion = atoi(num);
794 static int read_file_dna(FileData *fd)
798 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
799 if (bhead->code==DNA1) {
800 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
802 fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
805 fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
806 /* used to retrieve ID names from (bhead+1) */
807 fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
811 } else if (bhead->code==ENDB)
818 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
820 int readsize = read(filedata->filedes, buffer, size);
825 filedata->seek += readsize;
831 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
833 int readsize = gzread(filedata->gzfiledes, buffer, size);
838 filedata->seek += readsize;
844 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
846 // don't read more bytes then there are available in the buffer
847 int readsize = (int)MIN2(size, (unsigned int)(filedata->buffersize - filedata->seek));
849 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
850 filedata->seek += readsize;
855 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
857 static unsigned int seek= 1<<30; /* the current position */
858 static unsigned int offset= 0; /* size of previous chunks */
859 static MemFileChunk *chunk=NULL;
860 unsigned int chunkoffset, readsize, totread;
862 if(size==0) return 0;
864 if(seek != (unsigned int)filedata->seek) {
865 chunk= filedata->memfile->chunks.first;
869 if(seek + chunk->size > (unsigned) filedata->seek) break;
874 seek= filedata->seek;
881 /* first check if it's on the end if current chunk */
882 if(seek-offset == chunk->size) {
883 offset+= chunk->size;
887 /* debug, should never happen */
889 printf("illegal read, chunk zero\n");
893 chunkoffset= seek-offset;
894 readsize= size-totread;
896 /* data can be spread over multiple chunks, so clamp size
897 * to within this chunk, and then it will read further in
899 if(chunkoffset+readsize > chunk->size)
900 readsize= chunk->size-chunkoffset;
902 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
904 filedata->seek += readsize;
906 } while(totread < size);
914 static FileData *filedata_new(void)
916 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
919 fd->gzfiledes = NULL;
921 /* XXX, this doesn't need to be done all the time,
922 * but it keeps us reentrant, remove once we have
923 * a lib that provides a nice lock. - zr
925 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
927 fd->datamap = oldnewmap_new();
928 fd->globmap = oldnewmap_new();
929 fd->libmap = oldnewmap_new();
934 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
936 decode_blender_header(fd);
938 if (fd->flags & FD_FLAGS_FILE_OK) {
939 if (!read_file_dna(fd)) {
940 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", incomplete", fd->relabase);
941 blo_freefiledata(fd);
946 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", not a blend file", fd->relabase);
947 blo_freefiledata(fd);
954 /* cannot be called with relative paths anymore! */
955 /* on each new library added, it now checks for the current FileData and expands relativeness */
956 FileData *blo_openblenderfile(const char *filepath, ReportList *reports)
960 gzfile= gzopen(filepath, "rb");
962 if (gzfile == (gzFile)Z_NULL) {
963 BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", filepath, errno ? strerror(errno) : "Unknown error reading file");
966 FileData *fd = filedata_new();
967 fd->gzfiledes = gzfile;
968 fd->read = fd_read_gzip_from_file;
970 /* needed for library_append and read_libraries */
971 BLI_strncpy(fd->relabase, filepath, sizeof(fd->relabase));
973 return blo_decode_and_check(fd, reports);
977 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
979 if (!mem || memsize<SIZEOFBLENDERHEADER) {
980 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
983 FileData *fd= filedata_new();
985 fd->buffersize= memsize;
986 fd->read= fd_read_from_memory;
987 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
989 return blo_decode_and_check(fd, reports);
993 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
996 BKE_report(reports, RPT_ERROR, "Unable to open blend <memory>");
999 FileData *fd= filedata_new();
1000 fd->memfile= memfile;
1002 fd->read= fd_read_from_memfile;
1003 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
1005 return blo_decode_and_check(fd, reports);
1010 void blo_freefiledata(FileData *fd)
1014 if (fd->filedes != -1) {
1018 if (fd->gzfiledes != NULL)
1020 gzclose(fd->gzfiledes);
1023 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1024 MEM_freeN(fd->buffer);
1028 // Free all BHeadN data blocks
1029 BLI_freelistN(&fd->listbase);
1032 DNA_sdna_free(fd->memsdna);
1034 DNA_sdna_free(fd->filesdna);
1036 MEM_freeN(fd->compflags);
1039 oldnewmap_free(fd->datamap);
1041 oldnewmap_free(fd->globmap);
1043 oldnewmap_free(fd->imamap);
1044 if (fd->movieclipmap)
1045 oldnewmap_free(fd->movieclipmap);
1046 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1047 oldnewmap_free(fd->libmap);
1049 MEM_freeN(fd->bheadmap);
1055 /* ************ DIV ****************** */
1057 int BLO_has_bfile_extension(const char *str)
1059 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend") || BLI_testextensie(str, ".blend.gz"));
1062 int BLO_is_a_library(const char *path, char *dir, char *group)
1064 /* return ok when a blenderfile, in dir is the filename,
1065 * in group the type of libdata
1073 if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
1078 /* Find the last slash */
1079 fd= BLI_last_slash(dir);
1081 if(fd==NULL) return 0;
1083 if(BLO_has_bfile_extension(fd+1)) {
1084 /* the last part of the dir is a .blend file, no group follows */
1085 *fd= '/'; /* put back the removed slash separating the dir and the .blend file name */
1088 char *gp = fd+1; // in case we have a .blend file, gp points to the group
1090 /* Find the last slash */
1091 fd= BLI_last_slash(dir);
1092 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
1094 /* now we know that we are in a blend file and it is safe to
1095 assume that gp actually points to a group */
1096 if (strcmp("Screen", gp)!=0)
1097 BLI_strncpy(group, gp, GROUP_MAX);
1102 /* ************** OLD POINTERS ******************* */
1104 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1106 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1109 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1111 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1114 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1116 if(fd->imamap && adr)
1117 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1121 static void *newmclipadr(FileData *fd, void *adr) /* used to restore movie clip data after undo */
1123 if(fd->movieclipmap && adr)
1124 return oldnewmap_lookup_and_inc(fd->movieclipmap, adr);
1129 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1131 return oldnewmap_liblookup(fd->libmap, adr, lib);
1134 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1136 ID *id= newlibadr(fd, lib, adr);
1144 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1148 for (i=0; i<fd->libmap->nentries; i++) {
1149 OldNew *entry= &fd->libmap->entries[i];
1151 if (old==entry->newp && entry->nr==ID_ID) {
1153 if(new) entry->nr= GS( ((ID *)new)->name );
1159 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1163 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1166 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1170 change_idid_adr_fd(fd, old, new);
1175 /* lib linked proxy objects point to our local data, we need
1176 * to clear that pointer before reading the undo memfile since
1177 * the object might be removed, it is set again in reading
1178 * if the local object still exists */
1179 void blo_clear_proxy_pointers_from_lib(Main *oldmain)
1181 Object *ob= oldmain->object.first;
1183 for(;ob; ob= ob->id.next)
1185 ob->proxy_from= NULL;
1188 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1190 Image *ima= oldmain->image.first;
1191 Scene *sce= oldmain->scene.first;
1194 fd->imamap= oldnewmap_new();
1196 for(;ima; ima= ima->id.next) {
1197 Link *ibuf= ima->ibufs.first;
1198 for(; ibuf; ibuf= ibuf->next)
1199 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1201 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1202 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
1204 oldnewmap_insert(fd->imamap, ima->renders[a], ima->renders[a], 0);
1206 for(; sce; sce= sce->id.next) {
1209 for(node= sce->nodetree->nodes.first; node; node= node->next)
1210 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1215 /* set old main image ibufs to zero if it has been restored */
1216 /* this works because freeing old main only happens after this call */
1217 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1219 OldNew *entry= fd->imamap->entries;
1220 Image *ima= oldmain->image.first;
1221 Scene *sce= oldmain->scene.first;
1224 /* used entries were restored, so we put them to zero */
1225 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1230 for(;ima; ima= ima->id.next) {
1233 /* this mirrors direct_link_image */
1234 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1236 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1237 BLI_remlink(&ima->ibufs, ibuf);
1239 ima->gputexture= NULL;
1242 for(i=0; i<IMA_MAX_RENDER_SLOT; i++)
1243 ima->renders[i]= newimaadr(fd, ima->renders[i]);
1245 ima->gputexture= newimaadr(fd, ima->gputexture);
1247 for(; sce; sce= sce->id.next) {
1250 for(node= sce->nodetree->nodes.first; node; node= node->next)
1251 node->preview= newimaadr(fd, node->preview);
1256 void blo_make_movieclip_pointer_map(FileData *fd, Main *oldmain)
1258 MovieClip *clip= oldmain->movieclip.first;
1259 Scene *sce= oldmain->scene.first;
1261 fd->movieclipmap= oldnewmap_new();
1263 for(;clip; clip= clip->id.next) {
1265 oldnewmap_insert(fd->movieclipmap, clip->cache, clip->cache, 0);
1267 if(clip->tracking.camera.intrinsics)
1268 oldnewmap_insert(fd->movieclipmap, clip->tracking.camera.intrinsics, clip->tracking.camera.intrinsics, 0);
1271 for(; sce; sce= sce->id.next) {
1274 for(node= sce->nodetree->nodes.first; node; node= node->next)
1275 if(node->type==CMP_NODE_MOVIEDISTORTION)
1276 oldnewmap_insert(fd->movieclipmap, node->storage, node->storage, 0);
1281 /* set old main movie clips caches to zero if it has been restored */
1282 /* this works because freeing old main only happens after this call */
1283 void blo_end_movieclip_pointer_map(FileData *fd, Main *oldmain)
1285 OldNew *entry= fd->movieclipmap->entries;
1286 MovieClip *clip= oldmain->movieclip.first;
1287 Scene *sce= oldmain->scene.first;
1290 /* used entries were restored, so we put them to zero */
1291 for (i=0; i<fd->movieclipmap->nentries; i++, entry++) {
1296 for(;clip; clip= clip->id.next) {
1297 clip->cache= newmclipadr(fd, clip->cache);
1298 clip->tracking.camera.intrinsics= newmclipadr(fd, clip->tracking.camera.intrinsics);
1301 for(; sce; sce= sce->id.next) {
1304 for(node= sce->nodetree->nodes.first; node; node= node->next)
1305 if(node->type==CMP_NODE_MOVIEDISTORTION)
1306 node->storage= newmclipadr(fd, node->storage);
1312 /* undo file support: add all library pointers in lookup */
1313 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1315 Main *ptr= mainlist->first;
1316 ListBase *lbarray[MAX_LIBARRAY];
1318 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1319 int i= set_listbasepointers(ptr, lbarray);
1322 for(id= lbarray[i]->first; id; id= id->next)
1323 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1329 /* ********** END OLD POINTERS ****************** */
1330 /* ********** READ FILE ****************** */
1332 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1334 int blocksize, nblocks;
1337 data= (char *)(bhead+1);
1338 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1342 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1348 static void *read_struct(FileData *fd, BHead *bh, const char *blockname)
1353 /* switch is based on file dna */
1354 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1355 switch_endian_structs(fd->filesdna, bh);
1357 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1358 if(fd->compflags[bh->SDNAnr]==2) {
1359 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1361 temp= MEM_mallocN(bh->len, blockname);
1362 memcpy(temp, (bh+1), bh->len);
1370 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1374 if(lb->first==NULL) return;
1376 lb->first= newdataadr(fd, lb->first);
1380 ln->next= newdataadr(fd, ln->next);
1388 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1393 if(lb->first==NULL) return;
1394 poin= newdataadr(fd, lb->first);
1396 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1403 poin= newdataadr(fd, ln->next);
1405 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1415 static void test_pointer_array(FileData *fd, void **mat)
1417 #if defined(WIN32) && !defined(FREE_WINDOWS)
1418 __int64 *lpoin, *lmat;
1420 long long *lpoin, *lmat;
1425 /* manually convert the pointer array in
1426 * the old dna format to a pointer array in
1427 * the new dna format.
1430 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1432 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1433 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1437 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1438 SWITCH_LONGINT(*lpoin);
1439 *ipoin= (int) ((*lpoin) >> 3);
1447 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1448 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1462 /* ************ READ ID Properties *************** */
1464 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1465 static void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1467 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1472 /*since we didn't save the extra buffer, set totallen to len.*/
1473 prop->totallen = prop->len;
1474 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1476 array= (IDProperty*) prop->data.pointer;
1478 /* note!, idp-arrays didn't exist in 2.4x, so the pointer will be cleared
1479 * theres not really anything we can do to correct this, at least dont crash */
1486 for(i=0; i<prop->len; i++)
1487 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
1490 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1495 /*since we didn't save the extra buffer, set totallen to len.*/
1496 prop->totallen = prop->len;
1497 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1499 if(prop->subtype == IDP_GROUP) {
1500 test_pointer_array(fd, prop->data.pointer);
1501 array= prop->data.pointer;
1503 for(i=0; i<prop->len; i++)
1504 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1506 else if(prop->subtype == IDP_DOUBLE) {
1507 if (switch_endian) {
1508 for (i=0; i<prop->len; i++) {
1509 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1514 if (switch_endian) {
1515 for (i=0; i<prop->len; i++) {
1516 SWITCH_INT(((int*)prop->data.pointer)[i]);
1522 static void IDP_DirectLinkString(IDProperty *prop, FileData *fd)
1524 /*since we didn't save the extra string buffer, set totallen to len.*/
1525 prop->totallen = prop->len;
1526 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1529 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1531 ListBase *lb = &prop->data.group;
1536 /*Link child id properties now*/
1537 for (loop=prop->data.group.first; loop; loop=loop->next) {
1538 IDP_DirectLinkProperty(loop, switch_endian, fd);
1542 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1544 switch (prop->type) {
1546 IDP_DirectLinkGroup(prop, switch_endian, fd);
1549 IDP_DirectLinkString(prop, fd);
1552 IDP_DirectLinkArray(prop, switch_endian, fd);
1555 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1558 /*erg, stupid doubles. since I'm storing them
1559 in the same field as int val; val2 in the
1560 IDPropertyData struct, they have to deal with
1561 endianness specifically
1563 in theory, val and val2 would've already been swapped
1564 if switch_endian is true, so we have to first unswap
1565 them then reswap them as a single 64-bit entity.
1568 if (switch_endian) {
1569 SWITCH_INT(prop->data.val);
1570 SWITCH_INT(prop->data.val2);
1571 SWITCH_LONGINT(prop->data.val);
1579 static void IDP_LibLinkProperty(IDProperty *UNUSED(prop), int UNUSED(switch_endian), FileData *UNUSED(fd))
1583 /* ************ READ CurveMapping *************** */
1585 /* cuma itself has been read! */
1586 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1590 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1591 cumap->flag &= ~CUMA_PREMULLED;
1593 for(a=0; a<CM_TOT; a++) {
1594 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1595 cumap->cm[a].table= NULL;
1596 cumap->cm[a].premultable= NULL;
1600 /* ************ READ Brush *************** */
1601 /* library brush linking after fileread */
1602 static void lib_link_brush(FileData *fd, Main *main)
1606 /* only link ID pointers */
1607 for(brush= main->brush.first; brush; brush= brush->id.next) {
1608 if(brush->id.flag & LIB_NEEDLINK) {
1609 brush->id.flag -= LIB_NEEDLINK;
1611 brush->mtex.tex= newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
1612 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1617 static void direct_link_brush(FileData *fd, Brush *brush)
1619 /* brush itself has been read */
1622 brush->curve= newdataadr(fd, brush->curve);
1624 direct_link_curvemapping(fd, brush->curve);
1626 brush_curve_preset(brush, CURVE_PRESET_SHARP);
1628 brush->preview= NULL;
1629 brush->icon_imbuf= NULL;
1632 static void direct_link_script(FileData *UNUSED(fd), Script *script)
1635 SCRIPT_SET_NULL(script)
1639 /* ************ READ PACKEDFILE *************** */
1641 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1643 PackedFile *pf= newdataadr(fd, oldpf);
1646 pf->data= newdataadr(fd, pf->data);
1652 /* ************ READ IMAGE PREVIEW *************** */
1654 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1656 PreviewImage *prv= newdataadr(fd, old_prv);
1660 for (i=0; i < NUM_ICON_SIZES; ++i) {
1662 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1670 /* ************ READ ANIMATION STUFF ***************** */
1672 /* Legacy Data Support (for Version Patching) ----------------------------- */
1674 // XXX depreceated - old animation system
1675 static void lib_link_ipo(FileData *fd, Main *main)
1679 ipo= main->ipo.first;
1681 if(ipo->id.flag & LIB_NEEDLINK) {
1683 for(icu= ipo->curve.first; icu; icu= icu->next) {
1685 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1687 ipo->id.flag -= LIB_NEEDLINK;
1693 // XXX depreceated - old animation system
1694 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1698 link_list(fd, &(ipo->curve));
1699 icu= ipo->curve.first;
1701 icu->bezt= newdataadr(fd, icu->bezt);
1702 icu->bp= newdataadr(fd, icu->bp);
1703 icu->driver= newdataadr(fd, icu->driver);
1708 // XXX depreceated - old animation system
1709 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1711 bActionStrip *strip;
1712 bActionModifier *amod;
1714 for (strip=striplist->first; strip; strip=strip->next){
1715 strip->object = newlibadr(fd, id->lib, strip->object);
1716 strip->act = newlibadr_us(fd, id->lib, strip->act);
1717 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1718 for(amod= strip->modifiers.first; amod; amod= amod->next)
1719 amod->ob= newlibadr(fd, id->lib, amod->ob);
1723 // XXX depreceated - old animation system
1724 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1726 bActionStrip *strip;
1728 link_list(fd, strips);
1730 for(strip= strips->first; strip; strip= strip->next)
1731 link_list(fd, &strip->modifiers);
1734 // XXX depreceated - old animation system
1735 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1737 bConstraintChannel *chan;
1739 for (chan=chanbase->first; chan; chan=chan->next){
1740 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1744 /* Data Linking ----------------------------- */
1746 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1750 for (fcm= list->first; fcm; fcm= fcm->next) {
1751 /* data for specific modifiers */
1752 switch (fcm->type) {
1753 case FMODIFIER_TYPE_PYTHON:
1755 FMod_Python *data= (FMod_Python *)fcm->data;
1756 data->script = newlibadr(fd, id->lib, data->script);
1763 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1770 /* relink ID-block references... */
1771 for (fcu= list->first; fcu; fcu= fcu->next) {
1774 ChannelDriver *driver= fcu->driver;
1777 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1778 DRIVER_TARGETS_LOOPER(dvar)
1780 /* only relink if still used */
1781 if (tarIndex < dvar->num_targets)
1782 dtar->id= newlibadr(fd, id->lib, dtar->id);
1786 DRIVER_TARGETS_LOOPER_END
1791 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1796 /* NOTE: this assumes that link_list has already been called on the list */
1797 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1801 for (fcm= list->first; fcm; fcm= fcm->next) {
1802 /* relink general data */
1803 fcm->data = newdataadr(fd, fcm->data);
1806 /* do relinking of data for specific types */
1807 switch (fcm->type) {
1808 case FMODIFIER_TYPE_GENERATOR:
1810 FMod_Generator *data= (FMod_Generator *)fcm->data;
1812 data->coefficients= newdataadr(fd, data->coefficients);
1814 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1816 for(a = 0; a < data->arraysize; a++)
1817 SWITCH_INT(data->coefficients[a]);
1821 case FMODIFIER_TYPE_ENVELOPE:
1823 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1825 data->data= newdataadr(fd, data->data);
1828 case FMODIFIER_TYPE_PYTHON:
1830 FMod_Python *data= (FMod_Python *)fcm->data;
1832 data->prop = newdataadr(fd, data->prop);
1833 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1840 /* NOTE: this assumes that link_list has already been called on the list */
1841 static void direct_link_fcurves(FileData *fd, ListBase *list)
1845 /* link F-Curve data to F-Curve again (non ID-libs) */
1846 for (fcu= list->first; fcu; fcu= fcu->next) {
1848 fcu->bezt= newdataadr(fd, fcu->bezt);
1849 fcu->fpt= newdataadr(fd, fcu->fpt);
1852 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1855 fcu->grp= newdataadr(fd, fcu->grp);
1858 fcu->driver= newdataadr(fd, fcu->driver);
1860 ChannelDriver *driver= fcu->driver;
1863 driver->expr_comp= NULL;
1865 /* relink variables, targets and their paths */
1866 link_list(fd, &driver->variables);
1867 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1868 DRIVER_TARGETS_LOOPER(dvar)
1870 /* only relink the targets being used */
1871 if (tarIndex < dvar->num_targets)
1872 dtar->rna_path= newdataadr(fd, dtar->rna_path);
1874 dtar->rna_path= NULL;
1876 DRIVER_TARGETS_LOOPER_END
1881 link_list(fd, &fcu->modifiers);
1882 direct_link_fmodifiers(fd, &fcu->modifiers);
1887 static void lib_link_action(FileData *fd, Main *main)
1890 bActionChannel *chan;
1892 for (act= main->action.first; act; act= act->id.next) {
1893 if (act->id.flag & LIB_NEEDLINK) {
1894 act->id.flag -= LIB_NEEDLINK;
1896 // XXX depreceated - old animation system <<<
1897 for (chan=act->chanbase.first; chan; chan=chan->next) {
1898 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1899 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1901 // >>> XXX depreceated - old animation system
1903 lib_link_fcurves(fd, &act->id, &act->curves);
1908 static void direct_link_action(FileData *fd, bAction *act)
1910 bActionChannel *achan; // XXX depreceated - old animation system
1913 link_list(fd, &act->curves);
1914 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1915 link_list(fd, &act->groups);
1916 link_list(fd, &act->markers);
1918 // XXX depreceated - old animation system <<<
1919 for (achan = act->chanbase.first; achan; achan=achan->next) {
1920 achan->grp= newdataadr(fd, achan->grp);
1922 link_list(fd, &achan->constraintChannels);
1924 // >>> XXX depreceated - old animation system
1926 direct_link_fcurves(fd, &act->curves);
1928 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1929 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1930 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1934 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
1938 for (strip= list->first; strip; strip= strip->next) {
1939 /* check strip's children */
1940 lib_link_nladata_strips(fd, id, &strip->strips);
1942 /* check strip's F-Curves */
1943 lib_link_fcurves(fd, id, &strip->fcurves);
1945 /* reassign the counted-reference to action */
1946 strip->act = newlibadr_us(fd, id->lib, strip->act);
1948 /* fix action id-root (i.e. if it comes from a pre 2.57 .blend file) */
1949 if ((strip->act) && (strip->act->idroot == 0))
1950 strip->act->idroot = GS(id->name);
1954 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
1958 /* we only care about the NLA strips inside the tracks */
1959 for (nlt= list->first; nlt; nlt= nlt->next) {
1960 lib_link_nladata_strips(fd, id, &nlt->strips);
1964 /* This handles Animato NLA-Strips linking
1965 * NOTE: this assumes that link_list has already been called on the list
1967 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
1971 for (strip= list->first; strip; strip= strip->next) {
1972 /* strip's child strips */
1973 link_list(fd, &strip->strips);
1974 direct_link_nladata_strips(fd, &strip->strips);
1976 /* strip's F-Curves */
1977 link_list(fd, &strip->fcurves);
1978 direct_link_fcurves(fd, &strip->fcurves);
1980 /* strip's F-Modifiers */
1981 link_list(fd, &strip->modifiers);
1982 direct_link_modifiers(fd, &strip->modifiers);
1986 /* NOTE: this assumes that link_list has already been called on the list */
1987 static void direct_link_nladata(FileData *fd, ListBase *list)
1991 for (nlt= list->first; nlt; nlt= nlt->next) {
1992 /* relink list of strips */
1993 link_list(fd, &nlt->strips);
1995 /* relink strip data */
1996 direct_link_nladata_strips(fd, &nlt->strips);
2002 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
2007 /* here, we're only interested in the ID pointer stored in some of the paths */
2008 for (ks= list->first; ks; ks= ks->next) {
2009 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
2010 ksp->id= newlibadr(fd, id->lib, ksp->id);
2015 /* NOTE: this assumes that link_list has already been called on the list */
2016 static void direct_link_keyingsets(FileData *fd, ListBase *list)
2021 /* link KeyingSet data to KeyingSet again (non ID-libs) */
2022 for (ks= list->first; ks; ks= ks->next) {
2024 link_list(fd, &ks->paths);
2026 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
2028 ksp->rna_path= newdataadr(fd, ksp->rna_path);
2035 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
2040 /* link action data */
2041 adt->action= newlibadr_us(fd, id->lib, adt->action);
2042 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
2044 /* fix action id-roots (i.e. if they come from a pre 2.57 .blend file) */
2045 if ((adt->action) && (adt->action->idroot == 0))
2046 adt->action->idroot = GS(id->name);
2047 if ((adt->tmpact) && (adt->tmpact->idroot == 0))
2048 adt->tmpact->idroot = GS(id->name);
2051 lib_link_fcurves(fd, id, &adt->drivers);
2053 /* overrides don't have lib-link for now, so no need to do anything */
2056 lib_link_nladata(fd, id, &adt->nla_tracks);
2059 static void direct_link_animdata(FileData *fd, AnimData *adt)
2061 /* NOTE: must have called newdataadr already before doing this... */
2066 link_list(fd, &adt->drivers);
2067 direct_link_fcurves(fd, &adt->drivers);
2069 /* link overrides */
2073 link_list(fd, &adt->nla_tracks);
2074 direct_link_nladata(fd, &adt->nla_tracks);
2076 /* relink active strip - even though strictly speaking this should only be used
2077 * if we're in 'tweaking mode', we need to be able to have this loaded back for
2078 * undo, but also since users may not exit tweakmode before saving (#24535)
2080 // TODO: it's not really nice that anyone should be able to save the file in this
2081 // state, but it's going to be too hard to enforce this single case...
2082 adt->actstrip= newdataadr(fd, adt->actstrip);
2085 /* ************ READ MOTION PATHS *************** */
2087 /* direct data for cache */
2088 static void direct_link_motionpath(FileData *fd, bMotionPath *mpath)
2094 /* relink points cache */
2095 mpath->points= newdataadr(fd, mpath->points);
2098 /* ************ READ NODE TREE *************** */
2100 /* singe node tree (also used for material/scene trees), ntree is not NULL */
2101 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
2105 if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
2107 ntree->gpd= newlibadr_us(fd, id->lib, ntree->gpd);
2109 for(node= ntree->nodes.first; node; node= node->next)
2110 node->id= newlibadr_us(fd, id->lib, node->id);
2113 /* library ntree linking after fileread */
2114 static void lib_link_nodetree(FileData *fd, Main *main)
2118 /* only link ID pointers */
2119 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2120 if(ntree->id.flag & LIB_NEEDLINK) {
2121 ntree->id.flag -= LIB_NEEDLINK;
2122 lib_link_ntree(fd, &ntree->id, ntree);
2127 static void lib_nodetree_init_types_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2131 ntreeInitTypes(ntree);
2133 /* XXX could be replaced by do_versions for new nodes */
2134 for (node=ntree->nodes.first; node; node=node->next)
2135 node_verify_socket_templates(ntree, node);
2138 /* updates group node socket own_index so that
2139 * external links to/from the group node are preserved.
2141 static void lib_node_do_versions_group_indices(bNode *gnode)
2143 bNodeTree *ngroup= (bNodeTree*)gnode->id;
2145 bNodeSocket *sock, *gsock, *intsock;
2148 for (sock=gnode->outputs.first; sock; sock=sock->next) {
2149 int old_index = sock->to_index;
2150 for (gsock=ngroup->outputs.first; gsock; gsock=gsock->next) {
2151 if (gsock->link && gsock->link->fromsock->own_index == old_index) {
2152 sock->own_index = gsock->own_index;
2157 for (sock=gnode->inputs.first; sock; sock=sock->next) {
2158 int old_index = sock->to_index;
2159 /* can't use break in double loop */
2161 for (intnode=ngroup->nodes.first; intnode && !found; intnode=intnode->next) {
2162 for (intsock=intnode->inputs.first; intsock; intsock=intsock->next) {
2163 if (intsock->own_index == old_index && intsock->link) {
2164 sock->own_index = intsock->link->fromsock->own_index;
2173 /* updates external links for all group nodes in a tree */
2174 static void lib_nodetree_do_versions_group_indices_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2178 for (node=ntree->nodes.first; node; node=node->next) {
2179 if (node->type==NODE_GROUP) {
2180 bNodeTree *ngroup= (bNodeTree*)node->id;
2181 if (ngroup && (ngroup->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE))
2182 lib_node_do_versions_group_indices(node);
2187 /* make an update call for the tree */
2188 static void lib_nodetree_do_versions_update_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2191 ntreeUpdateTree(ntree);
2194 /* verify types for nodes and groups, all data has to be read */
2195 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
2197 static void lib_verify_nodetree(Main *main, int UNUSED(open))
2201 bNodeTreeType *ntreetype;
2203 /* this crashes blender on undo/redo
2205 reinit_nodesystem();
2208 /* set node->typeinfo pointers */
2209 for (i=0; i < NUM_NTREE_TYPES; ++i) {
2210 ntreetype= ntreeGetType(i);
2211 if (ntreetype && ntreetype->foreach_nodetree)
2212 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_init_types_cb);
2214 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
2215 lib_nodetree_init_types_cb(NULL, NULL, ntree);
2218 int has_old_groups=0;
2219 /* XXX this should actually be part of do_versions, but since we need
2220 * finished library linking, it is not possible there. Instead in do_versions
2221 * we have set the NTREE_DO_VERSIONS flag, so at this point we can do the
2222 * actual group node updates.
2224 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2225 if (ntree->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE) {
2226 /* this adds copies and links from all unlinked internal sockets to group inputs/outputs. */
2227 node_group_expose_all_sockets(ntree);
2232 if (has_old_groups) {
2233 for (i=0; i < NUM_NTREE_TYPES; ++i) {
2234 ntreetype= ntreeGetType(i);
2235 if (ntreetype && ntreetype->foreach_nodetree)
2236 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_group_indices_cb);
2240 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
2241 ntree->flag &= ~NTREE_DO_VERSIONS_GROUP_EXPOSE;
2244 /* verify all group user nodes */
2245 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2246 ntreeVerifyNodes(main, &ntree->id);
2249 /* make update calls where necessary */
2251 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next)
2253 ntreeUpdateTree(ntree);
2255 for (i=0; i < NUM_NTREE_TYPES; ++i) {
2256 ntreetype= ntreeGetType(i);
2257 if (ntreetype && ntreetype->foreach_nodetree)
2258 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_update_cb);
2263 static void direct_link_node_socket(FileData *fd, bNodeSocket *sock)
2265 sock->link= newdataadr(fd, sock->link);
2266 sock->storage= newdataadr(fd, sock->storage);
2267 sock->default_value= newdataadr(fd, sock->default_value);
2271 /* ntree itself has been read! */
2272 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2274 /* note: writing and reading goes in sync, for speed */
2279 ntree->init= 0; /* to set callbacks and force setting types */
2280 ntree->progress= NULL;
2281 ntree->execdata= NULL;
2283 ntree->adt= newdataadr(fd, ntree->adt);
2284 direct_link_animdata(fd, ntree->adt);
2286 link_list(fd, &ntree->nodes);
2287 for(node= ntree->nodes.first; node; node= node->next) {
2288 if(node->type == NODE_DYNAMIC) {
2290 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
2293 node->typeinfo= NULL;
2295 link_list(fd, &node->inputs);
2296 link_list(fd, &node->outputs);
2298 if(node->type == CMP_NODE_MOVIEDISTORTION) {
2299 node->storage= newmclipadr(fd, node->storage);
2301 node->storage= newdataadr(fd, node->storage);
2304 /* could be handlerized at some point */
2305 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
2306 direct_link_curvemapping(fd, node->storage);
2307 else if(ntree->type==NTREE_COMPOSIT) {
2308 if( ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
2309 direct_link_curvemapping(fd, node->storage);
2310 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2311 ((ImageUser *)node->storage)->ok= 1;
2313 else if( ntree->type==NTREE_TEXTURE) {
2314 if(node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME)
2315 direct_link_curvemapping(fd, node->storage);
2316 else if(node->type==TEX_NODE_IMAGE)
2317 ((ImageUser *)node->storage)->ok= 1;
2321 link_list(fd, &ntree->links);
2323 /* external sockets */
2324 link_list(fd, &ntree->inputs);
2325 link_list(fd, &ntree->outputs);
2327 /* and we connect the rest */
2328 for(node= ntree->nodes.first; node; node= node->next) {
2329 node->parent = newdataadr(fd, node->parent);
2330 node->preview= newimaadr(fd, node->preview);
2333 for(sock= node->inputs.first; sock; sock= sock->next)
2334 direct_link_node_socket(fd, sock);
2335 for(sock= node->outputs.first; sock; sock= sock->next)
2336 direct_link_node_socket(fd, sock);
2338 for(sock= ntree->inputs.first; sock; sock= sock->next)
2339 direct_link_node_socket(fd, sock);
2340 for(sock= ntree->outputs.first; sock; sock= sock->next)
2341 direct_link_node_socket(fd, sock);
2343 for(link= ntree->links.first; link; link= link->next) {
2344 link->fromnode= newdataadr(fd, link->fromnode);
2345 link->tonode= newdataadr(fd, link->tonode);
2346 link->fromsock= newdataadr(fd, link->fromsock);
2347 link->tosock= newdataadr(fd, link->tosock);
2350 /* type verification is in lib-link */
2353 /* ************ READ ARMATURE ***************** */
2355 /* temp struct used to transport needed info to lib_link_constraint_cb() */
2356 typedef struct tConstraintLinkData {
2359 } tConstraintLinkData;
2360 /* callback function used to relink constraint ID-links */
2361 static void lib_link_constraint_cb(bConstraint *UNUSED(con), ID **idpoin, void *userdata)
2363 tConstraintLinkData *cld= (tConstraintLinkData *)userdata;
2364 *idpoin = newlibadr(cld->fd, cld->id->lib, *idpoin);
2367 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2369 tConstraintLinkData cld;
2373 for (con = conlist->first; con; con=con->next) {
2374 /* patch for error introduced by changing constraints (dunno how) */
2375 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2376 if(con->data==NULL) {
2377 con->type= CONSTRAINT_TYPE_NULL;
2379 /* own ipo, all constraints have it */
2380 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2383 /* relink all ID-blocks used by the constraints */
2387 id_loop_constraints(conlist, lib_link_constraint_cb, &cld);
2390 static void direct_link_constraints(FileData *fd, ListBase *lb)
2395 for (con=lb->first; con; con=con->next) {
2396 con->data = newdataadr(fd, con->data);
2398 switch (con->type) {
2399 case CONSTRAINT_TYPE_PYTHON:
2401 bPythonConstraint *data= con->data;
2403 link_list(fd, &data->targets);
2405 data->prop = newdataadr(fd, data->prop);
2407 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2410 case CONSTRAINT_TYPE_SPLINEIK:
2412 bSplineIKConstraint *data= con->data;
2414 data->points= newdataadr(fd, data->points);
2417 case CONSTRAINT_TYPE_KINEMATIC:
2419 con->lin_error = 0.f;
2420 con->rot_error = 0.f;
2422 case CONSTRAINT_TYPE_CHILDOF:
2424 /* XXX version patch, in older code this flag wasn't always set, and is inherent to type */
2425 if(con->ownspace == CONSTRAINT_SPACE_POSE)
2426 con->flag |= CONSTRAINT_SPACEONCE;
2433 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2435 bPoseChannel *pchan;
2436 bArmature *arm= ob->data;
2443 /* always rebuild to match proxy or lib changes */
2444 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2447 /* sync proxy layer */
2448 if(pose->proxy_layer)
2449 arm->layer = pose->proxy_layer;
2451 /* sync proxy active bone */
2452 if(pose->proxy_act_bone[0]) {
2453 Bone *bone = get_named_bone(arm, pose->proxy_act_bone);
2455 arm->act_bone = bone;
2459 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2460 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2462 /* hurms... loop in a loop, but yah... later... (ton) */
2463 pchan->bone= get_named_bone(arm, pchan->name);
2465 pchan->custom= newlibadr_us(fd, arm->id.lib, pchan->custom);
2466 if(pchan->bone==NULL)
2468 else if(ob->id.lib==NULL && arm->id.lib) {
2469 /* local pose selection copied to armature, bit hackish */
2470 pchan->bone->flag &= ~BONE_SELECTED;
2471 pchan->bone->flag |= pchan->selectflag;
2476 ob->recalc= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
2477 pose->flag |= POSE_RECALC;
2481 static void lib_link_armature(FileData *fd, Main *main)
2485 arm= main->armature.first;
2488 if(arm->id.flag & LIB_NEEDLINK) {
2489 if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
2490 arm->id.flag -= LIB_NEEDLINK;
2496 static void direct_link_bones(FileData *fd, Bone* bone)
2500 bone->parent= newdataadr(fd, bone->parent);
2501 bone->prop= newdataadr(fd, bone->prop);
2503 IDP_DirectLinkProperty(bone->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2505 bone->flag &= ~BONE_DRAW_ACTIVE;
2507 link_list(fd, &bone->childbase);
2509 for(child=bone->childbase.first; child; child=child->next)
2510 direct_link_bones(fd, child);
2513 static void direct_link_armature(FileData *fd, bArmature *arm)
2517 link_list(fd, &arm->bonebase);
2521 arm->adt= newdataadr(fd, arm->adt);
2522 direct_link_animdata(fd, arm->adt);
2524 bone=arm->bonebase.first;
2526 direct_link_bones(fd, bone);
2530 arm->act_bone= newdataadr(fd, arm->act_bone);
2531 arm->act_edbone= NULL;
2534 /* ************ READ CAMERA ***************** */
2536 static void lib_link_camera(FileData *fd, Main *main)
2540 ca= main->camera.first;
2542 if(ca->id.flag & LIB_NEEDLINK) {
2543 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2545 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2547 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2549 ca->id.flag -= LIB_NEEDLINK;
2555 static void direct_link_camera(FileData *fd, Camera *ca)
2557 ca->adt= newdataadr(fd, ca->adt);
2558 direct_link_animdata(fd, ca->adt);
2562 /* ************ READ LAMP ***************** */
2564 static void lib_link_lamp(FileData *fd, Main *main)
2570 la= main->lamp.first;
2572 if(la->id.flag & LIB_NEEDLINK) {
2573 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2575 for(a=0; a<MAX_MTEX; a++) {
2578 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2579 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2583 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2586 lib_link_ntree(fd, &la->id, la->nodetree);
2588 la->id.flag -= LIB_NEEDLINK;
2594 static void direct_link_lamp(FileData *fd, Lamp *la)
2598 la->adt= newdataadr(fd, la->adt);
2599 direct_link_animdata(fd, la->adt);
2601 for(a=0; a<MAX_MTEX; a++) {
2602 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2605 la->curfalloff= newdataadr(fd, la->curfalloff);
2607 direct_link_curvemapping(fd, la->curfalloff);
2609 la->nodetree= newdataadr(fd, la->nodetree);
2611 direct_link_nodetree(fd, la->nodetree);
2613 la->preview = direct_link_preview_image(fd, la->preview);
2616 /* ************ READ keys ***************** */
2618 static void lib_link_key(FileData *fd, Main *main)
2622 key= main->key.first;
2624 /*check if we need to generate unique ids for the shapekeys*/
2629 for (block=key->block.first; block; block=block->next) {
2630 block->uid = key->uidgen++;
2634 if(key->id.flag & LIB_NEEDLINK) {
2635 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2637 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2638 key->from= newlibadr(fd, key->id.lib, key->from);
2640 key->id.flag -= LIB_NEEDLINK;
2646 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2649 char *data, *poin, *cp;
2651 elemsize= key->elemsize;
2654 for(a=0; a<kb->totelem; a++) {
2659 while( cp[0] ) { /* cp[0]==amount */
2661 switch(cp[1]) { /* cp[1]= type */
2667 SWITCH_INT((*poin));
2680 static void direct_link_key(FileData *fd, Key *key)
2684 link_list(fd, &(key->block));
2686 key->adt= newdataadr(fd, key->adt);
2687 direct_link_animdata(fd, key->adt);
2689 key->refkey= newdataadr(fd, key->refkey);
2691 kb= key->block.first;
2694 kb->data= newdataadr(fd, kb->data);
2696 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2697 switch_endian_keyblock(key, kb);
2703 /* ************ READ mball ***************** */
2705 static void lib_link_mball(FileData *fd, Main *main)
2710 mb= main->mball.first;
2712 if(mb->id.flag & LIB_NEEDLINK) {
2713 if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
2715 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2717 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2719 mb->id.flag -= LIB_NEEDLINK;
2725 static void direct_link_mball(FileData *fd, MetaBall *mb)
2727 mb->adt= newdataadr(fd, mb->adt);
2728 direct_link_animdata(fd, mb->adt);
2730 mb->mat= newdataadr(fd, mb->mat);
2731 test_pointer_array(fd, (void **)&mb->mat);
2733 link_list(fd, &(mb->elems));
2735 mb->disp.first= mb->disp.last= NULL;
2736 mb->editelems= NULL;
2738 /* mb->edit_elems.first= mb->edit_elems.last= NULL;*/
2742 /* ************ READ WORLD ***************** */
2744 static void lib_link_world(FileData *fd, Main *main)
2750 wrld= main->world.first;
2752 if(wrld->id.flag & LIB_NEEDLINK) {
2753 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2755 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2757 for(a=0; a<MAX_MTEX; a++) {
2758 mtex= wrld->mtex[a];
2760 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2761 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2766 lib_link_ntree(fd, &wrld->id, wrld->nodetree);
2768 wrld->id.flag -= LIB_NEEDLINK;
2770 wrld= wrld->id.next;
2774 static void direct_link_world(FileData *fd, World *wrld)
2778 wrld->adt= newdataadr(fd, wrld->adt);
2779 direct_link_animdata(fd, wrld->adt);
2781 for(a=0; a<MAX_MTEX; a++) {
2782 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2785 wrld->nodetree= newdataadr(fd, wrld->nodetree);
2787 direct_link_nodetree(fd, wrld->nodetree);
2789 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2793 /* ************ READ VFONT ***************** */
2795 static void lib_link_vfont(FileData *UNUSED(fd), Main *main)
2799 vf= main->vfont.first;
2801 if(vf->id.flag & LIB_NEEDLINK) {
2802 vf->id.flag -= LIB_NEEDLINK;
2808 static void direct_link_vfont(FileData *fd, VFont *vf)
2811 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2814 /* ************ READ TEXT ****************** */
2816 static void lib_link_text(FileData *UNUSED(fd), Main *main)
2820 text= main->text.first;
2822 if(text->id.flag & LIB_NEEDLINK) {
2823 text->id.flag -= LIB_NEEDLINK;
2825 text= text->id.next;
2829 static void direct_link_text(FileData *fd, Text *text)
2833 text->name= newdataadr(fd, text->name);
2836 text->undo_len= TXT_INIT_UNDO;
2837 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2839 text->compiled= NULL;
2842 if(text->flags & TXT_ISEXT) {
2847 link_list(fd, &text->lines);
2848 link_list(fd, &text->markers);
2850 text->curl= newdataadr(fd, text->curl);
2851 text->sell= newdataadr(fd, text->sell);
2853 ln= text->lines.first;
2855 ln->line= newdataadr(fd, ln->line);
2858 if (ln->len != (int) strlen(ln->line)) {
2859 printf("Error loading text, line lengths differ\n");
2860 ln->len = strlen(ln->line);
2866 text->flags = (text->flags) & ~TXT_ISEXT;
2871 /* ************ READ IMAGE ***************** */
2873 static void lib_link_image(FileData *fd, Main *main)
2877 ima= main->image.first;
2879 if(ima->id.flag & LIB_NEEDLINK) {
2880 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2882 ima->id.flag -= LIB_NEEDLINK;
2888 static void link_ibuf_list(FileData *fd, ListBase *lb)
2892 if(lb->first==NULL) return;
2894 lb->first= newimaadr(fd, lb->first);
2898 ln->next= newimaadr(fd, ln->next);
2906 static void direct_link_image(FileData *fd, Image *ima)
2908 /* for undo system, pointers could be restored */
2910 link_ibuf_list(fd, &ima->ibufs);
2912 ima->ibufs.first= ima->ibufs.last= NULL;
2914 /* if not restored, we keep the binded opengl index */
2915 if(ima->ibufs.first==NULL) {
2917 ima->gputexture= NULL;
2924 /* undo system, try to restore render buffers */
2928 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
2929 ima->renders[a]= newimaadr(fd, ima->renders[a]);
2932 memset(ima->renders, 0, sizeof(ima->renders));
2933 ima->last_render_slot= ima->render_slot;
2936 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2937 ima->preview = direct_link_preview_image(fd, ima->preview);
2942 /* ************ READ CURVE ***************** */
2944 static void lib_link_curve(FileData *fd, Main *main)
2949 cu= main->curve.first;
2951 if(cu->id.flag & LIB_NEEDLINK) {
2952 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2954 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2956 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2957 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2958 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2959 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2960 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2961 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2962 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2964 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2965 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2967 cu->id.flag -= LIB_NEEDLINK;
2974 static void switch_endian_knots(Nurb *nu)
2981 SWITCH_INT(nu->knotsu[len]);
2987 SWITCH_INT(nu->knotsv[len]);
2992 static void direct_link_curve(FileData *fd, Curve *cu)
2997 cu->adt= newdataadr(fd, cu->adt);
2998 direct_link_animdata(fd, cu->adt);
3000 cu->mat= newdataadr(fd, cu->mat);
3001 test_pointer_array(fd, (void **)&cu->mat);
3002 cu->str= newdataadr(fd, cu->str);
3003 cu->strinfo= newdataadr(fd, cu->strinfo);
3004 cu->tb= newdataadr(fd, cu->tb);
3006 if(cu->vfont == NULL) link_list(fd, &(cu->nurb));
3008 cu->nurb.first=cu->nurb.last= NULL;
3010 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
3012 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
3019 cu->tb[0].w = cu->linewidth;
3021 if (cu->wordspace == 0.0f) cu->wordspace = 1.0f;
3024 cu->bev.first=cu->bev.last= NULL;
3025 cu->disp.first=cu->disp.last= NULL;
3033 nu->bezt= newdataadr(fd, nu->bezt);
3034 nu->bp= newdataadr(fd, nu->bp);
3035 nu->knotsu= newdataadr(fd, nu->knotsu);
3036 nu->knotsv= newdataadr(fd, nu->knotsv);
3037 if (cu->vfont == NULL) nu->charidx= nu->mat_nr;
3039 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
3040 switch_endian_knots(nu);
3048 /* ************ READ TEX ***************** */
3050 static void lib_link_texture(FileData *fd, Main *main)
3054 tex= main->tex.first;
3056 if(tex->id.flag & LIB_NEEDLINK) {
3057 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
3059 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
3060 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
3061 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
3063 tex->pd->object= newlibadr(fd, tex->id.lib, tex->pd->object);
3064 if(tex->vd) tex->vd->object= newlibadr(fd, tex->id.lib, tex->vd->object);
3067 lib_link_ntree(fd, &tex->id, tex->nodetree);
3069 tex->id.flag -= LIB_NEEDLINK;
3075 static void direct_link_texture(FileData *fd, Tex *tex)
3077 tex->adt= newdataadr(fd, tex->adt);
3078 direct_link_animdata(fd, tex->adt);
3080 tex->plugin= newdataadr(fd, tex->plugin);
3082 tex->plugin->handle= NULL;
3083 open_plugin_tex(tex->plugin);
3084 /* initialize data for this instance, if an initialization
3087 if (tex->plugin->instance_init)
3088 tex->plugin->instance_init((void *) tex->plugin->data);
3090 tex->coba= newdataadr(fd, tex->coba);
3091 tex->env= newdataadr(fd, tex->env);
3093 tex->env->ima= NULL;
3094 memset(tex->env->cube, 0, 6*sizeof(void *));
3097 tex->pd= newdataadr(fd, tex->pd);
3099 tex->pd->point_tree = NULL;
3100 tex->pd->coba= newdataadr(fd, tex->pd->coba);
3101 tex->pd->falloff_curve= newdataadr(fd, tex->pd->falloff_curve);
3102 if(tex->pd->falloff_curve) {
3103 direct_link_curvemapping(fd, tex->pd->falloff_curve);
3107 tex->vd= newdataadr(fd, tex->vd);
3109 tex->vd->dataset = NULL;
3112 if(tex->type == TEX_VOXELDATA)
3113 tex->vd= MEM_callocN(sizeof(VoxelData), "direct_link_texture VoxelData");
3116 tex->nodetree= newdataadr(fd, tex->nodetree);
3118 direct_link_nodetree(fd, tex->nodetree);
3120 tex->preview = direct_link_preview_image(fd, tex->preview);
3127 /* ************ READ MATERIAL ***************** */
3129 static void lib_link_material(FileData *fd, Main *main)
3135 ma= main->mat.first;
3137 if(ma->id.flag & LIB_NEEDLINK) {
3138 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
3140 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3141 of library blocks that implement this.*/
3142 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3144 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
3145 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
3147 for(a=0; a<MAX_MTEX; a++) {
3150 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
3151 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
3156 lib_link_ntree(fd, &ma->id, ma->nodetree);
3158 ma->id.flag -= LIB_NEEDLINK;
3164 static void direct_link_material(FileData *fd, Material *ma)
3168 ma->adt= newdataadr(fd, ma->adt);
3169 direct_link_animdata(fd, ma->adt);
3171 for(a=0; a<MAX_MTEX; a++) {
3172 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
3175 ma->ramp_col= newdataadr(fd, ma->ramp_col);
3176 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
3178 ma->nodetree= newdataadr(fd, ma->nodetree);
3180 direct_link_nodetree(fd, ma->nodetree);
3182 ma->preview = direct_link_preview_image(fd, ma->preview);
3183 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
3186 /* ************ READ PARTICLE SETTINGS ***************** */
3187 /* update this also to writefile.c */
3188 static const char *ptcache_data_struct[] = {
3189 "", // BPHYS_DATA_INDEX
3190 "", // BPHYS_DATA_LOCATION
3191 "", // BPHYS_DATA_VELOCITY
3192 "", // BPHYS_DATA_ROTATION
3193 "", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
3194 "", // BPHYS_DATA_SIZE:
3195 "", // BPHYS_DATA_TIMES:
3196 "BoidData" // case BPHYS_DATA_BOIDS:
3198 static void direct_link_pointcache(FileData *fd, PointCache *cache)
3200 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
3202 PTCacheExtra *extra;
3205 link_list(fd, &cache->mem_cache);
3207 pm = cache->mem_cache.first;
3209 for(; pm; pm=pm->next) {
3210 for(i=0; i<BPHYS_TOT_DATA; i++) {
3211 pm->data[i] = newdataadr(fd, pm->data[i]);
3213 /* the cache saves non-struct data without DNA */
3214 if(pm->data[i] && ptcache_data_struct[i][0]=='\0' && (fd->flags & FD_FLAGS_SWITCH_ENDIAN)) {