Skip to content

Commit 1afc717

Browse files
authored
Merge pull request #1389 from johnhaddon/usdSceneStageCache
USDScene : Support reading from in-memory stages via UsdUtilsStageCache
2 parents 54c5f6c + 8e3f071 commit 1afc717

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Improvements
55
------------
66

77
- USD PrimitiveAlgo : Added `readPrimitiveVariable()` utility method for reading from regular `UsdAttributes`.
8+
- 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`.
89

910
Fixes
1011
-----

SConstruct

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,6 +2967,7 @@ else :
29672967
"usdLux",
29682968
"usdSkel",
29692969
"usdShade",
2970+
"usdUtils",
29702971
"sdf",
29712972
"tf",
29722973
"pcp",

contrib/IECoreUSD/src/IECoreUSD/USDScene.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ IECORE_PUSH_DEFAULT_VISIBILITY
7070
#include "pxr/usd/usdShade/material.h"
7171
#include "pxr/usd/usdShade/materialBindingAPI.h"
7272
#include "pxr/usd/usdShade/connectableAPI.h"
73+
#include "pxr/usd/usdUtils/stageCache.h"
7374
#ifdef IECOREUSD_WITH_OPENVDB
7475
#include "pxr/usd/usdVol/fieldBase.h"
7576
#endif
@@ -84,6 +85,7 @@ IECORE_POP_DEFAULT_VISIBILITY
8485

8586
#include "tbb/concurrent_hash_map.h"
8687

88+
#include <filesystem>
8789
#include <iostream>
8890
#include <mutex>
8991

@@ -688,10 +690,24 @@ class USDScene::IO : public RefCounted
688690
switch( openMode )
689691
{
690692
case IndexedIO::Read : {
691-
pxr::UsdStageRefPtr stage = pxr::UsdStage::Open( fileName );
693+
static const std::string g_stageCachePrefix( "stageCache:" );
694+
pxr::UsdStageRefPtr stage;
695+
if( boost::starts_with( fileName, g_stageCachePrefix ) )
696+
{
697+
// Get Id from filename of form "stageCache:{id}.usd"
698+
std::filesystem::path path( fileName.substr( g_stageCachePrefix.size() ) );
699+
path.replace_extension();
700+
stage = pxr::UsdUtilsStageCache::Get().Find(
701+
pxr::UsdStageCache::Id::FromString( path.string() )
702+
);
703+
}
704+
else
705+
{
706+
stage = pxr::UsdStage::Open( fileName );
707+
}
692708
if( !stage )
693709
{
694-
throw IECore::Exception( boost::str( boost::format( "USDScene : Failed to open USD file: '%1%'" ) % fileName ) );
710+
throw IECore::Exception( boost::str( boost::format( "USDScene : Failed to open USD stage : '%1%'" ) % fileName ) );
695711
}
696712
return stage;
697713
}

contrib/IECoreUSD/test/IECoreUSD/USDSceneTest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3911,5 +3911,15 @@ def testUnconnectedMaterialOutput( self ) :
39113911
self.assertNotIn( "cycles:surface", sphere.attributeNames() )
39123912
self.assertIsNone( sphere.readAttribute( "cycles:surface", 0 ) )
39133913

3914+
def testReadFromStageCache( self ) :
3915+
3916+
stage = pxr.Usd.Stage.CreateInMemory()
3917+
pxr.UsdGeom.Sphere.Define( stage, "/sphere" )
3918+
id = pxr.UsdUtils.StageCache.Get().Insert( stage )
3919+
3920+
root = IECoreScene.SceneInterface.create( "stageCache:{}.usd".format( id.ToString() ), IECore.IndexedIO.OpenMode.Read )
3921+
self.assertEqual( root.childNames(), [ "sphere" ] )
3922+
self.assertIsInstance( root.child( "sphere" ).readObject( 0 ), IECoreScene.SpherePrimitive )
3923+
39143924
if __name__ == "__main__":
39153925
unittest.main()

0 commit comments

Comments
 (0)