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