[Bf-blender-cvs] [d4e4358] master: Cleanup/refactor RNA IDs' `remove` functions.

Bastien Montagne noreply at git.blender.org
Sat Jun 25 18:36:48 CEST 2016


Commit: d4e435836dd9f903f641b55a0b5735e47dc4781f
Author: Bastien Montagne
Date:   Sat Jun 25 17:01:36 2016 +0200
Branches: master
https://developer.blender.org/rBd4e435836dd9f903f641b55a0b5735e47dc4781f

Cleanup/refactor RNA IDs' `remove` functions.

Those (one per ID type!) were uselessly duplicated, and badly inconsistent
(some types were actually unlinking before deletion, others were only working if already unlinked!).

Now we use same func and same API for all types, by default deletion is performed only if ID is no more used,
set `do_unlink` parameter to True to always delete ID even if still in use.
Only exception now is with Scene, since we always want to keep at least one!

Note that this will change default behavior of some types (since unlinking is never done anymore by default).

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

M	intern/cycles/blender/blender_mesh.cpp
M	source/blender/makesrna/intern/rna_main_api.c

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

diff --git a/intern/cycles/blender/blender_mesh.cpp b/intern/cycles/blender/blender_mesh.cpp
index 80db511..4bd385c 100644
--- a/intern/cycles/blender/blender_mesh.cpp
+++ b/intern/cycles/blender/blender_mesh.cpp
@@ -801,7 +801,7 @@ Mesh *BlenderSync::sync_mesh(BL::Object& b_ob,
 			}
 
 			/* free derived mesh */
-			b_data.meshes.remove(b_mesh);
+			b_data.meshes.remove(b_mesh, false);
 		}
 	}
 	mesh->geometry_flags = requested_geometry_flags;
@@ -1013,7 +1013,7 @@ void BlenderSync::sync_mesh_motion(BL::Object& b_ob,
 		sync_curves(mesh, b_mesh, b_ob, true, time_index);
 
 	/* free derived mesh */
-	b_data.meshes.remove(b_mesh);
+	b_data.meshes.remove(b_mesh, false);
 }
 
 CCL_NAMESPACE_END
diff --git a/source/blender/makesrna/intern/rna_main_api.c b/source/blender/makesrna/intern/rna_main_api.c
index 679f20f..c0a0bc0 100644
--- a/source/blender/makesrna/intern/rna_main_api.c
+++ b/source/blender/makesrna/intern/rna_main_api.c
@@ -62,6 +62,7 @@
 #include "BKE_object.h"
 #include "BKE_material.h"
 #include "BKE_icons.h"
+#include "BKE_idcode.h"
 #include "BKE_image.h"
 #include "BKE_texture.h"
 #include "BKE_scene.h"
@@ -114,30 +115,38 @@
 #  include "BPY_extern.h"
 #endif
 
+
+static void rna_Main_ID_remove(Main *bmain, ReportList *reports, PointerRNA *id_ptr, int do_unlink)
+{
+	ID *id = id_ptr->data;
+	if (do_unlink) {
+		BKE_libblock_delete(bmain, id);
+		RNA_POINTER_INVALIDATE(id_ptr);
+	}
+	else if (ID_REAL_USERS(id) <= 0) {
+		BKE_libblock_free(bmain, id);
+		RNA_POINTER_INVALIDATE(id_ptr);
+	}
+	else {
+		BKE_reportf(reports, RPT_ERROR,
+		            "%s '%s' must have zero users to be removed, found %d (try with unlink=True parameter)",
+		            BKE_idcode_to_name(GS(id->name)), id->name + 2, ID_REAL_USERS(id));
+	}
+}
+
+
 static Camera *rna_Main_cameras_new(Main *bmain, const char *name)
 {
 	ID *id = BKE_camera_add(bmain, name);
 	id_us_min(id);
 	return (Camera *)id;
 }
-static void rna_Main_cameras_remove(Main *bmain, ReportList *reports, PointerRNA *camera_ptr)
-{
-	Camera *camera = camera_ptr->data;
-	if (ID_REAL_USERS(camera) <= 0) {
-		BKE_libblock_free(bmain, camera);
-		RNA_POINTER_INVALIDATE(camera_ptr);
-	}
-	else {
-		BKE_reportf(reports, RPT_ERROR, "Camera '%s' must have zero users to be removed, found %d",
-		            camera->id.name + 2, ID_REAL_USERS(camera));
-	}
-}
 
 static Scene *rna_Main_scenes_new(Main *bmain, const char *name)
 {
 	return BKE_scene_add(bmain, name);
 }
-static void rna_Main_scenes_remove(Main *bmain, bContext *C, ReportList *reports, PointerRNA *scene_ptr)
+static void rna_Main_scenes_remove(Main *bmain, bContext *C, ReportList *reports, PointerRNA *scene_ptr, int do_unlink)
 {
 	/* don't call BKE_libblock_free(...) directly */
 	Scene *scene = scene_ptr->data;
@@ -146,24 +155,23 @@ static void rna_Main_scenes_remove(Main *bmain, bContext *C, ReportList *reports
 	if ((scene_new = scene->id.prev) ||
 	    (scene_new = scene->id.next))
 	{
-		bScreen *sc = CTX_wm_screen(C);
-		if (sc->scene == scene) {
+		if (do_unlink) {
+			bScreen *sc = CTX_wm_screen(C);
+			if (sc->scene == scene) {
 
 #ifdef WITH_PYTHON
-			BPy_BEGIN_ALLOW_THREADS;
+				BPy_BEGIN_ALLOW_THREADS;
 #endif
 
-			ED_screen_set_scene(C, sc, scene_new);
+				ED_screen_set_scene(C, sc, scene_new);
 
 #ifdef WITH_PYTHON
-			BPy_END_ALLOW_THREADS;
+				BPy_END_ALLOW_THREADS;
 #endif
 
+			}
 		}
-
-		BKE_libblock_remap(bmain, scene, scene_new, ID_REMAP_SKIP_INDIRECT_USAGE | ID_REMAP_SKIP_NEVER_NULL_USAGE);
-		BKE_libblock_free(bmain, scene);
-		RNA_POINTER_INVALIDATE(scene_ptr);
+		rna_Main_ID_remove(bmain, reports, scene_ptr, do_unlink);
 	}
 	else {
 		BKE_reportf(reports, RPT_ERROR, "Scene '%s' is the last, cannot be removed", scene->id.name + 2);
@@ -224,38 +232,12 @@ static Object *rna_Main_objects_new(Main *bmain, ReportList *reports, const char
 	return ob;
 }
 
-static void rna_Main_objects_remove(Main *bmain, ReportList *reports, PointerRNA *object_ptr)
-{
-	Object *object = object_ptr->data;
-	if (ID_REAL_USERS(object) <= 0) {
-		BKE_libblock_unlink(bmain, object, false);
-		BKE_libblock_free(bmain, object);
-		RNA_POINTER_INVALIDATE(object_ptr);
-	}
-	else {
-		BKE_reportf(reports, RPT_ERROR, "Object '%s' must have zero users to be removed, found %d",
-		            object->id.name + 2, ID_REAL_USERS(object));
-	}
-}
-
 static Material *rna_Main_materials_new(Main *bmain, const char *name)
 {
 	ID *id = (ID *)BKE_material_add(bmain, name);
 	id_us_min(id);
 	return (Material *)id;
 }
-static void rna_Main_materials_remove(Main *bmain, ReportList *reports, PointerRNA *material_ptr)
-{
-	Material *material = material_ptr->data;
-	if (ID_REAL_USERS(material) <= 0) {
-		BKE_libblock_free(bmain, material);
-		RNA_POINTER_INVALIDATE(material_ptr);
-	}
-	else {
-		BKE_reportf(reports, RPT_ERROR, "Material '%s' must have zero users to be removed, found %d",
-		            material->id.name + 2, ID_REAL_USERS(material));
-	}
-}
 
 static EnumPropertyItem *rna_Main_nodetree_type_itemf(bContext *UNUSED(C), PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
 {
@@ -273,18 +255,6 @@ static struct bNodeTree *rna_Main_nodetree_new(Main *bmain, const char *name, in
 	else
 		return NULL;
 }
-static void rna_Main_nodetree_remove(Main *bmain, ReportList *reports, PointerRNA *ntree_ptr)
-{
-	bNodeTree *ntree = ntree_ptr->data;
-	if (ID_REAL_USERS(ntree) <= 0) {
-		BKE_libblock_free(bmain, ntree);
-		RNA_POINTER_INVALIDATE(ntree_ptr);
-	}
-	else {
-		BKE_reportf(reports, RPT_ERROR, "Node tree '%s' must have zero users to be removed, found %d",
-		            ntree->id.name + 2, ID_REAL_USERS(ntree));
-	}
-}
 
 static Mesh *rna_Main_meshes_new(Main *bmain, const char *name)
 {
@@ -314,19 +284,6 @@ Mesh *rna_Main_meshes_new_from_object(
 	return BKE_mesh_new_from_object(bmain, sce, ob, apply_modifiers, settings, calc_tessface, calc_undeformed);
 }
 
-static void rna_Main_meshes_remove(Main *bmain, ReportList *reports, PointerRNA *mesh_ptr)
-{
-	Mesh *mesh = mesh_ptr->data;
-	if (ID_REAL_USERS(mesh) <= 0) {
-		BKE_libblock_free(bmain, mesh);
-		RNA_POINTER_INVALIDATE(mesh_ptr);
-	}
-	else {
-		BKE_reportf(reports, RPT_ERROR, "Mesh '%s' must have zero users to be removed, found %d",
-		            mesh->id.name + 2, ID_REAL_USERS(mesh));
-	}
-}
-
 static Lamp *rna_Main_lamps_new(Main *bmain, const char *name, int type)
 {
 	Lamp *lamp = BKE_lamp_add(bmain, name);
@@ -334,18 +291,6 @@ static Lamp *rna_Main_lamps_new(Main *bmain, const char *name, int type)
 	id_us_min(&lamp->id);
 	return lamp;
 }
-static void rna_Main_lamps_remove(Main *bmain, ReportList *reports, PointerRNA *lamp_ptr)
-{
-	Lamp *lamp = lamp_ptr->data;
-	if (ID_REAL_USERS(lamp) <= 0) {
-		BKE_libblock_free(bmain, lamp);
-		RNA_POINTER_INVALIDATE(lamp_ptr);
-	}
-	else {
-		BKE_reportf(reports, RPT_ERROR, "Lamp '%s' must have zero users to be removed, found %d",
-		            lamp->id.name + 2, ID_REAL_USERS(lamp));
-	}
-}
 
 static Image *rna_Main_images_new(Main *bmain, const char *name, int width, int height, int alpha, int float_buffer, int stereo3d)
 {
@@ -374,18 +319,6 @@ static Image *rna_Main_images_load(Main *bmain, ReportList *reports, const char
 	id_us_min((ID *)ima);
 	return ima;
 }
-static void rna_Main_images_remove(Main *bmain, ReportList *reports, PointerRNA *image_ptr)
-{
-	Image *image = image_ptr->data;
-	if (ID_REAL_USERS(image) <= 0) {
-		BKE_libblock_free(bmain, image);
-		RNA_POINTER_INVALIDATE(image_ptr);
-	}
-	else {
-		BKE_reportf(reports, RPT_ERROR, "Image '%s' must have zero users to be removed, found %d",
-		            image->id.name + 2, ID_REAL_USERS(image));
-	}
-}
 
 static Lattice *rna_Main_lattices_new(Main *bmain, const char *name)
 {
@@ -393,18 +326,6 @@ static Lattice *rna_Main_lattices_new(Main *bmain, const char *name)
 	id_us_min(&lt->id);
 	return lt;
 }
-static void rna_Main_lattices_remove(Main *bmain, ReportList *reports, PointerRNA *lt_ptr)
-{
-	Lattice *lt = lt_ptr->data;
-	if (ID_REAL_USERS(lt) <= 0) {
-		BKE_libblock_free(bmain, lt);
-		RNA_POINTER_INVALIDATE(lt_ptr);
-	}
-	else {
-		BKE_reportf(reports, RPT_ERROR, "Lattice '%s' must have zero users to be removed, found %d",
-		            lt->id.name + 2, ID_REAL_USERS(lt));
-	}
-}
 
 static Curve *rna_Main_curves_new(Main *bmain, const char *name, int type)
 {
@@ -412,18 +333,6 @@ static Curve *rna_Main_curves_new(Main *bmain, const char *name, int type)
 	id_us_min(&cu->id);
 	return cu;
 }
-static void rna_Main_curves_remove(Main *bmain, ReportList *reports, PointerRNA *cu_ptr)
-{
-	Curve *cu = cu_ptr->data;
-	if (ID_REAL_USERS(cu) <= 0) {
-		BKE_libblock_free(bmain, cu);
-		RNA_POINTER_INVALIDATE(cu_ptr);
-	}
-	else {
-		BKE_reportf(reports, RPT_ERROR, "Curve '%s' must have zero users to be removed, found %d",
-		            cu->id.name + 2, ID_REAL_USERS(cu));
-	}
-}
 
 static MetaBall *rna_Main_metaballs_new(Main *bmain, const char *name)
 {
@@ -431,18 +340,6 @@ static MetaBall *rna_Main_metaballs_new(Main *bmain, const char *name)
 	id_us_min(&mb->id);
 	return mb;
 }
-static void rna_Main_metaballs_remove(Main *bmain, ReportList *reports, PointerRNA *mb_ptr)
-{
-	MetaBall *mb = mb_ptr->data;
-	if (ID_REAL_USERS(mb) <= 0) {
-		BKE_libblock_free(bmain, mb);
-		RNA_POINTER_INVALIDATE(mb_ptr);
-	}
-	else {
-		BKE_reportf(reports, RPT_ERROR, "Metaball '%s' must have zero users to be removed, found %d",
-		            mb->id.name + 2, ID_REAL_USERS(mb));
-	}
-}
 
 static VFont *rna_Main_fonts_load(Main *bmain, ReportList *reports, const char *filepath, int check_existing)
 {
@@ -464,18 +361,6 @@ static VFont *rna_Main_fonts_load(Main *bmain, ReportList *reports, const char *
 	return font;
 
 }
-static void rna_Main_fonts_remove(Main *bmain, ReportList *reports, PointerRNA *vfont_ptr)
-{
-	VFont *vfont = vfont_ptr->data;
-	if (ID_REAL_USERS(vfont) <= 0) {
-		BKE_libblock_free(bmain, vfont);
-		RNA_POINTER_INVALIDATE(vfont_ptr);
-	}
-	else {
-		BKE_reportf(reports, RPT_ERROR, "Font '%s' must have zero users to be removed, found %d",
-		            vfont->id.name + 2, ID_REAL_USERS(vfont));
-	}
-}
 
 static Tex *rna_Main_textures_new(Main *bmain, const char *name, int type)
 {
@@ -484,18 +369,6 @@ static Tex *rna_Main_textures_new(Main *bmain, const cha

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list