Skip to content

Commit

Permalink
Merge pull request #4693 from johnhaddon/stringPlugConnections
Browse files Browse the repository at this point in the history
StringPlug/StringVectorDataPlug : Support interconnection
  • Loading branch information
ericmehl authored Jun 6, 2022
2 parents 9ba03cc + 2e22984 commit 07db256
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 5 deletions.
8 changes: 7 additions & 1 deletion Changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
0.61.x.x (relative to 0.61.11.0)
=========
========

Improvements
------------

- StringPlug : Added support for input connections from StringVectorDataPlugs. The string value is formed by joining the string array using spaces.
- StringVectorDataPlug : Added support for input connections from StringPlugs. The array value is formed by splitting the string on spaces.

Improvements
------------
Expand Down
26 changes: 26 additions & 0 deletions python/GafferTest/StringPlugTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,5 +377,31 @@ def testHashUsesValue( self ) :
for i in range( 10, 20 ) :
self.assertEqual( hashes[i], hashes[10] )

def testStringVectorDataInput( self ) :

node = Gaffer.Node()
node["user"]["string"] = Gaffer.StringPlug( flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic )
node["user"]["stringVector"] = Gaffer.StringVectorDataPlug( defaultValue = IECore.StringVectorData(), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic )

self.assertTrue( node["user"]["string"].acceptsInput( node["user"]["stringVector"] ) )
node["user"]["string"].setInput( node["user"]["stringVector"] )

hashes = set()
for input, output in [
( [], "", ),
( [ "test" ], "test" ),
( [ "a", "b", "c" ], "a b c" ),
( [ "dog", "cat" ], "dog cat" ),
( [ "a", "b", "", "c" ], "a b c" ),
] :

node["user"]["stringVector"].setValue( IECore.StringVectorData( input ) )

h = node["user"]["string"].hash()
self.assertNotIn( h, hashes )
hashes.add( h )

self.assertEqual( node["user"]["string"].getValue(), output )

if __name__ == "__main__":
unittest.main()
26 changes: 26 additions & 0 deletions python/GafferTest/TypedObjectPlugTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,5 +335,31 @@ def testRepr( self ) :
plug2 = eval( repr( plug ) )
self.assertEqual( plug2.defaultValue(), plug.defaultValue() )

def testStringVectorDataPlugWithStringInput( self ) :

node = Gaffer.Node()
node["user"]["string"] = Gaffer.StringPlug( flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic )
node["user"]["stringVector"] = Gaffer.StringVectorDataPlug( defaultValue = IECore.StringVectorData(), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic )

self.assertTrue( node["user"]["stringVector"].acceptsInput( node["user"]["string"] ) )
node["user"]["stringVector"].setInput( node["user"]["string"] )

hashes = set()
for input, output in [
( "", [] ),
( "test", [ "test" ] ),
( "a b c", [ "a", "b", "c" ] ),
( "dog cat", [ "dog", "cat" ] ),
( "a b c", [ "a", "b", "", "c" ] )
] :

node["user"]["string"].setValue( input )

h = node["user"]["stringVector"].hash()
self.assertNotIn( h, hashes )
hashes.add( h )

self.assertEqual( node["user"]["stringVector"].getValue(), IECore.StringVectorData( output ) )

if __name__ == "__main__":
unittest.main()
15 changes: 11 additions & 4 deletions src/Gaffer/StringPlug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@

#include "Gaffer/Context.h"
#include "Gaffer/Process.h"
#include "Gaffer/TypedObjectPlug.h"

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

using namespace IECore;
using namespace Gaffer;
Expand Down Expand Up @@ -73,7 +76,7 @@ bool StringPlug::acceptsInput( const Plug *input ) const
}
if( input )
{
return input->isInstanceOf( staticTypeId() );
return input->isInstanceOf( staticTypeId() ) || input->isInstanceOf( StringVectorDataPlug::staticTypeId() );
}
return true;
}
Expand Down Expand Up @@ -109,10 +112,14 @@ std::string StringPlug::getValue( const IECore::MurmurHash *precomputedHash ) co

void StringPlug::setFrom( const ValuePlug *other )
{
const StringPlug *tOther = IECore::runTimeCast<const StringPlug >( other );
if( tOther )
if( auto stringPlug = IECore::runTimeCast<const StringPlug >( other ) )
{
setValue( stringPlug->getValue() );
}
else if( auto stringVectorPlug = IECore::runTimeCast<const StringVectorDataPlug >( other ) )
{
setValue( tOther->getValue() );
ConstStringVectorDataPtr data = stringVectorPlug->getValue();
setValue( boost::algorithm::join( data->readable(), " " ) );
}
else
{
Expand Down
47 changes: 47 additions & 0 deletions src/Gaffer/TypedObjectPlug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@

#include "Gaffer/TypedObjectPlug.h"

#include "Gaffer/StringPlug.h"
#include "Gaffer/TypedObjectPlug.inl"

#include "boost/algorithm/string/classification.hpp"
#include "boost/algorithm/string/split.hpp"

namespace Gaffer
{

Expand Down Expand Up @@ -98,6 +102,49 @@ void CompoundObjectPlug::setFrom( const ValuePlug *other )
}
}

// Specialise StringVectorDataPlug to accept connections from StringPlug

template<>
bool StringVectorDataPlug::acceptsInput( const Plug *input ) const
{
if( !ValuePlug::acceptsInput( input ) )
{
return false;
}

if( input )
{
return
input->isInstanceOf( staticTypeId() ) ||
input->isInstanceOf( StringPlug::staticTypeId() )
;
}
return true;
}

template<>
void StringVectorDataPlug::setFrom( const ValuePlug *other )
{
if( auto stringVectorPlug = IECore::runTimeCast<const StringVectorDataPlug >( other ) )
{
setValue( stringVectorPlug->getValue() );
}
else if( auto stringPlug = IECore::runTimeCast<const StringPlug >( other ) )
{
IECore::StringVectorDataPtr value = new IECore::StringVectorData;
std::string s = stringPlug->getValue();
if( !s.empty() )
{
boost::split( value->writable(), s, boost::is_any_of( " " ) );
}
setValue( value );
}
else
{
throw IECore::Exception( "Unsupported plug type" );
}
}

// explicit instantiation
template class TypedObjectPlug<IECore::Object>;
template class TypedObjectPlug<IECore::BoolVectorData>;
Expand Down

0 comments on commit 07db256

Please sign in to comment.