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

Campbell Barton ideasman42 at gmail.com
Mon Apr 30 20:54:14 CEST 2012


Revision: 46127
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=46127
Author:   campbellbarton
Date:     2012-04-30 18:54:14 +0000 (Mon, 30 Apr 2012)
Log Message:
-----------
bmesh - python api

- bm.*.layers.*.verify()
- bm.*.layers.*.is_singleton
- bm.*.layers.*.copy_from(other)


also added api functons
- BM_data_layer_copy(...)
- CustomData_layertype_is_singleton(type)

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_customdata.h
    trunk/blender/source/blender/blenkernel/intern/customdata.c
    trunk/blender/source/blender/bmesh/intern/bmesh_interp.c
    trunk/blender/source/blender/bmesh/intern/bmesh_interp.h
    trunk/blender/source/blender/editors/mesh/mesh_data.c
    trunk/blender/source/blender/python/bmesh/bmesh_py_types_customdata.c

Modified: trunk/blender/source/blender/blenkernel/BKE_customdata.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_customdata.h	2012-04-30 18:37:34 UTC (rev 46126)
+++ trunk/blender/source/blender/blenkernel/BKE_customdata.h	2012-04-30 18:54:14 UTC (rev 46127)
@@ -305,6 +305,7 @@
 
 /* get the name of a layer type */
 const char *CustomData_layertype_name(int type);
+int         CustomData_layertype_is_singleton(int type);
 
 /* make sure the name of layer at index is unique */
 void CustomData_set_layer_unique_name(struct CustomData *data, int index);

Modified: trunk/blender/source/blender/blenkernel/intern/customdata.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/customdata.c	2012-04-30 18:37:34 UTC (rev 46126)
+++ trunk/blender/source/blender/blenkernel/intern/customdata.c	2012-04-30 18:54:14 UTC (rev 46127)
@@ -71,8 +71,12 @@
 	int size;          /* the memory size of one element of this layer's data */
 	const char *structname;  /* name of the struct used, for file writing */
 	int structnum;     /* number of structs per element, for file writing */
-	const char *defaultname; /* default layer name */
 
+	/* default layer name.
+	 * note! when NULL this is a way to ensure there is only ever one item
+	 * see: CustomData_layertype_is_singleton() */
+	const char *defaultname;
+
 	/* a function to copy count elements of this layer's data
 	 * (deep copy if appropriate)
 	 * if NULL, memcpy is used
@@ -2601,6 +2605,16 @@
 	return layerType_getName(type);
 }
 
+
+/**
+ * Can only ever be one of these.
+ */
+int CustomData_layertype_is_singleton(int type)
+{
+	const LayerTypeInfo *typeInfo = layerType_getInfo(type);
+	return typeInfo->defaultname != NULL;
+}
+
 static int  CustomData_is_property_layer(int type)
 {
 	if ((type == CD_PROP_FLT) || (type == CD_PROP_INT) || (type == CD_PROP_STR))

Modified: trunk/blender/source/blender/bmesh/intern/bmesh_interp.c
===================================================================
--- trunk/blender/source/blender/bmesh/intern/bmesh_interp.c	2012-04-30 18:37:34 UTC (rev 46126)
+++ trunk/blender/source/blender/bmesh/intern/bmesh_interp.c	2012-04-30 18:54:14 UTC (rev 46127)
@@ -853,6 +853,52 @@
 	if (olddata.layers) MEM_freeN(olddata.layers);
 }
 
+void BM_data_layer_copy(BMesh *bm, CustomData *data, int type, int src_n, int dst_n)
+{
+	BMIter iter;
+
+	if (&bm->vdata == data) {
+		BMVert *eve;
+
+		BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
+			void *ptr = CustomData_bmesh_get_n(data, eve->head.data, type, dst_n);
+			CustomData_bmesh_set_n(data, eve->head.data, type, src_n, ptr);
+		}
+	}
+	else if (&bm->edata == data) {
+		BMEdge *eed;
+
+		BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
+			void *ptr = CustomData_bmesh_get_n(data, eed->head.data, type, dst_n);
+			CustomData_bmesh_set_n(data, eed->head.data, type, src_n, ptr);
+		}
+	}
+	else if (&bm->pdata == data) {
+		BMFace *efa;
+
+		BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
+			void *ptr = CustomData_bmesh_get_n(data, efa->head.data, type, dst_n);
+			CustomData_bmesh_set_n(data, efa->head.data, type, src_n, ptr);
+		}
+	}
+	else if (&bm->ldata == data) {
+		BMIter liter;
+		BMFace *efa;
+		BMLoop *l;
+
+		BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
+			BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
+				void *ptr = CustomData_bmesh_get_n(data, l->head.data, type, dst_n);
+				CustomData_bmesh_set_n(data, l->head.data, type, src_n, ptr);
+			}
+		}
+	}
+	else {
+		/* should never reach this! */
+		BLI_assert(0);
+	}
+}
+
 float BM_elem_float_data_get(CustomData *cd, void *element, int type)
 {
 	float *f = CustomData_bmesh_get(cd, ((BMHeader *)element)->data, type);

Modified: trunk/blender/source/blender/bmesh/intern/bmesh_interp.h
===================================================================
--- trunk/blender/source/blender/bmesh/intern/bmesh_interp.h	2012-04-30 18:37:34 UTC (rev 46126)
+++ trunk/blender/source/blender/bmesh/intern/bmesh_interp.h	2012-04-30 18:54:14 UTC (rev 46127)
@@ -36,6 +36,8 @@
 void  BM_data_layer_add_named(BMesh *bm, CustomData *data, int type, const char *name);
 void  BM_data_layer_free(BMesh *em, CustomData *data, int type);
 void  BM_data_layer_free_n(BMesh *bm, CustomData *data, int type, int n);
+void  BM_data_layer_copy(BMesh *bm, CustomData *data, int type, int src_n, int dst_n);
+
 float BM_elem_float_data_get(CustomData *cd, void *element, int type);
 void  BM_elem_float_data_set(CustomData *cd, void *element, int type, const float val);
 

Modified: trunk/blender/source/blender/editors/mesh/mesh_data.c
===================================================================
--- trunk/blender/source/blender/editors/mesh/mesh_data.c	2012-04-30 18:37:34 UTC (rev 46126)
+++ trunk/blender/source/blender/editors/mesh/mesh_data.c	2012-04-30 18:54:14 UTC (rev 46127)
@@ -189,42 +189,6 @@
 	}
 }
 
-/* copies from active to 'index' */
-static void editmesh_face_copy_customdata(BMEditMesh *em, int type, int index)
-{
-	BMesh *bm = em->bm;
-	CustomData *pdata = &bm->pdata;
-	BMIter iter;
-	BMFace *efa;
-	const int n = CustomData_get_active_layer(pdata, type);
-
-	/* ensure all current elements follow new customdata layout */
-	BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
-		void *data = CustomData_bmesh_get_n(pdata, efa->head.data, type, n);
-		CustomData_bmesh_set_n(pdata, efa->head.data, type, index, data);
-	}
-}
-
-/* copies from active to 'index' */
-static void editmesh_loop_copy_customdata(BMEditMesh *em, int type, int index)
-{
-	BMesh *bm = em->bm;
-	CustomData *ldata = &bm->ldata;
-	BMIter iter;
-	BMIter liter;
-	BMFace *efa;
-	BMLoop *loop;
-	const int n = CustomData_get_active_layer(ldata, type);
-
-	/* ensure all current elements follow new customdata layout */
-	BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
-		BM_ITER_ELEM (loop, &liter, efa, BM_LOOPS_OF_FACE) {
-			void *data = CustomData_bmesh_get_n(ldata, loop->head.data, type, n);
-			CustomData_bmesh_set_n(ldata, loop->head.data, type, index, data);
-		}
-	}
-}
-
 int ED_mesh_uv_loop_reset_ex(struct bContext *C, struct Mesh *me, const int layernum)
 {
 	BMEditMesh *em = me->edit_btmesh;
@@ -360,7 +324,8 @@
 		BM_data_layer_add_named(em->bm, &em->bm->pdata, CD_MTEXPOLY, name);
 		/* copy data from active UV */
 		if (layernum) {
-			editmesh_face_copy_customdata(em, CD_MTEXPOLY, layernum);
+			const int layernum_dst = CustomData_get_active_layer(&em->bm->pdata, CD_MTEXPOLY);
+			BM_data_layer_copy(em->bm, &em->bm->pdata, CD_MTEXPOLY, layernum, layernum_dst);
 		}
 		if (active_set || layernum == 0) {
 			CustomData_set_layer_active(&em->bm->pdata, CD_MTEXPOLY, layernum);
@@ -370,7 +335,9 @@
 		BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_MLOOPUV, name);
 		/* copy data from active UV */
 		if (layernum) {
-			editmesh_loop_copy_customdata(em, CD_MLOOPUV, layernum);
+			const int layernum_dst = CustomData_get_active_layer(&em->bm->ldata, CD_MLOOPUV);
+			BM_data_layer_copy(em->bm, &em->bm->ldata, CD_MLOOPUV, layernum, layernum_dst);
+
 			is_init = TRUE;
 		}
 		if (active_set || layernum == 0) {
@@ -457,7 +424,8 @@
 		BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_MLOOPCOL, name);
 		/* copy data from active vertex color layer */
 		if (layernum) {
-			editmesh_loop_copy_customdata(em, CD_MLOOPCOL, layernum);
+			const int layernum_dst = CustomData_get_active_layer(&em->bm->ldata, CD_MLOOPCOL);
+			BM_data_layer_copy(em->bm, &em->bm->ldata, CD_MLOOPUV, layernum, layernum_dst);
 		}
 		if (active_set || layernum == 0) {
 			CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPCOL, layernum);

Modified: trunk/blender/source/blender/python/bmesh/bmesh_py_types_customdata.c
===================================================================
--- trunk/blender/source/blender/python/bmesh/bmesh_py_types_customdata.c	2012-04-30 18:37:34 UTC (rev 46126)
+++ trunk/blender/source/blender/python/bmesh/bmesh_py_types_customdata.c	2012-04-30 18:54:14 UTC (rev 46127)
@@ -124,7 +124,7 @@
 
 
 PyDoc_STRVAR(bpy_bmlayercollection_active_doc,
-"This meshes vert sequence (read-only).\n\n:type: :class:`BMVertSeq`"
+"The active layer of this type (read-only).\n\n:type: :class:`BMLayerItem`"
 );
 static PyObject *bpy_bmlayercollection_active_get(BPy_BMLayerItem *self, void *UNUSED(flag))
 {
@@ -145,6 +145,17 @@
 	}
 }
 
+
+PyDoc_STRVAR(bpy_bmlayercollection_is_singleton_doc,
+"This meshes vert sequence (read-only).\n\n:type: :class:`BMVertSeq`"
+);
+static PyObject *bpy_bmlayercollection_is_singleton_get(BPy_BMLayerItem *self, void *UNUSED(flag))
+{
+	BPY_BM_CHECK_OBJ(self);
+
+	return PyBool_FromLong(CustomData_layertype_is_singleton(self->type));
+}
+
 PyDoc_STRVAR(bpy_bmlayercollection_name_doc,
 "The layers unique name (read-only).\n\n:type: string"
 );
@@ -211,7 +222,8 @@
 
 static PyGetSetDef bpy_bmlayercollection_getseters[] = {
     /* BMESH_TODO, make writeable */
-    {(char *)"active", (getter)bpy_bmlayercollection_active_get, (setter)NULL, (char *)bpy_bmlayercollection_active_doc, NULL},
+    {(char *)"active",       (getter)bpy_bmlayercollection_active_get,       (setter)NULL, (char *)bpy_bmlayercollection_active_doc, NULL},
+    {(char *)"is_singleton", (getter)bpy_bmlayercollection_is_singleton_get, (setter)NULL, (char *)bpy_bmlayercollection_is_singleton_doc, NULL},
 
     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
 };
@@ -230,7 +242,88 @@
 /* BMLayerCollection
  * ----------------- */
 
+PyDoc_STRVAR(bpy_bmlayeritem_copy_from_doc,
+".. method:: copy_from(other)\n"
+"\n"
+"   Return a copy of the layer\n"
+"\n"
+"   :arg other: Another layer to copy from.\n"
+"   :arg other: :class:`BMLayerItem`\n"
+);
+static PyObject *bpy_bmlayeritem_copy_from(BPy_BMLayerItem *self, BPy_BMLayerItem *value)
+{
+	CustomData *data;
 
+	if (!BPy_BMLayerItem_Check(value)) {
+		PyErr_Format(PyExc_TypeError,
+		             "layer.copy_from(x): expected BMLayerItem, not '%.200s'",
+		             Py_TYPE(value)->tp_name);
+		return NULL;
+	}
+
+	BPY_BM_CHECK_OBJ(self);
+	BPY_BM_CHECK_OBJ(value);
+
+	if (self->bm != value->bm) {
+		PyErr_SetString(PyExc_ValueError,
+		                "layer.copy_from(): layer is from another mesh");
+		return NULL;
+	}
+
+	else if ((self->htype != value->htype) ||
+	         (self->type  != value->type) ||
+	         (self->index != value->index))
+	{
+		PyErr_SetString(PyExc_ValueError,

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list