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())
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")
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 "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());
}

View File

@ -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

View File

@ -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