part 1 of cleaning up my little array macro library to be a formal API. also removed...
[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 void recalc_all_library_objects(Main *main)
459 {
460         Object *ob;
461
462         /* flag for full recalc */
463         for(ob=main->object.first; ob; ob=ob->id.next)
464                 if(ob->id.lib)
465                         ob->recalc |= OB_RECALC;
466 }
467
468 /* note: MAX_LIBARRAY define should match this code */
469 int set_listbasepointers(Main *main, ListBase **lb)
470 {
471         int a = 0;
472
473         /* BACKWARDS! also watch order of free-ing! (mesh<->mat) */
474
475         lb[a++]= &(main->ipo);
476         lb[a++]= &(main->action); // xxx moved here to avoid problems when freeing with animato (aligorith)
477         lb[a++]= &(main->key);
478         lb[a++]= &(main->nodetree);
479         lb[a++]= &(main->image);
480         lb[a++]= &(main->tex);
481         lb[a++]= &(main->mat);
482         lb[a++]= &(main->vfont);
483         
484         /* Important!: When adding a new object type,
485          * the specific data should be inserted here 
486          */
487
488         lb[a++]= &(main->armature);
489
490         lb[a++]= &(main->mesh);
491         lb[a++]= &(main->curve);
492         lb[a++]= &(main->mball);
493
494         lb[a++]= &(main->wave);
495         lb[a++]= &(main->latt);
496         lb[a++]= &(main->lamp);
497         lb[a++]= &(main->camera);
498         
499         lb[a++]= &(main->text);
500         lb[a++]= &(main->sound);
501         lb[a++]= &(main->group);
502         lb[a++]= &(main->brush);
503         lb[a++]= &(main->script);
504         lb[a++]= &(main->particle);
505         
506         lb[a++]= &(main->world);
507         lb[a++]= &(main->screen);
508         lb[a++]= &(main->object);
509         lb[a++]= &(main->scene);
510         lb[a++]= &(main->library);
511         lb[a++]= &(main->wm);
512         lb[a++]= &(main->gpencil);
513         
514         lb[a]= NULL;
515
516         return a;
517 }
518
519 /* *********** ALLOC AND FREE *****************
520   
521 free_libblock(ListBase *lb, ID *id )
522         provide a list-basis and datablock, but only ID is read
523
524 void *alloc_libblock(ListBase *lb, type, name)
525         inserts in list and returns a new ID
526
527  ***************************** */
528
529 static ID *alloc_libblock_notest(short type)
530 {
531         ID *id= NULL;
532         
533         switch( type ) {
534                 case ID_SCE:
535                         id= MEM_callocN(sizeof(Scene), "scene");
536                         break;
537                 case ID_LI:
538                         id= MEM_callocN(sizeof(Library), "library");
539                         break;
540                 case ID_OB:
541                         id= MEM_callocN(sizeof(Object), "object");
542                         break;
543                 case ID_ME:
544                         id= MEM_callocN(sizeof(Mesh), "mesh");
545                         break;
546                 case ID_CU:
547                         id= MEM_callocN(sizeof(Curve), "curve");
548                         break;
549                 case ID_MB:
550                         id= MEM_callocN(sizeof(MetaBall), "mball");
551                         break;
552                 case ID_MA:
553                         id= MEM_callocN(sizeof(Material), "mat");
554                         break;
555                 case ID_TE:
556                         id= MEM_callocN(sizeof(Tex), "tex");
557                         break;
558                 case ID_IM:
559                         id= MEM_callocN(sizeof(Image), "image");
560                         break;
561                 case ID_WV:
562                         id= MEM_callocN(sizeof(Wave), "wave");
563                         break;
564                 case ID_LT:
565                         id= MEM_callocN(sizeof(Lattice), "latt");
566                         break;
567                 case ID_LA:
568                         id= MEM_callocN(sizeof(Lamp), "lamp");
569                         break;
570                 case ID_CA:
571                         id= MEM_callocN(sizeof(Camera), "camera");
572                         break;
573                 case ID_IP:
574                         id= MEM_callocN(sizeof(Ipo), "ipo");
575                         break;
576                 case ID_KE:
577                         id= MEM_callocN(sizeof(Key), "key");
578                         break;
579                 case ID_WO:
580                         id= MEM_callocN(sizeof(World), "world");
581                         break;
582                 case ID_SCR:
583                         id= MEM_callocN(sizeof(bScreen), "screen");
584                         break;
585                 case ID_VF:
586                         id= MEM_callocN(sizeof(VFont), "vfont");
587                         break;
588                 case ID_TXT:
589                         id= MEM_callocN(sizeof(Text), "text");
590                         break;
591                 case ID_SCRIPT:
592                         //XXX id= MEM_callocN(sizeof(Script), "script");
593                         break;
594                 case ID_SO:
595                         id= MEM_callocN(sizeof(bSound), "sound");
596                         break;
597                 case ID_GR:
598                         id= MEM_callocN(sizeof(Group), "group");
599                         break;
600                 case ID_AR:
601                         id = MEM_callocN(sizeof(bArmature), "armature");
602                         break;
603                 case ID_AC:
604                         id = MEM_callocN(sizeof(bAction), "action");
605                         break;
606                 case ID_NT:
607                         id = MEM_callocN(sizeof(bNodeTree), "nodetree");
608                         break;
609                 case ID_BR:
610                         id = MEM_callocN(sizeof(Brush), "brush");
611                         break;
612                 case ID_PA:
613                         id = MEM_callocN(sizeof(ParticleSettings), "ParticleSettings");
614                         break;
615                 case ID_WM:
616                         id = MEM_callocN(sizeof(wmWindowManager), "Window manager");
617                         break;
618                 case ID_GD:
619                         id = MEM_callocN(sizeof(bGPdata), "Grease Pencil");
620                         break;
621         }
622         return id;
623 }
624
625 /* used everywhere in blenkernel and text.c */
626 void *alloc_libblock(ListBase *lb, short type, const char *name)
627 {
628         ID *id= NULL;
629         
630         id= alloc_libblock_notest(type);
631         if(id) {
632                 BLI_addtail(lb, id);
633                 id->us= 1;
634                 id->icon_id = 0;
635                 *( (short *)id->name )= type;
636                 new_id(lb, id, name);
637                 /* alphabetic insterion: is in new_id */
638         }
639         return id;
640 }
641
642 /* by spec, animdata is first item after ID */
643 /* we still read ->adt itself, to ensure compiler warns when it doesnt exist */
644 static void id_copy_animdata(ID *id)
645 {
646         switch(GS(id->name)) {
647                 case ID_OB:
648                         ((Object *)id)->adt= BKE_copy_animdata(((Object *)id)->adt);
649                         break;
650                 case ID_CU:
651                         ((Curve *)id)->adt= BKE_copy_animdata(((Curve *)id)->adt);
652                         break;
653                 case ID_CA:
654                         ((Camera *)id)->adt= BKE_copy_animdata(((Camera *)id)->adt);
655                         break;
656                 case ID_KE:
657                         ((Key *)id)->adt= BKE_copy_animdata(((Key *)id)->adt);
658                         break;
659                 case ID_LA:
660                         ((Lamp *)id)->adt= BKE_copy_animdata(((Lamp *)id)->adt);
661                         break;
662                 case ID_MA:
663                         ((Material *)id)->adt= BKE_copy_animdata(((Material *)id)->adt);
664                         break;
665                 case ID_NT:
666                         ((bNodeTree *)id)->adt= BKE_copy_animdata(((bNodeTree *)id)->adt);
667                         break;
668                 case ID_SCE:
669                         ((Scene *)id)->adt= BKE_copy_animdata(((Scene *)id)->adt);
670                         break;
671                 case ID_TE:
672                         ((Tex *)id)->adt= BKE_copy_animdata(((Tex *)id)->adt);
673                         break;
674                 case ID_WO:
675                         ((World *)id)->adt= BKE_copy_animdata(((World *)id)->adt);
676                         break;
677         }
678         
679 }
680
681 /* used everywhere in blenkernel and text.c */
682 void *copy_libblock(void *rt)
683 {
684         ID *idn, *id;
685         ListBase *lb;
686         char *cp, *cpn;
687         int idn_len;
688         
689         id= rt;
690
691         lb= wich_libbase(G.main, GS(id->name));
692         idn= alloc_libblock(lb, GS(id->name), id->name+2);
693         
694         if(idn==NULL) {
695                 printf("ERROR: Illegal ID name for %s (Crashing now)\n", id->name);
696         }
697         
698         idn_len= MEM_allocN_len(idn);
699         if(idn_len - sizeof(ID) > 0) {
700                 cp= (char *)id;
701                 cpn= (char *)idn;
702                 memcpy(cpn+sizeof(ID), cp+sizeof(ID), idn_len - sizeof(ID));
703         }
704         
705         id->newid= idn;
706         idn->flag |= LIB_NEW;
707         if (id->properties) idn->properties = IDP_CopyProperty(id->properties);
708
709         id_copy_animdata(id);
710         
711         return idn;
712 }
713
714 static void free_library(Library *lib)
715 {
716     /* no freeing needed for libraries yet */
717 }
718
719 static void (*free_windowmanager_cb)(bContext *, wmWindowManager *)= NULL;
720
721 void set_free_windowmanager_cb(void (*func)(bContext *C, wmWindowManager *) )
722 {
723         free_windowmanager_cb= func;
724 }
725
726 /* used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c */
727 void free_libblock(ListBase *lb, void *idv)
728 {
729         ID *id= idv;
730         
731         switch( GS(id->name) ) {        /* GetShort from util.h */
732                 case ID_SCE:
733                         free_scene((Scene *)id);
734                         break;
735                 case ID_LI:
736                         free_library((Library *)id);
737                         break;
738                 case ID_OB:
739                         free_object((Object *)id);
740                         break;
741                 case ID_ME:
742                         free_mesh((Mesh *)id, 1);
743                         break;
744                 case ID_CU:
745                         free_curve((Curve *)id);
746                         break;
747                 case ID_MB:
748                         free_mball((MetaBall *)id);
749                         break;
750                 case ID_MA:
751                         free_material((Material *)id);
752                         break;
753                 case ID_TE:
754                         free_texture((Tex *)id);
755                         break;
756                 case ID_IM:
757                         free_image((Image *)id);
758                         break;
759                 case ID_WV:
760                         /* free_wave(id); */
761                         break;
762                 case ID_LT:
763                         free_lattice((Lattice *)id);
764                         break;
765                 case ID_LA:
766                         free_lamp((Lamp *)id);
767                         break;
768                 case ID_CA:
769                         free_camera((Camera*) id);
770                         break;
771                 case ID_IP:
772                         free_ipo((Ipo *)id);
773                         break;
774                 case ID_KE:
775                         free_key((Key *)id);
776                         break;
777                 case ID_WO:
778                         free_world((World *)id);
779                         break;
780                 case ID_SCR:
781                         free_screen((bScreen *)id);
782                         break;
783                 case ID_VF:
784                         free_vfont((VFont *)id);
785                         break;
786                 case ID_TXT:
787                         free_text((Text *)id);
788                         break;
789                 case ID_SCRIPT:
790                         //XXX free_script((Script *)id);
791                         break;
792                 case ID_SO:
793                         sound_free((bSound*)id);
794                         break;
795                 case ID_GR:
796                         free_group((Group *)id);
797                         break;
798                 case ID_AR:
799                         free_armature((bArmature *)id);
800                         break;
801                 case ID_AC:
802                         free_action((bAction *)id);
803                         break;
804                 case ID_NT:
805                         ntreeFreeTree((bNodeTree *)id);
806                         break;
807                 case ID_BR:
808                         free_brush((Brush *)id);
809                         break;
810                 case ID_PA:
811                         psys_free_settings((ParticleSettings *)id);
812                         break;
813                 case ID_WM:
814                         if(free_windowmanager_cb)
815                                 free_windowmanager_cb(NULL, (wmWindowManager *)id);
816                         break;
817                 case ID_GD:
818                         free_gpencil_data((bGPdata *)id);
819                         break;
820         }
821
822         if (id->properties) {
823                 IDP_FreeProperty(id->properties);
824                 MEM_freeN(id->properties);
825         }
826         BLI_remlink(lb, id);
827         MEM_freeN(id);
828
829 }
830
831 void free_libblock_us(ListBase *lb, void *idv)          /* test users */
832 {
833         ID *id= idv;
834         
835         id->us--;
836
837         if(id->us<0) {
838                 if(id->lib) printf("ERROR block %s %s users %d\n", id->lib->name, id->name, id->us);
839                 else printf("ERROR block %s users %d\n", id->name, id->us);
840         }
841         if(id->us==0) {
842                 if( GS(id->name)==ID_OB ) unlink_object(NULL, (Object *)id);
843                 
844                 free_libblock(lb, id);
845         }
846 }
847
848
849 void free_main(Main *mainvar)
850 {
851         /* also call when reading a file, erase all, etc */
852         ListBase *lbarray[MAX_LIBARRAY];
853         int a;
854
855         a= set_listbasepointers(mainvar, lbarray);
856         while(a--) {
857                 ListBase *lb= lbarray[a];
858                 ID *id;
859                 
860                 while ( (id= lb->first) ) {
861                         free_libblock(lb, id);
862                 }
863         }
864
865         MEM_freeN(mainvar);
866 }
867
868 /* ***************** ID ************************ */
869
870
871 ID *find_id(char *type, char *name)             /* type: "OB" or "MA" etc */
872 {
873         ID *id;
874         ListBase *lb;
875         
876         lb= wich_libbase(G.main, GS(type));
877         
878         id= lb->first;
879         while(id) {
880                 if(id->name[2]==name[0] && strcmp(id->name+2, name)==0 ) 
881                         return id;
882                 id= id->next;
883         }
884         return 0;
885 }
886
887 static void get_flags_for_id(ID *id, char *buf) 
888 {
889         int isfake= id->flag & LIB_FAKEUSER;
890         int isnode=0;
891                 /* Writeout the flags for the entry, note there
892                  * is a small hack that writes 5 spaces instead
893                  * of 4 if no flags are displayed... this makes
894                  * things usually line up ok - better would be
895                  * to have that explicit, oh well - zr
896                  */
897
898         if(GS(id->name)==ID_MA)
899                 isnode= ((Material *)id)->use_nodes;
900         if(GS(id->name)==ID_TE)
901                 isnode= ((Tex *)id)->use_nodes;
902         
903         if (id->us<0)
904                 sprintf(buf, "-1W ");
905         else if (!id->lib && !isfake && id->us && !isnode)
906                 sprintf(buf, "     ");
907         else if(isnode)
908                 sprintf(buf, "%c%cN%c ", id->lib?'L':' ', isfake?'F':' ', (id->us==0)?'O':' ');
909         else
910                 sprintf(buf, "%c%c%c ", id->lib?'L':' ', isfake?'F':' ', (id->us==0)?'O':' ');
911 }
912
913 #define IDPUP_NO_VIEWER 1
914
915 static void IDnames_to_dyn_pupstring(DynStr *pupds, ListBase *lb, ID *link, short *nr, int hideflag)
916 {
917         int i, nids= BLI_countlist(lb);
918                 
919         if (nr) *nr= -1;
920         
921         if (nr && nids>MAX_IDPUP) {
922                 BLI_dynstr_append(pupds, "DataBrowse %x-2");
923                 *nr= -2;
924         } else {
925                 ID *id;
926                 
927                 for (i=0, id= lb->first; id; id= id->next, i++) {
928                         char buf[32];
929                         
930                         if (nr && id==link) *nr= i+1;
931
932                         if (U.uiflag & USER_HIDE_DOT && id->name[2]=='.')
933                                 continue;
934                         if (hideflag & IDPUP_NO_VIEWER)
935                                 if (GS(id->name)==ID_IM)
936                                         if ( ((Image *)id)->source==IMA_SRC_VIEWER )
937                                                 continue;
938                         
939                         get_flags_for_id(id, buf);
940                                 
941                         BLI_dynstr_append(pupds, buf);
942                         BLI_dynstr_append(pupds, id->name+2);
943                         sprintf(buf, "%%x%d", i+1);
944                         BLI_dynstr_append(pupds, buf);
945                         
946                         /* icon */
947                         switch(GS(id->name))
948                         {
949                         case ID_MA: /* fall through */
950                         case ID_TE: /* fall through */
951                         case ID_IM: /* fall through */
952                         case ID_WO: /* fall through */
953                         case ID_LA: /* fall through */
954                                 sprintf(buf, "%%i%d", BKE_icon_getid(id) );
955                                 BLI_dynstr_append(pupds, buf);
956                                 break;
957                         default:
958                                 break;
959                         }
960                         
961                         if(id->next)
962                                 BLI_dynstr_append(pupds, "|");
963                 }
964         }
965 }
966
967
968 /* used by headerbuttons.c buttons.c editobject.c editseq.c */
969 /* if nr==NULL no MAX_IDPUP, this for non-header browsing */
970 void IDnames_to_pupstring(char **str, char *title, char *extraops, ListBase *lb, ID *link, short *nr)
971 {
972         DynStr *pupds= BLI_dynstr_new();
973
974         if (title) {
975                 BLI_dynstr_append(pupds, title);
976                 BLI_dynstr_append(pupds, "%t|");
977         }
978         
979         if (extraops) {
980                 BLI_dynstr_append(pupds, extraops);
981                 if (BLI_dynstr_get_len(pupds))
982                         BLI_dynstr_append(pupds, "|");
983         }
984
985         IDnames_to_dyn_pupstring(pupds, lb, link, nr, 0);
986         
987         *str= BLI_dynstr_get_cstring(pupds);
988         BLI_dynstr_free(pupds);
989 }
990
991 /* skips viewer images */
992 void IMAnames_to_pupstring(char **str, char *title, char *extraops, ListBase *lb, ID *link, short *nr)
993 {
994         DynStr *pupds= BLI_dynstr_new();
995         
996         if (title) {
997                 BLI_dynstr_append(pupds, title);
998                 BLI_dynstr_append(pupds, "%t|");
999         }
1000         
1001         if (extraops) {
1002                 BLI_dynstr_append(pupds, extraops);
1003                 if (BLI_dynstr_get_len(pupds))
1004                         BLI_dynstr_append(pupds, "|");
1005         }
1006         
1007         IDnames_to_dyn_pupstring(pupds, lb, link, nr, IDPUP_NO_VIEWER);
1008         
1009         *str= BLI_dynstr_get_cstring(pupds);
1010         BLI_dynstr_free(pupds);
1011 }
1012
1013
1014 /* used by buttons.c library.c mball.c */
1015 void splitIDname(char *name, char *left, int *nr)
1016 {
1017         int a;
1018         
1019         *nr= 0;
1020         strncpy(left, name, 21);
1021         
1022         a= strlen(name);
1023         if(a>1 && name[a-1]=='.') return;
1024         
1025         while(a--) {
1026                 if( name[a]=='.' ) {
1027                         left[a]= 0;
1028                         *nr= atol(name+a+1);
1029                         return;
1030                 }
1031                 if( isdigit(name[a])==0 ) break;
1032                 
1033                 left[a]= 0;
1034         }
1035         strcpy(left, name);     
1036 }
1037
1038 static void sort_alpha_id(ListBase *lb, ID *id)
1039 {
1040         ID *idtest;
1041         
1042         /* insert alphabetically */
1043         if(lb->first!=lb->last) {
1044                 BLI_remlink(lb, id);
1045                 
1046                 idtest= lb->first;
1047                 while(idtest) {
1048                         if(BLI_strcasecmp(idtest->name, id->name)>0 || idtest->lib) {
1049                                 BLI_insertlinkbefore(lb, idtest, id);
1050                                 break;
1051                         }
1052                         idtest= idtest->next;
1053                 }
1054                 /* as last */
1055                 if(idtest==0) {
1056                         BLI_addtail(lb, id);
1057                 }
1058         }
1059         
1060 }
1061
1062 /* 
1063  * Check to see if an ID name is already used, and find a new one if so.
1064  * Return 1 if created a new name (returned in name).
1065  *
1066  * Normally the ID that's being check is already in the ListBase, so ID *id
1067  * points at the new entry.  The Python Library module needs to know what
1068  * the name of a datablock will be before it is appended; in this case ID *id
1069  * id is NULL;
1070  */
1071
1072 int check_for_dupid(ListBase *lb, ID *id, char *name)
1073 {
1074         ID *idtest;
1075         int nr= 0, nrtest, a;
1076         const int maxtest=32;
1077         char left[32], leftest[32], in_use[32];
1078         
1079         /* make sure input name is terminated properly */
1080         if( strlen(name) > 21 ) name[21]= 0;
1081
1082         while (1) {
1083
1084                 /* phase 1: id already exists? */
1085                 for( idtest = lb->first; idtest; idtest = idtest->next ) {
1086                                 /* if idtest is not a lib */ 
1087                         if( id != idtest && idtest->lib == NULL ) {
1088                                 /* do not test alphabetic! */
1089                                 /* optimized */
1090                                 if( idtest->name[2] == name[0] ) {
1091                                         if(strcmp(name, idtest->name+2)==0) break;
1092                                 }
1093                         }
1094                 }
1095
1096                 /* if there is no double, done */
1097                 if( idtest == NULL ) return 0;
1098
1099                 /* we have a dup; need to make a new name */
1100                 /* quick check so we can reuse one of first 32 ids if vacant */
1101                 memset(in_use, 0, maxtest);
1102
1103                 /* get name portion, number portion ("name.number") */
1104                 splitIDname( name, left, &nr);
1105
1106                 /* if new name will be too long, truncate it */
1107                 if(nr>999 && strlen(left)>16) left[16]= 0;
1108                 else if(strlen(left)>17) left[17]= 0;
1109
1110                 for( idtest = lb->first; idtest; idtest = idtest->next ) {
1111                         if( id != idtest && idtest->lib == NULL ) {
1112                                 splitIDname(idtest->name+2, leftest, &nrtest);
1113                                 /* if base names match... */
1114                                 /* optimized */
1115                                 if( *left == *leftest && strcmp(left, leftest)==0 ) {
1116                                         if(nrtest < maxtest)
1117                                                 in_use[nrtest]= 1;      /* mark as used */
1118                                         if(nr <= nrtest)
1119                                                 nr= nrtest+1;           /* track largest unused */
1120                                 }
1121                         }
1122                 }
1123
1124                 /* decide which value of nr to use */
1125                 for(a=0; a<maxtest; a++) {
1126                         if(a>=nr) break;        /* stop when we've check up to biggest */
1127                         if( in_use[a]==0 ) { /* found an unused value */
1128                                 nr = a;
1129                                 break;
1130                         }
1131                 }
1132
1133                 /* if non-numbered name was not in use, reuse it */
1134                 if(nr==0) strcpy( name, left );
1135                 else {
1136                         if(nr > 999 && strlen(left) > 16) {
1137                                 /* this would overflow name buffer */
1138                                 left[16] = 0;
1139                                 strcpy( name, left );
1140                                 continue;
1141                         }
1142                         /* this format specifier is from hell... */
1143                         sprintf(name, "%s.%.3d", left, nr);
1144                 }
1145                 return 1;
1146         }
1147 }
1148
1149 /*
1150  * Only for local blocks: external en indirect blocks already have a
1151  * unique ID.
1152  *
1153  * return 1: created a new name
1154  */
1155
1156 int new_id(ListBase *lb, ID *id, const char *tname)
1157 {
1158         int result;
1159         char name[22];
1160         
1161         /* if library, don't rename */
1162         if(id->lib) return 0;
1163
1164         /* if no libdata given, look up based on ID */
1165         if(lb==NULL) lb= wich_libbase(G.main, GS(id->name));
1166
1167         if(tname==0) {  /* if no name given, use name of current ID */
1168                 strncpy(name, id->name+2, 21);
1169                 result= strlen(id->name+2);
1170         }
1171         else { /* else make a copy (tname args can be const) */
1172                 strncpy(name, tname, 21);
1173                 result= strlen(tname);
1174         }
1175
1176         /* if result > 21, strncpy don't put the final '\0' to name. */
1177         if( result >= 21 ) name[21]= 0;
1178
1179         result = check_for_dupid( lb, id, name );
1180         strcpy( id->name+2, name );
1181
1182         /* This was in 2.43 and previous releases
1183          * however all data in blender should be sorted, not just duplicate names
1184          * sorting should not hurt, but noting just incause it alters the way other
1185          * functions work, so sort every time */
1186         /* if( result )
1187                 sort_alpha_id(lb, id);*/
1188         
1189         sort_alpha_id(lb, id);
1190         
1191         return result;
1192 }
1193
1194 /* next to indirect usage in read/writefile also in editobject.c scene.c */
1195 void clear_id_newpoins()
1196 {
1197         ListBase *lbarray[MAX_LIBARRAY];
1198         ID *id;
1199         int a;
1200
1201         a= set_listbasepointers(G.main, lbarray);
1202         while(a--) {
1203                 id= lbarray[a]->first;
1204                 while(id) {
1205                         id->newid= 0;
1206                         id->flag &= ~LIB_NEW;
1207                         id= id->next;
1208                 }
1209         }
1210 }
1211
1212 /* only for library fixes */
1213 static void image_fix_relative_path(Image *ima)
1214 {
1215         if(ima->id.lib==NULL) return;
1216         if(strncmp(ima->name, "//", 2)==0) {
1217                 BLI_convertstringcode(ima->name, ima->id.lib->filename);
1218                 BLI_makestringcode(G.sce, ima->name);
1219         }
1220 }
1221
1222 #define LIBTAG(a)       if(a && a->id.lib) {a->id.flag &=~LIB_INDIRECT; a->id.flag |= LIB_EXTERN;}
1223
1224 static void lib_indirect_test_id(ID *id)
1225 {
1226         
1227         if(id->lib)
1228                 return;
1229         
1230         if(GS(id->name)==ID_OB) {               
1231                 Object *ob= (Object *)id;
1232                 bActionStrip *strip;
1233                 Mesh *me;
1234
1235                 int a;
1236         
1237                 // XXX old animation system!
1238                 for (strip=ob->nlastrips.first; strip; strip=strip->next){
1239                         LIBTAG(strip->object); 
1240                         LIBTAG(strip->act);
1241                         LIBTAG(strip->ipo);
1242                 }
1243         
1244                 for(a=0; a<ob->totcol; a++) {
1245                         LIBTAG(ob->mat[a]);
1246                 }
1247         
1248                 LIBTAG(ob->dup_group);
1249                 LIBTAG(ob->proxy);
1250                 
1251                 me= ob->data;
1252                 LIBTAG(me);
1253         }
1254 }
1255
1256 void tag_main(struct Main *mainvar, int tag)
1257 {
1258         ListBase *lbarray[MAX_LIBARRAY];
1259         ID *id;
1260         int a;
1261
1262         a= set_listbasepointers(mainvar, lbarray);
1263         while(a--) {
1264                 for(id= lbarray[a]->first; id; id= id->next) {
1265                         if(tag) id->flag |= LIB_DOIT;
1266                         else    id->flag &= ~LIB_DOIT;
1267                 }
1268         }
1269 }
1270
1271 /* if lib!=NULL, only all from lib local */
1272 void all_local(Library *lib, int untagged_only)
1273 {
1274         ListBase *lbarray[MAX_LIBARRAY], tempbase={0, 0};
1275         ID *id, *idn;
1276         int a;
1277
1278         a= set_listbasepointers(G.main, lbarray);
1279         while(a--) {
1280                 id= lbarray[a]->first;
1281                 
1282                 while(id) {
1283                         id->newid= NULL;
1284                         idn= id->next;          /* id is possibly being inserted again */
1285                         
1286                         /* The check on the second line (LIB_APPEND_TAG) is done so its
1287                          * possible to tag data you dont want to be made local, used for
1288                          * appending data, so any libdata alredy linked wont become local
1289                          * (very nasty to discover all your links are lost after appending)  
1290                          * */
1291                         if(id->flag & (LIB_EXTERN|LIB_INDIRECT|LIB_NEW) &&
1292                           (untagged_only==0 || !(id->flag & LIB_APPEND_TAG)))
1293                         {
1294                                 if(lib==NULL || id->lib==lib) {
1295                                         id->flag &= ~(LIB_EXTERN|LIB_INDIRECT|LIB_NEW);
1296
1297                                         if(id->lib) {
1298                                                 /* relative file patch */
1299                                                 if(GS(id->name)==ID_IM)
1300                                                         image_fix_relative_path((Image *)id);
1301                                                 
1302                                                 id->lib= NULL;
1303                                                 new_id(lbarray[a], id, 0);      /* new_id only does it with double names */
1304                                                 sort_alpha_id(lbarray[a], id);
1305                                         }
1306                                 }
1307                         }
1308                         id= idn;
1309                 }
1310                 
1311                 /* patch2: make it aphabetically */
1312                 while( (id=tempbase.first) ) {
1313                         BLI_remlink(&tempbase, id);
1314                         BLI_addtail(lbarray[a], id);
1315                         new_id(lbarray[a], id, 0);
1316                 }
1317         }
1318
1319         /* patch 3: make sure library data isn't indirect falsely... */
1320         a= set_listbasepointers(G.main, lbarray);
1321         while(a--) {
1322                 for(id= lbarray[a]->first; id; id=id->next)
1323                         lib_indirect_test_id(id);
1324         }
1325 }
1326
1327
1328 void test_idbutton(char *name)
1329 {
1330         /* called from buttons: when name already exists: call new_id */
1331         ListBase *lb;
1332         ID *idtest;
1333         
1334
1335         lb= wich_libbase(G.main, GS(name-2) );
1336         if(lb==0) return;
1337         
1338         /* search for id */
1339         idtest= lb->first;
1340         while(idtest) {
1341                 if( strcmp(idtest->name+2, name)==0) break;
1342                 idtest= idtest->next;
1343         }
1344
1345         if(idtest) if( new_id(lb, idtest, name)==0 ) sort_alpha_id(lb, idtest);
1346 }
1347
1348 void text_idbutton(struct ID *id, char *text)
1349 {
1350         if(id) {
1351                 if(GS(id->name)==ID_SCE)
1352                         strcpy(text, "SCE: ");
1353         else if(GS(id->name)==ID_SCE)
1354                         strcpy(text, "SCR: ");
1355         else if(GS(id->name)==ID_MA && ((Material*)id)->use_nodes)
1356                         strcpy(text, "NT: ");
1357         else {
1358                         text[0]= id->name[0];
1359                         text[1]= id->name[1];
1360                         text[2]= ':';
1361                         text[3]= ' ';
1362                         text[4]= 0;
1363         }
1364         }
1365         else
1366                 strcpy(text, "");
1367 }
1368
1369 void rename_id(ID *id, char *name)
1370 {
1371         ListBase *lb;
1372         
1373         strncpy(id->name+2, name, 21);
1374         lb= wich_libbase(G.main, GS(id->name) );
1375         
1376         new_id(lb, id, name);                           
1377 }
1378