Skip to content

Commit

Permalink
Improve Alembic plugin to read assets with ArResolver to support othe…
Browse files Browse the repository at this point in the history
…r sources except local files
  • Loading branch information
roggiezhang-nv committed Sep 19, 2024
1 parent c3b54ec commit 06a7e25
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion pxr/usd/plugin/usdAbc/alembicReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
/// \file alembicReader.cpp

#include "pxr/pxr.h"
#include "pxr/usd/ar/asset.h"
#include "pxr/usd/ar/resolvedPath.h"
#include "pxr/usd/ar/resolver.h"
#include "pxr/usd/plugin/usdAbc/alembicReader.h"
#include "pxr/usd/plugin/usdAbc/alembicUtil.h"
#include "pxr/usd/usdGeom/hermiteCurves.h"
Expand Down Expand Up @@ -36,6 +39,7 @@
#include <Alembic/AbcGeom/SchemaInfoDeclarations.h>
#include <Alembic/AbcGeom/Visibility.h>
#include <optional>
#include <fstream>
#include <functional>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -628,6 +632,34 @@ _ReaderSchema::GetPrimReaders(const std::string& schema) const
return empty;
}

/// \class _ArAssetStreamContext
/// \brief The context to save the associated std::istream
/// std::filebuf, and pxr:ArAsset.
///
/// It's used by the Alembic reader to read from istreams.
class _ArAssetStreamContext
{
public:
_ArAssetStreamContext(const std::shared_ptr<ArAsset> &asset) : _asset(asset)
{
if (asset && asset->GetFileUnsafe().first)
{
_fileBuf =
std::make_unique<std::filebuf>(asset->GetFileUnsafe().first);
}
// Note: Contructing std::istream with nullptr is allowed, but
// it returns a std::istream with invalid io state.
_istream = std::make_unique<std::istream>(_fileBuf.get());
}

std::istream *GetIStream() const { return _istream.get(); }

private:
std::unique_ptr<std::istream> _istream;
std::unique_ptr<std::filebuf> _fileBuf;
std::shared_ptr<ArAsset> _asset;
};

/// \class _ReaderContext
/// \brief The Alembic to Usd writer context.
///
Expand Down Expand Up @@ -853,6 +885,7 @@ class _ReaderContext {
_PrimMap _prims;
Prim* _pseudoRoot;
UsdAbc_TimeSamples _allTimeSamples;
std::vector<_ArAssetStreamContext> _allLayeredABCStreamContexts;
};

static
Expand Down Expand Up @@ -889,6 +922,16 @@ _ReaderContext::Open(const std::string& filePath, std::string* errorLog,
}
layeredABC.emplace_back(filePath);

std::vector<std::istream *> layeredABCStreams;
for (const std::string &path : layeredABC)
{
std::shared_ptr<ArAsset> asset =
ArGetResolver().OpenAsset(ArResolvedPath(path));
_ArAssetStreamContext context(asset);
layeredABCStreams.push_back(context.GetIStream());
_allLayeredABCStreamContexts.emplace_back(std::move(context));
}

#if PXR_HDF5_SUPPORT_ENABLED && !H5_HAVE_THREADSAFE
// HDF5 may not be thread-safe.
using lock_guard = std::lock_guard<std::recursive_mutex>;
Expand All @@ -907,7 +950,7 @@ _ReaderContext::Open(const std::string& filePath, std::string* errorLog,
}
#endif

IArchive archive = factory.getArchive(layeredABC, abcType);
IArchive archive = factory.getArchive(layeredABCStreams, abcType);

#if PXR_HDF5_SUPPORT_ENABLED && !H5_HAVE_THREADSAFE
if (abcType == IFactory::kHDF5 || abcType == IFactory::kLayer) {
Expand Down

0 comments on commit 06a7e25

Please sign in to comment.