PyAPI: buffer protocol support for mathutils types
Adding buffer protocol support increases the speed of copying a Vector (3D) array into a `numpy.array` by up to x3.8. Ref !144401
This commit is contained in:
@@ -863,6 +863,59 @@ static PyObject *Quaternion_str(QuaternionObject *self)
|
||||
|
||||
/** \} */
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name Quaternion Type: Buffer Protocol
|
||||
* \{ */
|
||||
|
||||
static int Quaternion_getbuffer(PyObject *obj, Py_buffer *view, int flags)
|
||||
{
|
||||
QuaternionObject *self = (QuaternionObject *)obj;
|
||||
if (UNLIKELY(BaseMath_Prepare_ForBufferAccess(self, view, flags) == -1)) {
|
||||
return -1;
|
||||
}
|
||||
if (UNLIKELY(BaseMath_ReadCallback(self) == -1)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(view, 0, sizeof(*view));
|
||||
|
||||
view->obj = (PyObject *)self;
|
||||
view->buf = (void *)self->quat;
|
||||
view->len = Py_ssize_t(QUAT_SIZE * sizeof(float));
|
||||
view->itemsize = sizeof(float);
|
||||
view->ndim = 1;
|
||||
if ((flags & PyBUF_WRITABLE) == 0) {
|
||||
view->readonly = 1;
|
||||
}
|
||||
if (flags & PyBUF_FORMAT) {
|
||||
view->format = (char *)"f";
|
||||
}
|
||||
|
||||
self->flag |= BASE_MATH_FLAG_HAS_BUFFER_VIEW;
|
||||
|
||||
Py_INCREF(self);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void Quaternion_releasebuffer(PyObject * /*exporter*/, Py_buffer *view)
|
||||
{
|
||||
QuaternionObject *self = (QuaternionObject *)view->obj;
|
||||
self->flag &= ~BASE_MATH_FLAG_HAS_BUFFER_VIEW;
|
||||
|
||||
if (view->readonly == 0) {
|
||||
if (UNLIKELY(BaseMath_WriteCallback(self) == -1)) {
|
||||
PyErr_Print();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static PyBufferProcs Quaternion_as_buffer = {
|
||||
(getbufferproc)Quaternion_getbuffer,
|
||||
(releasebufferproc)Quaternion_releasebuffer,
|
||||
};
|
||||
|
||||
/** \} */
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name Quaternion Type: Rich Compare
|
||||
* \{ */
|
||||
@@ -1801,7 +1854,7 @@ PyTypeObject quaternion_Type = {
|
||||
/*tp_str*/ (reprfunc)Quaternion_str,
|
||||
/*tp_getattro*/ nullptr,
|
||||
/*tp_setattro*/ nullptr,
|
||||
/*tp_as_buffer*/ nullptr,
|
||||
/*tp_as_buffer*/ &Quaternion_as_buffer,
|
||||
/*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
||||
/*tp_doc*/ quaternion_doc,
|
||||
/*tp_traverse*/ (traverseproc)BaseMathObject_traverse,
|
||||
|
||||
Reference in New Issue
Block a user