4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
31 * Contains management of ID's and libraries
32 * allocate and free of all library data
47 #include "MEM_guardedalloc.h"
49 /* all types are needed here, in order to do memory operations */
51 #include "DNA_listBase.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_object_types.h"
54 #include "DNA_mesh_types.h"
55 #include "DNA_lattice_types.h"
56 #include "DNA_curve_types.h"
57 #include "DNA_meta_types.h"
58 #include "DNA_material_types.h"
59 #include "DNA_texture_types.h"
60 #include "DNA_image_types.h"
61 #include "DNA_wave_types.h"
62 #include "DNA_lamp_types.h"
63 #include "DNA_camera_types.h"
64 #include "DNA_ipo_types.h"
65 #include "DNA_key_types.h"
66 #include "DNA_world_types.h"
67 #include "DNA_screen_types.h"
68 #include "DNA_vfont_types.h"
69 #include "DNA_text_types.h"
70 #include "DNA_sound_types.h"
71 #include "DNA_group_types.h"
72 #include "DNA_armature_types.h"
73 #include "DNA_action_types.h"
74 #include "DNA_userdef_types.h"
75 #include "DNA_node_types.h"
76 #include "DNA_nla_types.h"
77 #include "DNA_effect_types.h"
78 #include "DNA_brush_types.h"
79 #include "DNA_particle_types.h"
80 #include "DNA_space_types.h"
81 #include "DNA_windowmanager_types.h"
82 #include "DNA_anim_types.h"
83 #include "DNA_gpencil_types.h"
85 #include "BLI_blenlib.h"
86 #include "BLI_dynstr.h"
88 #include "BKE_animsys.h"
89 #include "BKE_context.h"
90 #include "BKE_library.h"
92 #include "BKE_global.h"
93 #include "BKE_sound.h"
94 #include "BKE_object.h"
95 #include "BKE_screen.h"
96 #include "BKE_script.h"
98 #include "BKE_material.h"
99 #include "BKE_curve.h"
100 #include "BKE_mball.h"
101 #include "BKE_text.h"
102 #include "BKE_texture.h"
103 #include "BKE_scene.h"
104 #include "BKE_icons.h"
105 #include "BKE_image.h"
108 #include "BKE_world.h"
109 #include "BKE_font.h"
110 #include "BKE_group.h"
111 #include "BKE_lattice.h"
112 #include "BKE_armature.h"
113 #include "BKE_action.h"
114 #include "BKE_node.h"
115 #include "BKE_effect.h"
116 #include "BKE_brush.h"
117 #include "BKE_idprop.h"
118 #include "BKE_particle.h"
119 #include "BKE_gpencil.h"
120 #include "BKE_fcurve.h"
122 #define MAX_IDPUP 60 /* was 24 */
124 /* GS reads the memory pointed at in a specific ordering.
125 only use this definition, makes little and big endian systems
126 work fine, in conjunction with MAKE_ID */
129 #define GS(a) (*((short *)(a)))
131 /* ************* general ************************ */
133 void id_lib_extern(ID *id)
136 if(id->flag & LIB_INDIRECT) {
137 id->flag -= LIB_INDIRECT;
138 id->flag |= LIB_EXTERN;
143 void id_us_plus(ID *id)
147 if(id->flag & LIB_INDIRECT) {
148 id->flag -= LIB_INDIRECT;
149 id->flag |= LIB_EXTERN;
154 void id_us_min(ID *id)
160 int id_make_local(ID *id, int test)
162 if(id->flag & LIB_INDIRECT)
165 switch(GS(id->name)) {
167 return 0; /* not implemented */
169 return 0; /* can't be linked */
171 if(!test) make_local_object((Object*)id);
175 make_local_mesh((Mesh*)id);
176 make_local_key(((Mesh*)id)->key);
181 make_local_curve((Curve*)id);
182 make_local_key(((Curve*)id)->key);
186 if(!test) make_local_mball((MetaBall*)id);
189 if(!test) make_local_material((Material*)id);
192 if(!test) make_local_texture((Tex*)id);
195 return 0; /* not implemented */
197 return 0; /* deprecated */
200 make_local_lattice((Lattice*)id);
201 make_local_key(((Lattice*)id)->key);
205 if(!test) make_local_lamp((Lamp*)id);
208 if(!test) make_local_camera((Camera*)id);
211 return 0; /* deprecated */
213 if(!test) make_local_key((Key*)id);
216 if(!test) make_local_world((World*)id);
219 return 0; /* can't be linked */
221 return 0; /* not implemented */
223 return 0; /* not implemented */
225 return 0; /* deprecated */
227 return 0; /* not implemented */
229 return 0; /* not implemented */
231 if(!test) make_local_armature((bArmature*)id);
234 if(!test) make_local_action((bAction*)id);
237 return 0; /* not implemented */
239 if(!test) make_local_brush((Brush*)id);
242 if(!test) make_local_particlesettings((ParticleSettings*)id);
245 return 0; /* can't be linked */
247 return 0; /* not implemented */
253 int id_copy(ID *id, ID **newid, int test)
255 if(!test) *newid= NULL;
258 * - make shallow copy, only this ID block
259 * - id.us of the new ID is set to 1 */
260 switch(GS(id->name)) {
262 return 0; /* can't be copied from here */
264 return 0; /* can't be copied from here */
266 if(!test) *newid= (ID*)copy_object((Object*)id);
269 if(!test) *newid= (ID*)copy_mesh((Mesh*)id);
272 if(!test) *newid= (ID*)copy_curve((Curve*)id);
275 if(!test) *newid= (ID*)copy_mball((MetaBall*)id);
278 if(!test) *newid= (ID*)copy_material((Material*)id);
281 if(!test) *newid= (ID*)copy_texture((Tex*)id);
284 return 0; /* not implemented */
286 return 0; /* deprecated */
288 if(!test) *newid= (ID*)copy_lattice((Lattice*)id);
291 if(!test) *newid= (ID*)copy_lamp((Lamp*)id);
294 if(!test) *newid= (ID*)copy_camera((Camera*)id);
297 return 0; /* deprecated */
299 if(!test) *newid= (ID*)copy_key((Key*)id);
302 if(!test) *newid= (ID*)copy_world((World*)id);
305 return 0; /* can't be copied from here */
307 return 0; /* not implemented */
309 if(!test) *newid= (ID*)copy_text((Text*)id);
312 return 0; /* deprecated */
314 return 0; /* not implemented */
316 if(!test) *newid= (ID*)copy_group((Group*)id);
319 if(!test) *newid= (ID*)copy_armature((bArmature*)id);
322 if(!test) *newid= (ID*)copy_action((bAction*)id);
325 if(!test) *newid= (ID*)ntreeCopyTree((bNodeTree*)id, 0);
328 if(!test) *newid= (ID*)copy_brush((Brush*)id);
331 if(!test) *newid= (ID*)psys_copy_settings((ParticleSettings*)id);
334 return 0; /* can't be copied from here */
336 return 0; /* not implemented */
342 int id_unlink(ID *id, int test)
344 Main *mainlib= G.main;
347 switch(GS(id->name)) {
350 unlink_text(mainlib, (Text*)id);
354 unlink_group((Group*)id);
358 unlink_object(NULL, (Object*)id);
365 lb= wich_libbase(mainlib, GS(id->name));
366 free_libblock(lb, id);
374 ListBase *wich_libbase(Main *mainlib, short type)
378 return &(mainlib->scene);
380 return &(mainlib->library);
382 return &(mainlib->object);
384 return &(mainlib->mesh);
386 return &(mainlib->curve);
388 return &(mainlib->mball);
390 return &(mainlib->mat);
392 return &(mainlib->tex);
394 return &(mainlib->image);
396 return &(mainlib->wave);
398 return &(mainlib->latt);
400 return &(mainlib->lamp);
402 return &(mainlib->camera);
404 return &(mainlib->ipo);
406 return &(mainlib->key);
408 return &(mainlib->world);
410 return &(mainlib->screen);
412 return &(mainlib->vfont);
414 return &(mainlib->text);
416 return &(mainlib->script);
418 return &(mainlib->sound);
420 return &(mainlib->group);
422 return &(mainlib->armature);
424 return &(mainlib->action);
426 return &(mainlib->nodetree);
428 return &(mainlib->brush);
430 return &(mainlib->particle);
432 return &(mainlib->wm);
434 return &(mainlib->gpencil);
439 /* Flag all ids in listbase */
440 void flag_listbase_ids(ListBase *lb, short flag, short value)
444 for(id= lb->first; id; id= id->next) id->flag |= flag;
447 for(id= lb->first; id; id= id->next) id->flag &= flag;
451 /* Flag all ids in listbase */
452 void flag_all_listbases_ids(short flag, short value)
454 ListBase *lbarray[MAX_LIBARRAY];
456 a= set_listbasepointers(G.main, lbarray);
457 while(a--) flag_listbase_ids(lbarray[a], flag, value);
460 void recalc_all_library_objects(Main *main)
464 /* flag for full recalc */
465 for(ob=main->object.first; ob; ob=ob->id.next)
467 ob->recalc |= OB_RECALC;
470 /* note: MAX_LIBARRAY define should match this code */
471 int set_listbasepointers(Main *main, ListBase **lb)
475 /* BACKWARDS! also watch order of free-ing! (mesh<->mat) */
477 lb[a++]= &(main->ipo);
478 lb[a++]= &(main->action); // xxx moved here to avoid problems when freeing with animato (aligorith)
479 lb[a++]= &(main->key);
480 lb[a++]= &(main->nodetree);
481 lb[a++]= &(main->image);
482 lb[a++]= &(main->tex);
483 lb[a++]= &(main->mat);
484 lb[a++]= &(main->vfont);
486 /* Important!: When adding a new object type,
487 * the specific data should be inserted here
490 lb[a++]= &(main->armature);
492 lb[a++]= &(main->mesh);
493 lb[a++]= &(main->curve);
494 lb[a++]= &(main->mball);
496 lb[a++]= &(main->wave);
497 lb[a++]= &(main->latt);
498 lb[a++]= &(main->lamp);
499 lb[a++]= &(main->camera);
501 lb[a++]= &(main->text);
502 lb[a++]= &(main->sound);
503 lb[a++]= &(main->group);
504 lb[a++]= &(main->brush);
505 lb[a++]= &(main->script);
506 lb[a++]= &(main->particle);
508 lb[a++]= &(main->world);
509 lb[a++]= &(main->screen);
510 lb[a++]= &(main->object);
511 lb[a++]= &(main->scene);
512 lb[a++]= &(main->library);
513 lb[a++]= &(main->wm);
514 lb[a++]= &(main->gpencil);
521 /* *********** ALLOC AND FREE *****************
523 free_libblock(ListBase *lb, ID *id )
524 provide a list-basis and datablock, but only ID is read
526 void *alloc_libblock(ListBase *lb, type, name)
527 inserts in list and returns a new ID
529 ***************************** */
531 static ID *alloc_libblock_notest(short type)
537 id= MEM_callocN(sizeof(Scene), "scene");
540 id= MEM_callocN(sizeof(Library), "library");
543 id= MEM_callocN(sizeof(Object), "object");
546 id= MEM_callocN(sizeof(Mesh), "mesh");
549 id= MEM_callocN(sizeof(Curve), "curve");
552 id= MEM_callocN(sizeof(MetaBall), "mball");
555 id= MEM_callocN(sizeof(Material), "mat");
558 id= MEM_callocN(sizeof(Tex), "tex");
561 id= MEM_callocN(sizeof(Image), "image");
564 id= MEM_callocN(sizeof(Wave), "wave");
567 id= MEM_callocN(sizeof(Lattice), "latt");
570 id= MEM_callocN(sizeof(Lamp), "lamp");
573 id= MEM_callocN(sizeof(Camera), "camera");
576 id= MEM_callocN(sizeof(Ipo), "ipo");
579 id= MEM_callocN(sizeof(Key), "key");
582 id= MEM_callocN(sizeof(World), "world");
585 id= MEM_callocN(sizeof(bScreen), "screen");
588 id= MEM_callocN(sizeof(VFont), "vfont");
591 id= MEM_callocN(sizeof(Text), "text");
594 //XXX id= MEM_callocN(sizeof(Script), "script");
597 id= MEM_callocN(sizeof(bSound), "sound");
600 id= MEM_callocN(sizeof(Group), "group");
603 id = MEM_callocN(sizeof(bArmature), "armature");
606 id = MEM_callocN(sizeof(bAction), "action");
609 id = MEM_callocN(sizeof(bNodeTree), "nodetree");
612 id = MEM_callocN(sizeof(Brush), "brush");
615 id = MEM_callocN(sizeof(ParticleSettings), "ParticleSettings");
618 id = MEM_callocN(sizeof(wmWindowManager), "Window manager");
621 id = MEM_callocN(sizeof(bGPdata), "Grease Pencil");
627 /* used everywhere in blenkernel and text.c */
628 void *alloc_libblock(ListBase *lb, short type, const char *name)
632 id= alloc_libblock_notest(type);
637 *( (short *)id->name )= type;
638 new_id(lb, id, name);
639 /* alphabetic insterion: is in new_id */
644 /* by spec, animdata is first item after ID */
645 /* and, trust that BKE_animdata_from_id() will only find AnimData for valid ID-types */
646 static void id_copy_animdata(ID *id)
648 AnimData *adt= BKE_animdata_from_id(id);
651 IdAdtTemplate *iat = (IdAdtTemplate *)id;
652 iat->adt= BKE_copy_animdata(iat->adt);
656 /* material nodes use this since they are not treated as libdata */
657 void copy_libblock_data(ID *id, const ID *id_from)
659 if (id_from->properties)
660 id->properties = IDP_CopyProperty(id_from->properties);
662 /* the duplicate should get a copy of the animdata */
663 id_copy_animdata(id);
666 /* used everywhere in blenkernel */
667 void *copy_libblock(void *rt)
676 lb= wich_libbase(G.main, GS(id->name));
677 idn= alloc_libblock(lb, GS(id->name), id->name+2);
680 printf("ERROR: Illegal ID name for %s (Crashing now)\n", id->name);
683 idn_len= MEM_allocN_len(idn);
684 if(idn_len - sizeof(ID) > 0) {
687 memcpy(cpn+sizeof(ID), cp+sizeof(ID), idn_len - sizeof(ID));
691 idn->flag |= LIB_NEW;
693 copy_libblock_data(idn, id);
698 static void free_library(Library *lib)
700 /* no freeing needed for libraries yet */
703 static void (*free_windowmanager_cb)(bContext *, wmWindowManager *)= NULL;
705 void set_free_windowmanager_cb(void (*func)(bContext *C, wmWindowManager *) )
707 free_windowmanager_cb= func;
710 void animdata_dtar_clear_cb(ID *id, AnimData *adt, void *userdata)
712 ChannelDriver *driver;
715 /* find the driver this belongs to and update it */
716 for (fcu=adt->drivers.first; fcu; fcu=fcu->next) {
721 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
722 DRIVER_TARGETS_USED_LOOPER(dvar)
724 if (dtar->id == userdata)
727 DRIVER_TARGETS_LOOPER_END
734 /* used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c */
735 void free_libblock(ListBase *lb, void *idv)
739 switch( GS(id->name) ) { /* GetShort from util.h */
741 free_scene((Scene *)id);
744 free_library((Library *)id);
747 free_object((Object *)id);
750 free_mesh((Mesh *)id);
753 free_curve((Curve *)id);
756 free_mball((MetaBall *)id);
759 free_material((Material *)id);
762 free_texture((Tex *)id);
765 free_image((Image *)id);
771 free_lattice((Lattice *)id);
774 free_lamp((Lamp *)id);
777 free_camera((Camera*) id);
786 free_world((World *)id);
789 free_screen((bScreen *)id);
792 free_vfont((VFont *)id);
795 free_text((Text *)id);
798 //XXX free_script((Script *)id);
801 sound_free((bSound*)id);
804 free_group_objects((Group *)id);
807 free_armature((bArmature *)id);
810 free_action((bAction *)id);
813 ntreeFreeTree((bNodeTree *)id);
816 free_brush((Brush *)id);
819 psys_free_settings((ParticleSettings *)id);
822 if(free_windowmanager_cb)
823 free_windowmanager_cb(NULL, (wmWindowManager *)id);
826 free_gpencil_data((bGPdata *)id);
830 if (id->properties) {
831 IDP_FreeProperty(id->properties);
832 MEM_freeN(id->properties);
837 /* this ID may be a driver target! */
838 BKE_animdata_main_cb(G.main, animdata_dtar_clear_cb, (void *)id);
843 void free_libblock_us(ListBase *lb, void *idv) /* test users */
850 if(id->lib) printf("ERROR block %s %s users %d\n", id->lib->name, id->name, id->us);
851 else printf("ERROR block %s users %d\n", id->name, id->us);
854 if( GS(id->name)==ID_OB ) unlink_object(NULL, (Object *)id);
856 free_libblock(lb, id);
861 void free_main(Main *mainvar)
863 /* also call when reading a file, erase all, etc */
864 ListBase *lbarray[MAX_LIBARRAY];
867 a= set_listbasepointers(mainvar, lbarray);
869 ListBase *lb= lbarray[a];
872 while ( (id= lb->first) ) {
873 free_libblock(lb, id);
880 /* ***************** ID ************************ */
883 ID *find_id(char *type, char *name) /* type: "OB" or "MA" etc */
885 ListBase *lb= wich_libbase(G.main, GS(type));
886 return BLI_findstring(lb, name, offsetof(ID, name) + 2);
889 static void get_flags_for_id(ID *id, char *buf)
891 int isfake= id->flag & LIB_FAKEUSER;
893 /* Writeout the flags for the entry, note there
894 * is a small hack that writes 5 spaces instead
895 * of 4 if no flags are displayed... this makes
896 * things usually line up ok - better would be
897 * to have that explicit, oh well - zr
900 if(GS(id->name)==ID_MA)
901 isnode= ((Material *)id)->use_nodes;
902 if(GS(id->name)==ID_TE)
903 isnode= ((Tex *)id)->use_nodes;
906 sprintf(buf, "-1W ");
907 else if (!id->lib && !isfake && id->us && !isnode)
910 sprintf(buf, "%c%cN%c ", id->lib?'L':' ', isfake?'F':' ', (id->us==0)?'O':' ');
912 sprintf(buf, "%c%c%c ", id->lib?'L':' ', isfake?'F':' ', (id->us==0)?'O':' ');
915 #define IDPUP_NO_VIEWER 1
917 static void IDnames_to_dyn_pupstring(DynStr *pupds, ListBase *lb, ID *link, short *nr, int hideflag)
919 int i, nids= BLI_countlist(lb);
923 if (nr && nids>MAX_IDPUP) {
924 BLI_dynstr_append(pupds, "DataBrowse %x-2");
929 for (i=0, id= lb->first; id; id= id->next, i++) {
932 if (nr && id==link) *nr= i+1;
934 if (U.uiflag & USER_HIDE_DOT && id->name[2]=='.')
936 if (hideflag & IDPUP_NO_VIEWER)
937 if (GS(id->name)==ID_IM)
938 if ( ((Image *)id)->source==IMA_SRC_VIEWER )
941 get_flags_for_id(id, buf);
943 BLI_dynstr_append(pupds, buf);
944 BLI_dynstr_append(pupds, id->name+2);
945 sprintf(buf, "%%x%d", i+1);
946 BLI_dynstr_append(pupds, buf);
951 case ID_MA: /* fall through */
952 case ID_TE: /* fall through */
953 case ID_IM: /* fall through */
954 case ID_WO: /* fall through */
955 case ID_LA: /* fall through */
956 sprintf(buf, "%%i%d", BKE_icon_getid(id) );
957 BLI_dynstr_append(pupds, buf);
964 BLI_dynstr_append(pupds, "|");
970 /* used by headerbuttons.c buttons.c editobject.c editseq.c */
971 /* if nr==NULL no MAX_IDPUP, this for non-header browsing */
972 void IDnames_to_pupstring(char **str, char *title, char *extraops, ListBase *lb, ID *link, short *nr)
974 DynStr *pupds= BLI_dynstr_new();
977 BLI_dynstr_append(pupds, title);
978 BLI_dynstr_append(pupds, "%t|");
982 BLI_dynstr_append(pupds, extraops);
983 if (BLI_dynstr_get_len(pupds))
984 BLI_dynstr_append(pupds, "|");
987 IDnames_to_dyn_pupstring(pupds, lb, link, nr, 0);
989 *str= BLI_dynstr_get_cstring(pupds);
990 BLI_dynstr_free(pupds);
993 /* skips viewer images */
994 void IMAnames_to_pupstring(char **str, char *title, char *extraops, ListBase *lb, ID *link, short *nr)
996 DynStr *pupds= BLI_dynstr_new();
999 BLI_dynstr_append(pupds, title);
1000 BLI_dynstr_append(pupds, "%t|");
1004 BLI_dynstr_append(pupds, extraops);
1005 if (BLI_dynstr_get_len(pupds))
1006 BLI_dynstr_append(pupds, "|");
1009 IDnames_to_dyn_pupstring(pupds, lb, link, nr, IDPUP_NO_VIEWER);
1011 *str= BLI_dynstr_get_cstring(pupds);
1012 BLI_dynstr_free(pupds);
1016 /* used by buttons.c library.c mball.c */
1017 void splitIDname(char *name, char *left, int *nr)
1022 strncpy(left, name, 21);
1025 if(a>1 && name[a-1]=='.') return;
1028 if( name[a]=='.' ) {
1030 *nr= atol(name+a+1);
1033 if( isdigit(name[a])==0 ) break;
1040 static void sort_alpha_id(ListBase *lb, ID *id)
1044 /* insert alphabetically */
1045 if(lb->first!=lb->last) {
1046 BLI_remlink(lb, id);
1050 if(BLI_strcasecmp(idtest->name, id->name)>0 || idtest->lib) {
1051 BLI_insertlinkbefore(lb, idtest, id);
1054 idtest= idtest->next;
1058 BLI_addtail(lb, id);
1065 * Check to see if there is an ID with the same name as 'name'.
1066 * Returns the ID if so, if not, returns NULL
1068 static ID *is_dupid(ListBase *lb, ID *id, char *name)
1072 for( idtest = lb->first; idtest; idtest = idtest->next ) {
1073 /* if idtest is not a lib */
1074 if( id != idtest && idtest->lib == NULL ) {
1075 /* do not test alphabetic! */
1077 if( idtest->name[2] == name[0] ) {
1078 if(strcmp(name, idtest->name+2)==0) break;
1087 * Check to see if an ID name is already used, and find a new one if so.
1088 * Return 1 if created a new name (returned in name).
1090 * Normally the ID that's being check is already in the ListBase, so ID *id
1091 * points at the new entry. The Python Library module needs to know what
1092 * the name of a datablock will be before it is appended; in this case ID *id
1096 static int check_for_dupid(ListBase *lb, ID *id, char *name)
1099 int nr= 0, nrtest, a;
1100 const int maxtest=32;
1101 char left[32], leftest[32], in_use[32];
1103 /* make sure input name is terminated properly */
1104 /* if( strlen(name) > 21 ) name[21]= 0; */
1105 /* removed since this is only ever called from one place - campbell */
1109 /* phase 1: id already exists? */
1110 idtest = is_dupid(lb, id, name);
1112 /* if there is no double, done */
1113 if( idtest == NULL ) return 0;
1115 /* we have a dup; need to make a new name */
1116 /* quick check so we can reuse one of first 32 ids if vacant */
1117 memset(in_use, 0, maxtest);
1119 /* get name portion, number portion ("name.number") */
1120 splitIDname( name, left, &nr);
1122 /* if new name will be too long, truncate it */
1123 if(nr>999 && strlen(left)>16) left[16]= 0;
1124 else if(strlen(left)>17) left[17]= 0;
1126 for( idtest = lb->first; idtest; idtest = idtest->next ) {
1127 if( id != idtest && idtest->lib == NULL ) {
1128 splitIDname(idtest->name+2, leftest, &nrtest);
1129 /* if base names match... */
1131 if( *left == *leftest && strcmp(left, leftest)==0 ) {
1132 if(nrtest < maxtest)
1133 in_use[nrtest]= 1; /* mark as used */
1135 nr= nrtest+1; /* track largest unused */
1140 /* decide which value of nr to use */
1141 for(a=0; a<maxtest; a++) {
1142 if(a>=nr) break; /* stop when we've check up to biggest */
1143 if( in_use[a]==0 ) { /* found an unused value */
1149 /* If the original name has no numeric suffix,
1150 * rather than just chopping and adding numbers,
1151 * shave off the end chars until we have a unique name */
1153 int len = strlen(name)-1;
1154 idtest= is_dupid(lb, id, name);
1156 while (idtest && len> 1) {
1158 idtest= is_dupid(lb, id, name);
1160 if (idtest == NULL) return 1;
1161 /* otherwise just continue and use a number suffix */
1164 if(nr > 999 && strlen(left) > 16) {
1165 /* this would overflow name buffer */
1167 strcpy( name, left );
1170 /* this format specifier is from hell... */
1171 sprintf(name, "%s.%.3d", left, nr);
1178 * Only for local blocks: external en indirect blocks already have a
1181 * return 1: created a new name
1184 int new_id(ListBase *lb, ID *id, const char *tname)
1189 /* if library, don't rename */
1190 if(id->lib) return 0;
1192 /* if no libdata given, look up based on ID */
1193 if(lb==NULL) lb= wich_libbase(G.main, GS(id->name));
1195 /* if no name given, use name of current ID
1196 * else make a copy (tname args can be const) */
1200 strncpy(name, tname, sizeof(name)-1);
1202 /* if result > 21, strncpy don't put the final '\0' to name.
1203 * easier to assign each time then to check if its needed */
1204 name[sizeof(name)-1]= 0;
1207 strcpy(name, ID_FALLBACK_NAME);
1209 result = check_for_dupid(lb, id, name);
1210 strcpy(id->name+2, name);
1212 /* This was in 2.43 and previous releases
1213 * however all data in blender should be sorted, not just duplicate names
1214 * sorting should not hurt, but noting just incause it alters the way other
1215 * functions work, so sort every time */
1217 sort_alpha_id(lb, id);*/
1219 sort_alpha_id(lb, id);
1224 /* next to indirect usage in read/writefile also in editobject.c scene.c */
1225 void clear_id_newpoins()
1227 ListBase *lbarray[MAX_LIBARRAY];
1231 a= set_listbasepointers(G.main, lbarray);
1233 id= lbarray[a]->first;
1236 id->flag &= ~LIB_NEW;
1242 /* only for library fixes */
1243 static void image_fix_relative_path(Image *ima)
1245 if(ima->id.lib==NULL) return;
1246 if(strncmp(ima->name, "//", 2)==0) {
1247 BLI_path_abs(ima->name, ima->id.lib->filename);
1248 BLI_path_rel(ima->name, G.sce);
1252 #define LIBTAG(a) if(a && a->id.lib) {a->id.flag &=~LIB_INDIRECT; a->id.flag |= LIB_EXTERN;}
1254 static void lib_indirect_test_id(ID *id)
1260 if(GS(id->name)==ID_OB) {
1261 Object *ob= (Object *)id;
1262 bActionStrip *strip;
1267 // XXX old animation system! --------------------------------------
1268 for (strip=ob->nlastrips.first; strip; strip=strip->next){
1269 LIBTAG(strip->object);
1273 // XXX: new animation system needs something like this?
1275 for(a=0; a<ob->totcol; a++) {
1279 LIBTAG(ob->dup_group);
1287 void tag_main(struct Main *mainvar, int tag)
1289 ListBase *lbarray[MAX_LIBARRAY];
1293 a= set_listbasepointers(mainvar, lbarray);
1295 for(id= lbarray[a]->first; id; id= id->next) {
1296 if(tag) id->flag |= LIB_DOIT;
1297 else id->flag &= ~LIB_DOIT;
1302 /* if lib!=NULL, only all from lib local */
1303 void all_local(Library *lib, int untagged_only)
1305 ListBase *lbarray[MAX_LIBARRAY], tempbase={0, 0};
1309 a= set_listbasepointers(G.main, lbarray);
1311 id= lbarray[a]->first;
1315 idn= id->next; /* id is possibly being inserted again */
1317 /* The check on the second line (LIB_PRE_EXISTING) is done so its
1318 * possible to tag data you dont want to be made local, used for
1319 * appending data, so any libdata alredy linked wont become local
1320 * (very nasty to discover all your links are lost after appending)
1322 if(id->flag & (LIB_EXTERN|LIB_INDIRECT|LIB_NEW) &&
1323 (untagged_only==0 || !(id->flag & LIB_PRE_EXISTING)))
1325 if(lib==NULL || id->lib==lib) {
1326 id->flag &= ~(LIB_EXTERN|LIB_INDIRECT|LIB_NEW);
1329 /* relative file patch */
1330 if(GS(id->name)==ID_IM)
1331 image_fix_relative_path((Image *)id);
1334 new_id(lbarray[a], id, 0); /* new_id only does it with double names */
1335 sort_alpha_id(lbarray[a], id);
1342 /* patch2: make it aphabetically */
1343 while( (id=tempbase.first) ) {
1344 BLI_remlink(&tempbase, id);
1345 BLI_addtail(lbarray[a], id);
1346 new_id(lbarray[a], id, 0);
1350 /* patch 3: make sure library data isn't indirect falsely... */
1351 a= set_listbasepointers(G.main, lbarray);
1353 for(id= lbarray[a]->first; id; id=id->next)
1354 lib_indirect_test_id(id);
1359 void test_idbutton(char *name)
1361 /* called from buttons: when name already exists: call new_id */
1366 lb= wich_libbase(G.main, GS(name-2) );
1370 idtest= BLI_findstring(lb, name, offsetof(ID, name) + 2);
1372 if(idtest) if( new_id(lb, idtest, name)==0 ) sort_alpha_id(lb, idtest);
1375 void text_idbutton(struct ID *id, char *text)
1378 if(GS(id->name)==ID_SCE)
1379 strcpy(text, "SCE: ");
1380 else if(GS(id->name)==ID_SCE)
1381 strcpy(text, "SCR: ");
1382 else if(GS(id->name)==ID_MA && ((Material*)id)->use_nodes)
1383 strcpy(text, "NT: ");
1385 text[0]= id->name[0];
1386 text[1]= id->name[1];
1396 void rename_id(ID *id, char *name)
1400 strncpy(id->name+2, name, 21);
1401 lb= wich_libbase(G.main, GS(id->name) );
1403 new_id(lb, id, name);