Skip to content

Commit

Permalink
Allow runtime adjustment of MaterialX Search Paths.
Browse files Browse the repository at this point in the history
UsdMtlx uses the two environment variables PXR_USDMTLX_STDLIB_SEARCH_PATHS and
PXR_USDMTLX_PUGIN_SEARCH_PATHS for the user to specify where to find the necessary
MaterialX files. The imaging side does not do this. This change adds that behavior to HdMtlx.
So now, if the location of the MaterialX provided libraries folder changes from the location
originally found at runtime (stored in PXR_MATERIALX_STDLIB_DIR) the new location(s)
can be indicated with PXR_MTLX_STDLIB_SEARCH_PATHS. The location(s) to any custom
MaterialX files can be indicated with PXR_MTLX_PLUGIN_SEARCH_PATHS.

(Based on GitHub PR PixarAnimationStudios#1628 by @vlasovi)

Fixes PixarAnimationStudios#1586

(Internal change: 2197997)
  • Loading branch information
klucknav authored and lkerley committed Jan 7, 2022
1 parent 885c0f1 commit 3f23e46
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 16 deletions.
2 changes: 2 additions & 0 deletions pxr/imaging/hdMtlx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
set(PXR_PREFIX pxr/imaging)
set(PXR_PACKAGE hdMtlx)

add_definitions(-DPXR_MATERIALX_STDLIB_DIR="${MATERIALX_STDLIB_DIR}")

pxr_library(hdMtlx
LIBRARIES
gf
Expand Down
52 changes: 52 additions & 0 deletions pxr/imaging/hdMtlx/hdMtlx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@

#include "pxr/usd/sdf/path.h"
#include "pxr/usd/sdr/registry.h"

#include "pxr/base/arch/fileSystem.h"
#include "pxr/base/tf/diagnostic.h"
#include "pxr/base/tf/getenv.h"

#include <MaterialXCore/Document.h>
#include <MaterialXCore/Node.h>
Expand All @@ -41,6 +44,55 @@ namespace mx = MaterialX;

PXR_NAMESPACE_OPEN_SCOPE

static const NdrStringVec
_GetSearchPathsFromEnvVar(const char* name)
{
const std::string paths = TfGetenv(name);
return !paths.empty()
? TfStringSplit(paths, ARCH_PATH_LIST_SEP)
: NdrStringVec();
}

static mx::FileSearchPath
_ComputeSearchPaths()
{
mx::FileSearchPath searchPaths;
// Add any additional custom mtlx path(s)
// Note this is the same envar used in UsdMtlxDiscoveryPlugin. This is used
// to indicate the location of any additional custom mtlx files.
static const auto customSearchPaths =
_GetSearchPathsFromEnvVar("PXR_MTLX_PLUGIN_SEARCH_PATHS");
for (auto path : customSearchPaths) {
searchPaths.append(mx::FilePath(path));
}

// Add the MaterialX/libraries path(s)
// Note this is the same envar used in UsdMtlxStandardLibraryPaths()
// This is used to indicate the location of the MaterialX/libraries folder
// if moved/changed from the path initilialized in PXR_MATERIALX_STDLIB_DIR.
static const auto stdlibSearchPaths =
_GetSearchPathsFromEnvVar("PXR_MTLX_STDLIB_SEARCH_PATHS");
for (auto path : stdlibSearchPaths) {
searchPaths.append(mx::FilePath(path));
}

// Add path to the MaterialX standard library discovered at build time.
searchPaths.append(mx::FilePath(PXR_MATERIALX_STDLIB_DIR));

return searchPaths;
}

// Return the MaterialX search paths. In order, this includes:
// - Paths set in the environment variable 'PXR_MTLX_PLUGIN_SEARCH_PATHS'
// - Paths set in the environment variable 'PXR_MTLX_STDLIB_SEARCH_PATHS'
// - Path to the MaterialX standard library discovered at build time.
const mx::FileSearchPath &
HdMtlxSearchPaths()
{
static const mx::FileSearchPath searchPaths = _ComputeSearchPaths();
return searchPaths;
}

// Return the MaterialX Node Type based on the corresponding NodeDef name,
// which is stored as the hdNodeType.
static TfToken
Expand Down
9 changes: 9 additions & 0 deletions pxr/imaging/hdMtlx/hdMtlx.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <unordered_map>

namespace MaterialX {
class FileSearchPath;
using DocumentPtr = std::shared_ptr<class Document>;
using StringMap = std::unordered_map<std::string, std::string>;
}
Expand All @@ -42,6 +43,14 @@ class VtValue;
struct HdMaterialNetwork2;
struct HdMaterialNode2;

/// Return the MaterialX search paths. In order, this includes:
/// - Paths set in the environment variable 'PXR_MTLX_PLUGIN_SEARCH_PATHS'
/// - Paths set in the environment variable 'PXR_MTLX_STDLIB_SEARCH_PATHS'
/// - Path to the MaterialX standard library discovered at build time.
HDMTLX_API
const MaterialX::FileSearchPath&
HdMtlxSearchPaths();

/// Converts the HdParameterValue to a string MaterialX can understand
HDMTLX_API
std::string
Expand Down
3 changes: 0 additions & 3 deletions pxr/imaging/hdSt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ set(optionalIncludeDirs "")
set(optionalPublicClasses "")
set(optionalPrivateClasses "")
if (${PXR_ENABLE_MATERIALX_SUPPORT})
add_definitions(-DPXR_MATERIALX_STDLIB_DIR="${MATERIALX_STDLIB_DIR}")
add_definitions(-DPXR_MATERIALX_BASE_DIR="${MATERIALX_BASE_DIR}")

list(APPEND optionalLibs ${MATERIALX_LIBRARIES} hdMtlx)
list(APPEND optionalIncludeDirs ${MATERIALX_INCLUDE_DIRS})
list(APPEND optionalPrivateClasses
Expand Down
3 changes: 1 addition & 2 deletions pxr/imaging/hdSt/materialXFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,7 @@ HdSt_ApplyMaterialXFilter(

// Load Standard Libraries/setup SearchPaths (for mxDoc and mxShaderGen)
mx::FilePathVec libraryFolders;
mx::FileSearchPath searchPath;
searchPath.append(mx::FilePath(PXR_MATERIALX_STDLIB_DIR));
mx::FileSearchPath searchPath = HdMtlxSearchPaths();
mx::DocumentPtr stdLibraries = mx::createDocument();
mx::loadLibraries(libraryFolders, searchPath, stdLibraries);

Expand Down
4 changes: 3 additions & 1 deletion pxr/usd/plugin/usdMtlx/discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ class UsdMtlxDiscoveryPlugin : public NdrDiscoveryPlugin {

UsdMtlxDiscoveryPlugin::UsdMtlxDiscoveryPlugin()
{
// Note this is the same envar used in HdMtlxSearchPaths(). This is used to
// indicate the location of any additional custom mtlx files.
static const auto searchPaths =
UsdMtlxGetSearchPathsFromEnvVar("PXR_USDMTLX_PLUGIN_SEARCH_PATHS");
UsdMtlxGetSearchPathsFromEnvVar("PXR_MTLX_PLUGIN_SEARCH_PATHS");

_searchPaths = searchPaths;
_allSearchPaths =
Expand Down
2 changes: 1 addition & 1 deletion pxr/usd/plugin/usdMtlx/testenv/testUsdMtlxDiscovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# language governing permissions and limitations under the Apache License.

import os
os.environ['PXR_USDMTLX_PLUGIN_SEARCH_PATHS'] = os.getcwd()
os.environ['PXR_MTLX_PLUGIN_SEARCH_PATHS'] = os.getcwd()

from pxr import Tf, Ndr, Sdr
import unittest
Expand Down
2 changes: 1 addition & 1 deletion pxr/usd/plugin/usdMtlx/testenv/testUsdMtlxParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# language governing permissions and limitations under the Apache License.

import os
os.environ['PXR_USDMTLX_STDLIB_SEARCH_PATHS'] = os.getcwd()
os.environ['PXR_MTLX_STDLIB_SEARCH_PATHS'] = os.getcwd()

from pxr import Tf, Sdr
import unittest
Expand Down
5 changes: 4 additions & 1 deletion pxr/usd/plugin/usdMtlx/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ UsdMtlxStandardLibraryPaths()
{
static const auto materialxLibraryPaths =
UsdMtlxMergeSearchPaths(
UsdMtlxGetSearchPathsFromEnvVar("PXR_USDMTLX_STDLIB_SEARCH_PATHS"),
// Note this is the same envar used in HdMtlxSearchPaths()
// This is used to indicate the location of the MaterialX/libraries
// folder if moved/changed from the path in PXR_MATERIALX_STDLIB_DIR
UsdMtlxGetSearchPathsFromEnvVar("PXR_MTLX_STDLIB_SEARCH_PATHS"),
NdrStringVec{
#ifdef PXR_MATERIALX_STDLIB_DIR
PXR_MATERIALX_STDLIB_DIR
Expand Down
3 changes: 0 additions & 3 deletions third_party/renderman-24/plugin/hdPrman/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ endif()
set(optionalIncludeDirs "")
set(optionalPublicClasses "")
if (${PXR_ENABLE_MATERIALX_SUPPORT})
add_definitions(-DPXR_MATERIALX_STDLIB_DIR="${MATERIALX_STDLIB_DIR}")
add_definitions(-DPXR_MATERIALX_BASE_DIR="${MATERIALX_BASE_DIR}")

list(APPEND optionalLibs ${MATERIALX_LIBRARIES} hdMtlx)
list(APPEND optionalIncludeDirs ${MATERIALX_INCLUDE_DIRS})
list(APPEND optionalPublicClasses matfiltMaterialX)
Expand Down
6 changes: 2 additions & 4 deletions third_party/renderman-24/plugin/hdPrman/matfiltMaterialX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,8 @@ MatfiltMaterialX(
if (!terminalNode->inputConnections.empty()) {

// Load Standard Libraries/setup SearchPaths (for mxDoc and mxShaderGen)
mx::FilePathVec libraryFolders = { "libraries", };
mx::FileSearchPath searchPath;
searchPath.append(mx::FilePath(PXR_MATERIALX_STDLIB_DIR));
searchPath.append(mx::FilePath(PXR_MATERIALX_BASE_DIR));
mx::FilePathVec libraryFolders;
mx::FileSearchPath searchPath = HdMtlxSearchPaths();
mx::DocumentPtr stdLibraries = mx::createDocument();
mx::loadLibraries(libraryFolders, searchPath, stdLibraries);

Expand Down

0 comments on commit 3f23e46

Please sign in to comment.