-
Notifications
You must be signed in to change notification settings - Fork 207
/
RampPlugValueWidget.py
223 lines (179 loc) · 8.76 KB
/
RampPlugValueWidget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
##########################################################################
#
# Copyright (c) 2013, Image Engine Design Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided with
# the distribution.
#
# * Neither the name of John Haddon nor the names of
# any other contributors to this software may be used to endorse or
# promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##########################################################################
import Gaffer
import GafferUI
import imath
import IECore
from Qt import QtCore
from Qt import QtGui
class RampPlugValueWidget( GafferUI.PlugValueWidget ) :
def __init__( self, plug, **kw ) :
column = GafferUI.ListContainer( GafferUI.ListContainer.Orientation.Vertical, spacing = 4 )
GafferUI.PlugValueWidget.__init__( self, column, plug, **kw )
with column :
with GafferUI.ListContainer( GafferUI.ListContainer.Orientation.Horizontal, spacing = 4 ) :
GafferUI.Label( "Display Mode" )
drawModeWidget = GafferUI.MultiSelectionMenu( allowMultipleSelection = False, allowEmptySelection = False )
drawModeWidget.append( "Ramp" )
drawModeWidget.append( "Curves" )
drawModeWidget.setSelection( "Ramp" )
drawModeWidget.selectionChangedSignal().connect( Gaffer.WeakMethod( self.__drawModeChanged ), scoped = False )
GafferUI.Spacer( imath.V2i( 0 ), parenting = { "expand" : True } )
# TODO: Since we don't have a good way to register metadata on child plugs, we just write the
# metadata on this child plug right before constructing a widget for it. There should probably
# be some way to do this genericly during initialization
Gaffer.Metadata.registerValue( plug['interpolation'],
"plugValueWidget:type", "GafferUI.PresetsPlugValueWidget", persistent=False )
for name, value in sorted( Gaffer.SplineDefinitionInterpolation.names.items() ):
Gaffer.Metadata.registerValue( plug['interpolation'], "preset:" + name, value, persistent=False )
GafferUI.PlugWidget( GafferUI.PlugValueWidget.create( plug["interpolation"] ) )
self.__splineWidget = GafferUI.SplineWidget()
if isinstance( plug, ( Gaffer.SplinefColor3fPlug, Gaffer.SplinefColor4fPlug ) ) :
self.__splineWidget.setDrawMode( self.__splineWidget.DrawMode.Ramp )
else:
self.__splineWidget.setDrawMode( self.__splineWidget.DrawMode.Splines )
self.__splineWidget._qtWidget().setMinimumHeight( 50 )
self.__slider = GafferUI.Slider()
self.__slider.setSizeEditable( True )
self.__slider.setMinimumSize( 2 )
self.__positionsChangedConnection = self.__slider.positionChangedSignal().connect( Gaffer.WeakMethod( self.__positionsChanged ), scoped = False )
self.__slider.indexRemovedSignal().connect( Gaffer.WeakMethod( self.__indexRemoved ), scoped = False )
self.__slider.selectedIndexChangedSignal().connect( Gaffer.WeakMethod( self.__selectedIndexChanged ), scoped = False )
self.__lastPositionChangedReason = None
self.__positionsMergeGroupId = 0
with GafferUI.ListContainer( GafferUI.ListContainer.Orientation.Horizontal, spacing = 4 ) :
self.__positionLabel = GafferUI.LabelPlugValueWidget( plug.pointXPlug( 0 ) )
self.__positionField = GafferUI.NumericPlugValueWidget( plug.pointXPlug( 0 ) )
self.__valueLabel = GafferUI.LabelPlugValueWidget( plug.pointYPlug( 0 ) )
if isinstance( plug.pointYPlug( 0 ), Gaffer.FloatPlug ):
self.__valueField = GafferUI.NumericPlugValueWidget( plug.pointYPlug( 0 ) )
else:
self.__valueField = GafferUI.ColorPlugValueWidget( plug.pointYPlug( 0 ) )
self.setPlug( plug )
def __drawModeChanged( self, drawModeWidget ) :
name = drawModeWidget.getSelection()[0]
self.__splineWidget.setDrawMode( self.__splineWidget.DrawMode.Ramp if name == "Ramp" else self.__splineWidget.DrawMode.Splines )
def setPlug( self, plug ) :
GafferUI.PlugValueWidget.setPlug( self, plug )
self.__slider.setSelectedIndex( 0 )
def getToolTip( self ) :
result = GafferUI.PlugValueWidget.getToolTip( self )
if self.getPlug() is not None :
if result :
result += "\n"
result += "## Actions\n\n"
result += "- Click empty space in slider to add handle\n"
result += "- Click handle to select\n"
result += "- Delete to remove selected handle\n"
result += "- Cursor left/right to nudge selected handle\n"
return result
def _updateFromPlug( self ) :
plug = self.getPlug()
with self.getContext() :
self.__splineWidget.setSpline( plug.getValue().spline() )
positions = []
for i in range( 0, plug.numPoints() ) :
positions.append( plug.pointXPlug( i ).getValue() )
with Gaffer.BlockedConnection( self.__positionsChangedConnection ) :
self.__slider.setPositions( positions )
def __positionsChanged( self, slider, reason ) :
if not slider.changesShouldBeMerged( self.__lastPositionChangedReason, reason ) :
self.__positionsMergeGroupId += 1
self.__lastPositionChangedReason = reason
rejected = False
plug = self.getPlug()
with Gaffer.UndoScope(
plug.ancestor( Gaffer.ScriptNode ),
mergeGroup = "RampPlugValudWidget%d%d" % ( id( self, ), self.__positionsMergeGroupId )
) :
if len( slider.getPositions() ) == plug.numPoints() :
# the user has moved an existing point on the slider
for index, position in enumerate( slider.getPositions() ) :
if plug.pointXPlug( index ).getValue() != position :
curPlug = plug.pointXPlug( index )
if curPlug.settable() and not Gaffer.MetadataAlgo.readOnly( curPlug ):
curPlug.setValue( position )
else:
rejected = True
else :
# a new position was added on the end by the user clicking
# on an empty area of the slider.
numPoints = plug.numPoints()
assert( len( slider.getPositions() ) == numPoints + 1 )
spline = plug.getValue().spline()
position = slider.getPositions()[numPoints]
if not ( plug.getInput() or plug.direction() == Gaffer.Plug.Direction.Out
or Gaffer.MetadataAlgo.readOnly( plug )
):
plug.addPoint()
plug.pointXPlug( numPoints ).setValue( position )
plug.pointYPlug( numPoints ).setValue( spline( position ) )
else:
rejected = True
if rejected:
self._updateFromPlug()
def __indexRemoved( self, slider, index ) :
plug = self.getPlug()
rejected = False
with Gaffer.UndoScope( plug.ancestor( Gaffer.ScriptNode ) ) :
if not ( plug.getInput() or plug.direction() == Gaffer.Plug.Direction.Out
or Gaffer.MetadataAlgo.readOnly( plug )
):
self.getPlug().removePoint( index )
else:
rejected = True
if rejected:
self._updateFromPlug()
def __selectedIndexChanged( self, slider ) :
index = slider.getSelectedIndex()
if self.getPlug() is not None and index is not None :
pointPlug = self.getPlug().pointPlug( index )
self.__positionLabel.setPlug( pointPlug["x"] )
self.__positionField.setPlug( pointPlug["x"] )
self.__valueLabel.setPlug( pointPlug["y"] )
self.__valueField.setPlug( pointPlug["y"] )
else :
self.__positionLabel.setPlug( None )
self.__positionField.setPlug( None )
self.__valueLabel.setPlug( None )
self.__valueField.setPlug( None )
self.__positionLabel.label().setText( "Position" )
self.__valueLabel.label().setText( "Value" )
# we don't register this automatically for any plugs, as it takes up a lot of room
# in the node editor. this means the SplinePlugValueWidget will be used instead, and
# that will launch a dialogue containing a RampPlugValueWidget when appropriate. for
# nodes which want a large editor directly in the node editor, the RampPlugValueWidget
# can be registered directly for specific plugs.