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 *****
28 * .blend file reading entry point
40 #include "MEM_guardedalloc.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_ghash.h"
44 #include "BLI_linklist.h"
46 #include "DNA_sdna_types.h"
47 #include "DNA_space_types.h"
48 #include "DNA_userdef_types.h"
50 #include "DNA_material_types.h"
52 #include "BKE_utildefines.h" // for ENDB
55 #include "BKE_global.h"
56 #include "BKE_library.h" // for free_main
58 #include "BLO_readfile.h"
59 #include "BLO_undofile.h"
64 #include "BLO_readblenfile.h"
66 #include "BLO_sys_types.h" // needed for intptr_t
69 * IDType stuff, I plan to move this
70 * out into its own file + prefix, and
71 * make sure all IDType handling goes through
80 #define IDTYPE_FLAGS_ISLINKABLE (1<<0)
83 static IDType idtypes[]= {
84 { ID_AC, "Action", IDTYPE_FLAGS_ISLINKABLE},
85 { ID_AR, "Armature", IDTYPE_FLAGS_ISLINKABLE},
86 { ID_BR, "Brush", IDTYPE_FLAGS_ISLINKABLE},
87 { ID_CA, "Camera", IDTYPE_FLAGS_ISLINKABLE},
88 { ID_CU, "Curve", IDTYPE_FLAGS_ISLINKABLE},
89 { ID_GR, "Group", IDTYPE_FLAGS_ISLINKABLE},
91 { ID_IM, "Image", IDTYPE_FLAGS_ISLINKABLE},
92 { ID_IP, "Ipo", IDTYPE_FLAGS_ISLINKABLE},
94 { ID_LA, "Lamp", IDTYPE_FLAGS_ISLINKABLE},
96 { ID_LI, "Library", 0},
97 { ID_LT, "Lattice", IDTYPE_FLAGS_ISLINKABLE},
98 { ID_MA, "Material", IDTYPE_FLAGS_ISLINKABLE},
99 { ID_MB, "Metaball", IDTYPE_FLAGS_ISLINKABLE},
100 { ID_ME, "Mesh", IDTYPE_FLAGS_ISLINKABLE},
101 { ID_NT, "NodeTree", IDTYPE_FLAGS_ISLINKABLE},
102 { ID_OB, "Object", IDTYPE_FLAGS_ISLINKABLE},
103 { ID_SCE, "Scene", IDTYPE_FLAGS_ISLINKABLE},
104 { ID_SCR, "Screen", 0},
105 { ID_SEQ, "Sequence", 0},
106 { ID_SE, "Sector", 0},
107 { ID_SO, "Sound", IDTYPE_FLAGS_ISLINKABLE},
108 { ID_TE, "Texture", IDTYPE_FLAGS_ISLINKABLE},
109 { ID_TXT, "Text", IDTYPE_FLAGS_ISLINKABLE},
110 { ID_VF, "VFont", IDTYPE_FLAGS_ISLINKABLE},
111 { ID_WO, "World", IDTYPE_FLAGS_ISLINKABLE},
114 static int nidtypes= sizeof(idtypes)/sizeof(idtypes[0]);
116 /* local prototypes --------------------- */
117 void BLO_blendhandle_print_sizes(BlendHandle *, void *);
120 static IDType *idtype_from_name(char *str)
125 if (BLI_streq(str, idtypes[i].name))
130 static IDType *idtype_from_code(int code)
135 if (code==idtypes[i].code)
141 static int bheadcode_is_idcode(int code)
143 return idtype_from_code(code)?1:0;
146 static int idcode_is_linkable(int code) {
147 IDType *idt= idtype_from_code(code);
148 return idt?(idt->flags&IDTYPE_FLAGS_ISLINKABLE):0;
151 char *BLO_idcode_to_name(int code)
153 IDType *idt= idtype_from_code(code);
155 return idt?idt->name:NULL;
158 int BLO_idcode_from_name(char *name)
160 IDType *idt= idtype_from_name(name);
162 return idt?idt->code:0;
165 /* Access routines used by filesel. */
167 BlendHandle *BLO_blendhandle_from_file(char *file)
171 return (BlendHandle*) blo_openblenderfile(file, &err);
174 void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp)
176 FileData *fd= (FileData*) bh;
180 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
181 if (bhead->code==ENDB)
184 short *sp= fd->filesdna->structs[bhead->SDNAnr];
185 char *name= fd->filesdna->types[ sp[0] ];
188 buf[0]= (bhead->code>>24)&0xFF;
189 buf[1]= (bhead->code>>16)&0xFF;
190 buf[2]= (bhead->code>>8)&0xFF;
191 buf[3]= (bhead->code>>0)&0xFF;
193 buf[0]= buf[0]?buf[0]:' ';
194 buf[1]= buf[1]?buf[1]:' ';
195 buf[2]= buf[2]?buf[2]:' ';
196 buf[3]= buf[3]?buf[3]:' ';
198 fprintf(fp, "['%.4s', '%s', %d, %ld ], \n", buf, name, bhead->nr, (intptr_t)bhead->len+sizeof(BHead));
204 LinkNode *BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype)
206 FileData *fd= (FileData*) bh;
207 LinkNode *names= NULL;
210 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
211 if (bhead->code==ofblocktype) {
212 char *idname= bhead_id_name(fd, bhead);
214 BLI_linklist_prepend(&names, strdup(idname+2));
215 } else if (bhead->code==ENDB)
222 LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype)
224 FileData *fd= (FileData*) bh;
225 LinkNode *previews= NULL;
229 PreviewImage* prv = NULL;
230 PreviewImage* new_prv = NULL;
232 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
233 if (bhead->code==ofblocktype) {
234 ID *id= (ID*) (bhead+1);
235 if ( (GS(id->name) == ID_MA) || (GS(id->name) == ID_TE)) {
236 new_prv = MEM_callocN(sizeof(PreviewImage), "newpreview");
237 BLI_linklist_prepend(&previews, new_prv);
240 } else if (bhead->code==DATA) {
242 if (bhead->SDNAnr == dna_findstruct_nr(fd->filesdna, "PreviewImage") ) {
243 prv = (PreviewImage*) (bhead+1);
245 memcpy(new_prv, prv, sizeof(PreviewImage));
247 unsigned int *rect = NULL;
249 new_prv->rect[0] = MEM_callocN(new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int), "prvrect");
250 bhead= blo_nextbhead(fd, bhead);
251 rect = (unsigned int*)(bhead+1);
252 rectlen = new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int);
253 memcpy(new_prv->rect[0], rect, bhead->len);
255 new_prv->rect[0] = NULL;
259 unsigned int *rect = NULL;
261 new_prv->rect[1] = MEM_callocN(new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int), "prvrect");
262 bhead= blo_nextbhead(fd, bhead);
263 rect = (unsigned int*)(bhead+1);
264 rectlen = new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int);
265 memcpy(new_prv->rect[1], rect, bhead->len);
267 new_prv->rect[1] = NULL;
271 } else if (bhead->code==ENDB) {
273 } else if (bhead->code==DATA) {
274 /* DATA blocks between IDBlock and Preview */
286 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh)
288 FileData *fd= (FileData*) bh;
289 GHash *gathered= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
290 LinkNode *names= NULL;
293 for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
294 if (bhead->code==ENDB) {
296 } else if (bheadcode_is_idcode(bhead->code)) {
297 if (idcode_is_linkable(bhead->code)) {
298 char *str= BLO_idcode_to_name(bhead->code);
300 if (!BLI_ghash_haskey(gathered, str)) {
301 BLI_linklist_prepend(&names, strdup(str));
302 BLI_ghash_insert(gathered, str, NULL);
308 BLI_ghash_free(gathered, NULL, NULL);
313 void BLO_blendhandle_close(BlendHandle *bh) {
314 FileData *fd= (FileData*) bh;
316 blo_freefiledata(fd);
321 BlendFileData *BLO_read_from_file(char *file, BlendReadError *error_r)
323 BlendFileData *bfd = NULL;
326 fd = blo_openblenderfile(file, error_r);
328 bfd= blo_read_file_internal(fd, error_r);
330 bfd->type= BLENFILETYPE_BLEND;
331 strncpy(bfd->main->name, file, sizeof(bfd->main->name)-1);
333 blo_freefiledata(fd);
339 BlendFileData *BLO_read_from_memory(void *mem, int memsize, BlendReadError *error_r)
341 BlendFileData *bfd = NULL;
344 fd = blo_openblendermemory(mem, memsize, error_r);
346 bfd= blo_read_file_internal(fd, error_r);
348 bfd->type= BLENFILETYPE_BLEND;
349 strcpy(bfd->main->name, "");
351 blo_freefiledata(fd);
357 BlendFileData *BLO_read_from_memfile(const char *filename, MemFile *memfile, BlendReadError *error_r)
359 BlendFileData *bfd = NULL;
363 fd = blo_openblendermemfile(memfile, error_r);
365 strcpy(fd->filename, filename);
367 /* separate libraries from G.main */
368 blo_split_main(&mainlist, G.main);
369 /* add the library pointers in oldmap lookup */
370 blo_add_library_pointer_map(&mainlist, fd);
372 /* makes lookup of existing images in G.main */
373 blo_make_image_pointer_map(fd);
375 bfd= blo_read_file_internal(fd, error_r);
377 bfd->type= BLENFILETYPE_BLEND;
378 strcpy(bfd->main->name, "");
381 /* ensures relinked images are not freed */
382 blo_end_image_pointer_map(fd);
384 /* move libraries from G.main to new main */
385 if(bfd && mainlist.first!=mainlist.last) {
387 /* Library structs themselves */
388 bfd->main->library= G.main->library;
389 G.main->library.first= G.main->library.last= NULL;
391 /* add the Library mainlist to the new main */
392 BLI_remlink(&mainlist, G.main);
393 BLI_addhead(&mainlist, bfd->main);
395 blo_join_main(&mainlist);
397 blo_freefiledata(fd);
403 void BLO_blendfiledata_free(BlendFileData *bfd)
406 free_main(bfd->main);
410 MEM_freeN(bfd->user);
416 char *BLO_bre_as_string(BlendReadError error)
422 case BRE_UNABLE_TO_OPEN:
423 return "Unable to open";
424 case BRE_UNABLE_TO_READ:
425 return "Unable to read";
427 case BRE_OUT_OF_MEMORY:
428 return "Out of memory";
429 case BRE_INTERNAL_ERROR:
430 return "<internal error>";
432 case BRE_NOT_A_BLEND:
433 return "File is not a Blender file";
434 case BRE_NOT_A_PUBFILE:
435 return "File is not a compressed, locked or signed Blender file";
437 return "File incomplete";
439 return "File corrupt";
442 return "File needs newer Blender version, please upgrade";
443 case BRE_NOT_ALLOWED:
444 return "File is locked";
447 return "File has no screen";
449 return "File has no scene";
453 return "<invalid read error>";