Merge with -r 22620:23107.
[blender.git] / source / blender / blenkernel / intern / library.c
1 /** 
2  * $Id$
3  * 
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
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.
15  *
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.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /*
31  *  Contains management of ID's and libraries
32  *  allocate and free of all library data
33  * 
34  */
35
36
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <string.h>
40 #include <stdlib.h>
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #include "MEM_guardedalloc.h"
47
48 /* all types are needed here, in order to do memory operations */
49 #include "DNA_ID.h"
50 #include "DNA_listBase.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_object_types.h"
53 #include "DNA_mesh_types.h"
54 #include "DNA_lattice_types.h"
55 #include "DNA_curve_types.h"
56 #include "DNA_meta_types.h"
57 #include "DNA_material_types.h"
58 #include "DNA_texture_types.h"
59 #include "DNA_image_types.h"
60 #include "DNA_wave_types.h"
61 #include "DNA_lamp_types.h"
62 #include "DNA_camera_types.h"
63 #include "DNA_ipo_types.h"
64 #include "DNA_key_types.h"
65 #include "DNA_world_types.h"
66 #include "DNA_screen_types.h"
67 #include "DNA_vfont_types.h"
68 #include "DNA_text_types.h"
69 #include "DNA_sound_types.h"
70 #include "DNA_group_types.h"
71 #include "DNA_armature_types.h"
72 #include "DNA_action_types.h"
73 #include "DNA_userdef_types.h"
74 #include "DNA_node_types.h"
75 #include "DNA_nla_types.h"
76 #include "DNA_effect_types.h"
77 #include "DNA_brush_types.h"
78 #include "DNA_particle_types.h"
79 #include "DNA_space_types.h"
80 #include "DNA_windowmanager_types.h"
81 #include "DNA_anim_types.h"
82 #include "DNA_gpencil_types.h"
83
84 #include "BLI_blenlib.h"
85 #include "BLI_dynstr.h"
86
87 #include "BKE_animsys.h"
88 #include "BKE_context.h"
89 #include "BKE_library.h"
90 #include "BKE_main.h"
91 #include "BKE_global.h"
92 #include "BKE_sound.h"
93 #include "BKE_object.h"
94 #include "BKE_screen.h"
95 #include "BKE_script.h"
96 #include "BKE_mesh.h"
97 #include "BKE_material.h"
98 #include "BKE_curve.h"
99 #include "BKE_mball.h"
100 #include "BKE_text.h"
101 #include "BKE_texture.h"
102 #include "BKE_scene.h"
103 #include "BKE_icons.h"
104 #include "BKE_image.h"
105 #include "BKE_ipo.h"
106 #include "BKE_key.h"
107 #include "BKE_world.h"
108 #include "BKE_font.h"
109 #include "BKE_group.h"
110 #include "BKE_lattice.h"
111 #include "BKE_armature.h"
112 #include "BKE_action.h"
113 #include "BKE_node.h"
114 #include "BKE_effect.h"
115 #include "BKE_brush.h"
116 #include "BKE_idprop.h"
117 #include "BKE_particle.h"
118 #include "BKE_gpencil.h"
119
120 #define MAX_IDPUP               60      /* was 24 */
121
122 /* GS reads the memory pointed at in a specific ordering. 
123    only use this definition, makes little and big endian systems
124    work fine, in conjunction with MAKE_ID */
125
126 /* from blendef: */
127 #define GS(a)   (*((short *)(a)))
128
129 /* ************* general ************************ */
130
131 void id_lib_extern(ID *id)
132 {
133         if(id) {
134                 if(id->flag & LIB_INDIRECT) {
135                         id->flag -= LIB_INDIRECT;
136                         id->flag |= LIB_EXTERN;
137                 }
138         }
139 }
140
141 void id_us_plus(ID *id)
142 {
143         if(id) {
144                 id->us++;
145                 if(id->flag & LIB_INDIRECT) {
146                         id->flag -= LIB_INDIRECT;
147                         id->flag |= LIB_EXTERN;
148                 }
149         }
150 }
151
152 void id_us_min(ID *id)
153 {
154         if(id)
155                 id->us--;
156 }
157
158 int id_make_local(ID *id, int test)
159 {
160         if(id->flag & LIB_INDIRECT)
161                 return 0;
162
163         switch(GS(id->name)) {
164                 case ID_SCE:
165                         return 0; /* not implemented */
166                 case ID_LI:
167                         return 0; /* can't be linked */
168                 case ID_OB:
169                         if(!test) make_local_object((Object*)id);
170                         return 1;
171                 case ID_ME:
172                         if(!test) {
173                                 make_local_mesh((Mesh*)id);
174                                 make_local_key(((Mesh*)id)->key);
175                         }
176                         return 1;
177                 case ID_CU:
178                         if(!test) {
179                                 make_local_curve((Curve*)id);
180                                 make_local_key(((Curve*)id)->key);
181                         }
182                         return 1;
183                 case ID_MB:
184                         if(!test) make_local_mball((MetaBall*)id);
185                         return 1;
186                 case ID_MA:
187                         if(!test) make_local_material((Material*)id);
188                         return 1;
189                 case ID_TE:
190                         if(!test) make_local_texture((Tex*)id);
191                         return 1;
192                 case ID_IM:
193                         return 0; /* not implemented */
194                 case ID_WV:
195                         return 0; /* deprecated */
196                 case ID_LT:
197                         if(!test) {
198                                 make_local_lattice((Lattice*)id);
199                                 make_local_key(((Lattice*)id)->key);
200                         }
201                         return 1;
202                 case ID_LA:
203                         if(!test) make_local_lamp((Lamp*)id);
204                         return 1;
205                 case ID_CA:
206                         if(!test) make_local_camera((Camera*)id);
207                         return 1;
208                 case ID_IP:
209                         return 0; /* deprecated */
210                 case ID_KE:
211                         if(!test) make_local_key((Key*)id);
212                         return 1;
213                 case ID_WO:
214                         if(!test) make_local_world((World*)id);
215                         return 1;
216                 case ID_SCR:
217                         return 0; /* can't be linked */
218                 case ID_VF:
219                         return 0; /* not implemented */
220                 case ID_TXT:
221                         return 0; /* not implemented */
222                 case ID_SCRIPT:
223                         return 0; /* deprecated */
224                 case ID_SO:
225                         return 0; /* not implemented */
226                 case ID_GR:
227                         return 0; /* not implemented */
228                 case ID_AR:
229                         if(!test) make_local_armature((bArmature*)id);
230                         return 1;
231                 case ID_AC:
232                         if(!test) make_local_action((bAction*)id);
233                         return 1;
234                 case ID_NT:
235                         return 0; /* not implemented */
236                 case ID_BR:
237                         if(!test) make_local_brush((Brush*)id);
238                         return 1;
239                 case ID_PA:
240                         if(!test) make_local_particlesettings((ParticleSettings*)id);
241                         return 1;
242                 case ID_WM:
243                         return 0; /* can't be linked */
244                 case ID_GD:
245                         return 0; /* not implemented */
246         }
247
248         return 0;
249 }
250
251 int id_copy(ID *id, ID **newid, int test)
252 {
253         if(!test) *newid= NULL;
254
255         /* conventions:
256          * - make shallow copy, only this ID block
257          * - id.us of the new ID is set to 1 */
258         switch(GS(id->name)) {
259                 case ID_SCE:
260                         return 0; /* can't be copied from here */
261                 case ID_LI:
262                         return 0; /* can't be copied from here */
263                 case ID_OB:
264                         if(!test) *newid= (ID*)copy_object((Object*)id);
265                         return 1;
266                 case ID_ME:
267                         if(!test) *newid= (ID*)copy_mesh((Mesh*)id);
268                         return 1;
269                 case ID_CU:
270                         if(!test) *newid= (ID*)copy_curve((Curve*)id);
271                         return 1;
272                 case ID_MB:
273                         if(!test) *newid= (ID*)copy_mball((MetaBall*)id);
274                         return 1;
275                 case ID_MA:
276                         if(!test) *newid= (ID*)copy_material((Material*)id);
277                         return 1;
278                 case ID_TE:
279                         if(!test) *newid= (ID*)copy_texture((Tex*)id);
280                         return 1;
281                 case ID_IM:
282                         return 0; /* not implemented */
283                 case ID_WV:
284                         return 0; /* deprecated */
285                 case ID_LT:
286                         if(!test) *newid= (ID*)copy_lattice((Lattice*)id);
287                         return 1;
288                 case ID_LA:
289                         if(!test) *newid= (ID*)copy_lamp((Lamp*)id);
290                         return 1;
291                 case ID_CA:
292                         if(!test) *newid= (ID*)copy_camera((Camera*)id);
293                         return 1;
294                 case ID_IP:
295                         return 0; /* deprecated */
296                 case ID_KE:
297                         if(!test) *newid= (ID*)copy_key((Key*)id);
298                         return 1;
299                 case ID_WO:
300                         if(!test) *newid= (ID*)copy_world((World*)id);
301                         return 1;
302                 case ID_SCR:
303                         return 0; /* can't be copied from here */
304                 case ID_VF:
305                         return 0; /* not implemented */
306                 case ID_TXT:
307                         if(!test) *newid= (ID*)copy_text((Text*)id);
308                         return 1;
309                 case ID_SCRIPT:
310                         return 0; /* deprecated */
311                 case ID_SO:
312                         return 0; /* not implemented */
313                 case ID_GR:
314                         if(!test) *newid= (ID*)copy_group((Group*)id);
315                         return 1;
316                 case ID_AR:
317                         if(!test) *newid= (ID*)copy_armature((bArmature*)id);
318                         return 1;
319                 case ID_AC:
320                         if(!test) *newid= (ID*)copy_action((bAction*)id);
321                         return 1;
322                 case ID_NT:
323                         if(!test) *newid= (ID*)ntreeCopyTree((bNodeTree*)id, 0);
324                         return 1;
325                 case ID_BR:
326                         if(!test) *newid= (ID*)copy_brush((Brush*)id);
327                         return 1;
328                 case ID_PA:
329                         if(!test) *newid= (ID*)psys_copy_settings((ParticleSettings*)id);
330                         return 1;
331                 case ID_WM:
332                         return 0; /* can't be copied from here */
333                 case ID_GD:
334                         return 0; /* not implemented */
335         }
336         
337         return 0;
338 }
339
340 int id_unlink(ID *id, int test)
341 {
342         Main *mainlib= G.main;
343         ListBase *lb;
344
345         switch(GS(id->name)) {
346                 case ID_TXT:
347                         if(test) return 1;
348                         unlink_text(mainlib, (Text*)id);
349                         break;
350                 case ID_GR:
351                         if(test) return 1;
352                         unlink_group((Group*)id);
353                         break;
354                 case ID_OB:
355                         if(test) return 1;
356                         unlink_object(NULL, (Object*)id);
357                         break;
358         }
359
360         if(id->us == 0) {
361                 if(test) return 1;
362
363                 lb= wich_libbase(mainlib, GS(id->name));
364                 free_libblock(lb, id);
365
366                 return 1;
367         }
368
369         return 0;
370 }
371
372 ListBase *wich_libbase(Main *mainlib, short type)
373 {
374         switch( type ) {
375                 case ID_SCE:
376                         return &(mainlib->scene);
377                 case ID_LI:
378                         return &(mainlib->library);
379                 case ID_OB:
380                         return &(mainlib->object);
381                 case ID_ME:
382                         return &(mainlib->mesh);
383                 case ID_CU:
384                         return &(mainlib->curve);
385                 case ID_MB:
386                         return &(mainlib->mball);
387                 case ID_MA:
388                         return &(mainlib->mat);
389                 case ID_TE:
390                         return &(mainlib->tex);
391                 case ID_IM:
392                         return &(mainlib->image);
393                 case ID_WV:
394                         return &(mainlib->wave);
395                 case ID_LT:
396                         return &(mainlib->latt);
397                 case ID_LA:
398                         return &(mainlib->lamp);
399                 case ID_CA:
400                         return &(mainlib->camera);
401                 case ID_IP:
402                         return &(mainlib->ipo);
403                 case ID_KE:
404                         return &(mainlib->key);
405                 case ID_WO:
406                         return &(mainlib->world);
407                 case ID_SCR:
408                         return &(mainlib->screen);
409                 case ID_VF:
410                         return &(mainlib->vfont);
411                 case ID_TXT:
412                         return &(mainlib->text);
413                 case ID_SCRIPT:
414                         return &(mainlib->script);
415                 case ID_SO:
416                         return &(mainlib->sound);
417                 case ID_GR:
418                         return &(mainlib->group);
419                 case ID_AR:
420                         return &(mainlib->armature);
421                 case ID_AC:
422                         return &(mainlib->action);
423                 case ID_NT:
424                         return &(mainlib->nodetree);
425                 case ID_BR:
426                         return &(mainlib->brush);
427                 case ID_PA:
428                         return &(mainlib->particle);
429                 case ID_WM:
430                         return &(mainlib->wm);
431                 case ID_GD:
432                         return &(mainlib->gpencil);
433         }
434         return 0;
435 }
436
437 /* Flag all ids in listbase */
438 void flag_listbase_ids(ListBase *lb, short flag, short value)
439 {
440         ID *id;
441         if (value) {
442                 for(id= lb->first; id; id= id->next) id->flag |= flag;
443         } else {
444                 flag = ~flag;
445                 for(id= lb->first; id; id= id->next) id->flag &= flag;
446         }
447 }
448
449 /* Flag all ids in listbase */
450 void flag_all_listbases_ids(short flag, short value)
451 {
452         ListBase *lbarray[MAX_LIBARRAY];
453         int a;
454         a= set_listbasepointers(G.main, lbarray);
455         while(a--)      flag_listbase_ids(lbarray[a], flag, value);
456 }
457
458
459 /* note: MAX_LIBARRAY define should match this code */
460 int set_listbasepointers(Main *main, ListBase **lb)
461 {
462         int a = 0;
463
464         /* BACKWARDS! also watch order of free-ing! (mesh<->mat) */
465
466         lb[a++]= &(main->ipo);
467         lb[a++]= &(main->action); // xxx moved here to avoid problems when freeing with animato (aligorith)
468         lb[a++]= &(main->key);
469         lb[a++]= &(main->nodetree);
470         lb[a++]= &(main->image);
471         lb[a++]= &(main->tex);
472         lb[a++]= &(main->mat);
473         lb[a++]= &(main->vfont);
474         
475         /* Important!: When adding a new object type,
476          * the specific data should be inserted here 
477          */
478
479         lb[a++]= &(main->armature);
480
481         lb[a++]= &(main->mesh);
482         lb[a++]= &(main->curve);
483         lb[a++]= &(main->mball);
484
485         lb[a++]= &(main->wave);
486         lb[a++]= &(main->latt);
487         lb[a++]= &(main->lamp);
488         lb[a++]= &(main->camera);
489         
490         lb[a++]= &(main->text);
491         lb[a++]= &(main->sound);
492         lb[a++]= &(main->group);
493         lb[a++]= &(main->brush);
494         lb[a++]= &(main->script);
495         lb[a++]= &(main->particle);
496         
497         lb[a++]= &(main->world);
498         lb[a++]= &(main->screen);
499         lb[a++]= &(main->object);
500         lb[a++]= &(main->scene);
501         lb[a++]= &(main->library);
502         lb[a++]= &(main->wm);
503         lb[a++]= &(main->gpencil);
504         
505         lb[a]= NULL;
506
507         return a;
508 }
509
510 /* *********** ALLOC AND FREE *****************
511   
512 free_libblock(ListBase *lb, ID *id )
513         provide a list-basis and datablock, but only ID is read
514
515 void *alloc_libblock(ListBase *lb, type, name)
516         inserts in list and returns a new ID
517
518  ***************************** */
519
520 static ID *alloc_libblock_notest(short type)
521 {
522         ID *id= NULL;
523         
524         switch( type ) {
525                 case ID_SCE:
526                         id= MEM_callocN(sizeof(Scene), "scene");
527                         break;
528                 case ID_LI:
529                         id= MEM_callocN(sizeof(Library), "library");
530                         break;
531                 case ID_OB:
532                         id= MEM_callocN(sizeof(Object), "object");
533                         break;
534                 case ID_ME:
535                         id= MEM_callocN(sizeof(Mesh), "mesh");
536                         break;
537                 case ID_CU:
538                         id= MEM_callocN(sizeof(Curve), "curve");
539                         break;
540                 case ID_MB:
541                         id= MEM_callocN(sizeof(MetaBall), "mball");
542                         break;
543                 case ID_MA:
544                         id= MEM_callocN(sizeof(Material), "mat");
545                         break;
546                 case ID_TE:
547                         id= MEM_callocN(sizeof(Tex), "tex");
548                         break;
549                 case ID_IM:
550                         id= MEM_callocN(sizeof(Image), "image");
551                         break;
552                 case ID_WV:
553                         id= MEM_callocN(sizeof(Wave), "wave");
554                         break;
555                 case ID_LT:
556                         id= MEM_callocN(sizeof(Lattice), "latt");
557                         break;
558                 case ID_LA:
559                         id= MEM_callocN(sizeof(Lamp), "lamp");
560                         break;
561                 case ID_CA:
562                         id= MEM_callocN(sizeof(Camera), "camera");
563                         break;
564                 case ID_IP:
565                         id= MEM_callocN(sizeof(Ipo), "ipo");
566                         break;
567                 case ID_KE:
568                         id= MEM_callocN(sizeof(Key), "key");
569                         break;
570                 case ID_WO:
571                         id= MEM_callocN(sizeof(World), "world");
572                         break;
573                 case ID_SCR:
574                         id= MEM_callocN(sizeof(bScreen), "screen");
575                         break;
576                 case ID_VF:
577                         id= MEM_callocN(sizeof(VFont), "vfont");
578                         break;
579                 case ID_TXT:
580                         id= MEM_callocN(sizeof(Text), "text");
581                         break;
582                 case ID_SCRIPT:
583                         //XXX id= MEM_callocN(sizeof(Script), "script");
584                         break;
585                 case ID_SO:
586                         id= MEM_callocN(sizeof(bSound), "sound");
587                         break;
588                 case ID_GR:
589                         id= MEM_callocN(sizeof(Group), "group");
590                         break;
591                 case ID_AR:
592                         id = MEM_callocN(sizeof(bArmature), "armature");
593                         break;
594                 case ID_AC:
595                         id = MEM_callocN(sizeof(bAction), "action");
596                         break;
597                 case ID_NT:
598                         id = MEM_callocN(sizeof(bNodeTree), "nodetree");
599                         break;
600                 case ID_BR:
601                         id = MEM_callocN(sizeof(Brush), "brush");
602                         break;
603                 case ID_PA:
604                         id = MEM_callocN(sizeof(ParticleSettings), "ParticleSettings");
605                         break;
606                 case ID_WM:
607                         id = MEM_callocN(sizeof(wmWindowManager), "Window manager");
608                         break;
609                 case ID_GD:
610                         id = MEM_callocN(sizeof(bGPdata), "Grease Pencil");
611                         break;
612         }
613         return id;
614 }
615
616 /* used everywhere in blenkernel and text.c */
617 void *alloc_libblock(ListBase *lb, short type, const char *name)
618 {
619         ID *id= NULL;
620         
621         id= alloc_libblock_notest(type);
622         if(id) {
623                 BLI_addtail(lb, id);
624                 id->us= 1;
625                 id->icon_id = 0;
626                 *( (short *)id->name )= type;
627                 new_id(lb, id, name);
628                 /* alphabetic insterion: is in new_id */
629         }
630         return id;
631 }
632
633 /* by spec, animdata is first item after ID */
634 /* we still read ->adt itself, to ensure compiler warns when it doesnt exist */
635 static void id_copy_animdata(ID *id)
636 {
637         switch(GS(id->name)) {
638                 case ID_OB:
639                         ((Object *)id)->adt= BKE_copy_animdata(((Object *)id)->adt);
640                         break;
641                 case ID_CU:
642                         ((Curve *)id)->adt= BKE_copy_animdata(((Curve *)id)->adt);
643                         break;
644                 case ID_CA:
645                         ((Camera *)id)->adt= BKE_copy_animdata(((Camera *)id)->adt);
646                         break;
647                 case ID_KE:
648                         ((Key *)id)->adt= BKE_copy_animdata(((Key *)id)->adt);
649                         break;
650                 case ID_LA:
651                         ((Lamp *)id)->adt= BKE_copy_animdata(((Lamp *)id)->adt);
652                         break;
653                 case ID_MA:
654                         ((Material *)id)->adt= BKE_copy_animdata(((Material *)id)->adt);
655                         break;
656                 case ID_NT:
657                         ((bNodeTree *)id)->adt= BKE_copy_animdata(((bNodeTree *)id)->adt);
658                         break;
659                 case ID_SCE:
660                         ((Scene *)id)->adt= BKE_copy_animdata(((Scene *)id)->adt);
661                         break;
662                 case ID_TE:
663                         ((Tex *)id)->adt= BKE_copy_animdata(((Tex *)id)->adt);
664                         break;
665                 case ID_WO:
666                         ((World *)id)->adt= BKE_copy_animdata(((World *)id)->adt);
667                         break;
668         }
669         
670 }
671
672 /* used everywhere in blenkernel and text.c */
673 void *copy_libblock(void *rt)
674 {
675         ID *idn, *id;
676         ListBase *lb;
677         char *cp, *cpn;
678         int idn_len;
679         
680         id= rt;
681
682         lb= wich_libbase(G.main, GS(id->name));
683         idn= alloc_libblock(lb, GS(id->name), id->name+2);
684         
685         if(idn==NULL) {
686                 printf("ERROR: Illegal ID name for %s (Crashing now)\n", id->name);
687         }
688         
689         idn_len= MEM_allocN_len(idn);
690         if(idn_len - sizeof(ID) > 0) {
691                 cp= (char *)id;
692                 cpn= (char *)idn;
693                 memcpy(cpn+sizeof(ID), cp+sizeof(ID), idn_len - sizeof(ID));
694         }
695         
696         id->newid= idn;
697         idn->flag |= LIB_NEW;
698         if (id->properties) idn->properties = IDP_CopyProperty(id->properties);
699
700         id_copy_animdata(id);
701         
702         return idn;
703 }
704
705 static void free_library(Library *lib)
706 {
707     /* no freeing needed for libraries yet */
708 }
709
710 static void (*free_windowmanager_cb)(bContext *, wmWindowManager *)= NULL;
711
712 void set_free_windowmanager_cb(void (*func)(bContext *C, wmWindowManager *) )
713 {
714         free_windowmanager_cb= func;
715 }
716
717 /* used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c */
718 void free_libblock(ListBase *lb, void *idv)
719 {
720         ID *id= idv;
721         
722         switch( GS(id->name) ) {        /* GetShort from util.h */
723                 case ID_SCE:
724                         free_scene((Scene *)id);
725                         break;
726                 case ID_LI:
727                         free_library((Library *)id);
728                         break;
729                 case ID_OB:
730                         free_object((Object *)id);
731                         break;
732                 case ID_ME:
733                         free_mesh((Mesh *)id);
734                         break;
735                 case ID_CU:
736                         free_curve((Curve *)id);
737                         break;
738                 case ID_MB:
739                         free_mball((MetaBall *)id);
740                         break;
741                 case ID_MA:
742                         free_material((Material *)id);
743                         break;
744                 case ID_TE:
745                         free_texture((Tex *)id);
746                         break;
747                 case ID_IM:
748                         free_image((Image *)id);
749                         break;
750                 case ID_WV:
751                         /* free_wave(id); */
752                         break;
753                 case ID_LT:
754                         free_lattice((Lattice *)id);
755                         break;
756                 case ID_LA:
757                         free_lamp((Lamp *)id);
758                         break;
759                 case ID_CA:
760                         free_camera((Camera*) id);
761                         break;
762                 case ID_IP:
763                         free_ipo((Ipo *)id);
764                         break;
765                 case ID_KE:
766                         free_key((Key *)id);
767                         break;
768                 case ID_WO:
769                         free_world((World *)id);
770                         break;
771                 case ID_SCR:
772                         free_screen((bScreen *)id);
773                         break;
774                 case ID_VF:
775                         free_vfont((VFont *)id);
776                         break;
777                 case ID_TXT:
778                         free_text((Text *)id);
779                         break;
780                 case ID_SCRIPT:
781                         //XXX free_script((Script *)id);
782                         break;
783                 case ID_SO:
784                         sound_free((bSound*)id);
785                         break;
786                 case ID_GR:
787                         free_group((Group *)id);
788                         break;
789                 case ID_AR:
790                         free_armature((bArmature *)id);
791                         break;
792                 case ID_AC:
793                         free_action((bAction *)id);
794                         break;
795                 case ID_NT:
796                         ntreeFreeTree((bNodeTree *)id);
797                         break;
798                 case ID_BR:
799                         free_brush((Brush *)id);
800                         break;
801                 case ID_PA:
802                         psys_free_settings((ParticleSettings *)id);
803                         break;
804                 case ID_WM:
805                         if(free_windowmanager_cb)
806                                 free_windowmanager_cb(NULL, (wmWindowManager *)id);
807                         break;
808                 case ID_GD:
809                         free_gpencil_data((bGPdata *)id);
810                         break;
811         }
812
813         if (id->properties) {
814                 IDP_FreeProperty(id->properties);
815                 MEM_freeN(id->properties);
816         }
817         BLI_remlink(lb, id);
818         MEM_freeN(id);
819
820 }
821
822 void free_libblock_us(ListBase *lb, void *idv)          /* test users */
823 {
824         ID *id= idv;
825         
826         id->us--;
827
828         if(id->us<0) {
829                 if(id->lib) printf("ERROR block %s %s users %d\n", id->lib->name, id->name, id->us);
830                 else printf("ERROR block %s users %d\n", id->name, id->us);
831         }
832         if(id->us==0) {
833                 if( GS(id->name)==ID_OB ) unlink_object(NULL, (Object *)id);
834                 
835                 free_libblock(lb, id);
836         }
837 }
838
839
840 void free_main(Main *mainvar)
841 {
842         /* also call when reading a file, erase all, etc */
843         ListBase *lbarray[MAX_LIBARRAY];
844         int a;
845
846         a= set_listbasepointers(mainvar, lbarray);
847         while(a--) {
848                 ListBase *lb= lbarray[a];
849                 ID *id;
850                 
851                 while ( (id= lb->first) ) {
852                         free_libblock(lb, id);
853                 }
854         }
855
856         MEM_freeN(mainvar);
857 }
858
859 /* ***************** ID ************************ */
860
861
862 ID *find_id(char *type, char *name)             /* type: "OB" or "MA" etc */
863 {
864         ID *id;
865         ListBase *lb;
866         
867         lb= wich_libbase(G.main, GS(type));
868         
869         id= lb->first;
870         while(id) {
871                 if(id->name[2]==name[0] && strcmp(id->name+2, name)==0 ) 
872                         return id;
873                 id= id->next;
874         }
875         return 0;
876 }
877
878 static void get_flags_for_id(ID *id, char *buf) 
879 {
880         int isfake= id->flag & LIB_FAKEUSER;
881         int isnode=0;
882                 /* Writeout the flags for the entry, note there
883                  * is a small hack that writes 5 spaces instead
884                  * of 4 if no flags are displayed... this makes
885                  * things usually line up ok - better would be
886                  * to have that explicit, oh well - zr
887                  */
888
889         if(GS(id->name)==ID_MA)
890                 isnode= ((Material *)id)->use_nodes;
891         if(GS(id->name)==ID_TE)
892                 isnode= ((Tex *)id)->use_nodes;
893         
894         if (id->us<0)
895                 sprintf(buf, "-1W ");
896         else if (!id->lib && !isfake && id->us && !isnode)
897                 sprintf(buf, "     ");
898         else if(isnode)
899                 sprintf(buf, "%c%cN%c ", id->lib?'L':' ', isfake?'F':' ', (id->us==0)?'O':' ');
900         else
901                 sprintf(buf, "%c%c%c ", id->lib?'L':' ', isfake?'F':' ', (id->us==0)?'O':' ');
902 }
903
904 #define IDPUP_NO_VIEWER 1
905
906 static void IDnames_to_dyn_pupstring(DynStr *pupds, ListBase *lb, ID *link, short *nr, int hideflag)
907 {
908         int i, nids= BLI_countlist(lb);
909                 
910         if (nr) *nr= -1;
911         
912         if (nr && nids>MAX_IDPUP) {
913                 BLI_dynstr_append(pupds, "DataBrowse %x-2");
914                 *nr= -2;
915         } else {
916                 ID *id;
917                 
918                 for (i=0, id= lb->first; id; id= id->next, i++) {
919                         char buf[32];
920                         
921                         if (nr && id==link) *nr= i+1;
922
923                         if (U.uiflag & USER_HIDE_DOT && id->name[2]=='.')
924                                 continue;
925                         if (hideflag & IDPUP_NO_VIEWER)
926                                 if (GS(id->name)==ID_IM)
927                                         if ( ((Image *)id)->source==IMA_SRC_VIEWER )
928                                                 continue;
929                         
930                         get_flags_for_id(id, buf);
931                                 
932                         BLI_dynstr_append(pupds, buf);
933                         BLI_dynstr_append(pupds, id->name+2);
934                         sprintf(buf, "%%x%d", i+1);
935                         BLI_dynstr_append(pupds, buf);
936                         
937                         /* icon */
938                         switch(GS(id->name))
939                         {
940                         case ID_MA: /* fall through */
941                         case ID_TE: /* fall through */
942                         case ID_IM: /* fall through */
943                         case ID_WO: /* fall through */
944                         case ID_LA: /* fall through */
945                                 sprintf(buf, "%%i%d", BKE_icon_getid(id) );
946                                 BLI_dynstr_append(pupds, buf);
947                                 break;
948                         default:
949                                 break;
950                         }
951                         
952                         if(id->next)
953                                 BLI_dynstr_append(pupds, "|");
954                 }
955         }
956 }
957
958
959 /* used by headerbuttons.c buttons.c editobject.c editseq.c */
960 /* if nr==NULL no MAX_IDPUP, this for non-header browsing */
961 void IDnames_to_pupstring(char **str, char *title, char *extraops, ListBase *lb, ID *link, short *nr)
962 {
963         DynStr *pupds= BLI_dynstr_new();
964
965         if (title) {
966                 BLI_dynstr_append(pupds, title);
967                 BLI_dynstr_append(pupds, "%t|");
968         }
969         
970         if (extraops) {
971                 BLI_dynstr_append(pupds, extraops);
972                 if (BLI_dynstr_get_len(pupds))
973                         BLI_dynstr_append(pupds, "|");
974         }
975
976         IDnames_to_dyn_pupstring(pupds, lb, link, nr, 0);
977         
978         *str= BLI_dynstr_get_cstring(pupds);
979         BLI_dynstr_free(pupds);
980 }
981
982 /* skips viewer images */
983 void IMAnames_to_pupstring(char **str, char *title, char *extraops, ListBase *lb, ID *link, short *nr)
984 {
985         DynStr *pupds= BLI_dynstr_new();
986         
987         if (title) {
988                 BLI_dynstr_append(pupds, title);
989                 BLI_dynstr_append(pupds, "%t|");
990         }
991         
992         if (extraops) {
993                 BLI_dynstr_append(pupds, extraops);
994                 if (BLI_dynstr_get_len(pupds))
995                         BLI_dynstr_append(pupds, "|");
996         }
997         
998         IDnames_to_dyn_pupstring(pupds, lb, link, nr, IDPUP_NO_VIEWER);
999         
1000         *str= BLI_dynstr_get_cstring(pupds);
1001         BLI_dynstr_free(pupds);
1002 }
1003
1004
1005 /* used by buttons.c library.c mball.c */
1006 void splitIDname(char *name, char *left, int *nr)
1007 {
1008         int a;
1009         
1010         *nr= 0;
1011         strncpy(left, name, 21);
1012         
1013         a= strlen(name);
1014         if(a>1 && name[a-1]=='.') return;
1015         
1016         while(a--) {
1017                 if( name[a]=='.' ) {
1018                         left[a]= 0;
1019                         *nr= atol(name+a+1);
1020                         return;
1021                 }
1022                 if( isdigit(name[a])==0 ) break;
1023                 
1024                 left[a]= 0;
1025         }
1026         strcpy(left, name);     
1027 }
1028
1029 static void sort_alpha_id(ListBase *lb, ID *id)
1030 {
1031         ID *idtest;
1032         
1033         /* insert alphabetically */
1034         if(lb->first!=lb->last) {
1035                 BLI_remlink(lb, id);
1036                 
1037                 idtest= lb->first;
1038                 while(idtest) {
1039                         if(BLI_strcasecmp(idtest->name, id->name)>0 || idtest->lib) {
1040                                 BLI_insertlinkbefore(lb, idtest, id);
1041                                 break;
1042                         }
1043                         idtest= idtest->next;
1044                 }
1045                 /* as last */
1046                 if(idtest==0) {
1047                         BLI_addtail(lb, id);
1048                 }
1049         }
1050         
1051 }
1052
1053 /* 
1054  * Check to see if an ID name is already used, and find a new one if so.
1055  * Return 1 if created a new name (returned in name).
1056  *
1057  * Normally the ID that's being check is already in the ListBase, so ID *id
1058  * points at the new entry.  The Python Library module needs to know what
1059  * the name of a datablock will be before it is appended; in this case ID *id
1060  * id is NULL;
1061  */
1062
1063 int check_for_dupid(ListBase *lb, ID *id, char *name)
1064 {
1065         ID *idtest;
1066         int nr= 0, nrtest, a;
1067         const int maxtest=32;
1068         char left[32], leftest[32], in_use[32];
1069         
1070         /* make sure input name is terminated properly */
1071         if( strlen(name) > 21 ) name[21]= 0;
1072
1073         while (1) {
1074
1075                 /* phase 1: id already exists? */
1076                 for( idtest = lb->first; idtest; idtest = idtest->next ) {
1077                                 /* if idtest is not a lib */ 
1078                         if( id != idtest && idtest->lib == NULL ) {
1079                                 /* do not test alphabetic! */
1080                                 /* optimized */
1081                                 if( idtest->name[2] == name[0] ) {
1082                                         if(strcmp(name, idtest->name+2)==0) break;
1083                                 }
1084                         }
1085                 }
1086
1087                 /* if there is no double, done */
1088                 if( idtest == NULL ) return 0;
1089
1090                 /* we have a dup; need to make a new name */
1091                 /* quick check so we can reuse one of first 32 ids if vacant */
1092                 memset(in_use, 0, maxtest);
1093
1094                 /* get name portion, number portion ("name.number") */
1095                 splitIDname( name, left, &nr);
1096
1097                 /* if new name will be too long, truncate it */
1098                 if(nr>999 && strlen(left)>16) left[16]= 0;
1099                 else if(strlen(left)>17) left[17]= 0;
1100
1101                 for( idtest = lb->first; idtest; idtest = idtest->next ) {
1102                         if( id != idtest && idtest->lib == NULL ) {
1103                                 splitIDname(idtest->name+2, leftest, &nrtest);
1104                                 /* if base names match... */
1105                                 /* optimized */
1106                                 if( *left == *leftest && strcmp(left, leftest)==0 ) {
1107                                         if(nrtest < maxtest)
1108                                                 in_use[nrtest]= 1;      /* mark as used */
1109                                         if(nr <= nrtest)
1110                                                 nr= nrtest+1;           /* track largest unused */
1111                                 }
1112                         }
1113                 }
1114
1115                 /* decide which value of nr to use */
1116                 for(a=0; a<maxtest; a++) {
1117                         if(a>=nr) break;        /* stop when we've check up to biggest */
1118                         if( in_use[a]==0 ) { /* found an unused value */
1119                                 nr = a;
1120                                 break;
1121                         }
1122                 }
1123
1124                 /* if non-numbered name was not in use, reuse it */
1125                 if(nr==0) strcpy( name, left );
1126                 else {
1127                         if(nr > 999 && strlen(left) > 16) {
1128                                 /* this would overflow name buffer */
1129                                 left[16] = 0;
1130                                 strcpy( name, left );
1131                                 continue;
1132                         }
1133                         /* this format specifier is from hell... */
1134                         sprintf(name, "%s.%.3d", left, nr);
1135                 }
1136                 return 1;
1137         }
1138 }
1139
1140 /*
1141  * Only for local blocks: external en indirect blocks already have a
1142  * unique ID.
1143  *
1144  * return 1: created a new name
1145  */
1146
1147 int new_id(ListBase *lb, ID *id, const char *tname)
1148 {
1149         int result;
1150         char name[22];
1151         
1152         /* if library, don't rename */
1153         if(id->lib) return 0;
1154
1155         /* if no libdata given, look up based on ID */
1156         if(lb==NULL) lb= wich_libbase(G.main, GS(id->name));
1157
1158         if(tname==0) {  /* if no name given, use name of current ID */
1159                 strncpy(name, id->name+2, 21);
1160                 result= strlen(id->name+2);
1161         }
1162         else { /* else make a copy (tname args can be const) */
1163                 strncpy(name, tname, 21);
1164                 result= strlen(tname);
1165         }
1166
1167         /* if result > 21, strncpy don't put the final '\0' to name. */
1168         if( result >= 21 ) name[21]= 0;
1169
1170         result = check_for_dupid( lb, id, name );
1171         strcpy( id->name+2, name );
1172
1173         /* This was in 2.43 and previous releases
1174          * however all data in blender should be sorted, not just duplicate names
1175          * sorting should not hurt, but noting just incause it alters the way other
1176          * functions work, so sort every time */
1177         /* if( result )
1178                 sort_alpha_id(lb, id);*/
1179         
1180         sort_alpha_id(lb, id);
1181         
1182         return result;
1183 }
1184
1185 /* next to indirect usage in read/writefile also in editobject.c scene.c */
1186 void clear_id_newpoins()
1187 {
1188         ListBase *lbarray[MAX_LIBARRAY];
1189         ID *id;
1190         int a;
1191
1192         a= set_listbasepointers(G.main, lbarray);
1193         while(a--) {
1194                 id= lbarray[a]->first;
1195                 while(id) {
1196                         id->newid= 0;
1197                         id->flag &= ~LIB_NEW;
1198                         id= id->next;
1199                 }
1200         }
1201 }
1202
1203 /* only for library fixes */
1204 static void image_fix_relative_path(Image *ima)
1205 {
1206         if(ima->id.lib==NULL) return;
1207         if(strncmp(ima->name, "//", 2)==0) {
1208                 BLI_convertstringcode(ima->name, ima->id.lib->filename);
1209                 BLI_makestringcode(G.sce, ima->name);
1210         }
1211 }
1212
1213 #define LIBTAG(a)       if(a && a->id.lib) {a->id.flag &=~LIB_INDIRECT; a->id.flag |= LIB_EXTERN;}
1214
1215 static void lib_indirect_test_id(ID *id)
1216 {
1217         
1218         if(id->lib)
1219                 return;
1220         
1221         if(GS(id->name)==ID_OB) {               
1222                 Object *ob= (Object *)id;
1223                 bActionStrip *strip;
1224                 Mesh *me;
1225
1226                 int a;
1227         
1228                 // XXX old animation system!
1229                 for (strip=ob->nlastrips.first; strip; strip=strip->next){
1230                         LIBTAG(strip->object); 
1231                         LIBTAG(strip->act);
1232                         LIBTAG(strip->ipo);
1233                 }
1234         
1235                 for(a=0; a<ob->totcol; a++) {
1236                         LIBTAG(ob->mat[a]);
1237                 }
1238         
1239                 LIBTAG(ob->dup_group);
1240                 LIBTAG(ob->proxy);
1241                 
1242                 me= ob->data;
1243                 LIBTAG(me);
1244         }
1245 }
1246
1247 void tag_main(struct Main *mainvar, int tag)
1248 {
1249         ListBase *lbarray[MAX_LIBARRAY];
1250         ID *id;
1251         int a;
1252
1253         a= set_listbasepointers(mainvar, lbarray);
1254         while(a--) {
1255                 for(id= lbarray[a]->first; id; id= id->next) {
1256                         if(tag) id->flag |= LIB_DOIT;
1257                         else    id->flag &= ~LIB_DOIT;
1258                 }
1259         }
1260 }
1261
1262 /* if lib!=NULL, only all from lib local */
1263 void all_local(Library *lib, int untagged_only)
1264 {
1265         ListBase *lbarray[MAX_LIBARRAY], tempbase={0, 0};
1266         ID *id, *idn;
1267         int a;
1268
1269         a= set_listbasepointers(G.main, lbarray);
1270         while(a--) {
1271                 id= lbarray[a]->first;
1272                 
1273                 while(id) {
1274                         id->newid= NULL;
1275                         idn= id->next;          /* id is possibly being inserted again */
1276                         
1277                         /* The check on the second line (LIB_APPEND_TAG) is done so its
1278                          * possible to tag data you dont want to be made local, used for
1279                          * appending data, so any libdata alredy linked wont become local
1280                          * (very nasty to discover all your links are lost after appending)  
1281                          * */
1282                         if(id->flag & (LIB_EXTERN|LIB_INDIRECT|LIB_NEW) &&
1283                           (untagged_only==0 || !(id->flag & LIB_APPEND_TAG)))
1284                         {
1285                                 if(lib==NULL || id->lib==lib) {
1286                                         id->flag &= ~(LIB_EXTERN|LIB_INDIRECT|LIB_NEW);
1287
1288                                         if(id->lib) {
1289                                                 /* relative file patch */
1290                                                 if(GS(id->name)==ID_IM)
1291                                                         image_fix_relative_path((Image *)id);
1292                                                 
1293                                                 id->lib= NULL;
1294                                                 new_id(lbarray[a], id, 0);      /* new_id only does it with double names */
1295                                                 sort_alpha_id(lbarray[a], id);
1296                                         }
1297                                 }
1298                         }
1299                         id= idn;
1300                 }
1301                 
1302                 /* patch2: make it aphabetically */
1303                 while( (id=tempbase.first) ) {
1304                         BLI_remlink(&tempbase, id);
1305                         BLI_addtail(lbarray[a], id);
1306                         new_id(lbarray[a], id, 0);
1307                 }
1308         }
1309
1310         /* patch 3: make sure library data isn't indirect falsely... */
1311         a= set_listbasepointers(G.main, lbarray);
1312         while(a--) {
1313                 for(id= lbarray[a]->first; id; id=id->next)
1314                         lib_indirect_test_id(id);
1315         }
1316 }
1317
1318
1319 void test_idbutton(char *name)
1320 {
1321         /* called from buttons: when name already exists: call new_id */
1322         ListBase *lb;
1323         ID *idtest;
1324         
1325
1326         lb= wich_libbase(G.main, GS(name-2) );
1327         if(lb==0) return;
1328         
1329         /* search for id */
1330         idtest= lb->first;
1331         while(idtest) {
1332                 if( strcmp(idtest->name+2, name)==0) break;
1333                 idtest= idtest->next;
1334         }
1335
1336         if(idtest) if( new_id(lb, idtest, name)==0 ) sort_alpha_id(lb, idtest);
1337 }
1338
1339 void text_idbutton(struct ID *id, char *text)
1340 {
1341         if(id) {
1342                 if(GS(id->name)==ID_SCE)
1343                         strcpy(text, "SCE: ");
1344         else if(GS(id->name)==ID_SCE)
1345                         strcpy(text, "SCR: ");
1346         else if(GS(id->name)==ID_MA && ((Material*)id)->use_nodes)
1347                         strcpy(text, "NT: ");
1348         else {
1349                         text[0]= id->name[0];
1350                         text[1]= id->name[1];
1351                         text[2]= ':';
1352                         text[3]= ' ';
1353                         text[4]= 0;
1354         }
1355         }
1356         else
1357                 strcpy(text, "");
1358 }
1359
1360 void rename_id(ID *id, char *name)
1361 {
1362         ListBase *lb;
1363         
1364         strncpy(id->name+2, name, 21);
1365         lb= wich_libbase(G.main, GS(id->name) );
1366         
1367         new_id(lb, id, name);                           
1368 }
1369