2dad108e32029eaeff642cfac91dba5f64ac5910
[blender-staging.git] / source / blender / blenkernel / intern / library.c
1 /** 
2  * $Id$
3  * 
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 /*
34  *  Contains management of ID's and libraries
35  *  allocate and free of all library data
36  * 
37  */
38
39
40 #include <stdio.h>
41 #include <ctype.h>
42 #include <string.h>
43 #include <stdlib.h>
44
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif
48
49 #include "MEM_guardedalloc.h"
50
51 /* all types are needed here, in order to do memory operations */
52 #include "DNA_ID.h"
53 #include "DNA_listBase.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_mesh_types.h"
57 #include "DNA_lattice_types.h"
58 #include "DNA_curve_types.h"
59 #include "DNA_meta_types.h"
60 #include "DNA_material_types.h"
61 #include "DNA_texture_types.h"
62 #include "DNA_image_types.h"
63 #include "DNA_wave_types.h"
64 #include "DNA_lamp_types.h"
65 #include "DNA_camera_types.h"
66 #include "DNA_ipo_types.h"
67 #include "DNA_key_types.h"
68 #include "DNA_world_types.h"
69 #include "DNA_screen_types.h"
70 #include "DNA_vfont_types.h"
71 #include "DNA_text_types.h"
72 #include "DNA_sound_types.h"
73 #include "DNA_group_types.h"
74 #include "DNA_armature_types.h"
75 #include "DNA_action_types.h"
76 #include "DNA_userdef_types.h"
77
78 #include "BLI_blenlib.h"
79 #include "BLI_dynstr.h"
80
81 #include "BKE_bad_level_calls.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_script.h"
89 #include "BKE_mesh.h"
90 #include "BKE_material.h"
91 #include "BKE_curve.h"
92 #include "BKE_mball.h"
93 #include "BKE_text.h"
94 #include "BKE_texture.h"
95 #include "BKE_scene.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 "BPI_script.h"
106
107 #define MAX_IDPUP               60      /* was 24 */
108 #define MAX_LIBARRAY    100 /* was 30, warning: used it readfile.c too */
109
110 /* ************* general ************************ */
111
112 void id_lib_extern(ID *id)
113 {
114         if(id) {
115                 if(id->flag & LIB_INDIRECT) {
116                         id->flag -= LIB_INDIRECT;
117                         id->flag |= LIB_EXTERN;
118                 }
119         }
120 }
121
122 void id_us_plus(ID *id)
123 {
124         if(id) {
125                 id->us++;
126                 if(id->flag & LIB_INDIRECT) {
127                         id->flag -= LIB_INDIRECT;
128                         id->flag |= LIB_EXTERN;
129                 }
130         }
131 }
132
133 ListBase *wich_libbase(Main *mainlib, short type)
134 {
135         switch( type ) {
136                 case ID_SCE:
137                         return &(mainlib->scene);
138                 case ID_LI:
139                         return &(mainlib->library);
140                 case ID_OB:
141                         return &(mainlib->object);
142                 case ID_ME:
143                         return &(mainlib->mesh);
144                 case ID_CU:
145                         return &(mainlib->curve);
146                 case ID_MB:
147                         return &(mainlib->mball);
148                 case ID_MA:
149                         return &(mainlib->mat);
150                 case ID_TE:
151                         return &(mainlib->tex);
152                 case ID_IM:
153                         return &(mainlib->image);
154                 case ID_WV:
155                         return &(mainlib->wave);
156                 case ID_LT:
157                         return &(mainlib->latt);
158                 case ID_LA:
159                         return &(mainlib->lamp);
160                 case ID_CA:
161                         return &(mainlib->camera);
162                 case ID_IP:
163                         return &(mainlib->ipo);
164                 case ID_KE:
165                         return &(mainlib->key);
166                 case ID_WO:
167                         return &(mainlib->world);
168                 case ID_SCR:
169                         return &(mainlib->screen);
170                 case ID_VF:
171                         return &(mainlib->vfont);
172                 case ID_TXT:
173                         return &(mainlib->text);
174                 case ID_SCRIPT:
175                         return &(mainlib->script);
176                 case ID_SO:
177                         return &(mainlib->sound);
178                 case ID_SAMPLE:
179                         /* declared as an external in sound.h !!! */
180                         return (samples);
181                 case ID_GR:
182                         return &(mainlib->group);
183                 case ID_AR:
184                         return &(mainlib->armature);
185                 case ID_AC:
186                         return &(mainlib->action);
187         }
188         return 0;
189 }
190
191 int set_listbasepointers(Main *main, ListBase **lb)
192 {
193         /* BACKWARDS! also watch order of free-ing! (mesh<->mat) */
194
195         lb[0]= &(main->ipo);
196         lb[1]= &(main->key);
197         lb[2]= &(main->image);
198         lb[3]= &(main->tex);
199         lb[4]= &(main->mat);
200         lb[5]= &(main->vfont);
201         
202         /* Important!: When adding a new object type,
203          * the specific data should be inserted here 
204          */
205
206         lb[6]= &(main->armature);
207         lb[7]= &(main->action);
208
209         lb[8]= &(main->mesh);
210         lb[9]= &(main->curve);
211         lb[10]= &(main->mball);
212
213         lb[11]= &(main->wave);
214         lb[12]= &(main->latt);
215         lb[13]= &(main->lamp);
216         lb[14]= &(main->camera);
217
218         lb[15]= &(main->world);
219         lb[16]= &(main->screen);
220         lb[17]= &(main->object);
221         lb[18]= &(main->scene);
222         lb[19]= &(main->library);
223         lb[20]= &(main->text);
224         lb[21]= &(main->sound);
225         lb[22]= &(main->group);
226
227         lb[23]= samples;
228         lb[24]= &(main->script);
229         lb[25]= NULL;
230
231         return 25;
232 }
233
234 /* *********** ALLOC AND FREE *****************
235   
236 free_libblock(ListBase *lb, ID *id )
237         provide a list-basis and datablock, but only ID is read
238
239 void *alloc_libblock(ListBase *lb, type, name)
240         inserts in list and returns a new ID
241
242  ***************************** */
243
244 static ID *alloc_libblock_notest(short type)
245 {
246         ID *id= 0;
247         
248         switch( type ) {
249                 case ID_SCE:
250                         id= MEM_callocN(sizeof(Scene), "scene");
251                         break;
252                 case ID_LI:
253                         id= MEM_callocN(sizeof(Library), "library");
254                         break;
255                 case ID_OB:
256                         id= MEM_callocN(sizeof(Object), "object");
257                         break;
258                 case ID_ME:
259                         id= MEM_callocN(sizeof(Mesh), "mesh");
260                         break;
261                 case ID_CU:
262                         id= MEM_callocN(sizeof(Curve), "curve");
263                         break;
264                 case ID_MB:
265                         id= MEM_callocN(sizeof(MetaBall), "mball");
266                         break;
267                 case ID_MA:
268                         id= MEM_callocN(sizeof(Material), "mat");
269                         break;
270                 case ID_TE:
271                         id= MEM_callocN(sizeof(Tex), "tex");
272                         break;
273                 case ID_IM:
274                         id= MEM_callocN(sizeof(Image), "image");
275                         break;
276                 case ID_WV:
277                         id= MEM_callocN(sizeof(Wave), "wave");
278                         break;
279                 case ID_LT:
280                         id= MEM_callocN(sizeof(Lattice), "latt");
281                         break;
282                 case ID_LA:
283                         id= MEM_callocN(sizeof(Lamp), "lamp");
284                         break;
285                 case ID_CA:
286                         id= MEM_callocN(sizeof(Camera), "camera");
287                         break;
288                 case ID_IP:
289                         id= MEM_callocN(sizeof(Ipo), "ipo");
290                         break;
291                 case ID_KE:
292                         id= MEM_callocN(sizeof(Key), "key");
293                         break;
294                 case ID_WO:
295                         id= MEM_callocN(sizeof(World), "world");
296                         break;
297                 case ID_SCR:
298                         id= MEM_callocN(sizeof(bScreen), "screen");
299                         break;
300                 case ID_VF:
301                         id= MEM_callocN(sizeof(VFont), "vfont");
302                         break;
303                 case ID_TXT:
304                         id= MEM_callocN(sizeof(Text), "text");
305                         break;
306                 case ID_SCRIPT:
307                         id= MEM_callocN(sizeof(Script), "script");
308                         break;
309                 case ID_SO:
310                         id= MEM_callocN(sizeof(bSound), "sound");
311                         break;
312                 case ID_SAMPLE:
313                         id = MEM_callocN(sizeof(bSample), "sound");
314                         break;
315                 case ID_GR:
316                         id= MEM_callocN(sizeof(Group), "sound");
317                         break;
318                 case ID_AR:
319                         id = MEM_callocN(sizeof(bArmature), "armature");
320                         break;
321                 case ID_AC:
322                         id = MEM_callocN(sizeof(bAction), "action");
323                         break;
324         }
325         return id;
326 }
327
328 // used everywhere in blenkernel and text.c
329 void *alloc_libblock(ListBase *lb, short type, char *name)
330 {
331         ID *id= 0;
332         
333         id= alloc_libblock_notest(type);
334         if(id) {
335                 BLI_addtail(lb, id);
336                 id->us= 1;
337                 *( (short *)id->name )= type;
338                 new_id(lb, id, name);
339                 /* alphabetic insterion: is in new_id */
340         }
341         return id;
342 }
343
344 /* GS reads the memory pointed at in a specific ordering. There are,
345  * however two definitions for it. I have jotted them down here, both,
346  * but I think the first one is actually used. The thing is that
347  * big-endian systems might read this the wrong way round. OTOH, we
348  * constructed the IDs that are read out with this macro explicitly as
349  * well. I expect we'll sort it out soon... */
350
351 /* from blendef: */
352 #define GS(a)   (*((short *)(a)))
353
354 /* from misc_util: flip the bytes from x  */
355 /*#define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
356
357 // used everywhere in blenkernel and text.c
358 void *copy_libblock(void *rt)
359 {
360         ID *idn, *id;
361         ListBase *lb;
362         char *cp, *cpn;
363         int idn_len;
364         
365         id= rt;
366
367         lb= wich_libbase(G.main, GS(id->name));
368         idn= alloc_libblock(lb, GS(id->name), id->name+2);
369         
370         idn_len= MEM_allocN_len(idn);
371         if(idn_len - sizeof(ID) > 0) {
372                 cp= (char *)id;
373                 cpn= (char *)idn;
374                 memcpy(cpn+sizeof(ID), cp+sizeof(ID), idn_len - sizeof(ID));
375         }
376         
377         id->newid= idn;
378         idn->flag |= LIB_NEW;
379         
380         return idn;
381 }
382
383 static void free_library(Library *lib)
384 {
385     /* no freeing needed for libraries yet */
386 }
387
388 // used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c
389 void free_libblock(ListBase *lb, void *idv)
390 {
391         ID *id= idv;
392         
393         switch( GS(id->name) ) {        /* GetShort from util.h */
394                 case ID_SCE:
395                         free_scene((Scene *)id);
396                         break;
397                 case ID_LI:
398                         free_library((Library *)id);
399                         break;
400                 case ID_OB:
401                         free_object((Object *)id);
402                         break;
403                 case ID_ME:
404                         free_mesh((Mesh *)id);
405                         break;
406                 case ID_CU:
407                         free_curve((Curve *)id);
408                         break;
409                 case ID_MB:
410                         free_mball((MetaBall *)id);
411                         break;
412                 case ID_MA:
413                         free_material((Material *)id);
414                         break;
415                 case ID_TE:
416                         free_texture((Tex *)id);
417                         break;
418                 case ID_IM:
419                         free_image((Image *)id);
420                         break;
421                 case ID_WV:
422                         /* free_wave(id); */
423                         break;
424                 case ID_LT:
425                         free_lattice((Lattice *)id);
426                         break;
427                 case ID_LA:
428                         free_lamp((Lamp *)id);
429                         break;
430                 case ID_CA:
431                         free_camera((Camera*) id);
432                         break;
433                 case ID_IP:
434                         free_ipo((Ipo *)id);
435                         break;
436                 case ID_KE:
437                         free_key((Key *)id);
438                         break;
439                 case ID_WO:
440                         free_world((World *)id);
441                         break;
442                 case ID_SCR:
443                         free_screen((bScreen *)id);
444                         break;
445                 case ID_VF:
446                         free_vfont((VFont *)id);
447                         break;
448                 case ID_TXT:
449                         free_text((Text *)id);
450                         break;
451                 case ID_SCRIPT:
452                         free_script((Script *)id);
453                         break;
454                 case ID_SO:
455                         sound_free_sound((bSound *)id);
456                         break;
457                 case ID_SAMPLE:
458                         sound_free_sample((bSample *)id);
459                         break;
460                 case ID_GR:
461                         free_group((Group *)id);
462                         break;
463                 case ID_AR:
464                         free_armature((bArmature *)id);
465                         break;
466                 case ID_AC:
467                         free_action((bAction *)id);
468                         break;
469         }
470
471         BLI_remlink(lb, id);
472         MEM_freeN(id);
473
474 }
475
476 void free_libblock_us(ListBase *lb, void *idv)          /* test users */
477 {
478         ID *id= idv;
479         
480         id->us--;
481
482         if(id->us<0) {
483                 if(id->lib) printf("ERROR block %s %s users %d\n", id->lib->name, id->name, id->us);
484                 else printf("ERROR block %s users %d\n", id->name, id->us);
485         }
486         if(id->us==0) {
487                 if( GS(id->name)==ID_OB ) unlink_object((Object *)id);
488                 
489                 free_libblock(lb, id);
490         }
491 }
492
493
494 void free_main(Main *mainvar)
495 {
496         /* also call when reading a file, erase all, etc */
497         ListBase *lbarray[MAX_LIBARRAY];
498         int a;
499
500         a= set_listbasepointers(mainvar, lbarray);
501         while(a--) {
502                 ListBase *lb= lbarray[a];
503                 ID *id;
504                 
505                 while ( (id= lb->first) ) {
506                         free_libblock(lb, id);
507                 }
508         }
509
510         MEM_freeN(mainvar);
511 }
512
513 /* ***************** ID ************************ */
514
515 // only used in exotic.c
516 ID *find_id(char *type, char *name)             /* type: "OB" or "MA" etc */
517 {
518         ID *id;
519         ListBase *lb;
520         
521         lb= wich_libbase(G.main, GS(type));
522         
523         id= lb->first;
524         while(id) {
525                 if( strcmp(id->name+2, name)==0 ) return id;
526                 id= id->next;
527         }
528         return 0;
529 }
530
531 static void get_flags_for_id(ID *id, char *buf) {
532         int isfake= id->flag & LIB_FAKEUSER;
533
534                 /* Writeout the flags for the entry, note there
535                  * is a small hack that writes 5 spaces instead
536                  * of 4 if no flags are displayed... this makes
537                  * things usually line up ok - better would be
538                  * to have that explicit, oh well - zr
539                  */
540
541         if (id->us<0)
542                 sprintf(buf, "-1W ");
543         else if (!id->lib && !isfake && id->us)
544                 sprintf(buf, "     ");
545         else
546                 sprintf(buf, "%c%c%c ", id->lib?'L':' ', isfake?'F':' ', (id->us==0)?'O':' ');
547 }
548
549 static void IDnames_to_dyn_pupstring(DynStr *pupds, ListBase *lb, ID *link, short *nr)
550 {
551         int i, nids= BLI_countlist(lb);
552                 
553         if (nr) *nr= -1;
554         
555         if (nr && nids>MAX_IDPUP) {
556                 BLI_dynstr_append(pupds, "DataBrowse %x-2");
557                 *nr= -2;
558         } else {
559                 ID *id;
560                 
561                 for (i=0, id= lb->first; id; id= id->next, i++) {
562                         char buf[32];
563                         
564                         if (nr && id==link) *nr= i+1;
565
566                         if (U.uiflag & USER_HIDE_DOT && id->name[2]=='.')
567                                 continue;
568                         
569                         get_flags_for_id(id, buf);
570                                 
571                         BLI_dynstr_append(pupds, buf);
572                         BLI_dynstr_append(pupds, id->name+2);
573                         sprintf(buf, "%%x%d", i+1);
574                         BLI_dynstr_append(pupds, buf);
575                         
576                         if(id->next)
577                                 BLI_dynstr_append(pupds, "|");
578                 }
579         }
580 }
581
582         /* Silly routine, the only difference between the one
583          * above is that it only adds items with a matching
584          * blocktype... this should be unified somehow... - zr
585          */
586 static void IPOnames_to_dyn_pupstring(DynStr *pupds, ListBase *lb, ID *link, short *nr, int blocktype)
587 {
588         ID *id;
589         int i, nids;
590         
591         for (id= lb->first, nids= 0; id; id= id->next) {
592                 Ipo *ipo= (Ipo*) id;
593                 
594                 if (ipo->blocktype==blocktype)
595                         nids++;
596         }
597         
598         if (nids>MAX_IDPUP) {
599                 BLI_dynstr_append(pupds, "DataBrowse %x-2");
600         } else {
601                 for (i=0, id= lb->first; id; id= id->next) {
602                         Ipo *ipo= (Ipo*) id;
603                         
604                         if (ipo->blocktype==blocktype) {
605                                 char buf[32];
606                         
607                                 if (id==link)
608                                         *nr= i+1;
609                                         
610                                 if (U.uiflag & USER_HIDE_DOT && id->name[2]=='.')
611                                         continue;
612
613                                 get_flags_for_id(id, buf);
614                                 
615                                 BLI_dynstr_append(pupds, buf);
616                                 BLI_dynstr_append(pupds, id->name+2);
617                                 sprintf(buf, "%%x%d", i+1);
618                                 BLI_dynstr_append(pupds, buf);
619                                 
620                                 if(id->next)
621                                         BLI_dynstr_append(pupds, "|");
622                                 
623                                 i++;
624                         }
625                 }
626         }
627 }
628
629 // used by headerbuttons.c buttons.c editobject.c editseq.c
630 // if nr==NULL no MAX_IDPUP, this for non-header browsing
631 void IDnames_to_pupstring(char **str, char *title, char *extraops, ListBase *lb, ID *link, short *nr)
632 {
633         DynStr *pupds= BLI_dynstr_new();
634
635         if (title) {
636                 BLI_dynstr_append(pupds, title);
637                 BLI_dynstr_append(pupds, "%t|");
638         }
639         
640         if (extraops) {
641                 BLI_dynstr_append(pupds, extraops);
642                 if (BLI_dynstr_get_len(pupds))
643                         BLI_dynstr_append(pupds, "|");
644         }
645
646         IDnames_to_dyn_pupstring(pupds, lb, link, nr);
647         
648         *str= BLI_dynstr_get_cstring(pupds);
649         BLI_dynstr_free(pupds);
650 }
651
652 // only used by headerbuttons.c
653 void IPOnames_to_pupstring(char **str, char *title, char *extraops, ListBase *lb, ID *link, short *nr, int blocktype)
654 {
655         DynStr *pupds= BLI_dynstr_new();
656         
657         if (title) {
658                 BLI_dynstr_append(pupds, title);
659                 BLI_dynstr_append(pupds, "%t|");
660         }
661         
662         if (extraops) {
663                 BLI_dynstr_append(pupds, extraops);
664                 if (BLI_dynstr_get_len(pupds))
665                         BLI_dynstr_append(pupds, "|");
666         }
667
668         IPOnames_to_dyn_pupstring(pupds, lb, link, nr, blocktype);      
669         
670         *str= BLI_dynstr_get_cstring(pupds);
671         BLI_dynstr_free(pupds);
672 }
673
674 // used by buttons.c library.c mball.c
675 void splitIDname(char *name, char *left, int *nr)
676 {
677         int a;
678         
679         *nr= 0;
680         strncpy(left, name, 21);
681         
682         a= strlen(name);
683         if(a>1 && name[a-1]=='.') return;
684         
685         while(a--) {
686                 if( name[a]=='.' ) {
687                         left[a]= 0;
688                         *nr= atol(name+a+1);
689                         return;
690                 }
691                 if( isdigit(name[a])==0 ) break;
692                 
693                 left[a]= 0;
694         }
695         strcpy(left, name);     
696 }
697
698 static void sort_alpha_id(ListBase *lb, ID *id)
699 {
700         ID *idtest;
701         
702         /* insert alphabetically */
703         if(lb->first!=lb->last) {
704                 BLI_remlink(lb, id);
705                 
706                 idtest= lb->first;
707                 while(idtest) {
708                         if(BLI_strcasecmp(idtest->name, id->name)>0 || idtest->lib) {
709                                 BLI_insertlinkbefore(lb, idtest, id);
710                                 break;
711                         }
712                         idtest= idtest->next;
713                 }
714                 /* as last */
715                 if(idtest==0) {
716                         BLI_addtail(lb, id);
717                 }
718         }
719         
720 }
721
722 int new_id(ListBase *lb, ID *id, char *tname)
723 /* only for local blocks: external en indirect blocks already have a unique ID */
724 /* return 1: created a new name */
725 {
726         ID *idtest;
727         int nr= 0, nrtest, maxtest=32, a;
728         char aname[32], *name, left[24], leftest[24], in_use[32];
729         
730         /* - split name
731          * - search
732          */
733
734         if(id->lib) return 0;
735
736         if(tname==0) name= id->name+2;
737         else {
738                 /* tname can be const */
739                 strncpy(aname, tname, 21);
740                 name= aname;
741                 
742                 if( strlen(name) > 21 ) name[21]= 0;
743         }
744
745         if(lb==0) lb= wich_libbase(G.main, GS(id->name));
746
747         /* phase 1: id already exists? */
748         idtest= lb->first;
749         while(idtest) {
750         
751                 if(id!=idtest && idtest->lib==0) {
752                         
753                         /* do not test alphabetic! */
754                         /* optimized */
755                         if( idtest->name[2] == name[0] ) {
756                                 if(strcmp(name, idtest->name+2)==0) break;
757                         }
758                 }
759                 
760                 idtest= idtest->next;
761         }       
762
763         /* if there is no double return */
764         if(idtest==0) {
765                 strcpy(id->name+2, name);
766                 return 0;
767         }
768         
769         memset(in_use, 0, maxtest);
770
771         splitIDname(name, left, &nr);
772         if(nr>999 && strlen(left)>16) left[16]= 0;
773         else if(strlen(left)>17) left[17]= 0;
774
775
776         idtest= lb->first;
777         while(idtest) {
778         
779                 if(id!=idtest && idtest->lib==0) {
780                         
781                         splitIDname(idtest->name+2, leftest, &nrtest);
782                         if(strcmp(left, leftest)==0) {
783                                 
784                                 if(nrtest<maxtest) in_use[nrtest]= 1;
785                                 if(nr <= nrtest) nr= nrtest+1;
786                         }
787                 }
788                 
789                 idtest= idtest->next;
790         }
791         
792         for(a=0; a<maxtest; a++) {
793                 if(a>=nr) break;
794                 if( in_use[a]==0 ) {
795                         nr= a;
796                         break;
797                 }
798         }
799         
800         if(nr==0) sprintf(id->name+2, "%s", left);
801         else {
802                 if (nr >= 1000 && strlen(left) > 16) {
803                         // this would overflow name buffer
804                         left[16]= 0;
805                         return (new_id(lb, id, left));
806                 }
807                 /* this format specifier is from hell... */
808                 sprintf(id->name+2, "%s.%.3d", left, nr);
809         }
810         
811         sort_alpha_id(lb, id);  
812
813         return 1;
814 }
815
816 // next to indirect usage in read/writefile also in editobject.c scene.c
817 void clear_id_newpoins()
818 {
819         ListBase *lbarray[MAX_LIBARRAY];
820         ID *id;
821         int a;
822
823         a= set_listbasepointers(G.main, lbarray);
824         while(a--) {
825                 id= lbarray[a]->first;
826                 while(id) {
827                         id->newid= 0;
828                         id->flag &= ~LIB_NEW;
829                         id= id->next;
830                 }
831         }
832 }
833
834 /* if lib!=NULL, only all from lib local */
835 void all_local(Library *lib)
836 {
837         ListBase *lbarray[MAX_LIBARRAY], tempbase={0, 0};
838         ID *id, *idn;
839         int a;
840
841         a= set_listbasepointers(G.main, lbarray);
842         while(a--) {
843                 id= lbarray[a]->first;
844                 
845                 while(id) {
846                         id->newid= NULL;
847                         idn= id->next;          /* id is possibly being inserted again */
848                         
849                         if(id->flag & (LIB_EXTERN|LIB_INDIRECT|LIB_NEW)) {
850                                 if(lib==NULL || id->lib==lib) {
851                                         id->flag &= ~(LIB_EXTERN|LIB_INDIRECT|LIB_NEW);
852
853                                         if(id->lib) {
854                                                 id->lib= NULL;
855                                                 new_id(lbarray[a], id, 0);      /* new_id only does it with double names */
856                                                 sort_alpha_id(lbarray[a], id);
857                                         }
858                                 }
859                         }
860                         id= idn;
861                 }
862                 
863                 /* patch2: make it aphabetically */
864                 while( (id=tempbase.first) ) {
865                         BLI_remlink(&tempbase, id);
866                         BLI_addtail(lbarray[a], id);
867                         new_id(lbarray[a], id, 0);
868                 }
869         }
870 }
871
872
873 void test_idbutton(char *name)
874 {
875         /* called from buttons: when name already exists: call new_id */
876         ListBase *lb;
877         ID *idtest;
878         
879
880         lb= wich_libbase(G.main, GS(name-2) );
881         if(lb==0) return;
882         
883         /* search for id */
884         idtest= lb->first;
885         while(idtest) {
886                 if( strcmp(idtest->name+2, name)==0) break;
887                 idtest= idtest->next;
888         }
889
890         if(idtest) if( new_id(lb, idtest, name)==0 ) sort_alpha_id(lb, idtest);
891 }
892
893 void rename_id(ID *id, char *name)
894 {
895         ListBase *lb;
896         
897         strncpy(id->name+2, name, 21);
898         lb= wich_libbase(G.main, GS(id->name) );
899         
900         new_id(lb, id, name);                           
901 }
902