4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
34 #include "BLI_winstuff.h"
38 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
39 #include <stdlib.h> // for getenv atoi
40 #include <fcntl.h> // for open
41 #include <string.h> // for strrchr strncmp strstr
42 #include <math.h> // for fabs
45 #include <unistd.h> // for read close
46 #include <sys/param.h> // for MAXPATHLEN
48 #include <io.h> // for open close read
51 #include "DNA_anim_types.h"
52 #include "DNA_action_types.h"
53 #include "DNA_armature_types.h"
55 #include "DNA_actuator_types.h"
56 #include "DNA_brush_types.h"
57 #include "DNA_camera_types.h"
58 #include "DNA_cloth_types.h"
59 #include "DNA_color_types.h"
60 #include "DNA_controller_types.h"
61 #include "DNA_constraint_types.h"
62 #include "DNA_curve_types.h"
63 #include "DNA_customdata_types.h"
64 #include "DNA_effect_types.h"
65 #include "DNA_fileglobal_types.h"
66 #include "DNA_genfile.h"
67 #include "DNA_group_types.h"
68 #include "DNA_gpencil_types.h"
69 #include "DNA_ipo_types.h"
70 #include "DNA_image_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_modifier_types.h"
79 #include "DNA_nla_types.h"
80 #include "DNA_node_types.h"
81 #include "DNA_object_types.h"
82 #include "DNA_object_force.h"
83 #include "DNA_object_fluidsim.h" // NT
84 #include "DNA_outliner_types.h"
85 #include "DNA_object_force.h"
86 #include "DNA_packedFile_types.h"
87 #include "DNA_particle_types.h"
88 #include "DNA_property_types.h"
89 #include "DNA_text_types.h"
90 #include "DNA_view3d_types.h"
91 #include "DNA_screen_types.h"
92 #include "DNA_sensor_types.h"
93 #include "DNA_sdna_types.h"
94 #include "DNA_scene_types.h"
95 #include "DNA_sequence_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_arithb.h"
107 #include "BLI_storage_types.h" // for relname flags
109 #include "BKE_animsys.h"
110 #include "BKE_action.h"
111 #include "BKE_armature.h"
112 #include "BKE_brush.h"
113 #include "BKE_cdderivedmesh.h"
114 #include "BKE_cloth.h"
115 #include "BKE_colortools.h"
116 #include "BKE_constraint.h"
117 #include "BKE_curve.h"
118 #include "BKE_customdata.h"
119 #include "BKE_deform.h"
120 #include "BKE_depsgraph.h"
121 #include "BKE_effect.h" /* give_parteff */
122 #include "BKE_fcurve.h"
123 #include "BKE_global.h" // for G
124 #include "BKE_group.h"
125 #include "BKE_image.h"
127 #include "BKE_key.h" //void set_four_ipo
128 #include "BKE_lattice.h"
129 #include "BKE_library.h" // for wich_libbase
130 #include "BKE_main.h" // for Main
131 #include "BKE_mesh.h" // for ME_ defines (patching)
132 #include "BKE_modifier.h"
133 #include "BKE_multires.h"
134 #include "BKE_node.h" // for tree type defines
135 #include "BKE_object.h"
136 #include "BKE_particle.h"
137 #include "BKE_pointcache.h"
138 #include "BKE_property.h" // for get_ob_property
139 #include "BKE_report.h"
140 #include "BKE_sca.h" // for init_actuator
141 #include "BKE_scene.h"
142 #include "BKE_softbody.h" // sbNew()
143 #include "BKE_bullet.h" // bsbNew()
144 #include "BKE_sculpt.h"
145 #include "BKE_sequence.h"
146 #include "BKE_texture.h" // for open_plugin_tex
147 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
148 #include "BKE_idprop.h"
150 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
151 //XXX #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
152 //XXX #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
153 #include "BLO_readfile.h"
154 #include "BLO_undofile.h"
155 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
157 #include "readfile.h"
162 Remark: still a weak point is the newadress() function, that doesnt solve reading from
163 multiple files at the same time
165 (added remark: oh, i thought that was solved? will look at that... (ton)
168 - Existing Library (Main) push or free
178 - read associated 'direct data'
179 - link direct data (internal and to LibBlock)
181 - read USER data, only when indicated (file is ~/.B.blend)
183 - per Library (per Main)
186 - find LibBlocks and attach IDs to Main
187 - if external LibBlock
189 - or it's already read,
194 - read associated direct data
195 - link direct data (internal and to LibBlock)
197 - per Library with unread LibBlocks
202 - read associated direct data
203 - link direct data (internal and to LibBlock)
206 - link all LibBlocks and indirect pointers to libblocks
207 - initialize FileGlobal and copy pointers to Global
210 /* also occurs in library.c */
211 /* GS reads the memory pointed at in a specific ordering. There are,
212 * however two definitions for it. I have jotted them down here, both,
213 * but I think the first one is actually used. The thing is that
214 * big-endian systems might read this the wrong way round. OTOH, we
215 * constructed the IDs that are read out with this macro explicitly as
216 * well. I expect we'll sort it out soon... */
219 #define GS(a) (*((short *)(a)))
221 /* from misc_util: flip the bytes from x */
222 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
224 // only used here in readfile.c
225 #define SWITCH_LONGINT(a) { \
228 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
229 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
230 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
231 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
235 typedef struct OldNew {
240 typedef struct OldNewMap {
242 int nentries, entriessize;
248 /* local prototypes */
249 static void *read_struct(FileData *fd, BHead *bh, char *blockname);
252 static OldNewMap *oldnewmap_new(void)
254 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
256 onm->entriessize= 1024;
257 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
262 static int verg_oldnewmap(const void *v1, const void *v2)
264 const struct OldNew *x1=v1, *x2=v2;
266 if( x1->old > x2->old) return 1;
267 else if( x1->old < x2->old) return -1;
272 static void oldnewmap_sort(FileData *fd)
274 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
275 fd->libmap->sorted= 1;
278 /* nr is zero for data, and ID code for libdata */
279 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
283 if(oldaddr==NULL || newaddr==NULL) return;
285 if (onm->nentries==onm->entriessize) {
286 int osize= onm->entriessize;
287 OldNew *oentries= onm->entries;
289 onm->entriessize*= 2;
290 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
292 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
296 entry= &onm->entries[onm->nentries++];
298 entry->newp= newaddr;
302 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
306 if (onm->lasthit<onm->nentries-1) {
307 OldNew *entry= &onm->entries[++onm->lasthit];
309 if (entry->old==addr) {
315 for (i=0; i<onm->nentries; i++) {
316 OldNew *entry= &onm->entries[i];
318 if (entry->old==addr) {
329 /* for libdata, nr has ID code, no increment */
330 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
334 if(addr==NULL) return NULL;
336 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
338 OldNew entry_s, *entry;
342 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
346 if (id && (!lib || id->lib)) {
352 for (i=0; i<onm->nentries; i++) {
353 OldNew *entry= &onm->entries[i];
355 if (entry->old==addr) {
358 if (id && (!lib || id->lib)) {
367 static void oldnewmap_free_unused(OldNewMap *onm)
371 for (i=0; i<onm->nentries; i++) {
372 OldNew *entry= &onm->entries[i];
374 MEM_freeN(entry->newp);
380 static void oldnewmap_clear(OldNewMap *onm)
386 static void oldnewmap_free(OldNewMap *onm)
388 MEM_freeN(onm->entries);
394 static void read_libraries(FileData *basefd, ListBase *mainlist);
396 /* ************ help functions ***************** */
398 static void add_main_to_main(Main *mainvar, Main *from)
400 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
403 a= set_listbasepointers(mainvar, lbarray);
404 a= set_listbasepointers(from, fromarray);
406 addlisttolist(lbarray[a], fromarray[a]);
410 void blo_join_main(ListBase *mainlist)
412 Main *tojoin, *mainl;
415 mainl= mainlist->first;
416 while ((tojoin= mainl->next)) {
417 add_main_to_main(mainl, tojoin);
418 BLI_remlink(mainlist, tojoin);
423 static void split_libdata(ListBase *lb, Main *first)
435 if(mainvar->curlib==id->lib) {
436 lbn= wich_libbase(mainvar, GS(id->name));
438 BLI_addtail(lbn, id);
441 mainvar= mainvar->next;
443 if(mainvar==0) printf("error split_libdata\n");
449 void blo_split_main(ListBase *mainlist, Main *main)
451 ListBase *lbarray[MAX_LIBARRAY];
455 mainlist->first= mainlist->last= main;
458 if(main->library.first==NULL)
461 for (lib= main->library.first; lib; lib= lib->id.next) {
462 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
463 libmain->curlib= lib;
464 BLI_addtail(mainlist, libmain);
467 i= set_listbasepointers(main, lbarray);
469 split_libdata(lbarray[i], main->next);
472 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
473 static void cleanup_path(const char *relabase, char *name)
475 char filename[FILE_MAXFILE];
477 BLI_splitdirstring(name, filename);
478 BLI_cleanup_dir(relabase, name);
479 strcat(name, filename);
482 static void read_file_version(FileData *fd, Main *main)
486 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
487 if (bhead->code==GLOB) {
488 FileGlobal *fg= read_struct(fd, bhead, "Global");
490 main->subversionfile= fg->subversion;
491 main->minversionfile= fg->minversion;
492 main->minsubversionfile= fg->minsubversion;
495 else if (bhead->code==ENDB)
502 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *name, const char *relabase)
506 char name1[FILE_MAXDIR+FILE_MAXFILE];
508 strncpy(name1, name, sizeof(name1)-1);
509 cleanup_path(relabase, name1);
510 // printf("blo_find_main: original in %s\n", name);
511 // printf("blo_find_main: converted to %s\n", name1);
513 for (m= mainlist->first; m; m= m->next) {
514 char *libname= (m->curlib)?m->curlib->filename:m->name;
516 if (BLI_streq(name1, libname)) {
517 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
522 m= MEM_callocN(sizeof(Main), "find_main");
523 BLI_addtail(mainlist, m);
525 lib= alloc_libblock(&m->library, ID_LI, "lib");
526 strncpy(lib->name, name, sizeof(lib->name)-1);
527 BLI_strncpy(lib->filename, name1, sizeof(lib->filename));
531 read_file_version(fd, m);
533 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", name);
538 /* ************ FILE PARSING ****************** */
540 static void switch_endian_bh4(BHead4 *bhead)
542 /* the ID_.. codes */
543 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
545 if (bhead->code != ENDB) {
546 SWITCH_INT(bhead->len);
547 SWITCH_INT(bhead->SDNAnr);
548 SWITCH_INT(bhead->nr);
552 static void switch_endian_bh8(BHead8 *bhead)
554 /* the ID_.. codes */
555 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
557 if (bhead->code != ENDB) {
558 SWITCH_INT(bhead->len);
559 SWITCH_INT(bhead->SDNAnr);
560 SWITCH_INT(bhead->nr);
564 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
566 BHead4 *bhead4 = (BHead4 *) bhead;
567 #if defined(WIN32) && !defined(FREE_WINDOWS)
573 bhead4->code= bhead8->code;
574 bhead4->len= bhead8->len;
576 if (bhead4->code != ENDB) {
578 // why is this here ??
579 if (do_endian_swap) {
580 SWITCH_LONGINT(bhead8->old);
583 /* this patch is to avoid a long long being read from not-eight aligned positions
584 is necessary on any modern 64bit architecture) */
585 memcpy(&old, &bhead8->old, 8);
586 bhead4->old = (int) (old >> 3);
588 bhead4->SDNAnr= bhead8->SDNAnr;
589 bhead4->nr= bhead8->nr;
593 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
595 BHead8 *bhead8 = (BHead8 *) bhead;
597 bhead8->code= bhead4->code;
598 bhead8->len= bhead4->len;
600 if (bhead8->code != ENDB) {
601 bhead8->old= bhead4->old;
602 bhead8->SDNAnr= bhead4->SDNAnr;
603 bhead8->nr= bhead4->nr;
607 static BHeadN *get_bhead(FileData *fd)
612 BHeadN *new_bhead = 0;
618 // First read the bhead structure.
619 // Depending on the platform the file was written on this can
620 // be a big or little endian BHead4 or BHead8 structure.
622 // As usual 'ENDB' (the last *partial* bhead of the file)
623 // needs some special handling. We don't want to EOF just yet.
625 if (fd->flags & FD_FLAGS_FILE_POINTSIZE_IS_4) {
627 readsize = fd->read(fd, &bhead4, sizeof(bhead4));
629 if (readsize == sizeof(bhead4) || bhead4.code == ENDB) {
630 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
631 switch_endian_bh4(&bhead4);
634 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
635 bh8_from_bh4(&bhead, &bhead4);
637 memcpy(&bhead, &bhead4, sizeof(bhead));
645 readsize = fd->read(fd, &bhead8, sizeof(bhead8));
647 if (readsize == sizeof(bhead8) || bhead8.code == ENDB) {
648 if (fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
649 switch_endian_bh8(&bhead8);
652 if (fd->flags & FD_FLAGS_POINTSIZE_DIFFERS) {
653 bh4_from_bh8(&bhead, &bhead8, (fd->flags & FD_FLAGS_SWITCH_ENDIAN));
655 memcpy(&bhead, &bhead8, sizeof(bhead));
663 /* make sure people are not trying to pass bad blend files */
664 if (bhead.len < 0) fd->eof = 1;
666 // bhead now contains the (converted) bhead structure. Now read
667 // the associated data and put everything in a BHeadN (creative naming !)
670 new_bhead = MEM_mallocN(sizeof(BHeadN) + bhead.len, "new_bhead");
672 new_bhead->next = new_bhead->prev = 0;
673 new_bhead->bhead = bhead;
675 readsize = fd->read(fd, new_bhead + 1, bhead.len);
677 if (readsize != bhead.len) {
679 MEM_freeN(new_bhead);
689 // We've read a new block. Now add it to the list
693 BLI_addtail(&fd->listbase, new_bhead);
699 BHead *blo_firstbhead(FileData *fd)
705 // Read in a new block if necessary
707 new_bhead = fd->listbase.first;
708 if (new_bhead == 0) {
709 new_bhead = get_bhead(fd);
713 bhead = &new_bhead->bhead;
719 BHead *blo_prevbhead(FileData *fd, BHead *thisblock)
721 BHeadN *bheadn= (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
722 BHeadN *prev= bheadn->prev;
724 return prev?&prev->bhead:NULL;
727 BHead *blo_nextbhead(FileData *fd, BHead *thisblock)
729 BHeadN *new_bhead = NULL;
733 // bhead is actually a sub part of BHeadN
734 // We calculate the BHeadN pointer from the BHead pointer below
735 new_bhead = (BHeadN *) (((char *) thisblock) - GET_INT_FROM_POINTER( &((BHeadN*)0)->bhead) );
737 // get the next BHeadN. If it doesn't exist we read in the next one
738 new_bhead = new_bhead->next;
739 if (new_bhead == 0) {
740 new_bhead = get_bhead(fd);
745 // here we do the reverse:
746 // go from the BHeadN pointer to the BHead pointer
747 bhead = &new_bhead->bhead;
753 static void decode_blender_header(FileData *fd)
755 char header[SIZEOFBLENDERHEADER], num[4];
758 // read in the header data
759 readsize = fd->read(fd, header, sizeof(header));
761 if (readsize == sizeof(header)) {
762 if(strncmp(header, "BLENDER", 7) == 0) {
763 int remove_this_endian_test= 1;
765 fd->flags |= FD_FLAGS_FILE_OK;
767 // what size are pointers in the file ?
769 fd->flags |= FD_FLAGS_FILE_POINTSIZE_IS_4;
770 if (sizeof(void *) != 4) {
771 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
774 if (sizeof(void *) != 8) {
775 fd->flags |= FD_FLAGS_POINTSIZE_DIFFERS;
779 // is the file saved in a different endian
781 if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
782 fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
785 // get the version number
787 memcpy(num, header+9, 3);
789 fd->fileversion = atoi(num);
794 static int read_file_dna(FileData *fd)
798 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
799 if (bhead->code==DNA1) {
800 int do_endian_swap= (fd->flags&FD_FLAGS_SWITCH_ENDIAN)?1:0;
802 fd->filesdna= DNA_sdna_from_data(&bhead[1], bhead->len, do_endian_swap);
805 fd->compflags= DNA_struct_get_compareflags(fd->filesdna, fd->memsdna);
806 /* used to retrieve ID names from (bhead+1) */
807 fd->id_name_offs= DNA_elem_offset(fd->filesdna, "ID", "char", "name[]");
811 } else if (bhead->code==ENDB)
818 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
820 int readsize = read(filedata->filedes, buffer, size);
825 filedata->seek += readsize;
831 static int fd_read_gzip_from_file(FileData *filedata, void *buffer, unsigned int size)
833 int readsize = gzread(filedata->gzfiledes, buffer, size);
838 filedata->seek += readsize;
844 static int fd_read_from_memory(FileData *filedata, void *buffer, unsigned int size)
846 // don't read more bytes then there are available in the buffer
847 int readsize = MIN2(size, filedata->buffersize - filedata->seek);
849 memcpy(buffer, filedata->buffer + filedata->seek, readsize);
850 filedata->seek += readsize;
855 static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int size)
857 static unsigned int seek= 1<<30; /* the current position */
858 static unsigned int offset= 0; /* size of previous chunks */
859 static MemFileChunk *chunk=NULL;
860 unsigned int chunkoffset, readsize, totread;
862 if(size==0) return 0;
864 if(seek != (unsigned int)filedata->seek) {
865 chunk= filedata->memfile->chunks.first;
869 if(seek + chunk->size > (unsigned) filedata->seek) break;
874 seek= filedata->seek;
881 /* first check if it's on the end if current chunk */
882 if(seek-offset == chunk->size) {
883 offset+= chunk->size;
887 /* debug, should never happen */
889 printf("illegal read, chunk zero\n");
893 chunkoffset= seek-offset;
894 readsize= size-totread;
896 /* data can be spread over multiple chunks, so clamp size
897 * to within this chunk, and then it will read further in
899 if(chunkoffset+readsize > chunk->size)
900 readsize= chunk->size-chunkoffset;
902 memcpy((char*)buffer+totread, chunk->buf+chunkoffset, readsize);
904 filedata->seek += readsize;
906 } while(totread < size);
914 static FileData *filedata_new(void)
916 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
919 fd->gzfiledes = NULL;
921 /* XXX, this doesn't need to be done all the time,
922 * but it keeps us reentrant, remove once we have
923 * a lib that provides a nice lock. - zr
925 fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, 0);
927 fd->datamap = oldnewmap_new();
928 fd->globmap = oldnewmap_new();
929 fd->libmap = oldnewmap_new();
934 static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
936 decode_blender_header(fd);
938 if (fd->flags & FD_FLAGS_FILE_OK) {
939 if (!read_file_dna(fd)) {
940 BKE_report(reports, RPT_ERROR, "File incomplete");
941 blo_freefiledata(fd);
946 BKE_report(reports, RPT_ERROR, "File is not a Blender file");
947 blo_freefiledata(fd);
954 /* cannot be called with relative paths anymore! */
955 /* on each new library added, it now checks for the current FileData and expands relativeness */
956 FileData *blo_openblenderfile(char *name, ReportList *reports)
960 gzfile= gzopen(name, "rb");
962 if (NULL == gzfile) {
963 BKE_report(reports, RPT_ERROR, "Unable to open");
966 FileData *fd = filedata_new();
967 fd->gzfiledes = gzfile;
968 fd->read = fd_read_gzip_from_file;
970 /* needed for library_append and read_libraries */
971 BLI_strncpy(fd->filename, name, sizeof(fd->filename));
973 return blo_decode_and_check(fd, reports);
977 FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
979 if (!mem || memsize<SIZEOFBLENDERHEADER) {
980 BKE_report(reports, RPT_ERROR, (mem)? "Unable to read": "Unable to open");
983 FileData *fd= filedata_new();
985 fd->buffersize= memsize;
986 fd->read= fd_read_from_memory;
987 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
989 return blo_decode_and_check(fd, reports);
993 FileData *blo_openblendermemfile(MemFile *memfile, ReportList *reports)
996 BKE_report(reports, RPT_ERROR, "Unable to open");
999 FileData *fd= filedata_new();
1000 fd->memfile= memfile;
1002 fd->read= fd_read_from_memfile;
1003 fd->flags|= FD_FLAGS_NOT_MY_BUFFER;
1005 return blo_decode_and_check(fd, reports);
1010 void blo_freefiledata(FileData *fd)
1014 if (fd->filedes != -1) {
1018 if (fd->gzfiledes != NULL)
1020 gzclose(fd->gzfiledes);
1023 if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
1024 MEM_freeN(fd->buffer);
1028 // Free all BHeadN data blocks
1029 BLI_freelistN(&fd->listbase);
1032 DNA_sdna_free(fd->memsdna);
1034 DNA_sdna_free(fd->filesdna);
1036 MEM_freeN(fd->compflags);
1039 oldnewmap_free(fd->datamap);
1041 oldnewmap_free(fd->globmap);
1043 oldnewmap_free(fd->imamap);
1044 if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP))
1045 oldnewmap_free(fd->libmap);
1047 MEM_freeN(fd->bheadmap);
1053 /* ************ DIV ****************** */
1055 int BLO_has_bfile_extension(char *str)
1057 return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz"));
1060 /* ************** OLD POINTERS ******************* */
1062 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1064 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1067 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1069 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1072 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1074 if(fd->imamap && adr)
1075 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1080 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1082 return oldnewmap_liblookup(fd->libmap, adr, lib);
1085 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1087 ID *id= newlibadr(fd, lib, adr);
1095 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1099 for (i=0; i<fd->libmap->nentries; i++) {
1100 OldNew *entry= &fd->libmap->entries[i];
1102 if (old==entry->newp && entry->nr==ID_ID) {
1104 if(new) entry->nr= GS( ((ID *)new)->name );
1110 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1114 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1117 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1121 change_idid_adr_fd(fd, old, new);
1126 /* lib linked proxy objects point to our local data, we need
1127 * to clear that pointer before reading the undo memfile since
1128 * the object might be removed, it is set again in reading
1129 * if the local object still exists */
1130 void blo_clear_proxy_pointers_from_lib(FileData *fd, Main *oldmain)
1132 Object *ob= oldmain->object.first;
1134 for(;ob; ob= ob->id.next)
1136 ob->proxy_from= NULL;
1139 void blo_make_image_pointer_map(FileData *fd, Main *oldmain)
1141 Image *ima= oldmain->image.first;
1142 Scene *sce= oldmain->scene.first;
1144 fd->imamap= oldnewmap_new();
1146 for(;ima; ima= ima->id.next) {
1147 Link *ibuf= ima->ibufs.first;
1148 for(; ibuf; ibuf= ibuf->next)
1149 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1151 oldnewmap_insert(fd->imamap, ima->gputexture, ima->gputexture, 0);
1153 for(; sce; sce= sce->id.next) {
1156 for(node= sce->nodetree->nodes.first; node; node= node->next)
1157 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1162 /* set old main image ibufs to zero if it has been restored */
1163 /* this works because freeing old main only happens after this call */
1164 void blo_end_image_pointer_map(FileData *fd, Main *oldmain)
1166 OldNew *entry= fd->imamap->entries;
1167 Image *ima= oldmain->image.first;
1168 Scene *sce= oldmain->scene.first;
1171 /* used entries were restored, so we put them to zero */
1172 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1177 for(;ima; ima= ima->id.next) {
1180 /* this mirrors direct_link_image */
1181 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1183 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1184 BLI_remlink(&ima->ibufs, ibuf);
1186 ima->gputexture= NULL;
1190 ima->gputexture= newimaadr(fd, ima->gputexture);
1192 for(; sce; sce= sce->id.next) {
1195 for(node= sce->nodetree->nodes.first; node; node= node->next)
1196 node->preview= newimaadr(fd, node->preview);
1201 /* undo file support: add all library pointers in lookup */
1202 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1204 Main *ptr= mainlist->first;
1205 ListBase *lbarray[MAX_LIBARRAY];
1207 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1208 int i= set_listbasepointers(ptr, lbarray);
1211 for(id= lbarray[i]->first; id; id= id->next)
1212 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1218 /* ********** END OLD POINTERS ****************** */
1219 /* ********** READ FILE ****************** */
1221 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1223 int blocksize, nblocks;
1226 data= (char *)(bhead+1);
1227 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1231 DNA_struct_switch_endian(filesdna, bhead->SDNAnr, data);
1237 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1242 /* switch is based on file dna */
1243 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1244 switch_endian_structs(fd->filesdna, bh);
1246 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1247 if(fd->compflags[bh->SDNAnr]==2) {
1248 temp= DNA_struct_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1250 temp= MEM_mallocN(bh->len, blockname);
1251 memcpy(temp, (bh+1), bh->len);
1259 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1263 if(lb->first==NULL) return;
1265 lb->first= newdataadr(fd, lb->first);
1269 ln->next= newdataadr(fd, ln->next);
1277 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1282 if(lb->first==0) return;
1283 poin= newdataadr(fd, lb->first);
1285 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1292 poin= newdataadr(fd, ln->next);
1294 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1304 static void test_pointer_array(FileData *fd, void **mat)
1306 #if defined(WIN32) && !defined(FREE_WINDOWS)
1307 __int64 *lpoin, *lmat;
1309 long long *lpoin, *lmat;
1311 int len, *ipoin, *imat;
1313 /* manually convert the pointer array in
1314 * the old dna format to a pointer array in
1315 * the new dna format.
1318 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1320 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1321 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1325 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1326 SWITCH_LONGINT(*lpoin);
1327 *ipoin= (int) ((*lpoin) >> 3);
1335 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1336 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1350 /* ************ READ ID Properties *************** */
1352 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1353 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd);
1355 static void IDP_DirectLinkIDPArray(IDProperty *prop, int switch_endian, FileData *fd)
1360 /*since we didn't save the extra buffer, set totallen to len.*/
1361 prop->totallen = prop->len;
1362 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1364 if (switch_endian) {
1365 test_pointer_array(fd, prop->data.pointer);
1366 array= (IDProperty**) prop->data.pointer;
1368 for(i=0; i<prop->len; i++)
1369 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1373 static void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, FileData *fd)
1378 /*since we didn't save the extra buffer, set totallen to len.*/
1379 prop->totallen = prop->len;
1380 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1382 if (switch_endian) {
1383 if(prop->subtype == IDP_GROUP) {
1384 test_pointer_array(fd, prop->data.pointer);
1385 array= prop->data.pointer;
1387 for(i=0; i<prop->len; i++)
1388 IDP_DirectLinkProperty(array[i], switch_endian, fd);
1390 else if(prop->subtype == IDP_DOUBLE) {
1391 for (i=0; i<prop->len; i++) {
1392 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1395 for (i=0; i<prop->len; i++) {
1396 SWITCH_INT(((int*)prop->data.pointer)[i]);
1402 static void IDP_DirectLinkString(IDProperty *prop, int switch_endian, FileData *fd)
1404 /*since we didn't save the extra string buffer, set totallen to len.*/
1405 prop->totallen = prop->len;
1406 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1409 static void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, FileData *fd)
1411 ListBase *lb = &prop->data.group;
1416 /*Link child id properties now*/
1417 for (loop=prop->data.group.first; loop; loop=loop->next) {
1418 IDP_DirectLinkProperty(loop, switch_endian, fd);
1422 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1424 switch (prop->type) {
1426 IDP_DirectLinkGroup(prop, switch_endian, fd);
1429 IDP_DirectLinkString(prop, switch_endian, fd);
1432 IDP_DirectLinkArray(prop, switch_endian, fd);
1435 IDP_DirectLinkIDPArray(prop, switch_endian, fd);
1438 /*erg, stupid doubles. since I'm storing them
1439 in the same field as int val; val2 in the
1440 IDPropertyData struct, they have to deal with
1441 endianness specifically
1443 in theory, val and val2 would've already been swapped
1444 if switch_endian is true, so we have to first unswap
1445 them then reswap them as a single 64-bit entity.
1448 if (switch_endian) {
1449 SWITCH_INT(prop->data.val);
1450 SWITCH_INT(prop->data.val2);
1451 SWITCH_LONGINT(prop->data.val);
1459 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, FileData *fd)
1463 /* ************ READ CurveMapping *************** */
1465 /* cuma itself has been read! */
1466 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1470 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1471 cumap->flag &= ~CUMA_PREMULLED;
1473 for(a=0; a<CM_TOT; a++) {
1474 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1475 cumap->cm[a].table= NULL;
1479 /* ************ READ Brush *************** */
1480 /* library brush linking after fileread */
1481 static void lib_link_brush(FileData *fd, Main *main)
1487 /* only link ID pointers */
1488 for(brush= main->brush.first; brush; brush= brush->id.next) {
1489 if(brush->id.flag & LIB_NEEDLINK) {
1490 brush->id.flag -= LIB_NEEDLINK;
1492 for(a=0; a<MAX_MTEX; a++) {
1493 mtex= brush->mtex[a];
1495 mtex->tex= newlibadr_us(fd, brush->id.lib, mtex->tex);
1498 brush->clone.image= newlibadr_us(fd, brush->id.lib, brush->clone.image);
1503 static void direct_link_brush(FileData *fd, Brush *brush)
1505 /* brush itself has been read */
1508 for(a=0; a<MAX_MTEX; a++)
1509 brush->mtex[a]= newdataadr(fd, brush->mtex[a]);
1512 brush->curve= newdataadr(fd, brush->curve);
1514 direct_link_curvemapping(fd, brush->curve);
1516 brush_curve_preset(brush, BRUSH_PRESET_SHARP);
1519 static void direct_link_script(FileData *fd, Script *script)
1522 SCRIPT_SET_NULL(script)
1526 /* ************ READ PACKEDFILE *************** */
1528 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1530 PackedFile *pf= newdataadr(fd, oldpf);
1533 pf->data= newdataadr(fd, pf->data);
1539 /* ************ READ IMAGE PREVIEW *************** */
1541 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1543 PreviewImage *prv= newdataadr(fd, old_prv);
1547 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1549 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1557 /* ************ READ SCRIPTLINK *************** */
1559 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1563 for(i=0; i<slink->totscript; i++) {
1564 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1568 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1570 slink->scripts= newdataadr(fd, slink->scripts);
1571 test_pointer_array(fd, (void **)&slink->scripts);
1573 slink->flag= newdataadr(fd, slink->flag);
1575 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1578 for(a=0; a<slink->totscript; a++) {
1579 SWITCH_SHORT(slink->flag[a]);
1584 /* ************ READ ANIMATION STUFF ***************** */
1586 /* Legacy Data Support (for Version Patching) ----------------------------- */
1588 // XXX depreceated - old animation system
1589 static void lib_link_ipo(FileData *fd, Main *main)
1593 ipo= main->ipo.first;
1595 if(ipo->id.flag & LIB_NEEDLINK) {
1597 for(icu= ipo->curve.first; icu; icu= icu->next) {
1599 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
1601 ipo->id.flag -= LIB_NEEDLINK;
1607 // XXX depreceated - old animation system
1608 static void direct_link_ipo(FileData *fd, Ipo *ipo)
1612 link_list(fd, &(ipo->curve));
1613 icu= ipo->curve.first;
1615 icu->bezt= newdataadr(fd, icu->bezt);
1616 icu->bp= newdataadr(fd, icu->bp);
1617 icu->driver= newdataadr(fd, icu->driver);
1622 // XXX depreceated - old animation system
1623 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1625 bActionStrip *strip;
1626 bActionModifier *amod;
1628 for (strip=striplist->first; strip; strip=strip->next){
1629 strip->object = newlibadr(fd, id->lib, strip->object);
1630 strip->act = newlibadr_us(fd, id->lib, strip->act);
1631 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1632 for(amod= strip->modifiers.first; amod; amod= amod->next)
1633 amod->ob= newlibadr(fd, id->lib, amod->ob);
1637 // XXX depreceated - old animation system
1638 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
1640 bActionStrip *strip;
1642 link_list(fd, strips);
1644 for(strip= strips->first; strip; strip= strip->next)
1645 link_list(fd, &strip->modifiers);
1648 // XXX depreceated - old animation system
1649 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1651 bConstraintChannel *chan;
1653 for (chan=chanbase->first; chan; chan=chan->next){
1654 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1658 /* Data Linking ----------------------------- */
1660 static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
1665 /* relink ID-block references... */
1666 for (fcu= list->first; fcu; fcu= fcu->next) {
1669 ChannelDriver *driver= fcu->driver;
1670 driver->id= newlibadr(fd, id->lib, driver->id);
1671 driver->id2= newlibadr(fd, id->lib, driver->id2);
1675 for (fcm= fcu->modifiers.first; fcm; fcm= fcm->next) {
1676 /* data for specific modifiers */
1677 switch (fcm->type) {
1678 case FMODIFIER_TYPE_PYTHON:
1680 FMod_Python *data= (FMod_Python *)fcm->data;
1681 data->script = newlibadr(fd, id->lib, data->script);
1689 /* NOTE: this assumes that link_list has already been called on the list */
1690 static void direct_link_fcurves(FileData *fd, ListBase *list)
1695 /* link F-Curve data to F-Curve again (non ID-libs) */
1696 for (fcu= list->first; fcu; fcu= fcu->next) {
1698 fcu->bezt= newdataadr(fd, fcu->bezt);
1699 fcu->fpt= newdataadr(fd, fcu->fpt);
1702 fcu->rna_path= newdataadr(fd, fcu->rna_path);
1705 fcu->grp= newdataadr(fd, fcu->grp);
1708 fcu->driver= newdataadr(fd, fcu->driver);
1710 ChannelDriver *driver= fcu->driver;
1712 driver->rna_path= newdataadr(fd, driver->rna_path);
1713 driver->rna_path2= newdataadr(fd, driver->rna_path2);
1717 link_list(fd, &fcu->modifiers);
1718 for (fcm= fcu->modifiers.first; fcm; fcm= fcm->next) {
1719 /* relink general data */
1720 fcm->data = newdataadr(fd, fcm->data);
1722 /* do relinking of data for specific types */
1723 switch (fcm->type) {
1724 case FMODIFIER_TYPE_GENERATOR:
1726 FMod_Generator *data= (FMod_Generator *)fcm->data;
1728 data->coefficients= newdataadr(fd, data->coefficients);
1731 case FMODIFIER_TYPE_ENVELOPE:
1733 FMod_Envelope *data= (FMod_Envelope *)fcm->data;
1735 data->data= newdataadr(fd, data->data);
1738 case FMODIFIER_TYPE_PYTHON:
1740 FMod_Python *data= (FMod_Python *)fcm->data;
1742 data->prop = newdataadr(fd, data->prop);
1743 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1752 static void lib_link_action(FileData *fd, Main *main)
1755 bActionChannel *chan;
1757 for (act= main->action.first; act; act= act->id.next) {
1758 if (act->id.flag & LIB_NEEDLINK) {
1759 act->id.flag -= LIB_NEEDLINK;
1761 // XXX depreceated - old animation system <<<
1762 for (chan=act->chanbase.first; chan; chan=chan->next) {
1763 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1764 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1766 // >>> XXX depreceated - old animation system
1768 lib_link_fcurves(fd, &act->id, &act->curves);
1773 static void direct_link_action(FileData *fd, bAction *act)
1775 bActionChannel *achan; // XXX depreceated - old animation system
1778 link_list(fd, &act->curves);
1779 link_list(fd, &act->chanbase); // XXX depreceated - old animation system
1780 link_list(fd, &act->groups);
1781 link_list(fd, &act->markers);
1783 // XXX depreceated - old animation system <<<
1784 for (achan = act->chanbase.first; achan; achan=achan->next) {
1785 achan->grp= newdataadr(fd, achan->grp);
1787 link_list(fd, &achan->constraintChannels);
1789 // >>> XXX depreceated - old animation system
1791 direct_link_fcurves(fd, &act->curves);
1793 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1794 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1795 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1801 static void lib_link_keyingsets(FileData *fd, ID *id, ListBase *list)
1806 /* here, we're only interested in the ID pointer stored in some of the paths */
1807 for (ks= list->first; ks; ks= ks->next) {
1808 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1809 ksp->id= newlibadr(fd, id->lib, ksp->id);
1814 /* NOTE: this assumes that link_list has already been called on the list */
1815 static void direct_link_keyingsets(FileData *fd, ListBase *list)
1820 /* link KeyingSet data to KeyingSet again (non ID-libs) */
1821 for (ks= list->first; ks; ks= ks->next) {
1823 link_list(fd, &ks->paths);
1825 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1827 ksp->rna_path= newdataadr(fd, ksp->rna_path);
1834 static void lib_link_animdata(FileData *fd, ID *id, AnimData *adt)
1839 /* link action data */
1840 adt->action= newlibadr_us(fd, id->lib, adt->action);
1843 lib_link_fcurves(fd, id, &adt->drivers);
1845 /* overrides don't have lib-link for now, so no need to do anything */
1851 static void direct_link_animdata(FileData *fd, AnimData *adt)
1853 /* NOTE: must have called newdataadr already before doing this... */
1858 link_list(fd, &adt->drivers);
1859 direct_link_fcurves(fd, &adt->drivers);
1861 /* link overrides */
1868 /* ************ READ NODE TREE *************** */
1870 /* singe node tree (also used for material/scene trees), ntree is not NULL */
1871 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
1875 if(ntree->adt) lib_link_animdata(fd, &ntree->id, ntree->adt);
1877 for(node= ntree->nodes.first; node; node= node->next)
1878 node->id= newlibadr_us(fd, id->lib, node->id);
1881 /* library ntree linking after fileread */
1882 static void lib_link_nodetree(FileData *fd, Main *main)
1886 /* only link ID pointers */
1887 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1888 if(ntree->id.flag & LIB_NEEDLINK) {
1889 ntree->id.flag -= LIB_NEEDLINK;
1890 lib_link_ntree(fd, &ntree->id, ntree);
1895 /* verify types for nodes and groups, all data has to be read */
1896 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
1898 static void lib_verify_nodetree(Main *main, int open)
1905 /* this crashes blender on undo/redo
1907 reinit_nodesystem();
1910 /* now create the own typeinfo structs an verify nodes */
1911 /* here we still assume no groups in groups */
1912 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1913 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
1914 ntreeMakeOwnType(ntree); /* for group usage */
1917 /* now verify all types in material trees, groups are set OK now */
1918 for(ma= main->mat.first; ma; ma= ma->id.next) {
1920 ntreeVerifyTypes(ma->nodetree);
1922 /* and scene trees */
1923 for(sce= main->scene.first; sce; sce= sce->id.next) {
1925 ntreeVerifyTypes(sce->nodetree);
1927 /* and texture trees */
1928 for(tx= main->tex.first; tx; tx= tx->id.next) {
1930 ntreeVerifyTypes(tx->nodetree);
1936 /* ntree itself has been read! */
1937 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
1939 /* note: writing and reading goes in sync, for speed */
1944 ntree->init= 0; /* to set callbacks and force setting types */
1945 ntree->owntype= NULL;
1946 ntree->timecursor= NULL;
1948 ntree->adt= newdataadr(fd, ntree->adt);
1949 direct_link_animdata(fd, ntree->adt);
1951 link_list(fd, &ntree->nodes);
1952 for(node= ntree->nodes.first; node; node= node->next) {
1953 if(node->type == NODE_DYNAMIC) {
1955 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
1956 node->typeinfo= NULL;
1959 node->storage= newdataadr(fd, node->storage);
1962 /* could be handlerized at some point */
1963 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
1964 direct_link_curvemapping(fd, node->storage);
1965 else if(ntree->type==NTREE_COMPOSIT) {
1966 if( ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB))
1967 direct_link_curvemapping(fd, node->storage);
1968 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
1969 ((ImageUser *)node->storage)->ok= 1;
1971 else if( ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) ) {
1972 direct_link_curvemapping(fd, node->storage);
1975 link_list(fd, &node->inputs);
1976 link_list(fd, &node->outputs);
1978 link_list(fd, &ntree->links);
1980 /* and we connect the rest */
1981 for(node= ntree->nodes.first; node; node= node->next) {
1982 node->preview= newimaadr(fd, node->preview);
1984 for(sock= node->inputs.first; sock; sock= sock->next)
1985 sock->link= newdataadr(fd, sock->link);
1986 for(sock= node->outputs.first; sock; sock= sock->next)
1987 sock->ns.data= NULL;
1989 for(link= ntree->links.first; link; link= link->next) {
1990 link->fromnode= newdataadr(fd, link->fromnode);
1991 link->tonode= newdataadr(fd, link->tonode);
1992 link->fromsock= newdataadr(fd, link->fromsock);
1993 link->tosock= newdataadr(fd, link->tosock);
1996 /* set selin and selout */
1997 for(node= ntree->nodes.first; node; node= node->next) {
1998 for(sock= node->inputs.first; sock; sock= sock->next) {
1999 if(sock->flag & SOCK_SEL) {
2004 for(sock= node->outputs.first; sock; sock= sock->next) {
2005 if(sock->flag & SOCK_SEL) {
2006 ntree->selout= sock;
2012 /* type verification is in lib-link */
2015 /* ************ READ ARMATURE ***************** */
2017 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
2021 for (con = conlist->first; con; con=con->next) {
2022 /* patch for error introduced by changing constraints (dunno how) */
2023 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
2024 if(con->data==NULL) {
2025 con->type= CONSTRAINT_TYPE_NULL;
2027 /* own ipo, all constraints have it */
2028 con->ipo= newlibadr_us(fd, id->lib, con->ipo); // XXX depreceated - old animation system
2030 switch (con->type) {
2031 case CONSTRAINT_TYPE_PYTHON:
2033 bPythonConstraint *data= (bPythonConstraint*)con->data;
2034 bConstraintTarget *ct;
2036 for (ct= data->targets.first; ct; ct= ct->next)
2037 ct->tar = newlibadr(fd, id->lib, ct->tar);
2039 data->text = newlibadr(fd, id->lib, data->text);
2040 //IDP_LibLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2043 case CONSTRAINT_TYPE_ACTION:
2045 bActionConstraint *data;
2046 data= ((bActionConstraint*)con->data);
2047 data->tar = newlibadr(fd, id->lib, data->tar);
2048 data->act = newlibadr(fd, id->lib, data->act);
2051 case CONSTRAINT_TYPE_LOCLIKE:
2053 bLocateLikeConstraint *data;
2054 data= ((bLocateLikeConstraint*)con->data);
2055 data->tar = newlibadr(fd, id->lib, data->tar);
2058 case CONSTRAINT_TYPE_ROTLIKE:
2060 bRotateLikeConstraint *data;
2061 data= ((bRotateLikeConstraint*)con->data);
2062 data->tar = newlibadr(fd, id->lib, data->tar);
2065 case CONSTRAINT_TYPE_SIZELIKE:
2067 bSizeLikeConstraint *data;
2068 data= ((bSizeLikeConstraint*)con->data);
2069 data->tar = newlibadr(fd, id->lib, data->tar);
2072 case CONSTRAINT_TYPE_KINEMATIC:
2074 bKinematicConstraint *data;
2075 data = ((bKinematicConstraint*)con->data);
2076 data->tar = newlibadr(fd, id->lib, data->tar);
2077 data->poletar = newlibadr(fd, id->lib, data->poletar);
2080 case CONSTRAINT_TYPE_TRACKTO:
2082 bTrackToConstraint *data;
2083 data = ((bTrackToConstraint*)con->data);
2084 data->tar = newlibadr(fd, id->lib, data->tar);
2087 case CONSTRAINT_TYPE_MINMAX:
2089 bMinMaxConstraint *data;
2090 data = ((bMinMaxConstraint*)con->data);
2091 data->tar = newlibadr(fd, id->lib, data->tar);
2094 case CONSTRAINT_TYPE_LOCKTRACK:
2096 bLockTrackConstraint *data;
2097 data= ((bLockTrackConstraint*)con->data);
2098 data->tar = newlibadr(fd, id->lib, data->tar);
2101 case CONSTRAINT_TYPE_FOLLOWPATH:
2103 bFollowPathConstraint *data;
2104 data= ((bFollowPathConstraint*)con->data);
2105 data->tar = newlibadr(fd, id->lib, data->tar);
2108 case CONSTRAINT_TYPE_STRETCHTO:
2110 bStretchToConstraint *data;
2111 data= ((bStretchToConstraint*)con->data);
2112 data->tar = newlibadr(fd, id->lib, data->tar);
2115 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
2117 bRigidBodyJointConstraint *data;
2118 data= ((bRigidBodyJointConstraint*)con->data);
2119 data->tar = newlibadr(fd, id->lib, data->tar);
2122 case CONSTRAINT_TYPE_CLAMPTO:
2124 bClampToConstraint *data;
2125 data= ((bClampToConstraint*)con->data);
2126 data->tar = newlibadr(fd, id->lib, data->tar);
2129 case CONSTRAINT_TYPE_CHILDOF:
2131 bChildOfConstraint *data;
2132 data= ((bChildOfConstraint*)con->data);
2133 data->tar = newlibadr(fd, id->lib, data->tar);
2136 case CONSTRAINT_TYPE_TRANSFORM:
2138 bTransformConstraint *data;
2139 data= ((bTransformConstraint*)con->data);
2140 data->tar = newlibadr(fd, id->lib, data->tar);
2143 case CONSTRAINT_TYPE_DISTLIMIT:
2145 bDistLimitConstraint *data;
2146 data= ((bDistLimitConstraint*)con->data);
2147 data->tar = newlibadr(fd, id->lib, data->tar);
2150 case CONSTRAINT_TYPE_NULL:
2156 static void direct_link_constraints(FileData *fd, ListBase *lb)
2161 for (cons=lb->first; cons; cons=cons->next) {
2162 cons->data = newdataadr(fd, cons->data);
2164 if (cons->type == CONSTRAINT_TYPE_PYTHON) {
2165 bPythonConstraint *data= cons->data;
2166 link_list(fd, &data->targets);
2167 data->prop = newdataadr(fd, data->prop);
2168 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2173 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
2175 bPoseChannel *pchan;
2176 bArmature *arm= ob->data;
2182 /* always rebuild to match proxy or lib changes */
2183 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
2185 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
2186 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
2188 /* hurms... loop in a loop, but yah... later... (ton) */
2189 pchan->bone= get_named_bone(arm, pchan->name);
2191 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
2192 if(pchan->bone==NULL)
2194 else if(ob->id.lib==NULL && arm->id.lib) {
2195 /* local pose selection copied to armature, bit hackish */
2196 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
2197 pchan->bone->flag |= pchan->selectflag;
2202 ob->recalc= OB_RECALC;
2203 pose->flag |= POSE_RECALC;
2207 static void lib_link_armature(FileData *fd, Main *main)
2211 arm= main->armature.first;
2214 if(arm->id.flag & LIB_NEEDLINK) {
2215 arm->id.flag -= LIB_NEEDLINK;
2221 static void direct_link_bones(FileData *fd, Bone* bone)
2225 bone->parent= newdataadr(fd, bone->parent);
2227 link_list(fd, &bone->childbase);
2229 for (child=bone->childbase.first; child; child=child->next) {
2230 direct_link_bones(fd, child);
2234 static void direct_link_armature(FileData *fd, bArmature *arm)
2238 link_list(fd, &arm->bonebase);
2241 bone=arm->bonebase.first;
2243 direct_link_bones(fd, bone);
2248 /* ************ READ CAMERA ***************** */
2250 static void lib_link_camera(FileData *fd, Main *main)
2254 ca= main->camera.first;
2256 if(ca->id.flag & LIB_NEEDLINK) {
2257 if (ca->adt) lib_link_animdata(fd, &ca->id, ca->adt);
2259 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo); // XXX depreceated - old animation system
2261 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
2263 lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
2265 ca->id.flag -= LIB_NEEDLINK;
2271 static void direct_link_camera(FileData *fd, Camera *ca)
2273 ca->adt= newdataadr(fd, ca->adt);
2274 direct_link_animdata(fd, ca->adt);
2276 direct_link_scriptlink(fd, &ca->scriptlink);
2280 /* ************ READ LAMP ***************** */
2282 static void lib_link_lamp(FileData *fd, Main *main)
2288 la= main->lamp.first;
2290 if(la->id.flag & LIB_NEEDLINK) {
2291 if (la->adt) lib_link_animdata(fd, &la->id, la->adt);
2293 for(a=0; a<MAX_MTEX; a++) {
2296 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2297 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2301 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo); // XXX depreceated - old animation system
2303 lib_link_scriptlink(fd, &la->id, &la->scriptlink);
2305 la->id.flag -= LIB_NEEDLINK;
2311 static void direct_link_lamp(FileData *fd, Lamp *la)
2315 la->adt= newdataadr(fd, la->adt);
2316 direct_link_animdata(fd, la->adt);
2318 direct_link_scriptlink(fd, &la->scriptlink);
2320 for(a=0; a<MAX_MTEX; a++) {
2321 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2324 la->curfalloff= newdataadr(fd, la->curfalloff);
2326 direct_link_curvemapping(fd, la->curfalloff);
2328 la->preview = direct_link_preview_image(fd, la->preview);
2331 /* ************ READ keys ***************** */
2333 static void lib_link_key(FileData *fd, Main *main)
2337 key= main->key.first;
2339 if(key->id.flag & LIB_NEEDLINK) {
2340 if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
2342 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo); // XXX depreceated - old animation system
2343 key->from= newlibadr(fd, key->id.lib, key->from);
2345 key->id.flag -= LIB_NEEDLINK;
2351 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2354 char *data, *poin, *cp;
2356 elemsize= key->elemsize;
2359 for(a=0; a<kb->totelem; a++) {
2364 while( cp[0] ) { /* cp[0]==amount */
2366 switch(cp[1]) { /* cp[1]= type */
2372 SWITCH_INT((*poin));
2385 static void direct_link_key(FileData *fd, Key *key)
2389 link_list(fd, &(key->block));
2391 key->adt= newdataadr(fd, key->adt);
2392 direct_link_animdata(fd, key->adt);
2394 key->refkey= newdataadr(fd, key->refkey);
2396 kb= key->block.first;
2399 kb->data= newdataadr(fd, kb->data);
2401 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2402 switch_endian_keyblock(key, kb);
2408 /* ************ READ mball ***************** */
2410 static void lib_link_mball(FileData *fd, Main *main)
2415 mb= main->mball.first;
2417 if(mb->id.flag & LIB_NEEDLINK) {
2419 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2421 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX depreceated - old animation system
2423 mb->id.flag -= LIB_NEEDLINK;
2429 static void direct_link_mball(FileData *fd, MetaBall *mb)
2431 mb->mat= newdataadr(fd, mb->mat);
2432 test_pointer_array(fd, (void **)&mb->mat);
2434 link_list(fd, &(mb->elems));
2436 mb->disp.first= mb->disp.last= NULL;
2437 mb->editelems= NULL;
2441 /* ************ READ WORLD ***************** */
2443 static void lib_link_world(FileData *fd, Main *main)
2449 wrld= main->world.first;
2451 if(wrld->id.flag & LIB_NEEDLINK) {
2452 if (wrld->adt) lib_link_animdata(fd, &wrld->id, wrld->adt);
2454 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo); // XXX depreceated - old animation system
2456 for(a=0; a<MAX_MTEX; a++) {
2457 mtex= wrld->mtex[a];
2459 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2460 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2464 lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
2466 wrld->id.flag -= LIB_NEEDLINK;
2468 wrld= wrld->id.next;
2472 static void direct_link_world(FileData *fd, World *wrld)
2476 wrld->adt= newdataadr(fd, wrld->adt);
2477 direct_link_animdata(fd, wrld->adt);
2479 direct_link_scriptlink(fd, &wrld->scriptlink);
2481 for(a=0; a<MAX_MTEX; a++) {
2482 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2484 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2488 /* ************ READ VFONT ***************** */
2490 static void lib_link_vfont(FileData *fd, Main *main)
2494 vf= main->vfont.first;
2496 if(vf->id.flag & LIB_NEEDLINK) {
2497 vf->id.flag -= LIB_NEEDLINK;
2503 static void direct_link_vfont(FileData *fd, VFont *vf)
2506 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2509 /* ************ READ TEXT ****************** */
2511 static void lib_link_text(FileData *fd, Main *main)
2515 text= main->text.first;
2517 if(text->id.flag & LIB_NEEDLINK) {
2518 text->id.flag -= LIB_NEEDLINK;
2520 text= text->id.next;
2524 static void direct_link_text(FileData *fd, Text *text)
2528 text->name= newdataadr(fd, text->name);
2531 text->undo_len= TXT_INIT_UNDO;
2532 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2534 text->compiled= NULL;
2537 if(text->flags & TXT_ISEXT) {
2542 link_list(fd, &text->lines);
2543 link_list(fd, &text->markers);
2545 text->curl= newdataadr(fd, text->curl);
2546 text->sell= newdataadr(fd, text->sell);
2548 ln= text->lines.first;
2550 ln->line= newdataadr(fd, ln->line);
2553 if (ln->len != (int) strlen(ln->line)) {
2554 printf("Error loading text, line lengths differ\n");
2555 ln->len = strlen(ln->line);
2561 text->flags = (text->flags) & ~TXT_ISEXT;
2566 /* ************ READ IMAGE ***************** */
2568 static void lib_link_image(FileData *fd, Main *main)
2572 ima= main->image.first;
2574 if(ima->id.flag & LIB_NEEDLINK) {
2575 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2577 ima->id.flag -= LIB_NEEDLINK;
2583 static void link_ibuf_list(FileData *fd, ListBase *lb)
2587 if(lb->first==NULL) return;
2589 lb->first= newimaadr(fd, lb->first);
2593 ln->next= newimaadr(fd, ln->next);
2601 static void direct_link_image(FileData *fd, Image *ima)
2603 /* for undo system, pointers could be restored */
2605 link_ibuf_list(fd, &ima->ibufs);
2607 ima->ibufs.first= ima->ibufs.last= NULL;
2609 /* if not restored, we keep the binded opengl index */
2610 if(ima->ibufs.first==NULL) {
2612 ima->gputexture= NULL;
2619 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2620 ima->preview = direct_link_preview_image(fd, ima->preview);
2625 /* ************ READ CURVE ***************** */
2627 static void lib_link_curve(FileData *fd, Main *main)
2632 cu= main->curve.first;
2634 if(cu->id.flag & LIB_NEEDLINK) {
2635 if(cu->adt) lib_link_animdata(fd, &cu->id, cu->adt);
2637 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2639 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2640 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2641 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2642 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2643 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2644 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2645 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2647 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo); // XXX depreceated - old animation system
2648 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2650 cu->id.flag -= LIB_NEEDLINK;
2657 static void switch_endian_knots(Nurb *nu)
2664 SWITCH_INT(nu->knotsu[len]);
2670 SWITCH_INT(nu->knotsv[len]);
2675 static void direct_link_curve(FileData *fd, Curve *cu)
2680 cu->adt= newdataadr(fd, cu->adt);
2681 direct_link_animdata(fd, cu->adt);
2683 cu->mat= newdataadr(fd, cu->mat);
2684 test_pointer_array(fd, (void **)&cu->mat);
2685 cu->str= newdataadr(fd, cu->str);
2686 cu->strinfo= newdataadr(fd, cu->strinfo);
2687 cu->tb= newdataadr(fd, cu->tb);
2689 if(cu->vfont==0) link_list(fd, &(cu->nurb));
2691 cu->nurb.first=cu->nurb.last= 0;
2693 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2695 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2702 cu->tb[0].w = cu->linewidth;
2704 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2707 cu->bev.first=cu->bev.last= NULL;
2708 cu->disp.first=cu->disp.last= NULL;
2710 cu->lastselbp= NULL;
2716 nu->bezt= newdataadr(fd, nu->bezt);
2717 nu->bp= newdataadr(fd, nu->bp);
2718 nu->knotsu= newdataadr(fd, nu->knotsu);
2719 nu->knotsv= newdataadr(fd, nu->knotsv);
2720 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2722 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2723 switch_endian_knots(nu);
2731 /* ************ READ TEX ***************** */
2733 static void lib_link_texture(FileData *fd, Main *main)
2737 tex= main->tex.first;
2739 if(tex->id.flag & LIB_NEEDLINK) {
2740 if(tex->adt) lib_link_animdata(fd, &tex->id, tex->adt);
2742 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2743 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2744 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2747 lib_link_ntree(fd, &tex->id, tex->nodetree);
2749 tex->id.flag -= LIB_NEEDLINK;
2755 static void direct_link_texture(FileData *fd, Tex *tex)
2757 tex->adt= newdataadr(fd, tex->adt);
2758 direct_link_animdata(fd, tex->adt);
2760 tex->plugin= newdataadr(fd, tex->plugin);
2762 tex->plugin->handle= 0;
2763 open_plugin_tex(tex->plugin);
2764 /* initialize data for this instance, if an initialization
2767 if (tex->plugin->instance_init)
2768 tex->plugin->instance_init((void *) tex->plugin->data);
2770 tex->coba= newdataadr(fd, tex->coba);
2771 tex->env= newdataadr(fd, tex->env);
2773 tex->env->ima= NULL;
2774 memset(tex->env->cube, 0, 6*sizeof(void *));
2778 tex->nodetree= newdataadr(fd, tex->nodetree);
2780 direct_link_nodetree(fd, tex->nodetree);
2782 tex->preview = direct_link_preview_image(fd, tex->preview);
2789 /* ************ READ MATERIAL ***************** */
2791 static void lib_link_material(FileData *fd, Main *main)
2797 ma= main->mat.first;
2799 if(ma->id.flag & LIB_NEEDLINK) {
2800 if(ma->adt) lib_link_animdata(fd, &ma->id, ma->adt);
2802 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2803 of library blocks that implement this.*/
2804 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2806 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2807 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2809 for(a=0; a<MAX_MTEX; a++) {
2812 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2813 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2816 lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
2819 lib_link_ntree(fd, &ma->id, ma->nodetree);
2821 ma->id.flag -= LIB_NEEDLINK;
2827 static void direct_link_material(FileData *fd, Material *ma)
2831 ma->adt= newdataadr(fd, ma->adt);
2832 direct_link_animdata(fd, ma->adt);
2834 for(a=0; a<MAX_MTEX; a++) {
2835 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2838 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2839 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2841 direct_link_scriptlink(fd, &ma->scriptlink);
2843 ma->nodetree= newdataadr(fd, ma->nodetree);
2845 direct_link_nodetree(fd, ma->nodetree);
2847 ma->preview = direct_link_preview_image(fd, ma->preview);
2848 ma->gpumaterial.first = ma->gpumaterial.last = NULL;
2851 /* ************ READ PARTICLE SETTINGS ***************** */
2853 static void direct_link_pointcache(FileData *fd, PointCache *cache)
2855 cache->flag &= ~(PTCACHE_SIMULATION_VALID|PTCACHE_BAKE_EDIT_ACTIVE);
2859 static void lib_link_particlesettings(FileData *fd, Main *main)
2861 ParticleSettings *part;
2863 part= main->particle.first;
2865 if(part->id.flag & LIB_NEEDLINK) {
2866 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo); // XXX depreceated - old animation system
2867 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
2868 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
2869 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
2870 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
2871 part->id.flag -= LIB_NEEDLINK;
2873 part= part->id.next;
2877 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
2879 part->pd= newdataadr(fd, part->pd);
2880 part->pd2= newdataadr(fd, part->pd2);
2883 static void lib_link_particlesystems(FileData *fd, Object *ob, ID *id, ListBase *particles)
2885 ParticleSystem *psys, *psysnext;
2888 for(psys=particles->first; psys; psys=psysnext){
2891 psysnext= psys->next;
2893 psys->part = newlibadr_us(fd, id->lib, psys->part);
2895 psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
2896 psys->keyed_ob = newlibadr(fd, id->lib, psys->keyed_ob);
2898 for(a=0,pa=psys->particles; a<psys->totpart; a++,pa++){
2899 pa->stick_ob=newlibadr(fd, id->lib, pa->stick_ob);
2903 /* particle modifier must be removed before particle system */
2904 ParticleSystemModifierData *psmd= psys_get_modifier(ob,psys);
2905 BLI_remlink(&ob->modifiers, psmd);
2906 modifier_free((ModifierData *)psmd);
2908 BLI_remlink(particles, psys);
2913 static void direct_link_particlesystems(FileData *fd, ListBase *particles)
2915 ParticleSystem *psys;
2918 for(psys=particles->first; psys; psys=psys->next) {
2919 psys->particles=newdataadr(fd,psys->particles);
2920 if(psys->particles && psys->particles->hair){
2921 ParticleData *pa = psys->particles;
2922 for(a=0; a<psys->totpart; a++, pa++)
2923 pa->hair=newdataadr(fd,pa->hair);
2925 if(psys->particles && psys->particles->keys){
2926 ParticleData *pa = psys->particles;
2927 for(a=0; a<psys->totpart; a++, pa++) {
2932 psys->flag &= ~PSYS_KEYED;
2934 psys->child=newdataadr(fd,psys->child);
2935 psys->effectors.first=psys->effectors.last=0;
2937 psys->soft= newdataadr(fd, psys->soft);
2939 SoftBody *sb = psys->soft;
2940 sb->particles = psys;
2941 sb->bpoint= NULL; // init pointers so it gets rebuilt nicely
2945 sb->pointcache= newdataadr(fd, sb->pointcache);
2947 direct_link_pointcache(fd, sb->pointcache);
2951 psys->free_edit = NULL;
2952 psys->pathcache = 0;
2953 psys->childcache = 0;
2954 psys->pathcachebufs.first = psys->pathcachebufs.last = 0;
2955 psys->childcachebufs.first = psys->childcachebufs.last = 0;
2956 psys->reactevents.first = psys->reactevents.last = 0;
2958 psys->pointcache= newdataadr(fd, psys->pointcache);
2959 if(psys->pointcache)
2960 direct_link_pointcache(fd, psys->pointcache);
2965 /* ************ READ MESH ***************** */
2967 static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
2972 for (i=0; i<totface; i++, tf++) {
2973 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
2974 if(tf->tpage && tf->tpage->id.us==0)
2975 tf->tpage->id.us= 1;
2979 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
2982 for(i=0; i<fdata->totlayer; i++) {
2983 CustomDataLayer *layer = &fdata->layers[i];
2985 if(layer->type == CD_MTFACE)
2986 lib_link_mtface(fd, me, layer->data, totface);
2991 static void lib_link_mesh(FileData *fd, Main *main)
2995 me= main->mesh.first;
2997 if(me->id.flag & LIB_NEEDLINK) {
3000 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
3001 of library blocks that implement this.*/
3002 if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
3004 /* this check added for python created meshes */
3006 for(i=0; i<me->totcol; i++) {
3007 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
3012 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
3013 me->key= newlibadr_us(fd, me->id.lib, me->key);
3014 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
3016 lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
3017 if(me->mr && me->mr->levels.first)
3018 lib_link_customdata_mtface(fd, me, &me->mr->fdata,
3019 ((MultiresLevel*)me->mr->levels.first)->totface);
3021 me->id.flag -= LIB_NEEDLINK;
3027 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
3034 for (i=0; i<count; i++) {
3035 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
3037 mdverts[i].totweight=0;
3041 static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps)
3046 for(i = 0; i < count; ++i) {
3047 mdisps[i].disps = newdataadr(fd, mdisps[i].disps);
3048 if(!mdisps[i].disps)
3049 mdisps[i].totdisp = 0;
3054 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
3058 data->layers= newdataadr(fd, data->layers);
3060 while (i < data->totlayer) {
3061 CustomDataLayer *layer = &data->layers[i];
3063 if (CustomData_verify_versions(data, i)) {
3064 layer->data = newdataadr(fd, layer->data);
3065 if(layer->type == CD_MDISPS)
3066 direct_link_mdisps(fd, count, layer->data);
3072 static void direct_link_mesh(FileData *fd, Mesh *mesh)
3074 mesh->mat= newdataadr(fd, mesh->mat);
3075 test_pointer_array(fd, (void **)&mesh->mat);
3077 mesh->mvert= newdataadr(fd, mesh->mvert);
3078 mesh->medge= newdataadr(fd, mesh->medge);
3079 mesh->mface= newdataadr(fd, mesh->mface);
3080 mesh->tface= newdataadr(fd, mesh->tface);
3081 mesh->mtface= newdataadr(fd, mesh->mtface);
3082 mesh->mcol= newdataadr(fd, mesh->mcol);
3083 mesh->msticky= newdataadr(fd, mesh->msticky);
3084 mesh->dvert= newdataadr(fd, mesh->dvert);
3086 /* Partial-mesh visibility (do this before using totvert, totface, or totedge!) */
3087 mesh->pv= newdataadr(fd, mesh->pv);
3089 mesh->pv->vert_map= newdataadr(fd, mesh->pv->vert_map);
3090 mesh->pv->edge_map= newdataadr(fd, mesh->pv->edge_map);
3091 mesh->pv->old_faces= newdataadr(fd, mesh->pv->old_faces);
3092 mesh->pv->old_edges= newdataadr(fd, mesh->pv->old_edges);
3095 /* normally direct_link_dverts should be called in direct_link_customdata,
3096 but for backwards compat in do_versions to work we do it here */
3097 direct_link_dverts(fd, mesh->pv ? mesh->pv->totvert : mesh->totvert, mesh->dvert);
3099 direct_link_customdata(fd, &mesh->vdata, mesh->pv ? mesh->pv->totvert : mesh->totvert);
3100 direct_link_customdata(fd, &mesh->edata, mesh->pv ? mesh->pv->totedge : mesh->totedge);
3101 direct_link_customdata(fd, &mesh->fdata, mesh->pv ? mesh->pv->totface : mesh->totface);
3104 mesh->mselect = NULL;
3105 mesh->edit_mesh= NULL;
3108 mesh->mr= newdataadr(fd, mesh->mr);
3112 link_list(fd, &mesh->mr->levels);
3113 lvl= mesh->mr->levels.first;
3115 direct_link_customdata(fd, &mesh->mr->vdata, lvl->totvert);
3116 direct_link_dverts(fd, lvl->totvert, CustomData_get(&mesh->mr->vdata, 0, CD_MDEFORMVERT));
3117 direct_link_customdata(fd, &mesh->mr->fdata, lvl->totface);
3119 if(!mesh->mr->edge_flags)
3120 mesh->mr->edge_flags= MEM_callocN(sizeof(short)*lvl->totedge, "Multires Edge Flags");
3121 if(!mesh->mr->edge_creases)
3122 mesh->mr->edge_creases= MEM_callocN(sizeof(char)*lvl->totedge, "Multires Edge Creases");
3124 mesh->mr->verts = newdataadr(fd, mesh->mr->verts);
3126 for(; lvl; lvl= lvl->next) {
3127 lvl->verts= newdataadr(fd, lvl->verts);
3128 lvl->faces= newdataadr(fd, lvl->faces);
3129 lvl->edges= newdataadr(fd, lvl->edges);
3130 lvl->colfaces= newdataadr(fd, lvl->colfaces);
3134 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
3135 TFace *tf= mesh->tface;
3138 for (i=0; i< (mesh->pv ? mesh->pv->totface : mesh->totface); i++, tf++) {
3139 SWITCH_INT(tf->col[0]);
3140 SWITCH_INT(tf->col[1]);
3141 SWITCH_INT(tf->col[2]);
3142 SWITCH_INT(tf->col[3]);
3147 /* ************ READ LATTICE ***************** */
3149 static void lib_link_latt(FileData *fd, Main *main)
3153 lt= main->latt.first;
3155 if(lt->id.flag & LIB_NEEDLINK) {
3157 lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo); // XXX depreceated - old animation system
3158 lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
3160 lt->id.flag -= LIB_NEEDLINK;
3166 static void direct_link_latt(FileData *fd, Lattice *lt)
3168 lt->def= newdataadr(fd, lt->def);
3170 lt->dvert= newdataadr(fd, lt->dvert);
3171 direct_link_dverts(fd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
3177 /* ************ READ OBJECT ***************** */
3179 static void lib_link_modifiers__linkModifiers(void *userData, Object *ob,
3182 FileData *fd = userData;
3184 *idpoin = newlibadr(fd, ob->id.lib, *idpoin);
3185 /* hardcoded bad exception; non-object modifier data gets user count (texture, displace) */
3186 if(*idpoin && GS((*idpoin)->name)!=ID_OB)
3189 static void lib_link_modifiers(FileData *fd, Object *ob)
3191 modifiers_foreachIDLink(ob, lib_link_modifiers__linkModifiers, fd);
3194 static void lib_link_object(FileData *fd, Main *main)
3204 ob= main->object.first;