Collections: API to get automatic name of new collection

This commit is contained in:
Dalai Felinto 2018-04-02 17:08:51 -03:00
parent 0faa065ed4
commit 1f291d5814
2 changed files with 30 additions and 15 deletions

View File

@ -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);

View File

@ -67,6 +67,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
*/
@ -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;
}