[Bf-blender-cvs] [0950cfd9d53] master: PyAPI: add read-only 'is_valid' attribute to mathutils types

Campbell Barton noreply at git.blender.org
Fri Sep 3 13:22:05 CEST 2021


Commit: 0950cfd9d53ef666cf820765e83586f72b04e9b6
Author: Campbell Barton
Date:   Fri Sep 3 21:18:03 2021 +1000
Branches: master
https://developer.blender.org/rB0950cfd9d53ef666cf820765e83586f72b04e9b6

PyAPI: add read-only 'is_valid' attribute to mathutils types

There was no convenient way to check if the owner
of a mathutils type was valid.

Added to support issue reported in T91111.

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

M	source/blender/python/mathutils/mathutils.c
M	source/blender/python/mathutils/mathutils.h
M	source/blender/python/mathutils/mathutils_Color.c
M	source/blender/python/mathutils/mathutils_Euler.c
M	source/blender/python/mathutils/mathutils_Matrix.c
M	source/blender/python/mathutils/mathutils_Quaternion.c
M	source/blender/python/mathutils/mathutils_Vector.c

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

diff --git a/source/blender/python/mathutils/mathutils.c b/source/blender/python/mathutils/mathutils.c
index be7dae6871b..0043fc36162 100644
--- a/source/blender/python/mathutils/mathutils.c
+++ b/source/blender/python/mathutils/mathutils.c
@@ -599,6 +599,15 @@ uchar Mathutils_RegisterCallback(Mathutils_Callback *cb)
   return i;
 }
 
+int _BaseMathObject_CheckCallback(BaseMathObject *self)
+{
+  Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
+  if (LIKELY(cb->check(self) != -1)) {
+    return 0;
+  }
+  return -1;
+}
+
 /* use macros to check for NULL */
 int _BaseMathObject_ReadCallback(BaseMathObject *self)
 {
@@ -687,6 +696,13 @@ PyObject *BaseMathObject_is_frozen_get(BaseMathObject *self, void *UNUSED(closur
   return PyBool_FromLong((self->flag & BASE_MATH_FLAG_IS_FROZEN) != 0);
 }
 
+char BaseMathObject_is_valid_doc[] =
+    "True when the owner of this data is valid.\n\n:type: boolean";
+PyObject *BaseMathObject_is_valid_get(BaseMathObject *self, void *UNUSED(closure))
+{
+  return PyBool_FromLong(BaseMath_CheckCallback(self) == 0);
+}
+
 char BaseMathObject_freeze_doc[] =
     ".. function:: freeze()\n"
     "\n"
diff --git a/source/blender/python/mathutils/mathutils.h b/source/blender/python/mathutils/mathutils.h
index 80be841785a..4aa26dcc5be 100644
--- a/source/blender/python/mathutils/mathutils.h
+++ b/source/blender/python/mathutils/mathutils.h
@@ -28,6 +28,7 @@ struct DynStr;
 
 extern char BaseMathObject_is_wrapped_doc[];
 extern char BaseMathObject_is_frozen_doc[];
+extern char BaseMathObject_is_valid_doc[];
 extern char BaseMathObject_owner_doc[];
 
 #define BASE_MATH_NEW(struct_name, root_type, base_type) \
@@ -81,6 +82,7 @@ typedef struct {
 PyObject *BaseMathObject_owner_get(BaseMathObject *self, void *);
 PyObject *BaseMathObject_is_wrapped_get(BaseMathObject *self, void *);
 PyObject *BaseMathObject_is_frozen_get(BaseMathObject *self, void *);
+PyObject *BaseMathObject_is_valid_get(BaseMathObject *self, void *);
 
 extern char BaseMathObject_freeze_doc[];
 PyObject *BaseMathObject_freeze(BaseMathObject *self);
@@ -117,6 +119,7 @@ struct Mathutils_Callback {
 
 unsigned char Mathutils_RegisterCallback(Mathutils_Callback *cb);
 
+int _BaseMathObject_CheckCallback(BaseMathObject *self);
 int _BaseMathObject_ReadCallback(BaseMathObject *self);
 int _BaseMathObject_WriteCallback(BaseMathObject *self);
 int _BaseMathObject_ReadIndexCallback(BaseMathObject *self, int index);
@@ -126,6 +129,8 @@ void _BaseMathObject_RaiseFrozenExc(const BaseMathObject *self);
 void _BaseMathObject_RaiseNotFrozenExc(const BaseMathObject *self);
 
 /* since this is called so often avoid where possible */
+#define BaseMath_CheckCallback(_self) \
+  (((_self)->cb_user ? _BaseMathObject_CheckCallback((BaseMathObject *)_self) : 0))
 #define BaseMath_ReadCallback(_self) \
   (((_self)->cb_user ? _BaseMathObject_ReadCallback((BaseMathObject *)_self) : 0))
 #define BaseMath_WriteCallback(_self) \
diff --git a/source/blender/python/mathutils/mathutils_Color.c b/source/blender/python/mathutils/mathutils_Color.c
index 7546f2ef730..13d712bddb0 100644
--- a/source/blender/python/mathutils/mathutils_Color.c
+++ b/source/blender/python/mathutils/mathutils_Color.c
@@ -866,6 +866,11 @@ static PyGetSetDef Color_getseters[] = {
      (setter)NULL,
      BaseMathObject_is_frozen_doc,
      NULL},
+    {"is_valid",
+     (getter)BaseMathObject_is_valid_get,
+     (setter)NULL,
+     BaseMathObject_is_valid_doc,
+     NULL},
     {"owner", (getter)BaseMathObject_owner_get, (setter)NULL, BaseMathObject_owner_doc, NULL},
     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
 };
diff --git a/source/blender/python/mathutils/mathutils_Euler.c b/source/blender/python/mathutils/mathutils_Euler.c
index 595d03b533b..1033d186fca 100644
--- a/source/blender/python/mathutils/mathutils_Euler.c
+++ b/source/blender/python/mathutils/mathutils_Euler.c
@@ -699,6 +699,11 @@ static PyGetSetDef Euler_getseters[] = {
      (setter)NULL,
      BaseMathObject_is_frozen_doc,
      NULL},
+    {"is_valid",
+     (getter)BaseMathObject_is_valid_get,
+     (setter)NULL,
+     BaseMathObject_is_valid_doc,
+     NULL},
     {"owner", (getter)BaseMathObject_owner_get, (setter)NULL, BaseMathObject_owner_doc, NULL},
     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
 };
diff --git a/source/blender/python/mathutils/mathutils_Matrix.c b/source/blender/python/mathutils/mathutils_Matrix.c
index 36b8b0b6d35..ce04a143aae 100644
--- a/source/blender/python/mathutils/mathutils_Matrix.c
+++ b/source/blender/python/mathutils/mathutils_Matrix.c
@@ -3148,6 +3148,11 @@ static PyGetSetDef Matrix_getseters[] = {
      (setter)NULL,
      BaseMathObject_is_frozen_doc,
      NULL},
+    {"is_valid",
+     (getter)BaseMathObject_is_valid_get,
+     (setter)NULL,
+     BaseMathObject_is_valid_doc,
+     NULL},
     {"owner", (getter)BaseMathObject_owner_get, (setter)NULL, BaseMathObject_owner_doc, NULL},
     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
 };
diff --git a/source/blender/python/mathutils/mathutils_Quaternion.c b/source/blender/python/mathutils/mathutils_Quaternion.c
index 77a30dcd447..525b2da7d06 100644
--- a/source/blender/python/mathutils/mathutils_Quaternion.c
+++ b/source/blender/python/mathutils/mathutils_Quaternion.c
@@ -1505,6 +1505,11 @@ static PyGetSetDef Quaternion_getseters[] = {
      (setter)NULL,
      BaseMathObject_is_frozen_doc,
      NULL},
+    {"is_valid",
+     (getter)BaseMathObject_is_valid_get,
+     (setter)NULL,
+     BaseMathObject_is_valid_doc,
+     NULL},
     {"owner", (getter)BaseMathObject_owner_get, (setter)NULL, BaseMathObject_owner_doc, NULL},
     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
 };
diff --git a/source/blender/python/mathutils/mathutils_Vector.c b/source/blender/python/mathutils/mathutils_Vector.c
index efcaa9b6a51..23758c5603e 100644
--- a/source/blender/python/mathutils/mathutils_Vector.c
+++ b/source/blender/python/mathutils/mathutils_Vector.c
@@ -2551,6 +2551,11 @@ static PyGetSetDef Vector_getseters[] = {
      (setter)NULL,
      BaseMathObject_is_frozen_doc,
      NULL},
+    {"is_valid",
+     (getter)BaseMathObject_is_valid_get,
+     (setter)NULL,
+     BaseMathObject_is_valid_doc,
+     NULL},
     {"owner", (getter)BaseMathObject_owner_get, (setter)NULL, BaseMathObject_owner_doc, NULL},
 
     /* Auto-generated swizzle attributes, see Python script above. */



More information about the Bf-blender-cvs mailing list