Move bpy_hydra.export_mtlx() exception handling to C code #28

Merged
Bogdan Nagirniak merged 9 commits from BLEN-392_pyerr_catch into hydra-render 2023-05-03 21:15:52 +02:00
4 changed files with 43 additions and 23 deletions
Showing only changes of commit 482d889e00 - Show all commits

View File

@ -100,25 +100,19 @@ class HydraRenderEngine(bpy.types.RenderEngine):
_bpy_hydra.engine_view_draw(self.engine_ptr, depsgraph.as_pointer(), context.as_pointer()) _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. """ """ Exports material to .mtlx file. It is called from Blender source code. """
try: try:
import materialx.utils as mx_utils 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: except ImportError:
print("ERROR: no MaterialX addon available") print("ERROR: no MaterialX addon available")
return False
except: raise TypeError("asasfas")
# 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()
return "" doc = mx_utils.export(material, None)
if not doc:
return False
mx_utils.export_to_file(doc, mtlx_file, export_deps=True, copy_deps=False)
return True

View File

@ -17,6 +17,7 @@
#include "blender_scene_delegate.h" #include "blender_scene_delegate.h"
#include "material.h" #include "material.h"
#include "mtlx_hydra_adapter.h" #include "mtlx_hydra_adapter.h"
#include "../utils.h"
namespace blender::render::hydra { namespace blender::render::hydra {
@ -65,25 +66,42 @@ void MaterialData::init()
RNA_pointer_create(NULL, &RNA_Material, id_, &materialptr); RNA_pointer_create(NULL, &RNA_Material, id_, &materialptr);
PyObject *material = pyrna_struct_CreatePyObject(&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); Py_DECREF(material);
std::string path; if (!PyErr_Occurred()) {
if (!PyObject_IsTrue(result)) {
if (!PyErr_Occurred()) { file_path = "";
path = PyUnicode_AsUTF8(result); }
Py_DECREF(result); Py_DECREF(result);
} }
else { else {
CLOG_ERROR(LOG_BSD, "Export error for %s", id_->name); file_path = "";
PyErr_Print(); 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); Py_DECREF(module);
PyGILState_Release(gstate); 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()); ID_LOG(2, "mtlx=%s", mtlx_path_.GetResolvedPath().c_str());
} }

View File

@ -99,4 +99,11 @@ std::string cache_image(Main *bmain,
return tempfile; 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 } // namespace blender::render::hydra

View File

@ -21,5 +21,6 @@ std::string cache_image(Main *bmain,
ImageUser *iuser, ImageUser *iuser,
ImageSaveOptions *opts, ImageSaveOptions *opts,
ReportList *reports); ReportList *reports);
std::string temp_file_path(std::string const &file_name);
} // namespace blender::render::hydra } // namespace blender::render::hydra