Undo: unified undo system w/ linear history
[blender.git] / source / blender / blenkernel / intern / library.c
index 0a681a75f845bc81158659ef8c3333781690a186..10b724f9f69706b6f169b6165898611390b8a132 100644 (file)
@@ -45,6 +45,7 @@
 #include "DNA_anim_types.h"
 #include "DNA_armature_types.h"
 #include "DNA_brush_types.h"
+#include "DNA_cachefile_types.h"
 #include "DNA_camera_types.h"
 #include "DNA_group_types.h"
 #include "DNA_gpencil_types.h"
@@ -56,6 +57,7 @@
 #include "DNA_material_types.h"
 #include "DNA_mesh_types.h"
 #include "DNA_meta_types.h"
+#include "DNA_modifier_types.h"
 #include "DNA_movieclip_types.h"
 #include "DNA_mask_types.h"
 #include "DNA_node_types.h"
 
 #include "BLI_blenlib.h"
 #include "BLI_utildefines.h"
+#include "BLI_ghash.h"
+#include "BLI_linklist.h"
+#include "BLI_memarena.h"
+#include "BLI_mempool.h"
+#include "BLI_string_utils.h"
 
 #include "BLI_threads.h"
 #include "BLT_translation.h"
 #include "BKE_bpath.h"
 #include "BKE_brush.h"
 #include "BKE_camera.h"
+#include "BKE_cachefile.h"
 #include "BKE_context.h"
 #include "BKE_curve.h"
 #include "BKE_depsgraph.h"
-#include "BKE_fcurve.h"
 #include "BKE_font.h"
 #include "BKE_global.h"
 #include "BKE_group.h"
 #include "BKE_idcode.h"
 #include "BKE_idprop.h"
 #include "BKE_image.h"
-#include "BKE_ipo.h"
 #include "BKE_key.h"
 #include "BKE_lamp.h"
 #include "BKE_lattice.h"
 #include "BKE_library.h"
 #include "BKE_library_query.h"
+#include "BKE_library_remap.h"
 #include "BKE_linestyle.h"
 #include "BKE_mesh.h"
 #include "BKE_material.h"
 #include "BKE_main.h"
 #include "BKE_mball.h"
-#include "BKE_movieclip.h"
 #include "BKE_mask.h"
+#include "BKE_movieclip.h"
 #include "BKE_node.h"
 #include "BKE_object.h"
 #include "BKE_paint.h"
 #include "BKE_particle.h"
 #include "BKE_packedFile.h"
-#include "BKE_speaker.h"
 #include "BKE_sound.h"
-#include "BKE_screen.h"
+#include "BKE_speaker.h"
 #include "BKE_scene.h"
 #include "BKE_text.h"
 #include "BKE_texture.h"
 #include "IMB_imbuf.h"
 #include "IMB_imbuf_types.h"
 
-#ifdef WITH_PYTHON
-#include "BPY_extern.h"
+#include "atomic_ops.h"
+
+//#define DEBUG_TIME
+
+#ifdef DEBUG_TIME
+#  include "PIL_time_utildefines.h"
 #endif
 
 /* GS reads the memory pointed at in a specific ordering. 
@@ -152,7 +162,7 @@ void BKE_id_lib_local_paths(Main *bmain, Library *lib, ID *id)
 
 void id_lib_extern(ID *id)
 {
-       if (id) {
+       if (id && ID_IS_LINKED(id)) {
                BLI_assert(BKE_idcode_is_linkable(GS(id->name)));
                if (id->tag & LIB_TAG_INDIRECT) {
                        id->tag -= LIB_TAG_INDIRECT;
@@ -182,7 +192,6 @@ void id_us_ensure_real(ID *id)
        }
 }
 
-/* Unused currently... */
 void id_us_clear_real(ID *id)
 {
        if (id && (id->tag & LIB_TAG_EXTRAUSER)) {
@@ -232,9 +241,7 @@ void id_us_min(ID *id)
                if (id->us <= limit) {
                        printf("ID user decrement error: %s (from '%s'): %d <= %d\n",
                               id->name, id->lib ? id->lib->filepath : "[Main]", id->us, limit);
-                       /* We cannot assert here, because of how we 'delete' datablocks currently (setting their usercount to zero),
-                        * this is weak but it's how it works for now. */
-                       /* BLI_assert(0); */
+                       BLI_assert(0);
                        id->us = limit;
                }
                else {
@@ -264,235 +271,416 @@ void id_fake_user_clear(ID *id)
        }
 }
 
-/* calls the appropriate make_local method for the block, unless test. Returns true
- * if the block can be made local. */
-bool id_make_local(ID *id, bool test)
+void BKE_id_clear_newpoin(ID *id)
+{
+       if (id->newid) {
+               id->newid->tag &= ~LIB_TAG_NEW;
+       }
+       id->newid = NULL;
+}
+
+static int id_expand_local_callback(
+        void *UNUSED(user_data), struct ID *id_self, struct ID **id_pointer, int cb_flag)
+{
+       if (cb_flag & IDWALK_CB_PRIVATE) {
+               return IDWALK_RET_NOP;
+       }
+
+       /* Can hapen that we get unlinkable ID here, e.g. with shapekey referring to itself (through drivers)...
+        * Just skip it, shape key can only be either indirectly linked, or fully local, period.
+        * And let's curse one more time that stupid useless shapekey ID type! */
+       if (*id_pointer && *id_pointer != id_self && BKE_idcode_is_linkable(GS((*id_pointer)->name))) {
+               id_lib_extern(*id_pointer);
+       }
+
+       return IDWALK_RET_NOP;
+}
+
+/**
+ * Expand ID usages of given id as 'extern' (and no more indirect) linked data. Used by ID copy/make_local functions.
+ */
+void BKE_id_expand_local(Main *bmain, ID *id)
+{
+       BKE_library_foreach_ID_link(bmain, id, id_expand_local_callback, NULL, IDWALK_READONLY);
+}
+
+/**
+ * Ensure new (copied) ID is fully made local.
+ */
+void BKE_id_copy_ensure_local(Main *bmain, const ID *old_id, ID *new_id)
+{
+       if (ID_IS_LINKED(old_id)) {
+               BKE_id_expand_local(bmain, new_id);
+               BKE_id_lib_local_paths(bmain, old_id->lib, new_id);
+       }
+}
+
+/**
+ * Generic 'make local' function, works for most of datablock types...
+ */
+void BKE_id_make_local_generic(Main *bmain, ID *id, const bool id_in_mainlist, const bool lib_local)
+{
+       bool is_local = false, is_lib = false;
+
+       /* - only lib users: do nothing (unless force_local is set)
+        * - only local users: set flag
+        * - mixed: make copy
+        * In case we make a whole lib's content local, we always want to localize, and we skip remapping (done later).
+        */
+
+       if (!ID_IS_LINKED(id)) {
+               return;
+       }
+
+       BKE_library_ID_test_usages(bmain, id, &is_local, &is_lib);
+
+       if (lib_local || is_local) {
+               if (!is_lib) {
+                       id_clear_lib_data_ex(bmain, id, id_in_mainlist);
+                       BKE_id_expand_local(bmain, id);
+               }
+               else {
+                       ID *id_new;
+
+                       /* Should not fail in expected usecases, but id_copy does not copy Scene e.g. */
+                       if (id_copy(bmain, id, &id_new, false)) {
+                               id_new->us = 0;
+
+                               /* setting newid is mandatory for complex make_lib_local logic... */
+                               ID_NEW_SET(id, id_new);
+                               Key *key = BKE_key_from_id(id), *key_new = BKE_key_from_id(id);
+                               if (key && key_new) {
+                                       ID_NEW_SET(key, key_new);
+                               }
+                               bNodeTree *ntree = ntreeFromID(id), *ntree_new = ntreeFromID(id_new);
+                               if (ntree && ntree_new) {
+                                       ID_NEW_SET(ntree, ntree_new);
+                               }
+
+                               if (!lib_local) {
+                                       BKE_libblock_remap(bmain, id, id_new, ID_REMAP_SKIP_INDIRECT_USAGE);
+                               }
+                       }
+               }
+       }
+}
+
+/**
+ * Calls the appropriate make_local method for the block, unless test is set.
+ *
+ * \note Always set ID->newid pointer in case it gets duplicated...
+ *
+ * \param lib_local Special flag used when making a whole library's content local, it needs specific handling.
+ *
+ * \return true if the block can be made local.
+ */
+bool id_make_local(Main *bmain, ID *id, const bool test, const bool lib_local)
 {
-       if (id->tag & LIB_TAG_INDIRECT)
+       /* We don't care whether ID is directly or indirectly linked in case we are making a whole lib local... */
+       if (!lib_local && (id->tag & LIB_TAG_INDIRECT)) {
                return false;
+       }
 
-       switch (GS(id->name)) {
+       switch ((ID_Type)GS(id->name)) {
                case ID_SCE:
-                       return false; /* not implemented */
-               case ID_LI:
-                       return false; /* can't be linked */
+                       if (!test) BKE_scene_make_local(bmain, (Scene *)id, lib_local);
+                       return true;
                case ID_OB:
-                       if (!test) BKE_object_make_local((Object *)id);
+                       if (!test) BKE_object_make_local(bmain, (Object *)id, lib_local);
                        return true;
                case ID_ME:
-                       if (!test) {
-                               BKE_mesh_make_local((Mesh *)id);
-                               BKE_key_make_local(((Mesh *)id)->key);
-                       }
+                       if (!test) BKE_mesh_make_local(bmain, (Mesh *)id, lib_local);
                        return true;
                case ID_CU:
-                       if (!test) {
-                               BKE_curve_make_local((Curve *)id);
-                               BKE_key_make_local(((Curve *)id)->key);
-                       }
+                       if (!test) BKE_curve_make_local(bmain, (Curve *)id, lib_local);
                        return true;
                case ID_MB:
-                       if (!test) BKE_mball_make_local((MetaBall *)id);
+                       if (!test) BKE_mball_make_local(bmain, (MetaBall *)id, lib_local);
                        return true;
                case ID_MA:
-                       if (!test) BKE_material_make_local((Material *)id);
+                       if (!test) BKE_material_make_local(bmain, (Material *)id, lib_local);
                        return true;
                case ID_TE:
-                       if (!test) BKE_texture_make_local((Tex *)id);
+                       if (!test) BKE_texture_make_local(bmain, (Tex *)id, lib_local);
                        return true;
                case ID_IM:
-                       if (!test) BKE_image_make_local((Image *)id);
+                       if (!test) BKE_image_make_local(bmain, (Image *)id, lib_local);
                        return true;
                case ID_LT:
-                       if (!test) {
-                               BKE_lattice_make_local((Lattice *)id);
-                               BKE_key_make_local(((Lattice *)id)->key);
-                       }
+                       if (!test) BKE_lattice_make_local(bmain, (Lattice *)id, lib_local);
                        return true;
                case ID_LA:
-                       if (!test) BKE_lamp_make_local((Lamp *)id);
+                       if (!test) BKE_lamp_make_local(bmain, (Lamp *)id, lib_local);
                        return true;
                case ID_CA:
-                       if (!test) BKE_camera_make_local((Camera *)id);
+                       if (!test) BKE_camera_make_local(bmain, (Camera *)id, lib_local);
                        return true;
                case ID_SPK:
-                       if (!test) BKE_speaker_make_local((Speaker *)id);
-                       return true;
-               case ID_IP:
-                       return false; /* deprecated */
-               case ID_KE:
-                       if (!test) BKE_key_make_local((Key *)id);
+                       if (!test) BKE_speaker_make_local(bmain, (Speaker *)id, lib_local);
                        return true;
                case ID_WO:
-                       if (!test) BKE_world_make_local((World *)id);
+                       if (!test) BKE_world_make_local(bmain, (World *)id, lib_local);
                        return true;
-               case ID_SCR:
-                       return false; /* can't be linked */
                case ID_VF:
-                       return false; /* not implemented */
+                       if (!test) BKE_vfont_make_local(bmain, (VFont *)id, lib_local);
+                       return true;
                case ID_TXT:
-                       return false; /* not implemented */
+                       if (!test) BKE_text_make_local(bmain, (Text *)id, lib_local);
+                       return true;
                case ID_SO:
-                       return false; /* not implemented */
+                       if (!test) BKE_sound_make_local(bmain, (bSound *)id, lib_local);
+                       return true;
                case ID_GR:
-                       return false; /* not implemented */
+                       if (!test) BKE_group_make_local(bmain, (Group *)id, lib_local);
+                       return true;
                case ID_AR:
-                       if (!test) BKE_armature_make_local((bArmature *)id);
+                       if (!test) BKE_armature_make_local(bmain, (bArmature *)id, lib_local);
                        return true;
                case ID_AC:
-                       if (!test) BKE_action_make_local((bAction *)id);
+                       if (!test) BKE_action_make_local(bmain, (bAction *)id, lib_local);
                        return true;
                case ID_NT:
-                       if (!test) ntreeMakeLocal((bNodeTree *)id, true);
+                       if (!test) ntreeMakeLocal(bmain, (bNodeTree *)id, true, lib_local);
                        return true;
                case ID_BR:
-                       if (!test) BKE_brush_make_local((Brush *)id);
+                       if (!test) BKE_brush_make_local(bmain, (Brush *)id, lib_local);
                        return true;
                case ID_PA:
-                       if (!test) BKE_particlesettings_make_local((ParticleSettings *)id);
+                       if (!test) BKE_particlesettings_make_local(bmain, (ParticleSettings *)id, lib_local);
                        return true;
-               case ID_WM:
-                       return false; /* can't be linked */
                case ID_GD:
-                       return false; /* not implemented */
+                       if (!test) BKE_gpencil_make_local(bmain, (bGPdata *)id, lib_local);
+                       return true;
+               case ID_MC:
+                       if (!test) BKE_movieclip_make_local(bmain, (MovieClip *)id, lib_local);
+                       return true;
+               case ID_MSK:
+                       if (!test) BKE_mask_make_local(bmain, (Mask *)id, lib_local);
+                       return true;
                case ID_LS:
-                       return false; /* not implemented */
+                       if (!test) BKE_linestyle_make_local(bmain, (FreestyleLineStyle *)id, lib_local);
+                       return true;
+               case ID_PAL:
+                       if (!test) BKE_palette_make_local(bmain, (Palette *)id, lib_local);
+                       return true;
+               case ID_PC:
+                       if (!test) BKE_paint_curve_make_local(bmain, (PaintCurve *)id, lib_local);
+                       return true;
+               case ID_CF:
+                       if (!test) BKE_cachefile_make_local(bmain, (CacheFile *)id, lib_local);
+                       return true;
+               case ID_SCR:
+               case ID_LI:
+               case ID_KE:
+               case ID_WM:
+                       return false; /* can't be linked */
+               case ID_IP:
+                       return false; /* deprecated */
        }
 
        return false;
 }
 
+struct IDCopyLibManagementData {
+       const ID *id_src;
+       ID *id_dst;
+       int flag;
+};
+
+/* Increases usercount as required, and remap self ID pointers. */
+static int id_copy_libmanagement_cb(void *user_data, ID *UNUSED(id_self), ID **id_pointer, int cb_flag)
+{
+       struct IDCopyLibManagementData *data = user_data;
+       ID *id = *id_pointer;
+
+       /* Remap self-references to new copied ID. */
+       if (id == data->id_src) {
+               /* We cannot use id_self here, it is not *always* id_dst (thanks to $£!+@#&/? nodetrees). */
+               id = *id_pointer = data->id_dst;
+       }
+
+       /* Increase used IDs refcount if needed and required. */
+       if ((data->flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0 && (cb_flag & IDWALK_CB_USER)) {
+               id_us_plus(id);
+       }
+
+       return IDWALK_RET_NOP;
+}
+
 /**
- * Invokes the appropriate copy method for the block and returns the result in
- * newid, unless test. Returns true if the block can be copied.
+ * Generic entry point for copying a datablock (new API).
+ *
+ * \note Copy is only affecting given data-block (no ID used by copied one will be affected, besides usercount).
+ *       There is only one exception, if LIB_ID_COPY_ACTIONS is defined, actions used by animdata will be duplicated.
+ *
+ * \note Usercount of new copy is always set to 1.
+ *
+ * \param bmain Main database, may be NULL only if LIB_ID_CREATE_NO_MAIN is specified.
+ * \param id Source datablock.
+ * \param r_newid Pointer to new (copied) ID pointer.
+ * \param flag Set of copy options, see DNA_ID.h enum for details (leave to zero for default, full copy).
+ * \param test If set, do not do any copy, just test whether copy is supported.
+ * \return False when copying that ID type is not supported, true otherwise.
  */
-bool id_copy(ID *id, ID **newid, bool test)
+/* XXX TODO remove test thing, *all* IDs should be copyable that way! */
+bool BKE_id_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag, const bool test)
 {
-       if (!test) *newid = NULL;
+#define LIB_ID_TYPES_NOCOPY ID_LI, ID_SCR, ID_WM,  /* Not supported */ \
+                            ID_IP  /* Deprecated */
+
+       BLI_assert(test || (r_newid != NULL));
+       /* Early output is source is NULL. */
+       if (id == NULL) {
+               return false;
+       }
+       /* Make sure destination pointer is all good. */
+       if ((flag & LIB_ID_CREATE_NO_ALLOCATE) == 0) {
+               if (r_newid != NULL) {
+                       *r_newid = NULL;
+               }
+       }
+       else {
+               if (r_newid != NULL && *r_newid != NULL) {
+                       /* Allow some garbage non-initialized memory to go in. */
+                       const size_t size = BKE_libblock_get_alloc_info(GS(id->name), NULL);
+                       memset(*r_newid, 0, size);
+               }
+       }
+       if (ELEM(GS(id->name), LIB_ID_TYPES_NOCOPY)) {
+               return false;
+       }
+       else if (test) {
+               return true;
+       }
+
+       BKE_libblock_copy_ex(bmain, id, r_newid, flag);
 
-       /* conventions:
-        * - make shallow copy, only this ID block
-        * - id.us of the new ID is set to 1 */
-       switch (GS(id->name)) {
+       switch ((ID_Type)GS(id->name)) {
                case ID_SCE:
-                       return false;  /* can't be copied from here */
-               case ID_LI:
-                       return false;  /* can't be copied from here */
+                       BKE_scene_copy_data(bmain, (Scene *)*r_newid, (Scene *)id, flag);
+                       break;
                case ID_OB:
-                       if (!test) *newid = (ID *)BKE_object_copy((Object *)id);
-                       return true;
+                       BKE_object_copy_data(bmain, (Object *)*r_newid, (Object *)id, flag);
+                       break;
                case ID_ME:
-                       if (!test) *newid = (ID *)BKE_mesh_copy((Mesh *)id);
-                       return true;
+                       BKE_mesh_copy_data(bmain, (Mesh *)*r_newid, (Mesh *)id, flag);
+                       break;
                case ID_CU:
-                       if (!test) *newid = (ID *)BKE_curve_copy((Curve *)id);
-                       return true;
+                       BKE_curve_copy_data(bmain, (Curve *)*r_newid, (Curve *)id, flag);
+                       break;
                case ID_MB:
-                       if (!test) *newid = (ID *)BKE_mball_copy((MetaBall *)id);
-                       return true;
+                       BKE_mball_copy_data(bmain, (MetaBall *)*r_newid, (MetaBall *)id, flag);
+                       break;
                case ID_MA:
-                       if (!test) *newid = (ID *)BKE_material_copy((Material *)id);
-                       return true;
+                       BKE_material_copy_data(bmain, (Material *)*r_newid, (Material *)id, flag);
+                       break;
                case ID_TE:
-                       if (!test) *newid = (ID *)BKE_texture_copy((Tex *)id);
-                       return true;
+                       BKE_texture_copy_data(bmain, (Tex *)*r_newid, (Tex *)id, flag);
+                       break;
                case ID_IM:
-                       if (!test) *newid = (ID *)BKE_image_copy(G.main, (Image *)id);
-                       return true;
+                       BKE_image_copy_data(bmain, (Image *)*r_newid, (Image *)id, flag);
+                       break;
                case ID_LT:
-                       if (!test) *newid = (ID *)BKE_lattice_copy((Lattice *)id);
-                       return true;
+                       BKE_lattice_copy_data(bmain, (Lattice *)*r_newid, (Lattice *)id, flag);
+                       break;
                case ID_LA:
-                       if (!test) *newid = (ID *)BKE_lamp_copy((Lamp *)id);
-                       return true;
+                       BKE_lamp_copy_data(bmain, (Lamp *)*r_newid, (Lamp *)id, flag);
+                       break;
                case ID_SPK:
-                       if (!test) *newid = (ID *)BKE_speaker_copy((Speaker *)id);
-                       return true;
+                       BKE_speaker_copy_data(bmain, (Speaker *)*r_newid, (Speaker *)id, flag);
+                       break;
                case ID_CA:
-                       if (!test) *newid = (ID *)BKE_camera_copy((Camera *)id);
-                       return true;
-               case ID_IP:
-                       return false;  /* deprecated */
+                       BKE_camera_copy_data(bmain, (Camera *)*r_newid, (Camera *)id, flag);
+                       break;
                case ID_KE:
-                       if (!test) *newid = (ID *)BKE_key_copy((Key *)id);
-                       return true;
+                       BKE_key_copy_data(bmain, (Key *)*r_newid, (Key *)id, flag);
+                       break;
                case ID_WO:
-                       if (!test) *newid = (ID *)BKE_world_copy((World *)id);
-                       return true;
-               case ID_SCR:
-                       return false;  /* can't be copied from here */
-               case ID_VF:
-                       return false;  /* not implemented */
+                       BKE_world_copy_data(bmain, (World *)*r_newid, (World *)id, flag);
+                       break;
                case ID_TXT:
-                       if (!test) *newid = (ID *)BKE_text_copy(G.main, (Text *)id);
-                       return true;
-               case ID_SO:
-                       return false;  /* not implemented */
+                       BKE_text_copy_data(bmain, (Text *)*r_newid, (Text *)id, flag);
+                       break;
                case ID_GR:
-                       if (!test) *newid = (ID *)BKE_group_copy((Group *)id);
-                       return true;
+                       BKE_group_copy_data(bmain, (Group *)*r_newid, (Group *)id, flag);
+                       break;
                case ID_AR:
-                       if (!test) *newid = (ID *)BKE_armature_copy((bArmature *)id);
-                       return true;
+                       BKE_armature_copy_data(bmain, (bArmature *)*r_newid, (bArmature *)id, flag);
+                       break;
                case ID_AC:
-                       if (!test) *newid = (ID *)BKE_action_copy((bAction *)id);
-                       return true;
+                       BKE_action_copy_data(bmain, (bAction *)*r_newid, (bAction *)id, flag);
+                       break;
                case ID_NT:
-                       if (!test) *newid = (ID *)ntreeCopyTree((bNodeTree *)id);
-                       return true;
+                       BKE_node_tree_copy_data(bmain, (bNodeTree *)*r_newid, (bNodeTree *)id, flag);
+                       break;
                case ID_BR:
-                       if (!test) *newid = (ID *)BKE_brush_copy((Brush *)id);
-                       return true;
+                       BKE_brush_copy_data(bmain, (Brush *)*r_newid, (Brush *)id, flag);
+                       break;
                case ID_PA:
-                       if (!test) *newid = (ID *)BKE_particlesettings_copy((ParticleSettings *)id);
-                       return true;
-               case ID_WM:
-                       return false;  /* can't be copied from here */
+                       BKE_particlesettings_copy_data(bmain, (ParticleSettings *)*r_newid, (ParticleSettings *)id, flag);
+                       break;
                case ID_GD:
-                       if (!test) *newid = (ID *)gpencil_data_duplicate((bGPdata *)id, false);
-                       return true;
+                       BKE_gpencil_copy_data(bmain, (bGPdata *)*r_newid, (bGPdata *)id, flag);
+                       break;
+               case ID_MC:
+                       BKE_movieclip_copy_data(bmain, (MovieClip *)*r_newid, (MovieClip *)id, flag);
+                       break;
                case ID_MSK:
-                       if (!test) *newid = (ID *)BKE_mask_copy((Mask *)id);
-                       return true;
+                       BKE_mask_copy_data(bmain, (Mask *)*r_newid, (Mask *)id, flag);
+                       break;
                case ID_LS:
-                       if (!test) *newid = (ID *)BKE_linestyle_copy(G.main, (FreestyleLineStyle *)id);
-                       return true;
-       }
-       
-       return false;
-}
-
-bool id_unlink(ID *id, int test)
-{
-       Main *mainlib = G.main;
-       short type = GS(id->name);
-
-       switch (type) {
-               case ID_TXT:
-                       if (test) return true;
-                       BKE_text_unlink(mainlib, (Text *)id);
+                       BKE_linestyle_copy_data(bmain, (FreestyleLineStyle *)*r_newid, (FreestyleLineStyle *)id, flag);
                        break;
-               case ID_GR:
-                       if (test) return true;
-                       BKE_group_unlink(mainlib, (Group *)id);
+               case ID_PAL:
+                       BKE_palette_copy_data(bmain, (Palette *)*r_newid, (Palette *)id, flag);
                        break;
-               case ID_OB:
-                       if (test) return true;
-                       BKE_object_unlink(mainlib, (Object *)id);
+               case ID_PC:
+                       BKE_paint_curve_copy_data(bmain, (PaintCurve *)*r_newid, (PaintCurve *)id, flag);
+                       break;
+               case ID_CF:
+                       BKE_cachefile_copy_data(bmain, (CacheFile *)*r_newid, (CacheFile *)id, flag);
+                       break;
+               case ID_SO:
+                       BKE_sound_copy_data(bmain, (bSound *)*r_newid, (bSound *)id, flag);
+                       break;
+               case ID_VF:
+                       BKE_vfont_copy_data(bmain, (VFont *)*r_newid, (VFont *)id, flag);
+                       break;
+               case ID_LI:
+               case ID_SCR:
+               case ID_WM:
+               case ID_IP:
+                       BLI_assert(0);  /* Should have been rejected at start of function! */
                        break;
        }
 
-       if (id->us == 0) {
-               if (test) return true;
+       /* Update ID refcount, remap pointers to self in new ID. */
+       struct IDCopyLibManagementData data = {.id_src = id, .id_dst = *r_newid, .flag = flag};
+       BKE_library_foreach_ID_link(bmain, *r_newid, id_copy_libmanagement_cb, &data, IDWALK_NOP);
 
-               BKE_libblock_free(mainlib, id);
-
-               return true;
+       /* Do not make new copy local in case we are copying outside of main...
+        * XXX TODO: is this behavior OK, or should we need own flag to control that? */
+       if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
+               BLI_assert((flag & LIB_ID_COPY_KEEP_LIB) == 0);
+               BKE_id_copy_ensure_local(bmain, id, *r_newid);
+       }
+       else {
+               (*r_newid)->lib = id->lib;
        }
 
-       return false;
+       return true;
+}
+
+/**
+ * Invokes the appropriate copy method for the block and returns the result in
+ * newid, unless test. Returns true if the block can be copied.
+ */
+bool id_copy(Main *bmain, const ID *id, ID **newid, bool test)
+{
+       return BKE_id_copy_ex(bmain, id, newid, 0, test);
 }
 
+/** Does *not* set ID->newid pointer. */
 bool id_single_user(bContext *C, ID *id, PointerRNA *ptr, PropertyRNA *prop)
 {
        ID *newid = NULL;
@@ -501,13 +689,13 @@ bool id_single_user(bContext *C, ID *id, PointerRNA *ptr, PropertyRNA *prop)
        if (id) {
                /* if property isn't editable, we're going to have an extra block hanging around until we save */
                if (RNA_property_editable(ptr, prop)) {
-                       if (id_copy(id, &newid, false) && newid) {
+                       if (id_copy(CTX_data_main(C), id, &newid, false) && newid) {
                                /* copy animation actions too */
-                               BKE_animdata_copy_id_action(id);
+                               BKE_animdata_copy_id_action(id, false);
                                /* us is 1 by convention, but RNA_property_pointer_set
                                 * will also increment it, so set it to zero */
                                newid->us = 0;
-                               
+
                                /* assign copy */
                                RNA_id_pointer_create(newid, &idptr);
                                RNA_property_pointer_set(ptr, prop, idptr);
@@ -521,48 +709,145 @@ bool id_single_user(bContext *C, ID *id, PointerRNA *ptr, PropertyRNA *prop)
        return false;
 }
 
-ListBase *which_libbase(Main *mainlib, short type)
+static int libblock_management_us_plus(void *UNUSED(user_data), ID *UNUSED(id_self), ID **id_pointer, int cb_flag)
 {
-       switch (type) {
-               case ID_SCE:
-                       return &(mainlib->scene);
-               case ID_LI:
-                       return &(mainlib->library);
-               case ID_OB:
-                       return &(mainlib->object);
-               case ID_ME:
-                       return &(mainlib->mesh);
-               case ID_CU:
-                       return &(mainlib->curve);
-               case ID_MB:
-                       return &(mainlib->mball);
-               case ID_MA:
-                       return &(mainlib->mat);
-               case ID_TE:
-                       return &(mainlib->tex);
-               case ID_IM:
-                       return &(mainlib->image);
-               case ID_LT:
-                       return &(mainlib->latt);
-               case ID_LA:
-                       return &(mainlib->lamp);
-               case ID_CA:
-                       return &(mainlib->camera);
-               case ID_IP:
-                       return &(mainlib->ipo);
-               case ID_KE:
-                       return &(mainlib->key);
-               case ID_WO:
-                       return &(mainlib->world);
-               case ID_SCR:
-                       return &(mainlib->screen);
-               case ID_VF:
-                       return &(mainlib->vfont);
-               case ID_TXT:
-                       return &(mainlib->text);
-               case ID_SPK:
-                       return &(mainlib->speaker);
-               case ID_SO:
+       if (cb_flag & IDWALK_CB_USER) {
+               id_us_plus(*id_pointer);
+       }
+       if (cb_flag & IDWALK_CB_USER_ONE) {
+               id_us_ensure_real(*id_pointer);
+       }
+
+       return IDWALK_RET_NOP;
+}
+
+static int libblock_management_us_min(void *UNUSED(user_data), ID *UNUSED(id_self), ID **id_pointer, int cb_flag)
+{
+       if (cb_flag & IDWALK_CB_USER) {
+               id_us_min(*id_pointer);
+       }
+       /* We can do nothing in IDWALK_CB_USER_ONE case! */
+
+       return IDWALK_RET_NOP;
+}
+
+/** Add a 'NO_MAIN' datablock to given main (also sets usercounts of its IDs if needed). */
+void BKE_libblock_management_main_add(Main *bmain, void *idv)
+{
+       ID *id = idv;
+
+       BLI_assert(bmain != NULL);
+       if ((id->tag & LIB_TAG_NO_MAIN) == 0) {
+               return;
+       }
+
+       if ((id->tag & LIB_TAG_NOT_ALLOCATED) != 0) {
+               /* We cannot add non-allocated ID to Main! */
+               return;
+       }
+
+       /* We cannot allow non-userrefcounting IDs in Main database! */
+       if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) != 0) {
+               BKE_library_foreach_ID_link(bmain, id, libblock_management_us_plus, NULL, IDWALK_NOP);
+       }
+
+       ListBase *lb = which_libbase(bmain, GS(id->name));
+       BKE_main_lock(bmain);
+       BLI_addtail(lb, id);
+       new_id(lb, id, NULL);
+       /* alphabetic insertion: is in new_id */
+       id->tag &= ~(LIB_TAG_NO_MAIN | LIB_TAG_NO_USER_REFCOUNT);
+       bmain->is_memfile_undo_written = false;
+       BKE_main_unlock(bmain);
+}
+
+/** Remove a datablock from given main (set it to 'NO_MAIN' status). */
+void BKE_libblock_management_main_remove(Main *bmain, void *idv)
+{
+       ID *id = idv;
+
+       BLI_assert(bmain != NULL);
+       if ((id->tag & LIB_TAG_NO_MAIN) != 0) {
+               return;
+       }
+
+       /* For now, allow userrefcounting IDs to get out of Main - can be handy in some cases... */
+
+       ListBase *lb = which_libbase(bmain, GS(id->name));
+       BKE_main_lock(bmain);
+       BLI_remlink(lb, id);
+       id->tag |= LIB_TAG_NO_MAIN;
+       bmain->is_memfile_undo_written = false;
+       BKE_main_unlock(bmain);
+}
+
+void BKE_libblock_management_usercounts_set(Main *bmain, void *idv)
+{
+       ID *id = idv;
+
+       if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) == 0) {
+               return;
+       }
+
+       BKE_library_foreach_ID_link(bmain, id, libblock_management_us_plus, NULL, IDWALK_NOP);
+       id->tag &= ~LIB_TAG_NO_USER_REFCOUNT;
+}
+
+void BKE_libblock_management_usercounts_clear(Main *bmain, void *idv)
+{
+       ID *id = idv;
+
+       /* We do not allow IDs in Main database to not be userrefcounting. */
+       if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) != 0 || (id->tag & LIB_TAG_NO_MAIN) != 0) {
+               return;
+       }
+
+       BKE_library_foreach_ID_link(bmain, id, libblock_management_us_min, NULL, IDWALK_NOP);
+       id->tag |= LIB_TAG_NO_USER_REFCOUNT;
+}
+
+ListBase *which_libbase(Main *mainlib, short type)
+{
+       switch ((ID_Type)type) {
+               case ID_SCE:
+                       return &(mainlib->scene);
+               case ID_LI:
+                       return &(mainlib->library);
+               case ID_OB:
+                       return &(mainlib->object);
+               case ID_ME:
+                       return &(mainlib->mesh);
+               case ID_CU:
+                       return &(mainlib->curve);
+               case ID_MB:
+                       return &(mainlib->mball);
+               case ID_MA:
+                       return &(mainlib->mat);
+               case ID_TE:
+                       return &(mainlib->tex);
+               case ID_IM:
+                       return &(mainlib->image);
+               case ID_LT:
+                       return &(mainlib->latt);
+               case ID_LA:
+                       return &(mainlib->lamp);
+               case ID_CA:
+                       return &(mainlib->camera);
+               case ID_IP:
+                       return &(mainlib->ipo);
+               case ID_KE:
+                       return &(mainlib->key);
+               case ID_WO:
+                       return &(mainlib->world);
+               case ID_SCR:
+                       return &(mainlib->screen);
+               case ID_VF:
+                       return &(mainlib->vfont);
+               case ID_TXT:
+                       return &(mainlib->text);
+               case ID_SPK:
+                       return &(mainlib->speaker);
+               case ID_SO:
                        return &(mainlib->sound);
                case ID_GR:
                        return &(mainlib->group);
@@ -590,6 +875,8 @@ ListBase *which_libbase(Main *mainlib, short type)
                        return &(mainlib->palettes);
                case ID_PC:
                        return &(mainlib->paintcurves);
+               case ID_CF:
+                       return &(mainlib->cachefiles);
        }
        return NULL;
 }
@@ -674,7 +961,7 @@ void BKE_main_lib_objects_recalc_all(Main *bmain)
 
        /* flag for full recalc */
        for (ob = bmain->object.first; ob; ob = ob->id.next) {
-               if (ob->id.lib) {
+               if (ID_IS_LINKED(ob)) {
                        DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
                }
        }
@@ -691,59 +978,56 @@ void BKE_main_lib_objects_recalc_all(Main *bmain)
  * \note MAX_LIBARRAY define should match this code */
 int set_listbasepointers(Main *main, ListBase **lb)
 {
-       int a = 0;
-
        /* BACKWARDS! also watch order of free-ing! (mesh<->mat), first items freed last.
         * This is important because freeing data decreases usercounts of other datablocks,
         * if this data is its self freed it can crash. */
-       lb[a++] = &(main->library);  /* Libraries may be accessed from pretty much any other ID... */
-       lb[a++] = &(main->ipo);
-       lb[a++] = &(main->action); /* moved here to avoid problems when freeing with animato (aligorith) */
-       lb[a++] = &(main->key);
-       lb[a++] = &(main->gpencil); /* referenced by nodes, objects, view, scene etc, before to free after. */
-       lb[a++] = &(main->nodetree);
-       lb[a++] = &(main->image);
-       lb[a++] = &(main->tex);
-       lb[a++] = &(main->mat);
-       lb[a++] = &(main->vfont);
+       lb[INDEX_ID_LI] = &(main->library);  /* Libraries may be accessed from pretty much any other ID... */
+       lb[INDEX_ID_IP] = &(main->ipo);
+       lb[INDEX_ID_AC] = &(main->action); /* moved here to avoid problems when freeing with animato (aligorith) */
+       lb[INDEX_ID_KE] = &(main->key);
+       lb[INDEX_ID_GD] = &(main->gpencil); /* referenced by nodes, objects, view, scene etc, before to free after. */
+       lb[INDEX_ID_NT] = &(main->nodetree);
+       lb[INDEX_ID_IM] = &(main->image);
+       lb[INDEX_ID_TE] = &(main->tex);
+       lb[INDEX_ID_MA] = &(main->mat);
+       lb[INDEX_ID_VF] = &(main->vfont);
        
        /* Important!: When adding a new object type,
         * the specific data should be inserted here 
         */
 
-       lb[a++] = &(main->armature);
-
-       lb[a++] = &(main->mesh);
-       lb[a++] = &(main->curve);
-       lb[a++] = &(main->mball);
-
-       lb[a++] = &(main->latt);
-       lb[a++] = &(main->lamp);
-       lb[a++] = &(main->camera);
-
-       lb[a++] = &(main->text);
-       lb[a++] = &(main->sound);
-       lb[a++] = &(main->group);
-       lb[a++] = &(main->palettes);
-       lb[a++] = &(main->paintcurves);
-       lb[a++] = &(main->brush);
-       lb[a++] = &(main->particle);
-       lb[a++] = &(main->speaker);
-
-       lb[a++] = &(main->world);
-       lb[a++] = &(main->movieclip);
-       lb[a++] = &(main->screen);
-       lb[a++] = &(main->object);
-       lb[a++] = &(main->linestyle); /* referenced by scenes */
-       lb[a++] = &(main->scene);
-       lb[a++] = &(main->wm);
-       lb[a++] = &(main->mask);
+       lb[INDEX_ID_AR] = &(main->armature);
+
+       lb[INDEX_ID_CF] = &(main->cachefiles);
+       lb[INDEX_ID_ME] = &(main->mesh);
+       lb[INDEX_ID_CU] = &(main->curve);
+       lb[INDEX_ID_MB] = &(main->mball);
+
+       lb[INDEX_ID_LT] = &(main->latt);
+       lb[INDEX_ID_LA] = &(main->lamp);
+       lb[INDEX_ID_CA] = &(main->camera);
+
+       lb[INDEX_ID_TXT] = &(main->text);
+       lb[INDEX_ID_SO]  = &(main->sound);
+       lb[INDEX_ID_GR]  = &(main->group);
+       lb[INDEX_ID_PAL] = &(main->palettes);
+       lb[INDEX_ID_PC]  = &(main->paintcurves);
+       lb[INDEX_ID_BR]  = &(main->brush);
+       lb[INDEX_ID_PA]  = &(main->particle);
+       lb[INDEX_ID_SPK] = &(main->speaker);
+
+       lb[INDEX_ID_WO]  = &(main->world);
+       lb[INDEX_ID_MC]  = &(main->movieclip);
+       lb[INDEX_ID_SCR] = &(main->screen);
+       lb[INDEX_ID_OB]  = &(main->object);
+       lb[INDEX_ID_LS]  = &(main->linestyle); /* referenced by scenes */
+       lb[INDEX_ID_SCE] = &(main->scene);
+       lb[INDEX_ID_WM]  = &(main->wm);
+       lb[INDEX_ID_MSK] = &(main->mask);
        
-       lb[a] = NULL;
+       lb[INDEX_ID_NULL] = NULL;
 
-       BLI_assert(a + 1 == MAX_LIBARRAY);
-
-       return a;
+       return (MAX_LIBARRAY - 1);
 }
 
 /* *********** ALLOC AND FREE *****************
@@ -756,116 +1040,73 @@ int set_listbasepointers(Main *main, ListBase **lb)
  *
  * **************************** */
 
+/**
+ * Get allocation size fo a given datablock type and optionally allocation name.
+ */
+size_t BKE_libblock_get_alloc_info(short type, const char **name)
+{
+#define CASE_RETURN(id_code, type)  \
+       case id_code:                   \
+               do {                        \
+                       if (name != NULL) {     \
+                               *name = #type;      \
+                       }                       \
+                       return sizeof(type);    \
+               } while(0)
+
+       switch ((ID_Type)type) {
+               CASE_RETURN(ID_SCE, Scene);
+               CASE_RETURN(ID_LI,  Library);
+               CASE_RETURN(ID_OB,  Object);
+               CASE_RETURN(ID_ME,  Mesh);
+               CASE_RETURN(ID_CU,  Curve);
+               CASE_RETURN(ID_MB,  MetaBall);
+               CASE_RETURN(ID_MA,  Material);
+               CASE_RETURN(ID_TE,  Tex);
+               CASE_RETURN(ID_IM,  Image);
+               CASE_RETURN(ID_LT,  Lattice);
+               CASE_RETURN(ID_LA,  Lamp);
+               CASE_RETURN(ID_CA,  Camera);
+               CASE_RETURN(ID_IP,  Ipo);
+               CASE_RETURN(ID_KE,  Key);
+               CASE_RETURN(ID_WO,  World);
+               CASE_RETURN(ID_SCR, bScreen);
+               CASE_RETURN(ID_VF,  VFont);
+               CASE_RETURN(ID_TXT, Text);
+               CASE_RETURN(ID_SPK, Speaker);
+               CASE_RETURN(ID_SO,  bSound);
+               CASE_RETURN(ID_GR,  Group);
+               CASE_RETURN(ID_AR,  bArmature);
+               CASE_RETURN(ID_AC,  bAction);
+               CASE_RETURN(ID_NT,  bNodeTree);
+               CASE_RETURN(ID_BR,  Brush);
+               CASE_RETURN(ID_PA,  ParticleSettings);
+               CASE_RETURN(ID_WM,  wmWindowManager);
+               CASE_RETURN(ID_GD,  bGPdata);
+               CASE_RETURN(ID_MC,  MovieClip);
+               CASE_RETURN(ID_MSK, Mask);
+               CASE_RETURN(ID_LS,  FreestyleLineStyle);
+               CASE_RETURN(ID_PAL, Palette);
+               CASE_RETURN(ID_PC,  PaintCurve);
+               CASE_RETURN(ID_CF,  CacheFile);
+       }
+       return 0;
+#undef CASE_RETURN
+}
+
 /**
  * Allocates and returns memory of the right size for the specified block type,
  * initialized to zero.
  */
 void *BKE_libblock_alloc_notest(short type)
 {
-       ID *id = NULL;
-       
-       switch (type) {
-               case ID_SCE:
-                       id = MEM_callocN(sizeof(Scene), "scene");
-                       break;
-               case ID_LI:
-                       id = MEM_callocN(sizeof(Library), "library");
-                       break;
-               case ID_OB:
-                       id = MEM_callocN(sizeof(Object), "object");
-                       break;
-               case ID_ME:
-                       id = MEM_callocN(sizeof(Mesh), "mesh");
-                       break;
-               case ID_CU:
-                       id = MEM_callocN(sizeof(Curve), "curve");
-                       break;
-               case ID_MB:
-                       id = MEM_callocN(sizeof(MetaBall), "mball");
-                       break;
-               case ID_MA:
-                       id = MEM_callocN(sizeof(Material), "mat");
-                       break;
-               case ID_TE:
-                       id = MEM_callocN(sizeof(Tex), "tex");
-                       break;
-               case ID_IM:
-                       id = MEM_callocN(sizeof(Image), "image");
-                       break;
-               case ID_LT:
-                       id = MEM_callocN(sizeof(Lattice), "latt");
-                       break;
-               case ID_LA:
-                       id = MEM_callocN(sizeof(Lamp), "lamp");
-                       break;
-               case ID_CA:
-                       id = MEM_callocN(sizeof(Camera), "camera");
-                       break;
-               case ID_IP:
-                       id = MEM_callocN(sizeof(Ipo), "ipo");
-                       break;
-               case ID_KE:
-                       id = MEM_callocN(sizeof(Key), "key");
-                       break;
-               case ID_WO:
-                       id = MEM_callocN(sizeof(World), "world");
-                       break;
-               case ID_SCR:
-                       id = MEM_callocN(sizeof(bScreen), "screen");
-                       break;
-               case ID_VF:
-                       id = MEM_callocN(sizeof(VFont), "vfont");
-                       break;
-               case ID_TXT:
-                       id = MEM_callocN(sizeof(Text), "text");
-                       break;
-               case ID_SPK:
-                       id = MEM_callocN(sizeof(Speaker), "speaker");
-                       break;
-               case ID_SO:
-                       id = MEM_callocN(sizeof(bSound), "sound");
-                       break;
-               case ID_GR:
-                       id = MEM_callocN(sizeof(Group), "group");
-                       break;
-               case ID_AR:
-                       id = MEM_callocN(sizeof(bArmature), "armature");
-                       break;
-               case ID_AC:
-                       id = MEM_callocN(sizeof(bAction), "action");
-                       break;
-               case ID_NT:
-                       id = MEM_callocN(sizeof(bNodeTree), "nodetree");
-                       break;
-               case ID_BR:
-                       id = MEM_callocN(sizeof(Brush), "brush");
-                       break;
-               case ID_PA:
-                       id = MEM_callocN(sizeof(ParticleSettings), "ParticleSettings");
-                       break;
-               case ID_WM:
-                       id = MEM_callocN(sizeof(wmWindowManager), "Window manager");
-                       break;
-               case ID_GD:
-                       id = MEM_callocN(sizeof(bGPdata), "Grease Pencil");
-                       break;
-               case ID_MC:
-                       id = MEM_callocN(sizeof(MovieClip), "Movie Clip");
-                       break;
-               case ID_MSK:
-                       id = MEM_callocN(sizeof(Mask), "Mask");
-                       break;
-               case ID_LS:
-                       id = MEM_callocN(sizeof(FreestyleLineStyle), "Freestyle Line Style");
-                       break;
-               case ID_PAL:
-                       id = MEM_callocN(sizeof(Palette), "Palette");
-                       break;
-               case ID_PC:
-                       id = MEM_callocN(sizeof(PaintCurve), "Paint Curve");
-                       break;
+       const char *name;
+       size_t size = BKE_libblock_get_alloc_info(type, &name);
+       if (size != 0) {
+               return MEM_callocN(size, name);
        }
-       return id;
+       BLI_assert(!"Request to allocate unknown data type");
+       return NULL;
 }
 
 /**
@@ -874,23 +1115,45 @@ void *BKE_libblock_alloc_notest(short type)
  * The user count is set to 1, all other content (apart from name and links) being
  * initialized to zero.
  */
-void *BKE_libblock_alloc(Main *bmain, short type, const char *name)
+void *BKE_libblock_alloc(Main *bmain, short type, const char *name, const int flag)
 {
-       ID *id = NULL;
-       ListBase *lb = which_libbase(bmain, type);
-       
-       id = BKE_libblock_alloc_notest(type);
+       BLI_assert((flag & LIB_ID_CREATE_NO_ALLOCATE) == 0);
+
+       ID *id = BKE_libblock_alloc_notest(type);
+
        if (id) {
-               BKE_main_lock(bmain);
-               BLI_addtail(lb, id);
-               id->us = 1;
+               if ((flag & LIB_ID_CREATE_NO_MAIN) != 0) {
+                       id->tag |= LIB_TAG_NO_MAIN;
+               }
+               if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) != 0) {
+                       id->tag |= LIB_TAG_NO_USER_REFCOUNT;
+               }
+
                id->icon_id = 0;
-               *( (short *)id->name) = type;
-               new_id(lb, id, name);
-               /* alphabetic insertion: is in new_id */
-               BKE_main_unlock(bmain);
+               *((short *)id->name) = type;
+               if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
+                       id->us = 1;
+               }
+               if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
+                       ListBase *lb = which_libbase(bmain, type);
+
+                       BKE_main_lock(bmain);
+                       BLI_addtail(lb, id);
+                       new_id(lb, id, name);
+                       bmain->is_memfile_undo_written = false;
+                       /* alphabetic insertion: is in new_id */
+                       BKE_main_unlock(bmain);
+
+                       /* TODO to be removed from here! */
+                       if ((flag & LIB_ID_CREATE_NO_DEG_TAG) == 0) {
+                               DAG_id_type_tag(bmain, type);
+                       }
+               }
+               else {
+                       BLI_strncpy(id->name + 2, name, sizeof(id->name) - 2);
+               }
        }
-       DAG_id_type_tag(bmain, type);
+
        return id;
 }
 
@@ -901,7 +1164,7 @@ void *BKE_libblock_alloc(Main *bmain, short type, const char *name)
 void BKE_libblock_init_empty(ID *id)
 {
        /* Note that only ID types that are not valid when filled of zero should have a callback here. */
-       switch (GS(id->name)) {
+       switch ((ID_Type)GS(id->name)) {
                case ID_SCE:
                        BKE_scene_init((Scene *)id);
                        break;
@@ -942,401 +1205,159 @@ void BKE_libblock_init_empty(ID *id)
                case ID_SPK:
                        BKE_speaker_init((Speaker *)id);
                        break;
-               case ID_CA:
-                       BKE_camera_init((Camera *)id);
-                       break;
-               case ID_IP:
-                       /* Should not be needed - animation from lib pre-2.5 is broken anyway. */
-                       BLI_assert(0);
-                       break;
-               case ID_KE:
-                       /* Shapekeys are a complex topic too - they depend on their 'user' data type...
-                        * They are not linkable, though, so it should never reach here anyway. */
-                       BLI_assert(0);
-                       break;
-               case ID_WO:
-                       BKE_world_init((World *)id);
-                       break;
-               case ID_SCR:
-                       /* Nothing to do. */
-                       break;
-               case ID_VF:
-                       BKE_vfont_init((VFont *)id);
-                       break;
-               case ID_TXT:
-                       BKE_text_init((Text *)id);
-                       break;
-               case ID_SO:
-                       /* Another fuzzy case, think NULLified content is OK here... */
-                       break;
-               case ID_GR:
-                       /* Nothing to do. */
-                       break;
-               case ID_AR:
-                       /* Nothing to do. */
-                       break;
-               case ID_AC:
-                       /* Nothing to do. */
-                       break;
-               case ID_NT:
-                       ntreeInitDefault((bNodeTree *)id);
-                       break;
-               case ID_BR:
-                       BKE_brush_init((Brush *)id);
-                       break;
-               case ID_PA:
-                       /* Nothing to do. */
-                       break;
-               case ID_PC:
-                       /* Nothing to do. */
-                       break;
-               case ID_WM:
-                       /* We should never reach this. */
-                       BLI_assert(0);
-                       break;
-               case ID_GD:
-                       /* Nothing to do. */
-                       break;
-               case ID_MSK:
-                       /* Nothing to do. */
-                       break;
-               case ID_LS:
-                       BKE_linestyle_init((FreestyleLineStyle *)id);
-                       break;
-       }
-}
-
-/* by spec, animdata is first item after ID */
-/* and, trust that BKE_animdata_from_id() will only find AnimData for valid ID-types */
-static void id_copy_animdata(ID *id, const bool do_action)
-{
-       AnimData *adt = BKE_animdata_from_id(id);
-       
-       if (adt) {
-               IdAdtTemplate *iat = (IdAdtTemplate *)id;
-               iat->adt = BKE_animdata_copy(iat->adt, do_action); /* could be set to false, need to investigate */
-       }
-}
-
-/* material nodes use this since they are not treated as libdata */
-void BKE_libblock_copy_data(ID *id, const ID *id_from, const bool do_action)
-{
-       if (id_from->properties)
-               id->properties = IDP_CopyProperty(id_from->properties);
-
-       /* the duplicate should get a copy of the animdata */
-       id_copy_animdata(id, do_action);
-}
-
-/* used everywhere in blenkernel */
-void *BKE_libblock_copy_ex(Main *bmain, ID *id)
-{
-       ID *idn;
-       size_t idn_len;
-
-       idn = BKE_libblock_alloc(bmain, GS(id->name), id->name + 2);
-
-       assert(idn != NULL);
-
-       idn_len = MEM_allocN_len(idn);
-       if ((int)idn_len - (int)sizeof(ID) > 0) { /* signed to allow neg result */
-               const char *cp = (const char *)id;
-               char *cpn = (char *)idn;
-
-               memcpy(cpn + sizeof(ID), cp + sizeof(ID), idn_len - sizeof(ID));
-       }
-       
-       id->newid = idn;
-       idn->tag |= LIB_TAG_NEW;
-
-       BKE_libblock_copy_data(idn, id, false);
-       
-       return idn;
-}
-
-void *BKE_libblock_copy_nolib(ID *id, const bool do_action)
-{
-       ID *idn;
-       size_t idn_len;
-
-       idn = BKE_libblock_alloc_notest(GS(id->name));
-       assert(idn != NULL);
-
-       BLI_strncpy(idn->name, id->name, sizeof(idn->name));
-
-       idn_len = MEM_allocN_len(idn);
-       if ((int)idn_len - (int)sizeof(ID) > 0) { /* signed to allow neg result */
-               const char *cp = (const char *)id;
-               char *cpn = (char *)idn;
-
-               memcpy(cpn + sizeof(ID), cp + sizeof(ID), idn_len - sizeof(ID));
-       }
-
-       id->newid = idn;
-       idn->tag |= LIB_TAG_NEW;
-       idn->us = 1;
-
-       BKE_libblock_copy_data(idn, id, do_action);
-
-       return idn;
-}
-
-void *BKE_libblock_copy(ID *id)
-{
-       return BKE_libblock_copy_ex(G.main, id);
-}
-
-static int id_relink_looper(void *UNUSED(user_data), ID *UNUSED(self_id), ID **id_pointer, const int cd_flag)
-{
-       ID *id = *id_pointer;
-       if (id) {
-               /* See: NEW_ID macro */
-               if (id->newid) {
-                       BKE_library_update_ID_link_user(id->newid, id, cd_flag);
-                       *id_pointer = id->newid;
-               }
-               else if (id->tag & LIB_TAG_NEW) {
-                       id->tag &= ~LIB_TAG_NEW;
-                       BKE_libblock_relink(id);
-               }
-       }
-       return IDWALK_RET_NOP;
-}
-
-void BKE_libblock_relink(ID *id)
-{
-       if (id->lib)
-               return;
-
-       BKE_library_foreach_ID_link(id, id_relink_looper, NULL, 0);
-}
-
-static void BKE_library_free(Library *lib)
-{
-       if (lib->packedfile)
-               freePackedFile(lib->packedfile);
-}
-
-static BKE_library_free_window_manager_cb free_windowmanager_cb = NULL;
-
-void BKE_library_callback_free_window_manager_set(BKE_library_free_window_manager_cb func)
-{
-       free_windowmanager_cb = func;
-}
-
-static BKE_library_free_notifier_reference_cb free_notifier_reference_cb = NULL;
-
-void BKE_library_callback_free_notifier_reference_set(BKE_library_free_notifier_reference_cb func)
-{
-       free_notifier_reference_cb = func;
-}
-
-static BKE_library_free_editor_id_reference_cb free_editor_id_reference_cb = NULL;
-
-void BKE_library_callback_free_editor_id_reference_set(BKE_library_free_editor_id_reference_cb func)
-{
-       free_editor_id_reference_cb = func;
-}
-
-static void animdata_dtar_clear_cb(ID *UNUSED(id), AnimData *adt, void *userdata)
-{
-       ChannelDriver *driver;
-       FCurve *fcu;
-
-       /* find the driver this belongs to and update it */
-       for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
-               driver = fcu->driver;
-               
-               if (driver) {
-                       DriverVar *dvar;
-                       for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
-                               DRIVER_TARGETS_USED_LOOPER(dvar) 
-                               {
-                                       if (dtar->id == userdata)
-                                               dtar->id = NULL;
-                               }
-                               DRIVER_TARGETS_LOOPER_END
-                       }
-               }
-       }
-}
-
-void BKE_libblock_free_data(Main *bmain, ID *id)
-{
-       if (id->properties) {
-               IDP_FreeProperty(id->properties);
-               MEM_freeN(id->properties);
-       }
-       
-       /* this ID may be a driver target! */
-       BKE_animdata_main_cb(bmain, animdata_dtar_clear_cb, (void *)id);
-}
-
-/* used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c */
-void BKE_libblock_free_ex(Main *bmain, void *idv, bool do_id_user)
-{
-       ID *id = idv;
-       short type = GS(id->name);
-       ListBase *lb = which_libbase(bmain, type);
-
-       DAG_id_type_tag(bmain, type);
-
-#ifdef WITH_PYTHON
-       BPY_id_release(id);
-#endif
-
-       switch (type) {    /* GetShort from util.h */
-               case ID_SCE:
-                       BKE_scene_free((Scene *)id);
-                       break;
-               case ID_LI:
-                       BKE_library_free((Library *)id);
-                       break;
-               case ID_OB:
-                       BKE_object_free_ex((Object *)id, do_id_user);
-                       break;
-               case ID_ME:
-                       BKE_mesh_free((Mesh *)id, 1);
-                       break;
-               case ID_CU:
-                       BKE_curve_free((Curve *)id);
-                       break;
-               case ID_MB:
-                       BKE_mball_free((MetaBall *)id);
-                       break;
-               case ID_MA:
-                       BKE_material_free((Material *)id);
-                       break;
-               case ID_TE:
-                       BKE_texture_free((Tex *)id);
-                       break;
-               case ID_IM:
-                       BKE_image_free((Image *)id);
-                       break;
-               case ID_LT:
-                       BKE_lattice_free((Lattice *)id);
-                       break;
-               case ID_LA:
-                       BKE_lamp_free((Lamp *)id);
-                       break;
-               case ID_CA:
-                       BKE_camera_free((Camera *) id);
-                       break;
-               case ID_IP:
-                       BKE_ipo_free((Ipo *)id);
-                       break;
-               case ID_KE:
-                       BKE_key_free((Key *)id);
+               case ID_CA:
+                       BKE_camera_init((Camera *)id);
                        break;
                case ID_WO:
-                       BKE_world_free((World *)id);
+                       BKE_world_init((World *)id);
                        break;
                case ID_SCR:
-                       BKE_screen_free((bScreen *)id);
+                       /* Nothing to do. */
                        break;
                case ID_VF:
-                       BKE_vfont_free((VFont *)id);
+                       BKE_vfont_init((VFont *)id);
                        break;
                case ID_TXT:
-                       BKE_text_free((Text *)id);
-                       break;
-               case ID_SPK:
-                       BKE_speaker_free((Speaker *)id);
+                       BKE_text_init((Text *)id);
                        break;
                case ID_SO:
-                       BKE_sound_free((bSound *)id);
+                       /* Another fuzzy case, think NULLified content is OK here... */
                        break;
                case ID_GR:
-                       BKE_group_free((Group *)id);
+                       /* Nothing to do. */
                        break;
                case ID_AR:
-                       BKE_armature_free((bArmature *)id);
+                       /* Nothing to do. */
                        break;
                case ID_AC:
-                       BKE_action_free((bAction *)id);
+                       /* Nothing to do. */
                        break;
                case ID_NT:
-                       ntreeFreeTree_ex((bNodeTree *)id, do_id_user);
+                       ntreeInitDefault((bNodeTree *)id);
                        break;
                case ID_BR:
-                       BKE_brush_free((Brush *)id);
+                       BKE_brush_init((Brush *)id);
                        break;
                case ID_PA:
-                       BKE_particlesettings_free((ParticleSettings *)id);
+                       /* Nothing to do. */
                        break;
-               case ID_WM:
-                       if (free_windowmanager_cb)
-                               free_windowmanager_cb(NULL, (wmWindowManager *)id);
+               case ID_PC:
+                       /* Nothing to do. */
                        break;
                case ID_GD:
-                       BKE_gpencil_free((bGPdata *)id);
-                       break;
-               case ID_MC:
-                       BKE_movieclip_free((MovieClip *)id);
+                       /* Nothing to do. */
                        break;
                case ID_MSK:
-                       BKE_mask_free(bmain, (Mask *)id);
+                       /* Nothing to do. */
                        break;
                case ID_LS:
-                       BKE_linestyle_free((FreestyleLineStyle *)id);
+                       BKE_linestyle_init((FreestyleLineStyle *)id);
                        break;
-               case ID_PAL:
-                       BKE_palette_free((Palette *)id);
+               case ID_CF:
+                       BKE_cachefile_init((CacheFile *)id);
                        break;
-               case ID_PC:
-                       BKE_paint_curve_free((PaintCurve *)id);
+               case ID_KE:
+                       /* Shapekeys are a complex topic too - they depend on their 'user' data type...
+                        * They are not linkable, though, so it should never reach here anyway. */
+                       BLI_assert(0);
+                       break;
+               case ID_WM:
+                       /* We should never reach this. */
+                       BLI_assert(0);
+                       break;
+               case ID_IP:
+                       /* Should not be needed - animation from lib pre-2.5 is broken anyway. */
+                       BLI_assert(0);
                        break;
+               default:
+                       BLI_assert(0);  /* Should never reach this point... */
        }
+}
 
-       /* avoid notifying on removed data */
-       BKE_main_lock(bmain);
+/* by spec, animdata is first item after ID */
+/* and, trust that BKE_animdata_from_id() will only find AnimData for valid ID-types */
+static void id_copy_animdata(Main *bmain, ID *id, const bool do_action)
+{
+       AnimData *adt = BKE_animdata_from_id(id);
+       
+       if (adt) {
+               IdAdtTemplate *iat = (IdAdtTemplate *)id;
+               iat->adt = BKE_animdata_copy(bmain, iat->adt, do_action); /* could be set to false, need to investigate */
+       }
+}
 
-       if (free_notifier_reference_cb) {
-               free_notifier_reference_cb(id);
+void BKE_libblock_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag)
+{
+       ID *new_id = *r_newid;
+
+       /* Grrrrrrrrr... Not adding 'root' nodetrees to bmain.... grrrrrrrrrrrrrrrrrrrr! */
+       /* This is taken from original ntree copy code, might be weak actually? */
+       const bool use_nodetree_alloc_exception = ((GS(id->name) == ID_NT) && (bmain != NULL) &&
+                                                  (BLI_findindex(&bmain->nodetree, id) < 0));
+
+       BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) != 0 || bmain != NULL);
+       BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) != 0 || (flag & LIB_ID_CREATE_NO_ALLOCATE) == 0);
+       BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) == 0 || (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) != 0);
+
+       if ((flag & LIB_ID_CREATE_NO_ALLOCATE) != 0) {
+               /* r_newid already contains pointer to allocated memory. */
+               /* TODO do we want to memset(0) whole mem before filling it? */
+               BLI_strncpy(new_id->name, id->name, sizeof(new_id->name));
+               new_id->us = 0;
+               new_id->tag |= LIB_TAG_NOT_ALLOCATED | LIB_TAG_NO_MAIN | LIB_TAG_NO_USER_REFCOUNT;
+               /* TODO Do we want/need to copy more from ID struct itself? */
+       }
+       else {
+               new_id = BKE_libblock_alloc(bmain, GS(id->name), id->name + 2, flag | (use_nodetree_alloc_exception ? LIB_ID_CREATE_NO_MAIN : 0));
        }
+       BLI_assert(new_id != NULL);
 
-       if (free_editor_id_reference_cb) {
-               free_editor_id_reference_cb(id);
+       const size_t id_len = BKE_libblock_get_alloc_info(GS(new_id->name), NULL);
+       const size_t id_offset = sizeof(ID);
+       if ((int)id_len - (int)id_offset > 0) { /* signed to allow neg result */ /* XXX ????? */
+               const char *cp = (const char *)id;
+               char *cpn = (char *)new_id;
+
+               memcpy(cpn + id_offset, cp + id_offset, id_len - id_offset);
        }
 
-       BLI_remlink(lb, id);
+       if (id->properties) {
+               new_id->properties = IDP_CopyProperty_ex(id->properties, flag);
+       }
 
-       BKE_libblock_free_data(bmain, id);
-       BKE_main_unlock(bmain);
+       /* the duplicate should get a copy of the animdata */
+       id_copy_animdata(bmain, new_id, (flag & LIB_ID_COPY_ACTIONS) != 0 && (flag & LIB_ID_CREATE_NO_MAIN) == 0);
+
+       if ((flag & LIB_ID_CREATE_NO_DEG_TAG) == 0 && (flag & LIB_ID_CREATE_NO_MAIN) == 0) {
+               DAG_id_type_tag(bmain, GS(new_id->name));
+       }
 
-       MEM_freeN(id);
+       *r_newid = new_id;
 }
 
-void BKE_libblock_free(Main *bmain, void *idv)
+/* used everywhere in blenkernel */
+void *BKE_libblock_copy(Main *bmain, const ID *id)
 {
-       BKE_libblock_free_ex(bmain, idv, true);
+       ID *idn;
+
+       BKE_libblock_copy_ex(bmain, id, &idn, 0);
+
+       return idn;
 }
 
-void BKE_libblock_free_us(Main *bmain, void *idv)      /* test users */
+void *BKE_libblock_copy_nolib(const ID *id, const bool do_action)
 {
-       ID *id = idv;
-       
-       id_us_min(id);
+       ID *idn;
 
-       /* XXX This is a temp (2.77) hack so that we keep same behavior as in 2.76 regarding groups when deleting an object.
-        *     Since only 'user_one' usage of objects is groups, and only 'real user' usage of objects is scenes,
-        *     removing that 'user_one' tag when there is no more real (scene) users of an object ensures it gets
-        *     fully unlinked.
-        *     Otherwise, there is no real way to get rid of an object anymore - better handling of this is TODO.
-        */
-       if ((GS(id->name) == ID_OB) && (id->us == 1)) {
-               id_us_clear_real(id);
-       }
+       BKE_libblock_copy_ex(NULL, id, &idn, LIB_ID_CREATE_NO_MAIN | LIB_ID_CREATE_NO_USER_REFCOUNT | (do_action ? LIB_ID_COPY_ACTIONS : 0));
 
-       if (id->us == 0) {
-               switch (GS(id->name)) {
-                       case ID_OB:
-                               BKE_object_unlink(bmain, (Object *)id);
-                               break;
-               }
-               
-               BKE_libblock_free(bmain, id);
-       }
+       return idn;
+}
+
+void BKE_library_free(Library *lib)
+{
+       if (lib->packedfile)
+               freePackedFile(lib->packedfile);
 }
 
 Main *BKE_main_new(void)
@@ -1363,45 +1384,46 @@ void BKE_main_free(Main *mainvar)
                
                while ( (id = lb->first) ) {
 #if 1
-                       BKE_libblock_free_ex(mainvar, id, false);
+                       BKE_libblock_free_ex(mainvar, id, false, false);
 #else
                        /* errors freeing ID's can be hard to track down,
                         * enable this so valgrind will give the line number in its error log */
                        switch (a) {
-                               case   0: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case   1: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case   2: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case   3: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case   4: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case   5: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case   6: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case   7: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case   8: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case   9: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  10: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  11: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  12: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  13: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  14: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  15: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  16: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  17: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  18: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  19: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  20: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  21: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  22: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  23: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  24: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  25: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  26: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  27: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  28: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  29: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  30: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  31: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  32: BKE_libblock_free_ex(mainvar, id, false); break;
-                               case  33: BKE_libblock_free_ex(mainvar, id, false); break;
+                               case   0: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case   1: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case   2: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case   3: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case   4: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case   5: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case   6: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case   7: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case   8: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case   9: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  10: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  11: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  12: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  13: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  14: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  15: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  16: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  17: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  18: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  19: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  20: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  21: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  22: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  23: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  24: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  25: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  26: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  27: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  28: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  29: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  30: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  31: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  32: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  33: BKE_libblock_free_ex(mainvar, id, false, false); break;
+                               case  34: BKE_libblock_free_ex(mainvar, id, false, false); break;
                                default:
                                        BLI_assert(0);
                                        break;
@@ -1410,6 +1432,10 @@ void BKE_main_free(Main *mainvar)
                }
        }
 
+       if (mainvar->relations) {
+               BKE_main_relations_free(mainvar);
+       }
+
        BLI_spin_end((SpinLock *)mainvar->lock);
        MEM_freeN(mainvar->lock);
        DEG_evaluation_context_free(mainvar->eval_ctx);
@@ -1426,6 +1452,78 @@ void BKE_main_unlock(struct Main *bmain)
        BLI_spin_unlock((SpinLock *) bmain->lock);
 }
 
+
+static int main_relations_create_cb(void *user_data, ID *id_self, ID **id_pointer, int cb_flag)
+{
+       MainIDRelations *rel = user_data;
+
+       if (*id_pointer) {
+               MainIDRelationsEntry *entry, **entry_p;
+
+               entry = BLI_mempool_alloc(rel->entry_pool);
+               if (BLI_ghash_ensure_p(rel->id_user_to_used, id_self, (void ***)&entry_p)) {
+                       entry->next = *entry_p;
+               }
+               else {
+                       entry->next = NULL;
+               }
+               entry->id_pointer = id_pointer;
+               entry->usage_flag = cb_flag;
+               *entry_p = entry;
+
+               entry = BLI_mempool_alloc(rel->entry_pool);
+               if (BLI_ghash_ensure_p(rel->id_used_to_user, *id_pointer, (void ***)&entry_p)) {
+                       entry->next = *entry_p;
+               }
+               else {
+                       entry->next = NULL;
+               }
+               entry->id_pointer = (ID **)id_self;
+               entry->usage_flag = cb_flag;
+               *entry_p = entry;
+       }
+
+       return IDWALK_RET_NOP;
+}
+
+/** Generate the mappings between used IDs and their users, and vice-versa. */
+void BKE_main_relations_create(Main *bmain)
+{
+       ListBase *lbarray[MAX_LIBARRAY];
+       ID *id;
+       int a;
+
+       if (bmain->relations != NULL) {
+               BKE_main_relations_free(bmain);
+       }
+
+       bmain->relations = MEM_mallocN(sizeof(*bmain->relations), __func__);
+       bmain->relations->id_used_to_user = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
+       bmain->relations->id_user_to_used = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
+       bmain->relations->entry_pool = BLI_mempool_create(sizeof(MainIDRelationsEntry), 128, 128, BLI_MEMPOOL_NOP);
+
+       for (a = set_listbasepointers(bmain, lbarray); a--; ) {
+               for (id = lbarray[a]->first; id; id = id->next) {
+                       BKE_library_foreach_ID_link(NULL, id, main_relations_create_cb, bmain->relations, IDWALK_READONLY);
+               }
+       }
+}
+
+void BKE_main_relations_free(Main *bmain)
+{
+       if (bmain->relations) {
+               if (bmain->relations->id_used_to_user) {
+                       BLI_ghash_free(bmain->relations->id_used_to_user, NULL, NULL);
+               }
+               if (bmain->relations->id_user_to_used) {
+                       BLI_ghash_free(bmain->relations->id_user_to_used, NULL, NULL);
+               }
+               BLI_mempool_destroy(bmain->relations->entry_pool);
+               MEM_freeN(bmain->relations);
+               bmain->relations = NULL;
+       }
+}
+
 /**
  * Generates a raw .blend file thumbnail data from given image.
  *
@@ -1541,7 +1639,7 @@ static ID *is_dupid(ListBase *lb, ID *id, const char *name)
        
        for (idtest = lb->first; idtest; idtest = idtest->next) {
                /* if idtest is not a lib */ 
-               if (id != idtest && idtest->lib == NULL) {
+               if (id != idtest && !ID_IS_LINKED(idtest)) {
                        /* do not test alphabetic! */
                        /* optimized */
                        if (idtest->name[2] == name[0]) {
@@ -1566,7 +1664,8 @@ static ID *is_dupid(ListBase *lb, ID *id, const char *name)
 static bool check_for_dupid(ListBase *lb, ID *id, char *name)
 {
        ID *idtest;
-       int nr = 0, a, left_len;
+       int nr = 0, a;
+       size_t left_len;
 #define MAX_IN_USE 64
        bool in_use[MAX_IN_USE];
        /* to speed up finding unused numbers within [1 .. MAX_IN_USE - 1] */
@@ -1590,18 +1689,22 @@ static bool check_for_dupid(ListBase *lb, ID *id, char *name)
 
                /* if new name will be too long, truncate it */
                if (nr > 999 && left_len > (MAX_ID_NAME - 8)) {  /* assumption: won't go beyond 9999 */
-                       left[MAX_ID_NAME - 8] = 0;
+                       left[MAX_ID_NAME - 8] = '\0';
                        left_len = MAX_ID_NAME - 8;
                }
                else if (left_len > (MAX_ID_NAME - 7)) {
-                       left[MAX_ID_NAME - 7] = 0;
+                       left[MAX_ID_NAME - 7] = '\0';
                        left_len = MAX_ID_NAME - 7;
                }
 
+               /* Code above may have generated invalid utf-8 string, due to raw truncation.
+                * Ensure we get a valid one now! */
+               left_len -= (size_t)BLI_utf8_invalid_strip(left, left_len);
+
                for (idtest = lb->first; idtest; idtest = idtest->next) {
                        int nrtest;
                        if ( (id != idtest) &&
-                            (idtest->lib == NULL) &&
+                            !ID_IS_LINKED(idtest) &&
                             (*name == *(idtest->name + 2)) &&
                             STREQLEN(name, idtest->name + 2, left_len) &&
                             (BLI_split_name_num(leftest, &nrtest, idtest->name + 2, '.') == left_len)
@@ -1638,7 +1741,7 @@ static bool check_for_dupid(ListBase *lb, ID *id, char *name)
                 * shave off the end chars until we have a unique name.
                 * Check the null terminators match as well so we don't get Cube.000 -> Cube.00 */
                if (nr == 0 && name[left_len] == '\0') {
-                       int len;
+                       size_t len;
                        /* FIXME: this code will never be executed, because either nr will be
                         * at least 1, or name will not end at left_len! */
                        BLI_assert(0);
@@ -1683,13 +1786,9 @@ bool new_id(ListBase *lb, ID *id, const char *tname)
        char name[MAX_ID_NAME - 2];
 
        /* if library, don't rename */
-       if (id->lib)
+       if (ID_IS_LINKED(id))
                return false;
 
-       /* if no libdata given, look up based on ID */
-       if (lb == NULL)
-               lb = which_libbase(G.main, GS(id->name));
-
        /* if no name given, use name of current ID
         * else make a copy (tname args can be const) */
        if (tname == NULL)
@@ -1728,9 +1827,10 @@ bool new_id(ListBase *lb, ID *id, const char *tname)
  * Pull an ID out of a library (make it local). Only call this for IDs that
  * don't have other library users.
  */
-void id_clear_lib_data_ex(Main *bmain, ID *id, bool id_in_mainlist)
+void id_clear_lib_data_ex(Main *bmain, ID *id, const bool id_in_mainlist)
 {
        bNodeTree *ntree = NULL;
+       Key *key = NULL;
 
        BKE_id_lib_local_paths(bmain, id->lib, id);
 
@@ -1741,22 +1841,14 @@ void id_clear_lib_data_ex(Main *bmain, ID *id, bool id_in_mainlist)
        if (id_in_mainlist)
                new_id(which_libbase(bmain, GS(id->name)), id, NULL);
 
-       /* internal bNodeTree blocks inside ID types below
-        * also stores id->lib, make sure this stays in sync.
-        */
-       ntree = ntreeFromID(id);
-
-       if (ntree) {
-               ntreeMakeLocal(ntree, false);
+       /* Internal bNodeTree blocks inside datablocks also stores id->lib, make sure this stays in sync. */
+       if ((ntree = ntreeFromID(id))) {
+               id_clear_lib_data_ex(bmain, &ntree->id, false);  /* Datablocks' nodetree is never in Main. */
        }
 
-       if (GS(id->name) == ID_OB) {
-               Object *object = (Object *)id;
-               if (object->proxy_from != NULL) {
-                       object->proxy_from->proxy = NULL;
-                       object->proxy_from->proxy_group = NULL;
-               }
-               object->proxy = object->proxy_from = object->proxy_group = NULL;
+       /* Same goes for shapekeys. */
+       if ((key = BKE_key_from_id(id))) {
+               id_clear_lib_data_ex(bmain, &key->id, id_in_mainlist);  /* sigh, why are keys in Main? */
        }
 }
 
@@ -1783,51 +1875,60 @@ void BKE_main_id_clear_newpoins(Main *bmain)
        }
 }
 
-static void lib_indirect_test_id(ID *id, Library *lib)
+
+static void library_make_local_copying_check(ID *id, GSet *loop_tags, MainIDRelations *id_relations, GSet *done_ids)
 {
-#define LIBTAG(a) \
-       if (a && a->id.lib) { a->id.tag &= ~LIB_TAG_INDIRECT; a->id.tag |= LIB_TAG_EXTERN; } (void)0
-       
-       if (id->lib) {
-               /* datablocks that were indirectly related are now direct links
-                * without this, appending data that has a link to other data will fail to write */
-               if (lib && id->lib->parent == lib) {
-                       id_lib_extern(id);
-               }
-               return;
+       if (BLI_gset_haskey(done_ids, id)) {
+               return;  /* Already checked, nothing else to do. */
        }
-       
-       if (GS(id->name) == ID_OB) {
-               Object *ob = (Object *)id;
-               Mesh *me;
 
-               int a;
+       MainIDRelationsEntry *entry = BLI_ghash_lookup(id_relations->id_used_to_user, id);
+       BLI_gset_insert(loop_tags, id);
+       for (; entry != NULL; entry = entry->next) {
+               ID *par_id = (ID *)entry->id_pointer;  /* used_to_user stores ID pointer, not pointer to ID pointer... */
+
+               /* Our oh-so-beloved 'from' pointers... */
+               if (entry->usage_flag & IDWALK_CB_LOOPBACK) {
+                       /* We totally disregard Object->proxy_from 'usage' here, this one would only generate fake positives. */
+                       if (GS(par_id->name) == ID_OB) {
+                               BLI_assert(((Object *)par_id)->proxy_from == (Object *)id);
+                               continue;
+                       }
 
-#if 0   /* XXX OLD ANIMSYS, NLASTRIPS ARE NO LONGER USED */
-               /* XXX old animation system! -------------------------------------- */
-               {
-                       bActionStrip *strip;
-                       for (strip = ob->nlastrips.first; strip; strip = strip->next) {
-                               LIBTAG(strip->object);
-                               LIBTAG(strip->act);
-                               LIBTAG(strip->ipo);
+                       /* Shapekeys are considered 'private' to their owner ID here, and never tagged (since they cannot be linked),
+                        * so we have to switch effective parent to their owner. */
+                       if (GS(par_id->name) == ID_KE) {
+                               par_id = ((Key *)par_id)->from;
                        }
                }
-               /* XXX: new animation system needs something like this? */
-#endif
 
-               for (a = 0; a < ob->totcol; a++) {
-                       LIBTAG(ob->mat[a]);
+               if (par_id->lib == NULL) {
+                       /* Local user, early out to avoid some gset querying... */
+                       continue;
+               }
+               if (!BLI_gset_haskey(done_ids, par_id)) {
+                       if (BLI_gset_haskey(loop_tags, par_id)) {
+                               /* We are in a 'dependency loop' of IDs, this does not say us anything, skip it.
+                                * Note that this is the situation that can lead to archipelagoes of linked data-blocks
+                                * (since all of them have non-local users, they would all be duplicated, leading to a loop of unused
+                                * linked data-blocks that cannot be freed since they all use each other...). */
+                               continue;
+                       }
+                       /* Else, recursively check that user ID. */
+                       library_make_local_copying_check(par_id, loop_tags, id_relations, done_ids);
                }
-       
-               LIBTAG(ob->dup_group);
-               LIBTAG(ob->proxy);
-               
-               me = ob->data;
-               LIBTAG(me);
-       }
 
-#undef LIBTAG
+               if (par_id->tag & LIB_TAG_DOIT) {
+                       /* This user will be fully local in future, so far so good, nothing to do here but check next user. */
+               }
+               else {
+                       /* This user won't be fully local in future, so current ID won't be either. And we are done checking it. */
+                       id->tag &= ~LIB_TAG_DOIT;
+                       break;
+               }
+       }
+       BLI_gset_add(done_ids, id);
+       BLI_gset_remove(loop_tags, id, NULL);
 }
 
 /** Make linked datablocks local.
@@ -1837,61 +1938,389 @@ static void lib_indirect_test_id(ID *id, Library *lib)
  * \param untagged_only If true, only make local datablocks not tagged with LIB_TAG_PRE_EXISTING.
  * \param set_fake If true, set fake user on all localized datablocks (except group and objects ones).
  */
-void BKE_library_make_local(Main *bmain, const Library *lib, const bool untagged_only, const bool set_fake)
+/* Note: Old (2.77) version was simply making (tagging) datablocks as local, without actually making any check whether
+ * they were also indirectly used or not...
+ *
+ * Current version uses regular id_make_local callback, with advanced pre-processing step to detect all cases of
+ * IDs currently indirectly used, but which will be used by local data only once this function is finished.
+ * This allows to avoid any unneeded duplication of IDs, and hence all time lost afterwards to remove
+ * orphaned linked data-blocks...
+ */
+void BKE_library_make_local(
+        Main *bmain, const Library *lib, GHash *old_to_new_ids, const bool untagged_only, const bool set_fake)
 {
        ListBase *lbarray[MAX_LIBARRAY];
-       ID *id, *idn;
+       ID *id;
        int a;
 
-       a = set_listbasepointers(bmain, lbarray);
-       while (a--) {
+       LinkNode *todo_ids = NULL;
+       LinkNode *copied_ids = NULL;
+       MemArena *linklist_mem = BLI_memarena_new(512 * sizeof(*todo_ids), __func__);
+
+       GSet *done_ids = BLI_gset_ptr_new(__func__);
+
+#ifdef DEBUG_TIME
+       TIMEIT_START(make_local);
+#endif
+
+       BKE_main_relations_create(bmain);
+
+#ifdef DEBUG_TIME
+       printf("Pre-compute current ID relations: Done.\n");
+       TIMEIT_VALUE_PRINT(make_local);
+#endif
+
+       /* Step 1: Detect datablocks to make local. */
+       for (a = set_listbasepointers(bmain, lbarray); a--; ) {
                id = lbarray[a]->first;
-               
-               while (id) {
-                       id->newid = NULL;
-                       idn = id->next;      /* id is possibly being inserted again */
-                       
-                       /* The check on the second line (LIB_TAG_PRE_EXISTING) is done so its
-                        * possible to tag data you don't want to be made local, used for
-                        * appending data, so any libdata already linked wont become local
-                        * (very nasty to discover all your links are lost after appending)  
-                        * */
-                       if (id->tag & (LIB_TAG_EXTERN | LIB_TAG_INDIRECT | LIB_TAG_NEW) &&
-                           ((untagged_only == false) || !(id->tag & LIB_TAG_PRE_EXISTING)))
+
+               /* Do not explicitly make local non-linkable IDs (shapekeys, in fact), they are assumed to be handled
+                * by real datablocks responsible of them. */
+               const bool do_skip = (id && !BKE_idcode_is_linkable(GS(id->name)));
+
+               for (; id; id = id->next) {
+                       ID *ntree = (ID *)ntreeFromID(id);
+
+                       id->tag &= ~LIB_TAG_DOIT;
+                       if (ntree != NULL) {
+                               ntree->tag &= ~LIB_TAG_DOIT;
+                       }
+
+                       if (id->lib == NULL) {
+                               id->tag &= ~(LIB_TAG_EXTERN | LIB_TAG_INDIRECT | LIB_TAG_NEW);
+                       }
+                       /* The check on the fourth line (LIB_TAG_PRE_EXISTING) is done so its possible to tag data you don't want to
+                        * be made local, used for appending data, so any libdata already linked wont become local (very nasty
+                        * to discover all your links are lost after appending).
+                        * Also, never ever make proxified objects local, would not make any sense. */
+                       /* Some more notes:
+                        *   - Shapekeys are never tagged here (since they are not linkable).
+                        *   - Nodetrees used in materials etc. have to be tagged manually, since they do not exist in Main (!).
+                        * This is ok-ish on 'make local' side of things (since those are handled by their 'owner' IDs),
+                        * but complicates slightly the pre-processing of relations between IDs at step 2... */
+                       else if (!do_skip && id->tag & (LIB_TAG_EXTERN | LIB_TAG_INDIRECT | LIB_TAG_NEW) &&
+                                ELEM(lib, NULL, id->lib) &&
+                                !(GS(id->name) == ID_OB && ((Object *)id)->proxy_from != NULL) &&
+                                ((untagged_only == false) || !(id->tag & LIB_TAG_PRE_EXISTING)))
                        {
-                               if (lib == NULL || id->lib == lib) {
-                                       if (id->lib) {
-                                               /* for Make Local > All we should be calling id_make_local,
-                                                * but doing that breaks append (see #36003 and #36006), we
-                                                * we should make it work with all datablocks and id.us==0 */
-                                               id_clear_lib_data(bmain, id); /* sets 'id->tag' */
-
-                                               /* why sort alphabetically here but not in
-                                                * id_clear_lib_data() ? - campbell */
-                                               id_sort_by_name(lbarray[a], id);
-                                       }
-                                       else {
-                                               id->tag &= ~(LIB_TAG_EXTERN | LIB_TAG_INDIRECT | LIB_TAG_NEW);
-                                       }
+                               BLI_linklist_prepend_arena(&todo_ids, id, linklist_mem);
+                               id->tag |= LIB_TAG_DOIT;
+
+                               /* Tag those nasty non-ID nodetrees, but do not add them to todo list, making them local is handled
+                                * by 'owner' ID. This is needed for library_make_local_copying_check() to work OK at step 2. */
+                               if (ntree != NULL) {
+                                       ntree->tag |= LIB_TAG_DOIT;
+                               }
+                       }
+                       else {
+                               /* Linked ID that we won't be making local (needed info for step 2, see below). */
+                               BLI_gset_add(done_ids, id);
+                       }
+               }
+       }
+
+#ifdef DEBUG_TIME
+       printf("Step 1: Detect datablocks to make local: Done.\n");
+       TIMEIT_VALUE_PRINT(make_local);
+#endif
+
+       /* Step 2: Check which datablocks we can directly make local (because they are only used by already, or future,
+        * local data), others will need to be duplicated. */
+       GSet *loop_tags = BLI_gset_ptr_new(__func__);
+       for (LinkNode *it = todo_ids; it; it = it->next) {
+               library_make_local_copying_check(it->link, loop_tags, bmain->relations, done_ids);
+               BLI_assert(BLI_gset_len(loop_tags) == 0);
+       }
+       BLI_gset_free(loop_tags, NULL);
+       BLI_gset_free(done_ids, NULL);
+
+       /* Next step will most likely add new IDs, better to get rid of this mapping now. */
+       BKE_main_relations_free(bmain);
+
+#ifdef DEBUG_TIME
+       printf("Step 2: Check which datablocks we can directly make local: Done.\n");
+       TIMEIT_VALUE_PRINT(make_local);
+#endif
+
+       /* Step 3: Make IDs local, either directly (quick and simple), or using generic process,
+        * which involves more complex checks and might instead create a local copy of original linked ID. */
+       for (LinkNode *it = todo_ids, *it_next; it; it = it_next) {
+               it_next = it->next;
+               id = it->link;
+
+               if (id->tag & LIB_TAG_DOIT) {
+                       /* We know all users of this object are local or will be made fully local, even if currently there are
+                        * some indirect usages. So instead of making a copy that we'll likely get rid of later, directly make
+                        * that data block local. Saves a tremendous amount of time with complex scenes... */
+                       id_clear_lib_data_ex(bmain, id, true);
+                       BKE_id_expand_local(bmain, id);
+                       id->tag &= ~LIB_TAG_DOIT;
+               }
+               else {
+                       /* In this specific case, we do want to make ID local even if it has no local usage yet... */
+                       if (GS(id->name) == ID_OB) {
+                               /* Special case for objects because we don't want proxy pointers to be
+                                * cleared yet. This will happen down the road in this function.
+                                */
+                               BKE_object_make_local_ex(bmain, (Object *)id, true, false);
+                       }
+                       else {
+                               id_make_local(bmain, id, false, true);
+                       }
+
+                       if (id->newid) {
+                               /* Reuse already allocated LinkNode (transferring it from todo_ids to copied_ids). */
+                               BLI_linklist_prepend_nlink(&copied_ids, id, it);
+                       }
+               }
+
+               if (set_fake) {
+                       if (!ELEM(GS(id->name), ID_OB, ID_GR)) {
+                               /* do not set fake user on objects, groups (instancing) */
+                               id_fake_user_set(id);
+                       }
+               }
+       }
+
+#ifdef DEBUG_TIME
+       printf("Step 3: Make IDs local: Done.\n");
+       TIMEIT_VALUE_PRINT(make_local);
+#endif
+
+       /* At this point, we are done with directly made local IDs. Now we have to handle duplicated ones, since their
+        * remaining linked original counterpart may not be needed anymore... */
+       todo_ids = NULL;
+
+       /* Step 4: We have to remap local usages of old (linked) ID to new (local) ID in a separated loop,
+        * as lbarray ordering is not enough to ensure us we did catch all dependencies
+        * (e.g. if making local a parent object before its child...). See T48907. */
+       /* TODO This is now the biggest step by far (in term of processing time). We may be able to gain here by
+        * using again main->relations mapping, but... this implies BKE_libblock_remap & co to be able to update
+        * main->relations on the fly. Have to think about it a bit more, and see whether new code is OK first, anyway. */
+       for (LinkNode *it = copied_ids; it; it = it->next) {
+               id = it->link;
+
+               BLI_assert(id->newid != NULL);
+               BLI_assert(id->lib != NULL);
+
+               BKE_libblock_remap(bmain, id, id->newid, ID_REMAP_SKIP_INDIRECT_USAGE);
+               if (old_to_new_ids) {
+                       BLI_ghash_insert(old_to_new_ids, id, id->newid);
+               }
+
+               /* Special hack for groups... Thing is, since we can't instantiate them here, we need to ensure
+                * they remain 'alive' (only instantiation is a real group 'user'... *sigh* See T49722. */
+               if (GS(id->name) == ID_GR && (id->tag & LIB_TAG_INDIRECT) != 0) {
+                       id_us_ensure_real(id->newid);
+               }
+       }
+
+#ifdef DEBUG_TIME
+       printf("Step 4: Remap local usages of old (linked) ID to new (local) ID: Done.\n");
+       TIMEIT_VALUE_PRINT(make_local);
+#endif
+
+       /* Note: Keeping both version of the code (old one being safer, since it still has checks against unused IDs)
+        * for now, we can remove old one once it has been tested for some time in master... */
+#if 1
+       /* Step 5: proxy 'remapping' hack. */
+       for (LinkNode *it = copied_ids; it; it = it->next) {
+               /* Attempt to re-link copied proxy objects. This allows appending of an entire scene
+                * from another blend file into this one, even when that blend file contains proxified
+                * armatures that have local references. Since the proxified object needs to be linked
+                * (not local), this will only work when the "Localize all" checkbox is disabled.
+                * TL;DR: this is a dirty hack on top of an already weak feature (proxies). */
+               if (GS(id->name) == ID_OB && ((Object *)id)->proxy != NULL) {
+                       Object *ob = (Object *)id;
+                       Object *ob_new = (Object *)id->newid;
+                       bool is_local = false, is_lib = false;
+
+                       /* Proxies only work when the proxified object is linked-in from a library. */
+                       if (ob->proxy->id.lib == NULL) {
+                               printf("Warning, proxy object %s will loose its link to %s, because the "
+                                      "proxified object is local.\n", id->newid->name, ob->proxy->id.name);
+                               continue;
+                       }
+
+                       BKE_library_ID_test_usages(bmain, id, &is_local, &is_lib);
+
+                       /* We can only switch the proxy'ing to a made-local proxy if it is no longer
+                        * referred to from a library. Not checking for local use; if new local proxy
+                        * was not used locally would be a nasty bug! */
+                       if (is_local || is_lib) {
+                               printf("Warning, made-local proxy object %s will loose its link to %s, "
+                                      "because the linked-in proxy is referenced (is_local=%i, is_lib=%i).\n",
+                                      id->newid->name, ob->proxy->id.name, is_local, is_lib);
+                       }
+                       else {
+                               /* we can switch the proxy'ing from the linked-in to the made-local proxy.
+                                * BKE_object_make_proxy() shouldn't be used here, as it allocates memory that
+                                * was already allocated by BKE_object_make_local_ex() (which called BKE_object_copy_ex). */
+                               ob_new->proxy = ob->proxy;
+                               ob_new->proxy_group = ob->proxy_group;
+                               ob_new->proxy_from = ob->proxy_from;
+                               ob_new->proxy->proxy_from = ob_new;
+                               ob->proxy = ob->proxy_from = ob->proxy_group = NULL;
+                       }
+               }
+       }
+
+#ifdef DEBUG_TIME
+       printf("Step 5: Proxy 'remapping' hack: Done.\n");
+       TIMEIT_VALUE_PRINT(make_local);
+#endif
+
+#else
+       LinkNode *linked_loop_candidates = NULL;
+
+       /* Step 5: remove datablocks that have been copied to be localized and are no more used in the end...
+        * Note that we may have to loop more than once here, to tackle dependencies between linked objects... */
+       bool do_loop = true;
+       while (do_loop) {
+               do_loop = false;
+               for (LinkNode *it = copied_ids; it; it = it->next) {
+                       if ((id = it->link) == NULL) {
+                               continue;
+                       }
+
+                       bool is_local = false, is_lib = false;
+
+                       BKE_library_ID_test_usages(bmain, id, &is_local, &is_lib);
+
+                       /* Attempt to re-link copied proxy objects. This allows appending of an entire scene
+                        * from another blend file into this one, even when that blend file contains proxified
+                        * armatures that have local references. Since the proxified object needs to be linked
+                        * (not local), this will only work when the "Localize all" checkbox is disabled.
+                        * TL;DR: this is a dirty hack on top of an already weak feature (proxies). */
+                       if (GS(id->name) == ID_OB && ((Object *)id)->proxy != NULL) {
+                               Object *ob = (Object *)id;
+                               Object *ob_new = (Object *)id->newid;
+
+                               /* Proxies only work when the proxified object is linked-in from a library. */
+                               if (ob->proxy->id.lib == NULL) {
+                                       printf("Warning, proxy object %s will loose its link to %s, because the "
+                                                  "proxified object is local.\n", id->newid->name, ob->proxy->id.name);
+                               }
+                               /* We can only switch the proxy'ing to a made-local proxy if it is no longer
+                                * referred to from a library. Not checking for local use; if new local proxy
+                                * was not used locally would be a nasty bug! */
+                               else if (is_local || is_lib) {
+                                       printf("Warning, made-local proxy object %s will loose its link to %s, "
+                                              "because the linked-in proxy is referenced (is_local=%i, is_lib=%i).\n",
+                                              id->newid->name, ob->proxy->id.name, is_local, is_lib);
                                }
+                               else {
+                                       /* we can switch the proxy'ing from the linked-in to the made-local proxy.
+                                        * BKE_object_make_proxy() shouldn't be used here, as it allocates memory that
+                                        * was already allocated by BKE_object_make_local_ex() (which called BKE_object_copy_ex). */
+                                       ob_new->proxy = ob->proxy;
+                                       ob_new->proxy_group = ob->proxy_group;
+                                       ob_new->proxy_from = ob->proxy_from;
+                                       ob_new->proxy->proxy_from = ob_new;
+                                       ob->proxy = ob->proxy_from = ob->proxy_group = NULL;
+                               }
+                       }
 
-                               if (set_fake) {
-                                       if (!ELEM(GS(id->name), ID_OB, ID_GR)) {
-                                               /* do not set fake user on objects, groups (instancing) */
-                                               id_fake_user_set(id);
+                       if (!is_local) {
+                               if (!is_lib) {  /* Not used at all, we can free it! */
+                                       BLI_assert(!"Unused linked data copy remaining from MakeLibLocal process, should not happen anymore");
+                                       printf("\t%s (from %s)\n", id->name, id->lib->id.name);
+                                       BKE_libblock_free(bmain, id);
+                                       it->link = NULL;
+                                       do_loop = true;
+                               }
+                               /* Only used by linked data, potential candidate to ugly lib-only dependency cycles... */
+                               else if ((id->tag & LIB_TAG_DOIT) == 0) {  /* Check TAG_DOIT to avoid adding same ID several times... */
+                                       /* Note that we store the node, not directly ID pointer, that way if it->link is set to NULL
+                                        * later we can skip it in lib-dependency cycles search later. */
+                                       BLI_linklist_prepend_arena(&linked_loop_candidates, it, linklist_mem);
+                                       id->tag |= LIB_TAG_DOIT;
+
+                                       /* Grrrrrrr... those half-datablocks-stuff... grrrrrrrrrrr...
+                                        * Here we have to also tag them as potential candidates, otherwise they would falsy report
+                                        * ID they used as 'directly used' in sixth step. */
+                                       ID *ntree = (ID *)ntreeFromID(id);
+                                       if (ntree != NULL) {
+                                               ntree->tag |= LIB_TAG_DOIT;
                                        }
                                }
                        }
+               }
+       }
+
+#ifdef DEBUG_TIME
+       printf("Step 5: Remove linked datablocks that have been copied and ended fully localized: Done.\n");
+       TIMEIT_VALUE_PRINT(make_local);
+#endif
+
+       /* Step 6: Try to find circle dependencies between indirectly-linked-only datablocks.
+        * Those are fake 'usages' that prevent their deletion. See T49775 for nice ugly case. */
+       BKE_library_unused_linked_data_set_tag(bmain, false);
+       for (LinkNode *it = linked_loop_candidates; it; it = it->next) {
+               if (it->link == NULL) {
+                       continue;
+               }
+               if ((id = ((LinkNode *)it->link)->link) == NULL) {
+                       it->link = NULL;
+                       continue;
+               }
+
+               /* Note: in theory here we are only handling datablocks forming exclusive linked dependency-cycles-based
+                * archipelagos, so no need to check again after we have deleted one, as done in previous step. */
+               if (id->tag & LIB_TAG_DOIT) {
+                       BLI_assert(!"Unused linked data copy remaining from MakeLibLocal process (archipelago case), should not happen anymore");
+                       /* Object's deletion rely on valid ob->data, but ob->data may have already been freed here...
+                        * Setting it to NULL may not be 100% correct, but should be safe and do the work. */
+                       if (GS(id->name) == ID_OB) {
+                               ((Object *)id)->data = NULL;
+                       }
 
-                       id = idn;
+                       /* Note: *in theory* IDs tagged here are fully *outside* of file scope, totally unused, so we can
+                        *       directly wipe them out without caring about clearing their usages.
+                        *       However, this is a highly-risky presumption, and nice crasher in case something goes wrong here.
+                        *       So for 2.78a will keep the safe option, and switch to more efficient one in master later. */
+#if 1
+                       BKE_libblock_free_ex(bmain, id, false, true);
+#else
+                       BKE_libblock_unlink(bmain, id, false, false);
+                       BKE_libblock_free(bmain, id);
+#endif
+                       ((LinkNode *)it->link)->link = NULL;  /* Not strictly necessary, but safer (see T49903)... */
+                       it->link = NULL;
                }
        }
 
-       a = set_listbasepointers(bmain, lbarray);
-       while (a--) {
-               for (id = lbarray[a]->first; id; id = id->next)
-                       lib_indirect_test_id(id, lib);
+#ifdef DEBUG_TIME
+       printf("Step 6: Try to find circle dependencies between indirectly-linked-only datablocks: Done.\n");
+       TIMEIT_VALUE_PRINT(make_local);
+#endif
+
+#endif
+
+       /* This is probably more of a hack than something we should do here, but...
+        * Issue is, the whole copying + remapping done in complex cases above may leave pose channels of armatures
+        * in complete invalid state (more precisely, the bone pointers of the pchans - very crappy cross-datablocks
+        * relationship), se we tag it to be fully recomputed, but this does not seems to be enough in some cases,
+        * and evaluation code ends up trying to evaluate a not-yet-updated armature object's deformations.
+        * Try "make all local" in 04_01_H.lighting.blend from Agent327 without this, e.g. */
+       for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
+               if (ob->data != NULL && ob->type == OB_ARMATURE && ob->pose != NULL && ob->pose->flag & POSE_RECALC) {
+                       BKE_pose_rebuild(ob, ob->data);
+               }
        }
+
+#ifdef DEBUG_TIME
+       printf("Hack: Forcefully rebuild armature object poses: Done.\n");
+       TIMEIT_VALUE_PRINT(make_local);
+#endif
+
+       BKE_main_id_clear_newpoins(bmain);
+       BLI_memarena_free(linklist_mem);
+
+#ifdef DEBUG_TIME
+       printf("Cleanup and finish: Done.\n");
+       TIMEIT_END(make_local);
+#endif
 }
 
 /**
@@ -1903,7 +2332,6 @@ void BLI_libblock_ensure_unique_name(Main *bmain, const char *name)
        ListBase *lb;
        ID *idtest;
 
-
        lb = which_libbase(bmain, GS(name));
        if (lb == NULL) return;
        
@@ -1959,3 +2387,13 @@ void BKE_library_filepath_set(Library *lib, const char *filepath)
                BLI_path_abs(lib->filepath, basepath);
        }
 }
+
+void BKE_id_tag_set_atomic(ID *id, int tag)
+{
+       atomic_fetch_and_or_int32(&id->tag, tag);
+}
+
+void BKE_id_tag_clear_atomic(ID *id, int tag)
+{
+       atomic_fetch_and_and_int32(&id->tag, ~tag);
+}