[Bf-blender-cvs] [e583d8e] layers: Layer collection nesting (DNA/RNA)

Dalai Felinto noreply at git.blender.org
Tue Nov 22 17:30:21 CET 2016


Commit: e583d8e21b2bfb755d8f8e8d1228ac456ff3312d
Author: Dalai Felinto
Date:   Tue Nov 22 17:24:48 2016 +0100
Branches: layers
https://developer.blender.org/rBe583d8e21b2bfb755d8f8e8d1228ac456ff3312d

Layer collection nesting (DNA/RNA)

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

M	source/blender/blenkernel/BKE_scene.h
M	source/blender/blenkernel/intern/scene.c
M	source/blender/makesdna/DNA_scene_types.h
M	source/blender/makesrna/intern/rna_scene.c

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

diff --git a/source/blender/blenkernel/BKE_scene.h b/source/blender/blenkernel/BKE_scene.h
index 5abc9e0..6ade94f 100644
--- a/source/blender/blenkernel/BKE_scene.h
+++ b/source/blender/blenkernel/BKE_scene.h
@@ -129,7 +129,11 @@ void BKE_scene_update_for_newframe_ex(struct EvaluationContext *eval_ctx, struct
 struct SceneLayer *BKE_scene_add_layer(struct Scene *sce, const char *name);
 bool BKE_scene_remove_layer(struct Main *main, struct Scene *scene, struct SceneLayer *sl);
 struct LayerCollection *BKE_scene_add_collection(struct SceneLayer *sl, const char *name);
+
+bool BKE_scene_remove_nested_collection(struct SceneLayer *sl, struct LayerCollection *lc_parent, struct LayerCollection *lc);
+struct SceneLayer *BKE_scene_layer_from_collection(struct Scene *scene, struct LayerCollection *lc);
 bool BKE_scene_remove_collection(struct SceneLayer *sl, struct LayerCollection *lc);
+struct LayerCollection *BKE_scene_add_nested_collection(struct SceneLayer *sl, struct LayerCollection *lc, const char *name);
 
 struct SceneRenderLayer *BKE_scene_add_render_layer(struct Scene *sce, const char *name);
 bool BKE_scene_remove_render_layer(struct Main *main, struct Scene *scene, struct SceneRenderLayer *srl);
diff --git a/source/blender/blenkernel/intern/scene.c b/source/blender/blenkernel/intern/scene.c
index bdea85e..9ea9f32 100644
--- a/source/blender/blenkernel/intern/scene.c
+++ b/source/blender/blenkernel/intern/scene.c
@@ -2034,6 +2034,64 @@ void BKE_scene_update_for_newframe_ex(EvaluationContext *eval_ctx, Main *bmain,
 #endif
 }
 
+/* Recursively check if collection is inside the collection ListBase */
+static bool collection_in_collections(ListBase *lb, LayerCollection *lc)
+{
+	for (LayerCollection *laycol = lb->first; laycol; laycol = laycol->next) {
+		if (laycol == lc)
+			return true;
+
+		if (collection_in_collections(&laycol->collections, lc))
+			return true;
+	}
+	return false;
+}
+
+SceneLayer *BKE_scene_layer_from_collection(Scene *scene, LayerCollection *lc)
+{
+	for (SceneLayer *sl = scene->layers.first; sl; sl = sl->next) {
+		if (collection_in_collections(&sl->collections, lc)) {
+			return sl;
+		}
+	}
+	return NULL;
+}
+
+LayerCollection *BKE_scene_add_nested_collection(SceneLayer *sl, LayerCollection *lc, const char *name)
+{
+	LayerCollection *lc_new;
+
+	lc_new = MEM_callocN(sizeof(LayerCollection), "new layer collection");
+	BLI_strncpy(lc_new->name, name, sizeof(lc_new->name));
+	BLI_uniquename(&sl->collections, lc_new, DATA_("Collection"), '.', offsetof(LayerCollection, name), sizeof(lc_new->name));
+	BLI_addtail(&lc->collections, lc_new);
+
+	return lc_new;
+}
+
+static void free_collection(LayerCollection *lc)
+{
+	BLI_freelistN(&lc->elements);
+	BLI_freelistN(&lc->overrides);
+}
+
+bool BKE_scene_remove_nested_collection(SceneLayer *sl, LayerCollection *lc_parent, LayerCollection *lc)
+{
+	const int act = BLI_findindex(&lc_parent->collections, lc);
+	if (act == -1) {
+		return false;
+	}
+
+	BLI_remlink(&lc_parent->collections, lc);
+	free_collection(lc);
+	MEM_freeN(lc);
+
+	/* TODO only change active_collection if necessary */
+	sl->active_collection = 0;
+
+	return true;
+}
+
 LayerCollection *BKE_scene_add_collection(SceneLayer *sl, const char *name)
 {
 	LayerCollection *lc;
@@ -2046,7 +2104,6 @@ LayerCollection *BKE_scene_add_collection(SceneLayer *sl, const char *name)
 	return lc;
 }
 
-
 bool BKE_scene_remove_collection(SceneLayer *sl, LayerCollection *lc)
 {
 	const int act = BLI_findindex(&sl->collections, lc);
@@ -2061,6 +2118,8 @@ bool BKE_scene_remove_collection(SceneLayer *sl, LayerCollection *lc)
 	}
 
 	BLI_remlink(&sl->collections, lc);
+	free_collection(lc);
+	MEM_freeN(lc);
 
 	BLI_freelistN(&lc->elements);
 	BLI_freelistN(&lc->overrides);
@@ -2108,8 +2167,7 @@ bool BKE_scene_remove_layer(Main *bmain, Scene *scene, SceneLayer *sl)
 	BLI_freelistN(&sl->base);
 
 	for (LayerCollection *lc = sl->collections.first; lc; lc = lc->next) {
-		BLI_freelistN(&lc->elements);
-		BLI_freelistN(&lc->overrides);
+		free_collection(lc);
 	}
 
 	BLI_freelistN(&sl->collections);
diff --git a/source/blender/makesdna/DNA_scene_types.h b/source/blender/makesdna/DNA_scene_types.h
index cb63ec9..cc1ef4d 100644
--- a/source/blender/makesdna/DNA_scene_types.h
+++ b/source/blender/makesdna/DNA_scene_types.h
@@ -95,7 +95,7 @@ typedef struct LayerCollection {
 	short pad[3];
 	ListBase elements;
 	ListBase overrides;
-	/* TODO nesting */
+	ListBase collections;	/* nested collections */
 	/* TODO dynamic adding of elements (name based) */
 } LayerCollection;
 
diff --git a/source/blender/makesrna/intern/rna_scene.c b/source/blender/makesrna/intern/rna_scene.c
index 4a12b0d..74e8658 100644
--- a/source/blender/makesrna/intern/rna_scene.c
+++ b/source/blender/makesrna/intern/rna_scene.c
@@ -1335,14 +1335,47 @@ static void rna_FFmpegSettings_codec_settings_update(Main *UNUSED(bmain), Scene
 }
 #endif
 
+static LayerCollection *rna_LayerNestedCollection_new(ID *id, LayerCollection *lc, const char *name)
+{
+	Scene *scene = (Scene *)id;
+	SceneLayer *sl = BKE_scene_layer_from_collection(scene, lc);
+	LayerCollection *lc_new = BKE_scene_add_nested_collection(sl, lc, name);
+
+	DAG_id_tag_update(id, 0);
+	WM_main_add_notifier(NC_SCENE | ND_LAYER, NULL);
+
+	return lc_new;
+}
+
+static void rna_LayerNestedCollection_remove(
+        ID *id, LayerCollection *lc_parent, ReportList *reports, PointerRNA *lc_ptr)
+{
+	Scene *scene = (Scene *)id;
+	SceneLayer *sl = BKE_scene_layer_from_collection(scene, lc_parent);
+	LayerCollection *lc = lc_ptr->data;
+
+	if (!BKE_scene_remove_nested_collection(sl, lc_parent, lc)) {
+		BKE_reportf(reports, RPT_ERROR, "Nested collection '%s' could not be removed from collection '%s'",
+		            lc->name, lc_parent->name );
+		return;
+	}
+
+	RNA_POINTER_INVALIDATE(lc_ptr);
+
+	DAG_id_tag_update(&scene->id, 0);
+	WM_main_add_notifier(NC_SCENE | ND_LAYER, NULL);
+}
+
 static int rna_Layer_active_collection_index_get(PointerRNA *ptr)
 {
+	/* TODO account for nested collections */
 	SceneLayer *sl = (SceneLayer *)ptr->data;
 	return sl->active_collection;
 }
 
 static void rna_Layer_active_collection_index_set(PointerRNA *ptr, int value)
 {
+	/* TODO account for nested collections */
 	SceneLayer *sl = (SceneLayer *)ptr->data;
 	int num_collections = BLI_listbase_count(&sl->collections);
 	sl->active_collection = min_ff(value, num_collections - 1);
@@ -1351,6 +1384,7 @@ static void rna_Layer_active_collection_index_set(PointerRNA *ptr, int value)
 static void rna_Layer_active_collection_index_range(
         PointerRNA *ptr, int *min, int *max, int *UNUSED(softmin), int *UNUSED(softmax))
 {
+	/* TODO account for nested collections */
 	SceneLayer *sl = (SceneLayer *)ptr->data;
 
 	*min = 0;
@@ -1359,6 +1393,7 @@ static void rna_Layer_active_collection_index_range(
 
 static PointerRNA rna_Layer_active_collection_get(PointerRNA *ptr)
 {
+	/* TODO account for nested collections */
 	SceneLayer *sl = (SceneLayer *)ptr->data;
 	LayerCollection *lc = BLI_findlink(&sl->collections, sl->active_collection);
 
@@ -1367,6 +1402,7 @@ static PointerRNA rna_Layer_active_collection_get(PointerRNA *ptr)
 
 static void rna_Layer_active_collection_set(PointerRNA *ptr, PointerRNA value)
 {
+	/* TODO account for nested collections */
 	SceneLayer *sl = (SceneLayer *)ptr->data;
 	LayerCollection *lc = (LayerCollection *)value.data;
 	const int index = BLI_findindex(&sl->collections, lc);
@@ -5073,6 +5109,36 @@ static void rna_def_gpu_fx(BlenderRNA *brna)
 }
 
 /* Scene Layers */
+
+static void rna_def_layer_nested_collections(BlenderRNA *brna, PropertyRNA *cprop)
+{
+	StructRNA *srna;
+	PropertyRNA *prop;
+
+	FunctionRNA *func;
+	PropertyRNA *parm;
+
+	RNA_def_property_srna(cprop, "LayerNestedCollections");
+	srna = RNA_def_struct(brna, "LayerNestedCollections", NULL);
+	RNA_def_struct_sdna(srna, "LayerCollection");
+	RNA_def_struct_ui_text(srna, "Layer Collections", "Collection of layer collections");
+
+	func = RNA_def_function(srna, "new", "rna_LayerNestedCollection_new");
+	RNA_def_function_ui_description(func, "Add a nested layer collection to collection ");
+	RNA_def_function_flag(func, FUNC_USE_SELF_ID);
+	parm = RNA_def_string(func, "name", "LayerCollection", 0, "", "New name for the layer collection (not unique)");
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+	parm = RNA_def_pointer(func, "result", "LayerCollection", "", "Newly created layer collection");
+	RNA_def_function_return(func, parm);
+
+	func = RNA_def_function(srna, "remove", "rna_LayerNestedCollection_remove");
+	RNA_def_function_ui_description(func, "Remove a layer collection");
+	RNA_def_function_flag(func, FUNC_USE_REPORTS | FUNC_USE_SELF_ID);
+	parm = RNA_def_pointer(func, "collection", "LayerCollection", "", "Layer collection to remove");
+	RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL | PROP_RNAPTR);
+	RNA_def_property_clear_flag(parm, PROP_THICK_WRAP);
+}
+
 static void rna_def_layer_collection(BlenderRNA *brna)
 {
 	StructRNA *srna;
@@ -5089,7 +5155,12 @@ static void rna_def_layer_collection(BlenderRNA *brna)
 
 	/* TODO baselist (selected objects, ...) */
 	/* TODO overrides */
-	/* TODO nested collections */
+
+	prop = RNA_def_property(srna, "collections", PROP_COLLECTION, PROP_NONE);
+	RNA_def_property_collection_sdna(prop, NULL, "collections", NULL);
+	RNA_def_property_struct_type(prop, "LayerCollection");
+	RNA_def_property_ui_text(prop, "Layer Collections", "");
+	rna_def_layer_nested_collections(brna, prop);
 
 	/* Flags */
 	prop = RNA_def_property(srna, "hide", PROP_BOOLEAN, PROP_NONE);




More information about the Bf-blender-cvs mailing list