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
5 changed files with 50 additions and 0 deletions
Showing only changes of commit 0b91809204 - Show all commits

View File

@ -397,6 +397,11 @@ void USD_ensure_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,
const char *filepath,
const USDImportParams *params,

View File

@ -6,6 +6,9 @@
#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 {
@ -32,4 +35,43 @@ void ensure_usd_plugin_path_registered()
#endif
}
void setup_usd_mtlx_environment()
BogdanNagirniak marked this conversation as resolved
Review
Move this to https://projects.blender.org/BogdanNagirniak/blender/src/branch/hydra-render/source/blender/render/hydra/python.cc#L27
{
/* 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);
BogdanNagirniak marked this conversation as resolved
Review

Use more c++ approach, for example std::string

Use more c++ approach, for example std::string
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

View File

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

View File

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

View File

@ -477,6 +477,7 @@ int main(int argc,
#ifdef WITH_USD
USD_ensure_plugin_path_registered();
USD_setup_usd_mtlx_environment();
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
#endif
#ifndef WITH_PYTHON_MODULE