From 482d889e008ee54f3162265182dd45f8cafd00bc Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 20 Apr 2023 16:40:53 +0300 Subject: [PATCH 1/6] Moving python error handling to c-code --- scripts/modules/bpy_hydra.py | 24 +++++-------- .../render/hydra/scene_delegate/material.cc | 34 ++++++++++++++----- source/blender/render/hydra/utils.cc | 7 ++++ source/blender/render/hydra/utils.h | 1 + 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/scripts/modules/bpy_hydra.py b/scripts/modules/bpy_hydra.py index 07eaa455912e..5b1c9a891871 100644 --- a/scripts/modules/bpy_hydra.py +++ b/scripts/modules/bpy_hydra.py @@ -100,25 +100,19 @@ class HydraRenderEngine(bpy.types.RenderEngine): _bpy_hydra.engine_view_draw(self.engine_ptr, depsgraph.as_pointer(), context.as_pointer()) -def export_mtlx(material): +def export_mtlx(material, mtlx_file): """ Exports material to .mtlx file. It is called from Blender source code. """ try: import materialx.utils as mx_utils - - doc = mx_utils.export(material, None) - if not doc: - return "" - - mtlx_file = mx_utils.get_temp_file(".mtlx", material.name) - mx_utils.export_to_file(doc, mtlx_file, export_deps=True, copy_deps=False) - return str(mtlx_file) - except ImportError: print("ERROR: no MaterialX addon available") + return False - except: - # This is a placeholder, this code will be moved to C part later - # This code shouldn't raise any exception due to breaking refcounts on RenderEngine - traceback.print_exc() + raise TypeError("asasfas") + + doc = mx_utils.export(material, None) + if not doc: + return False - return "" + mx_utils.export_to_file(doc, mtlx_file, export_deps=True, copy_deps=False) + return True diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index b385800f9b31..3f640452fe43 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -17,6 +17,7 @@ #include "blender_scene_delegate.h" #include "material.h" #include "mtlx_hydra_adapter.h" +#include "../utils.h" namespace blender::render::hydra { @@ -65,25 +66,42 @@ void MaterialData::init() RNA_pointer_create(NULL, &RNA_Material, id_, &materialptr); PyObject *material = pyrna_struct_CreatePyObject(&materialptr); - result = PyObject_CallFunction(func, "O", material); + char file_name[32]; + snprintf(file_name, 32, "mat_%016llx.mtlx", (uintptr_t)id_); + std::string file_path = temp_file_path(file_name); + result = PyObject_CallFunction(func, "Os", material, file_path.c_str()); Py_DECREF(material); - std::string path; - - if (!PyErr_Occurred()) { - path = PyUnicode_AsUTF8(result); + if (!PyErr_Occurred()) { + if (!PyObject_IsTrue(result)) { + file_path = ""; + } Py_DECREF(result); } else { - CLOG_ERROR(LOG_BSD, "Export error for %s", id_->name); - PyErr_Print(); + file_path = ""; + std::string err_str; + + /* Clearing exception data */ + PyObject *type, *value, *traceback; + PyErr_Fetch(&type, &value, &traceback); + if (value) { + PyObject *pstr = PyObject_Str(value); + err_str = PyUnicode_AsUTF8(pstr); + Py_DECREF(pstr); + } + Py_XDECREF(traceback); + Py_XDECREF(value); + Py_DECREF(type); + + CLOG_ERROR(LOG_BSD, "Export error for %s (%s): %s", p_id_.GetText(), id_->name, err_str.c_str()); } Py_DECREF(module); PyGILState_Release(gstate); - mtlx_path_ = pxr::SdfAssetPath(path, path); + mtlx_path_ = pxr::SdfAssetPath(file_path, file_path); ID_LOG(2, "mtlx=%s", mtlx_path_.GetResolvedPath().c_str()); } diff --git a/source/blender/render/hydra/utils.cc b/source/blender/render/hydra/utils.cc index 8d37a14ef7ac..25ec32c807bf 100644 --- a/source/blender/render/hydra/utils.cc +++ b/source/blender/render/hydra/utils.cc @@ -99,4 +99,11 @@ std::string cache_image(Main *bmain, return tempfile; } +std::string temp_file_path(std::string const &file_name) +{ + char tempfile[FILE_MAX]; + BLI_path_join(tempfile, sizeof(tempfile), BKE_tempdir_session(), "render_hydra", file_name.c_str()); + return tempfile; +} + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/utils.h b/source/blender/render/hydra/utils.h index 05d7c85fc610..90b06eb10cad 100644 --- a/source/blender/render/hydra/utils.h +++ b/source/blender/render/hydra/utils.h @@ -21,5 +21,6 @@ std::string cache_image(Main *bmain, ImageUser *iuser, ImageSaveOptions *opts, ReportList *reports); +std::string temp_file_path(std::string const &file_name); } // namespace blender::render::hydra -- 2.30.2 From 0b701aa059d25354eeb4fd6bbcbfccd451907832 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 20 Apr 2023 18:53:30 +0300 Subject: [PATCH 2/6] Polishing --- source/blender/render/hydra/scene_delegate/material.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 3f640452fe43..f51a31746fa1 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -81,14 +81,16 @@ void MaterialData::init() } else { file_path = ""; - std::string err_str; - /* Clearing exception data */ - PyObject *type, *value, *traceback; + /* Clearing and logging exception data */ + PyObject *type, *value, *traceback, *pstr; PyErr_Fetch(&type, &value, &traceback); + PyErr_NormalizeException(&type, &value, &traceback); + std::string err_str = ((PyTypeObject *)type)->tp_name; if (value) { PyObject *pstr = PyObject_Str(value); - err_str = PyUnicode_AsUTF8(pstr); + err_str += ": "; + err_str += PyUnicode_AsUTF8(pstr); Py_DECREF(pstr); } Py_XDECREF(traceback); -- 2.30.2 From 1103c352c5fe9466e4623e9cce069c18fae19c2b Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 20 Apr 2023 19:21:46 +0300 Subject: [PATCH 3/6] Reverted export_mtlx() to return string. Adjusted code --- scripts/modules/bpy_hydra.py | 11 ++++----- .../render/hydra/scene_delegate/material.cc | 23 ++++++++----------- source/blender/render/hydra/utils.cc | 7 ------ source/blender/render/hydra/utils.h | 1 - 4 files changed, 14 insertions(+), 28 deletions(-) diff --git a/scripts/modules/bpy_hydra.py b/scripts/modules/bpy_hydra.py index 5b1c9a891871..a702aeed286f 100644 --- a/scripts/modules/bpy_hydra.py +++ b/scripts/modules/bpy_hydra.py @@ -100,19 +100,18 @@ class HydraRenderEngine(bpy.types.RenderEngine): _bpy_hydra.engine_view_draw(self.engine_ptr, depsgraph.as_pointer(), context.as_pointer()) -def export_mtlx(material, mtlx_file): +def export_mtlx(material): """ Exports material to .mtlx file. It is called from Blender source code. """ try: import materialx.utils as mx_utils except ImportError: print("ERROR: no MaterialX addon available") - return False + return "" - raise TypeError("asasfas") - doc = mx_utils.export(material, None) if not doc: - return False + return "" + mtlx_file = mx_utils.get_temp_file(".mtlx", f"mat_{material.as_pointer():016x}") mx_utils.export_to_file(doc, mtlx_file, export_deps=True, copy_deps=False) - return True + return str(mtlx_file) diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index f51a31746fa1..76705aa2e1a1 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -17,7 +17,6 @@ #include "blender_scene_delegate.h" #include "material.h" #include "mtlx_hydra_adapter.h" -#include "../utils.h" namespace blender::render::hydra { @@ -66,24 +65,19 @@ void MaterialData::init() RNA_pointer_create(NULL, &RNA_Material, id_, &materialptr); PyObject *material = pyrna_struct_CreatePyObject(&materialptr); - char file_name[32]; - snprintf(file_name, 32, "mat_%016llx.mtlx", (uintptr_t)id_); - std::string file_path = temp_file_path(file_name); - result = PyObject_CallFunction(func, "Os", material, file_path.c_str()); + result = PyObject_CallFunction(func, "O", material); Py_DECREF(material); - if (!PyErr_Occurred()) { - if (!PyObject_IsTrue(result)) { - file_path = ""; - } + std::string path; + + if (!PyErr_Occurred()) { + path = PyUnicode_AsUTF8(result); Py_DECREF(result); } else { - file_path = ""; - /* Clearing and logging exception data */ - PyObject *type, *value, *traceback, *pstr; + PyObject *type, *value, *traceback; PyErr_Fetch(&type, &value, &traceback); PyErr_NormalizeException(&type, &value, &traceback); std::string err_str = ((PyTypeObject *)type)->tp_name; @@ -97,13 +91,14 @@ void MaterialData::init() Py_XDECREF(value); Py_DECREF(type); - CLOG_ERROR(LOG_BSD, "Export error for %s (%s): %s", p_id_.GetText(), id_->name, err_str.c_str()); + CLOG_ERROR( + LOG_BSD, "Export error for %s (%s): %s", p_id_.GetText(), id_->name, err_str.c_str()); } Py_DECREF(module); PyGILState_Release(gstate); - mtlx_path_ = pxr::SdfAssetPath(file_path, file_path); + mtlx_path_ = pxr::SdfAssetPath(path, path); ID_LOG(2, "mtlx=%s", mtlx_path_.GetResolvedPath().c_str()); } diff --git a/source/blender/render/hydra/utils.cc b/source/blender/render/hydra/utils.cc index 25ec32c807bf..8d37a14ef7ac 100644 --- a/source/blender/render/hydra/utils.cc +++ b/source/blender/render/hydra/utils.cc @@ -99,11 +99,4 @@ std::string cache_image(Main *bmain, return tempfile; } -std::string temp_file_path(std::string const &file_name) -{ - char tempfile[FILE_MAX]; - BLI_path_join(tempfile, sizeof(tempfile), BKE_tempdir_session(), "render_hydra", file_name.c_str()); - return tempfile; -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/utils.h b/source/blender/render/hydra/utils.h index 90b06eb10cad..05d7c85fc610 100644 --- a/source/blender/render/hydra/utils.h +++ b/source/blender/render/hydra/utils.h @@ -21,6 +21,5 @@ std::string cache_image(Main *bmain, ImageUser *iuser, ImageSaveOptions *opts, ReportList *reports); -std::string temp_file_path(std::string const &file_name); } // namespace blender::render::hydra -- 2.30.2 From a78074156c25e3c2afd36d259e5e7c32ad1e4c6a Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 20 Apr 2023 19:35:12 +0300 Subject: [PATCH 4/6] removed traceback --- scripts/modules/bpy_hydra.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/modules/bpy_hydra.py b/scripts/modules/bpy_hydra.py index a702aeed286f..21ad5e33d7c9 100644 --- a/scripts/modules/bpy_hydra.py +++ b/scripts/modules/bpy_hydra.py @@ -37,7 +37,6 @@ __all__ = ( ) import os -import traceback from pathlib import Path import bpy -- 2.30.2 From af70cf4253a40950c77d69a517eaebc3647fc27c Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 21 Apr 2023 15:50:48 +0300 Subject: [PATCH 5/6] Added printing traceback --- source/blender/render/hydra/scene_delegate/material.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index dd947a147998..5a064437a763 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -79,12 +79,15 @@ void MaterialData::init() err_str += PyUnicode_AsUTF8(pstr); Py_DECREF(pstr); } + CLOG_ERROR( + LOG_BSD, "Export error for %s (%s): %s", prim_id.GetText(), id->name, err_str.c_str()); + if (traceback) { + PyTraceBack_Print(traceback, PySys_GetObject("stderr")); + } Py_XDECREF(traceback); Py_XDECREF(value); Py_DECREF(type); - CLOG_ERROR( - LOG_BSD, "Export error for %s (%s): %s", prim_id.GetText(), id->name, err_str.c_str()); } Py_DECREF(module); -- 2.30.2 From 59aeea175c8876a4de9e995f7d9a87a4d947af61 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 21 Apr 2023 16:09:35 +0300 Subject: [PATCH 6/6] make format --- source/blender/render/hydra/scene_delegate/material.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index f22a590f29ac..380d9826d352 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -79,15 +79,17 @@ void MaterialData::init() err_str += PyUnicode_AsUTF8(pstr); Py_DECREF(pstr); } - CLOG_ERROR( - LOG_RENDER_HYDRA_SCENE, "Export error for %s (%s): %s", prim_id.GetText(), id->name, err_str.c_str()); + CLOG_ERROR(LOG_RENDER_HYDRA_SCENE, + "Export error for %s (%s): %s", + prim_id.GetText(), + id->name, + err_str.c_str()); if (traceback) { - PyTraceBack_Print(traceback, PySys_GetObject("stderr")); + PyTraceBack_Print(traceback, PySys_GetObject("stderr")); } Py_XDECREF(traceback); Py_XDECREF(value); Py_DECREF(type); - } Py_DECREF(module); -- 2.30.2