Skip to content

Commit

Permalink
TweakPlug : Support {source} token when replacing strings
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhaddon committed Apr 29, 2024
1 parent 2749e14 commit 3696120
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Improvements
- Added support for `uv.tangent` and `uv.tangent_sign` primitive variables to assist in rendering with normal maps (#5269).
- AttributeQuery, PrimitiveVariableQuery, ContextQuery, OptionQuery, ShaderQuery : Added support for querying arrays of length 1 as their equivalent scalar types.
- CodeWidget : Added <kbd>Ctrl</kbd>+<kbd>L</kbd> shortcut for selecting all text on the current line.
- AttributeTweaks, CameraTweaks, ShaderTweaks, OptionTweaks, PrimitiveVariableTweaks : Added tooltips documenting the tweak modes.
- AttributeTweaks, CameraTweaks, ShaderTweaks, OptionTweaks, PrimitiveVariableTweaks :
- Added support for a `{source}` token which is substituted with the original value when tweaking a string in `Replace` mode.
- Added tooltips documenting the tweak modes.

Fixes
-----
Expand Down
2 changes: 2 additions & 0 deletions include/Gaffer/TweakPlug.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ class GAFFER_API TweakPlug : public Gaffer::ValuePlug
const std::string &tweakName
) const;

void applyReplaceTweak( const IECore::Data *sourceData, IECore::Data *tweakData ) const;

static const char *modeToString( Gaffer::TweakPlug::Mode mode );

};
Expand Down
4 changes: 4 additions & 0 deletions include/Gaffer/TweakPlug.inl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ bool TweakPlug::applyTweak(
{
applyListTweak( currentValue, newData.get(), newData.get(), mode, name );
}
else if( mode == TweakPlug::Replace )
{
applyReplaceTweak( currentValue, newData.get() );
}

if( mode != Gaffer::TweakPlug::CreateIfMissing )
{
Expand Down
30 changes: 30 additions & 0 deletions python/GafferTest/TweakPlugTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,5 +451,35 @@ def testStringListOperations( self ) :
self.assertTrue( plug.applyTweak( data ) )
self.assertEqual( data["v"], IECore.StringData( result ) )

def testStringSubstitutions( self ) :

for source, tweakMode, tweakValue, result in [
# Create modes don't support substitutions.
( None, Gaffer.TweakPlug.Mode.Create, "{source}", "{source}" ),
( None, Gaffer.TweakPlug.Mode.CreateIfMissing, "{source}", "{source}" ),
( IECore.StringData( "a" ), Gaffer.TweakPlug.Mode.Create, "{source}", "{source}" ),
( IECore.StringData( "a" ), Gaffer.TweakPlug.Mode.CreateIfMissing, "{source}", "a" ),
# Neither do list modes.
( IECore.StringData( "a" ), Gaffer.TweakPlug.Mode.ListAppend, "{source}", "a {source}" ),
( IECore.StringData( "a" ), Gaffer.TweakPlug.Mode.ListPrepend, "{source}", "{source} a" ),
# Replace mode does support substitutions.
( IECore.StringData( "a" ), Gaffer.TweakPlug.Mode.Replace, "{source}", "a" ),
( IECore.StringData( "a" ), Gaffer.TweakPlug.Mode.Replace, "prefix{source}", "prefixa" ),
( IECore.StringData( "a" ), Gaffer.TweakPlug.Mode.Replace, "{source}suffix", "asuffix" ),
( IECore.StringData( "a" ), Gaffer.TweakPlug.Mode.Replace, "{source} {source}", "a a" ),
# And works with InternedStringData too.
( IECore.InternedStringData( "a" ), Gaffer.TweakPlug.Mode.Replace, "{source}", "a" ),
( IECore.InternedStringData( "a" ), Gaffer.TweakPlug.Mode.Replace, "prefix{source}", "prefixa" ),
( IECore.InternedStringData( "a" ), Gaffer.TweakPlug.Mode.Replace, "{source}suffix", "asuffix" ),
( IECore.InternedStringData( "a" ), Gaffer.TweakPlug.Mode.Replace, "{source} {source}", "a a" ),
] :
with self.subTest( source = source, tweakMode = tweakMode, tweakValue = tweakValue ) :
plug = Gaffer.TweakPlug( "v", tweakValue, tweakMode )
data = IECore.CompoundData( { "v" : source } )
self.assertTrue( plug.applyTweak( data ) )
if source is not None :
self.assertIs( type( data["v"] ), type( source ) )
self.assertEqual( data["v"].value, result )

if __name__ == "__main__":
unittest.main()
3 changes: 3 additions & 0 deletions python/GafferUI/TweakPlugValueWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ def __validModes( plug ) :
"""
Replaces an existing {property}. Errors if the {property} doesn't exist,
unless `ignoreMissing` is set, in which case the tweak is skipped.
When replacing a string {property}, the new value may contain a `{{source}}`
token, which will be substituted with the original value.
""",
Gaffer.TweakPlug.Mode.Add :
"""
Expand Down
16 changes: 16 additions & 0 deletions src/Gaffer/TweakPlug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "IECore/TypeTraits.h"

#include "boost/algorithm/string/join.hpp"
#include "boost/algorithm/string/replace.hpp"

#include "fmt/format.h"

Expand Down Expand Up @@ -412,6 +413,21 @@ void TweakPlug::applyListTweak(
);
}

void TweakPlug::applyReplaceTweak( const IECore::Data *sourceData, IECore::Data *tweakData ) const
{
if( auto stringData = IECore::runTimeCast<IECore::StringData>( tweakData ) )
{
boost::replace_all( stringData->writable(), "{source}", static_cast<const IECore::StringData *>( sourceData )->readable() );
}
else if( auto internedStringData = IECore::runTimeCast<IECore::InternedStringData>( tweakData ) )
{
internedStringData->writable() = boost::replace_all_copy(
internedStringData->readable().string(),
"{source}", static_cast<const IECore::InternedStringData *>( sourceData )->readable().string()
);
}
}

const char *TweakPlug::modeToString( Gaffer::TweakPlug::Mode mode )
{
switch( mode )
Expand Down

0 comments on commit 3696120

Please sign in to comment.