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

USDLayerWriter fixes #6199

Merged
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
5 changes: 5 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
1.4.15.x (relative to 1.4.15.3)
========

Fixes
-----

- USDLayerWriter :
- Fixed silent failures when unable to create the output file (#6197).
- Fixed leak of `usdLayerWriter:fileName` context variable.

1.4.15.3 (relative to 1.4.15.2)
========
Expand Down
37 changes: 37 additions & 0 deletions python/GafferUSDTest/USDLayerWriterTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@

import pathlib
import unittest
import os
import subprocess

import pxr.Usd

import IECore
import IECoreScene

import Gaffer
import GafferUSD
import GafferScene
import GafferSceneTest
Expand Down Expand Up @@ -231,5 +234,39 @@ def testChangePrimitiveType( self ) :
reader["fileName"].setValue( compositionFileName )
self.assertScenesEqual( reader["out"], spherePrimitive["out"], checks = self.allSceneChecks - { "sets" } )

def testNoContextLeaks( self ) :

sphere = GafferScene.Sphere()

layerWriter = GafferUSD.USDLayerWriter()
layerWriter["base"].setInput( sphere["out"] )
layerWriter["layer"].setInput( sphere["out"] )
layerWriter["fileName"].setValue( self.temporaryDirectory() / "layer.usda" )

with Gaffer.ContextMonitor( sphere ) as monitor :
layerWriter["task"].execute()

self.assertNotIn( "usdLayerWriter:fileName", monitor.combinedStatistics().variableNames() )

def testNoWritePermissions( self ) :

sphere = GafferScene.Sphere()

layerWriter = GafferUSD.USDLayerWriter()
layerWriter["base"].setInput( sphere["out"] )
layerWriter["layer"].setInput( sphere["out"] )
layerWriter["fileName"].setValue( self.temporaryDirectory() / "layer.usda" )

if os.name != "nt" :
self.temporaryDirectory().chmod( 444 )
else :
subprocess.check_call( [ "icacls", self.temporaryDirectory(), "/deny", "Users:(OI)(CI)(W)" ] )

with self.assertRaisesRegex( RuntimeError, 'Failed to export layer to "{}"'.format( layerWriter["fileName"].getValue() ) ) :
layerWriter["task"].execute()

if os.name == "nt" :
subprocess.check_call( [ "icacls", self.temporaryDirectory(), "/grant", "Users:(OI)(CI)(W)" ] )

if __name__ == "__main__":
unittest.main()
9 changes: 5 additions & 4 deletions src/GafferUSD/USDLayerWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ USDLayerWriter::USDLayerWriter( const std::string &name )

NameSwitchPtr sceneSwitch = new NameSwitch( "__sceneSwitch" );
sceneSwitch->selectorPlug()->setValue( "${usdLayerWriter:fileName}" );
sceneSwitch->deleteContextVariablesPlug()->setValue( "usdLayerWriter:fileName" );
sceneSwitch->setup( basePlug() );
sceneSwitch->inPlugs()->getChild<NameValuePlug>( 0 )->valuePlug()->setInput( basePlug() );
sceneSwitch->inPlugs()->getChild<NameValuePlug>( 1 )->valuePlug()->setInput( layerPlug() );
Expand Down Expand Up @@ -363,9 +364,6 @@ void USDLayerWriter::executeSequence( const std::vector<float> &frames ) const
Context::EditableScope context( Context::current() );
for( const auto &fileName : { baseFileName, layerFileName } )
{
/// \todo Stop this context variable leaking out into the scene
/// evaluation. There is some talk of giving NameSwitch a feature to do
/// this for us.
context.set( "usdLayerWriter:fileName", &fileName );
sceneWriter()->taskPlug()->executeSequence( frames );
}
Expand All @@ -381,5 +379,8 @@ void USDLayerWriter::executeSequence( const std::vector<float> &frames ) const
createDiff( layer->GetPseudoRoot(), *layer, baseLayer->GetPseudoRoot(), *baseLayer );

createDirectories( outputFileName );
layer->Export( outputFileName );
if( !layer->Export( outputFileName ) )
{
throw IECore::Exception( fmt::format( "Failed to export layer to \"{}\"", outputFileName ) );
}
}