4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
33 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
34 #include <stdlib.h> // for getenv atoi
35 #include <stddef.h> // for offsetof
36 #include <fcntl.h> // for open
37 #include <string.h> // for strrchr strncmp strstr
38 #include <math.h> // for fabs
41 #include <unistd.h> // for read close
42 #include <sys/param.h> // for MAXPATHLEN
44 #include <io.h> // for open close read
46 #include "BLI_winstuff.h"
49 #include "DNA_anim_types.h"
50 #include "DNA_action_types.h"
51 #include "DNA_armature_types.h"
53 #include "DNA_actuator_types.h"
54 #include "DNA_boid_types.h"
55 #include "DNA_brush_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_cloth_types.h"
58 #include "DNA_color_types.h"
59 #include "DNA_controller_types.h"
60 #include "DNA_constraint_types.h"
61 #include "DNA_curve_types.h"
62 #include "DNA_customdata_types.h"
63 #include "DNA_effect_types.h"
64 #include "DNA_fileglobal_types.h"
65 #include "DNA_genfile.h"
66 #include "DNA_group_types.h"
67 #include "DNA_gpencil_types.h"
68 #include "DNA_ipo_types.h"
69 #include "DNA_image_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_modifier_types.h"
78 #include "DNA_nla_types.h"
79 #include "DNA_node_types.h"
80 #include "DNA_object_types.h"
81 #include "DNA_object_force.h"
82 #include "DNA_object_fluidsim.h" // NT
83 #include "DNA_outliner_types.h"
84 #include "DNA_object_force.h"
85 #include "DNA_packedFile_types.h"
86 #include "DNA_particle_types.h"
87 #include "DNA_property_types.h"
88 #include "DNA_text_types.h"
89 #include "DNA_view3d_types.h"
90 #include "DNA_screen_types.h"
91 #include "DNA_sensor_types.h"
92 #include "DNA_sdna_types.h"
93 #include "DNA_scene_types.h"
94 #include "DNA_sequence_types.h"
95 #include "DNA_smoke_types.h"
96 #include "DNA_sound_types.h"
97 #include "DNA_space_types.h"
98 #include "DNA_texture_types.h"
99 #include "DNA_userdef_types.h"
100 #include "DNA_vfont_types.h"
101 #include "DNA_world_types.h"
102 #include "DNA_windowmanager_types.h"
104 #include "MEM_guardedalloc.h"
105 #include "BLI_blenlib.h"
106 #include "BLI_math.h"
108 #include "BKE_anim.h"
109 #include "BKE_action.h"
110 #include "BKE_armature.h"
111 #include "BKE_brush.h"
112 #include "BKE_colortools.h"
113 #include "BKE_constraint.h"
114 #include "BKE_context.h"
115 #include "BKE_curve.h"
116 #include "BKE_deform.h"
117 #include "BKE_effect.h" /* give_parteff */
118 #include "BKE_fcurve.h"
119 #include "BKE_global.h" // for G
120 #include "BKE_group.h"
121 #include "BKE_image.h"
122 #include "BKE_lattice.h"
123 #include "BKE_library.h" // for which_libbase
124 #include "BKE_main.h" // for Main
125 #include "BKE_mesh.h" // for ME_ defines (patching)
126 #include "BKE_modifier.h"
127 #include "BKE_multires.h"
128 #include "BKE_node.h" // for tree type defines
129 #include "BKE_object.h"
130 #include "BKE_paint.h"
131 #include "BKE_particle.h"
132 #include "BKE_pointcache.h"
133 #include "BKE_property.h" // for get_ob_property
134 #include "BKE_report.h"
135 #include "BKE_sca.h" // for init_actuator
136 #include "BKE_scene.h"
137 #include "BKE_screen.h"
138 #include "BKE_sequencer.h"
139 #include "BKE_texture.h" // for open_plugin_tex
140 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
142 #include "BKE_sound.h"
144 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
145 //XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
146 //XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
147 #include "BLO_readfile.h"
148 #include "BLO_undofile.h"
149 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
151 #include "readfile.h"
153 #include "PIL_time.h"
158 Remark: still a weak point is the newadress() function, that doesnt solve reading from
159 multiple files at the same time
161 (added remark: oh, i thought that was solved? will look at that... (ton)
164 - Existing Library (Main) push or free
174 - read associated 'direct data'
175 - link direct data (internal and to LibBlock)
177 - read USER data, only when indicated (file is ~/.B.blend or .B25.blend)
179 - per Library (per Main)
182 - find LibBlocks and attach IDs to Main
183 - if external LibBlock
185 - or it's already read,
190 - read associated direct data
191 - link direct data (internal and to LibBlock)
193 - per Library with unread LibBlocks
198 - read associated direct data
199 - link direct data (internal and to LibBlock)
202 - link all LibBlocks and indirect pointers to libblocks
203 - initialize FileGlobal and copy pointers to Global
206 /* also occurs in library.c */
207 /* GS reads the memory pointed at in a specific ordering. There are,
208 * however two definitions for it. I have jotted them down here, both,
209 * but I think the first one is actually used. The thing is that
210 * big-endian systems might read this the wrong way round. OTOH, we
211 * constructed the IDs that are read out with this macro explicitly as
212 * well. I expect we'll sort it out soon... */
215 #define GS(a) (*((short *)(a)))
217 /* from misc_util: flip the bytes from x */
218 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
220 // only used here in readfile.c
221 #define SWITCH_LONGINT(a) { \
224 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
225 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
226 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
227 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
231 typedef struct OldNew {
236 typedef struct OldNewMap {
238 int nentries, entriessize;
244 /* local prototypes */
245 static void *read_struct(FileData *fd, BHead *bh, char *blockname);
248 static OldNewMap *oldnewmap_new(void)
250 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
252 onm->entriessize= 1024;
253 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
258 static int verg_oldnewmap(const void *v1, const void *v2)
260 const struct OldNew *x1=v1, *x2=v2;
262 if( x1->old > x2->old) return 1;
263 else if( x1->old < x2->old) return -1;
268 static void oldnewmap_sort(FileData *fd)
270 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
271 fd->libmap->sorted= 1;
274 /* nr is zero for data, and ID code for libdata */
275 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
279 if(oldaddr==NULL || newaddr==NULL) return;
281 if (onm->nentries==onm->entriessize) {
282 int osize= onm->entriessize;
283 OldNew *oentries= onm->entries;
285 onm->entriessize*= 2;
286 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
288 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
292 entry= &onm->entries[onm->nentries++];
294 entry->newp= newaddr;
298 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
302 if (onm->lasthit<onm->nentries-1) {
303 OldNew *entry= &onm->entries[++onm->lasthit];
305 if (entry->old==addr) {
311 for (i=0; i<onm->nentries; i++) {
312 OldNew *entry= &onm->entries[i];
314 if (entry->old==addr) {
325 /* for libdata, nr has ID code, no increment */
326 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
330 if(addr==NULL) return NULL;
332 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
334 OldNew entry_s, *entry;
338 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
342 if (id && (!lib || id->lib)) {
348 for (i=0; i<onm->nentries; i++) {
349 OldNew *entry= &onm->entries[i];
351 if (entry->old==addr) {
354 if (id && (!lib || id->lib)) {
363 static void oldnewmap_free_unused(OldNewMap *onm)
367 for (i=0; i<onm->nentries; i++) {
368 OldNew *entry= &onm->entries[i];
370 MEM_freeN(entry->newp);
376 static void oldnewmap_clear(OldNewMap *onm)
382 static void oldnewmap_free(OldNewMap *onm)
384 MEM_freeN(onm->entries);
390 static void read_libraries(FileData *basefd, ListBase *mainlist);
392 /* ************ help functions ***************** */
394 static void add_main_to_main(Main *mainvar, Main *from)
396 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
399 a= set_listbasepointers(mainvar, lbarray);
400 a= set_listbasepointers(from, fromarray);
402 addlisttolist(lbarray[a], fromarray[a]);
406 void blo_join_main(ListBase *mainlist)
408 Main *tojoin, *mainl;
411 mainl= mainlist->first;
412 while ((tojoin= mainl->next)) {
413 add_main_to_main(mainl, tojoin);
414 BLI_remlink(mainlist, tojoin);
419 static void split_libdata(ListBase *lb, Main *first)
431 if(mainvar->curlib==id->lib) {
432 lbn= which_libbase(mainvar, GS(id->name));
434 BLI_addtail(lbn, id);
437 mainvar= mainvar->next;
439 if(mainvar==0) printf("error split_libdata\n");
445 void blo_split_main(ListBase *mainlist, Main *main)
447 ListBase *lbarray[MAX_LIBARRAY];
451 mainlist->first= mainlist->last= main;
454 if(main->library.first==NULL)
457 for (lib= main->library.first; lib; lib= lib->id.next) {
458 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
459 libmain->curlib= lib;
460 BLI_addtail(mainlist, libmain);
463 i= set_listbasepointers(main, lbarray);
465 split_libdata(lbarray[i], main->next);
468 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
469 static void cleanup_path(const char *relabase, char *name)
471 char filename[FILE_MAXFILE];
473 BLI_splitdirstring(name, filename);
474 BLI_cleanup_dir(relabase, name);
475 strcat(name, filename);
478 static void read_file_version(FileData *fd, Main *main)
482 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
483 if (bhead->code==GLOB) {
484 FileGlobal *fg= read_struct(fd, bhead, "Global");
486 main->subversionfile= fg->subversion;
487 main->minversionfile= fg->minversion;
488 main->minsubversionfile= fg->minsubversion;
491 else if (bhead->code==ENDB)
498 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *name, const char *relabase)
502 char name1[FILE_MAXDIR+FILE_MAXFILE];
504 strncpy(name1, name, sizeof(name1)-1);
505 cleanup_path(relabase, name1);
506 // printf("blo_find_main: original in %s\n", name);
507 // printf("blo_find_main: converted to %s\n", name1);
509 for (m= mainlist->first; m; m= m->next) {
510 char *libname= (m->curlib)?m->curlib->filepath:m->name;
512 if (BLI_streq(name1, libname)) {
513 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
518 m= MEM_callocN(sizeof(Main), "find_main");
519 BLI_addtail(mainlist, m);
521 lib= alloc_libblock(&m->library, ID_LI, "lib");
522 strncpy(lib->name, name, sizeof(lib->name)-1);
523 BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
527 read_file_version(fd, m);
529 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", name);
534 /* ************ FILE PARSING ****************** */
536 static void switch_endian_bh4(BHead4 *bhead)
538 /* the ID_.. codes */
539 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
541 if (bhead->code != ENDB) {
542 SWITCH_INT(bhead->len);
543 SWITCH_INT(bhead->SDNAnr);
544 SWITCH_INT(bhead->nr);
548 static void switch_endian_bh8(BHead8 *bhead)
550 /* the ID_.. codes */
551 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
553 if (bhead->code != ENDB) {
554 SWITCH_INT(bhead->len);
555 SWITCH_INT(bhead->SDNAnr);
556 SWITCH_INT(bhead->nr);
560 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
562 BHead4 *bhead4 = (BHead4 *) bhead;
563 #if defined(WIN32) && !defined(FREE_WINDOWS)
569 bhead4->code= bhead8->code;
570 bhead4->len= bhead8->len;
572 if (bhead4->code != ENDB) {
574 //perform a endian swap on 64bit pointers, otherwise the pointer might map to zero
575 //0x0000000000000000000012345678 would become 0x12345678000000000000000000000000
576 if (do_endian_swap) {
577 SWITCH_LONGINT(bhead8->old);
580 /* this patch is to avoid a long long being read from not-eight aligned positions
581 is necessary on any modern 64bit architecture) */
582 memcpy(&old, &bhead8->old, 8);
583 bhead4->old = (int) (old >> 3);
585 bhead4->SDNAnr= bhead8->SDNAnr;
586 bhead4->nr= bhead8->nr;
590 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
592 BHead8 *bhead8 = (BHead8 *) bhead;
594 bhead8->code= bhead4->code;
595 bhead8->len= bhead4->len;
597 if (bhead8->code != ENDB) {
598 bhead8->old= bhead4->old;
599 bhead8->SDNAnr= bhead4->SDNAnr;
600 bhead8->nr= bhead4->nr;
604 static BHeadN *get_bhead(FileData *fd)
609 BHeadN *new_bhead = 0;
615 /* not strictly needed but shuts valgrind up
616 * since uninitialized memory gets compared */
617 memset(&bhead8, 0, sizeof(BHead8));
618 memset(&bhead4, 0, sizeof(BHead4));
619 memset(&bhead, 0, sizeof(BHead));
621 // First read the bhead structure.
622 // Depending on the platform the file was written on this can
623 // be a big or little endian BHead4 or BHead8 structure.
625 // As usual 'ENDB' (the last *partial* bhead of the file)
626 // needs some special handling. We don't want to EOF just yet.
628 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
630 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
632 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
633 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
634 switch_endian_bh4(&bhead4);
637 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
638 bh8_from_bh4(&bhead, &bhead4);
640 memcpy(&bhead, &bhead4, sizeof(bhead));
648 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
650 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
651 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
652 switch_endian_bh8(&bhead8);
655 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
656 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
658 memcpy(&bhead, &bhead8, sizeof(bhead));
666 /* make sure people are not trying to pass bad blend files */
667 if (bhead.len < 0) fd->eof = 1;
669 // bhead now contains the (converted) bhead structure. Now read
670 // the associated data and put everything in a BHeadN (creative naming !)
673 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
675 new_bhead->next = new_bhead->prev = 0;
676 new_bhead->bhead = bhead;
678 readsize = fd->read(fd, new_bhead + 1, bhead.len);
680 if (readsize != bhead.len) {
682 MEM_freeN(new_bhead);
692 // We've read a new block. Now add it to the list
696 BLI_addtail(&fd->listbase, new_bhead);
702 BHead *blo_firstbhead(FileData *fd)
708 // Read in a new block if necessary
710 new_bhead = fd->listbase.first;
711 if (new_bhead == 0) {
712 new_bhead = get_bhead(fd);
716 bhead = &new_bhead->bhead;
722 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
724 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
725 BHeadN *prev= bheadn->prev;
727 return prev?&prev->bhead:NULL;
730 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
732 BHeadN *new_bhead = NULL;
736 // bhead is actually a sub part of BHeadN
737 // We calculate the BHeadN pointer from the BHead pointer below
738 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
740 // get the next BHeadN. If it doesn't exist we read in the next one
741 new_bhead = new_bhead->next;
742 if (new_bhead == 0) {
743 new_bhead = get_bhead(fd);
748 // here we do the reverse:
749 // go from the BHeadN pointer to the BHead pointer
750 bhead = &new_bhead->bhead;
756 static void decode_blender_header(FileData *fd)
758 char header[SIZEOFBLENDERHEADER], num[4];
761 // read in the header data
762 readsize = fd->read(fd, header, sizeof(header));
764 if (readsize == sizeof(header)) {
765 if(strncmp(header, "BLENDER", 7) == 0) {
766 int remove_this_endian_test= 1;
768 fd->flags |= FD_FLAGS_FILE_OK;
770 // what size are pointers in the file ?
772 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
773 if (sizeof(void *) != 4) {
774 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
777 if (sizeof(void *) != 8) {
778 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
782 // is the file saved in a different endian
784 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
785 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
788 // get the version number
790 memcpy(num, header+9, 3);
792 fd->fileversion = atoi(num);
797 static int read_file_dna(FileData *fd)
801 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
802 if (bhead->code==DNA1) {
803 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
805 fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
808 fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
809 /* used to retrieve ID names from (bhead+1) */
810 fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
814 } else if (bhead->code==ENDB)
821 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
823 int readsize = read(filedata->filedes, buffer, size);
828 filedata->seek += readsize;
834 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
836 int readsize = gzread(filedata->gzfiledes, buffer, size);
841 filedata->seek += readsize;
847 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
849 // don't read more bytes then there are available in the buffer
850 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
852 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
853 filedata->seek += readsize;
858 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
860 static unsigned int seek= 1<<30; /* the current position */
861 static unsigned int offset= 0; /* size of previous chunks */
862 static MemFileChunk *chunk=NULL;
863 unsigned int chunkoffset, readsize, totread;
865 if(size==0) return 0;
867 if(seek != (unsigned int)filedata->seek) {
868 chunk= filedata->memfile->chunks.first;
872 if(seek + chunk->size > (unsigned) filedata->seek) break;
877 seek= filedata->seek;
884 /* first check if it's on the end if current chunk */
885 if(seek-offset == chunk->size) {
886 offset+= chunk->size;
890 /* debug, should never happen */
892 printf("illegal read, chunk zero\n");
896 chunkoffset= seek-offset;
897 readsize= size-totread;
899 /* data can be spread over multiple chunks, so clamp size
900 * to within this chunk, and then it will read further in
902 if(chunkoffset+readsize > chunk->size)
903 readsize= chunk->size-chunkoffset;
905 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
907 filedata->seek += readsize;
909 } while(totread < size);
917 static FileData *filedata_new(void)
919 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
922 fd->gzfiledes = NULL;
924 /* XXX, this doesn't need to be done all the time,
925 * but it keeps us reentrant, remove once we have
926 * a lib that provides a nice lock. - zr
928 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
930 fd->datamap = oldnewmap_new();
931 fd->globmap = oldnewmap_new();
932 fd->libmap = oldnewmap_new();
937 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
939 decode_blender_header(fd);
941 if (fd->flags & FD_FLAGS_FILE_OK) {
942 if (!read_file_dna(fd)) {
943 BKE_report(reports, RPT_ERROR, "File incomplete");
944 blo_freefiledata(fd);
949 BKE_report(reports, RPT_ERROR, "File is not a Blender file");
950 blo_freefiledata(fd);
957 /* cannot be called with relative paths anymore! */
958 /* on each new library added, it now checks for the current FileData and expands relativeness */
959 FileData *blo_openblenderfile(char *name, ReportList *reports)
963 gzfile= gzopen(name, "rb");
965 if (gzfile == Z_NULL) {
966 BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", name, errno ? strerror(errno) : "Unknown erro reading file");
969 FileData *fd = filedata_new();
970 fd->gzfiledes = gzfile;
971 fd->read = fd_read_gzip_from_file;
973 /* needed for library_append and read_libraries */
974 BLI_strncpy(fd->relabase, name, sizeof(fd->relabase));
976 return blo_decode_and_check(fd, reports);
980 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
982 if (!mem || memsize<SIZEOFBLENDERHEADER) {
983 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
986 FileData *fd= filedata_new();
988 fd->buffersize= memsize;
989 fd->read= fd_read_from_memory;
990 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
992 return blo_decode_and_check(fd, reports);
996 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
999 BKE_report(reports, RPT_ERROR, "Unable to open blend <memory>");
1002 FileData *fd= filedata_new();
1003 fd->memfile= memfile;
1005 fd->read= fd_read_from_memfile;
1006 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
1008 return blo_decode_and_check(fd, reports);
1013 void blo_freefiledata(FileData *fd)
1017 if (fd->filedes != -1) {
1021 if (fd->gzfiledes != NULL)
1023 gzclose(fd->gzfiledes);
1026 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1027 MEM_freeN(fd->buffer);
1031 // Free all BHeadN data blocks
1032 BLI_freelistN(&fd->listbase);
1035 DNA_sdna_free(fd->memsdna);
1037 DNA_sdna_free(fd->filesdna);
1039 MEM_freeN(fd->compflags);
1042 oldnewmap_free(fd->datamap);
1044 oldnewmap_free(fd->globmap);
1046 oldnewmap_free(fd->imamap);
1047 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1048 oldnewmap_free(fd->libmap);
1050 MEM_freeN(fd->bheadmap);
1056 /* ************ DIV ****************** */
1058 int BLO_has_bfile_extension(char *str)
1060 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1063 int BLO_is_a_library(const char *path, char *dir, char *group)
1065 /* return ok when a blenderfile, in dir is the filename,
1066 * in group the type of libdata
1074 if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
1079 /* Find the last slash */
1080 fd= BLI_last_slash(dir);
1084 if(BLO_has_bfile_extension(fd+1)) {
1085 /* the last part of the dir is a .blend file, no group follows */
1086 *fd= '/'; /* put back the removed slash separating the dir and the .blend file name */
1089 char *gp = fd+1; // in case we have a .blend file, gp points to the group
1091 /* Find the last slash */
1092 fd= BLI_last_slash(dir);
1093 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
1095 /* now we know that we are in a blend file and it is safe to
1096 assume that gp actually points to a group */
1097 if (BLI_streq("Screen", gp)==0)
1098 BLI_strncpy(group, gp, GROUP_MAX);
1103 /* ************** OLD POINTERS ******************* */
1105 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1107 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1110 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1112 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1115 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1117 if(fd->imamap && adr)
1118 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1123 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1125 return oldnewmap_liblookup(fd->libmap, adr, lib);
1128 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1130 ID *id= newlibadr(fd, lib, adr);
1138 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1142 for (i=0; i<fd->libmap->nentries; i++) {
1143 OldNew *entry= &fd->libmap->entries[i];
1145 if (old==entry->newp && entry->nr==ID_ID) {
1147 if(new) entry->nr= GS( ((ID *)new)->name );
1153 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1157 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1160 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1164 change_idid_adr_fd(fd, old, new);
1169 /* lib linked proxy objects point to our local data, we need
1170 * to clear that pointer before reading the undo memfile since
1171 * the object might be removed, it is set again in reading
1172 * if the local object still exists */
1173 void blo_clear_proxy_pointers_from_lib(FileData *fd, Main *oldmain)
1175 Object *ob= oldmain->object.first;
1177 for(;ob; ob= ob->id.next)
1179 ob->proxy_from= NULL;
1182 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1184 Image *ima= oldmain->image.first;
1185 Scene *sce= oldmain->scene.first;
1188 fd->imamap= oldnewmap_new();
1190 for(;ima; ima= ima->id.next) {
1191 Link *ibuf= ima->ibufs.first;
1192 for(; ibuf; ibuf= ibuf->next)
1193 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1195 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1196 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
1198 oldnewmap_insert(fd->imamap, ima->renders[a], ima->renders[a], 0);
1200 for(; sce; sce= sce->id.next) {
1203 for(node= sce->nodetree->nodes.first; node; node= node->next)
1204 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1209 /* set old main image ibufs to zero if it has been restored */
1210 /* this works because freeing old main only happens after this call */
1211 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1213 OldNew *entry= fd->imamap->entries;
1214 Image *ima= oldmain->image.first;
1215 Scene *sce= oldmain->scene.first;
1218 /* used entries were restored, so we put them to zero */
1219 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1224 for(;ima; ima= ima->id.next) {
1227 /* this mirrors direct_link_image */
1228 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1230 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1231 BLI_remlink(&ima->ibufs, ibuf);
1233 ima->gputexture= NULL;
1237 ima->gputexture= newimaadr(fd, ima->gputexture);
1238 for(a=0; a<IMA_MAX_RENDER_SLOT; a++)
1239 ima->renders[a]= newimaadr(fd, ima->renders[a]);
1241 for(; sce; sce= sce->id.next) {
1244 for(node= sce->nodetree->nodes.first; node; node= node->next)
1245 node->preview= newimaadr(fd, node->preview);
1250 /* undo file support: add all library pointers in lookup */
1251 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1253 Main *ptr= mainlist->first;
1254 ListBase *lbarray[MAX_LIBARRAY];
1256 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1257 int i= set_listbasepointers(ptr, lbarray);
1260 for(id= lbarray[i]->first; id; id= id->next)
1261 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1267 /* ********** END OLD POINTERS ****************** */
1268 /* ********** READ FILE ****************** */
1270 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1272 int blocksize, nblocks;
1275 data= (char *)(bhead+1);
1276 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1280 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1286 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1291 /* switch is based on file dna */
1292 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1293 switch_endian_structs(fd->filesdna, bh);
1295 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1296 if(fd->compflags[bh->SDNAnr]==2) {
1297 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1299 temp= MEM_mallocN(bh->len, blockname);
1300 memcpy(temp, (bh+1), bh->len);
1308 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1312 if(lb->first==NULL) return;
1314 lb->first= newdataadr(fd, lb->first);
1318 ln->next= newdataadr(fd, ln->next);
1326 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1331 if(lb->first==0) return;
1332 poin= newdataadr(fd, lb->first);
1334 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1341 poin= newdataadr(fd, ln->next);
1343 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1353 static void test_pointer_array(FileData *fd, void **mat)
1355 #if defined(WIN32) && !defined(FREE_WINDOWS)
1356 __int64 *lpoin, *lmat;
1358 long long *lpoin, *lmat;
1363 /* manually convert the pointer array in
1364 * the old dna format to a pointer array in
1365 * the new dna format.
1368 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1370 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1371 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1375 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1376 SWITCH_LONGINT(*lpoin);
1377 *ipoin= (int) ((*lpoin) >> 3);
1385 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1386 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1400 /* ************ READ ID Properties *************** */
1402 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1403 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1405 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1410 /*since we didn't save the extra buffer, set totallen to len.*/
1411 prop->totallen = prop->len;
1412 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1414 array= (IDProperty*) prop->data.pointer;
1416 for(i=0; i<prop->len; i++)
1417 IDP_DirectLinkProperty(&array[i], switch_endian, fd);
1420 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1425 /*since we didn't save the extra buffer, set totallen to len.*/
1426 prop->totallen = prop->len;
1427 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1429 if(prop->subtype == IDP_GROUP) {
1430 test_pointer_array(fd, prop->data.pointer);
1431 array= prop->data.pointer;
1433 for(i=0; i<prop->len; i++)
1434 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1436 else if(prop->subtype == IDP_DOUBLE) {
1437 if (switch_endian) {
1438 for (i=0; i<prop->len; i++) {
1439 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1444 if (switch_endian) {
1445 for (i=0; i<prop->len; i++) {
1446 SWITCH_INT(((int*)prop->data.pointer)[i]);
1452 static void IDP_DirectLinkString(IDProperty *prop, int switch_endian, FileData *fd)
1454 /*since we didn't save the extra string buffer, set totallen to len.*/
1455 prop->totallen = prop->len;
1456 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1459 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1461 ListBase *lb = &prop->data.group;
1466 /*Link child id properties now*/
1467 for (loop=prop->data.group.first; loop; loop=loop->next) {
1468 IDP_DirectLinkProperty(loop, switch_endian, fd);
1472 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1474 switch (prop->type) {
1476 IDP_DirectLinkGroup(prop, switch_endian, fd);
1479 IDP_DirectLinkString(prop, switch_endian, fd);
1482 IDP_DirectLinkArray(prop, switch_endian, fd);
1485 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1488 /*erg, stupid doubles. since I'm storing them
1489 in the same field as int val; val2 in the
1490 IDPropertyData struct, they have to deal with
1491 endianness specifically
1493 in theory, val and val2 would've already been swapped
1494 if switch_endian is true, so we have to first unswap
1495 them then reswap them as a single 64-bit entity.
1498 if (switch_endian) {
1499 SWITCH_INT(prop->data.val);
1500 SWITCH_INT(prop->data.val2);
1501 SWITCH_LONGINT(prop->data.val);
1509 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1513 /* ************ READ CurveMapping *************** */
1515 /* cuma itself has been read! */
1516 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1520 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1521 cumap->flag &= ~CUMA_PREMULLED;
1523 for(a=0; a<CM_TOT; a++) {
1524 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1525 cumap->cm[a].table= NULL;
1526 cumap->cm[a].premultable= NULL;
1530 /* ************ READ Brush *************** */
1531 /* library brush linking after fileread */
1532 static void lib_link_brush(FileData *fd, Main *main)
1536 /* only link ID pointers */
1537 for(brush= main->brush.first; brush; brush= brush->id.next) {
1538 if(brush->id.flag & LIB_NEEDLINK) {
1539 brush->id.flag -= LIB_NEEDLINK;
1541 brush->mtex.tex= newlibadr_us(fd, brush->id.lib, brush->mtex.tex);
1542 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1547 static void direct_link_brush(FileData *fd, Brush *brush)
1549 /* brush itself has been read */
1552 brush->curve= newdataadr(fd, brush->curve);
1554 direct_link_curvemapping(fd, brush->curve);
1556 brush_curve_preset(brush, CURVE_PRESET_SHARP);
1559 static void direct_link_script(FileData *fd, Script *script)
1562 SCRIPT_SET_NULL(script)
1566 /* ************ READ PACKEDFILE *************** */
1568 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1570 PackedFile *pf= newdataadr(fd, oldpf);
1573 pf->data= newdataadr(fd, pf->data);
1579 /* ************ READ IMAGE PREVIEW *************** */
1581 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1583 PreviewImage *prv= newdataadr(fd, old_prv);
1587 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1589 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1597 /* ************ READ ANIMATION STUFF ***************** */
1599 /* Legacy Data Support (for Version Patching) ----------------------------- */
1601 // XXX depreceated - old animation system
1602 static void lib_link_ipo(FileData *fd, Main *main)
1606 ipo= main->ipo.first;
1608 if(ipo->id.flag & LIB_NEEDLINK) {
1610 for(icu= ipo->curve.first; icu; icu= icu->next) {
1612 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1614 ipo->id.flag -= LIB_NEEDLINK;
1620 // XXX depreceated - old animation system
1621 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1625 link_list(fd, &(ipo->curve));
1626 icu= ipo->curve.first;
1628 icu->bezt= newdataadr(fd, icu->bezt);
1629 icu->bp= newdataadr(fd, icu->bp);
1630 icu->driver= newdataadr(fd, icu->driver);
1635 // XXX depreceated - old animation system
1636 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1638 bActionStrip *strip;
1639 bActionModifier *amod;
1641 for (strip=striplist->first; strip; strip=strip->next){
1642 strip->object = newlibadr(fd, id->lib, strip->object);
1643 strip->act = newlibadr_us(fd, id->lib, strip->act);
1644 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1645 for(amod= strip->modifiers.first; amod; amod= amod->next)
1646 amod->ob= newlibadr(fd, id->lib, amod->ob);
1650 // XXX depreceated - old animation system
1651 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1653 bActionStrip *strip;
1655 link_list(fd, strips);
1657 for(strip= strips->first; strip; strip= strip->next)
1658 link_list(fd, &strip->modifiers);
1661 // XXX depreceated - old animation system
1662 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1664 bConstraintChannel *chan;
1666 for (chan=chanbase->first; chan; chan=chan->next){
1667 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1671 /* Data Linking ----------------------------- */
1673 static void lib_link_fmodifiers(FileData *fd, ID *id, ListBase *list)
1677 for (fcm= list->first; fcm; fcm= fcm->next) {
1678 /* data for specific modifiers */
1679 switch (fcm->type) {
1680 case FMODIFIER_TYPE_PYTHON:
1682 FMod_Python *data= (FMod_Python *)fcm->data;
1683 data->script = newlibadr(fd, id->lib, data->script);
1690 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1697 /* relink ID-block references... */
1698 for (fcu= list->first; fcu; fcu= fcu->next) {
1701 ChannelDriver *driver= fcu->driver;
1704 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1705 DRIVER_TARGETS_LOOPER(dvar)
1707 /* only relink if still used */
1708 if (tarIndex < dvar->num_targets)
1709 dtar->id= newlibadr(fd, id->lib, dtar->id);
1713 DRIVER_TARGETS_LOOPER_END
1718 lib_link_fmodifiers(fd, id, &fcu->modifiers);
1723 /* NOTE: this assumes that link_list has already been called on the list */
1724 static void direct_link_fmodifiers(FileData *fd, ListBase *list)
1728 for (fcm= list->first; fcm; fcm= fcm->next) {
1729 /* relink general data */
1730 fcm->data = newdataadr(fd, fcm->data);
1733 /* do relinking of data for specific types */
1734 switch (fcm->type) {
1735 case FMODIFIER_TYPE_GENERATOR:
1737 FMod_Generator *data= (FMod_Generator *)fcm->data;
1739 data->coefficients= newdataadr(fd, data->coefficients);
1742 case FMODIFIER_TYPE_ENVELOPE:
1744 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1746 data->data= newdataadr(fd, data->data);
1749 case FMODIFIER_TYPE_PYTHON:
1751 FMod_Python *data= (FMod_Python *)fcm->data;
1753 data->prop = newdataadr(fd, data->prop);
1754 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1761 /* NOTE: this assumes that link_list has already been called on the list */
1762 static void direct_link_fcurves(FileData *fd, ListBase *list)
1766 /* link F-Curve data to F-Curve again (non ID-libs) */
1767 for (fcu= list->first; fcu; fcu= fcu->next) {
1769 fcu->bezt= newdataadr(fd, fcu->bezt);
1770 fcu->fpt= newdataadr(fd, fcu->fpt);
1773 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1776 fcu->grp= newdataadr(fd, fcu->grp);
1779 fcu->driver= newdataadr(fd, fcu->driver);
1781 ChannelDriver *driver= fcu->driver;
1784 driver->expr_comp= NULL;
1786 /* relink variables, targets and their paths */
1787 link_list(fd, &driver->variables);
1788 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1789 DRIVER_TARGETS_LOOPER(dvar)
1791 /* only relink the targets being used */
1792 if (tarIndex < dvar->num_targets)
1793 dtar->rna_path= newdataadr(fd, dtar->rna_path);
1795 dtar->rna_path= NULL;
1797 DRIVER_TARGETS_LOOPER_END
1802 link_list(fd, &fcu->modifiers);
1803 direct_link_fmodifiers(fd, &fcu->modifiers);
1808 static void lib_link_action(FileData *fd, Main *main)
1811 bActionChannel *chan;
1813 for (act= main->action.first; act; act= act->id.next) {
1814 if (act->id.flag & LIB_NEEDLINK) {
1815 act->id.flag -= LIB_NEEDLINK;
1817 // XXX depreceated - old animation system <<<
1818 for (chan=act->chanbase.first; chan; chan=chan->next) {
1819 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1820 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1822 // >>> XXX depreceated - old animation system
1824 lib_link_fcurves(fd, &act->id, &act->curves);
1829 static void direct_link_action(FileData *fd, bAction *act)
1831 bActionChannel *achan; // XXX depreceated - old animation system
1834 link_list(fd, &act->curves);
1835 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1836 link_list(fd, &act->groups);
1837 link_list(fd, &act->markers);
1839 // XXX depreceated - old animation system <<<
1840 for (achan = act->chanbase.first; achan; achan=achan->next) {
1841 achan->grp= newdataadr(fd, achan->grp);
1843 link_list(fd, &achan->constraintChannels);
1845 // >>> XXX depreceated - old animation system
1847 direct_link_fcurves(fd, &act->curves);
1849 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1850 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1851 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1855 static void lib_link_nladata_strips(FileData *fd, ID *id, ListBase *list)
1859 for (strip= list->first; strip; strip= strip->next) {
1860 /* check strip's children */
1861 lib_link_nladata_strips(fd, id, &strip->strips);
1863 /* check strip's F-Curves */
1864 lib_link_fcurves(fd, id, &strip->fcurves);
1866 /* reassign the counted-reference to action */
1867 strip->act = newlibadr_us(fd, id->lib, strip->act);
1871 static void lib_link_nladata(FileData *fd, ID *id, ListBase *list)
1875 /* we only care about the NLA strips inside the tracks */
1876 for (nlt= list->first; nlt; nlt= nlt->next) {
1877 lib_link_nladata_strips(fd, id, &nlt->strips);
1881 /* This handles Animato NLA-Strips linking
1882 * NOTE: this assumes that link_list has already been called on the list
1884 static void direct_link_nladata_strips(FileData *fd, ListBase *list)
1888 for (strip= list->first; strip; strip= strip->next) {
1889 /* strip's child strips */
1890 link_list(fd, &strip->strips);
1891 direct_link_nladata_strips(fd, &strip->strips);
1893 /* strip's F-Curves */
1894 link_list(fd, &strip->fcurves);
1895 direct_link_fcurves(fd, &strip->fcurves);
1897 /* strip's F-Modifiers */
1898 link_list(fd, &strip->modifiers);
1899 direct_link_fcurves(fd, &strip->modifiers);
1903 /* NOTE: this assumes that link_list has already been called on the list */
1904 static void direct_link_nladata(FileData *fd, ListBase *list)
1908 for (nlt= list->first; nlt; nlt= nlt->next) {
1909 /* relink list of strips */
1910 link_list(fd, &nlt->strips);
1912 /* relink strip data */
1913 direct_link_nladata_strips(fd, &nlt->strips);
1919 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
1924 /* here, we're only interested in the ID pointer stored in some of the paths */
1925 for (ks= list->first; ks; ks= ks->next) {
1926 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1927 ksp->id= newlibadr(fd, id->lib, ksp->id);
1932 /* NOTE: this assumes that link_list has already been called on the list */
1933 static void direct_link_keyingsets(FileData *fd, ListBase *list)
1938 /* link KeyingSet data to KeyingSet again (non ID-libs) */
1939 for (ks= list->first; ks; ks= ks->next) {
1941 link_list(fd, &ks->paths);
1943 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1945 ksp->rna_path= newdataadr(fd, ksp->rna_path);
1952 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
1957 /* link action data */
1958 adt->action= newlibadr_us(fd, id->lib, adt->action);
1959 adt->tmpact= newlibadr_us(fd, id->lib, adt->tmpact);
1962 lib_link_fcurves(fd, id, &adt->drivers);
1964 /* overrides don't have lib-link for now, so no need to do anything */
1967 lib_link_nladata(fd, id, &adt->nla_tracks);
1970 static void direct_link_animdata(FileData *fd, AnimData *adt)
1972 /* NOTE: must have called newdataadr already before doing this... */
1977 link_list(fd, &adt->drivers);
1978 direct_link_fcurves(fd, &adt->drivers);
1980 /* link overrides */
1984 link_list(fd, &adt->nla_tracks);
1985 direct_link_nladata(fd, &adt->nla_tracks);
1987 /* clear temp pointers that may have been set... */
1988 // TODO: it's probably only a small cost to reload this anyway...
1989 adt->actstrip= NULL;
1992 /* ************ READ MOTION PATHS *************** */
1994 /* direct data for cache */
1995 static void direct_link_motionpath(FileData *fd, bMotionPath *mpath)
2001 /* relink points cache */
2002 mpath->points= newdataadr(fd, mpath->points);
2005 /* ************ READ NODE TREE *************** */
2007 /* singe node tree (also used for material/scene trees), ntree is not NULL */
2008 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
2012 if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
2014 ntree->gpd= newlibadr_us(fd, id->lib, ntree->gpd);
2016 for(node= ntree->nodes.first; node; node= node->next)
2017 node->id= newlibadr_us(fd, id->lib, node->id);
2020 /* library ntree linking after fileread */
2021 static void lib_link_nodetree(FileData *fd, Main *main)
2025 /* only link ID pointers */
2026 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2027 if(ntree->id.flag & LIB_NEEDLINK) {
2028 ntree->id.flag -= LIB_NEEDLINK;
2029 lib_link_ntree(fd, &ntree->id, ntree);
2034 /* verify types for nodes and groups, all data has to be read */
2035 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
2037 static void lib_verify_nodetree(Main *main, int open)
2044 /* this crashes blender on undo/redo
2046 reinit_nodesystem();
2049 /* now create the own typeinfo structs an verify nodes */
2050 /* here we still assume no groups in groups */
2051 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
2052 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
2053 ntreeMakeOwnType(ntree); /* for group usage */
2056 /* now verify all types in material trees, groups are set OK now */
2057 for(ma= main->mat.first; ma; ma= ma->id.next) {
2059 ntreeVerifyTypes(ma->nodetree);
2061 /* and scene trees */
2062 for(sce= main->scene.first; sce; sce= sce->id.next) {
2064 ntreeVerifyTypes(sce->nodetree);
2066 /* and texture trees */
2067 for(tx= main->tex.first; tx; tx= tx->id.next) {
2069 ntreeVerifyTypes(tx->nodetree);
2075 /* ntree itself has been read! */
2076 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
2078 /* note: writing and reading goes in sync, for speed */
2083 ntree->init= 0; /* to set callbacks and force setting types */
2084 ntree->owntype= NULL;
2085 ntree->progress= NULL;
2087 ntree->adt= newdataadr(fd, ntree->adt);
2088 direct_link_animdata(fd, ntree->adt);
2090 link_list(fd, &ntree->nodes);
2091 for(node= ntree->nodes.first; node; node= node->next) {
2092 if(node->type == NODE_DYNAMIC) {
2094 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
2095 node->typeinfo= NULL;
2098 node->storage= newdataadr(fd, node->storage);
2101 /* could be handlerized at some point */
2102 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
2103 direct_link_curvemapping(fd, node->storage);
2104 else if(ntree->type==NTREE_COMPOSIT) {
2105 if( ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
2106 direct_link_curvemapping(fd, node->storage);
2107 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
2108 ((ImageUser *)node->storage)->ok= 1;
2110 else if( ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) ) {
2111 direct_link_curvemapping(fd, node->storage);
2114 link_list(fd, &node->inputs);
2115 link_list(fd, &node->outputs);
2117 link_list(fd, &ntree->links);
2119 /* and we connect the rest */
2120 for(node= ntree->nodes.first; node; node= node->next) {
2121 node->preview= newimaadr(fd, node->preview);
2123 for(sock= node->inputs.first; sock; sock= sock->next)
2124 sock->link= newdataadr(fd, sock->link);
2125 for(sock= node->outputs.first; sock; sock= sock->next)
2126 sock->ns.data= NULL;
2128 for(link= ntree->links.first; link; link= link->next) {
2129 link->fromnode= newdataadr(fd, link->fromnode);
2130 link->tonode= newdataadr(fd, link->tonode);
2131 link->fromsock= newdataadr(fd, link->fromsock);
2132 link->tosock= newdataadr(fd, link->tosock);
2135 /* type verification is in lib-link */
2138 /* ************ READ ARMATURE ***************** */
2140 /* temp struct used to transport needed info to lib_link_constraint_cb() */
2141 typedef struct tConstraintLinkData {
2144 } tConstraintLinkData;
2145 /* callback function used to relink constraint ID-links */
2146 static void lib_link_constraint_cb(bConstraint *con, ID **idpoin, void *userdata)
2148 tConstraintLinkData *cld= (tConstraintLinkData *)userdata;
2149 *idpoin = newlibadr(cld->fd, cld->id->lib, *idpoin);
2152 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2154 tConstraintLinkData cld;
2158 for (con = conlist->first; con; con=con->next) {
2159 /* patch for error introduced by changing constraints (dunno how) */
2160 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2161 if(con->data==NULL) {
2162 con->type= CONSTRAINT_TYPE_NULL;
2164 /* own ipo, all constraints have it */
2165 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2168 /* relink all ID-blocks used by the constraints */
2172 id_loop_constraints(conlist, lib_link_constraint_cb, &cld);
2175 static void direct_link_constraints(FileData *fd, ListBase *lb)
2180 for (con=lb->first; con; con=con->next) {
2181 con->data = newdataadr(fd, con->data);
2183 switch (con->type) {
2184 case CONSTRAINT_TYPE_PYTHON:
2186 bPythonConstraint *data= con->data;
2188 link_list(fd, &data->targets);
2190 data->prop = newdataadr(fd, data->prop);
2192 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2195 case CONSTRAINT_TYPE_SPLINEIK:
2197 bSplineIKConstraint *data= con->data;
2199 data->points= newdataadr(fd, data->points);
2202 case CONSTRAINT_TYPE_KINEMATIC:
2204 con->lin_error = 0.f;
2205 con->rot_error = 0.f;
2212 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2214 bPoseChannel *pchan;
2215 bArmature *arm= ob->data;
2221 /* always rebuild to match proxy or lib changes */
2222 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2224 if (ob->proxy && pose->proxy_act_bone[0]) {
2225 Bone *bone = get_named_bone(arm, pose->proxy_act_bone);
2227 arm->act_bone = bone;
2230 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2231 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2233 /* hurms... loop in a loop, but yah... later... (ton) */
2234 pchan->bone= get_named_bone(arm, pchan->name);
2236 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
2237 if(pchan->bone==NULL)
2239 else if(ob->id.lib==NULL && arm->id.lib) {
2240 /* local pose selection copied to armature, bit hackish */
2241 pchan->bone->flag &= ~BONE_SELECTED;
2242 pchan->bone->flag |= pchan->selectflag;
2247 ob->recalc= OB_RECALC_ALL;
2248 pose->flag |= POSE_RECALC;
2252 static void lib_link_armature(FileData *fd, Main *main)
2256 arm= main->armature.first;
2259 if(arm->id.flag & LIB_NEEDLINK) {
2260 if (arm->adt) lib_link_animdata(fd, &arm->id, arm->adt);
2261 arm->id.flag -= LIB_NEEDLINK;
2267 static void direct_link_bones(FileData *fd, Bone* bone)
2271 bone->parent= newdataadr(fd, bone->parent);
2272 bone->prop= newdataadr(fd, bone->prop);
2274 IDP_DirectLinkProperty(bone->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2276 bone->flag &= ~BONE_DRAW_ACTIVE;
2278 link_list(fd, &bone->childbase);
2280 for(child=bone->childbase.first; child; child=child->next)
2281 direct_link_bones(fd, child);
2284 static void direct_link_armature(FileData *fd, bArmature *arm)
2288 link_list(fd, &arm->bonebase);
2292 arm->adt= newdataadr(fd, arm->adt);
2293 direct_link_animdata(fd, arm->adt);
2295 bone=arm->bonebase.first;
2297 direct_link_bones(fd, bone);
2301 arm->act_bone= newdataadr(fd, arm->act_bone);
2302 arm->act_edbone= NULL;
2305 /* ************ READ CAMERA ***************** */
2307 static void lib_link_camera(FileData *fd, Main *main)
2311 ca= main->camera.first;
2313 if(ca->id.flag & LIB_NEEDLINK) {
2314 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2316 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2318 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2320 ca->id.flag -= LIB_NEEDLINK;
2326 static void direct_link_camera(FileData *fd, Camera *ca)
2328 ca->adt= newdataadr(fd, ca->adt);
2329 direct_link_animdata(fd, ca->adt);
2333 /* ************ READ LAMP ***************** */
2335 static void lib_link_lamp(FileData *fd, Main *main)
2341 la= main->lamp.first;
2343 if(la->id.flag & LIB_NEEDLINK) {
2344 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2346 for(a=0; a<MAX_MTEX; a++) {
2349 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2350 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2354 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2356 la->id.flag -= LIB_NEEDLINK;
2362 static void direct_link_lamp(FileData *fd, Lamp *la)
2366 la->adt= newdataadr(fd, la->adt);
2367 direct_link_animdata(fd, la->adt);
2369 for(a=0; a<MAX_MTEX; a++) {
2370 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2373 la->curfalloff= newdataadr(fd, la->curfalloff);
2375 direct_link_curvemapping(fd, la->curfalloff);
2377 la->preview = direct_link_preview_image(fd, la->preview);
2380 /* ************ READ keys ***************** */
2382 static void lib_link_key(FileData *fd, Main *main)
2386 key= main->key.first;
2388 if(key->id.flag & LIB_NEEDLINK) {
2389 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2391 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2392 key->from= newlibadr(fd, key->id.lib, key->from);
2394 key->id.flag -= LIB_NEEDLINK;
2400 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2403 char *data, *poin, *cp;
2405 elemsize= key->elemsize;
2408 for(a=0; a<kb->totelem; a++) {
2413 while( cp[0] ) { /* cp[0]==amount */
2415 switch(cp[1]) { /* cp[1]= type */
2421 SWITCH_INT((*poin));
2434 static void direct_link_key(FileData *fd, Key *key)
2438 link_list(fd, &(key->block));
2440 key->adt= newdataadr(fd, key->adt);
2441 direct_link_animdata(fd, key->adt);
2443 key->refkey= newdataadr(fd, key->refkey);
2445 kb= key->block.first;
2448 kb->data= newdataadr(fd, kb->data);
2450 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2451 switch_endian_keyblock(key, kb);
2457 /* ************ READ mball ***************** */
2459 static void lib_link_mball(FileData *fd, Main *main)
2464 mb= main->mball.first;
2466 if(mb->id.flag & LIB_NEEDLINK) {
2467 if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
2469 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2471 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2473 mb->id.flag -= LIB_NEEDLINK;
2479 static void direct_link_mball(FileData *fd, MetaBall *mb)
2481 mb->adt= newdataadr(fd, mb->adt);
2482 direct_link_animdata(fd, mb->adt);
2484 mb->mat= newdataadr(fd, mb->mat);
2485 test_pointer_array(fd, (void **)&mb->mat);
2487 link_list(fd, &(mb->elems));
2489 mb->disp.first= mb->disp.last= NULL;
2490 mb->editelems= NULL;
2492 /* mb->edit_elems.first= mb->edit_elems.last= NULL;*/
2496 /* ************ READ WORLD ***************** */
2498 static void lib_link_world(FileData *fd, Main *main)
2504 wrld= main->world.first;
2506 if(wrld->id.flag & LIB_NEEDLINK) {
2507 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2509 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2511 for(a=0; a<MAX_MTEX; a++) {
2512 mtex= wrld->mtex[a];
2514 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2515 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2519 wrld->id.flag -= LIB_NEEDLINK;
2521 wrld= wrld->id.next;
2525 static void direct_link_world(FileData *fd, World *wrld)
2529 wrld->adt= newdataadr(fd, wrld->adt);
2530 direct_link_animdata(fd, wrld->adt);
2532 for(a=0; a<MAX_MTEX; a++) {
2533 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2535 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2539 /* ************ READ VFONT ***************** */
2541 static void lib_link_vfont(FileData *fd, Main *main)
2545 vf= main->vfont.first;
2547 if(vf->id.flag & LIB_NEEDLINK) {
2548 vf->id.flag -= LIB_NEEDLINK;
2554 static void direct_link_vfont(FileData *fd, VFont *vf)
2557 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2560 /* ************ READ TEXT ****************** */
2562 static void lib_link_text(FileData *fd, Main *main)
2566 text= main->text.first;
2568 if(text->id.flag & LIB_NEEDLINK) {
2569 text->id.flag -= LIB_NEEDLINK;
2571 text= text->id.next;
2575 static void direct_link_text(FileData *fd, Text *text)
2579 text->name= newdataadr(fd, text->name);
2582 text->undo_len= TXT_INIT_UNDO;
2583 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2585 text->compiled= NULL;
2588 if(text->flags & TXT_ISEXT) {
2593 link_list(fd, &text->lines);
2594 link_list(fd, &text->markers);
2596 text->curl= newdataadr(fd, text->curl);
2597 text->sell= newdataadr(fd, text->sell);
2599 ln= text->lines.first;
2601 ln->line= newdataadr(fd, ln->line);
2604 if (ln->len != (int) strlen(ln->line)) {
2605 printf("Error loading text, line lengths differ\n");
2606 ln->len = strlen(ln->line);
2612 text->flags = (text->flags) & ~TXT_ISEXT;
2617 /* ************ READ IMAGE ***************** */
2619 static void lib_link_image(FileData *fd, Main *main)
2623 ima= main->image.first;
2625 if(ima->id.flag & LIB_NEEDLINK) {
2626 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2628 ima->id.flag -= LIB_NEEDLINK;
2634 static void link_ibuf_list(FileData *fd, ListBase *lb)
2638 if(lb->first==NULL) return;
2640 lb->first= newimaadr(fd, lb->first);
2644 ln->next= newimaadr(fd, ln->next);
2652 static void direct_link_image(FileData *fd, Image *ima)
2654 /* for undo system, pointers could be restored */
2656 link_ibuf_list(fd, &ima->ibufs);
2658 ima->ibufs.first= ima->ibufs.last= NULL;
2660 /* if not restored, we keep the binded opengl index */
2661 if(ima->ibufs.first==NULL) {
2663 ima->gputexture= NULL;
2669 memset(ima->renders, 0, sizeof(ima->renders));
2670 ima->last_render_slot= ima->render_slot;
2672 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2673 ima->preview = direct_link_preview_image(fd, ima->preview);
2678 /* ************ READ CURVE ***************** */
2680 static void lib_link_curve(FileData *fd, Main *main)
2685 cu= main->curve.first;
2687 if(cu->id.flag & LIB_NEEDLINK) {
2688 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2690 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2692 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2693 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2694 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2695 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2696 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2697 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2698 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2700 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2701 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2703 cu->id.flag -= LIB_NEEDLINK;
2710 static void switch_endian_knots(Nurb *nu)
2717 SWITCH_INT(nu->knotsu[len]);
2723 SWITCH_INT(nu->knotsv[len]);
2728 static void direct_link_curve(FileData *fd, Curve *cu)
2733 cu->adt= newdataadr(fd, cu->adt);
2734 direct_link_animdata(fd, cu->adt);
2736 cu->mat= newdataadr(fd, cu->mat);
2737 test_pointer_array(fd, (void **)&cu->mat);
2738 cu->str= newdataadr(fd, cu->str);
2739 cu->strinfo= newdataadr(fd, cu->strinfo);
2740 cu->tb= newdataadr(fd, cu->tb);
2742 if(cu->vfont==0) link_list(fd, &(cu->nurb));
2744 cu->nurb.first=cu->nurb.last= 0;
2746 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2748 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2755 cu->tb[0].w = cu->linewidth;
2757 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2760 cu->bev.first=cu->bev.last= NULL;
2761 cu->disp.first=cu->disp.last= NULL;
2769 nu->bezt= newdataadr(fd, nu->bezt);
2770 nu->bp= newdataadr(fd, nu->bp);
2771 nu->knotsu= newdataadr(fd, nu->knotsu);
2772 nu->knotsv= newdataadr(fd, nu->knotsv);
2773 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2775 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2776 switch_endian_knots(nu);
2784 /* ************ READ TEX ***************** */
2786 static void lib_link_texture(FileData *fd, Main *main)
2790 tex= main->tex.first;
2792 if(tex->id.flag & LIB_NEEDLINK) {
2793 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
2795 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2796 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2797 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2799 tex->pd->object= newlibadr(fd, tex->id.lib, tex->pd->object);
2800 if(tex->vd) tex->vd->object= newlibadr(fd, tex->id.lib, tex->vd->object);
2803 lib_link_ntree(fd, &tex->id, tex->nodetree);
2805 tex->id.flag -= LIB_NEEDLINK;
2811 static void direct_link_texture(FileData *fd, Tex *tex)
2813 tex->adt= newdataadr(fd, tex->adt);
2814 direct_link_animdata(fd, tex->adt);
2816 tex->plugin= newdataadr(fd, tex->plugin);
2818 tex->plugin->handle= 0;
2819 open_plugin_tex(tex->plugin);
2820 /* initialize data for this instance, if an initialization
2823 if (tex->plugin->instance_init)
2824 tex->plugin->instance_init((void *) tex->plugin->data);
2826 tex->coba= newdataadr(fd, tex->coba);
2827 tex->env= newdataadr(fd, tex->env);
2829 tex->env->ima= NULL;
2830 memset(tex->env->cube, 0, 6*sizeof(void *));
2833 tex->pd= newdataadr(fd, tex->pd);
2835 tex->pd->point_tree = NULL;
2836 tex->pd->coba= newdataadr(fd, tex->pd->coba);
2839 tex->vd= newdataadr(fd, tex->vd);
2841 tex->vd->dataset = NULL;
2845 tex->nodetree= newdataadr(fd, tex->nodetree);
2847 direct_link_nodetree(fd, tex->nodetree);
2849 tex->preview = direct_link_preview_image(fd, tex->preview);
2856 /* ************ READ MATERIAL ***************** */
2858 static void lib_link_material(FileData *fd, Main *main)
2864 ma= main->mat.first;
2866 if(ma->id.flag & LIB_NEEDLINK) {
2867 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
2869 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2870 of library blocks that implement this.*/
2871 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2873 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2874 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2876 for(a=0; a<MAX_MTEX; a++) {
2879 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2880 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2885 lib_link_ntree(fd, &ma->id, ma->nodetree);
2887 ma->id.flag -= LIB_NEEDLINK;
2893 static void direct_link_material(FileData *fd, Material *ma)
2897 ma->adt= newdataadr(fd, ma->adt);
2898 direct_link_animdata(fd, ma->adt);
2900 for(a=0; a<MAX_MTEX; a++) {
2901 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2904 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2905 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2907 ma->nodetree= newdataadr(fd, ma->nodetree);
2909 direct_link_nodetree(fd, ma->nodetree);
2911 ma->preview = direct_link_preview_image(fd, ma->preview);
2912 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
2915 /* ************ READ PARTICLE SETTINGS ***************** */
2917 static void direct_link_pointcache(FileData *fd, PointCache *cache)
2919 if((cache->flag & PTCACHE_DISK_CACHE)==0) {
2923 link_list(fd, &cache->mem_cache);
2925 pm = cache->mem_cache.first;
2927 for(; pm; pm=pm->next) {
2929 pm->index_array = newdataadr(fd, pm->index_array);
2931 for(i=0; i<BPHYS_TOT_DATA; i++) {
2932 if(pm->data[i] && pm->data_types & (1<<i))
2933 pm->data[i] = newdataadr(fd, pm->data[i]);
2938 cache->mem_cache.first = cache->mem_cache.last = NULL;
2940 cache->flag &= ~PTCACHE_SIMULATION_VALID;
2943 cache->free_edit= NULL;
2946 static void direct_link_pointcache_list(FileData *fd, ListBase *ptcaches, PointCache **ocache)
2950 if(ptcaches->first) {
2951 link_list(fd, ptcaches);
2952 for(cache=ptcaches->first; cache; cache=cache->next)
2953 direct_link_pointcache(fd, cache);
2955 *ocache = newdataadr(fd, *ocache);
2958 /* old "single" caches need to be linked too */
2959 *ocache = newdataadr(fd, *ocache);
2960 direct_link_pointcache(fd, *ocache);
2962 ptcaches->first = ptcaches->last = *ocache;
2966 void lib_link_partdeflect(FileData *fd, ID *id, PartDeflect *pd)
2969 pd->tex=newlibadr_us(fd, id->lib, pd->tex);
2972 static void lib_link_particlesettings(FileData *fd, Main *main)
2974 ParticleSettings *part;
2975 ParticleDupliWeight *dw;
2977 part= main->particle.first;
2979 if(part->id.flag & LIB_NEEDLINK) {
2980 if (part->adt) lib_link_animdata(fd, &part->id, part->adt);
2981 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
2983 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
2984 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
2985 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
2986 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
2988 lib_link_partdeflect(fd, &part->id, part->pd);
2989 lib_link_partdeflect(fd, &part->id, part->pd2);
2991 if(part->effector_weights)
2992 part->effector_weights->group = newlibadr(fd, part->id.lib, part->effector_weights->group);
2994 dw = part->dupliweights.first;
2995 for(; dw; dw=dw->next)
2996 dw->ob = newlibadr(fd, part->id.lib, dw->ob);
2999 BoidState *state = part->boids->states.first;
3001 for(; state; state=state->next) {
3002 rule = state->rules.first;
3003 for(; rule; rule=rule->next)
3004 switch(rule->type) {
3005 case eBoidRuleType_Goal:
3006 case eBoidRuleType_Avoid:
3008 BoidRuleGoalAvoid *brga = (BoidRuleGoalAvoid*)rule;
3009 brga->ob = newlibadr(fd, part->id.lib, brga->ob);
3012 case eBoidRuleType_FollowLeader:
3014 BoidRuleFollowLeader *brfl = (BoidRuleFollowLeader*)rule;
3015 brfl->ob = newlibadr(fd, part->id.lib, brfl->ob);
3021 part->id.flag -= LIB_NEEDLINK;
3023 part= part->id.next;
3027 static void direct_link_partdeflect(PartDeflect *pd)
3029 if(pd) pd->rng=NULL;
3032 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
3034 part->adt= newdataadr(fd, part->adt);
3035 part->pd= newdataadr(fd, part->pd);
3036 part->pd2= newdataadr(fd, part->pd2);
3038 direct_link_animdata(fd, part->adt);
3039 direct_link_partdeflect(part->pd);
3040 direct_link_partdeflect(part->pd2);
3042 part->effector_weights = newdataadr(fd, part->effector_weights);
3043 if(!part->effector_weights)
3044 part->effector_weights = BKE_add_effector_weights(part->eff_group);
3046 link_list(fd, &part->dupliweights);
3048 part->boids= newdataadr(fd, part->boids);
3049 part->fluid= newdataadr(fd, part->fluid);
3053 link_list(fd, &part->boids->states);
3055 for(state=part->boids->states.first; state; state=state->next) {
3056 link_list(fd, &state->rules);
3057 link_list(fd, &state->conditions);
3058 link_list(fd, &state->actions);
3063 static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase *particles)
3065 ParticleSystem *psys, *psysnext;
3067 for(psys=particles->first; psys; psys=psysnext){
3068 psysnext= psys->next;
3070 psys->part = newlibadr_us(fd, id->lib, psys->part);
3072 ParticleTarget *pt = psys->targets.first;
3074 for(; pt; pt=pt->next)
3075 pt->ob=newlibadr(fd, id->lib, pt->ob);
3077 psys->parent= newlibadr_us(fd, id->lib, psys->parent);
3078 psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
3081 /* XXX - from reading existing code this seems correct but intended usage of
3082 * pointcache should /w cloth should be added in 'ParticleSystem' - campbell */
3083 psys->clmd->point_cache= psys->pointcache;
3084 psys->clmd->ptcaches.first= psys->clmd->ptcaches.last= NULL;
3085 psys->clmd->coll_parms->group= newlibadr(fd, id->lib, psys->clmd->coll_parms->group);
3089 /* particle modifier must be removed before particle system */
3090 ParticleSystemModifierData *psmd= psys_get_modifier(ob,psys);
3091 BLI_remlink(&ob->modifiers, psmd);
3092 modifier_free((ModifierData *)psmd);
3094 BLI_remlink(particles, psys);
3099 static void direct_link_particlesystems(FileData *fd, ListBase *particles)
3101 ParticleSystem *psys;
3105 for(psys=particles->first; psys; psys=psys->next) {
3106 psys->particles=newdataadr(fd,psys->particles);
3108 if(psys->particles && psys->particles->hair){
3109 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
3110 pa->hair=newdataadr(fd,pa->hair);
3113 if(psys->particles && psys->particles->keys){
3114 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++) {
3119 psys->flag &= ~PSYS_KEYED;
3122 if(psys->particles && psys->particles->boid) {
3123 pa = psys->particles;
3124 pa->boid = newdataadr(fd, pa->boid);
3125 for(a=1,pa++; a<psys->totpart; a++, pa++)
3126 pa->boid = (pa-1)->boid + 1;
3129 for(a=0,pa=psys->particles; a<psys->totpart; a++, pa++)
3134 psys->child = newdataadr(fd,psys->child);
3135 psys->effectors = NULL;
3137 link_list(fd, &psys->targets);
3140 psys->free_edit = NULL;
3141 psys->pathcache = NULL;
3142 psys->childcache = NULL;
3143 psys->pathcachebufs.first = psys->pathcachebufs.last = NULL;
3144 psys->childcachebufs.first = psys->childcachebufs.last = NULL;
3148 direct_link_pointcache_list(fd, &psys->ptcaches, &psys->pointcache);
3151 psys->clmd = newdataadr(fd, psys->clmd);
3152 psys->clmd->clothObject = NULL;
3154 psys->clmd->sim_parms= newdataadr(fd, psys->clmd->sim_parms);
3155 psys->clmd->coll_parms= newdataadr(fd, psys->clmd->coll_parms);
3157 if(psys->clmd->sim_parms) {
3158 if(psys->clmd->sim_parms->presets > 10)
3159 psys->clmd->sim_parms->presets = 0;
3162 psys->hair_in_dm = psys->hair_out_dm = NULL;
3164 psys->clmd->point_cache = psys->pointcache;
3172 /* ************ READ MESH ***************** */
3174 static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
3179 for (i=0; i<totface; i++, tf++) {
3180 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
3181 if(tf->tpage && tf->tpage->id.us==0)
3182 tf->tpage->id.us= 1;
3186 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
3189 for(i=0; i<fdata->totlayer; i++) {
3190 CustomDataLayer *layer = &fdata->layers[i];
3192 if(layer->type == CD_MTFACE)
3193 lib_link_mtface(fd, me, layer->data, totface);
3198 static void lib_link_mesh(FileData *fd, Main *main)
3202 me= main->mesh.first;
3204 if(me->id.flag & LIB_NEEDLINK) {
3207 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3208 of library blocks that implement this.*/
3209 if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3210 if (me->adt) lib_link_animdata(fd, &me->id, me->adt);
3212 /* this check added for python created meshes */
3214 for(i=0; i<me->totcol; i++) {