From fee0e6e211cea6d5bfe6860b5e00a0a657f5f5e9 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 9 Mar 2015 00:47:53 +1100 Subject: BMesh Py API: calc_edge_angle functions Make consistent with calc_edge_angle, take an optional fallback arg for non-manifold edges otherwise raise an exception. --- source/blender/python/bmesh/bmesh_py_types.c | 70 ++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 8 deletions(-) (limited to 'source/blender/python/bmesh') diff --git a/source/blender/python/bmesh/bmesh_py_types.c b/source/blender/python/bmesh/bmesh_py_types.c index 08a8a308138..20ee872b3be 100644 --- a/source/blender/python/bmesh/bmesh_py_types.c +++ b/source/blender/python/bmesh/bmesh_py_types.c @@ -1509,27 +1509,81 @@ static PyObject *bpy_bmedge_calc_length(BPy_BMEdge *self) } PyDoc_STRVAR(bpy_bmedge_calc_face_angle_doc, -".. method:: calc_face_angle()\n" +".. method:: calc_face_angle(fallback=None)\n" "\n" +" :arg fallback: return this when the edge doesn't have 2 faces\n" +" (instead of raising a :exc:`ValueError`).\n" +" :type fallback: any\n" " :return: The angle between 2 connected faces in radians.\n" " :rtype: float\n" ); -static PyObject *bpy_bmedge_calc_face_angle(BPy_BMEdge *self) +static PyObject *bpy_bmedge_calc_face_angle(BPy_BMEdge *self, PyObject *args) { + const float angle_invalid = -1.0f; + float angle; + PyObject *fallback = NULL; + BPY_BM_CHECK_OBJ(self); - return PyFloat_FromDouble(BM_edge_calc_face_angle(self->e)); + + if (!PyArg_ParseTuple(args, "|O:calc_face_angle", &fallback)) + return NULL; + + angle = BM_edge_calc_face_angle_ex(self->e, angle_invalid); + + if (angle == angle_invalid) { + /* avoid exception */ + if (fallback) { + Py_INCREF(fallback); + return fallback; + } + else { + PyErr_SetString(PyExc_ValueError, + "BMEdge.calc_face_angle(): " + "edge doesn't use 2 faces"); + return NULL; + } + } + + return PyFloat_FromDouble(angle); } PyDoc_STRVAR(bpy_bmedge_calc_face_angle_signed_doc, -".. method:: calc_face_angle_signed()\n" +".. method:: calc_face_angle_signed(fallback=None)\n" "\n" +" :arg fallback: return this when the edge doesn't have 2 faces\n" +" (instead of raising a :exc:`ValueError`).\n" +" :type fallback: any\n" " :return: The angle between 2 connected faces in radians (negative for concave join).\n" " :rtype: float\n" ); -static PyObject *bpy_bmedge_calc_face_angle_signed(BPy_BMEdge *self) +static PyObject *bpy_bmedge_calc_face_angle_signed(BPy_BMEdge *self, PyObject *args) { + const float angle_invalid = -FLT_MAX; + float angle; + PyObject *fallback = NULL; + BPY_BM_CHECK_OBJ(self); - return PyFloat_FromDouble(BM_edge_calc_face_angle_signed(self->e)); + + if (!PyArg_ParseTuple(args, "|O:calc_face_angle_signed", &fallback)) + return NULL; + + angle = BM_edge_calc_face_angle_signed_ex(self->e, angle_invalid); + + if (angle == angle_invalid) { + /* avoid exception */ + if (fallback) { + Py_INCREF(fallback); + return fallback; + } + else { + PyErr_SetString(PyExc_ValueError, + "BMEdge.calc_face_angle_signed(): " + "edge doesn't use 2 faces"); + return NULL; + } + } + + return PyFloat_FromDouble(angle); } PyDoc_STRVAR(bpy_bmedge_calc_tangent_doc, @@ -2597,8 +2651,8 @@ static struct PyMethodDef bpy_bmedge_methods[] = { {"other_vert", (PyCFunction)bpy_bmedge_other_vert, METH_O, bpy_bmedge_other_vert_doc}, {"calc_length", (PyCFunction)bpy_bmedge_calc_length, METH_NOARGS, bpy_bmedge_calc_length_doc}, - {"calc_face_angle", (PyCFunction)bpy_bmedge_calc_face_angle, METH_NOARGS, bpy_bmedge_calc_face_angle_doc}, - {"calc_face_angle_signed", (PyCFunction)bpy_bmedge_calc_face_angle_signed, METH_NOARGS, bpy_bmedge_calc_face_angle_signed_doc}, + {"calc_face_angle", (PyCFunction)bpy_bmedge_calc_face_angle, METH_VARARGS, bpy_bmedge_calc_face_angle_doc}, + {"calc_face_angle_signed", (PyCFunction)bpy_bmedge_calc_face_angle_signed, METH_VARARGS, bpy_bmedge_calc_face_angle_signed_doc}, {"calc_tangent", (PyCFunction)bpy_bmedge_calc_tangent, METH_VARARGS, bpy_bmedge_calc_tangent_doc}, {"normal_update", (PyCFunction)bpy_bmedge_normal_update, METH_NOARGS, bpy_bmedge_normal_update_doc}, -- cgit v1.2.3