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