Undo: unified undo system w/ linear history
[blender.git] / source / blender / blenkernel / intern / library.c
index c249577686e4d32a7349e38cc2e509429abcef39..10b724f9f69706b6f169b6165898611390b8a132 100644 (file)
@@ -162,7 +162,7 @@ void BKE_id_lib_local_paths(Main *bmain, Library *lib, ID *id)
 
 void id_lib_extern(ID *id)
 {
-       if (id && ID_IS_LINKED_DATABLOCK(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;
@@ -307,9 +307,9 @@ void BKE_id_expand_local(Main *bmain, ID *id)
 /**
  * Ensure new (copied) ID is fully made local.
  */
-void BKE_id_copy_ensure_local(Main *bmain, ID *old_id, ID *new_id)
+void BKE_id_copy_ensure_local(Main *bmain, const ID *old_id, ID *new_id)
 {
-       if (ID_IS_LINKED_DATABLOCK(old_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);
        }
@@ -328,7 +328,7 @@ void BKE_id_make_local_generic(Main *bmain, ID *id, const bool id_in_mainlist, c
         * 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_DATABLOCK(id)) {
+       if (!ID_IS_LINKED(id)) {
                return;
        }
 
@@ -383,7 +383,6 @@ bool id_make_local(Main *bmain, ID *id, const bool test, const bool lib_local)
 
        switch ((ID_Type)GS(id->name)) {
                case ID_SCE:
-                       /* Partially implemented (has no copy...). */
                        if (!test) BKE_scene_make_local(bmain, (Scene *)id, lib_local);
                        return true;
                case ID_OB:
@@ -423,14 +422,12 @@ bool id_make_local(Main *bmain, ID *id, const bool test, const bool lib_local)
                        if (!test) BKE_world_make_local(bmain, (World *)id, lib_local);
                        return true;
                case ID_VF:
-                       /* Partially implemented (has no copy...). */
                        if (!test) BKE_vfont_make_local(bmain, (VFont *)id, lib_local);
                        return true;
                case ID_TXT:
                        if (!test) BKE_text_make_local(bmain, (Text *)id, lib_local);
                        return true;
                case ID_SO:
-                       /* Partially implemented (has no copy...). */
                        if (!test) BKE_sound_make_local(bmain, (bSound *)id, lib_local);
                        return true;
                case ID_GR:
@@ -484,114 +481,203 @@ bool id_make_local(Main *bmain, ID *id, const bool test, const bool lib_local)
        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(Main *bmain, 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;
        }
 
-       /* conventions:
-        * - make shallow copy, only this ID block
-        * - id.us of the new ID is set to 1 */
+       BKE_libblock_copy_ex(bmain, id, r_newid, flag);
+
        switch ((ID_Type)GS(id->name)) {
+               case ID_SCE:
+                       BKE_scene_copy_data(bmain, (Scene *)*r_newid, (Scene *)id, flag);
+                       break;
                case ID_OB:
-                       if (!test) *newid = (ID *)BKE_object_copy(bmain, (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(bmain, (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(bmain, (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(bmain, (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(bmain, (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(bmain, (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(bmain, (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(bmain, (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(bmain, (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(bmain, (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(bmain, (Camera *)id);
-                       return true;
+                       BKE_camera_copy_data(bmain, (Camera *)*r_newid, (Camera *)id, flag);
+                       break;
                case ID_KE:
-                       if (!test) *newid = (ID *)BKE_key_copy(bmain, (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(bmain, (World *)id);
-                       return true;
+                       BKE_world_copy_data(bmain, (World *)*r_newid, (World *)id, flag);
+                       break;
                case ID_TXT:
-                       if (!test) *newid = (ID *)BKE_text_copy(bmain, (Text *)id);
-                       return true;
+                       BKE_text_copy_data(bmain, (Text *)*r_newid, (Text *)id, flag);
+                       break;
                case ID_GR:
-                       if (!test) *newid = (ID *)BKE_group_copy(bmain, (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(bmain, (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(bmain, (bAction *)id);
-                       return true;
+                       BKE_action_copy_data(bmain, (bAction *)*r_newid, (bAction *)id, flag);
+                       break;
                case ID_NT:
-                       if (!test) *newid = (ID *)ntreeCopyTree(bmain, (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(bmain, (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(bmain, (ParticleSettings *)id);
-                       return true;
+                       BKE_particlesettings_copy_data(bmain, (ParticleSettings *)*r_newid, (ParticleSettings *)id, flag);
+                       break;
                case ID_GD:
-                       if (!test) *newid = (ID *)BKE_gpencil_data_duplicate(bmain, (bGPdata *)id, false);
-                       return true;
+                       BKE_gpencil_copy_data(bmain, (bGPdata *)*r_newid, (bGPdata *)id, flag);
+                       break;
                case ID_MC:
-                       if (!test) *newid = (ID *)BKE_movieclip_copy(bmain, (MovieClip *)id);
-                       return true;
+                       BKE_movieclip_copy_data(bmain, (MovieClip *)*r_newid, (MovieClip *)id, flag);
+                       break;
                case ID_MSK:
-                       if (!test) *newid = (ID *)BKE_mask_copy(bmain, (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(bmain, (FreestyleLineStyle *)id);
-                       return true;
+                       BKE_linestyle_copy_data(bmain, (FreestyleLineStyle *)*r_newid, (FreestyleLineStyle *)id, flag);
+                       break;
                case ID_PAL:
-                       if (!test) *newid = (ID *)BKE_palette_copy(bmain, (Palette *)id);
-                       return true;
+                       BKE_palette_copy_data(bmain, (Palette *)*r_newid, (Palette *)id, flag);
+                       break;
                case ID_PC:
-                       if (!test) *newid = (ID *)BKE_paint_curve_copy(bmain, (PaintCurve *)id);
-                       return true;
+                       BKE_paint_curve_copy_data(bmain, (PaintCurve *)*r_newid, (PaintCurve *)id, flag);
+                       break;
                case ID_CF:
-                       if (!test) *newid = (ID *)BKE_cachefile_copy(bmain, (CacheFile *)id);
-                       return true;
-               case ID_SCE:
+                       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:
-                       return false;  /* can't be copied from here */
-               case ID_VF:
-               case ID_SO:
-                       return false;  /* not implemented */
                case ID_IP:
-                       return false;  /* deprecated */
+                       BLI_assert(0);  /* Should have been rejected at start of function! */
+                       break;
        }
-       
-       return false;
+
+       /* 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);
+
+       /* 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 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. */
@@ -623,6 +709,103 @@ bool id_single_user(bContext *C, ID *id, PointerRNA *ptr, PropertyRNA *prop)
        return false;
 }
 
+static int libblock_management_us_plus(void *UNUSED(user_data), ID *UNUSED(id_self), ID **id_pointer, int cb_flag)
+{
+       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) {
@@ -778,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 (ID_IS_LINKED_DATABLOCK(ob)) {
+               if (ID_IS_LINKED(ob)) {
                        DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
                }
        }
@@ -857,119 +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 ((ID_Type)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;
-               case ID_CF:
-                       id = MEM_callocN(sizeof(CacheFile), "Cache File");
-                       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;
 }
 
 /**
@@ -978,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;
 }
 
@@ -1117,70 +1276,80 @@ void BKE_libblock_init_empty(ID *id)
 
 /* 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)
+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(iat->adt, do_action); /* could be set to false, need to investigate */
+               iat->adt = BKE_animdata_copy(bmain, 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)
+void BKE_libblock_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag)
 {
-       if (id_from->properties)
-               id->properties = IDP_CopyProperty(id_from->properties);
+       ID *new_id = *r_newid;
 
-       /* the duplicate should get a copy of the animdata */
-       id_copy_animdata(id, do_action);
-}
+       /* 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));
 
-/* used everywhere in blenkernel */
-void *BKE_libblock_copy(Main *bmain, ID *id)
-{
-       ID *idn;
-       size_t idn_len;
+       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);
 
-       idn = BKE_libblock_alloc(bmain, GS(id->name), id->name + 2);
-
-       assert(idn != NULL);
+       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);
 
-       idn_len = MEM_allocN_len(idn);
-       if ((int)idn_len - (int)sizeof(ID) > 0) { /* signed to allow neg result */
+       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 *)idn;
+               char *cpn = (char *)new_id;
 
-               memcpy(cpn + sizeof(ID), cp + sizeof(ID), idn_len - sizeof(ID));
+               memcpy(cpn + id_offset, cp + id_offset, id_len - id_offset);
        }
 
-       BKE_libblock_copy_data(idn, id, false);
-       
-       return idn;
+       if (id->properties) {
+               new_id->properties = IDP_CopyProperty_ex(id->properties, flag);
+       }
+
+       /* 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));
+       }
+
+       *r_newid = new_id;
 }
 
-void *BKE_libblock_copy_nolib(ID *id, const bool do_action)
+/* used everywhere in blenkernel */
+void *BKE_libblock_copy(Main *bmain, const ID *id)
 {
        ID *idn;
-       size_t idn_len;
 
-       idn = BKE_libblock_alloc_notest(GS(id->name));
-       assert(idn != NULL);
+       BKE_libblock_copy_ex(bmain, id, &idn, 0);
 
-       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));
-       }
+       return idn;
+}
 
-       idn->us = 1;
+void *BKE_libblock_copy_nolib(const ID *id, const bool do_action)
+{
+       ID *idn;
 
-       BKE_libblock_copy_data(idn, id, do_action);
+       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));
 
        return idn;
 }
@@ -1470,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 && !ID_IS_LINKED_DATABLOCK(idtest)) {
+               if (id != idtest && !ID_IS_LINKED(idtest)) {
                        /* do not test alphabetic! */
                        /* optimized */
                        if (idtest->name[2] == name[0]) {
@@ -1535,7 +1704,7 @@ static bool check_for_dupid(ListBase *lb, ID *id, char *name)
                for (idtest = lb->first; idtest; idtest = idtest->next) {
                        int nrtest;
                        if ( (id != idtest) &&
-                            !ID_IS_LINKED_DATABLOCK(idtest) &&
+                            !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)
@@ -1617,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_IS_LINKED_DATABLOCK(id))
+       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)
@@ -1778,7 +1943,7 @@ static void library_make_local_copying_check(ID *id, GSet *loop_tags, MainIDRela
  *
  * 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 uneeded duplication of IDs, and hence all time lost afterwards to remove
+ * 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(
@@ -1864,7 +2029,7 @@ void BKE_library_make_local(
        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_size(loop_tags) == 0);
+               BLI_assert(BLI_gset_len(loop_tags) == 0);
        }
        BLI_gset_free(loop_tags, NULL);
        BLI_gset_free(done_ids, NULL);
@@ -1984,8 +2149,8 @@ void BKE_library_make_local(
                         * 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);
+                                      "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.
@@ -2041,8 +2206,8 @@ void BKE_library_make_local(
                                 * 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);
+                                              "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.
@@ -2167,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;
        
@@ -2226,10 +2390,10 @@ void BKE_library_filepath_set(Library *lib, const char *filepath)
 
 void BKE_id_tag_set_atomic(ID *id, int tag)
 {
-       atomic_fetch_and_or_uint32((uint32_t *)&id->tag, tag);
+       atomic_fetch_and_or_int32(&id->tag, tag);
 }
 
 void BKE_id_tag_clear_atomic(ID *id, int tag)
 {
-       atomic_fetch_and_and_uint32((uint32_t *)&id->tag, ~tag);
+       atomic_fetch_and_and_int32(&id->tag, ~tag);
 }