diff options
author | Dalai Felinto <dfelinto@gmail.com> | 2018-04-02 23:08:51 +0300 |
---|---|---|
committer | Dalai Felinto <dfelinto@gmail.com> | 2018-04-02 23:25:22 +0300 |
commit | 1f291d5814c3797d44889f5fa681e5d8cb718cf5 (patch) | |
tree | d474aaa88b9056d61ccf6553c9713c5b705abddb | |
parent | 0faa065ed4aaa87e53bb574d950ebf8ac0cfd4a3 (diff) |
Collections: API to get automatic name of new collection
-rw-r--r-- | source/blender/blenkernel/BKE_collection.h | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/collection.c | 43 |
2 files changed, 30 insertions, 15 deletions
diff --git a/source/blender/blenkernel/BKE_collection.h b/source/blender/blenkernel/BKE_collection.h index 309acce752d..d3a4d2b8d5b 100644 --- a/source/blender/blenkernel/BKE_collection.h +++ b/source/blender/blenkernel/BKE_collection.h @@ -62,6 +62,8 @@ void BKE_collection_object_move(struct ID *owner_id, struct SceneCollection *sc_ bool BKE_collection_object_exists(struct SceneCollection *scene_collection, struct Object *ob); struct SceneCollection *BKE_collection_from_index(struct Scene *scene, const int index); +void BKE_collection_new_name_get(struct ID *owner_id, struct SceneCollection *sc_parent, char *rname); + bool BKE_collection_objects_select(struct ViewLayer *view_layer, struct SceneCollection *scene_collection); struct Group *BKE_collection_group_create(struct Main *bmain, struct Scene *scene, struct LayerCollection *lc); diff --git a/source/blender/blenkernel/intern/collection.c b/source/blender/blenkernel/intern/collection.c index 29e3539ab59..15fda8a9786 100644 --- a/source/blender/blenkernel/intern/collection.c +++ b/source/blender/blenkernel/intern/collection.c @@ -68,6 +68,28 @@ static SceneCollection *collection_master_from_id(const ID *owner_id) } /** + * The automatic/fallback name of a new collection. + */ +void BKE_collection_new_name_get(ID *owner_id, SceneCollection *sc_parent, char *rname) +{ + SceneCollection *sc_master = collection_master_from_id(owner_id); + char *name; + + if (sc_parent == sc_master) { + name = BLI_sprintfN("Collection %d", BLI_listbase_count(&sc_master->scene_collections) + 1); + } + else { + const int number = BLI_listbase_count(&sc_parent->scene_collections) + 1; + const int digits = integer_digits_i(number); + const int max_len = sizeof(sc_parent->name) - 1 /* NULL terminator */ - (1 + digits) /* " %d" */; + name = BLI_sprintfN("%.*s %d", max_len, sc_parent->name, number); + } + + BLI_strncpy(rname, name, MAX_NAME); + MEM_freeN(name); +} + +/** * Add a new collection, but don't handle syncing with layer collections */ static SceneCollection *collection_add(ID *owner_id, SceneCollection *sc_parent, const int type, const char *name_custom) @@ -75,31 +97,22 @@ static SceneCollection *collection_add(ID *owner_id, SceneCollection *sc_parent, SceneCollection *sc_master = collection_master_from_id(owner_id); SceneCollection *sc = MEM_callocN(sizeof(SceneCollection), "New Collection"); sc->type = type; - const char *name = name_custom; + char name[MAX_NAME]; if (!sc_parent) { sc_parent = sc_master; } - if (!name) { - if (sc_parent == sc_master) { - name = BLI_sprintfN("Collection %d", BLI_listbase_count(&sc_master->scene_collections) + 1); - } - else { - const int number = BLI_listbase_count(&sc_parent->scene_collections) + 1; - const int digits = integer_digits_i(number); - const int max_len = sizeof(sc_parent->name) - 1 /* NULL terminator */ - (1 + digits) /* " %d" */; - name = BLI_sprintfN("%.*s %d", max_len, sc_parent->name, number); - } + if (name_custom != NULL) { + BLI_strncpy(name, name_custom, MAX_NAME); + } + else { + BKE_collection_new_name_get(owner_id, sc_parent, name); } BLI_addtail(&sc_parent->scene_collections, sc); BKE_collection_rename(owner_id, sc, name); - if (name != name_custom) { - MEM_freeN((char *)name); - } - return sc; } |