[Bf-blender-cvs] [4e4e872478c] blender2.8: PyAPI: Add gpu.matrix API

Campbell Barton noreply at git.blender.org
Sat Aug 19 16:00:39 CEST 2017


Commit: 4e4e872478c8b9e13c34e277ddb2ebf8517410cb
Author: Campbell Barton
Date:   Sun Aug 20 00:01:19 2017 +1000
Branches: blender2.8
https://developer.blender.org/rB4e4e872478c8b9e13c34e277ddb2ebf8517410cb

PyAPI: Add gpu.matrix API

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

M	source/blender/python/intern/CMakeLists.txt
M	source/blender/python/intern/gpu.c
M	source/blender/python/intern/gpu.h
A	source/blender/python/intern/gpu_py_matrix.c

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

diff --git a/source/blender/python/intern/CMakeLists.txt b/source/blender/python/intern/CMakeLists.txt
index 967e90d22cb..3ea3f760951 100644
--- a/source/blender/python/intern/CMakeLists.txt
+++ b/source/blender/python/intern/CMakeLists.txt
@@ -47,6 +47,7 @@ set(INC_SYS
 set(SRC
 	gpu.c
 	gpu_offscreen.c
+	gpu_py_matrix.c
 	bpy.c
 	bpy_app.c
 	bpy_app_alembic.c
diff --git a/source/blender/python/intern/gpu.c b/source/blender/python/intern/gpu.c
index 48230a723d2..be2c6b3caab 100644
--- a/source/blender/python/intern/gpu.c
+++ b/source/blender/python/intern/gpu.c
@@ -334,6 +334,10 @@ PyObject *GPU_initPython(void)
 	PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
 	Py_INCREF(submodule);
 
+	PyModule_AddObject(module, "matrix", (submodule = BPyInit_gpu_matrix()));
+	PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
+	Py_INCREF(submodule);
+
 	PyDict_SetItem(PyImport_GetModuleDict(), PyModule_GetNameObject(module), module);
 	return module;
 }
diff --git a/source/blender/python/intern/gpu.h b/source/blender/python/intern/gpu.h
index 0da44a4eb87..c6fc5e41c47 100644
--- a/source/blender/python/intern/gpu.h
+++ b/source/blender/python/intern/gpu.h
@@ -37,5 +37,6 @@
 PyObject *GPU_initPython(void);
 
 PyObject *BPyInit_gpu_offscreen(void);
+PyObject *BPyInit_gpu_matrix(void);
 
 #endif /* __GPU_H__ */
diff --git a/source/blender/python/intern/gpu_py_matrix.c b/source/blender/python/intern/gpu_py_matrix.c
new file mode 100644
index 00000000000..bef2bfb51b5
--- /dev/null
+++ b/source/blender/python/intern/gpu_py_matrix.c
@@ -0,0 +1,360 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Copyright 2015, Blender Foundation.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/python/intern/gpu_py_matrix.c
+ *  \ingroup pythonintern
+ *
+ * This file defines the gpu.matrix stack API.
+ */
+
+#include <Python.h>
+
+
+#include "BLI_utildefines.h"
+
+#include "../mathutils/mathutils.h"
+
+#include "../generic/py_capi_utils.h"
+
+#include "gpu.h"
+
+#include "GPU_matrix.h"
+
+/* -------------------------------------------------------------------- */
+
+/** \name Manage Stack
+ * \{ */
+
+PyDoc_STRVAR(pygpu_matrix_push_doc,
+"push()\n"
+"\n"
+"   Add to the matrix stack.\n"
+);
+static PyObject *pygpu_matrix_push(PyObject *UNUSED(self))
+{
+	gpuPushMatrix();
+	Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(pygpu_matrix_pop_doc,
+"pop()\n"
+"\n"
+"   Remove the last matrix from the stack.\n"
+);
+static PyObject *pygpu_matrix_pop(PyObject *UNUSED(self))
+{
+	gpuPopMatrix();
+	Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(pygpu_matrix_push_projection_doc,
+"push_projection()\n"
+"\n"
+"   Add to the projection matrix stack.\n"
+);
+static PyObject *pygpu_matrix_push_projection(PyObject *UNUSED(self))
+{
+	gpuPushMatrix();
+	Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(pygpu_matrix_pop_projection_doc,
+"pop_projection()\n"
+"\n"
+"   Remove the last projection matrix from the stack.\n"
+);
+static PyObject *pygpu_matrix_pop_projection(PyObject *UNUSED(self))
+{
+	gpuPopMatrix();
+	Py_RETURN_NONE;
+}
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+
+/** \name Manipulate State
+ * \{ */
+
+PyDoc_STRVAR(pygpu_matrix_multiply_matrix_doc,
+"multiply_matrix(matrix)\n"
+"\n"
+"   Multiply the current stack matrix.\n"
+"\n"
+"   :param matrix: A 4x4 matrix.\n"
+"   :type matrix: :class:`mathutils.Matrix`\n"
+);
+static PyObject *pygpu_matrix_multiply_matrix(PyObject *UNUSED(self), PyObject *value)
+{
+	MatrixObject *pymat;
+	if (!Matrix_Parse4x4(value, &pymat)) {
+		return NULL;
+	}
+	gpuMultMatrix(pymat->matrix);
+	Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(pygpu_matrix_scale_doc,
+"scale(scale)\n"
+"\n"
+"   Scale the current stack matrix.\n"
+"\n"
+"   :param scale: Scale the current stack matrix.\n"
+"   :type scale: sequence of 2 or 3 floats\n"
+);
+static PyObject *pygpu_matrix_scale(PyObject *UNUSED(self), PyObject *value)
+{
+	float scale[3];
+	int len;
+	if ((len = mathutils_array_parse(scale, 2, 3, value, "gpu.matrix.scale(): invalid vector arg")) == -1) {
+		return NULL;
+	}
+	if (len == 2) {
+		gpuScale2fv(scale);
+	}
+	else {
+		gpuScale3fv(scale);
+	}
+	Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(pygpu_matrix_scale_uniform_doc,
+"scale_uniform(scale)\n"
+"\n"
+"   :param scale: Scale the current stack matrix.\n"
+"   :type scale: sequence of 2 or 3 floats\n"
+);
+static PyObject *pygpu_matrix_scale_uniform(PyObject *UNUSED(self), PyObject *value)
+{
+	float scalar;
+	if ((scalar = PyFloat_AsDouble(value)) == -1.0f && PyErr_Occurred()) {
+		PyErr_Format(PyExc_TypeError,
+		             "expected a number, not %.200s",
+		             Py_TYPE(value)->tp_name);
+		return NULL;
+	}
+	gpuScaleUniform(scalar);
+	Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(pygpu_matrix_translate_doc,
+"translate(offset)\n"
+"\n"
+"   Scale the current stack matrix.\n"
+"\n"
+"   :param offset: Translate the current stack matrix.\n"
+"   :type offset: sequence of 2 or 3 floats\n"
+);
+static PyObject *pygpu_matrix_translate(PyObject *UNUSED(self), PyObject *value)
+{
+	float offset[3];
+	int len;
+	if ((len = mathutils_array_parse(offset, 2, 3, value, "gpu.matrix.translate(): invalid vector arg")) == -1) {
+		return NULL;
+	}
+	if (len == 2) {
+		gpuTranslate2fv(offset);
+	}
+	else {
+		gpuTranslate3fv(offset);
+	}
+	Py_RETURN_NONE;
+}
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+
+/** \name Write State
+ * \{ */
+
+PyDoc_STRVAR(pygpu_matrix_reset_doc,
+"reset()\n"
+"\n"
+"   Empty stack and set to identity.\n"
+);
+static PyObject *pygpu_matrix_reset(PyObject *UNUSED(self))
+{
+	gpuMatrixReset();
+	Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(pygpu_matrix_load_identity_doc,
+"load_identity()\n"
+"\n"
+"   Empty stack and set to identity.\n"
+);
+static PyObject *pygpu_matrix_load_identity(PyObject *UNUSED(self))
+{
+	gpuLoadIdentity();
+	Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(pygpu_matrix_load_matrix_doc,
+"load_matrix(matrix)\n"
+"\n"
+"   Load a matrix into the stack.\n"
+"\n"
+"   :param matrix: A 4x4 matrix.\n"
+"   :type matrix: :class:`mathutils.Matrix`\n"
+);
+static PyObject *pygpu_matrix_load_matrix(PyObject *UNUSED(self), PyObject *value)
+{
+	MatrixObject *pymat;
+	if (!Matrix_Parse4x4(value, &pymat)) {
+		return NULL;
+	}
+	gpuLoadMatrix(pymat->matrix);
+	Py_RETURN_NONE;
+}
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+
+/** \name Read State
+ * \{ */
+
+PyDoc_STRVAR(pygpu_matrix_get_projection_matrix_doc,
+"get_projection_matrix()\n"
+"\n"
+"   Return a copy of the projection matrix.\n"
+"\n"
+"   :return: A 4x4 projection matrix.\n"
+"   :rtype: :class:`mathutils.Matrix`\n"
+);
+static PyObject *pygpu_matrix_get_projection_matrix(PyObject *UNUSED(self))
+{
+	float matrix[4][4];
+	gpuGetModelViewMatrix(matrix);
+	return Matrix_CreatePyObject(&matrix[0][0], 4, 4, NULL);
+}
+
+
+PyDoc_STRVAR(pygpu_matrix_get_modal_view_matrix_doc,
+"get_view_matrix()\n"
+"\n"
+"   Return a copy of the view matrix.\n"
+"\n"
+"   :return: A 4x4 view matrix.\n"
+"   :rtype: :class:`mathutils.Matrix`\n"
+);
+static PyObject *pygpu_matrix_get_modal_view_matrix(PyObject *UNUSED(self))
+{
+	float matrix[4][4];
+	gpuGetProjectionMatrix(matrix);
+	return Matrix_CreatePyObject(&matrix[0][0], 4, 4, NULL);
+}
+
+PyDoc_STRVAR(pygpu_matrix_get_normal_matrix_doc,
+"get_normal_matrix()\n"
+"\n"
+"   Return a copy of the normal matrix.\n"
+"\n"
+"   :return: A 3x3 normal matrix.\n"
+"   :rtype: :class:`mathutils.Matrix`\n"
+);
+static PyObject *pygpu_matrix_get_normal_matrix(PyObject *UNUSED(self))
+{
+	float matrix[3][3];
+	gpuGetNormalMatrix(matrix);
+	return Matrix_CreatePyObject(&matrix[0][0], 3, 3, NULL);
+}
+
+/** \} */
+
+
+/* -------------------------------------------------------------------- */
+
+/** \name Module
+ * \{ */
+
+
+static struct PyMethodDef BPy_GPU_matrix_methods[] = {
+	/* Manage Stack */
+	{"push", (PyCFunction)pygpu_matrix_push, METH_NOARGS, pygpu_matrix_push_doc},
+	{"pop", (PyCFunction)pygpu_matrix_pop, METH_NOARGS, pygpu_matrix_pop_doc},
+
+	{"push_projection", (PyCFunction)pygpu_matrix_push_projection,
+	 METH_NOARGS, pygpu_matrix_push_projection_doc},
+	{"pop_projection", (PyCFunction)pygpu_matrix_pop_projection,
+	 METH_NOARGS, pygpu_matrix_pop_projection_doc},
+
+	/* Manipulate State */
+	{"multiply_matrix", (PyCFunction)pygpu_matrix_multiply_matrix,
+	 METH_O, pygpu_matrix_multiply_matrix_doc},
+	{"scale", (PyCFunction)pygpu_matrix_scale,
+	 METH_O, pygpu_matrix_scale_doc},
+	{"scale_uniform", (PyCFunction)pygpu_matrix_scale_uniform,
+	 METH_O, pygpu_matrix_scale_uniform_doc},
+	{"translate", (PyCFunction)pygpu_matrix_translate,
+	 METH_O, pygpu_matrix_translate_doc},
+
+	/* TODO */
+#if 0
+	{"rotate", (PyCFunction)pygpu_matrix_rotate,
+	 METH_O, pygpu_matrix_rotate_doc},
+	{"rotate_axis", (PyCFunction)pygpu_matrix_rotate_axis,
+	 METH_O, pygpu_matrix_rotate_axis_doc},
+	{"look_at", (PyCFunction)pygpu_matrix_look_at,
+	 METH_O, pygpu_matrix_look_at_doc},
+#endif
+
+	/* Write State */
+	{"reset", (PyCFunction)pygpu_matrix_reset,
+	 METH_NOARGS, pygpu_matrix_reset_doc},
+	{"load_identity", (PyCFunction)pygpu_matrix_load_identity,
+	 METH_NOARGS, pygpu_matrix_load_identity_doc},
+	{"load_matrix", (PyCFunction)pygpu_matrix_load_matrix,
+	 METH_O, pygpu_matrix_load_matrix_doc},
+
+	/* Read State */
+	{"get_projection_matrix", (PyCFunction)pygpu_matrix_get_projection_matrix,
+	 METH_NOARGS, pygpu_matrix_get_projection_matri

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list