Package nukescripts :: Module trackerlinkingdialog
[hide private]
[frames] | no frames]

Source Code for Module nukescripts.trackerlinkingdialog

  1  import nuke 
  2  import nukescripts 
  3   
  4  ############################################################################## 
  5  # A quick-n-dirty storage of "last values" for knobs that we want to appear 
  6  # with the value they were assigned the last time the dialog was open during 
  7  # this GUI session. 
  8   
  9  _gLastState = {} 
 10  _gTrackExpression = ["",""] 
 11   
12 -def _getLastState( knob, defaultValue=None ):
13 """Return the given knob's stored last state value. 14 If none exists, defaultValue is returned. 15 Values are stored in a dict referenced by knob name, so names must be unique!""" 16 global _gLastState 17 if _gLastState.has_key( knob.name() ): 18 return _gLastState[knob.name()] 19 else: 20 return defaultValue
21
22 -def _setLastState( knob ):
23 """Store the knob's current value as the 'last state' for the next time the dialog is opened. 24 Values are stored in a dict referenced by knob name, so names must be unique!""" 25 global _gLastState 26 _gLastState[knob.name()] = knob.value()
27 28 29 ############################################################################## 30 # LinkToTrack Panel 31 # This panel offers the user a selection of Tracker nodes in the project and 32 # returns an expression referencing either the Tracker's 'translate' knob or 33 # computing the average value of the selected track points on the node. 34
35 -class LinkToTrackPanel( nukescripts.PythonPanel ):
36 - def __init__( self, groupContext ):
37 nukescripts.PythonPanel.__init__( self, "Link to Tracker", "uk.co.thefoundry.LinkToTrackPanel" ) 38 39 # Initialising return variable 40 global _gTrackExpression 41 _gTrackExpression = ["",""] 42 43 # Show a list of trackers in the project. 44 self._trackers = [] 45 with nuke.toNode(groupContext): 46 for n in nuke.allNodes(): 47 if n.linkableKnobs(nuke.KnobType.eXYKnob): 48 self._trackers.append( n.name() ) 49 self._tracker = nuke.Enumeration_Knob( "trackers", "tracker node", self._trackers ) 50 self._tracker.setTooltip( "The Tracker node to link to." ) 51 self._tracker.setValue( _getLastState( self._tracker, self._trackers[0] ) ) 52 self.addKnob( self._tracker ) 53 54 # Choice of linking to the translate on the tracker or to track point positions. 55 self._link_to = nuke.Enumeration_Knob( "link_to", "link to", ["position", "translate", "translate as offset"] ) 56 self._link_to.setValue( _getLastState( self._link_to, 'translate' ) ) 57 self._link_to.setTooltip( "Choose whether to link to the translation computed in the Tracker's Transform tab or to a specific track point position. If multiple track points are chosen they will be averaged." ) 58 self.addKnob( self._link_to ) 59 self._link_to.setFlag( nuke.ENDLINE ) 60 61 # Creating all of the linkable knobs for all trackers in advance, then 62 # going to hide / show them based on the visibility below. 63 self._all_linkables = {} 64 for tracker in self._trackers: 65 n = nuke.toNode(tracker) 66 self._all_linkables[tracker] = [] 67 l = n.linkableKnobs(nuke.KnobType.eXYKnob) 68 for i in range(len(l)): 69 # Removing any translate links as they are serviced by the "link" enum above. 70 if l[i].knob().name() == "translate": 71 continue 72 if len(l[i].displayName()) > 0: 73 knobname = l[i].displayName() 74 exprname = "tracks." + str(i-1) 75 else: 76 knobname = l[i].knob().name() 77 exprname = knobname 78 point = nuke.Boolean_Knob( exprname, knobname ) 79 point.setTooltip( "Use this track point when linking to 'position'." ) 80 point.setVisible(False) 81 self.addKnob( point ) 82 self._all_linkables[tracker].append(point) 83 84 # Show the expression that's going to set in case user wants to tweak or 85 # copy it before closing the dialog. 86 self._expressionX = nuke.EvalString_Knob( "expression.x" ) 87 self._expressionX.setTooltip( "This is the expression that will be applied to the x position. You can edit it before closing the dialog but changing settings on this panel will rebuild it, losing your changes." ) 88 self.addKnob( self._expressionX ) 89 self._expressionY = nuke.EvalString_Knob( "expression.y" ) 90 self._expressionY.setTooltip( "This is the expression that will be applied to the y position. You can edit it before closing the dialog but changing settings on this panel will rebuild it, losing your changes." ) 91 self.addKnob( self._expressionY ) 92 93 # Set up knobs and linkable objects 94 self._updateEverything()
95 96
97 - def _updateEverything(self):
98 self._updateLinkableKnobInfo() 99 100 self._updateExpression()
101
102 - def _updateLinkableKnobInfo(self):
103 use_points = ( self._link_to.value() == 'position' ) 104 105 for tracker in self._trackers: 106 for point in self._all_linkables[tracker]: 107 point.setEnabled( use_points ) 108 point.setVisible(False) 109 110 firstTrack = 1 111 self._track_points = [] 112 for point in self._all_linkables[self._tracker.value()]: 113 point.setValue( _getLastState( point, (firstTrack==1) ) ) 114 point.setVisible(True) 115 self._track_points.append(point) 116 firstTrack = 0
117
118 - def _updatePointsEnabled(self):
119 # Display check boxes for selection of which track points should be linked. 120 # If more than one is selected the expression is set to average them. 121 """Enable the track point bools when linking to position; disable otherwise."""
122 #use_points = ( self._link_to.value() == 'position' ) 123 #for point in self._track_points: 124 # point.setEnabled( use_points ) 125 126
127 - def _updateExpression(self):
128 """Update the expression to reflect the current settings.""" 129 if self._link_to.value() == 'translate': 130 self._expressionX.setValue( self._tracker.value() + ".translate.x" ) 131 self._expressionY.setValue( self._tracker.value() + ".translate.y" ) 132 elif self._link_to.value() == 'translate as offset': 133 self._expressionX.setValue( "curve + " + self._tracker.value() + ".translate.x" ) 134 self._expressionY.setValue( "curve + " + self._tracker.value() + ".translate.y" ) 135 else: 136 pointCount = 0 137 exprX = " + ".join([self._tracker.value() + "." + point.name() + ".track_x" for point in self._track_points if point.value()]) 138 exprY = " + ".join([self._tracker.value() + "." + point.name() + ".track_y" for point in self._track_points if point.value()]) 139 pointCount = sum( (1 for point in self._track_points if point.value()) ) 140 if pointCount > 1: 141 exprX = "(" + exprX + ")/" + str(pointCount) 142 exprY = "(" + exprY + ")/" + str(pointCount) 143 self._expressionX.setValue( exprX ) 144 self._expressionY.setValue( exprY )
145
146 - def knobChanged( self, knob ):
147 _setLastState( knob ) 148 149 # If expression changes, don't try to update and possibly trash it. 150 if knob == self._expressionX or knob == self._expressionY: 151 return 152 153 self._updateExpression() 154 if knob == self._link_to or knob == self._tracker: 155 global _gLastTracker 156 _gLastTracker = knob.value() 157 self._updateEverything()
158
159 - def addToPane( self ):
161 162 163
164 -def trackerlinkingdialog(groupContext):
165 d = LinkToTrackPanel(groupContext) 166 if d.showModalDialog() == True: 167 global _gTrackExpression 168 _gTrackExpression = [d._expressionX.value(), d._expressionY.value()] 169 return [d._expressionX.value(), d._expressionY.value()]
170
171 -def trackerlinkingdialogexpressionx():
172 return _gTrackExpression[0]
173
174 -def trackerlinkingdialogexpressiony():
175 return _gTrackExpression[1]
176