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