[Bf-blender-cvs] [401b3d316a5] refactor-mesh-uv-map-generic: Fix bmesh python UV interface.

Martijn Versteegh noreply at git.blender.org
Tue Nov 8 14:13:39 CET 2022


Commit: 401b3d316a5d9898b37eb7d6c29aec44922b07e3
Author: Martijn Versteegh
Date:   Tue Nov 1 12:06:05 2022 +0100
Branches: refactor-mesh-uv-map-generic
https://developer.blender.org/rB401b3d316a5d9898b37eb7d6c29aec44922b07e3

Fix bmesh python UV interface.

Use a pointer in the python object to be able to write back on the set
functions. A problem remaining is that writing to an uninitialized
bool layer does not (lazily) create it but the write is just lost.

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

M	source/blender/bmesh/bmesh_class.h
M	source/blender/python/bmesh/bmesh_py_types_meshdata.c

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

diff --git a/source/blender/bmesh/bmesh_class.h b/source/blender/bmesh/bmesh_class.h
index e2d6044f243..5139cbec692 100644
--- a/source/blender/bmesh/bmesh_class.h
+++ b/source/blender/bmesh/bmesh_class.h
@@ -532,6 +532,34 @@ typedef bool (*BMLoopPairFilterFunc)(const BMLoop *, const BMLoop *, void *user_
 #define BM_ELEM_CD_GET_OPT_BOOL(ele, offset) \
   (offset == -1 ? false : *((bool *)((char *)(ele)->head.data + (offset))))
 
+
+#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
+#  define BM_ELEM_CD_GET_BOOL_P(ele, offset) \
+    (BLI_assert(offset != -1), \
+     _Generic(ele, \
+              GENERIC_TYPE_ANY((bool *)POINTER_OFFSET((ele)->head.data, offset), \
+                               _BM_GENERIC_TYPE_ELEM_NONCONST), \
+              GENERIC_TYPE_ANY((const bool *)POINTER_OFFSET((ele)->head.data, offset), \
+                               _BM_GENERIC_TYPE_ELEM_CONST)))
+#else
+#  define BM_ELEM_CD_GET_BOOL_P(ele, offset) \
+    (BLI_assert(offset != -1), (bool *)((char *)(ele)->head.data + (offset)))
+#endif
+
+#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
+#  define BM_ELEM_CD_GET_OPT_BOOL_P(ele, offset) \
+    ((offset != -1) ? \
+     _Generic(ele, \
+              GENERIC_TYPE_ANY((bool *)POINTER_OFFSET((ele)->head.data, offset), \
+                               _BM_GENERIC_TYPE_ELEM_NONCONST), \
+              GENERIC_TYPE_ANY((const bool *)POINTER_OFFSET((ele)->head.data, offset), \
+                               _BM_GENERIC_TYPE_ELEM_CONST)) : NULL)
+#else
+#  define BM_ELEM_CD_GET_OPT_BOOL_P(ele, offset) \
+    ((offset != -1) ? (bool *)((char *)(ele)->head.data + (offset)) : NULL)
+#endif
+
+
 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
 #  define BM_ELEM_CD_GET_VOID_P(ele, offset) \
     (BLI_assert(offset != -1), \
diff --git a/source/blender/python/bmesh/bmesh_py_types_meshdata.c b/source/blender/python/bmesh/bmesh_py_types_meshdata.c
index 81ab5c230b9..0381a969b01 100644
--- a/source/blender/python/bmesh/bmesh_py_types_meshdata.c
+++ b/source/blender/python/bmesh/bmesh_py_types_meshdata.c
@@ -36,10 +36,10 @@
 
 typedef struct BPy_BMLoopUV {
   PyObject_VAR_HEAD
-  float uv[2];
-  bool vertsel;
-  bool edgesel;
-  bool pinned;
+  float *uv;
+  bool *vertsel;
+  bool *edgesel;
+  bool *pinned;
 } BPy_BMLoopUV;
 
 PyDoc_STRVAR(bpy_bmloopuv_uv_doc,
@@ -66,31 +66,42 @@ PyDoc_STRVAR(bpy_bmloopuv_select_edge_doc, "UV edge select state.\n\n:type: bool
 
 static PyObject *bpy_bmloopuv_pin_uv_get(BPy_BMLoopUV *self, void *UNUSED(closure))
 {
-  return PyBool_FromLong(self->pinned);
+  return self->pinned ? PyBool_FromLong(*self->pinned) : false;
 }
 static int bpy_bmloopuv_pin_uv_set(BPy_BMLoopUV *self, PyObject *value, void *UNUSED(closure))
 {
-  self->pinned = PyC_Long_AsBool(value);
+  if (self->pinned) {
+    *self->pinned = PyC_Long_AsBool(value);
+  }
+  /* TODO(martijn) if (!self->pinmned) that means the layed does not exist , or at least didn't exist
+   * when the PY object was created
+   * we *should* create it here instead of just bailing...
+   * same for vertsel and edgesel
+   */
   return 0;
 }
 
 static PyObject *bpy_bmloopuv_select_get(BPy_BMLoopUV *self, void *UNUSED(closure))
 {
-  return PyBool_FromLong(self->vertsel);
+  return self->vertsel ? PyBool_FromLong(*self->vertsel) : false;
 }
 static int bpy_bmloopuv_select_set(BPy_BMLoopUV *self, PyObject *value, void *UNUSED(closure))
 {
-  self->vertsel = PyC_Long_AsBool(value);
+  if (self->vertsel) {
+    *self->vertsel = PyC_Long_AsBool(value);
+  }
   return 0;
 }
 
 static PyObject *bpy_bmloopuv_select_edge_get(BPy_BMLoopUV *self, void *UNUSED(closure))
 {
-  return PyBool_FromLong(self->edgesel);
+  return self->edgesel ? PyBool_FromLong(*self->edgesel) : false;
 }
 static int bpy_bmloopuv_select_edge_set(BPy_BMLoopUV *self, PyObject *value, void *UNUSED(closure))
 {
-  self->edgesel = PyC_Long_AsBool(value);
+  if (self->edgesel) {
+    *self->edgesel = PyC_Long_AsBool(value);
+  }
   return 0;
 }
 
@@ -166,9 +177,9 @@ PyObject *BPy_BMLoopUV_CreatePyObject(struct BMesh *bm, const int loop_index)
 
   BMLoop *l = BM_loop_at_index_find(bm, loop_index);
   float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
-  self->vertsel = BM_ELEM_CD_GET_OPT_BOOL(l, offsets.select_vert);
-  self->edgesel = BM_ELEM_CD_GET_OPT_BOOL(l, offsets.select_edge);
-  self->pinned = BM_ELEM_CD_GET_OPT_BOOL(l, offsets.pin);
+  self->vertsel = BM_ELEM_CD_GET_OPT_BOOL_P(l, offsets.select_vert);
+  self->edgesel = BM_ELEM_CD_GET_OPT_BOOL_P(l, offsets.select_edge);
+  self->pinned = BM_ELEM_CD_GET_OPT_BOOL_P(l, offsets.pin);
   copy_v2_v2(self->uv, luv);
 
   return (PyObject *)self;



More information about the Bf-blender-cvs mailing list