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