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.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
39 #include "BLI_winstuff.h"
42 #include <stdio.h> // for printf fopen fwrite fclose sprintf FILE
43 #include <stdlib.h> // for getenv atoi
44 #include <fcntl.h> // for open
45 #include <string.h> // for strrchr strncmp strstr
46 #include <math.h> // for fabs
49 #include <unistd.h> // for read close
50 #include <sys/param.h> // for MAXPATHLEN
52 #include <io.h> // for open close read
57 #include "DNA_action_types.h"
58 #include "DNA_armature_types.h"
60 #include "DNA_actuator_types.h"
61 #include "DNA_brush_types.h"
62 #include "DNA_camera_types.h"
63 #include "DNA_cloth_types.h"
64 #include "DNA_color_types.h"
65 #include "DNA_controller_types.h"
66 #include "DNA_constraint_types.h"
67 #include "DNA_curve_types.h"
68 #include "DNA_customdata_types.h"
69 #include "DNA_effect_types.h"
70 #include "DNA_fileglobal_types.h"
71 #include "DNA_group_types.h"
72 #include "DNA_gpencil_types.h"
73 #include "DNA_ipo_types.h"
74 #include "DNA_image_types.h"
75 #include "DNA_key_types.h"
76 #include "DNA_lattice_types.h"
77 #include "DNA_lamp_types.h"
78 #include "DNA_meta_types.h"
79 #include "DNA_material_types.h"
80 #include "DNA_mesh_types.h"
81 #include "DNA_meshdata_types.h"
82 #include "DNA_modifier_types.h"
83 #include "DNA_nla_types.h"
84 #include "DNA_node_types.h"
85 #include "DNA_object_types.h"
86 #include "DNA_object_force.h"
87 #include "DNA_object_fluidsim.h" // NT
88 #include "DNA_oops_types.h"
89 #include "DNA_object_force.h"
90 #include "DNA_packedFile_types.h"
91 #include "DNA_particle_types.h"
92 #include "DNA_property_types.h"
93 #include "DNA_text_types.h"
94 #include "DNA_view3d_types.h"
95 #include "DNA_screen_types.h"
96 #include "DNA_sensor_types.h"
97 #include "DNA_sdna_types.h"
98 #include "DNA_scene_types.h"
99 #include "DNA_sequence_types.h"
100 #include "DNA_sound_types.h"
101 #include "DNA_space_types.h"
102 #include "DNA_texture_types.h"
103 #include "DNA_userdef_types.h"
104 #include "DNA_vfont_types.h"
105 #include "DNA_world_types.h"
107 #include "MEM_guardedalloc.h"
108 #include "BLI_blenlib.h"
109 #include "BLI_arithb.h"
110 #include "BLI_storage_types.h" // for relname flags
112 #include "BDR_sculptmode.h"
114 #include "BKE_bad_level_calls.h" // for reopen_text build_seqar (from WHILE_SEQ) set_rects_butspace check_imasel_copy
116 #include "BKE_action.h"
117 #include "BKE_armature.h"
118 #include "BKE_cloth.h"
119 #include "BKE_colortools.h"
120 #include "BKE_constraint.h"
121 #include "BKE_curve.h"
122 #include "BKE_customdata.h"
123 #include "BKE_deform.h"
124 #include "BKE_depsgraph.h"
125 #include "BKE_effect.h" /* give_parteff */
126 #include "BKE_global.h" // for G
127 #include "BKE_group.h"
128 #include "BKE_image.h"
129 #include "BKE_key.h" //void set_four_ipo
130 #include "BKE_lattice.h"
131 #include "BKE_library.h" // for wich_libbase
132 #include "BKE_main.h" // for Main
133 #include "BKE_mesh.h" // for ME_ defines (patching)
134 #include "BKE_modifier.h"
135 #include "BKE_node.h" // for tree type defines
136 #include "BKE_object.h"
137 #include "BKE_particle.h"
138 #include "BKE_pointcache.h"
139 #include "BKE_property.h" // for get_property
140 #include "BKE_sca.h" // for init_actuator
141 #include "BKE_scene.h"
142 #include "BKE_softbody.h" // sbNew()
143 #include "BKE_sculpt.h"
144 #include "BKE_texture.h" // for open_plugin_tex
145 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
146 #include "BKE_idprop.h"
148 #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
149 #include "BIF_filelist.h" // badlevel too, where to move this? - elubie
150 #include "BIF_previewrender.h" // bedlelvel, for struct RenderInfo
151 #include "BLO_readfile.h"
152 #include "BLO_undofile.h"
153 #include "BLO_readblenfile.h" // streaming read pipe, for BLO_readblenfile BLO_readblenfilememory
155 #include "readfile.h"
159 #include "mydevice.h"
165 Remark: still a weak point is the newadress() function, that doesnt solve reading from
166 multiple files at the same time
168 (added remark: oh, i thought that was solved? will look at that... (ton)
171 - Existing Library (Main) push or free
181 - read associated 'direct data'
182 - link direct data (internal and to LibBlock)
184 - read USER data, only when indicated (file is ~/.B.blend)
186 - per Library (per Main)
189 - find LibBlocks and attach IDs to Main
190 - if external LibBlock
192 - or it's already read,
197 - read associated direct data
198 - link direct data (internal and to LibBlock)
200 - per Library with unread LibBlocks
205 - read associated direct data
206 - link direct data (internal and to LibBlock)
209 - link all LibBlocks and indirect pointers to libblocks
210 - initialize FileGlobal and copy pointers to Global
213 /* also occurs in library.c */
214 /* GS reads the memory pointed at in a specific ordering. There are,
215 * however two definitions for it. I have jotted them down here, both,
216 * but I think the first one is actually used. The thing is that
217 * big-endian systems might read this the wrong way round. OTOH, we
218 * constructed the IDs that are read out with this macro explicitly as
219 * well. I expect we'll sort it out soon... */
222 #define GS(a) (*((short *)(a)))
224 /* from misc_util: flip the bytes from x */
225 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
227 // only used here in readfile.c
228 #define SWITCH_LONGINT(a) { \
231 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
232 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
233 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
234 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
238 typedef struct OldNew {
243 typedef struct OldNewMap {
245 int nentries, entriessize;
251 /* local prototypes */
252 static void *read_struct(FileData *fd, BHead *bh, char *blockname);
255 static OldNewMap *oldnewmap_new(void)
257 OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
259 onm->entriessize= 1024;
260 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
265 static int verg_oldnewmap(const void *v1, const void *v2)
267 const struct OldNew *x1=v1, *x2=v2;
269 if( x1->old > x2->old) return 1;
270 else if( x1->old < x2->old) return -1;
275 static void oldnewmap_sort(FileData *fd)
277 qsort(fd->libmap->entries, fd->libmap->nentries, sizeof(OldNew), verg_oldnewmap);
278 fd->libmap->sorted= 1;
281 /* nr is zero for data, and ID code for libdata */
282 static void oldnewmap_insert(OldNewMap *onm, void *oldaddr, void *newaddr, int nr)
286 if(oldaddr==NULL || newaddr==NULL) return;
288 if (onm->nentries==onm->entriessize) {
289 int osize= onm->entriessize;
290 OldNew *oentries= onm->entries;
292 onm->entriessize*= 2;
293 onm->entries= MEM_mallocN(sizeof(*onm->entries)*onm->entriessize, "OldNewMap.entries");
295 memcpy(onm->entries, oentries, sizeof(*oentries)*osize);
299 entry= &onm->entries[onm->nentries++];
301 entry->newp= newaddr;
305 static void *oldnewmap_lookup_and_inc(OldNewMap *onm, void *addr)
309 if (onm->lasthit<onm->nentries-1) {
310 OldNew *entry= &onm->entries[++onm->lasthit];
312 if (entry->old==addr) {
318 for (i=0; i<onm->nentries; i++) {
319 OldNew *entry= &onm->entries[i];
321 if (entry->old==addr) {
332 /* for libdata, nr has ID code, no increment */
333 static void *oldnewmap_liblookup(OldNewMap *onm, void *addr, void *lib)
337 if(addr==NULL) return NULL;
339 /* lasthit works fine for non-libdata, linking there is done in same sequence as writing */
341 OldNew entry_s, *entry;
345 entry= bsearch(&entry_s, onm->entries, onm->nentries, sizeof(OldNew), verg_oldnewmap);
349 if (id && (!lib || id->lib)) {
355 for (i=0; i<onm->nentries; i++) {
356 OldNew *entry= &onm->entries[i];
358 if (entry->old==addr) {
361 if (id && (!lib || id->lib)) {
370 static void oldnewmap_free_unused(OldNewMap *onm)
374 for (i=0; i<onm->nentries; i++) {
375 OldNew *entry= &onm->entries[i];
377 MEM_freeN(entry->newp);
383 static void oldnewmap_clear(OldNewMap *onm)
389 static void oldnewmap_free(OldNewMap *onm)
391 MEM_freeN(onm->entries);
397 static void read_libraries(FileData *basefd, ListBase *mainlist);
399 /* ************ help functions ***************** */
401 static void add_main_to_main(Main *mainvar, Main *from)
403 ListBase *lbarray[MAX_LIBARRAY], *fromarray[MAX_LIBARRAY];
406 a= set_listbasepointers(mainvar, lbarray);
407 a= set_listbasepointers(from, fromarray);
409 addlisttolist(lbarray[a], fromarray[a]);
413 void blo_join_main(ListBase *mainlist)
415 Main *tojoin, *mainl;
418 mainl= mainlist->first;
419 while ((tojoin= mainl->next)) {
420 add_main_to_main(mainl, tojoin);
421 BLI_remlink(mainlist, tojoin);
426 static void split_libdata(ListBase *lb, Main *first)
438 if(mainvar->curlib==id->lib) {
439 lbn= wich_libbase(mainvar, GS(id->name));
441 BLI_addtail(lbn, id);
444 mainvar= mainvar->next;
446 if(mainvar==0) printf("error split_libdata\n");
452 void blo_split_main(ListBase *mainlist, Main *main)
454 ListBase *lbarray[MAX_LIBARRAY];
458 mainlist->first= mainlist->last= main;
461 if(main->library.first==NULL)
464 for (lib= main->library.first; lib; lib= lib->id.next) {
465 Main *libmain= MEM_callocN(sizeof(Main), "libmain");
466 libmain->curlib= lib;
467 BLI_addtail(mainlist, libmain);
470 i= set_listbasepointers(main, lbarray);
472 split_libdata(lbarray[i], main->next);
475 /* removes things like /blah/blah/../../blah/ etc, then writes in *name the full path */
476 static void cleanup_path(const char *relabase, char *name)
478 char filename[FILE_MAXFILE];
480 BLI_splitdirstring(name, filename);
481 BLI_cleanup_dir(relabase, name);
482 strcat(name, filename);
485 static void read_file_version(FileData *fd, Main *main)
489 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
490 if (bhead->code==GLOB) {
491 FileGlobal *fg= read_struct(fd, bhead, "Global");
493 main->subversionfile= fg->subversion;
494 main->minversionfile= fg->minversion;
495 main->minsubversionfile= fg->minsubversion;
498 else if (bhead->code==ENDB)
505 static Main *blo_find_main(FileData *fd, ListBase *mainlist, const char *name, const char *relabase)
509 char name1[FILE_MAXDIR+FILE_MAXFILE];
511 strncpy(name1, name, sizeof(name1)-1);
512 cleanup_path(relabase, name1);
513 // printf("blo_find_main: original in %s\n", name);
514 // printf("blo_find_main: converted to %s\n", name1);
516 for (m= mainlist->first; m; m= m->next) {
517 char *libname= (m->curlib)?m->curlib->filename:m->name;
519 if (BLI_streq(name1, libname)) {
520 if(G.f & G_DEBUG) printf("blo_find_main: found library %s\n", libname);
525 m= MEM_callocN(sizeof(Main), "find_main");
526 BLI_addtail(mainlist, m);
528 lib= alloc_libblock(&m->library, ID_LI, "lib");
529 strncpy(lib->name, name, sizeof(lib->name)-1);
530 BLI_strncpy(lib->filename, name1, sizeof(lib->filename));
534 read_file_version(fd, m);
536 if(G.f & G_DEBUG) printf("blo_find_main: added new lib %s\n", name);
541 /* ************ FILE PARSING ****************** */
543 static void switch_endian_bh4(BHead4 *bhead)
545 /* the ID_.. codes */
546 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
548 if (bhead->code != ENDB) {
549 SWITCH_INT(bhead->len);
550 SWITCH_INT(bhead->SDNAnr);
551 SWITCH_INT(bhead->nr);
555 static void switch_endian_bh8(BHead8 *bhead)
557 /* the ID_.. codes */
558 if((bhead->code & 0xFFFF)==0) bhead->code >>=16;
560 if (bhead->code != ENDB) {
561 SWITCH_INT(bhead->len);
562 SWITCH_INT(bhead->SDNAnr);
563 SWITCH_INT(bhead->nr);
567 static void bh4_from_bh8(BHead *bhead, BHead8 *bhead8, int do_endian_swap)
569 BHead4 *bhead4 = (BHead4 *) bhead;
570 #if defined(WIN32) && !defined(FREE_WINDOWS)
576 bhead4->code= bhead8->code;
577 bhead4->len= bhead8->len;
579 if (bhead4->code != ENDB) {
581 // why is this here ??
582 if (do_endian_swap) {
583 SWITCH_LONGINT(bhead8->old);
586 /* this patch is to avoid a long long being read from not-eight aligned positions
587 is necessary on any modern 64bit architecture) */
588 memcpy(&old, &bhead8->old, 8);
589 bhead4->old = (int) (old >> 3);
591 bhead4->SDNAnr= bhead8->SDNAnr;
592 bhead4->nr= bhead8->nr;
596 static void bh8_from_bh4(BHead *bhead, BHead4 *bhead4)
598 BHead8 *bhead8 = (BHead8 *) bhead;
600 bhead8->code= bhead4->code;
601 bhead8->len= bhead4->len;
603 if (bhead8->code != ENDB) {
604 bhead8->old= bhead4->old;
605 bhead8->SDNAnr= bhead4->SDNAnr;
606 bhead8->nr= bhead4->nr;
610 static BHeadN *get_bhead(FileData *fd)
615 BHeadN *new_bhead = 0;
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_get_structDNA_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, 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, 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, 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, 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 extern unsigned char DNAstr[]; /* DNA.c */
921 FileData *fd = MEM_callocN(sizeof(FileData), "FileData");
924 fd->gzfiledes = NULL;
926 /* XXX, this doesn't need to be done all the time,
927 * but it keeps us reentrant, remove once we have
928 * a lib that provides a nice lock. - zr
930 fd->memsdna = dna_sdna_from_data(DNAstr, DNAlen, 0);
932 fd->datamap = oldnewmap_new();
933 fd->globmap = oldnewmap_new();
934 fd->libmap = oldnewmap_new();
939 static FileData *blo_decode_and_check(FileData *fd, BlendReadError *error_r)
941 decode_blender_header(fd);
943 if (fd->flags & FD_FLAGS_FILE_OK) {
944 if (!read_file_dna(fd)) {
945 *error_r = BRE_INCOMPLETE;
946 blo_freefiledata(fd);
951 *error_r = BRE_NOT_A_BLEND;
952 blo_freefiledata(fd);
959 /* cannot be called with relative paths anymore! */
960 /* on each new library added, it now checks for the current FileData and expands relativeness */
961 FileData *blo_openblenderfile(char *name, BlendReadError *error_r)
965 gzfile= gzopen(name, "rb");
967 if (NULL == gzfile) {
968 *error_r = BRE_UNABLE_TO_OPEN;
971 FileData *fd = filedata_new();
972 fd->gzfiledes = gzfile;
973 BLI_strncpy(fd->filename, name, sizeof(fd->filename)); // now only in use by library append
974 fd->read = fd_read_gzip_from_file;
976 return blo_decode_and_check(fd, error_r);
980 FileData *blo_openblendermemory(void *mem, int memsize, BlendReadError *error_r)
982 if (!mem || memsize<SIZEOFBLENDERHEADER) {
983 *error_r = mem?BRE_UNABLE_TO_READ:BRE_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, error_r);
996 FileData *blo_openblendermemfile(MemFile *memfile, BlendReadError *error_r)
999 *error_r = BRE_UNABLE_TO_OPEN;
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, error_r);
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_freestructDNA(fd->memsdna);
1037 dna_freestructDNA(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 /* ************** OLD POINTERS ******************* */
1065 static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */
1067 return oldnewmap_lookup_and_inc(fd->datamap, adr);
1070 static void *newglobadr(FileData *fd, void *adr) /* direct datablocks with global linking */
1072 return oldnewmap_lookup_and_inc(fd->globmap, adr);
1075 static void *newimaadr(FileData *fd, void *adr) /* used to restore image data after undo */
1077 if(fd->imamap && adr)
1078 return oldnewmap_lookup_and_inc(fd->imamap, adr);
1083 static void *newlibadr(FileData *fd, void *lib, void *adr) /* only lib data */
1085 return oldnewmap_liblookup(fd->libmap, adr, lib);
1088 static void *newlibadr_us(FileData *fd, void *lib, void *adr) /* increases user number */
1090 ID *id= newlibadr(fd, lib, adr);
1098 static void change_idid_adr_fd(FileData *fd, void *old, void *new)
1102 for (i=0; i<fd->libmap->nentries; i++) {
1103 OldNew *entry= &fd->libmap->entries[i];
1105 if (old==entry->newp && entry->nr==ID_ID) {
1107 if(new) entry->nr= GS( ((ID *)new)->name );
1113 static void change_idid_adr(ListBase *mainlist, FileData *basefd, void *old, void *new)
1117 for(mainptr= mainlist->first; mainptr; mainptr= mainptr->next) {
1120 if(mainptr->curlib) fd= mainptr->curlib->filedata;
1124 change_idid_adr_fd(fd, old, new);
1129 /* assumed; G.main still exists */
1130 void blo_make_image_pointer_map(FileData *fd)
1132 Image *ima= G.main->image.first;
1133 Scene *sce= G.main->scene.first;
1135 fd->imamap= oldnewmap_new();
1137 for(;ima; ima= ima->id.next) {
1138 Link *ibuf= ima->ibufs.first;
1139 for(; ibuf; ibuf= ibuf->next)
1140 oldnewmap_insert(fd->imamap, ibuf, ibuf, 0);
1142 for(; sce; sce= sce->id.next) {
1145 for(node= sce->nodetree->nodes.first; node; node= node->next)
1146 oldnewmap_insert(fd->imamap, node->preview, node->preview, 0);
1151 /* set G.main image ibufs to zero if it has been restored */
1152 /* this works because freeing G.main only happens after this call */
1153 void blo_end_image_pointer_map(FileData *fd)
1155 OldNew *entry= fd->imamap->entries;
1156 Image *ima= G.main->image.first;
1157 Scene *sce= G.main->scene.first;
1160 /* used entries were restored, so we put them to zero */
1161 for (i=0; i<fd->imamap->nentries; i++, entry++) {
1166 for(;ima; ima= ima->id.next) {
1169 /* this mirrors direct_link_image */
1170 for(ibuf= ima->ibufs.first; ibuf; ibuf= next) {
1172 if(NULL==newimaadr(fd, ibuf)) { /* so was restored */
1173 BLI_remlink(&ima->ibufs, ibuf);
1178 for(; sce; sce= sce->id.next) {
1181 for(node= sce->nodetree->nodes.first; node; node= node->next)
1182 node->preview= newimaadr(fd, node->preview);
1187 /* undo file support: add all library pointers in lookup */
1188 void blo_add_library_pointer_map(ListBase *mainlist, FileData *fd)
1190 Main *ptr= mainlist->first;
1191 ListBase *lbarray[MAX_LIBARRAY];
1193 for(ptr= ptr->next; ptr; ptr= ptr->next) {
1194 int i= set_listbasepointers(ptr, lbarray);
1197 for(id= lbarray[i]->first; id; id= id->next)
1198 oldnewmap_insert(fd->libmap, id, id, GS(id->name));
1204 /* ********** END OLD POINTERS ****************** */
1205 /* ********** READ FILE ****************** */
1207 static void switch_endian_structs(struct SDNA *filesdna, BHead *bhead)
1209 int blocksize, nblocks;
1212 data= (char *)(bhead+1);
1213 blocksize= filesdna->typelens[ filesdna->structs[bhead->SDNAnr][0] ];
1217 dna_switch_endian_struct(filesdna, bhead->SDNAnr, data);
1223 static void *read_struct(FileData *fd, BHead *bh, char *blockname)
1228 /* switch is based on file dna */
1229 if (bh->SDNAnr && (fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1230 switch_endian_structs(fd->filesdna, bh);
1232 if (fd->compflags[bh->SDNAnr]) { /* flag==0: doesn't exist anymore */
1233 if(fd->compflags[bh->SDNAnr]==2) {
1234 temp= dna_reconstruct(fd->memsdna, fd->filesdna, fd->compflags, bh->SDNAnr, bh->nr, (bh+1));
1236 temp= MEM_mallocN(bh->len, blockname);
1237 memcpy(temp, (bh+1), bh->len);
1245 static void link_list(FileData *fd, ListBase *lb) /* only direct data */
1249 if(lb->first==NULL) return;
1251 lb->first= newdataadr(fd, lb->first);
1255 ln->next= newdataadr(fd, ln->next);
1263 static void link_glob_list(FileData *fd, ListBase *lb) /* for glob data */
1268 if(lb->first==0) return;
1269 poin= newdataadr(fd, lb->first);
1271 oldnewmap_insert(fd->globmap, lb->first, poin, 0);
1278 poin= newdataadr(fd, ln->next);
1280 oldnewmap_insert(fd->globmap, ln->next, poin, 0);
1290 static void test_pointer_array(FileData *fd, void **mat)
1292 #if defined(WIN32) && !defined(FREE_WINDOWS)
1293 __int64 *lpoin, *lmat;
1295 long long *lpoin, *lmat;
1297 int len, *ipoin, *imat;
1299 /* manually convert the pointer array in
1300 * the old dna format to a pointer array in
1301 * the new dna format.
1304 len= MEM_allocN_len(*mat)/fd->filesdna->pointerlen;
1306 if(fd->filesdna->pointerlen==8 && fd->memsdna->pointerlen==4) {
1307 ipoin=imat= MEM_mallocN( len*4, "newmatar");
1311 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN))
1312 SWITCH_LONGINT(*lpoin);
1313 *ipoin= (int) ((*lpoin) >> 3);
1321 if(fd->filesdna->pointerlen==4 && fd->memsdna->pointerlen==8) {
1322 lpoin=lmat= MEM_mallocN( len*8, "newmatar");
1336 /* ************ READ ID Properties *************** */
1338 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd);
1339 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd);
1341 void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, void *fd)
1345 /*since we didn't save the extra buffer, set totallen to len.*/
1346 prop->totallen = prop->len;
1347 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1349 if (switch_endian) {
1350 if (prop->subtype != IDP_DOUBLE) {
1351 for (i=0; i<prop->len; i++) {
1352 SWITCH_INT(((int*)prop->data.pointer)[i]);
1355 for (i=0; i<prop->len; i++) {
1356 SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
1362 void IDP_DirectLinkString(IDProperty *prop, int switch_endian, void *fd)
1364 /*since we didn't save the extra string buffer, set totallen to len.*/
1365 prop->totallen = prop->len;
1366 prop->data.pointer = newdataadr(fd, prop->data.pointer);
1369 void IDP_DirectLinkGroup(IDProperty *prop, int switch_endian, void *fd)
1371 ListBase *lb = &prop->data.group;
1376 /*Link child id properties now*/
1377 for (loop=prop->data.group.first; loop; loop=loop->next) {
1378 IDP_DirectLinkProperty(loop, switch_endian, fd);
1382 void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd)
1384 switch (prop->type) {
1386 IDP_DirectLinkGroup(prop, switch_endian, fd);
1389 IDP_DirectLinkString(prop, switch_endian, fd);
1392 IDP_DirectLinkArray(prop, switch_endian, fd);
1395 /*erg, stupid doubles. since I'm storing them
1396 in the same field as int val; val2 in the
1397 IDPropertyData struct, they have to deal with
1398 endianness specifically
1400 in theory, val and val2 would've already been swapped
1401 if switch_endian is true, so we have to first unswap
1402 them then reswap them as a single 64-bit entity.
1405 if (switch_endian) {
1406 SWITCH_INT(prop->data.val);
1407 SWITCH_INT(prop->data.val2);
1408 SWITCH_LONGINT(prop->data.val);
1416 void IDP_LibLinkProperty(IDProperty *prop, int switch_endian, void *fd)
1420 /* ************ READ Brush *************** */
1421 /* library brush linking after fileread */
1422 static void lib_link_brush(FileData *fd, Main *main)
1428 /* only link ID pointers */
1429 for(brush= main->brush.first; brush; brush= brush->id.next) {
1430 if(brush->id.flag & LIB_NEEDLINK) {
1431 brush->id.flag -= LIB_NEEDLINK;
1433 for(a=0; a<MAX_MTEX; a++) {
1434 mtex= brush->mtex[a];
1436 mtex->tex= newlibadr_us(fd, brush->id.lib, mtex->tex);
1442 static void direct_link_brush(FileData *fd, Brush *brush)
1444 /* brush itself has been read */
1447 for(a=0; a<MAX_MTEX; a++)
1448 brush->mtex[a]= newdataadr(fd, brush->mtex[a]);
1451 static void direct_link_script(FileData *fd, Script *script)
1454 SCRIPT_SET_NULL(script)
1457 /* ************ READ CurveMapping *************** */
1459 /* cuma itself has been read! */
1460 static void direct_link_curvemapping(FileData *fd, CurveMapping *cumap)
1464 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1465 cumap->flag &= ~CUMA_PREMULLED;
1467 for(a=0; a<CM_TOT; a++) {
1468 cumap->cm[a].curve= newdataadr(fd, cumap->cm[a].curve);
1469 cumap->cm[a].table= NULL;
1473 /* ************ READ NODE TREE *************** */
1475 /* singe node tree (also used for material/scene trees), ntree is not NULL */
1476 static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
1480 for(node= ntree->nodes.first; node; node= node->next)
1481 node->id= newlibadr_us(fd, id->lib, node->id);
1484 /* library ntree linking after fileread */
1485 static void lib_link_nodetree(FileData *fd, Main *main)
1489 /* only link ID pointers */
1490 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1491 if(ntree->id.flag & LIB_NEEDLINK) {
1492 ntree->id.flag -= LIB_NEEDLINK;
1493 lib_link_ntree(fd, &ntree->id, ntree);
1498 /* verify types for nodes and groups, all data has to be read */
1499 /* open = 0: appending/linking, open = 1: open new file (need to clean out dynamic
1501 static void lib_verify_nodetree(Main *main, int open)
1507 /* this crashes blender on undo/redo
1509 reinit_nodesystem();
1512 /* now create the own typeinfo structs an verify nodes */
1513 /* here we still assume no groups in groups */
1514 for(ntree= main->nodetree.first; ntree; ntree= ntree->id.next) {
1515 ntreeVerifyTypes(ntree); /* internal nodes, no groups! */
1516 ntreeMakeOwnType(ntree); /* for group usage */
1519 /* now verify all types in material trees, groups are set OK now */
1520 for(ma= main->mat.first; ma; ma= ma->id.next) {
1522 ntreeVerifyTypes(ma->nodetree);
1524 /* and scene trees */
1525 for(sce= main->scene.first; sce; sce= sce->id.next) {
1527 ntreeVerifyTypes(sce->nodetree);
1533 /* ntree itself has been read! */
1534 static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
1536 /* note: writing and reading goes in sync, for speed */
1541 ntree->init= 0; /* to set callbacks and force setting types */
1542 ntree->owntype= NULL;
1543 ntree->timecursor= NULL;
1545 link_list(fd, &ntree->nodes);
1546 for(node= ntree->nodes.first; node; node= node->next) {
1547 if(node->type == NODE_DYNAMIC) {
1549 node->custom1= BSET(node->custom1, NODE_DYNAMIC_LOADED);
1550 node->typeinfo= NULL;
1553 node->storage= newdataadr(fd, node->storage);
1556 /* could be handlerized at some point */
1557 if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
1558 direct_link_curvemapping(fd, node->storage);
1559 else if(ntree->type==NTREE_COMPOSIT) {
1560 if( ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB))
1561 direct_link_curvemapping(fd, node->storage);
1562 else if(ELEM3(node->type, CMP_NODE_IMAGE, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
1563 ((ImageUser *)node->storage)->ok= 1;
1566 link_list(fd, &node->inputs);
1567 link_list(fd, &node->outputs);
1569 link_list(fd, &ntree->links);
1571 /* and we connect the rest */
1572 for(node= ntree->nodes.first; node; node= node->next) {
1573 node->preview= newimaadr(fd, node->preview);
1575 for(sock= node->inputs.first; sock; sock= sock->next)
1576 sock->link= newdataadr(fd, sock->link);
1577 for(sock= node->outputs.first; sock; sock= sock->next)
1578 sock->ns.data= NULL;
1580 for(link= ntree->links.first; link; link= link->next) {
1581 link->fromnode= newdataadr(fd, link->fromnode);
1582 link->tonode= newdataadr(fd, link->tonode);
1583 link->fromsock= newdataadr(fd, link->fromsock);
1584 link->tosock= newdataadr(fd, link->tosock);
1587 /* set selin and selout */
1588 for(node= ntree->nodes.first; node; node= node->next) {
1589 for(sock= node->inputs.first; sock; sock= sock->next) {
1590 if(sock->flag & SOCK_SEL) {
1595 for(sock= node->outputs.first; sock; sock= sock->next) {
1596 if(sock->flag & SOCK_SEL) {
1597 ntree->selout= sock;
1603 /* type verification is in lib-link */
1606 /* ************ READ PACKEDFILE *************** */
1608 static PackedFile *direct_link_packedfile(FileData *fd, PackedFile *oldpf)
1610 PackedFile *pf= newdataadr(fd, oldpf);
1613 pf->data= newdataadr(fd, pf->data);
1619 /* ************ READ IMAGE PREVIEW *************** */
1621 static PreviewImage *direct_link_preview_image(FileData *fd, PreviewImage *old_prv)
1623 PreviewImage *prv= newdataadr(fd, old_prv);
1627 for (i=0; i < PREVIEW_MIPMAPS; ++i) {
1629 prv->rect[i] = newdataadr(fd, prv->rect[i]);
1637 /* ************ READ SCRIPTLINK *************** */
1639 static void lib_link_scriptlink(FileData *fd, ID *id, ScriptLink *slink)
1643 for(i=0; i<slink->totscript; i++) {
1644 slink->scripts[i]= newlibadr(fd, id->lib, slink->scripts[i]);
1648 static void direct_link_scriptlink(FileData *fd, ScriptLink *slink)
1650 slink->scripts= newdataadr(fd, slink->scripts);
1651 test_pointer_array(fd, (void **)&slink->scripts);
1653 slink->flag= newdataadr(fd, slink->flag);
1655 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
1658 for(a=0; a<slink->totscript; a++) {
1659 SWITCH_SHORT(slink->flag[a]);
1664 /* ************ READ ARMATURE ***************** */
1666 static void lib_link_nlastrips(FileData *fd, ID *id, ListBase *striplist)
1668 bActionStrip *strip;
1669 bActionModifier *amod;
1671 for (strip=striplist->first; strip; strip=strip->next){
1672 strip->object = newlibadr(fd, id->lib, strip->object);
1673 strip->act = newlibadr_us(fd, id->lib, strip->act);
1674 strip->ipo = newlibadr(fd, id->lib, strip->ipo);
1675 for(amod= strip->modifiers.first; amod; amod= amod->next)
1676 amod->ob= newlibadr(fd, id->lib, amod->ob);
1680 static void lib_link_constraint_channels(FileData *fd, ID *id, ListBase *chanbase)
1682 bConstraintChannel *chan;
1684 for (chan=chanbase->first; chan; chan=chan->next){
1685 chan->ipo = newlibadr_us(fd, id->lib, chan->ipo);
1689 static void lib_link_constraints(FileData *fd, ID *id, ListBase *conlist)
1693 for (con = conlist->first; con; con=con->next) {
1694 /* patch for error introduced by changing constraints (dunno how) */
1695 /* if con->data type changes, dna cannot resolve the pointer! (ton) */
1696 if(con->data==NULL) {
1697 con->type= CONSTRAINT_TYPE_NULL;
1699 /* own ipo, all constraints have it */
1700 con->ipo= newlibadr_us(fd, id->lib, con->ipo);
1702 switch (con->type) {
1703 case CONSTRAINT_TYPE_PYTHON:
1705 bPythonConstraint *data= (bPythonConstraint*)con->data;
1706 bConstraintTarget *ct;
1708 for (ct= data->targets.first; ct; ct= ct->next)
1709 ct->tar = newlibadr(fd, id->lib, ct->tar);
1711 data->text = newlibadr(fd, id->lib, data->text);
1712 //IDP_LibLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1715 case CONSTRAINT_TYPE_ACTION:
1717 bActionConstraint *data;
1718 data= ((bActionConstraint*)con->data);
1719 data->tar = newlibadr(fd, id->lib, data->tar);
1720 data->act = newlibadr(fd, id->lib, data->act);
1723 case CONSTRAINT_TYPE_LOCLIKE:
1725 bLocateLikeConstraint *data;
1726 data= ((bLocateLikeConstraint*)con->data);
1727 data->tar = newlibadr(fd, id->lib, data->tar);
1730 case CONSTRAINT_TYPE_ROTLIKE:
1732 bRotateLikeConstraint *data;
1733 data= ((bRotateLikeConstraint*)con->data);
1734 data->tar = newlibadr(fd, id->lib, data->tar);
1737 case CONSTRAINT_TYPE_SIZELIKE:
1739 bSizeLikeConstraint *data;
1740 data= ((bSizeLikeConstraint*)con->data);
1741 data->tar = newlibadr(fd, id->lib, data->tar);
1744 case CONSTRAINT_TYPE_KINEMATIC:
1746 bKinematicConstraint *data;
1747 data = ((bKinematicConstraint*)con->data);
1748 data->tar = newlibadr(fd, id->lib, data->tar);
1749 data->poletar = newlibadr(fd, id->lib, data->poletar);
1752 case CONSTRAINT_TYPE_TRACKTO:
1754 bTrackToConstraint *data;
1755 data = ((bTrackToConstraint*)con->data);
1756 data->tar = newlibadr(fd, id->lib, data->tar);
1759 case CONSTRAINT_TYPE_MINMAX:
1761 bMinMaxConstraint *data;
1762 data = ((bMinMaxConstraint*)con->data);
1763 data->tar = newlibadr(fd, id->lib, data->tar);
1766 case CONSTRAINT_TYPE_LOCKTRACK:
1768 bLockTrackConstraint *data;
1769 data= ((bLockTrackConstraint*)con->data);
1770 data->tar = newlibadr(fd, id->lib, data->tar);
1773 case CONSTRAINT_TYPE_FOLLOWPATH:
1775 bFollowPathConstraint *data;
1776 data= ((bFollowPathConstraint*)con->data);
1777 data->tar = newlibadr(fd, id->lib, data->tar);
1780 case CONSTRAINT_TYPE_STRETCHTO:
1782 bStretchToConstraint *data;
1783 data= ((bStretchToConstraint*)con->data);
1784 data->tar = newlibadr(fd, id->lib, data->tar);
1787 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
1789 bRigidBodyJointConstraint *data;
1790 data= ((bRigidBodyJointConstraint*)con->data);
1791 data->tar = newlibadr(fd, id->lib, data->tar);
1794 case CONSTRAINT_TYPE_CLAMPTO:
1796 bClampToConstraint *data;
1797 data= ((bClampToConstraint*)con->data);
1798 data->tar = newlibadr(fd, id->lib, data->tar);
1801 case CONSTRAINT_TYPE_CHILDOF:
1803 bChildOfConstraint *data;
1804 data= ((bChildOfConstraint*)con->data);
1805 data->tar = newlibadr(fd, id->lib, data->tar);
1808 case CONSTRAINT_TYPE_TRANSFORM:
1810 bTransformConstraint *data;
1811 data= ((bTransformConstraint*)con->data);
1812 data->tar = newlibadr(fd, id->lib, data->tar);
1815 case CONSTRAINT_TYPE_DISTLIMIT:
1817 bDistLimitConstraint *data;
1818 data= ((bDistLimitConstraint*)con->data);
1819 data->tar = newlibadr(fd, id->lib, data->tar);
1822 case CONSTRAINT_TYPE_NULL:
1828 static void direct_link_constraints(FileData *fd, ListBase *lb)
1833 for (cons=lb->first; cons; cons=cons->next) {
1834 cons->data = newdataadr(fd, cons->data);
1836 if (cons->type == CONSTRAINT_TYPE_PYTHON) {
1837 bPythonConstraint *data= cons->data;
1838 link_list(fd, &data->targets);
1839 data->prop = newdataadr(fd, data->prop);
1840 IDP_DirectLinkProperty(data->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
1845 static void lib_link_pose(FileData *fd, Object *ob, bPose *pose)
1847 bPoseChannel *pchan;
1848 bArmature *arm= ob->data;
1854 /* always rebuild to match proxy or lib changes */
1855 rebuild= ob->proxy || (ob->id.lib==NULL && arm->id.lib);
1857 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
1858 lib_link_constraints(fd, (ID *)ob, &pchan->constraints);
1860 /* hurms... loop in a loop, but yah... later... (ton) */
1861 pchan->bone= get_named_bone(arm, pchan->name);
1863 pchan->custom= newlibadr(fd, arm->id.lib, pchan->custom);
1864 if(pchan->bone==NULL)
1866 else if(ob->id.lib==NULL && arm->id.lib) {
1867 /* local pose selection copied to armature, bit hackish */
1868 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1869 pchan->bone->flag |= pchan->selectflag;
1874 ob->recalc= OB_RECALC;
1875 pose->flag |= POSE_RECALC;
1879 static void lib_link_armature(FileData *fd, Main *main)
1883 arm= main->armature.first;
1886 if(arm->id.flag & LIB_NEEDLINK) {
1887 arm->id.flag -= LIB_NEEDLINK;
1893 static void lib_link_action(FileData *fd, Main *main)
1896 bActionChannel *chan;
1898 act= main->action.first;
1900 if(act->id.flag & LIB_NEEDLINK) {
1901 act->id.flag -= LIB_NEEDLINK;
1903 for (chan=act->chanbase.first; chan; chan=chan->next) {
1904 chan->ipo= newlibadr_us(fd, act->id.lib, chan->ipo);
1905 lib_link_constraint_channels(fd, &act->id, &chan->constraintChannels);
1913 static void direct_link_bones(FileData *fd, Bone* bone)
1917 bone->parent= newdataadr(fd, bone->parent);
1919 link_list(fd, &bone->childbase);
1921 for (child=bone->childbase.first; child; child=child->next) {
1922 direct_link_bones(fd, child);
1927 static void direct_link_action(FileData *fd, bAction *act)
1929 bActionChannel *achan;
1932 link_list(fd, &act->chanbase);
1933 link_list(fd, &act->groups);
1934 link_list(fd, &act->markers);
1936 for (achan = act->chanbase.first; achan; achan=achan->next) {
1937 achan->grp= newdataadr(fd, achan->grp);
1939 link_list(fd, &achan->constraintChannels);
1942 for (agrp = act->groups.first; agrp; agrp= agrp->next) {
1943 if (agrp->channels.first) {
1944 agrp->channels.first= newdataadr(fd, agrp->channels.first);
1945 agrp->channels.last= newdataadr(fd, agrp->channels.last);
1950 static void direct_link_armature(FileData *fd, bArmature *arm)
1954 link_list(fd, &arm->bonebase);
1956 bone=arm->bonebase.first;
1958 direct_link_bones(fd, bone);
1963 /* ************ READ CAMERA ***************** */
1965 static void lib_link_camera(FileData *fd, Main *main)
1969 ca= main->camera.first;
1971 if(ca->id.flag & LIB_NEEDLINK) {
1973 ca->ipo= newlibadr_us(fd, ca->id.lib, ca->ipo);
1975 ca->dof_ob= newlibadr_us(fd, ca->id.lib, ca->dof_ob);
1977 lib_link_scriptlink(fd, &ca->id, &ca->scriptlink);
1979 ca->id.flag -= LIB_NEEDLINK;
1985 static void direct_link_camera(FileData *fd, Camera *ca)
1987 direct_link_scriptlink(fd, &ca->scriptlink);
1991 /* ************ READ LAMP ***************** */
1993 static void lib_link_lamp(FileData *fd, Main *main)
1999 la= main->lamp.first;
2001 if(la->id.flag & LIB_NEEDLINK) {
2003 for(a=0; a<MAX_MTEX; a++) {
2006 mtex->tex= newlibadr_us(fd, la->id.lib, mtex->tex);
2007 mtex->object= newlibadr(fd, la->id.lib, mtex->object);
2011 la->ipo= newlibadr_us(fd, la->id.lib, la->ipo);
2013 lib_link_scriptlink(fd, &la->id, &la->scriptlink);
2015 la->id.flag -= LIB_NEEDLINK;
2021 static void direct_link_lamp(FileData *fd, Lamp *la)
2025 direct_link_scriptlink(fd, &la->scriptlink);
2027 for(a=0; a<MAX_MTEX; a++) {
2028 la->mtex[a]= newdataadr(fd, la->mtex[a]);
2031 la->curfalloff= newdataadr(fd, la->curfalloff);
2033 direct_link_curvemapping(fd, la->curfalloff);
2035 la->preview = direct_link_preview_image(fd, la->preview);
2038 /* ************ READ keys ***************** */
2040 static void lib_link_key(FileData *fd, Main *main)
2044 key= main->key.first;
2046 if(key->id.flag & LIB_NEEDLINK) {
2048 key->ipo= newlibadr_us(fd, key->id.lib, key->ipo);
2049 key->from= newlibadr(fd, key->id.lib, key->from);
2051 key->id.flag -= LIB_NEEDLINK;
2057 static void switch_endian_keyblock(Key *key, KeyBlock *kb)
2060 char *data, *poin, *cp;
2062 elemsize= key->elemsize;
2065 for(a=0; a<kb->totelem; a++) {
2070 while( cp[0] ) { /* cp[0]==amount */
2072 switch(cp[1]) { /* cp[1]= type */
2078 SWITCH_INT((*poin));
2091 static void direct_link_key(FileData *fd, Key *key)
2095 link_list(fd, &(key->block));
2097 key->refkey= newdataadr(fd, key->refkey);
2099 kb= key->block.first;
2102 kb->data= newdataadr(fd, kb->data);
2104 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN)
2105 switch_endian_keyblock(key, kb);
2111 /* ************ READ mball ***************** */
2113 static void lib_link_mball(FileData *fd, Main *main)
2118 mb= main->mball.first;
2120 if(mb->id.flag & LIB_NEEDLINK) {
2122 for(a=0; a<mb->totcol; a++) mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
2124 mb->ipo= newlibadr_us(fd, mb->id.lib, mb->ipo);
2126 mb->id.flag -= LIB_NEEDLINK;
2132 static void direct_link_mball(FileData *fd, MetaBall *mb)
2134 mb->mat= newdataadr(fd, mb->mat);
2135 test_pointer_array(fd, (void **)&mb->mat);
2137 link_list(fd, &(mb->elems));
2139 mb->disp.first= mb->disp.last= 0;
2144 /* ************ READ WORLD ***************** */
2146 static void lib_link_world(FileData *fd, Main *main)
2152 wrld= main->world.first;
2154 if(wrld->id.flag & LIB_NEEDLINK) {
2156 wrld->ipo= newlibadr_us(fd, wrld->id.lib, wrld->ipo);
2158 for(a=0; a<MAX_MTEX; a++) {
2159 mtex= wrld->mtex[a];
2161 mtex->tex= newlibadr_us(fd, wrld->id.lib, mtex->tex);
2162 mtex->object= newlibadr(fd, wrld->id.lib, mtex->object);
2166 lib_link_scriptlink(fd, &wrld->id, &wrld->scriptlink);
2168 wrld->id.flag -= LIB_NEEDLINK;
2170 wrld= wrld->id.next;
2174 static void direct_link_world(FileData *fd, World *wrld)
2178 direct_link_scriptlink(fd, &wrld->scriptlink);
2180 for(a=0; a<MAX_MTEX; a++) {
2181 wrld->mtex[a]= newdataadr(fd, wrld->mtex[a]);
2183 wrld->preview = direct_link_preview_image(fd, wrld->preview);
2187 /* ************ READ IPO ***************** */
2189 static void lib_link_ipo(FileData *fd, Main *main)
2193 ipo= main->ipo.first;
2195 if(ipo->id.flag & LIB_NEEDLINK) {
2197 for(icu= ipo->curve.first; icu; icu= icu->next) {
2199 icu->driver->ob= newlibadr(fd, ipo->id.lib, icu->driver->ob);
2201 ipo->id.flag -= LIB_NEEDLINK;
2207 static void direct_link_ipo(FileData *fd, Ipo *ipo)
2211 link_list(fd, &(ipo->curve));
2212 icu= ipo->curve.first;
2214 icu->bezt= newdataadr(fd, icu->bezt);
2215 icu->bp= newdataadr(fd, icu->bp);
2216 icu->driver= newdataadr(fd, icu->driver);
2221 /* ************ READ VFONT ***************** */
2223 static void lib_link_vfont(FileData *fd, Main *main)
2227 vf= main->vfont.first;
2229 if(vf->id.flag & LIB_NEEDLINK) {
2230 vf->id.flag -= LIB_NEEDLINK;
2236 static void direct_link_vfont(FileData *fd, VFont *vf)
2239 vf->packedfile= direct_link_packedfile(fd, vf->packedfile);
2242 /* ************ READ TEXT ****************** */
2244 static void lib_link_text(FileData *fd, Main *main)
2248 text= main->text.first;
2250 if(text->id.flag & LIB_NEEDLINK) {
2251 text->id.flag -= LIB_NEEDLINK;
2253 text= text->id.next;
2257 static void direct_link_text(FileData *fd, Text *text)
2261 text->name= newdataadr(fd, text->name);
2264 text->undo_len= TXT_INIT_UNDO;
2265 text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
2267 text->compiled= NULL;
2270 if(text->flags & TXT_ISEXT) {
2275 link_list(fd, &text->lines);
2277 text->curl= newdataadr(fd, text->curl);
2278 text->sell= newdataadr(fd, text->sell);
2280 ln= text->lines.first;
2282 ln->line= newdataadr(fd, ln->line);
2285 if (ln->len != (int) strlen(ln->line)) {
2286 printf("Error loading text, line lengths differ\n");
2287 ln->len = strlen(ln->line);
2293 text->flags = (text->flags|TXT_ISTMP) & ~TXT_ISEXT;
2298 /* ************ READ IMAGE ***************** */
2300 static void lib_link_image(FileData *fd, Main *main)
2304 ima= main->image.first;
2306 if(ima->id.flag & LIB_NEEDLINK) {
2307 if (ima->id.properties) IDP_LibLinkProperty(ima->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2309 ima->id.flag -= LIB_NEEDLINK;
2315 static void link_ibuf_list(FileData *fd, ListBase *lb)
2319 if(lb->first==NULL) return;
2321 lb->first= newimaadr(fd, lb->first);
2325 ln->next= newimaadr(fd, ln->next);
2333 static void direct_link_image(FileData *fd, Image *ima)
2335 /* for undo system, pointers could be restored */
2337 link_ibuf_list(fd, &ima->ibufs);
2339 ima->ibufs.first= ima->ibufs.last= NULL;
2341 /* if not restored, we keep the binded opengl index */
2342 if(ima->ibufs.first==NULL)
2349 ima->packedfile = direct_link_packedfile(fd, ima->packedfile);
2350 ima->preview = direct_link_preview_image(fd, ima->preview);
2355 /* ************ READ CURVE ***************** */
2357 static void lib_link_curve(FileData *fd, Main *main)
2362 cu= main->curve.first;
2364 if(cu->id.flag & LIB_NEEDLINK) {
2366 for(a=0; a<cu->totcol; a++) cu->mat[a]= newlibadr_us(fd, cu->id.lib, cu->mat[a]);
2368 cu->bevobj= newlibadr(fd, cu->id.lib, cu->bevobj);
2369 cu->taperobj= newlibadr(fd, cu->id.lib, cu->taperobj);
2370 cu->textoncurve= newlibadr(fd, cu->id.lib, cu->textoncurve);
2371 cu->vfont= newlibadr_us(fd, cu->id.lib, cu->vfont);
2372 cu->vfontb= newlibadr_us(fd, cu->id.lib, cu->vfontb);
2373 cu->vfonti= newlibadr_us(fd, cu->id.lib, cu->vfonti);
2374 cu->vfontbi= newlibadr_us(fd, cu->id.lib, cu->vfontbi);
2376 cu->ipo= newlibadr_us(fd, cu->id.lib, cu->ipo);
2377 cu->key= newlibadr_us(fd, cu->id.lib, cu->key);
2379 cu->id.flag -= LIB_NEEDLINK;
2386 static void switch_endian_knots(Nurb *nu)
2393 SWITCH_INT(nu->knotsu[len]);
2399 SWITCH_INT(nu->knotsv[len]);
2404 static void direct_link_curve(FileData *fd, Curve *cu)
2409 cu->mat= newdataadr(fd, cu->mat);
2410 test_pointer_array(fd, (void **)&cu->mat);
2411 cu->str= newdataadr(fd, cu->str);
2412 cu->strinfo= newdataadr(fd, cu->strinfo);
2413 cu->tb= newdataadr(fd, cu->tb);
2415 if(cu->vfont==0) link_list(fd, &(cu->nurb));
2417 cu->nurb.first=cu->nurb.last= 0;
2419 tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBoxread");
2421 memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
2428 cu->tb[0].w = cu->linewidth;
2430 if (cu->wordspace == 0.0) cu->wordspace = 1.0;
2433 cu->bev.first=cu->bev.last= 0;
2434 cu->disp.first=cu->disp.last= 0;
2439 nu->bezt= newdataadr(fd, nu->bezt);
2440 nu->bp= newdataadr(fd, nu->bp);
2441 nu->knotsu= newdataadr(fd, nu->knotsu);
2442 nu->knotsv= newdataadr(fd, nu->knotsv);
2443 if (cu->vfont==0) nu->charidx= nu->mat_nr;
2445 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
2446 switch_endian_knots(nu);
2454 /* ************ READ TEX ***************** */
2456 static void lib_link_texture(FileData *fd, Main *main)
2460 tex= main->tex.first;
2462 if(tex->id.flag & LIB_NEEDLINK) {
2464 tex->ima= newlibadr_us(fd, tex->id.lib, tex->ima);
2465 tex->ipo= newlibadr_us(fd, tex->id.lib, tex->ipo);
2466 if(tex->env) tex->env->object= newlibadr(fd, tex->id.lib, tex->env->object);
2468 tex->id.flag -= LIB_NEEDLINK;
2474 static void direct_link_texture(FileData *fd, Tex *tex)
2476 tex->plugin= newdataadr(fd, tex->plugin);
2478 tex->plugin->handle= 0;
2479 open_plugin_tex(tex->plugin);
2480 /* initialize data for this instance, if an initialization
2483 if (tex->plugin->instance_init)
2484 tex->plugin->instance_init((void *) tex->plugin->data);
2486 tex->coba= newdataadr(fd, tex->coba);
2487 tex->env= newdataadr(fd, tex->env);
2489 tex->env->ima= NULL;
2490 memset(tex->env->cube, 0, 6*sizeof(void *));
2493 tex->preview = direct_link_preview_image(fd, tex->preview);
2500 /* ************ READ MATERIAL ***************** */
2502 static void lib_link_material(FileData *fd, Main *main)
2508 ma= main->mat.first;
2510 if(ma->id.flag & LIB_NEEDLINK) {
2511 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2512 of library blocks that implement this.*/
2513 if (ma->id.properties) IDP_LibLinkProperty(ma->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2515 ma->ipo= newlibadr_us(fd, ma->id.lib, ma->ipo);
2516 ma->group= newlibadr_us(fd, ma->id.lib, ma->group);
2518 for(a=0; a<MAX_MTEX; a++) {
2521 mtex->tex= newlibadr_us(fd, ma->id.lib, mtex->tex);
2522 mtex->object= newlibadr(fd, ma->id.lib, mtex->object);
2525 lib_link_scriptlink(fd, &ma->id, &ma->scriptlink);
2528 lib_link_ntree(fd, &ma->id, ma->nodetree);
2530 ma->id.flag -= LIB_NEEDLINK;
2536 static void direct_link_material(FileData *fd, Material *ma)
2540 for(a=0; a<MAX_MTEX; a++) {
2541 ma->mtex[a]= newdataadr(fd, ma->mtex[a]);
2544 ma->ramp_col= newdataadr(fd, ma->ramp_col);
2545 ma->ramp_spec= newdataadr(fd, ma->ramp_spec);
2547 direct_link_scriptlink(fd, &ma->scriptlink);
2549 ma->nodetree= newdataadr(fd, ma->nodetree);
2551 direct_link_nodetree(fd, ma->nodetree);
2553 ma->preview = direct_link_preview_image(fd, ma->preview);
2556 /* ************ READ PARTICLE SETTINGS ***************** */
2558 static void direct_link_pointcache(FileData *fd, PointCache *cache)
2560 cache->flag &= ~(PTCACHE_SIMULATION_VALID|PTCACHE_BAKE_EDIT_ACTIVE);
2564 static void lib_link_particlesettings(FileData *fd, Main *main)
2566 ParticleSettings *part;
2568 part= main->particle.first;
2570 if(part->id.flag & LIB_NEEDLINK) {
2571 part->ipo= newlibadr_us(fd, part->id.lib, part->ipo);
2572 part->dup_ob = newlibadr(fd, part->id.lib, part->dup_ob);
2573 part->dup_group = newlibadr(fd, part->id.lib, part->dup_group);
2574 part->eff_group = newlibadr(fd, part->id.lib, part->eff_group);
2575 part->bb_ob = newlibadr(fd, part->id.lib, part->bb_ob);
2576 part->id.flag -= LIB_NEEDLINK;
2578 part= part->id.next;
2582 static void direct_link_particlesettings(FileData *fd, ParticleSettings *part)
2584 part->pd= newdataadr(fd, part->pd);
2587 static void lib_link_particlesystems(FileData *fd, ID *id, ListBase *particles)
2589 ParticleSystem *psys;
2592 for(psys=particles->first; psys; psys=psys->next){
2594 psys->part = newlibadr_us(fd, id->lib, psys->part);
2595 psys->target_ob = newlibadr(fd, id->lib, psys->target_ob);
2596 psys->keyed_ob = newlibadr(fd, id->lib, psys->keyed_ob);
2598 for(a=0,pa=psys->particles; a<psys->totpart; a++,pa++){
2599 pa->stick_ob=newlibadr(fd, id->lib, pa->stick_ob);
2603 static void direct_link_particlesystems(FileData *fd, ListBase *particles)
2605 ParticleSystem *psys;
2608 for(psys=particles->first; psys; psys=psys->next) {
2609 psys->particles=newdataadr(fd,psys->particles);
2610 if(psys->particles && psys->particles->hair){
2611 ParticleData *pa = psys->particles;
2612 for(a=0; a<psys->totpart; a++, pa++)
2613 pa->hair=newdataadr(fd,pa->hair);
2615 if(psys->particles && psys->particles->keys){
2616 ParticleData *pa = psys->particles;
2617 for(a=0; a<psys->totpart; a++, pa++) {
2622 psys->flag &= ~PSYS_KEYED;
2624 psys->child=newdataadr(fd,psys->child);
2625 psys->effectors.first=psys->effectors.last=0;
2627 psys->soft= newdataadr(fd, psys->soft);
2629 SoftBody *sb = psys->soft;
2630 sb->particles = psys;
2631 sb->bpoint= NULL; // init pointers so it gets rebuilt nicely
2635 sb->pointcache= newdataadr(fd, sb->pointcache);
2637 direct_link_pointcache(fd, sb->pointcache);
2641 psys->pathcache = 0;
2642 psys->childcache = 0;
2643 psys->pathcachebufs.first = psys->pathcachebufs.last = 0;
2644 psys->childcachebufs.first = psys->childcachebufs.last = 0;
2645 psys->reactevents.first = psys->reactevents.last = 0;
2647 psys->pointcache= newdataadr(fd, psys->pointcache);
2648 if(psys->pointcache)
2649 direct_link_pointcache(fd, psys->pointcache);
2654 /* ************ READ MESH ***************** */
2656 static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
2661 for (i=0; i<totface; i++, tf++) {
2662 tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
2663 if(tf->tpage && tf->tpage->id.us==0)
2664 tf->tpage->id.us= 1;
2668 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
2671 for(i=0; i<fdata->totlayer; i++) {
2672 CustomDataLayer *layer = &fdata->layers[i];
2674 if(layer->type == CD_MTFACE)
2675 lib_link_mtface(fd, me, layer->data, totface);
2680 static void lib_link_mesh(FileData *fd, Main *main)
2684 me= main->mesh.first;
2686 if(me->id.flag & LIB_NEEDLINK) {
2689 /*Link ID Properties -- and copy this comment EXACTLY for easy finding
2690 of library blocks that implement this.*/
2691 if (me->id.properties) IDP_LibLinkProperty(me->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2693 /* this check added for python created meshes */
2695 for(i=0; i<me->totcol; i++) {
2696 me->mat[i]= newlibadr_us(fd, me->id.lib, me->mat[i]);
2701 me->ipo= newlibadr_us(fd, me->id.lib, me->ipo);
2702 me->key= newlibadr_us(fd, me->id.lib, me->key);
2703 me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
2705 lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
2706 if(me->mr && me->mr->levels.first)
2707 lib_link_customdata_mtface(fd, me, &me->mr->fdata,
2708 ((MultiresLevel*)me->mr->levels.first)->totface);
2710 me->id.flag -= LIB_NEEDLINK;
2716 static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
2723 for (i=0; i<count; i++) {
2724 mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
2726 mdverts[i].totweight=0;
2730 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
2734 data->layers= newdataadr(fd, data->layers);
2736 while (i < data->totlayer) {
2737 CustomDataLayer *layer = &data->layers[i];
2739 if (CustomData_verify_versions(data, i)) {
2740 layer->data = newdataadr(fd, layer->data);
2746 static void direct_link_mesh(FileData *fd, Mesh *mesh)
2748 mesh->mat= newdataadr(fd, mesh->mat);
2749 test_pointer_array(fd, (void **)&mesh->mat);
2751 mesh->mvert= newdataadr(fd, mesh->mvert);
2752 mesh->medge= newdataadr(fd, mesh->medge);
2753 mesh->mface= newdataadr(fd, mesh->mface);
2754 mesh->tface= newdataadr(fd, mesh->tface);
2755 mesh->mtface= newdataadr(fd, mesh->mtface);
2756 mesh->mcol= newdataadr(fd, mesh->mcol);
2757 mesh->msticky= newdataadr(fd, mesh->msticky);
2758 mesh->dvert= newdataadr(fd, mesh->dvert);
2760 /* Partial-mesh visibility (do this before using totvert, totface, or totedge!) */
2761 mesh->pv= newdataadr(fd, mesh->pv);
2763 mesh->pv->vert_map= newdataadr(fd, mesh->pv->vert_map);
2764 mesh->pv->edge_map= newdataadr(fd, mesh->pv->edge_map);
2765 mesh->pv->old_faces= newdataadr(fd, mesh->pv->old_faces);
2766 mesh->pv->old_edges= newdataadr(fd, mesh->pv->old_edges);
2769 /* normally direct_link_dverts should be called in direct_link_customdata,
2770 but for backwards compat in do_versions to work we do it here */
2771 direct_link_dverts(fd, mesh->pv ? mesh->pv->totvert : mesh->totvert, mesh->dvert);
2773 direct_link_customdata(fd, &mesh->vdata, mesh->pv ? mesh->pv->totvert : mesh->totvert);
2774 direct_link_customdata(fd, &mesh->edata, mesh->pv ? mesh->pv->totedge : mesh->totedge);
2775 direct_link_customdata(fd, &mesh->fdata, mesh->pv ? mesh->pv->totface : mesh->totface);
2778 mesh->mselect = NULL;
2781 mesh->mr= newdataadr(fd, mesh->mr);
2785 link_list(fd, &mesh->mr->levels);
2786 lvl= mesh->mr->levels.first;
2788 direct_link_customdata(fd, &mesh->mr->vdata, lvl->totvert);
2789 direct_link_dverts(fd, lvl->totvert, CustomData_get(&mesh->mr->vdata, 0, CD_MDEFORMVERT));
2790 direct_link_customdata(fd, &mesh->mr->fdata, lvl->totface);
2792 if(mesh->mr->edge_flags)
2793 mesh->mr->edge_flags= newdataadr(fd, mesh->mr->edge_flags);
2794 if(mesh->mr->edge_creases)
2795 mesh->mr->edge_creases= newdataadr(fd, mesh->mr->edge_creases);
2797 if(!mesh->mr->edge_flags)
2798 mesh->mr->edge_flags= MEM_callocN(sizeof(short)*lvl->totedge, "Multires Edge Flags");
2799 if(!mesh->mr->edge_creases)
2800 mesh->mr->edge_creases= MEM_callocN(sizeof(char)*lvl->totedge, "Multires Edge Creases");
2802 mesh->mr->verts = newdataadr(fd, mesh->mr->verts);
2804 for(; lvl; lvl= lvl->next) {
2805 lvl->verts= newdataadr(fd, lvl->verts);
2806 lvl->faces= newdataadr(fd, lvl->faces);
2807 lvl->edges= newdataadr(fd, lvl->edges);
2808 lvl->colfaces= newdataadr(fd, lvl->colfaces);
2809 lvl->edge_boundary_states= NULL;
2810 lvl->vert_face_map = lvl->vert_edge_map = NULL;
2815 if((fd->flags & FD_FLAGS_SWITCH_ENDIAN) && mesh->tface) {
2816 TFace *tf= mesh->tface;
2819 for (i=0; i< (mesh->pv ? mesh->pv->totface : mesh->totface); i++, tf++) {
2820 SWITCH_INT(tf->col[0]);
2821 SWITCH_INT(tf->col[1]);
2822 SWITCH_INT(tf->col[2]);
2823 SWITCH_INT(tf->col[3]);
2828 /* ************ READ LATTICE ***************** */
2830 static void lib_link_latt(FileData *fd, Main *main)
2834 lt= main->latt.first;
2836 if(lt->id.flag & LIB_NEEDLINK) {
2838 lt->ipo= newlibadr_us(fd, lt->id.lib, lt->ipo);
2839 lt->key= newlibadr_us(fd, lt->id.lib, lt->key);
2841 lt->id.flag -= LIB_NEEDLINK;
2847 static void direct_link_latt(FileData *fd, Lattice *lt)
2849 lt->def= newdataadr(fd, lt->def);
2851 lt->dvert= newdataadr(fd, lt->dvert);
2852 direct_link_dverts(fd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
2856 /* ************ READ OBJECT ***************** */
2858 static void lib_link_modifiers__linkModifiers(void *userData, Object *ob,
2861 FileData *fd = userData;
2863 *idpoin = newlibadr(fd, ob->id.lib, *idpoin);
2864 /* hardcoded bad exception; non-object modifier data gets user count (texture, displace) */
2865 if(*idpoin && GS((*idpoin)->name)!=ID_OB)
2868 static void lib_link_modifiers(FileData *fd, Object *ob)
2870 modifiers_foreachIDLink(ob, lib_link_modifiers__linkModifiers, fd);
2873 static void lib_link_object(FileData *fd, Main *main)
2883 ob= main->object.first;
2885 if(ob->id.flag & LIB_NEEDLINK) {
2886 if (ob->id.properties) IDP_LibLinkProperty(ob->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
2888 ob->parent= newlibadr(fd, ob->id.lib, ob->parent);
2889 ob->track= newlibadr(fd, ob->id.lib, ob->track);
2890 ob->ipo= newlibadr_us(fd, ob->id.lib, ob->ipo);
2891 ob->action = newlibadr_us(fd, ob->id.lib, ob->action);
2892 ob->poselib= newlibadr_us(fd, ob->id.lib, ob->poselib);
2893 ob->dup_group= newlibadr_us(fd, ob->id.lib, ob->dup_group);
2895 ob->proxy= newlibadr_us(fd, ob->id.lib, ob->proxy);
2897 /* paranoia check, actually a proxy_from pointer should never be written... */
2898 if(ob->proxy->id.lib==NULL) {
2899 ob->proxy->proxy_from= NULL;
2903 /* this triggers object_update to always use a copy */
2904 ob->proxy->proxy_from= ob;
2905 /* force proxy updates after load/undo, a bit weak */
2906 ob->recalc= ob->proxy->recalc= OB_RECALC;
2909 ob->proxy_group= newlibadr(fd, ob->id.lib, ob->proxy_group);
2912 ob->data= newlibadr_us(fd, ob->id.lib, ob->data);
2914 if(ob->data==NULL && poin!=NULL) {
2917 if(ob->id.lib) printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
2918 else printf("Object %s lost data.", ob->id.name+2);
2921 free_pose(ob->pose);
2923 ob->flag &= ~OB_POSEMODE;
2926 for(a=0; a<ob->totcol; a++) ob->mat[a]= newlibadr_us(fd, ob->id.lib, ob->mat[a]);
2928 ob->id.flag -= LIB_NEEDLINK;
2929 /* if id.us==0 a new base will be created later on */
2931 /* WARNING! Also check expand_object(), should reflect the stuff below. */
2932 lib_link_pose(fd, ob, ob->pose);
2933 lib_link_constraints(fd, &ob->id, &ob->constraints);
2934 lib_link_nlastrips(fd, &ob->id, &ob->nlastrips);
2935 lib_link_constraint_channels(fd, &ob->id, &ob->constraintChannels);
2937 for(paf= ob->effect.first; paf; paf= paf->next) {
2938 if(paf->type==EFF_PARTICLE) {
2939 paf->group= newlibadr_us(fd, ob->id.lib, paf->group);
2943 sens= ob->sensors.first;
2945 if(ob->id.lib==NULL) { // done in expand_main
2946 for(a=0; a<sens->totlinks; a++) {
2947 sens->links[a]= newglobadr(fd, sens->links[a]);
2950 if(sens->type==SENS_TOUCH) {
2951 bTouchSensor *ts= sens->data;
2952 ts->ma= newlibadr(fd, ob->id.lib, ts->ma);
2954 else if(sens->type==SENS_MESSAGE) {
2955 bMessageSensor *ms= sens->data;
2957 newlibadr(fd, ob->id.lib, ms->fromObject);
2962 cont= ob->controllers.first;
2964 if(ob->id.lib==NULL) { // done in expand_main
2965 for(a=0; a<cont->totlinks; a++) {
2966 cont->links[a]= newglobadr(fd, cont->links[a]);
2969 if(cont->type==CONT_PYTHON) {
2970 bPythonCont *pc= cont->data;
2971 pc->text= newlibadr(fd, ob->id.lib, pc->text);
2979 act= ob->actuators.first;
2981 if(act->type==ACT_SOUND) {
2982 bSoundActuator *sa= act->data;
2983 sa->sound= newlibadr_us(fd, ob->id.lib, sa->sound);
2985 else if(act->type==ACT_CD) {
2986 /* bCDActuator *cda= act->data; */
2988 else if(act->type==ACT_GAME) {
2989 /* bGameActuator *ga= act->data; */
2991 else if(act->type==ACT_CAMERA) {
2992 bCameraActuator *ca= act->data;
2993 ca->ob= newlibadr(fd, ob->id.lib, ca->ob);
2995 /* leave this one, it's obsolete but necessary to read for conversion */
2996 else if(act->type==ACT_ADD_OBJECT) {
2997 bAddObjectActuator *eoa= act->data;
2998 if(eoa) eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
3000 else if(act->type==ACT_EDIT_OBJECT) {
3001 bEditObjectActuator *eoa= act->data;
3006 eoa->ob= newlibadr(fd, ob->id.lib, eoa->ob);
3007 eoa->me= newlibadr(fd, ob->id.lib, eoa->me);
3010 else if(act->type==ACT_SCENE) {
3011 bSceneActuator *sa= act->data;
3012 sa->camera= newlibadr(fd, ob->id.lib, sa->camera);
3013 sa->scene= newlibadr(fd, ob->id.lib, sa->scene);
3015 else if(act->type==ACT_ACTION) {
3016 bActionActuator *aa= act->data;
3017 aa->act= newlibadr(fd, ob->id.lib, aa->act);
3019 else if(act->type==ACT_SHAPEACTION) {
3020 bActionActuator *aa= act->data;
3021 aa->act= newlibadr(fd, ob->id.lib, aa->act);
3023 else if(act->type==ACT_PROPERTY) {
3024 bPropertyActuator *pa= act->data;
3025 pa->ob= newlibadr(fd, ob->id.lib, pa->ob);
3027 else if(act->type==ACT_MESSAGE) {
3028 bMessageActuator *ma= act->data;
3029 ma->toObject= newlibadr(fd, ob->id.lib, ma->toObject);
3031 else if(act->type==ACT_2DFILTER){
3032 bTwoDFilterActuator *_2dfa = act->data;
3033 _2dfa->text= newlibadr(fd, ob->id.lib, _2dfa->text);
3035 else if(act->type==ACT_PARENT) {
3036 bParentActuator *parenta = act->data;
3037 parenta->ob = newlibadr(fd, ob->id.lib, parenta->ob);
3039 else if(act->type==ACT_STATE) {
3040 /* bStateActuator *statea = act->data; */
3045 if(ob->fluidsimSettings) {
3046 ob->fluidsimSettings->ipo = newlibadr_us(fd, ob->id.lib, ob->fluidsimSettings->ipo);
3052 ob->pd->tex=newlibadr_us(fd, ob->id.lib, ob->pd->tex);
3054 lib_link_scriptlink(fd, &ob->id, &ob->scriptlink);
3055 lib_link_particlesystems(fd, &ob->id, &ob->particlesystem);
3056 lib_link_modifiers(fd, ob);
3061 if(warn) error("WARNING IN CONSOLE");
3065 static void direct_link_pose(FileData *fd, bPose *pose)
3067 bPoseChannel *pchan;
3072 link_list(fd, &pose->chanbase);
3073 link_list(fd, &pose->agroups);
3075 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
3077 pchan->parent= newdataadr(fd, pchan->parent);
3078 pchan->child= newdataadr(fd, pchan->child);
3079 direct_link_constraints(fd, &pchan->constraints);
3080 pchan->iktree.first= pchan->iktree.last= NULL;
3085 static void direct_link_modifiers(FileData *fd, ListBase *lb)
3091 for (md=lb->first; md; md=md->next) {
3094 /* if modifiers disappear, or for upward compatibility */
3095 if(NULL==modifierType_getInfo(md->type))
3096 md->type= eModifierType_None;
3098 if (md->type==eModifierType_Subsurf) {
3099 SubsurfModifierData *smd = (SubsurfModifierData*) md;
3101 smd->emCache = smd->mCache = 0;
3103 else if (md->type==eModifierType_Cloth) {
3104 ClothModifierData *clmd = (ClothModifierData*) md;
3106 clmd->clothObject = NULL;
3108 clmd->sim_parms= newdataadr(fd, clmd->sim_parms);
3109 clmd->coll_parms= newdataadr(fd, clmd->coll_parms);
3110 clmd->point_cache= newdataadr(fd, clmd->point_cache);
3112 if(clmd->point_cache)
3113 direct_link_pointcache(fd, clmd->point_cache);
3115 if(clmd->sim_parms) {
3116 if(clmd->sim_parms->presets > 10)
3117 clmd->sim_parms->presets = 0;
3121 else if (md->type==eModifierType_Collision) {
3123 CollisionModifierData *collmd = (CollisionModifierData*) md;
3125 // TODO: CollisionModifier should use pointcache
3126 // + have proper reset events before enabling this
3127 collmd->x = newdataadr(fd, collmd->x);
3128 collmd->xnew = newdataadr(fd, collmd->xnew);
3129 collmd->mfaces = newdataadr(fd, collmd->mfaces);
3131 collmd->current_x = MEM_callocN(sizeof(MVert)*collmd->numverts,"current_x");
3132 collmd->current_xnew = MEM_callocN(sizeof(MVert)*collmd->numverts,"current_xnew");
3133 collmd->current_v = MEM_callocN(sizeof(MVert)*collmd->numverts,"current_v");
3137 collmd->xnew = NULL;
3138 collmd->current_x = NULL;
3139 collmd->current_xnew = NULL;
3140 collmd->current_v = NULL;
3142 collmd->numverts = 0;
3143 collmd->bvhtree = NULL;
3144 collmd->mfaces = NULL;
3147 else if (md->type==eModifierType_Hook) {
3148 HookModifierData *hmd = (HookModifierData*) md;
3150 hmd->indexar= newdataadr(fd, hmd->indexar);
3151 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
3153 for(a=0; a<hmd->totindex; a++) {
3154 SWITCH_INT(hmd->indexar[a]);
3157 } else if (md->type==eModifierType_ParticleSystem) {
3158 ParticleSystemModifierData *psmd = (ParticleSystemModifierData*) md;
3161 psmd->psys=newdataadr(fd, psmd->psys);
3162 psmd->flag &= ~eParticleSystemFlag_psys_updated;
3163 } else if (md->type==eModifierType_Explode) {
3164 ExplodeModifierData *psmd = (ExplodeModifierData*) md;
3168 else if (md->type==eModifierType_MeshDeform) {
3169 MeshDeformModifierData *mmd = (MeshDeformModifierData*) md;
3171 mmd->bindweights= newdataadr(fd, mmd->bindweights);
3172 mmd->bindcos= newdataadr(fd, mmd->bindcos);
3173 mmd->dyngrid= newdataadr(fd, mmd->dyngrid);
3174 mmd->dyninfluences= newdataadr(fd, mmd->dyninfluences);
3175 mmd->dynverts= newdataadr(fd, mmd->dynverts);
3177 if(fd->flags & FD_FLAGS_SWITCH_ENDIAN) {
3180 if(mmd->bindweights)
3181 for(a=0; a<mmd->totcagevert*mmd->totvert; a++)
3182 SWITCH_INT(mmd->bindweights[a])
3184 for(a=0; a<mmd->totcagevert*3; a++)
3185 SWITCH_INT(mmd->bindcos[a])
3187 for(a=0; a<mmd->totvert; a++)
3188 SWITCH_INT(mmd->dynverts[a])
3194 static void direct_link_nlastrips(FileData *fd, ListBase *strips)
3196 bActionStrip *strip;
3198 link_list(fd, strips);
3200 for(strip= strips->first; strip; strip= strip->next)
3201 link_list(fd, &strip->modifiers);
3204 static void direct_link_object(FileData *fd, Object *ob)
3213 /* weak weak... this was only meant as draw flag, now is used in give_base too */
3214 ob->flag &= ~OB_FROMGROUP;
3216 ob->disp.first=ob->disp.last= NULL;
3218 ob->pose= newdataadr(fd, ob->pose);
3219 direct_link_pose(fd, ob->pose);
3221 link_list(fd, &ob->defbase);
3222 direct_link_nlastrips(fd, &ob->nlastrips);
3223 link_list(fd, &ob->constraintChannels);
3225 direct_link_scriptlink(fd, &ob->scriptlink);
3227 ob->mat= newdataadr(fd, ob->mat);