From d2ecf664928fdaf144dab346271e4caa9ed2f241 Mon Sep 17 00:00:00 2001 From: Philipp Oeser Date: Wed, 26 Feb 2025 14:14:14 +0100 Subject: [PATCH] Fix #120697: better error for BVHTree.FromObject() When called on an object that you cannot get a mesh from (e.g. Empties), you would run into an unhelpful "SystemError: returned NULL without setting an exception" Now be more specific in the error message. Pull Request: https://projects.blender.org/blender/blender/pulls/135162 --- .../python/mathutils/mathutils_bvhtree.cc | 48 ++++++++++++++++--- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/source/blender/python/mathutils/mathutils_bvhtree.cc b/source/blender/python/mathutils/mathutils_bvhtree.cc index 2884e3737ff..54b9ab6374e 100644 --- a/source/blender/python/mathutils/mathutils_bvhtree.cc +++ b/source/blender/python/mathutils/mathutils_bvhtree.cc @@ -1037,6 +1037,7 @@ static const Mesh *bvh_get_mesh(const char *funcname, const CustomData_MeshMasks data_masks = CD_MASK_BAREMESH; const bool use_render = DEG_get_mode(depsgraph) == DAG_EVAL_RENDER; *r_free_mesh = false; + Mesh *mesh; /* Write the display mesh into the dummy mesh */ if (use_deform) { @@ -1049,15 +1050,33 @@ static const Mesh *bvh_get_mesh(const char *funcname, return nullptr; } + mesh = blender::bke::mesh_create_eval_final(depsgraph, scene, ob, &data_masks); + if (mesh == nullptr) { + PyErr_Format(PyExc_ValueError, + "%s(...): Cannot get a mesh from object '%s'", + ob->id.name + 2, + funcname); + return nullptr; + } + *r_free_mesh = true; - return blender::bke::mesh_create_eval_final(depsgraph, scene, ob, &data_masks); + return mesh; } if (ob_eval != nullptr) { if (use_cage) { - return blender::bke::mesh_get_eval_deform(depsgraph, scene, ob_eval, &data_masks); + mesh = blender::bke::mesh_get_eval_deform(depsgraph, scene, ob_eval, &data_masks); } - - return BKE_object_get_evaluated_mesh(ob_eval); + else { + mesh = BKE_object_get_evaluated_mesh(ob_eval); + } + if (mesh == nullptr) { + PyErr_Format(PyExc_ValueError, + "%s(...): Cannot get a mesh from object '%s'", + ob->id.name + 2, + funcname); + return nullptr; + } + return mesh; } PyErr_Format(PyExc_ValueError, @@ -1075,9 +1094,16 @@ static const Mesh *bvh_get_mesh(const char *funcname, funcname); return nullptr; } - + mesh = blender::bke::mesh_create_eval_no_deform_render(depsgraph, scene, ob, &data_masks); + if (mesh == nullptr) { + PyErr_Format(PyExc_ValueError, + "%s(...): Cannot get a mesh from object '%s'", + ob->id.name + 2, + funcname); + return nullptr; + } *r_free_mesh = true; - return blender::bke::mesh_create_eval_no_deform_render(depsgraph, scene, ob, &data_masks); + return mesh; } if (use_cage) { @@ -1088,8 +1114,16 @@ static const Mesh *bvh_get_mesh(const char *funcname, return nullptr; } + mesh = blender::bke::mesh_create_eval_no_deform(depsgraph, scene, ob, &data_masks); + if (mesh == nullptr) { + PyErr_Format(PyExc_ValueError, + "%s(...): Cannot get a mesh from object '%s'", + ob->id.name + 2, + funcname); + return nullptr; + } *r_free_mesh = true; - return blender::bke::mesh_create_eval_no_deform(depsgraph, scene, ob, &data_masks); + return mesh; } PyDoc_STRVAR(