BLEN-356: Automatically configure PXR_MTLX_STDLIB_SEARCH_PATHS #9

Merged
Bogdan Nagirniak merged 4 commits from blen-356-pxr_mtlx_stdlib_search_paths into hydra-render 2023-03-06 17:29:17 +01:00
7 changed files with 42 additions and 58 deletions
Showing only changes of commit 4f8a0f4935 - Show all commits

View File

@ -397,11 +397,6 @@ void USD_ensure_plugin_path_registered()
blender::io::usd::ensure_usd_plugin_path_registered(); blender::io::usd::ensure_usd_plugin_path_registered();
} }
void USD_setup_usd_mtlx_environment()
{
blender::io::usd::setup_usd_mtlx_environment();
}
bool USD_import(struct bContext *C, bool USD_import(struct bContext *C,
const char *filepath, const char *filepath,
const USDImportParams *params, const USDImportParams *params,

View File

@ -5,11 +5,6 @@
#include <pxr/base/plug/registry.h> #include <pxr/base/plug/registry.h>
#include "BKE_appdir.h"
#include "BLI_string.h"
#include "BLI_fileops.h"
#include "BLI_path_util.h"
namespace blender::io::usd { namespace blender::io::usd {
void ensure_usd_plugin_path_registered() void ensure_usd_plugin_path_registered()
@ -35,43 +30,4 @@ void ensure_usd_plugin_path_registered()
#endif #endif
} }
void setup_usd_mtlx_environment()
{
/* PXR_MTLX_STDLIB_SEARCH_PATHS - path to the MaterialX standard library
* USD's MaterialX code expects to be set. */
static bool mtlx_env_set = false;
if (mtlx_env_set) {
return;
}
mtlx_env_set = true;
char stdlib_path[FILE_MAX] = "";
size_t stdlib_path_len = BLI_path_join(
stdlib_path, sizeof(stdlib_path), BKE_appdir_program_dir(), "materialx", "libraries");
if (stdlib_path_len == 0 || !BLI_exists(stdlib_path)) {
return;
}
#ifdef _WIN32
# define PATH_SEP ";"
#else
# define PATH_SEP ":"
#endif
const char *env_var_name = "PXR_MTLX_STDLIB_SEARCH_PATHS";
const char *env_var = BLI_getenv(env_var_name);
if (!env_var) {
BLI_setenv(env_var_name, stdlib_path);
}
else {
size_t env_var_len = strlen(env_var);
size_t combined_len = stdlib_path_len + 1 + env_var_len + 1;
char *combined = (char *)malloc(sizeof(char) * combined_len);
BLI_snprintf(combined, combined_len, "%s" PATH_SEP "%s", stdlib_path, env_var);
BLI_setenv(env_var_name, combined);
free(combined);
}
}
} // namespace blender::io::usd } // namespace blender::io::usd

View File

@ -5,6 +5,5 @@
namespace blender::io::usd { namespace blender::io::usd {
void ensure_usd_plugin_path_registered(); void ensure_usd_plugin_path_registered();
void setup_usd_mtlx_environment();
} // namespace blender::io::usd } // namespace blender::io::usd

View File

@ -138,7 +138,6 @@ struct CacheReader *CacheReader_open_usd_object(struct CacheArchiveHandle *handl
void USD_CacheReader_incref(struct CacheReader *reader); void USD_CacheReader_incref(struct CacheReader *reader);
void USD_CacheReader_free(struct CacheReader *reader); void USD_CacheReader_free(struct CacheReader *reader);
void USD_ensure_plugin_path_registered(void); void USD_ensure_plugin_path_registered(void);
void USD_setup_usd_mtlx_environment(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -13,6 +13,9 @@
#include "glog/logging.h" #include "glog/logging.h"
#include "BKE_appdir.h" #include "BKE_appdir.h"
#include "BLI_string.h"
#include "BLI_fileops.h"
#include "BLI_path_util.h"
#include "finalEngine.h" #include "finalEngine.h"
#include "viewportEngine.h" #include "viewportEngine.h"
@ -21,12 +24,51 @@ using namespace std;
namespace blender::render::hydra { namespace blender::render::hydra {
void setup_usd_mtlx_environment()
{
/* PXR_MTLX_STDLIB_SEARCH_PATHS - path to the MaterialX standard library
* USD's MaterialX code expects to be set. */
static bool mtlx_env_set = false;
if (mtlx_env_set) {
return;
}
mtlx_env_set = true;
char stdlib_path[FILE_MAX] = "";
size_t stdlib_path_len = BLI_path_join(
stdlib_path, sizeof(stdlib_path), BKE_appdir_program_dir(), "materialx", "libraries");
if (stdlib_path_len == 0 || !BLI_exists(stdlib_path)) {
return;
}
#ifdef _WIN32
# define PATH_SEP ";"
#else
# define PATH_SEP ":"
#endif
const char *env_var_name = "PXR_MTLX_STDLIB_SEARCH_PATHS";
const char *env_var = BLI_getenv(env_var_name);
if (!env_var) {
BLI_setenv(env_var_name, stdlib_path);
}
else {
size_t env_var_len = strlen(env_var);
std::string combined(stdlib_path_len + 1 + env_var_len, '\0');
BLI_snprintf(combined.data(), combined.capacity(), "%s" PATH_SEP "%s", stdlib_path, env_var);
BLI_setenv(env_var_name, combined.c_str());
}
}
static PyObject *init_func(PyObject * /*self*/, PyObject *args) static PyObject *init_func(PyObject * /*self*/, PyObject *args)
{ {
LOG(INFO) << "init_func"; LOG(INFO) << "init_func";
pxr::PlugRegistry::GetInstance().RegisterPlugins(string(BKE_appdir_program_dir()) + "/blender.shared/usd"); pxr::PlugRegistry::GetInstance().RegisterPlugins(string(BKE_appdir_program_dir()) + "/blender.shared/usd");
setup_usd_mtlx_environment();
Py_RETURN_NONE; Py_RETURN_NONE;
} }

View File

@ -1513,10 +1513,6 @@ if(WITH_USD)
absolute_include_dirs(../blender/io/usd) absolute_include_dirs(../blender/io/usd)
endif() endif()
if(WITH_MATERIALX)
add_definitions(-DWITH_MATERIALX)
endif()
# Always install USD shared library and datafiles regardless if Blender # Always install USD shared library and datafiles regardless if Blender
# itself uses them, the bundled Python module still needs it. # itself uses them, the bundled Python module still needs it.
if((DEFINED LIBDIR) AND TARGETDIR_LIB) if((DEFINED LIBDIR) AND TARGETDIR_LIB)

View File

@ -477,9 +477,6 @@ int main(int argc,
#ifdef WITH_USD #ifdef WITH_USD
USD_ensure_plugin_path_registered(); USD_ensure_plugin_path_registered();
# ifdef WITH_MATERIALX
USD_setup_usd_mtlx_environment();
# endif
#endif #endif
George-Shakula marked this conversation as resolved
Review

do we need #ifdef WITH_MATERIALX ?

do we need `#ifdef WITH_MATERIALX` ?
Review

It's not a requirement but it makes sense to wrap it into an #ifdef. Because if there is no mtlx there is also no usdMtlx

It's not a requirement but it makes sense to wrap it into an #ifdef. Because if there is no mtlx there is also no usdMtlx
#ifndef WITH_PYTHON_MODULE #ifndef WITH_PYTHON_MODULE