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
43 #include "MEM_guardedalloc.h"
45 /* all types are needed here, in order to do memory operations */
46 #include "DNA_anim_types.h"
47 #include "DNA_armature_types.h"
48 #include "DNA_brush_types.h"
49 #include "DNA_camera_types.h"
50 #include "DNA_group_types.h"
51 #include "DNA_ipo_types.h"
52 #include "DNA_key_types.h"
53 #include "DNA_lamp_types.h"
54 #include "DNA_lattice_types.h"
55 #include "DNA_material_types.h"
56 #include "DNA_mesh_types.h"
57 #include "DNA_meta_types.h"
58 #include "DNA_nla_types.h"
59 #include "DNA_node_types.h"
60 #include "DNA_scene_types.h"
61 #include "DNA_screen_types.h"
62 #include "DNA_sound_types.h"
63 #include "DNA_text_types.h"
64 #include "DNA_vfont_types.h"
65 #include "DNA_windowmanager_types.h"
66 #include "DNA_world_types.h"
67 #include "DNA_gpencil_types.h"
70 #include "BLI_blenlib.h"
71 #include "BLI_dynstr.h"
73 #include "BKE_animsys.h"
74 #include "BKE_context.h"
75 #include "BKE_library.h"
77 #include "BKE_global.h"
78 #include "BKE_sound.h"
79 #include "BKE_object.h"
80 #include "BKE_screen.h"
82 #include "BKE_material.h"
83 #include "BKE_curve.h"
84 #include "BKE_mball.h"
86 #include "BKE_texture.h"
87 #include "BKE_scene.h"
88 #include "BKE_icons.h"
89 #include "BKE_image.h"
92 #include "BKE_world.h"
94 #include "BKE_group.h"
95 #include "BKE_lattice.h"
96 #include "BKE_armature.h"
97 #include "BKE_action.h"
99 #include "BKE_brush.h"
100 #include "BKE_idprop.h"
101 #include "BKE_particle.h"
102 #include "BKE_gpencil.h"
103 #include "BKE_fcurve.h"
105 #define MAX_IDPUP 60 /* was 24 */
107 /* GS reads the memory pointed at in a specific ordering.
108 only use this definition, makes little and big endian systems
109 work fine, in conjunction with MAKE_ID */
112 #define GS(a) (*((short *)(a)))
114 /* ************* general ************************ */
116 void id_lib_extern(ID *id)
119 if(id->flag & LIB_INDIRECT) {
120 id->flag -= LIB_INDIRECT;
121 id->flag |= LIB_EXTERN;
126 void id_us_plus(ID *id)
130 if(id->flag & LIB_INDIRECT) {
131 id->flag -= LIB_INDIRECT;
132 id->flag |= LIB_EXTERN;
137 void id_us_min(ID *id)
143 int id_make_local(ID *id, int test)
145 if(id->flag & LIB_INDIRECT)
148 switch(GS(id->name)) {
150 return 0; /* not implemented */
152 return 0; /* can't be linked */
154 if(!test) make_local_object((Object*)id);
158 make_local_mesh((Mesh*)id);
159 make_local_key(((Mesh*)id)->key);
164 make_local_curve((Curve*)id);
165 make_local_key(((Curve*)id)->key);
169 if(!test) make_local_mball((MetaBall*)id);
172 if(!test) make_local_material((Material*)id);
175 if(!test) make_local_texture((Tex*)id);
178 return 0; /* not implemented */
181 make_local_lattice((Lattice*)id);
182 make_local_key(((Lattice*)id)->key);
186 if(!test) make_local_lamp((Lamp*)id);
189 if(!test) make_local_camera((Camera*)id);
192 return 0; /* deprecated */
194 if(!test) make_local_key((Key*)id);
197 if(!test) make_local_world((World*)id);
200 return 0; /* can't be linked */
202 return 0; /* not implemented */
204 return 0; /* not implemented */
206 return 0; /* deprecated */
208 return 0; /* not implemented */
210 return 0; /* not implemented */
212 if(!test) make_local_armature((bArmature*)id);
215 if(!test) make_local_action((bAction*)id);
218 return 0; /* not implemented */
220 if(!test) make_local_brush((Brush*)id);
223 if(!test) make_local_particlesettings((ParticleSettings*)id);
226 return 0; /* can't be linked */
228 return 0; /* not implemented */
234 int id_copy(ID *id, ID **newid, int test)
236 if(!test) *newid= NULL;
239 * - make shallow copy, only this ID block
240 * - id.us of the new ID is set to 1 */
241 switch(GS(id->name)) {
243 return 0; /* can't be copied from here */
245 return 0; /* can't be copied from here */
247 if(!test) *newid= (ID*)copy_object((Object*)id);
250 if(!test) *newid= (ID*)copy_mesh((Mesh*)id);
253 if(!test) *newid= (ID*)copy_curve((Curve*)id);
256 if(!test) *newid= (ID*)copy_mball((MetaBall*)id);
259 if(!test) *newid= (ID*)copy_material((Material*)id);
262 if(!test) *newid= (ID*)copy_texture((Tex*)id);
265 if(!test) *newid= (ID*)copy_image((Image*)id);
268 if(!test) *newid= (ID*)copy_lattice((Lattice*)id);
271 if(!test) *newid= (ID*)copy_lamp((Lamp*)id);
274 if(!test) *newid= (ID*)copy_camera((Camera*)id);
277 return 0; /* deprecated */
279 if(!test) *newid= (ID*)copy_key((Key*)id);
282 if(!test) *newid= (ID*)copy_world((World*)id);
285 return 0; /* can't be copied from here */
287 return 0; /* not implemented */
289 if(!test) *newid= (ID*)copy_text((Text*)id);
292 return 0; /* deprecated */
294 return 0; /* not implemented */
296 if(!test) *newid= (ID*)copy_group((Group*)id);
299 if(!test) *newid= (ID*)copy_armature((bArmature*)id);
302 if(!test) *newid= (ID*)copy_action((bAction*)id);
305 if(!test) *newid= (ID*)ntreeCopyTree((bNodeTree*)id, 0);
308 if(!test) *newid= (ID*)copy_brush((Brush*)id);
311 if(!test) *newid= (ID*)psys_copy_settings((ParticleSettings*)id);
314 return 0; /* can't be copied from here */
316 return 0; /* not implemented */
322 int id_unlink(ID *id, int test)
324 Main *mainlib= G.main;
327 switch(GS(id->name)) {
330 unlink_text(mainlib, (Text*)id);
334 unlink_group((Group*)id);
338 unlink_object(NULL, (Object*)id);
345 lb= which_libbase(mainlib, GS(id->name));
346 free_libblock(lb, id);
354 ListBase *which_libbase(Main *mainlib, short type)
358 return &(mainlib->scene);
360 return &(mainlib->library);
362 return &(mainlib->object);
364 return &(mainlib->mesh);
366 return &(mainlib->curve);
368 return &(mainlib->mball);
370 return &(mainlib->mat);
372 return &(mainlib->tex);
374 return &(mainlib->image);
376 return &(mainlib->latt);
378 return &(mainlib->lamp);
380 return &(mainlib->camera);
382 return &(mainlib->ipo);
384 return &(mainlib->key);
386 return &(mainlib->world);
388 return &(mainlib->screen);
390 return &(mainlib->vfont);
392 return &(mainlib->text);
394 return &(mainlib->script);
396 return &(mainlib->sound);
398 return &(mainlib->group);
400 return &(mainlib->armature);
402 return &(mainlib->action);
404 return &(mainlib->nodetree);
406 return &(mainlib->brush);
408 return &(mainlib->particle);
410 return &(mainlib->wm);
412 return &(mainlib->gpencil);
417 /* Flag all ids in listbase */
418 void flag_listbase_ids(ListBase *lb, short flag, short value)
422 for(id= lb->first; id; id= id->next) id->flag |= flag;
425 for(id= lb->first; id; id= id->next) id->flag &= flag;
429 /* Flag all ids in listbase */
430 void flag_all_listbases_ids(short flag, short value)
432 ListBase *lbarray[MAX_LIBARRAY];
434 a= set_listbasepointers(G.main, lbarray);
435 while(a--) flag_listbase_ids(lbarray[a], flag, value);
438 void recalc_all_library_objects(Main *main)
442 /* flag for full recalc */
443 for(ob=main->object.first; ob; ob=ob->id.next)
445 ob->recalc |= OB_RECALC_ALL;
448 /* note: MAX_LIBARRAY define should match this code */
449 int set_listbasepointers(Main *main, ListBase **lb)
453 /* BACKWARDS! also watch order of free-ing! (mesh<->mat) */
455 lb[a++]= &(main->ipo);
456 lb[a++]= &(main->action); // xxx moved here to avoid problems when freeing with animato (aligorith)
457 lb[a++]= &(main->key);
458 lb[a++]= &(main->nodetree);
459 lb[a++]= &(main->image);
460 lb[a++]= &(main->tex);
461 lb[a++]= &(main->mat);
462 lb[a++]= &(main->vfont);
464 /* Important!: When adding a new object type,
465 * the specific data should be inserted here
468 lb[a++]= &(main->armature);
470 lb[a++]= &(main->mesh);
471 lb[a++]= &(main->curve);
472 lb[a++]= &(main->mball);
474 lb[a++]= &(main->latt);
475 lb[a++]= &(main->lamp);
476 lb[a++]= &(main->camera);
478 lb[a++]= &(main->text);
479 lb[a++]= &(main->sound);
480 lb[a++]= &(main->group);
481 lb[a++]= &(main->brush);
482 lb[a++]= &(main->script);
483 lb[a++]= &(main->particle);
485 lb[a++]= &(main->world);
486 lb[a++]= &(main->screen);
487 lb[a++]= &(main->object);
488 lb[a++]= &(main->scene);
489 lb[a++]= &(main->library);
490 lb[a++]= &(main->wm);
491 lb[a++]= &(main->gpencil);
498 /* *********** ALLOC AND FREE *****************
500 free_libblock(ListBase *lb, ID *id )
501 provide a list-basis and datablock, but only ID is read
503 void *alloc_libblock(ListBase *lb, type, name)
504 inserts in list and returns a new ID
506 ***************************** */
508 static ID *alloc_libblock_notest(short type)
514 id= MEM_callocN(sizeof(Scene), "scene");
517 id= MEM_callocN(sizeof(Library), "library");
520 id= MEM_callocN(sizeof(Object), "object");
523 id= MEM_callocN(sizeof(Mesh), "mesh");
526 id= MEM_callocN(sizeof(Curve), "curve");
529 id= MEM_callocN(sizeof(MetaBall), "mball");
532 id= MEM_callocN(sizeof(Material), "mat");
535 id= MEM_callocN(sizeof(Tex), "tex");
538 id= MEM_callocN(sizeof(Image), "image");
541 id= MEM_callocN(sizeof(Lattice), "latt");
544 id= MEM_callocN(sizeof(Lamp), "lamp");
547 id= MEM_callocN(sizeof(Camera), "camera");
550 id= MEM_callocN(sizeof(Ipo), "ipo");
553 id= MEM_callocN(sizeof(Key), "key");
556 id= MEM_callocN(sizeof(World), "world");
559 id= MEM_callocN(sizeof(bScreen), "screen");
562 id= MEM_callocN(sizeof(VFont), "vfont");
565 id= MEM_callocN(sizeof(Text), "text");
568 //XXX id= MEM_callocN(sizeof(Script), "script");
571 id= MEM_callocN(sizeof(bSound), "sound");
574 id= MEM_callocN(sizeof(Group), "group");
577 id = MEM_callocN(sizeof(bArmature), "armature");
580 id = MEM_callocN(sizeof(bAction), "action");
583 id = MEM_callocN(sizeof(bNodeTree), "nodetree");
586 id = MEM_callocN(sizeof(Brush), "brush");
589 id = MEM_callocN(sizeof(ParticleSettings), "ParticleSettings");
592 id = MEM_callocN(sizeof(wmWindowManager), "Window manager");
595 id = MEM_callocN(sizeof(bGPdata), "Grease Pencil");
601 /* used everywhere in blenkernel and text.c */
602 void *alloc_libblock(ListBase *lb, short type, const char *name)
606 id= alloc_libblock_notest(type);
611 *( (short *)id->name )= type;
612 new_id(lb, id, name);
613 /* alphabetic insterion: is in new_id */
618 /* by spec, animdata is first item after ID */
619 /* and, trust that BKE_animdata_from_id() will only find AnimData for valid ID-types */
620 static void id_copy_animdata(ID *id)
622 AnimData *adt= BKE_animdata_from_id(id);
625 IdAdtTemplate *iat = (IdAdtTemplate *)id;
626 iat->adt= BKE_copy_animdata(iat->adt);
630 /* material nodes use this since they are not treated as libdata */
631 void copy_libblock_data(ID *id, const ID *id_from)
633 if (id_from->properties)
634 id->properties = IDP_CopyProperty(id_from->properties);
636 /* the duplicate should get a copy of the animdata */
637 id_copy_animdata(id);
640 /* used everywhere in blenkernel */
641 void *copy_libblock(void *rt)
650 lb= which_libbase(G.main, GS(id->name));
651 idn= alloc_libblock(lb, GS(id->name), id->name+2);
654 printf("ERROR: Illegal ID name for %s (Crashing now)\n", id->name);
657 idn_len= MEM_allocN_len(idn);
658 if(idn_len - sizeof(ID) > 0) {
661 memcpy(cpn+sizeof(ID), cp+sizeof(ID), idn_len - sizeof(ID));
665 idn->flag |= LIB_NEW;
667 copy_libblock_data(idn, id);
672 static void free_library(Library *lib)
674 /* no freeing needed for libraries yet */
677 static void (*free_windowmanager_cb)(bContext *, wmWindowManager *)= NULL;
679 void set_free_windowmanager_cb(void (*func)(bContext *C, wmWindowManager *) )
681 free_windowmanager_cb= func;
684 void animdata_dtar_clear_cb(ID *id, AnimData *adt, void *userdata)
686 ChannelDriver *driver;
689 /* find the driver this belongs to and update it */
690 for (fcu=adt->drivers.first; fcu; fcu=fcu->next) {
695 for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
696 DRIVER_TARGETS_USED_LOOPER(dvar)
698 if (dtar->id == userdata)
701 DRIVER_TARGETS_LOOPER_END
708 /* used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c */
709 void free_libblock(ListBase *lb, void *idv)
713 switch( GS(id->name) ) { /* GetShort from util.h */
715 free_scene((Scene *)id);
718 free_library((Library *)id);
721 free_object((Object *)id);
724 free_mesh((Mesh *)id);
727 free_curve((Curve *)id);
730 free_mball((MetaBall *)id);
733 free_material((Material *)id);
736 free_texture((Tex *)id);
739 free_image((Image *)id);
742 free_lattice((Lattice *)id);
745 free_lamp((Lamp *)id);
748 free_camera((Camera*) id);
757 free_world((World *)id);
760 free_screen((bScreen *)id);
763 free_vfont((VFont *)id);
766 free_text((Text *)id);
769 //XXX free_script((Script *)id);
772 sound_free((bSound*)id);
775 free_group_objects((Group *)id);
778 free_armature((bArmature *)id);
781 free_action((bAction *)id);
784 ntreeFreeTree((bNodeTree *)id);
787 free_brush((Brush *)id);
790 psys_free_settings((ParticleSettings *)id);
793 if(free_windowmanager_cb)
794 free_windowmanager_cb(NULL, (wmWindowManager *)id);
797 free_gpencil_data((bGPdata *)id);
801 if (id->properties) {
802 IDP_FreeProperty(id->properties);
803 MEM_freeN(id->properties);
808 /* this ID may be a driver target! */
809 BKE_animdata_main_cb(G.main, animdata_dtar_clear_cb, (void *)id);
814 void free_libblock_us(ListBase *lb, void *idv) /* test users */
821 if(id->lib) printf("ERROR block %s %s users %d\n", id->lib->name, id->name, id->us);
822 else printf("ERROR block %s users %d\n", id->name, id->us);
825 if( GS(id->name)==ID_OB ) unlink_object(NULL, (Object *)id);
827 free_libblock(lb, id);
832 void free_main(Main *mainvar)
834 /* also call when reading a file, erase all, etc */
835 ListBase *lbarray[MAX_LIBARRAY];
838 a= set_listbasepointers(mainvar, lbarray);
840 ListBase *lb= lbarray[a];
843 while ( (id= lb->first) ) {
844 free_libblock(lb, id);
851 /* ***************** ID ************************ */
854 ID *find_id(char *type, char *name) /* type: "OB" or "MA" etc */
856 ListBase *lb= which_libbase(G.main, GS(type));
857 return BLI_findstring(lb, name, offsetof(ID, name) + 2);
860 static void get_flags_for_id(ID *id, char *buf)
862 int isfake= id->flag & LIB_FAKEUSER;
864 /* Writeout the flags for the entry, note there
865 * is a small hack that writes 5 spaces instead
866 * of 4 if no flags are displayed... this makes
867 * things usually line up ok - better would be
868 * to have that explicit, oh well - zr
871 if(GS(id->name)==ID_MA)
872 isnode= ((Material *)id)->use_nodes;
873 if(GS(id->name)==ID_TE)
874 isnode= ((Tex *)id)->use_nodes;
877 sprintf(buf, "-1W ");
878 else if (!id->lib && !isfake && id->us && !isnode)
881 sprintf(buf, "%c%cN%c ", id->lib?'L':' ', isfake?'F':' ', (id->us==0)?'O':' ');
883 sprintf(buf, "%c%c%c ", id->lib?'L':' ', isfake?'F':' ', (id->us==0)?'O':' ');
886 #define IDPUP_NO_VIEWER 1
888 static void IDnames_to_dyn_pupstring(DynStr *pupds, ListBase *lb, ID *link, short *nr, int hideflag)
890 int i, nids= BLI_countlist(lb);
894 if (nr && nids>MAX_IDPUP) {
895 BLI_dynstr_append(pupds, "DataBrowse %x-2");
900 for (i=0, id= lb->first; id; id= id->next, i++) {
903 if (nr && id==link) *nr= i+1;
905 if (U.uiflag & USER_HIDE_DOT && id->name[2]=='.')
907 if (hideflag & IDPUP_NO_VIEWER)
908 if (GS(id->name)==ID_IM)
909 if ( ((Image *)id)->source==IMA_SRC_VIEWER )
912 get_flags_for_id(id, buf);
914 BLI_dynstr_append(pupds, buf);
915 BLI_dynstr_append(pupds, id->name+2);
916 sprintf(buf, "%%x%d", i+1);
917 BLI_dynstr_append(pupds, buf);
922 case ID_MA: /* fall through */
923 case ID_TE: /* fall through */
924 case ID_IM: /* fall through */
925 case ID_WO: /* fall through */
926 case ID_LA: /* fall through */
927 sprintf(buf, "%%i%d", BKE_icon_getid(id) );
928 BLI_dynstr_append(pupds, buf);
935 BLI_dynstr_append(pupds, "|");
941 /* used by headerbuttons.c buttons.c editobject.c editseq.c */
942 /* if nr==NULL no MAX_IDPUP, this for non-header browsing */
943 void IDnames_to_pupstring(char **str, char *title, char *extraops, ListBase *lb, ID *link, short *nr)
945 DynStr *pupds= BLI_dynstr_new();
948 BLI_dynstr_append(pupds, title);
949 BLI_dynstr_append(pupds, "%t|");
953 BLI_dynstr_append(pupds, extraops);
954 if (BLI_dynstr_get_len(pupds))
955 BLI_dynstr_append(pupds, "|");
958 IDnames_to_dyn_pupstring(pupds, lb, link, nr, 0);
960 *str= BLI_dynstr_get_cstring(pupds);
961 BLI_dynstr_free(pupds);
964 /* skips viewer images */
965 void IMAnames_to_pupstring(char **str, char *title, char *extraops, ListBase *lb, ID *link, short *nr)
967 DynStr *pupds= BLI_dynstr_new();
970 BLI_dynstr_append(pupds, title);
971 BLI_dynstr_append(pupds, "%t|");
975 BLI_dynstr_append(pupds, extraops);
976 if (BLI_dynstr_get_len(pupds))
977 BLI_dynstr_append(pupds, "|");
980 IDnames_to_dyn_pupstring(pupds, lb, link, nr, IDPUP_NO_VIEWER);
982 *str= BLI_dynstr_get_cstring(pupds);
983 BLI_dynstr_free(pupds);
987 /* used by buttons.c library.c mball.c */
988 int splitIDname(char *name, char *left, int *nr)
993 strncpy(left, name, 21);
996 if(a>1 && name[a-1]=='.') return a;
1001 *nr= atol(name+a+1);
1004 if( isdigit(name[a])==0 ) break;
1009 for(a= 0; name[a]; a++)
1015 static void sort_alpha_id(ListBase *lb, ID *id)
1019 /* insert alphabetically */
1020 if(lb->first!=lb->last) {
1021 BLI_remlink(lb, id);
1025 if(BLI_strcasecmp(idtest->name, id->name)>0 || idtest->lib) {
1026 BLI_insertlinkbefore(lb, idtest, id);
1029 idtest= idtest->next;
1033 BLI_addtail(lb, id);
1040 * Check to see if there is an ID with the same name as 'name'.
1041 * Returns the ID if so, if not, returns NULL
1043 static ID *is_dupid(ListBase *lb, ID *id, char *name)
1047 for( idtest = lb->first; idtest; idtest = idtest->next ) {
1048 /* if idtest is not a lib */
1049 if( id != idtest && idtest->lib == NULL ) {
1050 /* do not test alphabetic! */
1052 if( idtest->name[2] == name[0] ) {
1053 if(strcmp(name, idtest->name+2)==0) break;
1062 * Check to see if an ID name is already used, and find a new one if so.
1063 * Return 1 if created a new name (returned in name).
1065 * Normally the ID that's being check is already in the ListBase, so ID *id
1066 * points at the new entry. The Python Library module needs to know what
1067 * the name of a datablock will be before it is appended; in this case ID *id
1071 static int check_for_dupid(ListBase *lb, ID *id, char *name)
1074 int nr= 0, nrtest, a, left_len;
1075 char left[32], leftest[32], in_use[32];
1077 /* make sure input name is terminated properly */
1078 /* if( strlen(name) > 21 ) name[21]= 0; */
1079 /* removed since this is only ever called from one place - campbell */
1083 /* phase 1: id already exists? */
1084 idtest = is_dupid(lb, id, name);
1086 /* if there is no double, done */
1087 if( idtest == NULL ) return 0;
1089 /* we have a dup; need to make a new name */
1090 /* quick check so we can reuse one of first 32 ids if vacant */
1091 memset(in_use, 0, sizeof(in_use));
1093 /* get name portion, number portion ("name.number") */
1094 left_len= splitIDname(name, left, &nr);
1096 /* if new name will be too long, truncate it */
1097 if(nr>999 && strlen(left)>16) left[16]= 0;
1098 else if(strlen(left)>17) left[17]= 0;
1100 for(idtest= lb->first; idtest; idtest= idtest->next) {
1101 if( (id != idtest) &&
1102 (idtest->lib == NULL) &&
1103 (*name == *(idtest->name+2)) &&
1104 (strncmp(name, idtest->name+2, left_len)==0) &&
1105 (splitIDname(idtest->name+2, leftest, &nrtest) == left_len)
1107 if(nrtest < sizeof(in_use))
1108 in_use[nrtest]= 1; /* mark as used */
1110 nr= nrtest+1; /* track largest unused */
1114 /* decide which value of nr to use */
1115 for(a=0; a < sizeof(in_use); a++) {
1116 if(a>=nr) break; /* stop when we've check up to biggest */
1117 if( in_use[a]==0 ) { /* found an unused value */
1123 /* If the original name has no numeric suffix,
1124 * rather than just chopping and adding numbers,
1125 * shave off the end chars until we have a unique name.
1126 * Check the null terminators match as well so we dont get Cube.000 -> Cube.00 */
1127 if (nr==0 && name[left_len]== left[left_len]) {
1128 int len = strlen(name)-1;
1129 idtest= is_dupid(lb, id, name);
1131 while (idtest && len> 1) {
1133 idtest= is_dupid(lb, id, name);
1135 if (idtest == NULL) return 1;
1136 /* otherwise just continue and use a number suffix */
1139 if(nr > 999 && strlen(left) > 16) {
1140 /* this would overflow name buffer */
1142 strcpy( name, left );
1145 /* this format specifier is from hell... */
1146 sprintf(name, "%s.%.3d", left, nr);
1153 * Only for local blocks: external en indirect blocks already have a
1156 * return 1: created a new name
1159 int new_id(ListBase *lb, ID *id, const char *tname)
1164 /* if library, don't rename */
1165 if(id->lib) return 0;
1167 /* if no libdata given, look up based on ID */
1168 if(lb==NULL) lb= which_libbase(G.main, GS(id->name));
1170 /* if no name given, use name of current ID
1171 * else make a copy (tname args can be const) */
1175 strncpy(name, tname, sizeof(name)-1);
1177 /* if result > 21, strncpy don't put the final '\0' to name.
1178 * easier to assign each time then to check if its needed */
1179 name[sizeof(name)-1]= 0;
1182 strcpy(name, ID_FALLBACK_NAME);
1184 result = check_for_dupid(lb, id, name);
1185 strcpy(id->name+2, name);
1187 /* This was in 2.43 and previous releases
1188 * however all data in blender should be sorted, not just duplicate names
1189 * sorting should not hurt, but noting just incause it alters the way other
1190 * functions work, so sort every time */
1192 sort_alpha_id(lb, id);*/
1194 sort_alpha_id(lb, id);
1199 /* next to indirect usage in read/writefile also in editobject.c scene.c */
1200 void clear_id_newpoins()
1202 ListBase *lbarray[MAX_LIBARRAY];
1206 a= set_listbasepointers(G.main, lbarray);
1208 id= lbarray[a]->first;
1211 id->flag &= ~LIB_NEW;
1217 /* only for library fixes */
1218 static void image_fix_relative_path(Image *ima)
1220 if(ima->id.lib==NULL) return;
1221 if(strncmp(ima->name, "//", 2)==0) {
1222 BLI_path_abs(ima->name, ima->id.lib->filepath);
1223 BLI_path_rel(ima->name, G.sce);
1227 #define LIBTAG(a) if(a && a->id.lib) {a->id.flag &=~LIB_INDIRECT; a->id.flag |= LIB_EXTERN;}
1229 static void lib_indirect_test_id(ID *id, Library *lib)
1233 /* datablocks that were indirectly related are now direct links
1234 * without this, appending data that has a link to other data will fail to write */
1235 if(lib && id->lib->parent == lib) {
1241 if(GS(id->name)==ID_OB) {
1242 Object *ob= (Object *)id;
1243 bActionStrip *strip;
1248 // XXX old animation system! --------------------------------------
1249 for (strip=ob->nlastrips.first; strip; strip=strip->next){
1250 LIBTAG(strip->object);
1254 // XXX: new animation system needs something like this?
1256 for(a=0; a<ob->totcol; a++) {
1260 LIBTAG(ob->dup_group);
1268 void tag_main(struct Main *mainvar, int tag)
1270 ListBase *lbarray[MAX_LIBARRAY];
1274 a= set_listbasepointers(mainvar, lbarray);
1276 for(id= lbarray[a]->first; id; id= id->next) {
1277 if(tag) id->flag |= LIB_DOIT;
1278 else id->flag &= ~LIB_DOIT;
1283 /* if lib!=NULL, only all from lib local */
1284 void all_local(Library *lib, int untagged_only)
1286 ListBase *lbarray[MAX_LIBARRAY], tempbase={0, 0};
1290 a= set_listbasepointers(G.main, lbarray);
1292 id= lbarray[a]->first;
1296 idn= id->next; /* id is possibly being inserted again */
1298 /* The check on the second line (LIB_PRE_EXISTING) is done so its
1299 * possible to tag data you dont want to be made local, used for
1300 * appending data, so any libdata already linked wont become local
1301 * (very nasty to discover all your links are lost after appending)
1303 if(id->flag & (LIB_EXTERN|LIB_INDIRECT|LIB_NEW) &&
1304 (untagged_only==0 || !(id->flag & LIB_PRE_EXISTING)))
1306 if(lib==NULL || id->lib==lib) {
1307 id->flag &= ~(LIB_EXTERN|LIB_INDIRECT|LIB_NEW);
1310 /* relative file patch */
1311 if(GS(id->name)==ID_IM)
1312 image_fix_relative_path((Image *)id);
1315 new_id(lbarray[a], id, 0); /* new_id only does it with double names */
1316 sort_alpha_id(lbarray[a], id);
1323 /* patch2: make it aphabetically */
1324 while( (id=tempbase.first) ) {
1325 BLI_remlink(&tempbase, id);
1326 BLI_addtail(lbarray[a], id);
1327 new_id(lbarray[a], id, 0);
1331 /* patch 3: make sure library data isn't indirect falsely... */
1332 a= set_listbasepointers(G.main, lbarray);
1334 for(id= lbarray[a]->first; id; id=id->next)
1335 lib_indirect_test_id(id, lib);
1340 void test_idbutton(char *name)
1342 /* called from buttons: when name already exists: call new_id */
1347 lb= which_libbase(G.main, GS(name-2) );
1351 idtest= BLI_findstring(lb, name, offsetof(ID, name) + 2);
1353 if(idtest) if( new_id(lb, idtest, name)==0 ) sort_alpha_id(lb, idtest);
1356 void text_idbutton(struct ID *id, char *text)
1359 if(GS(id->name)==ID_SCE)
1360 strcpy(text, "SCE: ");
1361 else if(GS(id->name)==ID_SCE)
1362 strcpy(text, "SCR: ");
1363 else if(GS(id->name)==ID_MA && ((Material*)id)->use_nodes)
1364 strcpy(text, "NT: ");
1366 text[0]= id->name[0];
1367 text[1]= id->name[1];
1377 void rename_id(ID *id, char *name)
1381 strncpy(id->name+2, name, 21);
1382 lb= which_libbase(G.main, GS(id->name) );
1384 new_id(lb, id, name);
1387 void name_uiprefix_id(char *name, ID *id)
1389 name[0] = id->lib ? 'L':' ';
1390 name[1] = id->flag & LIB_FAKEUSER ? 'F':' ';
1393 strcpy(name+3, id->name+2);