Skip to content

Commit

Permalink
Shader : Support optional parameters via OptionalValuePlug
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhaddon committed Jul 18, 2023
1 parent bad4cb7 commit 2f5ba2a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ API
---

- OptionalValuePlug : Added a new plug type that pairs an `enabled` BoolPlug with a `value` ValuePlug.
- Shader : Added support for using OptionalValuePlug to represent optional parameters.

1.3.0.0 (relative to 1.2.10.0)
=======
Expand Down
22 changes: 22 additions & 0 deletions python/GafferSceneTest/ShaderTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,5 +429,27 @@ def testSpline( self ) :
with self.assertRaisesRegex( RuntimeError, "n1.__outAttributes : Cannot support monotone cubic interpolation for splines with inputs, for plug n1.parameters.spline" ):
network = n1.attributes()["test:surface"]

def testOptionalParameter( self ) :

node = GafferSceneTest.TestShader( "n1" )
node["type"].setValue( "test:surface" )

shader = node.attributes()["test:surface"].outputShader()
self.assertNotIn( "optionalString", shader.parameters )

node["parameters"]["optionalString"]["enabled"].setValue( True )
shader = node.attributes()["test:surface"].outputShader()
self.assertIn( "optionalString", shader.parameters )
self.assertEqual( shader.parameters["optionalString"], IECore.StringData() )

node["parameters"]["optionalString"]["value"].setValue( "test" )
shader = node.attributes()["test:surface"].outputShader()
self.assertIn( "optionalString", shader.parameters )
self.assertEqual( shader.parameters["optionalString"], IECore.StringData( "test" ) )

node["parameters"]["optionalString"]["enabled"].setValue( False )
shader = node.attributes()["test:surface"].outputShader()
self.assertNotIn( "optionalString", shader.parameters )

if __name__ == "__main__":
unittest.main()
26 changes: 22 additions & 4 deletions src/GafferScene/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "Gaffer/PlugAlgo.h"
#include "Gaffer/Metadata.h"
#include "Gaffer/NumericPlug.h"
#include "Gaffer/OptionalValuePlug.h"
#include "Gaffer/ScriptNode.h"
#include "Gaffer/StringPlug.h"
#include "Gaffer/SplinePlug.h"
Expand Down Expand Up @@ -1024,10 +1025,16 @@ void Shader::compute( Gaffer::ValuePlug *output, const Gaffer::Context *context

void Shader::parameterHash( const Gaffer::Plug *parameterPlug, IECore::MurmurHash &h ) const
{
const ValuePlug *vplug = IECore::runTimeCast<const ValuePlug>( parameterPlug );
if( vplug )
if( auto optionalValuePlug = IECore::runTimeCast<const OptionalValuePlug>( parameterPlug ) )
{
vplug->hash( h );
if( optionalValuePlug->enabledPlug()->getValue() )
{
optionalValuePlug->valuePlug()->hash( h );
}
}
else if( auto valuePlug = IECore::runTimeCast<const ValuePlug>( parameterPlug ) )
{
valuePlug->hash( h );
}
else
{
Expand All @@ -1037,7 +1044,18 @@ void Shader::parameterHash( const Gaffer::Plug *parameterPlug, IECore::MurmurHas

IECore::DataPtr Shader::parameterValue( const Gaffer::Plug *parameterPlug ) const
{
if( const Gaffer::ValuePlug *valuePlug = IECore::runTimeCast<const Gaffer::ValuePlug>( parameterPlug ) )
if( auto optionalValuePlug = IECore::runTimeCast<const OptionalValuePlug>( parameterPlug ) )
{
if( optionalValuePlug->enabledPlug()->getValue() )
{
return Gaffer::PlugAlgo::getValueAsData( optionalValuePlug->valuePlug() );
}
else
{
return nullptr;
}
}
else if( auto valuePlug = IECore::runTimeCast<const Gaffer::ValuePlug>( parameterPlug ) )
{
return Gaffer::PlugAlgo::getValueAsData( valuePlug );
}
Expand Down
2 changes: 2 additions & 0 deletions src/GafferSceneTest/TestShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "GafferSceneTest/TestShader.h"

#include "Gaffer/CompoundNumericPlug.h"
#include "Gaffer/OptionalValuePlug.h"
#include "Gaffer/StringPlug.h"
#include "Gaffer/SplinePlug.h"

Expand All @@ -59,6 +60,7 @@ TestShader::TestShader( const std::string &name )
parametersPlug()->addChild( new IntPlug( "i" ) );
parametersPlug()->addChild( new Color3fPlug( "c" ) );
parametersPlug()->addChild( new SplinefColor3fPlug( "spline" ) );
parametersPlug()->addChild( new OptionalValuePlug( "optionalString", new Gaffer::StringPlug() ) );
}

TestShader::~TestShader()
Expand Down

0 comments on commit 2f5ba2a

Please sign in to comment.