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