diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2016-07-01 18:51:08 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2016-07-01 19:29:12 +0300 |
commit | 85c9aefe0fef6d9f862d34ca66beb0bb4c2bcf6c (patch) | |
tree | fcd225394d81edabd3c99ce1aa5aa489168767c4 /source/blender/editors | |
parent | ad717fe7373514e1695e016224b9b2180885a487 (diff) |
"Fix" crash when deleting linked object which has indirect usages.
This is in fact very hairy situation here... Objects are only refcounted by scenes,
any other usage is 'free', which means once all object instanciations are gone Blender
considers it can delete it.
There is a trap here though: indirect usages. Typically, we should never modify linked data
(because it is essencially useless, changes would be ignored and ost on next reload or
even undo/redo). This means indirect usages are not affected by default 'safe' remapping/unlinking.
For unlinking preceeding deletion however, this is not acceptable - we are likely to end with
a zero-user ID (aka deletable one) which is still actually used by other linked data.
Solution choosen here is double:
I) From 'user-space' (i.e. outliner, operators...), we check for cases where deleting datablocks
should not be allowed (indirect data or indirectly used data), and abort (with report) if needed.
II) From 'lower' level (BKE_library_remap and RNA), we also unlink from linked data,
which makes actual deletion possible and safe.
Note that with previous behavior (2.77 one), linked object would be deleted, including from linked data -
but then, once file is saved and reloaded, indirect usage would link back the deleted object,
without any instanciation in scene, which made it somehow virtual and unreachable...
With new behavior, this is no more possible, but on the other hand it means that in situations of dependency cycles
(two linked objects using each other), linked objects become impossible to delete (from user space).
Not sure what's best here, behavior with those corner cases of library linking is very poorly defined... :(
Diffstat (limited to 'source/blender/editors')
-rw-r--r-- | source/blender/editors/object/object_add.c | 29 | ||||
-rw-r--r-- | source/blender/editors/object/object_group.c | 2 | ||||
-rw-r--r-- | source/blender/editors/space_outliner/outliner_edit.c | 21 | ||||
-rw-r--r-- | source/blender/editors/space_outliner/outliner_tools.c | 35 |
4 files changed, 76 insertions, 11 deletions
diff --git a/source/blender/editors/object/object_add.c b/source/blender/editors/object/object_add.c index f85c76291cd..0f1c72fcd55 100644 --- a/source/blender/editors/object/object_add.c +++ b/source/blender/editors/object/object_add.c @@ -73,6 +73,7 @@ #include "BKE_lamp.h" #include "BKE_lattice.h" #include "BKE_library.h" +#include "BKE_library_query.h" #include "BKE_key.h" #include "BKE_main.h" #include "BKE_material.h" @@ -1108,11 +1109,18 @@ static void object_delete_check_glsl_update(Object *ob) /* note: now unlinks constraints as well */ void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Base *base) { - DAG_id_type_tag(bmain, ID_OB); + if (BKE_library_ID_is_indirectly_used(bmain, base->object) && ID_REAL_USERS(base->object) <= 1) { + /* We cannot delete indirectly used object... */ + printf("WARNING, undeletable object '%s', should have been catched before reaching this function!", + base->object->id.name + 2); + return; + } + BKE_scene_base_unlink(scene, base); object_delete_check_glsl_update(base->object); BKE_libblock_free_us(bmain, base->object); MEM_freeN(base); + DAG_id_type_tag(bmain, ID_OB); } static int object_delete_exec(bContext *C, wmOperator *op) @@ -1129,6 +1137,19 @@ static int object_delete_exec(bContext *C, wmOperator *op) CTX_DATA_BEGIN (C, Base *, base, selected_bases) { + const bool is_indirectly_used = BKE_library_ID_is_indirectly_used(bmain, base->object); + if (base->object->id.tag & LIB_TAG_INDIRECT) { + /* Can this case ever happen? */ + BKE_reportf(op->reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", base->object->id.name + 2); + continue; + } + else if (is_indirectly_used && ID_REAL_USERS(base->object) <= 1) { + BKE_reportf(op->reports, RPT_WARNING, + "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user", + base->object->id.name + 2, scene->id.name + 2); + continue; + } + /* deselect object -- it could be used in other scenes */ base->object->flag &= ~SELECT; @@ -1144,6 +1165,12 @@ static int object_delete_exec(bContext *C, wmOperator *op) if (scene_iter != scene && !(scene_iter->id.lib)) { base_other = BKE_scene_base_find(scene_iter, base->object); if (base_other) { + if (is_indirectly_used && ID_REAL_USERS(base->object) <= 1) { + BKE_reportf(op->reports, RPT_WARNING, + "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user", + base->object->id.name + 2, scene_iter->id.name + 2); + break; + } ED_base_object_free_and_unlink(bmain, scene_iter, base_other); } } diff --git a/source/blender/editors/object/object_group.c b/source/blender/editors/object/object_group.c index 2b87a890f0f..bcdd170c53c 100644 --- a/source/blender/editors/object/object_group.c +++ b/source/blender/editors/object/object_group.c @@ -528,7 +528,7 @@ static int group_unlink_exec(bContext *C, wmOperator *UNUSED(op)) if (!group) return OPERATOR_CANCELLED; - BKE_libblock_unlink(bmain, group, false); + BKE_libblock_unlink(bmain, group, false, false); BKE_libblock_free(bmain, group); WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, NULL); diff --git a/source/blender/editors/space_outliner/outliner_edit.c b/source/blender/editors/space_outliner/outliner_edit.c index 45c45ffe39c..7990c169fce 100644 --- a/source/blender/editors/space_outliner/outliner_edit.c +++ b/source/blender/editors/space_outliner/outliner_edit.c @@ -55,6 +55,7 @@ #include "BKE_global.h" #include "BKE_idcode.h" #include "BKE_library.h" +#include "BKE_library_query.h" #include "BKE_library_remap.h" #include "BKE_main.h" #include "BKE_outliner_treehash.h" @@ -307,7 +308,7 @@ void OUTLINER_OT_item_rename(wmOperatorType *ot) /* ID delete --------------------------------------------------- */ -static void id_delete(bContext *C, TreeElement *te, TreeStoreElem *tselem) +static void id_delete(bContext *C, ReportList *reports, TreeElement *te, TreeStoreElem *tselem) { Main *bmain = CTX_data_main(C); ID *id = tselem->id; @@ -316,16 +317,28 @@ static void id_delete(bContext *C, TreeElement *te, TreeStoreElem *tselem) BLI_assert(te->idcode != ID_LI || ((Library *)id)->parent == NULL); UNUSED_VARS_NDEBUG(te); + if (id->tag & LIB_TAG_INDIRECT) { + BKE_reportf(reports, RPT_WARNING, "Cannot delete indirectly linked id '%s'", id->name); + return; + } + else if (BKE_library_ID_is_indirectly_used(bmain, id) && ID_REAL_USERS(id) <= 1) { + BKE_reportf(reports, RPT_WARNING, + "Cannot delete id '%s', indirectly used datablocks need at least one user", + id->name); + return; + } + + BKE_libblock_delete(bmain, id); WM_event_add_notifier(C, NC_WINDOW, NULL); } void id_delete_cb( - bContext *C, ReportList *UNUSED(reports), Scene *UNUSED(scene), + bContext *C, ReportList *reports, Scene *UNUSED(scene), TreeElement *te, TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data)) { - id_delete(C, te, tselem); + id_delete(C, reports, te, tselem); } static int outliner_id_delete_invoke_do(bContext *C, ReportList *reports, TreeElement *te, const float mval[2]) @@ -339,7 +352,7 @@ static int outliner_id_delete_invoke_do(bContext *C, ReportList *reports, TreeEl "Cannot delete indirectly linked library '%s'", ((Library *)tselem->id)->filepath); return OPERATOR_CANCELLED; } - id_delete(C, te, tselem); + id_delete(C, reports, te, tselem); return OPERATOR_FINISHED; } } diff --git a/source/blender/editors/space_outliner/outliner_tools.c b/source/blender/editors/space_outliner/outliner_tools.c index d92fec4c90d..bfec62997e1 100644 --- a/source/blender/editors/space_outliner/outliner_tools.c +++ b/source/blender/editors/space_outliner/outliner_tools.c @@ -57,6 +57,7 @@ #include "BKE_fcurve.h" #include "BKE_group.h" #include "BKE_library.h" +#include "BKE_library_query.h" #include "BKE_library_remap.h" #include "BKE_main.h" #include "BKE_report.h" @@ -397,7 +398,7 @@ static void object_deselect_cb( } static void object_delete_cb( - bContext *C, ReportList *UNUSED(reports), Scene *scene, TreeElement *te, + bContext *C, ReportList *reports, Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data)) { Base *base = (Base *)te->directdata; @@ -405,6 +406,18 @@ static void object_delete_cb( if (base == NULL) base = BKE_scene_base_find(scene, (Object *)tselem->id); if (base) { + Main *bmain = CTX_data_main(C); + if (base->object->id.tag & LIB_TAG_INDIRECT) { + BKE_reportf(reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", base->object->id.name + 2); + return; + } + else if (BKE_library_ID_is_indirectly_used(bmain, base->object) && ID_REAL_USERS(base->object) <= 1) { + BKE_reportf(reports, RPT_WARNING, + "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user", + base->object->id.name + 2, scene->id.name + 2); + return; + } + // check also library later if (scene->obedit == base->object) ED_object_editmode_exit(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO); @@ -809,7 +822,7 @@ static void outliner_do_data_operation(SpaceOops *soops, int type, int event, Li } } -static Base *outline_delete_hierarchy(bContext *C, Scene *scene, Base *base) +static Base *outline_delete_hierarchy(bContext *C, ReportList *reports, Scene *scene, Base *base) { Base *child_base, *base_next; Object *parent; @@ -822,17 +835,29 @@ static Base *outline_delete_hierarchy(bContext *C, Scene *scene, Base *base) base_next = child_base->next; for (parent = child_base->object->parent; parent && (parent != base->object); parent = parent->parent); if (parent) { - base_next = outline_delete_hierarchy(C, scene, child_base); + base_next = outline_delete_hierarchy(C, reports, scene, child_base); } } base_next = base->next; + + Main *bmain = CTX_data_main(C); + if (base->object->id.tag & LIB_TAG_INDIRECT) { + BKE_reportf(reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", base->object->id.name + 2); + return base_next; + } + else if (BKE_library_ID_is_indirectly_used(bmain, base->object) && ID_REAL_USERS(base->object) <= 1) { + BKE_reportf(reports, RPT_WARNING, + "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user", + base->object->id.name + 2, scene->id.name + 2); + return base_next; + } ED_base_object_free_and_unlink(CTX_data_main(C), scene, base); return base_next; } static void object_delete_hierarchy_cb( - bContext *C, ReportList *UNUSED(reports), Scene *scene, + bContext *C, ReportList *reports, Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data)) { Base *base = (Base *)te->directdata; @@ -848,7 +873,7 @@ static void object_delete_hierarchy_cb( ED_object_editmode_exit(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO); } - outline_delete_hierarchy(C, scene, base); + outline_delete_hierarchy(C, reports, scene, base); /* leave for ED_outliner_id_unref to handle */ #if 0 te->directdata = NULL; |