Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / library.c
index 80bd1e9ec0339027fb915870661a0747c9b12b60..978f1acefbaf5c730987da3f7afc5f54ef323519 100644 (file)
 #include "BKE_mask.h"
 #include "BKE_node.h"
 #include "BKE_object.h"
-#include "BKE_particle.h"
 #include "BKE_packedFile.h"
 #include "BKE_speaker.h"
 #include "BKE_scene.h"
@@ -251,6 +250,24 @@ void id_fake_user_clear(ID *id)
        }
 }
 
+static int id_expand_local_callback(
+        void *UNUSED(user_data), struct ID *UNUSED(id_self), struct ID **id_pointer, int UNUSED(cd_flag))
+{
+       if (*id_pointer) {
+               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(ID *id)
+{
+       BKE_library_foreach_ID_link(id, id_expand_local_callback, NULL, 0);
+}
+
 /* calls the appropriate make_local method for the block, unless test. Returns true
  * if the block can be made local. */
 bool id_make_local(Main *bmain, ID *id, bool test)
@@ -276,25 +293,25 @@ bool id_make_local(Main *bmain, ID *id, bool test)
                        if (!test) BKE_mball_make_local(bmain, (MetaBall *)id);
                        return true;
                case ID_MA:
-                       if (!test) BKE_material_make_local((Material *)id);
+                       if (!test) BKE_material_make_local(bmain, (Material *)id);
                        return true;
                case ID_TE:
-                       if (!test) BKE_texture_make_local((Tex *)id);
+                       if (!test) BKE_texture_make_local(bmain, (Tex *)id);
                        return true;
                case ID_IM:
-                       if (!test) BKE_image_make_local((Image *)id);
+                       if (!test) BKE_image_make_local(bmain, (Image *)id);
                        return true;
                case ID_LT:
-                       if (!test) BKE_lattice_make_local((Lattice *)id);
+                       if (!test) BKE_lattice_make_local(bmain, (Lattice *)id);
                        return true;
                case ID_LA:
-                       if (!test) BKE_lamp_make_local((Lamp *)id);
+                       if (!test) BKE_lamp_make_local(bmain, (Lamp *)id);
                        return true;
                case ID_CA:
-                       if (!test) BKE_camera_make_local((Camera *)id);
+                       if (!test) BKE_camera_make_local(bmain, (Camera *)id);
                        return true;
                case ID_SPK:
-                       if (!test) BKE_speaker_make_local((Speaker *)id);
+                       if (!test) BKE_speaker_make_local(bmain, (Speaker *)id);
                        return true;
                case ID_IP:
                        return false; /* deprecated */
@@ -302,7 +319,7 @@ bool id_make_local(Main *bmain, ID *id, bool test)
                        if (!test) BKE_key_make_local(bmain, (Key *)id);
                        return true;
                case ID_WO:
-                       if (!test) BKE_world_make_local((World *)id);
+                       if (!test) BKE_world_make_local(bmain, (World *)id);
                        return true;
                case ID_SCR:
                        return false; /* can't be linked */
@@ -315,19 +332,16 @@ bool id_make_local(Main *bmain, ID *id, bool test)
                case ID_GR:
                        return false; /* not implemented */
                case ID_AR:
-                       if (!test) BKE_armature_make_local((bArmature *)id);
+                       if (!test) BKE_armature_make_local(bmain, (bArmature *)id);
                        return true;
                case ID_AC:
-                       if (!test) BKE_action_make_local((bAction *)id);
+                       if (!test) BKE_action_make_local(bmain, (bAction *)id);
                        return true;
                case ID_NT:
-                       if (!test) ntreeMakeLocal((bNodeTree *)id, true);
+                       if (!test) ntreeMakeLocal(bmain, (bNodeTree *)id, true);
                        return true;
                case ID_BR:
-                       if (!test) BKE_brush_make_local((Brush *)id);
-                       return true;
-               case ID_PA:
-                       if (!test) BKE_particlesettings_make_local((ParticleSettings *)id);
+                       if (!test) BKE_brush_make_local(bmain, (Brush *)id);
                        return true;
                case ID_WM:
                        return false; /* can't be linked */
@@ -344,9 +358,11 @@ bool id_make_local(Main *bmain, ID *id, bool test)
  * 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(ID *id, ID **newid, bool test)
+bool id_copy(Main *bmain, ID *id, ID **newid, bool test)
 {
-       if (!test) *newid = NULL;
+       if (!test) {
+               *newid = NULL;
+       }
 
        /* conventions:
         * - make shallow copy, only this ID block
@@ -357,83 +373,80 @@ bool id_copy(ID *id, ID **newid, bool test)
                case ID_LI:
                        return false;  /* can't be copied from here */
                case ID_OB:
-                       if (!test) *newid = (ID *)BKE_object_copy((Object *)id);
+                       if (!test) *newid = (ID *)BKE_object_copy(bmain, (Object *)id);
                        return true;
                case ID_ME:
-                       if (!test) *newid = (ID *)BKE_mesh_copy((Mesh *)id);
+                       if (!test) *newid = (ID *)BKE_mesh_copy(bmain, (Mesh *)id);
                        return true;
                case ID_CU:
-                       if (!test) *newid = (ID *)BKE_curve_copy((Curve *)id);
+                       if (!test) *newid = (ID *)BKE_curve_copy(bmain, (Curve *)id);
                        return true;
                case ID_MB:
-                       if (!test) *newid = (ID *)BKE_mball_copy((MetaBall *)id);
+                       if (!test) *newid = (ID *)BKE_mball_copy(bmain, (MetaBall *)id);
                        return true;
                case ID_MA:
-                       if (!test) *newid = (ID *)BKE_material_copy((Material *)id);
+                       if (!test) *newid = (ID *)BKE_material_copy(bmain, (Material *)id);
                        return true;
                case ID_TE:
-                       if (!test) *newid = (ID *)BKE_texture_copy((Tex *)id);
+                       if (!test) *newid = (ID *)BKE_texture_copy(bmain, (Tex *)id);
                        return true;
                case ID_IM:
-                       if (!test) *newid = (ID *)BKE_image_copy(G.main, (Image *)id);
+                       if (!test) *newid = (ID *)BKE_image_copy(bmain, (Image *)id);
                        return true;
                case ID_LT:
-                       if (!test) *newid = (ID *)BKE_lattice_copy((Lattice *)id);
+                       if (!test) *newid = (ID *)BKE_lattice_copy(bmain, (Lattice *)id);
                        return true;
                case ID_LA:
-                       if (!test) *newid = (ID *)BKE_lamp_copy((Lamp *)id);
+                       if (!test) *newid = (ID *)BKE_lamp_copy(bmain, (Lamp *)id);
                        return true;
                case ID_SPK:
-                       if (!test) *newid = (ID *)BKE_speaker_copy((Speaker *)id);
+                       if (!test) *newid = (ID *)BKE_speaker_copy(bmain, (Speaker *)id);
                        return true;
                case ID_CA:
-                       if (!test) *newid = (ID *)BKE_camera_copy((Camera *)id);
+                       if (!test) *newid = (ID *)BKE_camera_copy(bmain, (Camera *)id);
                        return true;
                case ID_IP:
                        return false;  /* deprecated */
                case ID_KE:
-                       if (!test) *newid = (ID *)BKE_key_copy((Key *)id);
+                       if (!test) *newid = (ID *)BKE_key_copy(bmain, (Key *)id);
                        return true;
                case ID_WO:
-                       if (!test) *newid = (ID *)BKE_world_copy((World *)id);
+                       if (!test) *newid = (ID *)BKE_world_copy(bmain, (World *)id);
                        return true;
                case ID_SCR:
                        return false;  /* can't be copied from here */
                case ID_VF:
                        return false;  /* not implemented */
                case ID_TXT:
-                       if (!test) *newid = (ID *)BKE_text_copy(G.main, (Text *)id);
+                       if (!test) *newid = (ID *)BKE_text_copy(bmain, (Text *)id);
                        return true;
                case ID_SO:
                        return false;  /* not implemented */
                case ID_GR:
-                       if (!test) *newid = (ID *)BKE_group_copy((Group *)id);
+                       if (!test) *newid = (ID *)BKE_group_copy(bmain, (Group *)id);
                        return true;
                case ID_AR:
-                       if (!test) *newid = (ID *)BKE_armature_copy((bArmature *)id);
+                       if (!test) *newid = (ID *)BKE_armature_copy(bmain, (bArmature *)id);
                        return true;
                case ID_AC:
-                       if (!test) *newid = (ID *)BKE_action_copy((bAction *)id);
+                       if (!test) *newid = (ID *)BKE_action_copy(bmain, (bAction *)id);
                        return true;
                case ID_NT:
-                       if (!test) *newid = (ID *)ntreeCopyTree((bNodeTree *)id);
+                       if (!test) *newid = (ID *)ntreeCopyTree(bmain, (bNodeTree *)id);
                        return true;
                case ID_BR:
-                       if (!test) *newid = (ID *)BKE_brush_copy((Brush *)id);
-                       return true;
-               case ID_PA:
-                       if (!test) *newid = (ID *)BKE_particlesettings_copy((ParticleSettings *)id);
+                       if (!test) *newid = (ID *)BKE_brush_copy(bmain, (Brush *)id);
                        return true;
                case ID_WM:
                        return false;  /* can't be copied from here */
                case ID_GD:
-                       if (!test) *newid = (ID *)gpencil_data_duplicate((bGPdata *)id, false);
+                       if (!test) *newid = (ID *)gpencil_data_duplicate(bmain, (bGPdata *)id, false);
                        return true;
                case ID_MSK:
-                       if (!test) *newid = (ID *)BKE_mask_copy((Mask *)id);
+                       if (!test) *newid = (ID *)BKE_mask_copy(bmain, (Mask *)id);
                        return true;
                case ID_LS:
-                       if (!test) *newid = (ID *)BKE_linestyle_copy(G.main, (FreestyleLineStyle *)id);
+                       if (!test) *newid = (ID *)BKE_linestyle_copy(bmain, (FreestyleLineStyle *)id);
                        return true;
        }
        
@@ -448,7 +461,7 @@ 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);
                                /* us is 1 by convention, but RNA_property_pointer_set
@@ -521,8 +534,6 @@ ListBase *which_libbase(Main *mainlib, short type)
                        return &(mainlib->nodetree);
                case ID_BR:
                        return &(mainlib->brush);
-               case ID_PA:
-                       return &(mainlib->particle);
                case ID_WM:
                        return &(mainlib->wm);
                case ID_GD:
@@ -674,7 +685,6 @@ int set_listbasepointers(Main *main, ListBase **lb)
        lb[a++] = &(main->palettes);
        lb[a++] = &(main->paintcurves);
        lb[a++] = &(main->brush);
-       lb[a++] = &(main->particle);
        lb[a++] = &(main->speaker);
 
        lb[a++] = &(main->world);
@@ -787,9 +797,6 @@ void *BKE_libblock_alloc_notest(short type)
                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;
@@ -931,9 +938,6 @@ void BKE_libblock_init_empty(ID *id)
                case ID_BR:
                        BKE_brush_init((Brush *)id);
                        break;
-               case ID_PA:
-                       /* Nothing to do. */
-                       break;
                case ID_PC:
                        /* Nothing to do. */
                        break;
@@ -976,7 +980,7 @@ void BKE_libblock_copy_data(ID *id, const ID *id_from, const bool do_action)
 }
 
 /* used everywhere in blenkernel */
-void *BKE_libblock_copy_ex(Main *bmain, ID *id)
+void *BKE_libblock_copy(Main *bmain, ID *id)
 {
        ID *idn;
        size_t idn_len;
@@ -1028,11 +1032,6 @@ void *BKE_libblock_copy_nolib(ID *id, const bool 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;
@@ -1472,7 +1471,7 @@ void id_clear_lib_data_ex(Main *bmain, ID *id, bool id_in_mainlist)
        ntree = ntreeFromID(id);
 
        if (ntree) {
-               ntreeMakeLocal(ntree, false);
+               ntreeMakeLocal(bmain, ntree, false);
        }
 
        if (GS(id->name) == ID_OB) {