-
Notifications
You must be signed in to change notification settings - Fork 24
/
trackerToRoto.py
215 lines (149 loc) · 6.79 KB
/
trackerToRoto.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
#This Source Code Form is subject to the terms of the Mozilla Public
#License, v. 2.0. If a copy of the MPL was not distributed with this
#file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#Created by Fabrice Fernandez on 26/06/2019.
import os
import string
from NatronEngine import*
from NatronGui import *
from PySide.QtGui import *
import NatronEngine
# TRACKER TO ROTO #
def trackerToRoto():
app = natron.getGuiInstance(0)
dialog = app.createModalDialog()
# set dialog title #
dialog.setWindowTitle("Tracker to roto")
# set dialog margins #
dialog.setContentsMargins(0, 0, 10, 10)
# UI creation #
line01 = dialog.createStringParam("sep01","")
line01.setType(NatronEngine.StringParam.TypeEnum.eStringTypeLabel)
line02 = dialog.createStringParam("sep02","")
line02.setType(NatronEngine.StringParam.TypeEnum.eStringTypeLabel)
# pulldown menu creation #
trackChoice = dialog.createChoiceParam("tracksOption", "Tracks : ")
entries = [ ("All", ""),("Selected", "")]
trackChoice.setOptions(entries)
trackChoice.setDefaultValue("All")
trackChoice.restoreDefaultValue()
dialog.refreshUserParamsGUI()
if dialog.exec_():
currentNode = app.getSelectedNodes()
# check if more than one node is selected #
if len(currentNode) <1 :
warning = natron.warningDialog("Warning","Select a node.")
else:
for n in currentNode :
myID = n.getPluginID()
# check if the selected node is a Tracker #
if myID != "fr.inria.built-in.Tracker" :
warning = natron.warningDialog("Warning","Select a Tracker.")
break
else:
# get 'Tracker' context, that holds all informations about tracks #
whichTracks = n.getTrackerContext()
# if choice is set to 'Selected', then use selected tracks #
if trackChoice.get() == 1:
myTracks = whichTracks.getSelectedTracks()
# if less than 3 tracks is selected, display warning message #
if len(myTracks) < 3:
warning = natron.warningDialog("Warning","Select at least 3 tracks.")
break
# if choice is set to 'All', then use all tracks #
else :
myTracks = whichTracks.getAllTracks()
# if less than 3 tracks is selected, display warning message #
if len(myTracks) < 3:
warning = natron.warningDialog("Warning","There must be at least 3 tracks.")
numTracks = str(len(myTracks))
print (numTracks + ' tracks used.' + '\n')
for currentTrack in myTracks:
xPos = currentTrack.getParam('centerPoint').getNumKeys(0)
print (str(xPos) + ' keyframes')
# ------------------------ #
# 'ROTO' NODE CREATION #
# ------------------------ #
# get 'Tracker' name #
myTrackerName = n.getLabel()
# get 'Tracker' position #
myTrackerPosition = n.getPosition()
# create a 'Roto' node #
myRoto = app.createNode("fr.inria.built-in.Roto")
# 'Roto' node name #
myRoto.setLabel(myTrackerName + '_To_Roto1')
# 'Roto' node position #
myRoto.setPosition(myTrackerPosition[0] + 200 ,myTrackerPosition[1] )
# get roto context #
rotoContext = myRoto.getRotoContext()
Layer1_layer = rotoContext.getBaseLayer()
# ---------------------------- #
# 'BEZIER SPLINE' CREATION #
# ---------------------------- #
# create one point bezier curve at frame 1 #
newBezier = rotoContext.createBezier(0.0,0.0,1)
newBezier.setScriptName("Roto_from_track1")
newBezier.setLabel("Roto_from_track1")
newBezier.setLocked(False)
newBezier.setVisible(True)
# add created bezier to base layer #
Layer1_layer.addItem(newBezier)
# creating other points default position (could be anything) #
# setup point counter #
pointCounter = 1
atCreationXPointPosition = 0
atCreationYPointPosition = 100
for pointCounter in range(1,int(numTracks)):
if pointCounter == numTracks:
newBezier.addControlPoint(atCreationYPointPosition,0)
else:
newBezier.addControlPoint(atCreationXPointPosition,atCreationYPointPosition)
atCreationXPointPosition += 100
atCreationYPointPosition += 100
pointCounter += 1
# closing the generated bezier #
newBezier.setCurveFinished(1)
# ----------- KEYFRAME GENERATION IF 'LINK' IS UNCHECKED --------- #
pointIndex = 0
# loop every track #
for currentTrack in myTracks:
# -------------------------------------------------------#
# get the number of keyframes for the X and Y dimensions #
# -------------------------------------------------------#
# get centerPoint x keyframes (same as centerPoint y) #
nXKeys = currentTrack.getParam("centerPoint").getNumKeys(0)
myTrackName = currentTrack.getScriptName()
# LOOPING EVERY FOUND KEYFRAME #
keyCounter = 0
print ('point ' + str(pointIndex) + ' :')
print ('---------------------------------------------------------------')
while keyCounter < (nXKeys):
# getKeyTime returns a tuple with a boolean value indicating if it succeeded and the keyframe time #
# returns x keyframe exact time in timeline #
gotXKeyTuple = currentTrack.getParam("centerPoint").getKeyTime(keyCounter, 0)
# 'xFrame' stores keyframe time found in timeline, second term in the tupple #
xFrame = gotXKeyTuple[1]
# returns y keyframe exact time in timeline #
gotYKeyTuple = currentTrack.getParam("centerPoint").getKeyTime(keyCounter, 1)
# 'yFrame' stores keyframe time found in timeline, second term in the tupple #
yFrame = gotYKeyTuple[1]
# get the X keyframe value at 'frame' time #
xValue = currentTrack.getParam("centerPoint").getValueAtTime(xFrame, 0)
# get the Y keyframe value at 'frame' time #
yValue = currentTrack.getParam("centerPoint").getValueAtTime(yFrame, 1)
# -------------------------------------------------------#
# set all bezier curve points position across time #
# -------------------------------------------------------#
# LOOPING EVERY BEZIER POINT #
pointsNumber = newBezier.getNumControlPoints()
newBezier.setPointAtIndex(pointIndex, xFrame, xValue, yValue, xValue, yValue, xValue, yValue)
newBezier.setFeatherPointAtIndex(pointIndex, xFrame, xValue, yValue, xValue, yValue, xValue, yValue)
print ('x = ' + str(xValue) + ' y = ' + str(yValue) + ' at frame ' + str(int(xFrame)) )
#print '\n'
#print ('set point ' + str(pointIndex) + ' y value at ' + str(yValue) + ' at frame ' + str(int(yFrame)) )
keyCounter += 1
print ('---------------------------------------------------------------')
print ('\n')
#print '---------------------------------------------------------------'
#print '***************************************************************'
pointIndex += 1