Skip to content

Commit

Permalink
TweakPlug : Specify when a fallback value is appropriate
Browse files Browse the repository at this point in the history
This works around issues with CreateIfMissing mode where the fallback value would mask missing values and no tweak would be created as nothing would be considered missing.
  • Loading branch information
murraystevenson committed May 23, 2024
1 parent e535b88 commit db87ba8
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion include/Gaffer/TweakPlug.inl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ bool TweakPlug::applyTweak(
return setDataFunctor( name, newData );
}

const IECore::Data *currentValue = getDataFunctor( name );
const IECore::Data *currentValue = getDataFunctor( name, /* fallback = */ mode != Gaffer::TweakPlug::CreateIfMissing );

if( IECore::runTimeCast<const IECore::InternedStringData>( currentValue ) )
{
Expand Down
23 changes: 23 additions & 0 deletions python/GafferSceneTest/AttributeTweaksTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,28 @@ def testLinkedLightsSourceSubstitution( self ) :
standardAttributes["attributes"]["linkedLights"]["value"].setValue( "someLights" )
self.assertEqual( tweaks["out"].attributes( "/plane" )["linkedLights"], IECore.StringData( "(someLights) - unwantedLights" ) )

def testLinkedLightsCreateIfMissing( self ) :

plane = GafferScene.Plane()

planeFilter = GafferScene.PathFilter()
planeFilter["paths"].setValue( IECore.StringVectorData( [ "/plane" ] ) )

standardAttributes = GafferScene.StandardAttributes()
standardAttributes["in"].setInput( plane["out"] )
standardAttributes["filter"].setInput( planeFilter["out"] )

self.assertNotIn( "linkedLights", standardAttributes["out"].attributes( "/plane" ) )

tweaks = GafferScene.AttributeTweaks()
tweaks["in"].setInput( standardAttributes["out"] )
tweaks["filter"].setInput( planeFilter["out"] )

testTweak = Gaffer.TweakPlug( "linkedLights", "defaultLights" )
testTweak["mode"].setValue( Gaffer.TweakPlug.Mode.CreateIfMissing )
tweaks["tweaks"].addChild( testTweak )

self.assertEqual( tweaks["out"].attributes( "/plane" )["linkedLights"], IECore.StringData( "defaultLights" ) )

if __name__ == "__main__" :
unittest.main()
2 changes: 1 addition & 1 deletion src/Gaffer/ContextVariableTweaks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void ContextVariableTweaks::processContext( Context::EditableScope &context, IEC
IECore::ObjectVectorPtr storageVector = new ObjectVector();

tweaksPlug->applyTweaks(
[&context, &sourceData]( const std::string &valueName )
[&context, &sourceData]( const std::string &valueName, const bool fallback )
{
DataPtr value = context.context()->getAsData( valueName, nullptr );
sourceData = value;
Expand Down
2 changes: 1 addition & 1 deletion src/Gaffer/TweakPlug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ Gaffer::PlugPtr TweakPlug::createCounterpart( const std::string &name, Direction
bool TweakPlug::applyTweak( IECore::CompoundData *parameters, MissingMode missingMode ) const
{
return applyTweak(
[&parameters]( const std::string &valueName ) { return parameters->member( valueName ); },
[&parameters]( const std::string &valueName, const bool fallback ) { return parameters->member( valueName ); },
[&parameters]( const std::string &valueName, DataPtr newData )
{
if( newData == nullptr )
Expand Down
4 changes: 2 additions & 2 deletions src/GafferScene/AttributeTweaks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ IECore::ConstCompoundObjectPtr AttributeTweaks::computeProcessedAttributes( cons
}

tweaksPlug->applyTweaks(
[&source]( const std::string &valueName ) -> const IECore::Data *
[&source]( const std::string &valueName, const bool fallback ) -> const IECore::Data *
{
const Data *result = source->member<Data>( valueName );
if( !result && valueName == "linkedLights" )
if( fallback && !result && valueName == "linkedLights" )
{
/// \todo Use a registry to provide default values for
/// all attributes.
Expand Down
2 changes: 1 addition & 1 deletion src/GafferScene/CameraTweaks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ IECore::ConstObjectPtr CameraTweaks::computeProcessedObject( const ScenePath &pa
tweaksPlug->applyTweaks(

// Getter
[&] ( const std::string &name ) {
[&] ( const std::string &name, const bool fallback ) {
if( name == "fieldOfView" )
{
virtualParameter = new FloatData( result->calculateFieldOfView()[0] );
Expand Down
2 changes: 1 addition & 1 deletion src/GafferScene/OptionTweaks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ IECore::ConstCompoundObjectPtr OptionTweaks::computeProcessedGlobals(
result->members() = inputGlobals->members();

tweaksPlug->applyTweaks(
[&result]( const std::string &valueName )
[&result]( const std::string &valueName, const bool fallback )
{
return result->member<Data>( g_namePrefix + valueName );
},
Expand Down
2 changes: 1 addition & 1 deletion src/GafferScene/ShaderTweaks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ bool ShaderTweaks::applyTweaks( IECoreScene::ShaderNetwork *shaderNetwork, Tweak

if(
tweakPlug->applyTweak(
[&parameter, &modifiedShader]( const std::string &valueName )
[&parameter, &modifiedShader]( const std::string &valueName, const bool fallback )
{
return modifiedShader.first->second->parametersData()->member( parameter.name );
},
Expand Down

0 comments on commit db87ba8

Please sign in to comment.