Skip to content

Commit 366c924

Browse files
ShaderTweaks : Support localising global shaders
1 parent cb0dea3 commit 366c924

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

Changes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Improvements
66

77
- RenderMan : Added dedicated viewport visualisers for RenderMan lights.
88
- LocaliseAttributes : Added support for localising global attributes, controlled by the new `includeGlobalAttributes` plug.
9-
- AttributeTweaks : Added support for localising global attributes when `localise` is enabled.
9+
- AttributeTweaks, ShaderTweaks : Added support for localising global attributes when `localise` is enabled.
1010
- AttributeQuery, ShaderQuery : Added support for querying global attributes when `inherit` is enabled.
1111

1212
API

python/GafferSceneTest/ShaderTweaksTest.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,26 @@ def testLocalise( self ) :
393393
group = GafferScene.Group()
394394
group["in"][0].setInput( plane["out"] )
395395

396+
globalSurface = GafferSceneTest.TestShader( "surface" )
397+
globalSurface["type"].setValue( "surface" )
398+
globalSurface["parameters"]["c"].setValue( imath.Color3f( 4, 5, 6 ) )
399+
globalSurface["parameters"]["i"].setValue( 3 )
400+
401+
globalAttributes = GafferScene.CustomAttributes()
402+
globalAttributes["in"].setInput( group["out"] )
403+
globalAttributes["global"].setValue( True )
404+
globalAttributes["extraAttributes"].setValue( IECore.CompoundObject( { "surface" : globalSurface.attributes()["surface"] } ) )
405+
396406
shader = GafferSceneTest.TestShader( "surface" )
397407
shader["type"].setValue( "surface" )
398408
shader["parameters"]["c"].setValue( imath.Color3f( 1, 2, 3 ) )
409+
shader["parameters"]["i"].setValue( 2 )
399410

400411
groupFilter = GafferScene.PathFilter()
401412
groupFilter["paths"].setValue( IECore.StringVectorData( [ "/group" ] ) )
402413

403414
assignment = GafferScene.ShaderAssignment()
404-
assignment["in"].setInput( group["out"] )
415+
assignment["in"].setInput( globalAttributes["out"] )
405416
assignment["filter"].setInput( groupFilter["out"] )
406417
assignment["shader"].setInput( shader["out"] )
407418

@@ -438,10 +449,21 @@ def testLocalise( self ) :
438449

439450
planeAttr = tweaks["out"].attributes( "/group/plane" )
440451
self.assertTrue( "surface" in planeAttr )
441-
self.assertEqual(
442-
planeAttr["surface"].getShader( "surface" ).parameters["c"].value,
443-
imath.Color3f( 3, 2, 1 )
444-
)
452+
shaderParameters = planeAttr["surface"].getShader( "surface" ).parameters
453+
self.assertEqual( shaderParameters["c"].value, imath.Color3f( 3, 2, 1 ) )
454+
self.assertEqual( shaderParameters["i"].value, 2 )
455+
456+
# Disable the shader assignment on "/group", our tweak should now localise
457+
# the global shader.
458+
assignment["enabled"].setValue( False )
459+
460+
self.assertFalse( "surface" in tweaks["out"].attributes( "/group" ) )
461+
462+
planeAttr = tweaks["out"].attributes( "/group/plane" )
463+
self.assertTrue( "surface" in planeAttr )
464+
shaderParameters = planeAttr["surface"].getShader( "surface" ).parameters
465+
self.assertEqual( shaderParameters["c"].value, imath.Color3f( 3, 2, 1 ) )
466+
self.assertEqual( shaderParameters["i"].value, 3 )
445467

446468
# Test disabling tweak results in no localisation
447469

python/GafferSceneUI/ShaderTweaksUI.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def _shaderAttributes( plugValueWidget, paths, affectedOnly ) :
191191
useFullAttr = node["localise"].getValue()
192192
attributeNamePatterns = node["shader"].getValue() if affectedOnly else "*"
193193
for path in paths :
194-
attributes = node["in"].fullAttributes( path ) if useFullAttr else node["in"].attributes( path )
194+
attributes = node["in"].fullAttributes( path, withGlobalAttributes = True ) if useFullAttr else node["in"].attributes( path )
195195
for name, attribute in attributes.items() :
196196
if not IECore.StringAlgo.matchMultiple( name, attributeNamePatterns ) :
197197
continue

src/GafferScene/ShaderTweaks.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,8 @@ bool ShaderTweaks::affectsProcessedAttributes( const Gaffer::Plug *input ) const
422422
tweaksPlug()->isAncestorOf( input ) ||
423423
input == shaderPlug() ||
424424
input == ignoreMissingPlug() ||
425-
input == localisePlug()
425+
input == localisePlug() ||
426+
( input == inPlug()->globalsPlug() && !localisePlug()->isSetToDefault() )
426427
;
427428
}
428429

@@ -451,7 +452,7 @@ void ShaderTweaks::hashProcessedAttributes( const ScenePath &path, const Gaffer:
451452

452453
if( localisePlug()->getValue() )
453454
{
454-
h.append( inPlug()->fullAttributesHash( path ) );
455+
h.append( inPlug()->fullAttributesHash( path, /* withGlobalAttributes = */ true ) );
455456
}
456457
}
457458
}
@@ -485,7 +486,7 @@ IECore::ConstCompoundObjectPtr ShaderTweaks::computeProcessedAttributes( const S
485486
ConstCompoundObjectPtr fullAttributes;
486487
if( localisePlug()->getValue() )
487488
{
488-
fullAttributes = inPlug()->fullAttributes( path );
489+
fullAttributes = inPlug()->fullAttributes( path, /* withGlobalAttributes = */ true );
489490
source = &fullAttributes->members();
490491
}
491492

0 commit comments

Comments
 (0)