Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ option(MATERIALX_BUILD_OCIO "Build OpenColorIO support for shader generators." O
option(MATERIALX_BUILD_TESTS "Build unit tests." OFF)
option(MATERIALX_BUILD_BENCHMARK_TESTS "Build benchmark tests." OFF)
option(MATERIALX_BUILD_OSOS "Build OSL .oso's of standard library shaders for the OSL Network generator" OFF)
option(MATERIALX_BUILD_NODEGRAPH_OSOS "Build NodeGraphs as OSOs, if disabled then NodeGraphs are not compiled as OSOs " OFF)

option(MATERIALX_BUILD_SHARED_LIBS "Build MaterialX libraries as shared rather than static." OFF)
option(MATERIALX_BUILD_DATA_LIBRARY "Build generated products from the MaterialX data library." OFF)
Expand Down
7 changes: 6 additions & 1 deletion libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ if(MATERIALX_BUILD_DATA_LIBRARY)

set(SENTINEL_FILE ${CMAKE_CURRENT_BINARY_DIR}/buildosos.sentinel)

set(SKIP_BUILDING_NODEGRAPHS "")
if (NOT MATERIALX_BUILD_NODEGRAPH_OSOS)
set(SKIP_BUILDING_NODEGRAPHS "--skipConvertingNodegraphs")
endif()

add_custom_command(
OUTPUT ${SENTINEL_FILE}
COMMAND touch ${SENTINEL_FILE}
Expand All @@ -46,7 +51,7 @@ if(MATERIALX_BUILD_DATA_LIBRARY)
--oslCompilerPath ${MATERIALX_OSL_BINARY_OSLC}
--oslIncludePath ${MATERIALX_OSL_INCLUDE_PATH}
--libraryRelativeOsoPath libraries/targets/genoslnetwork/osos
--removeNdPrefix true
${SKIP_BUILDING_NODEGRAPHS}
DEPENDS ${MATERIALX_DATA_LIBRARY_SOURCE_FILES} MaterialXGenOsl_LibsToOso
)

Expand Down
1 change: 1 addition & 0 deletions resources/Materials/TestSuite/_options.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

<!-- List of document paths for render tests -->
<input name="renderTestPaths" type="string" value="resources/Materials/Examples/StandardSurface,resources/Materials/TestSuite/stdlib/convolution,resources/Materials/TestSuite/stdlib/color_management,resources/Materials/TestSuite/stdlib/procedural,resources/Materials/TestSuite/pbrlib/surfaceshader,resources/Materials/TestSuite/nprlib,resources/Materials/TestSuite/pbrlib/bsdf" />
<!-- <input name="renderTestPaths" type="string" value="resources/Materials/Examples,resources/Materials/TestSuite" />-->

<!-- Enable reference quality rendering.
This option enables higher sample counts and supersampling in render tests,
Expand Down
62 changes: 61 additions & 1 deletion source/MaterialXFormat/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <cctype>
#include <cerrno>
#include <cstring>
#include <random>
#include <filesystem>

MATERIALX_NAMESPACE_BEGIN

Expand Down Expand Up @@ -278,8 +280,12 @@ FilePathVec FilePath::getSubDirectories() const
return dirs;
}

void FilePath::createDirectory() const
void FilePath::createDirectory(bool recursive) const
{
if (recursive && !getParentPath().isDirectory())
{
getParentPath().createDirectory(recursive);
}
#if defined(_WIN32)
_mkdir(asString().c_str());
#else
Expand Down Expand Up @@ -377,4 +383,58 @@ FileSearchPath getEnvironmentPath(const string& sep)
return FileSearchPath(searchPathEnv, sep);
}

FilePath FilePath::createTemporaryDirectory(const FilePath& parentDir)
{
// Get the parent directory - use system temp if empty
FilePath tempParent = parentDir;
if (tempParent.isEmpty())
{
tempParent = getSystemTemporaryDirectory();
}

// Ensure parent directory exists
if (!tempParent.exists())
{
tempParent.createDirectory(true);
}

if (!tempParent.isDirectory())
{
throw Exception("Parent path is not a directory: " + tempParent.asString());
}

// Generate a unique temporary directory name using std::filesystem
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(100000, 999999);

constexpr int maxAttempts = 1000;
for (int attempt = 0; attempt < maxAttempts; ++attempt)
{
string tempDirName = "materialx_tmp_" + std::to_string(dis(gen));
FilePath tempDirPath = tempParent / tempDirName;

if (!tempDirPath.isDirectory())
{
tempDirPath.createDirectory(true);
return tempDirPath;
}
}

throw Exception("Failed to create temporary directory after " + std::to_string(maxAttempts) + " attempts");
}

FilePath FilePath::getSystemTemporaryDirectory()
{
try
{
std::filesystem::path tempPath = std::filesystem::temp_directory_path();
return FilePath(tempPath.string());
}
catch (const std::filesystem::filesystem_error& ex)
{
throw Exception("Error getting system temporary directory: " + string(ex.what()));
}
}

MATERIALX_NAMESPACE_END
18 changes: 17 additions & 1 deletion source/MaterialXFormat/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ class MX_FORMAT_API FilePath
FilePathVec getSubDirectories() const;

/// Create a directory on the file system at the given path.
void createDirectory() const;
/// @param recursive If true, creates parent directories as needed.
/// If false (default), only creates the final directory.
/// @note Succeeds silently if directory already exists. Does not return
/// error status - use exists() or isDirectory() to verify success.
void createDirectory(bool recursive=false) const;

/// Set the current working directory of the file system.
bool setCurrentPath();
Expand All @@ -208,6 +212,18 @@ class MX_FORMAT_API FilePath
/// Return the directory containing the executable module.
static FilePath getModulePath();

/// Create a temporary directory with a unique name.
/// @param parentDir The parent directory for the temporary directory.
/// If empty, uses the system's default temporary directory.
/// @return A FilePath to the created temporary directory.
/// @throws Exception if the temporary directory cannot be created.
static FilePath createTemporaryDirectory(const FilePath& parentDir = FilePath());

/// Return the system's default temporary directory.
/// @return A FilePath to the system temporary directory.
/// @throws Exception if the system temporary directory cannot be determined.
static FilePath getSystemTemporaryDirectory();

private:
StringVec _vec;
Type _type;
Expand Down
7 changes: 7 additions & 0 deletions source/MaterialXFormat/XmlIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ void writeToXmlStream(DocumentPtr doc, std::ostream& stream, const XmlWriteOptio

void writeToXmlFile(DocumentPtr doc, const FilePath& filename, const XmlWriteOptions* writeOptions)
{
if (writeOptions && writeOptions->createDirectories)
{
if (!filename.getParentPath().isDirectory())
{
filename.getParentPath().createDirectory(true);
}
}
std::ofstream ofs(filename.asString());
writeToXmlStream(doc, ofs, writeOptions);
}
Expand Down
4 changes: 4 additions & 0 deletions source/MaterialXFormat/XmlIo.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class MX_FORMAT_API XmlWriteOptions
/// If provided, this function will be used to exclude specific elements
/// (those returning false) from the write operation. Defaults to nullptr.
ElementPredicate elementPredicate;

/// If true, any necessary directories will be created to write the
/// file.
bool createDirectories = false;
};

/// @class ExceptionParseError
Expand Down
14 changes: 14 additions & 0 deletions source/MaterialXGenOsl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ mx_add_library(MaterialXGenOsl
EXPORT_DEFINE
MATERIALX_GENOSL_EXPORTS)

if (OSL_FOUND)
target_link_libraries(${TARGET_NAME}
PRIVATE
OSL::oslcomp)
target_compile_definitions(${TARGET_NAME}
PRIVATE
USE_OSLCOMP)
endif()

target_compile_definitions(${TARGET_NAME} PRIVATE
MATERIALX_OSL_BINARY_OSLC=\"${MATERIALX_OSL_BINARY_OSLC}\"
MATERIALX_OSL_INCLUDE_PATH=\"${MATERIALX_OSL_INCLUDE_PATH}\"
)

if (MATERIALX_BUILD_OSOS)
file(GLOB GenNodes_SRC "${CMAKE_CURRENT_SOURCE_DIR}/LibsToOso.cpp")

Expand Down
Loading