Skip to content

Commit

Permalink
Merge pull request #1389 from johnhaddon/usdSceneStageCache
Browse files Browse the repository at this point in the history
USDScene : Support reading from in-memory stages via UsdUtilsStageCache
  • Loading branch information
johnhaddon authored Sep 15, 2023
2 parents 54c5f6c + 8e3f071 commit 1afc717
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
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()

0 comments on commit 1afc717

Please sign in to comment.