Skip to content

Commit

Permalink
Add dynamic 'emitMtlx' Sdf file format argument
Browse files Browse the repository at this point in the history
  • Loading branch information
pablode committed Apr 18, 2024
1 parent 49c3976 commit 64d7592
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/libguc/plugInfo.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -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@",
Expand Down
36 changes: 32 additions & 4 deletions src/libguc/src/fileFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <pxr/base/tf/registryManager.h>
#include <pxr/base/tf/envSetting.h>
#include <pxr/usd/usd/usdcFileFormat.h>
#include <pxr/usd/pcp/dynamicFileFormatContext.h>

#include <filesystem>

Expand All @@ -41,16 +42,14 @@ TF_DEFINE_PRIVATE_TOKENS(
_tokens,
(gltf)
(glb)
(emitMtlx)
);

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
Expand Down Expand Up @@ -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.
Expand All @@ -111,13 +123,16 @@ bool UsdGlTFFileFormat::Read(SdfLayer* layer,
return false;
}

SdfAbstractDataRefPtr layerData = InitData(layer->GetFileFormatArguments());
UsdGlTFDataConstPtr data = TfDynamic_cast<const UsdGlTFDataConstPtr>(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;
Expand All @@ -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;
}

Expand All @@ -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
22 changes: 20 additions & 2 deletions src/libguc/src/fileFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
#pragma once

#include <pxr/pxr.h>
#include <pxr/usd/sdf/data.h>
#include <pxr/usd/sdf/fileFormat.h>
#include <pxr/base/tf/staticTokens.h>
#include <pxr/usd/pcp/dynamicFileFormatInterface.h>
#include <iosfwd>
#include <string>

Expand All @@ -32,16 +34,20 @@ 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();

virtual ~UsdGlTFFileFormat();

public:
SdfAbstractDataRefPtr InitData(const FileFormatArguments& args) const override;

bool CanRead(const std::string &file) const override;

bool Read(SdfLayer* layer,
Expand All @@ -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
Expand Down

0 comments on commit 64d7592

Please sign in to comment.