Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USDScene : Support reading from in-memory stages via UsdUtilsStageCache #1389

Merged
merged 1 commit into from
Sep 15, 2023
Merged
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 Changes
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Improvements
------------

- USD PrimitiveAlgo : Added `readPrimitiveVariable()` utility method for reading from regular `UsdAttributes`.
- USDScene : Added support for reading from in-memory stages by passing a filename of the form `stageCache:{id}.usd` where `{id}` specifies a stage which has been inserted in the `UsdUtilsStageCache`.

Fixes
-----
Expand Down
1 change: 1 addition & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -2967,6 +2967,7 @@ else :
"usdLux",
"usdSkel",
"usdShade",
"usdUtils",
"sdf",
"tf",
"pcp",
Expand Down
20 changes: 18 additions & 2 deletions contrib/IECoreUSD/src/IECoreUSD/USDScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ IECORE_PUSH_DEFAULT_VISIBILITY
#include "pxr/usd/usdShade/material.h"
#include "pxr/usd/usdShade/materialBindingAPI.h"
#include "pxr/usd/usdShade/connectableAPI.h"
#include "pxr/usd/usdUtils/stageCache.h"
#ifdef IECOREUSD_WITH_OPENVDB
#include "pxr/usd/usdVol/fieldBase.h"
#endif
Expand All @@ -84,6 +85,7 @@ IECORE_POP_DEFAULT_VISIBILITY

#include "tbb/concurrent_hash_map.h"

#include <filesystem>
#include <iostream>
#include <mutex>

Expand Down Expand Up @@ -688,10 +690,24 @@ class USDScene::IO : public RefCounted
switch( openMode )
{
case IndexedIO::Read : {
pxr::UsdStageRefPtr stage = pxr::UsdStage::Open( fileName );
static const std::string g_stageCachePrefix( "stageCache:" );
pxr::UsdStageRefPtr stage;
if( boost::starts_with( fileName, g_stageCachePrefix ) )
{
// Get Id from filename of form "stageCache:{id}.usd"
std::filesystem::path path( fileName.substr( g_stageCachePrefix.size() ) );
path.replace_extension();
stage = pxr::UsdUtilsStageCache::Get().Find(
pxr::UsdStageCache::Id::FromString( path.string() )
);
}
else
{
stage = pxr::UsdStage::Open( fileName );
}
if( !stage )
{
throw IECore::Exception( boost::str( boost::format( "USDScene : Failed to open USD file: '%1%'" ) % fileName ) );
throw IECore::Exception( boost::str( boost::format( "USDScene : Failed to open USD stage : '%1%'" ) % fileName ) );
}
return stage;
}
Expand Down
10 changes: 10 additions & 0 deletions contrib/IECoreUSD/test/IECoreUSD/USDSceneTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3911,5 +3911,15 @@ def testUnconnectedMaterialOutput( self ) :
self.assertNotIn( "cycles:surface", sphere.attributeNames() )
self.assertIsNone( sphere.readAttribute( "cycles:surface", 0 ) )

def testReadFromStageCache( self ) :

stage = pxr.Usd.Stage.CreateInMemory()
pxr.UsdGeom.Sphere.Define( stage, "/sphere" )
id = pxr.UsdUtils.StageCache.Get().Insert( stage )

root = IECoreScene.SceneInterface.create( "stageCache:{}.usd".format( id.ToString() ), IECore.IndexedIO.OpenMode.Read )
self.assertEqual( root.childNames(), [ "sphere" ] )
self.assertIsInstance( root.child( "sphere" ).readObject( 0 ), IECoreScene.SpherePrimitive )

if __name__ == "__main__":
unittest.main()
Loading