[Bf-blender-cvs] [428a1aaf737] master: UI: add back Layout.introspect

Campbell Barton noreply at git.blender.org
Wed Sep 2 09:56:10 CEST 2020


Commit: 428a1aaf7372aaad793fe7cc03128db18e3ae602
Author: Campbell Barton
Date:   Tue Sep 1 15:23:55 2020 +1000
Branches: master
https://developer.blender.org/rB428a1aaf7372aaad793fe7cc03128db18e3ae602

UI: add back Layout.introspect

Add back this function, removed 2e14b7fb9770b.

Useful for checking operators used in menus.

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

M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/interface/interface_layout.c
M	source/blender/python/intern/CMakeLists.txt
M	source/blender/python/intern/bpy_rna.c
M	source/blender/python/intern/bpy_rna_types_capi.c
A	source/blender/python/intern/bpy_rna_ui.c
A	source/blender/python/intern/bpy_rna_ui.h

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

diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h
index 367f7965026..4fc537ca5c2 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -2407,6 +2407,9 @@ void uiItemTabsEnumR_prop(uiLayout *layout,
                           PropertyRNA *prop,
                           bool icon_only);
 
+/* Only for testing, inspecting layouts. */
+const char *UI_layout_introspect(uiLayout *layout);
+
 /* UI Operators */
 typedef struct uiDragColorHandle {
   float color[3];
diff --git a/source/blender/editors/interface/interface_layout.c b/source/blender/editors/interface/interface_layout.c
index 50355d350ac..e1f3e14eda1 100644
--- a/source/blender/editors/interface/interface_layout.c
+++ b/source/blender/editors/interface/interface_layout.c
@@ -31,6 +31,7 @@
 #include "DNA_userdef_types.h"
 
 #include "BLI_alloca.h"
+#include "BLI_dynstr.h"
 #include "BLI_listbase.h"
 #include "BLI_math.h"
 #include "BLI_rect.h"
@@ -5637,3 +5638,126 @@ void UI_paneltype_draw(bContext *C, PanelType *pt, uiLayout *layout)
 }
 
 /** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Layout (Debuging/Introspection)
+ *
+ * Serialize the layout as a Python compatible dictionary,
+ *
+ * \note Proper string escaping isn't used,
+ * triple quotes are used to prevent single quotes from interfering with Python syntax.
+ * If we want this to be fool-proof, we would need full Python compatible string escape support.
+ * As we don't use triple quotes in the UI it's good-enough in practice.
+ * \{ */
+
+static void ui_layout_introspect_button(DynStr *ds, uiButtonItem *bitem)
+{
+  uiBut *but = bitem->but;
+  BLI_dynstr_appendf(ds, "'type':%d, ", (int)but->type);
+  BLI_dynstr_appendf(ds, "'draw_string':'''%s''', ", but->drawstr);
+  /* Not exactly needed, rna has this. */
+  BLI_dynstr_appendf(ds, "'tip':'''%s''', ", but->tip ? but->tip : "");
+
+  if (but->optype) {
+    char *opstr = WM_operator_pystring_ex(
+        but->block->evil_C, NULL, false, true, but->optype, but->opptr);
+    BLI_dynstr_appendf(ds, "'operator':'''%s''', ", opstr ? opstr : "");
+    MEM_freeN(opstr);
+  }
+
+  {
+    PropertyRNA *prop = NULL;
+    wmOperatorType *ot = UI_but_operatortype_get_from_enum_menu(but, &prop);
+    if (ot) {
+      char *opstr = WM_operator_pystring_ex(but->block->evil_C, NULL, false, true, ot, NULL);
+      BLI_dynstr_appendf(ds, "'operator':'''%s''', ", opstr ? opstr : "");
+      BLI_dynstr_appendf(ds, "'property':'''%s''', ", prop ? RNA_property_identifier(prop) : "");
+      MEM_freeN(opstr);
+    }
+  }
+
+  if (but->rnaprop) {
+    BLI_dynstr_appendf(ds,
+                       "'rna':'%s.%s[%d]', ",
+                       RNA_struct_identifier(but->rnapoin.type),
+                       RNA_property_identifier(but->rnaprop),
+                       but->rnaindex);
+  }
+}
+
+static void ui_layout_introspect_items(DynStr *ds, ListBase *lb)
+{
+  uiItem *item;
+
+  BLI_dynstr_append(ds, "[");
+
+  for (item = lb->first; item; item = item->next) {
+
+    BLI_dynstr_append(ds, "{");
+
+#define CASE_ITEM(id) \
+  case id: { \
+    const char *id_str = STRINGIFY(id); \
+    BLI_dynstr_append(ds, "'type': '"); \
+    /* Skip 'ITEM_'. */ \
+    BLI_dynstr_append(ds, id_str + 5); \
+    BLI_dynstr_append(ds, "', "); \
+    break; \
+  } \
+    ((void)0)
+
+    switch (item->type) {
+      CASE_ITEM(ITEM_BUTTON);
+      CASE_ITEM(ITEM_LAYOUT_ROW);
+      CASE_ITEM(ITEM_LAYOUT_COLUMN);
+      CASE_ITEM(ITEM_LAYOUT_COLUMN_FLOW);
+      CASE_ITEM(ITEM_LAYOUT_ROW_FLOW);
+      CASE_ITEM(ITEM_LAYOUT_BOX);
+      CASE_ITEM(ITEM_LAYOUT_ABSOLUTE);
+      CASE_ITEM(ITEM_LAYOUT_SPLIT);
+      CASE_ITEM(ITEM_LAYOUT_OVERLAP);
+      CASE_ITEM(ITEM_LAYOUT_ROOT);
+      CASE_ITEM(ITEM_LAYOUT_GRID_FLOW);
+      CASE_ITEM(ITEM_LAYOUT_RADIAL);
+    }
+
+#undef CASE_ITEM
+
+    switch (item->type) {
+      case ITEM_BUTTON:
+        ui_layout_introspect_button(ds, (uiButtonItem *)item);
+        break;
+      default:
+        BLI_dynstr_append(ds, "'items':");
+        ui_layout_introspect_items(ds, &((uiLayout *)item)->items);
+        break;
+    }
+
+    BLI_dynstr_append(ds, "}");
+
+    if (item != lb->last) {
+      BLI_dynstr_append(ds, ", ");
+    }
+  }
+  /* Don't use a comma here as it's not needed and
+   * causes the result to evaluate to a tuple of 1. */
+  BLI_dynstr_append(ds, "]");
+}
+
+/**
+ * Evaluate layout items as a Python dictionary.
+ */
+const char *UI_layout_introspect(uiLayout *layout)
+{
+  DynStr *ds = BLI_dynstr_new();
+  uiLayout layout_copy = *layout;
+  layout_copy.item.next = NULL;
+  layout_copy.item.prev = NULL;
+  ListBase layout_dummy_list = {&layout_copy, &layout_copy};
+  ui_layout_introspect_items(ds, &layout_dummy_list);
+  const char *result = BLI_dynstr_get_cstring(ds);
+  BLI_dynstr_free(ds);
+  return result;
+}
+
+/** \} */
diff --git a/source/blender/python/intern/CMakeLists.txt b/source/blender/python/intern/CMakeLists.txt
index 44949c478cc..b50aee73a13 100644
--- a/source/blender/python/intern/CMakeLists.txt
+++ b/source/blender/python/intern/CMakeLists.txt
@@ -79,6 +79,7 @@ set(SRC
   bpy_rna_gizmo.c
   bpy_rna_id_collection.c
   bpy_rna_types_capi.c
+  bpy_rna_ui.c
   bpy_traceback.c
   bpy_utils_previews.c
   bpy_utils_units.c
@@ -116,6 +117,7 @@ set(SRC
   bpy_rna_gizmo.h
   bpy_rna_id_collection.h
   bpy_rna_types_capi.h
+  bpy_rna_ui.h
   bpy_traceback.h
   bpy_utils_previews.h
   bpy_utils_units.h
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c
index a3ded8813ac..cc981c7c2e1 100644
--- a/source/blender/python/intern/bpy_rna.c
+++ b/source/blender/python/intern/bpy_rna.c
@@ -9010,11 +9010,12 @@ void pyrna_struct_type_extend_capi(struct StructRNA *srna,
         py_method = PyClassMethod_New(cfunc);
         Py_DECREF(cfunc);
       }
-      else {
-        /* Currently only static and class methods are used. */
-        BLI_assert(method->ml_flags & METH_STATIC);
+      else if (method->ml_flags & METH_STATIC) {
         py_method = PyCFunction_New(method, NULL);
       }
+      else {
+        py_method = PyDescr_NewMethod(type, method);
+      }
 
       const int err = PyDict_SetItemString(dict, method->ml_name, py_method);
       Py_DECREF(py_method);
diff --git a/source/blender/python/intern/bpy_rna_types_capi.c b/source/blender/python/intern/bpy_rna_types_capi.c
index 5a2ba4a5cdb..30623724593 100644
--- a/source/blender/python/intern/bpy_rna_types_capi.c
+++ b/source/blender/python/intern/bpy_rna_types_capi.c
@@ -38,6 +38,7 @@
 #include "bpy_rna_callback.h"
 #include "bpy_rna_id_collection.h"
 #include "bpy_rna_types_capi.h"
+#include "bpy_rna_ui.h"
 
 #include "../generic/py_capi_utils.h"
 
@@ -72,6 +73,17 @@ static struct PyMethodDef pyrna_blenddatalibraries_methods[] = {
 
 /** \} */
 
+/* -------------------------------------------------------------------- */
+/** \name UI Layout
+ * \{ */
+
+static struct PyMethodDef pyrna_uilayout_methods[] = {
+    {NULL, NULL, 0, NULL}, /* #BPY_rna_uilayout_introspect_method_def */
+    {NULL, NULL, 0, NULL},
+};
+
+/** \} */
+
 /* -------------------------------------------------------------------- */
 /** \name Window Manager Clipboard Property
  *
@@ -205,6 +217,11 @@ void BPY_rna_types_extend_capi(void)
   BLI_assert(ARRAY_SIZE(pyrna_blenddatalibraries_methods) == 3);
   pyrna_struct_type_extend_capi(&RNA_BlendDataLibraries, pyrna_blenddatalibraries_methods, NULL);
 
+  /* uiLayout */
+  ARRAY_SET_ITEMS(pyrna_uilayout_methods, BPY_rna_uilayout_introspect_method_def);
+  BLI_assert(ARRAY_SIZE(pyrna_uilayout_methods) == 2);
+  pyrna_struct_type_extend_capi(&RNA_UILayout, pyrna_uilayout_methods, NULL);
+
   /* Space */
   pyrna_struct_type_extend_capi(&RNA_Space, pyrna_space_methods, NULL);
 
diff --git a/source/blender/python/intern/bpy_rna_ui.c b/source/blender/python/intern/bpy_rna_ui.c
new file mode 100644
index 00000000000..9f37b8300db
--- /dev/null
+++ b/source/blender/python/intern/bpy_rna_ui.c
@@ -0,0 +1,58 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup pythonintern
+ *
+ * This adds helpers to #uiLayout which can't be added easily to RNA it's self.
+ */
+
+#include <Python.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "../generic/py_capi_utils.h"
+
+#include "UI_interface.h"
+
+#include "bpy_rna.h"
+
+PyDoc_STRVAR(bpy_rna_uilayout_introspect_doc,
+             ".. method:: introspect()\n"
+             "\n"
+             "   Return a dictionary containing a textual representation of the UI layout.\n");
+static PyObject *bpy_rna_uilayout_introspect(PyObject *self)
+{
+  BPy_StructRNA *pyrna = (BPy_StructRNA *)self;
+  uiLayout *layout = pyrna->ptr.data;
+
+  const char *expr = UI_layout_introspect(layout);
+  PyObject *main_mod = NULL;
+  PyC_MainModule_Backup(&main_mod);
+  PyObject *py_dict = PyC_DefaultNameSpace("<introspect>");
+  PyObject *result = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
+  MEM_freeN((void *)expr);
+  Py_DECREF(py_dict);
+  PyC_MainModule_Restore(main_mod);
+  return result;
+}
+
+PyMethodDef BPY_rna_uilayout_introspect_method_def = {
+    "introspect",
+    (PyCFunction)bpy_rna_uilayout_introspect,
+    METH_NOARGS,
+    bpy_rna_uilayout_introspect_doc,
+};
diff --git a/source/blender/python/intern/bpy_rna_ui.h b/source/blender/python/intern/bpy_rna_ui.h
new file mode 100644
index 00000000000..bd2e87aae40
--- /dev/null
+++ b/source/blender/py

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list