<s> import functools <EOL> import IECore <EOL> import Gaffer <EOL> import GafferUI <EOL> class PresetsPlugValueWidget ( GafferUI . PlugValueWidget ) : <EOL> def __init__ ( self , plug , parenting = None ) : <EOL> self . __menuButton = GafferUI . MenuButton ( "<STR_LIT>" , menu = GafferUI . Menu ( Gaffer . WeakMethod ( self . __menuDefinition ) ) ) <EOL> GafferUI . PlugValueWidget . __init__ ( self , self . __menuButton , plug , parenting = parenting ) <EOL> self . __plugMetadataChangedConnection = Gaffer . Metadata . plugValueChangedSignal ( ) . connect ( Gaffer . WeakMethod ( self . __plugMetadataChanged ) ) <EOL> self . _addPopupMenu ( self . __menuButton ) <EOL> self . _updateFromPlug ( ) <EOL> def _updateFromPlug ( self ) : <EOL> self . __menuButton . setEnabled ( self . _editable ( ) ) <EOL> text = "<STR_LIT>" <EOL> if self . getPlug ( ) is not None : <EOL> with self . getContext ( ) : <EOL> text = Gaffer . NodeAlgo . currentPreset ( self . getPlug ( ) ) or "<STR_LIT>" <EOL> self . __menuButton . setText ( text ) <EOL> def __menuDefinition ( self ) : <EOL> result = IECore . MenuDefinition ( ) <EOL> if self . getPlug ( ) is None : <EOL> return result <EOL> currentPreset = Gaffer . NodeAlgo . currentPreset ( self . getPlug ( ) ) <EOL> for n in Gaffer . NodeAlgo . presets ( self . getPlug ( ) ) : <EOL> result . append ( <EOL> "<STR_LIT:/>" + n , <EOL> { <EOL> "<STR_LIT>" : functools . partial ( Gaffer . WeakMethod ( self . __applyPreset ) , preset = n ) , <EOL> "<STR_LIT>" : n == currentPreset , <EOL> } <EOL> ) <EOL> return result <EOL> def __applyPreset ( self , unused , preset ) : <EOL> with Gaffer . UndoContext ( self . getPlug ( ) . ancestor ( Gaffer . ScriptNode ) ) : <EOL> Gaffer . NodeAlgo . applyPreset ( self . getPlug ( ) , preset ) <EOL> def __plugMetadataChanged ( self , nodeTypeId , plugPath , key , plug ) : <EOL> if self . getPlug ( ) is None : <EOL> return <EOL> if plug is not None and not plug . isSame ( self . getPlug ( ) ) : <EOL> return <EOL> if not self . getPlug ( ) . node ( ) . isInstanceOf ( nodeTypeId ) : <EOL> return <EOL> if key . startswith ( "<STR_LIT>" ) : <EOL> self . _updateFromPlug ( ) </s>
