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
42 #include <stdarg.h> /* for va_start/end */
45 # include <unistd.h> // for read close
47 # include <io.h> // for open close read
48 # include "winsock2.h"
49 # include "BLI_winstuff.h"
52 /* allow readfile to use deprecated functionality */
53 #define DNA_DEPRECATED_ALLOW
55 #include "DNA_anim_types.h"
56 #include "DNA_armature_types.h"
57 #include "DNA_actuator_types.h"
58 #include "DNA_brush_types.h"
59 #include "DNA_camera_types.h"
60 #include "DNA_cloth_types.h"
61 #include "DNA_controller_types.h"
62 #include "DNA_constraint_types.h"
63 #include "DNA_dynamicpaint_types.h"
64 #include "DNA_effect_types.h"
65 #include "DNA_fileglobal_types.h"
66 #include "DNA_genfile.h"
67 #include "DNA_group_types.h"
68 #include "DNA_gpencil_types.h"
69 #include "DNA_ipo_types.h"
70 #include "DNA_key_types.h"
71 #include "DNA_lattice_types.h"
72 #include "DNA_lamp_types.h"
73 #include "DNA_meta_types.h"
74 #include "DNA_material_types.h"
75 #include "DNA_mesh_types.h"
76 #include "DNA_meshdata_types.h"
77 #include "DNA_nla_types.h"
78 #include "DNA_node_types.h"
79 #include "DNA_object_fluidsim.h" // NT
80 #include "DNA_object_types.h"
81 #include "DNA_packedFile_types.h"
82 #include "DNA_particle_types.h"
83 #include "DNA_property_types.h"
84 #include "DNA_text_types.h"
85 #include "DNA_view3d_types.h"
86 #include "DNA_screen_types.h"
87 #include "DNA_sensor_types.h"
88 #include "DNA_sdna_types.h"
89 #include "DNA_scene_types.h"
90 #include "DNA_sequence_types.h"
91 #include "DNA_smoke_types.h"
92 #include "DNA_speaker_types.h"
93 #include "DNA_sound_types.h"
94 #include "DNA_space_types.h"
95 #include "DNA_vfont_types.h"
96 #include "DNA_world_types.h"
97 #include "DNA_movieclip_types.h"
98 #include "DNA_mask_types.h"
100 #include "MEM_guardedalloc.h"
102 #include "BLI_utildefines.h"
103 #include "BLI_blenlib.h"
104 #include "BLI_math.h"
105 #include "BLI_edgehash.h"
107 #include "BKE_anim.h"
108 #include "BKE_action.h"
109 #include "BKE_armature.h"
110 #include "BKE_brush.h"
111 #include "BKE_colortools.h"
112 #include "BKE_constraint.h"
113 #include "BKE_context.h"
114 #include "BKE_curve.h"
115 #include "BKE_deform.h"
116 #include "BKE_effect.h"
117 #include "BKE_fcurve.h"
118 #include "BKE_global.h" // for G
119 #include "BKE_group.h"
120 #include "BKE_image.h"
121 #include "BKE_lattice.h"
122 #include "BKE_library.h" // for which_libbase
123 #include "BKE_idcode.h"
124 #include "BKE_material.h"
125 #include "BKE_main.h" // for Main
126 #include "BKE_mesh.h" // for ME_ defines (patching)
127 #include "BKE_modifier.h"
128 #include "BKE_multires.h"
129 #include "BKE_node.h" // for tree type defines
130 #include "BKE_ocean.h"
131 #include "BKE_object.h"
132 #include "BKE_paint.h"
133 #include "BKE_particle.h"
134 #include "BKE_pointcache.h"
135 #include "BKE_property.h" // for get_ob_property
136 #include "BKE_report.h"
137 #include "BKE_sca.h" // for init_actuator
138 #include "BKE_scene.h"
139 #include "BKE_screen.h"
140 #include "BKE_sequencer.h"
141 #include "BKE_text.h" // for txt_extended_ascii_as_utf8
142 #include "BKE_tracking.h"
143 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
144 #include "BKE_sound.h"
146 #include "IMB_imbuf.h" // for proxy / timecode versioning stuff
148 #include "NOD_socket.h"
150 #include "BLO_readfile.h"
151 #include "BLO_undofile.h"
153 #include "RE_engine.h"
155 #include "readfile.h"
157 #include "PIL_time.h"
162 * Remark: still a weak point is the newaddress() function, that doesnt solve reading from
163 * multiple files at the same time
165 * (added remark: oh, i thought that was solved? will look at that... (ton)
168 * - Existing Library (Main) push or free
169 * - allocate new Main
172 * - for each LibBlock
176 * - attach ID's to it
178 * - read associated 'direct data'
179 * - link direct data (internal and to LibBlock)
181 * - read USER data, only when indicated (file is ~/X.XX/startup.blend)
183 * - per Library (per Main)
186 * - find LibBlocks and attach IDs to Main
187 * - if external LibBlock
188 * - search all Main's
189 * - or it's already read,
194 * - read associated direct data
195 * - link direct data (internal and to LibBlock)
197 * - per Library with unread LibBlocks
202 * - read associated direct data
203 * - link direct data (internal and to LibBlock)
206 * - link all LibBlocks and indirect pointers to libblocks
207 * - initialize FileGlobal and copy pointers to Global
210 /* also occurs in library.c */
211 /* GS reads the memory pointed at in a specific ordering. There are,
212 * however two definitions for it. I have jotted them down here, both,
213 * but I think the first one is actually used. The thing is that
214 * big-endian systems might read this the wrong way round. OTOH, we
215 * constructed the IDs that are read out with this macro explicitly as
216 * well. I expect we'll sort it out soon... */
219 #define GS(a) (*((short *)(a)))
221 /* from misc_util: flip the bytes from x */
222 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
224 // only used here in readfile.c
225 #define SWITCH_LONGINT(a) { \
228 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
229 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
230 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
231 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; \
236 typedef struct OldNew {
241 typedef struct OldNewMap {
243 int nentries, entriessize;
249 /* local prototypes */
250 static void *read_struct(FileData *fd, BHead *bh, const char *blockname);
251 static void direct_link_modifiers(FileData *fd, ListBase *lb);
252 static void convert_tface_mt(FileData *fd, Main *main);
254 /* this function ensures that reports are printed,
255 * in the case of libraray linking errors this is important!
257 * bit kludge but better then doubling up on prints,
258 * we could alternatively have a versions of a report function which forces printing - campbell
260 static void BKE_reportf_wrap(ReportList *reports, ReportType type, const char *format, ...)
262 char fixed_buf[1024]; /* should be long enough */
266 va_start(args, format);
267 vsnprintf(fixed_buf, sizeof(fixed_buf), format, args);
270 fixed_buf[sizeof(fixed_buf) - 1] = '\0';
272 BKE_report(reports, type, fixed_buf);
274 if (G.background == 0) {
275 printf("%s\n", fixed_buf);
279 static OldNewMap *oldnewmap_new(void)
281 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
283 onm->entriessize = 1024;
284 onm->entries = MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
289 static int verg_oldnewmap(const void *v1, const void *v2)
291 const struct OldNew *x1=v1, *x2=v2;
293 if (x1->old > x2->old) return 1;
294 else if (x1->old < x2->old) return -1;
299 static void oldnewmap_sort(FileData *fd)
301 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
302 fd->libmap->sorted = 1;
305 /* nr is zero for data, and ID code for libdata */
306 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
310 if (oldaddr==NULL || newaddr==NULL) return;
312 if (onm->nentries == onm->entriessize) {
313 int osize = onm->entriessize;
314 OldNew *oentries = onm->entries;
316 onm->entriessize *= 2;
317 onm->entries = MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
319 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
323 entry = &onm->entries[onm->nentries++];
324 entry->old = oldaddr;
325 entry->newp = newaddr;
329 void blo_do_versions_oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
331 oldnewmap_insert(onm, oldaddr, newaddr, nr);
334 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
338 if (addr == NULL) return NULL;
340 if (onm->lasthit < onm->nentries-1) {
341 OldNew *entry = &onm->entries[++onm->lasthit];
343 if (entry->old == addr) {
349 for (i = 0; i < onm->nentries; i++) {
350 OldNew *entry = &onm->entries[i];
352 if (entry->old == addr) {
363 /* for libdata, nr has ID code, no increment */
364 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
368 if (addr == NULL) return NULL;
370 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
372 OldNew entry_s, *entry;
376 entry = bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
378 ID *id = entry->newp;
380 if (id && (!lib || id->lib)) {
386 for (i = 0; i < onm->nentries; i++) {
387 OldNew *entry = &onm->entries[i];
389 if (entry->old == addr) {
390 ID *id = entry->newp;
392 if (id && (!lib || id->lib)) {
401 static void oldnewmap_free_unused(OldNewMap *onm)
405 for (i = 0; i < onm->nentries; i++) {
406 OldNew *entry = &onm->entries[i];
407 if (entry->nr == 0) {
408 MEM_freeN(entry->newp);
414 static void oldnewmap_clear(OldNewMap *onm)
420 static void oldnewmap_free(OldNewMap *onm)
422 MEM_freeN(onm->entries);
428 static void read_libraries(FileData *basefd, ListBase *mainlist);
430 /* ************ help functions ***************** */
432 static void add_main_to_main(Main *mainvar, Main *from)
434 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
437 set_listbasepointers(mainvar, lbarray);
438 a = set_listbasepointers(from, fromarray);
440 BLI_movelisttolist(lbarray[a], fromarray[a]);
444 void blo_join_main(ListBase *mainlist)
446 Main *tojoin, *mainl;
448 mainl = mainlist->first;
449 while ((tojoin = mainl->next)) {
450 add_main_to_main(mainl, tojoin);
451 BLI_remlink(mainlist, tojoin);
456 static void split_libdata(ListBase *lb, Main *first)
468 if (mainvar->curlib == id->lib) {
469 lbn= which_libbase(mainvar, GS(id->name));
471 BLI_addtail(lbn, id);
474 mainvar = mainvar->next;
476 if (mainvar == NULL) printf("error split_libdata\n");
482 void blo_split_main(ListBase *mainlist, Main *main)
484 ListBase *lbarray[MAX_LIBARRAY];
488 mainlist->first = mainlist->last = main;
491 if (main->library.first == NULL)
494 for (lib = main->library.first; lib; lib = lib->id.next) {
495 Main *libmain = MEM_callocN(sizeof(Main), "libmain");
496 libmain->curlib = lib;
497 BLI_addtail(mainlist, libmain);
500 i = set_listbasepointers(main, lbarray);
502 split_libdata(lbarray[i], main->next);
505 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
506 static void cleanup_path(const char *relabase, char *name)
508 char filename[FILE_MAXFILE];
510 BLI_splitdirstring(name, filename);
511 BLI_cleanup_dir(relabase, name);
512 strcat(name, filename);
515 static void read_file_version(FileData *fd, Main *main)
519 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
520 if (bhead->code == GLOB) {
521 FileGlobal *fg= read_struct(fd, bhead, "Global");
523 main->subversionfile= fg->subversion;
524 main->minversionfile= fg->minversion;
525 main->minsubversionfile= fg->minsubversion;
528 else if (bhead->code == ENDB)
535 static Main *blo_find_main(FileData *fd, const char *filepath, const char *relabase)
537 ListBase *mainlist = fd->mainlist;
540 char name1[FILE_MAX];
542 BLI_strncpy(name1, filepath, sizeof(name1));
543 cleanup_path(relabase, name1);
544 // printf("blo_find_main: original in %s\n", name);
545 // printf("blo_find_main: converted to %s\n", name1);
547 for (m = mainlist->first; m; m = m->next) {
548 char *libname = (m->curlib) ? m->curlib->filepath : m->name;
550 if (BLI_path_cmp(name1, libname) == 0) {
551 if (G.debug & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
556 m = MEM_callocN(sizeof(Main), "find_main");
557 BLI_addtail(mainlist, m);
559 lib = BKE_libblock_alloc(&m->library, ID_LI, "lib");
560 BLI_strncpy(lib->name, filepath, sizeof(lib->name));
561 BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
565 read_file_version(fd, m);
567 if (G.debug & G_DEBUG) printf("blo_find_main: added new lib %s\n", filepath);
572 /* ************ FILE PARSING ****************** */
574 static void switch_endian_bh4(BHead4 *bhead)
576 /* the ID_.. codes */
577 if ((bhead->code & 0xFFFF)==0) bhead->code >>= 16;
579 if (bhead->code != ENDB) {
580 SWITCH_INT(bhead->len);
581 SWITCH_INT(bhead->SDNAnr);
582 SWITCH_INT(bhead->nr);
586 static void switch_endian_bh8(BHead8 *bhead)
588 /* the ID_.. codes */
589 if ((bhead->code & 0xFFFF)==0) bhead->code >>= 16;
591 if (bhead->code != ENDB) {
592 SWITCH_INT(bhead->len);
593 SWITCH_INT(bhead->SDNAnr);
594 SWITCH_INT(bhead->nr);
598 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
600 BHead4 *bhead4 = (BHead4 *) bhead;
601 #if defined(WIN32) && !defined(FREE_WINDOWS)
607 bhead4->code = bhead8->code;
608 bhead4->len = bhead8->len;
610 if (bhead4->code != ENDB) {
611 /* perform a endian swap on 64bit pointers, otherwise the pointer might map to zero
612 * 0x0000000000000000000012345678 would become 0x12345678000000000000000000000000
614 if (do_endian_swap) {
615 SWITCH_LONGINT(bhead8->old);
618 /* this patch is to avoid a long long being read from not-eight aligned positions
619 * is necessary on any modern 64bit architecture) */
620 memcpy(&old, &bhead8->old, 8);
621 bhead4->old = (int) (old >> 3);
623 bhead4->SDNAnr = bhead8->SDNAnr;
624 bhead4->nr = bhead8->nr;
628 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
630 BHead8 *bhead8 = (BHead8 *) bhead;
632 bhead8->code = bhead4->code;
633 bhead8->len = bhead4->len;
635 if (bhead8->code != ENDB) {
636 bhead8->old = bhead4->old;
637 bhead8->SDNAnr = bhead4->SDNAnr;
638 bhead8->nr= bhead4->nr;
642 static BHeadN *get_bhead(FileData *fd)
644 BHeadN *new_bhead = NULL;
649 /* initializing to zero isn't strictly needed but shuts valgrind up
650 * since uninitialized memory gets compared */
655 /* First read the bhead structure.
656 * Depending on the platform the file was written on this can
657 * be a big or little endian BHead4 or BHead8 structure.
659 * As usual 'ENDB' (the last *partial* bhead of the file)
660 * needs some special handling. We don't want to EOF just yet.
662 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
664 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
666 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
667 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
668 switch_endian_bh4(&bhead4);
671 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
672 bh8_from_bh4(&bhead, &bhead4);
675 memcpy(&bhead, &bhead4, sizeof(bhead));
685 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
687 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
688 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
689 switch_endian_bh8(&bhead8);
692 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
693 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
696 memcpy(&bhead, &bhead8, sizeof(bhead));
705 /* make sure people are not trying to pass bad blend files */
706 if (bhead.len < 0) fd->eof = 1;
708 /* bhead now contains the (converted) bhead structure. Now read
709 * the associated data and put everything in a BHeadN (creative naming !)
712 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
714 new_bhead->next = new_bhead->prev = NULL;
715 new_bhead->bhead = bhead;
717 readsize = fd->read(fd, new_bhead + 1, bhead.len);
719 if (readsize != bhead.len) {
721 MEM_freeN(new_bhead);
732 /* We've read a new block. Now add it to the list
736 BLI_addtail(&fd->listbase, new_bhead);
742 BHead *blo_firstbhead(FileData *fd)
748 * Read in a new block if necessary
750 new_bhead = fd->listbase.first;
751 if (new_bhead == NULL) {
752 new_bhead = get_bhead(fd);
756 bhead = &new_bhead->bhead;
762 BHead *blo_prevbhead(FileData *UNUSED(fd), BHead *thisblock)
764 BHeadN *bheadn = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
765 BHeadN *prev = bheadn->prev;
767 return (prev) ? &prev->bhead : NULL;
770 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
772 BHeadN *new_bhead = NULL;
776 // bhead is actually a sub part of BHeadN
777 // We calculate the BHeadN pointer from the BHead pointer below
778 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
780 // get the next BHeadN. If it doesn't exist we read in the next one
781 new_bhead = new_bhead->next;
782 if (new_bhead == NULL) {
783 new_bhead = get_bhead(fd);
788 // here we do the reverse:
789 // go from the BHeadN pointer to the BHead pointer
790 bhead = &new_bhead->bhead;
796 static void decode_blender_header(FileData *fd)
798 char header[SIZEOFBLENDERHEADER], num[4];
801 /* read in the header data */
802 readsize = fd->read(fd, header, sizeof(header));
804 if (readsize == sizeof(header)) {
805 if (strncmp(header, "BLENDER", 7) == 0) {
806 int remove_this_endian_test = 1;
808 fd->flags |= FD_FLAGS_FILE_OK;
810 /* what size are pointers in the file ? */
811 if (header[7]=='_') {
812 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
813 if (sizeof(void *) != 4) {
814 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
818 if (sizeof(void *) != 8) {
819 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
823 /* is the file saved in a different endian
826 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
827 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
830 /* get the version number */
831 memcpy(num, header + 9, 3);
833 fd->fileversion = atoi(num);
838 static int read_file_dna(FileData *fd)
842 for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
843 if (bhead->code == DNA1) {
844 int do_endian_swap = (fd->flags & FD_FLAGS_SWITCH_ENDIAN) ? 1 : 0;
846 fd->filesdna = DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
848 fd->compflags = DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
849 /* used to retrieve ID names from (bhead+1) */
850 fd->id_name_offs = DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
855 else if (bhead->code == ENDB)
862 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
864 int readsize = read(filedata->filedes, buffer, size);
870 filedata->seek += readsize;
876 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
878 int readsize = gzread(filedata->gzfiledes, buffer, size);
884 filedata->seek += readsize;
890 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
892 /* don't read more bytes then there are available in the buffer */
893 int readsize = (int)MIN2(size, (unsigned int)(filedata->buffersize - filedata->seek));
895 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
896 filedata->seek += readsize;
901 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
903 static unsigned int seek = (1<<30); /* the current position */
904 static unsigned int offset = 0; /* size of previous chunks */
905 static MemFileChunk *chunk = NULL;
906 unsigned int chunkoffset, readsize, totread;
908 if (size == 0) return 0;
910 if (seek != (unsigned int)filedata->seek) {
911 chunk = filedata->memfile->chunks.first;
915 if (seek + chunk->size > (unsigned) filedata->seek) break;
920 seek = filedata->seek;
927 /* first check if it's on the end if current chunk */
928 if (seek-offset == chunk->size) {
929 offset += chunk->size;
933 /* debug, should never happen */
935 printf("illegal read, chunk zero\n");
939 chunkoffset = seek-offset;
940 readsize = size-totread;
942 /* data can be spread over multiple chunks, so clamp size
943 * to within this chunk, and then it will read further in
945 if (chunkoffset+readsize > chunk->size)
946 readsize= chunk->size-chunkoffset;
948 memcpy((char*)buffer + totread, chunk->buf + chunkoffset, readsize);
950 filedata->seek += readsize;
952 } while (totread < size);
960 static FileData *filedata_new(void)
962 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
965 fd->gzfiledes = NULL;
967 /* XXX, this doesn't need to be done all the time,
968 * but it keeps us re-entrant, remove once we have
969 * a lib that provides a nice lock. - zr
971 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
973 fd->datamap = oldnewmap_new();
974 fd->globmap = oldnewmap_new();
975 fd->libmap = oldnewmap_new();
980 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
982 decode_blender_header(fd);
984 if (fd->flags & FD_FLAGS_FILE_OK) {
985 if (!read_file_dna(fd)) {
986 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", incomplete", fd->relabase);
987 blo_freefiledata(fd);
992 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file: \"%s\", not a blend file", fd->relabase);
993 blo_freefiledata(fd);
1000 /* cannot be called with relative paths anymore! */
1001 /* on each new library added, it now checks for the current FileData and expands relativeness */
1002 FileData *blo_openblenderfile(const char *filepath, ReportList *reports)
1006 gzfile = BLI_gzopen(filepath, "rb");
1008 if (gzfile == (gzFile)Z_NULL) {
1009 BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", filepath, errno ? strerror(errno) : "Unknown error reading file");
1013 FileData *fd = filedata_new();
1014 fd->gzfiledes = gzfile;
1015 fd->read = fd_read_gzip_from_file;
1017 /* needed for library_append and read_libraries */
1018 BLI_strncpy(fd->relabase, filepath, sizeof(fd->relabase));
1020 return blo_decode_and_check(fd, reports);
1024 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
1026 if (!mem || memsize<SIZEOFBLENDERHEADER) {
1027 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
1031 FileData *fd = filedata_new();
1033 fd->buffersize = memsize;
1034 fd->read = fd_read_from_memory;
1035 fd->flags |= FD_FLAGS_NOT_MY_BUFFER;
1037 return blo_decode_and_check(fd, reports);
1041 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
1044 BKE_report(reports, RPT_ERROR, "Unable to open blend <memory>");
1048 FileData *fd = filedata_new();
1049 fd->memfile = memfile;
1051 fd->read = fd_read_from_memfile;
1052 fd->flags |= FD_FLAGS_NOT_MY_BUFFER;
1054 return blo_decode_and_check(fd, reports);
1059 void blo_freefiledata(FileData *fd)
1062 if (fd->filedes != -1) {
1066 if (fd->gzfiledes != NULL) {
1067 gzclose(fd->gzfiledes);
1070 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1071 MEM_freeN(fd->buffer);
1075 // Free all BHeadN data blocks
1076 BLI_freelistN(&fd->listbase);
1079 DNA_sdna_free(fd->memsdna);
1081 DNA_sdna_free(fd->filesdna);
1083 MEM_freeN(fd->compflags);
1086 oldnewmap_free(fd->datamap);
1088 oldnewmap_free(fd->globmap);
1090 oldnewmap_free(fd->imamap);
1091 if (fd->movieclipmap)
1092 oldnewmap_free(fd->movieclipmap);
1093 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1094 oldnewmap_free(fd->libmap);
1096 MEM_freeN(fd->bheadmap);
1102 /* ************ DIV ****************** */
1104 int BLO_has_bfile_extension(const char *str)
1106 return (BLI_testextensie(str, ".ble") ||
1107 BLI_testextensie(str, ".blend") ||
1108 BLI_testextensie(str, ".blend.gz"));
1111 int BLO_is_a_library(const char *path, char *dir, char *group)
1113 /* return ok when a blenderfile, in dir is the filename,
1114 * in group the type of libdata
1121 if (len < 7) return 0;
1122 if ((dir[len - 1] != '/') && (dir[len - 1] != '\\')) return 0;
1125 dir[len - 1] = '\0';
1127 /* Find the last slash */
1128 fd = BLI_last_slash(dir);
1130 if (fd == NULL) return 0;
1132 if (BLO_has_bfile_extension(fd+1)) {
1133 /* the last part of the dir is a .blend file, no group follows */
1134 *fd = '/'; /* put back the removed slash separating the dir and the .blend file name */
1137 char *gp = fd + 1; // in case we have a .blend file, gp points to the group
1139 /* Find the last slash */
1140 fd = BLI_last_slash(dir);
1141 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
1143 /* now we know that we are in a blend file and it is safe to
1144 * assume that gp actually points to a group */
1145 if (strcmp("Screen", gp) != 0)
1146 BLI_strncpy(group, gp, GROUP_MAX);
1151 /* ************** OLD POINTERS ******************* */
1153 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1155 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1158 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1160 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1163 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1165 if (fd->imamap && adr)
1166 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1170 static void *newmclipadr(FileData *fd, void *adr) /* used to restore movie clip data after undo */
1172 if (fd->movieclipmap && adr)
1173 return oldnewmap_lookup_and_inc(fd->movieclipmap, adr);
1178 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1180 return oldnewmap_liblookup(fd->libmap, adr, lib);
1183 void *blo_do_versions_newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1185 return newlibadr(fd, lib, adr);
1188 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1190 ID *id = newlibadr(fd, lib, adr);
1198 void *blo_do_versions_newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1200 return newlibadr_us(fd, lib, adr);
1203 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1207 for (i = 0; i < fd->libmap->nentries; i++) {
1208 OldNew *entry = &fd->libmap->entries[i];
1210 if (old==entry->newp && entry->nr==ID_ID) {
1212 if (new) entry->nr = GS( ((ID *)new)->name );
1217 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1221 for (mainptr = mainlist->first; mainptr; mainptr = mainptr->next) {
1224 if (mainptr->curlib)
1225 fd = mainptr->curlib->filedata;
1230 change_idid_adr_fd(fd, old, new);
1235 /* lib linked proxy objects point to our local data, we need
1236 * to clear that pointer before reading the undo memfile since
1237 * the object might be removed, it is set again in reading
1238 * if the local object still exists */
1239 void blo_clear_proxy_pointers_from_lib(Main *oldmain)
1241 Object *ob = oldmain->object.first;
1243 for (; ob; ob= ob->id.next) {
1245 ob->proxy_from = NULL;
1249 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1251 Image *ima = oldmain->image.first;
1252 Scene *sce = oldmain->scene.first;
1255 fd->imamap = oldnewmap_new();
1257 for (; ima; ima = ima->id.next) {
1258 Link *ibuf = ima->ibufs.first;
1259 for (; ibuf; ibuf = ibuf->next)
1260 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1261 if (ima->gputexture)
1262 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1263 for (a=0; a < IMA_MAX_RENDER_SLOT; a++)
1264 if (ima->renders[a])
1265 oldnewmap_insert(fd->imamap, ima->renders[a], ima->renders[a], 0);
1267 for (; sce; sce = sce->id.next) {
1268 if (sce->nodetree) {
1270 for (node = sce->nodetree->nodes.first; node; node = node->next)
1271 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1276 /* set old main image ibufs to zero if it has been restored */
1277 /* this works because freeing old main only happens after this call */
1278 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1280 OldNew *entry = fd->imamap->entries;
1281 Image *ima = oldmain->image.first;
1282 Scene *sce = oldmain->scene.first;
1285 /* used entries were restored, so we put them to zero */
1286 for (i = 0; i < fd->imamap->nentries; i++, entry++) {
1291 for (; ima; ima = ima->id.next) {
1294 /* this mirrors direct_link_image */
1295 for (ibuf = ima->ibufs.first; ibuf; ibuf = next) {
1297 if (NULL == newimaadr(fd, ibuf)) { /* so was restored */
1298 BLI_remlink(&ima->ibufs, ibuf);
1300 ima->gputexture = NULL;
1303 for (i = 0; i < IMA_MAX_RENDER_SLOT; i++)
1304 ima->renders[i] = newimaadr(fd, ima->renders[i]);
1306 ima->gputexture = newimaadr(fd, ima->gputexture);
1308 for (; sce; sce = sce->id.next) {
1309 if (sce->nodetree) {
1311 for (node = sce->nodetree->nodes.first; node; node = node->next)
1312 node->preview = newimaadr(fd, node->preview);
1317 void blo_make_movieclip_pointer_map(FileData *fd, Main *oldmain)
1319 MovieClip *clip = oldmain->movieclip.first;
1320 Scene *sce = oldmain->scene.first;
1322 fd->movieclipmap = oldnewmap_new();
1324 for (; clip; clip = clip->id.next) {
1326 oldnewmap_insert(fd->movieclipmap, clip->cache, clip->cache, 0);
1328 if (clip->tracking.camera.intrinsics)
1329 oldnewmap_insert(fd->movieclipmap, clip->tracking.camera.intrinsics, clip->tracking.camera.intrinsics, 0);
1332 for (; sce; sce = sce->id.next) {
1333 if (sce->nodetree) {
1335 for (node = sce->nodetree->nodes.first; node; node= node->next)
1336 if (node->type == CMP_NODE_MOVIEDISTORTION)
1337 oldnewmap_insert(fd->movieclipmap, node->storage, node->storage, 0);
1342 /* set old main movie clips caches to zero if it has been restored */
1343 /* this works because freeing old main only happens after this call */
1344 void blo_end_movieclip_pointer_map(FileData *fd, Main *oldmain)
1346 OldNew *entry = fd->movieclipmap->entries;
1347 MovieClip *clip = oldmain->movieclip.first;
1348 Scene *sce = oldmain->scene.first;
1351 /* used entries were restored, so we put them to zero */
1352 for (i=0; i < fd->movieclipmap->nentries; i++, entry++) {
1357 for (; clip; clip = clip->id.next) {
1358 clip->cache = newmclipadr(fd, clip->cache);
1359 clip->tracking.camera.intrinsics = newmclipadr(fd, clip->tracking.camera.intrinsics);
1362 for (; sce; sce = sce->id.next) {
1363 if (sce->nodetree) {
1365 for (node = sce->nodetree->nodes.first; node; node = node->next)
1366 if (node->type == CMP_NODE_MOVIEDISTORTION)
1367 node->storage = newmclipadr(fd, node->storage);
1373 /* undo file support: add all library pointers in lookup */
1374 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1376 Main *ptr = mainlist->first;
1377 ListBase *lbarray[MAX_LIBARRAY];
1379 for (ptr = ptr->next; ptr; ptr = ptr->next) {
1380 int i = set_listbasepointers(ptr, lbarray);
1383 for (id = lbarray[i]->first; id; id = id->next)
1384 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1390 /* ********** END OLD POINTERS ****************** */
1391 /* ********** READ FILE ****************** */
1393 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1395 int blocksize, nblocks;
1398 data = (char *)(bhead+1);
1399 blocksize = filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1401 nblocks = bhead->nr;
1403 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1409 static void *read_struct(FileData *fd, BHead *bh, const char *blockname)
1414 /* switch is based on file dna */
1415 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1416 switch_endian_structs(fd->filesdna, bh);
1418 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1419 if (fd->compflags[bh->SDNAnr] == 2) {
1420 temp = DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1423 temp = MEM_mallocN(bh->len, blockname);
1424 memcpy(temp, (bh+1), bh->len);
1432 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1436 if (lb->first == NULL) return;
1438 lb->first = newdataadr(fd, lb->first);
1442 ln->next = newdataadr(fd, ln->next);
1450 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1455 if (lb->first == NULL) return;
1456 poin = newdataadr(fd, lb->first);
1458 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1465 poin = newdataadr(fd, ln->next);
1467 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1477 static void test_pointer_array(FileData *fd, void **mat)
1479 #if defined(WIN32) && !defined(FREE_WINDOWS)
1480 __int64 *lpoin, *lmat;
1482 long long *lpoin, *lmat;
1487 /* manually convert the pointer array in
1488 * the old dna format to a pointer array in
1489 * the new dna format.
1492 len = MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1494 if (fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1495 ipoin=imat= MEM_mallocN(len * 4, "newmatar");
1499 if ((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1500 SWITCH_LONGINT(*lpoin);
1501 *ipoin = (int)((*lpoin) >> 3);
1509 if (fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1510 lpoin = lmat = MEM_mallocN(len * 8, "newmatar");
1524 /* ************ READ ID Properties *************** */
1526 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1527 static void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1529 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1534 /* since we didn't save the extra buffer, set totallen to len */
1535 prop->totallen = prop->len;
1536 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1538 array = (IDProperty *)prop->data.pointer;
1540 /* note!, idp-arrays didn't exist in 2.4x, so the pointer will be cleared
1541 * theres not really anything we can do to correct this, at least don't crash */
1542 if (array == NULL) {
1548 for (i = 0; i < prop->len; i++)
1549 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
1552 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1557 /* since we didn't save the extra buffer, set totallen to len */
1558 prop->totallen = prop->len;
1559 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1561 if (prop->subtype == IDP_GROUP) {
1562 test_pointer_array(fd, prop->data.pointer);
1563 array = prop->data.pointer;
1565 for (i = 0; i < prop->len; i++)
1566 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1568 else if (prop->subtype == IDP_DOUBLE) {
1569 if (switch_endian) {
1570 for (i = 0; i < prop->len; i++) {
1571 SWITCH_LONGINT(((double *)prop->data.pointer)[i]);
1576 if (switch_endian) {
1577 for (i = 0; i < prop->len; i++) {
1578 SWITCH_INT(((int *)prop->data.pointer)[i]);
1584 static void IDP_DirectLinkString(IDProperty *prop, FileData *fd)
1586 /*since we didn't save the extra string buffer, set totallen to len.*/
1587 prop->totallen = prop->len;
1588 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1591 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1593 ListBase *lb = &prop->data.group;
1598 /*Link child id properties now*/
1599 for (loop=prop->data.group.first; loop; loop=loop->next) {
1600 IDP_DirectLinkProperty(loop, switch_endian, fd);
1604 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1606 switch (prop->type) {
1608 IDP_DirectLinkGroup(prop, switch_endian, fd);
1611 IDP_DirectLinkString(prop, fd);
1614 IDP_DirectLinkArray(prop, switch_endian, fd);
1617 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1620 /* erg, stupid doubles. since I'm storing them
1621 * in the same field as int val; val2 in the
1622 * IDPropertyData struct, they have to deal with
1623 * endianness specifically
1625 * in theory, val and val2 would've already been swapped
1626 * if switch_endian is true, so we have to first unswap
1627 * them then reswap them as a single 64-bit entity.
1630 if (switch_endian) {
1631 SWITCH_INT(prop->data.val);
1632 SWITCH_INT(prop->data.val2);
1633 SWITCH_LONGINT(prop->data.val);
1641 static void IDP_LibLinkProperty(IDProperty *UNUSED(prop), int UNUSED(switch_endian), FileData *UNUSED(fd))
1645 /* ************ READ CurveMapping *************** */
1647 /* cuma itself has been read! */
1648 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1652 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1653 cumap->flag &= ~CUMA_PREMULLED;
1655 for (a = 0; a < CM_TOT; a++) {
1656 cumap->cm[a].curve = newdataadr(fd, cumap->cm[a].curve);
1657 cumap->cm[a].table = NULL;
1658 cumap->cm[a].premultable = NULL;
1662 /* ************ READ Brush *************** */
1663 /* library brush linking after fileread */
1664 static void lib_link_brush(FileData *fd, Main *main)
1668 /* only link ID pointers */
1669 for (brush = main->brush.first; brush; brush = brush->id.next) {
1670 if (brush->id.flag & LIB_NEEDLINK) {
1671 brush->id.flag -= LIB_NEEDLINK;
1673 brush->mtex.tex = newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
1674 brush->clone.image = newlibadr_us(fd, brush->id.lib, brush->clone.image);
1679 static void direct_link_brush(FileData *fd, Brush *brush)
1681 /* brush itself has been read */
1684 brush->curve = newdataadr(fd, brush->curve);
1686 direct_link_curvemapping(fd, brush->curve);
1688 BKE_brush_curve_preset(brush, CURVE_PRESET_SHARP);
1690 brush->preview = NULL;
1691 brush->icon_imbuf = NULL;
1694 static void direct_link_script(FileData *UNUSED(fd), Script *script)
1697 SCRIPT_SET_NULL(script);
1701 /* ************ READ PACKEDFILE *************** */
1703 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1705 PackedFile *pf = newdataadr(fd, oldpf);
1708 pf->data = newdataadr(fd, pf->data);
1714 /* ************ READ IMAGE PREVIEW *************** */
1716 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1718 PreviewImage *prv = newdataadr(fd, old_prv);
1722 for (i = 0; i < NUM_ICON_SIZES; ++i) {
1724 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1732 /* ************ READ ANIMATION STUFF ***************** */
1734 /* Legacy Data Support (for Version Patching) ----------------------------- */
1736 // XXX depreceated - old animation system
1737 static void lib_link_ipo(FileData *fd, Main *main)
1741 for (ipo = main->ipo.first; ipo; ipo = ipo->id.next) {
1742 if (ipo->id.flag & LIB_NEEDLINK) {
1744 for (icu = ipo->curve.first; icu; icu = icu->next) {
1746 icu->driver->ob = newlibadr(fd, ipo->id.lib, icu->driver->ob);
1748 ipo->id.flag -= LIB_NEEDLINK;
1753 // XXX depreceated - old animation system
1754 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1758 link_list(fd, &(ipo->curve));
1760 for (icu = ipo->curve.first; icu; icu = icu->next) {
1761 icu->bezt = newdataadr(fd, icu->bezt);
1762 icu->bp = newdataadr(fd, icu->bp);
1763 icu->driver = newdataadr(fd, icu->driver);
1767 // XXX depreceated - old animation system
1768 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1770 bActionStrip *strip;
1771 bActionModifier *amod;
1773 for (strip=striplist->first; strip; strip=strip->next) {
1774 strip->object = newlibadr(fd, id->lib, strip->object);
1775 strip->act = newlibadr_us(fd, id->lib, strip->act);
1776 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1777 for (amod = strip->modifiers.first; amod; amod = amod->next)
1778 amod->ob = newlibadr(fd, id->lib, amod->ob);
1782 // XXX depreceated - old animation system
1783 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1785 bActionStrip *strip;
1787 link_list(fd, strips);
1789 for (strip = strips->first; strip; strip = strip->next)
1790 link_list(fd, &strip->modifiers);
1793 // XXX depreceated - old animation system
1794 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1796 bConstraintChannel *chan;
1798 for (chan=chanbase->first; chan; chan=chan->next) {
1799 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1803 /* Data Linking ----------------------------- */
1805 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1809 for (fcm = list->first; fcm; fcm = fcm->next) {
1810 /* data for specific modifiers */
1811 switch (fcm->type) {
1812 case FMODIFIER_TYPE_PYTHON:
1814 FMod_Python *data = (FMod_Python *)fcm->data;
1815 data->script = newlibadr(fd, id->lib, data->script);
1822 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1829 /* relink ID-block references... */
1830 for (fcu = list->first; fcu; fcu = fcu->next) {
1833 ChannelDriver *driver = fcu->driver;
1836 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1837 DRIVER_TARGETS_LOOPER(dvar)
1839 /* only relink if still used */
1840 if (tarIndex < dvar->num_targets)
1841 dtar->id = newlibadr(fd, id->lib, dtar->id);
1845 DRIVER_TARGETS_LOOPER_END
1850 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1855 /* NOTE: this assumes that link_list has already been called on the list */
1856 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1860 for (fcm = list->first; fcm; fcm = fcm->next) {
1861 /* relink general data */
1862 fcm->data = newdataadr(fd, fcm->data);
1865 /* do relinking of data for specific types */
1866 switch (fcm->type) {
1867 case FMODIFIER_TYPE_GENERATOR:
1869 FMod_Generator *data = (FMod_Generator *)fcm->data;
1871 data->coefficients = newdataadr(fd, data->coefficients);
1873 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1875 for (a = 0; a < data->arraysize; a++)
1876 SWITCH_INT(data->coefficients[a]);
1880 case FMODIFIER_TYPE_ENVELOPE:
1882 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1884 data->data= newdataadr(fd, data->data);
1887 case FMODIFIER_TYPE_PYTHON:
1889 FMod_Python *data = (FMod_Python *)fcm->data;
1891 data->prop = newdataadr(fd, data->prop);
1892 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1899 /* NOTE: this assumes that link_list has already been called on the list */
1900 static void direct_link_fcurves(FileData *fd, ListBase *list)
1904 /* link F-Curve data to F-Curve again (non ID-libs) */
1905 for (fcu = list->first; fcu; fcu = fcu->next) {
1907 fcu->bezt = newdataadr(fd, fcu->bezt);
1908 fcu->fpt = newdataadr(fd, fcu->fpt);
1911 fcu->rna_path = newdataadr(fd, fcu->rna_path);
1914 fcu->grp = newdataadr(fd, fcu->grp);
1916 /* clear disabled flag - allows disabled drivers to be tried again ([#32155]),
1917 * but also means that another method for "reviving disabled F-Curves" exists
1919 fcu->flag &= ~FCURVE_DISABLED;
1922 fcu->driver= newdataadr(fd, fcu->driver);
1924 ChannelDriver *driver= fcu->driver;
1927 /* compiled expression data will need to be regenerated (old pointer may still be set here) */
1928 driver->expr_comp = NULL;
1930 /* give the driver a fresh chance - the operating environment may be different now
1931 * (addons, etc. may be different) so the driver namespace may be sane now [#32155]
1933 driver->flag &= ~DRIVER_FLAG_INVALID;
1935 /* relink variables, targets and their paths */
1936 link_list(fd, &driver->variables);
1937 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1938 DRIVER_TARGETS_LOOPER(dvar)
1940 /* only relink the targets being used */
1941 if (tarIndex < dvar->num_targets)
1942 dtar->rna_path = newdataadr(fd, dtar->rna_path);
1944 dtar->rna_path = NULL;
1946 DRIVER_TARGETS_LOOPER_END
1951 link_list(fd, &fcu->modifiers);
1952 direct_link_fmodifiers(fd, &fcu->modifiers);
1957 static void lib_link_action(FileData *fd, Main *main)
1960 bActionChannel *chan;
1962 for (act = main->action.first; act; act = act->id.next) {
1963 if (act->id.flag & LIB_NEEDLINK) {
1964 act->id.flag -= LIB_NEEDLINK;
1966 // XXX depreceated - old animation system <<<
1967 for (chan=act->chanbase.first; chan; chan=chan->next) {
1968 chan->ipo = newlibadr_us(fd, act->id.lib, chan->ipo);
1969 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1971 // >>> XXX depreceated - old animation system
1973 lib_link_fcurves(fd, &act->id, &act->curves);
1978 static void direct_link_action(FileData *fd, bAction *act)
1980 bActionChannel *achan; // XXX depreceated - old animation system
1983 link_list(fd, &act->curves);
1984 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1985 link_list(fd, &act->groups);
1986 link_list(fd, &act->markers);
1988 // XXX depreceated - old animation system <<<
1989 for (achan = act->chanbase.first; achan; achan=achan->next) {
1990 achan->grp = newdataadr(fd, achan->grp);
1992 link_list(fd, &achan->constraintChannels);
1994 // >>> XXX depreceated - old animation system
1996 direct_link_fcurves(fd, &act->curves);
1998 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1999 agrp->channels.first= newdataadr(fd, agrp->channels.first);
2000 agrp->channels.last= newdataadr(fd, agrp->channels.last);
2004 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
2008 for (strip = list->first; strip; strip = strip->next) {
2009 /* check strip's children */
2010 lib_link_nladata_strips(fd, id, &strip->strips);
2012 /* check strip's F-Curves */
2013 lib_link_fcurves(fd, id, &strip->fcurves);
2015 /* reassign the counted-reference to action */
2016 strip->act = newlibadr_us(fd, id->lib, strip->act);
2018 /* fix action id-root (i.e. if it comes from a pre 2.57 .blend file) */
2019 if ((strip->act) && (strip->act->idroot == 0))
2020 strip->act->idroot = GS(id->name);
2024 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
2028 /* we only care about the NLA strips inside the tracks */
2029 for (nlt = list->first; nlt; nlt = nlt->next) {
2030 lib_link_nladata_strips(fd, id, &nlt->strips);
2034 /* This handles Animato NLA-Strips linking
2035 * NOTE: this assumes that link_list has already been called on the list
2037 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
2041 for (strip = list->first; strip; strip = strip->next) {
2042 /* strip's child strips */
2043 link_list(fd, &strip->strips);
2044 direct_link_nladata_strips(fd, &strip->strips);
2046 /* strip's F-Curves */
2047 link_list(fd, &strip->fcurves);
2048 direct_link_fcurves(fd, &strip->fcurves);
2050 /* strip's F-Modifiers */
2051 link_list(fd, &strip->modifiers);
2052 direct_link_fmodifiers(fd, &strip->modifiers);
2056 /* NOTE: this assumes that link_list has already been called on the list */
2057 static void direct_link_nladata(FileData *fd, ListBase *list)
2061 for (nlt = list->first; nlt; nlt = nlt->next) {
2062 /* relink list of strips */
2063 link_list(fd, &nlt->strips);
2065 /* relink strip data */
2066 direct_link_nladata_strips(fd, &nlt->strips);
2072 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
2077 /* here, we're only interested in the ID pointer stored in some of the paths */
2078 for (ks = list->first; ks; ks = ks->next) {
2079 for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
2080 ksp->id= newlibadr(fd, id->lib, ksp->id);
2085 /* NOTE: this assumes that link_list has already been called on the list */
2086 static void direct_link_keyingsets(FileData *fd, ListBase *list)
2091 /* link KeyingSet data to KeyingSet again (non ID-libs) */
2092 for (ks = list->first; ks; ks = ks->next) {
2094 link_list(fd, &ks->paths);
2096 for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
2098 ksp->rna_path= newdataadr(fd, ksp->rna_path);
2105 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
2110 /* link action data */
2111 adt->action= newlibadr_us(fd, id->lib, adt->action);
2112 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
2114 /* fix action id-roots (i.e. if they come from a pre 2.57 .blend file) */
2115 if ((adt->action) && (adt->action->idroot == 0))
2116 adt->action->idroot = GS(id->name);
2117 if ((adt->tmpact) && (adt->tmpact->idroot == 0))
2118 adt->tmpact->idroot = GS(id->name);
2121 lib_link_fcurves(fd, id, &adt->drivers);
2123 /* overrides don't have lib-link for now, so no need to do anything */
2126 lib_link_nladata(fd, id, &adt->nla_tracks);
2129 static void direct_link_animdata(FileData *fd, AnimData *adt)
2131 /* NOTE: must have called newdataadr already before doing this... */
2136 link_list(fd, &adt->drivers);
2137 direct_link_fcurves(fd, &adt->drivers);
2139 /* link overrides */
2143 link_list(fd, &adt->nla_tracks);
2144 direct_link_nladata(fd, &adt->nla_tracks);
2146 /* relink active strip - even though strictly speaking this should only be used
2147 * if we're in 'tweaking mode', we need to be able to have this loaded back for
2148 * undo, but also since users may not exit tweakmode before saving (#24535)
2150 // TODO: it's not really nice that anyone should be able to save the file in this
2151 // state, but it's going to be too hard to enforce this single case...
2152 adt->actstrip = newdataadr(fd, adt->actstrip);
2155 /* ************ READ MOTION PATHS *************** */
2157 /* direct data for cache */
2158 static void direct_link_motionpath(FileData *fd, bMotionPath *mpath)
2164 /* relink points cache */
2165 mpath->points = newdataadr(fd, mpath->points);
2168 /* ************ READ NODE TREE *************** */
2170 /* singe node tree (also used for material/scene trees), ntree is not NULL */
2171 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
2175 if (ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
2177 ntree->gpd = newlibadr_us(fd, id->lib, ntree->gpd);
2179 for (node = ntree->nodes.first; node; node = node->next)
2180 node->id = newlibadr_us(fd, id->lib, node->id);
2183 /* library ntree linking after fileread */
2184 static void lib_link_nodetree(FileData *fd, Main *main)
2188 /* only link ID pointers */
2189 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next) {
2190 if (ntree->id.flag & LIB_NEEDLINK) {
2191 ntree->id.flag -= LIB_NEEDLINK;
2192 lib_link_ntree(fd, &ntree->id, ntree);
2197 static void do_versions_socket_default_value(bNodeSocket *sock)
2199 bNodeSocketValueFloat *valfloat;
2200 bNodeSocketValueVector *valvector;
2201 bNodeSocketValueRGBA *valrgba;
2203 if (sock->default_value)
2206 switch (sock->type) {
2208 valfloat = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueFloat), "default socket value");
2209 valfloat->value = sock->ns.vec[0];
2210 valfloat->min = sock->ns.min;
2211 valfloat->max = sock->ns.max;
2212 valfloat->subtype = PROP_NONE;
2215 valvector = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueVector), "default socket value");
2216 copy_v3_v3(valvector->value, sock->ns.vec);
2217 valvector->min = sock->ns.min;
2218 valvector->max = sock->ns.max;
2219 valvector->subtype = PROP_NONE;
2222 valrgba = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueRGBA), "default socket value");
2223 copy_v4_v4(valrgba->value, sock->ns.vec);
2228 void blo_do_versions_nodetree_default_value(bNodeTree *ntree)
2232 for (node=ntree->nodes.first; node; node=node->next) {
2233 for (sock=node->inputs.first; sock; sock=sock->next)
2234 do_versions_socket_default_value(sock);
2235 for (sock=node->outputs.first; sock; sock=sock->next)
2236 do_versions_socket_default_value(sock);
2238 for (sock=ntree->inputs.first; sock; sock=sock->next)
2239 do_versions_socket_default_value(sock);
2240 for (sock=ntree->outputs.first; sock; sock=sock->next)
2241 do_versions_socket_default_value(sock);
2244 static void lib_nodetree_init_types_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2248 ntreeInitTypes(ntree);
2250 /* need to do this here instead of in do_versions, otherwise next function can crash */
2251 blo_do_versions_nodetree_default_value(ntree);
2253 /* XXX could be replaced by do_versions for new nodes */
2254 for (node=ntree->nodes.first; node; node=node->next)
2255 node_verify_socket_templates(ntree, node);
2258 /* updates group node socket own_index so that
2259 * external links to/from the group node are preserved.
2261 static void lib_node_do_versions_group_indices(bNode *gnode)
2263 bNodeTree *ngroup = (bNodeTree*)gnode->id;
2265 bNodeSocket *sock, *gsock, *intsock;
2268 for (sock=gnode->outputs.first; sock; sock=sock->next) {
2269 int old_index = sock->to_index;
2270 for (gsock=ngroup->outputs.first; gsock; gsock=gsock->next) {
2271 if (gsock->link && gsock->link->fromsock->own_index == old_index) {
2272 sock->own_index = gsock->own_index;
2277 for (sock=gnode->inputs.first; sock; sock=sock->next) {
2278 int old_index = sock->to_index;
2279 /* can't use break in double loop */
2281 for (intnode=ngroup->nodes.first; intnode && !found; intnode=intnode->next) {
2282 for (intsock=intnode->inputs.first; intsock; intsock=intsock->next) {
2283 if (intsock->own_index == old_index && intsock->link) {
2284 sock->own_index = intsock->link->fromsock->own_index;
2293 /* updates external links for all group nodes in a tree */
2294 static void lib_nodetree_do_versions_group_indices_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2298 for (node = ntree->nodes.first; node; node = node->next) {
2299 if (node->type == NODE_GROUP) {
2300 bNodeTree *ngroup = (bNodeTree*)node->id;
2301 if (ngroup && (ngroup->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE))
2302 lib_node_do_versions_group_indices(node);
2307 /* make an update call for the tree */
2308 static void lib_nodetree_do_versions_update_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2311 ntreeUpdateTree(ntree);
2314 /* verify types for nodes and groups, all data has to be read */
2315 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
2317 static void lib_verify_nodetree(Main *main, int UNUSED(open))
2321 bNodeTreeType *ntreetype;
2323 /* this crashes blender on undo/redo */
2326 reinit_nodesystem();
2330 /* set node->typeinfo pointers */
2331 for (i = 0; i < NUM_NTREE_TYPES; ++i) {
2332 ntreetype = ntreeGetType(i);
2333 if (ntreetype && ntreetype->foreach_nodetree)
2334 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_init_types_cb);
2336 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next)
2337 lib_nodetree_init_types_cb(NULL, NULL, ntree);
2340 int has_old_groups = 0;
2341 /* XXX this should actually be part of do_versions, but since we need
2342 * finished library linking, it is not possible there. Instead in do_versions
2343 * we have set the NTREE_DO_VERSIONS flag, so at this point we can do the
2344 * actual group node updates.
2346 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next) {
2347 if (ntree->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE) {
2348 /* this adds copies and links from all unlinked internal sockets to group inputs/outputs. */
2349 node_group_expose_all_sockets(ntree);
2354 if (has_old_groups) {
2355 for (i = 0; i < NUM_NTREE_TYPES; ++i) {
2356 ntreetype = ntreeGetType(i);
2357 if (ntreetype && ntreetype->foreach_nodetree)
2358 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_group_indices_cb);
2362 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next)
2363 ntree->flag &= ~NTREE_DO_VERSIONS_GROUP_EXPOSE;
2366 /* verify all group user nodes */
2367 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next) {
2368 ntreeVerifyNodes(main, &ntree->id);
2371 /* make update calls where necessary */
2373 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next)
2375 ntreeUpdateTree(ntree);
2377 for (i = 0; i < NUM_NTREE_TYPES; i++) {
2378 ntreetype = ntreeGetType(i);
2379 if (ntreetype && ntreetype->foreach_nodetree)
2380 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_update_cb);
2385 static void direct_link_node_socket(FileData *fd, bNodeSocket *sock)
2387 sock->link = newdataadr(fd, sock->link);
2388 sock->storage = newdataadr(fd, sock->storage);
2389 sock->default_value = newdataadr(fd, sock->default_value);
2393 /* ntree itself has been read! */
2394 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2396 /* note: writing and reading goes in sync, for speed */
2401 ntree->init = 0; /* to set callbacks and force setting types */
2402 ntree->progress = NULL;
2403 ntree->execdata = NULL;
2405 ntree->adt = newdataadr(fd, ntree->adt);
2406 direct_link_animdata(fd, ntree->adt);
2408 link_list(fd, &ntree->nodes);
2409 for (node = ntree->nodes.first; node; node = node->next) {
2410 node->typeinfo = NULL;
2412 link_list(fd, &node->inputs);
2413 link_list(fd, &node->outputs);
2415 if (node->type == CMP_NODE_MOVIEDISTORTION) {
2416 node->storage = newmclipadr(fd, node->storage);
2419 node->storage = newdataadr(fd, node->storage);
2421 if (node->storage) {
2422 /* could be handlerized at some point */
2423 if (ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
2424 direct_link_curvemapping(fd, node->storage);
2425 else if (ntree->type==NTREE_COMPOSIT) {
2426 if (ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
2427 direct_link_curvemapping(fd, node->storage);
2428 else if (ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2429 ((ImageUser *)node->storage)->ok = 1;
2431 else if ( ntree->type==NTREE_TEXTURE) {
2432 if (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME)
2433 direct_link_curvemapping(fd, node->storage);
2434 else if (node->type==TEX_NODE_IMAGE)
2435 ((ImageUser *)node->storage)->ok = 1;
2439 link_list(fd, &ntree->links);
2441 /* external sockets */
2442 link_list(fd, &ntree->inputs);
2443 link_list(fd, &ntree->outputs);
2445 /* and we connect the rest */
2446 for (node = ntree->nodes.first; node; node = node->next) {
2447 node->parent = newdataadr(fd, node->parent);
2448 node->preview = newimaadr(fd, node->preview);
2451 for (sock = node->inputs.first; sock; sock = sock->next)
2452 direct_link_node_socket(fd, sock);
2453 for (sock = node->outputs.first; sock; sock = sock->next)
2454 direct_link_node_socket(fd, sock);
2456 for (sock = ntree->inputs.first; sock; sock = sock->next)
2457 direct_link_node_socket(fd, sock);
2458 for (sock = ntree->outputs.first; sock; sock = sock->next)
2459 direct_link_node_socket(fd, sock);
2461 for (link = ntree->links.first; link; link= link->next) {
2462 link->fromnode = newdataadr(fd, link->fromnode);
2463 link->tonode = newdataadr(fd, link->tonode);
2464 link->fromsock = newdataadr(fd, link->fromsock);
2465 link->tosock = newdataadr(fd, link->tosock);
2468 /* type verification is in lib-link */
2471 /* ************ READ ARMATURE ***************** */
2473 /* temp struct used to transport needed info to lib_link_constraint_cb() */
2474 typedef struct tConstraintLinkData {
2477 } tConstraintLinkData;
2478 /* callback function used to relink constraint ID-links */
2479 static void lib_link_constraint_cb(bConstraint *UNUSED(con), ID **idpoin, short isReference, void *userdata)
2481 tConstraintLinkData *cld= (tConstraintLinkData *)userdata;
2483 /* for reference types, we need to increment the usercounts on load... */
2485 /* reference type - with usercount */
2486 *idpoin = newlibadr_us(cld->fd, cld->id->lib, *idpoin);
2489 /* target type - no usercount needed */
2490 *idpoin = newlibadr(cld->fd, cld->id->lib, *idpoin);
2494 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2496 tConstraintLinkData cld;
2500 for (con = conlist->first; con; con=con->next) {
2501 /* patch for error introduced by changing constraints (dunno how) */
2502 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2503 if (con->data == NULL) {
2504 con->type = CONSTRAINT_TYPE_NULL;
2506 /* own ipo, all constraints have it */
2507 con->ipo = newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2510 /* relink all ID-blocks used by the constraints */
2514 id_loop_constraints(conlist, lib_link_constraint_cb, &cld);
2517 static void direct_link_constraints(FileData *fd, ListBase *lb)
2522 for (con=lb->first; con; con=con->next) {
2523 con->data = newdataadr(fd, con->data);
2525 switch (con->type) {
2526 case CONSTRAINT_TYPE_PYTHON:
2528 bPythonConstraint *data= con->data;
2530 link_list(fd, &data->targets);
2532 data->prop = newdataadr(fd, data->prop);
2534 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2537 case CONSTRAINT_TYPE_SPLINEIK:
2539 bSplineIKConstraint *data= con->data;
2541 data->points= newdataadr(fd, data->points);
2544 case CONSTRAINT_TYPE_KINEMATIC:
2546 con->lin_error = 0.f;
2547 con->rot_error = 0.f;
2549 case CONSTRAINT_TYPE_CHILDOF:
2551 /* XXX version patch, in older code this flag wasn't always set, and is inherent to type */
2552 if (con->ownspace == CONSTRAINT_SPACE_POSE)
2553 con->flag |= CONSTRAINT_SPACEONCE;
2560 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2562 bPoseChannel *pchan;
2563 bArmature *arm = ob->data;
2570 /* always rebuild to match proxy or lib changes */
2571 rebuild = ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2574 /* sync proxy layer */
2575 if (pose->proxy_layer)
2576 arm->layer = pose->proxy_layer;
2578 /* sync proxy active bone */
2579 if (pose->proxy_act_bone[0]) {
2580 Bone *bone = BKE_armature_find_bone_name(arm, pose->proxy_act_bone);
2582 arm->act_bone = bone;
2586 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2587 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2589 /* hurms... loop in a loop, but yah... later... (ton) */
2590 pchan->bone = BKE_armature_find_bone_name(arm, pchan->name);
2592 pchan->custom = newlibadr_us(fd, arm->id.lib, pchan->custom);
2593 if (pchan->bone == NULL)
2595 else if (ob->id.lib==NULL && arm->id.lib) {
2596 /* local pose selection copied to armature, bit hackish */
2597 pchan->bone->flag &= ~BONE_SELECTED;
2598 pchan->bone->flag |= pchan->selectflag;
2603 ob->recalc = (OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
2604 pose->flag |= POSE_RECALC;
2608 static void lib_link_armature(FileData *fd, Main *main)
2612 for (arm = main->armature.first; arm; arm = arm->id.next) {
2613 if (arm->id.flag & LIB_NEEDLINK) {
2614 if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
2615 arm->id.flag -= LIB_NEEDLINK;
2620 static void direct_link_bones(FileData *fd, Bone* bone)
2624 bone->parent = newdataadr(fd, bone->parent);
2625 bone->prop = newdataadr(fd, bone->prop);
2627 IDP_DirectLinkProperty(bone->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2629 bone->flag &= ~BONE_DRAW_ACTIVE;
2631 link_list(fd, &bone->childbase);
2633 for (child=bone->childbase.first; child; child=child->next)
2634 direct_link_bones(fd, child);
2637 static void direct_link_armature(FileData *fd, bArmature *arm)
2641 link_list(fd, &arm->bonebase);
2645 arm->adt = newdataadr(fd, arm->adt);
2646 direct_link_animdata(fd, arm->adt);
2648 for (bone = arm->bonebase.first; bone; bone = bone->next) {
2649 direct_link_bones(fd, bone);
2652 arm->act_bone = newdataadr(fd, arm->act_bone);
2653 arm->act_edbone = NULL;
2656 /* ************ READ CAMERA ***************** */
2658 static void lib_link_camera(FileData *fd, Main *main)
2662 for (ca = main->camera.first; ca; ca = ca->id.next) {
2663 if (ca->id.flag & LIB_NEEDLINK) {
2664 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2666 ca->ipo = newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2668 ca->dof_ob = newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2670 ca->id.flag -= LIB_NEEDLINK;
2675 static void direct_link_camera(FileData *fd, Camera *ca)
2677 ca->adt = newdataadr(fd, ca->adt);
2678 direct_link_animdata(fd, ca->adt);
2682 /* ************ READ LAMP ***************** */
2684 static void lib_link_lamp(FileData *fd, Main *main)
2690 for (la = main->lamp.first; la; la = la->id.next) {
2691 if (la->id.flag & LIB_NEEDLINK) {
2692 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2694 for (a = 0; a < MAX_MTEX; a++) {
2697 mtex->tex = newlibadr_us(fd, la->id.lib, mtex->tex);
2698 mtex->object = newlibadr(fd, la->id.lib, mtex->object);
2702 la->ipo = newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2705 lib_link_ntree(fd, &la->id, la->nodetree);
2707 la->id.flag -= LIB_NEEDLINK;
2712 static void direct_link_lamp(FileData *fd, Lamp *la)
2716 la->adt = newdataadr(fd, la->adt);
2717 direct_link_animdata(fd, la->adt);
2719 for (a=0; a<MAX_MTEX; a++) {
2720 la->mtex[a] = newdataadr(fd, la->mtex[a]);
2723 la->curfalloff = newdataadr(fd, la->curfalloff);
2725 direct_link_curvemapping(fd, la->curfalloff);
2727 la->nodetree= newdataadr(fd, la->nodetree);
2729 direct_link_nodetree(fd, la->nodetree);
2731 la->preview = direct_link_preview_image(fd, la->preview);
2734 /* ************ READ keys ***************** */
2736 static void lib_link_key(FileData *fd, Main *main)
2740 for (key = main->key.first; key; key = key->id.next) {
2741 /*check if we need to generate unique ids for the shapekeys*/
2746 for (block=key->block.first; block; block=block->next) {
2747 block->uid = key->uidgen++;
2751 if (key->id.flag & LIB_NEEDLINK) {
2752 if (key->adt) lib_link_animdata(fd, &key->id, key->adt);
2754 key->ipo = newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2755 key->from = newlibadr(fd, key->id.lib, key->from);
2757 key->id.flag -= LIB_NEEDLINK;
2762 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2765 char *data, *poin, *cp;
2767 elemsize = key->elemsize;
2770 for (a = 0; a < kb->totelem; a++) {
2774 while (cp[0]) { /* cp[0] == amount */
2775 switch (cp[1]) { /* cp[1] = type */
2782 SWITCH_INT((*poin));
2794 static void direct_link_key(FileData *fd, Key *key)
2798 link_list(fd, &(key->block));
2800 key->adt = newdataadr(fd, key->adt);
2801 direct_link_animdata(fd, key->adt);
2803 key->refkey= newdataadr(fd, key->refkey);
2805 for (kb = key->block.first; kb; kb = kb->next) {
2806 kb->data = newdataadr(fd, kb->data);
2808 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2809 switch_endian_keyblock(key, kb);
2813 /* ************ READ mball ***************** */
2815 static void lib_link_mball(FileData *fd, Main *main)
2820 for (mb = main->mball.first; mb; mb = mb->id.next) {
2821 if (mb->id.flag & LIB_NEEDLINK) {
2822 if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
2824 for (a = 0; a < mb->totcol; a++)
2825 mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2827 mb->ipo = newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2829 mb->id.flag -= LIB_NEEDLINK;
2834 static void direct_link_mball(FileData *fd, MetaBall *mb)
2836 mb->adt = newdataadr(fd, mb->adt);
2837 direct_link_animdata(fd, mb->adt);
2839 mb->mat = newdataadr(fd, mb->mat);
2840 test_pointer_array(fd, (void **)&mb->mat);
2842 link_list(fd, &(mb->elems));
2844 mb->disp.first = mb->disp.last = NULL;
2845 mb->editelems = NULL;
2847 /* mb->edit_elems.first= mb->edit_elems.last= NULL;*/
2848 mb->lastelem = NULL;
2851 /* ************ READ WORLD ***************** */
2853 static void lib_link_world(FileData *fd, Main *main)
2859 for (wrld = main->world.first; wrld; wrld = wrld->id.next) {
2860 if (wrld->id.flag & LIB_NEEDLINK) {
2861 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2863 wrld->ipo = newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2865 for (a=0; a < MAX_MTEX; a++) {
2866 mtex = wrld->mtex[a];
2868 mtex->tex = newlibadr_us(fd, wrld->id.lib, mtex->tex);
2869 mtex->object = newlibadr(fd, wrld->id.lib, mtex->object);
2874 lib_link_ntree(fd, &wrld->id, wrld->nodetree);
2876 wrld->id.flag -= LIB_NEEDLINK;
2881 static void direct_link_world(FileData *fd, World *wrld)
2885 wrld->adt = newdataadr(fd, wrld->adt);
2886 direct_link_animdata(fd, wrld->adt);
2888 for (a = 0; a < MAX_MTEX; a++) {
2889 wrld->mtex[a] = newdataadr(fd, wrld->mtex[a]);
2892 wrld->nodetree = newdataadr(fd, wrld->nodetree);
2894 direct_link_nodetree(fd, wrld->nodetree);
2896 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2900 /* ************ READ VFONT ***************** */
2902 static void lib_link_vfont(FileData *UNUSED(fd), Main *main)
2906 for (vf = main->vfont.first; vf; vf = vf->id.next) {
2907 if (vf->id.flag & LIB_NEEDLINK) {
2908 vf->id.flag -= LIB_NEEDLINK;
2913 static void direct_link_vfont(FileData *fd, VFont *vf)
2916 vf->packedfile = direct_link_packedfile(fd, vf->packedfile);
2919 /* ************ READ TEXT ****************** */
2921 static void lib_link_text(FileData *UNUSED(fd), Main *main)
2925 for (text = main->text.first; text; text = text->id.next) {
2926 if (text->id.flag & LIB_NEEDLINK) {
2927 text->id.flag -= LIB_NEEDLINK;
2932 static void direct_link_text(FileData *fd, Text *text)
2936 text->name = newdataadr(fd, text->name);
2938 text->undo_pos = -1;
2939 text->undo_len = TXT_INIT_UNDO;
2940 text->undo_buf = MEM_mallocN(text->undo_len, "undo buf");
2942 text->compiled = NULL;
2945 if (text->flags & TXT_ISEXT) {
2946 BKE_text_reload(text);
2951 link_list(fd, &text->lines);
2952 link_list(fd, &text->markers);
2954 text->curl = newdataadr(fd, text->curl);
2955 text->sell = newdataadr(fd, text->sell);
2957 for (ln = text->lines.first; ln; ln = ln->next) {
2958 ln->line = newdataadr(fd, ln->line);
2961 if (ln->len != (int) strlen(ln->line)) {
2962 printf("Error loading text, line lengths differ\n");
2963 ln->len = strlen(ln->line);
2967 text->flags = (text->flags) & ~TXT_ISEXT;
2972 /* ************ READ IMAGE ***************** */
2974 static void lib_link_image(FileData *fd, Main *main)
2978 for (ima = main->image.first; ima; ima = ima->id.next) {
2979 if (ima->id.flag & LIB_NEEDLINK) {
2980 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2982 ima->id.flag -= LIB_NEEDLINK;
2987 static void link_ibuf_list(FileData *fd, ListBase *lb)
2991 if (lb->first == NULL) return;
2993 lb->first = newimaadr(fd, lb->first);
2997 ln->next = newimaadr(fd, ln->next);
3005 static void direct_link_image(FileData *fd, Image *ima)
3007 /* for undo system, pointers could be restored */
3009 link_ibuf_list(fd, &ima->ibufs);
3011 ima->ibufs.first = ima->ibufs.last = NULL;
3013 /* if not restored, we keep the binded opengl index */
3014 if (ima->ibufs.first == NULL) {
3016 ima->gputexture = NULL;
3021 ima->repbind = NULL;
3023 /* undo system, try to restore render buffers */
3027 for (a = 0; a < IMA_MAX_RENDER_SLOT; a++)
3028 ima->renders[a] = newimaadr(fd, ima->renders[a]);
3031 memset(ima->renders, 0, sizeof(ima->renders));
3032 ima->last_render_slot = ima->render_slot;
3035 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
3036 ima->preview = direct_link_preview_image(fd, ima->preview);
3041 /* ************ READ CURVE ***************** */
3043 static void lib_link_curve(FileData *fd, Main *main)
3048 for (cu = main->curve.first; cu; cu = cu->id.next) {
3049 if (cu->id.flag & LIB_NEEDLINK) {
3050 if (cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
3052 for (a = 0; a < cu->totcol; a++)
3053 cu->mat[a] = newlibadr_us(fd, cu->id.lib, cu->mat[a]);
3055 cu->bevobj = newlibadr(fd, cu->id.lib, cu->bevobj);
3056 cu->taperobj = newlibadr(fd, cu->id.lib, cu->taperobj);
3057 cu->textoncurve = newlibadr(fd, cu->id.lib, cu->textoncurve);
3058 cu->vfont = newlibadr_us(fd, cu->id.lib, cu->vfont);
3059 cu->vfontb = newlibadr_us(fd, cu->id.lib, cu->vfontb);
3060 cu->vfonti = newlibadr_us(fd, cu->id.lib, cu->vfonti);
3061 cu->vfontbi = newlibadr_us(fd, cu->id.lib, cu->vfontbi);
3063 cu->ipo = newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
3064 cu->key = newlibadr_us(fd, cu->id.lib, cu->key);
3066 cu->id.flag -= LIB_NEEDLINK;
3072 static void switch_endian_knots(Nurb *nu)
3079 SWITCH_INT(nu->knotsu[len]);
3085 SWITCH_INT(nu->knotsv[len]);
3090 static void direct_link_curve(FileData *fd, Curve *cu)
3095 cu->adt= newdataadr(fd, cu->adt);
3096 direct_link_animdata(fd, cu->adt);
3098 cu->mat = newdataadr(fd, cu->mat);
3099 test_pointer_array(fd, (void **)&cu->mat);
3100 cu->str = newdataadr(fd, cu->str);
3101 cu->strinfo= newdataadr(fd, cu->strinfo);
3102 cu->tb = newdataadr(fd, cu->tb);
3104 if (cu->vfont == NULL) link_list(fd, &(cu->nurb));
3106 cu->nurb.first=cu->nurb.last= NULL;
3108 tb = MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
3110 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
3118 cu->tb[0].w = cu->linewidth;
3120 if (cu->wordspace == 0.0f) cu->wordspace = 1.0f;
3123 cu->bev.first = cu->bev.last = NULL;
3124 cu->disp.first = cu->disp.last = NULL;
3125 cu->editnurb = NULL;
3128 cu->editfont = NULL;
3130 for (nu = cu->nurb.first; nu; nu = nu->next) {
3131 nu->bezt = newdataadr(fd, nu->bezt);
3132 nu->bp = newdataadr(fd, nu->bp);
3133 nu->knotsu = newdataadr(fd, nu->knotsu);
3134 nu->knotsv = newdataadr(fd, nu->knotsv);
3135 if (cu->vfont == NULL) nu->charidx= nu->mat_nr;
3137 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
3138 switch_endian_knots(nu);
3144 /* ************ READ TEX ***************** */
3146 static void lib_link_texture(FileData *fd, Main *main)
3150 for (tex = main->tex.first; tex; tex = tex->id.next) {
3151 if (tex->id.flag & LIB_NEEDLINK) {
3152 if (tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
3154 tex->ima = newlibadr_us(fd, tex->id.lib, tex->ima);
3155 tex->ipo = newlibadr_us(fd, tex->id.lib, tex->ipo);
3157 tex->env->object = newlibadr(fd, tex->id.lib, tex->env->object);
3159 tex->pd->object = newlibadr(fd, tex->id.lib, tex->pd->object);
3161 tex->vd->object = newlibadr(fd, tex->id.lib, tex->vd->object);
3163 tex->ot->object = newlibadr(fd, tex->id.lib, tex->ot->object);
3166 lib_link_ntree(fd, &tex->id, tex->nodetree);
3168 tex->id.flag -= LIB_NEEDLINK;
3173 static void direct_link_texture(FileData *fd, Tex *tex)
3175 tex->adt = newdataadr(fd, tex->adt);
3176 direct_link_animdata(fd, tex->adt);
3178 tex->coba = newdataadr(fd, tex->coba);
3179 tex->env = newdataadr(fd, tex->env);
3181 tex->env->ima = NULL;
3182 memset(tex->env->cube, 0, 6*sizeof(void *));
3185 tex->pd = newdataadr(fd, tex->pd);
3187 tex->pd->point_tree = NULL;
3188 tex->pd->coba = newdataadr(fd, tex->pd->coba);
3189 tex->pd->falloff_curve = newdataadr(fd, tex->pd->falloff_curve);
3190 if (tex->pd->falloff_curve) {
3191 direct_link_curvemapping(fd, tex->pd->falloff_curve);
3195 tex->vd = newdataadr(fd, tex->vd);
3197 tex->vd->dataset = NULL;
3201 if (tex->type == TEX_VOXELDATA)
3202 tex->vd = MEM_callocN(sizeof(VoxelData), "direct_link_texture VoxelData");
3205 tex->ot = newdataadr(fd, tex->ot);
3207 tex->nodetree = newdataadr(fd, tex->nodetree);
3209 direct_link_nodetree(fd, tex->nodetree);
3211 tex->preview = direct_link_preview_image(fd, tex->preview);
3218 /* ************ READ MATERIAL ***************** */
3220 static void lib_link_material(FileData *fd, Main *main)