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