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 : Fix crash trying to write to an open stage #1431

Merged
merged 1 commit into from
Sep 16, 2024
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 @@ Fixes
-----

- USDScene :
- Fixed crash attempting to write to a file that is already open for reading. An exception is now thrown instead.
- Fixed loading of skinned facevarying normals.
- `lightLink` and `shadowLink` collections on UsdLuxLightAPI are no longer treated as sets.

Expand Down
17 changes: 10 additions & 7 deletions contrib/IECoreUSD/src/IECoreUSD/USDScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,11 +793,11 @@ class USDScene::IO : public RefCounted

static pxr::UsdStageRefPtr makeStage( const std::string &fileName, IndexedIO::OpenMode openMode )
{
pxr::UsdStageRefPtr stage;
switch( openMode )
{
case IndexedIO::Read : {
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"
Expand All @@ -811,17 +811,20 @@ class USDScene::IO : public RefCounted
{
stage = pxr::UsdStage::Open( fileName );
}
if( !stage )
{
throw IECore::Exception( boost::str( boost::format( "USDScene : Failed to open USD stage : '%1%'" ) % fileName ) );
}
return stage;
break;
}
case IndexedIO::Write :
return pxr::UsdStage::CreateNew( fileName );
stage = pxr::UsdStage::CreateNew( fileName );
break;
default:
throw Exception( "Unsupported OpenMode" );
}

if( !stage )
{
throw IECore::Exception( boost::str( boost::format( "USDScene : Failed to open USD stage : '%1%'" ) % fileName ) );
}
return stage;
}

std::string m_fileName;
Expand Down
12 changes: 12 additions & 0 deletions contrib/IECoreUSD/test/IECoreUSD/USDSceneTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4359,5 +4359,17 @@ def testSetNameValidation( self ) :
with self.subTest( setName = setName ) :
self.assertEqual( root.readSet( setName ), IECore.PathMatcher( [ f"/set{setIndex}Member" ] ) )

def testWriteToOpenScene( self ) :

# Using posix-format filename, because Windows backslashes don't play nicely
# with `assertRaisesRegex()`.
fileName = ( pathlib.Path( self.temporaryDirectory() ) / "test.usda" ).as_posix()
IECoreScene.SceneInterface.create( fileName, IECore.IndexedIO.OpenMode.Write )

reader = IECoreScene.SceneInterface.create( fileName, IECore.IndexedIO.OpenMode.Read )

with self.assertRaisesRegex( RuntimeError, f"USDScene : Failed to open USD stage : '{fileName}'" ) :
IECoreScene.SceneInterface.create( fileName, IECore.IndexedIO.OpenMode.Write )

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