[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [44916] trunk/blender/source/blender/ python: bmesh py api:

Campbell Barton ideasman42 at gmail.com
Fri Mar 16 06:03:22 CET 2012


Revision: 44916
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=44916
Author:   campbellbarton
Date:     2012-03-16 05:03:13 +0000 (Fri, 16 Mar 2012)
Log Message:
-----------
bmesh py api:

Wrap customdata, so far you can access the data layers in a pythonic way but not manipulate the customdata yet.

provides dictionary like access to customdata layers, eg:
  texpoly = bm.faces.tex["UVMap"]
  print(bm.verts.shape.keys())  # un-intended pun, keys() works on all layers.
  print("MyInt" in bm.edges.int)  # __contains__
  layer = bm.faces.get("CheckForLayer")

Modified Paths:
--------------
    trunk/blender/source/blender/python/bmesh/bmesh_py_api.c
    trunk/blender/source/blender/python/bmesh/bmesh_py_types.c
    trunk/blender/source/blender/python/bmesh/bmesh_py_types_customdata.c
    trunk/blender/source/blender/python/bmesh/bmesh_py_types_customdata.h
    trunk/blender/source/blender/python/bmesh/bmesh_py_types_select.c
    trunk/blender/source/blender/python/bmesh/bmesh_py_types_select.h
    trunk/blender/source/blender/python/intern/bpy_rna.c

Modified: trunk/blender/source/blender/python/bmesh/bmesh_py_api.c
===================================================================
--- trunk/blender/source/blender/python/bmesh/bmesh_py_api.c	2012-03-16 04:14:57 UTC (rev 44915)
+++ trunk/blender/source/blender/python/bmesh/bmesh_py_api.c	2012-03-16 05:03:13 UTC (rev 44916)
@@ -35,6 +35,8 @@
 
 #include "bmesh_py_types.h"
 #include "bmesh_py_types_select.h"
+#include "bmesh_py_types_customdata.h"
+
 #include "bmesh_py_utils.h"
 
 #include "BLI_utildefines.h"
@@ -129,7 +131,8 @@
 	PyObject *sys_modules = PySys_GetObject("modules"); /* not pretty */
 
 	BPy_BM_init_types();
-	BPy_BM_init_select_types();
+	BPy_BM_init_types_select();
+	BPy_BM_init_types_customdata();
 
 	mod = PyModule_Create(&BPy_BM_module_def);
 

Modified: trunk/blender/source/blender/python/bmesh/bmesh_py_types.c
===================================================================
--- trunk/blender/source/blender/python/bmesh/bmesh_py_types.c	2012-03-16 04:14:57 UTC (rev 44915)
+++ trunk/blender/source/blender/python/bmesh/bmesh_py_types.c	2012-03-16 05:03:13 UTC (rev 44916)
@@ -46,6 +46,7 @@
 
 #include "bmesh_py_types.h" /* own include */
 #include "bmesh_py_types_select.h"
+#include "bmesh_py_types_customdata.h"
 
 /* Common Flags
  * ************ */
@@ -483,7 +484,39 @@
 	return BPy_BMLoop_CreatePyObject(self->bm, self->l->prev);
 }
 
+/* ElemSeq
+ * ^^^^^^^ */
 
+PyDoc_STRVAR(bpy_bmelemseq_layers_doc,
+"blah blah (read-only).\n\n:type: :class:`BMLayerAccess`"
+);
+static PyObject *bpy_bmelemseq_layers_get(BPy_BMElemSeq *self)
+{
+	char htype;
+	BPY_BM_CHECK_OBJ(self);
+
+	switch ((BMIterType)self->itype) {
+		case BM_VERTS_OF_MESH:
+			htype = BM_VERT;
+			break;
+		case BM_EDGES_OF_MESH:
+			htype = BM_EDGE;
+			break;
+		case BM_FACES_OF_MESH:
+			htype = BM_FACE;
+			break;
+
+			/* TODO - LOOPS */
+
+		default:
+			PyErr_SetString(PyExc_AttributeError, "layers attribute is only valid for vert/edge/face sequences");
+			return NULL;
+			break;
+	}
+
+	return BPy_BMLayerAccess_CreatePyObject(self->bm, htype);
+}
+
 static PyGetSetDef bpy_bmesh_getseters[] = {
     {(char *)"verts", (getter)bpy_bmelemseq_get, (setter)NULL, (char *)bpy_bmesh_verts_doc, (void *)BM_VERTS_OF_MESH},
     {(char *)"edges", (getter)bpy_bmelemseq_get, (setter)NULL, (char *)bpy_bmesh_edges_doc, (void *)BM_EDGES_OF_MESH},
@@ -592,7 +625,15 @@
     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
 };
 
+static PyGetSetDef bpy_bmelemseq_getseters[] = {
+    {(char *)"layers",    (getter)bpy_bmelemseq_layers_get, (setter)NULL, (char *)bpy_bmelemseq_layers_doc, NULL},
 
+    {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
+};
+
+
+
+
 /* Methods
  * ======= */
 
@@ -2549,7 +2590,7 @@
 	BPy_BMEdge_Type.tp_getset    = bpy_bmedge_getseters;
 	BPy_BMFace_Type.tp_getset    = bpy_bmface_getseters;
 	BPy_BMLoop_Type.tp_getset    = bpy_bmloop_getseters;
-	BPy_BMElemSeq_Type.tp_getset = NULL;
+	BPy_BMElemSeq_Type.tp_getset = bpy_bmelemseq_getseters;
 	BPy_BMIter_Type.tp_getset    = NULL;
 
 
@@ -2649,6 +2690,9 @@
 	mod_type_add(submodule, BPy_BMIter_Type);
 	mod_type_add(submodule, BPy_BMEditSelSeq_Type);
 	mod_type_add(submodule, BPy_BMEditSelIter_Type);
+	mod_type_add(submodule, BPy_BMLayerAccess_Type);
+	mod_type_add(submodule, BPy_BMLayerCollection_Type);
+	mod_type_add(submodule, BPy_BMLayerItem_Type);
 
 #undef mod_type_add
 

Modified: trunk/blender/source/blender/python/bmesh/bmesh_py_types_customdata.c
===================================================================
--- trunk/blender/source/blender/python/bmesh/bmesh_py_types_customdata.c	2012-03-16 04:14:57 UTC (rev 44915)
+++ trunk/blender/source/blender/python/bmesh/bmesh_py_types_customdata.c	2012-03-16 05:03:13 UTC (rev 44916)
@@ -35,5 +35,513 @@
 #include "bmesh.h"
 
 #include "bmesh_py_types.h"
+#include "bmesh_py_types_customdata.h"
 
-/* TODO */
+#include "BKE_customdata.h"
+
+static CustomData *bpy_bm_customdata_get(BMesh *bm, char htype)
+{
+	switch (htype) {
+		case BM_VERT:  return &bm->vdata;
+		case BM_EDGE:  return &bm->edata;
+		case BM_FACE:  return &bm->pdata;
+		case BM_LOOP:  return &bm->ldata;
+	}
+
+	BLI_assert(0);
+	return NULL;
+}
+
+static CustomDataLayer *bpy_bmlayeritem_get(BPy_BMLayerItem *self)
+{
+	CustomData *data = bpy_bm_customdata_get(self->bm, self->htype);
+	return &data->layers[CustomData_get_layer_index_n(data, self->type, self->index)];
+}
+
+/* py-type definitions
+ * ******************* */
+
+/* getseters
+ * ========= */
+
+static PyObject *bpy_bmlayeraccess_collection_get(BPy_BMLayerAccess *self, void *flag)
+{
+	const int type = (int)GET_INT_FROM_POINTER(flag);
+
+	BPY_BM_CHECK_OBJ(self);
+
+	return BPy_BMLayerCollection_CreatePyObject(self->bm, self->htype, type);
+}
+
+static PyObject *bpy_bmlayeritem_name_get(BPy_BMLayerItem *self, void *UNUSED(flag))
+{
+	CustomDataLayer *layer;
+
+	BPY_BM_CHECK_OBJ(self);
+
+	layer = bpy_bmlayeritem_get(self);
+	return PyUnicode_FromString(layer->name);
+}
+
+static PyGetSetDef bpy_bmlayeraccess_getseters[] = {
+    {(char *)"deform", (getter)bpy_bmlayeraccess_collection_get, (setter)NULL, (char *)NULL, (void *)CD_MDEFORMVERT},
+
+    {(char *)"float",  (getter)bpy_bmlayeraccess_collection_get, (setter)NULL, (char *)NULL, (void *)CD_PROP_FLT},
+    {(char *)"int",    (getter)bpy_bmlayeraccess_collection_get, (setter)NULL, (char *)NULL, (void *)CD_PROP_INT},
+    {(char *)"string", (getter)bpy_bmlayeraccess_collection_get, (setter)NULL, (char *)NULL, (void *)CD_PROP_STR},
+
+    {(char *)"tex",   (getter)bpy_bmlayeraccess_collection_get, (setter)NULL, (char *)NULL, (void *)CD_MTEXPOLY},
+    {(char *)"uv",    (getter)bpy_bmlayeraccess_collection_get, (setter)NULL, (char *)NULL, (void *)CD_MLOOPUV},
+    {(char *)"color", (getter)bpy_bmlayeraccess_collection_get, (setter)NULL, (char *)NULL, (void *)CD_MLOOPCOL},
+
+    {(char *)"shape",        (getter)bpy_bmlayeraccess_collection_get, (setter)NULL, (char *)NULL, (void *)CD_SHAPEKEY},
+    {(char *)"bevel_weight", (getter)bpy_bmlayeraccess_collection_get, (setter)NULL, (char *)NULL, (void *)CD_SHAPEKEY},
+    {(char *)"crease",       (getter)bpy_bmlayeraccess_collection_get, (setter)NULL, (char *)NULL, (void *)CD_CREASE},
+
+    {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
+};
+
+static PyGetSetDef bpy_bmlayeritem_getseters[] = {
+    /* BMESH_TODO, make writeable */
+    {(char *)"name", (getter)bpy_bmlayeritem_name_get, (setter)NULL, (char *)NULL, NULL},
+
+    {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
+};
+
+
+/* Methods
+ * ======= */
+
+/* BMLayerCollection
+ * ----------------- */
+
+PyDoc_STRVAR(bpy_bmlayercollection_keys_doc,
+".. method:: keys()\n"
+"\n"
+"   Return the identifiers of collection members\n"
+"   (matching pythons dict.keys() functionality).\n"
+"\n"
+"   :return: the identifiers for each member of this collection.\n"
+"   :rtype: list of strings\n"
+);
+static PyObject *bpy_bmlayercollection_keys(BPy_BMLayerCollection *self)
+{
+	PyObject *ret = PyList_New(0);
+	PyObject *item;
+	int index;
+	CustomData *data;
+
+	BPY_BM_CHECK_OBJ(self);
+
+	data = bpy_bm_customdata_get(self->bm, self->htype);
+	index = CustomData_get_layer_index(data, self->type);
+
+	ret = PyList_New(0);
+
+	if (index != -1) {
+		int tot = CustomData_number_of_layers(data, self->type);
+		for ( ; tot-- > 0; index++) {
+			item = PyUnicode_FromString(data->layers[index].name);
+			PyList_Append(ret, item);
+			Py_DECREF(item);
+		}
+	}
+
+	return ret;
+}
+
+PyDoc_STRVAR(bpy_bmlayercollection_values_doc,
+".. method:: items()\n"
+"\n"
+"   Return the identifiers of collection members\n"
+"   (matching pythons dict.items() functionality).\n"
+"\n"
+"   :return: (key, value) pairs for each member of this collection.\n"
+"   :rtype: list of tuples\n"
+);
+static PyObject *bpy_bmlayercollection_values(BPy_BMLayerCollection *self)
+{
+	PyObject *ret;
+	PyObject *item;
+	int index;
+	CustomData *data;
+
+	BPY_BM_CHECK_OBJ(self);
+
+	data = bpy_bm_customdata_get(self->bm, self->htype);
+	index = CustomData_get_layer_index(data, self->type);
+
+	ret = PyList_New(0);
+
+	if (index != -1) {
+		int tot = CustomData_number_of_layers(data, self->type);
+		for ( ; tot-- > 0; index++) {
+			item = PyTuple_New(2);
+			PyTuple_SET_ITEM(item, 0, PyUnicode_FromString(data->layers[index].name));
+			PyTuple_SET_ITEM(item, 1, BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index));
+			PyList_Append(ret, item);
+			Py_DECREF(item);
+		}
+	}
+
+	return ret;
+}
+
+PyDoc_STRVAR(bpy_bmlayercollection_items_doc,
+".. method:: values()\n"
+"\n"
+"   Return the values of collection\n"
+"   (matching pythons dict.values() functionality).\n"
+"\n"
+"   :return: the members of this collection.\n"
+"   :rtype: list\n"
+);
+static PyObject *bpy_bmlayercollection_items(BPy_BMLayerCollection *self)
+{
+	PyObject *ret;
+	PyObject *item;
+	int index;
+	CustomData *data;
+
+	BPY_BM_CHECK_OBJ(self);
+
+	data = bpy_bm_customdata_get(self->bm, self->htype);
+	index = CustomData_get_layer_index(data, self->type);
+
+	ret = PyList_New(0);
+
+	if (index != -1) {
+		int tot = CustomData_number_of_layers(data, self->type);
+		for ( ; tot-- > 0; index++) {
+			item = BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index);
+			PyList_Append(ret, item);
+			Py_DECREF(item);
+		}
+	}
+
+	return ret;
+}
+
+PyDoc_STRVAR(bpy_bmlayercollection_get_doc,
+".. method:: get(key, default=None)\n"
+"\n"
+"   Returns the value of the layer matching the key or default\n"
+"   when not found (matches pythons dictionary function of the same name).\n"
+"\n"
+"   :arg key: The key associated with the layer.\n"
+"   :type key: string\n"
+"   :arg default: Optional argument for the value to return if\n"
+"      *key* is not found.\n"
+"   :type default: Undefined\n"
+);
+static PyObject *bpy_bmlayercollection_get(BPy_BMLayerCollection *self, PyObject *args)
+{
+	const char *key;
+	PyObject* def = Py_None;
+
+	BPY_BM_CHECK_OBJ(self);
+
+	if (!PyArg_ParseTuple(args, "s|O:get", &key, &def)) {
+		return NULL;
+	}
+	else {
+		CustomData *data;
+		int index;
+
+		data = bpy_bm_customdata_get(self->bm, self->htype);
+		index = CustomData_get_named_layer_index(data, self->type, key);
+
+		if (index != -1) {
+			return BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index);
+		}
+	}
+
+	return Py_INCREF(def), def;
+}
+
+static struct PyMethodDef bpy_bmelemseq_methods[] = {
+    {"keys",     (PyCFunction)bpy_bmlayercollection_keys,     METH_NOARGS,  bpy_bmlayercollection_keys_doc},

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list