[Bf-blender-cvs] [3f657e7ef19] master: Python: show additional context for PyDriver errors in the stderr

Campbell Barton noreply at git.blender.org
Thu Jul 7 04:35:23 CEST 2022


Commit: 3f657e7ef19a39d7dca64ed5fdee57336f445c62
Author: Campbell Barton
Date:   Thu Jul 7 12:30:47 2022 +1000
Branches: master
https://developer.blender.org/rB3f657e7ef19a39d7dca64ed5fdee57336f445c62

Python: show additional context for PyDriver errors in the stderr

Showing the expression alone may not be enough to track down an error
evaluating a py-driver. Show information about the target ID & property
in the error message as well.

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

M	source/blender/python/intern/bpy_driver.c

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

diff --git a/source/blender/python/intern/bpy_driver.c b/source/blender/python/intern/bpy_driver.c
index 3134969d21c..9df3ea9b318 100644
--- a/source/blender/python/intern/bpy_driver.c
+++ b/source/blender/python/intern/bpy_driver.c
@@ -19,6 +19,7 @@
 #include "BKE_animsys.h"
 #include "BKE_fcurve_driver.h"
 #include "BKE_global.h"
+#include "BKE_idtype.h"
 
 #include "RNA_access.h"
 #include "RNA_prototypes.h"
@@ -273,13 +274,27 @@ void BPY_driver_reset(void)
   }
 }
 
-/** Error return function for #BPY_eval_pydriver. */
-static void pydriver_error(ChannelDriver *driver)
+/**
+ * Error return function for #BPY_eval_pydriver.
+ *
+ * \param anim_rna: Used to show the target when printing the error to give additional context.
+ */
+static void pydriver_error(ChannelDriver *driver, const struct PathResolvedRNA *anim_rna)
 {
   driver->flag |= DRIVER_FLAG_INVALID; /* Python expression failed. */
+
+  const char *null_str = "<null>";
+  const ID *id = anim_rna->ptr.owner_id;
   fprintf(stderr,
-          "\nError in Driver: The following Python expression failed:\n\t'%s'\n\n",
-          driver->expression);
+          "\n"
+          "Error in PyDriver: expression failed: %s\n"
+          "For target: (type=%s, name=\"%s\", property=%s, property_index=%d)\n"
+          "\n",
+          driver->expression,
+          id ? BKE_idtype_idcode_to_name(GS(id->name)) : null_str,
+          id ? id->name + 2 : null_str,
+          anim_rna->prop ? RNA_property_identifier(anim_rna->prop) : null_str,
+          anim_rna->prop_index);
 
   // BPy_errors_to_report(NULL); /* TODO: reports. */
   PyErr_Print();
@@ -718,11 +733,11 @@ float BPY_driver_exec(struct PathResolvedRNA *anim_rna,
 
   /* Process the result. */
   if (retval == NULL) {
-    pydriver_error(driver);
+    pydriver_error(driver, anim_rna);
   }
   else {
     if (UNLIKELY((result = PyFloat_AsDouble(retval)) == -1.0 && PyErr_Occurred())) {
-      pydriver_error(driver);
+      pydriver_error(driver, anim_rna);
       result = 0.0;
     }
     else {



More information about the Bf-blender-cvs mailing list