[Bf-blender-cvs] [1f291d5814c] blender2.8: Collections: API to get automatic name of new collection

Dalai Felinto noreply at git.blender.org
Mon Apr 2 22:55:02 CEST 2018


Commit: 1f291d5814c3797d44889f5fa681e5d8cb718cf5
Author: Dalai Felinto
Date:   Mon Apr 2 17:08:51 2018 -0300
Branches: blender2.8
https://developer.blender.org/rB1f291d5814c3797d44889f5fa681e5d8cb718cf5

Collections: API to get automatic name of new collection

===================================================================

M	source/blender/blenkernel/BKE_collection.h
M	source/blender/blenkernel/intern/collection.c

===================================================================

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



More information about the Bf-blender-cvs mailing list