Cleanup: de-duplicate function to instantiate objects

De-duplicates wm_append_loose_data_instantiate_object_base_instance_init
and object_base_instance_init.

Add BLO_object_instantiate_object_base_instance_init which also adds to
a collection since all callers did this.
This commit is contained in:
Campbell Barton 2021-10-21 16:44:05 +11:00
parent 2a047fadc0
commit 063c4e467d
3 changed files with 40 additions and 51 deletions

View File

@ -32,11 +32,13 @@ extern "C" {
struct BHead;
struct BlendThumbnail;
struct Collection;
struct FileData;
struct LinkNode;
struct ListBase;
struct Main;
struct MemFile;
struct Object;
struct ReportList;
struct Scene;
struct UserDef;
@ -333,6 +335,14 @@ void BLO_sanitize_experimental_features_userpref_blend(struct UserDef *userdef);
struct BlendThumbnail *BLO_thumbnail_from_file(const char *filepath);
void BLO_object_instantiate_object_base_instance_init(struct Main *bmain,
struct Collection *collection,
struct Object *ob,
struct ViewLayer *view_layer,
const struct View3D *v3d,
const int flag,
bool set_active);
/* datafiles (generated theme) */
extern const struct bTheme U_theme_default;
extern const struct UserDef U_default;

View File

@ -4471,7 +4471,7 @@ static bool object_in_any_collection(Main *bmain, Object *ob)
* Shared operations to perform on the object's base after adding it to the scene.
*/
static void object_base_instance_init(
Object *ob, bool set_selected, bool set_active, ViewLayer *view_layer, const View3D *v3d)
Object *ob, ViewLayer *view_layer, const View3D *v3d, const int flag, bool set_active)
{
Base *base = BKE_view_layer_base_find(view_layer, ob);
@ -4479,7 +4479,7 @@ static void object_base_instance_init(
base->local_view_bits |= v3d->local_view_uuid;
}
if (set_selected) {
if (flag & FILE_AUTOSELECT) {
if (base->flag & BASE_SELECTABLE) {
base->flag |= BASE_SELECTED;
}
@ -4492,6 +4492,22 @@ static void object_base_instance_init(
BKE_scene_object_base_flag_sync_from_base(base);
}
/**
* Exported for link/append to create objects as well.
*/
void BLO_object_instantiate_object_base_instance_init(Main *bmain,
Collection *collection,
Object *ob,
ViewLayer *view_layer,
const View3D *v3d,
const int flag,
bool set_active)
{
BKE_collection_object_add(bmain, collection, ob);
object_base_instance_init(ob, view_layer, v3d, flag, set_active);
}
static void add_loose_objects_to_scene(Main *mainvar,
Main *bmain,
Scene *scene,
@ -4540,13 +4556,11 @@ static void add_loose_objects_to_scene(Main *mainvar,
CLAMP_MIN(ob->id.us, 0);
ob->mode = OB_MODE_OBJECT;
BKE_collection_object_add(bmain, active_collection, ob);
const bool set_selected = (flag & FILE_AUTOSELECT) != 0;
/* Do NOT make base active here! screws up GUI stuff,
* if you want it do it at the editor level. */
const bool set_active = false;
object_base_instance_init(ob, set_selected, set_active, view_layer, v3d);
BLO_object_instantiate_object_base_instance_init(
bmain, active_collection, ob, view_layer, v3d, flag, set_active);
ob->id.tag &= ~LIB_TAG_INDIRECT;
ob->id.flag &= ~LIB_INDIRECT_WEAK_LINK;
@ -4602,13 +4616,11 @@ static void add_loose_object_data_to_scene(Main *mainvar,
id_us_plus(id);
BKE_object_materials_test(bmain, ob, ob->data);
BKE_collection_object_add(bmain, active_collection, ob);
const bool set_selected = (flag & FILE_AUTOSELECT) != 0;
/* Do NOT make base active here! screws up GUI stuff,
* if you want it do it at the editor level. */
bool set_active = false;
object_base_instance_init(ob, set_selected, set_active, view_layer, v3d);
BLO_object_instantiate_object_base_instance_init(
bmain, active_collection, ob, view_layer, v3d, flag, set_active);
copy_v3_v3(ob->loc, scene->cursor.location);
}
@ -4641,13 +4653,12 @@ static void add_collections_to_scene(Main *mainvar,
ob->type = OB_EMPTY;
ob->empty_drawsize = U.collection_instance_empty_size;
BKE_collection_object_add(bmain, active_collection, ob);
const bool set_selected = (flag & FILE_AUTOSELECT) != 0;
/* TODO: why is it OK to make this active here but not in other situations?
* See other callers of #object_base_instance_init */
const bool set_active = set_selected;
object_base_instance_init(ob, set_selected, set_active, view_layer, v3d);
BLO_object_instantiate_object_base_instance_init(
bmain, active_collection, ob, view_layer, v3d, flag, set_active);
DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);

View File

@ -312,31 +312,6 @@ static bool object_in_any_collection(Main *bmain, Object *ob)
return false;
}
/**
* Shared operations to perform on the object's base after adding it to the scene.
*/
static void wm_append_loose_data_instantiate_object_base_instance_init(
Object *ob, bool set_selected, bool set_active, ViewLayer *view_layer, const View3D *v3d)
{
Base *base = BKE_view_layer_base_find(view_layer, ob);
if (v3d != NULL) {
base->local_view_bits |= v3d->local_view_uuid;
}
if (set_selected) {
if (base->flag & BASE_SELECTABLE) {
base->flag |= BASE_SELECTED;
}
}
if (set_active) {
view_layer->basact = base;
}
BKE_scene_object_base_flag_sync_from_base(base);
}
static ID *wm_append_loose_data_instantiate_process_check(WMLinkAppendDataItem *item)
{
/* We consider that if we either kept it linked, or re-used already local data, instantiation
@ -402,7 +377,6 @@ static void wm_append_loose_data_instantiate(WMLinkAppendData *lapp_data,
Collection *active_collection = NULL;
const bool do_obdata = (lapp_data->flag & BLO_LIBLINK_OBDATA_INSTANCE) != 0;
const bool object_set_selected = (lapp_data->flag & FILE_AUTOSELECT) != 0;
/* Do NOT make base active here! screws up GUI stuff,
* if you want it do it at the editor level. */
const bool object_set_active = false;
@ -484,14 +458,12 @@ static void wm_append_loose_data_instantiate(WMLinkAppendData *lapp_data,
ob->type = OB_EMPTY;
ob->empty_drawsize = U.collection_instance_empty_size;
BKE_collection_object_add(bmain, active_collection, ob);
const bool set_selected = (lapp_data->flag & FILE_AUTOSELECT) != 0;
/* TODO: why is it OK to make this active here but not in other situations?
* See other callers of #object_base_instance_init */
const bool set_active = set_selected;
wm_append_loose_data_instantiate_object_base_instance_init(
ob, set_selected, set_active, view_layer, v3d);
BLO_object_instantiate_object_base_instance_init(
bmain, active_collection, ob, view_layer, v3d, lapp_data->flag, set_active);
/* Assign the collection. */
ob->instance_collection = collection;
@ -538,10 +510,8 @@ static void wm_append_loose_data_instantiate(WMLinkAppendData *lapp_data,
CLAMP_MIN(ob->id.us, 0);
ob->mode = OB_MODE_OBJECT;
BKE_collection_object_add(bmain, active_collection, ob);
wm_append_loose_data_instantiate_object_base_instance_init(
ob, object_set_selected, object_set_active, view_layer, v3d);
BLO_object_instantiate_object_base_instance_init(
bmain, active_collection, ob, view_layer, v3d, lapp_data->flag, object_set_active);
}
if (!do_obdata) {
@ -572,10 +542,8 @@ static void wm_append_loose_data_instantiate(WMLinkAppendData *lapp_data,
id_us_plus(id);
BKE_object_materials_test(bmain, ob, ob->data);
BKE_collection_object_add(bmain, active_collection, ob);
wm_append_loose_data_instantiate_object_base_instance_init(
ob, object_set_selected, object_set_active, view_layer, v3d);
BLO_object_instantiate_object_base_instance_init(
bmain, active_collection, ob, view_layer, v3d, lapp_data->flag, object_set_active);
copy_v3_v3(ob->loc, scene->cursor.location);