Fake user: add BKE_library helpers to set/clear that flag.
authorBastien Montagne <montagne29@wanadoo.fr>
Mon, 9 Nov 2015 19:59:42 +0000 (20:59 +0100)
committerBastien Montagne <montagne29@wanadoo.fr>
Mon, 9 Nov 2015 20:00:53 +0000 (21:00 +0100)
Since it also involves usercount manipulation, safer and cleaner to do it in BKE_library...

source/blender/blenkernel/BKE_library.h
source/blender/blenkernel/intern/brush.c
source/blender/blenkernel/intern/library.c
source/blender/blenkernel/intern/mask.c
source/blender/blenkernel/intern/paint.c
source/blender/blenloader/intern/readfile.c
source/blender/editors/interface/interface_templates.c
source/blender/editors/space_action/action_data.c
source/blender/editors/space_outliner/outliner_tools.c
source/blender/makesrna/intern/rna_ID.c
source/blender/makesrna/intern/rna_main_api.c

index e11e9496b2166f186c7fd799fcda5f7d52d2052b..e27198add4cc13e2d78879cd48d701a069fcf0c1 100644 (file)
@@ -64,6 +64,8 @@ void BKE_library_filepath_set(struct Library *lib, const char *filepath);
 void id_us_ensure_real(struct ID *id);
 void id_us_plus(struct ID *id);
 void id_us_min(struct ID *id);
+void id_fake_user_set(struct ID *id);
+void id_fake_user_clear(struct ID *id);
 
 bool id_make_local(struct ID *id, bool test);
 bool id_single_user(struct bContext *C, struct ID *id, struct PointerRNA *ptr, struct PropertyRNA *prop);
index 201750df5a735c160e9c5de2ecd585583ffc3ce7..92ab4d745c1e6ed01b3ec69b1414637f54441157 100644 (file)
@@ -136,7 +136,7 @@ void BKE_brush_init(Brush *brush)
        BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(brush, id));
 
        /* enable fake user by default */
-       brush->id.flag |= LIB_FAKEUSER;
+       id_fake_user_set(&brush->id);
 
        brush_defaults(brush);
 
@@ -193,11 +193,8 @@ Brush *BKE_brush_copy(Brush *brush)
        brushn->curve = curvemapping_copy(brush->curve);
 
        /* enable fake user by default */
-       if (!(brushn->id.flag & LIB_FAKEUSER)) {
-               brushn->id.flag |= LIB_FAKEUSER;
-               id_us_plus(&brushn->id);
-       }
-       
+       id_fake_user_set(&brush->id);
+
        if (brush->id.lib) {
                BKE_id_lib_local_paths(G.main, brush->id.lib, &brushn->id);
        }
@@ -280,15 +277,11 @@ void BKE_brush_make_local(Brush *brush)
                extern_local_brush(brush);
 
                /* enable fake user by default */
-               if (!(brush->id.flag & LIB_FAKEUSER)) {
-                       brush->id.flag |= LIB_FAKEUSER;
-                       id_us_plus(&brush->id);
-               }
+               id_fake_user_set(&brush->id);
        }
        else if (is_local && is_lib) {
-               Brush *brush_new = BKE_brush_copy(brush);
+               Brush *brush_new = BKE_brush_copy(brush);  /* Ensures FAKE_USER is set */
                brush_new->id.us = 1; /* only keep fake user */
-               brush_new->id.flag |= LIB_FAKEUSER;
 
                /* Remap paths of new ID using old library as base. */
                BKE_id_lib_local_paths(bmain, brush->id.lib, &brush_new->id);
index fe5ddb5fd93f6c8501be02800f4fec566a5d1f4d..5091ab7dfa05355dcf28575ca26f103dba15d687 100644 (file)
@@ -196,6 +196,22 @@ void id_us_min(ID *id)
        }
 }
 
+void id_fake_user_set(ID *id)
+{
+       if (id && !(id->flag & LIB_FAKEUSER)) {
+               id->flag |= LIB_FAKEUSER;
+               id_us_plus(id);
+       }
+}
+
+void id_fake_user_clear(ID *id)
+{
+       if (id && (id->flag & LIB_FAKEUSER)) {
+               id->flag &= ~LIB_FAKEUSER;
+               id_us_min(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)
@@ -1610,10 +1626,7 @@ void id_clear_lib_data(Main *bmain, ID *id)
 
        BKE_id_lib_local_paths(bmain, id->lib, id);
 
-       if (id->flag & LIB_FAKEUSER) {
-               id->us--;
-               id->flag &= ~LIB_FAKEUSER;
-       }
+       id_fake_user_clear(id);
 
        id->lib = NULL;
        id->flag = LIB_LOCAL;
index b79c72a425156aa92366ccda0a54b01ca393c8f9..cd26691c3e153c6a3de2e9139bb99f39ab451864 100644 (file)
@@ -804,7 +804,7 @@ static Mask *mask_alloc(Main *bmain, const char *name)
 
        mask = BKE_libblock_alloc(bmain, ID_MSK, name);
 
-       mask->id.flag |= LIB_FAKEUSER;
+       id_fake_user_set(&mask->id);
 
        return mask;
 }
@@ -843,10 +843,7 @@ Mask *BKE_mask_copy_nolib(Mask *mask)
        BKE_mask_layer_copy_list(&mask_new->masklayers, &mask->masklayers);
 
        /* enable fake user by default */
-       if (!(mask_new->id.flag & LIB_FAKEUSER)) {
-               mask_new->id.flag |= LIB_FAKEUSER;
-               id_us_plus(&mask_new->id);
-       }
+       id_fake_user_set(&mask->id);
 
        return mask_new;
 }
@@ -862,10 +859,7 @@ Mask *BKE_mask_copy(Mask *mask)
        BKE_mask_layer_copy_list(&mask_new->masklayers, &mask->masklayers);
 
        /* enable fake user by default */
-       if (!(mask_new->id.flag & LIB_FAKEUSER)) {
-               mask_new->id.flag |= LIB_FAKEUSER;
-               id_us_plus(&mask_new->id);
-       }
+       id_fake_user_set(&mask->id);
 
        if (mask->id.lib) {
                BKE_id_lib_local_paths(G.main, mask->id.lib, &mask_new->id);
index 06844b09a9b22d1ee69bb962983c57be722893c3..1b6fc92ef5e5b2621d9f7bd361465c3345879fca 100644 (file)
@@ -373,7 +373,7 @@ Palette *BKE_palette_add(Main *bmain, const char *name)
        palette = BKE_libblock_alloc(bmain, ID_PAL, name);
 
        /* enable fake user by default */
-       palette->id.flag |= LIB_FAKEUSER;
+       id_fake_user_set(&palette->id);
 
        return palette;
 }
index 9ebae184dd4ddf96dca8ff63466228a5afa4985d..73e13a762a97c76d9225befd3af132ed23ebd00b 100644 (file)
@@ -7928,8 +7928,7 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
        /* clear first 8 bits */
        id->flag = (id->flag & 0xFF00) | flag | LIB_NEED_LINK;
        id->lib = main->curlib;
-       if (id->flag & LIB_FAKEUSER) id->us= 1;
-       else id->us = 0;
+       id->us = (id->flag & LIB_FAKEUSER) ? 1 : 0;
        id->icon_id = 0;
        id->flag &= ~(LIB_ID_RECALC | LIB_ID_RECALC_DATA | LIB_DOIT | LIB_MISSING);
        
index 1238b883e9ac06b0ffe402c3dc72f3ba91d4a398..c606dd67f29010afc2dd43b162e809950aaf2c9f 100644 (file)
@@ -275,7 +275,7 @@ static void template_id_cb(bContext *C, void *arg_litem, void *arg_event)
 
                        if (id && CTX_wm_window(C)->eventstate->shift) {
                                /* only way to force-remove data (on save) */
-                               id->flag &= ~LIB_FAKEUSER;
+                               id_fake_user_clear(id);
                                id->us = 0;
                        }
 
index 8ce126b439eb7f21f3cb7456fd07ffc2f9a55333..a3be7791f9a6baf159ac878f904a2f236dfcf3cd 100644 (file)
@@ -581,10 +581,7 @@ void ED_animedit_unlink_action(bContext *C, ID *id, AnimData *adt, bAction *act,
                }
                
                /* Clear Fake User */
-               if (act->id.flag & LIB_FAKEUSER) {
-                       act->id.flag &= ~LIB_FAKEUSER;
-                       id_us_min(&act->id);
-               }
+               id_fake_user_clear(&act->id);
        }
        
        /* If in Tweak Mode, don't unlink. Instead, this 
index c0464c84061ac50ba92b3f8a200f362dc0dcf861..313a75f373a0d4ab5254157c4b9ee9dc0a8a397e 100644 (file)
@@ -421,10 +421,7 @@ static void id_fake_user_set_cb(bContext *UNUSED(C), Scene *UNUSED(scene), TreeE
 {
        ID *id = tselem->id;
        
-       if ((id) && ((id->flag & LIB_FAKEUSER) == 0)) {
-               id->flag |= LIB_FAKEUSER;
-               id_us_plus(id);
-       }
+       id_fake_user_set(id);
 }
 
 static void id_fake_user_clear_cb(bContext *UNUSED(C), Scene *UNUSED(scene), TreeElement *UNUSED(te),
@@ -432,10 +429,7 @@ static void id_fake_user_clear_cb(bContext *UNUSED(C), Scene *UNUSED(scene), Tre
 {
        ID *id = tselem->id;
        
-       if ((id) && (id->flag & LIB_FAKEUSER)) {
-               id->flag &= ~LIB_FAKEUSER;
-               id_us_min(id);
-       }
+       id_fake_user_clear(id);
 }
 
 static void id_select_linked_cb(bContext *C, Scene *UNUSED(scene), TreeElement *UNUSED(te),
index 43ffc7743c5b7264a2e27834814781418b198dce..e1e892a6b65ea991eaf84bb889c2999900ae0bba 100644 (file)
@@ -223,13 +223,11 @@ void rna_ID_fake_user_set(PointerRNA *ptr, int value)
 {
        ID *id = (ID *)ptr->data;
 
-       if (value && !(id->flag & LIB_FAKEUSER)) {
-               id->flag |= LIB_FAKEUSER;
-               id_us_plus(id);
+       if (value) {
+               id_fake_user_set(id);
        }
-       else if (!value && (id->flag & LIB_FAKEUSER)) {
-               id->flag &= ~LIB_FAKEUSER;
-               id_us_min(id);
+       else {
+               id_fake_user_clear(id);
        }
 }
 
@@ -329,8 +327,8 @@ static void rna_ID_update_tag(ID *id, ReportList *reports, int flag)
 
 static void rna_ID_user_clear(ID *id)
 {
+       id_fake_user_clear(id);
        id->us = 0; /* don't save */
-       id->flag &= ~LIB_FAKEUSER;
 }
 
 static AnimData * rna_ID_animation_data_create(ID *id, Main *bmain)
index 057fed50c3257e992e3aaa93cf8dd753632d3808..710ae97e4dcb4509ae264acbdd2ad561dbcfe3f9 100644 (file)
@@ -639,8 +639,7 @@ static void rna_Main_armatures_remove(Main *bmain, ReportList *reports, PointerR
 static bAction *rna_Main_actions_new(Main *bmain, const char *name)
 {
        bAction *act = add_empty_action(bmain, name);
-       id_us_min(&act->id);
-       act->id.flag &= ~LIB_FAKEUSER;
+       id_fake_user_clear(&act->id);
        return act;
 }
 static void rna_Main_actions_remove(Main *bmain, ReportList *reports, PointerRNA *act_ptr)