From 64d7592a5f06d5b390958a5e83f10549ec3d88ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Delgado=20Kr=C3=A4mer?= Date: Thu, 18 Apr 2024 23:32:33 +0200 Subject: [PATCH] Add dynamic 'emitMtlx' Sdf file format argument --- src/libguc/plugInfo.json.in | 9 +++++++++ src/libguc/src/fileFormat.cpp | 36 +++++++++++++++++++++++++++++++---- src/libguc/src/fileFormat.h | 22 +++++++++++++++++++-- 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/libguc/plugInfo.json.in b/src/libguc/plugInfo.json.in index fad79e4..a5324cd 100644 --- a/src/libguc/plugInfo.json.in +++ b/src/libguc/plugInfo.json.in @@ -16,6 +16,15 @@ "primary": true, "target": "usd" } + }, + "SdfMetadata": { + "emitMtlx": { + "type": "bool", + "default": "false", + "displayGroup": "Core", + "appliesTo": [ "layers" ], + "documentation:": "Emit MaterialX materials in addition to UsdPreviewSurfaces." + } } }, "LibraryPath": "@PLUG_INFO_LIBRARY_PATH@", diff --git a/src/libguc/src/fileFormat.cpp b/src/libguc/src/fileFormat.cpp index eaba639..7e5c3e3 100644 --- a/src/libguc/src/fileFormat.cpp +++ b/src/libguc/src/fileFormat.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include @@ -41,6 +42,7 @@ TF_DEFINE_PRIVATE_TOKENS( _tokens, (gltf) (glb) + (emitMtlx) ); TF_REGISTRY_FUNCTION(TfType) @@ -48,9 +50,6 @@ TF_REGISTRY_FUNCTION(TfType) SDF_DEFINE_FILE_FORMAT(UsdGlTFFileFormat, SdfFileFormat); } -TF_DEFINE_ENV_SETTING(USDGLTF_ENABLE_MTLX, false, - "Whether the UsdGlTF Sdf plugin emits MaterialX materials or not.") - // glTF files can contain embedded images. In order to support them in our Sdf file // format plugin, we create a temporary directory for each glTF file, write the images // to it, and reference them. Afterwards, this directory gets deleted, however I was @@ -94,6 +93,19 @@ UsdGlTFFileFormat::~UsdGlTFFileFormat() { } +SdfAbstractDataRefPtr UsdGlTFFileFormat::InitData(const FileFormatArguments& args) const +{ + UsdGlTFDataRefPtr data(new UsdGlTFData()); + + auto emitMtlxIt = args.find(_tokens->emitMtlx.GetText()); + if (emitMtlxIt != args.end()) + { + data->emitMtlx = (emitMtlxIt->second == "true"); + } + + return data; +} + bool UsdGlTFFileFormat::CanRead(const std::string& filePath) const { // FIXME: implement? In my tests, this is not even called. @@ -111,13 +123,16 @@ bool UsdGlTFFileFormat::Read(SdfLayer* layer, return false; } + SdfAbstractDataRefPtr layerData = InitData(layer->GetFileFormatArguments()); + UsdGlTFDataConstPtr data = TfDynamic_cast(layerData); + Converter::Params params = {}; params.srcDir = fs::path(resolvedPath).parent_path(); params.dstDir = s_tmpDirHolder.makeDir(); params.mtlxFileName = ""; // Not needed because of Mtlx-as-UsdShade option params.copyExistingFiles = false; params.genRelativePaths = false; - params.emitMtlx = TfGetEnvSetting(USDGLTF_ENABLE_MTLX); + params.emitMtlx = data->emitMtlx; params.mtlxAsUsdShade = true; params.explicitColorspaceTransforms = false; params.gltfPbrImpl = Converter::GltfPbrImpl::Runtime; @@ -144,6 +159,7 @@ bool UsdGlTFFileFormat::ReadFromString(SdfLayer* layer, { // glTF files often reference other files (e.g. a .bin payload or images). // Hence, without a file location, most glTF files can not be loaded correctly. + // TODO: but we could still try and return false on failure return false; } @@ -165,4 +181,16 @@ bool UsdGlTFFileFormat::WriteToStream(const SdfSpecHandle &spec, return usdcFormat->WriteToStream(spec, out, indent); } +void UsdGlTFFileFormat::ComposeFieldsForFileFormatArguments(const std::string& assetPath, + const PcpDynamicFileFormatContext& context, + FileFormatArguments* args, + VtValue *dependencyContextData) const +{ + VtValue emitMtlxValue; + if (context.ComposeValue(_tokens->emitMtlx, &emitMtlxValue)) + { + (*args)[_tokens->emitMtlx] = TfStringify(emitMtlxValue); + } +} + PXR_NAMESPACE_CLOSE_SCOPE diff --git a/src/libguc/src/fileFormat.h b/src/libguc/src/fileFormat.h index 3c1ad48..19667cc 100644 --- a/src/libguc/src/fileFormat.h +++ b/src/libguc/src/fileFormat.h @@ -17,8 +17,10 @@ #pragma once #include +#include #include #include +#include #include #include @@ -32,9 +34,11 @@ PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_PUBLIC_TOKENS(UsdGlTFFileFormatTokens, USDGLTF_FILE_FORMAT_TOKENS); TF_DECLARE_WEAK_AND_REF_PTRS(UsdGlTFFileFormat); +TF_DECLARE_WEAK_AND_REF_PTRS(UsdGlTFData); -class UsdGlTFFileFormat : public SdfFileFormat { -public: +class UsdGlTFFileFormat : public SdfFileFormat, public PcpDynamicFileFormatInterface +{ +protected: SDF_FILE_FORMAT_FACTORY_ACCESS; UsdGlTFFileFormat(); @@ -42,6 +46,8 @@ class UsdGlTFFileFormat : public SdfFileFormat { virtual ~UsdGlTFFileFormat(); public: + SdfAbstractDataRefPtr InitData(const FileFormatArguments& args) const override; + bool CanRead(const std::string &file) const override; bool Read(SdfLayer* layer, @@ -59,6 +65,18 @@ class UsdGlTFFileFormat : public SdfFileFormat { bool WriteToStream(const SdfSpecHandle &spec, std::ostream& out, size_t indent) const override; + +public: + void ComposeFieldsForFileFormatArguments(const std::string& assetPath, + const PcpDynamicFileFormatContext& context, + FileFormatArguments* args, + VtValue *dependencyContextData) const override; +}; + +class UsdGlTFData : public SdfData +{ +public: + bool emitMtlx = false; }; PXR_NAMESPACE_CLOSE_SCOPE