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 */
44 #include "BLI_utildefines.h"
46 # include <unistd.h> // for read close
48 # include <io.h> // for open close read
49 # include "winsock2.h"
50 # include "BLI_winstuff.h"
53 /* allow readfile to use deprecated functionality */
54 #define DNA_DEPRECATED_ALLOW
56 #include "DNA_anim_types.h"
57 #include "DNA_armature_types.h"
58 #include "DNA_actuator_types.h"
59 #include "DNA_brush_types.h"
60 #include "DNA_camera_types.h"
61 #include "DNA_cloth_types.h"
62 #include "DNA_controller_types.h"
63 #include "DNA_constraint_types.h"
64 #include "DNA_dynamicpaint_types.h"
65 #include "DNA_effect_types.h"
66 #include "DNA_fileglobal_types.h"
67 #include "DNA_genfile.h"
68 #include "DNA_group_types.h"
69 #include "DNA_gpencil_types.h"
70 #include "DNA_ipo_types.h"
71 #include "DNA_key_types.h"
72 #include "DNA_lattice_types.h"
73 #include "DNA_lamp_types.h"
74 #include "DNA_meta_types.h"
75 #include "DNA_material_types.h"
76 #include "DNA_mesh_types.h"
77 #include "DNA_meshdata_types.h"
78 #include "DNA_nla_types.h"
79 #include "DNA_node_types.h"
80 #include "DNA_object_fluidsim.h" // NT
81 #include "DNA_object_types.h"
82 #include "DNA_packedFile_types.h"
83 #include "DNA_particle_types.h"
84 #include "DNA_property_types.h"
85 #include "DNA_rigidbody_types.h"
86 #include "DNA_text_types.h"
87 #include "DNA_view3d_types.h"
88 #include "DNA_screen_types.h"
89 #include "DNA_sensor_types.h"
90 #include "DNA_sdna_types.h"
91 #include "DNA_scene_types.h"
92 #include "DNA_sequence_types.h"
93 #include "DNA_smoke_types.h"
94 #include "DNA_speaker_types.h"
95 #include "DNA_sound_types.h"
96 #include "DNA_space_types.h"
97 #include "DNA_vfont_types.h"
98 #include "DNA_world_types.h"
99 #include "DNA_movieclip_types.h"
100 #include "DNA_mask_types.h"
102 #include "MEM_guardedalloc.h"
104 #include "BLI_utildefines.h"
105 #include "BLI_endian_switch.h"
106 #include "BLI_blenlib.h"
107 #include "BLI_math.h"
108 #include "BLI_edgehash.h"
110 #include "BLF_translation.h"
112 #include "BKE_anim.h"
113 #include "BKE_action.h"
114 #include "BKE_armature.h"
115 #include "BKE_brush.h"
116 #include "BKE_colortools.h"
117 #include "BKE_constraint.h"
118 #include "BKE_context.h"
119 #include "BKE_curve.h"
120 #include "BKE_deform.h"
121 #include "BKE_depsgraph.h"
122 #include "BKE_effect.h"
123 #include "BKE_fcurve.h"
124 #include "BKE_global.h" // for G
125 #include "BKE_group.h"
126 #include "BKE_image.h"
127 #include "BKE_lattice.h"
128 #include "BKE_library.h" // for which_libbase
129 #include "BKE_idcode.h"
130 #include "BKE_material.h"
131 #include "BKE_main.h" // for Main
132 #include "BKE_mesh.h" // for ME_ defines (patching)
133 #include "BKE_modifier.h"
134 #include "BKE_multires.h"
135 #include "BKE_node.h" // for tree type defines
136 #include "BKE_ocean.h"
137 #include "BKE_object.h"
138 #include "BKE_paint.h"
139 #include "BKE_particle.h"
140 #include "BKE_pointcache.h"
141 #include "BKE_property.h" // for BKE_bproperty_object_get
142 #include "BKE_report.h"
143 #include "BKE_sca.h" // for init_actuator
144 #include "BKE_scene.h"
145 #include "BKE_screen.h"
146 #include "BKE_sequencer.h"
147 #include "BKE_text.h" // for txt_extended_ascii_as_utf8
148 #include "BKE_tracking.h"
149 #include "BKE_sound.h"
151 #include "IMB_imbuf.h" // for proxy / timecode versioning stuff
153 #include "NOD_socket.h"
155 #include "BLO_readfile.h"
156 #include "BLO_undofile.h"
157 #include "BLO_blend_defs.h"
159 #include "RE_engine.h"
161 #include "readfile.h"
163 #include "PIL_time.h"
168 * Remark: still a weak point is the newaddress() function, that doesnt solve reading from
169 * multiple files at the same time
171 * (added remark: oh, i thought that was solved? will look at that... (ton)
174 * - Existing Library (Main) push or free
175 * - allocate new Main
178 * - for each LibBlock
182 * - attach ID's to it
184 * - read associated 'direct data'
185 * - link direct data (internal and to LibBlock)
187 * - read USER data, only when indicated (file is ~/X.XX/startup.blend)
189 * - per Library (per Main)
192 * - find LibBlocks and attach IDs to Main
193 * - if external LibBlock
194 * - search all Main's
195 * - or it's already read,
200 * - read associated direct data
201 * - link direct data (internal and to LibBlock)
203 * - per Library with unread LibBlocks
208 * - read associated direct data
209 * - link direct data (internal and to LibBlock)
212 * - link all LibBlocks and indirect pointers to libblocks
213 * - initialize FileGlobal and copy pointers to Global
218 typedef struct OldNew {
223 typedef struct OldNewMap {
225 int nentries, entriessize;
231 /* local prototypes */
232 static void *read_struct(FileData *fd, BHead *bh, const char *blockname);
233 static void direct_link_modifiers(FileData *fd, ListBase *lb);
234 static void convert_tface_mt(FileData *fd, Main *main);
236 /* this function ensures that reports are printed,
237 * in the case of libraray linking errors this is important!
239 * bit kludge but better then doubling up on prints,
240 * we could alternatively have a versions of a report function which forces printing - campbell
243 static void BKE_reportf_wrap(ReportList *reports, ReportType type, const char *format, ...)
245 __attribute__ ((format(printf, 3, 4)))
248 static void BKE_reportf_wrap(ReportList *reports, ReportType type, const char *format, ...)
250 char fixed_buf[1024]; /* should be long enough */
254 va_start(args, format);
255 vsnprintf(fixed_buf, sizeof(fixed_buf), format, args);
258 fixed_buf[sizeof(fixed_buf) - 1] = '\0';
260 BKE_report(reports, type, fixed_buf);
262 if (G.background == 0) {
263 printf("%s\n", fixed_buf);
267 static OldNewMap *oldnewmap_new(void)
269 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
271 onm->entriessize = 1024;
272 onm->entries = MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
277 static int verg_oldnewmap(const void *v1, const void *v2)
279 const struct OldNew *x1=v1, *x2=v2;
281 if (x1->old > x2->old) return 1;
282 else if (x1->old < x2->old) return -1;
287 static void oldnewmap_sort(FileData *fd)
289 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
290 fd->libmap->sorted = 1;
293 /* nr is zero for data, and ID code for libdata */
294 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
298 if (oldaddr==NULL || newaddr==NULL) return;
300 if (onm->nentries == onm->entriessize) {
301 int osize = onm->entriessize;
302 OldNew *oentries = onm->entries;
304 onm->entriessize *= 2;
305 onm->entries = MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
307 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
311 entry = &onm->entries[onm->nentries++];
312 entry->old = oldaddr;
313 entry->newp = newaddr;
317 void blo_do_versions_oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
319 oldnewmap_insert(onm, oldaddr, newaddr, nr);
322 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
326 if (addr == NULL) return NULL;
328 if (onm->lasthit < onm->nentries-1) {
329 OldNew *entry = &onm->entries[++onm->lasthit];
331 if (entry->old == addr) {
337 for (i = 0; i < onm->nentries; i++) {
338 OldNew *entry = &onm->entries[i];
340 if (entry->old == addr) {
351 /* for libdata, nr has ID code, no increment */
352 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
358 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
360 OldNew entry_s, *entry;
364 entry = bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
366 ID *id = entry->newp;
368 if (id && (!lib || id->lib)) {
374 /* note, this can be a bottle neck when loading some files */
375 unsigned int nentries = (unsigned int)onm->nentries;
379 for (i = 0, entry = onm->entries; i < nentries; i++, entry++) {
380 if (entry->old == addr) {
381 ID *id = entry->newp;
382 if (id && (!lib || id->lib)) {
392 static void oldnewmap_free_unused(OldNewMap *onm)
396 for (i = 0; i < onm->nentries; i++) {
397 OldNew *entry = &onm->entries[i];
398 if (entry->nr == 0) {
399 MEM_freeN(entry->newp);
405 static void oldnewmap_clear(OldNewMap *onm)
411 static void oldnewmap_free(OldNewMap *onm)
413 MEM_freeN(onm->entries);
419 static void read_libraries(FileData *basefd, ListBase *mainlist);
421 /* ************ help functions ***************** */
423 static void add_main_to_main(Main *mainvar, Main *from)
425 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
428 set_listbasepointers(mainvar, lbarray);
429 a = set_listbasepointers(from, fromarray);
431 BLI_movelisttolist(lbarray[a], fromarray[a]);
435 void blo_join_main(ListBase *mainlist)
437 Main *tojoin, *mainl;
439 mainl = mainlist->first;
440 while ((tojoin = mainl->next)) {
441 add_main_to_main(mainl, tojoin);
442 BLI_remlink(mainlist, tojoin);
447 static void split_libdata(ListBase *lb, Main *first)
459 if (mainvar->curlib == id->lib) {
460 lbn= which_libbase(mainvar, GS(id->name));
462 BLI_addtail(lbn, id);
465 mainvar = mainvar->next;
467 if (mainvar == NULL) printf("error split_libdata\n");
473 void blo_split_main(ListBase *mainlist, Main *main)
475 ListBase *lbarray[MAX_LIBARRAY];
479 mainlist->first = mainlist->last = main;
482 if (main->library.first == NULL)
485 for (lib = main->library.first; lib; lib = lib->id.next) {
486 Main *libmain = MEM_callocN(sizeof(Main), "libmain");
487 libmain->curlib = lib;
488 BLI_addtail(mainlist, libmain);
491 i = set_listbasepointers(main, lbarray);
493 split_libdata(lbarray[i], main->next);
496 static void read_file_version(FileData *fd, Main *main)
500 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
501 if (bhead->code == GLOB) {
502 FileGlobal *fg= read_struct(fd, bhead, "Global");
504 main->subversionfile= fg->subversion;
505 main->minversionfile= fg->minversion;
506 main->minsubversionfile= fg->minsubversion;
509 else if (bhead->code == ENDB)
516 static Main *blo_find_main(FileData *fd, const char *filepath, const char *relabase)
518 ListBase *mainlist = fd->mainlist;
521 char name1[FILE_MAX];
523 BLI_strncpy(name1, filepath, sizeof(name1));
524 BLI_cleanup_path(relabase, name1);
526 // printf("blo_find_main: relabase %s\n", relabase);
527 // printf("blo_find_main: original in %s\n", filepath);
528 // printf("blo_find_main: converted to %s\n", name1);
530 for (m = mainlist->first; m; m = m->next) {
531 char *libname = (m->curlib) ? m->curlib->filepath : m->name;
533 if (BLI_path_cmp(name1, libname) == 0) {
534 if (G.debug & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
539 m = MEM_callocN(sizeof(Main), "find_main");
540 BLI_addtail(mainlist, m);
542 lib = BKE_libblock_alloc(&m->library, ID_LI, "lib");
543 BLI_strncpy(lib->name, filepath, sizeof(lib->name));
544 BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
548 read_file_version(fd, m);
550 if (G.debug & G_DEBUG) printf("blo_find_main: added new lib %s\n", filepath);
555 /* ************ FILE PARSING ****************** */
557 static void switch_endian_bh4(BHead4 *bhead)
559 /* the ID_.. codes */
560 if ((bhead->code & 0xFFFF)==0) bhead->code >>= 16;
562 if (bhead->code != ENDB) {
563 BLI_endian_switch_int32(&bhead->len);
564 BLI_endian_switch_int32(&bhead->SDNAnr);
565 BLI_endian_switch_int32(&bhead->nr);
569 static void switch_endian_bh8(BHead8 *bhead)
571 /* the ID_.. codes */
572 if ((bhead->code & 0xFFFF)==0) bhead->code >>= 16;
574 if (bhead->code != ENDB) {
575 BLI_endian_switch_int32(&bhead->len);
576 BLI_endian_switch_int32(&bhead->SDNAnr);
577 BLI_endian_switch_int32(&bhead->nr);
581 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
583 BHead4 *bhead4 = (BHead4 *) bhead;
586 bhead4->code = bhead8->code;
587 bhead4->len = bhead8->len;
589 if (bhead4->code != ENDB) {
590 /* perform a endian swap on 64bit pointers, otherwise the pointer might map to zero
591 * 0x0000000000000000000012345678 would become 0x12345678000000000000000000000000
593 if (do_endian_swap) {
594 BLI_endian_switch_int64(&bhead8->old);
597 /* this patch is to avoid a long long being read from not-eight aligned positions
598 * is necessary on any modern 64bit architecture) */
599 memcpy(&old, &bhead8->old, 8);
600 bhead4->old = (int) (old >> 3);
602 bhead4->SDNAnr = bhead8->SDNAnr;
603 bhead4->nr = bhead8->nr;
607 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
609 BHead8 *bhead8 = (BHead8 *) bhead;
611 bhead8->code = bhead4->code;
612 bhead8->len = bhead4->len;
614 if (bhead8->code != ENDB) {
615 bhead8->old = bhead4->old;
616 bhead8->SDNAnr = bhead4->SDNAnr;
617 bhead8->nr= bhead4->nr;
621 static BHeadN *get_bhead(FileData *fd)
623 BHeadN *new_bhead = NULL;
628 /* initializing to zero isn't strictly needed but shuts valgrind up
629 * since uninitialized memory gets compared */
634 /* First read the bhead structure.
635 * Depending on the platform the file was written on this can
636 * be a big or little endian BHead4 or BHead8 structure.
638 * As usual 'ENDB' (the last *partial* bhead of the file)
639 * needs some special handling. We don't want to EOF just yet.
641 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
643 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
645 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
646 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
647 switch_endian_bh4(&bhead4);
650 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
651 bh8_from_bh4(&bhead, &bhead4);
654 memcpy(&bhead, &bhead4, sizeof(bhead));
664 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
666 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
667 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
668 switch_endian_bh8(&bhead8);
671 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
672 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
675 memcpy(&bhead, &bhead8, sizeof(bhead));
684 /* make sure people are not trying to pass bad blend files */
685 if (bhead.len < 0) fd->eof = 1;
687 /* bhead now contains the (converted) bhead structure. Now read
688 * the associated data and put everything in a BHeadN (creative naming !)
691 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
693 new_bhead->next = new_bhead->prev = NULL;
694 new_bhead->bhead = bhead;
696 readsize = fd->read(fd, new_bhead + 1, bhead.len);
698 if (readsize != bhead.len) {
700 MEM_freeN(new_bhead);
711 /* We've read a new block. Now add it to the list
715 BLI_addtail(&fd->listbase, new_bhead);
721 BHead *blo_firstbhead(FileData *fd)
727 * Read in a new block if necessary
729 new_bhead = fd->listbase.first;
730 if (new_bhead == NULL) {
731 new_bhead = get_bhead(fd);
735 bhead = &new_bhead->bhead;
741 BHead *blo_prevbhead(FileData *UNUSED(fd), BHead *thisblock)
743 BHeadN *bheadn = (BHeadN *) (((char *) thisblock) - offsetof(BHeadN, bhead));
744 BHeadN *prev = bheadn->prev;
746 return (prev) ? &prev->bhead : NULL;
749 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
751 BHeadN *new_bhead = NULL;
755 /* bhead is actually a sub part of BHeadN
756 * We calculate the BHeadN pointer from the BHead pointer below */
757 new_bhead = (BHeadN *) (((char *) thisblock) - offsetof(BHeadN, bhead));
759 /* get the next BHeadN. If it doesn't exist we read in the next one */
760 new_bhead = new_bhead->next;
761 if (new_bhead == NULL) {
762 new_bhead = get_bhead(fd);
767 /* here we do the reverse:
768 * go from the BHeadN pointer to the BHead pointer */
769 bhead = &new_bhead->bhead;
775 static void decode_blender_header(FileData *fd)
777 char header[SIZEOFBLENDERHEADER], num[4];
780 /* read in the header data */
781 readsize = fd->read(fd, header, sizeof(header));
783 if (readsize == sizeof(header)) {
784 if (strncmp(header, "BLENDER", 7) == 0) {
785 int remove_this_endian_test = 1;
787 fd->flags |= FD_FLAGS_FILE_OK;
789 /* what size are pointers in the file ? */
790 if (header[7]=='_') {
791 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
792 if (sizeof(void *) != 4) {
793 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
797 if (sizeof(void *) != 8) {
798 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
802 /* is the file saved in a different endian
805 if (((((char *)&remove_this_endian_test)[0] == 1) ? L_ENDIAN : B_ENDIAN) != ((header[8] == 'v') ? L_ENDIAN : B_ENDIAN)) {
806 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
809 /* get the version number */
810 memcpy(num, header + 9, 3);
812 fd->fileversion = atoi(num);
817 static int read_file_dna(FileData *fd)
821 for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
822 if (bhead->code == DNA1) {
823 const bool do_endian_swap = (fd->flags & FD_FLAGS_SWITCH_ENDIAN) != 0;
825 fd->filesdna = DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
827 fd->compflags = DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
828 /* used to retrieve ID names from (bhead+1) */
829 fd->id_name_offs = DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
834 else if (bhead->code == ENDB)
841 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
843 int readsize = read(filedata->filedes, buffer, size);
849 filedata->seek += readsize;
855 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
857 int readsize = gzread(filedata->gzfiledes, buffer, size);
863 filedata->seek += readsize;
869 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
871 /* don't read more bytes then there are available in the buffer */
872 int readsize = (int)MIN2(size, (unsigned int)(filedata->buffersize - filedata->seek));
874 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
875 filedata->seek += readsize;
880 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
882 static unsigned int seek = (1<<30); /* the current position */
883 static unsigned int offset = 0; /* size of previous chunks */
884 static MemFileChunk *chunk = NULL;
885 unsigned int chunkoffset, readsize, totread;
887 if (size == 0) return 0;
889 if (seek != (unsigned int)filedata->seek) {
890 chunk = filedata->memfile->chunks.first;
894 if (seek + chunk->size > (unsigned) filedata->seek) break;
899 seek = filedata->seek;
906 /* first check if it's on the end if current chunk */
907 if (seek-offset == chunk->size) {
908 offset += chunk->size;
912 /* debug, should never happen */
914 printf("illegal read, chunk zero\n");
918 chunkoffset = seek-offset;
919 readsize = size-totread;
921 /* data can be spread over multiple chunks, so clamp size
922 * to within this chunk, and then it will read further in
924 if (chunkoffset+readsize > chunk->size)
925 readsize= chunk->size-chunkoffset;
927 memcpy((char *)buffer + totread, chunk->buf + chunkoffset, readsize);
929 filedata->seek += readsize;
931 } while (totread < size);
939 static FileData *filedata_new(void)
941 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
944 fd->gzfiledes = NULL;
946 /* XXX, this doesn't need to be done all the time,
947 * but it keeps us re-entrant, remove once we have
948 * a lib that provides a nice lock. - zr
950 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, false);
952 fd->datamap = oldnewmap_new();
953 fd->globmap = oldnewmap_new();
954 fd->libmap = oldnewmap_new();
959 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
961 decode_blender_header(fd);
963 if (fd->flags & FD_FLAGS_FILE_OK) {
964 if (!read_file_dna(fd)) {
965 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file '%s', incomplete", fd->relabase);
966 blo_freefiledata(fd);
971 BKE_reportf(reports, RPT_ERROR, "Failed to read blend file '%s', not a blend file", fd->relabase);
972 blo_freefiledata(fd);
979 /* cannot be called with relative paths anymore! */
980 /* on each new library added, it now checks for the current FileData and expands relativeness */
981 FileData *blo_openblenderfile(const char *filepath, ReportList *reports)
985 gzfile = BLI_gzopen(filepath, "rb");
987 if (gzfile == (gzFile)Z_NULL) {
988 BKE_reportf(reports, RPT_WARNING, "Unable to open '%s': %s",
989 filepath, errno ? strerror(errno) : TIP_("unknown error reading file"));
993 FileData *fd = filedata_new();
994 fd->gzfiledes = gzfile;
995 fd->read = fd_read_gzip_from_file;
997 /* needed for library_append and read_libraries */
998 BLI_strncpy(fd->relabase, filepath, sizeof(fd->relabase));
1000 return blo_decode_and_check(fd, reports);
1004 static int fd_read_gzip_from_memory(FileData *filedata, void *buffer, unsigned int size)
1008 filedata->strm.next_out = (Bytef *) buffer;
1009 filedata->strm.avail_out = size;
1011 // Inflate another chunk.
1012 err = inflate (&filedata->strm, Z_SYNC_FLUSH);
1014 if (err == Z_STREAM_END) {
1017 else if (err != Z_OK) {
1018 printf("fd_read_gzip_from_memory: zlib error\n");
1022 filedata->seek += size;
1027 static int fd_read_gzip_from_memory_init(FileData *fd)
1030 fd->strm.next_in = (Bytef *) fd->buffer;
1031 fd->strm.avail_in = fd->buffersize;
1032 fd->strm.total_out = 0;
1033 fd->strm.zalloc = Z_NULL;
1034 fd->strm.zfree = Z_NULL;
1036 if (inflateInit2(&fd->strm, (16+MAX_WBITS)) != Z_OK)
1039 fd->read = fd_read_gzip_from_memory;
1044 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
1046 if (!mem || memsize<SIZEOFBLENDERHEADER) {
1047 BKE_report(reports, RPT_WARNING, (mem) ? TIP_("Unable to read"): TIP_("Unable to open"));
1051 FileData *fd = filedata_new();
1055 fd->buffersize = memsize;
1058 if (cp[0] == 0x1f && cp[1] == 0x8b) {
1059 if (0 == fd_read_gzip_from_memory_init(fd)) {
1060 blo_freefiledata(fd);
1065 fd->read = fd_read_from_memory;
1067 fd->flags |= FD_FLAGS_NOT_MY_BUFFER;
1069 return blo_decode_and_check(fd, reports);
1073 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
1076 BKE_report(reports, RPT_WARNING, "Unable to open blend <memory>");
1080 FileData *fd = filedata_new();
1081 fd->memfile = memfile;
1083 fd->read = fd_read_from_memfile;
1084 fd->flags |= FD_FLAGS_NOT_MY_BUFFER;
1086 return blo_decode_and_check(fd, reports);
1091 void blo_freefiledata(FileData *fd)
1094 if (fd->filedes != -1) {
1098 if (fd->gzfiledes != NULL) {
1099 gzclose(fd->gzfiledes);
1102 if (fd->strm.next_in) {
1103 if (inflateEnd (&fd->strm) != Z_OK) {
1104 printf("close gzip stream error\n");
1108 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1109 MEM_freeN(fd->buffer);
1113 // Free all BHeadN data blocks
1114 BLI_freelistN(&fd->listbase);
1117 DNA_sdna_free(fd->memsdna);
1119 DNA_sdna_free(fd->filesdna);
1121 MEM_freeN(fd->compflags);
1124 oldnewmap_free(fd->datamap);
1126 oldnewmap_free(fd->globmap);
1128 oldnewmap_free(fd->imamap);
1129 if (fd->movieclipmap)
1130 oldnewmap_free(fd->movieclipmap);
1132 oldnewmap_free(fd->packedmap);
1133 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1134 oldnewmap_free(fd->libmap);
1136 MEM_freeN(fd->bheadmap);
1142 /* ************ DIV ****************** */
1144 int BLO_has_bfile_extension(const char *str)
1146 return (BLI_testextensie(str, ".ble") ||
1147 BLI_testextensie(str, ".blend") ||
1148 BLI_testextensie(str, ".blend.gz"));
1151 int BLO_is_a_library(const char *path, char *dir, char *group)
1153 /* return ok when a blenderfile, in dir is the filename,
1154 * in group the type of libdata
1161 if (len < 7) return 0;
1162 if ((dir[len - 1] != '/') && (dir[len - 1] != '\\')) return 0;
1165 dir[len - 1] = '\0';
1167 /* Find the last slash */
1168 fd = (char *)BLI_last_slash(dir);
1170 if (fd == NULL) return 0;
1172 if (BLO_has_bfile_extension(fd+1)) {
1173 /* the last part of the dir is a .blend file, no group follows */
1174 *fd = '/'; /* put back the removed slash separating the dir and the .blend file name */
1177 const char * const gp = fd + 1; // in case we have a .blend file, gp points to the group
1179 /* Find the last slash */
1180 fd = (char *)BLI_last_slash(dir);
1181 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
1183 /* now we know that we are in a blend file and it is safe to
1184 * assume that gp actually points to a group */
1185 if (strcmp("Screen", gp) != 0)
1186 BLI_strncpy(group, gp, GROUP_MAX);
1191 /* ************** OLD POINTERS ******************* */
1193 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1195 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1198 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1200 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1203 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1205 if (fd->imamap && adr)
1206 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1210 static void *newmclipadr(FileData *fd, void *adr) /* used to restore movie clip data after undo */
1212 if (fd->movieclipmap && adr)
1213 return oldnewmap_lookup_and_inc(fd->movieclipmap, adr);
1217 static void *newpackedadr(FileData *fd, void *adr) /* used to restore packed data after undo */
1219 if (fd->packedmap && adr)
1220 return oldnewmap_lookup_and_inc(fd->packedmap, adr);
1222 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1226 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1228 return oldnewmap_liblookup(fd->libmap, adr, lib);
1231 void *blo_do_versions_newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1233 return newlibadr(fd, lib, adr);
1236 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1238 ID *id = newlibadr(fd, lib, adr);
1246 void *blo_do_versions_newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1248 return newlibadr_us(fd, lib, adr);
1251 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1255 for (i = 0; i < fd->libmap->nentries; i++) {
1256 OldNew *entry = &fd->libmap->entries[i];
1258 if (old==entry->newp && entry->nr==ID_ID) {
1260 if (new) entry->nr = GS( ((ID *)new)->name );
1265 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1269 for (mainptr = mainlist->first; mainptr; mainptr = mainptr->next) {
1272 if (mainptr->curlib)
1273 fd = mainptr->curlib->filedata;
1278 change_idid_adr_fd(fd, old, new);
1283 /* lib linked proxy objects point to our local data, we need
1284 * to clear that pointer before reading the undo memfile since
1285 * the object might be removed, it is set again in reading
1286 * if the local object still exists */
1287 void blo_clear_proxy_pointers_from_lib(Main *oldmain)
1289 Object *ob = oldmain->object.first;
1291 for (; ob; ob= ob->id.next) {
1293 ob->proxy_from = NULL;
1297 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1299 Image *ima = oldmain->image.first;
1300 Scene *sce = oldmain->scene.first;
1303 fd->imamap = oldnewmap_new();
1305 for (; ima; ima = ima->id.next) {
1306 Link *ibuf = ima->ibufs.first;
1307 for (; ibuf; ibuf = ibuf->next)
1308 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1309 if (ima->gputexture)
1310 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1311 for (a=0; a < IMA_MAX_RENDER_SLOT; a++)
1312 if (ima->renders[a])
1313 oldnewmap_insert(fd->imamap, ima->renders[a], ima->renders[a], 0);
1315 for (; sce; sce = sce->id.next) {
1316 if (sce->nodetree) {
1318 for (node = sce->nodetree->nodes.first; node; node = node->next)
1319 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1324 /* set old main image ibufs to zero if it has been restored */
1325 /* this works because freeing old main only happens after this call */
1326 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1328 OldNew *entry = fd->imamap->entries;
1329 Image *ima = oldmain->image.first;
1330 Scene *sce = oldmain->scene.first;
1333 /* used entries were restored, so we put them to zero */
1334 for (i = 0; i < fd->imamap->nentries; i++, entry++) {
1339 for (; ima; ima = ima->id.next) {
1342 /* this mirrors direct_link_image */
1343 for (ibuf = ima->ibufs.first; ibuf; ibuf = next) {
1345 if (NULL == newimaadr(fd, ibuf)) { /* so was restored */
1346 BLI_remlink(&ima->ibufs, ibuf);
1348 ima->tpageflag &= ~IMA_GLBIND_IS_DATA;
1349 ima->gputexture = NULL;
1352 for (i = 0; i < IMA_MAX_RENDER_SLOT; i++)
1353 ima->renders[i] = newimaadr(fd, ima->renders[i]);
1355 ima->gputexture = newimaadr(fd, ima->gputexture);
1357 for (; sce; sce = sce->id.next) {
1358 if (sce->nodetree) {
1360 for (node = sce->nodetree->nodes.first; node; node = node->next)
1361 node->preview = newimaadr(fd, node->preview);
1366 void blo_make_movieclip_pointer_map(FileData *fd, Main *oldmain)
1368 MovieClip *clip = oldmain->movieclip.first;
1369 Scene *sce = oldmain->scene.first;
1371 fd->movieclipmap = oldnewmap_new();
1373 for (; clip; clip = clip->id.next) {
1375 oldnewmap_insert(fd->movieclipmap, clip->cache, clip->cache, 0);
1377 if (clip->tracking.camera.intrinsics)
1378 oldnewmap_insert(fd->movieclipmap, clip->tracking.camera.intrinsics, clip->tracking.camera.intrinsics, 0);
1381 for (; sce; sce = sce->id.next) {
1382 if (sce->nodetree) {
1384 for (node = sce->nodetree->nodes.first; node; node= node->next)
1385 if (node->type == CMP_NODE_MOVIEDISTORTION)
1386 oldnewmap_insert(fd->movieclipmap, node->storage, node->storage, 0);
1391 /* set old main movie clips caches to zero if it has been restored */
1392 /* this works because freeing old main only happens after this call */
1393 void blo_end_movieclip_pointer_map(FileData *fd, Main *oldmain)
1395 OldNew *entry = fd->movieclipmap->entries;
1396 MovieClip *clip = oldmain->movieclip.first;
1397 Scene *sce = oldmain->scene.first;
1400 /* used entries were restored, so we put them to zero */
1401 for (i=0; i < fd->movieclipmap->nentries; i++, entry++) {
1406 for (; clip; clip = clip->id.next) {
1407 clip->cache = newmclipadr(fd, clip->cache);
1408 clip->tracking.camera.intrinsics = newmclipadr(fd, clip->tracking.camera.intrinsics);
1411 for (; sce; sce = sce->id.next) {
1412 if (sce->nodetree) {
1414 for (node = sce->nodetree->nodes.first; node; node = node->next)
1415 if (node->type == CMP_NODE_MOVIEDISTORTION)
1416 node->storage = newmclipadr(fd, node->storage);
1421 /* XXX disabled this feature - packed files also belong in temp saves and quit.blend, to make restore work */
1423 static void insert_packedmap(FileData *fd, PackedFile *pf)
1425 oldnewmap_insert(fd->packedmap, pf, pf, 0);
1426 oldnewmap_insert(fd->packedmap, pf->data, pf->data, 0);
1429 void blo_make_packed_pointer_map(FileData *fd, Main *oldmain)
1436 fd->packedmap = oldnewmap_new();
1438 for (ima = oldmain->image.first; ima; ima = ima->id.next)
1439 if (ima->packedfile)
1440 insert_packedmap(fd, ima->packedfile);
1442 for (vfont = oldmain->vfont.first; vfont; vfont = vfont->id.next)
1443 if (vfont->packedfile)
1444 insert_packedmap(fd, vfont->packedfile);
1446 for (sound = oldmain->sound.first; sound; sound = sound->id.next)
1447 if (sound->packedfile)
1448 insert_packedmap(fd, sound->packedfile);
1450 for (lib = oldmain->library.first; lib; lib = lib->id.next)
1451 if (lib->packedfile)
1452 insert_packedmap(fd, lib->packedfile);
1456 /* set old main packed data to zero if it has been restored */
1457 /* this works because freeing old main only happens after this call */
1458 void blo_end_packed_pointer_map(FileData *fd, Main *oldmain)
1464 OldNew *entry = fd->packedmap->entries;
1467 /* used entries were restored, so we put them to zero */
1468 for (i=0; i < fd->packedmap->nentries; i++, entry++) {
1473 for (ima = oldmain->image.first; ima; ima = ima->id.next)
1474 ima->packedfile = newpackedadr(fd, ima->packedfile);
1476 for (vfont = oldmain->vfont.first; vfont; vfont = vfont->id.next)
1477 vfont->packedfile = newpackedadr(fd, vfont->packedfile);
1479 for (sound = oldmain->sound.first; sound; sound = sound->id.next)
1480 sound->packedfile = newpackedadr(fd, sound->packedfile);
1482 for (lib = oldmain->library.first; lib; lib = lib->id.next)
1483 lib->packedfile = newpackedadr(fd, lib->packedfile);
1487 /* undo file support: add all library pointers in lookup */
1488 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1490 Main *ptr = mainlist->first;
1491 ListBase *lbarray[MAX_LIBARRAY];
1493 for (ptr = ptr->next; ptr; ptr = ptr->next) {
1494 int i = set_listbasepointers(ptr, lbarray);
1497 for (id = lbarray[i]->first; id; id = id->next)
1498 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1504 /* ********** END OLD POINTERS ****************** */
1505 /* ********** READ FILE ****************** */
1507 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1509 int blocksize, nblocks;
1512 data = (char *)(bhead+1);
1513 blocksize = filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1515 nblocks = bhead->nr;
1517 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1523 static void *read_struct(FileData *fd, BHead *bh, const char *blockname)
1528 /* switch is based on file dna */
1529 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1530 switch_endian_structs(fd->filesdna, bh);
1532 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1533 if (fd->compflags[bh->SDNAnr] == 2) {
1534 temp = DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1537 temp = MEM_mallocN(bh->len, blockname);
1538 memcpy(temp, (bh+1), bh->len);
1546 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1550 if (lb->first == NULL) return;
1552 lb->first = newdataadr(fd, lb->first);
1556 ln->next = newdataadr(fd, ln->next);
1564 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1569 if (lb->first == NULL) return;
1570 poin = newdataadr(fd, lb->first);
1572 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1579 poin = newdataadr(fd, ln->next);
1581 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1591 static void test_pointer_array(FileData *fd, void **mat)
1593 int64_t *lpoin, *lmat;
1597 /* manually convert the pointer array in
1598 * the old dna format to a pointer array in
1599 * the new dna format.
1602 len = MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1604 if (fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1605 ipoin=imat= MEM_mallocN(len * 4, "newmatar");
1609 if ((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1610 BLI_endian_switch_int64(lpoin);
1611 *ipoin = (int)((*lpoin) >> 3);
1619 if (fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1620 lpoin = lmat = MEM_mallocN(len * 8, "newmatar");
1634 /* ************ READ ID Properties *************** */
1636 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1637 static void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1639 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1644 /* since we didn't save the extra buffer, set totallen to len */
1645 prop->totallen = prop->len;
1646 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1648 array = (IDProperty *)prop->data.pointer;
1650 /* note!, idp-arrays didn't exist in 2.4x, so the pointer will be cleared
1651 * theres not really anything we can do to correct this, at least don't crash */
1652 if (array == NULL) {
1658 for (i = 0; i < prop->len; i++)
1659 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
1662 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1667 /* since we didn't save the extra buffer, set totallen to len */
1668 prop->totallen = prop->len;
1669 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1671 if (prop->subtype == IDP_GROUP) {
1672 test_pointer_array(fd, prop->data.pointer);
1673 array = prop->data.pointer;
1675 for (i = 0; i < prop->len; i++)
1676 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1678 else if (prop->subtype == IDP_DOUBLE) {
1679 if (switch_endian) {
1680 BLI_endian_switch_double_array(prop->data.pointer, prop->len);
1684 if (switch_endian) {
1685 /* also used for floats */
1686 BLI_endian_switch_int32_array(prop->data.pointer, prop->len);
1691 static void IDP_DirectLinkString(IDProperty *prop, FileData *fd)
1693 /*since we didn't save the extra string buffer, set totallen to len.*/
1694 prop->totallen = prop->len;
1695 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1698 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1700 ListBase *lb = &prop->data.group;
1705 /*Link child id properties now*/
1706 for (loop=prop->data.group.first; loop; loop=loop->next) {
1707 IDP_DirectLinkProperty(loop, switch_endian, fd);
1711 static void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1713 switch (prop->type) {
1715 IDP_DirectLinkGroup(prop, switch_endian, fd);
1718 IDP_DirectLinkString(prop, fd);
1721 IDP_DirectLinkArray(prop, switch_endian, fd);
1724 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1727 /* erg, stupid doubles. since I'm storing them
1728 * in the same field as int val; val2 in the
1729 * IDPropertyData struct, they have to deal with
1730 * endianness specifically
1732 * in theory, val and val2 would've already been swapped
1733 * if switch_endian is true, so we have to first unswap
1734 * them then reswap them as a single 64-bit entity.
1737 if (switch_endian) {
1738 BLI_endian_switch_int32(&prop->data.val);
1739 BLI_endian_switch_int32(&prop->data.val2);
1740 BLI_endian_switch_int64((int64_t *)&prop->data.val);
1748 static void IDP_LibLinkProperty(IDProperty *UNUSED(prop), int UNUSED(switch_endian), FileData *UNUSED(fd))
1752 /* ************ READ ID *************** */
1754 static void direct_link_id(FileData *fd, ID *id)
1756 /*link direct data of ID properties*/
1757 if (id->properties) {
1758 id->properties = newdataadr(fd, id->properties);
1759 if (id->properties) { /* this case means the data was written incorrectly, it should not happen */
1760 IDP_DirectLinkProperty(id->properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1765 /* ************ READ CurveMapping *************** */
1767 /* cuma itself has been read! */
1768 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1772 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1773 cumap->flag &= ~CUMA_PREMULLED;
1775 for (a = 0; a < CM_TOT; a++) {
1776 cumap->cm[a].curve = newdataadr(fd, cumap->cm[a].curve);
1777 cumap->cm[a].table = NULL;
1778 cumap->cm[a].premultable = NULL;
1782 /* ************ READ Brush *************** */
1783 /* library brush linking after fileread */
1784 static void lib_link_brush(FileData *fd, Main *main)
1788 /* only link ID pointers */
1789 for (brush = main->brush.first; brush; brush = brush->id.next) {
1790 if (brush->id.flag & LIB_NEED_LINK) {
1791 brush->id.flag -= LIB_NEED_LINK;
1793 brush->mtex.tex = newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
1794 brush->clone.image = newlibadr_us(fd, brush->id.lib, brush->clone.image);
1799 static void direct_link_brush(FileData *fd, Brush *brush)
1801 /* brush itself has been read */
1804 brush->curve = newdataadr(fd, brush->curve);
1806 direct_link_curvemapping(fd, brush->curve);
1808 BKE_brush_curve_preset(brush, CURVE_PRESET_SHARP);
1810 brush->preview = NULL;
1811 brush->icon_imbuf = NULL;
1814 static void direct_link_script(FileData *UNUSED(fd), Script *script)
1817 SCRIPT_SET_NULL(script);
1821 /* ************ READ PACKEDFILE *************** */
1823 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1825 PackedFile *pf = newpackedadr(fd, oldpf);
1828 pf->data = newpackedadr(fd, pf->data);
1834 /* ************ READ IMAGE PREVIEW *************** */
1836 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1838 PreviewImage *prv = newdataadr(fd, old_prv);
1842 for (i = 0; i < NUM_ICON_SIZES; ++i) {
1844 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1846 prv->gputexture[i] = NULL;
1853 /* ************ READ ANIMATION STUFF ***************** */
1855 /* Legacy Data Support (for Version Patching) ----------------------------- */
1857 // XXX deprecated - old animation system
1858 static void lib_link_ipo(FileData *fd, Main *main)
1862 for (ipo = main->ipo.first; ipo; ipo = ipo->id.next) {
1863 if (ipo->id.flag & LIB_NEED_LINK) {
1865 for (icu = ipo->curve.first; icu; icu = icu->next) {
1867 icu->driver->ob = newlibadr(fd, ipo->id.lib, icu->driver->ob);
1869 ipo->id.flag -= LIB_NEED_LINK;
1874 // XXX deprecated - old animation system
1875 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1879 link_list(fd, &(ipo->curve));
1881 for (icu = ipo->curve.first; icu; icu = icu->next) {
1882 icu->bezt = newdataadr(fd, icu->bezt);
1883 icu->bp = newdataadr(fd, icu->bp);
1884 icu->driver = newdataadr(fd, icu->driver);
1888 // XXX deprecated - old animation system
1889 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1891 bActionStrip *strip;
1892 bActionModifier *amod;
1894 for (strip=striplist->first; strip; strip=strip->next) {
1895 strip->object = newlibadr(fd, id->lib, strip->object);
1896 strip->act = newlibadr_us(fd, id->lib, strip->act);
1897 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1898 for (amod = strip->modifiers.first; amod; amod = amod->next)
1899 amod->ob = newlibadr(fd, id->lib, amod->ob);
1903 // XXX deprecated - old animation system
1904 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1906 bActionStrip *strip;
1908 link_list(fd, strips);
1910 for (strip = strips->first; strip; strip = strip->next)
1911 link_list(fd, &strip->modifiers);
1914 // XXX deprecated - old animation system
1915 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1917 bConstraintChannel *chan;
1919 for (chan=chanbase->first; chan; chan=chan->next) {
1920 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1924 /* Data Linking ----------------------------- */
1926 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1930 for (fcm = list->first; fcm; fcm = fcm->next) {
1931 /* data for specific modifiers */
1932 switch (fcm->type) {
1933 case FMODIFIER_TYPE_PYTHON:
1935 FMod_Python *data = (FMod_Python *)fcm->data;
1936 data->script = newlibadr(fd, id->lib, data->script);
1943 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1950 /* relink ID-block references... */
1951 for (fcu = list->first; fcu; fcu = fcu->next) {
1954 ChannelDriver *driver = fcu->driver;
1957 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1958 DRIVER_TARGETS_LOOPER(dvar)
1960 /* only relink if still used */
1961 if (tarIndex < dvar->num_targets)
1962 dtar->id = newlibadr(fd, id->lib, dtar->id);
1966 DRIVER_TARGETS_LOOPER_END
1971 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1976 /* NOTE: this assumes that link_list has already been called on the list */
1977 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1981 for (fcm = list->first; fcm; fcm = fcm->next) {
1982 /* relink general data */
1983 fcm->data = newdataadr(fd, fcm->data);
1986 /* do relinking of data for specific types */
1987 switch (fcm->type) {
1988 case FMODIFIER_TYPE_GENERATOR:
1990 FMod_Generator *data = (FMod_Generator *)fcm->data;
1992 data->coefficients = newdataadr(fd, data->coefficients);
1994 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1995 BLI_endian_switch_float_array(data->coefficients, data->arraysize);
1999 case FMODIFIER_TYPE_ENVELOPE:
2001 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
2003 data->data= newdataadr(fd, data->data);
2006 case FMODIFIER_TYPE_PYTHON:
2008 FMod_Python *data = (FMod_Python *)fcm->data;
2010 data->prop = newdataadr(fd, data->prop);
2011 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2018 /* NOTE: this assumes that link_list has already been called on the list */
2019 static void direct_link_fcurves(FileData *fd, ListBase *list)
2023 /* link F-Curve data to F-Curve again (non ID-libs) */
2024 for (fcu = list->first; fcu; fcu = fcu->next) {
2026 fcu->bezt = newdataadr(fd, fcu->bezt);
2027 fcu->fpt = newdataadr(fd, fcu->fpt);
2030 fcu->rna_path = newdataadr(fd, fcu->rna_path);
2033 fcu->grp = newdataadr(fd, fcu->grp);
2035 /* clear disabled flag - allows disabled drivers to be tried again ([#32155]),
2036 * but also means that another method for "reviving disabled F-Curves" exists
2038 fcu->flag &= ~FCURVE_DISABLED;
2041 fcu->driver= newdataadr(fd, fcu->driver);
2043 ChannelDriver *driver= fcu->driver;
2046 /* compiled expression data will need to be regenerated (old pointer may still be set here) */
2047 driver->expr_comp = NULL;
2049 /* give the driver a fresh chance - the operating environment may be different now
2050 * (addons, etc. may be different) so the driver namespace may be sane now [#32155]
2052 driver->flag &= ~DRIVER_FLAG_INVALID;
2054 /* relink variables, targets and their paths */
2055 link_list(fd, &driver->variables);
2056 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
2057 DRIVER_TARGETS_LOOPER(dvar)
2059 /* only relink the targets being used */
2060 if (tarIndex < dvar->num_targets)
2061 dtar->rna_path = newdataadr(fd, dtar->rna_path);
2063 dtar->rna_path = NULL;
2065 DRIVER_TARGETS_LOOPER_END
2070 link_list(fd, &fcu->modifiers);
2071 direct_link_fmodifiers(fd, &fcu->modifiers);
2076 static void lib_link_action(FileData *fd, Main *main)
2079 bActionChannel *chan;
2081 for (act = main->action.first; act; act = act->id.next) {
2082 if (act->id.flag & LIB_NEED_LINK) {
2083 act->id.flag -= LIB_NEED_LINK;
2085 // XXX deprecated - old animation system <<<
2086 for (chan=act->chanbase.first; chan; chan=chan->next) {
2087 chan->ipo = newlibadr_us(fd, act->id.lib, chan->ipo);
2088 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
2090 // >>> XXX deprecated - old animation system
2092 lib_link_fcurves(fd, &act->id, &act->curves);
2097 static void direct_link_action(FileData *fd, bAction *act)
2099 bActionChannel *achan; // XXX deprecated - old animation system
2102 link_list(fd, &act->curves);
2103 link_list(fd, &act->chanbase); // XXX deprecated - old animation system
2104 link_list(fd, &act->groups);
2105 link_list(fd, &act->markers);
2107 // XXX deprecated - old animation system <<<
2108 for (achan = act->chanbase.first; achan; achan=achan->next) {
2109 achan->grp = newdataadr(fd, achan->grp);
2111 link_list(fd, &achan->constraintChannels);
2113 // >>> XXX deprecated - old animation system
2115 direct_link_fcurves(fd, &act->curves);
2117 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
2118 agrp->channels.first= newdataadr(fd, agrp->channels.first);
2119 agrp->channels.last= newdataadr(fd, agrp->channels.last);
2123 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
2127 for (strip = list->first; strip; strip = strip->next) {
2128 /* check strip's children */
2129 lib_link_nladata_strips(fd, id, &strip->strips);
2131 /* check strip's F-Curves */
2132 lib_link_fcurves(fd, id, &strip->fcurves);
2134 /* reassign the counted-reference to action */
2135 strip->act = newlibadr_us(fd, id->lib, strip->act);
2137 /* fix action id-root (i.e. if it comes from a pre 2.57 .blend file) */
2138 if ((strip->act) && (strip->act->idroot == 0))
2139 strip->act->idroot = GS(id->name);
2143 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
2147 /* we only care about the NLA strips inside the tracks */
2148 for (nlt = list->first; nlt; nlt = nlt->next) {
2149 lib_link_nladata_strips(fd, id, &nlt->strips);
2153 /* This handles Animato NLA-Strips linking
2154 * NOTE: this assumes that link_list has already been called on the list
2156 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
2160 for (strip = list->first; strip; strip = strip->next) {
2161 /* strip's child strips */
2162 link_list(fd, &strip->strips);
2163 direct_link_nladata_strips(fd, &strip->strips);
2165 /* strip's F-Curves */
2166 link_list(fd, &strip->fcurves);
2167 direct_link_fcurves(fd, &strip->fcurves);
2169 /* strip's F-Modifiers */
2170 link_list(fd, &strip->modifiers);
2171 direct_link_fmodifiers(fd, &strip->modifiers);
2175 /* NOTE: this assumes that link_list has already been called on the list */
2176 static void direct_link_nladata(FileData *fd, ListBase *list)
2180 for (nlt = list->first; nlt; nlt = nlt->next) {
2181 /* relink list of strips */
2182 link_list(fd, &nlt->strips);
2184 /* relink strip data */
2185 direct_link_nladata_strips(fd, &nlt->strips);
2191 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
2196 /* here, we're only interested in the ID pointer stored in some of the paths */
2197 for (ks = list->first; ks; ks = ks->next) {
2198 for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
2199 ksp->id= newlibadr(fd, id->lib, ksp->id);
2204 /* NOTE: this assumes that link_list has already been called on the list */
2205 static void direct_link_keyingsets(FileData *fd, ListBase *list)
2210 /* link KeyingSet data to KeyingSet again (non ID-libs) */
2211 for (ks = list->first; ks; ks = ks->next) {
2213 link_list(fd, &ks->paths);
2215 for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
2217 ksp->rna_path= newdataadr(fd, ksp->rna_path);
2224 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
2229 /* link action data */
2230 adt->action= newlibadr_us(fd, id->lib, adt->action);
2231 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
2233 /* fix action id-roots (i.e. if they come from a pre 2.57 .blend file) */
2234 if ((adt->action) && (adt->action->idroot == 0))
2235 adt->action->idroot = GS(id->name);
2236 if ((adt->tmpact) && (adt->tmpact->idroot == 0))
2237 adt->tmpact->idroot = GS(id->name);
2240 lib_link_fcurves(fd, id, &adt->drivers);
2242 /* overrides don't have lib-link for now, so no need to do anything */
2245 lib_link_nladata(fd, id, &adt->nla_tracks);
2248 static void direct_link_animdata(FileData *fd, AnimData *adt)
2250 /* NOTE: must have called newdataadr already before doing this... */
2255 link_list(fd, &adt->drivers);
2256 direct_link_fcurves(fd, &adt->drivers);
2258 /* link overrides */
2262 link_list(fd, &adt->nla_tracks);
2263 direct_link_nladata(fd, &adt->nla_tracks);
2265 /* relink active strip - even though strictly speaking this should only be used
2266 * if we're in 'tweaking mode', we need to be able to have this loaded back for
2267 * undo, but also since users may not exit tweakmode before saving (#24535)
2269 // TODO: it's not really nice that anyone should be able to save the file in this
2270 // state, but it's going to be too hard to enforce this single case...
2271 adt->actstrip = newdataadr(fd, adt->actstrip);
2274 /* ************ READ MOTION PATHS *************** */
2276 /* direct data for cache */
2277 static void direct_link_motionpath(FileData *fd, bMotionPath *mpath)
2283 /* relink points cache */
2284 mpath->points = newdataadr(fd, mpath->points);
2287 /* ************ READ NODE TREE *************** */
2289 /* singe node tree (also used for material/scene trees), ntree is not NULL */
2290 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
2294 if (ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
2296 ntree->gpd = newlibadr_us(fd, id->lib, ntree->gpd);
2298 for (node = ntree->nodes.first; node; node = node->next)
2299 node->id = newlibadr_us(fd, id->lib, node->id);
2302 /* library ntree linking after fileread */
2303 static void lib_link_nodetree(FileData *fd, Main *main)
2307 /* only link ID pointers */
2308 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next) {
2309 if (ntree->id.flag & LIB_NEED_LINK) {
2310 ntree->id.flag -= LIB_NEED_LINK;
2311 lib_link_ntree(fd, &ntree->id, ntree);
2316 static void do_versions_socket_default_value(bNodeSocket *sock)
2318 bNodeSocketValueFloat *valfloat;
2319 bNodeSocketValueVector *valvector;
2320 bNodeSocketValueRGBA *valrgba;
2322 if (sock->default_value)
2325 switch (sock->type) {
2327 valfloat = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueFloat), "default socket value");
2328 valfloat->value = sock->ns.vec[0];
2329 valfloat->min = sock->ns.min;
2330 valfloat->max = sock->ns.max;
2331 valfloat->subtype = PROP_NONE;
2334 valvector = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueVector), "default socket value");
2335 copy_v3_v3(valvector->value, sock->ns.vec);
2336 valvector->min = sock->ns.min;
2337 valvector->max = sock->ns.max;
2338 valvector->subtype = PROP_NONE;
2341 valrgba = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueRGBA), "default socket value");
2342 copy_v4_v4(valrgba->value, sock->ns.vec);
2347 void blo_do_versions_nodetree_default_value(bNodeTree *ntree)
2351 for (node=ntree->nodes.first; node; node=node->next) {
2352 for (sock=node->inputs.first; sock; sock=sock->next)
2353 do_versions_socket_default_value(sock);
2354 for (sock=node->outputs.first; sock; sock=sock->next)
2355 do_versions_socket_default_value(sock);
2357 for (sock=ntree->inputs.first; sock; sock=sock->next)
2358 do_versions_socket_default_value(sock);
2359 for (sock=ntree->outputs.first; sock; sock=sock->next)
2360 do_versions_socket_default_value(sock);
2363 static void lib_nodetree_init_types_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2367 ntreeInitTypes(ntree);
2369 /* need to do this here instead of in do_versions, otherwise next function can crash */
2370 blo_do_versions_nodetree_default_value(ntree);
2372 /* XXX could be replaced by do_versions for new nodes */
2373 for (node=ntree->nodes.first; node; node=node->next)
2374 node_verify_socket_templates(ntree, node);
2377 /* updates group node socket own_index so that
2378 * external links to/from the group node are preserved.
2380 static void lib_node_do_versions_group_indices(bNode *gnode)
2382 bNodeTree *ngroup = (bNodeTree*)gnode->id;
2384 bNodeSocket *sock, *gsock, *intsock;
2387 for (sock=gnode->outputs.first; sock; sock=sock->next) {
2388 int old_index = sock->to_index;
2389 for (gsock=ngroup->outputs.first; gsock; gsock=gsock->next) {
2390 if (gsock->link && gsock->link->fromsock->own_index == old_index) {
2391 sock->own_index = gsock->own_index;
2396 for (sock=gnode->inputs.first; sock; sock=sock->next) {
2397 int old_index = sock->to_index;
2398 /* can't use break in double loop */
2400 for (intnode=ngroup->nodes.first; intnode && !found; intnode=intnode->next) {
2401 for (intsock=intnode->inputs.first; intsock; intsock=intsock->next) {
2402 if (intsock->own_index == old_index && intsock->link) {
2403 sock->own_index = intsock->link->fromsock->own_index;
2412 /* updates external links for all group nodes in a tree */
2413 static void lib_nodetree_do_versions_group_indices_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2417 for (node = ntree->nodes.first; node; node = node->next) {
2418 if (node->type == NODE_GROUP) {
2419 bNodeTree *ngroup = (bNodeTree*)node->id;
2420 if (ngroup && (ngroup->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE))
2421 lib_node_do_versions_group_indices(node);
2426 /* make an update call for the tree */
2427 static void lib_nodetree_do_versions_update_cb(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
2430 ntreeUpdateTree(ntree);
2433 /* verify types for nodes and groups, all data has to be read */
2434 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
2436 static void lib_verify_nodetree(Main *main, int UNUSED(open))
2440 bNodeTreeType *ntreetype;
2442 /* this crashes blender on undo/redo */
2445 reinit_nodesystem();
2449 /* set node->typeinfo pointers */
2450 for (i = 0; i < NUM_NTREE_TYPES; ++i) {
2451 ntreetype = ntreeGetType(i);
2452 if (ntreetype && ntreetype->foreach_nodetree)
2453 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_init_types_cb);
2455 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next)
2456 lib_nodetree_init_types_cb(NULL, NULL, ntree);
2459 int has_old_groups = 0;
2460 /* XXX this should actually be part of do_versions, but since we need
2461 * finished library linking, it is not possible there. Instead in do_versions
2462 * we have set the NTREE_DO_VERSIONS flag, so at this point we can do the
2463 * actual group node updates.
2465 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next) {
2466 if (ntree->flag & NTREE_DO_VERSIONS_GROUP_EXPOSE) {
2467 /* this adds copies and links from all unlinked internal sockets to group inputs/outputs. */
2468 node_group_expose_all_sockets(ntree);
2473 if (has_old_groups) {
2474 for (i = 0; i < NUM_NTREE_TYPES; ++i) {
2475 ntreetype = ntreeGetType(i);
2476 if (ntreetype && ntreetype->foreach_nodetree)
2477 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_group_indices_cb);
2481 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next)
2482 ntree->flag &= ~NTREE_DO_VERSIONS_GROUP_EXPOSE;
2485 /* verify all group user nodes */
2486 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next) {
2487 ntreeVerifyNodes(main, &ntree->id);
2490 /* make update calls where necessary */
2492 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next)
2494 ntreeUpdateTree(ntree);
2496 for (i = 0; i < NUM_NTREE_TYPES; i++) {
2497 ntreetype = ntreeGetType(i);
2498 if (ntreetype && ntreetype->foreach_nodetree)
2499 ntreetype->foreach_nodetree(main, NULL, lib_nodetree_do_versions_update_cb);
2504 static void direct_link_node_socket(FileData *fd, bNodeSocket *sock)
2506 sock->link = newdataadr(fd, sock->link);
2507 sock->storage = newdataadr(fd, sock->storage);
2508 sock->default_value = newdataadr(fd, sock->default_value);
2512 /* ntree itself has been read! */
2513 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2515 /* note: writing and reading goes in sync, for speed */
2520 ntree->init = 0; /* to set callbacks and force setting types */
2521 ntree->progress = NULL;
2522 ntree->execdata = NULL;
2524 ntree->adt = newdataadr(fd, ntree->adt);
2525 direct_link_animdata(fd, ntree->adt);
2527 ntree->id.flag &= ~(LIB_ID_RECALC|LIB_ID_RECALC_DATA);
2529 link_list(fd, &ntree->nodes);
2530 for (node = ntree->nodes.first; node; node = node->next) {
2531 node->typeinfo = NULL;
2533 link_list(fd, &node->inputs);
2534 link_list(fd, &node->outputs);
2536 link_list(fd, &node->internal_links);
2537 for (link = node->internal_links.first; link; link = link->next) {
2538 link->fromnode = newdataadr(fd, link->fromnode);
2539 link->fromsock = newdataadr(fd, link->fromsock);
2540 link->tonode = newdataadr(fd, link->tonode);
2541 link->tosock = newdataadr(fd, link->tosock);
2544 if (node->type == CMP_NODE_MOVIEDISTORTION) {
2545 node->storage = newmclipadr(fd, node->storage);
2548 node->storage = newdataadr(fd, node->storage);
2550 if (node->storage) {
2551 /* could be handlerized at some point */
2552 if (ntree->type==NTREE_SHADER) {
2553 if (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB) {
2554 direct_link_curvemapping(fd, node->storage);
2556 else if (node->type==SH_NODE_SCRIPT) {
2557 NodeShaderScript *nss = (NodeShaderScript *) node->storage;
2558 nss->bytecode = newdataadr(fd, nss->bytecode);
2559 nss->prop = newdataadr(fd, nss->prop);
2561 IDP_DirectLinkProperty(nss->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2564 else if (ntree->type==NTREE_COMPOSIT) {
2565 if (ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
2566 direct_link_curvemapping(fd, node->storage);
2567 else if (ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2568 ((ImageUser *)node->storage)->ok = 1;
2570 else if ( ntree->type==NTREE_TEXTURE) {
2571 if (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME)
2572 direct_link_curvemapping(fd, node->storage);
2573 else if (node->type==TEX_NODE_IMAGE)
2574 ((ImageUser *)node->storage)->ok = 1;
2578 link_list(fd, &ntree->links);
2580 /* external sockets */
2581 link_list(fd, &ntree->inputs);
2582 link_list(fd, &ntree->outputs);
2584 /* and we connect the rest */
2585 for (node = ntree->nodes.first; node; node = node->next) {
2586 node->parent = newdataadr(fd, node->parent);
2587 node->preview = newimaadr(fd, node->preview);
2590 for (sock = node->inputs.first; sock; sock = sock->next)
2591 direct_link_node_socket(fd, sock);
2592 for (sock = node->outputs.first; sock; sock = sock->next)
2593 direct_link_node_socket(fd, sock);
2595 for (sock = ntree->inputs.first; sock; sock = sock->next)
2596 direct_link_node_socket(fd, sock);
2597 for (sock = ntree->outputs.first; sock; sock = sock->next)
2598 direct_link_node_socket(fd, sock);
2600 for (link = ntree->links.first; link; link= link->next) {
2601 link->fromnode = newdataadr(fd, link->fromnode);
2602 link->tonode = newdataadr(fd, link->tonode);
2603 link->fromsock = newdataadr(fd, link->fromsock);
2604 link->tosock = newdataadr(fd, link->tosock);
2607 /* type verification is in lib-link */
2610 /* ************ READ ARMATURE ***************** */
2612 /* temp struct used to transport needed info to lib_link_constraint_cb() */
2613 typedef struct tConstraintLinkData {
2616 } tConstraintLinkData;
2617 /* callback function used to relink constraint ID-links */
2618 static void lib_link_constraint_cb(bConstraint *UNUSED(con), ID **idpoin, short isReference, void *userdata)
2620 tConstraintLinkData *cld= (tConstraintLinkData *)userdata;
2622 /* for reference types, we need to increment the usercounts on load... */
2624 /* reference type - with usercount */
2625 *idpoin = newlibadr_us(cld->fd, cld->id->lib, *idpoin);
2628 /* target type - no usercount needed */
2629 *idpoin = newlibadr(cld->fd, cld->id->lib, *idpoin);
2633 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2635 tConstraintLinkData cld;
2639 for (con = conlist->first; con; con=con->next) {
2640 /* patch for error introduced by changing constraints (dunno how) */
2641 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2642 if (con->data == NULL) {
2643 con->type = CONSTRAINT_TYPE_NULL;
2645 /* own ipo, all constraints have it */
2646 con->ipo = newlibadr_us(fd, id->lib, con->ipo); // XXX deprecated - old animation system
2649 /* relink all ID-blocks used by the constraints */
2653 BKE_id_loop_constraints(conlist, lib_link_constraint_cb, &cld);
2656 static void direct_link_constraints(FileData *fd, ListBase *lb)
2661 for (con=lb->first; con; con=con->next) {
2662 con->data = newdataadr(fd, con->data);
2664 switch (con->type) {
2665 case CONSTRAINT_TYPE_PYTHON:
2667 bPythonConstraint *data= con->data;
2669 link_list(fd, &data->targets);
2671 data->prop = newdataadr(fd, data->prop);
2673 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2676 case CONSTRAINT_TYPE_SPLINEIK:
2678 bSplineIKConstraint *data= con->data;
2680 data->points= newdataadr(fd, data->points);
2683 case CONSTRAINT_TYPE_KINEMATIC:
2685 bKinematicConstraint *data = con->data;
2687 con->lin_error = 0.f;
2688 con->rot_error = 0.f;
2690 /* version patch for runtime flag, was not cleared in some case */
2691 data->flag &= ~CONSTRAINT_IK_AUTO;
2694 case CONSTRAINT_TYPE_CHILDOF:
2696 /* XXX version patch, in older code this flag wasn't always set, and is inherent to type */
2697 if (con->ownspace == CONSTRAINT_SPACE_POSE)
2698 con->flag |= CONSTRAINT_SPACEONCE;
2705 static void lib_link_pose(FileData *fd, Main *bmain, Object *ob, bPose *pose)
2707 bPoseChannel *pchan;
2708 bArmature *arm = ob->data;
2714 /* always rebuild to match proxy or lib changes, but on Undo */
2715 if (fd->memfile == NULL)
2716 if (ob->proxy || (ob->id.lib==NULL && arm->id.lib))
2720 /* sync proxy layer */
2721 if (pose->proxy_layer)
2722 arm->layer = pose->proxy_layer;
2724 /* sync proxy active bone */
2725 if (pose->proxy_act_bone[0]) {
2726 Bone *bone = BKE_armature_find_bone_name(arm, pose->proxy_act_bone);
2728 arm->act_bone = bone;
2732 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2733 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2735 /* hurms... loop in a loop, but yah... later... (ton) */
2736 pchan->bone = BKE_armature_find_bone_name(arm, pchan->name);
2738 pchan->custom = newlibadr_us(fd, arm->id.lib, pchan->custom);
2739 if (pchan->bone == NULL)
2741 else if (ob->id.lib==NULL && arm->id.lib) {
2742 /* local pose selection copied to armature, bit hackish */
2743 pchan->bone->flag &= ~BONE_SELECTED;
2744 pchan->bone->flag |= pchan->selectflag;
2749 DAG_id_tag_update_ex(bmain, &ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
2750 pose->flag |= POSE_RECALC;
2754 static void lib_link_armature(FileData *fd, Main *main)
2758 for (arm = main->armature.first; arm; arm = arm->id.next) {
2759 if (arm->id.flag & LIB_NEED_LINK) {
2760 if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
2761 arm->id.flag -= LIB_NEED_LINK;
2766 static void direct_link_bones(FileData *fd, Bone *bone)
2770 bone->parent = newdataadr(fd, bone->parent);
2771 bone->prop = newdataadr(fd, bone->prop);
2773 IDP_DirectLinkProperty(bone->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2775 bone->flag &= ~BONE_DRAW_ACTIVE;
2777 link_list(fd, &bone->childbase);
2779 for (child=bone->childbase.first; child; child=child->next)
2780 direct_link_bones(fd, child);
2783 static void direct_link_armature(FileData *fd, bArmature *arm)
2787 link_list(fd, &arm->bonebase);
2791 arm->adt = newdataadr(fd, arm->adt);
2792 direct_link_animdata(fd, arm->adt);
2794 for (bone = arm->bonebase.first; bone; bone = bone->next) {
2795 direct_link_bones(fd, bone);
2798 arm->act_bone = newdataadr(fd, arm->act_bone);
2799 arm->act_edbone = NULL;
2802 /* ************ READ CAMERA ***************** */
2804 static void lib_link_camera(FileData *fd, Main *main)
2808 for (ca = main->camera.first; ca; ca = ca->id.next) {
2809 if (ca->id.flag & LIB_NEED_LINK) {
2810 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2812 ca->ipo = newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX deprecated - old animation system
2814 ca->dof_ob = newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2816 ca->id.flag -= LIB_NEED_LINK;
2821 static void direct_link_camera(FileData *fd, Camera *ca)
2823 ca->adt = newdataadr(fd, ca->adt);
2824 direct_link_animdata(fd, ca->adt);
2828 /* ************ READ LAMP ***************** */
2830 static void lib_link_lamp(FileData *fd, Main *main)
2836 for (la = main->lamp.first; la; la = la->id.next) {
2837 if (la->id.flag & LIB_NEED_LINK) {
2838 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2840 for (a = 0; a < MAX_MTEX; a++) {
2843 mtex->tex = newlibadr_us(fd, la->id.lib, mtex->tex);
2844 mtex->object = newlibadr(fd, la->id.lib, mtex->object);
2848 la->ipo = newlibadr_us(fd, la->id.lib, la->ipo); // XXX deprecated - old animation system
2851 lib_link_ntree(fd, &la->id, la->nodetree);
2853 la->id.flag -= LIB_NEED_LINK;
2858 static void direct_link_lamp(FileData *fd, Lamp *la)
2862 la->adt = newdataadr(fd, la->adt);
2863 direct_link_animdata(fd, la->adt);
2865 for (a=0; a<MAX_MTEX; a++) {
2866 la->mtex[a] = newdataadr(fd, la->mtex[a]);
2869 la->curfalloff = newdataadr(fd, la->curfalloff);
2871 direct_link_curvemapping(fd, la->curfalloff);
2873 la->nodetree= newdataadr(fd, la->nodetree);
2875 direct_link_id(fd, &la->nodetree->id);
2876 direct_link_nodetree(fd, la->nodetree);
2879 la->preview = direct_link_preview_image(fd, la->preview);
2882 /* ************ READ keys ***************** */
2884 static void do_versions_key_uidgen(Key *key)
2889 for (block = key->block.first; block; block = block->next) {
2890 block->uid = key->uidgen++;
2894 static void lib_link_key(FileData *fd, Main *main)
2898 for (key = main->key.first; key; key = key->id.next) {
2899 /*check if we need to generate unique ids for the shapekeys*/
2901 do_versions_key_uidgen(key);
2904 if (key->id.flag & LIB_NEED_LINK) {
2905 if (key->adt) lib_link_animdata(fd, &key->id, key->adt);
2907 key->ipo = newlibadr_us(fd, key->id.lib, key->ipo); // XXX deprecated - old animation system
2908 key->from = newlibadr(fd, key->id.lib, key->from);
2910 key->id.flag -= LIB_NEED_LINK;
2915 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2918 char *data, *poin, *cp;
2920 elemsize = key->elemsize;
2923 for (a = 0; a < kb->totelem; a++) {
2927 while (cp[0]) { /* cp[0] == amount */
2928 switch (cp[1]) { /* cp[1] = type */
2933 BLI_endian_switch_float_array((float *)poin, b);
2934 poin += sizeof(float) * b;
2944 static void direct_link_key(FileData *fd, Key *key)
2948 link_list(fd, &(key->block));
2950 key->adt = newdataadr(fd, key->adt);
2951 direct_link_animdata(fd, key->adt);
2953 key->refkey= newdataadr(fd, key->refkey);
2955 for (kb = key->block.first; kb; kb = kb->next) {
2956 kb->data = newdataadr(fd, kb->data);
2958 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2959 switch_endian_keyblock(key, kb);
2963 /* ************ READ mball ***************** */
2965 static void lib_link_mball(FileData *fd, Main *main)
2970 for (mb = main->mball.first; mb; mb = mb->id.next) {
2971 if (mb->id.flag & LIB_NEED_LINK) {
2972 if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
2974 for (a = 0; a < mb->totcol; a++)
2975 mb->mat[a] = newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2977 mb->ipo = newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX deprecated - old animation system
2979 mb->id.flag -= LIB_NEED_LINK;
2984 static void direct_link_mball(FileData *fd, MetaBall *mb)
2986 mb->adt = newdataadr(fd, mb->adt);
2987 direct_link_animdata(fd, mb->adt);
2989 mb->mat = newdataadr(fd, mb->mat);
2990 test_pointer_array(fd, (void **)&mb->mat);
2992 link_list(fd, &(mb->elems));
2994 mb->disp.first = mb->disp.last = NULL;
2995 mb->editelems = NULL;
2997 /* mb->edit_elems.first= mb->edit_elems.last= NULL;*/
2998 mb->lastelem = NULL;
3001 /* ************ READ WORLD ***************** */
3003 static void lib_link_world(FileData *fd, Main *main)
3009 for (wrld = main->world.first; wrld; wrld = wrld->id.next) {
3010 if (wrld->id.flag & LIB_NEED_LINK) {
3011 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
3013 wrld->ipo = newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX deprecated - old animation system
3015 for (a=0; a < MAX_MTEX; a++) {
3016 mtex = wrld->mtex[a];
3018 mtex->tex = newlibadr_us(fd, wrld->id.lib, mtex->tex);
3019 mtex->object = newlibadr(fd, wrld->id.lib, mtex->object);
3024 lib_link_ntree(fd, &wrld->id, wrld->nodetree);
3026 wrld->id.flag -= LIB_NEED_LINK;
3031 static void direct_link_world(FileData *fd, World *wrld)
3035 wrld->adt = newdataadr(fd, wrld->adt);
3036 direct_link_animdata(fd, wrld->adt);
3038 for (a = 0; a < MAX_MTEX; a++) {
3039 wrld->mtex[a] = newdataadr(fd, wrld->mtex[a]);
3042 wrld->nodetree = newdataadr(fd, wrld->nodetree);
3043 if (wrld->nodetree) {
3044 direct_link_id(fd, &wrld->nodetree->id);
3045 direct_link_nodetree(fd, wrld->nodetree);
3048 wrld->preview = direct_link_preview_image(fd, wrld->preview);
3052 /* ************ READ VFONT ***************** */
3054 static void lib_link_vfont(FileData *UNUSED(fd), Main *main)
3058 for (vf = main->vfont.first; vf; vf = vf->id.next) {
3059 if (vf->id.flag & LIB_NEED_LINK) {
3060 vf->id.flag -= LIB_NEED_LINK;
3065 static void direct_link_vfont(FileData *fd, VFont *vf)
3069 vf->packedfile = direct_link_packedfile(fd, vf->packedfile);
3072 /* ************ READ TEXT ****************** */
3074 static void lib_link_text(FileData *UNUSED(fd), Main *main)
3078 for (text = main->text.first; text; text = text->id.next) {
3079 if (text->id.flag & LIB_NEED_LINK) {
3080 text->id.flag -= LIB_NEED_LINK;
3085 static void direct_link_text(FileData *fd, Text *text)
3089 text->name = newdataadr(fd, text->name);
3091 text->undo_pos = -1;
3092 text->undo_len = TXT_INIT_UNDO;
3093 text->undo_buf = MEM_mallocN(text->undo_len, "undo buf");
3095 text->compiled = NULL;
3098 if (text->flags & TXT_ISEXT) {
3099 BKE_text_reload(text);
3104 link_list(fd, &text->lines);
3106 text->curl = newdataadr(fd, text->curl);
3107 text->sell = newdataadr(fd, text->sell);
3109 for (ln = text->lines.first; ln; ln = ln->next) {
3110 ln->line = newdataadr(fd, ln->line);
3113 if (ln->len != (int) strlen(ln->line)) {
3114 printf("Error loading text, line lengths differ\n");
3115 ln->len = strlen(ln->line);
3119 text->flags = (text->flags) & ~TXT_ISEXT;
3124 /* ************ READ IMAGE ***************** */
3126 static void lib_link_image(FileData *fd, Main *main)
3130 for (ima = main->image.first; ima; ima = ima->id.next) {
3131 if (ima->id.flag & LIB_NEED_LINK) {
3132 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3134 ima->id.flag -= LIB_NEED_LINK;
3139 static void link_ibuf_list(FileData *fd, ListBase *lb)
3143 if (lb->first == NULL) return;
3145 lb->first = newimaadr(fd, lb->first);