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