From ca2b63a56718ecdb1a42fe346926fdd8c12d9f9b Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 1 Aug 2023 16:59:43 +0300 Subject: [PATCH 1/2] Moved check of existing MaterialX addon from bpy_hydra.py to material.cc and log warning only once. --- scripts/modules/bpy_hydra.py | 6 +----- source/blender/io/usd/hydra/material.cc | 27 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/scripts/modules/bpy_hydra.py b/scripts/modules/bpy_hydra.py index 56fe6664f165..81111da06a96 100644 --- a/scripts/modules/bpy_hydra.py +++ b/scripts/modules/bpy_hydra.py @@ -6,11 +6,7 @@ __all__ = ( 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 "" + import materialx.utils as mx_utils doc = mx_utils.export(material, None) if not doc: diff --git a/source/blender/io/usd/hydra/material.cc b/source/blender/io/usd/hydra/material.cc index 232b0373fab5..00833d05773f 100644 --- a/source/blender/io/usd/hydra/material.cc +++ b/source/blender/io/usd/hydra/material.cc @@ -113,9 +113,32 @@ pxr::HdCullStyle MaterialData::cull_style() const void MaterialData::export_mtlx() { - /* Call of python function hydra.export_mtlx() */ - PyGILState_STATE gstate; + + /* Checking materialx addon */ + static bool matx_addon_checked = false; + static bool has_matx_addon = false; + + if (!matx_addon_checked) { + gstate = PyGILState_Ensure(); + + PyObject *mx_module = PyImport_ImportModule("materialx"); + has_matx_addon = mx_module != nullptr; + Py_XDECREF(mx_module); + + if (!has_matx_addon) { + PyErr_Print(); + CLOG_WARN(LOG_RENDER_HYDRA_SCENE, "No MaterialX addon, materials won't be exported."); + } + PyGILState_Release(gstate); + matx_addon_checked = true; + } + + if (!has_matx_addon) { + return; + } + + /* Call of python function bpy_hydra.export_mtlx() */ gstate = PyGILState_Ensure(); PyObject *module, *dict, *func, *result; -- 2.30.2 From 5d03bf65baedf0083066468dd580608fcc236bab Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 2 Aug 2023 03:21:46 +0300 Subject: [PATCH 2/2] Adding second check into the lock --- source/blender/io/usd/hydra/material.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/source/blender/io/usd/hydra/material.cc b/source/blender/io/usd/hydra/material.cc index 7f73f7ba6676..455241a39c0c 100644 --- a/source/blender/io/usd/hydra/material.cc +++ b/source/blender/io/usd/hydra/material.cc @@ -121,17 +121,19 @@ void MaterialData::export_mtlx() if (!matx_addon_checked) { gstate = PyGILState_Ensure(); + /* Adding second check into the lock, good practice to make this fully correct */ + if (!matx_addon_checked) { + PyObject *mx_module = PyImport_ImportModule("materialx"); + has_matx_addon = mx_module != nullptr; + Py_XDECREF(mx_module); - PyObject *mx_module = PyImport_ImportModule("materialx"); - has_matx_addon = mx_module != nullptr; - Py_XDECREF(mx_module); - - if (!has_matx_addon) { - PyErr_Print(); - CLOG_WARN(LOG_RENDER_HYDRA_SCENE, "No MaterialX addon, materials won't be exported."); + if (!has_matx_addon) { + PyErr_Print(); + CLOG_WARN(LOG_HYDRA_SCENE, "No MaterialX addon, materials won't be exported."); + } + matx_addon_checked = true; } PyGILState_Release(gstate); - matx_addon_checked = true; } if (!has_matx_addon) { -- 2.30.2