[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [48431] trunk/blender/source/blender: all bmesh operators can now be accessed from bmesh.ops. * using a generic wrapper,

Campbell Barton ideasman42 at gmail.com
Sat Jun 30 13:14:26 CEST 2012


Revision: 48431
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=48431
Author:   campbellbarton
Date:     2012-06-30 11:14:10 +0000 (Sat, 30 Jun 2012)
Log Message:
-----------
all bmesh operators can now be accessed from bmesh.ops.* using a generic wrapper,
argument parsing still needs to have support added for vector, matrix and element types.

Modified Paths:
--------------
    trunk/blender/source/blender/bmesh/intern/bmesh_operator_api.h
    trunk/blender/source/blender/bmesh/intern/bmesh_operators.c
    trunk/blender/source/blender/python/bmesh/bmesh_py_api.c
    trunk/blender/source/blender/python/bmesh/bmesh_py_ops.c

Modified: trunk/blender/source/blender/bmesh/intern/bmesh_operator_api.h
===================================================================
--- trunk/blender/source/blender/bmesh/intern/bmesh_operator_api.h	2012-06-30 10:38:36 UTC (rev 48430)
+++ trunk/blender/source/blender/bmesh/intern/bmesh_operator_api.h	2012-06-30 11:14:10 UTC (rev 48431)
@@ -352,6 +352,9 @@
 void BMO_slot_map_to_flag(BMesh *bm, BMOperator *op, const char *slot_name,
                           const char hflag, const short oflag);
 
+void BMO_slot_buffer_from_all(BMesh *bm, BMOperator *op, const char *slot_name,
+                              const char htype);
+
 /* this part of the API is used to iterate over element buffer or
  * mapping slots.
  *

Modified: trunk/blender/source/blender/bmesh/intern/bmesh_operators.c
===================================================================
--- trunk/blender/source/blender/bmesh/intern/bmesh_operators.c	2012-06-30 10:38:36 UTC (rev 48430)
+++ trunk/blender/source/blender/bmesh/intern/bmesh_operators.c	2012-06-30 11:14:10 UTC (rev 48431)
@@ -225,9 +225,9 @@
  *
  * Returns a pointer to the slot of type 'slot_code'
  */
-BMOpSlot *BMO_slot_get(BMOperator *op, const char *slot_namee)
+BMOpSlot *BMO_slot_get(BMOperator *op, const char *slot_name)
 {
-	int slot_code = bmo_name_to_slotcode_check(opdefines[op->type], slot_namee);
+	int slot_code = bmo_name_to_slotcode_check(opdefines[op->type], slot_name);
 
 	if (slot_code < 0) {
 		return &BMOpEmptySlot;
@@ -645,7 +645,7 @@
  *
  * Copies all elements of a certain type into an operator slot.
  */
-static void BMO_slot_buffer_from_all(BMesh *bm, BMOperator *op, const char *slot_name, const char htype)
+void BMO_slot_buffer_from_all(BMesh *bm, BMOperator *op, const char *slot_name, const char htype)
 {
 	BMOpSlot *output = BMO_slot_get(op, slot_name);
 	int totelement = 0, i = 0;

Modified: trunk/blender/source/blender/python/bmesh/bmesh_py_api.c
===================================================================
--- trunk/blender/source/blender/python/bmesh/bmesh_py_api.c	2012-06-30 10:38:36 UTC (rev 48430)
+++ trunk/blender/source/blender/python/bmesh/bmesh_py_api.c	2012-06-30 11:14:10 UTC (rev 48431)
@@ -145,7 +145,8 @@
 	Py_INCREF(submodule);
 
 	PyModule_AddObject(mod, "ops", (submodule = BPyInit_bmesh_ops()));
-	PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule);
+	/* PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule); */
+	PyDict_SetItemString(sys_modules, "bmesh.ops", submodule); /* fake module */
 	Py_INCREF(submodule);
 
 	PyModule_AddObject(mod, "utils", (submodule = BPyInit_bmesh_utils()));

Modified: trunk/blender/source/blender/python/bmesh/bmesh_py_ops.c
===================================================================
--- trunk/blender/source/blender/python/bmesh/bmesh_py_ops.c	2012-06-30 10:38:36 UTC (rev 48430)
+++ trunk/blender/source/blender/python/bmesh/bmesh_py_ops.c	2012-06-30 11:14:10 UTC (rev 48431)
@@ -60,46 +60,190 @@
 	return 0;
 }
 
-PyDoc_STRVAR(bpy_bm_ops_convex_hull_doc,
-".. method:: convex_hull(bmesh, filter)\n"
-"\n"
-"   Face split with optional intermediate points.\n"
-"\n"
-"   :arg bmesh: The face to cut.\n"
-"   :type bmesh: :class:`bmesh.types.BMFace`\n"
-"   :arg filter: Set containing vertex flags to apply the operator.\n"
-"   :type filter: set\n"
-);
-static PyObject *bpy_bm_ops_convex_hull(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
+/* bmesh operator 'bmesh.ops.*' callable types
+ * ******************************************* */
+PyTypeObject bmesh_op_Type;
+
+typedef struct {
+	PyObject_HEAD /* required python macro   */
+	const char *opname;
+} BPy_BMeshOpFunc;
+
+PyObject *bpy_bmesh_op_CreatePyObject(const char *opname)
 {
-	static const char *kwlist[] = {"bmesh", "filter", NULL};
+	BPy_BMeshOpFunc *self = PyObject_New(BPy_BMeshOpFunc, &bmesh_op_Type);
 
+	self->opname = opname;
+
+	return (PyObject *)self;
+}
+
+static PyObject *bpy_bmesh_op_repr(BPy_BMeshOpFunc *self)
+{
+	return PyUnicode_FromFormat("<%.200s bmesh.ops.%.200s()>",
+	                            Py_TYPE(self)->tp_name,
+	                            self->opname);
+}
+
+
+static PyObject *pyrna_op_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *kw)
+{
 	BPy_BMesh *py_bm;
 	BMesh *bm;
 
-	PyObject *filter;
-	int filter_flags;
 	BMOperator bmop;
 
-	if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!:convex_hull", (char **)kwlist,
-	                                 &BPy_BMesh_Type, &py_bm,
-	                                 &PySet_Type,  &filter))
+	if ((PyTuple_GET_SIZE(args) == 1) &&
+	    (py_bm = (BPy_BMesh *)PyTuple_GET_ITEM(args, 0)) &&
+	    (BPy_BMesh_Check(py_bm))
+		)
 	{
+		BPY_BM_CHECK_OBJ(py_bm);
+		bm = py_bm->bm;
+	}
+	else {
+		PyErr_SetString(PyExc_TypeError,
+		                "calling a bmesh operator expects a single BMesh (non keyword) "
+		                "as the first argument");
 		return NULL;
 	}
 
-	BPY_BM_CHECK_OBJ(py_bm);
-	bm = py_bm->bm;
+	/* TODO - error check this!, though we do the error check on attribute access */
+	BMO_op_init(bm, &bmop, self->opname);
 
-	if (filter != NULL && PyC_FlagSet_ToBitfield(bpy_bm_hflag_all_flags, filter,
-	                                             &filter_flags, "convex_hull") == -1)
-	{
-		return NULL;
+	if (kw && PyDict_Size(kw) > 0) {
+		/* setup properties, see bpy_rna.c: pyrna_py_to_prop()
+		 * which shares this logic for parsing properties */
+
+		PyObject *key, *value;
+		Py_ssize_t pos = 0;
+		while (PyDict_Next(kw, &pos, &key, &value)) {
+			const char *slot_name = _PyUnicode_AsString(key);
+			BMOpSlot *slot = BMO_slot_get(&bmop, slot_name);
+
+			if (slot == NULL) {
+				PyErr_Format(PyExc_TypeError,
+				             "%.200s: keyword \"%.200s\" is invalid for this operator",
+				             self->opname, slot_name);
+				return NULL;
+			}
+
+			/* now assign the value */
+			switch (slot->slot_type) {
+				case BMO_OP_SLOT_BOOL:
+				{
+					int param;
+
+					param = PyLong_AsLong(value);
+
+					if (param < 0) {
+						PyErr_Format(PyExc_TypeError,
+						             "%.200s: keyword \"%.200s\" expected True/False or 0/1, not %.200s",
+						             self->opname, slot_name, Py_TYPE(value)->tp_name);
+						return NULL;
+					}
+					else {
+						slot->data.i = param;
+					}
+
+					break;
+				}
+				case BMO_OP_SLOT_INT:
+				{
+					int overflow;
+					long param = PyLong_AsLongAndOverflow(value, &overflow);
+					if (overflow || (param > INT_MAX) || (param < INT_MIN)) {
+						PyErr_Format(PyExc_ValueError,
+						             "%.200s: keyword \"%.200s\" value not in 'int' range "
+						             "(" STRINGIFY(INT_MIN) ", " STRINGIFY(INT_MAX) ")",
+						             self->opname, slot_name, Py_TYPE(value)->tp_name);
+						return NULL;
+					}
+					else if (param == -1 && PyErr_Occurred()) {
+						PyErr_Format(PyExc_TypeError,
+						             "%.200s: keyword \"%.200s\" expected an int, not %.200s",
+						             self->opname, slot_name, Py_TYPE(value)->tp_name);
+					}
+					else {
+						slot->data.i = (int)param;
+					}
+					break;
+				}
+				case BMO_OP_SLOT_FLT:
+				{
+					float param = PyFloat_AsDouble(value);
+					if (param == -1 && PyErr_Occurred()) {
+						PyErr_Format(PyExc_TypeError,
+						             "%.200s: keyword \"%.200s\" expected a float, not %.200s",
+						             self->opname, slot_name, Py_TYPE(value)->tp_name);
+					}
+					else {
+						slot->data.f = param;
+					}
+					break;
+				}
+				case BMO_OP_SLOT_ELEMENT_BUF:
+				{
+					/* there are many ways we could interpret arguments, for now...
+					 * - verts/edges/faces from the mesh direct,
+					 *   this way the operator takes every item.
+					 * - `TODO` a plain python sequence (list) of elements.
+					 * - `TODO`  an iterator. eg.
+					 *   face.verts
+					 * - `TODO`  (type, flag) pair, eg.
+					 *   ('VERT', {'TAG'})
+					 */
+
+#define BPY_BM_GENERIC_MESH_TEST(type_string)                                             \
+	if (((BPy_BMGeneric *)value)->bm != bm) {                                             \
+		PyErr_Format(PyExc_NotImplementedError,                                           \
+					 "%.200s: keyword \"%.200s\" " type_string " are from another bmesh", \
+					 self->opname, slot_name, slot->slot_type);                           \
+		return NULL;                                                                      \
 	}
 
-	BMO_op_initf(bm, &bmop,
-	             "convex_hull input=%hv",
-	             filter_flags);
+					if (BPy_BMVertSeq_Check(value)) {
+						BPY_BM_GENERIC_MESH_TEST("verts")
+						BMO_slot_buffer_from_all(bm, &bmop, slot_name, BM_VERT);
+					}
+					else if (BPy_BMEdgeSeq_Check(value)) {
+						BPY_BM_GENERIC_MESH_TEST("edges")
+						BMO_slot_buffer_from_all(bm, &bmop, slot_name, BM_EDGE);
+					}
+					else if (BPy_BMFaceSeq_Check(value)) {
+						BPY_BM_GENERIC_MESH_TEST("faces")
+						BMO_slot_buffer_from_all(bm, &bmop, slot_name, BM_FACE);
+					}
+					else if (BPy_BMElemSeq_Check(value)) {
+						BPY_BM_GENERIC_MESH_TEST("elements")
+
+						PyErr_Format(PyExc_NotImplementedError,
+						             "%.200s: keyword \"%.200s\" parsing element lists not working yet!",
+						             self->opname, slot_name, slot->slot_type);
+						return NULL;
+					}
+					else {
+						PyErr_Format(PyExc_TypeError,
+						             "%.200s: keyword \"%.200s\" expected "
+						             "a bmesh sequence, list, (htype, flag) pair, not %.200s",
+						             self->opname, slot_name, Py_TYPE(value)->tp_name);
+					}
+
+#undef BPY_BM_GENERIC_MESH_TEST
+
+					break;
+				}
+				default:
+					/* TODO --- many others */
+					PyErr_Format(PyExc_NotImplementedError,
+					             "%.200s: keyword \"%.200s\" type %d not working yet!",
+					             self->opname, slot_name, slot->slot_type);
+					return NULL;
+					break;
+			}
+		}
+	}
+
 	BMO_op_exec(bm, &bmop);
 	BMO_op_finish(bm, &bmop);
 
@@ -107,167 +251,225 @@
 		return NULL;
 	}
 
-	/* TODO, return values */
 	Py_RETURN_NONE;
 }
 
 
+PyTypeObject bmesh_op_Type = {
+	PyVarObject_HEAD_INIT(NULL, 0)
+	"BMeshOpFunc",              /* tp_name */
+	sizeof(BPy_BMeshOpFunc),    /* tp_basicsize */
+	0,                          /* tp_itemsize */
+	/* methods */
+	NULL,                       /* tp_dealloc */
+	NULL,                       /* printfunc tp_print; */
+	NULL,                       /* getattrfunc tp_getattr; */

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list