From db87ba8a9c0ecacd18be5703122783a9dfedff7b Mon Sep 17 00:00:00 2001 From: Murray Stevenson <50844517+murraystevenson@users.noreply.github.com> Date: Thu, 23 May 2024 11:27:33 -0700 Subject: [PATCH] TweakPlug : Specify when a fallback value is appropriate 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. --- include/Gaffer/TweakPlug.inl | 2 +- python/GafferSceneTest/AttributeTweaksTest.py | 23 +++++++++++++++++++ src/Gaffer/ContextVariableTweaks.cpp | 2 +- src/Gaffer/TweakPlug.cpp | 2 +- src/GafferScene/AttributeTweaks.cpp | 4 ++-- src/GafferScene/CameraTweaks.cpp | 2 +- src/GafferScene/OptionTweaks.cpp | 2 +- src/GafferScene/ShaderTweaks.cpp | 2 +- 8 files changed, 31 insertions(+), 8 deletions(-) diff --git a/include/Gaffer/TweakPlug.inl b/include/Gaffer/TweakPlug.inl index 9be92f5fa00..becd31ce344 100644 --- a/include/Gaffer/TweakPlug.inl +++ b/include/Gaffer/TweakPlug.inl @@ -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( currentValue ) ) { diff --git a/python/GafferSceneTest/AttributeTweaksTest.py b/python/GafferSceneTest/AttributeTweaksTest.py index cb2b876771d..5b693955396 100644 --- a/python/GafferSceneTest/AttributeTweaksTest.py +++ b/python/GafferSceneTest/AttributeTweaksTest.py @@ -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() diff --git a/src/Gaffer/ContextVariableTweaks.cpp b/src/Gaffer/ContextVariableTweaks.cpp index 1ff7d42f8ea..c0df96aee44 100644 --- a/src/Gaffer/ContextVariableTweaks.cpp +++ b/src/Gaffer/ContextVariableTweaks.cpp @@ -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; diff --git a/src/Gaffer/TweakPlug.cpp b/src/Gaffer/TweakPlug.cpp index dfa43ee6f67..07c04e205ff 100644 --- a/src/Gaffer/TweakPlug.cpp +++ b/src/Gaffer/TweakPlug.cpp @@ -279,7 +279,7 @@ Gaffer::PlugPtr TweakPlug::createCounterpart( const std::string &name, Direction bool TweakPlug::applyTweak( IECore::CompoundData *parameters, MissingMode missingMode ) const { return applyTweak( - [¶meters]( const std::string &valueName ) { return parameters->member( valueName ); }, + [¶meters]( const std::string &valueName, const bool fallback ) { return parameters->member( valueName ); }, [¶meters]( const std::string &valueName, DataPtr newData ) { if( newData == nullptr ) diff --git a/src/GafferScene/AttributeTweaks.cpp b/src/GafferScene/AttributeTweaks.cpp index d3497becd33..82080818ae1 100644 --- a/src/GafferScene/AttributeTweaks.cpp +++ b/src/GafferScene/AttributeTweaks.cpp @@ -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( valueName ); - if( !result && valueName == "linkedLights" ) + if( fallback && !result && valueName == "linkedLights" ) { /// \todo Use a registry to provide default values for /// all attributes. diff --git a/src/GafferScene/CameraTweaks.cpp b/src/GafferScene/CameraTweaks.cpp index 948459ea423..b77e5fedbcc 100644 --- a/src/GafferScene/CameraTweaks.cpp +++ b/src/GafferScene/CameraTweaks.cpp @@ -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] ); diff --git a/src/GafferScene/OptionTweaks.cpp b/src/GafferScene/OptionTweaks.cpp index 8d76071cba3..8031b3a5653 100644 --- a/src/GafferScene/OptionTweaks.cpp +++ b/src/GafferScene/OptionTweaks.cpp @@ -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( g_namePrefix + valueName ); }, diff --git a/src/GafferScene/ShaderTweaks.cpp b/src/GafferScene/ShaderTweaks.cpp index 75b097ba02c..5177d09bd61 100644 --- a/src/GafferScene/ShaderTweaks.cpp +++ b/src/GafferScene/ShaderTweaks.cpp @@ -332,7 +332,7 @@ bool ShaderTweaks::applyTweaks( IECoreScene::ShaderNetwork *shaderNetwork, Tweak if( tweakPlug->applyTweak( - [¶meter, &modifiedShader]( const std::string &valueName ) + [¶meter, &modifiedShader]( const std::string &valueName, const bool fallback ) { return modifiedShader.first->second->parametersData()->member( parameter.name ); },