diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2019-05-16 14:49:21 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2019-05-16 17:42:16 +0300 |
commit | 32d5d127cb49743578e8e1e9882d8a707a4e848e (patch) | |
tree | 72c18410ca6f3d503d8790bd936bc73273efb658 /source/blender/makesrna | |
parent | 22a91bb0bd6ac76142f832e0e07df0b5f9e8ead2 (diff) |
Tweak API to support adding evaluated meshes to main database
One of the usecases is to create mesh from an object is a manner similar to
how Apply Modifiers does it, and have it in the bmain so it can be referenced
by other objects.
This usecase is something what went unnoticed in the previous API changes, so
here is a followup.
Summary of changes:
* bpy.meshes.new_from_object() behaves almost the same as before this change.
The difference now is that it now ensures all referenced data-blocks are
original (for example, materials referenced by the mesh).
* object.to_mesh() now creates free-standing Mesh data-block which is outside
of any bmain. The object owns it, which guarantees the memory never leaks.
It is possible to force free memory by calling object.to_mesh_clear().
Reviewers: brecht
Reviewed By: brecht
Differential Revision: https://developer.blender.org/D4875
Diffstat (limited to 'source/blender/makesrna')
-rw-r--r-- | source/blender/makesrna/intern/rna_internal.h | 3 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_main_api.c | 4 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_object_api.c | 40 |
3 files changed, 31 insertions, 16 deletions
diff --git a/source/blender/makesrna/intern/rna_internal.h b/source/blender/makesrna/intern/rna_internal.h index d93f8c4414b..64024166c50 100644 --- a/source/blender/makesrna/intern/rna_internal.h +++ b/source/blender/makesrna/intern/rna_internal.h @@ -548,9 +548,6 @@ PointerRNA rna_pointer_inherit_refine(struct PointerRNA *ptr, struct StructRNA * int rna_parameter_size(struct PropertyRNA *parm); -struct Mesh *rna_Main_meshes_new_from_object(struct Main *bmain, - struct ReportList *reports, - struct Object *object); /* XXX, these should not need to be defined here~! */ struct MTex *rna_mtex_texture_slots_add(struct ID *self, diff --git a/source/blender/makesrna/intern/rna_main_api.c b/source/blender/makesrna/intern/rna_main_api.c index 8f48738a27e..f6fee2580b8 100644 --- a/source/blender/makesrna/intern/rna_main_api.c +++ b/source/blender/makesrna/intern/rna_main_api.c @@ -317,7 +317,7 @@ static Mesh *rna_Main_meshes_new(Main *bmain, const char *name) } /* copied from Mesh_getFromObject and adapted to RNA interface */ -Mesh *rna_Main_meshes_new_from_object(Main *bmain, ReportList *reports, Object *object) +static Mesh *rna_Main_meshes_new_from_object(Main *bmain, ReportList *reports, Object *object) { switch (object->type) { case OB_FONT: @@ -331,7 +331,7 @@ Mesh *rna_Main_meshes_new_from_object(Main *bmain, ReportList *reports, Object * return NULL; } - return BKE_mesh_new_from_object(bmain, object); + return BKE_mesh_new_from_object_to_bmain(bmain, object); } static Light *rna_Main_lights_new(Main *bmain, const char *name, int type) diff --git a/source/blender/makesrna/intern/rna_object_api.c b/source/blender/makesrna/intern/rna_object_api.c index d94abd4066a..da00e1f155d 100644 --- a/source/blender/makesrna/intern/rna_object_api.c +++ b/source/blender/makesrna/intern/rna_object_api.c @@ -375,11 +375,28 @@ static void rna_Object_camera_fit_coords( } /* copied from Mesh_getFromObject and adapted to RNA interface */ -static Mesh *rna_Object_to_mesh(Object *object, bContext *C, ReportList *reports) +static Mesh *rna_Object_to_mesh(Object *object, ReportList *reports) { - Main *bmain = CTX_data_main(C); + /* TODO(sergey): Make it more re-usable function, de-duplicate with + * rna_Main_meshes_new_from_object. */ + switch (object->type) { + case OB_FONT: + case OB_CURVE: + case OB_SURF: + case OB_MBALL: + case OB_MESH: + break; + default: + BKE_report(reports, RPT_ERROR, "Object does not have geometry data"); + return NULL; + } + + return BKE_object_to_mesh(object); +} - return rna_Main_meshes_new_from_object(bmain, reports, object); +static void rna_Object_to_mesh_clear(Object *object) +{ + BKE_object_to_mesh_clear(object); } static PointerRNA rna_Object_shape_key_add( @@ -875,16 +892,17 @@ void RNA_api_object(StructRNA *srna) /* mesh */ func = RNA_def_function(srna, "to_mesh", "rna_Object_to_mesh"); - RNA_def_function_ui_description(func, - "Create a Mesh data-block from the current state of the object"); - RNA_def_function_flag(func, FUNC_USE_REPORTS | FUNC_USE_CONTEXT); - parm = RNA_def_pointer(func, - "mesh", - "Mesh", - "", - "Mesh created from object, remove it if it is only used for export"); + RNA_def_function_ui_description( + func, + "Create a Mesh data-block from the current state of the object. The object owns the " + "data-block. To force free it use to_mesh_clear()"); + RNA_def_function_flag(func, FUNC_USE_REPORTS); + parm = RNA_def_pointer(func, "mesh", "Mesh", "", "Mesh created from object"); RNA_def_function_return(func, parm); + func = RNA_def_function(srna, "to_mesh_clear", "rna_Object_to_mesh_clear"); + RNA_def_function_ui_description(func, "Clears mesh data-block created by to_mesh()"); + /* Armature */ func = RNA_def_function(srna, "find_armature", "modifiers_isDeformedByArmature"); RNA_def_function_ui_description( |