2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/library.c
31 * Contains management of ID's and libraries
32 * allocate and free of all library data
42 #include "MEM_guardedalloc.h"
44 /* all types are needed here, in order to do memory operations */
45 #include "DNA_anim_types.h"
46 #include "DNA_armature_types.h"
47 #include "DNA_brush_types.h"
48 #include "DNA_cachefile_types.h"
49 #include "DNA_camera_types.h"
50 #include "DNA_group_types.h"
51 #include "DNA_gpencil_types.h"
52 #include "DNA_ipo_types.h"
53 #include "DNA_key_types.h"
54 #include "DNA_lamp_types.h"
55 #include "DNA_lattice_types.h"
56 #include "DNA_linestyle_types.h"
57 #include "DNA_material_types.h"
58 #include "DNA_mesh_types.h"
59 #include "DNA_meta_types.h"
60 #include "DNA_modifier_types.h"
61 #include "DNA_movieclip_types.h"
62 #include "DNA_mask_types.h"
63 #include "DNA_node_types.h"
64 #include "DNA_object_types.h"
65 #include "DNA_scene_types.h"
66 #include "DNA_screen_types.h"
67 #include "DNA_speaker_types.h"
68 #include "DNA_sound_types.h"
69 #include "DNA_text_types.h"
70 #include "DNA_vfont_types.h"
71 #include "DNA_windowmanager_types.h"
72 #include "DNA_world_types.h"
74 #include "BLI_blenlib.h"
75 #include "BLI_utildefines.h"
77 #include "BLI_threads.h"
78 #include "BLT_translation.h"
80 #include "BKE_action.h"
81 #include "BKE_animsys.h"
82 #include "BKE_armature.h"
83 #include "BKE_bpath.h"
84 #include "BKE_brush.h"
85 #include "BKE_camera.h"
86 #include "BKE_cachefile.h"
87 #include "BKE_context.h"
88 #include "BKE_curve.h"
89 #include "BKE_depsgraph.h"
91 #include "BKE_global.h"
92 #include "BKE_group.h"
93 #include "BKE_gpencil.h"
94 #include "BKE_idcode.h"
95 #include "BKE_idprop.h"
96 #include "BKE_image.h"
99 #include "BKE_lattice.h"
100 #include "BKE_library.h"
101 #include "BKE_library_query.h"
102 #include "BKE_library_remap.h"
103 #include "BKE_linestyle.h"
104 #include "BKE_mesh.h"
105 #include "BKE_material.h"
106 #include "BKE_main.h"
107 #include "BKE_mball.h"
108 #include "BKE_mask.h"
109 #include "BKE_movieclip.h"
110 #include "BKE_node.h"
111 #include "BKE_object.h"
112 #include "BKE_paint.h"
113 #include "BKE_particle.h"
114 #include "BKE_packedFile.h"
115 #include "BKE_sound.h"
116 #include "BKE_speaker.h"
117 #include "BKE_scene.h"
118 #include "BKE_text.h"
119 #include "BKE_texture.h"
120 #include "BKE_world.h"
122 #include "DEG_depsgraph.h"
124 #include "RNA_access.h"
126 #include "IMB_imbuf.h"
127 #include "IMB_imbuf_types.h"
129 /* GS reads the memory pointed at in a specific ordering.
130 * only use this definition, makes little and big endian systems
131 * work fine, in conjunction with MAKE_ID */
133 /* ************* general ************************ */
136 /* this has to be called from each make_local_* func, we could call
137 * from id_make_local() but then the make local functions would not be self
139 * also note that the id _must_ have a library - campbell */
140 void BKE_id_lib_local_paths(Main *bmain, Library *lib, ID *id)
142 const char *bpath_user_data[2] = {bmain->name, lib->filepath};
144 BKE_bpath_traverse_id(bmain, id,
145 BKE_bpath_relocate_visitor,
146 BKE_BPATH_TRAVERSE_SKIP_MULTIFILE,
147 (void *)bpath_user_data);
150 void id_lib_extern(ID *id)
152 if (id && ID_IS_LINKED_DATABLOCK(id)) {
153 BLI_assert(BKE_idcode_is_linkable(GS(id->name)));
154 if (id->tag & LIB_TAG_INDIRECT) {
155 id->tag -= LIB_TAG_INDIRECT;
156 id->tag |= LIB_TAG_EXTERN;
161 /* ensure we have a real user */
162 /* Note: Now that we have flags, we could get rid of the 'fake_user' special case, flags are enough to ensure
163 * we always have a real user.
164 * However, ID_REAL_USERS is used in several places outside of core library.c, so think we can wait later
165 * to make this change... */
166 void id_us_ensure_real(ID *id)
169 const int limit = ID_FAKE_USERS(id);
170 id->tag |= LIB_TAG_EXTRAUSER;
171 if (id->us <= limit) {
172 if (id->us < limit || ((id->us == limit) && (id->tag & LIB_TAG_EXTRAUSER_SET))) {
173 printf("ID user count error: %s (from '%s')\n", id->name, id->lib ? id->lib->filepath : "[Main]");
177 id->tag |= LIB_TAG_EXTRAUSER_SET;
182 void id_us_clear_real(ID *id)
184 if (id && (id->tag & LIB_TAG_EXTRAUSER)) {
185 if (id->tag & LIB_TAG_EXTRAUSER_SET) {
187 BLI_assert(id->us >= ID_FAKE_USERS(id));
189 id->tag &= ~(LIB_TAG_EXTRAUSER | LIB_TAG_EXTRAUSER_SET);
194 * Same as \a id_us_plus, but does not handle lib indirect -> extern.
195 * Only used by readfile.c so far, but simpler/safer to keep it here nonetheless.
197 void id_us_plus_no_lib(ID *id)
200 if ((id->tag & LIB_TAG_EXTRAUSER) && (id->tag & LIB_TAG_EXTRAUSER_SET)) {
201 BLI_assert(id->us >= 1);
202 /* No need to increase count, just tag extra user as no more set.
203 * Avoids annoying & inconsistent +1 in user count. */
204 id->tag &= ~LIB_TAG_EXTRAUSER_SET;
207 BLI_assert(id->us >= 0);
214 void id_us_plus(ID *id)
217 id_us_plus_no_lib(id);
222 /* decrements the user count for *id. */
223 void id_us_min(ID *id)
226 const int limit = ID_FAKE_USERS(id);
228 if (id->us <= limit) {
229 printf("ID user decrement error: %s (from '%s'): %d <= %d\n",
230 id->name, id->lib ? id->lib->filepath : "[Main]", id->us, limit);
238 if ((id->us == limit) && (id->tag & LIB_TAG_EXTRAUSER)) {
239 /* We need an extra user here, but never actually incremented user count for it so far, do it now. */
240 id_us_ensure_real(id);
245 void id_fake_user_set(ID *id)
247 if (id && !(id->flag & LIB_FAKEUSER)) {
248 id->flag |= LIB_FAKEUSER;
253 void id_fake_user_clear(ID *id)
255 if (id && (id->flag & LIB_FAKEUSER)) {
256 id->flag &= ~LIB_FAKEUSER;
261 static int id_expand_local_callback(
262 void *UNUSED(user_data), struct ID *UNUSED(id_self), struct ID **id_pointer, int UNUSED(cd_flag))
265 id_lib_extern(*id_pointer);
268 return IDWALK_RET_NOP;
272 * Expand ID usages of given id as 'extern' (and no more indirect) linked data. Used by ID copy/make_local functions.
274 void BKE_id_expand_local(ID *id)
276 BKE_library_foreach_ID_link(id, id_expand_local_callback, NULL, 0);
280 * Ensure new (copied) ID is fully made local.
282 void BKE_id_copy_ensure_local(Main *bmain, ID *old_id, ID *new_id)
284 if (ID_IS_LINKED_DATABLOCK(old_id)) {
285 BKE_id_expand_local(new_id);
286 BKE_id_lib_local_paths(bmain, old_id->lib, new_id);
291 * Generic 'make local' function, works for most of datablock types...
293 void BKE_id_make_local_generic(Main *bmain, ID *id, const bool id_in_mainlist, const bool lib_local)
295 bool is_local = false, is_lib = false;
297 /* - only lib users: do nothing (unless force_local is set)
298 * - only local users: set flag
300 * In case we make a whole lib's content local, we always want to localize, and we skip remapping (done later).
303 if (!ID_IS_LINKED_DATABLOCK(id)) {
307 BKE_library_ID_test_usages(bmain, id, &is_local, &is_lib);
309 if (lib_local || is_local) {
311 id_clear_lib_data_ex(bmain, id, id_in_mainlist);
312 BKE_id_expand_local(id);
317 /* Should not fail in expected usecases, but id_copy does not copy Scene e.g. */
318 if (id_copy(bmain, id, &id_new, false)) {
322 BKE_libblock_remap(bmain, id, id_new, ID_REMAP_SKIP_INDIRECT_USAGE);
330 * Calls the appropriate make_local method for the block, unless test is set.
332 * \param lib_local Special flag used when making a whole library's content local, it needs specific handling.
334 * \return true if the block can be made local.
336 bool id_make_local(Main *bmain, ID *id, const bool test, const bool lib_local)
338 /* We don't care whether ID is directly or indirectly linked in case we are making a whole lib local... */
339 if (!lib_local && (id->tag & LIB_TAG_INDIRECT)) {
343 switch ((ID_Type)GS(id->name)) {
345 /* Partially implemented (has no copy...). */
346 if (!test) BKE_scene_make_local(bmain, (Scene *)id, lib_local);
349 if (!test) BKE_object_make_local(bmain, (Object *)id, lib_local);
352 if (!test) BKE_mesh_make_local(bmain, (Mesh *)id, lib_local);
355 if (!test) BKE_curve_make_local(bmain, (Curve *)id, lib_local);
358 if (!test) BKE_mball_make_local(bmain, (MetaBall *)id, lib_local);
361 if (!test) BKE_material_make_local(bmain, (Material *)id, lib_local);
364 if (!test) BKE_texture_make_local(bmain, (Tex *)id, lib_local);
367 if (!test) BKE_image_make_local(bmain, (Image *)id, lib_local);
370 if (!test) BKE_lattice_make_local(bmain, (Lattice *)id, lib_local);
373 if (!test) BKE_lamp_make_local(bmain, (Lamp *)id, lib_local);
376 if (!test) BKE_camera_make_local(bmain, (Camera *)id, lib_local);
379 if (!test) BKE_speaker_make_local(bmain, (Speaker *)id, lib_local);
382 if (!test) BKE_world_make_local(bmain, (World *)id, lib_local);
385 /* Partially implemented (has no copy...). */
386 if (!test) BKE_vfont_make_local(bmain, (VFont *)id, lib_local);
389 if (!test) BKE_text_make_local(bmain, (Text *)id, lib_local);
392 /* Partially implemented (has no copy...). */
393 if (!test) BKE_sound_make_local(bmain, (bSound *)id, lib_local);
396 if (!test) BKE_group_make_local(bmain, (Group *)id, lib_local);
399 if (!test) BKE_armature_make_local(bmain, (bArmature *)id, lib_local);
402 if (!test) BKE_action_make_local(bmain, (bAction *)id, lib_local);
405 if (!test) ntreeMakeLocal(bmain, (bNodeTree *)id, true, lib_local);
408 if (!test) BKE_brush_make_local(bmain, (Brush *)id, lib_local);
411 if (!test) BKE_particlesettings_make_local(bmain, (ParticleSettings *)id, lib_local);
414 if (!test) BKE_gpencil_make_local(bmain, (bGPdata *)id, lib_local);
417 if (!test) BKE_movieclip_make_local(bmain, (MovieClip *)id, lib_local);
420 if (!test) BKE_mask_make_local(bmain, (Mask *)id, lib_local);
423 if (!test) BKE_linestyle_make_local(bmain, (FreestyleLineStyle *)id, lib_local);
426 if (!test) BKE_palette_make_local(bmain, (Palette *)id, lib_local);
429 if (!test) BKE_paint_curve_make_local(bmain, (PaintCurve *)id, lib_local);
432 if (!test) BKE_cachefile_make_local(bmain, (CacheFile *)id, lib_local);
438 return false; /* can't be linked */
440 return false; /* deprecated */
447 * Invokes the appropriate copy method for the block and returns the result in
448 * newid, unless test. Returns true if the block can be copied.
450 bool id_copy(Main *bmain, ID *id, ID **newid, bool test)
457 * - make shallow copy, only this ID block
458 * - id.us of the new ID is set to 1 */
459 switch ((ID_Type)GS(id->name)) {
461 if (!test) *newid = (ID *)BKE_object_copy(bmain, (Object *)id);
464 if (!test) *newid = (ID *)BKE_mesh_copy(bmain, (Mesh *)id);
467 if (!test) *newid = (ID *)BKE_curve_copy(bmain, (Curve *)id);
470 if (!test) *newid = (ID *)BKE_mball_copy(bmain, (MetaBall *)id);
473 if (!test) *newid = (ID *)BKE_material_copy(bmain, (Material *)id);
476 if (!test) *newid = (ID *)BKE_texture_copy(bmain, (Tex *)id);
479 if (!test) *newid = (ID *)BKE_image_copy(bmain, (Image *)id);
482 if (!test) *newid = (ID *)BKE_lattice_copy(bmain, (Lattice *)id);
485 if (!test) *newid = (ID *)BKE_lamp_copy(bmain, (Lamp *)id);
488 if (!test) *newid = (ID *)BKE_speaker_copy(bmain, (Speaker *)id);
491 if (!test) *newid = (ID *)BKE_camera_copy(bmain, (Camera *)id);
494 if (!test) *newid = (ID *)BKE_key_copy(bmain, (Key *)id);
497 if (!test) *newid = (ID *)BKE_world_copy(bmain, (World *)id);
500 if (!test) *newid = (ID *)BKE_text_copy(bmain, (Text *)id);
503 if (!test) *newid = (ID *)BKE_group_copy(bmain, (Group *)id);
506 if (!test) *newid = (ID *)BKE_armature_copy(bmain, (bArmature *)id);
509 if (!test) *newid = (ID *)BKE_action_copy(bmain, (bAction *)id);
512 if (!test) *newid = (ID *)ntreeCopyTree(bmain, (bNodeTree *)id);
515 if (!test) *newid = (ID *)BKE_brush_copy(bmain, (Brush *)id);
518 if (!test) *newid = (ID *)BKE_particlesettings_copy(bmain, (ParticleSettings *)id);
521 if (!test) *newid = (ID *)BKE_gpencil_data_duplicate(bmain, (bGPdata *)id, false);
524 if (!test) *newid = (ID *)BKE_movieclip_copy(bmain, (MovieClip *)id);
527 if (!test) *newid = (ID *)BKE_mask_copy(bmain, (Mask *)id);
530 if (!test) *newid = (ID *)BKE_linestyle_copy(bmain, (FreestyleLineStyle *)id);
533 if (!test) *newid = (ID *)BKE_palette_copy(bmain, (Palette *)id);
536 if (!test) *newid = (ID *)BKE_paint_curve_copy(bmain, (PaintCurve *)id);
539 if (!test) *newid = (ID *)BKE_cachefile_copy(bmain, (CacheFile *)id);
545 return false; /* can't be copied from here */
548 return false; /* not implemented */
550 return false; /* deprecated */
556 bool id_single_user(bContext *C, ID *id, PointerRNA *ptr, PropertyRNA *prop)
562 /* if property isn't editable, we're going to have an extra block hanging around until we save */
563 if (RNA_property_editable(ptr, prop)) {
564 if (id_copy(CTX_data_main(C), id, &newid, false) && newid) {
565 /* copy animation actions too */
566 BKE_animdata_copy_id_action(id);
567 /* us is 1 by convention, but RNA_property_pointer_set
568 * will also increment it, so set it to zero */
572 RNA_id_pointer_create(newid, &idptr);
573 RNA_property_pointer_set(ptr, prop, idptr);
574 RNA_property_update(C, ptr, prop);
584 ListBase *which_libbase(Main *mainlib, short type)
586 switch ((ID_Type)type) {
588 return &(mainlib->scene);
590 return &(mainlib->library);
592 return &(mainlib->object);
594 return &(mainlib->mesh);
596 return &(mainlib->curve);
598 return &(mainlib->mball);
600 return &(mainlib->mat);
602 return &(mainlib->tex);
604 return &(mainlib->image);
606 return &(mainlib->latt);
608 return &(mainlib->lamp);
610 return &(mainlib->camera);
612 return &(mainlib->ipo);
614 return &(mainlib->key);
616 return &(mainlib->world);
618 return &(mainlib->screen);
620 return &(mainlib->vfont);
622 return &(mainlib->text);
624 return &(mainlib->speaker);
626 return &(mainlib->sound);
628 return &(mainlib->group);
630 return &(mainlib->armature);
632 return &(mainlib->action);
634 return &(mainlib->nodetree);
636 return &(mainlib->brush);
638 return &(mainlib->particle);
640 return &(mainlib->wm);
642 return &(mainlib->gpencil);
644 return &(mainlib->movieclip);
646 return &(mainlib->mask);
648 return &(mainlib->linestyle);
650 return &(mainlib->palettes);
652 return &(mainlib->paintcurves);
654 return &(mainlib->cachefiles);
660 * Clear or set given tags for all ids in listbase (runtime tags).
662 void BKE_main_id_tag_listbase(ListBase *lb, const int tag, const bool value)
666 for (id = lb->first; id; id = id->next) {
671 const int ntag = ~tag;
672 for (id = lb->first; id; id = id->next) {
679 * Clear or set given tags for all ids of given type in bmain (runtime tags).
681 void BKE_main_id_tag_idcode(struct Main *mainvar, const short type, const int tag, const bool value)
683 ListBase *lb = which_libbase(mainvar, type);
685 BKE_main_id_tag_listbase(lb, tag, value);
689 * Clear or set given tags for all ids in bmain (runtime tags).
691 void BKE_main_id_tag_all(struct Main *mainvar, const int tag, const bool value)
693 ListBase *lbarray[MAX_LIBARRAY];
696 a = set_listbasepointers(mainvar, lbarray);
698 BKE_main_id_tag_listbase(lbarray[a], tag, value);
704 * Clear or set given flags for all ids in listbase (persistent flags).
706 void BKE_main_id_flag_listbase(ListBase *lb, const int flag, const bool value)
710 for (id = lb->first; id; id = id->next)
714 const int nflag = ~flag;
715 for (id = lb->first; id; id = id->next)
721 * Clear or set given flags for all ids in bmain (persistent flags).
723 void BKE_main_id_flag_all(Main *bmain, const int flag, const bool value)
725 ListBase *lbarray[MAX_LIBARRAY];
727 a = set_listbasepointers(bmain, lbarray);
729 BKE_main_id_flag_listbase(lbarray[a], flag, value);
733 void BKE_main_lib_objects_recalc_all(Main *bmain)
737 /* flag for full recalc */
738 for (ob = bmain->object.first; ob; ob = ob->id.next) {
739 if (ID_IS_LINKED_DATABLOCK(ob)) {
740 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
744 DAG_id_type_tag(bmain, ID_OB);
748 * puts into array *lb pointers to all the ListBase structs in main,
749 * and returns the number of them as the function result. This is useful for
750 * generic traversal of all the blocks in a Main (by traversing all the
751 * lists in turn), without worrying about block types.
753 * \note MAX_LIBARRAY define should match this code */
754 int set_listbasepointers(Main *main, ListBase **lb)
756 /* BACKWARDS! also watch order of free-ing! (mesh<->mat), first items freed last.
757 * This is important because freeing data decreases usercounts of other datablocks,
758 * if this data is its self freed it can crash. */
759 lb[INDEX_ID_LI] = &(main->library); /* Libraries may be accessed from pretty much any other ID... */
760 lb[INDEX_ID_IP] = &(main->ipo);
761 lb[INDEX_ID_AC] = &(main->action); /* moved here to avoid problems when freeing with animato (aligorith) */
762 lb[INDEX_ID_KE] = &(main->key);
763 lb[INDEX_ID_GD] = &(main->gpencil); /* referenced by nodes, objects, view, scene etc, before to free after. */
764 lb[INDEX_ID_NT] = &(main->nodetree);
765 lb[INDEX_ID_IM] = &(main->image);
766 lb[INDEX_ID_TE] = &(main->tex);
767 lb[INDEX_ID_MA] = &(main->mat);
768 lb[INDEX_ID_VF] = &(main->vfont);
770 /* Important!: When adding a new object type,
771 * the specific data should be inserted here
774 lb[INDEX_ID_AR] = &(main->armature);
776 lb[INDEX_ID_CF] = &(main->cachefiles);
777 lb[INDEX_ID_ME] = &(main->mesh);
778 lb[INDEX_ID_CU] = &(main->curve);
779 lb[INDEX_ID_MB] = &(main->mball);
781 lb[INDEX_ID_LT] = &(main->latt);
782 lb[INDEX_ID_LA] = &(main->lamp);
783 lb[INDEX_ID_CA] = &(main->camera);
785 lb[INDEX_ID_TXT] = &(main->text);
786 lb[INDEX_ID_SO] = &(main->sound);
787 lb[INDEX_ID_GR] = &(main->group);
788 lb[INDEX_ID_PAL] = &(main->palettes);
789 lb[INDEX_ID_PC] = &(main->paintcurves);
790 lb[INDEX_ID_BR] = &(main->brush);
791 lb[INDEX_ID_PA] = &(main->particle);
792 lb[INDEX_ID_SPK] = &(main->speaker);
794 lb[INDEX_ID_WO] = &(main->world);
795 lb[INDEX_ID_MC] = &(main->movieclip);
796 lb[INDEX_ID_SCR] = &(main->screen);
797 lb[INDEX_ID_OB] = &(main->object);
798 lb[INDEX_ID_LS] = &(main->linestyle); /* referenced by scenes */
799 lb[INDEX_ID_SCE] = &(main->scene);
800 lb[INDEX_ID_WM] = &(main->wm);
801 lb[INDEX_ID_MSK] = &(main->mask);
803 lb[INDEX_ID_NULL] = NULL;
805 return (MAX_LIBARRAY - 1);
808 /* *********** ALLOC AND FREE *****************
810 * BKE_libblock_free(ListBase *lb, ID *id )
811 * provide a list-basis and datablock, but only ID is read
813 * void *BKE_libblock_alloc(ListBase *lb, type, name)
814 * inserts in list and returns a new ID
816 * **************************** */
819 * Allocates and returns memory of the right size for the specified block type,
820 * initialized to zero.
822 void *BKE_libblock_alloc_notest(short type)
826 switch ((ID_Type)type) {
828 id = MEM_callocN(sizeof(Scene), "scene");
831 id = MEM_callocN(sizeof(Library), "library");
834 id = MEM_callocN(sizeof(Object), "object");
837 id = MEM_callocN(sizeof(Mesh), "mesh");
840 id = MEM_callocN(sizeof(Curve), "curve");
843 id = MEM_callocN(sizeof(MetaBall), "mball");
846 id = MEM_callocN(sizeof(Material), "mat");
849 id = MEM_callocN(sizeof(Tex), "tex");
852 id = MEM_callocN(sizeof(Image), "image");
855 id = MEM_callocN(sizeof(Lattice), "latt");
858 id = MEM_callocN(sizeof(Lamp), "lamp");
861 id = MEM_callocN(sizeof(Camera), "camera");
864 id = MEM_callocN(sizeof(Ipo), "ipo");
867 id = MEM_callocN(sizeof(Key), "key");
870 id = MEM_callocN(sizeof(World), "world");
873 id = MEM_callocN(sizeof(bScreen), "screen");
876 id = MEM_callocN(sizeof(VFont), "vfont");
879 id = MEM_callocN(sizeof(Text), "text");
882 id = MEM_callocN(sizeof(Speaker), "speaker");
885 id = MEM_callocN(sizeof(bSound), "sound");
888 id = MEM_callocN(sizeof(Group), "group");
891 id = MEM_callocN(sizeof(bArmature), "armature");
894 id = MEM_callocN(sizeof(bAction), "action");
897 id = MEM_callocN(sizeof(bNodeTree), "nodetree");
900 id = MEM_callocN(sizeof(Brush), "brush");
903 id = MEM_callocN(sizeof(ParticleSettings), "ParticleSettings");
906 id = MEM_callocN(sizeof(wmWindowManager), "Window manager");
909 id = MEM_callocN(sizeof(bGPdata), "Grease Pencil");
912 id = MEM_callocN(sizeof(MovieClip), "Movie Clip");
915 id = MEM_callocN(sizeof(Mask), "Mask");
918 id = MEM_callocN(sizeof(FreestyleLineStyle), "Freestyle Line Style");
921 id = MEM_callocN(sizeof(Palette), "Palette");
924 id = MEM_callocN(sizeof(PaintCurve), "Paint Curve");
927 id = MEM_callocN(sizeof(CacheFile), "Cache File");
934 * Allocates and returns a block of the specified type, with the specified name
935 * (adjusted as necessary to ensure uniqueness), and appended to the specified list.
936 * The user count is set to 1, all other content (apart from name and links) being
937 * initialized to zero.
939 void *BKE_libblock_alloc(Main *bmain, short type, const char *name)
942 ListBase *lb = which_libbase(bmain, type);
944 id = BKE_libblock_alloc_notest(type);
946 BKE_main_lock(bmain);
950 *( (short *)id->name) = type;
951 new_id(lb, id, name);
952 /* alphabetic insertion: is in new_id */
953 BKE_main_unlock(bmain);
955 DAG_id_type_tag(bmain, type);
960 * Initialize an ID of given type, such that it has valid 'empty' data.
961 * ID is assumed to be just calloc'ed.
963 void BKE_libblock_init_empty(ID *id)
965 /* Note that only ID types that are not valid when filled of zero should have a callback here. */
966 switch ((ID_Type)GS(id->name)) {
968 BKE_scene_init((Scene *)id);
975 Object *ob = (Object *)id;
981 BKE_mesh_init((Mesh *)id);
984 BKE_curve_init((Curve *)id);
987 BKE_mball_init((MetaBall *)id);
990 BKE_material_init((Material *)id);
993 BKE_texture_default((Tex *)id);
996 BKE_image_init((Image *)id);
999 BKE_lattice_init((Lattice *)id);
1002 BKE_lamp_init((Lamp *)id);
1005 BKE_speaker_init((Speaker *)id);
1008 BKE_camera_init((Camera *)id);
1011 BKE_world_init((World *)id);
1014 /* Nothing to do. */
1017 BKE_vfont_init((VFont *)id);
1020 BKE_text_init((Text *)id);
1023 /* Another fuzzy case, think NULLified content is OK here... */
1026 /* Nothing to do. */
1029 /* Nothing to do. */
1032 /* Nothing to do. */
1035 ntreeInitDefault((bNodeTree *)id);
1038 BKE_brush_init((Brush *)id);
1041 /* Nothing to do. */
1044 /* Nothing to do. */
1047 /* Nothing to do. */
1050 /* Nothing to do. */
1053 BKE_linestyle_init((FreestyleLineStyle *)id);
1056 BKE_cachefile_init((CacheFile *)id);
1059 /* Shapekeys are a complex topic too - they depend on their 'user' data type...
1060 * They are not linkable, though, so it should never reach here anyway. */
1064 /* We should never reach this. */
1068 /* Should not be needed - animation from lib pre-2.5 is broken anyway. */
1072 BLI_assert(0); /* Should never reach this point... */
1076 /* by spec, animdata is first item after ID */
1077 /* and, trust that BKE_animdata_from_id() will only find AnimData for valid ID-types */
1078 static void id_copy_animdata(ID *id, const bool do_action)
1080 AnimData *adt = BKE_animdata_from_id(id);
1083 IdAdtTemplate *iat = (IdAdtTemplate *)id;
1084 iat->adt = BKE_animdata_copy(iat->adt, do_action); /* could be set to false, need to investigate */
1088 /* material nodes use this since they are not treated as libdata */
1089 void BKE_libblock_copy_data(ID *id, const ID *id_from, const bool do_action)
1091 if (id_from->properties)
1092 id->properties = IDP_CopyProperty(id_from->properties);
1094 /* the duplicate should get a copy of the animdata */
1095 id_copy_animdata(id, do_action);
1098 /* used everywhere in blenkernel */
1099 void *BKE_libblock_copy(Main *bmain, ID *id)
1104 idn = BKE_libblock_alloc(bmain, GS(id->name), id->name + 2);
1106 assert(idn != NULL);
1108 idn_len = MEM_allocN_len(idn);
1109 if ((int)idn_len - (int)sizeof(ID) > 0) { /* signed to allow neg result */
1110 const char *cp = (const char *)id;
1111 char *cpn = (char *)idn;
1113 memcpy(cpn + sizeof(ID), cp + sizeof(ID), idn_len - sizeof(ID));
1117 idn->tag |= LIB_TAG_NEW;
1119 BKE_libblock_copy_data(idn, id, false);
1124 void *BKE_libblock_copy_nolib(ID *id, const bool do_action)
1129 idn = BKE_libblock_alloc_notest(GS(id->name));
1130 assert(idn != NULL);
1132 BLI_strncpy(idn->name, id->name, sizeof(idn->name));
1134 idn_len = MEM_allocN_len(idn);
1135 if ((int)idn_len - (int)sizeof(ID) > 0) { /* signed to allow neg result */
1136 const char *cp = (const char *)id;
1137 char *cpn = (char *)idn;
1139 memcpy(cpn + sizeof(ID), cp + sizeof(ID), idn_len - sizeof(ID));
1143 idn->tag |= LIB_TAG_NEW;
1146 BKE_libblock_copy_data(idn, id, do_action);
1151 static int id_relink_looper(void *UNUSED(user_data), ID *UNUSED(self_id), ID **id_pointer, const int cd_flag)
1153 ID *id = *id_pointer;
1155 /* See: NEW_ID macro */
1157 BKE_library_update_ID_link_user(id->newid, id, cd_flag);
1158 *id_pointer = id->newid;
1160 else if (id->tag & LIB_TAG_NEW) {
1161 id->tag &= ~LIB_TAG_NEW;
1162 BKE_libblock_relink(id);
1165 return IDWALK_RET_NOP;
1168 void BKE_libblock_relink(ID *id)
1170 if (ID_IS_LINKED_DATABLOCK(id))
1173 BKE_library_foreach_ID_link(id, id_relink_looper, NULL, 0);
1176 void BKE_library_free(Library *lib)
1178 if (lib->packedfile)
1179 freePackedFile(lib->packedfile);
1182 Main *BKE_main_new(void)
1184 Main *bmain = MEM_callocN(sizeof(Main), "new main");
1185 bmain->eval_ctx = DEG_evaluation_context_new(DAG_EVAL_VIEWPORT);
1186 bmain->lock = MEM_mallocN(sizeof(SpinLock), "main lock");
1187 BLI_spin_init((SpinLock *)bmain->lock);
1191 void BKE_main_free(Main *mainvar)
1193 /* also call when reading a file, erase all, etc */
1194 ListBase *lbarray[MAX_LIBARRAY];
1197 MEM_SAFE_FREE(mainvar->blen_thumb);
1199 a = set_listbasepointers(mainvar, lbarray);
1201 ListBase *lb = lbarray[a];
1204 while ( (id = lb->first) ) {
1206 BKE_libblock_free_ex(mainvar, id, false);
1208 /* errors freeing ID's can be hard to track down,
1209 * enable this so valgrind will give the line number in its error log */
1211 case 0: BKE_libblock_free_ex(mainvar, id, false); break;
1212 case 1: BKE_libblock_free_ex(mainvar, id, false); break;
1213 case 2: BKE_libblock_free_ex(mainvar, id, false); break;
1214 case 3: BKE_libblock_free_ex(mainvar, id, false); break;
1215 case 4: BKE_libblock_free_ex(mainvar, id, false); break;
1216 case 5: BKE_libblock_free_ex(mainvar, id, false); break;
1217 case 6: BKE_libblock_free_ex(mainvar, id, false); break;
1218 case 7: BKE_libblock_free_ex(mainvar, id, false); break;
1219 case 8: BKE_libblock_free_ex(mainvar, id, false); break;
1220 case 9: BKE_libblock_free_ex(mainvar, id, false); break;
1221 case 10: BKE_libblock_free_ex(mainvar, id, false); break;
1222 case 11: BKE_libblock_free_ex(mainvar, id, false); break;
1223 case 12: BKE_libblock_free_ex(mainvar, id, false); break;
1224 case 13: BKE_libblock_free_ex(mainvar, id, false); break;
1225 case 14: BKE_libblock_free_ex(mainvar, id, false); break;
1226 case 15: BKE_libblock_free_ex(mainvar, id, false); break;
1227 case 16: BKE_libblock_free_ex(mainvar, id, false); break;
1228 case 17: BKE_libblock_free_ex(mainvar, id, false); break;
1229 case 18: BKE_libblock_free_ex(mainvar, id, false); break;
1230 case 19: BKE_libblock_free_ex(mainvar, id, false); break;
1231 case 20: BKE_libblock_free_ex(mainvar, id, false); break;
1232 case 21: BKE_libblock_free_ex(mainvar, id, false); break;
1233 case 22: BKE_libblock_free_ex(mainvar, id, false); break;
1234 case 23: BKE_libblock_free_ex(mainvar, id, false); break;
1235 case 24: BKE_libblock_free_ex(mainvar, id, false); break;
1236 case 25: BKE_libblock_free_ex(mainvar, id, false); break;
1237 case 26: BKE_libblock_free_ex(mainvar, id, false); break;
1238 case 27: BKE_libblock_free_ex(mainvar, id, false); break;
1239 case 28: BKE_libblock_free_ex(mainvar, id, false); break;
1240 case 29: BKE_libblock_free_ex(mainvar, id, false); break;
1241 case 30: BKE_libblock_free_ex(mainvar, id, false); break;
1242 case 31: BKE_libblock_free_ex(mainvar, id, false); break;
1243 case 32: BKE_libblock_free_ex(mainvar, id, false); break;
1244 case 33: BKE_libblock_free_ex(mainvar, id, false); break;
1245 case 34: BKE_libblock_free_ex(mainvar, id, false); break;
1254 BLI_spin_end((SpinLock *)mainvar->lock);
1255 MEM_freeN(mainvar->lock);
1256 DEG_evaluation_context_free(mainvar->eval_ctx);
1260 void BKE_main_lock(struct Main *bmain)
1262 BLI_spin_lock((SpinLock *) bmain->lock);
1265 void BKE_main_unlock(struct Main *bmain)
1267 BLI_spin_unlock((SpinLock *) bmain->lock);
1271 * Generates a raw .blend file thumbnail data from given image.
1273 * \param bmain If not NULL, also store generated data in this Main.
1274 * \param img ImBuf image to generate thumbnail data from.
1275 * \return The generated .blend file raw thumbnail data.
1277 BlendThumbnail *BKE_main_thumbnail_from_imbuf(Main *bmain, ImBuf *img)
1279 BlendThumbnail *data = NULL;
1282 MEM_SAFE_FREE(bmain->blen_thumb);
1286 const size_t sz = BLEN_THUMB_MEMSIZE(img->x, img->y);
1287 data = MEM_mallocN(sz, __func__);
1289 IMB_rect_from_float(img); /* Just in case... */
1290 data->width = img->x;
1291 data->height = img->y;
1292 memcpy(data->rect, img->rect, sz - sizeof(*data));
1296 bmain->blen_thumb = data;
1302 * Generates an image from raw .blend file thumbnail \a data.
1304 * \param bmain Use this bmain->blen_thumb data if given \a data is NULL.
1305 * \param data Raw .blend file thumbnail data.
1306 * \return An ImBuf from given data, or NULL if invalid.
1308 ImBuf *BKE_main_thumbnail_to_imbuf(Main *bmain, BlendThumbnail *data)
1312 if (!data && bmain) {
1313 data = bmain->blen_thumb;
1317 /* Note: we cannot use IMB_allocFromBuffer(), since it tries to dupalloc passed buffer, which will fail
1318 * here (we do not want to pass the first two ints!). */
1319 img = IMB_allocImBuf((unsigned int)data->width, (unsigned int)data->height, 32, IB_rect | IB_metadata);
1320 memcpy(img->rect, data->rect, BLEN_THUMB_MEMSIZE(data->width, data->height) - sizeof(*data));
1327 * Generates an empty (black) thumbnail for given Main.
1329 void BKE_main_thumbnail_create(struct Main *bmain)
1331 MEM_SAFE_FREE(bmain->blen_thumb);
1333 bmain->blen_thumb = MEM_callocN(BLEN_THUMB_MEMSIZE(BLEN_THUMB_SIZE, BLEN_THUMB_SIZE), __func__);
1334 bmain->blen_thumb->width = BLEN_THUMB_SIZE;
1335 bmain->blen_thumb->height = BLEN_THUMB_SIZE;
1338 /* ***************** ID ************************ */
1339 ID *BKE_libblock_find_name_ex(struct Main *bmain, const short type, const char *name)
1341 ListBase *lb = which_libbase(bmain, type);
1342 BLI_assert(lb != NULL);
1343 return BLI_findstring(lb, name, offsetof(ID, name) + 2);
1345 ID *BKE_libblock_find_name(const short type, const char *name)
1347 return BKE_libblock_find_name_ex(G.main, type, name);
1351 void id_sort_by_name(ListBase *lb, ID *id)
1355 /* insert alphabetically */
1356 if (lb->first != lb->last) {
1357 BLI_remlink(lb, id);
1361 if (BLI_strcasecmp(idtest->name, id->name) > 0 || (idtest->lib && !id->lib)) {
1362 BLI_insertlinkbefore(lb, idtest, id);
1365 idtest = idtest->next;
1368 if (idtest == NULL) {
1369 BLI_addtail(lb, id);
1376 * Check to see if there is an ID with the same name as 'name'.
1377 * Returns the ID if so, if not, returns NULL
1379 static ID *is_dupid(ListBase *lb, ID *id, const char *name)
1383 for (idtest = lb->first; idtest; idtest = idtest->next) {
1384 /* if idtest is not a lib */
1385 if (id != idtest && !ID_IS_LINKED_DATABLOCK(idtest)) {
1386 /* do not test alphabetic! */
1388 if (idtest->name[2] == name[0]) {
1389 if (STREQ(name, idtest->name + 2)) break;
1398 * Check to see if an ID name is already used, and find a new one if so.
1399 * Return true if created a new name (returned in name).
1401 * Normally the ID that's being check is already in the ListBase, so ID *id
1402 * points at the new entry. The Python Library module needs to know what
1403 * the name of a datablock will be before it is appended; in this case ID *id
1407 static bool check_for_dupid(ListBase *lb, ID *id, char *name)
1410 int nr = 0, a, left_len;
1411 #define MAX_IN_USE 64
1412 bool in_use[MAX_IN_USE];
1413 /* to speed up finding unused numbers within [1 .. MAX_IN_USE - 1] */
1415 char left[MAX_ID_NAME + 8], leftest[MAX_ID_NAME + 8];
1419 /* phase 1: id already exists? */
1420 idtest = is_dupid(lb, id, name);
1422 /* if there is no double, done */
1423 if (idtest == NULL) return false;
1425 /* we have a dup; need to make a new name */
1426 /* quick check so we can reuse one of first MAX_IN_USE - 1 ids if vacant */
1427 memset(in_use, false, sizeof(in_use));
1429 /* get name portion, number portion ("name.number") */
1430 left_len = BLI_split_name_num(left, &nr, name, '.');
1432 /* if new name will be too long, truncate it */
1433 if (nr > 999 && left_len > (MAX_ID_NAME - 8)) { /* assumption: won't go beyond 9999 */
1434 left[MAX_ID_NAME - 8] = 0;
1435 left_len = MAX_ID_NAME - 8;
1437 else if (left_len > (MAX_ID_NAME - 7)) {
1438 left[MAX_ID_NAME - 7] = 0;
1439 left_len = MAX_ID_NAME - 7;
1442 for (idtest = lb->first; idtest; idtest = idtest->next) {
1444 if ( (id != idtest) &&
1445 !ID_IS_LINKED_DATABLOCK(idtest) &&
1446 (*name == *(idtest->name + 2)) &&
1447 STREQLEN(name, idtest->name + 2, left_len) &&
1448 (BLI_split_name_num(leftest, &nrtest, idtest->name + 2, '.') == left_len)
1451 /* will get here at least once, otherwise is_dupid call above would have returned NULL */
1452 if (nrtest < MAX_IN_USE)
1453 in_use[nrtest] = true; /* mark as used */
1455 nr = nrtest + 1; /* track largest unused */
1458 /* At this point, 'nr' will typically be at least 1. (but not always) */
1459 // BLI_assert(nr >= 1);
1461 /* decide which value of nr to use */
1462 for (a = 0; a < MAX_IN_USE; a++) {
1463 if (a >= nr) break; /* stop when we've checked up to biggest */ /* redundant check */
1464 if (!in_use[a]) { /* found an unused value */
1466 /* can only be zero if all potential duplicate names had
1467 * nonzero numeric suffixes, which means name itself has
1468 * nonzero numeric suffix (else no name conflict and wouldn't
1469 * have got here), which means name[left_len] is not a null */
1473 /* At this point, nr is either the lowest unused number within [0 .. MAX_IN_USE - 1],
1474 * or 1 greater than the largest used number if all those low ones are taken.
1475 * We can't be bothered to look for the lowest unused number beyond (MAX_IN_USE - 1). */
1477 /* If the original name has no numeric suffix,
1478 * rather than just chopping and adding numbers,
1479 * shave off the end chars until we have a unique name.
1480 * Check the null terminators match as well so we don't get Cube.000 -> Cube.00 */
1481 if (nr == 0 && name[left_len] == '\0') {
1483 /* FIXME: this code will never be executed, because either nr will be
1484 * at least 1, or name will not end at left_len! */
1488 idtest = is_dupid(lb, id, name);
1490 while (idtest && len > 1) {
1492 idtest = is_dupid(lb, id, name);
1494 if (idtest == NULL) return true;
1495 /* otherwise just continue and use a number suffix */
1498 if (nr > 999 && left_len > (MAX_ID_NAME - 8)) {
1499 /* this would overflow name buffer */
1500 left[MAX_ID_NAME - 8] = 0;
1501 /* left_len = MAX_ID_NAME - 8; */ /* for now this isn't used again */
1502 memcpy(name, left, sizeof(char) * (MAX_ID_NAME - 7));
1505 /* this format specifier is from hell... */
1506 BLI_snprintf(name, sizeof(id->name) - 2, "%s.%.3d", left, nr);
1515 * Only for local blocks: external en indirect blocks already have a
1518 * return true: created a new name
1521 bool new_id(ListBase *lb, ID *id, const char *tname)
1524 char name[MAX_ID_NAME - 2];
1526 /* if library, don't rename */
1527 if (ID_IS_LINKED_DATABLOCK(id))
1530 /* if no libdata given, look up based on ID */
1532 lb = which_libbase(G.main, GS(id->name));
1534 /* if no name given, use name of current ID
1535 * else make a copy (tname args can be const) */
1537 tname = id->name + 2;
1539 BLI_strncpy(name, tname, sizeof(name));
1541 if (name[0] == '\0') {
1542 /* disallow empty names */
1543 BLI_strncpy(name, DATA_(ID_FALLBACK_NAME), sizeof(name));
1546 /* disallow non utf8 chars,
1547 * the interface checks for this but new ID's based on file names don't */
1548 BLI_utf8_invalid_strip(name, strlen(name));
1551 result = check_for_dupid(lb, id, name);
1552 strcpy(id->name + 2, name);
1554 /* This was in 2.43 and previous releases
1555 * however all data in blender should be sorted, not just duplicate names
1556 * sorting should not hurt, but noting just incase it alters the way other
1557 * functions work, so sort every time */
1560 id_sort_by_name(lb, id);
1563 id_sort_by_name(lb, id);
1569 * Pull an ID out of a library (make it local). Only call this for IDs that
1570 * don't have other library users.
1572 void id_clear_lib_data_ex(Main *bmain, ID *id, const bool id_in_mainlist)
1574 bNodeTree *ntree = NULL;
1577 BKE_id_lib_local_paths(bmain, id->lib, id);
1579 id_fake_user_clear(id);
1582 id->tag &= ~(LIB_TAG_INDIRECT | LIB_TAG_EXTERN);
1584 new_id(which_libbase(bmain, GS(id->name)), id, NULL);
1586 /* Internal bNodeTree blocks inside datablocks also stores id->lib, make sure this stays in sync. */
1587 if ((ntree = ntreeFromID(id))) {
1588 id_clear_lib_data_ex(bmain, &ntree->id, false); /* Datablocks' nodetree is never in Main. */
1591 /* Same goes for shapekeys. */
1592 if ((key = BKE_key_from_id(id))) {
1593 id_clear_lib_data_ex(bmain, &key->id, id_in_mainlist); /* sigh, why are keys in Main? */
1596 if (GS(id->name) == ID_OB) {
1597 Object *object = (Object *)id;
1598 if (object->proxy_from != NULL) {
1599 object->proxy_from->proxy = NULL;
1600 object->proxy_from->proxy_group = NULL;
1602 object->proxy = object->proxy_from = object->proxy_group = NULL;
1606 void id_clear_lib_data(Main *bmain, ID *id)
1608 id_clear_lib_data_ex(bmain, id, true);
1611 /* next to indirect usage in read/writefile also in editobject.c scene.c */
1612 void BKE_main_id_clear_newpoins(Main *bmain)
1614 ListBase *lbarray[MAX_LIBARRAY];
1618 a = set_listbasepointers(bmain, lbarray);
1620 id = lbarray[a]->first;
1623 id->tag &= ~LIB_TAG_NEW;
1629 /** Make linked datablocks local.
1631 * \param bmain Almost certainly G.main.
1632 * \param lib If not NULL, only make local datablocks from this library.
1633 * \param untagged_only If true, only make local datablocks not tagged with LIB_TAG_PRE_EXISTING.
1634 * \param set_fake If true, set fake user on all localized datablocks (except group and objects ones).
1636 /* XXX TODO This function should probably be reworked.
1638 * Old (2.77) version was simply making (tagging) datablocks as local, without actually making any check whether
1639 * they were also indirectly used or not...
1641 * Current version uses regular id_make_local callback, but this is not super-efficient since this ends up
1642 * duplicating some IDs and then removing original ones (due to missing knowledge of which ID uses some other ID).
1644 * We could first check all IDs and detect those to be made local that are only used by other local or future-local
1645 * datablocks, and directly tag those as local (instead of going through id_make_local) maybe...
1647 * We'll probably need at some point a true dependency graph between datablocks, but for now this should work
1648 * good enough (performances is not a critical point here anyway).
1650 void BKE_library_make_local(Main *bmain, const Library *lib, const bool untagged_only, const bool set_fake)
1652 ListBase *lbarray[MAX_LIBARRAY];
1656 for (a = set_listbasepointers(bmain, lbarray); a--; ) {
1657 id = lbarray[a]->first;
1659 /* Do not explicitly make local non-linkable IDs (shapekeys, in fact), they are assumed to be handled
1660 * by real datablocks responsible of them. */
1661 const bool do_skip = (id && !BKE_idcode_is_linkable(GS(id->name)));
1663 for (; id; id = id_next) {
1665 id_next = id->next; /* id is possibly being inserted again */
1667 /* The check on the second line (LIB_TAG_PRE_EXISTING) is done so its
1668 * possible to tag data you don't want to be made local, used for
1669 * appending data, so any libdata already linked wont become local
1670 * (very nasty to discover all your links are lost after appending)
1672 if (!do_skip && id->tag & (LIB_TAG_EXTERN | LIB_TAG_INDIRECT | LIB_TAG_NEW) &&
1673 ((untagged_only == false) || !(id->tag & LIB_TAG_PRE_EXISTING)))
1675 if (lib == NULL || id->lib == lib) {
1677 /* In this specific case, we do want to make ID local even if it has no local usage yet... */
1678 id_make_local(bmain, id, false, true);
1681 id->tag &= ~(LIB_TAG_EXTERN | LIB_TAG_INDIRECT | LIB_TAG_NEW);
1686 if (!ELEM(GS(id->name), ID_OB, ID_GR)) {
1687 /* do not set fake user on objects, groups (instancing) */
1688 id_fake_user_set(id);
1695 /* We have to remap local usages of old (linked) ID to new (local) id in a second loop, as lbarray ordering is not
1696 * enough to ensure us we did catch all dependencies (e.g. if making local a parent object before its child...).
1698 for (a = set_listbasepointers(bmain, lbarray); a--; ) {
1699 for (id = lbarray[a]->first; id; id = id->next) {
1701 BKE_libblock_remap(bmain, id, id->newid, ID_REMAP_SKIP_INDIRECT_USAGE);
1706 /* Third step: remove datablocks that have been copied to be localized and are no more used in the end...
1707 * Note that we may have to loop more than once here, to tackle dependencies between linked objects... */
1708 bool do_loop = true;
1711 for (a = set_listbasepointers(bmain, lbarray); a--; ) {
1712 for (id = lbarray[a]->first; id; id = id_next) {
1715 bool is_local = false, is_lib = false;
1717 BKE_library_ID_test_usages(bmain, id, &is_local, &is_lib);
1719 /* Attempt to re-link appended proxy objects. This allows appending of an entire scene
1720 * from another blend file into this one, even when that blend file contains proxified
1721 * armatures. Since the proxified object needs to be linked (not local), this will
1722 * only work when the "Localize all" checkbox is disabled.
1723 * TL;DR: this is a dirty hack on top of an already weak feature (proxies). */
1724 if (GS(id->name) == ID_OB && ((Object *)id)->proxy != NULL) {
1725 Object *ob = (Object *)id;
1726 Object *ob_new = (Object *)id->newid;
1728 /* Proxies only work when the proxied object is linked-in from a library. */
1729 if (ob->proxy->id.lib == NULL) {
1730 printf("Warning, proxy object %s will loose its link to %s, because the "
1731 "proxied object is local.\n", id->newid->name, ob->proxy->id.name);
1733 /* We can only switch the proxy'ing to a made-local proxy if it is no longer
1734 * referred to from a library. Not checking for local use; if new local proxy
1735 * was not used locally would be a nasty bug! */
1736 else if (is_local || is_lib) {
1737 printf("Warning, made-local proxy object %s will loose its link to %s, "
1738 "because the linked-in proxy is referenced (is_local=%i, is_lib=%i).\n",
1739 id->newid->name, ob->proxy->id.name, is_local, is_lib);
1742 /* we can switch the proxy'ing from the linked-in to the made-local proxy. */
1743 BKE_object_make_proxy(ob_new, ob->proxy, ob->proxy_group);
1744 ob->proxy = ob->proxy_from = ob->proxy_group = NULL;
1748 if (!is_local && !is_lib) {
1749 BKE_libblock_free(bmain, id);
1759 * Use after setting the ID's name
1760 * When name exists: call 'new_id'
1762 void BLI_libblock_ensure_unique_name(Main *bmain, const char *name)
1768 lb = which_libbase(bmain, GS(name));
1769 if (lb == NULL) return;
1772 idtest = BLI_findstring(lb, name + 2, offsetof(ID, name) + 2);
1774 if (idtest && !new_id(lb, idtest, idtest->name + 2)) {
1775 id_sort_by_name(lb, idtest);
1780 * Sets the name of a block to name, suitably adjusted for uniqueness.
1782 void BKE_libblock_rename(Main *bmain, ID *id, const char *name)
1784 ListBase *lb = which_libbase(bmain, GS(id->name));
1785 new_id(lb, id, name);
1789 * Returns in name the name of the block, with a 3-character prefix prepended
1790 * indicating whether it comes from a library, has a fake user, or no users.
1792 void BKE_id_ui_prefix(char name[MAX_ID_NAME + 1], const ID *id)
1794 name[0] = id->lib ? (ID_MISSING(id) ? 'M' : 'L') : ' ';
1795 name[1] = (id->flag & LIB_FAKEUSER) ? 'F' : ((id->us == 0) ? '0' : ' ');
1798 strcpy(name + 3, id->name + 2);
1801 void BKE_library_filepath_set(Library *lib, const char *filepath)
1803 /* in some cases this is used to update the absolute path from the
1805 if (lib->name != filepath) {
1806 BLI_strncpy(lib->name, filepath, sizeof(lib->name));
1809 BLI_strncpy(lib->filepath, filepath, sizeof(lib->filepath));
1811 /* not essential but set filepath is an absolute copy of value which
1812 * is more useful if its kept in sync */
1813 if (BLI_path_is_rel(lib->filepath)) {
1814 /* note that the file may be unsaved, in this case, setting the
1815 * filepath on an indirectly linked path is not allowed from the
1816 * outliner, and its not really supported but allow from here for now
1817 * since making local could cause this to be directly linked - campbell
1819 const char *basepath = lib->parent ? lib->parent->filepath : G.main->name;
1820 BLI_path_abs(lib->filepath, basepath);