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 14 additions and 28 deletions
Showing only changes of commit 1103c352c5 - Show all commits

View File

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

View File

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

View File

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

View File

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