1 import nuke
2 import nukescripts
3
4
5
6
7
8
9 _gLastState = {}
10 _gTrackExpression = ["",""]
11
21
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
31
32
33
34
37 nukescripts.PythonPanel.__init__( self, "Link to Tracker", "uk.co.thefoundry.LinkToTrackPanel" )
38
39
40 global _gTrackExpression
41 _gTrackExpression = ["",""]
42
43
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
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
62
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
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
85
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
94 self._updateEverything()
95
96
101
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
119
120
121 """Enable the track point bools when linking to position; disable otherwise."""
122
123
124
125
126
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
158
161
162
163
170
173
176